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