home *** CD-ROM | disk | FTP | other *** search
/ Power GUI Programming with VisualAge C++ / powergui.iso / powergui / toolbar / cbuthdr / graphbut.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1996-10-29  |  4.0 KB  |  133 lines

  1. //************************************************************
  2. // Tool Bars  - Custom Button Handler 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 <igraphic.hpp>
  9. #include <igelipse.hpp>
  10. #include "graphbut.hpp"
  11.  
  12.  
  13.  
  14. // GraphicButton Constructor
  15. GraphicButton::GraphicButton (unsigned long identifier,
  16.                          IWindow*      parent,
  17.                          IWindow*      owner,
  18.                          const ISize&   graphicSize,
  19.                          IGraphic*       upGraphic,
  20.                          IGraphic*       downGraphic)
  21.      : ICustomButton(identifier, parent, owner),
  22.        fUpGraphic (upGraphic),
  23.        fDownGraphic (downGraphic),
  24.        fGraphicButtonHandler(new GraphicButtonHandler()),
  25.        fGraphicSize(graphicSize)
  26. {
  27.  
  28. // Attach the graphic button's handler.
  29. fGraphicButtonHandler->handleEventsFor(this);
  30.  
  31. // Create the default graphics, if necessary.
  32. IGraphicBundle bundle;
  33. if (fUpGraphic == 0)
  34. {
  35.   fUpGraphic = (IGraphic*)new IGEllipse(IRectangle(IPoint(0,0), fGraphicSize));
  36.   bundle.setFillColor(IColor::red);
  37.   fUpGraphic->setGraphicBundle(bundle);
  38. }
  39. if (fDownGraphic == 0)
  40. {
  41.   fDownGraphic = (IGraphic*)new IGEllipse(IRectangle(IPoint(0,0), fGraphicSize));
  42.   bundle.setFillColor(IColor::green);
  43.   fDownGraphic->setGraphicBundle(bundle);
  44. }
  45.  
  46. }
  47.  
  48. // GraphicButton Destructor
  49. GraphicButton::~GraphicButton ( )
  50. {
  51.    delete fUpGraphic;
  52.    delete fDownGraphic;
  53.    delete fGraphicButtonHandler;
  54.  
  55. }
  56.  
  57. // Return the unlatched graphic.
  58. IGraphic* GraphicButton::upGraphic ( ) const
  59. {
  60.   return fUpGraphic;
  61. }
  62.  
  63. // Return the latched graphic.
  64. IGraphic* GraphicButton::downGraphic ( ) const
  65. {
  66.   return fDownGraphic;
  67. }
  68.  
  69.  
  70. //  Calculate the size of the graphic button (including
  71. // the base button and the graphic).
  72. ISize GraphicButton::calcMinimumSize ( ) const
  73. {
  74.   IGraphicContext gc(this->handle());
  75.   ISize buttonSize = ICustomButton::calcMinimumSize();
  76.   unsigned long 
  77.     upGraphicHeight = fUpGraphic->boundingRect(gc).size().height();
  78.   unsigned long 
  79.     downGraphicHeight = fDownGraphic->boundingRect(gc).size().height();
  80.   if (upGraphicHeight > downGraphicHeight)
  81.     buttonSize += ISize(0, upGraphicHeight);
  82.   else
  83.     buttonSize += ISize(0, downGraphicHeight);
  84.   return buttonSize;
  85. }
  86.  
  87. // Draw the foreground of the button.
  88. void GraphicButtonHandler::drawForeground (ICustomButtonDrawEvent& event)
  89. {
  90.   // Store the graphic context.
  91.   IGraphicContext& gc = event.graphicContext();
  92.  
  93.   // Determine and set the recoordination height so that the
  94.   // code is portable.  (We draw in Windows coordinates here.)
  95.   GraphicButton* graphicButton = (GraphicButton*)event.customButton();
  96.   IRectangle drawingRect = event.drawingArea();
  97. #ifndef IC_PM // Remove #ifndef when PM supports this.
  98.   gc.setRecoordinationHeight(graphicButton->size().height());
  99. #endif
  100.  
  101.   // Determine which graphic to draw.
  102.   IGraphic* graphic;
  103.   if (event.drawUp() == true)
  104.     graphic = graphicButton->upGraphic();
  105.   else
  106.     graphic = graphicButton->downGraphic();
  107.  
  108.   // Determine the size of the graphic.
  109.   IPoint graphicPos;
  110.   ISize  graphicSize;
  111.   graphicSize = graphic->boundingRect(gc).size();
  112.  
  113.   // Center graphic horizontally.
  114.   graphicPos.setX(drawingRect.minX()+((drawingRect.width()-graphicSize.width())/2));
  115.   graphicPos.setY(drawingRect.minY()+2);
  116.  
  117.   // Draw the graphic.
  118.   graphic->translateBy(graphicPos);
  119.   graphic->drawOn(gc);
  120.   graphic->translateBy(-graphicPos);
  121.  
  122.   // Update the drawing area so that it only includes the
  123.   // space below our graphic (where we want the text drawn).
  124.   drawingRect.sizeTo(
  125.          ISize(drawingRect.width(),
  126.                drawingRect.height()-graphicSize.height()));
  127.   drawingRect.moveBy(ISize(0,graphicSize.height()));
  128.   event.setDrawingArea(drawingRect);
  129.  
  130.   // Call our inherited class to draw the text. 
  131.   Inherited::drawForeground(event);
  132. }
  133.