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