]> git.saurik.com Git - wxWidgets.git/blob - src/msw/checkbox.cpp
no message
[wxWidgets.git] / src / msw / checkbox.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: checkbox.cpp
3 // Purpose: wxCheckBox
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 "checkbox.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 "wx/checkbox.h"
25 #endif
26
27 #include "wx/msw/private.h"
28
29 #if !USE_SHARED_LIBRARY
30 IMPLEMENT_DYNAMIC_CLASS(wxCheckBox, wxControl)
31 IMPLEMENT_DYNAMIC_CLASS(wxBitmapCheckBox, wxCheckBox)
32 #endif
33
34 bool wxCheckBox::MSWCommand(WXUINT WXUNUSED(param), WXWORD WXUNUSED(id))
35 {
36 wxCommandEvent event(wxEVENT_TYPE_CHECKBOX_COMMAND, m_windowId);
37 event.SetInt(GetValue());
38 event.SetEventObject(this);
39 ProcessCommand(event);
40 return TRUE;
41 }
42
43 // Single check box item
44 bool wxCheckBox::Create(wxWindow *parent, wxWindowID id, const wxString& label,
45 const wxPoint& pos,
46 const wxSize& size, long style,
47 const wxValidator& validator,
48 const wxString& name)
49 {
50 SetName(name);
51 SetValidator(validator);
52 if (parent) parent->AddChild(this);
53
54 SetBackgroundColour(parent->GetBackgroundColour()) ;
55 SetForegroundColour(parent->GetForegroundColour()) ;
56
57 m_windowStyle = style;
58
59 wxString Label = label;
60 if (Label == "")
61 Label = " "; // Apparently needed or checkbox won't show
62
63 if ( id == -1 )
64 m_windowId = NewControlId();
65 else
66 m_windowId = id;
67
68 int x = pos.x;
69 int y = pos.y;
70 int width = size.x;
71 int height = size.y;
72
73 long msStyle = BS_AUTOCHECKBOX | WS_TABSTOP | WS_CHILD | WS_VISIBLE;
74 if ( style & wxALIGN_RIGHT )
75 msStyle |= BS_LEFTTEXT;
76
77 // We perhaps have different concepts of 3D here - a 3D border,
78 // versus a 3D button.
79 // So we only wish to give a border if this is specified
80 // in the style.
81 bool want3D;
82 WXDWORD exStyle = Determine3DEffects(0, &want3D) ;
83
84 // Even with extended styles, need to combine with WS_BORDER
85 // for them to look right.
86 if ( want3D || wxStyleHasBorder(m_windowStyle) )
87 msStyle |= WS_BORDER;
88
89 m_hWnd = (WXHWND)CreateWindowEx(exStyle, "BUTTON", Label,
90 msStyle,
91 0, 0, 0, 0,
92 (HWND)parent->GetHWND(), (HMENU)m_windowId,
93 wxGetInstance(), NULL);
94
95 #if CTL3D
96 if (want3D)
97 {
98 Ctl3dSubclassCtl((HWND)m_hWnd);
99 m_useCtl3D = TRUE;
100 }
101 #endif
102
103 // Subclass again for purposes of dialog editing mode
104 SubclassWin(m_hWnd);
105
106 SetFont(*parent->GetFont());
107
108 SetSize(x, y, width, height);
109
110 return TRUE;
111 }
112
113 void wxCheckBox::SetLabel(const wxString& label)
114 {
115 SetWindowText((HWND)GetHWND(), label);
116 }
117
118 void wxCheckBox::SetSize(int x, int y, int width, int height, int sizeFlags)
119 {
120 int currentX, currentY;
121 GetPosition(&currentX, &currentY);
122 int x1 = x;
123 int y1 = y;
124 int w1 = width;
125 int h1 = height;
126
127 if (x == -1 || (sizeFlags & wxSIZE_ALLOW_MINUS_ONE))
128 x1 = currentX;
129 if (y == -1 || (sizeFlags & wxSIZE_ALLOW_MINUS_ONE))
130 y1 = currentY;
131
132 AdjustForParentClientOrigin(x1, y1, sizeFlags);
133
134 int current_width, cyf;
135 HWND button = (HWND) GetHWND();
136
137 int nLen = GetWindowTextLength(button);
138 wxString str;
139 GetWindowText(button, str.GetWriteBuf(nLen), nLen);
140 str.UngetWriteBuf();
141
142 if ( !str.IsEmpty() )
143 {
144 GetTextExtent(str, &current_width, &cyf, NULL, NULL, GetFont());
145 if (w1 < 0)
146 w1 = (int)(current_width + RADIO_SIZE);
147 if (h1 < 0)
148 {
149 h1 = (int)(cyf);
150 if (h1 < RADIO_SIZE)
151 h1 = RADIO_SIZE;
152 }
153 }
154 else
155 {
156 if (w1 < 0)
157 w1 = RADIO_SIZE;
158 if (h1 < 0)
159 h1 = RADIO_SIZE;
160 }
161
162 MoveWindow(button, x1, y1, w1, h1, TRUE);
163 }
164
165 void wxCheckBox::SetValue(bool val)
166 {
167 SendMessage((HWND) GetHWND(), BM_SETCHECK, val, 0);
168 }
169
170 bool wxCheckBox::GetValue(void) const
171 {
172 #ifdef __WIN32__
173 return (SendMessage((HWND) GetHWND(), BM_GETCHECK, 0, 0) == BST_CHECKED);
174 #else
175 return ((0x003 & SendMessage((HWND) GetHWND(), BM_GETCHECK, 0, 0)) == 0x003);
176 #endif
177 }
178
179 WXHBRUSH wxCheckBox::OnCtlColor(WXHDC pDC, WXHWND pWnd, WXUINT nCtlColor,
180 WXUINT message, WXWPARAM wParam, WXLPARAM lParam)
181 {
182 #if CTL3D
183 if ( m_useCtl3D )
184 {
185 HBRUSH hbrush = Ctl3dCtlColorEx(message, wParam, lParam);
186
187 return (WXHBRUSH) hbrush;
188 }
189 #endif
190
191 if (GetParent()->GetTransparentBackground())
192 SetBkMode((HDC) pDC, TRANSPARENT);
193 else
194 SetBkMode((HDC) pDC, OPAQUE);
195
196 ::SetBkColor((HDC) pDC, RGB(GetBackgroundColour().Red(), GetBackgroundColour().Green(), GetBackgroundColour().Blue()));
197 ::SetTextColor((HDC) pDC, RGB(GetForegroundColour().Red(), GetForegroundColour().Green(), GetForegroundColour().Blue()));
198
199 wxBrush *backgroundBrush = wxTheBrushList->FindOrCreateBrush(GetBackgroundColour(), wxSOLID);
200
201 // Note that this will be cleaned up in wxApp::OnIdle, if backgroundBrush
202 // has a zero usage count.
203 // backgroundBrush->RealizeResource();
204 return (WXHBRUSH) backgroundBrush->GetResourceHandle();
205 }
206
207 void wxCheckBox::Command (wxCommandEvent & event)
208 {
209 SetValue ((event.GetInt() != 0));
210 ProcessCommand (event);
211 }
212
213 bool wxBitmapCheckBox::Create(wxWindow *parent, wxWindowID id, const wxBitmap *label,
214 const wxPoint& pos,
215 const wxSize& size, long style,
216 const wxValidator& validator,
217 const wxString& name)
218 {
219 SetName(name);
220 SetValidator(validator);
221 if (parent) parent->AddChild(this);
222
223 SetBackgroundColour(parent->GetBackgroundColour()) ;
224 SetForegroundColour(parent->GetForegroundColour()) ;
225 m_windowStyle = style;
226
227 if ( id == -1 )
228 m_windowId = NewControlId();
229 else
230 m_windowId = id;
231
232 int x = pos.x;
233 int y = pos.y;
234 int width = size.x;
235 int height = size.y;
236
237 checkWidth = -1 ;
238 checkHeight = -1 ;
239 long msStyle = CHECK_FLAGS;
240
241 HWND wx_button = CreateWindowEx(MakeExtendedStyle(m_windowStyle), CHECK_CLASS, "toggle",
242 msStyle,
243 0, 0, 0, 0, (HWND) parent->GetHWND(), (HMENU)m_windowId,
244 wxGetInstance(), NULL);
245
246 #if CTL3D
247 if (!(GetParent()->GetWindowStyleFlag() & wxUSER_COLOURS))
248 {
249 Ctl3dSubclassCtl(wx_button);
250 m_useCtl3D = TRUE;
251 }
252 #endif
253
254 m_hWnd = (WXHWND)wx_button;
255
256 // Subclass again for purposes of dialog editing mode
257 SubclassWin((WXHWND)wx_button);
258
259 // SetFont(parent->GetFont());
260
261 SetSize(x, y, width, height);
262
263 ShowWindow(wx_button, SW_SHOW);
264 return TRUE;
265 }
266
267 void wxBitmapCheckBox::SetLabel(const wxBitmap *bitmap)
268 {
269 }
270
271 void wxBitmapCheckBox::SetSize(int x, int y, int width, int height, int sizeFlags)
272 {
273 int currentX, currentY;
274 GetPosition(&currentX, &currentY);
275
276 int x1 = x;
277 int y1 = y;
278 int w1 = width;
279 int h1 = height;
280
281 if (x == -1 || (sizeFlags & wxSIZE_ALLOW_MINUS_ONE))
282 x1 = currentX;
283 if (y == -1 || (sizeFlags & wxSIZE_ALLOW_MINUS_ONE))
284 y1 = currentY;
285
286 AdjustForParentClientOrigin(x1, y1, sizeFlags);
287
288 HWND button = (HWND) GetHWND();
289 /*
290 if (w1<0)
291 w1 = checkWidth + FB_MARGIN ;
292 if (h1<0)
293 h1 = checkHeight + FB_MARGIN ;
294 */
295 MoveWindow(button, x1, y1, w1, h1, TRUE);
296 }
297
298 void wxBitmapCheckBox::SetValue(bool val)
299 {
300 SendMessage((HWND) GetHWND(), BM_SETCHECK, val, 0);
301 }
302
303 bool wxBitmapCheckBox::GetValue(void) const
304 {
305 return ((0x003 & SendMessage((HWND) GetHWND(), BM_GETCHECK, 0, 0)) == 0x003);
306 }
307
308