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