]> git.saurik.com Git - wxWidgets.git/blob - src/msw/radiobox.cpp
avoiding a OS-Level set focus for a control that already has the focus
[wxWidgets.git] / src / msw / radiobox.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: msw/radiobox.cpp
3 // Purpose: wxRadioBox implementation
4 // Author: Julian Smart
5 // Modified by:
6 // Created: 04/01/98
7 // RCS-ID: $Id$
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 // ===========================================================================
13 // declarations
14 // ===========================================================================
15
16 // ---------------------------------------------------------------------------
17 // headers
18 // ---------------------------------------------------------------------------
19
20 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
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 #if wxUSE_RADIOBOX
32
33 #ifndef WX_PRECOMP
34 #include "wx/bitmap.h"
35 #include "wx/brush.h"
36 #include "wx/radiobox.h"
37 #include "wx/settings.h"
38 #include "wx/log.h"
39 #endif
40
41 #include "wx/msw/private.h"
42
43 #if wxUSE_TOOLTIPS
44 #if !defined(__GNUWIN32_OLD__) || defined(__CYGWIN10__)
45 #include <commctrl.h>
46 #endif
47 #include "wx/tooltip.h"
48 #endif // wxUSE_TOOLTIPS
49
50 // TODO: wxCONSTRUCTOR
51 #if 0 // wxUSE_EXTENDED_RTTI
52 WX_DEFINE_FLAGS( wxRadioBoxStyle )
53
54 wxBEGIN_FLAGS( wxRadioBoxStyle )
55 // new style border flags, we put them first to
56 // use them for streaming out
57 wxFLAGS_MEMBER(wxBORDER_SIMPLE)
58 wxFLAGS_MEMBER(wxBORDER_SUNKEN)
59 wxFLAGS_MEMBER(wxBORDER_DOUBLE)
60 wxFLAGS_MEMBER(wxBORDER_RAISED)
61 wxFLAGS_MEMBER(wxBORDER_STATIC)
62 wxFLAGS_MEMBER(wxBORDER_NONE)
63
64 // old style border flags
65 wxFLAGS_MEMBER(wxSIMPLE_BORDER)
66 wxFLAGS_MEMBER(wxSUNKEN_BORDER)
67 wxFLAGS_MEMBER(wxDOUBLE_BORDER)
68 wxFLAGS_MEMBER(wxRAISED_BORDER)
69 wxFLAGS_MEMBER(wxSTATIC_BORDER)
70 wxFLAGS_MEMBER(wxBORDER)
71
72 // standard window styles
73 wxFLAGS_MEMBER(wxTAB_TRAVERSAL)
74 wxFLAGS_MEMBER(wxCLIP_CHILDREN)
75 wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW)
76 wxFLAGS_MEMBER(wxWANTS_CHARS)
77 wxFLAGS_MEMBER(wxFULL_REPAINT_ON_RESIZE)
78 wxFLAGS_MEMBER(wxALWAYS_SHOW_SB )
79 wxFLAGS_MEMBER(wxVSCROLL)
80 wxFLAGS_MEMBER(wxHSCROLL)
81
82 wxFLAGS_MEMBER(wxRA_SPECIFY_COLS)
83 wxFLAGS_MEMBER(wxRA_HORIZONTAL)
84 wxFLAGS_MEMBER(wxRA_SPECIFY_ROWS)
85 wxFLAGS_MEMBER(wxRA_VERTICAL)
86
87 wxEND_FLAGS( wxRadioBoxStyle )
88
89 IMPLEMENT_DYNAMIC_CLASS_XTI(wxRadioBox, wxControl,"wx/radiobox.h")
90
91 wxBEGIN_PROPERTIES_TABLE(wxRadioBox)
92 wxEVENT_PROPERTY( Select , wxEVT_COMMAND_RADIOBOX_SELECTED , wxCommandEvent )
93 wxPROPERTY_FLAGS( WindowStyle , wxRadioBoxStyle , long , SetWindowStyleFlag , GetWindowStyleFlag , , 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style
94 wxEND_PROPERTIES_TABLE()
95
96 #else
97 IMPLEMENT_DYNAMIC_CLASS(wxRadioBox, wxControl)
98 #endif
99
100 /*
101 selection
102 content
103 label
104 dimension
105 item
106 */
107
108 // there are two possible ways to create the radio buttons: either as children
109 // of the radiobox or as siblings of it - allow playing with both variants for
110 // now, eventually we will choose the best one for our purposes
111 //
112 // two main problems are the keyboard navigation inside the radiobox (arrows
113 // should switch between buttons, not pass focus to the next control) and the
114 // tooltips - a tooltip is associated with the radiobox itself, not the
115 // children...
116 //
117 // the problems with setting this to 1:
118 // a) Alt-<mnemonic of radiobox> isn't handled properly by IsDialogMessage()
119 // because it sets focus to the next control accepting it which is not a
120 // radio button but a radiobox sibling in this case - the only solution to
121 // this would be to handle Alt-<mnemonic> ourselves
122 // b) the problems with setting radiobox colours under Win98/2K were reported
123 // but I couldn't reproduce it so I have no idea about what causes it
124 //
125 // the problems with setting this to 0:
126 // a) the tooltips are not shown for the radiobox - possible solution: make
127 // TTM_WINDOWFROMPOS handling code in msw/tooltip.cpp work (easier said than
128 // done because I don't know why it doesn't work)
129 #define RADIOBTN_PARENT_IS_RADIOBOX 0
130
131 // ---------------------------------------------------------------------------
132 // private functions
133 // ---------------------------------------------------------------------------
134
135 // wnd proc for radio buttons
136 #ifdef __WIN32__
137 LRESULT APIENTRY _EXPORT wxRadioBtnWndProc(HWND hWnd,
138 UINT message,
139 WPARAM wParam,
140 LPARAM lParam);
141
142 // ---------------------------------------------------------------------------
143 // global vars
144 // ---------------------------------------------------------------------------
145
146 // the pointer to standard radio button wnd proc
147 static WXFARPROC s_wndprocRadioBtn = (WXFARPROC)NULL;
148
149 #endif // __WIN32__
150
151 // ===========================================================================
152 // implementation
153 // ===========================================================================
154
155 // ---------------------------------------------------------------------------
156 // wxRadioBox
157 // ---------------------------------------------------------------------------
158
159 int wxRadioBox::GetCount() const
160 {
161 return m_noItems;
162 }
163
164 int wxRadioBox::GetColumnCount() const
165 {
166 return GetNumHor();
167 }
168
169 int wxRadioBox::GetRowCount() const
170 {
171 return GetNumVer();
172 }
173
174 // returns the number of rows
175 int wxRadioBox::GetNumVer() const
176 {
177 if ( m_windowStyle & wxRA_SPECIFY_ROWS )
178 {
179 return m_majorDim;
180 }
181 else
182 {
183 return (m_noItems + m_majorDim - 1)/m_majorDim;
184 }
185 }
186
187 // returns the number of columns
188 int wxRadioBox::GetNumHor() const
189 {
190 if ( m_windowStyle & wxRA_SPECIFY_ROWS )
191 {
192 return (m_noItems + m_majorDim - 1)/m_majorDim;
193 }
194 else
195 {
196 return m_majorDim;
197 }
198 }
199
200 bool wxRadioBox::MSWCommand(WXUINT cmd, WXWORD id)
201 {
202 if ( cmd == BN_CLICKED )
203 {
204 if (id == GetId())
205 return TRUE;
206
207 int selectedButton = -1;
208
209 for ( int i = 0; i < m_noItems; i++ )
210 {
211 if ( id == wxGetWindowId(m_radioButtons[i]) )
212 {
213 selectedButton = i;
214
215 break;
216 }
217 }
218
219 if ( selectedButton == -1 )
220 {
221 // just ignore it - due to a hack with WM_NCHITTEST handling in our
222 // wnd proc, we can receive dummy click messages when we click near
223 // the radiobox edge (this is ugly but Julian wouldn't let me get
224 // rid of this...)
225 return FALSE;
226 }
227
228 if ( selectedButton != m_selectedButton )
229 {
230 m_selectedButton = selectedButton;
231
232 SendNotificationEvent();
233 }
234 //else: don't generate events when the selection doesn't change
235
236 return TRUE;
237 }
238 else
239 return FALSE;
240 }
241
242 // Radio box item
243 wxRadioBox::wxRadioBox()
244 {
245 m_selectedButton = -1;
246 m_noItems = 0;
247 m_noRowsOrCols = 0;
248 m_radioButtons = NULL;
249 m_majorDim = 0;
250 m_radioWidth = NULL;
251 m_radioHeight = NULL;
252 }
253
254 bool wxRadioBox::Create(wxWindow *parent,
255 wxWindowID id,
256 const wxString& title,
257 const wxPoint& pos,
258 const wxSize& size,
259 int n,
260 const wxString choices[],
261 int majorDim,
262 long style,
263 const wxValidator& val,
264 const wxString& name)
265 {
266 // initialize members
267 m_selectedButton = -1;
268 m_noItems = 0;
269
270 m_majorDim = majorDim == 0 ? n : majorDim;
271 m_noRowsOrCols = majorDim;
272
273 // common initialization
274 if ( !CreateControl(parent, id, pos, size, style, val, name) )
275 return FALSE;
276
277 // create the static box
278 if ( !MSWCreateControl(wxT("BUTTON"), BS_GROUPBOX | WS_GROUP,
279 pos, size, title, 0) )
280 return FALSE;
281
282 // and now create the buttons
283 m_noItems = n;
284 #if RADIOBTN_PARENT_IS_RADIOBOX
285 HWND hwndParent = GetHwnd();
286 #else
287 HWND hwndParent = GetHwndOf(parent);
288 #endif
289
290 // Some radio boxes test consecutive id.
291 (void)NewControlId();
292 m_radioButtons = new WXHWND[n];
293 m_radioWidth = new int[n];
294 m_radioHeight = new int[n];
295
296 WXHFONT hfont = 0;
297 wxFont font = GetFont();
298 if ( font.Ok() )
299 {
300 hfont = font.GetResourceHandle();
301 }
302
303 for ( int i = 0; i < n; i++ )
304 {
305 m_radioWidth[i] =
306 m_radioHeight[i] = -1;
307 long styleBtn = BS_AUTORADIOBUTTON | WS_TABSTOP | WS_CHILD | WS_VISIBLE;
308 if ( i == 0 && style == 0 )
309 styleBtn |= WS_GROUP;
310
311 long newId = NewControlId();
312
313 HWND hwndBtn = ::CreateWindow(_T("BUTTON"),
314 choices[i],
315 styleBtn,
316 0, 0, 0, 0, // will be set in SetSize()
317 hwndParent,
318 (HMENU)newId,
319 wxGetInstance(),
320 NULL);
321
322 if ( !hwndBtn )
323 {
324 wxLogLastError(wxT("CreateWindow(radio btn)"));
325
326 return FALSE;
327 }
328
329 m_radioButtons[i] = (WXHWND)hwndBtn;
330
331 SubclassRadioButton((WXHWND)hwndBtn);
332
333 if ( hfont )
334 {
335 ::SendMessage(hwndBtn, WM_SETFONT, (WPARAM)hfont, 0L);
336 }
337
338 m_subControls.Add(newId);
339 }
340
341 // Create a dummy radio control to end the group.
342 (void)::CreateWindow(_T("BUTTON"),
343 wxEmptyString,
344 WS_GROUP | BS_AUTORADIOBUTTON | WS_CHILD,
345 0, 0, 0, 0, hwndParent,
346 (HMENU)NewControlId(), wxGetInstance(), NULL);
347
348 #ifdef __WXWINCE__
349 // Set the z-order correctly
350 SetWindowPos(GetHwnd(), HWND_BOTTOM, 0, 0, 0, 0, SWP_NOMOVE|SWP_NOSIZE);
351 #endif
352
353 SetSelection(0);
354 SetSize(pos.x, pos.y, size.x, size.y);
355
356 // Now that we have items determine what is the best size and set it.
357 SetBestSize(size);
358
359 return TRUE;
360 }
361
362 bool wxRadioBox::Create(wxWindow *parent,
363 wxWindowID id,
364 const wxString& title,
365 const wxPoint& pos,
366 const wxSize& size,
367 const wxArrayString& choices,
368 int majorDim,
369 long style,
370 const wxValidator& val,
371 const wxString& name)
372 {
373 wxCArrayString chs(choices);
374 return Create(parent, id, title, pos, size, chs.GetCount(),
375 chs.GetStrings(), majorDim, style, val, name);
376 }
377
378 wxRadioBox::~wxRadioBox()
379 {
380 m_isBeingDeleted = TRUE;
381
382 if (m_radioButtons)
383 {
384 int i;
385 for (i = 0; i < m_noItems; i++)
386 ::DestroyWindow((HWND)m_radioButtons[i]);
387 delete[] m_radioButtons;
388 }
389
390 if (m_radioWidth)
391 delete[] m_radioWidth;
392 if (m_radioHeight)
393 delete[] m_radioHeight;
394
395 }
396
397 void wxRadioBox::SetString(int item, const wxString& label)
398 {
399 wxCHECK_RET( item >= 0 && item < m_noItems, wxT("invalid radiobox index") );
400
401 m_radioWidth[item] = m_radioHeight[item] = -1;
402 SetWindowText((HWND)m_radioButtons[item], label.c_str());
403 }
404
405 void wxRadioBox::SetSelection(int N)
406 {
407 wxCHECK_RET( (N >= 0) && (N < m_noItems), wxT("invalid radiobox index") );
408
409 // Following necessary for Win32s, because Win32s translate BM_SETCHECK
410 if (m_selectedButton >= 0 && m_selectedButton < m_noItems)
411 ::SendMessage((HWND) m_radioButtons[m_selectedButton], BM_SETCHECK, 0, 0L);
412
413 ::SendMessage((HWND)m_radioButtons[N], BM_SETCHECK, 1, 0L);
414
415 m_selectedButton = N;
416 }
417
418 // Get single selection, for single choice list items
419 int wxRadioBox::GetSelection() const
420 {
421 return m_selectedButton;
422 }
423
424 // Find string for position
425 wxString wxRadioBox::GetString(int item) const
426 {
427 wxCHECK_MSG( item >= 0 && item < m_noItems, wxEmptyString,
428 wxT("invalid radiobox index") );
429
430 return wxGetWindowText(m_radioButtons[item]);
431 }
432
433 // ----------------------------------------------------------------------------
434 // size calculations
435 // ----------------------------------------------------------------------------
436
437 wxSize wxRadioBox::GetMaxButtonSize() const
438 {
439 // calculate the max button size
440 int widthMax = 0,
441 heightMax = 0;
442 for ( int i = 0 ; i < m_noItems; i++ )
443 {
444 int width, height;
445 if ( m_radioWidth[i] < 0 )
446 {
447 GetTextExtent(wxGetWindowText(m_radioButtons[i]), &width, &height);
448
449 // adjust the size to take into account the radio box itself
450 // FIXME this is totally bogus!
451 width += RADIO_SIZE;
452 height *= 3;
453 height /= 2;
454 }
455 else
456 {
457 width = m_radioWidth[i];
458 height = m_radioHeight[i];
459 }
460
461 if ( widthMax < width )
462 widthMax = width;
463 if ( heightMax < height )
464 heightMax = height;
465 }
466
467 return wxSize(widthMax, heightMax);
468 }
469
470 wxSize wxRadioBox::GetTotalButtonSize(const wxSize& sizeBtn) const
471 {
472 // the radiobox should be big enough for its buttons
473 int cx1, cy1;
474 wxGetCharSize(m_hWnd, &cx1, &cy1, GetFont());
475
476 int extraHeight = cy1;
477
478 /* We'll assume the adjustments below are OK for Win 3.1 too
479 #if defined(CTL3D) && !CTL3D
480 // Requires a bigger group box in plain Windows
481 extraHeight *= 3;
482 extraHeight /= 2;
483 #endif
484 */
485
486 int height = GetNumVer() * sizeBtn.y + cy1/2 + extraHeight;
487 int width = GetNumHor() * (sizeBtn.x + cx1) + cx1;
488
489 // Add extra space under the label, if it exists.
490 if (!wxControl::GetLabel().IsEmpty())
491 height += cy1/2;
492
493 // and also wide enough for its label
494 int widthLabel;
495 GetTextExtent(GetTitle(), &widthLabel, NULL);
496 widthLabel += RADIO_SIZE; // FIXME this is bogus too
497 if ( widthLabel > width )
498 width = widthLabel;
499
500 return wxSize(width, height);
501 }
502
503 wxSize wxRadioBox::DoGetBestSize() const
504 {
505 return GetTotalButtonSize(GetMaxButtonSize());
506 }
507
508 // Restored old code.
509 void wxRadioBox::DoSetSize(int x, int y, int width, int height, int sizeFlags)
510 {
511 int currentX, currentY;
512 GetPosition(&currentX, &currentY);
513 int widthOld, heightOld;
514 GetSize(&widthOld, &heightOld);
515
516 int xx = x;
517 int yy = y;
518
519 if (x == -1 && !(sizeFlags & wxSIZE_ALLOW_MINUS_ONE))
520 xx = currentX;
521 if (y == -1 && !(sizeFlags & wxSIZE_ALLOW_MINUS_ONE))
522 yy = currentY;
523
524 #if RADIOBTN_PARENT_IS_RADIOBOX
525 int y_offset = 0;
526 int x_offset = 0;
527 #else
528 int y_offset = yy;
529 int x_offset = xx;
530 #endif
531
532 int cx1, cy1;
533 wxGetCharSize(m_hWnd, &cx1, &cy1, GetFont());
534
535 // Attempt to have a look coherent with other platforms: We compute the
536 // biggest toggle dim, then we align all items according this value.
537 wxSize maxSize = GetMaxButtonSize();
538 int maxWidth = maxSize.x,
539 maxHeight = maxSize.y;
540
541 wxSize totSize = GetTotalButtonSize(maxSize);
542 int totWidth = totSize.x,
543 totHeight = totSize.y;
544
545 // only change our width/height if asked for
546 if ( width == -1 )
547 {
548 if ( sizeFlags & wxSIZE_AUTO_WIDTH )
549 width = totWidth;
550 else
551 width = widthOld;
552 }
553
554 if ( height == -1 )
555 {
556 if ( sizeFlags & wxSIZE_AUTO_HEIGHT )
557 height = totHeight;
558 else
559 height = heightOld;
560 }
561
562 ::MoveWindow(GetHwnd(), xx, yy, width, height, TRUE);
563
564 // Now position all the buttons: the current button will be put at
565 // wxPoint(x_offset, y_offset) and the new row/column will start at
566 // startX/startY. The size of all buttons will be the same wxSize(maxWidth,
567 // maxHeight) except for the buttons in the last column which should extend
568 // to the right border of radiobox and thus can be wider than this.
569
570 // Also, remember that wxRA_SPECIFY_COLS means that we arrange buttons in
571 // left to right order and m_majorDim is the number of columns while
572 // wxRA_SPECIFY_ROWS means that the buttons are arranged top to bottom and
573 // m_majorDim is the number of rows.
574
575 x_offset += cx1;
576 y_offset += cy1;
577
578 // Add extra space under the label, if it exists.
579 if (!wxControl::GetLabel().IsEmpty())
580 y_offset += cy1/2;
581
582 int startX = x_offset;
583 int startY = y_offset;
584
585 for ( int i = 0; i < m_noItems; i++ )
586 {
587 // the last button in the row may be wider than the other ones as the
588 // radiobox may be wider than the sum of the button widths (as it
589 // happens, for example, when the radiobox label is very long)
590 bool isLastInTheRow;
591 if ( m_windowStyle & wxRA_SPECIFY_COLS )
592 {
593 // item is the last in its row if it is a multiple of the number of
594 // columns or if it is just the last item
595 int n = i + 1;
596 isLastInTheRow = ((n % m_majorDim) == 0) || (n == m_noItems);
597 }
598 else // wxRA_SPECIFY_ROWS
599 {
600 // item is the last in the row if it is in the last columns
601 isLastInTheRow = i >= (m_noItems/m_majorDim)*m_majorDim;
602 }
603
604 // is this the start of new row/column?
605 if ( i && (i % m_majorDim == 0) )
606 {
607 if ( m_windowStyle & wxRA_SPECIFY_ROWS )
608 {
609 // start of new column
610 y_offset = startY;
611 x_offset += maxWidth + cx1;
612 }
613 else // start of new row
614 {
615 x_offset = startX;
616 y_offset += maxHeight;
617 if (m_radioWidth[0]>0)
618 y_offset += cy1/2;
619 }
620 }
621
622 int widthBtn;
623 if ( isLastInTheRow )
624 {
625 // make the button go to the end of radio box
626 widthBtn = startX + width - x_offset - 2*cx1;
627 if ( widthBtn < maxWidth )
628 widthBtn = maxWidth;
629 }
630 else
631 {
632 // normal button, always of the same size
633 widthBtn = maxWidth;
634 }
635
636 // VZ: make all buttons of the same, maximal size - like this they
637 // cover the radiobox entirely and the radiobox tooltips are always
638 // shown (otherwise they are not when the mouse pointer is in the
639 // radiobox part not belonging to any radiobutton)
640 ::MoveWindow((HWND)m_radioButtons[i],
641 x_offset, y_offset, widthBtn, maxHeight,
642 TRUE);
643
644 // where do we put the next button?
645 if ( m_windowStyle & wxRA_SPECIFY_ROWS )
646 {
647 // below this one
648 y_offset += maxHeight;
649 if (m_radioWidth[0]>0)
650 y_offset += cy1/2;
651 }
652 else
653 {
654 // to the right of this one
655 x_offset += widthBtn + cx1;
656 }
657 }
658 }
659
660 void wxRadioBox::GetSize(int *width, int *height) const
661 {
662 RECT rect;
663 rect.left = -1; rect.right = -1; rect.top = -1; rect.bottom = -1;
664
665 if (m_hWnd)
666 wxFindMaxSize(m_hWnd, &rect);
667
668 int i;
669 for (i = 0; i < m_noItems; i++)
670 wxFindMaxSize(m_radioButtons[i], &rect);
671
672 *width = rect.right - rect.left;
673 *height = rect.bottom - rect.top;
674 }
675
676 void wxRadioBox::GetPosition(int *x, int *y) const
677 {
678 wxWindow *parent = GetParent();
679 RECT rect = { -1, -1, -1, -1 };
680
681 int i;
682 for (i = 0; i < m_noItems; i++)
683 wxFindMaxSize(m_radioButtons[i], &rect);
684
685 if (m_hWnd)
686 wxFindMaxSize(m_hWnd, &rect);
687
688 // Since we now have the absolute screen coords, if there's a parent we
689 // must subtract its top left corner
690 POINT point;
691 point.x = rect.left;
692 point.y = rect.top;
693 if (parent)
694 {
695 ::ScreenToClient((HWND) parent->GetHWND(), &point);
696 }
697
698 // We may be faking the client origin. So a window that's really at (0, 30)
699 // may appear (to wxWin apps) to be at (0, 0).
700 if (GetParent())
701 {
702 wxPoint pt(GetParent()->GetClientAreaOrigin());
703 point.x -= pt.x;
704 point.y -= pt.y;
705 }
706
707 *x = point.x;
708 *y = point.y;
709 }
710
711 void wxRadioBox::SetFocus()
712 {
713 if (m_noItems > 0)
714 {
715 ::SetFocus((HWND)m_radioButtons[m_selectedButton == -1
716 ? 0
717 : m_selectedButton]);
718 }
719
720 }
721
722 bool wxRadioBox::Show(bool show)
723 {
724 if ( !wxControl::Show(show) )
725 return FALSE;
726
727 int nCmdShow = show ? SW_SHOW : SW_HIDE;
728 for ( int i = 0; i < m_noItems; i++ )
729 {
730 ::ShowWindow((HWND)m_radioButtons[i], nCmdShow);
731 }
732
733 return TRUE;
734 }
735
736 // Enable a specific button
737 void wxRadioBox::Enable(int item, bool enable)
738 {
739 wxCHECK_RET( item >= 0 && item < m_noItems,
740 wxT("invalid item in wxRadioBox::Enable()") );
741
742 ::EnableWindow((HWND) m_radioButtons[item], enable);
743 }
744
745 // Enable all controls
746 bool wxRadioBox::Enable(bool enable)
747 {
748 if ( !wxControl::Enable(enable) )
749 return FALSE;
750
751 for (int i = 0; i < m_noItems; i++)
752 ::EnableWindow((HWND) m_radioButtons[i], enable);
753
754 return TRUE;
755 }
756
757 // Show a specific button
758 void wxRadioBox::Show(int item, bool show)
759 {
760 wxCHECK_RET( item >= 0 && item < m_noItems,
761 wxT("invalid item in wxRadioBox::Show()") );
762
763 ::ShowWindow((HWND)m_radioButtons[item], show ? SW_SHOW : SW_HIDE);
764 }
765
766 bool wxRadioBox::ContainsHWND(WXHWND hWnd) const
767 {
768 size_t count = GetCount();
769 for ( size_t i = 0; i < count; i++ )
770 {
771 if ( GetRadioButtons()[i] == hWnd )
772 return TRUE;
773 }
774
775 return FALSE;
776 }
777
778 void wxRadioBox::Command(wxCommandEvent & event)
779 {
780 SetSelection (event.m_commandInt);
781 SetFocus();
782 ProcessCommand (event);
783 }
784
785 // NB: if this code is changed, wxGetWindowForHWND() which relies on having the
786 // radiobox pointer in GWL_USERDATA for radio buttons must be updated too!
787 void wxRadioBox::SubclassRadioButton(WXHWND hWndBtn)
788 {
789 HWND hwndBtn = (HWND)hWndBtn;
790
791 if ( !s_wndprocRadioBtn )
792 s_wndprocRadioBtn = (WXFARPROC)wxGetWindowProc(hwndBtn);
793
794 wxSetWindowProc(hwndBtn, wxRadioBtnWndProc);
795 wxSetWindowUserData(hwndBtn, this);
796 }
797
798 void wxRadioBox::SendNotificationEvent()
799 {
800 wxCommandEvent event(wxEVT_COMMAND_RADIOBOX_SELECTED, m_windowId);
801 event.SetInt( m_selectedButton );
802 event.SetString( GetString(m_selectedButton) );
803 event.SetEventObject( this );
804 ProcessCommand(event);
805 }
806
807 bool wxRadioBox::SetFont(const wxFont& font)
808 {
809 if ( !wxControl::SetFont(font) )
810 {
811 // nothing to do
812 return FALSE;
813 }
814
815 // also set the font of our radio buttons
816 WXHFONT hfont = wxFont(font).GetResourceHandle();
817 for ( int n = 0; n < m_noItems; n++ )
818 {
819 HWND hwndBtn = (HWND)m_radioButtons[n];
820 ::SendMessage(hwndBtn, WM_SETFONT, (WPARAM)hfont, 0L);
821
822 // otherwise the buttons are not redrawn correctly
823 ::InvalidateRect(hwndBtn, NULL, FALSE /* don't erase bg */);
824 }
825
826 return TRUE;
827 }
828
829 // ----------------------------------------------------------------------------
830 // our window proc
831 // ----------------------------------------------------------------------------
832
833 WXLRESULT wxRadioBox::MSWWindowProc(WXUINT nMsg, WXWPARAM wParam, WXLPARAM lParam)
834 {
835 switch ( nMsg )
836 {
837 #ifdef __WIN32__
838 case WM_CTLCOLORSTATIC:
839 // set the colour of the radio buttons to be the same as ours
840 {
841 HDC hdc = (HDC)wParam;
842
843 const wxColour& colBack = GetBackgroundColour();
844 ::SetBkColor(hdc, wxColourToRGB(colBack));
845 ::SetTextColor(hdc, wxColourToRGB(GetForegroundColour()));
846
847 wxBrush *brush = wxTheBrushList->FindOrCreateBrush(colBack, wxSOLID);
848
849 return (WXLRESULT)brush->GetResourceHandle();
850 }
851 #endif // Win32
852
853 // VZ: this code breaks radiobox redrawing under Windows XP, don't use
854 // it. If you need to get messages from the static controls,
855 // create them as a child of another (non static) window
856 #if 0
857 // This is required for the radiobox to be sensitive to mouse input,
858 // e.g. for Dialog Editor.
859 case WM_NCHITTEST:
860 {
861 int xPos = LOWORD(lParam); // horizontal position of cursor
862 int yPos = HIWORD(lParam); // vertical position of cursor
863
864 ScreenToClient(&xPos, &yPos);
865
866 // Make sure you can drag by the top of the groupbox, but let
867 // other (enclosed) controls get mouse events also
868 if (yPos < 10)
869 return (long)HTCLIENT;
870 }
871 break;
872 #endif // 0
873 }
874
875 return wxControl::MSWWindowProc(nMsg, wParam, lParam);
876 }
877
878 WXHBRUSH wxRadioBox::OnCtlColor(WXHDC pDC, WXHWND WXUNUSED(pWnd), WXUINT WXUNUSED(nCtlColor),
879 #if wxUSE_CTL3D
880 WXUINT message,
881 WXWPARAM wParam,
882 WXLPARAM lParam
883 #else
884 WXUINT WXUNUSED(message),
885 WXWPARAM WXUNUSED(wParam),
886 WXLPARAM WXUNUSED(lParam)
887 #endif
888 )
889 {
890 #if wxUSE_CTL3D
891 if ( m_useCtl3D )
892 {
893 HBRUSH hbrush = Ctl3dCtlColorEx(message, wParam, lParam);
894 return (WXHBRUSH) hbrush;
895 }
896 #endif // wxUSE_CTL3D
897
898 HDC hdc = (HDC)pDC;
899 wxColour colBack = GetBackgroundColour();
900
901 ::SetBkColor(hdc, wxColourToRGB(colBack));
902 ::SetTextColor(hdc, wxColourToRGB(GetForegroundColour()));
903
904 wxBrush *brush = wxTheBrushList->FindOrCreateBrush(colBack, wxSOLID);
905
906 return (WXHBRUSH)brush->GetResourceHandle();
907 }
908
909
910 // ---------------------------------------------------------------------------
911 // window proc for radio buttons
912 // ---------------------------------------------------------------------------
913
914 #ifdef __WIN32__
915
916 LRESULT APIENTRY _EXPORT wxRadioBtnWndProc(HWND hwnd,
917 UINT message,
918 WPARAM wParam,
919 LPARAM lParam)
920 {
921 switch ( message )
922 {
923 case WM_GETDLGCODE:
924 // we must tell IsDialogMessage()/our kbd processing code that we
925 // want to process arrows ourselves because neither of them is
926 // smart enough to handle arrows properly for us
927 {
928 long lDlgCode = ::CallWindowProc(CASTWNDPROC s_wndprocRadioBtn, hwnd,
929 message, wParam, lParam);
930
931 return lDlgCode | DLGC_WANTARROWS;
932 }
933
934 #if wxUSE_TOOLTIPS
935 case WM_NOTIFY:
936 {
937 NMHDR* hdr = (NMHDR *)lParam;
938 if ( hdr->code == TTN_NEEDTEXT )
939 {
940 wxRadioBox *
941 radiobox = (wxRadioBox *)wxGetWindowUserData(hwnd);
942
943 wxCHECK_MSG( radiobox, 0,
944 wxT("radio button without radio box?") );
945
946 wxToolTip *tooltip = radiobox->GetToolTip();
947 if ( tooltip )
948 {
949 TOOLTIPTEXT *ttt = (TOOLTIPTEXT *)lParam;
950 ttt->lpszText = (wxChar *)tooltip->GetTip().c_str();
951 }
952
953 // processed
954 return 0;
955 }
956 }
957 break;
958 #endif // wxUSE_TOOLTIPS
959
960 case WM_KEYDOWN:
961 {
962 wxRadioBox *radiobox = (wxRadioBox *)wxGetWindowUserData(hwnd);
963
964 wxCHECK_MSG( radiobox, 0, wxT("radio button without radio box?") );
965
966 bool processed = TRUE;
967
968 wxDirection dir;
969 switch ( wParam )
970 {
971 case VK_UP:
972 dir = wxUP;
973 break;
974
975 case VK_LEFT:
976 dir = wxLEFT;
977 break;
978
979 case VK_DOWN:
980 dir = wxDOWN;
981 break;
982
983 case VK_RIGHT:
984 dir = wxRIGHT;
985 break;
986
987 default:
988 processed = FALSE;
989
990 // just to suppress the compiler warning
991 dir = wxALL;
992 }
993
994 if ( processed )
995 {
996 int selOld = radiobox->GetSelection();
997 int selNew = radiobox->GetNextItem
998 (
999 selOld,
1000 dir,
1001 radiobox->GetWindowStyle()
1002 );
1003
1004 if ( selNew != selOld )
1005 {
1006 radiobox->SetSelection(selNew);
1007 radiobox->SetFocus();
1008
1009 // emulate the button click
1010 radiobox->SendNotificationEvent();
1011
1012 return 0;
1013 }
1014 }
1015 }
1016 break;
1017
1018 case WM_SETFOCUS:
1019 case WM_KILLFOCUS:
1020 {
1021 wxRadioBox *radiobox = (wxRadioBox *)wxGetWindowUserData(hwnd);
1022
1023 wxCHECK_MSG( radiobox, 0, wxT("radio button without radio box?") );
1024
1025 // if we don't do this, no focus events are generated for the
1026 // radiobox and, besides, we need to notify the parent about
1027 // the focus change, otherwise the focus handling logic in
1028 // wxControlContainer doesn't work
1029 if ( message == WM_SETFOCUS )
1030 radiobox->HandleSetFocus((WXHWND)wParam);
1031 else
1032 radiobox->HandleKillFocus((WXHWND)wParam);
1033 }
1034 break;
1035
1036 #ifdef __WIN32__
1037 case WM_HELP:
1038 {
1039 wxRadioBox *radiobox = (wxRadioBox *)wxGetWindowUserData(hwnd);
1040
1041 wxCHECK_MSG( radiobox, 0, wxT("radio button without radio box?") );
1042
1043 bool processed wxDUMMY_INITIALIZE(true);
1044
1045 // HELPINFO doesn't seem to be supported on WinCE.
1046 #ifndef __WXWINCE__
1047 HELPINFO* info = (HELPINFO*) lParam;
1048 // Don't yet process menu help events, just windows
1049 if (info->iContextType == HELPINFO_WINDOW)
1050 #endif
1051 {
1052 wxWindow* subjectOfHelp = radiobox;
1053 bool eventProcessed = FALSE;
1054 while (subjectOfHelp && !eventProcessed)
1055 {
1056 wxHelpEvent helpEvent(wxEVT_HELP, subjectOfHelp->GetId(),
1057 #ifdef __WXWINCE__
1058 wxPoint(0, 0)
1059 #else
1060 wxPoint(info->MousePos.x, info->MousePos.y)
1061 #endif
1062 ) ; // info->iCtrlId);
1063 helpEvent.SetEventObject(radiobox);
1064 eventProcessed = radiobox->GetEventHandler()->ProcessEvent(helpEvent);
1065
1066 // Go up the window hierarchy until the event is handled (or not)
1067 subjectOfHelp = subjectOfHelp->GetParent();
1068 }
1069 processed = eventProcessed;
1070 }
1071 #ifndef __WXWINCE__
1072 else if (info->iContextType == HELPINFO_MENUITEM)
1073 {
1074 wxHelpEvent helpEvent(wxEVT_HELP, info->iCtrlId) ;
1075 helpEvent.SetEventObject(radiobox);
1076 processed = radiobox->GetEventHandler()->ProcessEvent(helpEvent);
1077 }
1078 else
1079 processed = false;
1080 #endif
1081 if (processed)
1082 return 0;
1083
1084 break;
1085 }
1086 #endif // __WIN32__
1087 }
1088
1089 return ::CallWindowProc(CASTWNDPROC s_wndprocRadioBtn, hwnd, message, wParam, lParam);
1090 }
1091
1092 #endif // __WIN32__
1093
1094 #endif // wxUSE_RADIOBOX