home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / NBEZ.ZIP / NBEZ.C < prev    next >
Text File  |  1992-12-05  |  20KB  |  418 lines

  1. /*********************************************************************
  2.  *                                                                   *
  3.  * MODULE NAME :  nbez.c                 AUTHOR:  Rick Fishman       *
  4.  * DATE WRITTEN:  12-05-92                                           *
  5.  *                                                                   *
  6.  * HOW TO RUN THIS PROGRAM:                                          *
  7.  *                                                                   *
  8.  *  Just enter NBEZ on the command line.                             *
  9.  *                                                                   *
  10.  * MODULE DESCRIPTION:                                               *
  11.  *                                                                   *
  12.  *  Only module for NBEZ.EXE, a program that demonstrates the        *
  13.  *  simplest way to create a Notebook control with only MAJOR tabs.  *
  14.  *  It loads the dialogs that will be associated with the Notebook   *
  15.  *  pages when the Notebook is initialized. No error-checking is     *
  16.  *  done to make the program more streamlined.                       *
  17.  *                                                                   *
  18.  *  To provide more functionality, like using pages with MINOR tabs  *
  19.  *  and loading the dialogs on demand, get NBBASE.ZIP.               *
  20.  *                                                                   *
  21.  * NOTES:                                                            *
  22.  *                                                                   *
  23.  *  This program is strictly a sample and should be treated as such. *
  24.  *  There is nothing real-world about it and the dialogs that it     *
  25.  *  uses do nothing useful. I think it still demonstrates the        *
  26.  *  notebook control as well as can be expected though.              *
  27.  *                                                                   *
  28.  *  I hope this code proves useful for other PM programmers. The     *
  29.  *  more of us the better!                                           *
  30.  *                                                                   *
  31.  * HISTORY:                                                          *
  32.  *                                                                   *
  33.  *  12-05-92 - Copied source code from the NBBASE.EXE sample and     *
  34.  *                streamlined it.                                    *
  35.  *                                                                   *
  36.  *  Rick Fishman                                                     *
  37.  *  Code Blazers, Inc.                                               *
  38.  *  4113 Apricot                                                     *
  39.  *  Irvine, CA. 92720                                                *
  40.  *  CIS ID: 72251,750                                                *
  41.  *                                                                   *
  42.  *********************************************************************/
  43.  
  44. #pragma strings(readonly)   // used for debug version of memory mgmt routines
  45.  
  46. /*********************************************************************/
  47. /*------- Include relevant sections of the OS/2 header files --------*/
  48. /*********************************************************************/
  49.  
  50. #define  INCL_GPILCIDS
  51. #define  INCL_GPIPRIMITIVES
  52. #define  INCL_WINDIALOGS
  53. #define  INCL_WINERRORS
  54. #define  INCL_WINFRAMEMGR
  55. #define  INCL_WINMESSAGEMGR
  56. #define  INCL_WINSTDBOOK
  57. #define  INCL_WINSYS
  58. #define  INCL_WINWINDOWMGR
  59.  
  60. /**********************************************************************/
  61. /*----------------------------- INCLUDES -----------------------------*/
  62. /**********************************************************************/
  63.  
  64. #include <os2.h>
  65. #include <stdio.h>
  66. #include <stdarg.h>
  67. #include <stdlib.h>
  68. #include <string.h>
  69. #include "nbez.h"
  70.  
  71. /*********************************************************************/
  72. /*------------------- APPLICATION DEFINITIONS -----------------------*/
  73. /*********************************************************************/
  74.  
  75. #define FRAME_FLAGS           (FCF_TASKLIST | FCF_TITLEBAR   | FCF_SYSMENU | \
  76.                                FCF_MINMAX   | FCF_SIZEBORDER | FCF_ICON)
  77.  
  78. #define TAB_WIDTH_MARGIN      10   // Padding for the width of a notebook tab
  79. #define TAB_HEIGHT_MARGIN     6    // Padding for the height of a notebook tab
  80.  
  81. /**********************************************************************/
  82. /*----------------------- FUNCTION PROTOTYPES ------------------------*/
  83. /**********************************************************************/
  84.  
  85.        INT  main            ( VOID );
  86. static VOID CreateNotebook  ( HWND hwndClient );
  87. static VOID SetUpPage       ( HWND hwndNB, INT iArrayIndex );
  88. static VOID SetTabDimensions( HWND hwndNB );
  89.  
  90. FNWP wpClient, wpPage1, wpPage2, wpPage3, wpPage4;
  91.  
  92. /**********************************************************************/
  93. /*------------------------ GLOBAL VARIABLES --------------------------*/
  94. /**********************************************************************/
  95.  
  96. NBPAGE nbpage[] =    // INFORMATION ABOUT NOTEBOOK PAGES (see NBEZ.H)
  97. {
  98.     { wpPage1, "Page 1", "Page ~1", IDD_PAGE1 },
  99.     { wpPage2, "Page 2", "Page ~2", IDD_PAGE2 },
  100.     { wpPage3, "Page 3", "Page ~3", IDD_PAGE3 },
  101.     { wpPage4, "Page 4", "Page ~4", IDD_PAGE4 }
  102. };
  103.  
  104. #define PAGE_COUNT (sizeof( nbpage ) / sizeof( NBPAGE ))
  105.  
  106. /**********************************************************************/
  107. /*------------------------------- main -------------------------------*/
  108. /*                                                                    */
  109. /*  PROGRAM ENTRYPOINT                                                */
  110. /*                                                                    */
  111. /*  INPUT: nothing                                                    */
  112. /*                                                                    */
  113. /*  1.                                                                */
  114. /*                                                                    */
  115. /*  OUTPUT: nothing                                                   */
  116. /*                                                                    */
  117. /*--------------------------------------------------------------------*/
  118. /**********************************************************************/
  119. INT main( VOID )
  120. {
  121.     HAB   hab;
  122.     HMQ   hmq;
  123.     HWND  hwndFrame, hwndClient;
  124.     QMSG  qmsg;
  125.     ULONG flFrame = FRAME_FLAGS;
  126.  
  127.     hab = WinInitialize( 0 );
  128.  
  129.     hmq = WinCreateMsgQueue( hab, 0 );
  130.  
  131.     // CS_CLIPCHILDREN so the client doesn't need to paint the area covered
  132.     // by the notebook. CS_SIZEREDRAW so the notebook gets sized correctly
  133.     // the first time the Frame/Client get drawn.
  134.  
  135.     WinRegisterClass( hab, NOTEBOOK_WINCLASS, wpClient,
  136.                       CS_CLIPCHILDREN | CS_SIZEREDRAW, 0 );
  137.  
  138.     hwndFrame = WinCreateStdWindow( HWND_DESKTOP, 0, &flFrame,
  139.                                     NOTEBOOK_WINCLASS, NULL, 0, NULLHANDLE,
  140.                                     ID_NBWINFRAME, &hwndClient );
  141.  
  142.     WinSetWindowPos( hwndFrame, NULLHANDLE, 50, 50, 500, 450,
  143.                      SWP_SIZE | SWP_MOVE | SWP_SHOW | SWP_ACTIVATE );
  144.  
  145.     WinSetWindowText( hwndFrame, PROGRAM_TITLE );
  146.  
  147.     while( WinGetMsg( hab, &qmsg, NULLHANDLE, 0, 0 ) )
  148.         WinDispatchMsg( hab, &qmsg );
  149.  
  150.     WinDestroyWindow( hwndFrame );
  151.  
  152.     WinDestroyMsgQueue( hmq );
  153.  
  154.     WinTerminate( hab );
  155.  
  156.     return 0;
  157. }
  158.  
  159. /**********************************************************************/
  160. /*----------------------------- wpClient -----------------------------*/
  161. /*                                                                    */
  162. /*  CLIENT WINDOW PROCEDURE                                           */
  163. /*                                                                    */
  164. /*  INPUT: window proc params                                         */
  165. /*                                                                    */
  166. /*  1.                                                                */
  167. /*                                                                    */
  168. /*  OUTPUT: retcode from processing message                           */
  169. /*                                                                    */
  170. /*--------------------------------------------------------------------*/
  171. /**********************************************************************/
  172. MRESULT EXPENTRY wpClient( HWND hwnd, ULONG msg, MPARAM mp1, MPARAM mp2 )
  173. {
  174.     switch( msg )
  175.     {
  176.         case WM_CREATE:
  177.  
  178.             CreateNotebook( hwnd );
  179.  
  180.             break;
  181.  
  182.  
  183.         case WM_SIZE:
  184.  
  185.             // Size the notebook with the client window
  186.  
  187.             WinSetWindowPos( WinWindowFromID( hwnd, ID_NB ), 0, 0, 0,
  188.                              SHORT1FROMMP( mp2 ), SHORT2FROMMP( mp2 ),
  189.                              SWP_SIZE | SWP_SHOW );
  190.  
  191.             break;
  192.  
  193.  
  194.         case WM_ERASEBACKGROUND:
  195.  
  196.             // Paint the client in the default background color
  197.  
  198.             return (MRESULT) TRUE;
  199.     }
  200.  
  201.     return WinDefWindowProc( hwnd, msg, mp1, mp2 );
  202. }
  203.  
  204. /**********************************************************************/
  205. /*-------------------------- CreateNotebook --------------------------*/
  206. /*                                                                    */
  207. /*  CREATE THE NOTEBOOK WINDOW                                        */
  208. /*                                                                    */
  209. /*  INPUT: client window handle                                       */
  210. /*                                                                    */
  211. /*  1.                                                                */
  212. /*                                                                    */
  213. /*  OUTPUT: nothing                                                   */
  214. /*                                                                    */
  215. /*--------------------------------------------------------------------*/
  216. /**********************************************************************/
  217. static VOID CreateNotebook( HWND hwndClient )
  218. {
  219.     HWND hwndNB;
  220.     INT  i;
  221.  
  222.     // Create the notebook. Its parent and owner will be the client window.
  223.     // Its pages will show on the bottom right of the notebook. Its major tabs
  224.     // will be on the right and they will be rounded. The status text will be
  225.     // centered. Its binding will be spiraled rather than solid. The tab text
  226.     // will be left-justified.
  227.  
  228.     hwndNB = WinCreateWindow( hwndClient, WC_NOTEBOOK, NULL,
  229.                 BKS_BACKPAGESBR | BKS_MAJORTABRIGHT | BKS_ROUNDEDTABS |
  230.                 BKS_STATUSTEXTCENTER | BKS_SPIRALBIND | BKS_TABTEXTLEFT |
  231.                 WS_GROUP | WS_TABSTOP | WS_VISIBLE,
  232.                 0, 0, 0, 0, hwndClient, HWND_TOP, ID_NB, NULL, NULL );
  233.  
  234.     SetTabDimensions( hwndNB );
  235.  
  236.     // Insert all the pages into the notebook and load and associate the dialog
  237.     // boxes with them.
  238.  
  239.     for( i = 0; i < PAGE_COUNT; i++ )
  240.         SetUpPage( hwndNB, i );
  241. }
  242.  
  243. /**********************************************************************/
  244. /*----------------------------- SetUpPage ----------------------------*/
  245. /*                                                                    */
  246. /*  SET UP A NOTEBOOK PAGE.                                           */
  247. /*                                                                    */
  248. /*  INPUT: window handle of notebook control,                         */
  249. /*         index into nbpage array                                    */
  250. /*                                                                    */
  251. /*  1.                                                                */
  252. /*                                                                    */
  253. /*  OUTPUT: nothing                                                   */
  254. /*                                                                    */
  255. /*--------------------------------------------------------------------*/
  256. /**********************************************************************/
  257. static VOID SetUpPage( HWND hwndNB, INT iPage )
  258. {
  259.     ULONG ulPageId;
  260.     HWND  hwndDlg, hwndClient = WinQueryWindow( hwndNB, QW_PARENT );
  261.  
  262.     // Insert a page into the notebook and store it in the array of page data.
  263.     // Specify that it is to have status text and the window associated with
  264.     // each page will be automatically sized by the notebook according to the
  265.     // size of the page.
  266.  
  267.     ulPageId = (ULONG) WinSendMsg( hwndNB, BKM_INSERTPAGE, NULL,
  268.                             MPFROM2SHORT( BKA_MAJOR | BKA_STATUSTEXTON |
  269.                                           BKA_AUTOPAGESIZE, BKA_LAST ) );
  270.  
  271.     // Set the text into the status line.
  272.  
  273.     WinSendMsg( hwndNB, BKM_SETSTATUSLINETEXT, MPFROMP( ulPageId ),
  274.                 MPFROMP( nbpage[ iPage ].szStatusLineText ) );
  275.  
  276.     // Set the text into the tab for this page.
  277.  
  278.     WinSendMsg( hwndNB, BKM_SETTABTEXT, MPFROMP( ulPageId ),
  279.                 MPFROMP( nbpage[ iPage ].szTabText ) );
  280.  
  281.     // Load the dialog
  282.  
  283.     hwndDlg = WinLoadDlg( hwndClient, hwndClient, nbpage[ iPage ].pfnwpDlg, 0,
  284.                           nbpage[ iPage ].idDlg, NULL );
  285.  
  286.     // Associate the dialog with the notebook page
  287.  
  288.     WinSendMsg( hwndNB, BKM_SETPAGEWINDOWHWND, MPFROMP( ulPageId ),
  289.                 MPFROMLONG( hwndDlg ) );
  290. }
  291.  
  292. /**********************************************************************/
  293. /*-------------------------- SetTabDimensions ------------------------*/
  294. /*                                                                    */
  295. /*  SET THE DIMENSIONS OF THE NOTEBOOK TABS.                          */
  296. /*                                                                    */
  297. /*  INPUT: window handle of notebook control                          */
  298. /*                                                                    */
  299. /*  1.                                                                */
  300. /*                                                                    */
  301. /*  OUTPUT: nothing                                                   */
  302. /*                                                                    */
  303. /*--------------------------------------------------------------------*/
  304. /**********************************************************************/
  305. static VOID SetTabDimensions( HWND hwndNB )
  306. {
  307.     HPS          hps = WinGetPS( hwndNB );
  308.     FONTMETRICS  fm;
  309.     INT          i, iLongestText = 0;
  310.     POINTL       aptl[ TXTBOX_COUNT ];
  311.  
  312.     (void) memset( &fm, 0, sizeof( FONTMETRICS ) );
  313.  
  314.     // Calculate the height of a tab as the height of an average font character
  315.     // plus a margin value.
  316.  
  317.     GpiQueryFontMetrics( hps, sizeof( FONTMETRICS ), &fm );
  318.  
  319.     fm.lMaxBaselineExt += TAB_HEIGHT_MARGIN;
  320.  
  321.     // Calculate the longest tab text
  322.  
  323.     for( i = 0; i < PAGE_COUNT; i++ )
  324.     {
  325.         GpiQueryTextBox( hps, strlen( nbpage[ i ].szTabText ),
  326.                          nbpage[ i ].szTabText, TXTBOX_COUNT, aptl );
  327.  
  328.         if( aptl[ TXTBOX_CONCAT ].x > iLongestText )
  329.             iLongestText = aptl[ TXTBOX_CONCAT ].x;
  330.     }
  331.  
  332.     WinReleasePS( hps );
  333.  
  334.     // Add a margin amount to the longest tab text
  335.  
  336.     iLongestText += TAB_WIDTH_MARGIN;
  337.  
  338.     // Set the tab dimensions for the pages. Note that the docs as of this
  339.     // writing say to use BKA_MAJOR and BKA_MINOR in mp2 but you really need
  340.     // BKA_MAJORTAB and BKA_MINORTAB.
  341.  
  342.     WinSendMsg( hwndNB, BKM_SETDIMENSIONS,
  343.                 MPFROM2SHORT( iLongestText, (SHORT)fm.lMaxBaselineExt ),
  344.                 MPFROMSHORT( BKA_MAJORTAB ) );
  345. }
  346.  
  347. /**********************************************************************/
  348. /*----------------------------- wpPage1 ------------------------------*/
  349. /*                                                                    */
  350. /*  WINDOW PROCEDURE FOR PAGE 1 DIALOG                                */
  351. /*                                                                    */
  352. /*  INPUT: window handle, message id, message parameter 1 and 2.      */
  353. /*                                                                    */
  354. /*  1.                                                                */
  355. /*                                                                    */
  356. /*  OUTPUT: return code                                               */
  357. /*--------------------------------------------------------------------*/
  358. /**********************************************************************/
  359. MRESULT EXPENTRY wpPage1( HWND hwnd, ULONG msg, MPARAM mp1, MPARAM mp2 )
  360. {
  361.     return WinDefDlgProc( hwnd, msg, mp1, mp2 );
  362. }
  363.  
  364. /**********************************************************************/
  365. /*----------------------------- wpPage2 ------------------------------*/
  366. /*                                                                    */
  367. /*  WINDOW PROCEDURE FOR PAGE 2 DIALOG                                */
  368. /*                                                                    */
  369. /*  INPUT: window handle, message id, message parameter 1 and 2.      */
  370. /*                                                                    */
  371. /*  1.                                                                */
  372. /*                                                                    */
  373. /*  OUTPUT: return code                                               */
  374. /*--------------------------------------------------------------------*/
  375. /**********************************************************************/
  376. MRESULT EXPENTRY wpPage2( HWND hwnd, ULONG msg, MPARAM mp1, MPARAM mp2 )
  377. {
  378.     return WinDefDlgProc( hwnd, msg, mp1, mp2 );
  379. }
  380.  
  381. /**********************************************************************/
  382. /*----------------------------- wpPage3 ------------------------------*/
  383. /*                                                                    */
  384. /*  WINDOW PROCEDURE FOR PAGE 3 DIALOG                                */
  385. /*                                                                    */
  386. /*  INPUT: window handle, message id, message parameter 1 and 2.      */
  387. /*                                                                    */
  388. /*  1.                                                                */
  389. /*                                                                    */
  390. /*  OUTPUT: return code                                               */
  391. /*--------------------------------------------------------------------*/
  392. /**********************************************************************/
  393. MRESULT EXPENTRY wpPage3( HWND hwnd, ULONG msg, MPARAM mp1, MPARAM mp2 )
  394. {
  395.     return WinDefDlgProc( hwnd, msg, mp1, mp2 );
  396. }
  397.  
  398. /**********************************************************************/
  399. /*----------------------------- wpPage4 ------------------------------*/
  400. /*                                                                    */
  401. /*  WINDOW PROCEDURE FOR PAGE 4 DIALOG                                */
  402. /*                                                                    */
  403. /*  INPUT: window handle, message id, message parameter 1 and 2.      */
  404. /*                                                                    */
  405. /*  1.                                                                */
  406. /*                                                                    */
  407. /*  OUTPUT: return code                                               */
  408. /*--------------------------------------------------------------------*/
  409. /**********************************************************************/
  410. MRESULT EXPENTRY wpPage4( HWND hwnd, ULONG msg, MPARAM mp1, MPARAM mp2 )
  411. {
  412.     return WinDefDlgProc( hwnd, msg, mp1, mp2 );
  413. }
  414.  
  415. /************************************************************************
  416.  *                      E N D   O F   S O U R C E                       *
  417.  ************************************************************************/
  418.