]> git.saurik.com Git - wxWidgets.git/blame - src/msw/radiobox.cpp
Removed redundant Init call (bug 1724495)
[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
34526434
VZ
464bool wxRadioBox::Reparent(wxWindowBase *newParent)
465{
466 if ( !wxStaticBox::Reparent(newParent) )
467 {
468 return false;
469 }
470
471 HWND hwndParent = GetHwndOf(GetParent());
472 for ( size_t item = 0; item < m_radioButtons->GetCount(); item++ )
473 {
474 ::SetParent((*m_radioButtons)[item], hwndParent);
475 }
476#ifdef __WXWINCE__
477 // put static box under the buttons in the Z-order
478 SetWindowPos(GetHwnd(), HWND_BOTTOM, 0, 0, 0, 0, SWP_NOMOVE|SWP_NOSIZE);
479#endif
480 return true;
481}
482
bd0a76e2
VZ
483WX_FORWARD_STD_METHODS_TO_SUBWINDOWS(wxRadioBox, wxStaticBox, m_radioButtons)
484
3ca6a5f0
BP
485// ----------------------------------------------------------------------------
486// size calculations
487// ----------------------------------------------------------------------------
488
489wxSize wxRadioBox::GetMaxButtonSize() const
490{
491 // calculate the max button size
492 int widthMax = 0,
493 heightMax = 0;
aa61d352
VZ
494 const unsigned int count = GetCount();
495 for ( unsigned int i = 0 ; i < count; i++ )
3ca6a5f0
BP
496 {
497 int width, height;
498 if ( m_radioWidth[i] < 0 )
499 {
bd0a76e2 500 GetTextExtent(wxGetWindowText((*m_radioButtons)[i]), &width, &height);
3ca6a5f0
BP
501
502 // adjust the size to take into account the radio box itself
503 // FIXME this is totally bogus!
504 width += RADIO_SIZE;
505 height *= 3;
506 height /= 2;
507 }
508 else
509 {
510 width = m_radioWidth[i];
511 height = m_radioHeight[i];
512 }
513
514 if ( widthMax < width )
515 widthMax = width;
516 if ( heightMax < height )
517 heightMax = height;
518 }
519
520 return wxSize(widthMax, heightMax);
521}
522
523wxSize wxRadioBox::GetTotalButtonSize(const wxSize& sizeBtn) const
524{
525 // the radiobox should be big enough for its buttons
526 int cx1, cy1;
7a5e53ab 527 wxGetCharSize(m_hWnd, &cx1, &cy1, GetFont());
3ca6a5f0
BP
528
529 int extraHeight = cy1;
530
21e0a4d5
VZ
531 int height = GetRowCount() * sizeBtn.y + cy1/2 + extraHeight;
532 int width = GetColumnCount() * (sizeBtn.x + cx1) + cx1;
3ca6a5f0 533
c7fd6717 534 // Add extra space under the label, if it exists.
1a87edf2 535 if (!wxControl::GetLabel().empty())
c7fd6717
JS
536 height += cy1/2;
537
3ca6a5f0
BP
538 // and also wide enough for its label
539 int widthLabel;
32cd189d 540 GetTextExtent(GetLabelText(), &widthLabel, NULL);
3ca6a5f0
BP
541 widthLabel += RADIO_SIZE; // FIXME this is bogus too
542 if ( widthLabel > width )
543 width = widthLabel;
544
545 return wxSize(width, height);
546}
547
548wxSize wxRadioBox::DoGetBestSize() const
549{
d96b9caf
VZ
550 if ( !m_radioButtons )
551 {
552 // if we're not fully initialized yet, we can't meaningfully compute
553 // our best size, we'll do it later
554 return wxSize(1, 1);
555 }
556
31582e4e
RD
557 wxSize best = GetTotalButtonSize(GetMaxButtonSize());
558 CacheBestSize(best);
559 return best;
3ca6a5f0
BP
560}
561
1f916a19 562// Restored old code.
bfc6fde4 563void wxRadioBox::DoSetSize(int x, int y, int width, int height, int sizeFlags)
1f916a19 564{
4438caf4
VZ
565 int currentX, currentY;
566 GetPosition(&currentX, &currentY);
c219cecc
VZ
567 int widthOld, heightOld;
568 GetSize(&widthOld, &heightOld);
569
4438caf4
VZ
570 int xx = x;
571 int yy = y;
572
4d08943e 573 if (x == wxDefaultCoord && !(sizeFlags & wxSIZE_ALLOW_MINUS_ONE))
4438caf4 574 xx = currentX;
4d08943e 575 if (y == wxDefaultCoord && !(sizeFlags & wxSIZE_ALLOW_MINUS_ONE))
4438caf4
VZ
576 yy = currentY;
577
99c613c5
JG
578 int y_offset = yy;
579 int x_offset = xx;
f048e32f 580
3ca6a5f0 581 int cx1, cy1;
7a5e53ab 582 wxGetCharSize(m_hWnd, &cx1, &cy1, GetFont());
4438caf4
VZ
583
584 // Attempt to have a look coherent with other platforms: We compute the
585 // biggest toggle dim, then we align all items according this value.
3ca6a5f0
BP
586 wxSize maxSize = GetMaxButtonSize();
587 int maxWidth = maxSize.x,
588 maxHeight = maxSize.y;
4438caf4 589
3ca6a5f0
BP
590 wxSize totSize = GetTotalButtonSize(maxSize);
591 int totWidth = totSize.x,
592 totHeight = totSize.y;
593
594 // only change our width/height if asked for
4d08943e 595 if ( width == wxDefaultCoord )
1f916a19 596 {
3ca6a5f0
BP
597 if ( sizeFlags & wxSIZE_AUTO_WIDTH )
598 width = totWidth;
4438caf4 599 else
3ca6a5f0 600 width = widthOld;
1f916a19 601 }
1f916a19 602
4d08943e 603 if ( height == wxDefaultCoord )
4438caf4 604 {
3ca6a5f0
BP
605 if ( sizeFlags & wxSIZE_AUTO_HEIGHT )
606 height = totHeight;
4438caf4 607 else
3ca6a5f0
BP
608 height = heightOld;
609 }
4438caf4 610
7d86a2d4 611 DoMoveWindow(xx, yy, width, height);
c219cecc 612
3ca6a5f0
BP
613 // Now position all the buttons: the current button will be put at
614 // wxPoint(x_offset, y_offset) and the new row/column will start at
615 // startX/startY. The size of all buttons will be the same wxSize(maxWidth,
616 // maxHeight) except for the buttons in the last column which should extend
617 // to the right border of radiobox and thus can be wider than this.
c219cecc 618
3ca6a5f0 619 // Also, remember that wxRA_SPECIFY_COLS means that we arrange buttons in
21e0a4d5 620 // left to right order and GetMajorDim() is the number of columns while
3ca6a5f0 621 // wxRA_SPECIFY_ROWS means that the buttons are arranged top to bottom and
21e0a4d5 622 // GetMajorDim() is the number of rows.
4438caf4 623
3ca6a5f0
BP
624 x_offset += cx1;
625 y_offset += cy1;
1f916a19 626
c7fd6717 627 // Add extra space under the label, if it exists.
1a87edf2 628 if (!wxControl::GetLabel().empty())
c7fd6717 629 y_offset += cy1/2;
3ca6a5f0 630
4438caf4
VZ
631 int startX = x_offset;
632 int startY = y_offset;
1f916a19 633
aa61d352
VZ
634 const unsigned int count = GetCount();
635 for (unsigned int i = 0; i < count; i++)
1f916a19 636 {
3ca6a5f0
BP
637 // the last button in the row may be wider than the other ones as the
638 // radiobox may be wider than the sum of the button widths (as it
639 // happens, for example, when the radiobox label is very long)
640 bool isLastInTheRow;
641 if ( m_windowStyle & wxRA_SPECIFY_COLS )
642 {
643 // item is the last in its row if it is a multiple of the number of
644 // columns or if it is just the last item
aa61d352 645 unsigned int n = i + 1;
21e0a4d5 646 isLastInTheRow = ((n % GetMajorDim()) == 0) || (n == count);
3ca6a5f0
BP
647 }
648 else // wxRA_SPECIFY_ROWS
4438caf4 649 {
3ca6a5f0 650 // item is the last in the row if it is in the last columns
21e0a4d5 651 isLastInTheRow = i >= (count/GetMajorDim())*GetMajorDim();
3ca6a5f0
BP
652 }
653
654 // is this the start of new row/column?
21e0a4d5 655 if ( i && (i % GetMajorDim() == 0) )
3ca6a5f0
BP
656 {
657 if ( m_windowStyle & wxRA_SPECIFY_ROWS )
4438caf4 658 {
3ca6a5f0 659 // start of new column
4438caf4
VZ
660 y_offset = startY;
661 x_offset += maxWidth + cx1;
662 }
3ca6a5f0 663 else // start of new row
4438caf4
VZ
664 {
665 x_offset = startX;
666 y_offset += maxHeight;
667 if (m_radioWidth[0]>0)
668 y_offset += cy1/2;
669 }
670 }
3ca6a5f0
BP
671
672 int widthBtn;
673 if ( isLastInTheRow )
4438caf4 674 {
3ca6a5f0
BP
675 // make the button go to the end of radio box
676 widthBtn = startX + width - x_offset - 2*cx1;
677 if ( widthBtn < maxWidth )
678 widthBtn = maxWidth;
4438caf4
VZ
679 }
680 else
681 {
3ca6a5f0
BP
682 // normal button, always of the same size
683 widthBtn = maxWidth;
4438caf4 684 }
1f916a19 685
bd0a76e2
VZ
686 // make all buttons of the same, maximal size - like this they cover
687 // the radiobox entirely and the radiobox tooltips are always shown
688 // (otherwise they are not when the mouse pointer is in the radiobox
689 // part not belonging to any radiobutton)
99c613c5 690 DoMoveSibling((*m_radioButtons)[i], x_offset, y_offset, widthBtn, maxHeight);
4438caf4 691
3ca6a5f0
BP
692 // where do we put the next button?
693 if ( m_windowStyle & wxRA_SPECIFY_ROWS )
4438caf4 694 {
3ca6a5f0 695 // below this one
4438caf4
VZ
696 y_offset += maxHeight;
697 if (m_radioWidth[0]>0)
698 y_offset += cy1/2;
699 }
700 else
3ca6a5f0
BP
701 {
702 // to the right of this one
703 x_offset += widthBtn + cx1;
704 }
1f916a19 705 }
1f916a19
JS
706}
707
dc26eeb3
VZ
708int wxRadioBox::GetItemFromPoint(const wxPoint& pt) const
709{
710 const unsigned int count = GetCount();
711 for ( unsigned int i = 0; i < count; i++ )
712 {
713 RECT rect = wxGetWindowRect((*m_radioButtons)[i]);
714
715 if ( rect.left <= pt.x && pt.x < rect.right &&
716 rect.top <= pt.y && pt.y < rect.bottom )
717 {
718 return i;
719 }
720 }
721
722 return wxNOT_FOUND;
723}
724
c3732409
VZ
725// ----------------------------------------------------------------------------
726// radio box drawing
727// ----------------------------------------------------------------------------
728
729#ifndef __WXWINCE__
730
731WXHRGN wxRadioBox::MSWGetRegionWithoutChildren()
732{
733 RECT rc;
734 ::GetWindowRect(GetHwnd(), &rc);
735 HRGN hrgn = ::CreateRectRgn(rc.left, rc.top, rc.right + 1, rc.bottom + 1);
736
aa61d352
VZ
737 const unsigned int count = GetCount();
738 for ( unsigned int i = 0; i < count; ++i )
c3732409 739 {
455c8f1d
VZ
740 // don't clip out hidden children
741 if ( !IsItemShown(i) )
742 continue;
743
c3732409
VZ
744 ::GetWindowRect((*m_radioButtons)[i], &rc);
745 AutoHRGN hrgnchild(::CreateRectRgnIndirect(&rc));
746 ::CombineRgn(hrgn, hrgn, hrgnchild, RGN_DIFF);
747 }
748
749 return (WXHRGN)hrgn;
750}
751
752#endif // __WXWINCE__
753
e373f51b
VZ
754// ---------------------------------------------------------------------------
755// window proc for radio buttons
756// ---------------------------------------------------------------------------
757
758LRESULT APIENTRY _EXPORT wxRadioBtnWndProc(HWND hwnd,
8614c467 759 UINT message,
e373f51b
VZ
760 WPARAM wParam,
761 LPARAM lParam)
762{
8614c467 763 switch ( message )
e373f51b 764 {
8614c467
VZ
765 case WM_GETDLGCODE:
766 // we must tell IsDialogMessage()/our kbd processing code that we
767 // want to process arrows ourselves because neither of them is
768 // smart enough to handle arrows properly for us
769 {
8ad6ad99 770 long lDlgCode = ::CallWindowProc(CASTWNDPROC s_wndprocRadioBtn, hwnd,
8614c467 771 message, wParam, lParam);
8ad6ad99 772
8614c467
VZ
773 return lDlgCode | DLGC_WANTARROWS;
774 }
e373f51b 775
8614c467 776 case WM_KEYDOWN:
9a5ccab4 777 {
975b6bcf 778 wxRadioBox *radiobox = (wxRadioBox *)wxGetWindowUserData(hwnd);
f048e32f 779
8614c467 780 wxCHECK_MSG( radiobox, 0, wxT("radio button without radio box?") );
f048e32f 781
4d08943e 782 bool processed = true;
f048e32f 783
e421922f 784 wxDirection dir;
8614c467
VZ
785 switch ( wParam )
786 {
787 case VK_UP:
1e6feb95 788 dir = wxUP;
8614c467 789 break;
f048e32f 790
8614c467 791 case VK_LEFT:
1e6feb95 792 dir = wxLEFT;
8614c467 793 break;
f048e32f 794
8614c467 795 case VK_DOWN:
1e6feb95 796 dir = wxDOWN;
8614c467 797 break;
f048e32f 798
8614c467 799 case VK_RIGHT:
1e6feb95 800 dir = wxRIGHT;
8614c467
VZ
801 break;
802
803 default:
4d08943e 804 processed = false;
1e6feb95
VZ
805
806 // just to suppress the compiler warning
807 dir = wxALL;
8614c467
VZ
808 }
809
810 if ( processed )
f048e32f 811 {
1e6feb95
VZ
812 int selOld = radiobox->GetSelection();
813 int selNew = radiobox->GetNextItem
814 (
815 selOld,
816 dir,
817 radiobox->GetWindowStyle()
818 );
3ca6a5f0 819
8614c467
VZ
820 if ( selNew != selOld )
821 {
822 radiobox->SetSelection(selNew);
974931fe 823 radiobox->SetFocus();
8614c467
VZ
824
825 // emulate the button click
826 radiobox->SendNotificationEvent();
9a5ccab4 827
8614c467
VZ
828 return 0;
829 }
f048e32f 830 }
9a5ccab4 831 }
21687069
VZ
832 break;
833
1dab049c
VZ
834 case WM_SETFOCUS:
835 case WM_KILLFOCUS:
836 {
975b6bcf 837 wxRadioBox *radiobox = (wxRadioBox *)wxGetWindowUserData(hwnd);
1dab049c
VZ
838
839 wxCHECK_MSG( radiobox, 0, wxT("radio button without radio box?") );
840
841 // if we don't do this, no focus events are generated for the
842 // radiobox and, besides, we need to notify the parent about
843 // the focus change, otherwise the focus handling logic in
844 // wxControlContainer doesn't work
845 if ( message == WM_SETFOCUS )
846 radiobox->HandleSetFocus((WXHWND)wParam);
847 else
848 radiobox->HandleKillFocus((WXHWND)wParam);
849 }
850 break;
851
bd0a76e2 852#ifndef __WXWINCE__
fac93396 853 case WM_HELP:
e421922f 854 {
975b6bcf 855 wxRadioBox *radiobox = (wxRadioBox *)wxGetWindowUserData(hwnd);
fac93396 856
e421922f 857 wxCHECK_MSG( radiobox, 0, wxT("radio button without radio box?") );
fac93396 858
bd0a76e2
VZ
859 bool processed = false;
860
861 wxEvtHandler * const handler = radiobox->GetEventHandler();
fac93396 862
e421922f 863 HELPINFO* info = (HELPINFO*) lParam;
bd0a76e2 864 if ( info->iContextType == HELPINFO_WINDOW )
fac93396 865 {
bd0a76e2
VZ
866 for ( wxWindow* subjectOfHelp = radiobox;
867 subjectOfHelp;
868 subjectOfHelp = subjectOfHelp->GetParent() )
e421922f 869 {
bd0a76e2
VZ
870 wxHelpEvent helpEvent(wxEVT_HELP,
871 subjectOfHelp->GetId(),
872 wxPoint(info->MousePos.x,
873 info->MousePos.y));
e421922f 874 helpEvent.SetEventObject(radiobox);
bd0a76e2
VZ
875 if ( handler->ProcessEvent(helpEvent) )
876 {
877 processed = true;
878 break;
879 }
e421922f 880 }
fac93396 881 }
e421922f
VZ
882 else if (info->iContextType == HELPINFO_MENUITEM)
883 {
bd0a76e2 884 wxHelpEvent helpEvent(wxEVT_HELP, info->iCtrlId);
e421922f 885 helpEvent.SetEventObject(radiobox);
bd0a76e2 886 processed = handler->ProcessEvent(helpEvent);
e421922f 887 }
bd0a76e2
VZ
888
889 if ( processed )
e421922f 890 return 0;
e421922f 891 }
c3732409 892 break;
bd0a76e2 893#endif // !__WXWINCE__
e373f51b
VZ
894 }
895
8ad6ad99 896 return ::CallWindowProc(CASTWNDPROC s_wndprocRadioBtn, hwnd, message, wParam, lParam);
e373f51b 897}
42e69d6b 898
1e6feb95 899#endif // wxUSE_RADIOBOX