Menus Declare the menu Toolbars
Programming guideline of WinDom

Handle the menu

There are three ways :

We describe the three way to proceed.

handle WM_MNSELECTED

This message is returned by EvntWIndom() when a window menu item is selected by the user. The message has the following structure :

               evnt.buff[0] = WM_MNSELECTED
               evnt.buff[1] = application identifier
               evnt.buff[2] = always 0
               evnt.buff[3] = window targetted handle
               evnt.buff[4] = title selected index
               evnt.buff[5] = item selected index
Here a typical example :

         main() {
             int res, title;
             WINDOW *win;
     
             /* create a window with a menu */
             ...
             while( 1) {
                 res = EvntWindom( MU_MESAG);
                 if( res & MU_MESAG && evnt.buff[0] == WM_MNSELECTED) {
                     title = evnt.buff[4];
                     /* get the window targetted */
                     win = WindHandle( evnt.buff[3]);
                     /* Handle the menu */
                     switch( evnt.buff[5]) {
                     case ITEM1:
                         break;
                     case ITEM2:
                         break;
                     }
                     /* unhighlight the menu title */
                     MenuTnormal( win, evnt.buff[4], 1);
                 }
             }
         }
     
handle MN_SELECTED

This message, returned by EvntWindom() when a user selects a desktop menu item, has a similar structure than the previous one :

               evnt.buff[0] = WM_MNSELECTED
               evnt.buff[1] = application identifier
               evnt.buff[2] = always 0
               evnt.buff[3] = title selected index
               evnt.buff[4] = item selected index
And now an example :

         main() {
             int res, title;
             WINDOW *win;
     
             /* Diverses WinDom initialization ... */
     
             while( 1) {
                 res = EvntWindom( MU_MESAG);
                 if( res & MU_MESAG && evnt.buff[0] == MN_SELECTED) {
                     title = evnt.buff[3];
                     /* Handle the menu */
                     switch( evnt.buff[4]) {
                     case ITEM1:
                         break;
                     case ITEM2:
                         break;
                     }
                     /* unhighlight the menu title */
                     MenuTnormal( NULL, evnt.buff[3], 1);
                 }
             }
         }
Bind event WM_MNSELECTED to a function

A function can be linked to the window menu event with the call :

         WindSet( win, menu, do_menu);
where menu is the address of a menu object tree and do_menu is an event function which handle the user selection in the menu. This function is called by EvntWindom() when event WM_MNSELECTED occurs. This method have the advantage to have a global action in WinDom : each time EvntWindom() function is invoked, user selection of menu is taken into account.

         void main()
         {
             int res;
             WINDOW *win;  /* target window */
             OBJECT *tree;
             void do_menu( WINDOW *);
     
             ApplInit();
             RsrcLoad( "menu.rsc");
             /* create the window */
             win = WindCreate( NAME|MOVBER|CLOSER|SMALLER, app.x, app.y, app.w, app.h);
     
             /* add a menu and an event menu function */
             rsrc_gaddr( 0, MY_MENU, &menu);
             WindSet( win, WF_MENU, menu, do_menu);
     
             while(1) EvntWindom( MU_MESAG);
     
             RsrcFree();
             ApplExit();
         }
     
         /* Here the menu function */
     
         void do_menu( WINDOW *win) {
             int title = evnt.buff[4];
     
             switch(( evnt.buff[5]) {
             case ENTRE1:
                  break;
             case ENTRE2:
                  break;
             }
             MenuTnormal( win, title, 1);
          }
     
Note : the call

         WindSet( win, WF_MENU, menu, do_menu);
is strictly equivalent to :

         WindSet( win, WF_MENU, menu, NULL);
         EvntAttach( win, WM_MNSELECTED, do_menu);
Bind event MN_SELECTED to a function

As WM_MNSELECTED message, MN_SELECTED message, which designs user selection in the desktop menu, can be binded to a function. It is performed by the following call :

         EvntAttach( NULL, MN_SELECTED, do_menu);
Bind menu object to function

It is the third method to handle a menu in WinDom. Instead of handle the user selection event by catching an event message and trait it in a switch structure, we can attach each entry of a menu to a specific function. The method is the same for window menu and desktop menu. The following call :

         ObjcAttach( OC_MENU, win, QUIT, BIND_FUNCTION, quit);
attached the quit() function to the entry QUIT of the menu of the window win. If win is NULL, the desktop menu is addressed.

Object selection event function have a different prototype than event function :

         void quit( WINDOW *win, int obj, int mode, int title);
where win is the window descriptor containing the menu (it is NULL for the desktop menu, obj is the index of the selected entry, mode is equal to OC_MENU and title is the index of menu title hilighted by the user selection. This value is typically used by MenuTnormal() to unhilght the menu title. Example :

     #include <windom.h>
     #include <stdlib.h>
     
     /* Quit the application */
     
     void quitapp(void) {
         while( wglb.first) {
             ApplWrite( app.id, WM_DESTROY, wglb.first->handle);
             EvntWindom( MU_MESAG);
         }
         RsrcFree();
         ApplExit();
         exit( 0);
     }
     
     /* Give information */
     
     void information( WINDOW *win, int obj, int mode, int title) {
         FormAlert( 1, "[1][We have selected item %d][OK]", obj);
         MenuTnormal( NULL, title, 1);
     }
     
     /* Open a window */
     
     void openwin( WINDOW *win, int obj, int mode, int title) {
         WINDOW *win;
         OBJECT *menu;
     
         win = WindCreate( WAT_NOINFO, app.x, app.y, app.w, app.h);
         rsrc_gaddr( 0, WINMENU, &menu);
         WindSet( win, WF_MENU, menu, NULL);
         ObjcAttach( OC_MENU, win, CLOSE, BIND_FUNC, closewin);
     
         WindOpen( win, -1, -1, 300, 200);
         MenuTnormal( win, title, 1);
     }
     
     /* Close the window */
     
     void closewin( WINDOW *win, int obj, int mode, int title) {
         MenuTnormal( win, title, 1);
     }
     
     void main( void) {
         OBJECT *menu;
         WINDOW *win;
     
         ApplInit();
         RsrcLoad( "resource.rsc");
     
         rsrc_gaddr( 0, DESKMENU, &menu);
         MenuBar( menu, 1);
         ObjcAttach( OC_MENU, NULL, QUIT, BIND_FUNC, quitapp);
         ObjcAttach( OC_MENU, NULL, INFORMATION, BIND_FUNC, information);
         ObjcAttach( OC_MENU, NULL, OPEN, BIND_FUNC, openwin);
     
         for(;;) EvntWindom( MU_MESAG);
     }