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
&& ((m_windowStyle
& wxSIMPLE_BORDER
) || (m_windowStyle
& wxRAISED_BORDER
) ||
87 (m_windowStyle
& wxSUNKEN_BORDER
) || (m_windowStyle
& wxDOUBLE_BORDER
)))
90 m_hWnd
= (WXHWND
)CreateWindowEx(exStyle
, "BUTTON", Label
,
93 (HWND
)parent
->GetHWND(), (HMENU
)m_windowId
,
94 wxGetInstance(), NULL
);
99 Ctl3dSubclassCtl((HWND
)m_hWnd
);
104 // Subclass again for purposes of dialog editing mode
107 SetFont(*parent
->GetFont());
109 SetSize(x
, y
, width
, height
);
114 void wxCheckBox::SetLabel(const wxString
& label
)
116 SetWindowText((HWND
)GetHWND(), label
);
119 void wxCheckBox::SetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
121 int currentX
, currentY
;
122 GetPosition(¤tX
, ¤tY
);
128 if (x
== -1 || (sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
130 if (y
== -1 || (sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
133 AdjustForParentClientOrigin(x1
, y1
, sizeFlags
);
135 int current_width
, cyf
;
136 HWND button
= (HWND
) GetHWND();
138 int nLen
= GetWindowTextLength(button
);
140 GetWindowText(button
, str
.GetWriteBuf(nLen
), nLen
);
143 if ( !str
.IsEmpty() )
145 GetTextExtent(str
, ¤t_width
, &cyf
, NULL
, NULL
, GetFont());
147 w1
= (int)(current_width
+ RADIO_SIZE
);
163 MoveWindow(button
, x1
, y1
, w1
, h1
, TRUE
);
166 void wxCheckBox::SetValue(bool val
)
168 SendMessage((HWND
) GetHWND(), BM_SETCHECK
, val
, 0);
171 bool wxCheckBox::GetValue(void) const
174 return (SendMessage((HWND
) GetHWND(), BM_GETCHECK
, 0, 0) == BST_CHECKED
);
176 return ((0x003 & SendMessage((HWND
) GetHWND(), BM_GETCHECK
, 0, 0)) == 0x003);
180 WXHBRUSH
wxCheckBox::OnCtlColor(WXHDC pDC
, WXHWND pWnd
, WXUINT nCtlColor
,
181 WXUINT message
, WXWPARAM wParam
, WXLPARAM lParam
)
186 HBRUSH hbrush
= Ctl3dCtlColorEx(message
, wParam
, lParam
);
188 return (WXHBRUSH
) hbrush
;
192 if (GetParent()->GetTransparentBackground())
193 SetBkMode((HDC
) pDC
, TRANSPARENT
);
195 SetBkMode((HDC
) pDC
, OPAQUE
);
197 ::SetBkColor((HDC
) pDC
, RGB(GetBackgroundColour().Red(), GetBackgroundColour().Green(), GetBackgroundColour().Blue()));
198 ::SetTextColor((HDC
) pDC
, RGB(GetForegroundColour().Red(), GetForegroundColour().Green(), GetForegroundColour().Blue()));
200 wxBrush
*backgroundBrush
= wxTheBrushList
->FindOrCreateBrush(GetBackgroundColour(), wxSOLID
);
202 // Note that this will be cleaned up in wxApp::OnIdle, if backgroundBrush
203 // has a zero usage count.
204 // backgroundBrush->RealizeResource();
205 return (WXHBRUSH
) backgroundBrush
->GetResourceHandle();
208 void wxCheckBox::Command (wxCommandEvent
& event
)
210 SetValue ((event
.GetInt() != 0));
211 ProcessCommand (event
);
214 bool wxBitmapCheckBox::Create(wxWindow
*parent
, wxWindowID id
, const wxBitmap
*label
,
216 const wxSize
& size
, long style
,
217 const wxValidator
& validator
,
218 const wxString
& name
)
221 SetValidator(validator
);
222 if (parent
) parent
->AddChild(this);
224 SetBackgroundColour(parent
->GetBackgroundColour()) ;
225 SetForegroundColour(parent
->GetForegroundColour()) ;
226 m_windowStyle
= style
;
229 m_windowId
= NewControlId();
240 long msStyle
= CHECK_FLAGS
;
242 HWND wx_button
= CreateWindowEx(MakeExtendedStyle(m_windowStyle
), CHECK_CLASS
, "toggle",
244 0, 0, 0, 0, (HWND
) parent
->GetHWND(), (HMENU
)m_windowId
,
245 wxGetInstance(), NULL
);
248 if (!(GetParent()->GetWindowStyleFlag() & wxUSER_COLOURS
))
250 Ctl3dSubclassCtl(wx_button
);
255 m_hWnd
= (WXHWND
)wx_button
;
257 // Subclass again for purposes of dialog editing mode
258 SubclassWin((WXHWND
)wx_button
);
260 // SetFont(parent->GetFont());
262 SetSize(x
, y
, width
, height
);
264 ShowWindow(wx_button
, SW_SHOW
);
268 void wxBitmapCheckBox::SetLabel(const wxBitmap
*bitmap
)
272 void wxBitmapCheckBox::SetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
274 int currentX
, currentY
;
275 GetPosition(¤tX
, ¤tY
);
282 if (x
== -1 || (sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
284 if (y
== -1 || (sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
287 AdjustForParentClientOrigin(x1
, y1
, sizeFlags
);
289 HWND button
= (HWND
) GetHWND();
292 w1 = checkWidth + FB_MARGIN ;
294 h1 = checkHeight + FB_MARGIN ;
296 MoveWindow(button
, x1
, y1
, w1
, h1
, TRUE
);
299 void wxBitmapCheckBox::SetValue(bool val
)
301 SendMessage((HWND
) GetHWND(), BM_SETCHECK
, val
, 0);
304 bool wxBitmapCheckBox::GetValue(void) const
306 return ((0x003 & SendMessage((HWND
) GetHWND(), BM_GETCHECK
, 0, 0)) == 0x003);