]>
Commit | Line | Data |
---|---|---|
2bda0e17 KB |
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 | ||
debe6624 | 34 | bool wxCheckBox::MSWCommand(WXUINT WXUNUSED(param), WXWORD WXUNUSED(id)) |
2bda0e17 KB |
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 | |
debe6624 | 44 | bool wxCheckBox::Create(wxWindow *parent, wxWindowID id, const wxString& label, |
2bda0e17 | 45 | const wxPoint& pos, |
debe6624 | 46 | const wxSize& size, long style, |
2bda0e17 KB |
47 | const wxValidator& validator, |
48 | const wxString& name) | |
49 | { | |
50 | SetName(name); | |
51 | SetValidator(validator); | |
52 | if (parent) parent->AddChild(this); | |
53 | ||
fd71308f JS |
54 | SetBackgroundColour(parent->GetBackgroundColour()) ; |
55 | SetForegroundColour(parent->GetForegroundColour()) ; | |
2bda0e17 KB |
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 | ||
27fda0b6 VZ |
73 | long msStyle = BS_AUTOCHECKBOX | WS_TABSTOP | WS_CHILD | WS_VISIBLE; |
74 | if ( style & wxALIGN_RIGHT ) | |
75 | msStyle |= BS_LEFTTEXT; | |
2bda0e17 KB |
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. | |
c085e333 | 86 | if ( want3D || wxStyleHasBorder(m_windowStyle) ) |
2bda0e17 KB |
87 | msStyle |= WS_BORDER; |
88 | ||
27fda0b6 VZ |
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); | |
2bda0e17 KB |
94 | |
95 | #if CTL3D | |
96 | if (want3D) | |
97 | { | |
27fda0b6 | 98 | Ctl3dSubclassCtl((HWND)m_hWnd); |
2bda0e17 KB |
99 | m_useCtl3D = TRUE; |
100 | } | |
101 | #endif | |
102 | ||
2bda0e17 | 103 | // Subclass again for purposes of dialog editing mode |
27fda0b6 | 104 | SubclassWin(m_hWnd); |
2bda0e17 | 105 | |
27fda0b6 | 106 | SetFont(*parent->GetFont()); |
2bda0e17 KB |
107 | |
108 | SetSize(x, y, width, height); | |
109 | ||
2bda0e17 KB |
110 | return TRUE; |
111 | } | |
112 | ||
113 | void wxCheckBox::SetLabel(const wxString& label) | |
114 | { | |
27fda0b6 | 115 | SetWindowText((HWND)GetHWND(), label); |
2bda0e17 KB |
116 | } |
117 | ||
debe6624 | 118 | void wxCheckBox::SetSize(int x, int y, int width, int height, int sizeFlags) |
2bda0e17 KB |
119 | { |
120 | int currentX, currentY; | |
121 | GetPosition(¤tX, ¤tY); | |
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 | ||
81d66cf3 JS |
132 | AdjustForParentClientOrigin(x1, y1, sizeFlags); |
133 | ||
debe6624 | 134 | int current_width, cyf; |
2bda0e17 KB |
135 | HWND button = (HWND) GetHWND(); |
136 | ||
27fda0b6 VZ |
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, ¤t_width, &cyf, NULL, NULL, GetFont()); | |
145 | if (w1 < 0) | |
146 | w1 = (int)(current_width + RADIO_SIZE); | |
147 | if (h1 < 0) | |
2bda0e17 | 148 | { |
27fda0b6 VZ |
149 | h1 = (int)(cyf); |
150 | if (h1 < RADIO_SIZE) | |
2bda0e17 KB |
151 | h1 = RADIO_SIZE; |
152 | } | |
27fda0b6 VZ |
153 | } |
154 | else | |
155 | { | |
156 | if (w1 < 0) | |
157 | w1 = RADIO_SIZE; | |
158 | if (h1 < 0) | |
159 | h1 = RADIO_SIZE; | |
160 | } | |
2bda0e17 KB |
161 | |
162 | MoveWindow(button, x1, y1, w1, h1, TRUE); | |
2bda0e17 KB |
163 | } |
164 | ||
debe6624 | 165 | void wxCheckBox::SetValue(bool val) |
2bda0e17 KB |
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 | ||
debe6624 | 179 | WXHBRUSH wxCheckBox::OnCtlColor(WXHDC pDC, WXHWND pWnd, WXUINT nCtlColor, |
2bda0e17 KB |
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 | ||
debe6624 | 213 | bool wxBitmapCheckBox::Create(wxWindow *parent, wxWindowID id, const wxBitmap *label, |
2bda0e17 | 214 | const wxPoint& pos, |
debe6624 | 215 | const wxSize& size, long style, |
2bda0e17 KB |
216 | const wxValidator& validator, |
217 | const wxString& name) | |
218 | { | |
219 | SetName(name); | |
220 | SetValidator(validator); | |
221 | if (parent) parent->AddChild(this); | |
222 | ||
fd71308f JS |
223 | SetBackgroundColour(parent->GetBackgroundColour()) ; |
224 | SetForegroundColour(parent->GetForegroundColour()) ; | |
2bda0e17 KB |
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 | ||
debe6624 | 271 | void wxBitmapCheckBox::SetSize(int x, int y, int width, int height, int sizeFlags) |
2bda0e17 KB |
272 | { |
273 | int currentX, currentY; | |
274 | GetPosition(¤tX, ¤tY); | |
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 | ||
81d66cf3 JS |
286 | AdjustForParentClientOrigin(x1, y1, sizeFlags); |
287 | ||
2bda0e17 KB |
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); | |
2bda0e17 KB |
296 | } |
297 | ||
debe6624 | 298 | void wxBitmapCheckBox::SetValue(bool val) |
2bda0e17 KB |
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 |