]> git.saurik.com Git - wxWidgets.git/blame - src/msw/checkbox.cpp
Unix compilation fixed
[wxWidgets.git] / src / msw / checkbox.cpp
CommitLineData
2bda0e17
KB
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
4438caf4 9// Licence: wxWindows license
2bda0e17
KB
10/////////////////////////////////////////////////////////////////////////////
11
4438caf4
VZ
12// ============================================================================
13// declarations
14// ============================================================================
15
16// ----------------------------------------------------------------------------
17// headers
18// ----------------------------------------------------------------------------
19
2bda0e17 20#ifdef __GNUG__
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
31#ifndef WX_PRECOMP
4438caf4
VZ
32 #include "wx/checkbox.h"
33 #include "wx/brush.h"
f6bcfd97
BP
34 #include "wx/dcscreen.h"
35 #include "wx/settings.h"
2bda0e17
KB
36#endif
37
38#include "wx/msw/private.h"
39
4438caf4
VZ
40// ----------------------------------------------------------------------------
41// macros
42// ----------------------------------------------------------------------------
43
11b6a93b
VZ
44IMPLEMENT_DYNAMIC_CLASS(wxCheckBox, wxControl)
45IMPLEMENT_DYNAMIC_CLASS(wxBitmapCheckBox, wxCheckBox)
2bda0e17 46
4438caf4
VZ
47// ============================================================================
48// implementation
49// ============================================================================
50
51// ----------------------------------------------------------------------------
52// wxCheckBox
53// ----------------------------------------------------------------------------
54
debe6624 55bool wxCheckBox::MSWCommand(WXUINT WXUNUSED(param), WXWORD WXUNUSED(id))
2bda0e17 56{
4438caf4
VZ
57 wxCommandEvent event(wxEVT_COMMAND_CHECKBOX_CLICKED, m_windowId);
58 event.SetInt(GetValue());
59 event.SetEventObject(this);
60 ProcessCommand(event);
61 return TRUE;
2bda0e17
KB
62}
63
64// Single check box item
11b6a93b
VZ
65bool 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)
2bda0e17 72{
4438caf4 73 SetName(name);
11b6a93b 74#if wxUSE_VALIDATORS
4438caf4 75 SetValidator(validator);
11b6a93b 76#endif // wxUSE_VALIDATORS
4438caf4
VZ
77 if (parent) parent->AddChild(this);
78
79 SetBackgroundColour(parent->GetBackgroundColour()) ;
80 SetForegroundColour(parent->GetForegroundColour()) ;
81
82 m_windowStyle = style;
83
b94ae1ea
VZ
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
4438caf4 88 wxString Label = label;
223d09f6
KB
89 if (Label == wxT(""))
90 Label = wxT(" "); // Apparently needed or checkbox won't show
b94ae1ea 91#endif // 0
4438caf4
VZ
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
b0766406
JS
107 if ( style & wxCLIP_SIBLINGS )
108 msStyle |= WS_CLIPSIBLINGS;
109
4438caf4
VZ
110 // We perhaps have different concepts of 3D here - a 3D border,
111 // versus a 3D button.
112 // So we only wish to give a border if this is specified
113 // in the style.
114 bool want3D;
115 WXDWORD exStyle = Determine3DEffects(0, &want3D) ;
116
117 // Even with extended styles, need to combine with WS_BORDER
118 // for them to look right.
119 /*
120 if ( want3D || wxStyleHasBorder(m_windowStyle) )
121 msStyle |= WS_BORDER;
122 */
123
b94ae1ea 124 m_hWnd = (WXHWND)CreateWindowEx(exStyle, wxT("BUTTON"), label,
4438caf4
VZ
125 msStyle,
126 0, 0, 0, 0,
127 (HWND)parent->GetHWND(), (HMENU)m_windowId,
128 wxGetInstance(), NULL);
2bda0e17 129
1f112209 130#if wxUSE_CTL3D
4438caf4
VZ
131 if (want3D)
132 {
133 Ctl3dSubclassCtl(GetHwnd());
134 m_useCtl3D = TRUE;
135 }
2bda0e17
KB
136#endif
137
4438caf4
VZ
138 // Subclass again for purposes of dialog editing mode
139 SubclassWin(m_hWnd);
2bda0e17 140
4438caf4 141 SetFont(parent->GetFont());
2bda0e17 142
4438caf4
VZ
143 SetSize(x, y, width, height);
144
145 return TRUE;
2bda0e17
KB
146}
147
148void wxCheckBox::SetLabel(const wxString& label)
149{
f6bcfd97 150 SetWindowText(GetHwnd(), label);
2bda0e17
KB
151}
152
f68586e5 153wxSize wxCheckBox::DoGetBestSize() const
2bda0e17 154{
f6bcfd97 155 static int s_checkSize = 0;
81d66cf3 156
f6bcfd97
BP
157 if ( !s_checkSize )
158 {
159 wxScreenDC dc;
160 dc.SetFont(wxSystemSettings::GetSystemFont(wxSYS_DEFAULT_GUI_FONT));
2bda0e17 161
1fb45475 162 s_checkSize = (3*dc.GetCharHeight())/2;
f6bcfd97
BP
163 }
164
165 wxString str = wxGetWindowText(GetHWND());
166
167 int wCheckbox, hCheckbox;
4438caf4
VZ
168 if ( !str.IsEmpty() )
169 {
170 GetTextExtent(str, &wCheckbox, &hCheckbox);
f6bcfd97 171 wCheckbox += s_checkSize + GetCharWidth();
27fda0b6 172
f6bcfd97
BP
173 if ( hCheckbox < s_checkSize )
174 hCheckbox = s_checkSize;
4438caf4
VZ
175 }
176 else
2bda0e17 177 {
f6bcfd97
BP
178 wCheckbox = s_checkSize;
179 hCheckbox = s_checkSize;
2bda0e17
KB
180 }
181
4438caf4 182 return wxSize(wCheckbox, hCheckbox);
2bda0e17
KB
183}
184
debe6624 185void wxCheckBox::SetValue(bool val)
2bda0e17 186{
4438caf4 187 SendMessage(GetHwnd(), BM_SETCHECK, val, 0);
2bda0e17
KB
188}
189
2432b92d
JS
190#ifndef BST_CHECKED
191#define BST_CHECKED 0x0001
192#endif
193
bfc6fde4 194bool wxCheckBox::GetValue() const
2bda0e17
KB
195{
196#ifdef __WIN32__
4438caf4 197 return (SendMessage(GetHwnd(), BM_GETCHECK, 0, 0) == BST_CHECKED);
2bda0e17 198#else
f6bcfd97 199 return ((0x001 & SendMessage(GetHwnd(), BM_GETCHECK, 0, 0)) == 0x001);
2bda0e17
KB
200#endif
201}
202
2bda0e17
KB
203void wxCheckBox::Command (wxCommandEvent & event)
204{
205 SetValue ((event.GetInt() != 0));
206 ProcessCommand (event);
207}
208
4438caf4
VZ
209// ----------------------------------------------------------------------------
210// wxBitmapCheckBox
211// ----------------------------------------------------------------------------
212
33ac7e6f 213bool wxBitmapCheckBox::Create(wxWindow *parent, wxWindowID id, const wxBitmap *WXUNUSED(label),
2bda0e17 214 const wxPoint& pos,
debe6624 215 const wxSize& size, long style,
2bda0e17
KB
216 const wxValidator& validator,
217 const wxString& name)
218{
219 SetName(name);
11b6a93b 220#if wxUSE_VALIDATORS
2bda0e17 221 SetValidator(validator);
11b6a93b 222#endif // wxUSE_VALIDATORS
2bda0e17
KB
223 if (parent) parent->AddChild(this);
224
fd71308f
JS
225 SetBackgroundColour(parent->GetBackgroundColour()) ;
226 SetForegroundColour(parent->GetForegroundColour()) ;
2bda0e17
KB
227 m_windowStyle = style;
228
4438caf4
VZ
229 if ( id == -1 )
230 m_windowId = NewControlId();
231 else
232 m_windowId = id;
2bda0e17
KB
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
223d09f6 243 HWND wx_button = CreateWindowEx(MakeExtendedStyle(m_windowStyle), CHECK_CLASS, wxT("toggle"),
2bda0e17
KB
244 msStyle,
245 0, 0, 0, 0, (HWND) parent->GetHWND(), (HMENU)m_windowId,
246 wxGetInstance(), NULL);
247
1f112209 248#if wxUSE_CTL3D
2bda0e17
KB
249 if (!(GetParent()->GetWindowStyleFlag() & wxUSER_COLOURS))
250 {
251 Ctl3dSubclassCtl(wx_button);
4438caf4 252 m_useCtl3D = TRUE;
2bda0e17
KB
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
2bda0e17
KB
261 SetSize(x, y, width, height);
262
263 ShowWindow(wx_button, SW_SHOW);
4438caf4 264
2bda0e17
KB
265 return TRUE;
266}
267
33ac7e6f 268void wxBitmapCheckBox::SetLabel(const wxBitmap& WXUNUSED(bitmap))
2bda0e17 269{
223d09f6 270 wxFAIL_MSG(wxT("not implemented"));
2bda0e17 271}