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