]>
Commit | Line | Data |
---|---|---|
1 | ///////////////////////////////////////////////////////////////////////////// | |
2 | // Name: src/msw/checkbox.cpp | |
3 | // Purpose: wxCheckBox | |
4 | // Author: Julian Smart | |
5 | // Modified by: | |
6 | // Created: 04/01/98 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) Julian Smart | |
9 | // Licence: wxWindows licence | |
10 | ///////////////////////////////////////////////////////////////////////////// | |
11 | ||
12 | // ============================================================================ | |
13 | // declarations | |
14 | // ============================================================================ | |
15 | ||
16 | // ---------------------------------------------------------------------------- | |
17 | // headers | |
18 | // ---------------------------------------------------------------------------- | |
19 | ||
20 | // For compilers that support precompilation, includes "wx.h". | |
21 | #include "wx/wxprec.h" | |
22 | ||
23 | #ifdef __BORLANDC__ | |
24 | #pragma hdrstop | |
25 | #endif | |
26 | ||
27 | #if wxUSE_CHECKBOX | |
28 | ||
29 | #include "wx/checkbox.h" | |
30 | ||
31 | #ifndef WX_PRECOMP | |
32 | #include "wx/brush.h" | |
33 | #include "wx/dcscreen.h" | |
34 | #include "wx/settings.h" | |
35 | #endif | |
36 | ||
37 | #include "wx/msw/dc.h" // for wxDCTemp | |
38 | #include "wx/msw/uxtheme.h" | |
39 | #include "wx/renderer.h" | |
40 | ||
41 | // ---------------------------------------------------------------------------- | |
42 | // constants | |
43 | // ---------------------------------------------------------------------------- | |
44 | ||
45 | #ifndef BST_UNCHECKED | |
46 | #define BST_UNCHECKED 0x0000 | |
47 | #endif | |
48 | ||
49 | #ifndef BST_CHECKED | |
50 | #define BST_CHECKED 0x0001 | |
51 | #endif | |
52 | ||
53 | #ifndef BST_INDETERMINATE | |
54 | #define BST_INDETERMINATE 0x0002 | |
55 | #endif | |
56 | ||
57 | #ifndef DT_HIDEPREFIX | |
58 | #define DT_HIDEPREFIX 0x00100000 | |
59 | #endif | |
60 | ||
61 | #ifndef BP_CHECKBOX | |
62 | #define BP_CHECKBOX 3 | |
63 | #endif | |
64 | ||
65 | // these values are defined in tmschema.h (except the first one) | |
66 | enum | |
67 | { | |
68 | CBS_INVALID, | |
69 | CBS_UNCHECKEDNORMAL, | |
70 | CBS_UNCHECKEDHOT, | |
71 | CBS_UNCHECKEDPRESSED, | |
72 | CBS_UNCHECKEDDISABLED, | |
73 | CBS_CHECKEDNORMAL, | |
74 | CBS_CHECKEDHOT, | |
75 | CBS_CHECKEDPRESSED, | |
76 | CBS_CHECKEDDISABLED, | |
77 | CBS_MIXEDNORMAL, | |
78 | CBS_MIXEDHOT, | |
79 | CBS_MIXEDPRESSED, | |
80 | CBS_MIXEDDISABLED | |
81 | }; | |
82 | ||
83 | // these are our own | |
84 | enum | |
85 | { | |
86 | CBS_HOT_OFFSET = 1, | |
87 | CBS_PRESSED_OFFSET = 2, | |
88 | CBS_DISABLED_OFFSET = 3 | |
89 | }; | |
90 | ||
91 | // ============================================================================ | |
92 | // implementation | |
93 | // ============================================================================ | |
94 | ||
95 | #if wxUSE_EXTENDED_RTTI | |
96 | WX_DEFINE_FLAGS( wxCheckBoxStyle ) | |
97 | ||
98 | wxBEGIN_FLAGS( wxCheckBoxStyle ) | |
99 | // new style border flags, we put them first to | |
100 | // use them for streaming out | |
101 | wxFLAGS_MEMBER(wxBORDER_SIMPLE) | |
102 | wxFLAGS_MEMBER(wxBORDER_SUNKEN) | |
103 | wxFLAGS_MEMBER(wxBORDER_DOUBLE) | |
104 | wxFLAGS_MEMBER(wxBORDER_RAISED) | |
105 | wxFLAGS_MEMBER(wxBORDER_STATIC) | |
106 | wxFLAGS_MEMBER(wxBORDER_NONE) | |
107 | ||
108 | // old style border flags | |
109 | wxFLAGS_MEMBER(wxSIMPLE_BORDER) | |
110 | wxFLAGS_MEMBER(wxSUNKEN_BORDER) | |
111 | wxFLAGS_MEMBER(wxDOUBLE_BORDER) | |
112 | wxFLAGS_MEMBER(wxRAISED_BORDER) | |
113 | wxFLAGS_MEMBER(wxSTATIC_BORDER) | |
114 | wxFLAGS_MEMBER(wxNO_BORDER) | |
115 | ||
116 | // standard window styles | |
117 | wxFLAGS_MEMBER(wxTAB_TRAVERSAL) | |
118 | wxFLAGS_MEMBER(wxCLIP_CHILDREN) | |
119 | wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW) | |
120 | wxFLAGS_MEMBER(wxWANTS_CHARS) | |
121 | wxFLAGS_MEMBER(wxNO_FULL_REPAINT_ON_RESIZE) | |
122 | wxFLAGS_MEMBER(wxALWAYS_SHOW_SB ) | |
123 | wxFLAGS_MEMBER(wxVSCROLL) | |
124 | wxFLAGS_MEMBER(wxHSCROLL) | |
125 | ||
126 | wxEND_FLAGS( wxCheckBoxStyle ) | |
127 | ||
128 | IMPLEMENT_DYNAMIC_CLASS_XTI(wxCheckBox, wxControl,"wx/checkbox.h") | |
129 | ||
130 | wxBEGIN_PROPERTIES_TABLE(wxCheckBox) | |
131 | wxEVENT_PROPERTY( Click , wxEVT_COMMAND_CHECKBOX_CLICKED , wxCommandEvent ) | |
132 | ||
133 | wxPROPERTY( Font , wxFont , SetFont , GetFont , EMPTY_MACROVALUE , 0 /*flags*/ , wxT("Helpstring") , wxT("group")) | |
134 | wxPROPERTY( Label,wxString, SetLabel, GetLabel, wxString() , 0 /*flags*/ , wxT("Helpstring") , wxT("group")) | |
135 | wxPROPERTY( Value ,bool, SetValue, GetValue, EMPTY_MACROVALUE, 0 /*flags*/ , wxT("Helpstring") , wxT("group")) | |
136 | wxPROPERTY_FLAGS( WindowStyle , wxCheckBoxStyle , long , SetWindowStyleFlag , GetWindowStyleFlag , EMPTY_MACROVALUE, 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style | |
137 | wxEND_PROPERTIES_TABLE() | |
138 | ||
139 | wxBEGIN_HANDLERS_TABLE(wxCheckBox) | |
140 | wxEND_HANDLERS_TABLE() | |
141 | ||
142 | wxCONSTRUCTOR_6( wxCheckBox , wxWindow* , Parent , wxWindowID , Id , wxString , Label , wxPoint , Position , wxSize , Size , long , WindowStyle ) | |
143 | #else | |
144 | IMPLEMENT_DYNAMIC_CLASS(wxCheckBox, wxControl) | |
145 | #endif | |
146 | ||
147 | ||
148 | // ---------------------------------------------------------------------------- | |
149 | // wxCheckBox creation | |
150 | // ---------------------------------------------------------------------------- | |
151 | ||
152 | void wxCheckBox::Init() | |
153 | { | |
154 | m_state = wxCHK_UNCHECKED; | |
155 | m_isPressed = | |
156 | m_isHot = false; | |
157 | } | |
158 | ||
159 | bool wxCheckBox::Create(wxWindow *parent, | |
160 | wxWindowID id, | |
161 | const wxString& label, | |
162 | const wxPoint& pos, | |
163 | const wxSize& size, long style, | |
164 | const wxValidator& validator, | |
165 | const wxString& name) | |
166 | { | |
167 | Init(); | |
168 | ||
169 | if ( !CreateControl(parent, id, pos, size, style, validator, name) ) | |
170 | return false; | |
171 | ||
172 | long msStyle = WS_TABSTOP; | |
173 | ||
174 | if ( style & wxCHK_3STATE ) | |
175 | { | |
176 | msStyle |= BS_3STATE; | |
177 | } | |
178 | else | |
179 | { | |
180 | wxASSERT_MSG( !Is3rdStateAllowedForUser(), | |
181 | wxT("Using wxCH_ALLOW_3RD_STATE_FOR_USER") | |
182 | wxT(" style flag for a 2-state checkbox is useless") ); | |
183 | msStyle |= BS_CHECKBOX; | |
184 | } | |
185 | ||
186 | if ( style & wxALIGN_RIGHT ) | |
187 | { | |
188 | msStyle |= BS_LEFTTEXT | BS_RIGHT; | |
189 | } | |
190 | ||
191 | return MSWCreateControl(wxT("BUTTON"), msStyle, pos, size, label, 0); | |
192 | } | |
193 | ||
194 | // ---------------------------------------------------------------------------- | |
195 | // wxCheckBox geometry | |
196 | // ---------------------------------------------------------------------------- | |
197 | ||
198 | wxSize wxCheckBox::DoGetBestSize() const | |
199 | { | |
200 | static int s_checkSize = 0; | |
201 | ||
202 | if ( !s_checkSize ) | |
203 | { | |
204 | wxScreenDC dc; | |
205 | dc.SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT)); | |
206 | ||
207 | s_checkSize = dc.GetCharHeight(); | |
208 | } | |
209 | ||
210 | wxString str = wxGetWindowText(GetHWND()); | |
211 | ||
212 | int wCheckbox, hCheckbox; | |
213 | if ( !str.empty() ) | |
214 | { | |
215 | GetTextExtent(GetLabelText(str), &wCheckbox, &hCheckbox); | |
216 | wCheckbox += s_checkSize + GetCharWidth(); | |
217 | ||
218 | if ( hCheckbox < s_checkSize ) | |
219 | hCheckbox = s_checkSize; | |
220 | } | |
221 | else | |
222 | { | |
223 | wCheckbox = s_checkSize; | |
224 | hCheckbox = s_checkSize; | |
225 | } | |
226 | #ifdef __WXWINCE__ | |
227 | hCheckbox += 1; | |
228 | #endif | |
229 | ||
230 | wxSize best(wCheckbox, hCheckbox); | |
231 | CacheBestSize(best); | |
232 | return best; | |
233 | } | |
234 | ||
235 | // ---------------------------------------------------------------------------- | |
236 | // wxCheckBox operations | |
237 | // ---------------------------------------------------------------------------- | |
238 | ||
239 | void wxCheckBox::SetValue(bool val) | |
240 | { | |
241 | Set3StateValue(val ? wxCHK_CHECKED : wxCHK_UNCHECKED); | |
242 | } | |
243 | ||
244 | bool wxCheckBox::GetValue() const | |
245 | { | |
246 | return Get3StateValue() != wxCHK_UNCHECKED; | |
247 | } | |
248 | ||
249 | void wxCheckBox::Command(wxCommandEvent& event) | |
250 | { | |
251 | int state = event.GetInt(); | |
252 | wxCHECK_RET( (state == wxCHK_UNCHECKED) || (state == wxCHK_CHECKED) | |
253 | || (state == wxCHK_UNDETERMINED), | |
254 | wxT("event.GetInt() returned an invalid checkbox state") ); | |
255 | ||
256 | Set3StateValue((wxCheckBoxState) state); | |
257 | ProcessCommand(event); | |
258 | } | |
259 | ||
260 | wxCOMPILE_TIME_ASSERT(wxCHK_UNCHECKED == BST_UNCHECKED | |
261 | && wxCHK_CHECKED == BST_CHECKED | |
262 | && wxCHK_UNDETERMINED == BST_INDETERMINATE, EnumValuesIncorrect); | |
263 | ||
264 | void wxCheckBox::DoSet3StateValue(wxCheckBoxState state) | |
265 | { | |
266 | m_state = state; | |
267 | if ( !IsOwnerDrawn() ) | |
268 | ::SendMessage(GetHwnd(), BM_SETCHECK, (WPARAM) state, 0); | |
269 | else // owner drawn buttons don't react to this message | |
270 | Refresh(); | |
271 | } | |
272 | ||
273 | wxCheckBoxState wxCheckBox::DoGet3StateValue() const | |
274 | { | |
275 | return m_state; | |
276 | } | |
277 | ||
278 | bool wxCheckBox::MSWCommand(WXUINT cmd, WXWORD WXUNUSED(id)) | |
279 | { | |
280 | if ( cmd != BN_CLICKED && cmd != BN_DBLCLK ) | |
281 | return false; | |
282 | ||
283 | // first update the value so that user event handler gets the new checkbox | |
284 | // value | |
285 | ||
286 | // ownerdrawn buttons don't manage their state themselves unlike usual | |
287 | // auto checkboxes so do it ourselves in any case | |
288 | wxCheckBoxState state; | |
289 | if ( Is3rdStateAllowedForUser() ) | |
290 | { | |
291 | state = (wxCheckBoxState)((m_state + 1) % 3); | |
292 | } | |
293 | else // 2 state checkbox (at least from users point of view) | |
294 | { | |
295 | // note that wxCHK_UNDETERMINED also becomes unchecked when clicked | |
296 | state = m_state == wxCHK_UNCHECKED ? wxCHK_CHECKED : wxCHK_UNCHECKED; | |
297 | } | |
298 | ||
299 | DoSet3StateValue(state); | |
300 | ||
301 | ||
302 | // generate the event | |
303 | wxCommandEvent event(wxEVT_COMMAND_CHECKBOX_CLICKED, m_windowId); | |
304 | ||
305 | event.SetInt(state); | |
306 | event.SetEventObject(this); | |
307 | ProcessCommand(event); | |
308 | ||
309 | return true; | |
310 | } | |
311 | ||
312 | // ---------------------------------------------------------------------------- | |
313 | // owner drawn checkboxes stuff | |
314 | // ---------------------------------------------------------------------------- | |
315 | ||
316 | bool wxCheckBox::SetForegroundColour(const wxColour& colour) | |
317 | { | |
318 | if ( !wxCheckBoxBase::SetForegroundColour(colour) ) | |
319 | return false; | |
320 | ||
321 | // the only way to change the checkbox foreground colour under Windows XP | |
322 | // is to owner draw it | |
323 | if ( wxUxThemeEngine::GetIfActive() ) | |
324 | MakeOwnerDrawn(colour.Ok()); | |
325 | ||
326 | return true; | |
327 | } | |
328 | ||
329 | bool wxCheckBox::IsOwnerDrawn() const | |
330 | { | |
331 | return | |
332 | (::GetWindowLong(GetHwnd(), GWL_STYLE) & BS_OWNERDRAW) == BS_OWNERDRAW; | |
333 | } | |
334 | ||
335 | void wxCheckBox::MakeOwnerDrawn(bool ownerDrawn) | |
336 | { | |
337 | long style = ::GetWindowLong(GetHwnd(), GWL_STYLE); | |
338 | ||
339 | // note that BS_CHECKBOX & BS_OWNERDRAW != 0 so we can't operate on | |
340 | // them as on independent style bits | |
341 | if ( ownerDrawn ) | |
342 | { | |
343 | style &= ~(BS_CHECKBOX | BS_3STATE); | |
344 | style |= BS_OWNERDRAW; | |
345 | ||
346 | Connect(wxEVT_ENTER_WINDOW, | |
347 | wxMouseEventHandler(wxCheckBox::OnMouseEnterOrLeave)); | |
348 | Connect(wxEVT_LEAVE_WINDOW, | |
349 | wxMouseEventHandler(wxCheckBox::OnMouseEnterOrLeave)); | |
350 | Connect(wxEVT_LEFT_DOWN, wxMouseEventHandler(wxCheckBox::OnMouseLeft)); | |
351 | Connect(wxEVT_LEFT_UP, wxMouseEventHandler(wxCheckBox::OnMouseLeft)); | |
352 | Connect(wxEVT_SET_FOCUS, wxFocusEventHandler(wxCheckBox::OnFocus)); | |
353 | Connect(wxEVT_KILL_FOCUS, wxFocusEventHandler(wxCheckBox::OnFocus)); | |
354 | } | |
355 | else // reset to default colour | |
356 | { | |
357 | style &= ~BS_OWNERDRAW; | |
358 | style |= HasFlag(wxCHK_3STATE) ? BS_3STATE : BS_CHECKBOX; | |
359 | ||
360 | Disconnect(wxEVT_ENTER_WINDOW, | |
361 | wxMouseEventHandler(wxCheckBox::OnMouseEnterOrLeave)); | |
362 | Disconnect(wxEVT_LEAVE_WINDOW, | |
363 | wxMouseEventHandler(wxCheckBox::OnMouseEnterOrLeave)); | |
364 | Disconnect(wxEVT_LEFT_DOWN, wxMouseEventHandler(wxCheckBox::OnMouseLeft)); | |
365 | Disconnect(wxEVT_LEFT_UP, wxMouseEventHandler(wxCheckBox::OnMouseLeft)); | |
366 | Disconnect(wxEVT_SET_FOCUS, wxFocusEventHandler(wxCheckBox::OnFocus)); | |
367 | Disconnect(wxEVT_KILL_FOCUS, wxFocusEventHandler(wxCheckBox::OnFocus)); | |
368 | } | |
369 | ||
370 | ::SetWindowLong(GetHwnd(), GWL_STYLE, style); | |
371 | ||
372 | if ( !ownerDrawn ) | |
373 | { | |
374 | // ensure that controls state is consistent with internal state | |
375 | DoSet3StateValue(m_state); | |
376 | } | |
377 | } | |
378 | ||
379 | void wxCheckBox::OnMouseEnterOrLeave(wxMouseEvent& event) | |
380 | { | |
381 | m_isHot = event.GetEventType() == wxEVT_ENTER_WINDOW; | |
382 | if ( !m_isHot ) | |
383 | m_isPressed = false; | |
384 | ||
385 | Refresh(); | |
386 | ||
387 | event.Skip(); | |
388 | } | |
389 | ||
390 | void wxCheckBox::OnMouseLeft(wxMouseEvent& event) | |
391 | { | |
392 | // TODO: we should capture the mouse here to be notified about left up | |
393 | // event but this interferes with BN_CLICKED generation so if we | |
394 | // want to do this we'd need to generate them ourselves | |
395 | m_isPressed = event.GetEventType() == wxEVT_LEFT_DOWN; | |
396 | Refresh(); | |
397 | ||
398 | event.Skip(); | |
399 | } | |
400 | ||
401 | void wxCheckBox::OnFocus(wxFocusEvent& event) | |
402 | { | |
403 | Refresh(); | |
404 | ||
405 | event.Skip(); | |
406 | } | |
407 | ||
408 | bool wxCheckBox::MSWOnDraw(WXDRAWITEMSTRUCT *item) | |
409 | { | |
410 | DRAWITEMSTRUCT *dis = (DRAWITEMSTRUCT *)item; | |
411 | ||
412 | if ( !IsOwnerDrawn() || dis->CtlType != ODT_BUTTON ) | |
413 | return wxCheckBoxBase::MSWOnDraw(item); | |
414 | ||
415 | // calculate the rectangles for the check mark itself and the label | |
416 | HDC hdc = dis->hDC; | |
417 | RECT& rect = dis->rcItem; | |
418 | RECT rectCheck, | |
419 | rectLabel; | |
420 | rectCheck.top = | |
421 | rectLabel.top = rect.top; | |
422 | rectCheck.bottom = | |
423 | rectLabel.bottom = rect.bottom; | |
424 | const int checkSize = GetBestSize().y; | |
425 | const int MARGIN = 3; | |
426 | ||
427 | const bool isRightAligned = HasFlag(wxALIGN_RIGHT); | |
428 | if ( isRightAligned ) | |
429 | { | |
430 | rectCheck.right = rect.right; | |
431 | rectCheck.left = rectCheck.right - checkSize; | |
432 | ||
433 | rectLabel.right = rectCheck.left - MARGIN; | |
434 | rectLabel.left = rect.left; | |
435 | } | |
436 | else // normal, left-aligned checkbox | |
437 | { | |
438 | rectCheck.left = rect.left; | |
439 | rectCheck.right = rectCheck.left + checkSize; | |
440 | ||
441 | rectLabel.left = rectCheck.right + MARGIN; | |
442 | rectLabel.right = rect.right; | |
443 | } | |
444 | ||
445 | // show we draw a focus rect? | |
446 | const bool isFocused = m_isPressed || FindFocus() == this; | |
447 | ||
448 | ||
449 | // draw the checkbox itself | |
450 | wxDCTemp dc(hdc); | |
451 | ||
452 | int flags = 0; | |
453 | if ( !IsEnabled() ) | |
454 | flags |= wxCONTROL_DISABLED; | |
455 | switch ( Get3StateValue() ) | |
456 | { | |
457 | case wxCHK_CHECKED: | |
458 | flags |= wxCONTROL_CHECKED; | |
459 | break; | |
460 | ||
461 | case wxCHK_UNDETERMINED: | |
462 | flags |= wxCONTROL_PRESSED; | |
463 | break; | |
464 | ||
465 | default: | |
466 | wxFAIL_MSG( _T("unexpected Get3StateValue() return value") ); | |
467 | // fall through | |
468 | ||
469 | case wxCHK_UNCHECKED: | |
470 | // no extra styles needed | |
471 | break; | |
472 | } | |
473 | ||
474 | if ( wxFindWindowAtPoint(wxGetMousePosition()) == this ) | |
475 | flags |= wxCONTROL_CURRENT; | |
476 | ||
477 | wxRendererNative::Get(). | |
478 | DrawCheckBox(this, dc, wxRectFromRECT(rectCheck), flags); | |
479 | ||
480 | // draw the text | |
481 | const wxString& label = GetLabel(); | |
482 | ||
483 | // first we need to measure it | |
484 | UINT fmt = DT_NOCLIP; | |
485 | ||
486 | // drawing underlying doesn't look well with focus rect (and the native | |
487 | // control doesn't do it) | |
488 | if ( isFocused ) | |
489 | fmt |= DT_HIDEPREFIX; | |
490 | if ( isRightAligned ) | |
491 | fmt |= DT_RIGHT; | |
492 | // TODO: also use DT_HIDEPREFIX if the system is configured so | |
493 | ||
494 | // we need to get the label real size first if we have to draw a focus rect | |
495 | // around it | |
496 | if ( isFocused ) | |
497 | { | |
498 | if ( !::DrawText(hdc, label.wx_str(), label.length(), &rectLabel, | |
499 | fmt | DT_CALCRECT) ) | |
500 | { | |
501 | wxLogLastError(_T("DrawText(DT_CALCRECT)")); | |
502 | } | |
503 | } | |
504 | ||
505 | if ( !IsEnabled() ) | |
506 | { | |
507 | ::SetTextColor(hdc, ::GetSysColor(COLOR_GRAYTEXT)); | |
508 | } | |
509 | ||
510 | if ( !::DrawText(hdc, label.wx_str(), label.length(), &rectLabel, fmt) ) | |
511 | { | |
512 | wxLogLastError(_T("DrawText()")); | |
513 | } | |
514 | ||
515 | // finally draw the focus | |
516 | if ( isFocused ) | |
517 | { | |
518 | rectLabel.left--; | |
519 | rectLabel.right++; | |
520 | if ( !::DrawFocusRect(hdc, &rectLabel) ) | |
521 | { | |
522 | wxLogLastError(_T("DrawFocusRect()")); | |
523 | } | |
524 | } | |
525 | ||
526 | return true; | |
527 | } | |
528 | ||
529 | #endif // wxUSE_CHECKBOX |