📄 menudemo.c
字号:
/*--------------------------------------------------------------------------*//* Demonstrates use of the menuutil instrument driver *//*--------------------------------------------------------------------------*/#define HELP_MSG \"This example demonstrate the use of the Menu Utility instrument driver to \n\create a list of files in the following menus:\n\\n\ FILE menu for previously opened files\n\ WINDOW menu for currently opened windows\n\\n\In addition the list of files in the FILE menu will be stored in the Windows\n\Registry.\n\\n\Note: The File menu only mimics the opening and closing of files."/*--------------------------------------------------------------------------*//* Includes *//*--------------------------------------------------------------------------*/#include <cvirte.h> /* Initialize CVI libraries */#include <userint.h>#include "menudemo.h"#include "menuutil.h"/*--------------------------------------------------------------------------*//* Defines *//*--------------------------------------------------------------------------*/#ifdef WIN32 #define DEMO_REGISTRY_NAME "Software\\National Instruments\\CVI Sample\\MenuDemo"#else #define DEMO_REGISTRY_NAME "menudemo.ini"#endif/*--------------------------------------------------------------------------*//* Variables *//*--------------------------------------------------------------------------*/static int panelHandle;static int gMenubar;static IniText sIniTextHandle = 0;static menuList sFileMenuListHandle = 0;static menuList sWindowMenuListHandle = 0;static char sMsgBuffer[512];static int numChildPanels = 0;static int topLeftValue = 0;typedef struct tWindowMenuCallbackDataRec{ char fileName[MAX_PATHNAME_LEN]; int panel;} tWindowMenuCallbackData;/*--------------------------------------------------------------------------*//* Prototypes *//*--------------------------------------------------------------------------*/static int DimMenuItems(void);int GetTopChildWindow(int panelHandle);static int SaveOptionsForUIR(void);static int GetOptionsForUIR(void);static int CreateWindowMenuList(void); static int RemoveWindowMenuList(void); static int AddFileNameToWindowMenuList(menuList menuListHandle, char *fullPathAndFilename, int panel);static int AddFileNameToFileMenuList(menuList menuListHandle, char *fullPathAndFilename);static int RemoveFileNameFromMenuList(menuList menuListHandle, char *fullPathAndFilename);static void CVICALLBACK FILEMenuListCallbackFunc (menuList list, int menuIndex, int event, void *callbackData);static void CVICALLBACK WINDOWMenuListCallbackFunc (menuList list, int menuIndex, int event, void *callbackData);/*--------------------------------------------------------------------------*//* 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, "menudemo.uir", PANEL)) < 0) return -1; gMenubar = GetPanelMenuBar (panelHandle); GetOptionsForUIR(); DimMenuItems(); CreateWindowMenuList(); DisplayPanel (panelHandle); RunUserInterface (); RemoveWindowMenuList(); SaveOptionsForUIR(); return 0;}/*--------------------------------------------------------------------------*//* DimMenuItems *//* *//* Routine will dim File and Window menu items depending on whether a file *//* is open */ /*--------------------------------------------------------------------------*/static int DimMenuItems(void) { int fileOpen; SetMenuBarAttribute (panelHandle+1, MAINMENU_FILE_SAVE, ATTR_DIMMED, !numChildPanels); SetMenuBarAttribute (panelHandle+1, MAINMENU_FILE_SAVEAS, ATTR_DIMMED, !numChildPanels); SetMenuBarAttribute (panelHandle+1, MAINMENU_FILE_CLOSE, ATTR_DIMMED, !numChildPanels); SetMenuBarAttribute (panelHandle+1, MAINMENU_WINDOW_CLOSEALL, ATTR_DIMMED, !numChildPanels); return 0; } /*--------------------------------------------------------------------------*//* CreateWindowMenuList - Create menu list in WINDOW menu *//*--------------------------------------------------------------------------*/static int CreateWindowMenuList(void) { /* Create WINDOW menulist if it does not already exist */ if (!sWindowMenuListHandle) sWindowMenuListHandle = MU_CreateMenuList(gMenubar, MAINMENU_WINDOW, MAINMENU_WINDOW_WINDOW_LINE, 5, WINDOWMenuListCallbackFunc); if (sWindowMenuListHandle) { /* Update Attributes */ MU_SetMenuListAttribute(sWindowMenuListHandle, 0, ATTR_MENULIST_APPEND_SHORTCUT, 1); MU_SetMenuListAttribute(sWindowMenuListHandle, 0, ATTR_MENULIST_ALLOW_DUPLICATE_ITEMS, 0); } return 0;}/*--------------------------------------------------------------------------*//* RemoveWindowMenuList - Remove menu list from WINDOW menu *//*--------------------------------------------------------------------------*/static int RemoveWindowMenuList(void) { /* Destroy FILE menulist */ if (sFileMenuListHandle) MU_DeleteMenuList(sWindowMenuListHandle); return 0; }/*--------------------------------------------------------------------------*//* GetOptionsForUIR() *//* *//* Parameters: none *//* Return: success = 1 *//* Purpose: Get saved UIR options and update the UIR *//*--------------------------------------------------------------------------*/static int GetOptionsForUIR(void){ int success = 1; /* Create an INIfile handle */ if (!sIniTextHandle) sIniTextHandle = Ini_New (0); /* Get options and update UIR */ if (sIniTextHandle) { /* Read previous data from registry */ MU_ReadRegistryInfo(sIniTextHandle, DEMO_REGISTRY_NAME); /* Create FILE menulist if it does not already exist */ if (!sFileMenuListHandle) sFileMenuListHandle = MU_CreateMenuList(gMenubar, MAINMENU_FILE, MAINMENU_FILE_ABOVE_EXIT_LINE, 5, FILEMenuListCallbackFunc); if (sFileMenuListHandle) { /* Update Attributes */ MU_SetMenuListAttribute(sFileMenuListHandle, 0, ATTR_MENULIST_APPEND_SHORTCUT, 1); MU_SetMenuListAttribute(sFileMenuListHandle, 0, ATTR_MENULIST_ALLOW_DUPLICATE_ITEMS, 0); /* Update FILE menulist with files from INI file */ MU_GetFileListFromIniFile(sFileMenuListHandle, sIniTextHandle, "FILE MenuList", "Filename", 1); } } else success = 0; return success;} /*--------------------------------------------------------------------------*//* SaveOptionsForUIR() *//* *//* Parameters: none *//* Return: success = 1 *//* Purpose: Save options from current state of UIR *//*--------------------------------------------------------------------------*/static int SaveOptionsForUIR(void){ int success = 1; /* Update options from UIR and Save options */ if (sIniTextHandle) { /* Save FILE menulist filenames to INI file */ if (sFileMenuListHandle) MU_PutFileListInIniFile(sFileMenuListHandle, sIniTextHandle, "FILE MenuList", "Filename", TRUE); /* Destroy FILE menulist */ if (sFileMenuListHandle) MU_DeleteMenuList(sFileMenuListHandle); /* Save options from UIR to INI file */ if (!MU_WriteRegistryInfo(sIniTextHandle, DEMO_REGISTRY_NAME)) success = 0; } else success = 0; /* Discard INI handle */ if (sIniTextHandle) Ini_Dispose (sIniTextHandle); return success;} /*--------------------------------------------------------------------------*//* AddFileNameToFileMenuList() *//* *//* Parameters: sequence ID *//* Return: success = 1 *//* Purpose: Get saved UIR options and update the UIR *//*--------------------------------------------------------------------------*/static int AddFileNameToFileMenuList(menuList sMenuListHandle, char *fullPathAndFilename){ int retVal = 0; if ((sMenuListHandle) && (fullPathAndFilename) && (fullPathAndFilename[0] != '\0') ) { retVal = MU_AddItemToMenuList(sMenuListHandle, FRONT_OF_LIST, MU_MakeShortFileName(NULL, fullPathAndFilename, 32), StrDup(fullPathAndFilename)); } return retVal; } /*--------------------------------------------------------------------------*//* AddFileNameToWindowMenuList() *//* *//* Parameters: sequence ID *//* Return: success = 1 *//* Purpose: Get saved UIR options and update the UIR *//*--------------------------------------------------------------------------*/static int AddFileNameToWindowMenuList(menuList sMenuListHandle, char *fullPathAndFilename, int panel){ int retVal = 0; tWindowMenuCallbackData *callbackData; if ((sMenuListHandle) && (fullPathAndFilename) && (fullPathAndFilename[0] != '\0') ) { if (callbackData = calloc (1,sizeof(tWindowMenuCallbackData))) { strcpy(callbackData->fileName, fullPathAndFilename); callbackData->panel = panel; retVal = MU_AddItemToMenuList(sMenuListHandle, FRONT_OF_LIST, MU_MakeShortFileName(NULL, fullPathAndFilename, 32), callbackData); } } return retVal; } /*--------------------------------------------------------------------------*//* RemoveFileNameFromMenuList() *//* *//* Parameters: sequence ID *//* Return: success = 1 *//* Purpose: Get saved UIR options and update the UIR *//*--------------------------------------------------------------------------*/static int RemoveFileNameFromMenuList(menuList sMenuListHandle, char *fullPathAndFilename){ int retVal = 0; int item = 0; int i; int totalItems = 0; char *fileName; /* Find menu item and remove from list */ if ((sMenuListHandle) && (fullPathAndFilename) && (fullPathAndFilename[0] != '\0') ) { if (totalItems = MU_GetNumMenuListItems (sMenuListHandle)) { for (item=1;item<=totalItems;item++) { fileName = 0; MU_GetMenuListAttribute (sMenuListHandle, item, ATTR_MENULIST_ITEM_CALLBACK_DATA, &fileName); if ((fileName) && (strcmp(fileName,fullPathAndFilename)==0) ) break; } if (item<=totalItems) MU_DeleteMenuListItem (sMenuListHandle, item); } } return retVal; } /*--------------------------------------------------------------------------*//* FILEMenuListCallbackFunc() *//* *//* Parameters: Standard Menu Callback parameters *//* Note: "callbackData" will be pointer to *//* the long filename chosen by user *//* Return: none *//* Purpose: This menu callback function is called *//* when a filename is chosen in the list of *//* files in the FILE menu item *//*--------------------------------------------------------------------------*/static void CVICALLBACK FILEMenuListCallbackFunc (menuList list, int menuIndex, int event, void *callbackData){ int status; int childPanel; char *fileName = (char *) callbackData; if (event == EVENT_DISCARD) { if (fileName) free(fileName); } else if (fileName) { /* Create a child panel to represent the newly opened file */ if ((childPanel = LoadPanel (panelHandle, "menudemo.uir", FILEPANEL)) >= 0) { /* Set top and left for child panel */ SetPanelAttribute(childPanel, ATTR_TOP, topLeftValue*25+50); SetPanelAttribute(childPanel, ATTR_LEFT, topLeftValue*25+25);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -