home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / NBSIZE.ZIP / CLIENT.C next >
Text File  |  1993-01-03  |  20KB  |  471 lines

  1. /*********************************************************************
  2.  *                                                                   *
  3.  * MODULE NAME :  client.c               AUTHOR:  Rick Fishman       *
  4.  * DATE WRITTEN:  11-28-92                                           *
  5.  *                                                                   *
  6.  * MODULE DESCRIPTION:                                               *
  7.  *                                                                   *
  8.  *  Module that creates a frame/client window and associates the     *
  9.  *  client with the Client Notebook page.                            *
  10.  *                                                                   *
  11.  * HISTORY:                                                          *
  12.  *                                                                   *
  13.  *  11-28-92 - Program coded.                                        *
  14.  *                                                                   *
  15.  *  Rick Fishman                                                     *
  16.  *  Code Blazers, Inc.                                               *
  17.  *  4113 Apricot                                                     *
  18.  *  Irvine, CA. 92720                                                *
  19.  *  CIS ID: 72251,750                                                *
  20.  *                                                                   *
  21.  *********************************************************************/
  22.  
  23. #pragma strings(readonly)   // used for debug version of memory mgmt routines
  24.  
  25. /*********************************************************************/
  26. /*------- Include relevant sections of the OS/2 header files --------*/
  27. /*********************************************************************/
  28.  
  29. #define  INCL_GPICONTROL
  30. #define  INCL_GPILCIDS
  31. #define  INCL_GPIPRIMITIVES
  32. #define  INCL_WINERRORS
  33. #define  INCL_WINFRAMEMGR
  34. #define  INCL_WINMESSAGEMGR
  35. #define  INCL_WINSTDBOOK
  36. #define  INCL_WINSYS
  37. #define  INCL_WINWINDOWMGR
  38.  
  39. /**********************************************************************/
  40. /*----------------------------- INCLUDES -----------------------------*/
  41. /**********************************************************************/
  42.  
  43. #include <os2.h>
  44. #include <stdio.h>
  45. #include <stdarg.h>
  46. #include <stdlib.h>
  47. #include <string.h>
  48. #include "nbsize.h"
  49.  
  50. /*********************************************************************/
  51. /*------------------- APPLICATION DEFINITIONS -----------------------*/
  52. /*********************************************************************/
  53.  
  54. #define CLIENT_WINCLASS    "ClientWinClass"
  55.  
  56. #define FONTFACE_NAME      "Helv"
  57.  
  58. #define STRING_TO_DISPLAY  "Hi"
  59.  
  60. /**********************************************************************/
  61. /*----------------------- FUNCTION PROTOTYPES ------------------------*/
  62. /**********************************************************************/
  63.  
  64. static VOID wmCreate        ( HWND hwndClient );
  65. static VOID wmPaint         ( HWND hwndClient );
  66. static VOID wmSize          ( INT cxNew, INT cyNew );
  67. static BOOL FillFattrs      ( HPS hps, HWND hwndClient );
  68. static BOOL FillFattrsStruct( HPS hps, HWND hwndClient, PFONTMETRICS pfm );
  69. static INT  GetFontCount    ( HPS hps, HWND hwndClient, PBOOL pfUseOurFont );
  70. static VOID PaintText       ( HPS hps, HWND hwndClient );
  71.  
  72. static FNWP wpClient;
  73.  
  74. /**********************************************************************/
  75. /*------------------------ GLOBAL VARIABLES --------------------------*/
  76. /**********************************************************************/
  77.  
  78. static FATTRS fattrs;
  79. static LONG   lcid = 1;
  80. static INT    cxClient, cyClient;
  81.  
  82. /**********************************************************************/
  83. /*------------------------ CreateClientPage --------------------------*/
  84. /*                                                                    */
  85. /*  CREATE WINDOW AND ASSOCIATE IT WITH A NOTEBOOK PAGE               */
  86. /*                                                                    */
  87. /*  INPUT: window handle of parent and owner,                         */
  88. /*         notebook window handle,                                    */
  89. /*         notebook page ID                                           */
  90. /*                                                                    */
  91. /*  1.                                                                */
  92. /*                                                                    */
  93. /*  OUTPUT: client window handle                                      */
  94. /*                                                                    */
  95. /*--------------------------------------------------------------------*/
  96. /**********************************************************************/
  97. HWND CreateClientPage( HWND hwndParent, HWND hwndNB, ULONG ulPageID )
  98. {
  99.     HWND  hwndFrame, hwndClient = NULLHANDLE;
  100.     ULONG flFrame = 0;
  101.  
  102.     if( WinRegisterClass( ANCHOR( hwndNB ), CLIENT_WINCLASS, wpClient, 0, 0 ) )
  103.     {
  104.         hwndFrame = WinCreateStdWindow( hwndParent, 0, &flFrame,
  105.                                         CLIENT_WINCLASS, NULL, 0, NULLHANDLE,
  106.                                         ID_CLIENTS_FRAME, &hwndClient );
  107.  
  108.         if( hwndClient )
  109.         {
  110.             if( !WinSendMsg( hwndNB, BKM_SETPAGEWINDOWHWND,
  111.                              MPFROMLONG( ulPageID ),
  112.                              MPFROMLONG( hwndClient ) ) )
  113.             {
  114.                 WinDestroyWindow( hwndFrame );
  115.  
  116.                 hwndClient = NULLHANDLE;
  117.  
  118.                 Msg( "CreateClientPage SETPAGEWINDOWHWND RC(%X)",
  119.                      HWNDERR( hwndNB ) );
  120.             }
  121.         }
  122.         else
  123.             Msg( "CreateClientPage WinCreateStdWindow RC(%X)",
  124.                  HWNDERR( hwndNB ) );
  125.     }
  126.     else
  127.         Msg( "CreateClientPage WinRegisterClass RC(%X)", HWNDERR( hwndNB ) );
  128.  
  129.     return hwndClient;
  130. }
  131.  
  132. /**********************************************************************/
  133. /*----------------------------- wpClient -----------------------------*/
  134. /*                                                                    */
  135. /*  CLIENT WINDOW PROCEDURE                                           */
  136. /*                                                                    */
  137. /*  INPUT: window proc params                                         */
  138. /*                                                                    */
  139. /*  1.                                                                */
  140. /*                                                                    */
  141. /*  OUTPUT: retcode from processing message                           */
  142. /*                                                                    */
  143. /*--------------------------------------------------------------------*/
  144. /**********************************************************************/
  145. static MRESULT EXPENTRY wpClient( HWND hwnd, ULONG msg, MPARAM mp1, MPARAM mp2 )
  146. {
  147.     switch( msg )
  148.     {
  149.         case WM_CREATE:
  150.  
  151.             wmCreate( hwnd );
  152.  
  153.             break;
  154.  
  155.  
  156.         case WM_SIZE:
  157.  
  158.             wmSize( (INT) SHORT1FROMMP( mp2 ), (INT) SHORT2FROMMP( mp2 ) );
  159.  
  160.             break;
  161.  
  162.  
  163.         case WM_PAINT:
  164.  
  165.             wmPaint( hwnd );
  166.  
  167.             break;
  168.     }
  169.  
  170.     return WinDefWindowProc( hwnd, msg, mp1, mp2 );
  171. }
  172.  
  173. /**********************************************************************/
  174. /*----------------------------- wmCreate -----------------------------*/
  175. /*                                                                    */
  176. /*  PROCESS WM_CREATE MESSAGE FOR CLIENT WINDOW                       */
  177. /*                                                                    */
  178. /*  INPUT: client window handle                                       */
  179. /*                                                                    */
  180. /*  1.                                                                */
  181. /*                                                                    */
  182. /*  OUTPUT: nothing                                                   */
  183. /*                                                                    */
  184. /*--------------------------------------------------------------------*/
  185. /**********************************************************************/
  186. static VOID wmCreate( HWND hwndClient )
  187. {
  188.     HPS hps = WinGetPS( hwndClient );
  189.  
  190.     if( hps )
  191.     {
  192.         if( !FillFattrs( hps, hwndClient ) )
  193.             lcid = 0;
  194.     }
  195.     else
  196.         Msg( "wmCreate WinGetPS RC(%X)", HWNDERR( hwndClient ) );
  197.  
  198.     return;
  199. }
  200.  
  201. /**********************************************************************/
  202. /*----------------------------- wmPaint ------------------------------*/
  203. /*                                                                    */
  204. /*  PROCESS WM_PAINT MESSAGE FOR CLIENT WINDOW                        */
  205. /*                                                                    */
  206. /*  INPUT: client window handle                                       */
  207. /*                                                                    */
  208. /*  1.                                                                */
  209. /*                                                                    */
  210. /*  OUTPUT: nothing                                                   */
  211. /*                                                                    */
  212. /*--------------------------------------------------------------------*/
  213. /**********************************************************************/
  214. static VOID wmPaint( HWND hwndClient )
  215. {
  216.     HPS hps = WinBeginPaint( hwndClient, NULLHANDLE, NULL );
  217.  
  218.     if( hps )
  219.     {
  220.         RECTL rcl;
  221.  
  222.         GpiSetColor( hps, CLR_RED );
  223.         GpiSetMix( hps, FM_OVERPAINT );
  224.         GpiSetBackColor( hps, CLR_WHITE );
  225.         GpiSetBackMix( hps, BM_OVERPAINT );
  226.  
  227.         if( WinQueryWindowRect( hwndClient, &rcl ) )
  228.         {
  229.             if( !WinFillRect( hps, &rcl, CLR_WHITE ) )
  230.                 Msg( "wmPaint WinFillRect RC(%X)", HWNDERR( hwndClient ) );
  231.         }
  232.         else
  233.             Msg( "wmPaint WinQueryWindowRect RC(%X)", HWNDERR( hwndClient ) );
  234.  
  235.         if( GPI_ERROR == GpiCreateLogFont( hps, NULL, lcid, &fattrs ) )
  236.             Msg( "wmPaint GpiCreateLogFont RC(%X)", HWNDERR( hwndClient ) );
  237.         else
  238.         {
  239.             if( GpiSetCharSet( hps, lcid ) )
  240.                 PaintText( hps, hwndClient );
  241.             else
  242.                 Msg( "wmPaint GpiSetCharSet RC(%X)", HWNDERR( hwndClient ) );
  243.         }
  244.  
  245.         if( !WinEndPaint( hps ) )
  246.             Msg( "wmPaint WinEndPaint RC(%X)", HWNDERR( hwndClient ) );
  247.     }
  248.     else
  249.         Msg( "wmPaint WinBeginPaint RC(%X)", HWNDERR( hwndClient ) );
  250.  
  251.     return;
  252. }
  253.  
  254. /**********************************************************************/
  255. /*------------------------------ wmSize ------------------------------*/
  256. /*                                                                    */
  257. /*  PROCESS WM_SIZE MESSAGE FOR CLIENT WINDOW                         */
  258. /*                                                                    */
  259. /*  INPUT: client window handle,                                      */
  260. /*         new window width,                                          */
  261. /*         new window height                                          */
  262. /*                                                                    */
  263. /*  1.                                                                */
  264. /*                                                                    */
  265. /*  OUTPUT: nothing                                                   */
  266. /*                                                                    */
  267. /*--------------------------------------------------------------------*/
  268. /**********************************************************************/
  269. static VOID wmSize( INT cxNew, INT cyNew )
  270. {
  271.     cxClient = cxNew;
  272.  
  273.     cyClient = cyNew;
  274.  
  275.     return;
  276. }
  277.  
  278. /**********************************************************************/
  279. /*---------------------------- FillFattrs ----------------------------*/
  280. /*                                                                    */
  281. /*  DO EVERYTHING NECESSARY TO FILL IN THE FATTRS STRUCT FOR OUR FONT */
  282. /*                                                                    */
  283. /*  INPUT: presentation space handle,                                 */
  284. /*         client window handle                                       */
  285. /*                                                                    */
  286. /*  1.                                                                */
  287. /*                                                                    */
  288. /*  OUTPUT: TRUE or FALSE if successful or not                        */
  289. /*                                                                    */
  290. /*--------------------------------------------------------------------*/
  291. /**********************************************************************/
  292. static BOOL FillFattrs( HPS hps, HWND hwndClient )
  293. {
  294.     BOOL         fUseOurFont, fSuccess = FALSE;
  295.     PFONTMETRICS afm;
  296.     HDC          hdc;
  297.     LONG         cFonts;
  298.     INT          i;
  299.  
  300.     hdc = GpiQueryDevice( hps );
  301.  
  302.     if( hdc && hdc != HDC_ERROR )
  303.     {
  304.         cFonts = GetFontCount( hps, hwndClient, &fUseOurFont );
  305.  
  306.         afm = (PFONTMETRICS) malloc( cFonts * sizeof( FONTMETRICS ) );
  307.  
  308.         if( afm )
  309.         {
  310.             if( GPI_ALTERROR != GpiQueryFonts( hps, QF_PUBLIC,
  311.                                         fUseOurFont ? FONTFACE_NAME : NULL,
  312.                                         &cFonts, sizeof( FONTMETRICS ), afm ) )
  313.             {
  314.                 for( i = 0; i < cFonts; i++)
  315.                 {
  316.                     if( afm[ i ].fsDefn & FM_DEFN_OUTLINE )
  317.                     {
  318.                         if( FillFattrsStruct( hps, hwndClient, &afm[ i ] ) )
  319.                             fSuccess = TRUE;
  320.  
  321.                         break;
  322.                     }
  323.                 }
  324.             }
  325.             else
  326.                 Msg( "FillFattrs GpiQueryFonts RC(%X)", HWNDERR( hwndClient ) );
  327.  
  328.             free( afm );
  329.         }
  330.         else
  331.             Msg( "Out of memory in FillFattrs!" );
  332.     }
  333.     else
  334.         Msg( "FillFattrs GpiQueryDevice RC(%X)", HWNDERR( hwndClient ) );
  335.  
  336.     return fSuccess;
  337. }
  338.  
  339. /**********************************************************************/
  340. /*------------------------- FillFattrsStruct -------------------------*/
  341. /*                                                                    */
  342. /*  FILL THE FATTRS STRUCTURE FOR THE FONT THAT WE WILL USE.          */
  343. /*                                                                    */
  344. /*  INPUT: presentation space handle,                                 */
  345. /*         client window handle,                                      */
  346. /*         pointer to FONTMETRICS struct for our font                 */
  347. /*                                                                    */
  348. /*  1.                                                                */
  349. /*                                                                    */
  350. /*  OUTPUT: TRUE or FALSE if successful or not                        */
  351. /*                                                                    */
  352. /*--------------------------------------------------------------------*/
  353. /**********************************************************************/
  354. static BOOL FillFattrsStruct( HPS hps, HWND hwndClient, PFONTMETRICS pfm )
  355. {
  356.     BOOL fSuccess = TRUE;
  357.  
  358.     fattrs.usCodePage = GpiQueryCp( hps );
  359.  
  360.     if( fattrs.usCodePage != GPI_ERROR )
  361.     {
  362.         (void) strcpy( fattrs.szFacename, pfm->szFacename );
  363.  
  364.         fattrs.usRecordLength = sizeof( FATTRS );
  365.         fattrs.idRegistry = pfm->idRegistry;
  366.         fattrs.fsFontUse = FATTR_FONTUSE_OUTLINE;
  367.     }
  368.     else
  369.     {
  370.         fSuccess = FALSE;
  371.  
  372.         Msg( "FillFattrsStruct GpiQueryCp RC(%X)", HWNDERR( hwndClient ) );
  373.     }
  374.  
  375.     return fSuccess;
  376. }
  377.  
  378. /**********************************************************************/
  379. /*--------------------------- GetFontCount ---------------------------*/
  380. /*                                                                    */
  381. /*  GET A COUNT OF THE FONTS AVAILABLE.                               */
  382. /*                                                                    */
  383. /*  INPUT: presentation space handle,                                 */
  384. /*         client window handle,                                      */
  385. /*         pointer to bool that returns if we are using the original  */
  386. /*           font requested                                           */
  387. /*                                                                    */
  388. /*  1.                                                                */
  389. /*                                                                    */
  390. /*  OUTPUT: count of fonts                                            */
  391. /*                                                                    */
  392. /*--------------------------------------------------------------------*/
  393. /**********************************************************************/
  394. static INT GetFontCount( HPS hps, HWND hwndClient, PBOOL pfUseOurFont )
  395. {
  396.     LONG cFonts, cFontsRequested = 0;
  397.  
  398.     *pfUseOurFont = TRUE;
  399.  
  400.     cFonts = GpiQueryFonts( hps, QF_PUBLIC, FONTFACE_NAME, &cFontsRequested, 0,
  401.                             NULL );
  402.  
  403.     if( cFonts == GPI_ALTERROR )
  404.     {
  405.         cFonts = 0;
  406.  
  407.         Msg( "GetFontCount GpiQueryFonts RC(%X)", HWNDERR( hwndClient ) );
  408.     }
  409.     else if( !cFonts )
  410.     {
  411.         *pfUseOurFont = FALSE;
  412.  
  413.         cFontsRequested  = 0;
  414.  
  415.         cFonts = GpiQueryFonts( hps, QF_PUBLIC, NULL, &cFontsRequested, 0,
  416.                                 NULL );
  417.  
  418.         if( !cFonts )
  419.             Msg( "GetFontCount GpiQueryFonts found NO FONTS in your system!" );
  420.         else if( cFonts == GPI_ALTERROR )
  421.         {
  422.             cFonts = 0;
  423.  
  424.             Msg( "GetFontCount GpiQueryFonts RC(%X)", HWNDERR( hwndClient ) );
  425.         }
  426.     }
  427.  
  428.     return cFonts;
  429. }
  430.  
  431. /**********************************************************************/
  432. /*--------------------------- PaintText ------------------------------*/
  433. /*                                                                    */
  434. /*  PAINT TEXT IN THE CLIENT AREA                                     */
  435. /*                                                                    */
  436. /*  INPUT: presentation space handle,                                 */
  437. /*         client window handle                                       */
  438. /*                                                                    */
  439. /*  1.                                                                */
  440. /*                                                                    */
  441. /*  OUTPUT: nothing                                                   */
  442. /*                                                                    */
  443. /*--------------------------------------------------------------------*/
  444. /**********************************************************************/
  445. static VOID PaintText( HPS hps, HWND hwndClient )
  446. {
  447.     SIZEF  sizf;
  448.     POINTL ptl;
  449.  
  450.     sizf.cx = MAKEFIXED( cxClient, 0 );
  451.     sizf.cy = MAKEFIXED( cyClient, 0 );
  452.  
  453.     if( GpiSetCharBox( hps, &sizf ) )
  454.     {
  455.         ptl.x = 0;
  456.         ptl.y = 0;
  457.  
  458.         if( GPI_ERROR == GpiCharStringPosAt( hps, &ptl, NULL, 0,
  459.                   strlen( STRING_TO_DISPLAY ), STRING_TO_DISPLAY, NULL ) )
  460.             Msg( "PaintText GpiCharStringPosAt RC(%X)", HWNDERR( hwndClient ) );
  461.     }
  462.     else
  463.         Msg( "PaintText GpiSetCharBox RC(%X)", HWNDERR( hwndClient ) );
  464.  
  465.     return;
  466. }
  467.  
  468. /************************************************************************
  469.  *                      E N D   O F   S O U R C E                       *
  470.  ************************************************************************/
  471.