]>
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 VZ |
172 | HWND hwndBtn = ::CreateWindow(_T("BUTTON"), |
173 | choices[i], | |
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 | ||
261 | bool wxRadioBox::MSWCommand(WXUINT cmd, WXWORD id) | |
262 | { | |
263 | if ( cmd == BN_CLICKED ) | |
da07e033 | 264 | { |
bd0a76e2 VZ |
265 | if (id == GetId()) |
266 | return true; | |
267 | ||
268 | int selectedButton = wxNOT_FOUND; | |
269 | ||
aa61d352 VZ |
270 | const unsigned int count = GetCount(); |
271 | for ( unsigned int i = 0; i < count; i++ ) | |
bd0a76e2 VZ |
272 | { |
273 | if ( id == wxGetWindowId((*m_radioButtons)[i]) ) | |
274 | { | |
275 | selectedButton = i; | |
276 | ||
277 | break; | |
278 | } | |
279 | } | |
280 | ||
281 | if ( selectedButton == wxNOT_FOUND ) | |
282 | { | |
283 | // just ignore it - due to a hack with WM_NCHITTEST handling in our | |
284 | // wnd proc, we can receive dummy click messages when we click near | |
285 | // the radiobox edge (this is ugly but Julian wouldn't let me get | |
286 | // rid of this...) | |
287 | return false; | |
288 | } | |
289 | ||
290 | if ( selectedButton != m_selectedButton ) | |
291 | { | |
292 | m_selectedButton = selectedButton; | |
293 | ||
294 | SendNotificationEvent(); | |
295 | } | |
296 | //else: don't generate events when the selection doesn't change | |
297 | ||
298 | return true; | |
da07e033 | 299 | } |
bd0a76e2 VZ |
300 | else |
301 | return false; | |
302 | } | |
42e69d6b | 303 | |
bd0a76e2 VZ |
304 | void wxRadioBox::Command(wxCommandEvent & event) |
305 | { | |
687706f5 | 306 | SetSelection (event.GetInt()); |
bd0a76e2 VZ |
307 | SetFocus(); |
308 | ProcessCommand(event); | |
309 | } | |
2bda0e17 | 310 | |
bd0a76e2 VZ |
311 | void wxRadioBox::SendNotificationEvent() |
312 | { | |
313 | wxCommandEvent event(wxEVT_COMMAND_RADIOBOX_SELECTED, m_windowId); | |
314 | event.SetInt( m_selectedButton ); | |
aa61d352 | 315 | event.SetString(GetString(m_selectedButton)); |
bd0a76e2 VZ |
316 | event.SetEventObject( this ); |
317 | ProcessCommand(event); | |
2bda0e17 KB |
318 | } |
319 | ||
bd0a76e2 VZ |
320 | // ---------------------------------------------------------------------------- |
321 | // simple accessors | |
322 | // ---------------------------------------------------------------------------- | |
323 | ||
aa61d352 | 324 | unsigned int wxRadioBox::GetCount() const |
2bda0e17 | 325 | { |
04988ec4 | 326 | return m_radioButtons ? m_radioButtons->GetCount() : 0u; |
bd0a76e2 | 327 | } |
d66a042c | 328 | |
aa61d352 | 329 | void wxRadioBox::SetString(unsigned int item, const wxString& label) |
bd0a76e2 | 330 | { |
789f6795 | 331 | wxCHECK_RET( IsValid(item), wxT("invalid radiobox index") ); |
e373f51b | 332 | |
bd0a76e2 VZ |
333 | m_radioWidth[item] = |
334 | m_radioHeight[item] = wxDefaultCoord; | |
2bda0e17 | 335 | |
bd0a76e2 | 336 | ::SetWindowText((*m_radioButtons)[item], label.c_str()); |
31582e4e RD |
337 | |
338 | InvalidateBestSize(); | |
2bda0e17 KB |
339 | } |
340 | ||
bd0a76e2 | 341 | void wxRadioBox::SetSelection(int N) |
2bda0e17 | 342 | { |
789f6795 | 343 | wxCHECK_RET( IsValid(N), wxT("invalid radiobox index") ); |
bd0a76e2 | 344 | |
27758ba7 VZ |
345 | // unselect the old button |
346 | if ( m_selectedButton != wxNOT_FOUND ) | |
347 | ::SendMessage((*m_radioButtons)[m_selectedButton], BM_SETCHECK, 0, 0L); | |
348 | ||
349 | // and select the new one | |
bd0a76e2 VZ |
350 | ::SendMessage((*m_radioButtons)[N], BM_SETCHECK, 1, 0L); |
351 | ||
352 | m_selectedButton = N; | |
2bda0e17 KB |
353 | } |
354 | ||
355 | // Find string for position | |
aa61d352 | 356 | wxString wxRadioBox::GetString(unsigned int item) const |
2bda0e17 | 357 | { |
789f6795 | 358 | wxCHECK_MSG( IsValid(item), wxEmptyString, |
1e6feb95 VZ |
359 | wxT("invalid radiobox index") ); |
360 | ||
bd0a76e2 VZ |
361 | return wxGetWindowText((*m_radioButtons)[item]); |
362 | } | |
363 | ||
364 | void wxRadioBox::SetFocus() | |
365 | { | |
366 | if ( GetCount() > 0 ) | |
367 | { | |
368 | ::SetFocus((*m_radioButtons)[m_selectedButton == wxNOT_FOUND | |
369 | ? 0 | |
370 | : m_selectedButton]); | |
371 | } | |
372 | } | |
373 | ||
374 | // Enable a specific button | |
aa61d352 | 375 | bool wxRadioBox::Enable(unsigned int item, bool enable) |
bd0a76e2 | 376 | { |
789f6795 | 377 | wxCHECK_MSG( IsValid(item), false, |
bd0a76e2 VZ |
378 | wxT("invalid item in wxRadioBox::Enable()") ); |
379 | ||
789f6795 WS |
380 | BOOL ret = ::EnableWindow((*m_radioButtons)[item], enable); |
381 | ||
3bfa7be9 VZ |
382 | return (ret == 0) != enable; |
383 | } | |
384 | ||
aa61d352 | 385 | bool wxRadioBox::IsItemEnabled(unsigned int item) const |
3bfa7be9 VZ |
386 | { |
387 | wxCHECK_MSG( IsValid(item), false, | |
f2be5580 | 388 | wxT("invalid item in wxRadioBox::IsItemEnabled()") ); |
3bfa7be9 VZ |
389 | |
390 | return ::IsWindowEnabled((*m_radioButtons)[item]) != 0; | |
2bda0e17 KB |
391 | } |
392 | ||
bd0a76e2 | 393 | // Show a specific button |
aa61d352 | 394 | bool wxRadioBox::Show(unsigned int item, bool show) |
bd0a76e2 | 395 | { |
789f6795 | 396 | wxCHECK_MSG( IsValid(item), false, |
bd0a76e2 VZ |
397 | wxT("invalid item in wxRadioBox::Show()") ); |
398 | ||
789f6795 WS |
399 | BOOL ret = ::ShowWindow((*m_radioButtons)[item], show ? SW_SHOW : SW_HIDE); |
400 | ||
3bfa7be9 VZ |
401 | bool changed = (ret != 0) != show; |
402 | if ( changed ) | |
403 | { | |
31582e4e | 404 | InvalidateBestSize(); |
3bfa7be9 VZ |
405 | } |
406 | ||
31582e4e | 407 | return changed; |
bd0a76e2 VZ |
408 | } |
409 | ||
aa61d352 | 410 | bool wxRadioBox::IsItemShown(unsigned int item) const |
3bfa7be9 VZ |
411 | { |
412 | wxCHECK_MSG( IsValid(item), false, | |
f2be5580 | 413 | wxT("invalid item in wxRadioBox::IsItemShown()") ); |
3bfa7be9 VZ |
414 | |
415 | // don't use IsWindowVisible() here because it would return false if the | |
416 | // radiobox itself is hidden while we want to only return false if this | |
417 | // button specifically is hidden | |
418 | return (::GetWindowLong((*m_radioButtons)[item], | |
419 | GWL_STYLE) & WS_VISIBLE) != 0; | |
420 | } | |
421 | ||
c670c855 VZ |
422 | #if wxUSE_TOOLTIPS |
423 | ||
424 | bool wxRadioBox::HasToolTips() const | |
425 | { | |
426 | return wxStaticBox::HasToolTips() || wxRadioBoxBase::HasItemToolTips(); | |
427 | } | |
428 | ||
429 | void wxRadioBox::DoSetItemToolTip(unsigned int item, wxToolTip *tooltip) | |
430 | { | |
431 | // we have already checked for the item to be valid in wxRadioBoxBase | |
432 | const HWND hwndRbtn = (*m_radioButtons)[item]; | |
433 | if ( tooltip != NULL ) | |
434 | tooltip->Add(hwndRbtn); | |
435 | else // unset the tooltip | |
436 | wxToolTip::Remove(hwndRbtn); | |
437 | } | |
438 | ||
439 | #endif // wxUSE_TOOLTIPS | |
440 | ||
bd0a76e2 VZ |
441 | WX_FORWARD_STD_METHODS_TO_SUBWINDOWS(wxRadioBox, wxStaticBox, m_radioButtons) |
442 | ||
3ca6a5f0 BP |
443 | // ---------------------------------------------------------------------------- |
444 | // size calculations | |
445 | // ---------------------------------------------------------------------------- | |
446 | ||
447 | wxSize wxRadioBox::GetMaxButtonSize() const | |
448 | { | |
449 | // calculate the max button size | |
450 | int widthMax = 0, | |
451 | heightMax = 0; | |
aa61d352 VZ |
452 | const unsigned int count = GetCount(); |
453 | for ( unsigned int i = 0 ; i < count; i++ ) | |
3ca6a5f0 BP |
454 | { |
455 | int width, height; | |
456 | if ( m_radioWidth[i] < 0 ) | |
457 | { | |
bd0a76e2 | 458 | GetTextExtent(wxGetWindowText((*m_radioButtons)[i]), &width, &height); |
3ca6a5f0 BP |
459 | |
460 | // adjust the size to take into account the radio box itself | |
461 | // FIXME this is totally bogus! | |
462 | width += RADIO_SIZE; | |
463 | height *= 3; | |
464 | height /= 2; | |
465 | } | |
466 | else | |
467 | { | |
468 | width = m_radioWidth[i]; | |
469 | height = m_radioHeight[i]; | |
470 | } | |
471 | ||
472 | if ( widthMax < width ) | |
473 | widthMax = width; | |
474 | if ( heightMax < height ) | |
475 | heightMax = height; | |
476 | } | |
477 | ||
478 | return wxSize(widthMax, heightMax); | |
479 | } | |
480 | ||
481 | wxSize wxRadioBox::GetTotalButtonSize(const wxSize& sizeBtn) const | |
482 | { | |
483 | // the radiobox should be big enough for its buttons | |
484 | int cx1, cy1; | |
7a5e53ab | 485 | wxGetCharSize(m_hWnd, &cx1, &cy1, GetFont()); |
3ca6a5f0 BP |
486 | |
487 | int extraHeight = cy1; | |
488 | ||
21e0a4d5 VZ |
489 | int height = GetRowCount() * sizeBtn.y + cy1/2 + extraHeight; |
490 | int width = GetColumnCount() * (sizeBtn.x + cx1) + cx1; | |
3ca6a5f0 | 491 | |
c7fd6717 | 492 | // Add extra space under the label, if it exists. |
1a87edf2 | 493 | if (!wxControl::GetLabel().empty()) |
c7fd6717 JS |
494 | height += cy1/2; |
495 | ||
3ca6a5f0 BP |
496 | // and also wide enough for its label |
497 | int widthLabel; | |
32cd189d | 498 | GetTextExtent(GetLabelText(), &widthLabel, NULL); |
3ca6a5f0 BP |
499 | widthLabel += RADIO_SIZE; // FIXME this is bogus too |
500 | if ( widthLabel > width ) | |
501 | width = widthLabel; | |
502 | ||
503 | return wxSize(width, height); | |
504 | } | |
505 | ||
506 | wxSize wxRadioBox::DoGetBestSize() const | |
507 | { | |
d96b9caf VZ |
508 | if ( !m_radioButtons ) |
509 | { | |
510 | // if we're not fully initialized yet, we can't meaningfully compute | |
511 | // our best size, we'll do it later | |
512 | return wxSize(1, 1); | |
513 | } | |
514 | ||
31582e4e RD |
515 | wxSize best = GetTotalButtonSize(GetMaxButtonSize()); |
516 | CacheBestSize(best); | |
517 | return best; | |
3ca6a5f0 BP |
518 | } |
519 | ||
1f916a19 | 520 | // Restored old code. |
bfc6fde4 | 521 | void wxRadioBox::DoSetSize(int x, int y, int width, int height, int sizeFlags) |
1f916a19 | 522 | { |
4438caf4 VZ |
523 | int currentX, currentY; |
524 | GetPosition(¤tX, ¤tY); | |
c219cecc VZ |
525 | int widthOld, heightOld; |
526 | GetSize(&widthOld, &heightOld); | |
527 | ||
4438caf4 VZ |
528 | int xx = x; |
529 | int yy = y; | |
530 | ||
4d08943e | 531 | if (x == wxDefaultCoord && !(sizeFlags & wxSIZE_ALLOW_MINUS_ONE)) |
4438caf4 | 532 | xx = currentX; |
4d08943e | 533 | if (y == wxDefaultCoord && !(sizeFlags & wxSIZE_ALLOW_MINUS_ONE)) |
4438caf4 VZ |
534 | yy = currentY; |
535 | ||
99c613c5 JG |
536 | int y_offset = yy; |
537 | int x_offset = xx; | |
f048e32f | 538 | |
3ca6a5f0 | 539 | int cx1, cy1; |
7a5e53ab | 540 | wxGetCharSize(m_hWnd, &cx1, &cy1, GetFont()); |
4438caf4 VZ |
541 | |
542 | // Attempt to have a look coherent with other platforms: We compute the | |
543 | // biggest toggle dim, then we align all items according this value. | |
3ca6a5f0 BP |
544 | wxSize maxSize = GetMaxButtonSize(); |
545 | int maxWidth = maxSize.x, | |
546 | maxHeight = maxSize.y; | |
4438caf4 | 547 | |
3ca6a5f0 BP |
548 | wxSize totSize = GetTotalButtonSize(maxSize); |
549 | int totWidth = totSize.x, | |
550 | totHeight = totSize.y; | |
551 | ||
552 | // only change our width/height if asked for | |
4d08943e | 553 | if ( width == wxDefaultCoord ) |
1f916a19 | 554 | { |
3ca6a5f0 BP |
555 | if ( sizeFlags & wxSIZE_AUTO_WIDTH ) |
556 | width = totWidth; | |
4438caf4 | 557 | else |
3ca6a5f0 | 558 | width = widthOld; |
1f916a19 | 559 | } |
1f916a19 | 560 | |
4d08943e | 561 | if ( height == wxDefaultCoord ) |
4438caf4 | 562 | { |
3ca6a5f0 BP |
563 | if ( sizeFlags & wxSIZE_AUTO_HEIGHT ) |
564 | height = totHeight; | |
4438caf4 | 565 | else |
3ca6a5f0 BP |
566 | height = heightOld; |
567 | } | |
4438caf4 | 568 | |
7d86a2d4 | 569 | DoMoveWindow(xx, yy, width, height); |
c219cecc | 570 | |
3ca6a5f0 BP |
571 | // Now position all the buttons: the current button will be put at |
572 | // wxPoint(x_offset, y_offset) and the new row/column will start at | |
573 | // startX/startY. The size of all buttons will be the same wxSize(maxWidth, | |
574 | // maxHeight) except for the buttons in the last column which should extend | |
575 | // to the right border of radiobox and thus can be wider than this. | |
c219cecc | 576 | |
3ca6a5f0 | 577 | // Also, remember that wxRA_SPECIFY_COLS means that we arrange buttons in |
21e0a4d5 | 578 | // left to right order and GetMajorDim() is the number of columns while |
3ca6a5f0 | 579 | // wxRA_SPECIFY_ROWS means that the buttons are arranged top to bottom and |
21e0a4d5 | 580 | // GetMajorDim() is the number of rows. |
4438caf4 | 581 | |
3ca6a5f0 BP |
582 | x_offset += cx1; |
583 | y_offset += cy1; | |
1f916a19 | 584 | |
c7fd6717 | 585 | // Add extra space under the label, if it exists. |
1a87edf2 | 586 | if (!wxControl::GetLabel().empty()) |
c7fd6717 | 587 | y_offset += cy1/2; |
3ca6a5f0 | 588 | |
4438caf4 VZ |
589 | int startX = x_offset; |
590 | int startY = y_offset; | |
1f916a19 | 591 | |
aa61d352 VZ |
592 | const unsigned int count = GetCount(); |
593 | for (unsigned int i = 0; i < count; i++) | |
1f916a19 | 594 | { |
3ca6a5f0 BP |
595 | // the last button in the row may be wider than the other ones as the |
596 | // radiobox may be wider than the sum of the button widths (as it | |
597 | // happens, for example, when the radiobox label is very long) | |
598 | bool isLastInTheRow; | |
599 | if ( m_windowStyle & wxRA_SPECIFY_COLS ) | |
600 | { | |
601 | // item is the last in its row if it is a multiple of the number of | |
602 | // columns or if it is just the last item | |
aa61d352 | 603 | unsigned int n = i + 1; |
21e0a4d5 | 604 | isLastInTheRow = ((n % GetMajorDim()) == 0) || (n == count); |
3ca6a5f0 BP |
605 | } |
606 | else // wxRA_SPECIFY_ROWS | |
4438caf4 | 607 | { |
3ca6a5f0 | 608 | // item is the last in the row if it is in the last columns |
21e0a4d5 | 609 | isLastInTheRow = i >= (count/GetMajorDim())*GetMajorDim(); |
3ca6a5f0 BP |
610 | } |
611 | ||
612 | // is this the start of new row/column? | |
21e0a4d5 | 613 | if ( i && (i % GetMajorDim() == 0) ) |
3ca6a5f0 BP |
614 | { |
615 | if ( m_windowStyle & wxRA_SPECIFY_ROWS ) | |
4438caf4 | 616 | { |
3ca6a5f0 | 617 | // start of new column |
4438caf4 VZ |
618 | y_offset = startY; |
619 | x_offset += maxWidth + cx1; | |
620 | } | |
3ca6a5f0 | 621 | else // start of new row |
4438caf4 VZ |
622 | { |
623 | x_offset = startX; | |
624 | y_offset += maxHeight; | |
625 | if (m_radioWidth[0]>0) | |
626 | y_offset += cy1/2; | |
627 | } | |
628 | } | |
3ca6a5f0 BP |
629 | |
630 | int widthBtn; | |
631 | if ( isLastInTheRow ) | |
4438caf4 | 632 | { |
3ca6a5f0 BP |
633 | // make the button go to the end of radio box |
634 | widthBtn = startX + width - x_offset - 2*cx1; | |
635 | if ( widthBtn < maxWidth ) | |
636 | widthBtn = maxWidth; | |
4438caf4 VZ |
637 | } |
638 | else | |
639 | { | |
3ca6a5f0 BP |
640 | // normal button, always of the same size |
641 | widthBtn = maxWidth; | |
4438caf4 | 642 | } |
1f916a19 | 643 | |
bd0a76e2 VZ |
644 | // make all buttons of the same, maximal size - like this they cover |
645 | // the radiobox entirely and the radiobox tooltips are always shown | |
646 | // (otherwise they are not when the mouse pointer is in the radiobox | |
647 | // part not belonging to any radiobutton) | |
99c613c5 | 648 | DoMoveSibling((*m_radioButtons)[i], x_offset, y_offset, widthBtn, maxHeight); |
4438caf4 | 649 | |
3ca6a5f0 BP |
650 | // where do we put the next button? |
651 | if ( m_windowStyle & wxRA_SPECIFY_ROWS ) | |
4438caf4 | 652 | { |
3ca6a5f0 | 653 | // below this one |
4438caf4 VZ |
654 | y_offset += maxHeight; |
655 | if (m_radioWidth[0]>0) | |
656 | y_offset += cy1/2; | |
657 | } | |
658 | else | |
3ca6a5f0 BP |
659 | { |
660 | // to the right of this one | |
661 | x_offset += widthBtn + cx1; | |
662 | } | |
1f916a19 | 663 | } |
1f916a19 JS |
664 | } |
665 | ||
dc26eeb3 VZ |
666 | int wxRadioBox::GetItemFromPoint(const wxPoint& pt) const |
667 | { | |
668 | const unsigned int count = GetCount(); | |
669 | for ( unsigned int i = 0; i < count; i++ ) | |
670 | { | |
671 | RECT rect = wxGetWindowRect((*m_radioButtons)[i]); | |
672 | ||
673 | if ( rect.left <= pt.x && pt.x < rect.right && | |
674 | rect.top <= pt.y && pt.y < rect.bottom ) | |
675 | { | |
676 | return i; | |
677 | } | |
678 | } | |
679 | ||
680 | return wxNOT_FOUND; | |
681 | } | |
682 | ||
c3732409 VZ |
683 | // ---------------------------------------------------------------------------- |
684 | // radio box drawing | |
685 | // ---------------------------------------------------------------------------- | |
686 | ||
687 | #ifndef __WXWINCE__ | |
688 | ||
689 | WXHRGN wxRadioBox::MSWGetRegionWithoutChildren() | |
690 | { | |
691 | RECT rc; | |
692 | ::GetWindowRect(GetHwnd(), &rc); | |
693 | HRGN hrgn = ::CreateRectRgn(rc.left, rc.top, rc.right + 1, rc.bottom + 1); | |
694 | ||
aa61d352 VZ |
695 | const unsigned int count = GetCount(); |
696 | for ( unsigned int i = 0; i < count; ++i ) | |
c3732409 | 697 | { |
455c8f1d VZ |
698 | // don't clip out hidden children |
699 | if ( !IsItemShown(i) ) | |
700 | continue; | |
701 | ||
c3732409 VZ |
702 | ::GetWindowRect((*m_radioButtons)[i], &rc); |
703 | AutoHRGN hrgnchild(::CreateRectRgnIndirect(&rc)); | |
704 | ::CombineRgn(hrgn, hrgn, hrgnchild, RGN_DIFF); | |
705 | } | |
706 | ||
707 | return (WXHRGN)hrgn; | |
708 | } | |
709 | ||
710 | #endif // __WXWINCE__ | |
711 | ||
e373f51b VZ |
712 | // --------------------------------------------------------------------------- |
713 | // window proc for radio buttons | |
714 | // --------------------------------------------------------------------------- | |
715 | ||
716 | LRESULT APIENTRY _EXPORT wxRadioBtnWndProc(HWND hwnd, | |
8614c467 | 717 | UINT message, |
e373f51b VZ |
718 | WPARAM wParam, |
719 | LPARAM lParam) | |
720 | { | |
8614c467 | 721 | switch ( message ) |
e373f51b | 722 | { |
8614c467 VZ |
723 | case WM_GETDLGCODE: |
724 | // we must tell IsDialogMessage()/our kbd processing code that we | |
725 | // want to process arrows ourselves because neither of them is | |
726 | // smart enough to handle arrows properly for us | |
727 | { | |
8ad6ad99 | 728 | long lDlgCode = ::CallWindowProc(CASTWNDPROC s_wndprocRadioBtn, hwnd, |
8614c467 | 729 | message, wParam, lParam); |
8ad6ad99 | 730 | |
8614c467 VZ |
731 | return lDlgCode | DLGC_WANTARROWS; |
732 | } | |
e373f51b | 733 | |
8614c467 | 734 | case WM_KEYDOWN: |
9a5ccab4 | 735 | { |
975b6bcf | 736 | wxRadioBox *radiobox = (wxRadioBox *)wxGetWindowUserData(hwnd); |
f048e32f | 737 | |
8614c467 | 738 | wxCHECK_MSG( radiobox, 0, wxT("radio button without radio box?") ); |
f048e32f | 739 | |
4d08943e | 740 | bool processed = true; |
f048e32f | 741 | |
e421922f | 742 | wxDirection dir; |
8614c467 VZ |
743 | switch ( wParam ) |
744 | { | |
745 | case VK_UP: | |
1e6feb95 | 746 | dir = wxUP; |
8614c467 | 747 | break; |
f048e32f | 748 | |
8614c467 | 749 | case VK_LEFT: |
1e6feb95 | 750 | dir = wxLEFT; |
8614c467 | 751 | break; |
f048e32f | 752 | |
8614c467 | 753 | case VK_DOWN: |
1e6feb95 | 754 | dir = wxDOWN; |
8614c467 | 755 | break; |
f048e32f | 756 | |
8614c467 | 757 | case VK_RIGHT: |
1e6feb95 | 758 | dir = wxRIGHT; |
8614c467 VZ |
759 | break; |
760 | ||
761 | default: | |
4d08943e | 762 | processed = false; |
1e6feb95 VZ |
763 | |
764 | // just to suppress the compiler warning | |
765 | dir = wxALL; | |
8614c467 VZ |
766 | } |
767 | ||
768 | if ( processed ) | |
f048e32f | 769 | { |
1e6feb95 VZ |
770 | int selOld = radiobox->GetSelection(); |
771 | int selNew = radiobox->GetNextItem | |
772 | ( | |
773 | selOld, | |
774 | dir, | |
775 | radiobox->GetWindowStyle() | |
776 | ); | |
3ca6a5f0 | 777 | |
8614c467 VZ |
778 | if ( selNew != selOld ) |
779 | { | |
780 | radiobox->SetSelection(selNew); | |
974931fe | 781 | radiobox->SetFocus(); |
8614c467 VZ |
782 | |
783 | // emulate the button click | |
784 | radiobox->SendNotificationEvent(); | |
9a5ccab4 | 785 | |
8614c467 VZ |
786 | return 0; |
787 | } | |
f048e32f | 788 | } |
9a5ccab4 | 789 | } |
21687069 VZ |
790 | break; |
791 | ||
1dab049c VZ |
792 | case WM_SETFOCUS: |
793 | case WM_KILLFOCUS: | |
794 | { | |
975b6bcf | 795 | wxRadioBox *radiobox = (wxRadioBox *)wxGetWindowUserData(hwnd); |
1dab049c VZ |
796 | |
797 | wxCHECK_MSG( radiobox, 0, wxT("radio button without radio box?") ); | |
798 | ||
799 | // if we don't do this, no focus events are generated for the | |
800 | // radiobox and, besides, we need to notify the parent about | |
801 | // the focus change, otherwise the focus handling logic in | |
802 | // wxControlContainer doesn't work | |
803 | if ( message == WM_SETFOCUS ) | |
804 | radiobox->HandleSetFocus((WXHWND)wParam); | |
805 | else | |
806 | radiobox->HandleKillFocus((WXHWND)wParam); | |
807 | } | |
808 | break; | |
809 | ||
bd0a76e2 | 810 | #ifndef __WXWINCE__ |
fac93396 | 811 | case WM_HELP: |
e421922f | 812 | { |
975b6bcf | 813 | wxRadioBox *radiobox = (wxRadioBox *)wxGetWindowUserData(hwnd); |
fac93396 | 814 | |
e421922f | 815 | wxCHECK_MSG( radiobox, 0, wxT("radio button without radio box?") ); |
fac93396 | 816 | |
bd0a76e2 VZ |
817 | bool processed = false; |
818 | ||
819 | wxEvtHandler * const handler = radiobox->GetEventHandler(); | |
fac93396 | 820 | |
e421922f | 821 | HELPINFO* info = (HELPINFO*) lParam; |
bd0a76e2 | 822 | if ( info->iContextType == HELPINFO_WINDOW ) |
fac93396 | 823 | { |
bd0a76e2 VZ |
824 | for ( wxWindow* subjectOfHelp = radiobox; |
825 | subjectOfHelp; | |
826 | subjectOfHelp = subjectOfHelp->GetParent() ) | |
e421922f | 827 | { |
bd0a76e2 VZ |
828 | wxHelpEvent helpEvent(wxEVT_HELP, |
829 | subjectOfHelp->GetId(), | |
830 | wxPoint(info->MousePos.x, | |
831 | info->MousePos.y)); | |
e421922f | 832 | helpEvent.SetEventObject(radiobox); |
bd0a76e2 VZ |
833 | if ( handler->ProcessEvent(helpEvent) ) |
834 | { | |
835 | processed = true; | |
836 | break; | |
837 | } | |
e421922f | 838 | } |
fac93396 | 839 | } |
e421922f VZ |
840 | else if (info->iContextType == HELPINFO_MENUITEM) |
841 | { | |
bd0a76e2 | 842 | wxHelpEvent helpEvent(wxEVT_HELP, info->iCtrlId); |
e421922f | 843 | helpEvent.SetEventObject(radiobox); |
bd0a76e2 | 844 | processed = handler->ProcessEvent(helpEvent); |
e421922f | 845 | } |
bd0a76e2 VZ |
846 | |
847 | if ( processed ) | |
e421922f | 848 | return 0; |
e421922f | 849 | } |
c3732409 | 850 | break; |
bd0a76e2 | 851 | #endif // !__WXWINCE__ |
e373f51b VZ |
852 | } |
853 | ||
8ad6ad99 | 854 | return ::CallWindowProc(CASTWNDPROC s_wndprocRadioBtn, hwnd, message, wParam, lParam); |
e373f51b | 855 | } |
42e69d6b | 856 | |
1e6feb95 | 857 | #endif // wxUSE_RADIOBOX |