1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk/tglbtn.cpp
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
8 // Copyright: (c) 2000 Johnny C. Norris II
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 // For compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
14
15 #if wxUSE_TOGGLEBTN
16
17 #include "wx/tglbtn.h"
18
19 #ifndef WX_PRECOMP
20 #include "wx/button.h"
21 #endif
22
23 #include "wx/gtk/private.h"
24 #include "wx/gtk/private/eventsdisabler.h"
25 #include "wx/gtk/private/image.h"
26 #include "wx/gtk/private/list.h"
27
28 extern bool g_blockEventsOnDrag;
29
30 extern "C" {
gtk_togglebutton_clicked_callback(GtkWidget * WXUNUSED (widget),wxToggleButton * cb)31 static void gtk_togglebutton_clicked_callback(GtkWidget *WXUNUSED(widget), wxToggleButton *cb)
32 {
33 if (g_blockEventsOnDrag)
34 return;
35
36 // Generate a wx event.
37 wxCommandEvent event(wxEVT_TOGGLEBUTTON, cb->GetId());
38 event.SetInt(cb->GetValue());
39 event.SetEventObject(cb);
40 cb->HandleWindowEvent(event);
41 }
42 }
43
44 wxDEFINE_EVENT( wxEVT_TOGGLEBUTTON, wxCommandEvent );
45
46 // ------------------------------------------------------------------------
47 // wxBitmapToggleButton
48 // ------------------------------------------------------------------------
49
50 wxIMPLEMENT_DYNAMIC_CLASS(wxBitmapToggleButton, wxToggleButton);
51
Create(wxWindow * parent,wxWindowID id,const wxBitmap & bitmap,const wxPoint & pos,const wxSize & size,long style,const wxValidator & validator,const wxString & name)52 bool wxBitmapToggleButton::Create(wxWindow *parent, wxWindowID id,
53 const wxBitmap &bitmap, const wxPoint &pos,
54 const wxSize &size, long style,
55 const wxValidator& validator,
56 const wxString &name)
57 {
58 if ( !wxToggleButton::Create(parent, id, wxEmptyString, pos, size, style | wxBU_NOTEXT | wxBU_EXACTFIT,
59 validator, name) )
60 return false;
61
62 if ( bitmap.IsOk() )
63 {
64 SetBitmapLabel(bitmap);
65
66 // we need to adjust the size after setting the bitmap as it may be too
67 // big for the default button size
68 SetInitialSize(size);
69 }
70
71 return true;
72 }
73
74
75 // ------------------------------------------------------------------------
76 // wxToggleButton
77 // ------------------------------------------------------------------------
78
79 wxIMPLEMENT_DYNAMIC_CLASS(wxToggleButton, wxControl);
80
Create(wxWindow * parent,wxWindowID id,const wxString & label,const wxPoint & pos,const wxSize & size,long style,const wxValidator & validator,const wxString & name)81 bool wxToggleButton::Create(wxWindow *parent, wxWindowID id,
82 const wxString &label, const wxPoint &pos,
83 const wxSize &size, long style,
84 const wxValidator& validator,
85 const wxString &name)
86 {
87 if (!PreCreation(parent, pos, size) ||
88 !CreateBase(parent, id, pos, size, style, validator, name ))
89 {
90 wxFAIL_MSG(wxT("wxToggleButton creation failed"));
91 return false;
92 }
93
94 // create either a standard toggle button with text label (which may still contain
95 // an image under GTK+ 2.6+) or a bitmap-only toggle button if we don't have any
96 // label
97 const bool
98 useLabel = !(style & wxBU_NOTEXT) && !label.empty();
99 if ( useLabel )
100 {
101 m_widget = gtk_toggle_button_new_with_mnemonic("");
102 }
103 else // no label, suppose we will have a bitmap
104 {
105 m_widget = gtk_toggle_button_new();
106
107 GtkWidget* image = wxGtkImage::New(this);
108 gtk_widget_show(image);
109 gtk_container_add(GTK_CONTAINER(m_widget), image);
110 }
111
112 g_object_ref(m_widget);
113
114 if ( useLabel )
115 SetLabel(label);
116
117 g_signal_connect (m_widget, "clicked",
118 G_CALLBACK (gtk_togglebutton_clicked_callback),
119 this);
120
121 m_parent->DoAddChild(this);
122
123 PostCreation(size);
124
125 return true;
126 }
127
GTKDisableEvents()128 void wxToggleButton::GTKDisableEvents()
129 {
130 g_signal_handlers_block_by_func(m_widget,
131 (gpointer) gtk_togglebutton_clicked_callback, this);
132 }
133
GTKEnableEvents()134 void wxToggleButton::GTKEnableEvents()
135 {
136 g_signal_handlers_unblock_by_func(m_widget,
137 (gpointer) gtk_togglebutton_clicked_callback, this);
138 }
139
140 // void SetValue(bool state)
141 // Set the value of the toggle button.
SetValue(bool state)142 void wxToggleButton::SetValue(bool state)
143 {
144 wxCHECK_RET(m_widget != NULL, wxT("invalid toggle button"));
145
146 if (state == GetValue())
147 return;
148
149 wxGtkEventsDisabler<wxToggleButton> noEvents(this);
150
151 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(m_widget), state);
152 GTKUpdateBitmap();
153 }
154
155 // bool GetValue() const
156 // Get the value of the toggle button.
GetValue() const157 bool wxToggleButton::GetValue() const
158 {
159 wxCHECK_MSG(m_widget != NULL, false, wxT("invalid toggle button"));
160
161 return gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(m_widget)) != 0;
162 }
163
SetLabel(const wxString & label)164 void wxToggleButton::SetLabel(const wxString& label)
165 {
166 wxCHECK_RET(m_widget != NULL, wxT("invalid toggle button"));
167
168 wxAnyButton::SetLabel(label);
169
170 if ( HasFlag(wxBU_NOTEXT) )
171 {
172 // Don't try to update the label for a button not showing it, this is
173 // unnecessary and can also actually replace the image we show with the
174 // label entirely breaking the button code, see #13693.
175 return;
176 }
177
178 const wxString labelGTK = GTKConvertMnemonics(label);
179
180 gtk_button_set_label(GTK_BUTTON(m_widget), wxGTK_CONV(labelGTK));
181
182 GTKApplyWidgetStyle( false );
183 }
184
185 #if wxUSE_MARKUP
DoSetLabelMarkup(const wxString & markup)186 bool wxToggleButton::DoSetLabelMarkup(const wxString& markup)
187 {
188 wxCHECK_MSG( m_widget != NULL, false, "invalid toggle button" );
189
190 const wxString stripped = RemoveMarkup(markup);
191 if ( stripped.empty() && !markup.empty() )
192 return false;
193
194 wxControl::SetLabel(stripped);
195
196 if ( !HasFlag(wxBU_NOTEXT) )
197 {
198 GtkLabel * const label = GTKGetLabel();
199 wxCHECK_MSG( label, false, "no label in this toggle button?" );
200
201 GTKSetLabelWithMarkupForLabel(label, markup);
202 }
203
204 return true;
205 }
206 #endif // wxUSE_MARKUP
207
GTKGetLabel() const208 GtkLabel *wxToggleButton::GTKGetLabel() const
209 {
210 GtkWidget* child = gtk_bin_get_child(GTK_BIN(m_widget));
211 return GTK_LABEL(child);
212 }
213
DoApplyWidgetStyle(GtkRcStyle * style)214 void wxToggleButton::DoApplyWidgetStyle(GtkRcStyle *style)
215 {
216 GTKApplyStyle(m_widget, style);
217 GtkWidget* child = gtk_bin_get_child(GTK_BIN(m_widget));
218 GTKApplyStyle(child, style);
219
220 #ifndef __WXGTK4__
221 wxGCC_WARNING_SUPPRESS(deprecated-declarations)
222 // for buttons with images, the path to the label is (at least in 2.12)
223 // GtkButton -> GtkAlignment -> GtkHBox -> GtkLabel
224 if ( GTK_IS_ALIGNMENT(child) )
225 {
226 GtkWidget* box = gtk_bin_get_child(GTK_BIN(child));
227 if ( GTK_IS_BOX(box) )
228 {
229 wxGtkList list(gtk_container_get_children(GTK_CONTAINER(box)));
230 for (GList* item = list; item; item = item->next)
231 {
232 GTKApplyStyle(GTK_WIDGET(item->data), style);
233 }
234 }
235 }
236 wxGCC_WARNING_RESTORE()
237 #endif
238 }
239
240 // Get the "best" size for this control.
DoGetBestSize() const241 wxSize wxToggleButton::DoGetBestSize() const
242 {
243 wxSize ret(wxAnyButton::DoGetBestSize());
244
245 if (!HasFlag(wxBU_EXACTFIT))
246 {
247 if (ret.x < 80) ret.x = 80;
248 }
249
250 return ret;
251 }
252
253 // static
254 wxVisualAttributes
GetClassDefaultAttributes(wxWindowVariant WXUNUSED (variant))255 wxToggleButton::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
256 {
257 return GetDefaultAttributesFromGTKWidget(gtk_toggle_button_new());
258 }
259
260 #endif // wxUSE_TOGGLEBTN
261