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