replace wx_{const,static,reinterpret}_cast with their standard C++ equivalents
[wxWidgets.git] / src / msw / tglbtn.cpp
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.
7 // Modified by:
8 // Created: 08.02.01
9 // RCS-ID: $Id$
10 // Copyright: (c) 2000 Johnny C. Norris II
11 // License: wxWindows licence
12 /////////////////////////////////////////////////////////////////////////////
13
14 // ============================================================================
15 // declarations
16 // ============================================================================
17
18 // ----------------------------------------------------------------------------
19 // headers
20 // ----------------------------------------------------------------------------
21
22 #include "wx/wxprec.h"
23
24 #ifdef __BORLANDC__
25 #pragma hdrstop
26 #endif
27
28 #if wxUSE_TOGGLEBTN
29
30 #include "wx/tglbtn.h"
31
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
41 #include "wx/msw/private.h"
42 #include "wx/msw/private/button.h"
43
44 // ----------------------------------------------------------------------------
45 // macros
46 // ----------------------------------------------------------------------------
47
48 IMPLEMENT_DYNAMIC_CLASS(wxToggleButton, wxControl)
49 DEFINE_EVENT_TYPE(wxEVT_COMMAND_TOGGLEBUTTON_CLICKED)
50
51 // ============================================================================
52 // implementation
53 // ============================================================================
54
55 // ----------------------------------------------------------------------------
56 // wxToggleButton
57 // ----------------------------------------------------------------------------
58
59 // Single check box item
60 bool wxToggleButton::Create(wxWindow *parent,
61 wxWindowID id,
62 const wxString& label,
63 const wxPoint& pos,
64 const wxSize& size, long style,
65 const wxValidator& validator,
66 const wxString& name)
67 {
68 if ( !CreateControl(parent, id, pos, size, style, validator, name) )
69 return false;
70
71 // if the label contains several lines we must explicitly tell the button
72 // about it or it wouldn't draw it correctly ("\n"s would just appear as
73 // black boxes)
74 //
75 // NB: we do it here and not in MSWGetStyle() because we need the label
76 // value and the label is not set yet when MSWGetStyle() is called
77 WXDWORD exstyle;
78 WXDWORD msStyle = MSWGetStyle(style, &exstyle);
79 msStyle |= wxMSWButton::GetMultilineStyle(label);
80
81 return MSWCreateControl(_T("BUTTON"), msStyle, pos, size, label, exstyle);
82 }
83
84 WXDWORD wxToggleButton::MSWGetStyle(long style, WXDWORD *exstyle) const
85 {
86 WXDWORD msStyle = wxControl::MSWGetStyle(style, exstyle);
87
88 msStyle |= BS_AUTOCHECKBOX | BS_PUSHLIKE | WS_TABSTOP;
89
90 if ( style & wxBU_LEFT )
91 msStyle |= BS_LEFT;
92 if ( style & wxBU_RIGHT )
93 msStyle |= BS_RIGHT;
94 if ( style & wxBU_TOP )
95 msStyle |= BS_TOP;
96 if ( style & wxBU_BOTTOM )
97 msStyle |= BS_BOTTOM;
98
99 return msStyle;
100 }
101
102 wxSize wxToggleButton::DoGetBestSize() const
103 {
104 return wxMSWButton::ComputeBestSize(const_cast<wxToggleButton *>(this));
105 }
106
107 void wxToggleButton::SetLabel(const wxString& label)
108 {
109 wxMSWButton::UpdateMultilineStyle(GetHwnd(), label);
110
111 wxToggleButtonBase::SetLabel(label);
112 }
113
114 void wxToggleButton::SetValue(bool val)
115 {
116 ::SendMessage(GetHwnd(), BM_SETCHECK, val, 0);
117 }
118
119 bool wxToggleButton::GetValue() const
120 {
121 return ::SendMessage(GetHwnd(), BM_GETCHECK, 0, 0) == BST_CHECKED;
122 }
123
124 void wxToggleButton::Command(wxCommandEvent& event)
125 {
126 SetValue(event.GetInt() != 0);
127 ProcessCommand(event);
128 }
129
130 bool wxToggleButton::MSWCommand(WXUINT WXUNUSED(param), WXWORD WXUNUSED(id))
131 {
132 wxCommandEvent event(wxEVT_COMMAND_TOGGLEBUTTON_CLICKED, m_windowId);
133 event.SetInt(GetValue());
134 event.SetEventObject(this);
135 ProcessCommand(event);
136 return true;
137 }
138
139 #endif // wxUSE_TOGGLEBTN