home *** CD-ROM | disk | FTP | other *** search
/ Power GUI Programming with VisualAge C++ / powergui.iso / powergui / menus / dynsubmn / dynsubmn.cpp next >
Encoding:
C/C++ Source or Header  |  1996-10-29  |  3.9 KB  |  174 lines

  1. //************************************************************
  2. // Menus - Dynamic Submenu Example
  3. //
  4. // Copyright (C) 1994, Law, Leong, Love, Olson, Tsuji.
  5. // Copyright (c) 1997 John Wiley & Sons, Inc. 
  6. // All Rights Reserved.
  7. //************************************************************
  8. #include <iapp.hpp>
  9. #include <icmdhdr.hpp>
  10. #include <iframe.hpp>
  11. #include <imenuhdr.hpp>
  12. #include <imle.hpp>
  13. #include <isubmenu.hpp>
  14. #include "dynsubmn.h"
  15.  
  16. // Menu handler to dynamically modify a drop-down menu.
  17. class MenuHandler : public IMenuHandler
  18. {
  19. public:
  20.   MenuHandler (IMultiLineEdit& editWnd)
  21.     : editWindow(editWnd) {}
  22. protected:
  23. virtual Boolean
  24.   menuShowing   ( IMenuEvent& menuEvent,
  25.                   ISubmenu&   submenuAboutToShow );
  26. private:
  27. IMultiLineEdit
  28.  &editWindow;
  29. MenuHandler& operator=(const MenuHandler&);
  30. };
  31.  
  32. // Command handler to capture menu commands.
  33. class CommandHandler : public ICommandHandler
  34. {
  35. public:
  36.   CommandHandler ( IMultiLineEdit& status)
  37.             : aStatus(status) {}
  38.  
  39. protected:
  40. virtual Boolean
  41.   command              ( ICommandEvent& event );
  42.  
  43. private:
  44. IMultiLineEdit
  45.  &aStatus;
  46. CommandHandler& operator=(const CommandHandler&);
  47. };
  48.  
  49. void main()
  50. {
  51. // Create a frame window with a menubar
  52. // loaded from a resource file.
  53. IFrameWindow
  54.   frame ("Dynamic Submenu Example", MAIN_MENU,
  55.          IFrameWindow::defaultStyle() | IFrameWindow::menuBar );
  56.  
  57. // Create an edit area in the client
  58. // and a command handler to write in it.
  59. IMultiLineEdit
  60.   editArea(ID_EDIT, &frame, &frame);
  61. CommandHandler
  62.   commandHandler(editArea);
  63.  
  64. // Add the command handler to the frame so
  65. // that it handles the menu messages sent.
  66. commandHandler
  67.   .handleEventsFor(&frame);
  68.  
  69. // Add a menu handler to dynamically alter the edit submenu.
  70. MenuHandler
  71.   editMenuHandler(editArea);
  72. editMenuHandler.handleEventsFor(&frame);
  73.  
  74. // Set the focus and show the application.
  75. editArea.setFocus();
  76. frame
  77.   .setClient(&editArea)
  78.   .show();
  79. IApplication::current().run();
  80. }
  81.  
  82. IBase::Boolean MenuHandler::menuShowing( IMenuEvent& event,
  83.                                          ISubmenu&   submenu )
  84. {
  85.   // Enable and disable the appropriate "Edit flags".
  86.   Boolean modified = false;
  87.   if (submenu.id() == MI_EDIT)
  88.   {
  89.      if (!editWindow.hasSelectedText())
  90.      {
  91.         submenu.disableItem(MI_COPY);
  92.         submenu.disableItem(MI_CUT);
  93.         modified = true;
  94.      }
  95.      if (!editWindow.isWriteable())
  96.      {
  97.         submenu.disableItem(MI_CUT);
  98.         submenu.disableItem(MI_PASTE);
  99.         submenu.disableItem(MI_READONLY);
  100.         modified = true;
  101.      }
  102.      else
  103.      {
  104.         submenu.disableItem(MI_READWRITE);
  105.         modified = true;
  106.      }
  107.  
  108.      if (!editWindow.clipboardHasTextFormat())
  109.      {
  110.         submenu.disableItem(MI_PASTE);
  111.         modified = true;
  112.      }
  113.   }
  114.   return modified;
  115. }
  116.  
  117.  
  118. IBase::Boolean CommandHandler::command( ICommandEvent& event )
  119. {
  120.   switch(event.commandId())
  121.   {
  122.     case MI_CUT           :
  123.     {
  124.       aStatus.cut();
  125.       return true;
  126.     }
  127.  
  128.     case MI_COPY          :
  129.     {
  130.       aStatus.copy();
  131.       return true;
  132.     }
  133.  
  134.     case MI_PASTE         :
  135.     {
  136.       aStatus.paste();
  137.       return true;
  138.     }
  139.  
  140.     case MI_READONLY   :
  141.     {
  142.       aStatus.disableDataUpdate();
  143.       return true;
  144.     }
  145.  
  146.     case MI_READWRITE   :
  147.     {
  148.       aStatus.enableDataUpdate();
  149.       return true;
  150.     }
  151.  
  152.     case MI_FILE          :
  153.     case MI_NEW           :
  154.     case MI_OPEN          :
  155.     case MI_SAVE          :
  156.     case MI_SAVEAS        :
  157.     case MI_EDIT          :
  158.     case MI_UNDO          :
  159.     case MI_EXAMPLE       :
  160.     case MI_BITMAP        :
  161.     case MI_HELP          :
  162.     case MI_GENERAL_HELP  :
  163.     case MI_CASCADE1      :
  164.     case MI_CASCADE2      :
  165.     case MI_CASCADE3      :
  166.     {
  167.        aStatus.setText(event.commandId());
  168.        return true;
  169.     }
  170.   }
  171. return false;
  172. }
  173.  
  174.