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