home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / BTNPLC.ZIP / BTNPLC.C
C/C++ Source or Header  |  1990-06-22  |  5KB  |  175 lines

  1. #define INCL_WINWINDOWMGR
  2. #include <os2.h>
  3.  
  4. #include <malloc.h>
  5.  
  6. USHORT  PlaceDialogButtons
  7. (
  8.     HAB     hab,                /* Anchor block handle  */
  9.     HWND    hwndDlg,            /* Dialog window handle */
  10.     SHORT   cButtons,           /* Count of buttons     */
  11.     PSHORT  aidButtons          /* Array of button ids  */
  12. );
  13.  
  14.  
  15. /*********************************************************************
  16.  
  17.     FUNCTION:
  18.  
  19.         PlaceDialogButtons
  20.  
  21.     PURPOSE:
  22.  
  23.         Place dialog buttons evenly across bottom of dialog box
  24.  
  25.     COPYRIGHT:
  26.  
  27.         Version 1.0, June 17, 1990
  28.  
  29.         Copyright 1990, David C. Briccetti
  30.         All Rights Reserved
  31.  
  32.         May be used for any purpose but not sold
  33.  
  34.     DESCRIPTION:
  35.  
  36.         This function dynamically positions dialog pushbuttons across
  37.         the bottom of a dialog box.  It is useful in cases where
  38.         different pushbuttons appear in different circumstances.
  39.  
  40.         For example, you may have a dialog box like the following:
  41.  
  42.         +---------------------------------+
  43.         |                                 |
  44.         |               ...               |
  45.         |                                 |
  46.         |                                 |
  47.         |  +----+  +--------+  +-------+  |
  48.         |  | Ok |  | Cancel |  | Print |  |
  49.         |  +----+  +--------+  +-------+  |
  50.         |                                 |
  51.         +---------------------------------+
  52.  
  53.         In some cases you may want the dialog box to appear without
  54.         the Print pushbutton.  You can use the same dialog box template
  55.         but hide the Print button in the WM_INITDLG message processing.
  56.         Then the box will look like this:
  57.  
  58.         +---------------------------------+
  59.         |                                 |
  60.         |               ...               |
  61.         |                                 |
  62.         |                                 |
  63.         |  +----+  +--------+             |
  64.         |  | Ok |  | Cancel |             |
  65.         |  +----+  +--------+             |
  66.         |                                 |
  67.         +---------------------------------+
  68.  
  69.         The remaining buttons are not evenly spaced, but you can call
  70.         PlaceDialogButtons which moves them so that they appear as
  71.         follows:
  72.  
  73.         +---------------------------------+
  74.         |                                 |
  75.         |               ...               |
  76.         |                                 |
  77.         |                                 |
  78.         |      +----+      +--------+     |
  79.         |      | Ok |      | Cancel |     |
  80.         |      +----+      +--------+     |
  81.         |                                 |
  82.         +---------------------------------+
  83.  
  84.         Before calling the function you allocate an array containing
  85.         the button identifiers.  The code to position the buttons
  86.         looks like this:
  87.  
  88.             PSHORT          aidButtons;
  89.  
  90.             aidButtons    = malloc (2 * sizeof (SHORT));
  91.             aidButtons[0] = DID_OK;
  92.             aidButtons[1] = DID_CANCEL;
  93.             PlaceDialogButtons (hab, hwndDlg, 2, aidButtons);
  94.             free (aidButtons);
  95.  
  96.  *********************************************************************/
  97.  
  98. USHORT  PlaceDialogButtons
  99. (
  100.     HAB     hab,                /* Anchor block handle  */
  101.     HWND    hwndDlg,            /* Dialog window handle */
  102.     SHORT   cButtons,           /* Count of buttons     */
  103.     PSHORT  aidButtons          /* Array of button ids  */
  104. )
  105. {
  106.     SWP     swpDlg;             /* SWP structure for width of dlg box   */
  107.     PSWP    aswpButtons;        /* Array of SWP structures for buttons  */
  108.     SHORT   cxTotalButtonWidths;/* Total of button widths               */
  109.     SHORT   cxFreeSpace;        /* Free space not used by buttons       */
  110.     SHORT   cxButtonMargin;     /* Space between buttons                */
  111.     SHORT   xCurrentButton;     /* Var for placement of each button     */
  112.     SHORT   i;                  /* Loop variable                        */
  113.  
  114.     WinQueryWindowPos (hwndDlg, &swpDlg);   /* Get dlg box dimensions   */
  115.  
  116.  
  117.     /*
  118.      *  Allocate space for array of SWP structures for
  119.      *  positioning buttons
  120.      */
  121.  
  122.     aswpButtons = malloc (cButtons * sizeof (SWP));
  123.  
  124.  
  125.     /*
  126.      *  Get button position info, and sum button widths
  127.      */
  128.  
  129.     for (i = 0, cxTotalButtonWidths = 0; i < cButtons; ++i )
  130.     {
  131.         WinQueryWindowPos (
  132.             WinWindowFromID (hwndDlg, aidButtons[i]),
  133.             &aswpButtons[i]);
  134.         cxTotalButtonWidths += aswpButtons[i].cx;
  135.     }
  136.  
  137.  
  138.     /*
  139.      *  Calculate free width and new button margins
  140.      */
  141.  
  142.     cxFreeSpace    = swpDlg.cx - cxTotalButtonWidths;
  143.     cxButtonMargin = cxFreeSpace / (cButtons + 1);
  144.  
  145.  
  146.     /*
  147.      *  Adjust SWP structures for moving buttons to new
  148.      *  positions
  149.      */
  150.  
  151.     for (i = 0, xCurrentButton = cxButtonMargin;
  152.         i < cButtons; ++i )
  153.     {
  154.         aswpButtons[i].x   = xCurrentButton;
  155.         aswpButtons[i].fs  = SWP_MOVE;
  156.         xCurrentButton    += (aswpButtons[i].cx + cxButtonMargin);
  157.     }
  158.  
  159.  
  160.     /*
  161.      *  Move buttons
  162.      */
  163.  
  164.     WinSetMultWindowPos (hab, aswpButtons, cButtons);
  165.  
  166.  
  167.     /*
  168.      *  Clean up
  169.      */
  170.  
  171.     free (aswpButtons);
  172.  
  173.     return 0;
  174. }
  175.