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