Pass validator, not wxDefaultValidator!
[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 #if wxUSE_CHECKBOX
32
33 #ifndef WX_PRECOMP
34 #include "wx/checkbox.h"
35 #include "wx/brush.h"
36 #include "wx/dcscreen.h"
37 #include "wx/settings.h"
38 #endif
39
40 #include "wx/msw/private.h"
41
42 // ----------------------------------------------------------------------------
43 // macros
44 // ----------------------------------------------------------------------------
45
46 IMPLEMENT_DYNAMIC_CLASS(wxCheckBox, wxControl)
47 IMPLEMENT_DYNAMIC_CLASS(wxBitmapCheckBox, wxCheckBox)
48
49 // ============================================================================
50 // implementation
51 // ============================================================================
52
53 // ----------------------------------------------------------------------------
54 // wxCheckBox
55 // ----------------------------------------------------------------------------
56
57 bool wxCheckBox::MSWCommand(WXUINT WXUNUSED(param), WXWORD WXUNUSED(id))
58 {
59 wxCommandEvent event(wxEVT_COMMAND_CHECKBOX_CLICKED, m_windowId);
60 event.SetInt(GetValue());
61 event.SetEventObject(this);
62 ProcessCommand(event);
63 return TRUE;
64 }
65
66 bool wxCheckBox::Create(wxWindow *parent,
67 wxWindowID id,
68 const wxString& label,
69 const wxPoint& pos,
70 const wxSize& size, long style,
71 const wxValidator& validator,
72 const wxString& name)
73 {
74 if ( !CreateControl(parent, id, pos, size, style, validator, name) )
75 return FALSE;
76
77 long msStyle = BS_AUTOCHECKBOX | WS_TABSTOP;
78 if ( style & wxALIGN_RIGHT )
79 msStyle |= BS_LEFTTEXT;
80
81 return MSWCreateControl(wxT("BUTTON"), msStyle, pos, size, label, 0);
82 }
83
84 void wxCheckBox::SetLabel(const wxString& label)
85 {
86 SetWindowText(GetHwnd(), label);
87 }
88
89 wxSize wxCheckBox::DoGetBestSize() const
90 {
91 static int s_checkSize = 0;
92
93 if ( !s_checkSize )
94 {
95 wxScreenDC dc;
96 dc.SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT));
97
98 s_checkSize = dc.GetCharHeight();
99 }
100
101 wxString str = wxGetWindowText(GetHWND());
102
103 int wCheckbox, hCheckbox;
104 if ( !str.IsEmpty() )
105 {
106 GetTextExtent(str, &wCheckbox, &hCheckbox);
107 wCheckbox += s_checkSize + GetCharWidth();
108
109 if ( hCheckbox < s_checkSize )
110 hCheckbox = s_checkSize;
111 }
112 else
113 {
114 wCheckbox = s_checkSize;
115 hCheckbox = s_checkSize;
116 }
117
118 return wxSize(wCheckbox, hCheckbox);
119 }
120
121 void wxCheckBox::SetValue(bool val)
122 {
123 SendMessage(GetHwnd(), BM_SETCHECK, val, 0);
124 }
125
126 #ifndef BST_CHECKED
127 #define BST_CHECKED 0x0001
128 #endif
129
130 bool wxCheckBox::GetValue() const
131 {
132 #ifdef __WIN32__
133 return (SendMessage(GetHwnd(), BM_GETCHECK, 0, 0) == BST_CHECKED);
134 #else
135 return ((0x001 & SendMessage(GetHwnd(), BM_GETCHECK, 0, 0)) == 0x001);
136 #endif
137 }
138
139 void wxCheckBox::Command (wxCommandEvent & event)
140 {
141 SetValue ((event.GetInt() != 0));
142 ProcessCommand (event);
143 }
144
145 // ----------------------------------------------------------------------------
146 // wxBitmapCheckBox
147 // ----------------------------------------------------------------------------
148
149 bool wxBitmapCheckBox::Create(wxWindow *parent, wxWindowID id, const wxBitmap *WXUNUSED(label),
150 const wxPoint& pos,
151 const wxSize& size, long style,
152 const wxValidator& validator,
153 const wxString& name)
154 {
155 SetName(name);
156 #if wxUSE_VALIDATORS
157 SetValidator(validator);
158 #endif // wxUSE_VALIDATORS
159 if (parent) parent->AddChild(this);
160
161 SetBackgroundColour(parent->GetBackgroundColour()) ;
162 SetForegroundColour(parent->GetForegroundColour()) ;
163 m_windowStyle = style;
164
165 if ( id == -1 )
166 m_windowId = NewControlId();
167 else
168 m_windowId = id;
169
170 int x = pos.x;
171 int y = pos.y;
172 int width = size.x;
173 int height = size.y;
174
175 checkWidth = -1 ;
176 checkHeight = -1 ;
177 long msStyle = CHECK_FLAGS;
178
179 HWND wx_button = CreateWindowEx(MakeExtendedStyle(m_windowStyle), CHECK_CLASS, wxT("toggle"),
180 msStyle,
181 0, 0, 0, 0, (HWND) parent->GetHWND(), (HMENU)m_windowId,
182 wxGetInstance(), NULL);
183
184 #if wxUSE_CTL3D
185 if (!(GetParent()->GetWindowStyleFlag() & wxUSER_COLOURS))
186 {
187 Ctl3dSubclassCtl(wx_button);
188 m_useCtl3D = TRUE;
189 }
190 #endif
191
192 m_hWnd = (WXHWND)wx_button;
193
194 // Subclass again for purposes of dialog editing mode
195 SubclassWin((WXHWND)wx_button);
196
197 SetSize(x, y, width, height);
198
199 ShowWindow(wx_button, SW_SHOW);
200
201 return TRUE;
202 }
203
204 void wxBitmapCheckBox::SetLabel(const wxBitmap& WXUNUSED(bitmap))
205 {
206 wxFAIL_MSG(wxT("not implemented"));
207 }
208
209 #endif // wxUSE_CHECKBOX