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