]>
Commit | Line | Data |
---|---|---|
2bda0e17 | 1 | ///////////////////////////////////////////////////////////////////////////// |
2b5f62a0 | 2 | // Name: msw/checkbox.cpp |
2bda0e17 KB |
3 | // Purpose: wxCheckBox |
4 | // Author: Julian Smart | |
5 | // Modified by: | |
6 | // Created: 04/01/98 | |
7 | // RCS-ID: $Id$ | |
6c9a19aa JS |
8 | // Copyright: (c) Julian Smart |
9 | // Licence: wxWindows licence | |
2bda0e17 KB |
10 | ///////////////////////////////////////////////////////////////////////////// |
11 | ||
4438caf4 VZ |
12 | // ============================================================================ |
13 | // declarations | |
14 | // ============================================================================ | |
15 | ||
16 | // ---------------------------------------------------------------------------- | |
17 | // headers | |
18 | // ---------------------------------------------------------------------------- | |
19 | ||
14f355c2 | 20 | #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA) |
4438caf4 | 21 | #pragma implementation "checkbox.h" |
2bda0e17 KB |
22 | #endif |
23 | ||
24 | // For compilers that support precompilation, includes "wx.h". | |
25 | #include "wx/wxprec.h" | |
26 | ||
27 | #ifdef __BORLANDC__ | |
4438caf4 | 28 | #pragma hdrstop |
2bda0e17 KB |
29 | #endif |
30 | ||
1e6feb95 VZ |
31 | #if wxUSE_CHECKBOX |
32 | ||
2bda0e17 | 33 | #ifndef WX_PRECOMP |
4438caf4 VZ |
34 | #include "wx/checkbox.h" |
35 | #include "wx/brush.h" | |
f6bcfd97 BP |
36 | #include "wx/dcscreen.h" |
37 | #include "wx/settings.h" | |
2bda0e17 KB |
38 | #endif |
39 | ||
40 | #include "wx/msw/private.h" | |
41 | ||
8941fa88 VZ |
42 | #ifndef BST_UNCHECKED |
43 | #define BST_UNCHECKED 0x0000 | |
44 | #endif | |
45 | ||
2b5f62a0 VZ |
46 | #ifndef BST_CHECKED |
47 | #define BST_CHECKED 0x0001 | |
48 | #endif | |
2bda0e17 | 49 | |
8941fa88 VZ |
50 | #ifndef BST_INDETERMINATE |
51 | #define BST_INDETERMINATE 0x0002 | |
52 | #endif | |
53 | ||
4438caf4 VZ |
54 | // ============================================================================ |
55 | // implementation | |
56 | // ============================================================================ | |
57 | ||
51741307 | 58 | #if wxUSE_EXTENDED_RTTI |
bc9fb572 JS |
59 | WX_DEFINE_FLAGS( wxCheckBoxStyle ) |
60 | ||
3ff066a4 | 61 | wxBEGIN_FLAGS( wxCheckBoxStyle ) |
bc9fb572 JS |
62 | // new style border flags, we put them first to |
63 | // use them for streaming out | |
3ff066a4 SC |
64 | wxFLAGS_MEMBER(wxBORDER_SIMPLE) |
65 | wxFLAGS_MEMBER(wxBORDER_SUNKEN) | |
66 | wxFLAGS_MEMBER(wxBORDER_DOUBLE) | |
67 | wxFLAGS_MEMBER(wxBORDER_RAISED) | |
68 | wxFLAGS_MEMBER(wxBORDER_STATIC) | |
69 | wxFLAGS_MEMBER(wxBORDER_NONE) | |
bc9fb572 JS |
70 | |
71 | // old style border flags | |
3ff066a4 SC |
72 | wxFLAGS_MEMBER(wxSIMPLE_BORDER) |
73 | wxFLAGS_MEMBER(wxSUNKEN_BORDER) | |
74 | wxFLAGS_MEMBER(wxDOUBLE_BORDER) | |
75 | wxFLAGS_MEMBER(wxRAISED_BORDER) | |
76 | wxFLAGS_MEMBER(wxSTATIC_BORDER) | |
8941fa88 | 77 | wxFLAGS_MEMBER(wxNO_BORDER) |
bc9fb572 JS |
78 | |
79 | // standard window styles | |
3ff066a4 SC |
80 | wxFLAGS_MEMBER(wxTAB_TRAVERSAL) |
81 | wxFLAGS_MEMBER(wxCLIP_CHILDREN) | |
82 | wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW) | |
83 | wxFLAGS_MEMBER(wxWANTS_CHARS) | |
8941fa88 | 84 | wxFLAGS_MEMBER(wxNO_FULL_REPAINT_ON_RESIZE) |
3ff066a4 SC |
85 | wxFLAGS_MEMBER(wxALWAYS_SHOW_SB ) |
86 | wxFLAGS_MEMBER(wxVSCROLL) | |
87 | wxFLAGS_MEMBER(wxHSCROLL) | |
bc9fb572 | 88 | |
3ff066a4 | 89 | wxEND_FLAGS( wxCheckBoxStyle ) |
bc9fb572 | 90 | |
51741307 SC |
91 | IMPLEMENT_DYNAMIC_CLASS_XTI(wxCheckBox, wxControl,"wx/checkbox.h") |
92 | ||
3ff066a4 SC |
93 | wxBEGIN_PROPERTIES_TABLE(wxCheckBox) |
94 | wxEVENT_PROPERTY( Click , wxEVT_COMMAND_CHECKBOX_CLICKED , wxCommandEvent ) | |
51741307 | 95 | |
3ff066a4 SC |
96 | wxPROPERTY( Font , wxFont , SetFont , GetFont , , 0 /*flags*/ , wxT("Helpstring") , wxT("group")) |
97 | wxPROPERTY( Label,wxString, SetLabel, GetLabel, wxString() , 0 /*flags*/ , wxT("Helpstring") , wxT("group")) | |
98 | wxPROPERTY( Value ,bool, SetValue, GetValue, , 0 /*flags*/ , wxT("Helpstring") , wxT("group")) | |
99 | wxPROPERTY_FLAGS( WindowStyle , wxCheckBoxStyle , long , SetWindowStyleFlag , GetWindowStyleFlag , , 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style | |
100 | wxEND_PROPERTIES_TABLE() | |
2b5f62a0 | 101 | |
3ff066a4 SC |
102 | wxBEGIN_HANDLERS_TABLE(wxCheckBox) |
103 | wxEND_HANDLERS_TABLE() | |
51741307 | 104 | |
3ff066a4 | 105 | wxCONSTRUCTOR_6( wxCheckBox , wxWindow* , Parent , wxWindowID , Id , wxString , Label , wxPoint , Position , wxSize , Size , long , WindowStyle ) |
51741307 SC |
106 | #else |
107 | IMPLEMENT_DYNAMIC_CLASS(wxCheckBox, wxControl) | |
108 | #endif | |
066f1b7a | 109 | |
066f1b7a | 110 | |
4438caf4 VZ |
111 | // ---------------------------------------------------------------------------- |
112 | // wxCheckBox | |
113 | // ---------------------------------------------------------------------------- | |
114 | ||
debe6624 | 115 | bool wxCheckBox::MSWCommand(WXUINT WXUNUSED(param), WXWORD WXUNUSED(id)) |
2bda0e17 | 116 | { |
4438caf4 | 117 | wxCommandEvent event(wxEVT_COMMAND_CHECKBOX_CLICKED, m_windowId); |
8941fa88 VZ |
118 | wxCheckBoxState state = Get3StateValue(); |
119 | ||
120 | // If the style flag to allow the user setting the undetermined state | |
121 | // is not set, then skip the undetermined state and set it to unchecked. | |
122 | if ( state == wxCHK_UNDETERMINED && !Is3rdStateAllowedForUser() ) | |
123 | { | |
124 | state = wxCHK_UNCHECKED; | |
125 | Set3StateValue(state); | |
126 | } | |
127 | ||
128 | event.SetInt(state); | |
4438caf4 VZ |
129 | event.SetEventObject(this); |
130 | ProcessCommand(event); | |
8941fa88 | 131 | |
4438caf4 | 132 | return TRUE; |
2bda0e17 KB |
133 | } |
134 | ||
11b6a93b VZ |
135 | bool wxCheckBox::Create(wxWindow *parent, |
136 | wxWindowID id, | |
137 | const wxString& label, | |
138 | const wxPoint& pos, | |
139 | const wxSize& size, long style, | |
140 | const wxValidator& validator, | |
141 | const wxString& name) | |
2bda0e17 | 142 | { |
787a85c2 | 143 | if ( !CreateControl(parent, id, pos, size, style, validator, name) ) |
8912d7eb | 144 | return FALSE; |
4438caf4 | 145 | |
8941fa88 VZ |
146 | m_style = style; |
147 | ||
148 | long msStyle = WS_TABSTOP; | |
149 | ||
150 | if ( style & wxCHK_3STATE ) | |
151 | { | |
152 | msStyle |= BS_AUTO3STATE; | |
153 | } | |
154 | else | |
155 | { | |
156 | wxASSERT_MSG( !Is3rdStateAllowedForUser(), | |
157 | wxT("Using wxCH_ALLOW_3RD_STATE_FOR_USER") | |
158 | wxT(" style flag for a 2-state checkbox is useless") ); | |
159 | msStyle |= BS_AUTOCHECKBOX; | |
160 | } | |
161 | ||
4438caf4 | 162 | if ( style & wxALIGN_RIGHT ) |
8941fa88 | 163 | { |
4438caf4 | 164 | msStyle |= BS_LEFTTEXT; |
8941fa88 | 165 | } |
4438caf4 | 166 | |
8912d7eb | 167 | return MSWCreateControl(wxT("BUTTON"), msStyle, pos, size, label, 0); |
2bda0e17 KB |
168 | } |
169 | ||
170 | void wxCheckBox::SetLabel(const wxString& label) | |
171 | { | |
f6bcfd97 | 172 | SetWindowText(GetHwnd(), label); |
2bda0e17 KB |
173 | } |
174 | ||
f68586e5 | 175 | wxSize wxCheckBox::DoGetBestSize() const |
2bda0e17 | 176 | { |
f6bcfd97 | 177 | static int s_checkSize = 0; |
81d66cf3 | 178 | |
f6bcfd97 BP |
179 | if ( !s_checkSize ) |
180 | { | |
181 | wxScreenDC dc; | |
a756f210 | 182 | dc.SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT)); |
2bda0e17 | 183 | |
fb1a41cd | 184 | s_checkSize = dc.GetCharHeight(); |
f6bcfd97 BP |
185 | } |
186 | ||
187 | wxString str = wxGetWindowText(GetHWND()); | |
188 | ||
189 | int wCheckbox, hCheckbox; | |
4438caf4 VZ |
190 | if ( !str.IsEmpty() ) |
191 | { | |
192 | GetTextExtent(str, &wCheckbox, &hCheckbox); | |
f6bcfd97 | 193 | wCheckbox += s_checkSize + GetCharWidth(); |
27fda0b6 | 194 | |
f6bcfd97 BP |
195 | if ( hCheckbox < s_checkSize ) |
196 | hCheckbox = s_checkSize; | |
4438caf4 VZ |
197 | } |
198 | else | |
2bda0e17 | 199 | { |
f6bcfd97 BP |
200 | wCheckbox = s_checkSize; |
201 | hCheckbox = s_checkSize; | |
2bda0e17 KB |
202 | } |
203 | ||
4438caf4 | 204 | return wxSize(wCheckbox, hCheckbox); |
2bda0e17 KB |
205 | } |
206 | ||
debe6624 | 207 | void wxCheckBox::SetValue(bool val) |
2bda0e17 | 208 | { |
8941fa88 VZ |
209 | if (val) |
210 | { | |
211 | Set3StateValue(wxCHK_CHECKED); | |
212 | } | |
213 | else | |
214 | { | |
215 | Set3StateValue(wxCHK_UNCHECKED); | |
216 | } | |
2bda0e17 KB |
217 | } |
218 | ||
bfc6fde4 | 219 | bool wxCheckBox::GetValue() const |
2bda0e17 | 220 | { |
8941fa88 | 221 | return (Get3StateValue() != 0); |
2bda0e17 KB |
222 | } |
223 | ||
2b5f62a0 | 224 | void wxCheckBox::Command(wxCommandEvent& event) |
2bda0e17 | 225 | { |
8941fa88 VZ |
226 | int state = event.GetInt(); |
227 | wxCHECK_RET( (state == wxCHK_UNCHECKED) || (state == wxCHK_CHECKED) | |
228 | || (state == wxCHK_UNDETERMINED), | |
229 | wxT("event.GetInt() returned an invalid checkbox state") ); | |
230 | ||
231 | Set3StateValue((wxCheckBoxState) state); | |
2b5f62a0 | 232 | ProcessCommand(event); |
2bda0e17 | 233 | } |
1e6feb95 | 234 | |
8941fa88 VZ |
235 | wxCOMPILE_TIME_ASSERT(wxCHK_UNCHECKED == BST_UNCHECKED |
236 | && wxCHK_CHECKED == BST_CHECKED | |
237 | && wxCHK_UNDETERMINED == BST_INDETERMINATE, EnumValuesIncorrect); | |
238 | ||
239 | void wxCheckBox::DoSet3StateValue(wxCheckBoxState state) | |
240 | { | |
241 | ::SendMessage(GetHwnd(), BM_SETCHECK, (WPARAM) state, 0); | |
242 | } | |
243 | ||
244 | wxCheckBoxState wxCheckBox::DoGet3StateValue() const | |
245 | { | |
246 | #ifdef __WIN32__ | |
247 | return (wxCheckBoxState) ::SendMessage(GetHwnd(), BM_GETCHECK, 0, 0); | |
248 | #else | |
249 | return (wxCheckBoxState) ((::SendMessage(GetHwnd(), BM_GETCHECK, 0, 0) | |
250 | & 0x001) == 0x001); | |
251 | #endif | |
252 | ||
253 | } | |
254 | ||
1e6feb95 | 255 | #endif // wxUSE_CHECKBOX |