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(wxEVT_COMMAND_CHECKBOX_CLICKED
, 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.
87 if ( want3D || wxStyleHasBorder(m_windowStyle) )
91 m_hWnd
= (WXHWND
)CreateWindowEx(exStyle
, "BUTTON", Label
,
94 (HWND
)parent
->GetHWND(), (HMENU
)m_windowId
,
95 wxGetInstance(), NULL
);
100 Ctl3dSubclassCtl((HWND
)m_hWnd
);
105 // Subclass again for purposes of dialog editing mode
108 SetFont(parent
->GetFont());
110 SetSize(x
, y
, width
, height
);
115 void wxCheckBox::SetLabel(const wxString
& label
)
117 SetWindowText((HWND
)GetHWND(), label
);
120 void wxCheckBox::SetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
122 int currentX
, currentY
;
123 GetPosition(¤tX
, ¤tY
);
129 if (x
== -1 || (sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
131 if (y
== -1 || (sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
134 AdjustForParentClientOrigin(x1
, y1
, sizeFlags
);
136 int current_width
, cyf
;
137 HWND button
= (HWND
) GetHWND();
139 int nLen
= GetWindowTextLength(button
);
141 GetWindowText(button
, str
.GetWriteBuf(nLen
), nLen
);
144 if ( !str
.IsEmpty() )
146 GetTextExtent(str
, ¤t_width
, &cyf
, NULL
, NULL
, & GetFont());
148 w1
= (int)(current_width
+ RADIO_SIZE
);
164 MoveWindow(button
, x1
, y1
, w1
, h1
, TRUE
);
167 void wxCheckBox::SetValue(bool val
)
169 SendMessage((HWND
) GetHWND(), BM_SETCHECK
, val
, 0);
172 bool wxCheckBox::GetValue(void) const
175 return (SendMessage((HWND
) GetHWND(), BM_GETCHECK
, 0, 0) == BST_CHECKED
);
177 return ((0x003 & SendMessage((HWND
) GetHWND(), BM_GETCHECK
, 0, 0)) == 0x003);
181 WXHBRUSH
wxCheckBox::OnCtlColor(WXHDC pDC
, WXHWND pWnd
, WXUINT nCtlColor
,
182 WXUINT message
, WXWPARAM wParam
, WXLPARAM lParam
)
187 HBRUSH hbrush
= Ctl3dCtlColorEx(message
, wParam
, lParam
);
189 return (WXHBRUSH
) hbrush
;
193 if (GetParent()->GetTransparentBackground())
194 SetBkMode((HDC
) pDC
, TRANSPARENT
);
196 SetBkMode((HDC
) pDC
, OPAQUE
);
198 ::SetBkColor((HDC
) pDC
, RGB(GetBackgroundColour().Red(), GetBackgroundColour().Green(), GetBackgroundColour().Blue()));
199 ::SetTextColor((HDC
) pDC
, RGB(GetForegroundColour().Red(), GetForegroundColour().Green(), GetForegroundColour().Blue()));
201 wxBrush
*backgroundBrush
= wxTheBrushList
->FindOrCreateBrush(GetBackgroundColour(), wxSOLID
);
203 // Note that this will be cleaned up in wxApp::OnIdle, if backgroundBrush
204 // has a zero usage count.
205 // backgroundBrush->RealizeResource();
206 return (WXHBRUSH
) backgroundBrush
->GetResourceHandle();
209 void wxCheckBox::Command (wxCommandEvent
& event
)
211 SetValue ((event
.GetInt() != 0));
212 ProcessCommand (event
);
215 bool wxBitmapCheckBox::Create(wxWindow
*parent
, wxWindowID id
, const wxBitmap
*label
,
217 const wxSize
& size
, long style
,
218 const wxValidator
& validator
,
219 const wxString
& name
)
222 SetValidator(validator
);
223 if (parent
) parent
->AddChild(this);
225 SetBackgroundColour(parent
->GetBackgroundColour()) ;
226 SetForegroundColour(parent
->GetForegroundColour()) ;
227 m_windowStyle
= style
;
230 m_windowId
= NewControlId();
241 long msStyle
= CHECK_FLAGS
;
243 HWND wx_button
= CreateWindowEx(MakeExtendedStyle(m_windowStyle
), CHECK_CLASS
, "toggle",
245 0, 0, 0, 0, (HWND
) parent
->GetHWND(), (HMENU
)m_windowId
,
246 wxGetInstance(), NULL
);
249 if (!(GetParent()->GetWindowStyleFlag() & wxUSER_COLOURS
))
251 Ctl3dSubclassCtl(wx_button
);
256 m_hWnd
= (WXHWND
)wx_button
;
258 // Subclass again for purposes of dialog editing mode
259 SubclassWin((WXHWND
)wx_button
);
261 // SetFont(parent->GetFont());
263 SetSize(x
, y
, width
, height
);
265 ShowWindow(wx_button
, SW_SHOW
);
269 void wxBitmapCheckBox::SetLabel(const wxBitmap
*bitmap
)
273 void wxBitmapCheckBox::SetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
275 int currentX
, currentY
;
276 GetPosition(¤tX
, ¤tY
);
283 if (x
== -1 || (sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
285 if (y
== -1 || (sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
288 AdjustForParentClientOrigin(x1
, y1
, sizeFlags
);
290 HWND button
= (HWND
) GetHWND();
293 w1 = checkWidth + FB_MARGIN ;
295 h1 = checkHeight + FB_MARGIN ;
297 MoveWindow(button
, x1
, y1
, w1
, h1
, TRUE
);
300 void wxBitmapCheckBox::SetValue(bool val
)
302 SendMessage((HWND
) GetHWND(), BM_SETCHECK
, val
, 0);
305 bool wxBitmapCheckBox::GetValue(void) const
307 return ((0x003 & SendMessage((HWND
) GetHWND(), BM_GETCHECK
, 0, 0)) == 0x003);