📄 menudemo.c
字号:
/*---------------------------------------------------------------------------*/
/* */
/* FILE: menudemo.c */
/* */
/* PURPOSE: This example illustrates how to use the Menu Utility instrument */
/* driver to implement Most Recently Used (MRU) sections on CVI */
/* menus. This particular example implements MRU lists for the File*/
/* and Window menus. The MRU lists can be stored in the Inifle */
/* objects, provided by the Inifile instrument driver. */
/* */
/*---------------------------------------------------------------------------*/
/*---------------------------------------------------------------------------*/
/* Include files */
/*---------------------------------------------------------------------------*/
#include <cvirte.h>
#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
/*---------------------------------------------------------------------------*/
/* Types */
/*---------------------------------------------------------------------------*/
typedef struct WindowMenuCallbackDataRec_Tag
{
char fileName[MAX_PATHNAME_LEN];
int panel;
} WindowMenuCallbackData;
/*---------------------------------------------------------------------------*/
/* Module-globals */
/*---------------------------------------------------------------------------*/
static int g_panelHandle;
static int g_menubarHandle;
static IniText g_iniTextHandle = 0;
static menuList g_fileMenuListHandle = 0;
static menuList g_winMenuListHandle = 0;
static char g_msgBuffer[512];
static int g_numChildPanels = 0;
static int g_topLeftValue = 0;
/*---------------------------------------------------------------------------*/
/* Internal function prototypes */
/*---------------------------------------------------------------------------*/
static int DimMenuItems (void);
static 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 int CheckForFileNameInMenuList (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);
/*---------------------------------------------------------------------------*/
/* This is the application's entry-point. */
/*---------------------------------------------------------------------------*/
int main (int argc, char *argv[])
{
if (InitCVIRTE (0, argv, 0) == 0)
return -1;
if ((g_panelHandle = LoadPanel (0, "menudemo.uir", PANEL)) < 0)
return -1;
g_menubarHandle = GetPanelMenuBar (g_panelHandle);
GetOptionsForUIR ();
DimMenuItems ();
CreateWindowMenuList ();
DisplayPanel (g_panelHandle);
RunUserInterface ();
/* Free resources and return */
RemoveWindowMenuList ();
SaveOptionsForUIR ();
DiscardPanel (g_panelHandle);
CloseCVIRTE ();
return 0;
}
/*---------------------------------------------------------------------------*/
/* Dim File and Window menu items depending on whether or not a file is */
/* open. */
/*---------------------------------------------------------------------------*/
static int DimMenuItems(void)
{
SetMenuBarAttribute (g_menubarHandle, MAINMENU_FILE_SAVE, ATTR_DIMMED,
!g_numChildPanels);
SetMenuBarAttribute (g_menubarHandle, MAINMENU_FILE_SAVEAS, ATTR_DIMMED,
!g_numChildPanels);
SetMenuBarAttribute (g_menubarHandle, MAINMENU_FILE_CLOSE, ATTR_DIMMED,
!g_numChildPanels);
SetMenuBarAttribute (g_menubarHandle, MAINMENU_WINDOW_CLOSEALL,
ATTR_DIMMED, !g_numChildPanels);
SetMenuBarAttribute (g_menubarHandle, MAINMENU_WINDOW_HIDEALL,
ATTR_DIMMED, !g_numChildPanels);
return 0;
}
/*---------------------------------------------------------------------------*/
/* Create a menu list in the WINDOW menu. */
/*---------------------------------------------------------------------------*/
static int CreateWindowMenuList (void)
{
if (!g_winMenuListHandle)
g_winMenuListHandle = MU_CreateMenuList (g_menubarHandle,
MAINMENU_WINDOW, -1, 5,
WINDOWMenuListCallbackFunc);
if (g_winMenuListHandle)
{
/* Update Attributes of the successfully createed list*/
MU_SetMenuListAttribute (g_winMenuListHandle, 0,
ATTR_MENULIST_APPEND_SHORTCUT, 1);
MU_SetMenuListAttribute (g_winMenuListHandle, 0,
ATTR_MENULIST_ALLOW_DUPLICATE_ITEMS, 0);
}
else
MessagePopup ("MenuDemo", "Unable to create WINDOW menu list.");
return 0;
}
/*--------------------------------------------------------------------------*/
/* Remove menu list from WINDOW menu. */
/*--------------------------------------------------------------------------*/
static int RemoveWindowMenuList(void)
{
if (g_fileMenuListHandle)
MU_DeleteMenuList (g_winMenuListHandle);
return 0;
}
/*---------------------------------------------------------------------------*/
/* Get saved UIR options and update the UIR -- this means initializing the */
/* MRU File menu item list. */
/*---------------------------------------------------------------------------*/
static int GetOptionsForUIR (void)
{
int success = 1;
/* Create an INI object */
if (!g_iniTextHandle)
g_iniTextHandle = Ini_New (0);
/* Get options and update UIR */
if (g_iniTextHandle)
{
/* Read previous MRU list data from system */
MU_ReadRegistryInfo (g_iniTextHandle, DEMO_REGISTRY_NAME);
/* Create FILE menulist if it does not already exist */
if (!g_fileMenuListHandle)
g_fileMenuListHandle = MU_CreateMenuList (g_menubarHandle,
MAINMENU_FILE,
MAINMENU_FILE_ABOVE_EXIT_LINE,
5,
FILEMenuListCallbackFunc);
if (g_fileMenuListHandle)
{
/* Update attributes */
MU_SetMenuListAttribute (g_fileMenuListHandle, 0,
ATTR_MENULIST_APPEND_SHORTCUT, 1);
MU_SetMenuListAttribute (g_fileMenuListHandle, 0,
ATTR_MENULIST_ALLOW_DUPLICATE_ITEMS,
0);
/* Update FILE menulist with files from INI object */
MU_GetFileListFromIniFile (g_fileMenuListHandle, g_iniTextHandle,
"FILE MenuList", "Filename", 1);
}
}
else
success = 0;
return success;
}
/*---------------------------------------------------------------------------*/
/* Save options from current state of UIR to the system. Thius means storing*/
/* the MRU list on the File menu. */
/*---------------------------------------------------------------------------*/
static int SaveOptionsForUIR (void)
{
int success = 1;
if (g_iniTextHandle)
{
/* Save FILE menulist filenames to INI object */
if (g_fileMenuListHandle)
MU_PutFileListInIniFile (g_fileMenuListHandle, g_iniTextHandle,
"FILE MenuList", "Filename", TRUE);
/* Destroy FILE menulist */
if (g_fileMenuListHandle)
MU_DeleteMenuList (g_fileMenuListHandle);
/* Save options from UIR to system */
if (!MU_WriteRegistryInfo (g_iniTextHandle, DEMO_REGISTRY_NAME))
success = 0;
}
else
success = 0;
/* Discard INI object */
if (g_iniTextHandle)
Ini_Dispose (g_iniTextHandle);
return success;
}
/*--------------------------------------------------------------------------*/
/* Add a particular file name to the File menu's MRU list. */
/*--------------------------------------------------------------------------*/
static int AddFileNameToFileMenuList (menuList menuListHandle,
char *fullPathAndFilename)
{
int retVal = 0;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -