1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/msw/tglbtn.cpp
3 // Purpose: Definition of the wxToggleButton class, which implements a
4 // toggle button under wxMSW.
5 // Author: John Norris, minor changes by Axel Schlueter
6 // and William Gallafent.
10 // Copyright: (c) 2000 Johnny C. Norris II
11 // License: wxWindows licence
12 /////////////////////////////////////////////////////////////////////////////
14 // ============================================================================
16 // ============================================================================
18 // ----------------------------------------------------------------------------
20 // ----------------------------------------------------------------------------
22 #include "wx/wxprec.h"
30 #include "wx/tglbtn.h"
33 #include "wx/button.h"
35 #include "wx/dcscreen.h"
36 #include "wx/settings.h"
41 #include "wx/renderer.h"
42 #include "wx/dcclient.h"
44 #include "wx/msw/private.h"
45 #include "wx/msw/private/button.h"
47 // ----------------------------------------------------------------------------
49 // ----------------------------------------------------------------------------
51 wxDEFINE_EVENT( wxEVT_COMMAND_TOGGLEBUTTON_CLICKED
, wxCommandEvent
)
53 // ============================================================================
55 // ============================================================================
57 //-----------------------------------------------------------------------------
58 // wxBitmapToggleButton
59 //-----------------------------------------------------------------------------
61 IMPLEMENT_DYNAMIC_CLASS(wxBitmapToggleButton
, wxControl
)
63 BEGIN_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
)
70 void wxBitmapToggleButton::Init()
77 bool 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
)
83 if (!wxToggleButtonBase::Create( parent
, id
, pos
, size
, style
, validator
, name
))
88 if (size
.x
== -1 || size
.y
== -1)
90 wxSize new_size
= GetBestSize();
101 void wxBitmapToggleButton::SetValue(bool state
)
103 if (m_capturing
) return;
105 if (state
== m_depressed
) return;
111 bool wxBitmapToggleButton::GetValue() const
116 void wxBitmapToggleButton::SetLabel(const wxBitmap
& label
)
119 m_disabledBitmap
= wxBitmap();
124 bool wxBitmapToggleButton::Enable(bool enable
)
126 if (m_capturing
) return false;
128 if (!wxToggleButtonBase::Enable( enable
))
136 void wxBitmapToggleButton::OnPaint(wxPaintEvent
&WXUNUSED(event
))
138 wxSize size
= GetSize();
140 wxBitmap bitmap
= m_bitmap
;
143 wxRendererNative
&renderer
= wxRendererNative::Get();
146 flags
|= wxCONTROL_PRESSED
;
147 wxRect
rect(0,0,size
.x
,size
.y
);
148 renderer
.DrawPushButton( this, dc
, rect
, flags
);
154 if (!m_disabledBitmap
.IsOk())
156 wxImage image
= m_bitmap
.ConvertToImage();
157 m_disabledBitmap
= wxBitmap( image
.ConvertToGreyscale() );
160 bitmap
= m_disabledBitmap
;
163 wxSize bsize
= bitmap
.GetSize();
165 if (m_depressed
) offset
= 1;
166 dc
.DrawBitmap( bitmap
, (size
.x
-bsize
.x
) / 2 + offset
, (size
.y
-bsize
.y
) / 2 + offset
, true );
171 void wxBitmapToggleButton::OnMouse(wxMouseEvent
&event
)
176 wxSize size
= GetSize();
177 bool mouse_in
= ((event
.GetX() > 0) && (event
.GetX() < size
.x
) &&
178 (event
.GetY() > 0) && (event
.GetY() < size
.y
));
182 bool old_depressed
= m_depressed
;
184 m_depressed
= !m_oldValue
;
186 m_depressed
= m_oldValue
;
194 wxCommandEvent
event(wxEVT_COMMAND_TOGGLEBUTTON_CLICKED
, m_windowId
);
195 event
.SetInt(GetValue());
196 event
.SetEventObject(this);
197 ProcessCommand(event
);
201 if (old_depressed
!= m_depressed
)
206 if (event
.LeftDown())
209 m_oldValue
= m_depressed
;
210 m_depressed
= !m_oldValue
;
217 void wxBitmapToggleButton::OnChar(wxKeyEvent
&event
)
219 if (event
.GetKeyCode() == WXK_SPACE
)
221 m_depressed
= !m_depressed
;
224 wxCommandEvent
event(wxEVT_COMMAND_TOGGLEBUTTON_CLICKED
, m_windowId
);
225 event
.SetInt(GetValue());
226 event
.SetEventObject(this);
227 ProcessCommand(event
);
231 void wxBitmapToggleButton::OnSize(wxSizeEvent
&WXUNUSED(event
))
236 wxSize
wxBitmapToggleButton::DoGetBestSize() const
238 if (!m_bitmap
.IsOk())
239 return wxSize(16,16);
241 wxSize ret
= m_bitmap
.GetSize();
248 // ----------------------------------------------------------------------------
250 // ----------------------------------------------------------------------------
252 IMPLEMENT_DYNAMIC_CLASS(wxToggleButton
, wxControl
)
254 // Single check box item
255 bool wxToggleButton::Create(wxWindow
*parent
,
257 const wxString
& label
,
259 const wxSize
& size
, long style
,
260 const wxValidator
& validator
,
261 const wxString
& name
)
263 if ( !CreateControl(parent
, id
, pos
, size
, style
, validator
, name
) )
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
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
273 WXDWORD msStyle
= MSWGetStyle(style
, &exstyle
);
274 msStyle
|= wxMSWButton::GetMultilineStyle(label
);
276 return MSWCreateControl(_T("BUTTON"), msStyle
, pos
, size
, label
, exstyle
);
279 WXDWORD
wxToggleButton::MSWGetStyle(long style
, WXDWORD
*exstyle
) const
281 WXDWORD msStyle
= wxControl::MSWGetStyle(style
, exstyle
);
283 msStyle
|= BS_AUTOCHECKBOX
| BS_PUSHLIKE
| WS_TABSTOP
;
285 if ( style
& wxBU_LEFT
)
287 if ( style
& wxBU_RIGHT
)
289 if ( style
& wxBU_TOP
)
291 if ( style
& wxBU_BOTTOM
)
292 msStyle
|= BS_BOTTOM
;
297 wxSize
wxToggleButton::DoGetBestSize() const
299 return wxMSWButton::ComputeBestSize(const_cast<wxToggleButton
*>(this));
302 void wxToggleButton::SetLabel(const wxString
& label
)
304 wxMSWButton::UpdateMultilineStyle(GetHwnd(), label
);
306 wxToggleButtonBase::SetLabel(label
);
309 void wxToggleButton::SetValue(bool val
)
311 ::SendMessage(GetHwnd(), BM_SETCHECK
, val
, 0);
314 bool wxToggleButton::GetValue() const
316 return ::SendMessage(GetHwnd(), BM_GETCHECK
, 0, 0) == BST_CHECKED
;
319 void wxToggleButton::Command(wxCommandEvent
& event
)
321 SetValue(event
.GetInt() != 0);
322 ProcessCommand(event
);
325 bool wxToggleButton::MSWCommand(WXUINT
WXUNUSED(param
), WXWORD
WXUNUSED(id
))
327 wxCommandEvent
event(wxEVT_COMMAND_TOGGLEBUTTON_CLICKED
, m_windowId
);
328 event
.SetInt(GetValue());
329 event
.SetEventObject(this);
330 ProcessCommand(event
);
334 #endif // wxUSE_TOGGLEBTN