]> git.saurik.com Git - wxWidgets.git/blame - src/msw/radiobox.cpp
attempted Unicode fixes for BC++
[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 184 m_selectedButton = -1;
4afd7529 185 m_noItems = 0;
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 198 // and now create the buttons
4afd7529 199 m_noItems = n;
f048e32f
VZ
200#if RADIOBTN_PARENT_IS_RADIOBOX
201 HWND hwndParent = GetHwnd();
202#else
203 HWND hwndParent = GetHwndOf(parent);
204#endif
da07e033
VZ
205
206 // Some radio boxes test consecutive id.
e373f51b 207 (void)NewControlId();
da07e033 208 m_radioButtons = new WXHWND[n];
e373f51b
VZ
209 m_radioWidth = new int[n];
210 m_radioHeight = new int[n];
f048e32f
VZ
211
212 WXHFONT hfont = 0;
213 wxFont& font = GetFont();
214 if ( font.Ok() )
da07e033 215 {
f048e32f
VZ
216 hfont = font.GetResourceHandle();
217 }
218
219 for ( int i = 0; i < n; i++ )
220 {
221 m_radioWidth[i] =
222 m_radioHeight[i] = -1;
223 long styleBtn = BS_AUTORADIOBUTTON | WS_CHILD | WS_VISIBLE;
e373f51b 224 if ( i == 0 && style == 0 )
f048e32f
VZ
225 styleBtn |= WS_GROUP;
226
da07e033 227 long newId = NewControlId();
da07e033 228
f048e32f
VZ
229 HWND hwndBtn = ::CreateWindow(_T("BUTTON"),
230 choices[i],
231 styleBtn,
232 0, 0, 0, 0, // will be set in SetSize()
e373f51b 233 hwndParent,
f048e32f
VZ
234 (HMENU)newId,
235 wxGetInstance(),
e373f51b 236 NULL);
2bda0e17 237
f048e32f
VZ
238 if ( !hwndBtn )
239 {
240 wxLogLastError("CreateWindow(radio btn)");
241
242 return FALSE;
243 }
244
e373f51b 245 m_radioButtons[i] = (WXHWND)hwndBtn;
42e69d6b 246
e373f51b 247 SubclassRadioButton((WXHWND)hwndBtn);
2bda0e17 248
f048e32f 249 if ( hfont )
da07e033 250 {
f048e32f 251 ::SendMessage(hwndBtn, WM_SETFONT, (WPARAM)hfont, 0L);
da07e033 252 }
e373f51b 253
f048e32f 254 m_subControls.Add(newId);
da07e033 255 }
e373f51b 256
da07e033 257 // Create a dummy radio control to end the group.
f048e32f
VZ
258 (void)::CreateWindow(_T("BUTTON"),
259 _T(""),
260 WS_GROUP | BS_AUTORADIOBUTTON | WS_CHILD,
e373f51b
VZ
261 0, 0, 0, 0, hwndParent,
262 (HMENU)NewControlId(), wxGetInstance(), NULL);
2bda0e17 263
da07e033 264 SetSelection(0);
2bda0e17 265
f048e32f 266 SetSize(pos.x, pos.y, size.x, size.y);
2bda0e17 267
da07e033 268 return TRUE;
2bda0e17 269}
2bda0e17 270
e373f51b 271wxRadioBox::~wxRadioBox()
2bda0e17 272{
da07e033 273 m_isBeingDeleted = TRUE;
1eb20d4a 274
da07e033
VZ
275 if (m_radioButtons)
276 {
277 int i;
278 for (i = 0; i < m_noItems; i++)
42e69d6b 279 ::DestroyWindow((HWND)m_radioButtons[i]);
da07e033
VZ
280 delete[] m_radioButtons;
281 }
42e69d6b 282
da07e033 283 if (m_radioWidth)
e373f51b 284 delete[] m_radioWidth;
da07e033 285 if (m_radioHeight)
e373f51b 286 delete[] m_radioHeight;
2bda0e17
KB
287
288}
289
d66a042c
VZ
290wxString wxRadioBox::GetLabel(int item) const
291{
223d09f6 292 wxCHECK_MSG( item >= 0 && item < m_noItems, wxT(""), wxT("invalid radiobox index") );
d66a042c
VZ
293
294 return wxGetWindowText(m_radioButtons[item]);
295}
296
debe6624 297void wxRadioBox::SetLabel(int item, const wxString& label)
2bda0e17 298{
223d09f6 299 wxCHECK_RET( item >= 0 && item < m_noItems, wxT("invalid radiobox index") );
d66a042c 300
e373f51b 301 m_radioWidth[item] = m_radioHeight[item] = -1;
42e69d6b 302 SetWindowText((HWND)m_radioButtons[item], label.c_str());
2bda0e17
KB
303}
304
debe6624 305void wxRadioBox::SetLabel(int item, wxBitmap *bitmap)
2bda0e17 306{
da07e033 307 /*
e373f51b
VZ
308 m_radioWidth[item] = bitmap->GetWidth() + FB_MARGIN;
309 m_radioHeight[item] = bitmap->GetHeight() + FB_MARGIN;
da07e033 310 */
223d09f6 311 wxFAIL_MSG(wxT("not implemented"));
2bda0e17
KB
312}
313
314int wxRadioBox::FindString(const wxString& s) const
315{
42e69d6b 316 for (int i = 0; i < m_noItems; i++)
da07e033 317 {
42e69d6b 318 if ( s == wxGetWindowText(m_radioButtons[i]) )
da07e033
VZ
319 return i;
320 }
42e69d6b
VZ
321
322 return wxNOT_FOUND;
2bda0e17
KB
323}
324
debe6624 325void wxRadioBox::SetSelection(int N)
2bda0e17 326{
223d09f6 327 wxCHECK_RET( (N >= 0) && (N < m_noItems), wxT("invalid radiobox index") );
2bda0e17 328
da07e033
VZ
329 // Following necessary for Win32s, because Win32s translate BM_SETCHECK
330 if (m_selectedButton >= 0 && m_selectedButton < m_noItems)
e373f51b
VZ
331 ::SendMessage((HWND) m_radioButtons[m_selectedButton], BM_SETCHECK, 0, 0L);
332
333 ::SendMessage((HWND)m_radioButtons[N], BM_SETCHECK, 1, 0L);
334 ::SetFocus((HWND)m_radioButtons[N]);
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
debe6624 346wxString wxRadioBox::GetString(int N) const
2bda0e17 347{
e373f51b 348 return wxGetWindowText(m_radioButtons[N]);
2bda0e17
KB
349}
350
1f916a19 351// Restored old code.
bfc6fde4 352void wxRadioBox::DoSetSize(int x, int y, int width, int height, int sizeFlags)
1f916a19 353{
4438caf4
VZ
354 int currentX, currentY;
355 GetPosition(&currentX, &currentY);
c219cecc
VZ
356 int widthOld, heightOld;
357 GetSize(&widthOld, &heightOld);
358
4438caf4
VZ
359 int xx = x;
360 int yy = y;
361
362 if (x == -1 || (sizeFlags & wxSIZE_ALLOW_MINUS_ONE))
363 xx = currentX;
364 if (y == -1 || (sizeFlags & wxSIZE_ALLOW_MINUS_ONE))
365 yy = currentY;
366
f048e32f
VZ
367#if RADIOBTN_PARENT_IS_RADIOBOX
368 int y_offset = 0;
369 int x_offset = 0;
370#else
4438caf4
VZ
371 int y_offset = yy;
372 int x_offset = xx;
f048e32f
VZ
373#endif
374
4438caf4
VZ
375 int current_width, cyf;
376
377 int cx1,cy1;
378 wxGetCharSize(m_hWnd, &cx1, &cy1, & GetFont());
379
380 // Attempt to have a look coherent with other platforms: We compute the
381 // biggest toggle dim, then we align all items according this value.
382 int maxWidth = -1;
383 int maxHeight = -1;
384
385 int i;
386 for (i = 0 ; i < m_noItems; i++)
1f916a19 387 {
4438caf4
VZ
388 int eachWidth;
389 int eachHeight;
390 if (m_radioWidth[i]<0)
391 {
392 // It's a labelled toggle
f048e32f
VZ
393 GetTextExtent(wxGetWindowText(m_radioButtons[i]),
394 &current_width, &cyf);
4438caf4
VZ
395 eachWidth = (int)(current_width + RADIO_SIZE);
396 eachHeight = (int)((3*cyf)/2);
397 }
398 else
399 {
400 eachWidth = m_radioWidth[i];
401 eachHeight = m_radioHeight[i];
402 }
403
404 if (maxWidth<eachWidth)
405 maxWidth = eachWidth;
406 if (maxHeight<eachHeight)
407 maxHeight = eachHeight;
1f916a19 408 }
1f916a19 409
4438caf4
VZ
410 if (m_hWnd)
411 {
412 int totWidth;
413 int totHeight;
1f916a19 414
4438caf4 415 int nbHor = GetNumHor(),
f048e32f 416 nbVer = GetNumVer();
1f916a19 417
4438caf4
VZ
418 // this formula works, but I don't know why.
419 // Please, be sure what you do if you modify it!!
420 if (m_radioWidth[0]<0)
421 totHeight = (nbVer * maxHeight) + cy1/2;
422 else
423 totHeight = nbVer * (maxHeight+cy1/2);
424 totWidth = nbHor * (maxWidth+cx1);
1f916a19 425
4438caf4
VZ
426 int extraHeight = cy1;
427
c219cecc 428#if defined(CTL3D) && !CTL3D
4438caf4
VZ
429 // Requires a bigger group box in plain Windows
430 extraHeight *= 3;
431 extraHeight /= 2;
1f916a19 432#endif
4438caf4 433
c219cecc
VZ
434 // only change our width/height if asked for
435 if ( width == -1 )
436 {
437 if ( sizeFlags & wxSIZE_AUTO_WIDTH )
438 width = totWidth + cx1;
439 else
440 width = widthOld;
441 }
442
443 if ( height == -1 )
444 {
445 if ( sizeFlags & wxSIZE_AUTO_HEIGHT )
446 height = totHeight + extraHeight;
a17e237f 447 else
c219cecc
VZ
448 height = heightOld;
449 }
450
f048e32f 451 ::MoveWindow(GetHwnd(), xx, yy, width, height, TRUE);
4438caf4
VZ
452
453 x_offset += cx1;
454 y_offset += cy1;
455 }
1f916a19 456
c219cecc 457#if defined(CTL3D) && (!CTL3D)
4438caf4
VZ
458 y_offset += (int)(cy1/2); // Fudge factor since buttons overlapped label
459 // JACS 2/12/93. CTL3D draws group label quite high.
1f916a19 460#endif
4438caf4
VZ
461 int startX = x_offset;
462 int startY = y_offset;
1f916a19 463
4438caf4 464 for ( i = 0 ; i < m_noItems; i++)
1f916a19 465 {
4438caf4
VZ
466 // Bidimensional radio adjustment
467 if (i&&((i%m_majorDim)==0)) // Why is this omitted for i = 0?
468 {
469 if (m_windowStyle & wxRA_VERTICAL)
470 {
471 y_offset = startY;
472 x_offset += maxWidth + cx1;
473 }
474 else
475 {
476 x_offset = startX;
477 y_offset += maxHeight;
478 if (m_radioWidth[0]>0)
479 y_offset += cy1/2;
480 }
481 }
482 int eachWidth;
483 int eachHeight;
484 if (m_radioWidth[i]<0)
485 {
486 // It's a labeled item
f048e32f
VZ
487 GetTextExtent(wxGetWindowText(m_radioButtons[i]),
488 &current_width, &cyf);
4438caf4
VZ
489
490 // How do we find out radio button bitmap size!!
491 // By adjusting them carefully, manually :-)
492 eachWidth = (int)(current_width + RADIO_SIZE);
493 eachHeight = (int)((3*cyf)/2);
494 }
495 else
496 {
497 eachWidth = m_radioWidth[i];
498 eachHeight = m_radioHeight[i];
499 }
1f916a19 500
f048e32f
VZ
501 // VZ: make all buttons of the same, maximal size - like this they
502 // cover the radiobox entirely and the radiobox tooltips are always
503 // shown (otherwise they are not when the mouse pointer is in the
504 // radiobox part not belonging to any radiobutton)
505 ::MoveWindow((HWND)m_radioButtons[i],
506 x_offset, y_offset, maxWidth, maxHeight,
507 TRUE);
4438caf4
VZ
508
509 if (m_windowStyle & wxRA_SPECIFY_ROWS)
510 {
511 y_offset += maxHeight;
512 if (m_radioWidth[0]>0)
513 y_offset += cy1/2;
514 }
515 else
516 x_offset += maxWidth + cx1;
1f916a19 517 }
1f916a19
JS
518}
519
2bda0e17
KB
520void wxRadioBox::GetSize(int *width, int *height) const
521{
da07e033
VZ
522 RECT rect;
523 rect.left = -1; rect.right = -1; rect.top = -1; rect.bottom = -1;
2bda0e17 524
da07e033
VZ
525 if (m_hWnd)
526 wxFindMaxSize(m_hWnd, &rect);
2bda0e17 527
da07e033
VZ
528 int i;
529 for (i = 0; i < m_noItems; i++)
530 wxFindMaxSize(m_radioButtons[i], &rect);
2bda0e17 531
da07e033
VZ
532 *width = rect.right - rect.left;
533 *height = rect.bottom - rect.top;
2bda0e17
KB
534}
535
536void wxRadioBox::GetPosition(int *x, int *y) const
537{
da07e033 538 wxWindow *parent = GetParent();
f048e32f 539 RECT rect = { -1, -1, -1, -1 };
da07e033
VZ
540
541 int i;
542 for (i = 0; i < m_noItems; i++)
543 wxFindMaxSize(m_radioButtons[i], &rect);
544
545 if (m_hWnd)
546 wxFindMaxSize(m_hWnd, &rect);
547
f048e32f
VZ
548 // Since we now have the absolute screen coords, if there's a parent we
549 // must subtract its top left corner
da07e033
VZ
550 POINT point;
551 point.x = rect.left;
552 point.y = rect.top;
553 if (parent)
554 {
555 ::ScreenToClient((HWND) parent->GetHWND(), &point);
556 }
f048e32f
VZ
557
558 // We may be faking the client origin. So a window that's really at (0, 30)
559 // may appear (to wxWin apps) to be at (0, 0).
da07e033
VZ
560 if (GetParent())
561 {
562 wxPoint pt(GetParent()->GetClientAreaOrigin());
563 point.x -= pt.x;
564 point.y -= pt.y;
565 }
566
567 *x = point.x;
568 *y = point.y;
2bda0e17
KB
569}
570
e373f51b 571void wxRadioBox::SetFocus()
2bda0e17 572{
da07e033
VZ
573 if (m_noItems > 0)
574 {
575 if (m_selectedButton == -1)
576 ::SetFocus((HWND) m_radioButtons[0]);
577 else
578 ::SetFocus((HWND) m_radioButtons[m_selectedButton]);
579 }
2bda0e17
KB
580
581}
582
debe6624 583bool wxRadioBox::Show(bool show)
2bda0e17 584{
42e69d6b
VZ
585 if ( !wxControl::Show(show) )
586 return FALSE;
587
588 int nCmdShow = show ? SW_SHOW : SW_HIDE;
589 for ( int i = 0; i < m_noItems; i++ )
590 {
591 ::ShowWindow((HWND)m_radioButtons[i], nCmdShow);
592 }
593
da07e033 594 return TRUE;
2bda0e17
KB
595}
596
597// Enable a specific button
debe6624 598void wxRadioBox::Enable(int item, bool enable)
2bda0e17 599{
42e69d6b 600 wxCHECK_RET( item >= 0 && item < m_noItems,
223d09f6 601 wxT("invalid item in wxRadioBox::Enable()") );
42e69d6b
VZ
602
603 ::EnableWindow((HWND) m_radioButtons[item], enable);
2bda0e17
KB
604}
605
606// Enable all controls
cc2b7472 607bool wxRadioBox::Enable(bool enable)
2bda0e17 608{
cc2b7472
VZ
609 if ( !wxControl::Enable(enable) )
610 return FALSE;
1eb20d4a 611
42e69d6b 612 for (int i = 0; i < m_noItems; i++)
da07e033 613 ::EnableWindow((HWND) m_radioButtons[i], enable);
cc2b7472
VZ
614
615 return TRUE;
2bda0e17
KB
616}
617
618// Show a specific button
debe6624 619void wxRadioBox::Show(int item, bool show)
2bda0e17 620{
42e69d6b 621 wxCHECK_RET( item >= 0 && item < m_noItems,
223d09f6 622 wxT("invalid item in wxRadioBox::Show()") );
42e69d6b
VZ
623
624 ::ShowWindow((HWND)m_radioButtons[item], show ? SW_SHOW : SW_HIDE);
2bda0e17
KB
625}
626
2bda0e17 627// For single selection items only
e373f51b 628wxString wxRadioBox::GetStringSelection() const
2bda0e17 629{
e373f51b
VZ
630 wxString result;
631 int sel = GetSelection();
da07e033 632 if (sel > -1)
e373f51b
VZ
633 result = GetString(sel);
634
635 return result;
2bda0e17
KB
636}
637
e373f51b 638bool wxRadioBox::SetStringSelection(const wxString& s)
2bda0e17 639{
da07e033
VZ
640 int sel = FindString (s);
641 if (sel > -1)
2bda0e17 642 {
da07e033
VZ
643 SetSelection (sel);
644 return TRUE;
2bda0e17 645 }
da07e033
VZ
646 else
647 return FALSE;
2bda0e17
KB
648}
649
2bda0e17
KB
650bool wxRadioBox::ContainsHWND(WXHWND hWnd) const
651{
da07e033 652 int i;
2bda0e17 653 for (i = 0; i < Number(); i++)
42e69d6b 654 {
da07e033
VZ
655 if (GetRadioButtons()[i] == hWnd)
656 return TRUE;
42e69d6b
VZ
657 }
658
da07e033 659 return FALSE;
2bda0e17
KB
660}
661
4afd7529 662void wxRadioBox::Command(wxCommandEvent & event)
2bda0e17 663{
da07e033
VZ
664 SetSelection (event.m_commandInt);
665 ProcessCommand (event);
2bda0e17
KB
666}
667
e373f51b
VZ
668void wxRadioBox::SubclassRadioButton(WXHWND hWndBtn)
669{
42e69d6b 670#ifdef __WIN32__
e373f51b
VZ
671 HWND hwndBtn = (HWND)hWndBtn;
672
673 if ( !s_wndprocRadioBtn )
20e85460 674 s_wndprocRadioBtn = (WXFARPROC)::GetWindowLong(hwndBtn, GWL_WNDPROC);
e373f51b 675
2a47d3c1 676 // No GWL_USERDATA in Win16, so omit this subclassing.
e373f51b
VZ
677 ::SetWindowLong(hwndBtn, GWL_WNDPROC, (long)wxRadioBtnWndProc);
678 ::SetWindowLong(hwndBtn, GWL_USERDATA, (long)this);
42e69d6b 679#endif // __WIN32__
e373f51b
VZ
680}
681
9a5ccab4
VZ
682void wxRadioBox::SendNotificationEvent()
683{
684 wxCommandEvent event(wxEVT_COMMAND_RADIOBOX_SELECTED, m_windowId);
685 event.SetInt( m_selectedButton );
36dda0de 686 event.SetString( GetString(m_selectedButton) );
9a5ccab4
VZ
687 event.SetEventObject( this );
688 ProcessCommand(event);
689}
690
4afd7529
VZ
691bool wxRadioBox::SetFont(const wxFont& font)
692{
693 if ( !wxControl::SetFont(font) )
694 {
695 // nothing to do
696 return FALSE;
697 }
698
699 // also set the font of our radio buttons
700 WXHFONT hfont = wxFont(font).GetResourceHandle();
701 for ( int n = 0; n < m_noItems; n++ )
702 {
703 ::SendMessage((HWND)m_radioButtons[n], WM_SETFONT, (WPARAM)hfont, 0L);
704 }
705
706 return TRUE;
707}
708
c92d798f
JS
709long wxRadioBox::MSWWindowProc(WXUINT nMsg, WXWPARAM wParam, WXLPARAM lParam)
710{
711 // This is required for the radiobox to be sensitive to mouse input,
712 // e.g. for Dialog Editor.
713 if (nMsg == WM_NCHITTEST)
714 {
715 int xPos = LOWORD(lParam); // horizontal position of cursor
716 int yPos = HIWORD(lParam); // vertical position of cursor
717
718 ScreenToClient(&xPos, &yPos);
719
720 // Make sure you can drag by the top of the groupbox, but let
721 // other (enclosed) controls get mouse events also
722 if (yPos < 10)
723 return (long)HTCLIENT;
724 }
725
726 return wxControl::MSWWindowProc(nMsg, wParam, lParam);
727}
728
e373f51b
VZ
729// ---------------------------------------------------------------------------
730// window proc for radio buttons
731// ---------------------------------------------------------------------------
732
2a47d3c1
JS
733#ifdef __WIN32__
734
e373f51b
VZ
735LRESULT APIENTRY _EXPORT wxRadioBtnWndProc(HWND hwnd,
736 UINT msg,
737 WPARAM wParam,
738 LPARAM lParam)
739{
f048e32f
VZ
740 bool processed = FALSE;
741 if ( msg == WM_KEYDOWN
742#if wxUSE_TOOLTIPS
743 || msg == WM_NOTIFY
744#endif // wxUSE_TOOLTIPS
745 )
e373f51b
VZ
746 {
747 wxRadioBox *radiobox = (wxRadioBox *)::GetWindowLong(hwnd, GWL_USERDATA);
748
223d09f6 749 wxCHECK_MSG( radiobox, 0, wxT("radio button without radio box?") );
e373f51b 750
f048e32f
VZ
751#if wxUSE_TOOLTIPS
752 if ( msg == WM_NOTIFY )
e373f51b 753 {
f048e32f
VZ
754 NMHDR* hdr = (NMHDR *)lParam;
755 if ( (int)hdr->code == TTN_NEEDTEXT )
756 {
757 wxToolTip *tt = radiobox->GetToolTip();
758 if ( tt )
e373f51b 759 {
f048e32f
VZ
760 TOOLTIPTEXT *ttt = (TOOLTIPTEXT *)lParam;
761 ttt->lpszText = (wxChar *)tt->GetTip().c_str();
e373f51b 762
f048e32f 763 processed = TRUE;
e373f51b 764 }
f048e32f 765 }
e373f51b 766 }
f048e32f
VZ
767 else // msg == WM_KEYDOWN
768#endif // wxUSE_TOOLTIPS
e373f51b 769 {
f048e32f
VZ
770 processed = TRUE;
771
772 int sel = radiobox->GetSelection();
773
774 switch ( wParam )
9a5ccab4 775 {
f048e32f
VZ
776 case VK_UP:
777 sel--;
778 break;
779
780 case VK_LEFT:
781 sel -= radiobox->GetNumVer();
782 break;
783
784 case VK_DOWN:
785 sel++;
786 break;
787
788 case VK_RIGHT:
789 sel += radiobox->GetNumVer();
790 break;
791
792 case VK_TAB:
793 {
794 wxNavigationKeyEvent event;
795 event.SetDirection(!(::GetKeyState(VK_SHIFT) & 0x100));
796 event.SetWindowChange(FALSE);
797 event.SetEventObject(radiobox);
798
799 if ( radiobox->GetEventHandler()->ProcessEvent(event) )
800 return 0;
801 }
802 // fall through
803
804 default:
805 processed = FALSE;
806 }
807
808 if ( processed )
809 {
810 if ( sel >= 0 && sel < radiobox->Number() )
811 {
812 radiobox->SetSelection(sel);
9a5ccab4 813
f048e32f
VZ
814 // emulate the button click
815 radiobox->SendNotificationEvent();
816 }
9a5ccab4 817 }
e373f51b
VZ
818 }
819 }
820
f048e32f 821 if ( processed )
e373f51b 822 return 0;
f048e32f
VZ
823
824 return ::CallWindowProc(CASTWNDPROC s_wndprocRadioBtn, hwnd, msg, wParam, lParam);
e373f51b 825}
42e69d6b
VZ
826
827#endif // __WIN32__
e373f51b 828