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