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