]>
Commit | Line | Data |
---|---|---|
2bda0e17 | 1 | ///////////////////////////////////////////////////////////////////////////// |
1e6feb95 | 2 | // Name: msw/radiobut.cpp |
2bda0e17 KB |
3 | // Purpose: wxRadioButton |
4 | // Author: Julian Smart | |
5 | // Modified by: | |
6 | // Created: 04/01/98 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) Julian Smart and Markus Holzem | |
c085e333 | 9 | // Licence: wxWindows license |
2bda0e17 KB |
10 | ///////////////////////////////////////////////////////////////////////////// |
11 | ||
5f199b71 VZ |
12 | // ============================================================================ |
13 | // declarations | |
14 | // ============================================================================ | |
15 | ||
16 | // ---------------------------------------------------------------------------- | |
17 | // headers | |
18 | // ---------------------------------------------------------------------------- | |
19 | ||
2bda0e17 | 20 | #ifdef __GNUG__ |
1e6feb95 | 21 | #pragma implementation "radiobut.h" |
2bda0e17 KB |
22 | #endif |
23 | ||
24 | // For compilers that support precompilation, includes "wx.h". | |
25 | #include "wx/wxprec.h" | |
26 | ||
27 | #ifdef __BORLANDC__ | |
1e6feb95 | 28 | #pragma hdrstop |
2bda0e17 KB |
29 | #endif |
30 | ||
1e6feb95 VZ |
31 | #if wxUSE_RADIOBTN |
32 | ||
2bda0e17 | 33 | #ifndef WX_PRECOMP |
1e6feb95 VZ |
34 | #include "wx/radiobut.h" |
35 | #include "wx/settings.h" | |
36 | #include "wx/brush.h" | |
2bda0e17 KB |
37 | #endif |
38 | ||
39 | #include "wx/msw/private.h" | |
40 | ||
5f199b71 VZ |
41 | // ============================================================================ |
42 | // wxRadioButton implementation | |
43 | // ============================================================================ | |
44 | ||
45 | // ---------------------------------------------------------------------------- | |
46 | // wxRadioButton creation | |
47 | // ---------------------------------------------------------------------------- | |
48 | ||
2bda0e17 | 49 | IMPLEMENT_DYNAMIC_CLASS(wxRadioButton, wxControl) |
2bda0e17 | 50 | |
5f199b71 | 51 | void wxRadioButton::Init() |
621793f4 | 52 | { |
5f199b71 | 53 | m_focusJustSet = FALSE; |
621793f4 JS |
54 | } |
55 | ||
5f199b71 VZ |
56 | bool wxRadioButton::Create(wxWindow *parent, |
57 | wxWindowID id, | |
58 | const wxString& label, | |
59 | const wxPoint& pos, | |
60 | const wxSize& size, | |
61 | long style, | |
62 | const wxValidator& validator, | |
63 | const wxString& name) | |
2bda0e17 | 64 | { |
5f199b71 VZ |
65 | if ( !CreateControl(parent, id, pos, size, style, validator, name) ) |
66 | return FALSE; | |
2bda0e17 | 67 | |
5f199b71 | 68 | long msStyle = HasFlag(wxRB_GROUP) ? WS_GROUP : 0; |
2bda0e17 | 69 | |
5f199b71 | 70 | msStyle |= BS_AUTORADIOBUTTON; |
2bda0e17 | 71 | |
5f199b71 | 72 | if ( HasFlag(wxCLIP_SIBLINGS) ) |
b0766406 JS |
73 | msStyle |= WS_CLIPSIBLINGS; |
74 | ||
5f199b71 VZ |
75 | if ( !MSWCreateControl(_T("BUTTON"), msStyle, pos, size, label, 0) ) |
76 | return FALSE; | |
b0766406 | 77 | |
5f199b71 VZ |
78 | // for compatibility with wxGTK, the first radio button in a group is |
79 | // always checked (this makes sense anyhow as you need to ensure that at | |
80 | // least one button in the group is checked and this is the simlpest way to | |
81 | // do it) | |
82 | if ( HasFlag(wxRB_GROUP) ) | |
83 | SetValue(TRUE); | |
c085e333 | 84 | |
5f199b71 | 85 | return TRUE; |
2bda0e17 KB |
86 | } |
87 | ||
5f199b71 VZ |
88 | // ---------------------------------------------------------------------------- |
89 | // wxRadioButton functions | |
90 | // ---------------------------------------------------------------------------- | |
2bda0e17 | 91 | |
5f199b71 | 92 | void wxRadioButton::SetValue(bool value) |
2bda0e17 | 93 | { |
5f199b71 VZ |
94 | // BST_CHECKED is defined as 1, BST_UNCHECKED as 0, so we can just pass |
95 | // value as is (we don't sue BST_XXX here as they're not defined for Win16) | |
96 | (void)::SendMessage(GetHwnd(), BM_SETCHECK, (WPARAM)value, 0L); | |
2bda0e17 KB |
97 | } |
98 | ||
5f199b71 | 99 | bool wxRadioButton::GetValue() const |
2bda0e17 | 100 | { |
5f199b71 VZ |
101 | // NB: this will also return TRUE for BST_INDETERMINATE value if we ever |
102 | // have 3-state radio buttons | |
103 | return ::SendMessage(GetHwnd(), BM_GETCHECK, 0, 0L) != 0; | |
2bda0e17 KB |
104 | } |
105 | ||
5f199b71 VZ |
106 | // ---------------------------------------------------------------------------- |
107 | // wxRadioButton event processing | |
108 | // ---------------------------------------------------------------------------- | |
109 | ||
110 | void wxRadioButton::Command (wxCommandEvent& event) | |
2bda0e17 | 111 | { |
5f199b71 VZ |
112 | SetValue(event.m_commandInt != 0); |
113 | ProcessCommand(event); | |
2bda0e17 KB |
114 | } |
115 | ||
5f199b71 | 116 | void wxRadioButton::SetFocus() |
2bda0e17 | 117 | { |
5f199b71 VZ |
118 | // when the radio button receives a WM_SETFOCUS message it generates a |
119 | // BN_CLICKED which is totally unexpected and leads to catastrophic results | |
120 | // if you pop up a dialog from the radio button event handler as, when the | |
121 | // dialog is dismissed, the focus is returned to the radio button which | |
122 | // generates BN_CLICKED which leads to showing another dialog and so on | |
123 | // without end! | |
124 | // | |
125 | // to aviod this, we drop the pseudo BN_CLICKED events generated when the | |
126 | // button gains focus | |
127 | m_focusJustSet = TRUE; | |
128 | ||
129 | wxControl::SetFocus(); | |
2bda0e17 KB |
130 | } |
131 | ||
5f199b71 | 132 | bool wxRadioButton::MSWCommand(WXUINT param, WXWORD WXUNUSED(id)) |
f6bcfd97 | 133 | { |
5f199b71 VZ |
134 | if ( param != BN_CLICKED ) |
135 | return FALSE; | |
136 | ||
137 | if ( m_focusJustSet ) | |
f6bcfd97 | 138 | { |
5f199b71 VZ |
139 | // see above: we want to ignore this event |
140 | m_focusJustSet = FALSE; | |
f6bcfd97 | 141 | } |
5f199b71 VZ |
142 | else // a real clicked event |
143 | { | |
144 | wxCommandEvent event(wxEVT_COMMAND_RADIOBUTTON_SELECTED, GetId()); | |
145 | event.SetEventObject( this ); | |
146 | event.SetInt( GetValue() ); | |
f6bcfd97 | 147 | |
5f199b71 VZ |
148 | ProcessCommand(event); |
149 | } | |
f6bcfd97 | 150 | |
5f199b71 | 151 | return TRUE; |
f6bcfd97 | 152 | } |
2bda0e17 | 153 | |
5f199b71 VZ |
154 | // ---------------------------------------------------------------------------- |
155 | // wxRadioButton geometry | |
156 | // ---------------------------------------------------------------------------- | |
157 | ||
158 | wxSize wxRadioButton::DoGetBestSize() const | |
2bda0e17 | 159 | { |
5f199b71 | 160 | static int s_radioSize = 0; |
2bda0e17 | 161 | |
5f199b71 VZ |
162 | if ( !s_radioSize ) |
163 | { | |
164 | wxScreenDC dc; | |
165 | dc.SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT)); | |
2bda0e17 | 166 | |
5f199b71 VZ |
167 | s_radioSize = dc.GetCharHeight(); |
168 | } | |
2bda0e17 | 169 | |
5f199b71 | 170 | wxString str = GetLabel(); |
2bda0e17 | 171 | |
5f199b71 VZ |
172 | int wRadio, hRadio; |
173 | if ( !str.empty() ) | |
174 | { | |
175 | GetTextExtent(str, &wRadio, &hRadio); | |
176 | wRadio += s_radioSize + GetCharWidth(); | |
2bda0e17 | 177 | |
5f199b71 VZ |
178 | if ( hRadio < s_radioSize ) |
179 | hRadio = s_radioSize; | |
180 | } | |
181 | else | |
182 | { | |
183 | wRadio = s_radioSize; | |
184 | hRadio = s_radioSize; | |
185 | } | |
2bda0e17 | 186 | |
5f199b71 | 187 | return wxSize(wRadio, hRadio); |
2bda0e17 KB |
188 | } |
189 | ||
1e6feb95 | 190 | #endif // wxUSE_RADIOBTN |