]> git.saurik.com Git - wxWidgets.git/blame - src/msw/radiobox.cpp
use a linked list instead of array for saved messages to fix problems when Dispatch...
[wxWidgets.git] / src / msw / radiobox.cpp
CommitLineData
2bda0e17 1/////////////////////////////////////////////////////////////////////////////
3ca6a5f0
BP
2// Name: msw/radiobox.cpp
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
14f355c2 20#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
da07e033 21 #pragma implementation "radiobox.h"
2bda0e17
KB
22#endif
23
24// For compilers that support precompilation, includes "wx.h".
25#include "wx/wxprec.h"
26
27#ifdef __BORLANDC__
da07e033 28 #pragma hdrstop
2bda0e17
KB
29#endif
30
1e6feb95
VZ
31#if wxUSE_RADIOBOX
32
2bda0e17 33#ifndef WX_PRECOMP
da07e033
VZ
34 #include "wx/bitmap.h"
35 #include "wx/brush.h"
36 #include "wx/radiobox.h"
f6bcfd97 37 #include "wx/settings.h"
381dd4bf 38 #include "wx/log.h"
2bda0e17
KB
39#endif
40
41#include "wx/msw/private.h"
42
f048e32f 43#if wxUSE_TOOLTIPS
ae090fdb 44 #if !defined(__GNUWIN32_OLD__) || defined(__CYGWIN10__)
8614c467 45 #include <commctrl.h>
5ace0213 46 #endif
f048e32f
VZ
47 #include "wx/tooltip.h"
48#endif // wxUSE_TOOLTIPS
49
885b88a2
JS
50#if defined(__WXWINCE__) && !defined(WS_EX_TRANSPARENT)
51#define WS_EX_TRANSPARENT 0
52#endif
53
3ff066a4 54// TODO: wxCONSTRUCTOR
bc9fb572
JS
55#if 0 // wxUSE_EXTENDED_RTTI
56WX_DEFINE_FLAGS( wxRadioBoxStyle )
57
3ff066a4 58wxBEGIN_FLAGS( wxRadioBoxStyle )
bc9fb572
JS
59 // new style border flags, we put them first to
60 // use them for streaming out
3ff066a4
SC
61 wxFLAGS_MEMBER(wxBORDER_SIMPLE)
62 wxFLAGS_MEMBER(wxBORDER_SUNKEN)
63 wxFLAGS_MEMBER(wxBORDER_DOUBLE)
64 wxFLAGS_MEMBER(wxBORDER_RAISED)
65 wxFLAGS_MEMBER(wxBORDER_STATIC)
66 wxFLAGS_MEMBER(wxBORDER_NONE)
4d08943e 67
bc9fb572 68 // old style border flags
3ff066a4
SC
69 wxFLAGS_MEMBER(wxSIMPLE_BORDER)
70 wxFLAGS_MEMBER(wxSUNKEN_BORDER)
71 wxFLAGS_MEMBER(wxDOUBLE_BORDER)
72 wxFLAGS_MEMBER(wxRAISED_BORDER)
73 wxFLAGS_MEMBER(wxSTATIC_BORDER)
cb0afb26 74 wxFLAGS_MEMBER(wxBORDER)
bc9fb572
JS
75
76 // standard window styles
3ff066a4
SC
77 wxFLAGS_MEMBER(wxTAB_TRAVERSAL)
78 wxFLAGS_MEMBER(wxCLIP_CHILDREN)
79 wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW)
80 wxFLAGS_MEMBER(wxWANTS_CHARS)
cb0afb26 81 wxFLAGS_MEMBER(wxFULL_REPAINT_ON_RESIZE)
3ff066a4
SC
82 wxFLAGS_MEMBER(wxALWAYS_SHOW_SB )
83 wxFLAGS_MEMBER(wxVSCROLL)
84 wxFLAGS_MEMBER(wxHSCROLL)
85
86 wxFLAGS_MEMBER(wxRA_SPECIFY_COLS)
87 wxFLAGS_MEMBER(wxRA_HORIZONTAL)
88 wxFLAGS_MEMBER(wxRA_SPECIFY_ROWS)
89 wxFLAGS_MEMBER(wxRA_VERTICAL)
90
91wxEND_FLAGS( wxRadioBoxStyle )
bc9fb572
JS
92
93IMPLEMENT_DYNAMIC_CLASS_XTI(wxRadioBox, wxControl,"wx/radiobox.h")
94
3ff066a4
SC
95wxBEGIN_PROPERTIES_TABLE(wxRadioBox)
96 wxEVENT_PROPERTY( Select , wxEVT_COMMAND_RADIOBOX_SELECTED , wxCommandEvent )
97 wxPROPERTY_FLAGS( WindowStyle , wxRadioBoxStyle , long , SetWindowStyleFlag , GetWindowStyleFlag , , 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style
98wxEND_PROPERTIES_TABLE()
bc9fb572
JS
99
100#else
d3acd369 101IMPLEMENT_DYNAMIC_CLASS(wxRadioBox, wxControl)
bc9fb572 102#endif
2bda0e17 103
066f1b7a 104/*
4d08943e
WS
105 selection
106 content
107 label
108 dimension
109 item
066f1b7a
SC
110*/
111
8614c467
VZ
112// there are two possible ways to create the radio buttons: either as children
113// of the radiobox or as siblings of it - allow playing with both variants for
114// now, eventually we will choose the best one for our purposes
f048e32f 115//
8614c467
VZ
116// two main problems are the keyboard navigation inside the radiobox (arrows
117// should switch between buttons, not pass focus to the next control) and the
118// tooltips - a tooltip is associated with the radiobox itself, not the
119// children...
120//
121// the problems with setting this to 1:
122// a) Alt-<mnemonic of radiobox> isn't handled properly by IsDialogMessage()
123// because it sets focus to the next control accepting it which is not a
124// radio button but a radiobox sibling in this case - the only solution to
125// this would be to handle Alt-<mnemonic> ourselves
126// b) the problems with setting radiobox colours under Win98/2K were reported
127// but I couldn't reproduce it so I have no idea about what causes it
128//
129// the problems with setting this to 0:
130// a) the tooltips are not shown for the radiobox - possible solution: make
131// TTM_WINDOWFROMPOS handling code in msw/tooltip.cpp work (easier said than
132// done because I don't know why it doesn't work)
d1e418ea 133#define RADIOBTN_PARENT_IS_RADIOBOX 0
f048e32f 134
e373f51b
VZ
135// ---------------------------------------------------------------------------
136// private functions
137// ---------------------------------------------------------------------------
138
e373f51b 139// wnd proc for radio buttons
2a47d3c1 140#ifdef __WIN32__
e373f51b
VZ
141LRESULT APIENTRY _EXPORT wxRadioBtnWndProc(HWND hWnd,
142 UINT message,
143 WPARAM wParam,
144 LPARAM lParam);
145
146// ---------------------------------------------------------------------------
147// global vars
148// ---------------------------------------------------------------------------
149
150// the pointer to standard radio button wnd proc
457e6c54 151static WXFARPROC s_wndprocRadioBtn = (WXFARPROC)NULL;
e373f51b 152
42e69d6b
VZ
153#endif // __WIN32__
154
e373f51b
VZ
155// ===========================================================================
156// implementation
157// ===========================================================================
158
159// ---------------------------------------------------------------------------
160// wxRadioBox
161// ---------------------------------------------------------------------------
162
1e6feb95
VZ
163int wxRadioBox::GetCount() const
164{
165 return m_noItems;
166}
167
168int wxRadioBox::GetColumnCount() const
169{
170 return GetNumHor();
171}
172
173int wxRadioBox::GetRowCount() const
174{
175 return GetNumVer();
176}
177
3ca6a5f0 178// returns the number of rows
e373f51b
VZ
179int wxRadioBox::GetNumVer() const
180{
181 if ( m_windowStyle & wxRA_SPECIFY_ROWS )
182 {
183 return m_majorDim;
184 }
185 else
186 {
187 return (m_noItems + m_majorDim - 1)/m_majorDim;
188 }
189}
190
3ca6a5f0 191// returns the number of columns
e373f51b
VZ
192int wxRadioBox::GetNumHor() const
193{
194 if ( m_windowStyle & wxRA_SPECIFY_ROWS )
195 {
196 return (m_noItems + m_majorDim - 1)/m_majorDim;
197 }
198 else
199 {
200 return m_majorDim;
201 }
202}
203
42e69d6b 204bool wxRadioBox::MSWCommand(WXUINT cmd, WXWORD id)
2bda0e17 205{
42e69d6b 206 if ( cmd == BN_CLICKED )
da07e033 207 {
f6bcfd97 208 if (id == GetId())
4d08943e 209 return true;
f6bcfd97 210
4d08943e 211 int selectedButton = wxID_ANY;
e373f51b
VZ
212
213 for ( int i = 0; i < m_noItems; i++ )
214 {
cc2b7472 215 if ( id == wxGetWindowId(m_radioButtons[i]) )
e373f51b 216 {
9a5ccab4 217 selectedButton = i;
e373f51b
VZ
218
219 break;
220 }
221 }
222
4d08943e 223 if ( selectedButton == wxID_ANY )
3ca6a5f0
BP
224 {
225 // just ignore it - due to a hack with WM_NCHITTEST handling in our
226 // wnd proc, we can receive dummy click messages when we click near
227 // the radiobox edge (this is ugly but Julian wouldn't let me get
228 // rid of this...)
4d08943e 229 return false;
3ca6a5f0 230 }
2bda0e17 231
9a5ccab4
VZ
232 if ( selectedButton != m_selectedButton )
233 {
234 m_selectedButton = selectedButton;
235
236 SendNotificationEvent();
237 }
238 //else: don't generate events when the selection doesn't change
e373f51b 239
4d08943e 240 return true;
da07e033 241 }
e373f51b 242 else
4d08943e 243 return false;
2bda0e17
KB
244}
245
2bda0e17 246// Radio box item
e373f51b 247wxRadioBox::wxRadioBox()
2bda0e17 248{
4d08943e 249 m_selectedButton = wxID_ANY;
da07e033
VZ
250 m_noItems = 0;
251 m_noRowsOrCols = 0;
252 m_radioButtons = NULL;
e373f51b
VZ
253 m_majorDim = 0;
254 m_radioWidth = NULL;
255 m_radioHeight = NULL;
2bda0e17
KB
256}
257
f048e32f
VZ
258bool wxRadioBox::Create(wxWindow *parent,
259 wxWindowID id,
260 const wxString& title,
261 const wxPoint& pos,
262 const wxSize& size,
263 int n,
264 const wxString choices[],
265 int majorDim,
266 long style,
267 const wxValidator& val,
268 const wxString& name)
2bda0e17 269{
f048e32f 270 // initialize members
4d08943e 271 m_selectedButton = wxID_ANY;
4afd7529 272 m_noItems = 0;
2bda0e17 273
3ca6a5f0 274 m_majorDim = majorDim == 0 ? n : majorDim;
da07e033 275 m_noRowsOrCols = majorDim;
da07e033 276
f048e32f
VZ
277 // common initialization
278 if ( !CreateControl(parent, id, pos, size, style, val, name) )
4d08943e 279 return false;
2bda0e17 280
f048e32f 281 // create the static box
d9317fd4 282 if ( !MSWCreateControl(wxT("BUTTON"), BS_GROUPBOX | WS_GROUP,
65546961 283 pos, size, title, WS_EX_TRANSPARENT) )
4d08943e 284 return false;
da07e033 285
f048e32f 286 // and now create the buttons
4afd7529 287 m_noItems = n;
f048e32f
VZ
288#if RADIOBTN_PARENT_IS_RADIOBOX
289 HWND hwndParent = GetHwnd();
290#else
291 HWND hwndParent = GetHwndOf(parent);
292#endif
da07e033
VZ
293
294 // Some radio boxes test consecutive id.
e373f51b 295 (void)NewControlId();
da07e033 296 m_radioButtons = new WXHWND[n];
e373f51b
VZ
297 m_radioWidth = new int[n];
298 m_radioHeight = new int[n];
f048e32f
VZ
299
300 WXHFONT hfont = 0;
daa38c00 301 wxFont font = GetFont();
f048e32f 302 if ( font.Ok() )
da07e033 303 {
f048e32f
VZ
304 hfont = font.GetResourceHandle();
305 }
306
307 for ( int i = 0; i < n; i++ )
308 {
309 m_radioWidth[i] =
4d08943e 310 m_radioHeight[i] = wxDefaultCoord;
d3acd369 311 long styleBtn = BS_AUTORADIOBUTTON | WS_TABSTOP | WS_CHILD | WS_VISIBLE;
e373f51b 312 if ( i == 0 && style == 0 )
f048e32f
VZ
313 styleBtn |= WS_GROUP;
314
da07e033 315 long newId = NewControlId();
da07e033 316
f048e32f
VZ
317 HWND hwndBtn = ::CreateWindow(_T("BUTTON"),
318 choices[i],
319 styleBtn,
320 0, 0, 0, 0, // will be set in SetSize()
e373f51b 321 hwndParent,
f048e32f
VZ
322 (HMENU)newId,
323 wxGetInstance(),
e373f51b 324 NULL);
2bda0e17 325
f048e32f
VZ
326 if ( !hwndBtn )
327 {
f6bcfd97 328 wxLogLastError(wxT("CreateWindow(radio btn)"));
f048e32f 329
4d08943e 330 return false;
f048e32f
VZ
331 }
332
e373f51b 333 m_radioButtons[i] = (WXHWND)hwndBtn;
42e69d6b 334
e373f51b 335 SubclassRadioButton((WXHWND)hwndBtn);
2bda0e17 336
f048e32f 337 if ( hfont )
da07e033 338 {
f048e32f 339 ::SendMessage(hwndBtn, WM_SETFONT, (WPARAM)hfont, 0L);
da07e033 340 }
e373f51b 341
f048e32f 342 m_subControls.Add(newId);
da07e033 343 }
e373f51b 344
da07e033 345 // Create a dummy radio control to end the group.
f048e32f 346 (void)::CreateWindow(_T("BUTTON"),
fda7962d 347 wxEmptyString,
f048e32f 348 WS_GROUP | BS_AUTORADIOBUTTON | WS_CHILD,
e373f51b
VZ
349 0, 0, 0, 0, hwndParent,
350 (HMENU)NewControlId(), wxGetInstance(), NULL);
2bda0e17 351
64133c38
JS
352#ifdef __WXWINCE__
353 // Set the z-order correctly
d26e1ab2 354 SetWindowPos(GetHwnd(), HWND_BOTTOM, 0, 0, 0, 0, SWP_NOMOVE|SWP_NOSIZE);
64133c38
JS
355#endif
356
da07e033 357 SetSelection(0);
f048e32f 358 SetSize(pos.x, pos.y, size.x, size.y);
2bda0e17 359
7a5a5718
RD
360 // Now that we have items determine what is the best size and set it.
361 SetBestSize(size);
4d08943e
WS
362
363 return true;
2bda0e17 364}
2bda0e17 365
584ad2a3
MB
366bool wxRadioBox::Create(wxWindow *parent,
367 wxWindowID id,
368 const wxString& title,
369 const wxPoint& pos,
370 const wxSize& size,
371 const wxArrayString& choices,
372 int majorDim,
373 long style,
374 const wxValidator& val,
375 const wxString& name)
376{
377 wxCArrayString chs(choices);
378 return Create(parent, id, title, pos, size, chs.GetCount(),
379 chs.GetStrings(), majorDim, style, val, name);
380}
381
e373f51b 382wxRadioBox::~wxRadioBox()
2bda0e17 383{
4d08943e 384 m_isBeingDeleted = true;
1eb20d4a 385
da07e033
VZ
386 if (m_radioButtons)
387 {
388 int i;
389 for (i = 0; i < m_noItems; i++)
42e69d6b 390 ::DestroyWindow((HWND)m_radioButtons[i]);
da07e033
VZ
391 delete[] m_radioButtons;
392 }
42e69d6b 393
da07e033 394 if (m_radioWidth)
e373f51b 395 delete[] m_radioWidth;
da07e033 396 if (m_radioHeight)
e373f51b 397 delete[] m_radioHeight;
2bda0e17
KB
398
399}
400
1e6feb95 401void wxRadioBox::SetString(int item, const wxString& label)
2bda0e17 402{
223d09f6 403 wxCHECK_RET( item >= 0 && item < m_noItems, wxT("invalid radiobox index") );
d66a042c 404
4d08943e 405 m_radioWidth[item] = m_radioHeight[item] = wxDefaultCoord;
42e69d6b 406 SetWindowText((HWND)m_radioButtons[item], label.c_str());
2bda0e17
KB
407}
408
debe6624 409void wxRadioBox::SetSelection(int N)
2bda0e17 410{
223d09f6 411 wxCHECK_RET( (N >= 0) && (N < m_noItems), wxT("invalid radiobox index") );
2bda0e17 412
da07e033
VZ
413 // Following necessary for Win32s, because Win32s translate BM_SETCHECK
414 if (m_selectedButton >= 0 && m_selectedButton < m_noItems)
e373f51b
VZ
415 ::SendMessage((HWND) m_radioButtons[m_selectedButton], BM_SETCHECK, 0, 0L);
416
417 ::SendMessage((HWND)m_radioButtons[N], BM_SETCHECK, 1, 0L);
2bda0e17 418
da07e033 419 m_selectedButton = N;
2bda0e17
KB
420}
421
422// Get single selection, for single choice list items
e373f51b 423int wxRadioBox::GetSelection() const
2bda0e17 424{
da07e033 425 return m_selectedButton;
2bda0e17
KB
426}
427
428// Find string for position
1e6feb95 429wxString wxRadioBox::GetString(int item) const
2bda0e17 430{
1e6feb95
VZ
431 wxCHECK_MSG( item >= 0 && item < m_noItems, wxEmptyString,
432 wxT("invalid radiobox index") );
433
434 return wxGetWindowText(m_radioButtons[item]);
2bda0e17
KB
435}
436
3ca6a5f0
BP
437// ----------------------------------------------------------------------------
438// size calculations
439// ----------------------------------------------------------------------------
440
441wxSize wxRadioBox::GetMaxButtonSize() const
442{
443 // calculate the max button size
444 int widthMax = 0,
445 heightMax = 0;
446 for ( int i = 0 ; i < m_noItems; i++ )
447 {
448 int width, height;
449 if ( m_radioWidth[i] < 0 )
450 {
451 GetTextExtent(wxGetWindowText(m_radioButtons[i]), &width, &height);
452
453 // adjust the size to take into account the radio box itself
454 // FIXME this is totally bogus!
455 width += RADIO_SIZE;
456 height *= 3;
457 height /= 2;
458 }
459 else
460 {
461 width = m_radioWidth[i];
462 height = m_radioHeight[i];
463 }
464
465 if ( widthMax < width )
466 widthMax = width;
467 if ( heightMax < height )
468 heightMax = height;
469 }
470
471 return wxSize(widthMax, heightMax);
472}
473
474wxSize wxRadioBox::GetTotalButtonSize(const wxSize& sizeBtn) const
475{
476 // the radiobox should be big enough for its buttons
477 int cx1, cy1;
7a5e53ab 478 wxGetCharSize(m_hWnd, &cx1, &cy1, GetFont());
3ca6a5f0
BP
479
480 int extraHeight = cy1;
481
c7fd6717 482 /* We'll assume the adjustments below are OK for Win 3.1 too
3ca6a5f0
BP
483#if defined(CTL3D) && !CTL3D
484 // Requires a bigger group box in plain Windows
485 extraHeight *= 3;
486 extraHeight /= 2;
487#endif
c7fd6717 488 */
4d08943e 489
3ca6a5f0
BP
490 int height = GetNumVer() * sizeBtn.y + cy1/2 + extraHeight;
491 int width = GetNumHor() * (sizeBtn.x + cx1) + cx1;
492
c7fd6717
JS
493 // Add extra space under the label, if it exists.
494 if (!wxControl::GetLabel().IsEmpty())
495 height += cy1/2;
496
3ca6a5f0
BP
497 // and also wide enough for its label
498 int widthLabel;
499 GetTextExtent(GetTitle(), &widthLabel, NULL);
500 widthLabel += RADIO_SIZE; // FIXME this is bogus too
501 if ( widthLabel > width )
502 width = widthLabel;
503
504 return wxSize(width, height);
505}
506
507wxSize wxRadioBox::DoGetBestSize() const
508{
509 return GetTotalButtonSize(GetMaxButtonSize());
510}
511
1f916a19 512// Restored old code.
bfc6fde4 513void wxRadioBox::DoSetSize(int x, int y, int width, int height, int sizeFlags)
1f916a19 514{
4438caf4
VZ
515 int currentX, currentY;
516 GetPosition(&currentX, &currentY);
c219cecc
VZ
517 int widthOld, heightOld;
518 GetSize(&widthOld, &heightOld);
519
4438caf4
VZ
520 int xx = x;
521 int yy = y;
522
4d08943e 523 if (x == wxDefaultCoord && !(sizeFlags & wxSIZE_ALLOW_MINUS_ONE))
4438caf4 524 xx = currentX;
4d08943e 525 if (y == wxDefaultCoord && !(sizeFlags & wxSIZE_ALLOW_MINUS_ONE))
4438caf4
VZ
526 yy = currentY;
527
f048e32f
VZ
528#if RADIOBTN_PARENT_IS_RADIOBOX
529 int y_offset = 0;
530 int x_offset = 0;
531#else
4438caf4
VZ
532 int y_offset = yy;
533 int x_offset = xx;
f048e32f
VZ
534#endif
535
3ca6a5f0 536 int cx1, cy1;
7a5e53ab 537 wxGetCharSize(m_hWnd, &cx1, &cy1, GetFont());
4438caf4
VZ
538
539 // Attempt to have a look coherent with other platforms: We compute the
540 // biggest toggle dim, then we align all items according this value.
3ca6a5f0
BP
541 wxSize maxSize = GetMaxButtonSize();
542 int maxWidth = maxSize.x,
543 maxHeight = maxSize.y;
4438caf4 544
3ca6a5f0
BP
545 wxSize totSize = GetTotalButtonSize(maxSize);
546 int totWidth = totSize.x,
547 totHeight = totSize.y;
548
549 // only change our width/height if asked for
4d08943e 550 if ( width == wxDefaultCoord )
1f916a19 551 {
3ca6a5f0
BP
552 if ( sizeFlags & wxSIZE_AUTO_WIDTH )
553 width = totWidth;
4438caf4 554 else
3ca6a5f0 555 width = widthOld;
1f916a19 556 }
1f916a19 557
4d08943e 558 if ( height == wxDefaultCoord )
4438caf4 559 {
3ca6a5f0
BP
560 if ( sizeFlags & wxSIZE_AUTO_HEIGHT )
561 height = totHeight;
4438caf4 562 else
3ca6a5f0
BP
563 height = heightOld;
564 }
4438caf4 565
3ca6a5f0 566 ::MoveWindow(GetHwnd(), xx, yy, width, height, TRUE);
c219cecc 567
3ca6a5f0
BP
568 // Now position all the buttons: the current button will be put at
569 // wxPoint(x_offset, y_offset) and the new row/column will start at
570 // startX/startY. The size of all buttons will be the same wxSize(maxWidth,
571 // maxHeight) except for the buttons in the last column which should extend
572 // to the right border of radiobox and thus can be wider than this.
c219cecc 573
3ca6a5f0
BP
574 // Also, remember that wxRA_SPECIFY_COLS means that we arrange buttons in
575 // left to right order and m_majorDim is the number of columns while
576 // wxRA_SPECIFY_ROWS means that the buttons are arranged top to bottom and
577 // m_majorDim is the number of rows.
4438caf4 578
3ca6a5f0
BP
579 x_offset += cx1;
580 y_offset += cy1;
1f916a19 581
c7fd6717
JS
582 // Add extra space under the label, if it exists.
583 if (!wxControl::GetLabel().IsEmpty())
584 y_offset += cy1/2;
3ca6a5f0 585
4438caf4
VZ
586 int startX = x_offset;
587 int startY = y_offset;
1f916a19 588
3ca6a5f0 589 for ( int i = 0; i < m_noItems; i++ )
1f916a19 590 {
3ca6a5f0
BP
591 // the last button in the row may be wider than the other ones as the
592 // radiobox may be wider than the sum of the button widths (as it
593 // happens, for example, when the radiobox label is very long)
594 bool isLastInTheRow;
595 if ( m_windowStyle & wxRA_SPECIFY_COLS )
596 {
597 // item is the last in its row if it is a multiple of the number of
598 // columns or if it is just the last item
599 int n = i + 1;
600 isLastInTheRow = ((n % m_majorDim) == 0) || (n == m_noItems);
601 }
602 else // wxRA_SPECIFY_ROWS
4438caf4 603 {
3ca6a5f0
BP
604 // item is the last in the row if it is in the last columns
605 isLastInTheRow = i >= (m_noItems/m_majorDim)*m_majorDim;
606 }
607
608 // is this the start of new row/column?
609 if ( i && (i % m_majorDim == 0) )
610 {
611 if ( m_windowStyle & wxRA_SPECIFY_ROWS )
4438caf4 612 {
3ca6a5f0 613 // start of new column
4438caf4
VZ
614 y_offset = startY;
615 x_offset += maxWidth + cx1;
616 }
3ca6a5f0 617 else // start of new row
4438caf4
VZ
618 {
619 x_offset = startX;
620 y_offset += maxHeight;
621 if (m_radioWidth[0]>0)
622 y_offset += cy1/2;
623 }
624 }
3ca6a5f0
BP
625
626 int widthBtn;
627 if ( isLastInTheRow )
4438caf4 628 {
3ca6a5f0
BP
629 // make the button go to the end of radio box
630 widthBtn = startX + width - x_offset - 2*cx1;
631 if ( widthBtn < maxWidth )
632 widthBtn = maxWidth;
4438caf4
VZ
633 }
634 else
635 {
3ca6a5f0
BP
636 // normal button, always of the same size
637 widthBtn = maxWidth;
4438caf4 638 }
1f916a19 639
f048e32f
VZ
640 // VZ: make all buttons of the same, maximal size - like this they
641 // cover the radiobox entirely and the radiobox tooltips are always
642 // shown (otherwise they are not when the mouse pointer is in the
643 // radiobox part not belonging to any radiobutton)
644 ::MoveWindow((HWND)m_radioButtons[i],
3ca6a5f0 645 x_offset, y_offset, widthBtn, maxHeight,
f048e32f 646 TRUE);
4438caf4 647
3ca6a5f0
BP
648 // where do we put the next button?
649 if ( m_windowStyle & wxRA_SPECIFY_ROWS )
4438caf4 650 {
3ca6a5f0 651 // below this one
4438caf4
VZ
652 y_offset += maxHeight;
653 if (m_radioWidth[0]>0)
654 y_offset += cy1/2;
655 }
656 else
3ca6a5f0
BP
657 {
658 // to the right of this one
659 x_offset += widthBtn + cx1;
660 }
1f916a19 661 }
1f916a19
JS
662}
663
2bda0e17
KB
664void wxRadioBox::GetSize(int *width, int *height) const
665{
4d08943e 666 RECT rect = { -1, -1, -1, -1 };
2bda0e17 667
da07e033
VZ
668 if (m_hWnd)
669 wxFindMaxSize(m_hWnd, &rect);
2bda0e17 670
da07e033
VZ
671 int i;
672 for (i = 0; i < m_noItems; i++)
673 wxFindMaxSize(m_radioButtons[i], &rect);
2bda0e17 674
da07e033
VZ
675 *width = rect.right - rect.left;
676 *height = rect.bottom - rect.top;
2bda0e17
KB
677}
678
679void wxRadioBox::GetPosition(int *x, int *y) const
680{
da07e033 681 wxWindow *parent = GetParent();
f048e32f 682 RECT rect = { -1, -1, -1, -1 };
da07e033
VZ
683
684 int i;
685 for (i = 0; i < m_noItems; i++)
686 wxFindMaxSize(m_radioButtons[i], &rect);
687
688 if (m_hWnd)
689 wxFindMaxSize(m_hWnd, &rect);
690
f048e32f
VZ
691 // Since we now have the absolute screen coords, if there's a parent we
692 // must subtract its top left corner
da07e033
VZ
693 POINT point;
694 point.x = rect.left;
695 point.y = rect.top;
696 if (parent)
697 {
698 ::ScreenToClient((HWND) parent->GetHWND(), &point);
699 }
f048e32f
VZ
700
701 // We may be faking the client origin. So a window that's really at (0, 30)
702 // may appear (to wxWin apps) to be at (0, 0).
da07e033
VZ
703 if (GetParent())
704 {
705 wxPoint pt(GetParent()->GetClientAreaOrigin());
706 point.x -= pt.x;
707 point.y -= pt.y;
708 }
709
710 *x = point.x;
711 *y = point.y;
2bda0e17
KB
712}
713
e373f51b 714void wxRadioBox::SetFocus()
2bda0e17 715{
da07e033
VZ
716 if (m_noItems > 0)
717 {
4d08943e 718 ::SetFocus((HWND)m_radioButtons[m_selectedButton == wxID_ANY
974931fe
VZ
719 ? 0
720 : m_selectedButton]);
da07e033 721 }
2bda0e17
KB
722
723}
724
debe6624 725bool wxRadioBox::Show(bool show)
2bda0e17 726{
42e69d6b 727 if ( !wxControl::Show(show) )
4d08943e 728 return false;
42e69d6b
VZ
729
730 int nCmdShow = show ? SW_SHOW : SW_HIDE;
731 for ( int i = 0; i < m_noItems; i++ )
732 {
733 ::ShowWindow((HWND)m_radioButtons[i], nCmdShow);
734 }
735
4d08943e 736 return true;
2bda0e17
KB
737}
738
739// Enable a specific button
debe6624 740void wxRadioBox::Enable(int item, bool enable)
2bda0e17 741{
42e69d6b 742 wxCHECK_RET( item >= 0 && item < m_noItems,
223d09f6 743 wxT("invalid item in wxRadioBox::Enable()") );
42e69d6b
VZ
744
745 ::EnableWindow((HWND) m_radioButtons[item], enable);
2bda0e17
KB
746}
747
748// Enable all controls
cc2b7472 749bool wxRadioBox::Enable(bool enable)
2bda0e17 750{
cc2b7472 751 if ( !wxControl::Enable(enable) )
4d08943e 752 return false;
1eb20d4a 753
42e69d6b 754 for (int i = 0; i < m_noItems; i++)
da07e033 755 ::EnableWindow((HWND) m_radioButtons[i], enable);
cc2b7472 756
4d08943e 757 return true;
2bda0e17
KB
758}
759
760// Show a specific button
debe6624 761void wxRadioBox::Show(int item, bool show)
2bda0e17 762{
42e69d6b 763 wxCHECK_RET( item >= 0 && item < m_noItems,
223d09f6 764 wxT("invalid item in wxRadioBox::Show()") );
42e69d6b
VZ
765
766 ::ShowWindow((HWND)m_radioButtons[item], show ? SW_SHOW : SW_HIDE);
2bda0e17
KB
767}
768
2bda0e17
KB
769bool wxRadioBox::ContainsHWND(WXHWND hWnd) const
770{
1e6feb95
VZ
771 size_t count = GetCount();
772 for ( size_t i = 0; i < count; i++ )
42e69d6b 773 {
1e6feb95 774 if ( GetRadioButtons()[i] == hWnd )
4d08943e 775 return true;
42e69d6b
VZ
776 }
777
4d08943e 778 return false;
2bda0e17
KB
779}
780
4afd7529 781void wxRadioBox::Command(wxCommandEvent & event)
2bda0e17 782{
da07e033 783 SetSelection (event.m_commandInt);
974931fe 784 SetFocus();
da07e033 785 ProcessCommand (event);
2bda0e17
KB
786}
787
8614c467
VZ
788// NB: if this code is changed, wxGetWindowForHWND() which relies on having the
789// radiobox pointer in GWL_USERDATA for radio buttons must be updated too!
e373f51b
VZ
790void wxRadioBox::SubclassRadioButton(WXHWND hWndBtn)
791{
792 HWND hwndBtn = (HWND)hWndBtn;
793
794 if ( !s_wndprocRadioBtn )
975b6bcf 795 s_wndprocRadioBtn = (WXFARPROC)wxGetWindowProc(hwndBtn);
e373f51b 796
975b6bcf
VZ
797 wxSetWindowProc(hwndBtn, wxRadioBtnWndProc);
798 wxSetWindowUserData(hwndBtn, this);
e373f51b
VZ
799}
800
9a5ccab4
VZ
801void wxRadioBox::SendNotificationEvent()
802{
803 wxCommandEvent event(wxEVT_COMMAND_RADIOBOX_SELECTED, m_windowId);
804 event.SetInt( m_selectedButton );
36dda0de 805 event.SetString( GetString(m_selectedButton) );
9a5ccab4
VZ
806 event.SetEventObject( this );
807 ProcessCommand(event);
808}
809
4afd7529
VZ
810bool wxRadioBox::SetFont(const wxFont& font)
811{
812 if ( !wxControl::SetFont(font) )
813 {
814 // nothing to do
4d08943e 815 return false;
4afd7529
VZ
816 }
817
818 // also set the font of our radio buttons
819 WXHFONT hfont = wxFont(font).GetResourceHandle();
820 for ( int n = 0; n < m_noItems; n++ )
821 {
f6bcfd97
BP
822 HWND hwndBtn = (HWND)m_radioButtons[n];
823 ::SendMessage(hwndBtn, WM_SETFONT, (WPARAM)hfont, 0L);
4afd7529 824
f6bcfd97
BP
825 // otherwise the buttons are not redrawn correctly
826 ::InvalidateRect(hwndBtn, NULL, FALSE /* don't erase bg */);
827 }
8614c467 828
4d08943e 829 return true;
4afd7529
VZ
830}
831
8614c467
VZ
832// ----------------------------------------------------------------------------
833// our window proc
834// ----------------------------------------------------------------------------
835
c140b7e7 836WXLRESULT wxRadioBox::MSWWindowProc(WXUINT nMsg, WXWPARAM wParam, WXLPARAM lParam)
c92d798f 837{
65546961 838#if RADIOBTN_PARENT_IS_RADIOBOX
d3acd369 839 switch ( nMsg )
c92d798f 840 {
65546961 841 // handle this message to set correct colours for our buttons here
d3acd369 842 case WM_CTLCOLORSTATIC:
65546961
VZ
843 {
844 WXHDC hdc;
845 WXHWND hwnd;
846 UnpackCtlColor(wParam, lParam, &hdc, &hwnd);
847
848 WXHBRUSH hbr = MSWControlColor((WXHDC)hdc);
849 if ( hbr )
850 return (WXLRESULT)hbr;
851 //else: fall through to default window proc
852 }
d3acd369 853 }
65546961 854#endif // RADIOBTN_PARENT_IS_RADIOBOX
8614c467
VZ
855
856 return wxControl::MSWWindowProc(nMsg, wParam, lParam);
c92d798f
JS
857}
858
e373f51b
VZ
859// ---------------------------------------------------------------------------
860// window proc for radio buttons
861// ---------------------------------------------------------------------------
862
2a47d3c1
JS
863#ifdef __WIN32__
864
e373f51b 865LRESULT APIENTRY _EXPORT wxRadioBtnWndProc(HWND hwnd,
8614c467 866 UINT message,
e373f51b
VZ
867 WPARAM wParam,
868 LPARAM lParam)
869{
8614c467 870 switch ( message )
e373f51b 871 {
8614c467
VZ
872 case WM_GETDLGCODE:
873 // we must tell IsDialogMessage()/our kbd processing code that we
874 // want to process arrows ourselves because neither of them is
875 // smart enough to handle arrows properly for us
876 {
8ad6ad99 877 long lDlgCode = ::CallWindowProc(CASTWNDPROC s_wndprocRadioBtn, hwnd,
8614c467 878 message, wParam, lParam);
8ad6ad99 879
8614c467
VZ
880 return lDlgCode | DLGC_WANTARROWS;
881 }
e373f51b 882
8614c467
VZ
883#if wxUSE_TOOLTIPS
884 case WM_NOTIFY:
f048e32f 885 {
8614c467 886 NMHDR* hdr = (NMHDR *)lParam;
2b5f62a0 887 if ( hdr->code == TTN_NEEDTEXT )
e373f51b 888 {
975b6bcf
VZ
889 wxRadioBox *
890 radiobox = (wxRadioBox *)wxGetWindowUserData(hwnd);
e373f51b 891
8614c467
VZ
892 wxCHECK_MSG( radiobox, 0,
893 wxT("radio button without radio box?") );
894
895 wxToolTip *tooltip = radiobox->GetToolTip();
896 if ( tooltip )
897 {
898 TOOLTIPTEXT *ttt = (TOOLTIPTEXT *)lParam;
899 ttt->lpszText = (wxChar *)tooltip->GetTip().c_str();
900 }
901
902 // processed
903 return 0;
e373f51b 904 }
f048e32f 905 }
8614c467 906 break;
f048e32f 907#endif // wxUSE_TOOLTIPS
f048e32f 908
8614c467 909 case WM_KEYDOWN:
9a5ccab4 910 {
975b6bcf 911 wxRadioBox *radiobox = (wxRadioBox *)wxGetWindowUserData(hwnd);
f048e32f 912
8614c467 913 wxCHECK_MSG( radiobox, 0, wxT("radio button without radio box?") );
f048e32f 914
4d08943e 915 bool processed = true;
f048e32f 916
e421922f 917 wxDirection dir;
8614c467
VZ
918 switch ( wParam )
919 {
920 case VK_UP:
1e6feb95 921 dir = wxUP;
8614c467 922 break;
f048e32f 923
8614c467 924 case VK_LEFT:
1e6feb95 925 dir = wxLEFT;
8614c467 926 break;
f048e32f 927
8614c467 928 case VK_DOWN:
1e6feb95 929 dir = wxDOWN;
8614c467 930 break;
f048e32f 931
8614c467 932 case VK_RIGHT:
1e6feb95 933 dir = wxRIGHT;
8614c467
VZ
934 break;
935
936 default:
4d08943e 937 processed = false;
1e6feb95
VZ
938
939 // just to suppress the compiler warning
940 dir = wxALL;
8614c467
VZ
941 }
942
943 if ( processed )
f048e32f 944 {
1e6feb95
VZ
945 int selOld = radiobox->GetSelection();
946 int selNew = radiobox->GetNextItem
947 (
948 selOld,
949 dir,
950 radiobox->GetWindowStyle()
951 );
3ca6a5f0 952
8614c467
VZ
953 if ( selNew != selOld )
954 {
955 radiobox->SetSelection(selNew);
974931fe 956 radiobox->SetFocus();
8614c467
VZ
957
958 // emulate the button click
959 radiobox->SendNotificationEvent();
9a5ccab4 960
8614c467
VZ
961 return 0;
962 }
f048e32f 963 }
9a5ccab4 964 }
21687069
VZ
965 break;
966
1dab049c
VZ
967 case WM_SETFOCUS:
968 case WM_KILLFOCUS:
969 {
975b6bcf 970 wxRadioBox *radiobox = (wxRadioBox *)wxGetWindowUserData(hwnd);
1dab049c
VZ
971
972 wxCHECK_MSG( radiobox, 0, wxT("radio button without radio box?") );
973
974 // if we don't do this, no focus events are generated for the
975 // radiobox and, besides, we need to notify the parent about
976 // the focus change, otherwise the focus handling logic in
977 // wxControlContainer doesn't work
978 if ( message == WM_SETFOCUS )
979 radiobox->HandleSetFocus((WXHWND)wParam);
980 else
981 radiobox->HandleKillFocus((WXHWND)wParam);
982 }
983 break;
984
fac93396
JS
985#ifdef __WIN32__
986 case WM_HELP:
e421922f 987 {
975b6bcf 988 wxRadioBox *radiobox = (wxRadioBox *)wxGetWindowUserData(hwnd);
fac93396 989
e421922f 990 wxCHECK_MSG( radiobox, 0, wxT("radio button without radio box?") );
fac93396 991
5cb598ae 992 bool processed wxDUMMY_INITIALIZE(true);
fac93396 993
4676948b
JS
994 // HELPINFO doesn't seem to be supported on WinCE.
995#ifndef __WXWINCE__
e421922f
VZ
996 HELPINFO* info = (HELPINFO*) lParam;
997 // Don't yet process menu help events, just windows
998 if (info->iContextType == HELPINFO_WINDOW)
4676948b 999#endif
fac93396 1000 {
e421922f 1001 wxWindow* subjectOfHelp = radiobox;
4d08943e 1002 bool eventProcessed = false;
e421922f
VZ
1003 while (subjectOfHelp && !eventProcessed)
1004 {
4676948b
JS
1005 wxHelpEvent helpEvent(wxEVT_HELP, subjectOfHelp->GetId(),
1006#ifdef __WXWINCE__
1007 wxPoint(0, 0)
1008#else
1009 wxPoint(info->MousePos.x, info->MousePos.y)
1010#endif
1011 ) ; // info->iCtrlId);
e421922f
VZ
1012 helpEvent.SetEventObject(radiobox);
1013 eventProcessed = radiobox->GetEventHandler()->ProcessEvent(helpEvent);
fac93396 1014
e421922f
VZ
1015 // Go up the window hierarchy until the event is handled (or not)
1016 subjectOfHelp = subjectOfHelp->GetParent();
1017 }
1018 processed = eventProcessed;
fac93396 1019 }
4676948b 1020#ifndef __WXWINCE__
e421922f
VZ
1021 else if (info->iContextType == HELPINFO_MENUITEM)
1022 {
1023 wxHelpEvent helpEvent(wxEVT_HELP, info->iCtrlId) ;
1024 helpEvent.SetEventObject(radiobox);
1025 processed = radiobox->GetEventHandler()->ProcessEvent(helpEvent);
1026 }
4676948b 1027 else
5cb598ae 1028 processed = false;
4676948b 1029#endif
e421922f
VZ
1030 if (processed)
1031 return 0;
fac93396 1032
e421922f
VZ
1033 break;
1034 }
1035#endif // __WIN32__
e373f51b
VZ
1036 }
1037
8ad6ad99 1038 return ::CallWindowProc(CASTWNDPROC s_wndprocRadioBtn, hwnd, message, wParam, lParam);
e373f51b 1039}
42e69d6b
VZ
1040
1041#endif // __WIN32__
e373f51b 1042
1e6feb95 1043#endif // wxUSE_RADIOBOX