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

  1. //************************************************************
  2. // Menus  - Creating Pop-up Menus Dynamically
  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 <imenubar.hpp>
  12. #include <imenuhdr.hpp>
  13. #include <imnitem.hpp>
  14. #include <ipopmenu.hpp>
  15. #include <isysmenu.hpp>
  16. #include <istattxt.hpp>
  17. #include "dynpopup.h"
  18.  
  19. // Menu handler to capture pop-up menu requests.
  20. class MenuHandler : public IMenuHandler
  21. {
  22. protected:
  23. virtual Boolean
  24.   makePopUpMenu(IMenuEvent& menuEvent);
  25. };
  26.  
  27. // Command handler to capture menu commands.
  28. class CommandHandler : public ICommandHandler
  29. {
  30. public:
  31.   CommandHandler ( IStaticText& status)
  32.             : aStatus(status) {}
  33.  
  34. protected:
  35. virtual Boolean
  36.   command              ( ICommandEvent& event );
  37.  
  38. private:
  39. IStaticText
  40.  &aStatus;
  41. CommandHandler& operator= ( const CommandHandler&);
  42. };
  43.  
  44. void main()
  45. {
  46. IFrameWindow
  47.   frame ("Dynamically-Created Pop-Up Menu Example");
  48.  
  49. // Add the menubar from a resource file.
  50. IMenuBar
  51.   menuBar(MAIN_MENU, &frame);
  52. #ifdef IC_WIN
  53. // For windows, bitmap is not automatically loaded into menu.
  54. menuBar.setBitmap(MI_BITMAP, MI_BITMAP);
  55. #endif
  56.  
  57. // Create a Status Area in the Client
  58. // and a command handler to write in it.
  59. IStaticText
  60.   statusArea(ID_STATUS, &frame, &frame);
  61. CommandHandler
  62.   commandHandler(statusArea);
  63.  
  64. // Add the command handler to the frame to receive the
  65. // menu commands and to the status area to receive any
  66. // pop-up menu commands sent.
  67. commandHandler
  68.   .handleEventsFor(&frame)
  69.   .handleEventsFor(&statusArea);
  70.  
  71. // Add a pop-up menu to the client status area.
  72. MenuHandler
  73.   textPopUpHandler;
  74. textPopUpHandler.handleEventsFor(&statusArea);
  75.  
  76. // Set the focus and show the application.
  77. frame
  78.   .setClient(&statusArea)
  79.   .setFocus()
  80.   .show();
  81. IApplication::current().run();
  82.  
  83. }
  84.  
  85. IBase::Boolean MenuHandler :: makePopUpMenu(IMenuEvent& event)
  86. {
  87.   // Create the pop-up menu.
  88.   IPopUpMenu * popUp;
  89.   popUp=new IPopUpMenu(event.window(), POPUP_MENU);
  90.  
  91.   // Create menu items for "Close" and "Help".
  92.   IMenuItem close(ISystemMenu::idClose,
  93.                   IMenuItem::postSystemCommand);
  94.   close.setText(ISystemMenu::idClose);
  95.   IMenuItem help(MI_HELP, IMenuItem::postHelp);
  96.   help.setText(MI_HELP);
  97.  
  98.   // Add the menu items to the pop-up menu.
  99.   (*popUp)
  100.     .addText(MI_EDIT,   MI_EDIT)
  101.     .addSubmenu(MI_EDIT)
  102.       .addText(MI_UNDO,  MI_UNDO, MI_EDIT)
  103.       .addSeparator(MI_EDIT)
  104.       .addText(MI_CUT,   MI_CUT,   MI_EDIT)
  105.       .addText(MI_COPY,  MI_COPY,  MI_EDIT)
  106.       .addText(MI_PASTE, MI_PASTE, MI_EDIT)
  107.     .addText(MI_EXAMPLE, MI_EXAMPLE)
  108.     .addSubmenu(MI_EXAMPLE)
  109.       .addBitmap(MI_BITMAP, MI_BITMAP, MI_EXAMPLE)
  110.       .addSeparator(MI_EXAMPLE)
  111.       .addItem(close, MI_EXAMPLE)
  112.       .addItem(help, MI_EXAMPLE)
  113.     .setAutoDeleteObject();
  114.  
  115.   // Show the pop-up menu.
  116.   (*popUp)
  117.     .show(event.mousePosition());
  118.   return true;
  119. }
  120.  
  121.  
  122. IBase::Boolean CommandHandler::command( ICommandEvent& event )
  123. {
  124.   switch(event.commandId())
  125.   {
  126.     case MI_FILE          :
  127.     case MI_NEW           :
  128.     case MI_OPEN          :
  129.     case MI_SAVE          :
  130.     case MI_SAVEAS        :
  131.     case MI_EDIT          :
  132.     case MI_UNDO          :
  133.     case MI_CUT           :
  134.     case MI_COPY          :
  135.     case MI_PASTE         :
  136.     case MI_EXAMPLE       :
  137.     case MI_BITMAP        :
  138.     case MI_HELP          :
  139.     case MI_GENERAL_HELP  :
  140.     {
  141.        aStatus.setText(event.commandId());
  142.        return true;
  143.     }
  144.  
  145.   }
  146. return false;
  147. }
  148.  
  149.