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