📄 list.c
字号:
/*--------------------------------------------------------------------------*//* Toolbox List function example *//*--------------------------------------------------------------------------*/#define HELP_MSG \"This example shows how to use the list functions in the Toolbox instrument\n\driver. The Toolbox instrument can create a list which holds items of any\n\specified size. Some of the list functions are:\n\\n\ ListType ListCreate (int Item_Size);\n\\n\ int ListNumItems (ListType List);\n\\n\ int ListInsertItem (ListType List, void *Pointer_To_Item,\n\ int Position_To_Insert_At);\n\\n\ void ListRemoveItem (ListType List, void *Item_Destination,\n\ int Position_Of_Item_To_Remove);\n\\n\ void ListGetItem (ListType List, void *Item_Destination,\n\ int Position_Of_Item_To_Get);\n\\n\ void * CVIFUNC ListGetPtrToItem(ListType list, int itemPosition);\n\\n\ void ListDispose (int Item_Size);\n\\n\\n\This example uses a structure containing 3 members as a list item and allows\n\you to display, add, and remove items from the list."/*---------------------------------------------------------------------------*//* Includes *//*---------------------------------------------------------------------------*/#include <utility.h>#include <userint.h>#include "toolbox.h"#include "list.h"/*---------------------------------------------------------------------------*//* Defines *//*---------------------------------------------------------------------------*/#define SIZEOF_STRING_MEMBER 256/*---------------------------------------------------------------------------*//* Variables *//*---------------------------------------------------------------------------*/static int panelHandle;static ListType myList = 0;/* Create an arbitrary structure to use in list*/typedef struct MyStructRec { int integerValue; double doubleValue; char* stringValue;} tMyStruct; tMyStruct *pMyStruct;/*---------------------------------------------------------------------------*//* Prototypes *//*---------------------------------------------------------------------------*/int CreateMyList(void);int DestroyMyList(void);int DimControls(void);int UpdateUIR(void); /*---------------------------------------------------------------------------*//* Main *//*---------------------------------------------------------------------------*/int main (int argc, char *argv[]){ if (InitCVIRTE (0, argv, 0) == 0) /* Initialize CVI libraries */ return -1; /* out of memory */ if ((panelHandle = LoadPanel (0, "list.uir", PANEL)) < 0) return -1; /*Create list and generate default items in List */ if (!CreateMyList()) goto Leave; DimControls(); DisplayPanel (panelHandle); RunUserInterface (); Leave: /* Destroy list */ DestroyMyList(); return 0; }/*---------------------------------------------------------------------------*//* CreateMyList *//*---------------------------------------------------------------------------*/int CreateMyList(void){ tMyStruct tempItem; int item; /* Create a list and specify the size of each item in the list */ myList = ListCreate (sizeof(tMyStruct)); if (!myList) { MessagePopup("ListCreate", "Error allocating memory for list"); goto Leave; } /* Fill list with 10 values */ 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 Leave; sprintf(tempItem.stringValue, "String Entry %d", item); /* Add each element of the structure to the list */ if (!ListInsertItem (myList, &tempItem, END_OF_LIST)) { MessagePopup("","Error Inserting Item"); goto Leave; } } /* Update UIR */ SetCtrlVal(panelHandle, PANEL_DISPLAYITEM, 1); UpdateUIR(); return 1; Leave: return 0; }/*---------------------------------------------------------------------------*//* DestroyMyList *//*---------------------------------------------------------------------------*/int DestroyMyList(void){ int error; int numItems; int item; tMyStruct *tempItem; /* Get the number of items in list */ numItems = ListNumItems (myList); /* Access each item and free the malloc'd memory string and remove from list */ for (item=numItems; item>0; item--) { tempItem = ListGetPtrToItem (myList, item); if (tempItem) { if (tempItem->stringValue) free (tempItem->stringValue); ListRemoveItem (myList, 0, item); } } ListDispose (myList); myList = 0; return 1;}/*---------------------------------------------------------------------------*//* DimControls *//*---------------------------------------------------------------------------*/int DimControls(void){ int numItems; if (!myList) return 0; /* Create a list and specify the size of each item in the list */ numItems = ListNumItems (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;}/*---------------------------------------------------------------------------*//* UpdateUIR *//*---------------------------------------------------------------------------*/int UpdateUIR(void) { int numItems; int currentItem; tMyStruct *tempItem; if (!myList) numItems = 0; else numItems = ListNumItems (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, ¤tItem); if (currentItem>0) { tempItem = ListGetPtrToItem (myList, currentItem); if (tempItem) { SetCtrlVal(panelHandle, PANEL_ITEMINTEGER, tempItem->integerValue); SetCtrlVal(panelHandle, PANEL_ITEMDOUBLE, tempItem->doubleValue); SetCtrlVal(panelHandle, PANEL_ITEMSTRING, tempItem->stringValue); } } DimControls(); return 1;}/*---------------------------------------------------------------------------*//* DeleteItemCallback *//*---------------------------------------------------------------------------*/int CVICALLBACK DeleteItemCallback (int panel, int control, int event, void *callbackData, int eventData1, int eventData2){ tMyStruct *tempItem; int currentItem; switch (event) { case EVENT_COMMIT: if (!myList) return 0; GetCtrlVal(panel, PANEL_DISPLAYITEM, ¤tItem); tempItem = ListGetPtrToItem (myList, currentItem); if (tempItem) { if (tempItem->stringValue) free (tempItem->stringValue); ListRemoveItem (myList, 0, currentItem); /* Update the UIR */ SetCtrlVal(panel, PANEL_DISPLAYITEM, currentItem-1); UpdateUIR(); } break; } return 0;}/*---------------------------------------------------------------------------*//* NewItemCallback *//*---------------------------------------------------------------------------*/int CVICALLBACK NewItemCallback (int panel, int control, int event, void *callbackData, int eventData1, int eventData2){ tMyStruct tempItem; int currentItem; switch (event) { case EVENT_COMMIT: if (!myList) return 0; GetCtrlVal(panel, PANEL_DISPLAYITEM, ¤tItem); tempItem.integerValue = 0; tempItem.doubleValue = 0.0; tempItem.stringValue = calloc(SIZEOF_STRING_MEMBER, 1); if (!tempItem.stringValue) goto Leave; sprintf(tempItem.stringValue, ""); /* Add each element of the structure to the list */ if (!ListInsertItem (myList, &tempItem, currentItem+1)) goto Leave; /* Update the UIR */ SetCtrlVal(panel, PANEL_DISPLAYITEM, currentItem+1); UpdateUIR(); break; } return 0; Leave: MessagePopup("Add New Item","Error Inserting Item"); if (!tempItem.stringValue) free (tempItem.stringValue); return 0;}/*---------------------------------------------------------------------------*//* ChangeItemCallback *//*---------------------------------------------------------------------------*/int CVICALLBACK ChangeItemCallback (int panel, int control, int event, void *callbackData, int eventData1, int eventData2){ switch (event) { case EVENT_COMMIT: if (myList) UpdateUIR(); break; } return 0;}/*---------------------------------------------------------------------------*//* ChangeItemValueCallback *//*---------------------------------------------------------------------------*/int CVICALLBACK ChangeItemValueCallback (int panel, int control, int event, void *callbackData, int eventData1, int eventData2){ tMyStruct *tempItem; int currentItem; switch (event) { case EVENT_COMMIT: if (!myList) return 0; GetCtrlVal(panel, PANEL_DISPLAYITEM, ¤tItem); tempItem = ListGetPtrToItem (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;}/*---------------------------------------------------------------------------*//* QuitCallback *//*---------------------------------------------------------------------------*/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;}/*---------------------------------------------------------------------------*//* HelpCallback *//*---------------------------------------------------------------------------*/int CVICALLBACK HelpCallback (int panel, int control, int event, void *callbackData, int eventData1, int eventData2){ switch (event) { case EVENT_RIGHT_CLICK: case EVENT_COMMIT: MessagePopup ("Toolbox List Example Program", HELP_MSG); break; } return 0;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -