home *** CD-ROM | disk | FTP | other *** search
/ Power GUI Programming with VisualAge C++ / powergui.iso / powergui / static / textcv / textcv.cpp < prev   
Encoding:
C/C++ Source or Header  |  1996-10-29  |  5.7 KB  |  200 lines

  1. //************************************************************
  2. // Static Controls - Static Text on a Canvas
  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 <ifont.hpp>
  9. #include <iframe.hpp>
  10. #include <iapp.hpp>
  11. #include <imcelcv.hpp>
  12. #include <iradiobt.hpp>
  13. #include <iselhdr.hpp>
  14. #include <istattxt.hpp>
  15. #include <istring.hpp>
  16. #include <icconst.h>
  17.  
  18. // Objects of this class support writing their text across
  19. // multiple lines when they are made a child window of a 
  20. // multicell or set canvas.  The text is wrapped to fit in a 
  21. // percentage of the parent window's width. Because this class
  22. // relies on the function IFont::texLines, it does not support
  23. // double-byte characters.
  24. class MultiLineStaticText : public IStaticText {
  25. public:
  26.   MultiLineStaticText ( unsigned long id,
  27.                         IWindow* parent,
  28.                         IWindow* owner,
  29.                         IStaticText::Style style =
  30.                           IStaticText::defaultStyle() )
  31.     : IStaticText( id, parent, owner, IRectangle(), style ),
  32.       parentFraction( 0.5 )
  33.   { 
  34.     this->disableMinimumSizeCaching();
  35.     this->setAlignment( IStaticText::topLeftWrapped ); 
  36.   }
  37. virtual MultiLineStaticText
  38.  &setFraction         ( double fraction )
  39.   {
  40.     parentFraction = (fraction > 1) ? 1 : fraction;
  41.     return *this;
  42.   }
  43. virtual double
  44.   fraction            ( ) const
  45.   { return parentFraction; }
  46. protected:
  47. virtual ISize
  48.   calcMinimumSize     ( ) const
  49.   {
  50.     unsigned long recommendedWidth =
  51.       (unsigned long)(this->parent()->size().width() * this->fraction());
  52.  
  53.     // Get the current font information to see what size the 
  54.     // text needs to be.
  55.     IFont font( this ); 
  56.     unsigned long minWidth = font.minTextWidth( this->text() );
  57.  
  58.     // At least show the longest word.
  59.     if (recommendedWidth < minWidth)
  60.        recommendedWidth = minWidth;
  61.  
  62.     unsigned long lines =
  63.        font.textLines( this->text(), recommendedWidth );
  64.  
  65.     return ISize( recommendedWidth,
  66.                   lines * font.maxCharHeight() );
  67.   }
  68. private:
  69. double
  70.   parentFraction;
  71. MultiLineStaticText ( const MultiLineStaticText&);
  72. MultiLineStaticText& operator= ( const MultiLineStaticText&);
  73. };
  74.  
  75. // This class is used to dynamically change the text of a
  76. // static text control to help illustrate the benefits of
  77. // using the IStaticText::setLimit function.
  78. class HumptyDumptySelectHandler : public ISelectHandler {
  79. public:
  80.   HumptyDumptySelectHandler ( IStaticText* outputArea )
  81.   { output = outputArea; }
  82. protected:
  83. virtual Boolean
  84.   selected      ( IControlEvent& event )
  85.   {
  86.     char* text = " ";
  87.     switch ( event.controlWindow()->id() )
  88.     {
  89.        case 1:
  90.          text = "Humpty Dumpty sat on a wall.";
  91.          break;
  92.        case 2:
  93.          text = "Humpty Dumpty had a great fall.";
  94.          break;
  95.        case 3:
  96.          text = "All the King's horses and all the King's men,";
  97.          break;
  98.        case 4:
  99.          text = "Couldn't put Humpty together again.";
  100.          break;
  101.        default:
  102.          break;
  103.     }
  104.     if (output)
  105.     {
  106.        output->setText( text );
  107.     }
  108.     return false;
  109.   }
  110. private:
  111. IStaticText
  112.  *output;
  113. };
  114.  
  115. void main ( )
  116. {
  117.   IFrameWindow frame( "Static Text and Canvas Example" );
  118.   IMultiCellCanvas client( IC_FRAME_CLIENT_ID, &frame, &frame );
  119.   frame.setClient( &client );
  120.  
  121.   // Create child windows.
  122.   IStaticText
  123.     output   ( 10, &client, &client ),
  124.     separator( 11, &client, &client );
  125.   output.setLimit( 45 );
  126.   separator
  127.    .setFillColor( IColor::black )
  128.    .setMinimumSize( ISize( 10, 2 ) );
  129.  
  130.   IMultiCellCanvas
  131.     headings( 14, &client, &client );
  132.   MultiLineStaticText
  133.     heading1( 12, &headings, &headings ),
  134.     heading2( 13, &headings, &headings );
  135.   heading1
  136.    .setFraction( 0.4 )
  137.    .setText( "Select a radio button from the group below." );
  138.   heading2
  139.    .setFraction( 0.4 )
  140.    .setText( "This text consists of several words and may"
  141.                + IString( " wrap across several lines." ));
  142.  
  143.   headings
  144.    .addToCell( &heading1,  1, 1 )
  145.    .addToCell( &heading2,  3, 1 );
  146.   headings
  147.    .setColumnWidth( 1, 10, true )
  148.    .setColumnWidth( 2, 10, true )
  149.    .setColumnWidth( 3, 10, true );
  150.  
  151.   IRadioButton
  152.     none  ( 0, &client, &client ),
  153.     first ( 1, &client, &client ),
  154.     second( 2, &client, &client ),
  155.     third ( 3, &client, &client ),
  156.     fourth( 4, &client, &client );
  157.   none
  158.    .setText( "None" )
  159.    .enableTabStop()
  160.    .enableGroup();
  161.   first.setText( "First" );
  162.   second.setText( "Second" );
  163.   third.setText( "Third" );
  164.   fourth.setText( "Fourth" );
  165.  
  166.   client
  167.    .addToCell( &headings,  2, 2, 2 )
  168.    .addToCell( &none,      2, 4 )
  169.    .addToCell( &first,     2, 6 )
  170.    .addToCell( &second,    2, 8 )
  171.    .addToCell( &third,     2, 10 )
  172.    .addToCell( &fourth,    2, 12 )
  173.    .addToCell( &separator, 2, 14, 2 )
  174.    .addToCell( &output,    2, 15, 2 );
  175.   client
  176.    .setColumnWidth( 3, 0, true )
  177.    .setColumnWidth( 4,
  178.                     IMultiCellCanvas::defaultCell().width() );
  179.  
  180.   unsigned long defaultHeight =
  181.                   IMultiCellCanvas::defaultCell().height();
  182.   client
  183.    .setRowHeight( 1,  defaultHeight, true )
  184.    .setRowHeight( 3,  defaultHeight, true )
  185.    .setRowHeight( 13, defaultHeight, true )
  186.    .setRowHeight( 16, defaultHeight, true );
  187.  
  188.   HumptyDumptySelectHandler selHdr( &output );
  189.   selHdr.handleEventsFor( &client );
  190.  
  191.   // Size and show the window now.
  192.   frame
  193.    .moveSizeToClient( IRectangle( IPoint( 50, 50 ),
  194.                                   client.minimumSize() ))
  195.    .setFocus()
  196.    .show();
  197.  
  198.   IApplication::current().run();
  199. }
  200.