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