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