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

  1. //************************************************************
  2. // Fonts - Using Advanced Font Functions
  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 <iframe.hpp>
  12. #include <igrafctx.hpp>
  13. #include <iglist.hpp>
  14. #include <igstring.hpp>
  15. #include <ipoint.hpp>
  16. #include <istring.hpp>
  17. #include "advfont.hpp"
  18.  
  19. // Specific font names vary between operating systems,
  20. // complicating the issue of writing portable applications.
  21. // Avoid hardcoding the names in the code as follows:
  22. #ifdef IC_PM
  23. #define OS_FONT_1   "Helvetica"
  24. #else  // Windows
  25. #define OS_FONT_1   "Arial"
  26. #endif
  27.  
  28. IColor colArray[8] = { IColor::blue, IColor::red, IColor::green,
  29.                        IColor::black, IColor::cyan, IColor::yellow,
  30.                        IColor::pink, IColor::darkGray };
  31. IPoint ptArray[16] = { IPoint(0,4), IPoint(1,3), IPoint(2,2),
  32.                        IPoint(3,1), IPoint(4,0), IPoint(3,-1),
  33.                        IPoint(2,-2), IPoint(1,-3), IPoint(0,-4),
  34.                        IPoint(-1,-3), IPoint(-2,-2), IPoint(-3,-1),
  35.                        IPoint(-4,0), IPoint(-3,1), IPoint(-2,2),
  36.                        IPoint(-1,3) };
  37.  
  38. void main()
  39. {
  40.   // Create the frame and a canvas client to draw on.
  41.   IFrameWindow aFrame( "Using Advanced Font Functions" );
  42.   FontDrawingArea aClient( IC_FRAME_CLIENT_ID,
  43.                            &aFrame, &aFrame );
  44.   aFrame.setClient( &aClient );
  45.  
  46.   // Size and show the frame and run the application.
  47.   aFrame
  48.     .moveSizeToClient( aClient.rect() )
  49.     .setFocus()
  50.     .show();
  51.   IApplication::current().run();
  52. }
  53.  
  54.  
  55. /*------------------------------------------------------------------------------
  56. | FontDrawingArea - Construct an IDrawing canvas and paint the pattern         |
  57. |                   in the middle, adding all graphic strings to the list.     |
  58. ------------------------------------------------------------------------------*/
  59. FontDrawingArea::FontDrawingArea( unsigned long windowId, IWindow* parent,
  60.                                   IWindow* owner, const IRectangle& initial )
  61.   : IDrawingCanvas( windowId, parent, owner, initial,
  62.                     IDrawingCanvas::defaultStyle() | IWindow::clipSiblings )
  63. {
  64.   // Initialize string with a catchy phrase.
  65.   IString str( "PowerGUI Programming is COOL!" );
  66.  
  67.   // Get the graphic context of the canvas for drawing.
  68.   IGraphicContext gc( this->handle() );
  69.  
  70.   // Create a graphic bundle for setting colors.
  71.   IGraphicBundle gb(gc);
  72.  
  73.   // Create a graphic list for saving graphic strings.
  74.   this->setGraphicList( new IGList() );
  75.  
  76.   // Create a font object, providing name and point size.
  77.   IFont font( OS_FONT_1, 14, false, true );
  78.   font.setBold();
  79.  
  80.   // Calculate the size of the displayed string in this font
  81.   // and size the window accordingly, adding a buffer each way.
  82.   unsigned long stringSize = font.textWidth( str );
  83.   IRectangle drawRect( IPoint( 20, 30 ),
  84.                        ISize( 2*stringSize+30, 2*stringSize+30 ));
  85.   this->moveSizeTo( drawRect );
  86.  
  87.   // Get the center point of the font drawing area.
  88.   IPoint point( drawRect.centerXCenterY() );
  89.   point -= IPoint( 20, 20 );
  90.  
  91.   // Iterate through the list of angles and colors, changing the font
  92.   // and graphic attributes and build a list of graphic strings drawn
  93.   // at that angle with those colors.
  94.   int i;
  95.   for ( i=0; i<16; i++ )
  96.   {
  97. #ifdef IC_WIN
  98.     font.setFontShear( ptArray[i] );
  99. #endif
  100.     font.setFontAngle( ptArray[i] );
  101.     gb.setPenColor( colArray[i%8] );
  102.  
  103.     // Create a graphic string, starting at point
  104.     // with text and font you want.
  105.     IGString* text = new IGString( str, point, font );
  106.     text->setGraphicBundle( gb );
  107.     text->drawOn(gc);
  108.  
  109.     // Add the graphic string (with attributes) to the list.
  110.     this->graphicList()->addAsLast( *text );
  111.   }
  112. }
  113.  
  114.  
  115. /*------------------------------------------------------------------------------
  116. | FontDrawingArea - Destroy the IDrawing canvas and free all of the graphic    |
  117. |                   strings in the list and the list itself.                   |
  118. ------------------------------------------------------------------------------*/
  119. FontDrawingArea::~FontDrawingArea( )
  120. {
  121.   // Delete all the graphic objects in the drawing canvas.
  122.   IGList::Cursor graphicsCursor( *graphicList() );
  123.   for ( graphicsCursor.setToFirst();
  124.         graphicsCursor.isValid();
  125.         graphicsCursor.setToNext() )
  126.   {
  127.     IGraphic* graphic(&(graphicList()->graphicAt(graphicsCursor)));
  128.     delete graphic;
  129.   } /* endfor */
  130.   delete graphicList();
  131. }
  132.  
  133.  
  134.