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