use the new wxSystemSettings API everywhere
[wxWidgets.git] / src / msw / radiobut.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: msw/radiobut.cpp
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
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
11
12 #ifdef __GNUG__
13 #pragma implementation "radiobut.h"
14 #endif
15
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
18
19 #ifdef __BORLANDC__
20 #pragma hdrstop
21 #endif
22
23 #if wxUSE_RADIOBTN
24
25 #ifndef WX_PRECOMP
26 #include "wx/radiobut.h"
27 #include "wx/settings.h"
28 #include "wx/brush.h"
29 #endif
30
31 #include "wx/msw/private.h"
32
33 IMPLEMENT_DYNAMIC_CLASS(wxRadioButton, wxControl)
34 // IMPLEMENT_DYNAMIC_CLASS(wxBitmapRadioButton, wxRadioButton)
35
36 bool wxRadioButton::MSWCommand(WXUINT param, WXWORD WXUNUSED(id))
37 {
38 if (param == BN_CLICKED)
39 {
40 wxCommandEvent event(wxEVT_COMMAND_RADIOBUTTON_SELECTED, m_windowId);
41 event.SetEventObject( this );
42 event.SetInt( GetValue() );
43 ProcessCommand(event);
44 return TRUE;
45 }
46 else return FALSE;
47 }
48
49 bool wxRadioButton::Create(wxWindow *parent, wxWindowID id,
50 const wxString& label,
51 const wxPoint& pos,
52 const wxSize& size, long style,
53 const wxValidator& validator,
54 const wxString& name)
55 {
56 SetName(name);
57 #if wxUSE_VALIDATORS
58 SetValidator(validator);
59 #endif // wxUSE_VALIDATORS
60
61 if (parent) parent->AddChild(this);
62
63 SetBackgroundColour(parent->GetBackgroundColour());
64 SetForegroundColour(parent->GetForegroundColour());
65
66 if ( id == -1 )
67 m_windowId = (int)NewControlId();
68 else
69 m_windowId = id;
70
71 int x = pos.x;
72 int y = pos.y;
73 int width = size.x;
74 int height = size.y;
75
76 m_windowStyle = style ;
77
78 long groupStyle = 0;
79 if (m_windowStyle & wxRB_GROUP)
80 groupStyle = WS_GROUP;
81
82 // long msStyle = groupStyle | RADIO_FLAGS;
83 long msStyle = groupStyle | BS_AUTORADIOBUTTON | WS_CHILD | WS_VISIBLE /* | WS_CLIPSIBLINGS */;
84
85 if ( m_windowStyle & wxCLIP_SIBLINGS )
86 msStyle |= WS_CLIPSIBLINGS;
87
88
89 bool want3D;
90 WXDWORD exStyle = Determine3DEffects(0, &want3D) ;
91
92 // Even with extended styles, need to combine with WS_BORDER
93 // for them to look right.
94 /*
95 if ( want3D || wxStyleHasBorder(m_windowStyle) )
96 msStyle |= WS_BORDER;
97 */
98
99 m_hWnd = (WXHWND) CreateWindowEx(exStyle, RADIO_CLASS, (const wxChar *)label,
100 msStyle,0,0,0,0,
101 (HWND) parent->GetHWND(), (HMENU)m_windowId, wxGetInstance(), NULL);
102
103 wxCHECK_MSG( m_hWnd, FALSE, wxT("Failed to create radiobutton") );
104
105 #if wxUSE_CTL3D
106 if (want3D)
107 {
108 Ctl3dSubclassCtl((HWND) m_hWnd);
109 m_useCtl3D = TRUE;
110 }
111 #endif
112
113 SetFont(parent->GetFont());
114
115 // Subclass again for purposes of dialog editing mode
116 SubclassWin((WXHWND)m_hWnd);
117
118 // SetValue(value);
119
120 // start GRW fix
121 if (label != wxT(""))
122 {
123 int label_width, label_height;
124 GetTextExtent(label, &label_width, &label_height, NULL, NULL, & this->GetFont());
125 if (width < 0)
126 width = (int)(label_width + RADIO_SIZE);
127 if (height<0)
128 {
129 height = (int)(label_height);
130 if (height < RADIO_SIZE)
131 height = RADIO_SIZE;
132 }
133 }
134 else
135 {
136 if (width < 0)
137 width = RADIO_SIZE;
138 if (height < 0)
139 height = RADIO_SIZE;
140 }
141 // end GRW fix
142
143 SetSize(x, y, width, height);
144
145 // for compatibility with wxGTK, the first radio button in a group is
146 // always checked (this makes sense anyhow as you need to ensure that at
147 // least one button in the group is checked and this is the simlpest way to
148 // do it)
149 if ( m_windowStyle & wxRB_GROUP )
150 SetValue(TRUE);
151
152 return TRUE;
153 }
154
155
156 void wxRadioButton::SetLabel(const wxString& label)
157 {
158 SetWindowText((HWND) GetHWND(), (const wxChar *)label);
159 }
160
161 void wxRadioButton::SetValue(bool value)
162 {
163 // Following necessary for Win32s, because Win32s translate BM_SETCHECK
164 SendMessage((HWND) GetHWND(), BM_SETCHECK, (WPARAM)value, 0L);
165 }
166
167 // Get single selection
168 bool wxRadioButton::GetValue(void) const
169 {
170 return (SendMessage((HWND) GetHWND(), BM_GETCHECK, 0, 0L) != 0);
171 }
172
173 void wxRadioButton::Command (wxCommandEvent & event)
174 {
175 SetValue ( (event.m_commandInt != 0) );
176 ProcessCommand (event);
177 }
178
179 WXHBRUSH wxRadioButton::OnCtlColor(WXHDC pDC, WXHWND WXUNUSED(pWnd), WXUINT WXUNUSED(nCtlColor),
180 #if wxUSE_CTL3D
181 WXUINT message,
182 WXWPARAM wParam,
183 WXLPARAM lParam
184 #else
185 WXUINT WXUNUSED(message),
186 WXWPARAM WXUNUSED(wParam),
187 WXLPARAM WXUNUSED(lParam)
188 #endif
189 )
190 {
191 #if wxUSE_CTL3D
192 if ( m_useCtl3D )
193 {
194 HBRUSH hbrush = Ctl3dCtlColorEx(message, wParam, lParam);
195 return (WXHBRUSH) hbrush;
196 }
197 #endif // wxUSE_CTL3D
198
199 HDC hdc = (HDC)pDC;
200 if (GetParent()->GetTransparentBackground())
201 SetBkMode(hdc, TRANSPARENT);
202 else
203 SetBkMode(hdc, OPAQUE);
204
205 wxColour colBack = GetBackgroundColour();
206
207 if (!IsEnabled())
208 colBack = wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE);
209
210 ::SetBkColor(hdc, wxColourToRGB(colBack));
211 ::SetTextColor(hdc, wxColourToRGB(GetForegroundColour()));
212
213 wxBrush *brush = wxTheBrushList->FindOrCreateBrush(colBack, wxSOLID);
214
215 return (WXHBRUSH)brush->GetResourceHandle();
216 }
217
218 // Not implemented
219 #if 0
220 bool wxBitmapRadioButton::Create(wxWindow *parent, wxWindowID id,
221 const wxBitmap *bitmap,
222 const wxPoint& pos,
223 const wxSize& size, long style,
224 const wxValidator& validator,
225 const wxString& name)
226 {
227 SetName(name);
228 SetValidator(validator);
229
230 if (parent) parent->AddChild(this);
231 SetBackgroundColour(parent->GetBackgroundColour());
232 SetForegroundColour(parent->GetForegroundColour());
233
234 if ( id == -1 )
235 m_windowId = (int)NewControlId();
236 else
237 m_windowId = id;
238
239 int x = pos.x;
240 int y = pos.y;
241 int width = size.x;
242 int height = size.y;
243 m_windowStyle = style ;
244
245 long groupStyle = 0;
246 if (m_windowStyle & wxRB_GROUP)
247 groupStyle = WS_GROUP;
248
249 // long msStyle = groupStyle | RADIO_FLAGS;
250 long msStyle = groupStyle | BS_RADIOBUTTON | WS_CHILD | WS_VISIBLE ;
251
252 m_hWnd = (WXHWND) CreateWindowEx(MakeExtendedStyle(m_windowStyle), RADIO_CLASS, "toggle",
253 msStyle,0,0,0,0,
254 (HWND) parent->GetHWND(), (HMENU)m_windowId, wxGetInstance(), NULL);
255
256 wxCHECK_MSG( m_hWnd, "Failed to create radio button", FALSE );
257
258 #if wxUSE_CTL3D
259 if (!(GetParent()->GetWindowStyleFlag() & wxUSER_COLOURS))
260 {
261 Ctl3dSubclassCtl((HWND) GetHWND());
262 m_useCtl3D = TRUE;
263 }
264 #endif
265
266 // Subclass again for purposes of dialog editing mode
267 SubclassWin(GetHWND());
268
269 SetSize(x, y, width, height);
270
271 return TRUE;
272 }
273
274 void wxBitmapRadioButton::SetLabel(const wxBitmap *bitmap)
275 {
276 }
277
278 void wxBitmapRadioButton::SetValue(bool value)
279 {
280 // Following necessary for Win32s, because Win32s translate BM_SETCHECK
281 SendMessage((HWND) GetHWND(), BM_SETCHECK, (WPARAM)value, 0L);
282 }
283
284 // Get single selection, for single choice list items
285 bool wxBitmapRadioButton::GetValue(void) const
286 {
287 return (bool)SendMessage((HWND) GetHWND(), BM_GETCHECK, 0, 0L);
288 }
289
290 #endif
291
292 #endif // wxUSE_RADIOBTN