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