1 ///////////////////////////////////////////////////////////////////////////// 
   4 // Author:      Julian Smart 
   8 // Copyright:   (c) Julian Smart and Markus Holzem 
   9 // Licence:     wxWindows license 
  10 ///////////////////////////////////////////////////////////////////////////// 
  13 #pragma implementation "checkbox.h" 
  16 // For compilers that support precompilation, includes "wx.h". 
  17 #include "wx/wxprec.h" 
  24 #include "wx/checkbox.h" 
  28 #include "wx/msw/private.h" 
  30 #if !USE_SHARED_LIBRARY 
  31 IMPLEMENT_DYNAMIC_CLASS(wxCheckBox
, wxControl
) 
  32 IMPLEMENT_DYNAMIC_CLASS(wxBitmapCheckBox
, wxCheckBox
) 
  35 bool wxCheckBox::MSWCommand(WXUINT 
WXUNUSED(param
), WXWORD 
WXUNUSED(id
)) 
  37   wxCommandEvent 
event(wxEVT_COMMAND_CHECKBOX_CLICKED
, m_windowId
); 
  38   event
.SetInt(GetValue()); 
  39   event
.SetEventObject(this); 
  40   ProcessCommand(event
); 
  44 // Single check box item 
  45 bool wxCheckBox::Create(wxWindow 
*parent
, wxWindowID id
, const wxString
& label
, 
  47            const wxSize
& size
, long style
, 
  48            const wxValidator
& validator
, 
  52   SetValidator(validator
); 
  53   if (parent
) parent
->AddChild(this); 
  55   SetBackgroundColour(parent
->GetBackgroundColour()) ; 
  56   SetForegroundColour(parent
->GetForegroundColour()) ; 
  58   m_windowStyle 
= style
; 
  60   wxString Label 
= label
; 
  62     Label 
= _T(" "); // Apparently needed or checkbox won't show 
  65                 m_windowId 
= NewControlId(); 
  74   long msStyle 
= BS_AUTOCHECKBOX 
| WS_TABSTOP 
| WS_CHILD 
| WS_VISIBLE
; 
  75   if ( style 
& wxALIGN_RIGHT 
) 
  76     msStyle 
|= BS_LEFTTEXT
; 
  78   // We perhaps have different concepts of 3D here - a 3D border, 
  79   // versus a 3D button. 
  80   // So we only wish to give a border if this is specified 
  83   WXDWORD exStyle 
= Determine3DEffects(0, &want3D
) ; 
  85   // Even with extended styles, need to combine with WS_BORDER 
  86   // for them to look right. 
  88   if ( want3D || wxStyleHasBorder(m_windowStyle) ) 
  92   m_hWnd 
= (WXHWND
)CreateWindowEx(exStyle
, _T("BUTTON"), Label
, 
  95                                   (HWND
)parent
->GetHWND(), (HMENU
)m_windowId
, 
  96                                   wxGetInstance(), NULL
); 
 101     Ctl3dSubclassCtl((HWND
)m_hWnd
); 
 106   // Subclass again for purposes of dialog editing mode 
 109   SetFont(parent
->GetFont()); 
 111   SetSize(x
, y
, width
, height
); 
 116 void wxCheckBox::SetLabel(const wxString
& label
) 
 118   SetWindowText((HWND
)GetHWND(), label
); 
 121 void wxCheckBox::DoSetSize(int x
, int y
, int width
, int height
, int sizeFlags
) 
 123   int currentX
, currentY
; 
 124   GetPosition(¤tX
, ¤tY
); 
 130   if (x 
== -1 || (sizeFlags 
& wxSIZE_ALLOW_MINUS_ONE
)) 
 132   if (y 
== -1 || (sizeFlags 
& wxSIZE_ALLOW_MINUS_ONE
)) 
 135   AdjustForParentClientOrigin(x1
, y1
, sizeFlags
); 
 137   int current_width
, cyf
; 
 138   HWND button 
= (HWND
) GetHWND(); 
 140   int nLen 
= GetWindowTextLength(button
); 
 142   GetWindowText(button
, str
.GetWriteBuf(nLen
), nLen
); 
 145   if ( !str
.IsEmpty() ) 
 147     GetTextExtent(str
, ¤t_width
, &cyf
, NULL
, NULL
, & this->GetFont()); 
 149       w1 
= (int)(current_width 
+ RADIO_SIZE
); 
 165   MoveWindow(button
, x1
, y1
, w1
, h1
, TRUE
); 
 168 void wxCheckBox::SetValue(bool val
) 
 170   SendMessage((HWND
) GetHWND(), BM_SETCHECK
, val
, 0); 
 174 #define BST_CHECKED 0x0001 
 177 bool wxCheckBox::GetValue() const 
 180   return (SendMessage((HWND
) GetHWND(), BM_GETCHECK
, 0, 0) == BST_CHECKED
); 
 182   return ((0x003 & SendMessage((HWND
) GetHWND(), BM_GETCHECK
, 0, 0)) == 0x003); 
 186 WXHBRUSH 
wxCheckBox::OnCtlColor(WXHDC pDC
, WXHWND pWnd
, WXUINT nCtlColor
, 
 187                         WXUINT message
, WXWPARAM wParam
, WXLPARAM lParam
) 
 192     HBRUSH hbrush 
= Ctl3dCtlColorEx(message
, wParam
, lParam
); 
 194       return (WXHBRUSH
) hbrush
; 
 198   if (GetParent()->GetTransparentBackground()) 
 199     SetBkMode((HDC
) pDC
, TRANSPARENT
); 
 201     SetBkMode((HDC
) pDC
, OPAQUE
); 
 203   ::SetBkColor((HDC
) pDC
, RGB(GetBackgroundColour().Red(), GetBackgroundColour().Green(), GetBackgroundColour().Blue())); 
 204   ::SetTextColor((HDC
) pDC
, RGB(GetForegroundColour().Red(), GetForegroundColour().Green(), GetForegroundColour().Blue())); 
 206   wxBrush 
*backgroundBrush 
= wxTheBrushList
->FindOrCreateBrush(GetBackgroundColour(), wxSOLID
); 
 208   // Note that this will be cleaned up in wxApp::OnIdle, if backgroundBrush 
 209   // has a zero usage count. 
 210 //  backgroundBrush->RealizeResource(); 
 211   return (WXHBRUSH
) backgroundBrush
->GetResourceHandle(); 
 214 void wxCheckBox::Command (wxCommandEvent 
& event
) 
 216   SetValue ((event
.GetInt() != 0)); 
 217   ProcessCommand (event
); 
 220 bool wxBitmapCheckBox::Create(wxWindow 
*parent
, wxWindowID id
, const wxBitmap 
*label
, 
 222            const wxSize
& size
, long style
, 
 223            const wxValidator
& validator
, 
 224            const wxString
& name
) 
 227   SetValidator(validator
); 
 228   if (parent
) parent
->AddChild(this); 
 230   SetBackgroundColour(parent
->GetBackgroundColour()) ; 
 231   SetForegroundColour(parent
->GetForegroundColour()) ; 
 232   m_windowStyle 
= style
; 
 235                 m_windowId 
= NewControlId(); 
 246   long msStyle 
= CHECK_FLAGS
; 
 248   HWND wx_button 
= CreateWindowEx(MakeExtendedStyle(m_windowStyle
), CHECK_CLASS
, _T("toggle"), 
 250                     0, 0, 0, 0, (HWND
) parent
->GetHWND(), (HMENU
)m_windowId
, 
 251                     wxGetInstance(), NULL
); 
 254   if (!(GetParent()->GetWindowStyleFlag() & wxUSER_COLOURS
)) 
 256     Ctl3dSubclassCtl(wx_button
); 
 261   m_hWnd 
= (WXHWND
)wx_button
; 
 263   // Subclass again for purposes of dialog editing mode 
 264   SubclassWin((WXHWND
)wx_button
); 
 266 //  SetFont(parent->GetFont()); 
 268   SetSize(x
, y
, width
, height
); 
 270   ShowWindow(wx_button
, SW_SHOW
); 
 274 void wxBitmapCheckBox::SetLabel(const wxBitmap
& bitmap
) 
 278 void wxBitmapCheckBox::DoSetSize(int x
, int y
, int width
, int height
, int sizeFlags
) 
 280   int currentX
, currentY
; 
 281   GetPosition(¤tX
, ¤tY
); 
 288   if (x 
== -1 || (sizeFlags 
& wxSIZE_ALLOW_MINUS_ONE
)) 
 290   if (y 
== -1 || (sizeFlags 
& wxSIZE_ALLOW_MINUS_ONE
)) 
 293   AdjustForParentClientOrigin(x1
, y1
, sizeFlags
); 
 295   HWND button 
= (HWND
) GetHWND(); 
 298       w1 = checkWidth + FB_MARGIN ; 
 300       h1 = checkHeight + FB_MARGIN ; 
 302   MoveWindow(button
, x1
, y1
, w1
, h1
, TRUE
); 
 305 void wxBitmapCheckBox::SetValue(bool val
) 
 307   SendMessage((HWND
) GetHWND(), BM_SETCHECK
, val
, 0); 
 310 bool wxBitmapCheckBox::GetValue() const 
 312   return ((0x003 & SendMessage((HWND
) GetHWND(), BM_GETCHECK
, 0, 0)) == 0x003);