home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / yacl-012.zip / uidemo / menu1 / menudrv.cxx < prev    next >
C/C++ Source or Header  |  1995-01-09  |  6KB  |  230 lines

  1.  
  2.  
  3. #include "menudrv.h"
  4. #include "ids.h"
  5. #include "box.h"
  6.  
  7. #include "ui/ui.h"
  8.  
  9.  
  10. #if defined(__GNUC__)
  11. template class CL_Binding<MenuDriver>; // Instantiate the binding
  12. #endif
  13.  
  14. extern UI_Application* _TheApplication;
  15. extern UI_MenuBar*     MainMenu;
  16.  
  17. typedef CL_Binding<MenuDriver> MenuBinding;
  18. MenuDriver::MenuDriver()
  19. {
  20.     box = NULL;
  21.     (*MainMenu)[ID_STOP]->Disable();
  22.     UI_CompositeVObject* root = _TheApplication->MainWindow();
  23.     UI_Rectangle shape = root->ClientArea ();
  24.     status = new UI_Label (root, UI_Rectangle (0, shape.Height()-30,
  25.                                                shape.Width(), 30));
  26.     status->ShowBorder ();
  27.     status->Title() = "Ready";
  28.     MenuBinding bind (this, &MenuDriver::Resize);
  29.     root->AddEventDependent (Event_Reconfigure, bind, 1);
  30. }
  31.  
  32. MenuDriver::~MenuDriver()
  33. {
  34.     if (box)
  35.         _TheApplication->Destroy(box);
  36. }
  37.  
  38.  
  39. struct DispatcherStruct {
  40.     UI_ViewID              menu_id;
  41.     MenuBinding::MethodPtr method;
  42.     UI_EventType           event;
  43.     long                   parameter;
  44. } FAR dispatch [] = {
  45.     {ID_START,    MenuDriver::Start,  Event_Select,  1}
  46.   , {ID_STOP,     MenuDriver::Stop,   Event_Select,  1}
  47.   , {ID_SETSEL,   MenuDriver::SetSel, Event_Select,  1}
  48.   , {ID_EMPTY,    MenuDriver::Empty,  Event_Select,  1}
  49.   , {ID_REFILL,   MenuDriver::Refill, Event_Select,  1}
  50.   , {ID_SORT,     MenuDriver::Sort,   Event_Select,  1}
  51.   , {ID_QUIT,     MenuDriver::Quit,   Event_Select,  1}
  52.  
  53.   , {ID_SUBMENU1, MenuDriver::Inform, Event_Select,  ID_SUBMENU1}
  54.   , {ID_SUBMENU2, MenuDriver::Inform, Event_Select,  ID_SUBMENU2}
  55.  
  56.   , {ID_START, MenuDriver::Change, Event_GetFocus, ID_START}
  57.   , {ID_STOP,  MenuDriver::Change, Event_GetFocus, ID_STOP}
  58.   , {ID_SETSEL,MenuDriver::Change, Event_GetFocus, ID_SETSEL}
  59.   , {ID_QUIT,  MenuDriver::Change, Event_GetFocus, ID_QUIT}
  60.  
  61.   , {ID_START, MenuDriver::Change, Event_LoseFocus, 0}
  62.   , {ID_STOP,  MenuDriver::Change, Event_LoseFocus, 0}
  63.   , {ID_SETSEL,MenuDriver::Change, Event_LoseFocus, 0}
  64.   , {ID_QUIT,  MenuDriver::Change, Event_LoseFocus, 0}
  65.  
  66.   , {ID_APP2,  MenuDriver::Change, Event_GetFocus, 0}
  67.   , {ID_APP3,  MenuDriver::Change, Event_GetFocus, 0}
  68.     
  69. };
  70.  
  71.  
  72.  
  73. void MenuDriver::Setup (UI_MenuBar* menu)
  74. {
  75.     for (short i = 0; i < sizeof dispatch/sizeof (DispatcherStruct); i++) {
  76.         (*menu)[dispatch[i].menu_id]->AddEventDependent
  77.             (dispatch[i].event, MenuBinding (this, dispatch[i].method),
  78.              dispatch[i].parameter);
  79.     }
  80. }
  81.  
  82.  
  83.  
  84.  
  85. bool MenuDriver::Start (CL_Object&, long)
  86. {
  87.     if (!box) {
  88.         box = new Box (_TheApplication->MainWindow(),
  89.                        UI_Rectangle (20, 50, 400, 250));
  90.         (*MainMenu)[ID_STOP]->Enable();
  91.         (*MainMenu)[ID_START]->Disable();
  92.         return TRUE;
  93.     }
  94.     return FALSE;
  95. }
  96.  
  97.  
  98.  
  99. bool MenuDriver::Stop  (CL_Object&, long)
  100. {
  101.     if (box) {
  102.         _TheApplication->Destroy(box);
  103.         box = NULL;
  104.     }
  105.     (*MainMenu)[ID_START]->Enable();
  106.     (*MainMenu)[ID_STOP]->Disable();
  107.     return TRUE;
  108. }
  109.  
  110.  
  111. bool MenuDriver::Quit  (CL_Object&, long)
  112. {
  113.     _TheApplication->End();
  114.     return TRUE;
  115. }
  116.  
  117. bool MenuDriver::Change (CL_Object& o, long menu_id)
  118. {
  119.     CL_String& s  = (CL_String &) status->Model();
  120.     UI_Event& e = (UI_Event&) o;
  121.     if (e.Type() == Event_LoseFocus) {
  122.         s = "";
  123.         return TRUE;
  124.     }
  125.     switch (menu_id) {
  126.     case ID_START:
  127.         s = "Creates the composite";
  128.         break;
  129.         
  130.     case ID_STOP:
  131.         s = "Destroys the composite";
  132.         break;
  133.         
  134.     case ID_QUIT:
  135.         s = "Terminates the application";
  136.         break;
  137.         
  138.     case ID_SETSEL:
  139.         s = "Sets selections in string views";
  140.         break;
  141.  
  142.     default:
  143.         s = "Ready";
  144.     }
  145.     return TRUE;
  146. }
  147.  
  148.  
  149. bool MenuDriver::SetSel (CL_Object&, long)
  150. {
  151.     if (!box)
  152.         return TRUE;
  153.     UI_StringViewSingleSel& sngl = (UI_StringViewSingleSel&)
  154.         *((*box)[ID_SINGLE_VIEW]);
  155.     UI_StringViewSingleSel& mult = (UI_StringViewSingleSel&)
  156.         *((*box)[ID_MULT_VIEW]);
  157.     sngl.Selection() = 1;
  158.     mult.Selection() = CL_IntegerSet (2,4); // Select three items
  159.     return TRUE;
  160. }
  161.  
  162.  
  163.  
  164.  
  165. bool MenuDriver::Empty (CL_Object&, long)
  166. {
  167.     if (box) {
  168.         UI_StringSequence& seq = box->Sequence();
  169.         seq.MakeEmpty();
  170.     }
  171.     return TRUE;
  172. }
  173.  
  174.  
  175. bool MenuDriver::Refill (CL_Object&, long)
  176. {
  177.     if (box) {
  178.         UI_StringSequence& seq = box->Sequence();
  179.         const char* sq[] =
  180.                      {"One", "Two", "Three", "Four", "Five", "Six", "Seven",
  181.                       "Eight", "Nine", "Ten", "Eleven", "Twelve",
  182.                       "Thirteen", "Fourteen", "Fifteen"};
  183.         seq = CL_StringSequence (sq, 15);
  184.     }
  185.     return TRUE;
  186. }
  187.  
  188. #include <iostream.h> // DEBUG
  189. bool MenuDriver::Resize (CL_Object&, long)
  190. {
  191.     if (!box)
  192.         return TRUE;
  193.     UI_Rectangle& shape = _TheApplication->MainWindow()->ClientArea();
  194.     UI_Rectangle& boxShape = box->ClientArea();
  195.     UI_Point p ((shape.Width()  - boxShape.Width())/2,
  196.                 (shape.Height() - boxShape.Height())/2);
  197.     boxShape.Origin (p);
  198.     return TRUE;
  199. }
  200.  
  201.  
  202. bool MenuDriver::Sort (CL_Object&, long)
  203. {
  204.     if (box) {
  205.         UI_StringSequence& seq = box->Sequence();
  206.         seq.Sort();
  207.     }
  208.     return TRUE;
  209. }
  210.  
  211.  
  212. bool MenuDriver::Inform (CL_Object&, long item_id)
  213. {
  214.     if (box) {
  215.         UI_StringSequence& seq = box->Sequence();
  216.         seq[3] = "three";
  217.         seq[4] = "four";
  218.     }
  219.     UI_SimpleDialog ("You chose " + (*MainMenu)[item_id]->Title());
  220.     if (item_id == ID_SUBMENU1) {
  221.         MainMenu->Add (298, "New item", ID_APP2);
  222.         MainMenu->Add (299, "Another New item", ID_APP2);
  223.         MainMenu->Add (300, "Secondary item", ID_APP2, 0);
  224.     }
  225.     else
  226.         MainMenu->Remove (ID_APP2);
  227.     return TRUE;
  228. }
  229.  
  230.