home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / ocl_con2.zip / ocl_con2.cpp < prev    next >
C/C++ Source or Header  |  1997-11-02  |  5KB  |  146 lines

  1. // Stéphane Charette, charette@writeme.com
  2. // IBM Open Class Library Custom Drawing Container Sample
  3. // (using Visual Age C++ v3.0 with OS/2 v4.0)
  4. // 1997Nov02
  5.  
  6. // includes
  7. #include "ocl_con2.hpp"
  8.  
  9. // defines and includes needed to "paint" text at a specified location
  10. #define INCL_WIN
  11. #define INCL_PM
  12. #include <os2.h>
  13.  
  14.  
  15. // class constructor
  16. MyApplicationClass::MyApplicationClass() :
  17.    IFrameWindow(  IResourceId(0x1000),
  18.                   IFrameWindow::classDefaultStyle  |
  19.                   IFrameWindow::animated           |
  20.                   IFrameWindow::dialogBackground   ),
  21.    container(     0x1002, this, this               ),
  22.    col1(          offsetof( MyCnrObject, first_name) ),
  23.    col2(          offsetof( MyCnrObject, last_name ) )
  24. {
  25.    // setup some basic container attributes
  26.    container.  setMultipleSelection().
  27.                setExtendedSelection().
  28.                showDetailsViewTitles().
  29.                setDeleteColumnsOnClose( false ).   // <-- cols are in the class
  30.                setDeleteObjectsOnClose().
  31.                showDetailsView().
  32.                enableDrawItem();    // <-- required for custom-draw containers
  33.  
  34.    // set the headings for the container columns
  35.    col1. setHeadingText( "First Name" ).
  36.          showSeparators();
  37.    col2. setHeadingText( "Last Name" ).
  38.          showSeparators( IContainerColumn::horizontalSeparator );
  39.  
  40.    // add the columns to the container
  41.    container.addColumn( &col1 ).addColumn( &col2 );
  42.  
  43.    // put some fake data in the container
  44.    // note that the colour values I used are some of the built-in
  45.    // definitions that are available when using #include <os2.h>:
  46.    //    CLR_DEFAULT    CLR_BLACK      CLR_BLUE       CLR_RED
  47.    //    CLR_PINK       CLR_GREEN      CLR_CYAN       CLR_YELLOW
  48.    //    CLR_BROWN      CLR_DARKGRAY   CLR_DARKBLUE   CLR_DARKRED
  49.    //    CLR_DARKPINK   CLR_DARKGREEN  CLR_DARKCYAN   CLR_PALEGRAY
  50.    container.addObject( new MyCnrObject( "Luc",    "Fontaine", CLR_WHITE   ) );
  51.    container.addObject( new MyCnrObject( "David",  "LeBlanc",  CLR_PINK    ) );
  52.    container.addObject( new MyCnrObject( "Steph",  "Charette", CLR_RED     ) );
  53.    container.addObject( new MyCnrObject( "Suzie",  "Falkner",  CLR_WHITE   ) );
  54.    container.addObject( new MyCnrObject( "Lucy",   "Beattle",  CLR_DARKRED ) );
  55.    container.addObject( new MyCnrObject( "Amy",    "Thurston", CLR_BLUE    ) );
  56.    container.addObject( new MyCnrObject( "Frank",  "Jones",    CLR_WHITE   ) );
  57.    container.addObject( new MyCnrObject( "Kevin",  "Woods",    CLR_GREEN   ) );
  58.    container.addObject( new MyCnrObject( "Jenny",  "Marks",    CLR_DEFAULT ) );
  59.    container.addObject( new MyCnrObject( "Gene",   "Moody",    CLR_WHITE   ) );
  60.    container.refresh();
  61.  
  62.    // tell the frame window that the container will be the main client
  63.    setClient( &container ).show().setFocus();
  64.  
  65.    // setup container draw handler
  66.    ICnrDrawHandler::handleEventsFor( &container );
  67.    return;
  68. }
  69.  
  70.  
  71. MyApplicationClass::drawDetailsItem( ICnrDrawItemEvent &event )
  72. {
  73.    // get the object being drawn
  74.    MyCnrObject *myObj = (MyCnrObject*)event.object();
  75.  
  76.    if( myObj == 0 )
  77.    {
  78.       // titles are being drawn -- let OS/2 take care of that,
  79.       // even though we could do it ourselves if we wanted to
  80.       // -- do nothing --
  81.    }
  82.    else
  83.    {
  84.       // get the rectangle that is to be repainted
  85.       RECTL rect = event.itemRect().asRECTL();
  86.  
  87.       // lets pretend this is a special case -- is this the object "Frank?"
  88.       if( myObj->first_name == "Frank" )
  89.       {
  90.          // are we drawing the first column?
  91.          if( event.column() == &col1 )
  92.          {
  93.             // draw the first name in cyan
  94.             WinFillRect( event.itemPresSpaceHandle(), &rect, CLR_CYAN );
  95.          }
  96.          else
  97.          {
  98.             // draw the last name in yellow
  99.             WinFillRect( event.itemPresSpaceHandle(), &rect, CLR_YELLOW );
  100.          }
  101.       }
  102.       else
  103.       {
  104.          // what colour should we use to draw the background?
  105.  
  106.          // get the colour from the object -- we specified it when the object
  107.          // was created during the class constructor
  108.  
  109.          // instead, we could be checking the state of some boolean flag
  110.          // (or whatever) and then selecting the colour to use; for example,
  111.          // if an object is marked as OVERDUE_FEES_DUE instead of NO_FEES_DUE
  112.          // then the colour might be red instead of green, etc...
  113.  
  114.          // paint the rectangle!
  115.          WinFillRect( event.itemPresSpaceHandle(), &rect, myObj->colour );
  116.       }
  117.    }
  118.  
  119.    // return the event as having been unhandled -- this will ensure
  120.    // that OS/2 will now draw the text.  If we set/return TRUE instead,
  121.    // then we also have to draw the text ourselves
  122.    event.setResult( false );
  123.    return false;
  124. }
  125.  
  126.  
  127. MyApplicationClass::~MyApplicationClass()
  128. {
  129.    // stop handling events
  130.    ICnrDrawHandler::stopHandlingEventsFor( &container );
  131.    return;
  132. }
  133.  
  134.  
  135. int main()
  136. {
  137.    // create an instance of the test dialog...
  138.    MyApplicationClass *application = new MyApplicationClass();
  139.    // run it...
  140.    IApplication::current().run();
  141.    // delete it...
  142.    delete( application );
  143.    return 0;
  144. }
  145.  
  146.