home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 1998 May / Pcwk5b98.iso / Borland / Cplus45 / BC45 / OWL1.PAK / CHECKBOX.CPP < prev    next >
Text File  |  1995-08-29  |  4KB  |  131 lines

  1. // ObjectWindows - (C) Copyright 1992 by Borland International
  2.  
  3. /* --------------------------------------------------------
  4.   CHECKBOX.CPP
  5.   Defines type TCheckBox.  This defines the basic behavior
  6.   for all check boxes.
  7.   -------------------------------------------------------- */
  8.  
  9. #include "checkbox.h"
  10. #include "groupbox.h"
  11.  
  12. /* Constructor for a TCheckBox object.  Initializes its data
  13.    fields using passed parameters and default values. */
  14. TCheckBox::TCheckBox(PTWindowsObject AParent, int AnId, LPSTR ATitle,
  15.                      int X, int Y, int W, int H, PTGroupBox AGroup,
  16.                      PTModule AModule)
  17.                : TButton(AParent, AnId, ATitle, X, Y, W, H, FALSE, AModule)
  18. {
  19.   Group = AGroup;
  20. /* Don't use TButton's inherited style - it conflicts with
  21.    BS_AUTOCHECKBOX. */
  22.   Attr.Style = WS_CHILD | WS_VISIBLE | WS_TABSTOP | BS_AUTOCHECKBOX;
  23. }
  24.  
  25. /* Constructor for a TCheckBox to be associated with a MS-Windows
  26.   interface element created by MS-Windows from a resource definition.
  27.   Initializes its data fields using passed parameters.  Data transfer
  28.   is enabled for the TCheckBox. */
  29. TCheckBox::TCheckBox(PTWindowsObject AParent, int ResourceId,
  30.                      PTGroupBox AGroup, PTModule AModule)
  31.                : TButton(AParent, ResourceId, AModule)
  32. {
  33.   Group = AGroup;
  34.   EnableTransfer();
  35. }
  36.  
  37. /* Transfers state information for the TCheckBox. The TransferFlag
  38.   passed specifies whether data is to be read from or written to
  39.   the passed buffer, or whether the data element size is simply to
  40.   be returned. The return value is the size (in bytes) of the
  41.   transfer data. */
  42. WORD TCheckBox::Transfer(Pvoid DataPtr, WORD TransferFlag)
  43. {
  44.     WORD CheckFlag;
  45.  
  46.   if ( TransferFlag == TF_GETDATA )
  47.   {
  48.     CheckFlag = GetCheck();
  49.     _fmemcpy(DataPtr, &CheckFlag, sizeof(CheckFlag));
  50.   }
  51.   else
  52.     if ( TransferFlag == TF_SETDATA )
  53.       SetCheck( *(WORD *)DataPtr );
  54.   return sizeof(CheckFlag);
  55. }
  56.  
  57. /* Returns the check state of the associated check box.  Returns
  58.    BF_UNCHECKED (0), BF_CHECKED(1), or (if 3-state) BF_GRAYED(2). */
  59. WORD TCheckBox::GetCheck()
  60. {
  61.   return (WORD)SendMessage(HWindow, BM_GETCHECK, 0, 0L);
  62. }
  63.  
  64. /* Sets the check state of the associated check box.  Unchecks, checks,
  65.    or grays the checkbox (if 3-state) according to the CheckFlag passed.
  66.    (Pass BF_UNCHECKED (0), BF_CHECKED (1), or BF_GRAYED (2)). If a Group
  67.    has been specified for the TCheckBox, notifies the Group that the
  68.    state of the check box has changed. */
  69. void TCheckBox::SetCheck(WORD CheckFlag)
  70. {
  71.   SendMessage(HWindow, BM_SETCHECK, CheckFlag, 0);
  72.   if ( Group )
  73.     Group->SelectionChanged(Attr.Id);
  74. }
  75.  
  76. /* Places a checkmark in associated check box. */
  77. void TCheckBox::Check()
  78. {
  79.   SetCheck(BF_CHECKED);
  80. }
  81.  
  82. /* Removes a checkmark from the associated check box. */
  83. void TCheckBox::Uncheck()
  84. {
  85.   SetCheck(BF_UNCHECKED);
  86. }
  87.  
  88. /* Toggles the check state of the check box. */
  89. void TCheckBox::Toggle()
  90. {
  91.     if ( (GetWindowLong(HWindow, GWL_STYLE) & BS_AUTO3STATE)
  92.                                      == BS_AUTO3STATE )
  93.     SetCheck((GetCheck() + 1) % 3);
  94.     else
  95.     SetCheck((GetCheck() + 1) % 2);
  96. }
  97.  
  98. /* Responds to an incoming BN_CLICKED message.  If a Group has been
  99.   specified for the TCheckBox, notifies the Group that the state of
  100.   this TCheckBox has changed. */
  101. void TCheckBox::BNClicked(TMessage& Msg)
  102. {
  103.   DefWndProc(Msg);
  104.   if ( Group )
  105.     Group->SelectionChanged(Attr.Id);
  106.   DefNotificationProc(Msg);
  107. }
  108.  
  109. /* Reads an instance of TCheckBox from the passed ipstream. */
  110. void *TCheckBox::read(ipstream& is)
  111. {
  112.   TWindow::read(is);
  113.   GetSiblingPtr(is, (PTWindowsObject)Group);
  114.   return this;
  115. }
  116.  
  117. /* Writes the TCheckBox to the passed opstream. */
  118. void TCheckBox::write(opstream& os)
  119. {
  120.   TWindow::write(os);
  121.   PutSiblingPtr(os, Group);
  122.   }
  123.  
  124. TStreamable *TCheckBox::build()
  125. {
  126.   return new TCheckBox(streamableInit);
  127. }
  128.  
  129. TStreamableClass RegCheckBox("TCheckBox", TCheckBox::build,
  130.                  __DELTA(TCheckBox));
  131.