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