home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / NBSIZE.ZIP / DIALOG.C < prev    next >
Text File  |  1993-01-04  |  16KB  |  353 lines

  1. /*********************************************************************
  2.  *                                                                   *
  3.  * MODULE NAME :  dialog.c               AUTHOR:  Rick Fishman       *
  4.  * DATE WRITTEN:  11-28-92                                           *
  5.  *                                                                   *
  6.  * MODULE DESCRIPTION:                                               *
  7.  *                                                                   *
  8.  *  Module that loads a dialog and associates it with the Dialog     *
  9.  *  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_WINDIALOGS
  30. #define  INCL_WINERRORS
  31. #define  INCL_WINFRAMEMGR
  32. #define  INCL_WINMESSAGEMGR
  33. #define  INCL_WINSTDBOOK
  34. #define  INCL_WINWINDOWMGR
  35.  
  36. /**********************************************************************/
  37. /*----------------------------- INCLUDES -----------------------------*/
  38. /**********************************************************************/
  39.  
  40. #include <os2.h>
  41. #include <stdio.h>
  42. #include <stdarg.h>
  43. #include <stdlib.h>
  44. #include <string.h>
  45. #include "nbsize.h"
  46.  
  47. /*********************************************************************/
  48. /*------------------- APPLICATION DEFINITIONS -----------------------*/
  49. /*********************************************************************/
  50.  
  51. INT idButton[] = { PB_UNDO, PB_DEFAULT, PB_HELP };
  52.  
  53. #define BUTTON_COUNT    (sizeof( idButton ) / sizeof( INT ))
  54.  
  55. #define CONTROL_COUNT   BUTTON_COUNT + 1    // Add the MLE
  56.  
  57. #define BUTTONS_X          12  // Left origin of buttons on dlg   (dialog units)
  58. #define BUTTONS_Y          5   // Botton origin of buttons on dlg (dialog units)
  59. #define BUTTON_MAX_CX      65  // Maximum width of button         (dialog units)
  60. #define BUTTON_MAX_CY      15  // Maximum heigth of button        (dialog units)
  61. #define BUTTON_MIN_SPACING 3   // Minimum spacing betw buttons    (dialog units)
  62.  
  63. #define MLE_TOP_CUSHION    5   // Space betw top of dlg and MLE   (dialog units)
  64. #define MLE_MIN_CY         20  // Minimum height of the MLE       (dialog units)
  65. #define MLE_X              6   // x origin of MLE w/in dialog box (dialog units)
  66. #define MLE_Y_OVER_BUTTONS 5   // y origin of MLE above buttons   (dialog units)
  67.  
  68. /**********************************************************************/
  69. /*----------------------- FUNCTION PROTOTYPES ------------------------*/
  70. /**********************************************************************/
  71.  
  72. static VOID wmSize         ( HWND hwndDlg, ULONG cxNew, ULONG cyNew );
  73. static INT  CalcButtonParms( ULONG cxDlg, INT iButtonCount, PINT pcxButton,
  74.                              PINT pcyButton );
  75. static VOID CalcMLESwp     ( HWND hwndDlg, INT cxDlg, INT cyDlg, PSWP pswp,
  76.                              PINT pcyButton );
  77.  
  78. static FNWP wpDlg;
  79.  
  80. /**********************************************************************/
  81. /*------------------------ GLOBAL VARIABLES --------------------------*/
  82. /**********************************************************************/
  83.  
  84. static POINTL ptlBtns;              // x,y origin of buttons
  85. static SIZEL  sizlMaxBtn;           // Maximum cx,cy of button
  86. static INT    cxMinBtnSpacing,      // Minimum spacing between buttons
  87.               cyMinMle,             // Minimum height of an MLE
  88.               cyMleTopCushion,      // Space betw top of MLE and top of dlgbox
  89.               xMle,                 // x origin of MLE within dialog box
  90.               yMleAboveButtons;     // y origin of MLE from top of buttons
  91.  
  92. /**********************************************************************/
  93. /*------------------------ CreateDialogPage --------------------------*/
  94. /*                                                                    */
  95. /*  LOAD DIALOG AND ASSOCIATE IT WITH A NOTEBOOK PAGE                 */
  96. /*                                                                    */
  97. /*  INPUT: window handle of parent and owner,                         */
  98. /*         notebook window handle,                                    */
  99. /*         notebook page ID                                           */
  100. /*                                                                    */
  101. /*  1.                                                                */
  102. /*                                                                    */
  103. /*  OUTPUT: client window handle                                      */
  104. /*                                                                    */
  105. /*--------------------------------------------------------------------*/
  106. /**********************************************************************/
  107. HWND CreateDialogPage( HWND hwndParent, HWND hwndNB, ULONG ulPageID )
  108. {
  109.     HWND hwndDlg = WinLoadDlg( hwndParent, hwndParent, wpDlg, 0, IDD_DIALOG,
  110.                                NULL );
  111.  
  112.     if( hwndDlg )
  113.     {
  114.         if( WinSendMsg( hwndNB, BKM_SETPAGEWINDOWHWND,
  115.                         MPFROMLONG( ulPageID ), MPFROMLONG( hwndDlg ) ) )
  116.         {
  117.             POINTL aptl[ 5 ];
  118.  
  119.             aptl[ 0 ].x = BUTTONS_X;
  120.             aptl[ 0 ].y = BUTTONS_Y;
  121.             aptl[ 1 ].x = BUTTON_MAX_CX;
  122.             aptl[ 1 ].y = BUTTON_MAX_CY;
  123.             aptl[ 2 ].x = BUTTON_MIN_SPACING;
  124.             aptl[ 2 ].y = MLE_MIN_CY;
  125.             aptl[ 3 ].x = MLE_X;
  126.             aptl[ 3 ].y = MLE_Y_OVER_BUTTONS;
  127.             aptl[ 4 ].x = 0;
  128.             aptl[ 4 ].y = MLE_TOP_CUSHION;
  129.  
  130.             if( WinMapDlgPoints( HWND_DESKTOP, aptl, 4, TRUE ) )
  131.             {
  132.                 ptlBtns  = aptl[ 0 ];
  133.  
  134.                 sizlMaxBtn.cx = aptl[ 1 ].x;
  135.                 sizlMaxBtn.cy = aptl[ 1 ].y;
  136.  
  137.                 cxMinBtnSpacing = aptl[ 2 ].x;
  138.  
  139.                 cyMinMle = aptl[ 2 ].y;
  140.  
  141.                 xMle = aptl[ 3 ].x;
  142.                 yMleAboveButtons = aptl[ 3 ].y;
  143.  
  144.                 cyMleTopCushion = aptl[ 4 ].y;
  145.             }
  146.             else
  147.                 Msg( "CreateDialogPage WinMagDlgPoints RC(%X)",
  148.                      HWNDERR( hwndNB ) );
  149.         }
  150.         else
  151.         {
  152.             WinDestroyWindow( hwndDlg );
  153.  
  154.             hwndDlg = NULLHANDLE;
  155.  
  156.             Msg( "CreateDialogPage SETPAGEWINDOWHWND RC(%X)",
  157.                  HWNDERR( hwndNB ) );
  158.         }
  159.     }
  160.     else
  161.         Msg( "CreateDialogPage WinLoadDlg RC(%X)", HWNDERR( hwndNB ) );
  162.  
  163.     return hwndDlg;
  164. }
  165.  
  166. /**********************************************************************/
  167. /*------------------------------ wpDlg -------------------------------*/
  168. /*                                                                    */
  169. /*  DIALOG BOX WINDOW PROCEDURE                                       */
  170. /*                                                                    */
  171. /*  INPUT: window handle, message id, message parameter 1 and 2.      */
  172. /*                                                                    */
  173. /*  1.                                                                */
  174. /*                                                                    */
  175. /*  OUTPUT: return code                                               */
  176. /*                                                                    */
  177. /*--------------------------------------------------------------------*/
  178. /**********************************************************************/
  179. static MRESULT EXPENTRY wpDlg( HWND hwnd, ULONG msg, MPARAM mp1, MPARAM mp2 )
  180. {
  181.     switch( msg )
  182.     {
  183.         // Calling WinDefWindowProc for this message will generate a WM_SIZE
  184.         // message. We'll use the WM_ADJUSTWINDOWPOS message though...
  185.         // (Normally dialogs don't get WM_SIZE messages)
  186.  
  187.         case WM_WINDOWPOSCHANGED:
  188.  
  189.             WinDefWindowProc( hwnd, msg, mp1, mp2 );
  190.  
  191.             break;
  192.  
  193.  
  194.         case WM_SIZE:
  195.  
  196.             break;
  197.  
  198.  
  199.         case WM_ADJUSTWINDOWPOS:
  200.         {
  201.             PSWP pswp = (PSWP) mp1;
  202.  
  203.             if( (pswp->fl & SWP_SIZE) && pswp->cx && pswp->cy )
  204.                 wmSize( hwnd, pswp->cx, pswp->cy );
  205.  
  206.             break;
  207.         }
  208.  
  209.         case WM_COMMAND:
  210.  
  211.             return 0;
  212.     }
  213.  
  214.     return WinDefDlgProc( hwnd, msg, mp1, mp2 );
  215. }
  216.  
  217. /**********************************************************************/
  218. /*------------------------------ wmSize ------------------------------*/
  219. /*                                                                    */
  220. /*  PROCESS A SIZE FOR THE DIALOG BOX (WM_ADJUSTWINDOWPOS).           */
  221. /*                                                                    */
  222. /*  INPUT: dialog box window handle,                                  */
  223. /*         new window width,                                          */
  224. /*         new window height                                          */
  225. /*                                                                    */
  226. /*  1.                                                                */
  227. /*                                                                    */
  228. /*  OUTPUT: nothing                                                   */
  229. /*                                                                    */
  230. /*--------------------------------------------------------------------*/
  231. /**********************************************************************/
  232. static VOID wmSize( HWND hwndDlg, ULONG cxNew, ULONG cyNew )
  233. {
  234.     SWP swpDlg;
  235.  
  236.     if( WinQueryWindowPos( hwndDlg, &swpDlg ) )
  237.     {
  238.         if( swpDlg.cx != cxNew || swpDlg.cy != cyNew )
  239.         {
  240.             SWP aswp[ CONTROL_COUNT ];
  241.             INT i, cxButton, cyButton, xButton = ptlBtns.x;
  242.             INT iButtonSpacing = CalcButtonParms( cxNew, BUTTON_COUNT,
  243.                                                   &cxButton, &cyButton );
  244.  
  245.             CalcMLESwp( hwndDlg, cxNew, cyNew, &aswp[ 0 ], &cyButton );
  246.  
  247.             for( i = 1; i <= BUTTON_COUNT; i++ )
  248.             {
  249.                 aswp[ i ].hwnd = WinWindowFromID( hwndDlg, idButton[ i - 1 ] );
  250.                 aswp[ i ].hwndInsertBehind = HWND_BOTTOM;
  251.                 aswp[ i ].x                = xButton;
  252.                 aswp[ i ].y                = ptlBtns.y;
  253.                 aswp[ i ].cx               = cxButton;
  254.                 aswp[ i ].cy               = cyButton;
  255.                 aswp[ i ].fl               = SWP_SIZE | SWP_MOVE | SWP_SHOW;
  256.  
  257.                 xButton += (cxButton + iButtonSpacing);
  258.             }
  259.  
  260.             if( !WinSetMultWindowPos( ANCHOR( hwndDlg ), aswp, CONTROL_COUNT ) )
  261.                 Msg( "DialogPage wmSize WinSetMultWindowPos RC(%X)",
  262.                      HWNDERR( hwndDlg ) );
  263.         }
  264.     }
  265.     else
  266.         Msg( "DialogPage wmSize WinQueryWindowPos RC(%X)", HWNDERR( hwndDlg ) );
  267.  
  268.     return;
  269. }
  270.  
  271. /**********************************************************************/
  272. /*-------------------------- CalcButtonParms -------------------------*/
  273. /*                                                                    */
  274. /*  CALCULATE SPACING BETWEEN BUTTONS AND BUTTON HEIGHT.              */
  275. /*                                                                    */
  276. /*  INPUT: width of the dialog box,                                   */
  277. /*         number of buttons,                                         */
  278. /*         pointer to variable to hold width of button,               */
  279. /*         pointer to variable to hold height of button               */
  280. /*                                                                    */
  281. /*  1 -                                                               */
  282. /*                                                                    */
  283. /*  OUTPUT: button spacing in pixels                                  */
  284. /*                                                                    */
  285. /*--------------------------------------------------------------------*/
  286. /**********************************************************************/
  287. static INT CalcButtonParms( ULONG cxDlg, INT iButtonCount, PINT pcxButton,
  288.                             PINT pcyButton )
  289. {
  290.     INT iSpacing, iSpaceLeft = cxDlg - (ptlBtns.x * 2);
  291.  
  292.     *pcxButton = sizlMaxBtn.cx;
  293.     *pcyButton = sizlMaxBtn.cy;
  294.  
  295.     iSpaceLeft -= (iButtonCount * sizlMaxBtn.cx);
  296.  
  297.     iSpacing = iSpaceLeft / (iButtonCount - 1);
  298.  
  299.     if( iSpacing < cxMinBtnSpacing )
  300.     {
  301.         iSpacing = cxMinBtnSpacing;
  302.  
  303.         *pcxButton = ((cxDlg - (ptlBtns.x * 2)) -
  304.                       (iSpacing * (iButtonCount - 1))) / iButtonCount;
  305.     }
  306.  
  307.     return iSpacing;
  308. }
  309.  
  310. /**********************************************************************/
  311. /*---------------------------- CalcMLESwp ----------------------------*/
  312. /*                                                                    */
  313. /*  FILL IN THE SWP STRUCTURE FOR THE MLE.                            */
  314. /*                                                                    */
  315. /*  INPUT: dialog box window handle,                                  */
  316. /*         dialog box width,                                          */
  317. /*         dialog box height,                                         */
  318. /*         pointer to SWP structure for MLE,                          */
  319. /*         pointer to variable holding height of buttons              */
  320. /*                                                                    */
  321. /*  1 -                                                               */
  322. /*                                                                    */
  323. /*  OUTPUT: nothing                                                   */
  324. /*                                                                    */
  325. /*--------------------------------------------------------------------*/
  326. /**********************************************************************/
  327. static VOID CalcMLESwp( HWND hwndDlg, INT cxDlg, INT cyDlg, PSWP pswp,
  328.                         PINT pcyButton )
  329. {
  330.     pswp->hwnd             = WinWindowFromID( hwndDlg, MLE_ON_DIALOG );
  331.     pswp->hwndInsertBehind = HWND_BOTTOM;
  332.     pswp->x                = xMle;
  333.     pswp->cx               = cxDlg - (xMle * 2);
  334.     pswp->fl               = SWP_SIZE | SWP_MOVE | SWP_SHOW;
  335.     pswp->y                = yMleAboveButtons + ptlBtns.y + *pcyButton;
  336.     pswp->cy               = (cyDlg - pswp->y) - cyMleTopCushion;
  337.  
  338.     if( pswp->cy < cyMinMle )
  339.     {
  340.         *pcyButton -= (cyMinMle - pswp->cy);
  341.  
  342.         pswp->y -= (cyMinMle - pswp->cy);
  343.  
  344.         pswp->cy = cyMinMle;
  345.     }
  346.  
  347.     return;
  348. }
  349.  
  350. /************************************************************************
  351.  *                      E N D   O F   S O U R C E                       *
  352.  ************************************************************************/
  353.