home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / dovetail.zip / rbutton.cc < prev    next >
C/C++ Source or Header  |  1994-04-07  |  3KB  |  117 lines

  1. #define INCL_WIN
  2. #include <os2.h>
  3. #include <stdio.h>
  4. #include "rbutton.h"
  5.  
  6. /*
  7.   constructor to use in case the buttons already exist.
  8.   Used by dialog boxes that are defined in the resource file
  9. */
  10. Rbutton::Rbutton(HWND hDlg, ULONG *ulId)
  11.    {
  12.     //  Create an array of radiobuttons.
  13.     int i;
  14.  
  15.     for(i=0; ulId[i]; i++)
  16.         ;
  17.     num_buttons = i;
  18.     pControls = new Control [i];
  19.     for(i=num_buttons-1; i>=0; i--)
  20.       pControls[i].hWnd = WinWindowFromID(hDlg,ulId[i]);
  21. }
  22.  
  23. /*
  24.   constructor to call when the buttons must be created on the fly
  25. */
  26. Rbutton::Rbutton(HWND hParent, ULONG *ulId, char **title, SHORT xp, SHORT yp,
  27.                  SHORT dx, SHORT dy)
  28.    {
  29.     //  Create an array of radiobuttons.
  30.   /*
  31.     for some mysterious reason, everything works correctly
  32.     if I create the buttons in reverse order.  I suspect the
  33.     resource compiler must do this or something.  In any
  34.     case, it works.  Two things appear weird about this:
  35.     the LAST button created gets the WS_GROUP identifier,
  36.     and it is index 0 when BM_QUERYCHECKINDEX is used.
  37.   */
  38.     int i;
  39.  
  40.     for(i=0; ulId[i]; i++)
  41.         ;
  42.     num_buttons = i;
  43.     dy /= num_buttons;
  44.     pControls = new Control [i];
  45.     for(i=num_buttons-1; i>=0; i--) {
  46.  
  47.    pControls[i].hWnd = WinCreateWindow(
  48.               hParent,       /* Parent Window Handle */
  49.               WC_BUTTON,     /* Button Window class */
  50.               (PSZ) title[i],         /* Button Title */
  51.               BS_AUTORADIOBUTTON |
  52.               ((i == 0) ? WS_GROUP : 0),       /* Button style */
  53.               xp,   /* Xcoord */
  54.               yp+i*dy,   /* Ycoord */
  55.               dx,   /* Width */
  56.               dy,    /* Height */
  57.               hParent,       /* Owner Window Handle */
  58.               HWND_TOP,      /* Top of Z order */
  59.               ulId[i],          /* Window ID */
  60.               (PVOID)NULL,   /* Control Data Structure */
  61.               (PVOID)NULL);  /* no presentation parameters */
  62.     pControls[i].fCreated = TRUE;
  63.   }
  64. }
  65.  
  66. // make invisible
  67. BOOL Rbutton::Hide()
  68. {
  69.   int i;
  70.   BOOL b=TRUE;
  71.     for(i=0; i<num_buttons; i++)
  72.        b = b && pControls[i].Hide();
  73. return b;
  74. }
  75.  
  76. // make visible
  77. BOOL Rbutton::Show()
  78. {
  79. int i;
  80. BOOL b = TRUE;
  81.   for(i=0; i<num_buttons; i++)
  82.      b = b && pControls[i].Show();
  83. return b;
  84. }
  85.  
  86. Rbutton::~Rbutton()
  87. {
  88. int i;
  89.   delete []  pControls;
  90. }
  91.  
  92. // return index of currently chosen radiobutton
  93. Rbutton::operator ULONG()
  94. {
  95.   return (ULONG) WinSendMsg(
  96.                pControls[0].hWnd,     /* my window handle */
  97.                BM_QUERYCHECKINDEX, /* Message for querrying buttons */
  98.                NULL,
  99.                NULL);
  100. }
  101.  
  102. // set current radio button
  103. VOID Rbutton::SetState(ULONG ulWhich)
  104. {
  105.   if(ulWhich >= num_buttons) ulWhich = 0;
  106.   WinSendMsg(pControls[ulWhich].hWnd,BM_SETCHECK, (MPARAM) TRUE,NULL);
  107. }
  108.  
  109. // adjust position/sizes of windows to fit given box.
  110. VOID Rbutton::SetPos(SHORT xp, SHORT yp, SHORT width, SHORT height)
  111. {
  112. int i,dy;
  113. dy = height / num_buttons;
  114. for(i=0; i<num_buttons; i++)
  115.    pControls[i].SetPos(xp,yp+i*dy,width,dy);
  116. }
  117.