Fixes for 16-bit compilation
[wxWidgets.git] / src / msw / radiobut.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: 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 #ifndef WX_PRECOMP
24 #include <stdio.h>
25 #include "wx/setup.h"
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 bool want3D;
86 WXDWORD exStyle = Determine3DEffects(0, &want3D) ;
87
88 // Even with extended styles, need to combine with WS_BORDER
89 // for them to look right.
90 /*
91 if ( want3D || wxStyleHasBorder(m_windowStyle) )
92 msStyle |= WS_BORDER;
93 */
94
95 m_hWnd = (WXHWND) CreateWindowEx(exStyle, RADIO_CLASS, (const wxChar *)label,
96 msStyle,0,0,0,0,
97 (HWND) parent->GetHWND(), (HMENU)m_windowId, wxGetInstance(), NULL);
98
99 wxCHECK_MSG( m_hWnd, FALSE, wxT("Failed to create radiobutton") );
100
101 #if wxUSE_CTL3D
102 if (want3D)
103 {
104 Ctl3dSubclassCtl((HWND) m_hWnd);
105 m_useCtl3D = TRUE;
106 }
107 #endif
108
109 SetFont(parent->GetFont());
110
111 // Subclass again for purposes of dialog editing mode
112 SubclassWin((WXHWND)m_hWnd);
113
114 // SetValue(value);
115
116 // start GRW fix
117 if (label != wxT(""))
118 {
119 int label_width, label_height;
120 GetTextExtent(label, &label_width, &label_height, NULL, NULL, & this->GetFont());
121 if (width < 0)
122 width = (int)(label_width + RADIO_SIZE);
123 if (height<0)
124 {
125 height = (int)(label_height);
126 if (height < RADIO_SIZE)
127 height = RADIO_SIZE;
128 }
129 }
130 else
131 {
132 if (width < 0)
133 width = RADIO_SIZE;
134 if (height < 0)
135 height = RADIO_SIZE;
136 }
137 // end GRW fix
138
139 SetSize(x, y, width, height);
140
141 // for compatibility with wxGTK, the first radio button in a group is
142 // always checked (this makes sense anyhow as you need to ensure that at
143 // least one button in the group is checked and this is the simlpest way to
144 // do it)
145 if ( m_windowStyle & wxRB_GROUP )
146 SetValue(TRUE);
147
148 return TRUE;
149 }
150
151
152 void wxRadioButton::SetLabel(const wxString& label)
153 {
154 SetWindowText((HWND) GetHWND(), (const wxChar *)label);
155 }
156
157 void wxRadioButton::SetValue(bool value)
158 {
159 // Following necessary for Win32s, because Win32s translate BM_SETCHECK
160 SendMessage((HWND) GetHWND(), BM_SETCHECK, (WPARAM)value, 0L);
161 }
162
163 // Get single selection
164 bool wxRadioButton::GetValue(void) const
165 {
166 return (SendMessage((HWND) GetHWND(), BM_GETCHECK, 0, 0L) != 0);
167 }
168
169 void wxRadioButton::Command (wxCommandEvent & event)
170 {
171 SetValue ( (event.m_commandInt != 0) );
172 ProcessCommand (event);
173 }
174
175 WXHBRUSH wxRadioButton::OnCtlColor(WXHDC pDC, WXHWND WXUNUSED(pWnd), WXUINT WXUNUSED(nCtlColor),
176 #if wxUSE_CTL3D
177 WXUINT message,
178 WXWPARAM wParam,
179 WXLPARAM lParam
180 #else
181 WXUINT WXUNUSED(message),
182 WXWPARAM WXUNUSED(wParam),
183 WXLPARAM WXUNUSED(lParam)
184 #endif
185 )
186 {
187 #if wxUSE_CTL3D
188 if ( m_useCtl3D )
189 {
190 HBRUSH hbrush = Ctl3dCtlColorEx(message, wParam, lParam);
191 return (WXHBRUSH) hbrush;
192 }
193 #endif // wxUSE_CTL3D
194
195 HDC hdc = (HDC)pDC;
196 if (GetParent()->GetTransparentBackground())
197 SetBkMode(hdc, TRANSPARENT);
198 else
199 SetBkMode(hdc, OPAQUE);
200
201 wxColour colBack = GetBackgroundColour();
202
203 if (!IsEnabled())
204 colBack = wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DFACE);
205
206 ::SetBkColor(hdc, wxColourToRGB(colBack));
207 ::SetTextColor(hdc, wxColourToRGB(GetForegroundColour()));
208
209 wxBrush *brush = wxTheBrushList->FindOrCreateBrush(colBack, wxSOLID);
210
211 return (WXHBRUSH)brush->GetResourceHandle();
212 }
213
214 // Not implemented
215 #if 0
216 bool wxBitmapRadioButton::Create(wxWindow *parent, wxWindowID id,
217 const wxBitmap *bitmap,
218 const wxPoint& pos,
219 const wxSize& size, long style,
220 const wxValidator& validator,
221 const wxString& name)
222 {
223 SetName(name);
224 SetValidator(validator);
225
226 if (parent) parent->AddChild(this);
227 SetBackgroundColour(parent->GetBackgroundColour());
228 SetForegroundColour(parent->GetForegroundColour());
229
230 if ( id == -1 )
231 m_windowId = (int)NewControlId();
232 else
233 m_windowId = id;
234
235 int x = pos.x;
236 int y = pos.y;
237 int width = size.x;
238 int height = size.y;
239 m_windowStyle = style ;
240
241 long groupStyle = 0;
242 if (m_windowStyle & wxRB_GROUP)
243 groupStyle = WS_GROUP;
244
245 // long msStyle = groupStyle | RADIO_FLAGS;
246 long msStyle = groupStyle | BS_RADIOBUTTON | WS_CHILD | WS_VISIBLE ;
247
248 m_hWnd = (WXHWND) CreateWindowEx(MakeExtendedStyle(m_windowStyle), RADIO_CLASS, "toggle",
249 msStyle,0,0,0,0,
250 (HWND) parent->GetHWND(), (HMENU)m_windowId, wxGetInstance(), NULL);
251
252 wxCHECK_MSG( m_hWnd, "Failed to create radio button", FALSE );
253
254 #if wxUSE_CTL3D
255 if (!(GetParent()->GetWindowStyleFlag() & wxUSER_COLOURS))
256 {
257 Ctl3dSubclassCtl((HWND) GetHWND());
258 m_useCtl3D = TRUE;
259 }
260 #endif
261
262 // Subclass again for purposes of dialog editing mode
263 SubclassWin(GetHWND());
264
265 SetSize(x, y, width, height);
266
267 return TRUE;
268 }
269
270 void wxBitmapRadioButton::SetLabel(const wxBitmap *bitmap)
271 {
272 }
273
274 void wxBitmapRadioButton::SetValue(bool value)
275 {
276 // Following necessary for Win32s, because Win32s translate BM_SETCHECK
277 SendMessage((HWND) GetHWND(), BM_SETCHECK, (WPARAM)value, 0L);
278 }
279
280 // Get single selection, for single choice list items
281 bool wxBitmapRadioButton::GetValue(void) const
282 {
283 return (bool)SendMessage((HWND) GetHWND(), BM_GETCHECK, 0, 0L);
284 }
285
286 #endif