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