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