Sorry, I went and removed consts as per the style guide :-)
[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->GetDefaultBackgroundColour()) ;
55 SetForegroundColour(parent->GetDefaultForegroundColour()) ;
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;
74
75 // We perhaps have different concepts of 3D here - a 3D border,
76 // versus a 3D button.
77 // So we only wish to give a border if this is specified
78 // in the style.
79 bool want3D;
80 WXDWORD exStyle = Determine3DEffects(0, &want3D) ;
81
82 // Even with extended styles, need to combine with WS_BORDER
83 // for them to look right.
84 if (want3D && ((m_windowStyle & wxSIMPLE_BORDER) || (m_windowStyle & wxRAISED_BORDER) ||
85 (m_windowStyle & wxSUNKEN_BORDER) || (m_windowStyle & wxDOUBLE_BORDER)))
86 msStyle |= WS_BORDER;
87
88 HWND wx_button = CreateWindowEx(exStyle, "BUTTON", (const char *)Label,
89 msStyle,
90 0, 0, 0, 0, (HWND) parent->GetHWND(), (HMENU)m_windowId,
91 wxGetInstance(), NULL);
92
93 #if CTL3D
94 if (want3D)
95 {
96 Ctl3dSubclassCtl(wx_button);
97 m_useCtl3D = TRUE;
98 }
99 #endif
100
101 m_hWnd = (WXHWND) wx_button;
102
103 // Subclass again for purposes of dialog editing mode
104 SubclassWin((WXHWND) wx_button);
105
106 SetFont(* parent->GetFont());
107
108 SetSize(x, y, width, height);
109
110 ShowWindow(wx_button, SW_SHOW);
111 return TRUE;
112 }
113
114 void wxCheckBox::SetLabel(const wxString& label)
115 {
116 SetWindowText((HWND) GetHWND(), (const char *)label);
117 }
118
119 void wxCheckBox::SetSize(int x, int y, int width, int height, int sizeFlags)
120 {
121 int currentX, currentY;
122 GetPosition(&currentX, &currentY);
123 int x1 = x;
124 int y1 = y;
125 int w1 = width;
126 int h1 = height;
127
128 if (x == -1 || (sizeFlags & wxSIZE_ALLOW_MINUS_ONE))
129 x1 = currentX;
130 if (y == -1 || (sizeFlags & wxSIZE_ALLOW_MINUS_ONE))
131 y1 = currentY;
132
133 char buf[300];
134
135 int current_width, cyf;
136 HWND button = (HWND) GetHWND();
137
138 GetWindowText(button, buf, 300);
139 if (buf[0])
140 {
141 GetTextExtent(buf, &current_width, &cyf, NULL, NULL, GetFont());
142 if (w1 < 0)
143 w1 = (int)(current_width + RADIO_SIZE);
144 if (h1 < 0)
145 {
146 h1 = (int)(cyf);
147 if (h1 < RADIO_SIZE)
148 h1 = RADIO_SIZE;
149 }
150 }
151 else
152 {
153 if (w1 < 0)
154 w1 = RADIO_SIZE;
155 if (h1 < 0)
156 h1 = RADIO_SIZE;
157 }
158
159 MoveWindow(button, x1, y1, w1, h1, TRUE);
160 }
161
162 void wxCheckBox::SetValue(bool val)
163 {
164 SendMessage((HWND) GetHWND(), BM_SETCHECK, val, 0);
165 }
166
167 bool wxCheckBox::GetValue(void) const
168 {
169 #ifdef __WIN32__
170 return (SendMessage((HWND) GetHWND(), BM_GETCHECK, 0, 0) == BST_CHECKED);
171 #else
172 return ((0x003 & SendMessage((HWND) GetHWND(), BM_GETCHECK, 0, 0)) == 0x003);
173 #endif
174 }
175
176 WXHBRUSH wxCheckBox::OnCtlColor(WXHDC pDC, WXHWND pWnd, WXUINT nCtlColor,
177 WXUINT message, WXWPARAM wParam, WXLPARAM lParam)
178 {
179 #if CTL3D
180 if ( m_useCtl3D )
181 {
182 HBRUSH hbrush = Ctl3dCtlColorEx(message, wParam, lParam);
183
184 return (WXHBRUSH) hbrush;
185 }
186 #endif
187
188 if (GetParent()->GetTransparentBackground())
189 SetBkMode((HDC) pDC, TRANSPARENT);
190 else
191 SetBkMode((HDC) pDC, OPAQUE);
192
193 ::SetBkColor((HDC) pDC, RGB(GetBackgroundColour().Red(), GetBackgroundColour().Green(), GetBackgroundColour().Blue()));
194 ::SetTextColor((HDC) pDC, RGB(GetForegroundColour().Red(), GetForegroundColour().Green(), GetForegroundColour().Blue()));
195
196 wxBrush *backgroundBrush = wxTheBrushList->FindOrCreateBrush(GetBackgroundColour(), wxSOLID);
197
198 // Note that this will be cleaned up in wxApp::OnIdle, if backgroundBrush
199 // has a zero usage count.
200 // backgroundBrush->RealizeResource();
201 return (WXHBRUSH) backgroundBrush->GetResourceHandle();
202 }
203
204 void wxCheckBox::Command (wxCommandEvent & event)
205 {
206 SetValue ((event.GetInt() != 0));
207 ProcessCommand (event);
208 }
209
210 bool wxBitmapCheckBox::Create(wxWindow *parent, wxWindowID id, const wxBitmap *label,
211 const wxPoint& pos,
212 const wxSize& size, long style,
213 const wxValidator& validator,
214 const wxString& name)
215 {
216 SetName(name);
217 SetValidator(validator);
218 if (parent) parent->AddChild(this);
219
220 SetBackgroundColour(parent->GetDefaultBackgroundColour()) ;
221 SetForegroundColour(parent->GetDefaultForegroundColour()) ;
222 m_windowStyle = style;
223
224 if ( id == -1 )
225 m_windowId = NewControlId();
226 else
227 m_windowId = id;
228
229 int x = pos.x;
230 int y = pos.y;
231 int width = size.x;
232 int height = size.y;
233
234 checkWidth = -1 ;
235 checkHeight = -1 ;
236 long msStyle = CHECK_FLAGS;
237
238 HWND wx_button = CreateWindowEx(MakeExtendedStyle(m_windowStyle), CHECK_CLASS, "toggle",
239 msStyle,
240 0, 0, 0, 0, (HWND) parent->GetHWND(), (HMENU)m_windowId,
241 wxGetInstance(), NULL);
242
243 #if CTL3D
244 if (!(GetParent()->GetWindowStyleFlag() & wxUSER_COLOURS))
245 {
246 Ctl3dSubclassCtl(wx_button);
247 m_useCtl3D = TRUE;
248 }
249 #endif
250
251 m_hWnd = (WXHWND)wx_button;
252
253 // Subclass again for purposes of dialog editing mode
254 SubclassWin((WXHWND)wx_button);
255
256 // SetFont(parent->GetFont());
257
258 SetSize(x, y, width, height);
259
260 ShowWindow(wx_button, SW_SHOW);
261 return TRUE;
262 }
263
264 void wxBitmapCheckBox::SetLabel(const wxBitmap *bitmap)
265 {
266 }
267
268 void wxBitmapCheckBox::SetSize(int x, int y, int width, int height, int sizeFlags)
269 {
270 int currentX, currentY;
271 GetPosition(&currentX, &currentY);
272
273 int x1 = x;
274 int y1 = y;
275 int w1 = width;
276 int h1 = height;
277
278 if (x == -1 || (sizeFlags & wxSIZE_ALLOW_MINUS_ONE))
279 x1 = currentX;
280 if (y == -1 || (sizeFlags & wxSIZE_ALLOW_MINUS_ONE))
281 y1 = currentY;
282
283 HWND button = (HWND) GetHWND();
284 /*
285 if (w1<0)
286 w1 = checkWidth + FB_MARGIN ;
287 if (h1<0)
288 h1 = checkHeight + FB_MARGIN ;
289 */
290 MoveWindow(button, x1, y1, w1, h1, TRUE);
291 }
292
293 void wxBitmapCheckBox::SetValue(bool val)
294 {
295 SendMessage((HWND) GetHWND(), BM_SETCHECK, val, 0);
296 }
297
298 bool wxBitmapCheckBox::GetValue(void) const
299 {
300 return ((0x003 & SendMessage((HWND) GetHWND(), BM_GETCHECK, 0, 0)) == 0x003);
301 }
302
303