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/msw/private.h"
43 // ----------------------------------------------------------------------------
45 // ----------------------------------------------------------------------------
47 IMPLEMENT_DYNAMIC_CLASS(wxToggleButton
, wxControl
)
48 DEFINE_EVENT_TYPE(wxEVT_COMMAND_TOGGLEBUTTON_CLICKED
)
50 #define BUTTON_HEIGHT_FROM_CHAR_HEIGHT(cy) (11*EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy)/10)
52 // ============================================================================
54 // ============================================================================
56 // ----------------------------------------------------------------------------
58 // ----------------------------------------------------------------------------
60 bool wxToggleButton::MSWCommand(WXUINT
WXUNUSED(param
), WXWORD
WXUNUSED(id
))
62 wxCommandEvent
event(wxEVT_COMMAND_TOGGLEBUTTON_CLICKED
, m_windowId
);
63 event
.SetInt(GetValue());
64 event
.SetEventObject(this);
65 ProcessCommand(event
);
69 // Single check box item
70 bool wxToggleButton::Create(wxWindow
*parent
, wxWindowID id
,
71 const wxString
& label
,
73 const wxSize
& size
, long style
,
74 const wxValidator
& validator
,
77 if ( !CreateControl(parent
, id
, pos
, size
, style
, validator
, name
) )
80 if ( !MSWCreateControl(wxT("BUTTON"), label
, pos
, size
) )
86 WXDWORD
wxToggleButton::MSWGetStyle(long style
, WXDWORD
*exstyle
) const
88 WXDWORD msStyle
= wxControl::MSWGetStyle(style
, exstyle
);
91 #define BS_PUSHLIKE 0x00001000L
94 msStyle
|= BS_AUTOCHECKBOX
| BS_PUSHLIKE
| WS_TABSTOP
;
98 if(style
& wxBU_RIGHT
)
102 if(style
& wxBU_BOTTOM
)
103 msStyle
|= BS_BOTTOM
;
108 wxSize
wxToggleButton::DoGetBestSize() const
110 wxString label
= wxGetWindowText(GetHWND());
112 GetTextExtent(GetLabelText(label
), &wBtn
, NULL
);
115 wxGetCharSize(GetHWND(), &wChar
, &hChar
, GetFont());
117 // add a margin - the button is wider than just its label
120 // the button height is proportional to the height of the font used
121 int hBtn
= BUTTON_HEIGHT_FROM_CHAR_HEIGHT(hChar
);
124 // make all buttons of at least standard size unless wxBU_EXACTFIT is given
125 if ( !HasFlag(wxBU_EXACTFIT
) )
127 const wxSize szMin
= wxButton::GetDefaultSize();
128 if ( wBtn
< szMin
.x
)
130 if ( hBtn
< szMin
.y
)
133 #endif // wxUSE_BUTTON
135 wxSize
sz(wBtn
, hBtn
);
141 void wxToggleButton::SetValue(bool val
)
143 ::SendMessage(GetHwnd(), BM_SETCHECK
, val
, 0);
147 #define BST_CHECKED 0x0001
150 bool wxToggleButton::GetValue() const
153 return (::SendMessage(GetHwnd(), BM_GETCHECK
, 0, 0) == BST_CHECKED
);
155 return ((0x001 & ::SendMessage(GetHwnd(), BM_GETCHECK
, 0, 0)) == 0x001);
159 void wxToggleButton::Command(wxCommandEvent
& event
)
161 SetValue((event
.GetInt() != 0));
162 ProcessCommand(event
);
165 #endif // wxUSE_TOGGLEBTN