Under WinCE, we need to send the groupbox z position to the bottom
[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 RECT rect;
351 GetWindowRect(GetHwnd(), & rect);
352 SetWindowPos(GetHwnd(), HWND_BOTTOM, rect.left, rect.top, rect.right - rect.left, rect.bottom - rect.top, 0);
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] = -1;
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 == -1 && !(sizeFlags & wxSIZE_ALLOW_MINUS_ONE))
522 xx = currentX;
523 if (y == -1 && !(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 == -1 )
549 {
550 if ( sizeFlags & wxSIZE_AUTO_WIDTH )
551 width = totWidth;
552 else
553 width = widthOld;
554 }
555
556 if ( height == -1 )
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;
665 rect.left = -1; rect.right = -1; rect.top = -1; rect.bottom = -1;
666
667 if (m_hWnd)
668 wxFindMaxSize(m_hWnd, &rect);
669
670 int i;
671 for (i = 0; i < m_noItems; i++)
672 wxFindMaxSize(m_radioButtons[i], &rect);
673
674 *width = rect.right - rect.left;
675 *height = rect.bottom - rect.top;
676 }
677
678 void wxRadioBox::GetPosition(int *x, int *y) const
679 {
680 wxWindow *parent = GetParent();
681 RECT rect = { -1, -1, -1, -1 };
682
683 int i;
684 for (i = 0; i < m_noItems; i++)
685 wxFindMaxSize(m_radioButtons[i], &rect);
686
687 if (m_hWnd)
688 wxFindMaxSize(m_hWnd, &rect);
689
690 // Since we now have the absolute screen coords, if there's a parent we
691 // must subtract its top left corner
692 POINT point;
693 point.x = rect.left;
694 point.y = rect.top;
695 if (parent)
696 {
697 ::ScreenToClient((HWND) parent->GetHWND(), &point);
698 }
699
700 // We may be faking the client origin. So a window that's really at (0, 30)
701 // may appear (to wxWin apps) to be at (0, 0).
702 if (GetParent())
703 {
704 wxPoint pt(GetParent()->GetClientAreaOrigin());
705 point.x -= pt.x;
706 point.y -= pt.y;
707 }
708
709 *x = point.x;
710 *y = point.y;
711 }
712
713 void wxRadioBox::SetFocus()
714 {
715 if (m_noItems > 0)
716 {
717 ::SetFocus((HWND)m_radioButtons[m_selectedButton == -1
718 ? 0
719 : m_selectedButton]);
720 }
721
722 }
723
724 bool wxRadioBox::Show(bool show)
725 {
726 if ( !wxControl::Show(show) )
727 return FALSE;
728
729 int nCmdShow = show ? SW_SHOW : SW_HIDE;
730 for ( int i = 0; i < m_noItems; i++ )
731 {
732 ::ShowWindow((HWND)m_radioButtons[i], nCmdShow);
733 }
734
735 return TRUE;
736 }
737
738 // Enable a specific button
739 void wxRadioBox::Enable(int item, bool enable)
740 {
741 wxCHECK_RET( item >= 0 && item < m_noItems,
742 wxT("invalid item in wxRadioBox::Enable()") );
743
744 ::EnableWindow((HWND) m_radioButtons[item], enable);
745 }
746
747 // Enable all controls
748 bool wxRadioBox::Enable(bool enable)
749 {
750 if ( !wxControl::Enable(enable) )
751 return FALSE;
752
753 for (int i = 0; i < m_noItems; i++)
754 ::EnableWindow((HWND) m_radioButtons[i], enable);
755
756 return TRUE;
757 }
758
759 // Show a specific button
760 void wxRadioBox::Show(int item, bool show)
761 {
762 wxCHECK_RET( item >= 0 && item < m_noItems,
763 wxT("invalid item in wxRadioBox::Show()") );
764
765 ::ShowWindow((HWND)m_radioButtons[item], show ? SW_SHOW : SW_HIDE);
766 }
767
768 bool wxRadioBox::ContainsHWND(WXHWND hWnd) const
769 {
770 size_t count = GetCount();
771 for ( size_t i = 0; i < count; i++ )
772 {
773 if ( GetRadioButtons()[i] == hWnd )
774 return TRUE;
775 }
776
777 return FALSE;
778 }
779
780 void wxRadioBox::Command(wxCommandEvent & event)
781 {
782 SetSelection (event.m_commandInt);
783 SetFocus();
784 ProcessCommand (event);
785 }
786
787 // NB: if this code is changed, wxGetWindowForHWND() which relies on having the
788 // radiobox pointer in GWL_USERDATA for radio buttons must be updated too!
789 void wxRadioBox::SubclassRadioButton(WXHWND hWndBtn)
790 {
791 HWND hwndBtn = (HWND)hWndBtn;
792
793 if ( !s_wndprocRadioBtn )
794 s_wndprocRadioBtn = (WXFARPROC)wxGetWindowProc(hwndBtn);
795
796 wxSetWindowProc(hwndBtn, wxRadioBtnWndProc);
797 wxSetWindowUserData(hwndBtn, this);
798 }
799
800 void wxRadioBox::SendNotificationEvent()
801 {
802 wxCommandEvent event(wxEVT_COMMAND_RADIOBOX_SELECTED, m_windowId);
803 event.SetInt( m_selectedButton );
804 event.SetString( GetString(m_selectedButton) );
805 event.SetEventObject( this );
806 ProcessCommand(event);
807 }
808
809 bool wxRadioBox::SetFont(const wxFont& font)
810 {
811 if ( !wxControl::SetFont(font) )
812 {
813 // nothing to do
814 return FALSE;
815 }
816
817 // also set the font of our radio buttons
818 WXHFONT hfont = wxFont(font).GetResourceHandle();
819 for ( int n = 0; n < m_noItems; n++ )
820 {
821 HWND hwndBtn = (HWND)m_radioButtons[n];
822 ::SendMessage(hwndBtn, WM_SETFONT, (WPARAM)hfont, 0L);
823
824 // otherwise the buttons are not redrawn correctly
825 ::InvalidateRect(hwndBtn, NULL, FALSE /* don't erase bg */);
826 }
827
828 return TRUE;
829 }
830
831 // ----------------------------------------------------------------------------
832 // our window proc
833 // ----------------------------------------------------------------------------
834
835 WXLRESULT wxRadioBox::MSWWindowProc(WXUINT nMsg, WXWPARAM wParam, WXLPARAM lParam)
836 {
837 switch ( nMsg )
838 {
839 #ifdef __WIN32__
840 case WM_CTLCOLORSTATIC:
841 // set the colour of the radio buttons to be the same as ours
842 {
843 HDC hdc = (HDC)wParam;
844
845 const wxColour& colBack = GetBackgroundColour();
846 ::SetBkColor(hdc, wxColourToRGB(colBack));
847 ::SetTextColor(hdc, wxColourToRGB(GetForegroundColour()));
848
849 wxBrush *brush = wxTheBrushList->FindOrCreateBrush(colBack, wxSOLID);
850
851 return (WXLRESULT)brush->GetResourceHandle();
852 }
853 #endif // Win32
854
855 // VZ: this code breaks radiobox redrawing under Windows XP, don't use
856 // it. If you need to get messages from the static controls,
857 // create them as a child of another (non static) window
858 #if 0
859 // This is required for the radiobox to be sensitive to mouse input,
860 // e.g. for Dialog Editor.
861 case WM_NCHITTEST:
862 {
863 int xPos = LOWORD(lParam); // horizontal position of cursor
864 int yPos = HIWORD(lParam); // vertical position of cursor
865
866 ScreenToClient(&xPos, &yPos);
867
868 // Make sure you can drag by the top of the groupbox, but let
869 // other (enclosed) controls get mouse events also
870 if (yPos < 10)
871 return (long)HTCLIENT;
872 }
873 break;
874 #endif // 0
875 }
876
877 return wxControl::MSWWindowProc(nMsg, wParam, lParam);
878 }
879
880 WXHBRUSH wxRadioBox::OnCtlColor(WXHDC pDC, WXHWND WXUNUSED(pWnd), WXUINT WXUNUSED(nCtlColor),
881 #if wxUSE_CTL3D
882 WXUINT message,
883 WXWPARAM wParam,
884 WXLPARAM lParam
885 #else
886 WXUINT WXUNUSED(message),
887 WXWPARAM WXUNUSED(wParam),
888 WXLPARAM WXUNUSED(lParam)
889 #endif
890 )
891 {
892 #if wxUSE_CTL3D
893 if ( m_useCtl3D )
894 {
895 HBRUSH hbrush = Ctl3dCtlColorEx(message, wParam, lParam);
896 return (WXHBRUSH) hbrush;
897 }
898 #endif // wxUSE_CTL3D
899
900 HDC hdc = (HDC)pDC;
901 wxColour colBack = GetBackgroundColour();
902
903 ::SetBkColor(hdc, wxColourToRGB(colBack));
904 ::SetTextColor(hdc, wxColourToRGB(GetForegroundColour()));
905
906 wxBrush *brush = wxTheBrushList->FindOrCreateBrush(colBack, wxSOLID);
907
908 return (WXHBRUSH)brush->GetResourceHandle();
909 }
910
911
912 // ---------------------------------------------------------------------------
913 // window proc for radio buttons
914 // ---------------------------------------------------------------------------
915
916 #ifdef __WIN32__
917
918 LRESULT APIENTRY _EXPORT wxRadioBtnWndProc(HWND hwnd,
919 UINT message,
920 WPARAM wParam,
921 LPARAM lParam)
922 {
923 switch ( message )
924 {
925 case WM_GETDLGCODE:
926 // we must tell IsDialogMessage()/our kbd processing code that we
927 // want to process arrows ourselves because neither of them is
928 // smart enough to handle arrows properly for us
929 {
930 long lDlgCode = ::CallWindowProc(CASTWNDPROC s_wndprocRadioBtn, hwnd,
931 message, wParam, lParam);
932
933 return lDlgCode | DLGC_WANTARROWS;
934 }
935
936 #if wxUSE_TOOLTIPS
937 case WM_NOTIFY:
938 {
939 NMHDR* hdr = (NMHDR *)lParam;
940 if ( hdr->code == TTN_NEEDTEXT )
941 {
942 wxRadioBox *
943 radiobox = (wxRadioBox *)wxGetWindowUserData(hwnd);
944
945 wxCHECK_MSG( radiobox, 0,
946 wxT("radio button without radio box?") );
947
948 wxToolTip *tooltip = radiobox->GetToolTip();
949 if ( tooltip )
950 {
951 TOOLTIPTEXT *ttt = (TOOLTIPTEXT *)lParam;
952 ttt->lpszText = (wxChar *)tooltip->GetTip().c_str();
953 }
954
955 // processed
956 return 0;
957 }
958 }
959 break;
960 #endif // wxUSE_TOOLTIPS
961
962 case WM_KEYDOWN:
963 {
964 wxRadioBox *radiobox = (wxRadioBox *)wxGetWindowUserData(hwnd);
965
966 wxCHECK_MSG( radiobox, 0, wxT("radio button without radio box?") );
967
968 bool processed = TRUE;
969
970 wxDirection dir;
971 switch ( wParam )
972 {
973 case VK_UP:
974 dir = wxUP;
975 break;
976
977 case VK_LEFT:
978 dir = wxLEFT;
979 break;
980
981 case VK_DOWN:
982 dir = wxDOWN;
983 break;
984
985 case VK_RIGHT:
986 dir = wxRIGHT;
987 break;
988
989 default:
990 processed = FALSE;
991
992 // just to suppress the compiler warning
993 dir = wxALL;
994 }
995
996 if ( processed )
997 {
998 int selOld = radiobox->GetSelection();
999 int selNew = radiobox->GetNextItem
1000 (
1001 selOld,
1002 dir,
1003 radiobox->GetWindowStyle()
1004 );
1005
1006 if ( selNew != selOld )
1007 {
1008 radiobox->SetSelection(selNew);
1009 radiobox->SetFocus();
1010
1011 // emulate the button click
1012 radiobox->SendNotificationEvent();
1013
1014 return 0;
1015 }
1016 }
1017 }
1018 break;
1019
1020 case WM_SETFOCUS:
1021 case WM_KILLFOCUS:
1022 {
1023 wxRadioBox *radiobox = (wxRadioBox *)wxGetWindowUserData(hwnd);
1024
1025 wxCHECK_MSG( radiobox, 0, wxT("radio button without radio box?") );
1026
1027 // if we don't do this, no focus events are generated for the
1028 // radiobox and, besides, we need to notify the parent about
1029 // the focus change, otherwise the focus handling logic in
1030 // wxControlContainer doesn't work
1031 if ( message == WM_SETFOCUS )
1032 radiobox->HandleSetFocus((WXHWND)wParam);
1033 else
1034 radiobox->HandleKillFocus((WXHWND)wParam);
1035 }
1036 break;
1037
1038 #ifdef __WIN32__
1039 case WM_HELP:
1040 {
1041 wxRadioBox *radiobox = (wxRadioBox *)wxGetWindowUserData(hwnd);
1042
1043 wxCHECK_MSG( radiobox, 0, wxT("radio button without radio box?") );
1044
1045 bool processed wxDUMMY_INITIALIZE(true);
1046
1047 // HELPINFO doesn't seem to be supported on WinCE.
1048 #ifndef __WXWINCE__
1049 HELPINFO* info = (HELPINFO*) lParam;
1050 // Don't yet process menu help events, just windows
1051 if (info->iContextType == HELPINFO_WINDOW)
1052 #endif
1053 {
1054 wxWindow* subjectOfHelp = radiobox;
1055 bool eventProcessed = FALSE;
1056 while (subjectOfHelp && !eventProcessed)
1057 {
1058 wxHelpEvent helpEvent(wxEVT_HELP, subjectOfHelp->GetId(),
1059 #ifdef __WXWINCE__
1060 wxPoint(0, 0)
1061 #else
1062 wxPoint(info->MousePos.x, info->MousePos.y)
1063 #endif
1064 ) ; // info->iCtrlId);
1065 helpEvent.SetEventObject(radiobox);
1066 eventProcessed = radiobox->GetEventHandler()->ProcessEvent(helpEvent);
1067
1068 // Go up the window hierarchy until the event is handled (or not)
1069 subjectOfHelp = subjectOfHelp->GetParent();
1070 }
1071 processed = eventProcessed;
1072 }
1073 #ifndef __WXWINCE__
1074 else if (info->iContextType == HELPINFO_MENUITEM)
1075 {
1076 wxHelpEvent helpEvent(wxEVT_HELP, info->iCtrlId) ;
1077 helpEvent.SetEventObject(radiobox);
1078 processed = radiobox->GetEventHandler()->ProcessEvent(helpEvent);
1079 }
1080 else
1081 processed = false;
1082 #endif
1083 if (processed)
1084 return 0;
1085
1086 break;
1087 }
1088 #endif // __WIN32__
1089 }
1090
1091 return ::CallWindowProc(CASTWNDPROC s_wndprocRadioBtn, hwnd, message, wParam, lParam);
1092 }
1093
1094 #endif // __WIN32__
1095
1096 #endif // wxUSE_RADIOBOX