]> git.saurik.com Git - wxWidgets.git/blame - src/msw/radiobox.cpp
*** empty log message ***
[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 <stdio.h>
33 #include "wx/setup.h"
34 #include "wx/bitmap.h"
35 #include "wx/brush.h"
36 #include "wx/radiobox.h"
2bda0e17
KB
37#endif
38
39#include "wx/msw/private.h"
40
41#if !USE_SHARED_LIBRARY
da07e033 42 IMPLEMENT_DYNAMIC_CLASS(wxRadioBox, wxControl)
2bda0e17
KB
43#endif
44
e373f51b
VZ
45// ---------------------------------------------------------------------------
46// private functions
47// ---------------------------------------------------------------------------
48
e373f51b 49// wnd proc for radio buttons
2a47d3c1 50#ifdef __WIN32__
e373f51b
VZ
51LRESULT APIENTRY _EXPORT wxRadioBtnWndProc(HWND hWnd,
52 UINT message,
53 WPARAM wParam,
54 LPARAM lParam);
55
56// ---------------------------------------------------------------------------
57// global vars
58// ---------------------------------------------------------------------------
59
60// the pointer to standard radio button wnd proc
20e85460 61static WXFARPROC s_wndprocRadioBtn = (WXFARPROC)NULL;
e373f51b 62
42e69d6b
VZ
63#endif // __WIN32__
64
e373f51b
VZ
65// ===========================================================================
66// implementation
67// ===========================================================================
68
69// ---------------------------------------------------------------------------
70// wxRadioBox
71// ---------------------------------------------------------------------------
72
73int wxRadioBox::GetNumVer() const
74{
75 if ( m_windowStyle & wxRA_SPECIFY_ROWS )
76 {
77 return m_majorDim;
78 }
79 else
80 {
81 return (m_noItems + m_majorDim - 1)/m_majorDim;
82 }
83}
84
85int wxRadioBox::GetNumHor() const
86{
87 if ( m_windowStyle & wxRA_SPECIFY_ROWS )
88 {
89 return (m_noItems + m_majorDim - 1)/m_majorDim;
90 }
91 else
92 {
93 return m_majorDim;
94 }
95}
96
42e69d6b 97bool wxRadioBox::MSWCommand(WXUINT cmd, WXWORD id)
2bda0e17 98{
42e69d6b 99 if ( cmd == BN_CLICKED )
da07e033 100 {
9a5ccab4 101 int selectedButton = -1;
e373f51b
VZ
102
103 for ( int i = 0; i < m_noItems; i++ )
104 {
cc2b7472 105 if ( id == wxGetWindowId(m_radioButtons[i]) )
e373f51b 106 {
9a5ccab4 107 selectedButton = i;
e373f51b
VZ
108
109 break;
110 }
111 }
112
837e5743 113 wxASSERT_MSG( selectedButton != -1, _T("click from alien button?") );
2bda0e17 114
9a5ccab4
VZ
115 if ( selectedButton != m_selectedButton )
116 {
117 m_selectedButton = selectedButton;
118
119 SendNotificationEvent();
120 }
121 //else: don't generate events when the selection doesn't change
e373f51b 122
da07e033
VZ
123 return TRUE;
124 }
e373f51b
VZ
125 else
126 return FALSE;
2bda0e17
KB
127}
128
129#if WXWIN_COMPATIBILITY
130wxRadioBox::wxRadioBox(wxWindow *parent, wxFunction func, const char *title,
da07e033
VZ
131 int x, int y, int width, int height,
132 int n, char **choices,
133 int majorDim, long style, const char *name)
2bda0e17
KB
134{
135 wxString *choices2 = new wxString[n];
136 for ( int i = 0; i < n; i ++) choices2[i] = choices[i];
137 Create(parent, -1, title, wxPoint(x, y), wxSize(width, height), n, choices2, majorDim, style,
da07e033 138 wxDefaultValidator, name);
2bda0e17
KB
139 Callback(func);
140 delete choices2;
141}
142
143#endif
144
145// Radio box item
e373f51b 146wxRadioBox::wxRadioBox()
2bda0e17 147{
da07e033
VZ
148 m_selectedButton = -1;
149 m_noItems = 0;
150 m_noRowsOrCols = 0;
151 m_radioButtons = NULL;
e373f51b
VZ
152 m_majorDim = 0;
153 m_radioWidth = NULL;
154 m_radioHeight = NULL;
2bda0e17
KB
155}
156
debe6624 157bool wxRadioBox::Create(wxWindow *parent, wxWindowID id, const wxString& title,
da07e033
VZ
158 const wxPoint& pos, const wxSize& size,
159 int n, const wxString choices[],
160 int majorDim, long style,
161 const wxValidator& val, const wxString& name)
2bda0e17 162{
da07e033
VZ
163 m_selectedButton = -1;
164 m_noItems = n;
2bda0e17 165
da07e033
VZ
166 SetName(name);
167 SetValidator(val);
2bda0e17 168
da07e033 169 parent->AddChild(this);
e373f51b
VZ
170 m_backgroundColour = parent->GetBackgroundColour();
171 m_foregroundColour = parent->GetForegroundColour();
2bda0e17 172
da07e033 173 m_windowStyle = (long&)style;
2bda0e17 174
da07e033
VZ
175 int x = pos.x;
176 int y = pos.y;
177 int width = size.x;
178 int height = size.y;
2bda0e17 179
da07e033
VZ
180 if (id == -1)
181 m_windowId = NewControlId();
182 else
183 m_windowId = id;
184
e373f51b
VZ
185 if ( majorDim == 0 )
186 m_majorDim = n;
187 else
188 m_majorDim = majorDim;
da07e033 189 m_noRowsOrCols = majorDim;
da07e033
VZ
190
191 long msStyle = GROUP_FLAGS;
192
193 bool want3D;
e373f51b 194 WXDWORD exStyle = Determine3DEffects(0, &want3D);
da07e033 195
e373f51b 196 HWND hwndParent = (HWND)parent->GetHWND();
da07e033
VZ
197
198 m_hWnd = (WXHWND)::CreateWindowEx
e373f51b
VZ
199 (
200 (DWORD)exStyle,
201 GROUP_CLASS,
202 title,
203 msStyle,
204 0, 0, 0, 0,
205 hwndParent,
206 (HMENU)m_windowId,
207 wxGetInstance(),
208 NULL
209 );
c085e333 210
1f112209 211#if wxUSE_CTL3D
da07e033
VZ
212 if (want3D)
213 {
214 Ctl3dSubclassCtl((HWND)m_hWnd);
215 m_useCtl3D = TRUE;
216 }
e373f51b 217#endif // wxUSE_CTL3D
2bda0e17 218
da07e033
VZ
219 SetFont(parent->GetFont());
220
e373f51b 221 SubclassWin(m_hWnd);
da07e033
VZ
222
223 // Some radio boxes test consecutive id.
e373f51b 224 (void)NewControlId();
da07e033 225 m_radioButtons = new WXHWND[n];
e373f51b
VZ
226 m_radioWidth = new int[n];
227 m_radioHeight = new int[n];
da07e033
VZ
228 int i;
229 for (i = 0; i < n; i++)
230 {
e373f51b 231 m_radioWidth[i] = m_radioHeight[i] = -1;
da07e033 232 long groupStyle = 0;
e373f51b 233 if ( i == 0 && style == 0 )
da07e033
VZ
234 groupStyle = WS_GROUP;
235 long newId = NewControlId();
236 long msStyle = groupStyle | RADIO_FLAGS;
237
e373f51b
VZ
238 HWND hwndBtn = CreateWindowEx(exStyle, RADIO_CLASS,
239 choices[i], msStyle,
240 0,0,0,0,
241 hwndParent,
242 (HMENU)newId, wxGetInstance(),
243 NULL);
2bda0e17 244
e373f51b 245 m_radioButtons[i] = (WXHWND)hwndBtn;
42e69d6b 246
e373f51b 247 SubclassRadioButton((WXHWND)hwndBtn);
2bda0e17 248
e373f51b
VZ
249 wxFont& font = GetFont();
250 if ( font.Ok() )
da07e033 251 {
e373f51b
VZ
252 SendMessage(hwndBtn, WM_SETFONT,
253 (WPARAM)font.GetResourceHandle(), 0L);
da07e033 254 }
e373f51b 255
42e69d6b 256 m_subControls.Append((wxObject *)(WXWORD)newId);
da07e033 257 }
e373f51b 258
da07e033 259 // Create a dummy radio control to end the group.
837e5743 260 (void)CreateWindowEx(0, RADIO_CLASS, _T(""), WS_GROUP | RADIO_FLAGS,
e373f51b
VZ
261 0, 0, 0, 0, hwndParent,
262 (HMENU)NewControlId(), wxGetInstance(), NULL);
2bda0e17 263
da07e033 264 SetSelection(0);
2bda0e17 265
da07e033 266 SetSize(x, y, width, height);
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{
837e5743 292 wxCHECK_MSG( item >= 0 && item < m_noItems, _T(""), _T("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{
837e5743 299 wxCHECK_RET( item >= 0 && item < m_noItems, _T("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 */
837e5743 311 wxFAIL_MSG(_T("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{
837e5743 327 wxCHECK_RET( (N >= 0) && (N < m_noItems), _T("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
367 wxString buf;
368
369 int y_offset = yy;
370 int x_offset = xx;
371 int current_width, cyf;
372
373 int cx1,cy1;
374 wxGetCharSize(m_hWnd, &cx1, &cy1, & GetFont());
375
376 // Attempt to have a look coherent with other platforms: We compute the
377 // biggest toggle dim, then we align all items according this value.
378 int maxWidth = -1;
379 int maxHeight = -1;
380
381 int i;
382 for (i = 0 ; i < m_noItems; i++)
1f916a19 383 {
4438caf4
VZ
384 int eachWidth;
385 int eachHeight;
386 if (m_radioWidth[i]<0)
387 {
388 // It's a labelled toggle
389 buf = wxGetWindowText(m_radioButtons[i]);
390 GetTextExtent(buf, &current_width, &cyf);
391 eachWidth = (int)(current_width + RADIO_SIZE);
392 eachHeight = (int)((3*cyf)/2);
393 }
394 else
395 {
396 eachWidth = m_radioWidth[i];
397 eachHeight = m_radioHeight[i];
398 }
399
400 if (maxWidth<eachWidth)
401 maxWidth = eachWidth;
402 if (maxHeight<eachHeight)
403 maxHeight = eachHeight;
1f916a19 404 }
1f916a19 405
4438caf4
VZ
406 if (m_hWnd)
407 {
408 int totWidth;
409 int totHeight;
1f916a19 410
4438caf4 411 int nbHor = GetNumHor(),
e373f51b 412 nbVer = GetNumVer();
1f916a19 413
4438caf4
VZ
414 // this formula works, but I don't know why.
415 // Please, be sure what you do if you modify it!!
416 if (m_radioWidth[0]<0)
417 totHeight = (nbVer * maxHeight) + cy1/2;
418 else
419 totHeight = nbVer * (maxHeight+cy1/2);
420 totWidth = nbHor * (maxWidth+cx1);
1f916a19 421
4438caf4
VZ
422 int extraHeight = cy1;
423
c219cecc 424#if defined(CTL3D) && !CTL3D
4438caf4
VZ
425 // Requires a bigger group box in plain Windows
426 extraHeight *= 3;
427 extraHeight /= 2;
1f916a19 428#endif
4438caf4 429
c219cecc
VZ
430 // only change our width/height if asked for
431 if ( width == -1 )
432 {
433 if ( sizeFlags & wxSIZE_AUTO_WIDTH )
434 width = totWidth + cx1;
435 else
436 width = widthOld;
437 }
438
439 if ( height == -1 )
440 {
441 if ( sizeFlags & wxSIZE_AUTO_HEIGHT )
442 height = totHeight + extraHeight;
443 else
444 height = heightOld;
445 }
446
447 MoveWindow(GetHwnd(), x_offset, y_offset, width, height, TRUE);
4438caf4
VZ
448
449 x_offset += cx1;
450 y_offset += cy1;
451 }
1f916a19 452
c219cecc 453#if defined(CTL3D) && (!CTL3D)
4438caf4
VZ
454 y_offset += (int)(cy1/2); // Fudge factor since buttons overlapped label
455 // JACS 2/12/93. CTL3D draws group label quite high.
1f916a19 456#endif
4438caf4
VZ
457 int startX = x_offset;
458 int startY = y_offset;
1f916a19 459
4438caf4 460 for ( i = 0 ; i < m_noItems; i++)
1f916a19 461 {
4438caf4
VZ
462 // Bidimensional radio adjustment
463 if (i&&((i%m_majorDim)==0)) // Why is this omitted for i = 0?
464 {
465 if (m_windowStyle & wxRA_VERTICAL)
466 {
467 y_offset = startY;
468 x_offset += maxWidth + cx1;
469 }
470 else
471 {
472 x_offset = startX;
473 y_offset += maxHeight;
474 if (m_radioWidth[0]>0)
475 y_offset += cy1/2;
476 }
477 }
478 int eachWidth;
479 int eachHeight;
480 if (m_radioWidth[i]<0)
481 {
482 // It's a labeled item
483 buf = wxGetWindowText(m_radioButtons[i]);
484 GetTextExtent(buf, &current_width, &cyf);
485
486 // How do we find out radio button bitmap size!!
487 // By adjusting them carefully, manually :-)
488 eachWidth = (int)(current_width + RADIO_SIZE);
489 eachHeight = (int)((3*cyf)/2);
490 }
491 else
492 {
493 eachWidth = m_radioWidth[i];
494 eachHeight = m_radioHeight[i];
495 }
1f916a19 496
4438caf4
VZ
497 MoveWindow((HWND)m_radioButtons[i], x_offset, y_offset,
498 eachWidth, eachHeight,
499 TRUE);
500
501 if (m_windowStyle & wxRA_SPECIFY_ROWS)
502 {
503 y_offset += maxHeight;
504 if (m_radioWidth[0]>0)
505 y_offset += cy1/2;
506 }
507 else
508 x_offset += maxWidth + cx1;
1f916a19 509 }
1f916a19
JS
510}
511
2bda0e17
KB
512void wxRadioBox::GetSize(int *width, int *height) const
513{
da07e033
VZ
514 RECT rect;
515 rect.left = -1; rect.right = -1; rect.top = -1; rect.bottom = -1;
2bda0e17 516
da07e033
VZ
517 if (m_hWnd)
518 wxFindMaxSize(m_hWnd, &rect);
2bda0e17 519
da07e033
VZ
520 int i;
521 for (i = 0; i < m_noItems; i++)
522 wxFindMaxSize(m_radioButtons[i], &rect);
2bda0e17 523
da07e033
VZ
524 *width = rect.right - rect.left;
525 *height = rect.bottom - rect.top;
2bda0e17
KB
526}
527
528void wxRadioBox::GetPosition(int *x, int *y) const
529{
da07e033
VZ
530 wxWindow *parent = GetParent();
531 RECT rect;
532 rect.left = -1; rect.right = -1; rect.top = -1; rect.bottom = -1;
533
534 int i;
535 for (i = 0; i < m_noItems; i++)
536 wxFindMaxSize(m_radioButtons[i], &rect);
537
538 if (m_hWnd)
539 wxFindMaxSize(m_hWnd, &rect);
540
541 // Since we now have the absolute screen coords,
542 // if there's a parent we must subtract its top left corner
543 POINT point;
544 point.x = rect.left;
545 point.y = rect.top;
546 if (parent)
547 {
548 ::ScreenToClient((HWND) parent->GetHWND(), &point);
549 }
550 // We may be faking the client origin.
551 // So a window that's really at (0, 30) may appear
552 // (to wxWin apps) to be at (0, 0).
553 if (GetParent())
554 {
555 wxPoint pt(GetParent()->GetClientAreaOrigin());
556 point.x -= pt.x;
557 point.y -= pt.y;
558 }
559
560 *x = point.x;
561 *y = point.y;
2bda0e17
KB
562}
563
e373f51b 564void wxRadioBox::SetFocus()
2bda0e17 565{
da07e033
VZ
566 if (m_noItems > 0)
567 {
568 if (m_selectedButton == -1)
569 ::SetFocus((HWND) m_radioButtons[0]);
570 else
571 ::SetFocus((HWND) m_radioButtons[m_selectedButton]);
572 }
2bda0e17
KB
573
574}
575
debe6624 576bool wxRadioBox::Show(bool show)
2bda0e17 577{
42e69d6b
VZ
578 if ( !wxControl::Show(show) )
579 return FALSE;
580
581 int nCmdShow = show ? SW_SHOW : SW_HIDE;
582 for ( int i = 0; i < m_noItems; i++ )
583 {
584 ::ShowWindow((HWND)m_radioButtons[i], nCmdShow);
585 }
586
da07e033 587 return TRUE;
2bda0e17
KB
588}
589
590// Enable a specific button
debe6624 591void wxRadioBox::Enable(int item, bool enable)
2bda0e17 592{
42e69d6b
VZ
593 wxCHECK_RET( item >= 0 && item < m_noItems,
594 _T("invalid item in wxRadioBox::Enable()") );
595
596 ::EnableWindow((HWND) m_radioButtons[item], enable);
2bda0e17
KB
597}
598
599// Enable all controls
cc2b7472 600bool wxRadioBox::Enable(bool enable)
2bda0e17 601{
cc2b7472
VZ
602 if ( !wxControl::Enable(enable) )
603 return FALSE;
1eb20d4a 604
42e69d6b 605 for (int i = 0; i < m_noItems; i++)
da07e033 606 ::EnableWindow((HWND) m_radioButtons[i], enable);
cc2b7472
VZ
607
608 return TRUE;
2bda0e17
KB
609}
610
611// Show a specific button
debe6624 612void wxRadioBox::Show(int item, bool show)
2bda0e17 613{
42e69d6b
VZ
614 wxCHECK_RET( item >= 0 && item < m_noItems,
615 _T("invalid item in wxRadioBox::Show()") );
616
617 ::ShowWindow((HWND)m_radioButtons[item], show ? SW_SHOW : SW_HIDE);
2bda0e17
KB
618}
619
debe6624 620WXHBRUSH wxRadioBox::OnCtlColor(WXHDC pDC, WXHWND pWnd, WXUINT nCtlColor,
da07e033 621 WXUINT message, WXWPARAM wParam, WXLPARAM lParam)
2bda0e17 622{
1f112209 623#if wxUSE_CTL3D
da07e033
VZ
624 if ( m_useCtl3D )
625 {
626 HBRUSH hbrush = Ctl3dCtlColorEx(message, wParam, lParam);
627 return (WXHBRUSH) hbrush;
628 }
2bda0e17
KB
629#endif
630
da07e033
VZ
631 if (GetParent()->GetTransparentBackground())
632 SetBkMode((HDC) pDC, TRANSPARENT);
633 else
634 SetBkMode((HDC) pDC, OPAQUE);
2bda0e17 635
da07e033
VZ
636 ::SetBkColor((HDC) pDC, RGB(GetBackgroundColour().Red(), GetBackgroundColour().Green(), GetBackgroundColour().Blue()));
637 ::SetTextColor((HDC) pDC, RGB(GetForegroundColour().Red(), GetForegroundColour().Green(), GetForegroundColour().Blue()));
2bda0e17 638
da07e033 639 wxBrush *backgroundBrush = wxTheBrushList->FindOrCreateBrush(GetBackgroundColour(), wxSOLID);
2bda0e17 640
da07e033 641 return (WXHBRUSH) backgroundBrush->GetResourceHandle();
2bda0e17
KB
642}
643
644// For single selection items only
e373f51b 645wxString wxRadioBox::GetStringSelection() const
2bda0e17 646{
e373f51b
VZ
647 wxString result;
648 int sel = GetSelection();
da07e033 649 if (sel > -1)
e373f51b
VZ
650 result = GetString(sel);
651
652 return result;
2bda0e17
KB
653}
654
e373f51b 655bool wxRadioBox::SetStringSelection(const wxString& s)
2bda0e17 656{
da07e033
VZ
657 int sel = FindString (s);
658 if (sel > -1)
2bda0e17 659 {
da07e033
VZ
660 SetSelection (sel);
661 return TRUE;
2bda0e17 662 }
da07e033
VZ
663 else
664 return FALSE;
2bda0e17
KB
665}
666
2bda0e17
KB
667bool wxRadioBox::ContainsHWND(WXHWND hWnd) const
668{
da07e033 669 int i;
2bda0e17 670 for (i = 0; i < Number(); i++)
42e69d6b 671 {
da07e033
VZ
672 if (GetRadioButtons()[i] == hWnd)
673 return TRUE;
42e69d6b
VZ
674 }
675
da07e033 676 return FALSE;
2bda0e17
KB
677}
678
679void wxRadioBox::Command (wxCommandEvent & event)
680{
da07e033
VZ
681 SetSelection (event.m_commandInt);
682 ProcessCommand (event);
2bda0e17
KB
683}
684
42e69d6b 685long wxRadioBox::MSWWindowProc(WXUINT msg, WXWPARAM wParam, WXLPARAM lParam)
983162bd 686{
42e69d6b
VZ
687 long rc = 0;
688 bool processed = FALSE;
689
690 switch ( msg )
983162bd 691 {
42e69d6b
VZ
692 case WM_NCHITTEST:
693 {
694 int xPos = LOWORD(lParam); // horizontal position of cursor
695 int yPos = HIWORD(lParam); // vertical position of cursor
983162bd 696
42e69d6b 697 ScreenToClient(&xPos, &yPos);
983162bd 698
42e69d6b
VZ
699 // Make sure you can drag by the top of the groupbox, but let
700 // other (enclosed) controls get mouse events also
701 if ( yPos < 10 )
702 {
703 rc = HTCLIENT;
704 processed = TRUE;
705 }
706 }
707 break;
983162bd
JS
708 }
709
42e69d6b
VZ
710 if ( !processed )
711 rc = wxControl::MSWWindowProc(msg, wParam, lParam);
712
713 return rc;
983162bd 714}
2bda0e17 715
e373f51b
VZ
716void wxRadioBox::SubclassRadioButton(WXHWND hWndBtn)
717{
42e69d6b 718#ifdef __WIN32__
e373f51b
VZ
719 HWND hwndBtn = (HWND)hWndBtn;
720
721 if ( !s_wndprocRadioBtn )
20e85460 722 s_wndprocRadioBtn = (WXFARPROC)::GetWindowLong(hwndBtn, GWL_WNDPROC);
e373f51b 723
2a47d3c1 724 // No GWL_USERDATA in Win16, so omit this subclassing.
e373f51b
VZ
725 ::SetWindowLong(hwndBtn, GWL_WNDPROC, (long)wxRadioBtnWndProc);
726 ::SetWindowLong(hwndBtn, GWL_USERDATA, (long)this);
42e69d6b 727#endif // __WIN32__
e373f51b
VZ
728}
729
9a5ccab4
VZ
730void wxRadioBox::SendNotificationEvent()
731{
732 wxCommandEvent event(wxEVT_COMMAND_RADIOBOX_SELECTED, m_windowId);
733 event.SetInt( m_selectedButton );
734 event.SetEventObject( this );
735 ProcessCommand(event);
736}
737
e373f51b
VZ
738// ---------------------------------------------------------------------------
739// window proc for radio buttons
740// ---------------------------------------------------------------------------
741
2a47d3c1
JS
742#ifdef __WIN32__
743
e373f51b
VZ
744LRESULT APIENTRY _EXPORT wxRadioBtnWndProc(HWND hwnd,
745 UINT msg,
746 WPARAM wParam,
747 LPARAM lParam)
748{
749 bool processed = TRUE;
750 if ( msg != WM_KEYDOWN )
751 processed = FALSE;
752
753 if ( processed )
754 {
755 wxRadioBox *radiobox = (wxRadioBox *)::GetWindowLong(hwnd, GWL_USERDATA);
756
837e5743 757 wxCHECK_MSG( radiobox, 0, _T("radio button without radio box?") );
e373f51b
VZ
758
759 int sel = radiobox->GetSelection();
760
761 switch ( wParam )
762 {
763 case VK_UP:
764 sel--;
765 break;
766
767 case VK_LEFT:
768 sel -= radiobox->GetNumVer();
769 break;
770
771 case VK_DOWN:
772 sel++;
773 break;
774
775 case VK_RIGHT:
776 sel += radiobox->GetNumVer();
777 break;
778
779 case VK_TAB:
780 {
781 wxNavigationKeyEvent event;
782 event.SetDirection(!(::GetKeyState(VK_SHIFT) & 0x100));
783 event.SetWindowChange(FALSE);
784 event.SetEventObject(radiobox);
785
786 if ( radiobox->GetEventHandler()->ProcessEvent(event) )
787 return 0;
788 }
789 // fall through
790
791 default:
792 processed = FALSE;
793 }
794
795 if ( processed )
796 {
797 if ( sel >= 0 && sel < radiobox->Number() )
9a5ccab4 798 {
e373f51b 799 radiobox->SetSelection(sel);
9a5ccab4
VZ
800
801 // emulate the button click
802 radiobox->SendNotificationEvent();
803 }
e373f51b
VZ
804 }
805 }
806
807 if ( !processed )
20e85460 808 return ::CallWindowProc(CASTWNDPROC s_wndprocRadioBtn, hwnd, msg, wParam, lParam);
e373f51b
VZ
809 else
810 return 0;
811}
42e69d6b
VZ
812
813#endif // __WIN32__
e373f51b 814