home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c500 / 4.ddi / EDIT.WEX / EFONT.C < prev    next >
Encoding:
C/C++ Source or Header  |  1992-05-28  |  6.6 KB  |  257 lines

  1. /*
  2.  *%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  3.  *%                                       %
  4.  *%    Copyright (C) 1991, by WATCOM Systems Inc. All rights reserved.    %
  5.  *%                                       %
  6.  *%     Permission is granted to anyone to use this example program for       %
  7.  *%     any purpose on any computer system, subject to the following       %
  8.  *%    restrictions:                               %
  9.  *%                                       %
  10.  *%     1. This example is provided on an "as is" basis, without warranty. %
  11.  *%       You indemnify, hold harmless and defend WATCOM from and against %
  12.  *%       any claims or lawsuits, including attorney's, that arise or       %
  13.  *%       result from the use or distribution of this example, or any     %
  14.  *%       modification thereof.                       %
  15.  *%                                       %
  16.  *%     2. You may not remove, alter or suppress this notice from this       %
  17.  *%        example program or any modification thereof.               %
  18.  *%                                       %
  19.  *%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  20.  *
  21.  * EFONT.C
  22.  *
  23.  * Windows edit program: font functions
  24.  *             note that common dialog stuff could be used
  25.  *             here, but for demonstration purposes,
  26.  *             we do this anyways...
  27.  *
  28.  */
  29. #include <windows.h>
  30. #include <stdio.h>
  31. #include <string.h>
  32. #include "edit.h"
  33.  
  34. #define    FONT_DATA    1
  35. #define    FONT_SIZES    2
  36.  
  37. static LPFINFO FontHead,FontTail;
  38. static LPFINFO CurrFont;
  39.  
  40. static HFONT CFont;
  41.  
  42. /*
  43.  * MakeFont - make the current font
  44.  */
  45. static void MakeFont( HWND hwnd, int index, int size )
  46. {
  47. LPFINFO    tmp;
  48.  
  49.     if( CFont != NULL ) DeleteObject( CFont );
  50.  
  51.     tmp = FontHead;
  52.     while( tmp->index != index ) tmp = tmp->next;
  53.  
  54.     CFont = CreateFont(
  55.     tmp->sizes[ size ],
  56.     0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE,
  57.     tmp->charset,
  58.     OUT_DEFAULT_PRECIS,
  59.     CLIP_DEFAULT_PRECIS,
  60.     DEFAULT_QUALITY,
  61.     tmp->pitch_family,
  62.     tmp->name );
  63.  
  64.     if( hwnd != NULL ) {
  65.     SendDlgItemMessage( hwnd, FONT_SAMPLE, WM_SETFONT, CFont, TRUE );
  66.     }
  67.  
  68. } /* MakeFont */
  69.  
  70.  
  71. /*
  72.  * GetFont - get a new font
  73.  */
  74. BOOL _EXPORT FAR PASCAL GetFont( HWND hwnd, unsigned msg, WORD wparam,
  75.                 LONG lparam )
  76. {
  77.  
  78.     int        i;
  79.     int        sel,sel2;
  80.     char    str[128];
  81.     LPFINFO    tmp;
  82.  
  83.     switch( msg ) {
  84.     case WM_INITDIALOG:
  85.         tmp = FontHead;
  86.     while( tmp != NULL ) {
  87.         SendDlgItemMessage( hwnd, FONT_NAME, LB_ADDSTRING, NULL,
  88.                 (LONG) (LPSTR) tmp->name );
  89.         SendDlgItemMessage( hwnd, FONT_NAME, LB_SETCURSEL, 0, 0L );
  90.         tmp = tmp->next;
  91.     }
  92.     for( i=0; i < FontHead->size_count; i++ ) {
  93.         sprintf( str, "%d", FontHead->sizes[i]);
  94.         SendDlgItemMessage( hwnd, FONT_SIZE, LB_ADDSTRING,
  95.         0, (LONG) (LPSTR) str );
  96.         SendDlgItemMessage( hwnd, FONT_SIZE, LB_SETCURSEL, 0, 0L);
  97.     }
  98.     return( TRUE );
  99.     break;
  100.  
  101.     case WM_COMMAND:
  102.     if( (wparam == FONT_NAME || wparam == FONT_SIZE ) &&
  103.         HIWORD( lparam ) == LBN_DBLCLK ) {
  104.         wparam = IDOK;
  105.     }
  106.     switch( wparam ) {
  107.     case IDOK:
  108.         sel = SendDlgItemMessage( hwnd, FONT_NAME, LB_GETCURSEL, 0, 0L );
  109.         sel2 = SendDlgItemMessage( hwnd, FONT_SIZE, LB_GETCURSEL, 0, 0L );
  110.         if( sel == LB_ERR  || sel2 == LB_ERR ) {
  111.         EndDialog( hwnd, 0 );
  112.         } else {
  113.         MakeFont( NULL, sel, sel2 );
  114.         EndDialog( hwnd, 1 );
  115.         }
  116.         break;
  117.  
  118.     case IDCANCEL:
  119.         EndDialog( hwnd, 0 );
  120.         break;
  121.  
  122.     case FONT_SIZE:
  123.         if( HIWORD( lparam ) == LBN_SELCHANGE ) {
  124.         sel = SendDlgItemMessage( hwnd, FONT_NAME, LB_GETCURSEL, 0, 0L );
  125.         if( sel == LB_ERR) break;
  126.         sel2 = SendDlgItemMessage( hwnd, FONT_SIZE, LB_GETCURSEL, 0, 0L );
  127.         if( sel2 == LB_ERR ) break;
  128.         MakeFont( hwnd, sel, sel2 );
  129.         }
  130.         break;
  131.     case FONT_NAME:
  132.         if( HIWORD( lparam ) == LBN_SELCHANGE ) {
  133.         sel = SendDlgItemMessage( hwnd, FONT_NAME, LB_GETCURSEL, 0, 0L );
  134.         if( sel == LB_ERR ) break;
  135.         tmp = FontHead;
  136.         while( tmp->index != sel ) tmp = tmp->next;
  137.         SendDlgItemMessage( hwnd, FONT_SIZE, LB_RESETCONTENT, 0, 0L );
  138.         for( i=0; i< tmp->size_count; i++ ) {
  139.             sprintf( str, "%d", tmp->sizes[i] );
  140.             SendDlgItemMessage( hwnd, FONT_SIZE, LB_ADDSTRING,
  141.                 0, (LONG) (LPSTR) str );
  142.             SendDlgItemMessage( hwnd, FONT_SIZE, LB_SETCURSEL, 0, 0L );
  143.         }
  144.         MakeFont( hwnd, sel, 0 );
  145.         }
  146.         break;
  147.     }
  148.  
  149.     }
  150.     return (FALSE);
  151.  
  152. } /* GetFont */
  153.  
  154. /*
  155.  * FontSelect - select a new font
  156.  */
  157. void FontSelect( LPEDATA ed )
  158. {
  159. FARPROC fp;
  160.  
  161.     if( CFont != NULL ) DeleteObject( CFont );
  162.     CFont = NULL;
  163.  
  164.     fp = MakeProcInstance( GetFont, ed->inst );
  165.     if( DialogBox( ed->inst, "GetFont", ed->hwnd, fp ) ) {
  166.     if( ed->font != NULL ) DeleteObject( ed->font );
  167.     ed->font = CFont;
  168.     SendMessage( ed->editwnd, WM_SETFONT, ed->font, TRUE );
  169.     }
  170.     FreeProcInstance( fp );
  171.  
  172. } /* FontSelect */
  173.  
  174. /*
  175.  * EnumFontsProc - get all fonts
  176.  */
  177. int _EXPORT FAR PASCAL EnumFontsProc( LPLOGFONT logfont,
  178.             LPTEXTMETRIC textmetric, short fonttype, LPSTR data )
  179. {
  180. LOGFONT     far *farfont;
  181. LPFINFO        curr;
  182. short        _FAR *newsize;
  183.  
  184.     fonttype = fonttype;    /* shut compiler up */
  185.     textmetric = textmetric;
  186.  
  187.     switch( (WORD) data ) {
  188.     case FONT_DATA:
  189.         curr = MemAlloc( sizeof( font_info ) );
  190.     if( curr == NULL ) return( FALSE );
  191.     farfont = MK_FP32( logfont );
  192.     curr->charset = farfont->lfCharSet;
  193.     curr->pitch_family = farfont->lfPitchAndFamily;
  194.     _fstrcpy( curr->name, farfont->lfFaceName );
  195.     if( FontTail == NULL ) {
  196.         FontHead = FontTail = curr;
  197.         curr->index = 0;
  198.     } else {
  199.         FontTail->next = curr;
  200.         curr->index = FontTail->index + 1;
  201.         FontTail = curr;
  202.     }
  203.     return( curr->index + 1 );
  204.  
  205.     case FONT_SIZES:
  206.         newsize = MemRealloc( CurrFont->sizes,
  207.         sizeof( short ) * (CurrFont->size_count+1 ) );
  208.     if( newsize == NULL ) return( 0 );
  209.     CurrFont->sizes = newsize;
  210.     farfont = MK_FP32( logfont );
  211.     CurrFont->sizes[ CurrFont->size_count ] = farfont->lfHeight;
  212.     CurrFont->size_count++;
  213.     return( CurrFont->size_count );
  214.     }
  215.  
  216. } /* EnumFontsProc */
  217.  
  218. /*
  219.  * GetAllFonts - get all fonts available
  220.  */
  221. void GetAllFonts( LPEDATA ed )
  222. {
  223.     HDC        hdc;
  224.     FARPROC    fp;
  225.     LPFINFO    tmp,next;
  226.  
  227.     /*
  228.      * erase old list
  229.      */
  230.     tmp = FontHead;
  231.     while( tmp != NULL ) {
  232.         next = tmp->next;
  233.     MemFree( tmp );
  234.     tmp = next;
  235.     }
  236.  
  237.     fp = MakeProcInstance( EnumFontsProc, ed->inst );
  238.     hdc = GetDC( ed->hwnd );
  239.     EnumFonts( hdc, (LPSTR) NULL, fp,
  240.             (LPSTR) PASS_WORD_AS_POINTER( FONT_DATA ) );
  241.  
  242.     /*
  243.      * get all sizes
  244.      */
  245.     CurrFont = FontHead;
  246.     while( CurrFont != NULL ) {
  247.         CurrFont->size_count = 0;
  248.     EnumFonts( hdc, CurrFont->name, fp,
  249.         (LPSTR) PASS_WORD_AS_POINTER( FONT_SIZES ) );
  250.     CurrFont = CurrFont->next;
  251.     }
  252.  
  253.     ReleaseDC( ed->hwnd, hdc );
  254.     FreeProcInstance( fp );
  255.  
  256. } /* GetAllFonts */
  257.