]> git.saurik.com Git - wxWidgets.git/blob - src/msw/radiobox.cpp
cleanup after an exteption in the postcreate code, added StaticText tests.
[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 SetSelection(0);
349
350 SetSize(pos.x, pos.y, size.x, size.y);
351
352 return TRUE;
353 }
354
355 bool wxRadioBox::Create(wxWindow *parent,
356 wxWindowID id,
357 const wxString& title,
358 const wxPoint& pos,
359 const wxSize& size,
360 const wxArrayString& choices,
361 int majorDim,
362 long style,
363 const wxValidator& val,
364 const wxString& name)
365 {
366 wxCArrayString chs(choices);
367 return Create(parent, id, title, pos, size, chs.GetCount(),
368 chs.GetStrings(), majorDim, style, val, name);
369 }
370
371 wxRadioBox::~wxRadioBox()
372 {
373 m_isBeingDeleted = TRUE;
374
375 if (m_radioButtons)
376 {
377 int i;
378 for (i = 0; i < m_noItems; i++)
379 ::DestroyWindow((HWND)m_radioButtons[i]);
380 delete[] m_radioButtons;
381 }
382
383 if (m_radioWidth)
384 delete[] m_radioWidth;
385 if (m_radioHeight)
386 delete[] m_radioHeight;
387
388 }
389
390 void wxRadioBox::SetString(int item, const wxString& label)
391 {
392 wxCHECK_RET( item >= 0 && item < m_noItems, wxT("invalid radiobox index") );
393
394 m_radioWidth[item] = m_radioHeight[item] = -1;
395 SetWindowText((HWND)m_radioButtons[item], label.c_str());
396 }
397
398 void wxRadioBox::SetSelection(int N)
399 {
400 wxCHECK_RET( (N >= 0) && (N < m_noItems), wxT("invalid radiobox index") );
401
402 // Following necessary for Win32s, because Win32s translate BM_SETCHECK
403 if (m_selectedButton >= 0 && m_selectedButton < m_noItems)
404 ::SendMessage((HWND) m_radioButtons[m_selectedButton], BM_SETCHECK, 0, 0L);
405
406 ::SendMessage((HWND)m_radioButtons[N], BM_SETCHECK, 1, 0L);
407
408 m_selectedButton = N;
409 }
410
411 // Get single selection, for single choice list items
412 int wxRadioBox::GetSelection() const
413 {
414 return m_selectedButton;
415 }
416
417 // Find string for position
418 wxString wxRadioBox::GetString(int item) const
419 {
420 wxCHECK_MSG( item >= 0 && item < m_noItems, wxEmptyString,
421 wxT("invalid radiobox index") );
422
423 return wxGetWindowText(m_radioButtons[item]);
424 }
425
426 // ----------------------------------------------------------------------------
427 // size calculations
428 // ----------------------------------------------------------------------------
429
430 wxSize wxRadioBox::GetMaxButtonSize() const
431 {
432 // calculate the max button size
433 int widthMax = 0,
434 heightMax = 0;
435 for ( int i = 0 ; i < m_noItems; i++ )
436 {
437 int width, height;
438 if ( m_radioWidth[i] < 0 )
439 {
440 GetTextExtent(wxGetWindowText(m_radioButtons[i]), &width, &height);
441
442 // adjust the size to take into account the radio box itself
443 // FIXME this is totally bogus!
444 width += RADIO_SIZE;
445 height *= 3;
446 height /= 2;
447 }
448 else
449 {
450 width = m_radioWidth[i];
451 height = m_radioHeight[i];
452 }
453
454 if ( widthMax < width )
455 widthMax = width;
456 if ( heightMax < height )
457 heightMax = height;
458 }
459
460 return wxSize(widthMax, heightMax);
461 }
462
463 wxSize wxRadioBox::GetTotalButtonSize(const wxSize& sizeBtn) const
464 {
465 // the radiobox should be big enough for its buttons
466 int cx1, cy1;
467 wxGetCharSize(m_hWnd, &cx1, &cy1, &GetFont());
468
469 int extraHeight = cy1;
470
471 /* We'll assume the adjustments below are OK for Win 3.1 too
472 #if defined(CTL3D) && !CTL3D
473 // Requires a bigger group box in plain Windows
474 extraHeight *= 3;
475 extraHeight /= 2;
476 #endif
477 */
478
479 int height = GetNumVer() * sizeBtn.y + cy1/2 + extraHeight;
480 int width = GetNumHor() * (sizeBtn.x + cx1) + cx1;
481
482 // Add extra space under the label, if it exists.
483 if (!wxControl::GetLabel().IsEmpty())
484 height += cy1/2;
485
486 // and also wide enough for its label
487 int widthLabel;
488 GetTextExtent(GetTitle(), &widthLabel, NULL);
489 widthLabel += RADIO_SIZE; // FIXME this is bogus too
490 if ( widthLabel > width )
491 width = widthLabel;
492
493 return wxSize(width, height);
494 }
495
496 wxSize wxRadioBox::DoGetBestSize() const
497 {
498 return GetTotalButtonSize(GetMaxButtonSize());
499 }
500
501 // Restored old code.
502 void wxRadioBox::DoSetSize(int x, int y, int width, int height, int sizeFlags)
503 {
504 int currentX, currentY;
505 GetPosition(&currentX, &currentY);
506 int widthOld, heightOld;
507 GetSize(&widthOld, &heightOld);
508
509 int xx = x;
510 int yy = y;
511
512 if (x == -1 && !(sizeFlags & wxSIZE_ALLOW_MINUS_ONE))
513 xx = currentX;
514 if (y == -1 && !(sizeFlags & wxSIZE_ALLOW_MINUS_ONE))
515 yy = currentY;
516
517 #if RADIOBTN_PARENT_IS_RADIOBOX
518 int y_offset = 0;
519 int x_offset = 0;
520 #else
521 int y_offset = yy;
522 int x_offset = xx;
523 #endif
524
525 int cx1, cy1;
526 wxGetCharSize(m_hWnd, &cx1, &cy1, & GetFont());
527
528 // Attempt to have a look coherent with other platforms: We compute the
529 // biggest toggle dim, then we align all items according this value.
530 wxSize maxSize = GetMaxButtonSize();
531 int maxWidth = maxSize.x,
532 maxHeight = maxSize.y;
533
534 wxSize totSize = GetTotalButtonSize(maxSize);
535 int totWidth = totSize.x,
536 totHeight = totSize.y;
537
538 // only change our width/height if asked for
539 if ( width == -1 )
540 {
541 if ( sizeFlags & wxSIZE_AUTO_WIDTH )
542 width = totWidth;
543 else
544 width = widthOld;
545 }
546
547 if ( height == -1 )
548 {
549 if ( sizeFlags & wxSIZE_AUTO_HEIGHT )
550 height = totHeight;
551 else
552 height = heightOld;
553 }
554
555 ::MoveWindow(GetHwnd(), xx, yy, width, height, TRUE);
556
557 // Now position all the buttons: the current button will be put at
558 // wxPoint(x_offset, y_offset) and the new row/column will start at
559 // startX/startY. The size of all buttons will be the same wxSize(maxWidth,
560 // maxHeight) except for the buttons in the last column which should extend
561 // to the right border of radiobox and thus can be wider than this.
562
563 // Also, remember that wxRA_SPECIFY_COLS means that we arrange buttons in
564 // left to right order and m_majorDim is the number of columns while
565 // wxRA_SPECIFY_ROWS means that the buttons are arranged top to bottom and
566 // m_majorDim is the number of rows.
567
568 x_offset += cx1;
569 y_offset += cy1;
570
571 // Add extra space under the label, if it exists.
572 if (!wxControl::GetLabel().IsEmpty())
573 y_offset += cy1/2;
574
575 int startX = x_offset;
576 int startY = y_offset;
577
578 for ( int i = 0; i < m_noItems; i++ )
579 {
580 // the last button in the row may be wider than the other ones as the
581 // radiobox may be wider than the sum of the button widths (as it
582 // happens, for example, when the radiobox label is very long)
583 bool isLastInTheRow;
584 if ( m_windowStyle & wxRA_SPECIFY_COLS )
585 {
586 // item is the last in its row if it is a multiple of the number of
587 // columns or if it is just the last item
588 int n = i + 1;
589 isLastInTheRow = ((n % m_majorDim) == 0) || (n == m_noItems);
590 }
591 else // wxRA_SPECIFY_ROWS
592 {
593 // item is the last in the row if it is in the last columns
594 isLastInTheRow = i >= (m_noItems/m_majorDim)*m_majorDim;
595 }
596
597 // is this the start of new row/column?
598 if ( i && (i % m_majorDim == 0) )
599 {
600 if ( m_windowStyle & wxRA_SPECIFY_ROWS )
601 {
602 // start of new column
603 y_offset = startY;
604 x_offset += maxWidth + cx1;
605 }
606 else // start of new row
607 {
608 x_offset = startX;
609 y_offset += maxHeight;
610 if (m_radioWidth[0]>0)
611 y_offset += cy1/2;
612 }
613 }
614
615 int widthBtn;
616 if ( isLastInTheRow )
617 {
618 // make the button go to the end of radio box
619 widthBtn = startX + width - x_offset - 2*cx1;
620 if ( widthBtn < maxWidth )
621 widthBtn = maxWidth;
622 }
623 else
624 {
625 // normal button, always of the same size
626 widthBtn = maxWidth;
627 }
628
629 // VZ: make all buttons of the same, maximal size - like this they
630 // cover the radiobox entirely and the radiobox tooltips are always
631 // shown (otherwise they are not when the mouse pointer is in the
632 // radiobox part not belonging to any radiobutton)
633 ::MoveWindow((HWND)m_radioButtons[i],
634 x_offset, y_offset, widthBtn, maxHeight,
635 TRUE);
636
637 // where do we put the next button?
638 if ( m_windowStyle & wxRA_SPECIFY_ROWS )
639 {
640 // below this one
641 y_offset += maxHeight;
642 if (m_radioWidth[0]>0)
643 y_offset += cy1/2;
644 }
645 else
646 {
647 // to the right of this one
648 x_offset += widthBtn + cx1;
649 }
650 }
651 }
652
653 void wxRadioBox::GetSize(int *width, int *height) const
654 {
655 RECT rect;
656 rect.left = -1; rect.right = -1; rect.top = -1; rect.bottom = -1;
657
658 if (m_hWnd)
659 wxFindMaxSize(m_hWnd, &rect);
660
661 int i;
662 for (i = 0; i < m_noItems; i++)
663 wxFindMaxSize(m_radioButtons[i], &rect);
664
665 *width = rect.right - rect.left;
666 *height = rect.bottom - rect.top;
667 }
668
669 void wxRadioBox::GetPosition(int *x, int *y) const
670 {
671 wxWindow *parent = GetParent();
672 RECT rect = { -1, -1, -1, -1 };
673
674 int i;
675 for (i = 0; i < m_noItems; i++)
676 wxFindMaxSize(m_radioButtons[i], &rect);
677
678 if (m_hWnd)
679 wxFindMaxSize(m_hWnd, &rect);
680
681 // Since we now have the absolute screen coords, if there's a parent we
682 // must subtract its top left corner
683 POINT point;
684 point.x = rect.left;
685 point.y = rect.top;
686 if (parent)
687 {
688 ::ScreenToClient((HWND) parent->GetHWND(), &point);
689 }
690
691 // We may be faking the client origin. So a window that's really at (0, 30)
692 // may appear (to wxWin apps) to be at (0, 0).
693 if (GetParent())
694 {
695 wxPoint pt(GetParent()->GetClientAreaOrigin());
696 point.x -= pt.x;
697 point.y -= pt.y;
698 }
699
700 *x = point.x;
701 *y = point.y;
702 }
703
704 void wxRadioBox::SetFocus()
705 {
706 if (m_noItems > 0)
707 {
708 ::SetFocus((HWND)m_radioButtons[m_selectedButton == -1
709 ? 0
710 : m_selectedButton]);
711 }
712
713 }
714
715 bool wxRadioBox::Show(bool show)
716 {
717 if ( !wxControl::Show(show) )
718 return FALSE;
719
720 int nCmdShow = show ? SW_SHOW : SW_HIDE;
721 for ( int i = 0; i < m_noItems; i++ )
722 {
723 ::ShowWindow((HWND)m_radioButtons[i], nCmdShow);
724 }
725
726 return TRUE;
727 }
728
729 // Enable a specific button
730 void wxRadioBox::Enable(int item, bool enable)
731 {
732 wxCHECK_RET( item >= 0 && item < m_noItems,
733 wxT("invalid item in wxRadioBox::Enable()") );
734
735 ::EnableWindow((HWND) m_radioButtons[item], enable);
736 }
737
738 // Enable all controls
739 bool wxRadioBox::Enable(bool enable)
740 {
741 if ( !wxControl::Enable(enable) )
742 return FALSE;
743
744 for (int i = 0; i < m_noItems; i++)
745 ::EnableWindow((HWND) m_radioButtons[i], enable);
746
747 return TRUE;
748 }
749
750 // Show a specific button
751 void wxRadioBox::Show(int item, bool show)
752 {
753 wxCHECK_RET( item >= 0 && item < m_noItems,
754 wxT("invalid item in wxRadioBox::Show()") );
755
756 ::ShowWindow((HWND)m_radioButtons[item], show ? SW_SHOW : SW_HIDE);
757 }
758
759 bool wxRadioBox::ContainsHWND(WXHWND hWnd) const
760 {
761 size_t count = GetCount();
762 for ( size_t i = 0; i < count; i++ )
763 {
764 if ( GetRadioButtons()[i] == hWnd )
765 return TRUE;
766 }
767
768 return FALSE;
769 }
770
771 void wxRadioBox::Command(wxCommandEvent & event)
772 {
773 SetSelection (event.m_commandInt);
774 SetFocus();
775 ProcessCommand (event);
776 }
777
778 // NB: if this code is changed, wxGetWindowForHWND() which relies on having the
779 // radiobox pointer in GWL_USERDATA for radio buttons must be updated too!
780 void wxRadioBox::SubclassRadioButton(WXHWND hWndBtn)
781 {
782 HWND hwndBtn = (HWND)hWndBtn;
783
784 if ( !s_wndprocRadioBtn )
785 s_wndprocRadioBtn = (WXFARPROC)wxGetWindowProc(hwndBtn);
786
787 wxSetWindowProc(hwndBtn, wxRadioBtnWndProc);
788 wxSetWindowUserData(hwndBtn, this);
789 }
790
791 void wxRadioBox::SendNotificationEvent()
792 {
793 wxCommandEvent event(wxEVT_COMMAND_RADIOBOX_SELECTED, m_windowId);
794 event.SetInt( m_selectedButton );
795 event.SetString( GetString(m_selectedButton) );
796 event.SetEventObject( this );
797 ProcessCommand(event);
798 }
799
800 bool wxRadioBox::SetFont(const wxFont& font)
801 {
802 if ( !wxControl::SetFont(font) )
803 {
804 // nothing to do
805 return FALSE;
806 }
807
808 // also set the font of our radio buttons
809 WXHFONT hfont = wxFont(font).GetResourceHandle();
810 for ( int n = 0; n < m_noItems; n++ )
811 {
812 HWND hwndBtn = (HWND)m_radioButtons[n];
813 ::SendMessage(hwndBtn, WM_SETFONT, (WPARAM)hfont, 0L);
814
815 // otherwise the buttons are not redrawn correctly
816 ::InvalidateRect(hwndBtn, NULL, FALSE /* don't erase bg */);
817 }
818
819 return TRUE;
820 }
821
822 // ----------------------------------------------------------------------------
823 // our window proc
824 // ----------------------------------------------------------------------------
825
826 WXLRESULT wxRadioBox::MSWWindowProc(WXUINT nMsg, WXWPARAM wParam, WXLPARAM lParam)
827 {
828 switch ( nMsg )
829 {
830 #ifdef __WIN32__
831 case WM_CTLCOLORSTATIC:
832 // set the colour of the radio buttons to be the same as ours
833 {
834 HDC hdc = (HDC)wParam;
835
836 const wxColour& colBack = GetBackgroundColour();
837 ::SetBkColor(hdc, wxColourToRGB(colBack));
838 ::SetTextColor(hdc, wxColourToRGB(GetForegroundColour()));
839
840 wxBrush *brush = wxTheBrushList->FindOrCreateBrush(colBack, wxSOLID);
841
842 return (WXLRESULT)brush->GetResourceHandle();
843 }
844 #endif // Win32
845
846 // VZ: this code breaks radiobox redrawing under Windows XP, don't use
847 // it. If you need to get messages from the static controls,
848 // create them as a child of another (non static) window
849 #if 0
850 // This is required for the radiobox to be sensitive to mouse input,
851 // e.g. for Dialog Editor.
852 case WM_NCHITTEST:
853 {
854 int xPos = LOWORD(lParam); // horizontal position of cursor
855 int yPos = HIWORD(lParam); // vertical position of cursor
856
857 ScreenToClient(&xPos, &yPos);
858
859 // Make sure you can drag by the top of the groupbox, but let
860 // other (enclosed) controls get mouse events also
861 if (yPos < 10)
862 return (long)HTCLIENT;
863 }
864 break;
865 #endif // 0
866 }
867
868 return wxControl::MSWWindowProc(nMsg, wParam, lParam);
869 }
870
871 WXHBRUSH wxRadioBox::OnCtlColor(WXHDC pDC, WXHWND WXUNUSED(pWnd), WXUINT WXUNUSED(nCtlColor),
872 #if wxUSE_CTL3D
873 WXUINT message,
874 WXWPARAM wParam,
875 WXLPARAM lParam
876 #else
877 WXUINT WXUNUSED(message),
878 WXWPARAM WXUNUSED(wParam),
879 WXLPARAM WXUNUSED(lParam)
880 #endif
881 )
882 {
883 #if wxUSE_CTL3D
884 if ( m_useCtl3D )
885 {
886 HBRUSH hbrush = Ctl3dCtlColorEx(message, wParam, lParam);
887 return (WXHBRUSH) hbrush;
888 }
889 #endif // wxUSE_CTL3D
890
891 HDC hdc = (HDC)pDC;
892 wxColour colBack = GetBackgroundColour();
893
894 if (!IsEnabled())
895 colBack = wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE);
896
897 ::SetBkColor(hdc, wxColourToRGB(colBack));
898 ::SetTextColor(hdc, wxColourToRGB(GetForegroundColour()));
899
900 wxBrush *brush = wxTheBrushList->FindOrCreateBrush(colBack, wxSOLID);
901
902 return (WXHBRUSH)brush->GetResourceHandle();
903 }
904
905
906 // ---------------------------------------------------------------------------
907 // window proc for radio buttons
908 // ---------------------------------------------------------------------------
909
910 #ifdef __WIN32__
911
912 LRESULT APIENTRY _EXPORT wxRadioBtnWndProc(HWND hwnd,
913 UINT message,
914 WPARAM wParam,
915 LPARAM lParam)
916 {
917 switch ( message )
918 {
919 case WM_GETDLGCODE:
920 // we must tell IsDialogMessage()/our kbd processing code that we
921 // want to process arrows ourselves because neither of them is
922 // smart enough to handle arrows properly for us
923 {
924 long lDlgCode = ::CallWindowProc(CASTWNDPROC s_wndprocRadioBtn, hwnd,
925 message, wParam, lParam);
926
927 return lDlgCode | DLGC_WANTARROWS;
928 }
929
930 #if wxUSE_TOOLTIPS
931 case WM_NOTIFY:
932 {
933 NMHDR* hdr = (NMHDR *)lParam;
934 if ( hdr->code == TTN_NEEDTEXT )
935 {
936 wxRadioBox *
937 radiobox = (wxRadioBox *)wxGetWindowUserData(hwnd);
938
939 wxCHECK_MSG( radiobox, 0,
940 wxT("radio button without radio box?") );
941
942 wxToolTip *tooltip = radiobox->GetToolTip();
943 if ( tooltip )
944 {
945 TOOLTIPTEXT *ttt = (TOOLTIPTEXT *)lParam;
946 ttt->lpszText = (wxChar *)tooltip->GetTip().c_str();
947 }
948
949 // processed
950 return 0;
951 }
952 }
953 break;
954 #endif // wxUSE_TOOLTIPS
955
956 case WM_KEYDOWN:
957 {
958 wxRadioBox *radiobox = (wxRadioBox *)wxGetWindowUserData(hwnd);
959
960 wxCHECK_MSG( radiobox, 0, wxT("radio button without radio box?") );
961
962 bool processed = TRUE;
963
964 wxDirection dir;
965 switch ( wParam )
966 {
967 case VK_UP:
968 dir = wxUP;
969 break;
970
971 case VK_LEFT:
972 dir = wxLEFT;
973 break;
974
975 case VK_DOWN:
976 dir = wxDOWN;
977 break;
978
979 case VK_RIGHT:
980 dir = wxRIGHT;
981 break;
982
983 default:
984 processed = FALSE;
985
986 // just to suppress the compiler warning
987 dir = wxALL;
988 }
989
990 if ( processed )
991 {
992 int selOld = radiobox->GetSelection();
993 int selNew = radiobox->GetNextItem
994 (
995 selOld,
996 dir,
997 radiobox->GetWindowStyle()
998 );
999
1000 if ( selNew != selOld )
1001 {
1002 radiobox->SetSelection(selNew);
1003 radiobox->SetFocus();
1004
1005 // emulate the button click
1006 radiobox->SendNotificationEvent();
1007
1008 return 0;
1009 }
1010 }
1011 }
1012 break;
1013
1014 case WM_SETFOCUS:
1015 case WM_KILLFOCUS:
1016 {
1017 wxRadioBox *radiobox = (wxRadioBox *)wxGetWindowUserData(hwnd);
1018
1019 wxCHECK_MSG( radiobox, 0, wxT("radio button without radio box?") );
1020
1021 // if we don't do this, no focus events are generated for the
1022 // radiobox and, besides, we need to notify the parent about
1023 // the focus change, otherwise the focus handling logic in
1024 // wxControlContainer doesn't work
1025 if ( message == WM_SETFOCUS )
1026 radiobox->HandleSetFocus((WXHWND)wParam);
1027 else
1028 radiobox->HandleKillFocus((WXHWND)wParam);
1029 }
1030 break;
1031
1032 #ifdef __WIN32__
1033 case WM_HELP:
1034 {
1035 wxRadioBox *radiobox = (wxRadioBox *)wxGetWindowUserData(hwnd);
1036
1037 wxCHECK_MSG( radiobox, 0, wxT("radio button without radio box?") );
1038
1039 bool processed = TRUE;
1040
1041 // HELPINFO doesn't seem to be supported on WinCE.
1042 #ifndef __WXWINCE__
1043 HELPINFO* info = (HELPINFO*) lParam;
1044 // Don't yet process menu help events, just windows
1045 if (info->iContextType == HELPINFO_WINDOW)
1046 #endif
1047 {
1048 wxWindow* subjectOfHelp = radiobox;
1049 bool eventProcessed = FALSE;
1050 while (subjectOfHelp && !eventProcessed)
1051 {
1052 wxHelpEvent helpEvent(wxEVT_HELP, subjectOfHelp->GetId(),
1053 #ifdef __WXWINCE__
1054 wxPoint(0, 0)
1055 #else
1056 wxPoint(info->MousePos.x, info->MousePos.y)
1057 #endif
1058 ) ; // info->iCtrlId);
1059 helpEvent.SetEventObject(radiobox);
1060 eventProcessed = radiobox->GetEventHandler()->ProcessEvent(helpEvent);
1061
1062 // Go up the window hierarchy until the event is handled (or not)
1063 subjectOfHelp = subjectOfHelp->GetParent();
1064 }
1065 processed = eventProcessed;
1066 }
1067 #ifndef __WXWINCE__
1068 else if (info->iContextType == HELPINFO_MENUITEM)
1069 {
1070 wxHelpEvent helpEvent(wxEVT_HELP, info->iCtrlId) ;
1071 helpEvent.SetEventObject(radiobox);
1072 processed = radiobox->GetEventHandler()->ProcessEvent(helpEvent);
1073 }
1074 else
1075 processed = FALSE;
1076 #endif
1077 if (processed)
1078 return 0;
1079
1080 break;
1081 }
1082 #endif // __WIN32__
1083 }
1084
1085 return ::CallWindowProc(CASTWNDPROC s_wndprocRadioBtn, hwnd, message, wParam, lParam);
1086 }
1087
1088 #endif // __WIN32__
1089
1090 #endif // wxUSE_RADIOBOX