]>
Commit | Line | Data |
---|---|---|
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 | // ============================================================================ | |
13 | // declarations | |
14 | // ============================================================================ | |
15 | ||
16 | // ---------------------------------------------------------------------------- | |
17 | // headers | |
18 | // ---------------------------------------------------------------------------- | |
19 | ||
20 | #ifdef __GNUG__ | |
21 | #pragma implementation "checkbox.h" | |
22 | #endif | |
23 | ||
24 | // For compilers that support precompilation, includes "wx.h". | |
25 | #include "wx/wxprec.h" | |
26 | ||
27 | #ifdef __BORLANDC__ | |
28 | #pragma hdrstop | |
29 | #endif | |
30 | ||
31 | #ifndef WX_PRECOMP | |
32 | #include "wx/checkbox.h" | |
33 | #include "wx/brush.h" | |
34 | #endif | |
35 | ||
36 | #include "wx/msw/private.h" | |
37 | ||
38 | // ---------------------------------------------------------------------------- | |
39 | // macros | |
40 | // ---------------------------------------------------------------------------- | |
41 | ||
42 | IMPLEMENT_DYNAMIC_CLASS(wxCheckBox, wxControl) | |
43 | IMPLEMENT_DYNAMIC_CLASS(wxBitmapCheckBox, wxCheckBox) | |
44 | ||
45 | // ============================================================================ | |
46 | // implementation | |
47 | // ============================================================================ | |
48 | ||
49 | // ---------------------------------------------------------------------------- | |
50 | // wxCheckBox | |
51 | // ---------------------------------------------------------------------------- | |
52 | ||
53 | bool wxCheckBox::MSWCommand(WXUINT WXUNUSED(param), WXWORD WXUNUSED(id)) | |
54 | { | |
55 | wxCommandEvent event(wxEVT_COMMAND_CHECKBOX_CLICKED, m_windowId); | |
56 | event.SetInt(GetValue()); | |
57 | event.SetEventObject(this); | |
58 | ProcessCommand(event); | |
59 | return TRUE; | |
60 | } | |
61 | ||
62 | // Single check box item | |
63 | bool wxCheckBox::Create(wxWindow *parent, | |
64 | wxWindowID id, | |
65 | const wxString& label, | |
66 | const wxPoint& pos, | |
67 | const wxSize& size, long style, | |
68 | const wxValidator& validator, | |
69 | const wxString& name) | |
70 | { | |
71 | SetName(name); | |
72 | #if wxUSE_VALIDATORS | |
73 | SetValidator(validator); | |
74 | #endif // wxUSE_VALIDATORS | |
75 | if (parent) parent->AddChild(this); | |
76 | ||
77 | SetBackgroundColour(parent->GetBackgroundColour()) ; | |
78 | SetForegroundColour(parent->GetForegroundColour()) ; | |
79 | ||
80 | m_windowStyle = style; | |
81 | ||
82 | // VZ: disabling this ugliness which completely breaks checkboxes in wxGrid | |
83 | // whoever did it, please tell me where and how does the checkbox fail | |
84 | // to appear | |
85 | #if 0 | |
86 | wxString Label = label; | |
87 | if (Label == wxT("")) | |
88 | Label = wxT(" "); // Apparently needed or checkbox won't show | |
89 | #endif // 0 | |
90 | ||
91 | if ( id == -1 ) | |
92 | m_windowId = NewControlId(); | |
93 | else | |
94 | m_windowId = id; | |
95 | ||
96 | int x = pos.x; | |
97 | int y = pos.y; | |
98 | int width = size.x; | |
99 | int height = size.y; | |
100 | ||
101 | long msStyle = BS_AUTOCHECKBOX | WS_TABSTOP | WS_CHILD | WS_VISIBLE; | |
102 | if ( style & wxALIGN_RIGHT ) | |
103 | msStyle |= BS_LEFTTEXT; | |
104 | ||
105 | // We perhaps have different concepts of 3D here - a 3D border, | |
106 | // versus a 3D button. | |
107 | // So we only wish to give a border if this is specified | |
108 | // in the style. | |
109 | bool want3D; | |
110 | WXDWORD exStyle = Determine3DEffects(0, &want3D) ; | |
111 | ||
112 | // Even with extended styles, need to combine with WS_BORDER | |
113 | // for them to look right. | |
114 | /* | |
115 | if ( want3D || wxStyleHasBorder(m_windowStyle) ) | |
116 | msStyle |= WS_BORDER; | |
117 | */ | |
118 | ||
119 | m_hWnd = (WXHWND)CreateWindowEx(exStyle, wxT("BUTTON"), label, | |
120 | msStyle, | |
121 | 0, 0, 0, 0, | |
122 | (HWND)parent->GetHWND(), (HMENU)m_windowId, | |
123 | wxGetInstance(), NULL); | |
124 | ||
125 | #if wxUSE_CTL3D | |
126 | if (want3D) | |
127 | { | |
128 | Ctl3dSubclassCtl(GetHwnd()); | |
129 | m_useCtl3D = TRUE; | |
130 | } | |
131 | #endif | |
132 | ||
133 | // Subclass again for purposes of dialog editing mode | |
134 | SubclassWin(m_hWnd); | |
135 | ||
136 | SetFont(parent->GetFont()); | |
137 | ||
138 | SetSize(x, y, width, height); | |
139 | ||
140 | return TRUE; | |
141 | } | |
142 | ||
143 | void wxCheckBox::SetLabel(const wxString& label) | |
144 | { | |
145 | SetWindowText(GetHwnd(), label); | |
146 | } | |
147 | ||
148 | #define CHECK_SIZE 13 | |
149 | ||
150 | wxSize wxCheckBox::DoGetBestSize() const | |
151 | { | |
152 | int wCheckbox, hCheckbox; | |
153 | ||
154 | wxString str = wxGetWindowText(GetHWND()) + " "; | |
155 | ||
156 | if ( !str.IsEmpty() ) | |
157 | { | |
158 | GetTextExtent(str, &wCheckbox, &hCheckbox); | |
159 | wCheckbox += CHECK_SIZE; | |
160 | ||
161 | if ( hCheckbox < CHECK_SIZE ) | |
162 | hCheckbox = CHECK_SIZE; | |
163 | } | |
164 | else | |
165 | { | |
166 | wCheckbox = CHECK_SIZE; | |
167 | hCheckbox = CHECK_SIZE; | |
168 | } | |
169 | ||
170 | return wxSize(wCheckbox, hCheckbox); | |
171 | } | |
172 | ||
173 | void wxCheckBox::SetValue(bool val) | |
174 | { | |
175 | SendMessage(GetHwnd(), BM_SETCHECK, val, 0); | |
176 | } | |
177 | ||
178 | #ifndef BST_CHECKED | |
179 | #define BST_CHECKED 0x0001 | |
180 | #endif | |
181 | ||
182 | bool wxCheckBox::GetValue() const | |
183 | { | |
184 | #ifdef __WIN32__ | |
185 | return (SendMessage(GetHwnd(), BM_GETCHECK, 0, 0) == BST_CHECKED); | |
186 | #else | |
187 | return ((0x003 & SendMessage(GetHwnd(), BM_GETCHECK, 0, 0)) == 0x003); | |
188 | #endif | |
189 | } | |
190 | ||
191 | void wxCheckBox::Command (wxCommandEvent & event) | |
192 | { | |
193 | SetValue ((event.GetInt() != 0)); | |
194 | ProcessCommand (event); | |
195 | } | |
196 | ||
197 | // ---------------------------------------------------------------------------- | |
198 | // wxBitmapCheckBox | |
199 | // ---------------------------------------------------------------------------- | |
200 | ||
201 | bool wxBitmapCheckBox::Create(wxWindow *parent, wxWindowID id, const wxBitmap *label, | |
202 | const wxPoint& pos, | |
203 | const wxSize& size, long style, | |
204 | const wxValidator& validator, | |
205 | const wxString& name) | |
206 | { | |
207 | SetName(name); | |
208 | #if wxUSE_VALIDATORS | |
209 | SetValidator(validator); | |
210 | #endif // wxUSE_VALIDATORS | |
211 | if (parent) parent->AddChild(this); | |
212 | ||
213 | SetBackgroundColour(parent->GetBackgroundColour()) ; | |
214 | SetForegroundColour(parent->GetForegroundColour()) ; | |
215 | m_windowStyle = style; | |
216 | ||
217 | if ( id == -1 ) | |
218 | m_windowId = NewControlId(); | |
219 | else | |
220 | m_windowId = id; | |
221 | ||
222 | int x = pos.x; | |
223 | int y = pos.y; | |
224 | int width = size.x; | |
225 | int height = size.y; | |
226 | ||
227 | checkWidth = -1 ; | |
228 | checkHeight = -1 ; | |
229 | long msStyle = CHECK_FLAGS; | |
230 | ||
231 | HWND wx_button = CreateWindowEx(MakeExtendedStyle(m_windowStyle), CHECK_CLASS, wxT("toggle"), | |
232 | msStyle, | |
233 | 0, 0, 0, 0, (HWND) parent->GetHWND(), (HMENU)m_windowId, | |
234 | wxGetInstance(), NULL); | |
235 | ||
236 | #if wxUSE_CTL3D | |
237 | if (!(GetParent()->GetWindowStyleFlag() & wxUSER_COLOURS)) | |
238 | { | |
239 | Ctl3dSubclassCtl(wx_button); | |
240 | m_useCtl3D = TRUE; | |
241 | } | |
242 | #endif | |
243 | ||
244 | m_hWnd = (WXHWND)wx_button; | |
245 | ||
246 | // Subclass again for purposes of dialog editing mode | |
247 | SubclassWin((WXHWND)wx_button); | |
248 | ||
249 | SetSize(x, y, width, height); | |
250 | ||
251 | ShowWindow(wx_button, SW_SHOW); | |
252 | ||
253 | return TRUE; | |
254 | } | |
255 | ||
256 | void wxBitmapCheckBox::SetLabel(const wxBitmap& bitmap) | |
257 | { | |
258 | wxFAIL_MSG(wxT("not implemented")); | |
259 | } |