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"
27 #include "wx/msw/private.h"
29 #if !USE_SHARED_LIBRARY
30 IMPLEMENT_DYNAMIC_CLASS(wxCheckBox
, wxControl
)
31 IMPLEMENT_DYNAMIC_CLASS(wxBitmapCheckBox
, wxCheckBox
)
34 bool wxCheckBox::MSWCommand(WXUINT
WXUNUSED(param
), WXWORD
WXUNUSED(id
))
36 wxCommandEvent
event(wxEVENT_TYPE_CHECKBOX_COMMAND
, m_windowId
);
37 event
.SetInt(GetValue());
38 event
.SetEventObject(this);
39 ProcessCommand(event
);
43 // Single check box item
44 bool wxCheckBox::Create(wxWindow
*parent
, wxWindowID id
, const wxString
& label
,
46 const wxSize
& size
, long style
,
47 const wxValidator
& validator
,
51 SetValidator(validator
);
52 if (parent
) parent
->AddChild(this);
54 SetBackgroundColour(parent
->GetBackgroundColour()) ;
55 SetForegroundColour(parent
->GetForegroundColour()) ;
57 m_windowStyle
= style
;
59 wxString Label
= label
;
61 Label
= " "; // Apparently needed or checkbox won't show
64 m_windowId
= NewControlId();
73 long msStyle
= BS_AUTOCHECKBOX
| WS_TABSTOP
| WS_CHILD
| WS_VISIBLE
;
74 if ( style
& wxALIGN_RIGHT
)
75 msStyle
|= BS_LEFTTEXT
;
77 // We perhaps have different concepts of 3D here - a 3D border,
78 // versus a 3D button.
79 // So we only wish to give a border if this is specified
82 WXDWORD exStyle
= Determine3DEffects(0, &want3D
) ;
84 // Even with extended styles, need to combine with WS_BORDER
85 // for them to look right.
86 if ( want3D
|| wxStyleHasBorder(m_windowStyle
) )
89 m_hWnd
= (WXHWND
)CreateWindowEx(exStyle
, "BUTTON", Label
,
92 (HWND
)parent
->GetHWND(), (HMENU
)m_windowId
,
93 wxGetInstance(), NULL
);
98 Ctl3dSubclassCtl((HWND
)m_hWnd
);
103 // Subclass again for purposes of dialog editing mode
106 SetFont(*parent
->GetFont());
108 SetSize(x
, y
, width
, height
);
113 void wxCheckBox::SetLabel(const wxString
& label
)
115 SetWindowText((HWND
)GetHWND(), label
);
118 void wxCheckBox::SetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
120 int currentX
, currentY
;
121 GetPosition(¤tX
, ¤tY
);
127 if (x
== -1 || (sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
129 if (y
== -1 || (sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
132 AdjustForParentClientOrigin(x1
, y1
, sizeFlags
);
134 int current_width
, cyf
;
135 HWND button
= (HWND
) GetHWND();
137 int nLen
= GetWindowTextLength(button
);
139 GetWindowText(button
, str
.GetWriteBuf(nLen
), nLen
);
142 if ( !str
.IsEmpty() )
144 GetTextExtent(str
, ¤t_width
, &cyf
, NULL
, NULL
, GetFont());
146 w1
= (int)(current_width
+ RADIO_SIZE
);
162 MoveWindow(button
, x1
, y1
, w1
, h1
, TRUE
);
165 void wxCheckBox::SetValue(bool val
)
167 SendMessage((HWND
) GetHWND(), BM_SETCHECK
, val
, 0);
170 bool wxCheckBox::GetValue(void) const
173 return (SendMessage((HWND
) GetHWND(), BM_GETCHECK
, 0, 0) == BST_CHECKED
);
175 return ((0x003 & SendMessage((HWND
) GetHWND(), BM_GETCHECK
, 0, 0)) == 0x003);
179 WXHBRUSH
wxCheckBox::OnCtlColor(WXHDC pDC
, WXHWND pWnd
, WXUINT nCtlColor
,
180 WXUINT message
, WXWPARAM wParam
, WXLPARAM lParam
)
185 HBRUSH hbrush
= Ctl3dCtlColorEx(message
, wParam
, lParam
);
187 return (WXHBRUSH
) hbrush
;
191 if (GetParent()->GetTransparentBackground())
192 SetBkMode((HDC
) pDC
, TRANSPARENT
);
194 SetBkMode((HDC
) pDC
, OPAQUE
);
196 ::SetBkColor((HDC
) pDC
, RGB(GetBackgroundColour().Red(), GetBackgroundColour().Green(), GetBackgroundColour().Blue()));
197 ::SetTextColor((HDC
) pDC
, RGB(GetForegroundColour().Red(), GetForegroundColour().Green(), GetForegroundColour().Blue()));
199 wxBrush
*backgroundBrush
= wxTheBrushList
->FindOrCreateBrush(GetBackgroundColour(), wxSOLID
);
201 // Note that this will be cleaned up in wxApp::OnIdle, if backgroundBrush
202 // has a zero usage count.
203 // backgroundBrush->RealizeResource();
204 return (WXHBRUSH
) backgroundBrush
->GetResourceHandle();
207 void wxCheckBox::Command (wxCommandEvent
& event
)
209 SetValue ((event
.GetInt() != 0));
210 ProcessCommand (event
);
213 bool wxBitmapCheckBox::Create(wxWindow
*parent
, wxWindowID id
, const wxBitmap
*label
,
215 const wxSize
& size
, long style
,
216 const wxValidator
& validator
,
217 const wxString
& name
)
220 SetValidator(validator
);
221 if (parent
) parent
->AddChild(this);
223 SetBackgroundColour(parent
->GetBackgroundColour()) ;
224 SetForegroundColour(parent
->GetForegroundColour()) ;
225 m_windowStyle
= style
;
228 m_windowId
= NewControlId();
239 long msStyle
= CHECK_FLAGS
;
241 HWND wx_button
= CreateWindowEx(MakeExtendedStyle(m_windowStyle
), CHECK_CLASS
, "toggle",
243 0, 0, 0, 0, (HWND
) parent
->GetHWND(), (HMENU
)m_windowId
,
244 wxGetInstance(), NULL
);
247 if (!(GetParent()->GetWindowStyleFlag() & wxUSER_COLOURS
))
249 Ctl3dSubclassCtl(wx_button
);
254 m_hWnd
= (WXHWND
)wx_button
;
256 // Subclass again for purposes of dialog editing mode
257 SubclassWin((WXHWND
)wx_button
);
259 // SetFont(parent->GetFont());
261 SetSize(x
, y
, width
, height
);
263 ShowWindow(wx_button
, SW_SHOW
);
267 void wxBitmapCheckBox::SetLabel(const wxBitmap
*bitmap
)
271 void wxBitmapCheckBox::SetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
273 int currentX
, currentY
;
274 GetPosition(¤tX
, ¤tY
);
281 if (x
== -1 || (sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
283 if (y
== -1 || (sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
286 AdjustForParentClientOrigin(x1
, y1
, sizeFlags
);
288 HWND button
= (HWND
) GetHWND();
291 w1 = checkWidth + FB_MARGIN ;
293 h1 = checkHeight + FB_MARGIN ;
295 MoveWindow(button
, x1
, y1
, w1
, h1
, TRUE
);
298 void wxBitmapCheckBox::SetValue(bool val
)
300 SendMessage((HWND
) GetHWND(), BM_SETCHECK
, val
, 0);
303 bool wxBitmapCheckBox::GetValue(void) const
305 return ((0x003 & SendMessage((HWND
) GetHWND(), BM_GETCHECK
, 0, 0)) == 0x003);