home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / sftick.zip / adv / font / FONTDLG.C < prev    next >
Text File  |  1994-04-21  |  8KB  |  276 lines

  1. #define INCL_WIN
  2. #define INCL_STDDLG
  3. #define INCL_GPI
  4.  
  5. #include <os2.h>
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <string.h>
  9. #include "fontdlg.h"
  10.  
  11. typedef struct {
  12.    FONTDLG fdFontDlg ;
  13. } MYFONTINFO, *PMYFONTINFO ;
  14.  
  15. MRESULT EXPENTRY ClientWndProc ( HWND hwndWnd,
  16.                                  ULONG ulMsg,
  17.                                  MPARAM mpParm1,
  18.                                  MPARAM mpParm2 ) ;
  19.  
  20. VOID SetFont ( HWND hwndWnd, PMYFONTINFO pmfiFont ) ;
  21.  
  22. #define CLS_CLIENT               "MyClass"
  23.  
  24. INT main ( VOID )
  25. {
  26.    HAB   habAnchor ;
  27.    HMQ   hmqQueue ;
  28.    ULONG ulFlags ;
  29.    HWND  hwndFrame ;
  30.    HWND  hwndClient ;
  31.    QMSG  qmMsg ;
  32.  
  33.    /* initialization */
  34.    habAnchor = WinInitialize ( 0 ) ;
  35.    hmqQueue = WinCreateMsgQueue ( habAnchor, 0 ) ;
  36.  
  37.    /* register class */
  38.  
  39.    WinRegisterClass ( habAnchor,
  40.                       CLS_CLIENT,
  41.                       ClientWndProc,
  42.                       CS_SIZEREDRAW,
  43.                       sizeof ( PVOID )) ;
  44.  
  45.    ulFlags = FCF_STANDARD & ~FCF_ACCELTABLE & ~FCF_ICON ;
  46.  
  47.    hwndFrame = WinCreateStdWindow ( HWND_DESKTOP,
  48.                                     0,
  49.                                     &ulFlags,
  50.                                     CLS_CLIENT,
  51.                                     "Font Dialog Example",
  52.                                     0,
  53.                                     NULLHANDLE,
  54.                                     RES_CLIENT,
  55.                                     &hwndClient ) ;
  56.  
  57.    if ( hwndFrame ) {
  58.  
  59.       WinSetWindowPos ( hwndFrame,
  60.                       NULLHANDLE,
  61.                       50,
  62.                       50,
  63.                       500,
  64.                       250,
  65.                       SWP_SIZE |
  66.                       SWP_MOVE |
  67.                       SWP_ACTIVATE |
  68.                       SWP_SHOW ) ;
  69.  
  70.       while ( WinGetMsg ( habAnchor, &qmMsg, NULLHANDLE, 0, 0 ))
  71.          WinDispatchMsg ( habAnchor, &qmMsg ) ;
  72.  
  73.       WinDestroyWindow ( hwndFrame ) ;
  74.    } /* endif */
  75.  
  76.    /* clean-up */
  77.  
  78.    WinDestroyMsgQueue ( hmqQueue ) ;
  79.    WinTerminate ( habAnchor ) ;
  80.    return 0 ;
  81. }
  82.  
  83. MRESULT EXPENTRY ClientWndProc ( HWND hwndWnd,
  84.                                  ULONG ulMsg,
  85.                                  MPARAM mpParm1,
  86.                                  MPARAM mpParm2 )
  87. {
  88.    PMYFONTINFO pmfiFont ;
  89.  
  90.    pmfiFont = WinQueryWindowPtr ( hwndWnd, 0 ) ;
  91.  
  92.    switch ( ulMsg ) {
  93.  
  94.    case WM_CREATE:
  95.  
  96.       /* create window word for font info */
  97.  
  98.       pmfiFont = malloc ( sizeof ( MYFONTINFO )) ;
  99.       if ( pmfiFont == NULL ) {
  100.          WinAlarm ( HWND_DESKTOP, WA_ERROR ) ;
  101.          WinMessageBox ( HWND_DESKTOP,
  102.                          hwndWnd,
  103.                          "No memory could be allocated !",
  104.                          "Error",
  105.                          0,
  106.                          MB_INFORMATION | MB_OK ) ;
  107.          return MRFROMSHORT ( TRUE ) ;
  108.       } /* endif */
  109.  
  110.       WinSetWindowPtr ( hwndWnd, 0, pmfiFont ) ;
  111.  
  112.       memset ( pmfiFont, 0, sizeof ( MYFONTINFO )) ;
  113.       break ;
  114.    case WM_DESTROY:
  115.       if ( pmfiFont ) {
  116.          free ( pmfiFont ) ;
  117.       } /* endif */
  118.       break ;
  119.  
  120.    case WM_PAINT:
  121.       {
  122.          HPS   hpsPaint ;
  123.          ULONG ulReturn ;
  124.          RECTL rclPaint ;
  125.          CHAR  achFontName [200] , achMsg [256] ;
  126.  
  127.          hpsPaint = WinBeginPaint ( hwndWnd, NULLHANDLE, &rclPaint ) ;
  128.          /* do we have a PP_FONT... presparam, if so, what is it? */
  129.          ulReturn = WinQueryPresParam ( hwndWnd,
  130.                                         PP_FONTNAMESIZE,
  131.                                         0,
  132.                                         NULL,
  133.                                         256,
  134.                                         achFontName,
  135.                                         0 ) ;
  136.          /* Success */
  137.          if ( ulReturn ) {
  138.             sprintf ( achMsg,
  139.                       "The font selected is \"%s\"",
  140.                       achFontName ) ;
  141.          } else {
  142.             /* none listed */
  143.             strcpy ( achMsg, "No font selected" ) ;
  144.          } /* endif */
  145.  
  146.          /* clear window */
  147.          WinFillRect ( hpsPaint, &rclPaint, SYSCLR_WINDOW ) ;
  148.          WinQueryWindowRect ( hwndWnd, &rclPaint ) ;
  149.  
  150.          /* draw text centered on window */
  151.          WinDrawText ( hpsPaint,
  152.                        - 1,
  153.                        achMsg,
  154.                        & rclPaint,
  155.                        0,
  156.                        0,
  157.                        DT_VCENTER | DT_CENTER | DT_TEXTATTRS ) ;
  158.  
  159.          WinEndPaint ( hpsPaint ) ;
  160.       }
  161.       break ;
  162.  
  163.    case WM_COMMAND:
  164.  
  165.       switch ( SHORT1FROMMP ( mpParm1 )) {
  166.  
  167.       case IDM_FONT:
  168.          SetFont ( hwndWnd , pmfiFont ) ;
  169.          /* want to repaint window */
  170.          WinInvalidateRect ( hwndWnd, NULL, TRUE ) ;
  171.          /* force a repaint */
  172.          WinUpdateWindow ( hwndWnd ) ;
  173.          break ;
  174.  
  175.       case IDM_EXIT:
  176.          WinPostMsg ( hwndWnd, WM_QUIT, 0, 0 ) ;
  177.          break ;
  178.  
  179.       default:
  180.          return WinDefWindowProc ( hwndWnd,
  181.                                    ulMsg,
  182.                                    mpParm1,
  183.                                    mpParm2 ) ;
  184.       } /* endswitch */
  185.       break ;
  186.  
  187.    default:
  188.       return WinDefWindowProc ( hwndWnd,
  189.                                 ulMsg,
  190.                                 mpParm1,
  191.                                 mpParm2 ) ;
  192.    } /* endswitch */
  193.  
  194.    return MRFROMSHORT ( FALSE ) ;
  195. }
  196.  
  197. VOID SetFont ( HWND hwndWnd, PMYFONTINFO pmfiFont )
  198. {
  199.    FATTRS faAttrs ;
  200.    FIXED  fxSzFont ;
  201.    CHAR   achFamily [256] ;
  202.    CHAR   achFont [256] ;
  203.  
  204.  
  205.    /* save FATTRS structure that currently exists */
  206.    faAttrs = pmfiFont->fdFontDlg.fAttrs ;
  207.    /* save currently existing point size */
  208.    fxSzFont = pmfiFont->fdFontDlg.fxPointSize ;
  209.  
  210.    /* clear out values */
  211.    memset ( &pmfiFont->fdFontDlg, 0, sizeof ( FONTDLG )) ;
  212.    memset ( achFont, 0, 256 ) ;
  213.  
  214.    /* mandatory initialization values */
  215.    pmfiFont->fdFontDlg.cbSize = sizeof ( FONTDLG ) ;
  216.    pmfiFont->fdFontDlg.hpsScreen = WinGetPS ( hwndWnd ) ;
  217.    pmfiFont->fdFontDlg.pszFamilyname = achFamily ;
  218.    pmfiFont->fdFontDlg.fl = FNTS_CENTER | FNTS_INITFROMFATTRS ;
  219.    pmfiFont->fdFontDlg.clrFore = CLR_BLACK ;
  220.  
  221.    /* reset to original values */
  222.    pmfiFont->fdFontDlg.fAttrs = faAttrs ;
  223.    pmfiFont->fdFontDlg.fxPointSize = fxSzFont ;
  224.  
  225.    /* bring up dialog */
  226.  
  227.    if ( WinFontDlg ( HWND_DESKTOP,
  228.                      hwndWnd,
  229.                      &pmfiFont->fdFontDlg ) != DID_OK ) {
  230.       WinAlarm ( HWND_DESKTOP, WA_ERROR ) ;
  231.       return ;
  232.    } /* endif */
  233.  
  234.    WinReleasePS ( pmfiFont->fdFontDlg.hpsScreen ) ;
  235.  
  236.    sprintf ( achFont,
  237.              "%d.%s",
  238.              FIXEDINT ( pmfiFont->fdFontDlg.fxPointSize ) ,
  239.              pmfiFont->fdFontDlg.fAttrs.szFacename ) ;
  240.  
  241.    if ( pmfiFont->fdFontDlg.fAttrs.fsSelection &
  242.          FATTR_SEL_ITALIC ) {
  243.       strcat ( achFont, ".Italic" ) ;
  244.    } /* endif */
  245.  
  246.    if ( pmfiFont->fdFontDlg.fAttrs.fsSelection &
  247.          FATTR_SEL_UNDERSCORE ) {
  248.       strcat ( achFont, ".Underscore" ) ;
  249.    } /* endif */
  250.  
  251.    if ( pmfiFont->fdFontDlg.fAttrs.fsSelection &
  252.          FATTR_SEL_STRIKEOUT ) {
  253.       strcat ( achFont, ".Strikeout" ) ;
  254.    } /* endif */
  255.  
  256.    if ( pmfiFont->fdFontDlg.fAttrs.fsSelection &
  257.          FATTR_SEL_BOLD ) {
  258.       strcat ( achFont, ".Bold" ) ;
  259.    } /* endif */
  260.  
  261.    if ( pmfiFont->fdFontDlg.fAttrs.fsSelection &
  262.          FATTR_SEL_OUTLINE ) {
  263.       strcat ( achFont, ".Outline" ) ;
  264.    } /* endif */
  265.  
  266.  
  267.    /* set the window font */
  268.  
  269.    WinSetPresParam ( hwndWnd,
  270.                      PP_FONTNAMESIZE,
  271.                      strlen ( achFont ) + 1,
  272.                      achFont ) ;
  273.  
  274.    return ;
  275. }
  276.