home *** CD-ROM | disk | FTP | other *** search
/ C Programming Starter Kit 2.0 / SamsPublishing-CProgrammingStarterKit-v2.0-Win31.iso / bc45 / owlsrc.pak / CHECKBOX.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1997-07-24  |  3.9 KB  |  162 lines

  1. //----------------------------------------------------------------------------
  2. // ObjectWindows
  3. // (C) Copyright 1991, 1994 by Borland International, All Rights Reserved
  4. //
  5. //   Implemenation ofclass TCheckBox.  This defines the basic behavior
  6. //   for all check boxes.
  7. //----------------------------------------------------------------------------
  8. #pragma hdrignore SECTION
  9. #include <owl/owlpch.h>
  10. #include <owl/checkbox.h>
  11. #include <owl/groupbox.h>
  12. #include <owl/applicat.h>
  13. #include <bwcc.h>
  14.  
  15. #if !defined(SECTION) || SECTION == 1
  16.  
  17. DEFINE_RESPONSE_TABLE1(TCheckBox, TButton)
  18.   EV_WM_GETDLGCODE,
  19.   EV_NOTIFY_AT_CHILD(BN_CLICKED, BNClicked),
  20. END_RESPONSE_TABLE;
  21.  
  22. //
  23. // constructor for a TCheckBox object
  24. //
  25. TCheckBox::TCheckBox(TWindow*        parent,
  26.                      int             id,
  27.                      const char far* title,
  28.                      int x, int y, int w, int h,
  29.                      TGroupBox*      group,
  30.                      TModule*        module)
  31. :
  32.   TButton(parent, id, title, x, y, w, h, false, module)
  33. {
  34.   Group = group;
  35.   //
  36.   // Don't use TButton's inherited style - it conflicts with BS_AUTOCHECKBOX
  37.   //
  38.   Attr.Style = WS_CHILD | WS_VISIBLE | WS_TABSTOP | BS_AUTOCHECKBOX;
  39. }
  40.  
  41. //
  42. // constructor for a TCheckBox to be associated with a MS-Windows interface
  43. // element created by MS-Windows from a resource definition
  44. //
  45. // data transfer is enabled for the TCheckBox
  46. //
  47. TCheckBox::TCheckBox(TWindow*   parent,
  48.                      int        resourceId,
  49.                      TGroupBox* group,
  50.                      TModule*   module)
  51.   : TButton(parent, resourceId, module)
  52. {
  53.   Group = group;
  54.   EnableTransfer();
  55. }
  56.  
  57. //
  58. // Return name of predefined BWCC or Windows check box class
  59. //
  60. char far*
  61. TCheckBox::GetClassName()
  62. {
  63.   if (GetApplication()->BWCCEnabled())
  64.     return CHECK_CLASS;
  65.   else
  66.     return "BUTTON";
  67. }
  68.  
  69. //
  70. // transfers state information for the TCheckBox
  71. //
  72. // the direction passed specifies whether data is to be read from or
  73. // written to the passed buffer, or whether the data element size is simply
  74. // to be returned
  75. //
  76. // the return value is the size (in bytes) of the transfer data
  77. //
  78. uint
  79. TCheckBox::Transfer(void* buffer, TTransferDirection direction)
  80. {
  81.   if (direction == tdGetData)
  82.     *(uint*)buffer = GetCheck();
  83.  
  84.   else if (direction == tdSetData)
  85.     SetCheck(*(uint*)buffer);
  86.  
  87.   return sizeof(uint);
  88. }
  89.  
  90. //
  91. // sets the check state
  92. //
  93. // unchecks, checks, or grays the checkbox (if 3-state) according to the
  94. // CheckFlag passed (pass BF_UNCHECKED(0), BF_CHECKED(1), or BF_GRAYED(2))
  95. //
  96. // if a Group has been specified for the TCheckBox, notifies the Group that
  97. // the state of the check box has changed
  98. //
  99. void
  100. TCheckBox::SetCheck(uint check)
  101. {
  102.   SendMessage(BM_SETCHECK, check);
  103.  
  104.   if (Group)
  105.     Group->SelectionChanged(Attr.Id);
  106. }
  107.  
  108. //
  109. // toggles the check state of the check box
  110. //
  111. void
  112. TCheckBox::Toggle()
  113. {
  114.   if ((GetWindowLong(GWL_STYLE) & BS_AUTO3STATE) == BS_AUTO3STATE)
  115.     SetCheck((GetCheck() + 1) % 3);
  116.  
  117.   else
  118.     SetCheck((GetCheck() + 1) % 2);
  119. }
  120.  
  121. //
  122. // responds to an incoming BN_CLICKED message
  123. //
  124. // if a Group has been specified for the TCheckBox, notifies the Group that
  125. // the state of this TCheckBox has changed
  126. //
  127. void
  128. TCheckBox::BNClicked()
  129. {
  130.   if (Group)
  131.     Group->SelectionChanged(Attr.Id);
  132.  
  133.   DefaultProcessing();  // give parent an opportunity to handle...
  134. }
  135.  
  136. #endif
  137. #if !defined(SECTION) || SECTION == 2
  138.  
  139. IMPLEMENT_STREAMABLE1(TCheckBox, TButton);
  140.  
  141. //
  142. // reads an instance of TCheckBox from the passed ipstream
  143. //
  144. void*
  145. TCheckBox::Streamer::Read(ipstream& is, uint32 /*version*/) const
  146. {
  147.   ReadBaseObject((TButton*)GetObject(), is);
  148.   is >> GetObject()->Group;
  149.   return GetObject();
  150. }
  151.  
  152. //
  153. // writes the TCheckBox to the passed opstream
  154. //
  155. void
  156. TCheckBox::Streamer::Write(opstream& os) const
  157. {
  158.   WriteBaseObject((TButton*)GetObject(), os);
  159.   os << GetObject()->Group;
  160. }
  161. #endif
  162.