]>
Commit | Line | Data |
---|---|---|
2bda0e17 | 1 | ///////////////////////////////////////////////////////////////////////////// |
faa49bfd | 2 | // Name: src/msw/radiobox.cpp |
3ca6a5f0 | 3 | // Purpose: wxRadioBox implementation |
2bda0e17 KB |
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 | ||
e373f51b 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__ | |
da07e033 | 24 | #pragma hdrstop |
2bda0e17 KB |
25 | #endif |
26 | ||
1e6feb95 VZ |
27 | #if wxUSE_RADIOBOX |
28 | ||
cc11cc69 WS |
29 | #include "wx/radiobox.h" |
30 | ||
2bda0e17 | 31 | #ifndef WX_PRECOMP |
da07e033 VZ |
32 | #include "wx/bitmap.h" |
33 | #include "wx/brush.h" | |
f6bcfd97 | 34 | #include "wx/settings.h" |
381dd4bf | 35 | #include "wx/log.h" |
2bda0e17 KB |
36 | #endif |
37 | ||
bd0a76e2 | 38 | #include "wx/msw/subwin.h" |
2bda0e17 | 39 | |
f048e32f | 40 | #if wxUSE_TOOLTIPS |
f048e32f VZ |
41 | #include "wx/tooltip.h" |
42 | #endif // wxUSE_TOOLTIPS | |
43 | ||
3ff066a4 | 44 | // TODO: wxCONSTRUCTOR |
bc9fb572 JS |
45 | #if 0 // wxUSE_EXTENDED_RTTI |
46 | WX_DEFINE_FLAGS( wxRadioBoxStyle ) | |
47 | ||
3ff066a4 | 48 | wxBEGIN_FLAGS( wxRadioBoxStyle ) |
bc9fb572 JS |
49 | // new style border flags, we put them first to |
50 | // use them for streaming out | |
3ff066a4 SC |
51 | wxFLAGS_MEMBER(wxBORDER_SIMPLE) |
52 | wxFLAGS_MEMBER(wxBORDER_SUNKEN) | |
53 | wxFLAGS_MEMBER(wxBORDER_DOUBLE) | |
54 | wxFLAGS_MEMBER(wxBORDER_RAISED) | |
55 | wxFLAGS_MEMBER(wxBORDER_STATIC) | |
56 | wxFLAGS_MEMBER(wxBORDER_NONE) | |
4d08943e | 57 | |
bc9fb572 | 58 | // old style border flags |
3ff066a4 SC |
59 | wxFLAGS_MEMBER(wxSIMPLE_BORDER) |
60 | wxFLAGS_MEMBER(wxSUNKEN_BORDER) | |
61 | wxFLAGS_MEMBER(wxDOUBLE_BORDER) | |
62 | wxFLAGS_MEMBER(wxRAISED_BORDER) | |
63 | wxFLAGS_MEMBER(wxSTATIC_BORDER) | |
cb0afb26 | 64 | wxFLAGS_MEMBER(wxBORDER) |
bc9fb572 JS |
65 | |
66 | // standard window styles | |
3ff066a4 SC |
67 | wxFLAGS_MEMBER(wxTAB_TRAVERSAL) |
68 | wxFLAGS_MEMBER(wxCLIP_CHILDREN) | |
69 | wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW) | |
70 | wxFLAGS_MEMBER(wxWANTS_CHARS) | |
cb0afb26 | 71 | wxFLAGS_MEMBER(wxFULL_REPAINT_ON_RESIZE) |
3ff066a4 SC |
72 | wxFLAGS_MEMBER(wxALWAYS_SHOW_SB ) |
73 | wxFLAGS_MEMBER(wxVSCROLL) | |
74 | wxFLAGS_MEMBER(wxHSCROLL) | |
75 | ||
76 | wxFLAGS_MEMBER(wxRA_SPECIFY_COLS) | |
77 | wxFLAGS_MEMBER(wxRA_HORIZONTAL) | |
78 | wxFLAGS_MEMBER(wxRA_SPECIFY_ROWS) | |
79 | wxFLAGS_MEMBER(wxRA_VERTICAL) | |
80 | ||
81 | wxEND_FLAGS( wxRadioBoxStyle ) | |
bc9fb572 JS |
82 | |
83 | IMPLEMENT_DYNAMIC_CLASS_XTI(wxRadioBox, wxControl,"wx/radiobox.h") | |
84 | ||
3ff066a4 SC |
85 | wxBEGIN_PROPERTIES_TABLE(wxRadioBox) |
86 | wxEVENT_PROPERTY( Select , wxEVT_COMMAND_RADIOBOX_SELECTED , wxCommandEvent ) | |
87 | wxPROPERTY_FLAGS( WindowStyle , wxRadioBoxStyle , long , SetWindowStyleFlag , GetWindowStyleFlag , , 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style | |
88 | wxEND_PROPERTIES_TABLE() | |
bc9fb572 JS |
89 | |
90 | #else | |
d3acd369 | 91 | IMPLEMENT_DYNAMIC_CLASS(wxRadioBox, wxControl) |
bc9fb572 | 92 | #endif |
2bda0e17 | 93 | |
066f1b7a | 94 | /* |
4d08943e WS |
95 | selection |
96 | content | |
97 | label | |
98 | dimension | |
99 | item | |
066f1b7a SC |
100 | */ |
101 | ||
e373f51b VZ |
102 | // --------------------------------------------------------------------------- |
103 | // private functions | |
104 | // --------------------------------------------------------------------------- | |
105 | ||
e373f51b VZ |
106 | // wnd proc for radio buttons |
107 | LRESULT APIENTRY _EXPORT wxRadioBtnWndProc(HWND hWnd, | |
108 | UINT message, | |
109 | WPARAM wParam, | |
110 | LPARAM lParam); | |
111 | ||
112 | // --------------------------------------------------------------------------- | |
113 | // global vars | |
114 | // --------------------------------------------------------------------------- | |
115 | ||
116 | // the pointer to standard radio button wnd proc | |
457e6c54 | 117 | static WXFARPROC s_wndprocRadioBtn = (WXFARPROC)NULL; |
e373f51b VZ |
118 | |
119 | // =========================================================================== | |
120 | // implementation | |
121 | // =========================================================================== | |
122 | ||
123 | // --------------------------------------------------------------------------- | |
bd0a76e2 | 124 | // wxRadioBox creation |
e373f51b VZ |
125 | // --------------------------------------------------------------------------- |
126 | ||
2bda0e17 | 127 | // Radio box item |
bd0a76e2 | 128 | void wxRadioBox::Init() |
2bda0e17 | 129 | { |
bd0a76e2 | 130 | m_selectedButton = wxNOT_FOUND; |
da07e033 | 131 | m_radioButtons = NULL; |
e373f51b VZ |
132 | m_radioWidth = NULL; |
133 | m_radioHeight = NULL; | |
2bda0e17 KB |
134 | } |
135 | ||
f048e32f VZ |
136 | bool wxRadioBox::Create(wxWindow *parent, |
137 | wxWindowID id, | |
138 | const wxString& title, | |
139 | const wxPoint& pos, | |
140 | const wxSize& size, | |
141 | int n, | |
142 | const wxString choices[], | |
143 | int majorDim, | |
144 | long style, | |
145 | const wxValidator& val, | |
146 | const wxString& name) | |
2bda0e17 | 147 | { |
f048e32f | 148 | // common initialization |
bd0a76e2 | 149 | if ( !wxStaticBox::Create(parent, id, title, pos, size, style, name) ) |
4d08943e | 150 | return false; |
2bda0e17 | 151 | |
bd0a76e2 VZ |
152 | #if wxUSE_VALIDATORS |
153 | SetValidator(val); | |
154 | #else | |
155 | wxUnusedVar(val); | |
156 | #endif // wxUSE_VALIDATORS/!wxUSE_VALIDATORS | |
da07e033 | 157 | |
bd0a76e2 | 158 | m_radioButtons = new wxSubwindows(n); |
e373f51b VZ |
159 | m_radioWidth = new int[n]; |
160 | m_radioHeight = new int[n]; | |
f048e32f | 161 | |
f048e32f VZ |
162 | for ( int i = 0; i < n; i++ ) |
163 | { | |
164 | m_radioWidth[i] = | |
4d08943e | 165 | m_radioHeight[i] = wxDefaultCoord; |
d3acd369 | 166 | long styleBtn = BS_AUTORADIOBUTTON | WS_TABSTOP | WS_CHILD | WS_VISIBLE; |
1f621c2f | 167 | if ( i == 0 ) |
f048e32f VZ |
168 | styleBtn |= WS_GROUP; |
169 | ||
da07e033 | 170 | long newId = NewControlId(); |
da07e033 | 171 | |
f048e32f | 172 | HWND hwndBtn = ::CreateWindow(_T("BUTTON"), |
e0a050e3 | 173 | choices[i].wx_str(), |
f048e32f VZ |
174 | styleBtn, |
175 | 0, 0, 0, 0, // will be set in SetSize() | |
99c613c5 | 176 | GetHwndOf(parent), |
f048e32f VZ |
177 | (HMENU)newId, |
178 | wxGetInstance(), | |
e373f51b | 179 | NULL); |
2bda0e17 | 180 | |
f048e32f VZ |
181 | if ( !hwndBtn ) |
182 | { | |
f6bcfd97 | 183 | wxLogLastError(wxT("CreateWindow(radio btn)")); |
f048e32f | 184 | |
4d08943e | 185 | return false; |
f048e32f VZ |
186 | } |
187 | ||
bd0a76e2 | 188 | (*m_radioButtons)[i] = hwndBtn; |
42e69d6b | 189 | |
e373f51b | 190 | SubclassRadioButton((WXHWND)hwndBtn); |
2bda0e17 | 191 | |
f048e32f | 192 | m_subControls.Add(newId); |
da07e033 | 193 | } |
e373f51b | 194 | |
da07e033 | 195 | // Create a dummy radio control to end the group. |
f048e32f | 196 | (void)::CreateWindow(_T("BUTTON"), |
fda7962d | 197 | wxEmptyString, |
f048e32f | 198 | WS_GROUP | BS_AUTORADIOBUTTON | WS_CHILD, |
99c613c5 | 199 | 0, 0, 0, 0, GetHwndOf(parent), |
e373f51b | 200 | (HMENU)NewControlId(), wxGetInstance(), NULL); |
2bda0e17 | 201 | |
bd0a76e2 VZ |
202 | m_radioButtons->SetFont(GetFont()); |
203 | ||
64133c38 JS |
204 | #ifdef __WXWINCE__ |
205 | // Set the z-order correctly | |
d26e1ab2 | 206 | SetWindowPos(GetHwnd(), HWND_BOTTOM, 0, 0, 0, 0, SWP_NOMOVE|SWP_NOSIZE); |
64133c38 JS |
207 | #endif |
208 | ||
f3984afa | 209 | SetMajorDim(majorDim == 0 ? n : majorDim, style); |
da07e033 | 210 | SetSelection(0); |
f048e32f | 211 | SetSize(pos.x, pos.y, size.x, size.y); |
2bda0e17 | 212 | |
7a5a5718 | 213 | // Now that we have items determine what is the best size and set it. |
170acdc9 | 214 | SetInitialSize(size); |
4d08943e WS |
215 | |
216 | return true; | |
2bda0e17 | 217 | } |
2bda0e17 | 218 | |
584ad2a3 MB |
219 | bool wxRadioBox::Create(wxWindow *parent, |
220 | wxWindowID id, | |
221 | const wxString& title, | |
222 | const wxPoint& pos, | |
223 | const wxSize& size, | |
224 | const wxArrayString& choices, | |
225 | int majorDim, | |
226 | long style, | |
227 | const wxValidator& val, | |
228 | const wxString& name) | |
229 | { | |
230 | wxCArrayString chs(choices); | |
231 | return Create(parent, id, title, pos, size, chs.GetCount(), | |
232 | chs.GetStrings(), majorDim, style, val, name); | |
233 | } | |
234 | ||
e373f51b | 235 | wxRadioBox::~wxRadioBox() |
2bda0e17 | 236 | { |
4d08943e | 237 | m_isBeingDeleted = true; |
1eb20d4a | 238 | |
bd0a76e2 VZ |
239 | delete m_radioButtons; |
240 | delete[] m_radioWidth; | |
241 | delete[] m_radioHeight; | |
242 | } | |
243 | ||
244 | // NB: if this code is changed, wxGetWindowForHWND() which relies on having the | |
245 | // radiobox pointer in GWL_USERDATA for radio buttons must be updated too! | |
246 | void wxRadioBox::SubclassRadioButton(WXHWND hWndBtn) | |
247 | { | |
248 | HWND hwndBtn = (HWND)hWndBtn; | |
249 | ||
250 | if ( !s_wndprocRadioBtn ) | |
251 | s_wndprocRadioBtn = (WXFARPROC)wxGetWindowProc(hwndBtn); | |
252 | ||
253 | wxSetWindowProc(hwndBtn, wxRadioBtnWndProc); | |
254 | wxSetWindowUserData(hwndBtn, this); | |
255 | } | |
256 | ||
257 | // ---------------------------------------------------------------------------- | |
258 | // events generation | |
259 | // ---------------------------------------------------------------------------- | |
260 | ||
0edeeb6d | 261 | bool wxRadioBox::MSWCommand(WXUINT cmd, WXWORD id_) |
bd0a76e2 | 262 | { |
0edeeb6d VZ |
263 | const int id = (signed short)id_; |
264 | ||
bd0a76e2 | 265 | if ( cmd == BN_CLICKED ) |
da07e033 | 266 | { |
bd0a76e2 VZ |
267 | if (id == GetId()) |
268 | return true; | |
269 | ||
270 | int selectedButton = wxNOT_FOUND; | |
271 | ||
aa61d352 VZ |
272 | const unsigned int count = GetCount(); |
273 | for ( unsigned int i = 0; i < count; i++ ) | |
bd0a76e2 | 274 | { |
bf511d57 VZ |
275 | const HWND hwndBtn = (*m_radioButtons)[i]; |
276 | if ( id == wxGetWindowId(hwndBtn) ) | |
bd0a76e2 | 277 | { |
bf511d57 VZ |
278 | // we can get BN_CLICKED for a button which just became focused |
279 | // but it may not be checked, in which case we shouldn't | |
280 | // generate a radiobox selection changed event for it | |
281 | if ( ::SendMessage(hwndBtn, BM_GETCHECK, 0, 0) == BST_CHECKED ) | |
282 | selectedButton = i; | |
bd0a76e2 VZ |
283 | |
284 | break; | |
285 | } | |
286 | } | |
287 | ||
288 | if ( selectedButton == wxNOT_FOUND ) | |
289 | { | |
290 | // just ignore it - due to a hack with WM_NCHITTEST handling in our | |
291 | // wnd proc, we can receive dummy click messages when we click near | |
292 | // the radiobox edge (this is ugly but Julian wouldn't let me get | |
293 | // rid of this...) | |
294 | return false; | |
295 | } | |
296 | ||
297 | if ( selectedButton != m_selectedButton ) | |
298 | { | |
299 | m_selectedButton = selectedButton; | |
300 | ||
301 | SendNotificationEvent(); | |
302 | } | |
303 | //else: don't generate events when the selection doesn't change | |
304 | ||
305 | return true; | |
da07e033 | 306 | } |
bd0a76e2 VZ |
307 | else |
308 | return false; | |
309 | } | |
42e69d6b | 310 | |
bd0a76e2 VZ |
311 | void wxRadioBox::Command(wxCommandEvent & event) |
312 | { | |
687706f5 | 313 | SetSelection (event.GetInt()); |
bd0a76e2 VZ |
314 | SetFocus(); |
315 | ProcessCommand(event); | |
316 | } | |
2bda0e17 | 317 | |
bd0a76e2 VZ |
318 | void wxRadioBox::SendNotificationEvent() |
319 | { | |
320 | wxCommandEvent event(wxEVT_COMMAND_RADIOBOX_SELECTED, m_windowId); | |
321 | event.SetInt( m_selectedButton ); | |
aa61d352 | 322 | event.SetString(GetString(m_selectedButton)); |
bd0a76e2 VZ |
323 | event.SetEventObject( this ); |
324 | ProcessCommand(event); | |
2bda0e17 KB |
325 | } |
326 | ||
bd0a76e2 VZ |
327 | // ---------------------------------------------------------------------------- |
328 | // simple accessors | |
329 | // ---------------------------------------------------------------------------- | |
330 | ||
aa61d352 | 331 | unsigned int wxRadioBox::GetCount() const |
2bda0e17 | 332 | { |
04988ec4 | 333 | return m_radioButtons ? m_radioButtons->GetCount() : 0u; |
bd0a76e2 | 334 | } |
d66a042c | 335 | |
aa61d352 | 336 | void wxRadioBox::SetString(unsigned int item, const wxString& label) |
bd0a76e2 | 337 | { |
789f6795 | 338 | wxCHECK_RET( IsValid(item), wxT("invalid radiobox index") ); |
e373f51b | 339 | |
bd0a76e2 VZ |
340 | m_radioWidth[item] = |
341 | m_radioHeight[item] = wxDefaultCoord; | |
2bda0e17 | 342 | |
bd0a76e2 | 343 | ::SetWindowText((*m_radioButtons)[item], label.c_str()); |
31582e4e RD |
344 | |
345 | InvalidateBestSize(); | |
2bda0e17 KB |
346 | } |
347 | ||
bd0a76e2 | 348 | void wxRadioBox::SetSelection(int N) |
2bda0e17 | 349 | { |
789f6795 | 350 | wxCHECK_RET( IsValid(N), wxT("invalid radiobox index") ); |
bd0a76e2 | 351 | |
27758ba7 VZ |
352 | // unselect the old button |
353 | if ( m_selectedButton != wxNOT_FOUND ) | |
354 | ::SendMessage((*m_radioButtons)[m_selectedButton], BM_SETCHECK, 0, 0L); | |
355 | ||
356 | // and select the new one | |
bd0a76e2 VZ |
357 | ::SendMessage((*m_radioButtons)[N], BM_SETCHECK, 1, 0L); |
358 | ||
359 | m_selectedButton = N; | |
2bda0e17 KB |
360 | } |
361 | ||
362 | // Find string for position | |
aa61d352 | 363 | wxString wxRadioBox::GetString(unsigned int item) const |
2bda0e17 | 364 | { |
789f6795 | 365 | wxCHECK_MSG( IsValid(item), wxEmptyString, |
1e6feb95 VZ |
366 | wxT("invalid radiobox index") ); |
367 | ||
bd0a76e2 VZ |
368 | return wxGetWindowText((*m_radioButtons)[item]); |
369 | } | |
370 | ||
371 | void wxRadioBox::SetFocus() | |
372 | { | |
373 | if ( GetCount() > 0 ) | |
374 | { | |
375 | ::SetFocus((*m_radioButtons)[m_selectedButton == wxNOT_FOUND | |
376 | ? 0 | |
377 | : m_selectedButton]); | |
378 | } | |
379 | } | |
380 | ||
381 | // Enable a specific button | |
aa61d352 | 382 | bool wxRadioBox::Enable(unsigned int item, bool enable) |
bd0a76e2 | 383 | { |
789f6795 | 384 | wxCHECK_MSG( IsValid(item), false, |
bd0a76e2 VZ |
385 | wxT("invalid item in wxRadioBox::Enable()") ); |
386 | ||
789f6795 WS |
387 | BOOL ret = ::EnableWindow((*m_radioButtons)[item], enable); |
388 | ||
3bfa7be9 VZ |
389 | return (ret == 0) != enable; |
390 | } | |
391 | ||
aa61d352 | 392 | bool wxRadioBox::IsItemEnabled(unsigned int item) const |
3bfa7be9 VZ |
393 | { |
394 | wxCHECK_MSG( IsValid(item), false, | |
f2be5580 | 395 | wxT("invalid item in wxRadioBox::IsItemEnabled()") ); |
3bfa7be9 VZ |
396 | |
397 | return ::IsWindowEnabled((*m_radioButtons)[item]) != 0; | |
2bda0e17 KB |
398 | } |
399 | ||
bd0a76e2 | 400 | // Show a specific button |
aa61d352 | 401 | bool wxRadioBox::Show(unsigned int item, bool show) |
bd0a76e2 | 402 | { |
789f6795 | 403 | wxCHECK_MSG( IsValid(item), false, |
bd0a76e2 VZ |
404 | wxT("invalid item in wxRadioBox::Show()") ); |
405 | ||
789f6795 WS |
406 | BOOL ret = ::ShowWindow((*m_radioButtons)[item], show ? SW_SHOW : SW_HIDE); |
407 | ||
3bfa7be9 VZ |
408 | bool changed = (ret != 0) != show; |
409 | if ( changed ) | |
410 | { | |
31582e4e | 411 | InvalidateBestSize(); |
3bfa7be9 VZ |
412 | } |
413 | ||
31582e4e | 414 | return changed; |
bd0a76e2 VZ |
415 | } |
416 | ||
aa61d352 | 417 | bool wxRadioBox::IsItemShown(unsigned int item) const |
3bfa7be9 VZ |
418 | { |
419 | wxCHECK_MSG( IsValid(item), false, | |
f2be5580 | 420 | wxT("invalid item in wxRadioBox::IsItemShown()") ); |
3bfa7be9 VZ |
421 | |
422 | // don't use IsWindowVisible() here because it would return false if the | |
423 | // radiobox itself is hidden while we want to only return false if this | |
424 | // button specifically is hidden | |
425 | return (::GetWindowLong((*m_radioButtons)[item], | |
426 | GWL_STYLE) & WS_VISIBLE) != 0; | |
427 | } | |
428 | ||
c670c855 VZ |
429 | #if wxUSE_TOOLTIPS |
430 | ||
431 | bool wxRadioBox::HasToolTips() const | |
432 | { | |
433 | return wxStaticBox::HasToolTips() || wxRadioBoxBase::HasItemToolTips(); | |
434 | } | |
435 | ||
436 | void wxRadioBox::DoSetItemToolTip(unsigned int item, wxToolTip *tooltip) | |
437 | { | |
438 | // we have already checked for the item to be valid in wxRadioBoxBase | |
439 | const HWND hwndRbtn = (*m_radioButtons)[item]; | |
440 | if ( tooltip != NULL ) | |
441 | tooltip->Add(hwndRbtn); | |
442 | else // unset the tooltip | |
443 | wxToolTip::Remove(hwndRbtn); | |
444 | } | |
445 | ||
446 | #endif // wxUSE_TOOLTIPS | |
447 | ||
bd0a76e2 VZ |
448 | WX_FORWARD_STD_METHODS_TO_SUBWINDOWS(wxRadioBox, wxStaticBox, m_radioButtons) |
449 | ||
3ca6a5f0 BP |
450 | // ---------------------------------------------------------------------------- |
451 | // size calculations | |
452 | // ---------------------------------------------------------------------------- | |
453 | ||
454 | wxSize wxRadioBox::GetMaxButtonSize() const | |
455 | { | |
456 | // calculate the max button size | |
457 | int widthMax = 0, | |
458 | heightMax = 0; | |
aa61d352 VZ |
459 | const unsigned int count = GetCount(); |
460 | for ( unsigned int i = 0 ; i < count; i++ ) | |
3ca6a5f0 BP |
461 | { |
462 | int width, height; | |
463 | if ( m_radioWidth[i] < 0 ) | |
464 | { | |
bd0a76e2 | 465 | GetTextExtent(wxGetWindowText((*m_radioButtons)[i]), &width, &height); |
3ca6a5f0 BP |
466 | |
467 | // adjust the size to take into account the radio box itself | |
468 | // FIXME this is totally bogus! | |
469 | width += RADIO_SIZE; | |
470 | height *= 3; | |
471 | height /= 2; | |
472 | } | |
473 | else | |
474 | { | |
475 | width = m_radioWidth[i]; | |
476 | height = m_radioHeight[i]; | |
477 | } | |
478 | ||
479 | if ( widthMax < width ) | |
480 | widthMax = width; | |
481 | if ( heightMax < height ) | |
482 | heightMax = height; | |
483 | } | |
484 | ||
485 | return wxSize(widthMax, heightMax); | |
486 | } | |
487 | ||
488 | wxSize wxRadioBox::GetTotalButtonSize(const wxSize& sizeBtn) const | |
489 | { | |
490 | // the radiobox should be big enough for its buttons | |
491 | int cx1, cy1; | |
7a5e53ab | 492 | wxGetCharSize(m_hWnd, &cx1, &cy1, GetFont()); |
3ca6a5f0 BP |
493 | |
494 | int extraHeight = cy1; | |
495 | ||
21e0a4d5 VZ |
496 | int height = GetRowCount() * sizeBtn.y + cy1/2 + extraHeight; |
497 | int width = GetColumnCount() * (sizeBtn.x + cx1) + cx1; | |
3ca6a5f0 | 498 | |
c7fd6717 | 499 | // Add extra space under the label, if it exists. |
1a87edf2 | 500 | if (!wxControl::GetLabel().empty()) |
c7fd6717 JS |
501 | height += cy1/2; |
502 | ||
3ca6a5f0 BP |
503 | // and also wide enough for its label |
504 | int widthLabel; | |
32cd189d | 505 | GetTextExtent(GetLabelText(), &widthLabel, NULL); |
3ca6a5f0 BP |
506 | widthLabel += RADIO_SIZE; // FIXME this is bogus too |
507 | if ( widthLabel > width ) | |
508 | width = widthLabel; | |
509 | ||
510 | return wxSize(width, height); | |
511 | } | |
512 | ||
513 | wxSize wxRadioBox::DoGetBestSize() const | |
514 | { | |
d96b9caf VZ |
515 | if ( !m_radioButtons ) |
516 | { | |
517 | // if we're not fully initialized yet, we can't meaningfully compute | |
518 | // our best size, we'll do it later | |
519 | return wxSize(1, 1); | |
520 | } | |
521 | ||
31582e4e RD |
522 | wxSize best = GetTotalButtonSize(GetMaxButtonSize()); |
523 | CacheBestSize(best); | |
524 | return best; | |
3ca6a5f0 BP |
525 | } |
526 | ||
1f916a19 | 527 | // Restored old code. |
bfc6fde4 | 528 | void wxRadioBox::DoSetSize(int x, int y, int width, int height, int sizeFlags) |
1f916a19 | 529 | { |
4438caf4 VZ |
530 | int currentX, currentY; |
531 | GetPosition(¤tX, ¤tY); | |
c219cecc VZ |
532 | int widthOld, heightOld; |
533 | GetSize(&widthOld, &heightOld); | |
534 | ||
4438caf4 VZ |
535 | int xx = x; |
536 | int yy = y; | |
537 | ||
4d08943e | 538 | if (x == wxDefaultCoord && !(sizeFlags & wxSIZE_ALLOW_MINUS_ONE)) |
4438caf4 | 539 | xx = currentX; |
4d08943e | 540 | if (y == wxDefaultCoord && !(sizeFlags & wxSIZE_ALLOW_MINUS_ONE)) |
4438caf4 VZ |
541 | yy = currentY; |
542 | ||
99c613c5 JG |
543 | int y_offset = yy; |
544 | int x_offset = xx; | |
f048e32f | 545 | |
3ca6a5f0 | 546 | int cx1, cy1; |
7a5e53ab | 547 | wxGetCharSize(m_hWnd, &cx1, &cy1, GetFont()); |
4438caf4 VZ |
548 | |
549 | // Attempt to have a look coherent with other platforms: We compute the | |
550 | // biggest toggle dim, then we align all items according this value. | |
3ca6a5f0 BP |
551 | wxSize maxSize = GetMaxButtonSize(); |
552 | int maxWidth = maxSize.x, | |
553 | maxHeight = maxSize.y; | |
4438caf4 | 554 | |
3ca6a5f0 BP |
555 | wxSize totSize = GetTotalButtonSize(maxSize); |
556 | int totWidth = totSize.x, | |
557 | totHeight = totSize.y; | |
558 | ||
559 | // only change our width/height if asked for | |
4d08943e | 560 | if ( width == wxDefaultCoord ) |
1f916a19 | 561 | { |
3ca6a5f0 BP |
562 | if ( sizeFlags & wxSIZE_AUTO_WIDTH ) |
563 | width = totWidth; | |
4438caf4 | 564 | else |
3ca6a5f0 | 565 | width = widthOld; |
1f916a19 | 566 | } |
1f916a19 | 567 | |
4d08943e | 568 | if ( height == wxDefaultCoord ) |
4438caf4 | 569 | { |
3ca6a5f0 BP |
570 | if ( sizeFlags & wxSIZE_AUTO_HEIGHT ) |
571 | height = totHeight; | |
4438caf4 | 572 | else |
3ca6a5f0 BP |
573 | height = heightOld; |
574 | } | |
4438caf4 | 575 | |
7d86a2d4 | 576 | DoMoveWindow(xx, yy, width, height); |
c219cecc | 577 | |
3ca6a5f0 BP |
578 | // Now position all the buttons: the current button will be put at |
579 | // wxPoint(x_offset, y_offset) and the new row/column will start at | |
580 | // startX/startY. The size of all buttons will be the same wxSize(maxWidth, | |
581 | // maxHeight) except for the buttons in the last column which should extend | |
582 | // to the right border of radiobox and thus can be wider than this. | |
c219cecc | 583 | |
3ca6a5f0 | 584 | // Also, remember that wxRA_SPECIFY_COLS means that we arrange buttons in |
21e0a4d5 | 585 | // left to right order and GetMajorDim() is the number of columns while |
3ca6a5f0 | 586 | // wxRA_SPECIFY_ROWS means that the buttons are arranged top to bottom and |
21e0a4d5 | 587 | // GetMajorDim() is the number of rows. |
4438caf4 | 588 | |
3ca6a5f0 BP |
589 | x_offset += cx1; |
590 | y_offset += cy1; | |
1f916a19 | 591 | |
c7fd6717 | 592 | // Add extra space under the label, if it exists. |
1a87edf2 | 593 | if (!wxControl::GetLabel().empty()) |
c7fd6717 | 594 | y_offset += cy1/2; |
3ca6a5f0 | 595 | |
4438caf4 VZ |
596 | int startX = x_offset; |
597 | int startY = y_offset; | |
1f916a19 | 598 | |
aa61d352 VZ |
599 | const unsigned int count = GetCount(); |
600 | for (unsigned int i = 0; i < count; i++) | |
1f916a19 | 601 | { |
3ca6a5f0 BP |
602 | // the last button in the row may be wider than the other ones as the |
603 | // radiobox may be wider than the sum of the button widths (as it | |
604 | // happens, for example, when the radiobox label is very long) | |
605 | bool isLastInTheRow; | |
606 | if ( m_windowStyle & wxRA_SPECIFY_COLS ) | |
607 | { | |
608 | // item is the last in its row if it is a multiple of the number of | |
609 | // columns or if it is just the last item | |
aa61d352 | 610 | unsigned int n = i + 1; |
21e0a4d5 | 611 | isLastInTheRow = ((n % GetMajorDim()) == 0) || (n == count); |
3ca6a5f0 BP |
612 | } |
613 | else // wxRA_SPECIFY_ROWS | |
4438caf4 | 614 | { |
3ca6a5f0 | 615 | // item is the last in the row if it is in the last columns |
21e0a4d5 | 616 | isLastInTheRow = i >= (count/GetMajorDim())*GetMajorDim(); |
3ca6a5f0 BP |
617 | } |
618 | ||
619 | // is this the start of new row/column? | |
21e0a4d5 | 620 | if ( i && (i % GetMajorDim() == 0) ) |
3ca6a5f0 BP |
621 | { |
622 | if ( m_windowStyle & wxRA_SPECIFY_ROWS ) | |
4438caf4 | 623 | { |
3ca6a5f0 | 624 | // start of new column |
4438caf4 VZ |
625 | y_offset = startY; |
626 | x_offset += maxWidth + cx1; | |
627 | } | |
3ca6a5f0 | 628 | else // start of new row |
4438caf4 VZ |
629 | { |
630 | x_offset = startX; | |
631 | y_offset += maxHeight; | |
632 | if (m_radioWidth[0]>0) | |
633 | y_offset += cy1/2; | |
634 | } | |
635 | } | |
3ca6a5f0 BP |
636 | |
637 | int widthBtn; | |
638 | if ( isLastInTheRow ) | |
4438caf4 | 639 | { |
3ca6a5f0 BP |
640 | // make the button go to the end of radio box |
641 | widthBtn = startX + width - x_offset - 2*cx1; | |
642 | if ( widthBtn < maxWidth ) | |
643 | widthBtn = maxWidth; | |
4438caf4 VZ |
644 | } |
645 | else | |
646 | { | |
3ca6a5f0 BP |
647 | // normal button, always of the same size |
648 | widthBtn = maxWidth; | |
4438caf4 | 649 | } |
1f916a19 | 650 | |
bd0a76e2 VZ |
651 | // make all buttons of the same, maximal size - like this they cover |
652 | // the radiobox entirely and the radiobox tooltips are always shown | |
653 | // (otherwise they are not when the mouse pointer is in the radiobox | |
654 | // part not belonging to any radiobutton) | |
99c613c5 | 655 | DoMoveSibling((*m_radioButtons)[i], x_offset, y_offset, widthBtn, maxHeight); |
4438caf4 | 656 | |
3ca6a5f0 BP |
657 | // where do we put the next button? |
658 | if ( m_windowStyle & wxRA_SPECIFY_ROWS ) | |
4438caf4 | 659 | { |
3ca6a5f0 | 660 | // below this one |
4438caf4 VZ |
661 | y_offset += maxHeight; |
662 | if (m_radioWidth[0]>0) | |
663 | y_offset += cy1/2; | |
664 | } | |
665 | else | |
3ca6a5f0 BP |
666 | { |
667 | // to the right of this one | |
668 | x_offset += widthBtn + cx1; | |
669 | } | |
1f916a19 | 670 | } |
1f916a19 JS |
671 | } |
672 | ||
dc26eeb3 VZ |
673 | int wxRadioBox::GetItemFromPoint(const wxPoint& pt) const |
674 | { | |
675 | const unsigned int count = GetCount(); | |
676 | for ( unsigned int i = 0; i < count; i++ ) | |
677 | { | |
678 | RECT rect = wxGetWindowRect((*m_radioButtons)[i]); | |
679 | ||
680 | if ( rect.left <= pt.x && pt.x < rect.right && | |
681 | rect.top <= pt.y && pt.y < rect.bottom ) | |
682 | { | |
683 | return i; | |
684 | } | |
685 | } | |
686 | ||
687 | return wxNOT_FOUND; | |
688 | } | |
689 | ||
c3732409 VZ |
690 | // ---------------------------------------------------------------------------- |
691 | // radio box drawing | |
692 | // ---------------------------------------------------------------------------- | |
693 | ||
694 | #ifndef __WXWINCE__ | |
695 | ||
696 | WXHRGN wxRadioBox::MSWGetRegionWithoutChildren() | |
697 | { | |
698 | RECT rc; | |
699 | ::GetWindowRect(GetHwnd(), &rc); | |
700 | HRGN hrgn = ::CreateRectRgn(rc.left, rc.top, rc.right + 1, rc.bottom + 1); | |
701 | ||
aa61d352 VZ |
702 | const unsigned int count = GetCount(); |
703 | for ( unsigned int i = 0; i < count; ++i ) | |
c3732409 | 704 | { |
455c8f1d VZ |
705 | // don't clip out hidden children |
706 | if ( !IsItemShown(i) ) | |
707 | continue; | |
708 | ||
c3732409 VZ |
709 | ::GetWindowRect((*m_radioButtons)[i], &rc); |
710 | AutoHRGN hrgnchild(::CreateRectRgnIndirect(&rc)); | |
711 | ::CombineRgn(hrgn, hrgn, hrgnchild, RGN_DIFF); | |
712 | } | |
713 | ||
714 | return (WXHRGN)hrgn; | |
715 | } | |
716 | ||
717 | #endif // __WXWINCE__ | |
718 | ||
e373f51b VZ |
719 | // --------------------------------------------------------------------------- |
720 | // window proc for radio buttons | |
721 | // --------------------------------------------------------------------------- | |
722 | ||
723 | LRESULT APIENTRY _EXPORT wxRadioBtnWndProc(HWND hwnd, | |
8614c467 | 724 | UINT message, |
e373f51b VZ |
725 | WPARAM wParam, |
726 | LPARAM lParam) | |
727 | { | |
8614c467 | 728 | switch ( message ) |
e373f51b | 729 | { |
8614c467 VZ |
730 | case WM_GETDLGCODE: |
731 | // we must tell IsDialogMessage()/our kbd processing code that we | |
732 | // want to process arrows ourselves because neither of them is | |
733 | // smart enough to handle arrows properly for us | |
734 | { | |
8ad6ad99 | 735 | long lDlgCode = ::CallWindowProc(CASTWNDPROC s_wndprocRadioBtn, hwnd, |
8614c467 | 736 | message, wParam, lParam); |
8ad6ad99 | 737 | |
8614c467 VZ |
738 | return lDlgCode | DLGC_WANTARROWS; |
739 | } | |
e373f51b | 740 | |
8614c467 | 741 | case WM_KEYDOWN: |
9a5ccab4 | 742 | { |
975b6bcf | 743 | wxRadioBox *radiobox = (wxRadioBox *)wxGetWindowUserData(hwnd); |
f048e32f | 744 | |
8614c467 | 745 | wxCHECK_MSG( radiobox, 0, wxT("radio button without radio box?") ); |
f048e32f | 746 | |
4d08943e | 747 | bool processed = true; |
f048e32f | 748 | |
e421922f | 749 | wxDirection dir; |
8614c467 VZ |
750 | switch ( wParam ) |
751 | { | |
752 | case VK_UP: | |
1e6feb95 | 753 | dir = wxUP; |
8614c467 | 754 | break; |
f048e32f | 755 | |
8614c467 | 756 | case VK_LEFT: |
1e6feb95 | 757 | dir = wxLEFT; |
8614c467 | 758 | break; |
f048e32f | 759 | |
8614c467 | 760 | case VK_DOWN: |
1e6feb95 | 761 | dir = wxDOWN; |
8614c467 | 762 | break; |
f048e32f | 763 | |
8614c467 | 764 | case VK_RIGHT: |
1e6feb95 | 765 | dir = wxRIGHT; |
8614c467 VZ |
766 | break; |
767 | ||
768 | default: | |
4d08943e | 769 | processed = false; |
1e6feb95 VZ |
770 | |
771 | // just to suppress the compiler warning | |
772 | dir = wxALL; | |
8614c467 VZ |
773 | } |
774 | ||
775 | if ( processed ) | |
f048e32f | 776 | { |
1e6feb95 VZ |
777 | int selOld = radiobox->GetSelection(); |
778 | int selNew = radiobox->GetNextItem | |
779 | ( | |
780 | selOld, | |
781 | dir, | |
782 | radiobox->GetWindowStyle() | |
783 | ); | |
3ca6a5f0 | 784 | |
8614c467 VZ |
785 | if ( selNew != selOld ) |
786 | { | |
787 | radiobox->SetSelection(selNew); | |
974931fe | 788 | radiobox->SetFocus(); |
8614c467 VZ |
789 | |
790 | // emulate the button click | |
791 | radiobox->SendNotificationEvent(); | |
9a5ccab4 | 792 | |
8614c467 VZ |
793 | return 0; |
794 | } | |
f048e32f | 795 | } |
9a5ccab4 | 796 | } |
21687069 VZ |
797 | break; |
798 | ||
1dab049c VZ |
799 | case WM_SETFOCUS: |
800 | case WM_KILLFOCUS: | |
801 | { | |
975b6bcf | 802 | wxRadioBox *radiobox = (wxRadioBox *)wxGetWindowUserData(hwnd); |
1dab049c VZ |
803 | |
804 | wxCHECK_MSG( radiobox, 0, wxT("radio button without radio box?") ); | |
805 | ||
806 | // if we don't do this, no focus events are generated for the | |
807 | // radiobox and, besides, we need to notify the parent about | |
808 | // the focus change, otherwise the focus handling logic in | |
809 | // wxControlContainer doesn't work | |
810 | if ( message == WM_SETFOCUS ) | |
811 | radiobox->HandleSetFocus((WXHWND)wParam); | |
812 | else | |
813 | radiobox->HandleKillFocus((WXHWND)wParam); | |
814 | } | |
815 | break; | |
816 | ||
bd0a76e2 | 817 | #ifndef __WXWINCE__ |
fac93396 | 818 | case WM_HELP: |
e421922f | 819 | { |
975b6bcf | 820 | wxRadioBox *radiobox = (wxRadioBox *)wxGetWindowUserData(hwnd); |
fac93396 | 821 | |
e421922f | 822 | wxCHECK_MSG( radiobox, 0, wxT("radio button without radio box?") ); |
fac93396 | 823 | |
bd0a76e2 VZ |
824 | bool processed = false; |
825 | ||
826 | wxEvtHandler * const handler = radiobox->GetEventHandler(); | |
fac93396 | 827 | |
e421922f | 828 | HELPINFO* info = (HELPINFO*) lParam; |
bd0a76e2 | 829 | if ( info->iContextType == HELPINFO_WINDOW ) |
fac93396 | 830 | { |
bd0a76e2 VZ |
831 | for ( wxWindow* subjectOfHelp = radiobox; |
832 | subjectOfHelp; | |
833 | subjectOfHelp = subjectOfHelp->GetParent() ) | |
e421922f | 834 | { |
bd0a76e2 VZ |
835 | wxHelpEvent helpEvent(wxEVT_HELP, |
836 | subjectOfHelp->GetId(), | |
837 | wxPoint(info->MousePos.x, | |
838 | info->MousePos.y)); | |
e421922f | 839 | helpEvent.SetEventObject(radiobox); |
bd0a76e2 VZ |
840 | if ( handler->ProcessEvent(helpEvent) ) |
841 | { | |
842 | processed = true; | |
843 | break; | |
844 | } | |
e421922f | 845 | } |
fac93396 | 846 | } |
e421922f VZ |
847 | else if (info->iContextType == HELPINFO_MENUITEM) |
848 | { | |
bd0a76e2 | 849 | wxHelpEvent helpEvent(wxEVT_HELP, info->iCtrlId); |
e421922f | 850 | helpEvent.SetEventObject(radiobox); |
bd0a76e2 | 851 | processed = handler->ProcessEvent(helpEvent); |
e421922f | 852 | } |
bd0a76e2 VZ |
853 | |
854 | if ( processed ) | |
e421922f | 855 | return 0; |
e421922f | 856 | } |
c3732409 | 857 | break; |
bd0a76e2 | 858 | #endif // !__WXWINCE__ |
e373f51b VZ |
859 | } |
860 | ||
8ad6ad99 | 861 | return ::CallWindowProc(CASTWNDPROC s_wndprocRadioBtn, hwnd, message, wParam, lParam); |
e373f51b | 862 | } |
42e69d6b | 863 | |
1e6feb95 | 864 | #endif // wxUSE_RADIOBOX |