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