home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / yacl-012.zip / uidemo / menu0 / menudrv.cxx < prev    next >
C/C++ Source or Header  |  1995-04-08  |  4KB  |  149 lines

  1.  
  2.  
  3. #include "menudrv.h"
  4. #include "ids.h"
  5.  
  6. #include "ui/menu.h"
  7. #include "ui/applic.h"
  8. #include "ui/stddlg.h"
  9.  
  10. #if defined(__GNUC__)
  11. template class CL_Binding<MenuDriver>; // Instantiate the binding
  12. #endif
  13.  
  14. extern UI_Application* _TheApplication;
  15. typedef CL_Binding<MenuDriver> MenuBinding;
  16.  
  17. UI_MenuItemDescriptor ActSubmenu [] = { // Cascaded submenu
  18.   {"&Month"           , ID_SUBMONTH}
  19. , {"&Days"            , ID_SUBDAY}
  20. , {NULL               , 0}
  21. };
  22.  
  23. UI_MenuItemDescriptor ActMenu [] = {    // Actions pull-down menu
  24.     {"&Add month"     , ID_ADD ,     NULL      }
  25.   , {"&Enable/Disable", ID_DIS ,     ActSubmenu}
  26.   , {"&Remove month"  , ID_REM ,     NULL      }
  27.   , {UIMenu_Separator , NULL   ,     NULL      }
  28.   , {"&Quit"          , ID_QUIT,     NULL      }
  29.   , {NULL             , 0      ,     NULL      }
  30. };
  31.  
  32. UI_MenuItemDescriptor MonthMnu [] = {   // Month pull-down menu
  33.     {"&Jan"           , ID_JAN}
  34.   , {"&Feb"           , ID_FEB}
  35.   , {"&Mar"           , ID_MAR}
  36.   , {"&Apr"           , ID_APR}
  37.   , {NULL             , 0     }
  38. };
  39.  
  40. UI_MenuItemDescriptor DaysMenu [] = {   // Days pull-down menu
  41.     {"Monday"         , ID_MON}
  42.   , {"Tuesday"        , ID_TUE}
  43.   , {"Wednesday"      , ID_WED}
  44.   , {"Thursday"       , ID_THU}
  45.   , {"Friday"         , ID_FRI}
  46.   , {NULL             ,      0}
  47. };
  48.  
  49. UI_MenuItemDescriptor MainMenuDesc [] = {   // Main menu bar
  50.     {"&Action"        , ID_ACT  ,  ActMenu }
  51.   , {"&Month"         , ID_MONTH,  MonthMnu}
  52.   , {"&Day"           , ID_DAYS ,  DaysMenu}
  53.   , {NULL             ,        0,  NULL    }
  54. };
  55.  
  56. struct DispatcherStruct {
  57.     UI_ViewID              menu_id;
  58.     MenuBinding::MethodPtr method;
  59.     UI_EventType           event;
  60.     long                   parameter;
  61. } FAR dispatch [] = {
  62.     {ID_JAN,      MenuDriver::Inform,         Event_Select,  ID_JAN}      
  63.   , {ID_FEB,      MenuDriver::Inform,         Event_Select,  ID_FEB}      
  64.   , {ID_MAR,      MenuDriver::Inform,         Event_Select,  ID_MAR}      
  65.   , {ID_APR,      MenuDriver::Inform,         Event_Select,  ID_APR}      
  66.                                                                           
  67.   , {ID_ADD,      MenuDriver::Add,            Event_Select,  0}           
  68.   , {ID_REM,      MenuDriver::Remove,         Event_Select,  0}           
  69.   , {ID_SUBMONTH, MenuDriver::DisableMonth,   Event_Select,  0} 
  70.   , {ID_SUBDAY,   MenuDriver::DisableDays,    Event_Select,  0}   
  71.   , {ID_QUIT,     MenuDriver::Quit,           Event_Select,  0}
  72. };
  73.  
  74.  
  75.  
  76.  
  77. MenuDriver::MenuDriver (UI_CompositeVObject* root)
  78. {
  79.     _menu =  new UI_MenuBar (root, MainMenuDesc);
  80.     for (short i = 0; i < sizeof dispatch/sizeof (DispatcherStruct); i++) {
  81.         MenuBinding bind (this, dispatch[i].method);
  82.         (*_menu)[dispatch[i].menu_id]->AddEventDependent
  83.             (dispatch[i].event, bind, dispatch[i].parameter);
  84.     }
  85.     (*_menu)[ID_REM]->Disable();
  86.     _daysEnabled = TRUE;
  87. }
  88.  
  89.  
  90.  
  91.  
  92. bool MenuDriver::Inform (CL_Object&, long item_id)
  93. {
  94.     UI_SimpleDialog ("You chose " + (*_menu)[item_id]->Title());
  95.     return TRUE;
  96. }
  97.  
  98.  
  99. bool MenuDriver::Add (CL_Object&, long)
  100. {
  101.     _menu->Add (ID_MAY, "Ma&y", ID_MONTH);
  102.     (*_menu)[ID_ADD]->Disable();
  103.     (*_menu)[ID_REM]->Enable();
  104.     UI_SimpleDialog ("Added an item to Month menu");
  105.     return TRUE;
  106. }
  107.  
  108.  
  109. bool MenuDriver::Remove (CL_Object&, long)
  110. {
  111.     _menu->Remove (ID_MAY);
  112.     (*_menu)[ID_REM]->Disable();
  113.     (*_menu)[ID_ADD]->Enable();
  114.     UI_SimpleDialog ("Removed an item from Month menu");
  115.     return TRUE;
  116. }
  117.  
  118.  
  119.  
  120. bool MenuDriver::DisableMonth (CL_Object&, long)
  121. {
  122.     UI_MenuItem* m = (*_menu)[ID_MONTH];
  123.     m->ToggleEnabledState();
  124.     if (m->IsEnabled())
  125.         m->Title() = "&Month";
  126.     else
  127.         m->Title() = "No month";
  128.     return TRUE;
  129. }
  130.  
  131.  
  132. bool MenuDriver::DisableDays  (CL_Object&, long)
  133. {
  134.     short n = _menu->ChildCount (ID_DAYS);
  135.     for (short i = 0; i < n; i++)
  136.         _menu->Child(ID_DAYS, i)->ToggleEnabledState();
  137.     char* msg = _daysEnabled ? " disabled" : " enabled";
  138.     UI_SimpleDialog (CL_String ("All days ") + msg);
  139.     _daysEnabled = _daysEnabled ? FALSE : TRUE;
  140.     return TRUE;
  141. }
  142.  
  143.  
  144. bool MenuDriver::Quit (CL_Object&, long)
  145. {
  146.     _TheApplication->End();
  147.     return TRUE;
  148. }
  149.