]> git.saurik.com Git - wxWidgets.git/blob - src/msw/radiobut.cpp
new sample
[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/brush.h"
28 #endif
29
30 #include "wx/msw/private.h"
31
32 #if !USE_SHARED_LIBRARY
33 IMPLEMENT_DYNAMIC_CLASS(wxRadioButton, wxControl)
34 // IMPLEMENT_DYNAMIC_CLASS(wxBitmapRadioButton, wxRadioButton)
35 #endif
36
37 bool wxRadioButton::MSWCommand(WXUINT param, WXWORD id)
38 {
39 if (param == BN_CLICKED)
40 {
41 wxCommandEvent event(wxEVT_COMMAND_RADIOBUTTON_SELECTED, m_windowId);
42 event.SetEventObject( this );
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 SetValidator(validator);
58
59 if (parent) parent->AddChild(this);
60
61 SetBackgroundColour(parent->GetBackgroundColour());
62 SetForegroundColour(parent->GetForegroundColour());
63
64 if ( id == -1 )
65 m_windowId = (int)NewControlId();
66 else
67 m_windowId = id;
68
69 int x = pos.x;
70 int y = pos.y;
71 int width = size.x;
72 int height = size.y;
73
74 m_windowStyle = style ;
75
76 long groupStyle = 0;
77 if (m_windowStyle & wxRB_GROUP)
78 groupStyle = WS_GROUP;
79
80 // long msStyle = groupStyle | RADIO_FLAGS;
81 long msStyle = groupStyle | BS_AUTORADIOBUTTON | WS_CHILD | WS_VISIBLE ;
82
83 bool want3D;
84 WXDWORD exStyle = Determine3DEffects(0, &want3D) ;
85
86 // Even with extended styles, need to combine with WS_BORDER
87 // for them to look right.
88 /*
89 if ( want3D || wxStyleHasBorder(m_windowStyle) )
90 msStyle |= WS_BORDER;
91 */
92
93 m_hWnd = (WXHWND) CreateWindowEx(exStyle, RADIO_CLASS, (const wxChar *)label,
94 msStyle,0,0,0,0,
95 (HWND) parent->GetHWND(), (HMENU)m_windowId, wxGetInstance(), NULL);
96
97 wxCHECK_MSG( m_hWnd, FALSE, _T("Failed to create radiobutton") );
98
99 #if wxUSE_CTL3D
100 if (want3D)
101 {
102 Ctl3dSubclassCtl((HWND) m_hWnd);
103 m_useCtl3D = TRUE;
104 }
105 #endif
106
107 SetFont(parent->GetFont());
108
109 // Subclass again for purposes of dialog editing mode
110 SubclassWin((WXHWND)m_hWnd);
111
112 // SetValue(value);
113
114 // start GRW fix
115 if (label != _T(""))
116 {
117 int label_width, label_height;
118 GetTextExtent(label, &label_width, &label_height, NULL, NULL, & this->GetFont());
119 if (width < 0)
120 width = (int)(label_width + RADIO_SIZE);
121 if (height<0)
122 {
123 height = (int)(label_height);
124 if (height < RADIO_SIZE)
125 height = RADIO_SIZE;
126 }
127 }
128 else
129 {
130 if (width < 0)
131 width = RADIO_SIZE;
132 if (height < 0)
133 height = RADIO_SIZE;
134 }
135 // end GRW fix
136
137 SetSize(x, y, width, height);
138
139 return TRUE;
140 }
141
142
143 void wxRadioButton::SetLabel(const wxString& label)
144 {
145 SetWindowText((HWND) GetHWND(), (const wxChar *)label);
146 }
147
148 void wxRadioButton::SetValue(bool value)
149 {
150 // Following necessary for Win32s, because Win32s translate BM_SETCHECK
151 SendMessage((HWND) GetHWND(), BM_SETCHECK, (WPARAM)value, 0L);
152 }
153
154 // Get single selection
155 bool wxRadioButton::GetValue(void) const
156 {
157 return (SendMessage((HWND) GetHWND(), BM_GETCHECK, 0, 0L) != 0);
158 }
159
160 WXHBRUSH wxRadioButton::OnCtlColor(WXHDC pDC, WXHWND pWnd, WXUINT nCtlColor,
161 WXUINT message, WXWPARAM wParam, WXLPARAM lParam)
162 {
163 #if wxUSE_CTL3D
164 if ( m_useCtl3D )
165 {
166 HBRUSH hbrush = Ctl3dCtlColorEx(message, wParam, lParam);
167 return (WXHBRUSH) hbrush;
168 }
169 #endif
170
171 if (GetParent()->GetTransparentBackground())
172 SetBkMode((HDC) pDC, TRANSPARENT);
173 else
174 SetBkMode((HDC) pDC, OPAQUE);
175
176 ::SetBkColor((HDC) pDC, RGB(GetBackgroundColour().Red(), GetBackgroundColour().Green(), GetBackgroundColour().Blue()));
177 ::SetTextColor((HDC) pDC, RGB(GetForegroundColour().Red(), GetForegroundColour().Green(), GetForegroundColour().Blue()));
178
179 wxBrush *backgroundBrush = wxTheBrushList->FindOrCreateBrush(GetBackgroundColour(), wxSOLID);
180
181 // Note that this will be cleaned up in wxApp::OnIdle, if backgroundBrush
182 // has a zero usage count.
183 // backgroundBrush->RealizeResource();
184 return (WXHBRUSH) backgroundBrush->GetResourceHandle();
185 }
186
187 void wxRadioButton::Command (wxCommandEvent & event)
188 {
189 SetValue ( (event.m_commandInt != 0) );
190 ProcessCommand (event);
191 }
192
193
194 // Not implemented
195 #if 0
196 bool wxBitmapRadioButton::Create(wxWindow *parent, wxWindowID id,
197 const wxBitmap *bitmap,
198 const wxPoint& pos,
199 const wxSize& size, long style,
200 const wxValidator& validator,
201 const wxString& name)
202 {
203 SetName(name);
204 SetValidator(validator);
205
206 if (parent) parent->AddChild(this);
207 SetBackgroundColour(parent->GetBackgroundColour());
208 SetForegroundColour(parent->GetForegroundColour());
209
210 if ( id == -1 )
211 m_windowId = (int)NewControlId();
212 else
213 m_windowId = id;
214
215 int x = pos.x;
216 int y = pos.y;
217 int width = size.x;
218 int height = size.y;
219 m_windowStyle = style ;
220
221 long groupStyle = 0;
222 if (m_windowStyle & wxRB_GROUP)
223 groupStyle = WS_GROUP;
224
225 // long msStyle = groupStyle | RADIO_FLAGS;
226 long msStyle = groupStyle | BS_RADIOBUTTON | WS_CHILD | WS_VISIBLE ;
227
228 m_hWnd = (WXHWND) CreateWindowEx(MakeExtendedStyle(m_windowStyle), RADIO_CLASS, "toggle",
229 msStyle,0,0,0,0,
230 (HWND) parent->GetHWND(), (HMENU)m_windowId, wxGetInstance(), NULL);
231
232 wxCHECK_MSG( m_hWnd, "Failed to create radio button", FALSE );
233
234 #if wxUSE_CTL3D
235 if (!(GetParent()->GetWindowStyleFlag() & wxUSER_COLOURS))
236 {
237 Ctl3dSubclassCtl((HWND) GetHWND());
238 m_useCtl3D = TRUE;
239 }
240 #endif
241
242 // Subclass again for purposes of dialog editing mode
243 SubclassWin(GetHWND());
244
245 SetSize(x, y, width, height);
246
247 return TRUE;
248 }
249
250 void wxBitmapRadioButton::SetLabel(const wxBitmap *bitmap)
251 {
252 }
253
254 void wxBitmapRadioButton::SetValue(bool value)
255 {
256 // Following necessary for Win32s, because Win32s translate BM_SETCHECK
257 SendMessage((HWND) GetHWND(), BM_SETCHECK, (WPARAM)value, 0L);
258 }
259
260 // Get single selection, for single choice list items
261 bool wxBitmapRadioButton::GetValue(void) const
262 {
263 return (bool)SendMessage((HWND) GetHWND(), BM_GETCHECK, 0, 0L);
264 }
265
266 #endif