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

  1. //************************************************************
  2. // Views - Using the Font Dialog
  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 <iapp.hpp>
  9. #include <icolor.hpp>
  10. #include <ifont.hpp>
  11. #include <ifontdlg.hpp>
  12. #include <igrafctx.hpp>
  13. #include <iglist.hpp>
  14. #include <igstring.hpp>
  15. #include <ipoint.hpp>
  16. #include <istring.hpp>
  17. #include "fontdlg.hpp"
  18. #include "fontdlg.h"
  19.  
  20. // Specific font names vary between operating systems,
  21. // complicating the issue of writing portable applications.
  22. // Avoid hardcoding the names in the code as follows:
  23. #ifdef IC_PM
  24. #define OS_FONT_1   "Helvetica"
  25. #else  // Windows
  26. #define OS_FONT_1   "Arial"
  27. #endif
  28.  
  29. IColor colArray[8] = { IColor::blue, IColor::red, IColor::green, IColor::black,
  30.                        IColor::cyan, IColor::yellow, IColor::pink, IColor::darkGray };
  31. IPoint ptArray[16] = { IPoint(0,4), IPoint(1,3), IPoint(2,2), IPoint(3,1),
  32.                        IPoint(4,0), IPoint(3,-1), IPoint(2,-2), IPoint(1,-3),
  33.                        IPoint(0,-4), IPoint(-1,-3), IPoint(-2,-2), IPoint(-3,-1),
  34.                        IPoint(-4,0), IPoint(-3,1), IPoint(-2,2), IPoint(-1,3) };
  35.  
  36. void main()
  37. {
  38.   // Create the font dialog example window.
  39.   FontDialogExample aFrame( "Using the Font Dialog" );
  40.  
  41.   // Show the example window and run the application.
  42.   aFrame
  43.     .setFocus()
  44.     .show();
  45.   IApplication::current().run();
  46. }
  47.  
  48.  
  49. /*------------------------------------------------------------------------------
  50. | FontDialogExample                                                            |
  51. |   Construct the frame, initial font and drawing area and attach              |
  52. |   the command handler.  Size the frame to match the client area.             |
  53. ------------------------------------------------------------------------------*/
  54. FontDialogExample::FontDialogExample( char* title )
  55.   : IFrameWindow( title, MAIN_WINDOW,
  56.      IFrameWindow::defaultStyle() | IFrameWindow::menuBar ),
  57.     currentFont( OS_FONT_1, 14, false, true ),
  58.     currentArea( IC_FRAME_CLIENT_ID, this, this, currentFont )
  59. {
  60.   this->setClient( ¤tArea );
  61.   handleEventsFor( this );
  62.   this->moveSizeToClient( currentArea.rect() );
  63. }
  64.  
  65.  
  66. /*------------------------------------------------------------------------------
  67. | FontDialogExample::updateDemo                                                |
  68. |   Update the font drawing area with the new font.                            |
  69. ------------------------------------------------------------------------------*/
  70. FontDialogExample& FontDialogExample::updateDemo( IFont& newFont )
  71. {
  72.   currentArea.disableUpdate();
  73.   currentArea.refreshList( newFont );
  74.   currentArea.enableUpdate();
  75.   this->moveSizeToClient( currentArea.rect() );
  76.  
  77.   return *this;
  78. }
  79.  
  80.  
  81. /*------------------------------------------------------------------------------
  82. | FontDialogExample::command                                                   |
  83. |   Handle menu commands.                                                      |
  84. ------------------------------------------------------------------------------*/
  85. IBase::Boolean FontDialogExample::command( ICommandEvent& cmdEvent )
  86. {
  87.   switch ( cmdEvent.commandId() )
  88.   {
  89.     case MI_FONTDLG:
  90.     {
  91. #if (IC_MAJOR_VERSION > 310)
  92.       // Create a font dialog handler, passing the frame
  93.       // window for use during modeless processing.
  94.       MyDlgHandler applyHandler( this );
  95. #endif
  96.       // Initialize a settings object with the current font.
  97.       IFontDialog::Settings settings( ¤tFont );
  98.  
  99.       // Set the preview text, title and initial position.
  100.       settings.setPreviewText( "PowerGUI is COOL!" );
  101.       settings.setTitle( "Select a New Font" );
  102.       settings.setPosition( IPoint( 450, 2 ) );
  103.  
  104.       // Create a modeless dialog, containing an apply button
  105.       // and only list vector fonts.  Attach the font dialog
  106.       // handler created above to handle the apply button.
  107.       IFontDialog fntDlg( IWindow::desktopWindow(), this,
  108. #if (IC_MAJOR_VERSION > 310)
  109.                           &applyHandler,
  110.                           IFontDialog::modeless |
  111. #endif
  112.                           IFontDialog::applyButton |
  113.                           IFontDialog::vectorOnly, settings );
  114.  
  115.       // Check if a user selected a font and if so, update
  116.       // the drawing area using the new font.
  117.       if (fntDlg.pressedOK())
  118.         this->updateDemo( currentFont );
  119.  
  120.       return (true);
  121.     }
  122.  
  123.     case MI_EXIT:
  124.       this->close();
  125.       return(true);
  126.   }
  127.  
  128.   return(false);
  129. }
  130.  
  131.  
  132. /*------------------------------------------------------------------------------
  133. | FontDrawingArea - Construct an IDrawing canvas and paint the pattern         |
  134. |                   in the middle, adding all graphic strings to the list.     |
  135. ------------------------------------------------------------------------------*/
  136. FontDrawingArea::FontDrawingArea( unsigned long windowId, IWindow* parent,
  137.                                   IWindow* owner, IFont& initialFont )
  138.   : IDrawingCanvas( windowId, parent, owner, IRectangle(),
  139.                     IDrawingCanvas::defaultStyle() | IWindow::clipSiblings )
  140. {
  141.   refreshList( initialFont );
  142. }
  143.  
  144.  
  145. /*------------------------------------------------------------------------------
  146. | ~FontDrawingArea - Destroy the IDrawing canvas and free all of the graphic   |
  147. |                    strings in the list and the list itself.                  |
  148. ------------------------------------------------------------------------------*/
  149. FontDrawingArea::~FontDrawingArea( )
  150. {
  151.   // Delete all the graphic objects in the drawing canvas.
  152.   IGList::Cursor graphicsCursor( *graphicList() );
  153.   for ( graphicsCursor.setToFirst();
  154.         graphicsCursor.isValid();
  155.         graphicsCursor.setToNext() )
  156.   {
  157.     IGraphic* graphic(&(graphicList()->graphicAt(graphicsCursor)));
  158.     delete graphic;
  159.   }
  160.   delete graphicList();
  161. }
  162.  
  163.  
  164. /*------------------------------------------------------------------------------
  165. | FontDrawingArea::refreshList                                                 |
  166. |   Create the graphic strings with the correct font, angles, colors and       |
  167. |   add them to the graphic list for drawing by the handler.                   |
  168. ------------------------------------------------------------------------------*/
  169. FontDrawingArea& FontDrawingArea::refreshList( IFont& newFont )
  170. {
  171.   // Delete any graphic objects in the drawing canvas.
  172.   if ( graphicList() )
  173.   {
  174.     IGList::Cursor graphicsCursor( *graphicList() );
  175.     for ( graphicsCursor.setToFirst();
  176.           graphicsCursor.isValid();
  177.           graphicsCursor.setToNext() )
  178.     {
  179.       IGraphic* graphic(&(graphicList()->graphicAt(graphicsCursor)));
  180.       delete graphic;
  181.     }
  182.     delete graphicList();
  183.   }
  184.  
  185.   // Initialize string with a catchy phrase.
  186.   IString str( "PowerGUI Programming is COOL!" );
  187.  
  188.   // Get the graphic context of the canvas for drawing.
  189.   IGraphicContext gc( this->handle() );
  190.  
  191.   // Create a graphic bundle for setting colors.
  192.   IGraphicBundle gb(gc);
  193.  
  194.   // Create a graphic list for saving graphic strings.
  195.   this->setGraphicList( new IGList() );
  196.  
  197.   IFont font( newFont );
  198.  
  199.   // Calculate the size of the displayed string in this font
  200.   // and size the window accordingly, adding a buffer each way.
  201.   unsigned long strSize = font.textWidth( str );
  202.   IRectangle drawRect( IPoint( 20, 40 ),
  203.                        ISize( 2*strSize+30, 2*strSize+30 ));
  204.   this->moveSizeTo( drawRect );
  205.  
  206.   // Get the center point of the font drawing area.
  207.   IPoint point( drawRect.centerXCenterY() );
  208.   point -= IPoint( 20, 20 );
  209.  
  210.   // Iterate through the list of angles and colors, changing the font
  211.   // and graphic attributes and build a list of graphic strings drawn
  212.   // at that angle with those colors.
  213.   int i;
  214.   for ( i=0; i<16; i++ )
  215.   {
  216. #ifdef IC_WIN
  217.     font.setFontShear( ptArray[i] );
  218. #endif
  219.     font.setFontAngle( ptArray[i] );
  220.     gb.setPenColor( colArray[i%8] );
  221.  
  222.     // Create a graphic string, starting at point
  223.     // with text and font you want.
  224.     IGString* text = new IGString( str, point, font );
  225.     text->setGraphicBundle( gb );
  226.     text->drawOn(gc);
  227.  
  228.     // Add the graphic string (with attributes) to the list.
  229.     this->graphicList()->addAsLast( *text );
  230.   }
  231.   return *this;
  232. }
  233.  
  234. #if (IC_MAJOR_VERSION > 310)
  235. /*------------------------------------------------------------------------------
  236. | MyDlgHandler::modelessApply                                                  |
  237. |   Handle the press of the apply button for the modeless font dialog.         |
  238. ------------------------------------------------------------------------------*/
  239. IBase::Boolean MyDlgHandler::modelessApply( IFontDialog* modelessDialog,
  240.                                             IFont*       appliedFont )
  241. {
  242.   fhwnd->updateDemo( *appliedFont );
  243.   return true;
  244. }
  245. #endif
  246.