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

  1. //************************************************************
  2. // Menus - Custom Drawing 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 <iframe.hpp>
  9. #include <istattxt.hpp>
  10. #include <iapp.hpp>
  11. #include <ifont.hpp>
  12. #include <imenubar.hpp>
  13. #include <imnitem.hpp>
  14. #include <imndihdr.hpp>
  15. #include <icmdhdr.hpp>
  16. #include <irect.hpp>
  17. #include <istring.hpp>
  18. #include <igrafctx.hpp>
  19. #include <igstring.hpp>
  20. #include "drawmenu.h"
  21.  
  22. // Menu handler to capture Draw Item requests.
  23. class MenuDrawHandler : public IMenuDrawItemHandler
  24. {
  25. public:
  26.   MenuDrawHandler ()
  27.       : font("Tms Rmn", 24) {}
  28.  
  29. protected:
  30. virtual Boolean
  31.   setSize       ( IMenuDrawItemEvent& event,
  32.                   ISize&              newSize ),
  33.   draw          ( IMenuDrawItemEvent& event,
  34.                   DrawFlag&           flag ),
  35.   highlight     ( IMenuDrawItemEvent& event ),
  36.   unhighlight   ( IMenuDrawItemEvent& event ),
  37.   drawText      ( IMenuDrawItemEvent& event,
  38.                   Boolean             highlight );
  39.  
  40. private:
  41. IFont
  42.   font;
  43. };
  44.  
  45. // Command handler to capture menu commands.
  46. class CommandHandler : public ICommandHandler
  47. {
  48. public:
  49.   CommandHandler ( IStaticText& status)
  50.             : aStatus(status)  {}
  51.  
  52. protected:
  53. virtual Boolean
  54.   command    ( ICommandEvent& event );
  55.  
  56. private:
  57. IStaticText
  58.  &aStatus;
  59. CommandHandler& operator=(const CommandHandler&);
  60. };
  61.  
  62. void main()
  63. {
  64. IFrameWindow
  65.   frame ("Custom Menu Drawing Example");
  66.  
  67. // Add the Menubar from a resource file.
  68. IMenuBar
  69.   menuBar(MAIN_MENU, &frame);
  70. #ifdef IC_WIN
  71. menuBar.setBitmap(MI_BITMAP, MI_BITMAP);
  72. #endif
  73.  
  74. // Change a menu item to the drawItem style.
  75. IMenuItem drawItem = menuBar.menuItem(MI_DRAWITEM);
  76. drawItem.setDrawItem();
  77. menuBar.setItem(drawItem);
  78.  
  79. // Create a status area in the client and a
  80. // command handler to write in it.  Add the
  81. // command handler to the frame to handle
  82. // menu commands.
  83. IStaticText
  84.   statusArea(ID_STATUS, &frame, &frame);
  85. CommandHandler
  86.   commandHandler(statusArea);
  87. commandHandler
  88.   .handleEventsFor(&frame);
  89.  
  90. // Add a MenuDrawHandler to the frame
  91. MenuDrawHandler
  92.   drawHandler;
  93. drawHandler
  94.   .handleEventsFor(&frame);
  95.  
  96. // Set the focus and show the app
  97. frame
  98.   .setClient(&statusArea)
  99.   .setFocus()
  100.   .show();
  101. IApplication::current().run();
  102. }
  103.  
  104.  
  105. IBase::Boolean MenuDrawHandler::setSize(IMenuDrawItemEvent& event,
  106.                                         ISize& newSize)
  107. {
  108.   // Since we don't know the text contents, default menu item width large
  109.   // enough for 5 Ws and height to character height plus side padding
  110.   newSize.setWidth( font.textWidth("WWWWW") )
  111.          .setHeight(font.maxCharHeight()+5);
  112.   return true;
  113. }
  114.  
  115. IBase::Boolean MenuDrawHandler::highlight(IMenuDrawItemEvent& event)
  116. {
  117.   return drawText(event,true);
  118. }
  119.  
  120. IBase::Boolean MenuDrawHandler::unhighlight(IMenuDrawItemEvent& event)
  121. {
  122.   return drawText(event,false);
  123. }
  124.  
  125. IBase::Boolean MenuDrawHandler::draw(IMenuDrawItemEvent& event,
  126.                                      DrawFlag& flag)
  127. {
  128.   return drawText(event,false);
  129. }
  130.  
  131. IBase::Boolean MenuDrawHandler::drawText(IMenuDrawItemEvent& event,
  132.                                          Boolean             highlight )
  133. {
  134.   // This routine assumes that all drawing is for text items whose
  135.   // text is contained in a STRINGTABLE resource with an id the
  136.   // same as the item id.
  137.   IString str = IApplication::current().userResourceLibrary()
  138.                    .loadString(event.itemId());
  139.   str = " "+str;    // Add pad to left of item text
  140.  
  141.   // Get the point to draw at and wrapper the graphic context for drawing use
  142.   IPoint point( event.itemRect().minXMinY() );
  143.   point.setY(point.y() + 2);                         // Vertically center
  144.   IGraphicContext gc(event.itemPresSpaceHandle());
  145.  
  146.   // If we should highlight this, change color to red
  147.   if (highlight)
  148.     gc.setPenColor( IColor::red );
  149.  
  150.   // otherwise use default menu text color
  151.   else
  152.     gc.setPenColor(IGUIColor(IGUIColor::menuText));
  153.  
  154.   // Create a graphic string, starting at point with text and font desired
  155.   IGString text( str, point, font );
  156.   text.drawOn(gc);
  157.  
  158.   return true;
  159. }
  160.  
  161.  
  162. IBase::Boolean CommandHandler::command( ICommandEvent& event )
  163. {
  164.   switch(event.commandId())
  165.   {
  166.     default:
  167.     {
  168.       try {
  169.        aStatus.setText(event.commandId());
  170.       } catch (...) {}
  171.       return true;
  172.     }
  173.   }
  174. }
  175.  
  176.