home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 1998 May / Pcwk5b98.iso / Borland / Cplus45 / BC45 / GDIDEMO.PAK / FONTX.CPP < prev    next >
C/C++ Source or Header  |  1995-08-29  |  4KB  |  153 lines

  1. //----------------------------------------------------------------------------
  2. // ObjectWindows - (C) Copyright 1991, 1993 by Borland International
  3. //   Font demo window for GDIDemo program
  4. //----------------------------------------------------------------------------
  5. #include <owl\owlpch.h>
  6. #include <owl\applicat.h>
  7. #include <owl\dc.h>
  8. #include <owl\scroller.h>
  9. #include <string.h>
  10. #include "fontx.h"
  11.  
  12. struct FontInfoRec {
  13.   TFont* Font;     // Logical font object
  14.   int    Height;   // Height of logical font in pixels
  15.   int    Width;    // Width of name of the font in pixels
  16.   char   Name[LF_FACESIZE];  // Name of this font
  17. };
  18.  
  19. // local variables used by EnumerateFonts callback function
  20. //
  21. static int         FontUsers = 0;
  22. static FontInfoRec FontInfo[MaxNumFonts];
  23. static int         NumFonts;            // Number of system fonts available
  24. static TDC*        TheDC = 0;
  25.  
  26. // EnumerateFont is a call back function.  It receives information
  27. //  about system fonts.  It creates an example of each font by calling
  28. //  CreateFont. When MaxNumFonts have been processed, 0 is returned
  29. //  notifying windows to stop sending information, otherwise 1 is
  30. //  returned telling windows to send more information if available
  31. //
  32. #if defined(__WIN32__)
  33. int __stdcall
  34. #else
  35. int far pascal _export
  36. #endif
  37. EnumerateFont(LOGFONT far* logFont, TEXTMETRIC far*, int, LPARAM)
  38. {
  39.   // Create the font described by logFont
  40.   //
  41.   FontInfo[NumFonts].Font = new TFont(logFont);
  42.  
  43.   // Save the height of the font for positioning when drawing in the window
  44.   //
  45.   FontInfo[NumFonts].Height = logFont->lfHeight;
  46.  
  47.   // Save the name of the font for drawing in the window
  48.   //
  49.   strcpy(FontInfo[NumFonts].Name, logFont->lfFaceName);
  50.   TheDC->SelectObject(*FontInfo[NumFonts].Font);
  51.  
  52.   TSize extent(0,0);
  53.   TheDC->GetTextExtent(logFont->lfFaceName, strlen(logFont->lfFaceName),
  54.                        extent);
  55.   FontInfo[NumFonts].Width = extent.cx;
  56.   TheDC->RestoreFont();
  57.   NumFonts++;
  58.  
  59.   return NumFonts >= MaxNumFonts ?
  60.       0 :      // Don't send any more information, the array is full
  61.       1;       // Send more information if available
  62. }
  63.  
  64. // Collect all of the system fonts
  65. //
  66. void
  67. GetFontInfo()
  68. {
  69.   if (FontUsers == 0) {
  70.     TheDC = new TScreenDC;
  71.     NumFonts = 0;
  72.  
  73.     // Create an instance of the call back function.  This allows
  74.     // our program to refer to an exported function.  Otherwise the
  75.     // Data segment will not be correct. This is a no-op in 32bit.
  76.     //
  77.     TProcInstance enumProc((FARPROC)EnumerateFont);
  78.  
  79.     // Gather information about all fonts that are allowable in our DC
  80.     //
  81.     EnumFonts(*TheDC, 0, (OLDFONTENUMPROC)(FARPROC)enumProc, 0);
  82.  
  83.     delete TheDC;
  84.     TheDC = 0;
  85.   }
  86.   FontUsers++;
  87. }
  88.  
  89. // Release font information
  90. //
  91. void
  92. ReleaseFontInfo()
  93. {
  94.   FontUsers--;
  95.   if (FontUsers == 0)
  96.     for (int i = 0; i < NumFonts; i++) {
  97.       delete FontInfo[i].Font;
  98.       FontInfo[i].Font = 0;
  99.     }
  100. }
  101.  
  102. // TFontWindow ----------------------------------------------------
  103.  
  104. DEFINE_RESPONSE_TABLE1(TFontWindow, TBaseDemoWindow)
  105.   EV_WM_SIZE,
  106. END_RESPONSE_TABLE;
  107.  
  108. IMPLEMENT_CASTABLE1(TFontWindow, TBaseDemoWindow);
  109.  
  110. // Initialize object and collect font information
  111. //
  112. TFontWindow::TFontWindow() : TBaseDemoWindow()
  113. {
  114.   GetFontInfo();
  115.   Attr.Style |= WS_VSCROLL | WS_HSCROLL;
  116.   FontsHeight = 0;
  117.   FontsWidth = 0;
  118.   for (int i = 0; i < NumFonts; i++) {
  119.     FontsHeight += FontInfo[i].Height;
  120.     if (FontsWidth < FontInfo[i].Width)
  121.       FontsWidth = FontInfo[i].Width;
  122.   }
  123.   Scroller = new TScroller(this, 1, 1, 0, 0);
  124. }
  125.  
  126. TFontWindow::~TFontWindow()
  127. {
  128.   ReleaseFontInfo();
  129. }
  130.  
  131. // Draw each font name in it's font in the Display context.  Each
  132. //  line is incremented by the height of the font
  133. //
  134. void
  135. TFontWindow::Paint(TDC& dc, BOOL, TRect&)
  136. {
  137.   TPoint position(10,0);
  138.   for (int i = 0; i < NumFonts; i++) {
  139.     dc.SelectObject(*FontInfo[i].Font);
  140.     dc.TextOut(position, FontInfo[i].Name, strlen(FontInfo[i].Name));
  141.     position.Offset(0, FontInfo[i].Height);
  142.   }
  143. }
  144.  
  145. void
  146. TFontWindow::EvSize(UINT SizeType, TSize& Size)
  147. {
  148.   TBaseDemoWindow::EvSize(SizeType, Size);
  149.   if (Scroller)
  150.     Scroller->SetRange(FontsWidth - Size.cx + 10,
  151.                        FontsHeight - Size.cy);
  152. }
  153.