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