]> git.saurik.com Git - wxWidgets.git/blame - src/gtk/tglbtn.cpp
Go back to using a fixed value of 3 for lines/columns per action for mouse wheel...
[wxWidgets.git] / src / gtk / tglbtn.cpp
CommitLineData
1db8dc4a 1/////////////////////////////////////////////////////////////////////////////
f1e01716 2// Name: src/gtk/tglbtn.cpp
1db8dc4a
VZ
3// Purpose: Definition of the wxToggleButton class, which implements a
4// toggle button under wxGTK.
5// Author: John Norris, minor changes by Axel Schlueter
6// Modified by:
7// Created: 08.02.01
1db8dc4a 8// Copyright: (c) 2000 Johnny C. Norris II
526954c5 9// Licence: wxWindows licence
1db8dc4a
VZ
10/////////////////////////////////////////////////////////////////////////////
11
14f355c2
VS
12// For compilers that support precompilation, includes "wx.h".
13#include "wx/wxprec.h"
14
f1e01716
WS
15#if wxUSE_TOGGLEBTN
16
1db8dc4a
VZ
17#include "wx/tglbtn.h"
18
f1e01716
WS
19#ifndef WX_PRECOMP
20 #include "wx/button.h"
21#endif
1db8dc4a 22
9dc44eff 23#include <gtk/gtk.h>
9e691f46 24#include "wx/gtk/private.h"
1db8dc4a 25
1db8dc4a 26extern bool g_blockEventsOnDrag;
1db8dc4a 27
865bb325 28extern "C" {
9864c56d 29static void gtk_togglebutton_clicked_callback(GtkWidget *WXUNUSED(widget), wxToggleButton *cb)
1db8dc4a 30{
8ab75332 31 if (g_blockEventsOnDrag)
91af0895
WS
32 return;
33
91af0895 34 // Generate a wx event.
ce7fe42e 35 wxCommandEvent event(wxEVT_TOGGLEBUTTON, cb->GetId());
91af0895
WS
36 event.SetInt(cb->GetValue());
37 event.SetEventObject(cb);
937013e0 38 cb->HandleWindowEvent(event);
1db8dc4a 39}
865bb325 40}
1db8dc4a 41
ce7fe42e 42wxDEFINE_EVENT( wxEVT_TOGGLEBUTTON, wxCommandEvent );
1db8dc4a 43
4f856067 44// ------------------------------------------------------------------------
10ff9c61 45// wxBitmapToggleButton
4f856067
RR
46// ------------------------------------------------------------------------
47
b4354db1 48IMPLEMENT_DYNAMIC_CLASS(wxBitmapToggleButton, wxToggleButton)
4f856067 49
10ff9c61 50bool wxBitmapToggleButton::Create(wxWindow *parent, wxWindowID id,
b4354db1 51 const wxBitmap &bitmap, const wxPoint &pos,
4f856067
RR
52 const wxSize &size, long style,
53 const wxValidator& validator,
54 const wxString &name)
55{
b4354db1
VZ
56 if ( !wxToggleButton::Create(parent, id, wxEmptyString, pos, size, style | wxBU_NOTEXT | wxBU_EXACTFIT,
57 validator, name) )
91af0895 58 return false;
4f856067 59
b4354db1 60 if ( bitmap.IsOk() )
4f856067 61 {
b4354db1 62 SetBitmapLabel(bitmap);
4f856067 63
b4354db1
VZ
64 // we need to adjust the size after setting the bitmap as it may be too
65 // big for the default button size
66 SetInitialSize(size);
ad60f9e7
JS
67 }
68
91af0895 69 return true;
4f856067
RR
70}
71
9d522606 72
4f856067
RR
73// ------------------------------------------------------------------------
74// wxToggleButton
75// ------------------------------------------------------------------------
76
77IMPLEMENT_DYNAMIC_CLASS(wxToggleButton, wxControl)
78
1db8dc4a
VZ
79bool wxToggleButton::Create(wxWindow *parent, wxWindowID id,
80 const wxString &label, const wxPoint &pos,
81 const wxSize &size, long style,
82 const wxValidator& validator,
83 const wxString &name)
84{
91af0895 85 if (!PreCreation(parent, pos, size) ||
e338053c
PC
86 !CreateBase(parent, id, pos, size, style, validator, name ))
87 {
91af0895
WS
88 wxFAIL_MSG(wxT("wxToggleButton creation failed"));
89 return false;
90 }
1db8dc4a 91
b4354db1
VZ
92 // create either a standard toggle button with text label (which may still contain
93 // an image under GTK+ 2.6+) or a bitmap-only toggle button if we don't have any
94 // label
95 const bool
96 useLabel = !(style & wxBU_NOTEXT) && !label.empty();
97 if ( useLabel )
98 {
99 m_widget = gtk_toggle_button_new_with_mnemonic("");
100 }
101 else // no label, suppose we will have a bitmap
102 {
103 m_widget = gtk_toggle_button_new();
104
105 GtkWidget *image = gtk_image_new();
106 gtk_widget_show(image);
107 gtk_container_add(GTK_CONTAINER(m_widget), image);
108 }
109
9ff9d30c 110 g_object_ref(m_widget);
ecf3b4a5 111
b4354db1
VZ
112 if ( useLabel )
113 SetLabel(label);
1db8dc4a 114
9fa72bd2
MR
115 g_signal_connect (m_widget, "clicked",
116 G_CALLBACK (gtk_togglebutton_clicked_callback),
117 this);
1db8dc4a 118
91af0895 119 m_parent->DoAddChild(this);
1db8dc4a 120
91af0895
WS
121 PostCreation(size);
122
123 return true;
1db8dc4a
VZ
124}
125
c71ab7c1
RR
126void wxToggleButton::GTKDisableEvents()
127{
128 g_signal_handlers_block_by_func(m_widget,
129 (gpointer) gtk_togglebutton_clicked_callback, this);
130}
131
132void wxToggleButton::GTKEnableEvents()
133{
134 g_signal_handlers_unblock_by_func(m_widget,
135 (gpointer) gtk_togglebutton_clicked_callback, this);
136}
137
1db8dc4a
VZ
138// void SetValue(bool state)
139// Set the value of the toggle button.
140void wxToggleButton::SetValue(bool state)
141{
91af0895 142 wxCHECK_RET(m_widget != NULL, wxT("invalid toggle button"));
1db8dc4a 143
91af0895
WS
144 if (state == GetValue())
145 return;
1db8dc4a 146
5e53c1c2 147 GTKDisableEvents();
1db8dc4a 148
91af0895 149 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(m_widget), state);
1db8dc4a 150
5e53c1c2 151 GTKEnableEvents();
1db8dc4a
VZ
152}
153
154// bool GetValue() const
155// Get the value of the toggle button.
156bool wxToggleButton::GetValue() const
157{
91af0895 158 wxCHECK_MSG(m_widget != NULL, false, wxT("invalid toggle button"));
1db8dc4a 159
385e8575 160 return gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(m_widget)) != 0;
1db8dc4a
VZ
161}
162
1db8dc4a
VZ
163void wxToggleButton::SetLabel(const wxString& label)
164{
8ab696e0 165 wxCHECK_RET(m_widget != NULL, wxT("invalid toggle button"));
1db8dc4a 166
b4354db1 167 wxAnyButton::SetLabel(label);
1db8dc4a 168
b29bd6b4
VZ
169 if ( HasFlag(wxBU_NOTEXT) )
170 {
171 // Don't try to update the label for a button not showing it, this is
172 // unnecessary and can also actually replace the image we show with the
173 // label entirely breaking the button code, see #13693.
174 return;
175 }
176
ecf3b4a5
RR
177 const wxString labelGTK = GTKConvertMnemonics(label);
178
179 gtk_button_set_label(GTK_BUTTON(m_widget), wxGTK_CONV(labelGTK));
180
496e7ec6 181 GTKApplyWidgetStyle( false );
1db8dc4a
VZ
182}
183
b4354db1
VZ
184#if wxUSE_MARKUP
185bool wxToggleButton::DoSetLabelMarkup(const wxString& markup)
1db8dc4a 186{
b4354db1
VZ
187 wxCHECK_MSG( m_widget != NULL, false, "invalid toggle button" );
188
189 const wxString stripped = RemoveMarkup(markup);
190 if ( stripped.empty() && !markup.empty() )
91af0895 191 return false;
1db8dc4a 192
b4354db1
VZ
193 wxControl::SetLabel(stripped);
194
b29bd6b4
VZ
195 if ( !HasFlag(wxBU_NOTEXT) )
196 {
197 GtkLabel * const label = GTKGetLabel();
198 wxCHECK_MSG( label, false, "no label in this toggle button?" );
1db8dc4a 199
b29bd6b4
VZ
200 GTKSetLabelWithMarkupForLabel(label, markup);
201 }
ad60f9e7 202
91af0895 203 return true;
1db8dc4a 204}
b4354db1 205#endif // wxUSE_MARKUP
1db8dc4a 206
b4354db1 207GtkLabel *wxToggleButton::GTKGetLabel() const
1db8dc4a 208{
b4354db1
VZ
209 GtkWidget* child = gtk_bin_get_child(GTK_BIN(m_widget));
210 return GTK_LABEL(child);
1db8dc4a
VZ
211}
212
b4354db1 213void wxToggleButton::DoApplyWidgetStyle(GtkRcStyle *style)
1db8dc4a 214{
9dc44eff
PC
215 GTKApplyStyle(m_widget, style);
216 GTKApplyStyle(gtk_bin_get_child(GTK_BIN(m_widget)), style);
1db8dc4a
VZ
217}
218
1db8dc4a
VZ
219// Get the "best" size for this control.
220wxSize wxToggleButton::DoGetBestSize() const
221{
b4354db1 222 wxSize ret(wxAnyButton::DoGetBestSize());
91af0895 223
8ab696e0
RR
224 if (!HasFlag(wxBU_EXACTFIT))
225 {
226 if (ret.x < 80) ret.x = 80;
227 }
91af0895 228
9f884528
RD
229 CacheBestSize(ret);
230 return ret;
1db8dc4a
VZ
231}
232
9d522606
RD
233// static
234wxVisualAttributes
235wxToggleButton::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
236{
7fff16b8 237 return GetDefaultAttributesFromGTKWidget(gtk_toggle_button_new());
9d522606
RD
238}
239
1db8dc4a 240#endif // wxUSE_TOGGLEBTN