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