]> git.saurik.com Git - wxWidgets.git/blob - src/msw/checkbox.cpp
Added chapter on collection and container classes to contents
[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 // ============================================================================
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 #include "wx/dcscreen.h"
35 #include "wx/settings.h"
36 #endif
37
38 #include "wx/msw/private.h"
39
40 // ----------------------------------------------------------------------------
41 // macros
42 // ----------------------------------------------------------------------------
43
44 IMPLEMENT_DYNAMIC_CLASS(wxCheckBox, wxControl)
45 IMPLEMENT_DYNAMIC_CLASS(wxBitmapCheckBox, wxCheckBox)
46
47 // ============================================================================
48 // implementation
49 // ============================================================================
50
51 // ----------------------------------------------------------------------------
52 // wxCheckBox
53 // ----------------------------------------------------------------------------
54
55 bool wxCheckBox::MSWCommand(WXUINT WXUNUSED(param), WXWORD WXUNUSED(id))
56 {
57 wxCommandEvent event(wxEVT_COMMAND_CHECKBOX_CLICKED, m_windowId);
58 event.SetInt(GetValue());
59 event.SetEventObject(this);
60 ProcessCommand(event);
61 return TRUE;
62 }
63
64 // Single check box item
65 bool wxCheckBox::Create(wxWindow *parent,
66 wxWindowID id,
67 const wxString& label,
68 const wxPoint& pos,
69 const wxSize& size, long style,
70 const wxValidator& validator,
71 const wxString& name)
72 {
73 SetName(name);
74 #if wxUSE_VALIDATORS
75 SetValidator(validator);
76 #endif // wxUSE_VALIDATORS
77 if (parent) parent->AddChild(this);
78
79 SetBackgroundColour(parent->GetBackgroundColour()) ;
80 SetForegroundColour(parent->GetForegroundColour()) ;
81
82 m_windowStyle = style;
83
84 // VZ: disabling this ugliness which completely breaks checkboxes in wxGrid
85 // whoever did it, please tell me where and how does the checkbox fail
86 // to appear
87 #if 0
88 wxString Label = label;
89 if (Label == wxT(""))
90 Label = wxT(" "); // Apparently needed or checkbox won't show
91 #endif // 0
92
93 if ( id == -1 )
94 m_windowId = NewControlId();
95 else
96 m_windowId = id;
97
98 int x = pos.x;
99 int y = pos.y;
100 int width = size.x;
101 int height = size.y;
102
103 long msStyle = BS_AUTOCHECKBOX | WS_TABSTOP | WS_CHILD | WS_VISIBLE;
104 if ( style & wxALIGN_RIGHT )
105 msStyle |= BS_LEFTTEXT;
106
107 // We perhaps have different concepts of 3D here - a 3D border,
108 // versus a 3D button.
109 // So we only wish to give a border if this is specified
110 // in the style.
111 bool want3D;
112 WXDWORD exStyle = Determine3DEffects(0, &want3D) ;
113
114 // Even with extended styles, need to combine with WS_BORDER
115 // for them to look right.
116 /*
117 if ( want3D || wxStyleHasBorder(m_windowStyle) )
118 msStyle |= WS_BORDER;
119 */
120
121 m_hWnd = (WXHWND)CreateWindowEx(exStyle, wxT("BUTTON"), label,
122 msStyle,
123 0, 0, 0, 0,
124 (HWND)parent->GetHWND(), (HMENU)m_windowId,
125 wxGetInstance(), NULL);
126
127 #if wxUSE_CTL3D
128 if (want3D)
129 {
130 Ctl3dSubclassCtl(GetHwnd());
131 m_useCtl3D = TRUE;
132 }
133 #endif
134
135 // Subclass again for purposes of dialog editing mode
136 SubclassWin(m_hWnd);
137
138 SetFont(parent->GetFont());
139
140 SetSize(x, y, width, height);
141
142 return TRUE;
143 }
144
145 void wxCheckBox::SetLabel(const wxString& label)
146 {
147 SetWindowText(GetHwnd(), label);
148 }
149
150 wxSize wxCheckBox::DoGetBestSize() const
151 {
152 static int s_checkSize = 0;
153
154 if ( !s_checkSize )
155 {
156 wxScreenDC dc;
157 dc.SetFont(wxSystemSettings::GetSystemFont(wxSYS_DEFAULT_GUI_FONT));
158
159 // the height of a standard button in the dialog units is 8,
160 // translate this to pixels (as one dialog unit is precisely equal to
161 // 8 character heights, it's just the char height)
162 s_checkSize = dc.GetCharHeight();
163 }
164
165 wxString str = wxGetWindowText(GetHWND());
166
167 int wCheckbox, hCheckbox;
168 if ( !str.IsEmpty() )
169 {
170 GetTextExtent(str, &wCheckbox, &hCheckbox);
171 wCheckbox += s_checkSize + GetCharWidth();
172
173 if ( hCheckbox < s_checkSize )
174 hCheckbox = s_checkSize;
175 }
176 else
177 {
178 wCheckbox = s_checkSize;
179 hCheckbox = s_checkSize;
180 }
181
182 return wxSize(wCheckbox, hCheckbox);
183 }
184
185 void wxCheckBox::SetValue(bool val)
186 {
187 SendMessage(GetHwnd(), BM_SETCHECK, val, 0);
188 }
189
190 #ifndef BST_CHECKED
191 #define BST_CHECKED 0x0001
192 #endif
193
194 bool wxCheckBox::GetValue() const
195 {
196 #ifdef __WIN32__
197 return (SendMessage(GetHwnd(), BM_GETCHECK, 0, 0) == BST_CHECKED);
198 #else
199 return ((0x001 & SendMessage(GetHwnd(), BM_GETCHECK, 0, 0)) == 0x001);
200 #endif
201 }
202
203 void wxCheckBox::Command (wxCommandEvent & event)
204 {
205 SetValue ((event.GetInt() != 0));
206 ProcessCommand (event);
207 }
208
209 // ----------------------------------------------------------------------------
210 // wxBitmapCheckBox
211 // ----------------------------------------------------------------------------
212
213 bool wxBitmapCheckBox::Create(wxWindow *parent, wxWindowID id, const wxBitmap *label,
214 const wxPoint& pos,
215 const wxSize& size, long style,
216 const wxValidator& validator,
217 const wxString& name)
218 {
219 SetName(name);
220 #if wxUSE_VALIDATORS
221 SetValidator(validator);
222 #endif // wxUSE_VALIDATORS
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, wxT("toggle"),
244 msStyle,
245 0, 0, 0, 0, (HWND) parent->GetHWND(), (HMENU)m_windowId,
246 wxGetInstance(), NULL);
247
248 #if wxUSE_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 SetSize(x, y, width, height);
262
263 ShowWindow(wx_button, SW_SHOW);
264
265 return TRUE;
266 }
267
268 void wxBitmapCheckBox::SetLabel(const wxBitmap& bitmap)
269 {
270 wxFAIL_MSG(wxT("not implemented"));
271 }