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
8 // Copyright: (c) 2000 Johnny C. Norris II
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // For compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
17 #include "wx/tglbtn.h"
20 #include "wx/button.h"
24 #include "wx/gtk/private.h"
26 extern bool g_blockEventsOnDrag
;
29 static void gtk_togglebutton_clicked_callback(GtkWidget
*WXUNUSED(widget
), wxToggleButton
*cb
)
31 if (g_blockEventsOnDrag
)
34 // Generate a wx event.
35 wxCommandEvent
event(wxEVT_TOGGLEBUTTON
, cb
->GetId());
36 event
.SetInt(cb
->GetValue());
37 event
.SetEventObject(cb
);
38 cb
->HandleWindowEvent(event
);
42 wxDEFINE_EVENT( wxEVT_TOGGLEBUTTON
, wxCommandEvent
);
44 // ------------------------------------------------------------------------
45 // wxBitmapToggleButton
46 // ------------------------------------------------------------------------
48 IMPLEMENT_DYNAMIC_CLASS(wxBitmapToggleButton
, wxToggleButton
)
50 bool wxBitmapToggleButton::Create(wxWindow
*parent
, wxWindowID id
,
51 const wxBitmap
&bitmap
, const wxPoint
&pos
,
52 const wxSize
&size
, long style
,
53 const wxValidator
& validator
,
56 if ( !wxToggleButton::Create(parent
, id
, wxEmptyString
, pos
, size
, style
| wxBU_NOTEXT
| wxBU_EXACTFIT
,
62 SetBitmapLabel(bitmap
);
64 // we need to adjust the size after setting the bitmap as it may be too
65 // big for the default button size
73 // ------------------------------------------------------------------------
75 // ------------------------------------------------------------------------
77 IMPLEMENT_DYNAMIC_CLASS(wxToggleButton
, wxControl
)
79 bool wxToggleButton::Create(wxWindow
*parent
, wxWindowID id
,
80 const wxString
&label
, const wxPoint
&pos
,
81 const wxSize
&size
, long style
,
82 const wxValidator
& validator
,
85 if (!PreCreation(parent
, pos
, size
) ||
86 !CreateBase(parent
, id
, pos
, size
, style
, validator
, name
))
88 wxFAIL_MSG(wxT("wxToggleButton creation failed"));
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
96 useLabel
= !(style
& wxBU_NOTEXT
) && !label
.empty();
99 m_widget
= gtk_toggle_button_new_with_mnemonic("");
101 else // no label, suppose we will have a bitmap
103 m_widget
= gtk_toggle_button_new();
105 GtkWidget
*image
= gtk_image_new();
106 gtk_widget_show(image
);
107 gtk_container_add(GTK_CONTAINER(m_widget
), image
);
110 g_object_ref(m_widget
);
115 g_signal_connect (m_widget
, "clicked",
116 G_CALLBACK (gtk_togglebutton_clicked_callback
),
119 m_parent
->DoAddChild(this);
126 void wxToggleButton::GTKDisableEvents()
128 g_signal_handlers_block_by_func(m_widget
,
129 (gpointer
) gtk_togglebutton_clicked_callback
, this);
132 void wxToggleButton::GTKEnableEvents()
134 g_signal_handlers_unblock_by_func(m_widget
,
135 (gpointer
) gtk_togglebutton_clicked_callback
, this);
138 // void SetValue(bool state)
139 // Set the value of the toggle button.
140 void wxToggleButton::SetValue(bool state
)
142 wxCHECK_RET(m_widget
!= NULL
, wxT("invalid toggle button"));
144 if (state
== GetValue())
149 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(m_widget
), state
);
154 // bool GetValue() const
155 // Get the value of the toggle button.
156 bool wxToggleButton::GetValue() const
158 wxCHECK_MSG(m_widget
!= NULL
, false, wxT("invalid toggle button"));
160 return gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(m_widget
)) != 0;
163 void wxToggleButton::SetLabel(const wxString
& label
)
165 wxCHECK_RET(m_widget
!= NULL
, wxT("invalid toggle button"));
167 wxAnyButton::SetLabel(label
);
169 if ( HasFlag(wxBU_NOTEXT
) )
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.
177 const wxString labelGTK
= GTKConvertMnemonics(label
);
179 gtk_button_set_label(GTK_BUTTON(m_widget
), wxGTK_CONV(labelGTK
));
181 GTKApplyWidgetStyle( false );
185 bool wxToggleButton::DoSetLabelMarkup(const wxString
& markup
)
187 wxCHECK_MSG( m_widget
!= NULL
, false, "invalid toggle button" );
189 const wxString stripped
= RemoveMarkup(markup
);
190 if ( stripped
.empty() && !markup
.empty() )
193 wxControl::SetLabel(stripped
);
195 if ( !HasFlag(wxBU_NOTEXT
) )
197 GtkLabel
* const label
= GTKGetLabel();
198 wxCHECK_MSG( label
, false, "no label in this toggle button?" );
200 GTKSetLabelWithMarkupForLabel(label
, markup
);
205 #endif // wxUSE_MARKUP
207 GtkLabel
*wxToggleButton::GTKGetLabel() const
209 GtkWidget
* child
= gtk_bin_get_child(GTK_BIN(m_widget
));
210 return GTK_LABEL(child
);
213 void wxToggleButton::DoApplyWidgetStyle(GtkRcStyle
*style
)
215 GTKApplyStyle(m_widget
, style
);
216 GTKApplyStyle(gtk_bin_get_child(GTK_BIN(m_widget
)), style
);
219 // Get the "best" size for this control.
220 wxSize
wxToggleButton::DoGetBestSize() const
222 wxSize
ret(wxAnyButton::DoGetBestSize());
224 if (!HasFlag(wxBU_EXACTFIT
))
226 if (ret
.x
< 80) ret
.x
= 80;
235 wxToggleButton::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
237 return GetDefaultAttributesFromGTKWidget(gtk_toggle_button_new());
240 #endif // wxUSE_TOGGLEBTN