home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / fontdlg.zip / FONTDL.TXT
Text File  |  1993-08-23  |  10KB  |  343 lines

  1.  
  2. 16.8 An Example Program: FONTDLG
  3.  
  4. ***********************************************************
  5. *                       FONTDLG.C                         *
  6. ***********************************************************
  7.  
  8. #define INCL_WIN
  9. #define INCL_STDDLG
  10. #define INCL_GPI
  11.  
  12. #include <os2.h>
  13. #include <stdio.h>
  14. #include <stdlib.h>
  15. #include <string.h>
  16. #include "fontdlg.h"
  17.  
  18. typedef struct {
  19.    FONTDLG fdFontDlg ;
  20.    USHORT  bInit ;
  21. } MYFONTINFO, *PMYFONTINFO ;
  22.  
  23. MRESULT EXPENTRY ClientWndProc ( HWND hwndWnd,
  24.                                  ULONG ulMsg,
  25.                                  MPARAM mpParm1,
  26.                                  MPARAM mpParm2 ) ;
  27.  
  28. VOID SetFont ( HWND hwndWnd, PMYFONTINFO pmfiFont ) ;
  29.  
  30. #define CLS_CLIENT               "MyClass"
  31.  
  32. INT main ( VOID )
  33. {
  34.    HAB   habAnchor ;
  35.    HMQ   hmqQueue ;
  36.    ULONG ulFlags ;
  37.    HWND  hwndFrame ;
  38.    HWND  hwndClient ;
  39.    BOOL  bLoop ;
  40.    QMSG  qmMsg ;
  41.  
  42.    habAnchor = WinInitialize ( 0 ) ;
  43.    hmqQueue = WinCreateMsgQueue ( habAnchor, 0 ) ;
  44.  
  45.    WinRegisterClass ( habAnchor,
  46.                       CLS_CLIENT,
  47.                       ClientWndProc,
  48.                       CS_SIZEREDRAW,
  49.                       sizeof ( PVOID )) ;
  50.  
  51.    ulFlags = FCF_STANDARD & ~FCF_ACCELTABLE & ~FCF_ICON ;
  52.  
  53.    hwndFrame = WinCreateStdWindow ( HWND_DESKTOP,
  54.                                     0,
  55.                                     &ulFlags,
  56.                                     CLS_CLIENT,
  57.                                     "Font Dialog Example",
  58.                                     0,
  59.                                     NULLHANDLE,
  60.                                     RES_CLIENT,
  61.                                     &hwndClient ) ;
  62.  
  63.    if ( hwndFrame != NULLHANDLE ) {
  64.  
  65.       WinSetWindowPos ( hwndFrame,
  66.                       NULLHANDLE,
  67.                       50,
  68.                       50,
  69.                       500,
  70.                       250,
  71.                       SWP_SIZE |
  72.                       SWP_MOVE |
  73.                       SWP_ACTIVATE |
  74.                       SWP_SHOW ) ;
  75.  
  76.       bLoop = WinGetMsg ( habAnchor, &qmMsg, NULLHANDLE, 0, 0 ) ;
  77.       while ( bLoop ) {
  78.          WinDispatchMsg ( habAnchor, &qmMsg ) ;
  79.          bLoop = WinGetMsg ( habAnchor, &qmMsg, NULLHANDLE, 0, 0 ) ;
  80.       } /* endwhile */
  81.  
  82.       WinDestroyWindow ( hwndFrame ) ;
  83.    } /* endif */
  84.  
  85.    WinDestroyMsgQueue ( hmqQueue ) ;
  86.    WinTerminate ( habAnchor ) ;
  87.    return 0 ;
  88. }
  89.  
  90. MRESULT EXPENTRY ClientWndProc ( HWND hwndWnd,
  91.                                  ULONG ulMsg,
  92.                                  MPARAM mpParm1,
  93.                                  MPARAM mpParm2 )
  94. {
  95.    PMYFONTINFO pmfiFont ;
  96.  
  97.    pmfiFont = WinQueryWindowPtr ( hwndWnd, 0 ) ;
  98.  
  99.    switch ( ulMsg ) {
  100.  
  101.    case WM_CREATE:
  102.  
  103.       pmfiFont = malloc ( sizeof ( MYFONTINFO )) ;
  104.       if ( pmfiFont == NULL ) {
  105.          WinAlarm ( HWND_DESKTOP, WA_ERROR ) ;
  106.          WinMessageBox ( HWND_DESKTOP,
  107.                          hwndWnd,
  108.                          "No memory could be allocated !",
  109.                          "Error",
  110.                          0,
  111.                          MB_INFORMATION | MB_OK ) ;
  112.          return MRFROMSHORT ( TRUE ) ;
  113.       } /* endif */
  114.  
  115.       WinSetWindowPtr ( hwndWnd, 0, pmfiFont ) ;
  116.  
  117.       memset ( pmfiFont, 0, sizeof ( MYFONTINFO )) ;
  118.       pmfiFont -> bInit = FALSE ;
  119.       break ;
  120.    case WM_DESTROY:
  121.       if ( pmfiFont != NULL ) {
  122.          free ( pmfiFont ) ;
  123.       } /* endif */
  124.       break ;
  125.  
  126.    case WM_PAINT:
  127.       {
  128.          HPS   hpsPaint ;
  129.          ULONG ulReturn ;
  130.          RECTL rclPaint ;
  131.          CHAR  achFontName [200] , achMsg [256] ;
  132.  
  133.          hpsPaint = WinBeginPaint ( hwndWnd, NULLHANDLE, & rclPaint ) ;
  134.          ulReturn = WinQueryPresParam ( hwndWnd,
  135.                                         PP_FONTNAMESIZE,
  136.                                         0,
  137.                                         NULL,
  138.                                         256,
  139.                                         achFontName,
  140.                                         0 ) ;
  141.          if ( ulReturn ) {
  142.             sprintf ( achMsg,
  143.                       "The font selected is \"%s\"",
  144.                       achFontName ) ;
  145.          } else {
  146.             strcpy ( achMsg, "No font selected" ) ;
  147.          } /* endif */
  148.  
  149.          WinFillRect ( hpsPaint, &rclPaint, SYSCLR_WINDOW ) ;
  150.          WinQueryWindowRect ( hwndWnd, &rclPaint ) ;
  151.  
  152.          WinDrawText ( hpsPaint,
  153.                        - 1,
  154.                        achMsg,
  155.                        & rclPaint,
  156.                        0,
  157.                        0,
  158.                        DT_VCENTER | DT_CENTER | DT_TEXTATTRS ) ;
  159.  
  160.          WinEndPaint ( hpsPaint ) ;
  161.       }
  162.       break ;
  163.  
  164.    case WM_COMMAND:
  165.  
  166.       switch ( SHORT1FROMMP ( mpParm1 )) {
  167.  
  168.       case IDM_FONT:
  169.          SetFont ( hwndWnd , pmfiFont ) ;
  170.          WinInvalidateRect ( hwndWnd, NULL, TRUE ) ;
  171.          WinUpdateWindow ( hwndWnd ) ;
  172.          break ;
  173.  
  174.       case IDM_EXIT:
  175.          WinPostMsg ( hwndWnd, WM_CLOSE, 0, 0 ) ;
  176.          break ;
  177.  
  178.       default:
  179.          return WinDefWindowProc ( hwndWnd,
  180.                                    ulMsg,
  181.                                    mpParm1,
  182.                                    mpParm2 ) ;
  183.       } /* endswitch */
  184.       break ;
  185.  
  186.    default:
  187.       return WinDefWindowProc ( hwndWnd,
  188.                                 ulMsg,
  189.                                 mpParm1,
  190.                                 mpParm2 ) ;
  191.    } /* endswitch */
  192.  
  193.    return MRFROMSHORT ( FALSE ) ;
  194. }
  195.  
  196. VOID SetFont ( HWND hwndWnd, PMYFONTINFO pmfiFont )
  197. {
  198.    FATTRS faAttrs ;
  199.    FIXED  fxSzFont ;
  200.    CHAR   achFamily [256] ;
  201.    CHAR   achFont [256] ;
  202.  
  203.  
  204.    faAttrs = pmfiFont -> fdFontDlg.fAttrs ;
  205.    fxSzFont = pmfiFont -> fdFontDlg.fxPointSize ;
  206.  
  207.    memset ( &pmfiFont -> fdFontDlg, 0, sizeof ( FONTDLG )) ;
  208.    memset ( achFont, 0, 256 ) ;
  209.  
  210.    pmfiFont -> fdFontDlg.cbSize = sizeof ( FONTDLG ) ;
  211.    pmfiFont -> fdFontDlg.hpsScreen = WinGetPS ( hwndWnd ) ;
  212.    pmfiFont -> fdFontDlg.pszFamilyname = achFamily ;
  213.    pmfiFont -> fdFontDlg.usFamilyBufLen = sizeof ( achFamily ) ;
  214.    pmfiFont -> fdFontDlg.fl = FNTS_CENTER | FNTS_INITFROMFATTRS ;
  215.    pmfiFont -> fdFontDlg.clrFore = CLR_BLACK ;
  216.    pmfiFont -> bInit = TRUE ;
  217.  
  218.    if ( pmfiFont -> bInit ) {
  219.       pmfiFont -> fdFontDlg.fAttrs = faAttrs ;
  220.       pmfiFont -> fdFontDlg.fxPointSize = fxSzFont ;
  221.    } /* endif */
  222.  
  223.    if ( WinFontDlg ( HWND_DESKTOP,
  224.                      hwndWnd,
  225.                      &pmfiFont -> fdFontDlg ) != DID_OK ) {
  226.       WinAlarm ( HWND_DESKTOP, WA_ERROR ) ;
  227.       return ;
  228.    } /* endif */
  229.  
  230.    WinReleasePS ( pmfiFont -> fdFontDlg.hpsScreen ) ;
  231.  
  232.    sprintf ( achFont,
  233.              "%d.%s",
  234.              FIXEDINT ( pmfiFont -> fdFontDlg.fxPointSize ) ,
  235.              pmfiFont -> fdFontDlg.fAttrs.szFacename ) ;
  236.  
  237.    if ( pmfiFont -> fdFontDlg.fAttrs.fsSelection &
  238.          FATTR_SEL_ITALIC ) {
  239.       strcat ( achFont, ".Italic" ) ;
  240.    } /* endif */
  241.  
  242.    if ( pmfiFont -> fdFontDlg.fAttrs.fsSelection &
  243.          FATTR_SEL_UNDERSCORE ) {
  244.       strcat ( achFont, ".Underscore" ) ;
  245.    } /* endif */
  246.  
  247.    if ( pmfiFont -> fdFontDlg.fAttrs.fsSelection &
  248.          FATTR_SEL_STRIKEOUT ) {
  249.       strcat ( achFont, ".Strikeout" ) ;
  250.    } /* endif */
  251.  
  252.    if ( pmfiFont -> fdFontDlg.fAttrs.fsSelection &
  253.          FATTR_SEL_BOLD ) {
  254.       strcat ( achFont, ".Bold" ) ;
  255.    } /* endif */
  256.  
  257.    if ( pmfiFont -> fdFontDlg.fAttrs.fsSelection &
  258.          FATTR_SEL_OUTLINE ) {
  259.       strcat ( achFont, ".Outline" ) ;
  260.    } /* endif */
  261.  
  262.    WinSetPresParam ( hwndWnd,
  263.                      PP_FONTNAMESIZE,
  264.                      strlen ( achFont ) + 1,
  265.                      achFont ) ;
  266.  
  267.    return ;
  268. }
  269.  
  270. ***********************************************************
  271. *                       FONTDLG.RC                        *
  272. ***********************************************************
  273.  
  274. #include <os2.h>
  275. #include "fontdlg.h"
  276.  
  277. MENU RES_CLIENT
  278. {
  279.    SUBMENU "~Fonts", IDM_SUB1
  280.    {
  281.       MENUITEM "~Change font...", IDM_FONT
  282.       MENUITEM "E~xit", IDM_EXIT
  283.    }
  284. }
  285.  
  286. ***********************************************************
  287. *                       FONTDLG.H                         *
  288. ***********************************************************
  289.  
  290. #define RES_CLIENT               256
  291. #define IDM_SUB1                 512
  292. #define IDM_FONT                 513
  293. #define IDM_EXIT                 514
  294.  
  295. ***********************************************************
  296. *                       FONTDLG.MAK                       *
  297. ***********************************************************
  298.  
  299. FONTDLG.EXE:                    FONTDLG.OBJ \
  300.                                 FONTDLG.RES
  301.         LINK386 /CO @<<
  302. FONTDLG
  303. FONTDLG
  304. FONTDLG
  305. OS2386
  306. FONTDLG
  307. <<
  308.         RC FONTDLG.RES FONTDLG.EXE
  309.  
  310. FONTDLG.RES:                    FONTDLG.RC \
  311.                                 FONTDLG.H
  312.         RC -r FONTDLG.RC FONTDLG.RES
  313.  
  314. FONTDLG.OBJ:                    FONTDLG.C \
  315.                                 FONTDLG.H
  316.         ICC -C+ -Kb+ -Ss+ -Ti FONTDLG.C
  317.  
  318. ***********************************************************
  319. *                       FONTDLG.DEF                       *
  320. ***********************************************************
  321.  
  322. NAME FONTDLG WINDOWAPI
  323. DESCRIPTION 'Font dialog example
  324.              Copyright (c) 1992 by Kathleen Panov.
  325.              All rights reserved.'
  326. STACKSIZE 32768
  327.  
  328. ***********************************************************
  329. *                       NOTICE                            *
  330. ***********************************************************
  331.  
  332. The FONTDLG example is from "The Art of OS/2 C Programming" 
  333. by Kathleen Panov, Arthur Panov, and Larry Salomon, Jr., 
  334. ISBN 0-89435-446-9, published in August 1993 by QED Publishing 
  335. Group.
  336.  
  337. The example was uploaded by the publisher with the assistance 
  338. of the authors for use by Forum members.
  339.  
  340. You may contact the authors through QED at 76620,2720.
  341.  
  342.  
  343.