home *** CD-ROM | disk | FTP | other *** search
/ Power GUI Programming with VisualAge C++ / powergui.iso / powergui / listctls / drawlist / listdhdr.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1996-10-29  |  5.0 KB  |  174 lines

  1. /************************************************************
  2. / List Controls - List Box Custom Drawing
  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 <ilistbox.hpp>
  9. #include <irect.hpp>
  10. #include <ifont.hpp>
  11. #include <icolor.hpp>
  12. #include <igbitmap.hpp>
  13. #include <igstring.hpp>
  14. #include <igrafctx.hpp>
  15. #include <igrect.hpp>
  16. #include <igbundle.hpp>
  17. #include <istring.hpp>
  18. #include "listdhdr.hpp"
  19. #include "listitem.hpp"
  20.  
  21. DrawHandler::DrawHandler ( unsigned long indentBits )
  22.      :   fIndent( indentBits ),
  23.          fBitmapIndent ( 0 ),
  24.          fMaxCharHeight ( 0 ) ,
  25.          fInternalLeading( 0 ) ,
  26.          fExternalLeading( 0 ) ,
  27.          fCollapseHandle (ISystemBitmapHandle::collapsedTree),
  28.          fExpandHandle (ISystemBitmapHandle::expandedTree),
  29.          fCollapseBitmap(new IGBitmap( fCollapseHandle) ),
  30.          fExpandBitmap( new IGBitmap ( fExpandHandle) )
  31. {
  32. }
  33.  
  34.  
  35.  
  36. DrawHandler::setItemSize   ( IListBoxSizeItemEvent& event )
  37. {
  38.   if(fMaxCharHeight==0)
  39.   {
  40.     IListBox* list = (IListBox*)event.controlWindow();
  41.     IFont font( list );
  42.     fMaxCharHeight = font.maxCharHeight();
  43.     fExternalLeading = font.externalLeading();
  44.     fInternalLeading = font.internalLeading();
  45.     unsigned long itemHeight = fMaxCharHeight+fExternalLeading+fInternalLeading;
  46.     unsigned long bitmapHeight = 16;
  47.     fCollapseBitmap->sizeTo(ISize(bitmapHeight,bitmapHeight));
  48.     fExpandBitmap->sizeTo(ISize(bitmapHeight,bitmapHeight));
  49.     fBitmapIndent = (itemHeight-bitmapHeight)/2;
  50.   }
  51.   event.setItemSize(ISize(0,fMaxCharHeight+fExternalLeading+fInternalLeading));
  52.   return true;
  53. }
  54.  
  55. // Handle selecting and item.
  56. Boolean DrawHandler::selectItem ( IListBoxDrawItemEvent& event )
  57. {
  58.   // Draw with selection.
  59.   event.setSelectionStateDrawn(true);
  60.   return this->drawListItem( event, true);
  61. }
  62.  
  63. // Handle deselecting an item.
  64. Boolean DrawHandler::deselectItem ( IListBoxDrawItemEvent& event )
  65. {
  66.   // Draw without selection.
  67.   event.setSelectionStateDrawn(true);
  68.   return this->drawListItem( event, false );
  69. }
  70.  
  71. // Handle the drawing of the list box items.
  72. Boolean DrawHandler::drawItem ( IListBoxDrawItemEvent& event )
  73. {
  74.   if ( event.isSelected() )
  75.   {
  76.     event.setSelectionStateDrawn(true);
  77.     return this->drawListItem( event, true );
  78.   }
  79.   else
  80.     return this->drawListItem( event, false );
  81.  
  82.  
  83. }
  84.  
  85. // Handle the drawing of the list box items.
  86. Boolean DrawHandler::drawListItem ( IListBoxDrawItemEvent& event,
  87.                                     Boolean selected)
  88. {
  89.   IListBox* list = (IListBox*)event.controlWindow();
  90.   IRectangle rect = event.itemRect();
  91.  
  92.   // Fill the background.
  93.   IColor
  94.     background         = list->backgroundColor(),
  95.     foreground         = list->foregroundColor(),
  96.     selectedBackground = list->hiliteBackgroundColor(),
  97.     selectedForeground = list->hiliteForegroundColor();
  98.  
  99.   IGraphicContext graphicContext(list->handle());
  100.   IGraphicBundle bundle(graphicContext);
  101.  
  102.   if(!selected)
  103.   {
  104.     bundle.setFillColor(background);
  105.     bundle.setPenColor(background);
  106.     bundle.setBackgroundColor(background);
  107.   }
  108.   else
  109.   {
  110.     bundle.setFillColor(selectedBackground);
  111.     bundle.setPenColor(selectedBackground);
  112.     bundle.setBackgroundColor(selectedBackground);
  113.   }
  114.   IGRectangle graphicRectangle(rect);
  115.   graphicRectangle.setGraphicBundle(bundle);
  116.   graphicRectangle.drawOn(graphicContext);
  117.  
  118.  
  119.   // Convert the item data from the list box item text.
  120.   ListItem* data =
  121.      (ListItem*)(list->itemText( event.itemId() ).asUnsigned());
  122.  
  123.   // Determine the indent for the class based on the
  124.   // number of inherited classes.
  125.   if ( data->inheritedClasses() != 0 )
  126.   {
  127.      ISize indentSize( data->inheritedClasses() * (fIndent+1), 0 );
  128.      rect.moveBy( indentSize );
  129.   }
  130.  
  131.   // Draw a bitmap based on the collapsed flag
  132.   // if the class is not a leaf.
  133.   if (data->children() )
  134.   {
  135.      IGBitmap* currentBitmap;
  136.      if(data->collapsed())
  137.        currentBitmap=fCollapseBitmap;
  138.      else
  139.        currentBitmap=fExpandBitmap;
  140.  
  141.      currentBitmap->moveTo(rect.minXMinY()+ISize(fBitmapIndent, fBitmapIndent));
  142.      bundle.setPenColor(foreground);
  143.      currentBitmap->setGraphicBundle(bundle);
  144.      currentBitmap->drawOn(graphicContext);
  145.   }
  146.  
  147.  
  148.   // Setup the bundle for text
  149.   if(selected)
  150.   {
  151.     bundle.setFillColor(selectedBackground);
  152.     bundle.setPenColor(selectedForeground);
  153.     bundle.setBackgroundColor(selectedBackground);
  154.   }
  155.   else
  156.   {
  157.     bundle.setFillColor(background);
  158.     bundle.setPenColor(foreground);
  159.     bundle.setBackgroundColor(background);
  160.   }
  161.  
  162.   rect
  163.    .moveBy( ISize(fCollapseBitmap->size().width()+fBitmapIndent + 3, 3) )
  164.    .sizeTo( ISize( rect.size().width(),
  165.                    fMaxCharHeight ));
  166.  
  167.   // Draw the text.
  168.   IGString graphicString(data->text(), rect.minXMinY()+2);
  169.   graphicString.setGraphicBundle(bundle);
  170.   graphicString.drawOn(graphicContext);
  171.  
  172.   return true;
  173. }
  174.