⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 list.c

📁 labwindow 编程的toolbox例程。
💻 C
字号:
/*---------------------------------------------------------------------------*/
/*                                                                           */
/* FILE:    list.c                                                           */
/*                                                                           */
/* PURPOSE: This example illustrates how to use the List functions in the    */
/*          Programmer's Toolbox instrument driver to manage a dynamic list  */
/*          of data structures.  This API is a nice alternative to a         */
/*          traditional linked-list.  Using the API, you can add and remove  */
/*          items, search, and sort.                                         */
/*                                                                           */
/*          This example stores a structure with three members in a List,    */
/*          and allows you to view the items, add, and remove them.          */
/*                                                                           */
/*---------------------------------------------------------------------------*/

/*---------------------------------------------------------------------------*/
/* Include files                                                             */
/*---------------------------------------------------------------------------*/
#include <utility.h>
#include <userint.h>
#include "toolbox.h"
#include "list.h"

/*---------------------------------------------------------------------------*/
/* Defines                                                                   */
/*---------------------------------------------------------------------------*/
#define SIZEOF_STRING_MEMBER 256

/*---------------------------------------------------------------------------*/
/* Types                                                                     */
/*---------------------------------------------------------------------------*/
typedef struct MyStruct_Tag       
{   
    int    integerValue;
    double doubleValue;
    char*  stringValue;
} MyStruct;

/*---------------------------------------------------------------------------*/
/* Module-globals                                                            */
/*---------------------------------------------------------------------------*/
//static int panelHandle;
static ListType g_myList = 0;

/*---------------------------------------------------------------------------*/
/* Internal function prototypes                                              */
/*---------------------------------------------------------------------------*/
static int CreateMyList  (int panelHandle);
static int DestroyMyList (void);
static int DimControls   (int panelHandle);
static int UpdateUIR     (int panelHandle);
    
/*---------------------------------------------------------------------------*/
/* This is the application's entry-point.                                    */
/*---------------------------------------------------------------------------*/
int main (int argc, char *argv[])
{
    int panelHandle;
    
    if (InitCVIRTE (0, argv, 0) == 0)
        return -1;
    if ((panelHandle = LoadPanel (0, "list.uir", PANEL)) < 0)
        return -1;
        
    /* Create a List and insert some default items */
    if (!CreateMyList(panelHandle))
        goto Leave;
    DimControls (panelHandle);
    DisplayPanel (panelHandle);
    RunUserInterface ();
    
Leave:
    
    /* Free resources and return */
    DestroyMyList ();
    DiscardPanel (panelHandle);
    CloseCVIRTE ();
    return 0;   
}


/*---------------------------------------------------------------------------*/
/* Create a new List and insert some default items.                          */
/*---------------------------------------------------------------------------*/
static int CreateMyList (int panelHandle)
{
    int      item;
    MyStruct tempItem;
    
    /* Create a list and specify the size of each item in the list */
    g_myList = ListCreate (sizeof(MyStruct));
    if (!g_myList)
        {
        MessagePopup("List Demo", "Error allocating memory for list");
        goto Error;
        }   
    
    /* Fill the List with 10 MyStruct items */
    for (item = 1; item <= 10; item++)
        {
        tempItem.integerValue = item;
        tempItem.doubleValue = item*5.0;
        tempItem.stringValue = calloc (SIZEOF_STRING_MEMBER, 1);
        if (!tempItem.stringValue)
            goto Error;
        sprintf (tempItem.stringValue, "String Entry %d", item);
        if (!ListInsertItem (g_myList, &tempItem, END_OF_LIST)) 
            {
            MessagePopup ("List Demo","Error Inserting Item");
            goto Error;
            }
        }
    
    /* Update the GUI with the Lit information */
    SetCtrlVal (panelHandle, PANEL_DISPLAYITEM, 1);
    UpdateUIR (panelHandle);
    return 1;
    
Error:
    return 0;
}

/*---------------------------------------------------------------------------*/
/* Destroy the List and all asociated item resources.                        */
/*---------------------------------------------------------------------------*/
static int DestroyMyList (void)
{
    int      numItems;
    int      item;
    MyStruct *tempItem;
    
    /* Get the number of items in list */
    numItems = ListNumItems (g_myList);
    
    /* Access each item, free the malloc'd memory string, and remove it from */
    /* the List. */
    for (item = numItems; item > 0; item--)
        {
        tempItem = ListGetPtrToItem (g_myList, item);
        if (tempItem) 
            {
            if (tempItem->stringValue)
                free (tempItem->stringValue);
            ListRemoveItem (g_myList, 0, item);
            }    
        }           
    ListDispose (g_myList);
    g_myList = 0;
    return 1;
}

/*---------------------------------------------------------------------------*/
/* Dim UIR controls to reflect currently available options.                  */
/*---------------------------------------------------------------------------*/
static int DimControls (int panelHandle)
{
    int numItems;
    
    if (!g_myList) 
        return 0;    
    numItems = ListNumItems (g_myList);
    SetCtrlAttribute (panelHandle, PANEL_ITEMDOUBLE , ATTR_DIMMED, !numItems);
    SetCtrlAttribute (panelHandle, PANEL_ITEMINTEGER, ATTR_DIMMED, !numItems);
    SetCtrlAttribute (panelHandle, PANEL_ITEMSTRING , ATTR_DIMMED, !numItems);
    SetCtrlAttribute (panelHandle, PANEL_DISPLAYITEM, ATTR_DIMMED, !numItems);
    SetCtrlAttribute (panelHandle, PANEL_DELETEITEM , ATTR_DIMMED, !numItems);
    return 1;
}

/*---------------------------------------------------------------------------*/
/* Update the UIR to display the correct information about the List contents.*/
/*---------------------------------------------------------------------------*/
static int UpdateUIR (int panelHandle) 
{
    int      numItems;
    int      currentItem;
    MyStruct *tempItem;
    
    if (!g_myList) 
        numItems = 0;    
    else 
        numItems = ListNumItems (g_myList);
    SetCtrlVal (panelHandle, PANEL_TOTALITEMS, numItems);
    SetCtrlAttribute (panelHandle, PANEL_DISPLAYITEM, ATTR_MIN_VALUE,
                      (numItems) ? 1 : 0);
    SetCtrlAttribute (panelHandle, PANEL_DISPLAYITEM, ATTR_MAX_VALUE,
                      (numItems) ? numItems : 0);
    GetCtrlVal (panelHandle, PANEL_DISPLAYITEM, &currentItem);
    if (currentItem > 0) 
        {
        tempItem = ListGetPtrToItem (g_myList, currentItem);
        if (tempItem)
            {
            SetCtrlVal (panelHandle, PANEL_ITEMINTEGER,
                        tempItem->integerValue);
            SetCtrlVal (panelHandle, PANEL_ITEMDOUBLE,  tempItem->doubleValue);
            SetCtrlVal (panelHandle, PANEL_ITEMSTRING,  tempItem->stringValue);
            }
        }   
    DimControls (panelHandle);
    return 1;
}

/*---------------------------------------------------------------------------*/
/* Delete an item from the List                                              */
/*---------------------------------------------------------------------------*/
int CVICALLBACK DeleteItemCallback (int panel, int control, int event,
                                    void *callbackData, int eventData1,
                                    int eventData2)
{
    int      currentItem;
    MyStruct *tempItem;
    
    switch (event)
        {
        case EVENT_COMMIT:
            if (!g_myList)
                return 0;
            GetCtrlVal (panel, PANEL_DISPLAYITEM, &currentItem);
            tempItem = ListGetPtrToItem (g_myList, currentItem);
            
            /* If we got a pinter to the item, free its memory and remove it */
            /* from the List                                                 */
            if (tempItem) 
                {
                if (tempItem->stringValue)
                    free (tempItem->stringValue);
                ListRemoveItem (g_myList, 0, currentItem);
                
                /* Update the UIR */                
                SetCtrlVal (panel, PANEL_DISPLAYITEM, currentItem - 1);
                UpdateUIR (panel);
            }    
            break;
    }
    return 0;
}
/*---------------------------------------------------------------------------*/
/* Add a new item to the list, with default values, and make it the currently*/
/* displayed item.                                                           */
/*---------------------------------------------------------------------------*/
int CVICALLBACK NewItemCallback (int panel, int control, int event,
                                 void *callbackData, int eventData1,
                                 int eventData2)
{
    int      currentItem;
    MyStruct tempItem;
    
    switch (event)
        {
        case EVENT_COMMIT:
            if (!g_myList)
                return 0;
            GetCtrlVal (panel, PANEL_DISPLAYITEM, &currentItem);
            tempItem.integerValue = 0;
            tempItem.doubleValue = 0.0;
            tempItem.stringValue = calloc (SIZEOF_STRING_MEMBER, 1);
            if (!tempItem.stringValue)
                goto Error;
            sprintf (tempItem.stringValue, "");

            /* Add the newly created item to the List */
            if (!ListInsertItem (g_myList, &tempItem, currentItem + 1)) 
                goto Error;
                
            /* Update the UIR */                
            UpdateUIR (panel);
            SetCtrlVal (panel, PANEL_DISPLAYITEM, currentItem + 1);
            UpdateUIR (panel);
            break;
        }
    return 0;
    
Error:
    MessagePopup ("List Demo","Error inserting item into List.");
    if (!tempItem.stringValue) 
        free (tempItem.stringValue);
    return 0;
}

/*---------------------------------------------------------------------------*/
/* Change the item currently being displayed.                                */
/*---------------------------------------------------------------------------*/
int CVICALLBACK ChangeItemCallback (int panel, int control, int event,
                                    void *callbackData, int eventData1,
                                    int eventData2)
{
    switch (event)
        {
        case EVENT_VAL_CHANGED:
            if (g_myList)
                UpdateUIR (panel);
            break;
        }
    return 0;
}

/*---------------------------------------------------------------------------*/
/* Respond to a change in the item value controls to update the contents of  */
/* the current item with the new values.                                     */
/*---------------------------------------------------------------------------*/
int CVICALLBACK ChangeItemValueCallback (int panel, int control, int event,
                                         void *callbackData, int eventData1,
                                         int eventData2)
{
    int      currentItem;
    MyStruct *tempItem;
    
    switch (event)
        {
        case EVENT_COMMIT:
            if (!g_myList)
                return 0;
            GetCtrlVal(panel, PANEL_DISPLAYITEM, &currentItem);
            tempItem = ListGetPtrToItem (g_myList, currentItem);
            if (tempItem) 
                {
                if (control == PANEL_ITEMINTEGER) 
                    GetCtrlVal (panel, PANEL_ITEMINTEGER,
                                &tempItem->integerValue);
                else if (control == PANEL_ITEMDOUBLE) 
                    GetCtrlVal (panel, PANEL_ITEMDOUBLE,
                                &tempItem->doubleValue);
                else if (control == PANEL_ITEMSTRING) 
                    GetCtrlVal (panel, PANEL_ITEMSTRING,
                                tempItem->stringValue);
                }    
            
            break;
        }
    return 0;
}

int CVICALLBACK PanelCB (int panel, int event, void *callbackData,
                         int eventData1, int eventData2)
{
    switch (event)
        {
        case EVENT_CLOSE:
            QuitUserInterface (0);
            break;
        }
    return 0;
}

int CVICALLBACK QuitCallback (int panel, int control, int event,
        void *callbackData, int eventData1, int eventData2)
{
    switch (event)
        {
        case EVENT_COMMIT:
            QuitUserInterface (0);
            break;
        }
    return 0;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -