]> git.saurik.com Git - wxWidgets.git/blame - src/msw/tglbtn.cpp
Removed useless wxDisplaySize() calls from wxHtmlPrintout.
[wxWidgets.git] / src / msw / tglbtn.cpp
CommitLineData
1db8dc4a
VZ
1/////////////////////////////////////////////////////////////////////////////
2// Name: src/msw/tglbtn.cpp
3// Purpose: Definition of the wxToggleButton class, which implements a
4// toggle button under wxMSW.
f1e01716
WS
5// Author: John Norris, minor changes by Axel Schlueter
6// and William Gallafent.
1db8dc4a
VZ
7// Modified by:
8// Created: 08.02.01
9// RCS-ID: $Id$
10// Copyright: (c) 2000 Johnny C. Norris II
706fb893 11// License: wxWindows licence
1db8dc4a
VZ
12/////////////////////////////////////////////////////////////////////////////
13
14// ============================================================================
8907154c 15// declarations
1db8dc4a
VZ
16// ============================================================================
17
18// ----------------------------------------------------------------------------
19// headers
20// ----------------------------------------------------------------------------
21
22#include "wx/wxprec.h"
23
24#ifdef __BORLANDC__
25 #pragma hdrstop
26#endif
27
1db8dc4a
VZ
28#if wxUSE_TOGGLEBTN
29
f1e01716
WS
30#include "wx/tglbtn.h"
31
1db8dc4a
VZ
32#ifndef WX_PRECOMP
33 #include "wx/button.h"
34 #include "wx/brush.h"
35 #include "wx/dcscreen.h"
36 #include "wx/settings.h"
37
38 #include "wx/log.h"
39#endif // WX_PRECOMP
40
76c13f8f
RR
41#include "wx/renderer.h"
42#include "wx/dcclient.h"
43
1db8dc4a 44#include "wx/msw/private.h"
9016f3ad 45#include "wx/msw/private/button.h"
1db8dc4a
VZ
46
47// ----------------------------------------------------------------------------
48// macros
49// ----------------------------------------------------------------------------
50
9b11752c 51wxDEFINE_EVENT( wxEVT_COMMAND_TOGGLEBUTTON_CLICKED, wxCommandEvent );
1db8dc4a 52
1db8dc4a
VZ
53// ============================================================================
54// implementation
55// ============================================================================
56
76c13f8f
RR
57//-----------------------------------------------------------------------------
58// wxBitmapToggleButton
59//-----------------------------------------------------------------------------
60
61IMPLEMENT_DYNAMIC_CLASS(wxBitmapToggleButton, wxControl)
62
63BEGIN_EVENT_TABLE(wxBitmapToggleButton,wxToggleButtonBase)
64 EVT_PAINT(wxBitmapToggleButton::OnPaint)
65 EVT_MOUSE_EVENTS(wxBitmapToggleButton::OnMouse)
66 EVT_CHAR(wxBitmapToggleButton::OnChar)
67 EVT_SIZE(wxBitmapToggleButton::OnSize)
35524f61 68END_EVENT_TABLE()
76c13f8f
RR
69
70void wxBitmapToggleButton::Init()
71{
72 m_depressed = false;
73 m_oldValue = false;
74 m_capturing = false;
75}
76
77bool wxBitmapToggleButton::Create( wxWindow *parent, wxWindowID id,
78 const wxBitmap& label,const wxPoint& pos, const wxSize& size, long style,
79 const wxValidator& validator, const wxString& name )
80{
81 Init();
82
83 if (!wxToggleButtonBase::Create( parent, id, pos, size, style, validator, name ))
84 return false;
03647350 85
76c13f8f 86 m_bitmap = label;
03647350 87
76c13f8f
RR
88 if (size.x == -1 || size.y == -1)
89 {
90 wxSize new_size = GetBestSize();
91 if (size.x != -1)
92 new_size.x = size.x;
93 if (size.y != -1)
94 new_size.y = size.y;
95 SetSize( new_size );
96 }
03647350 97
76c13f8f
RR
98 return true;
99}
100
101void wxBitmapToggleButton::SetValue(bool state)
102{
103 if (m_capturing) return;
104
105 if (state == m_depressed) return;
03647350 106
76c13f8f
RR
107 m_depressed = state;
108 Refresh();
109}
110
111bool wxBitmapToggleButton::GetValue() const
112{
113 return m_depressed;
114}
115
116void wxBitmapToggleButton::SetLabel(const wxBitmap& label)
117{
118 m_bitmap = label;
119 m_disabledBitmap = wxBitmap();
03647350 120
76c13f8f
RR
121 Refresh();
122}
123
124bool wxBitmapToggleButton::Enable(bool enable)
125{
126 if (m_capturing) return false;
127
128 if (!wxToggleButtonBase::Enable( enable ))
129 return false;
03647350 130
76c13f8f 131 Refresh();
03647350 132
76c13f8f
RR
133 return true;
134}
135
136void wxBitmapToggleButton::OnPaint(wxPaintEvent &WXUNUSED(event))
137{
138 wxSize size = GetSize();
03647350 139
76c13f8f 140 wxBitmap bitmap = m_bitmap;
03647350 141
76c13f8f
RR
142 wxPaintDC dc(this);
143 wxRendererNative &renderer = wxRendererNative::Get();
144 int flags = 0;
145 if (m_depressed)
146 flags |= wxCONTROL_PRESSED;
147 wxRect rect(0,0,size.x,size.y);
148 renderer.DrawPushButton( this, dc, rect, flags );
03647350 149
76c13f8f
RR
150 if (bitmap.IsOk())
151 {
152 if (!IsEnabled())
153 {
154 if (!m_disabledBitmap.IsOk())
155 {
156 wxImage image = m_bitmap.ConvertToImage();
157 m_disabledBitmap = wxBitmap( image.ConvertToGreyscale() );
158 }
03647350 159
76c13f8f
RR
160 bitmap = m_disabledBitmap;
161 }
03647350 162
76c13f8f
RR
163 wxSize bsize = bitmap.GetSize();
164 int offset = 0;
165 if (m_depressed) offset = 1;
166 dc.DrawBitmap( bitmap, (size.x-bsize.x) / 2 + offset, (size.y-bsize.y) / 2 + offset, true );
167 }
03647350 168
76c13f8f
RR
169}
170
171void wxBitmapToggleButton::OnMouse(wxMouseEvent &event)
172{
173 if (!IsEnabled())
174 return;
03647350 175
76c13f8f 176 wxSize size = GetSize();
03647350 177 bool mouse_in = ((event.GetX() > 0) && (event.GetX() < size.x) &&
76c13f8f 178 (event.GetY() > 0) && (event.GetY() < size.y));
03647350 179
76c13f8f
RR
180 if (m_capturing)
181 {
182 bool old_depressed = m_depressed;
183 if (mouse_in)
184 m_depressed = !m_oldValue;
185 else
186 m_depressed = m_oldValue;
03647350 187
76c13f8f
RR
188 if (event.LeftUp())
189 {
db06bfb4 190 ReleaseMouse();
76c13f8f
RR
191 m_capturing = false;
192 if (mouse_in)
193 {
194 wxCommandEvent event(wxEVT_COMMAND_TOGGLEBUTTON_CLICKED, m_windowId);
195 event.SetInt(GetValue());
196 event.SetEventObject(this);
197 ProcessCommand(event);
198 }
199 }
03647350 200
76c13f8f
RR
201 if (old_depressed != m_depressed)
202 Refresh();
203 }
204 else
205 {
206 if (event.LeftDown())
207 {
208 m_capturing = true;
209 m_oldValue = m_depressed;
210 m_depressed = !m_oldValue;
211 CaptureMouse();
212 Refresh();
213 }
214 }
215}
216
217void wxBitmapToggleButton::OnChar(wxKeyEvent &event)
218{
219 if (event.GetKeyCode() == WXK_SPACE)
220 {
221 m_depressed = !m_depressed;
222 Refresh();
03647350 223
76c13f8f
RR
224 wxCommandEvent event(wxEVT_COMMAND_TOGGLEBUTTON_CLICKED, m_windowId);
225 event.SetInt(GetValue());
226 event.SetEventObject(this);
227 ProcessCommand(event);
228 }
229}
230
231void wxBitmapToggleButton::OnSize(wxSizeEvent &WXUNUSED(event))
232{
233 Refresh();
234}
235
236wxSize wxBitmapToggleButton::DoGetBestSize() const
237{
238 if (!m_bitmap.IsOk())
239 return wxSize(16,16);
03647350 240
76c13f8f
RR
241 wxSize ret = m_bitmap.GetSize();
242 ret.x += 8;
243 ret.y += 8;
244 return ret;
245}
246
247
1db8dc4a
VZ
248// ----------------------------------------------------------------------------
249// wxToggleButton
250// ----------------------------------------------------------------------------
251
76c13f8f
RR
252IMPLEMENT_DYNAMIC_CLASS(wxToggleButton, wxControl)
253
1db8dc4a 254// Single check box item
9016f3ad
VZ
255bool wxToggleButton::Create(wxWindow *parent,
256 wxWindowID id,
1db8dc4a
VZ
257 const wxString& label,
258 const wxPoint& pos,
259 const wxSize& size, long style,
260 const wxValidator& validator,
261 const wxString& name)
262{
b67e8d38 263 if ( !CreateControl(parent, id, pos, size, style, validator, name) )
bfbb0b4c
WS
264 return false;
265
9016f3ad
VZ
266 // if the label contains several lines we must explicitly tell the button
267 // about it or it wouldn't draw it correctly ("\n"s would just appear as
268 // black boxes)
269 //
270 // NB: we do it here and not in MSWGetStyle() because we need the label
271 // value and the label is not set yet when MSWGetStyle() is called
272 WXDWORD exstyle;
273 WXDWORD msStyle = MSWGetStyle(style, &exstyle);
274 msStyle |= wxMSWButton::GetMultilineStyle(label);
275
9a83f860 276 return MSWCreateControl(wxT("BUTTON"), msStyle, pos, size, label, exstyle);
b67e8d38 277}
1db8dc4a 278
b67e8d38
RD
279WXDWORD wxToggleButton::MSWGetStyle(long style, WXDWORD *exstyle) const
280{
281 WXDWORD msStyle = wxControl::MSWGetStyle(style, exstyle);
1db8dc4a 282
b67e8d38 283 msStyle |= BS_AUTOCHECKBOX | BS_PUSHLIKE | WS_TABSTOP;
b0766406 284
9016f3ad 285 if ( style & wxBU_LEFT )
1db8dc4a 286 msStyle |= BS_LEFT;
9016f3ad 287 if ( style & wxBU_RIGHT )
1db8dc4a 288 msStyle |= BS_RIGHT;
9016f3ad 289 if ( style & wxBU_TOP )
1db8dc4a 290 msStyle |= BS_TOP;
9016f3ad 291 if ( style & wxBU_BOTTOM )
1db8dc4a 292 msStyle |= BS_BOTTOM;
1db8dc4a 293
b67e8d38 294 return msStyle;
1db8dc4a
VZ
295}
296
1db8dc4a
VZ
297wxSize wxToggleButton::DoGetBestSize() const
298{
5c33522f 299 return wxMSWButton::ComputeBestSize(const_cast<wxToggleButton *>(this));
9016f3ad
VZ
300}
301
302void wxToggleButton::SetLabel(const wxString& label)
303{
304 wxMSWButton::UpdateMultilineStyle(GetHwnd(), label);
305
306 wxToggleButtonBase::SetLabel(label);
1db8dc4a
VZ
307}
308
309void wxToggleButton::SetValue(bool val)
310{
bfbb0b4c 311 ::SendMessage(GetHwnd(), BM_SETCHECK, val, 0);
1db8dc4a
VZ
312}
313
1db8dc4a
VZ
314bool wxToggleButton::GetValue() const
315{
9016f3ad 316 return ::SendMessage(GetHwnd(), BM_GETCHECK, 0, 0) == BST_CHECKED;
1db8dc4a
VZ
317}
318
9016f3ad 319void wxToggleButton::Command(wxCommandEvent& event)
1db8dc4a 320{
9016f3ad
VZ
321 SetValue(event.GetInt() != 0);
322 ProcessCommand(event);
323}
324
325bool wxToggleButton::MSWCommand(WXUINT WXUNUSED(param), WXWORD WXUNUSED(id))
326{
327 wxCommandEvent event(wxEVT_COMMAND_TOGGLEBUTTON_CLICKED, m_windowId);
328 event.SetInt(GetValue());
329 event.SetEventObject(this);
330 ProcessCommand(event);
331 return true;
1db8dc4a
VZ
332}
333
334#endif // wxUSE_TOGGLEBTN