]> git.saurik.com Git - wxWidgets.git/blame - src/msw/tglbtn.cpp
Expose the Apple menu so it can be setup manually.
[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
1db8dc4a 9// Copyright: (c) 2000 Johnny C. Norris II
526954c5 10// Licence: wxWindows licence
1db8dc4a
VZ
11/////////////////////////////////////////////////////////////////////////////
12
13// ============================================================================
8907154c 14// declarations
1db8dc4a
VZ
15// ============================================================================
16
17// ----------------------------------------------------------------------------
18// headers
19// ----------------------------------------------------------------------------
20
21#include "wx/wxprec.h"
22
23#ifdef __BORLANDC__
24 #pragma hdrstop
25#endif
26
1db8dc4a
VZ
27#if wxUSE_TOGGLEBTN
28
f1e01716
WS
29#include "wx/tglbtn.h"
30
1db8dc4a
VZ
31#ifndef WX_PRECOMP
32 #include "wx/button.h"
33 #include "wx/brush.h"
34 #include "wx/dcscreen.h"
35 #include "wx/settings.h"
36
37 #include "wx/log.h"
38#endif // WX_PRECOMP
39
40#include "wx/msw/private.h"
9016f3ad 41#include "wx/msw/private/button.h"
1db8dc4a
VZ
42
43// ----------------------------------------------------------------------------
44// macros
45// ----------------------------------------------------------------------------
46
ce7fe42e 47wxDEFINE_EVENT( wxEVT_TOGGLEBUTTON, wxCommandEvent );
1db8dc4a 48
1db8dc4a
VZ
49// ============================================================================
50// implementation
51// ============================================================================
52
76c13f8f
RR
53//-----------------------------------------------------------------------------
54// wxBitmapToggleButton
55//-----------------------------------------------------------------------------
56
b4354db1 57IMPLEMENT_DYNAMIC_CLASS(wxBitmapToggleButton, wxToggleButton)
76c13f8f
RR
58
59bool wxBitmapToggleButton::Create( wxWindow *parent, wxWindowID id,
60 const wxBitmap& label,const wxPoint& pos, const wxSize& size, long style,
61 const wxValidator& validator, const wxString& name )
62{
b4354db1 63 if (!wxToggleButton::Create( parent, id, wxEmptyString, pos, size, style, validator, name ))
76c13f8f 64 return false;
03647350 65
b4354db1 66 SetBitmap(label);
03647350 67
76c13f8f
RR
68 if (size.x == -1 || size.y == -1)
69 {
70 wxSize new_size = GetBestSize();
71 if (size.x != -1)
72 new_size.x = size.x;
73 if (size.y != -1)
74 new_size.y = size.y;
75 SetSize( new_size );
76 }
03647350 77
76c13f8f
RR
78 return true;
79}
80
76c13f8f 81
1db8dc4a
VZ
82// ----------------------------------------------------------------------------
83// wxToggleButton
84// ----------------------------------------------------------------------------
85
76c13f8f
RR
86IMPLEMENT_DYNAMIC_CLASS(wxToggleButton, wxControl)
87
b4354db1
VZ
88void wxToggleButton::Init()
89{
90 m_state = false;
91}
92
1db8dc4a 93// Single check box item
9016f3ad
VZ
94bool wxToggleButton::Create(wxWindow *parent,
95 wxWindowID id,
1db8dc4a
VZ
96 const wxString& label,
97 const wxPoint& pos,
98 const wxSize& size, long style,
99 const wxValidator& validator,
100 const wxString& name)
101{
b4354db1
VZ
102 Init();
103
b67e8d38 104 if ( !CreateControl(parent, id, pos, size, style, validator, name) )
bfbb0b4c
WS
105 return false;
106
9016f3ad
VZ
107 // if the label contains several lines we must explicitly tell the button
108 // about it or it wouldn't draw it correctly ("\n"s would just appear as
109 // black boxes)
110 //
111 // NB: we do it here and not in MSWGetStyle() because we need the label
112 // value and the label is not set yet when MSWGetStyle() is called
113 WXDWORD exstyle;
114 WXDWORD msStyle = MSWGetStyle(style, &exstyle);
115 msStyle |= wxMSWButton::GetMultilineStyle(label);
116
9a83f860 117 return MSWCreateControl(wxT("BUTTON"), msStyle, pos, size, label, exstyle);
b67e8d38 118}
1db8dc4a 119
b67e8d38
RD
120WXDWORD wxToggleButton::MSWGetStyle(long style, WXDWORD *exstyle) const
121{
122 WXDWORD msStyle = wxControl::MSWGetStyle(style, exstyle);
1db8dc4a 123
b67e8d38 124 msStyle |= BS_AUTOCHECKBOX | BS_PUSHLIKE | WS_TABSTOP;
b0766406 125
9016f3ad 126 if ( style & wxBU_LEFT )
1db8dc4a 127 msStyle |= BS_LEFT;
9016f3ad 128 if ( style & wxBU_RIGHT )
1db8dc4a 129 msStyle |= BS_RIGHT;
9016f3ad 130 if ( style & wxBU_TOP )
1db8dc4a 131 msStyle |= BS_TOP;
9016f3ad 132 if ( style & wxBU_BOTTOM )
1db8dc4a 133 msStyle |= BS_BOTTOM;
1db8dc4a 134
b67e8d38 135 return msStyle;
1db8dc4a
VZ
136}
137
1db8dc4a
VZ
138void wxToggleButton::SetValue(bool val)
139{
b4354db1
VZ
140 m_state = val;
141 if ( IsOwnerDrawn() )
142 {
143 Refresh();
144 }
145 else
146 {
147 ::SendMessage(GetHwnd(), BM_SETCHECK, val, 0);
148 }
1db8dc4a
VZ
149}
150
1db8dc4a
VZ
151bool wxToggleButton::GetValue() const
152{
b4354db1
VZ
153 if ( IsOwnerDrawn() )
154 {
155 return m_state;
156 }
157 else
158 {
159 return ::SendMessage(GetHwnd(), BM_GETCHECK, 0, 0) == BST_CHECKED;
160 }
1db8dc4a
VZ
161}
162
9016f3ad 163void wxToggleButton::Command(wxCommandEvent& event)
1db8dc4a 164{
9016f3ad
VZ
165 SetValue(event.GetInt() != 0);
166 ProcessCommand(event);
167}
168
b4354db1 169bool wxToggleButton::MSWCommand(WXUINT param, WXWORD WXUNUSED(id))
9016f3ad 170{
b4354db1
VZ
171 if ( param != BN_CLICKED && param != BN_DBLCLK )
172 return false;
173
174 // first update the value so that user event handler gets the new
175 // toggle button value
176
177 // ownerdrawn buttons don't manage their state themselves unlike usual
178 // auto checkboxes so do it ourselves in any case
179 m_state = !m_state;
180
ce7fe42e 181 wxCommandEvent event(wxEVT_TOGGLEBUTTON, m_windowId);
9016f3ad
VZ
182 event.SetInt(GetValue());
183 event.SetEventObject(this);
184 ProcessCommand(event);
185 return true;
1db8dc4a
VZ
186}
187
b4354db1
VZ
188wxAnyButton::State wxToggleButton::GetNormalState() const
189{
190 if ( GetValue() )
191 return State_Pressed;
192 else
193 return State_Normal;
194}
195
1db8dc4a 196#endif // wxUSE_TOGGLEBTN