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