]> git.saurik.com Git - wxWidgets.git/blob - src/msw/checkbox.cpp
fixed setfocus() flicker in listctrl
[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 #endif
35
36 #include "wx/msw/private.h"
37
38 // ----------------------------------------------------------------------------
39 // macros
40 // ----------------------------------------------------------------------------
41
42 #if !USE_SHARED_LIBRARY
43 IMPLEMENT_DYNAMIC_CLASS(wxCheckBox, wxControl)
44 IMPLEMENT_DYNAMIC_CLASS(wxBitmapCheckBox, wxCheckBox)
45 #endif
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, wxWindowID id, 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 SetValidator(validator);
73 if (parent) parent->AddChild(this);
74
75 SetBackgroundColour(parent->GetBackgroundColour()) ;
76 SetForegroundColour(parent->GetForegroundColour()) ;
77
78 m_windowStyle = style;
79
80 wxString Label = label;
81 if (Label == wxT(""))
82 Label = wxT(" "); // Apparently needed or checkbox won't show
83
84 if ( id == -1 )
85 m_windowId = NewControlId();
86 else
87 m_windowId = id;
88
89 int x = pos.x;
90 int y = pos.y;
91 int width = size.x;
92 int height = size.y;
93
94 long msStyle = BS_AUTOCHECKBOX | WS_TABSTOP | WS_CHILD | WS_VISIBLE;
95 if ( style & wxALIGN_RIGHT )
96 msStyle |= BS_LEFTTEXT;
97
98 // We perhaps have different concepts of 3D here - a 3D border,
99 // versus a 3D button.
100 // So we only wish to give a border if this is specified
101 // in the style.
102 bool want3D;
103 WXDWORD exStyle = Determine3DEffects(0, &want3D) ;
104
105 // Even with extended styles, need to combine with WS_BORDER
106 // for them to look right.
107 /*
108 if ( want3D || wxStyleHasBorder(m_windowStyle) )
109 msStyle |= WS_BORDER;
110 */
111
112 m_hWnd = (WXHWND)CreateWindowEx(exStyle, wxT("BUTTON"), Label,
113 msStyle,
114 0, 0, 0, 0,
115 (HWND)parent->GetHWND(), (HMENU)m_windowId,
116 wxGetInstance(), NULL);
117
118 #if wxUSE_CTL3D
119 if (want3D)
120 {
121 Ctl3dSubclassCtl(GetHwnd());
122 m_useCtl3D = TRUE;
123 }
124 #endif
125
126 // Subclass again for purposes of dialog editing mode
127 SubclassWin(m_hWnd);
128
129 SetFont(parent->GetFont());
130
131 SetSize(x, y, width, height);
132
133 return TRUE;
134 }
135
136 void wxCheckBox::SetLabel(const wxString& label)
137 {
138 SetWindowText(GetHwnd(), label);
139 }
140
141 wxSize wxCheckBox::DoGetBestSize() const
142 {
143 int wCheckbox, hCheckbox;
144
145 wxString str = wxGetWindowText(GetHWND());
146
147 if ( !str.IsEmpty() )
148 {
149 GetTextExtent(str, &wCheckbox, &hCheckbox);
150 wCheckbox += RADIO_SIZE;
151
152 if ( hCheckbox < RADIO_SIZE )
153 hCheckbox = RADIO_SIZE;
154 }
155 else
156 {
157 wCheckbox = RADIO_SIZE;
158 hCheckbox = RADIO_SIZE;
159 }
160
161 return wxSize(wCheckbox, hCheckbox);
162 }
163
164 void wxCheckBox::SetValue(bool val)
165 {
166 SendMessage(GetHwnd(), BM_SETCHECK, val, 0);
167 }
168
169 #ifndef BST_CHECKED
170 #define BST_CHECKED 0x0001
171 #endif
172
173 bool wxCheckBox::GetValue() const
174 {
175 #ifdef __WIN32__
176 return (SendMessage(GetHwnd(), BM_GETCHECK, 0, 0) == BST_CHECKED);
177 #else
178 return ((0x003 & SendMessage(GetHwnd(), BM_GETCHECK, 0, 0)) == 0x003);
179 #endif
180 }
181
182 WXHBRUSH wxCheckBox::OnCtlColor(WXHDC pDC, WXHWND pWnd, WXUINT nCtlColor,
183 WXUINT message, WXWPARAM wParam, WXLPARAM lParam)
184 {
185 #if wxUSE_CTL3D
186 if ( m_useCtl3D )
187 {
188 HBRUSH hbrush = Ctl3dCtlColorEx(message, wParam, lParam);
189
190 return (WXHBRUSH) hbrush;
191 }
192 #endif
193
194 if (GetParent()->GetTransparentBackground())
195 SetBkMode((HDC) pDC, TRANSPARENT);
196 else
197 SetBkMode((HDC) pDC, OPAQUE);
198
199 ::SetBkColor((HDC) pDC, RGB(GetBackgroundColour().Red(), GetBackgroundColour().Green(), GetBackgroundColour().Blue()));
200 ::SetTextColor((HDC) pDC, RGB(GetForegroundColour().Red(), GetForegroundColour().Green(), GetForegroundColour().Blue()));
201
202 wxBrush *backgroundBrush = wxTheBrushList->FindOrCreateBrush(GetBackgroundColour(), wxSOLID);
203
204 // Note that this will be cleaned up in wxApp::OnIdle, if backgroundBrush
205 // has a zero usage count.
206 // backgroundBrush->RealizeResource();
207 return (WXHBRUSH) backgroundBrush->GetResourceHandle();
208 }
209
210 void wxCheckBox::Command (wxCommandEvent & event)
211 {
212 SetValue ((event.GetInt() != 0));
213 ProcessCommand (event);
214 }
215
216 // ----------------------------------------------------------------------------
217 // wxBitmapCheckBox
218 // ----------------------------------------------------------------------------
219
220 bool wxBitmapCheckBox::Create(wxWindow *parent, wxWindowID id, const wxBitmap *label,
221 const wxPoint& pos,
222 const wxSize& size, long style,
223 const wxValidator& validator,
224 const wxString& name)
225 {
226 SetName(name);
227 SetValidator(validator);
228 if (parent) parent->AddChild(this);
229
230 SetBackgroundColour(parent->GetBackgroundColour()) ;
231 SetForegroundColour(parent->GetForegroundColour()) ;
232 m_windowStyle = style;
233
234 if ( id == -1 )
235 m_windowId = NewControlId();
236 else
237 m_windowId = id;
238
239 int x = pos.x;
240 int y = pos.y;
241 int width = size.x;
242 int height = size.y;
243
244 checkWidth = -1 ;
245 checkHeight = -1 ;
246 long msStyle = CHECK_FLAGS;
247
248 HWND wx_button = CreateWindowEx(MakeExtendedStyle(m_windowStyle), CHECK_CLASS, wxT("toggle"),
249 msStyle,
250 0, 0, 0, 0, (HWND) parent->GetHWND(), (HMENU)m_windowId,
251 wxGetInstance(), NULL);
252
253 #if wxUSE_CTL3D
254 if (!(GetParent()->GetWindowStyleFlag() & wxUSER_COLOURS))
255 {
256 Ctl3dSubclassCtl(wx_button);
257 m_useCtl3D = TRUE;
258 }
259 #endif
260
261 m_hWnd = (WXHWND)wx_button;
262
263 // Subclass again for purposes of dialog editing mode
264 SubclassWin((WXHWND)wx_button);
265
266 SetSize(x, y, width, height);
267
268 ShowWindow(wx_button, SW_SHOW);
269
270 return TRUE;
271 }
272
273 void wxBitmapCheckBox::SetLabel(const wxBitmap& bitmap)
274 {
275 wxFAIL_MSG(wxT("not implemented"));
276 }