added some wxMSW stuff
[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(const WXUINT WXUNUSED(param), const 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, const wxWindowID id, const wxString& label,
45 const wxPoint& pos,
46 const wxSize& size, const 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(const int x, const int y, const int width, const int height, const 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 float current_width;
136
137 float cyf;
138
139 HWND button = (HWND) GetHWND();
140
141 GetWindowText(button, buf, 300);
142 if (buf[0])
143 {
144 GetTextExtent(buf, &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 #if WXWIN_COMPATIBILITY
166 GetEventHandler()->OldOnSize(width, height);
167 #else
168 wxSizeEvent event(wxSize(width, height), m_windowId);
169 event.eventObject = this;
170 GetEventHandler()->ProcessEvent(event);
171 #endif
172 */
173
174 }
175
176 void wxCheckBox::SetValue(const bool val)
177 {
178 SendMessage((HWND) GetHWND(), BM_SETCHECK, val, 0);
179 }
180
181 bool wxCheckBox::GetValue(void) const
182 {
183 #ifdef __WIN32__
184 return (SendMessage((HWND) GetHWND(), BM_GETCHECK, 0, 0) == BST_CHECKED);
185 #else
186 return ((0x003 & SendMessage((HWND) GetHWND(), BM_GETCHECK, 0, 0)) == 0x003);
187 #endif
188 }
189
190 WXHBRUSH wxCheckBox::OnCtlColor(const WXHDC pDC, const WXHWND pWnd, const WXUINT nCtlColor,
191 WXUINT message, WXWPARAM wParam, WXLPARAM lParam)
192 {
193 #if CTL3D
194 if ( m_useCtl3D )
195 {
196 HBRUSH hbrush = Ctl3dCtlColorEx(message, wParam, lParam);
197
198 return (WXHBRUSH) hbrush;
199 }
200 #endif
201
202 if (GetParent()->GetTransparentBackground())
203 SetBkMode((HDC) pDC, TRANSPARENT);
204 else
205 SetBkMode((HDC) pDC, OPAQUE);
206
207 ::SetBkColor((HDC) pDC, RGB(GetBackgroundColour().Red(), GetBackgroundColour().Green(), GetBackgroundColour().Blue()));
208 ::SetTextColor((HDC) pDC, RGB(GetForegroundColour().Red(), GetForegroundColour().Green(), GetForegroundColour().Blue()));
209
210 wxBrush *backgroundBrush = wxTheBrushList->FindOrCreateBrush(GetBackgroundColour(), wxSOLID);
211
212 // Note that this will be cleaned up in wxApp::OnIdle, if backgroundBrush
213 // has a zero usage count.
214 // backgroundBrush->RealizeResource();
215 return (WXHBRUSH) backgroundBrush->GetResourceHandle();
216 }
217
218 void wxCheckBox::Command (wxCommandEvent & event)
219 {
220 SetValue ((event.GetInt() != 0));
221 ProcessCommand (event);
222 }
223
224 bool wxBitmapCheckBox::Create(wxWindow *parent, const wxWindowID id, const wxBitmap *label,
225 const wxPoint& pos,
226 const wxSize& size, const long style,
227 const wxValidator& validator,
228 const wxString& name)
229 {
230 SetName(name);
231 SetValidator(validator);
232 if (parent) parent->AddChild(this);
233
234 SetBackgroundColour(parent->GetDefaultBackgroundColour()) ;
235 SetForegroundColour(parent->GetDefaultForegroundColour()) ;
236 m_windowStyle = style;
237
238 if ( id == -1 )
239 m_windowId = NewControlId();
240 else
241 m_windowId = id;
242
243 int x = pos.x;
244 int y = pos.y;
245 int width = size.x;
246 int height = size.y;
247
248 checkWidth = -1 ;
249 checkHeight = -1 ;
250 long msStyle = CHECK_FLAGS;
251
252 HWND wx_button = CreateWindowEx(MakeExtendedStyle(m_windowStyle), CHECK_CLASS, "toggle",
253 msStyle,
254 0, 0, 0, 0, (HWND) parent->GetHWND(), (HMENU)m_windowId,
255 wxGetInstance(), NULL);
256
257 #if CTL3D
258 if (!(GetParent()->GetWindowStyleFlag() & wxUSER_COLOURS))
259 {
260 Ctl3dSubclassCtl(wx_button);
261 m_useCtl3D = TRUE;
262 }
263 #endif
264
265 m_hWnd = (WXHWND)wx_button;
266
267 // Subclass again for purposes of dialog editing mode
268 SubclassWin((WXHWND)wx_button);
269
270 // SetFont(parent->GetFont());
271
272 SetSize(x, y, width, height);
273
274 ShowWindow(wx_button, SW_SHOW);
275 return TRUE;
276 }
277
278 void wxBitmapCheckBox::SetLabel(const wxBitmap *bitmap)
279 {
280 }
281
282 void wxBitmapCheckBox::SetSize(const int x, const int y, const int width, const int height, const int sizeFlags)
283 {
284 int currentX, currentY;
285 GetPosition(&currentX, &currentY);
286
287 int x1 = x;
288 int y1 = y;
289 int w1 = width;
290 int h1 = height;
291
292 if (x == -1 || (sizeFlags & wxSIZE_ALLOW_MINUS_ONE))
293 x1 = currentX;
294 if (y == -1 || (sizeFlags & wxSIZE_ALLOW_MINUS_ONE))
295 y1 = currentY;
296
297 HWND button = (HWND) GetHWND();
298 /*
299 if (w1<0)
300 w1 = checkWidth + FB_MARGIN ;
301 if (h1<0)
302 h1 = checkHeight + FB_MARGIN ;
303 */
304 MoveWindow(button, x1, y1, w1, h1, TRUE);
305
306 #if WXWIN_COMPATIBILITY
307 GetEventHandler()->OldOnSize(width, height);
308 #else
309 wxSizeEvent event(wxSize(width, height), m_windowId);
310 event.eventObject = this;
311 GetEventHandler()->ProcessEvent(event);
312 #endif
313 }
314
315 void wxBitmapCheckBox::SetValue(const bool val)
316 {
317 SendMessage((HWND) GetHWND(), BM_SETCHECK, val, 0);
318 }
319
320 bool wxBitmapCheckBox::GetValue(void) const
321 {
322 return ((0x003 & SendMessage((HWND) GetHWND(), BM_GETCHECK, 0, 0)) == 0x003);
323 }
324
325