Sample8: Menu Widget.

Two kind of menu are available in MGUI: Pulldown Menu and Popup Menu. The Pulldown Menu consists of a ‘MENUBAR’, usually positioned on the top of the window, containing one or more ‘menu titles’. By selecting a menu title, a list of items (MENU) is open. You can navigate in a menu via the mouse or the arrow keys. An item can be linked to an application callback or it can open another MENU (a SubMenu). The Popup Menu can be open anywhere on the screen. Is is primarily used to implement context-sensitive menus accessed via the right mouse button click.
The MenuBar is an ordinary Terminal Object, so it has no reserved position and you can create it as a child of whatever SHELL or FORM you want. However, the canonical position of a MenuBar is the top of a window so you should create it as the first child of the SHELL. The MenuBar is a special Terminal Object since it is the container for its Menu Objects. Once the menu bar is created, you can attach to it the titles and the corresponding Menus. Items are created as children of a Menu with an optional callback function and accelerator key. The item can also be a container for a SubMenu and finally it can be made unsensitive or checked with automatic toggle and radio behaviour.

/* C version */ #include <stdio.h> #include <stdlib.h> #include <string.h> #include "mgui.h" void ExitCB(MENU_ITEM item, MVALUE status, void *b) {    MShellDestroy((MOBJECT)b);    MGUITerm();    exit(0); } void ToggleItemCB(MENU_ITEM item, MVALUE status, void *udata) {    char str[128];    sprintf(str, "The selected menu item\nhas been %s",       (status ? "Activated" : "Deactivated"));    MMessageDialog("", str, "Ok", NULL); } void RadioItemCB(MENU_ITEM item, MVALUE status, void *udata) {    char str[128];    if (!status)       return;    sprintf(str, "Menu item <%lx> selected", item);    MMessageDialog("", str, "Ok", NULL); } void MGUIMain(int argc, char **argv) {    MOBJECT shell, menubar, menu, submenu;    MENU_ITEM pd_item_id;    shell = MCreateShell("Sample 8", SF_NO_CLOSE);    MObjectSetBackgroundImageFile(shell, "tile5.bmp", BI_TILED);    menubar = MCreateMenuBar(shell, HELV_L_FONT);    MMenuBarSetAcc(menubar, F10);    menu = MCreateMenu(menubar, "File");    MMenuAddItem(menu, "Quit", ExitCB, shell);    menu = MCreateMenu(menubar, "Submenu");    MMenuAddToggleItem(menu, "Item 1", ToggleItemCB, NULL);    MMenuAddToggleItem(menu, "Item 2", ToggleItemCB, NULL);    pd_item_id = MMenuAddItem(menu, "Item 3", NULL, NULL);    MMenuAddToggleItem(menu, "Item 4", ToggleItemCB, NULL);    submenu = MCreateSubMenu(pd_item_id);    MMenuAddRadioItem(submenu, "RadioItem 3-1", RadioItemCB, NULL);    MMenuAddRadioItem(submenu, "RadioItem 3-2", RadioItemCB, NULL);    MMenuAddRadioItem(submenu, "RadioItem 3-3", RadioItemCB, NULL);    MMenuAddSeparator(submenu);    MMenuAddRadioItem(submenu, "RadioItem 3-4", RadioItemCB, NULL);    MMenuAddRadioItem(submenu, "RadioItem 3-5", RadioItemCB, NULL);    MShellRealize(shell);    MMainLoop(); }
The ExitCB() callback, linked to the ‘Exit’ item in the ‘File’ menu, causes the program termination when it is selected. The ToggleItemCB() callback is called when the corresponding toggle-item is selected. The item’s check status is reported as the second argument of the callback call.
The RadioItemCB() is bound to radio-items. It’s called for both the deactivated and the activated item. The code in this example opens a message window only for the newly activated item. The check status can be obtained via a call to the function MMenuItemGetCheckStatus(), specifying the item identifier. It can be forced via two function calls: MMenuItemSetCheckStatus() and MMenuItemToggleCheckStatus(). Unlike the radio and toggle buttons, the change is not propagated to the callback.
// C++ version
#include <stdio.h>
#include <string.h>
#include "mguipp.h"

class CaAppl : public CmAppl {
public:
   void start(int, char **);
};

CaAppl appInstance;

class CaMainWindow : public CmShell {
public:
   CaMainWindow(const char *title, int flags);
   void toggleItemCB(int status);
   void radioItemCB(int status);
   void exitCB(int);
};

void CaMainWindow::exitCB(int)
{
   delete this;
   CmAppl::end(0);
}

void CaAppl::start(int argc, char **argv)
{
   CaMainWindow *win = new CaMainWindow("Example 8", SF_NO_CLOSE);
   win->realize();
   mainLoop();
}

void CaMainWindow::toggleItemCB(int status)
{
   char str[128];

   sprintf(str, "The selected menu item\nhas been %s",
   (status ? "Activated" : "Deactivated"));
   messageDialog("", str, "Ok", NULL);
}

void CaMainWindow::radioItemCB(int status)
{
   char str[128];

   if (!status)
      return;
   sprintf(str, "Menu item has been selected");
   messageDialog("", str, "Ok", NULL);
}

CaMainWindow::CaMainWindow(const char *title, int flags)
: CmShell(title, flags)
{
   CmMenuBar *menubar;
   CmMenu *menu, *submenu;
   CmMenuItem *item;

   setBackgroundImageFile("tile5.bmp", BI_TILED);

   menubar = new CmMenuBar(this, HELV_L_FONT);
   menubar->setAcceleratorKey(F10);
   menu = new CmMenu(menubar, "File");
   new CmMenuItem(menu, "Quit", this, (MENUITEM_CB)exitCB);
   menu = new CmMenu(menubar, "Submenu");
   new CmMenuToggleItem(menu, "Item 1", this, (MENUITEM_CB)toggleItemCB);
   new CmMenuToggleItem(menu, "Item 2", this, (MENUITEM_CB)toggleItemCB);
   item = new CmMenuItem(menu, "Item 3");
   new CmMenuToggleItem(menu, "Item 4", this, (MENUITEM_CB)toggleItemCB);

   submenu = new CmMenu(item);
   new CmMenuRadioItem(submenu, "RadioItem 3-1", this, (MENUITEM_CB)radioItemCB);
   new CmMenuRadioItem(submenu, "RadioItem 3-2", this, (MENUITEM_CB)radioItemCB);
   new CmMenuRadioItem(submenu, "RadioItem 3-3", this, (MENUITEM_CB)radioItemCB);
   new CmMenuSeparator(submenu);
   new CmMenuRadioItem(submenu, "RadioItem 3-4", this, (MENUITEM_CB)radioItemCB);
   new CmMenuRadioItem(submenu, "RadioItem 3-5", this, (MENUITEM_CB)radioItemCB);
}

Screenshot

Back