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
9 // Copyright: (c) 2000 Johnny C. Norris II
10 // Licence: wxWindows licence
11 /////////////////////////////////////////////////////////////////////////////
13 // For compilers that support precompilation, includes "wx.h".
14 #include "wx/wxprec.h"
18 #include "wx/tglbtn.h"
21 #include "wx/button.h"
25 #include "wx/gtk/private.h"
27 extern bool g_blockEventsOnDrag
;
30 static void gtk_togglebutton_clicked_callback(GtkWidget
*WXUNUSED(widget
), wxToggleButton
*cb
)
32 if (g_blockEventsOnDrag
)
35 // Generate a wx event.
36 wxCommandEvent
event(wxEVT_COMMAND_TOGGLEBUTTON_CLICKED
, cb
->GetId());
37 event
.SetInt(cb
->GetValue());
38 event
.SetEventObject(cb
);
39 cb
->HandleWindowEvent(event
);
43 wxDEFINE_EVENT( wxEVT_COMMAND_TOGGLEBUTTON_CLICKED
, wxCommandEvent
);
45 // ------------------------------------------------------------------------
46 // wxBitmapToggleButton
47 // ------------------------------------------------------------------------
49 IMPLEMENT_DYNAMIC_CLASS(wxBitmapToggleButton
, wxToggleButton
)
51 bool wxBitmapToggleButton::Create(wxWindow
*parent
, wxWindowID id
,
52 const wxBitmap
&bitmap
, const wxPoint
&pos
,
53 const wxSize
&size
, long style
,
54 const wxValidator
& validator
,
57 if ( !wxToggleButton::Create(parent
, id
, wxEmptyString
, pos
, size
, style
| wxBU_NOTEXT
| wxBU_EXACTFIT
,
63 SetBitmapLabel(bitmap
);
65 // we need to adjust the size after setting the bitmap as it may be too
66 // big for the default button size
74 // ------------------------------------------------------------------------
76 // ------------------------------------------------------------------------
78 IMPLEMENT_DYNAMIC_CLASS(wxToggleButton
, wxControl
)
80 bool wxToggleButton::Create(wxWindow
*parent
, wxWindowID id
,
81 const wxString
&label
, const wxPoint
&pos
,
82 const wxSize
&size
, long style
,
83 const wxValidator
& validator
,
86 if (!PreCreation(parent
, pos
, size
) ||
87 !CreateBase(parent
, id
, pos
, size
, style
, validator
, name
))
89 wxFAIL_MSG(wxT("wxToggleButton creation failed"));
93 // create either a standard toggle button with text label (which may still contain
94 // an image under GTK+ 2.6+) or a bitmap-only toggle button if we don't have any
97 useLabel
= !(style
& wxBU_NOTEXT
) && !label
.empty();
100 m_widget
= gtk_toggle_button_new_with_mnemonic("");
102 else // no label, suppose we will have a bitmap
104 m_widget
= gtk_toggle_button_new();
106 GtkWidget
*image
= gtk_image_new();
107 gtk_widget_show(image
);
108 gtk_container_add(GTK_CONTAINER(m_widget
), image
);
111 g_object_ref(m_widget
);
116 g_signal_connect (m_widget
, "clicked",
117 G_CALLBACK (gtk_togglebutton_clicked_callback
),
120 m_parent
->DoAddChild(this);
127 void wxToggleButton::GTKDisableEvents()
129 g_signal_handlers_block_by_func(m_widget
,
130 (gpointer
) gtk_togglebutton_clicked_callback
, this);
133 void wxToggleButton::GTKEnableEvents()
135 g_signal_handlers_unblock_by_func(m_widget
,
136 (gpointer
) gtk_togglebutton_clicked_callback
, this);
139 // void SetValue(bool state)
140 // Set the value of the toggle button.
141 void wxToggleButton::SetValue(bool state
)
143 wxCHECK_RET(m_widget
!= NULL
, wxT("invalid toggle button"));
145 if (state
== GetValue())
150 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(m_widget
), state
);
155 // bool GetValue() const
156 // Get the value of the toggle button.
157 bool wxToggleButton::GetValue() const
159 wxCHECK_MSG(m_widget
!= NULL
, false, wxT("invalid toggle button"));
161 return gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(m_widget
)) != 0;
164 void wxToggleButton::SetLabel(const wxString
& label
)
166 wxCHECK_RET(m_widget
!= NULL
, wxT("invalid toggle button"));
168 wxAnyButton::SetLabel(label
);
170 if ( HasFlag(wxBU_NOTEXT
) )
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.
178 const wxString labelGTK
= GTKConvertMnemonics(label
);
180 gtk_button_set_label(GTK_BUTTON(m_widget
), wxGTK_CONV(labelGTK
));
182 GTKApplyWidgetStyle( false );
186 bool wxToggleButton::DoSetLabelMarkup(const wxString
& markup
)
188 wxCHECK_MSG( m_widget
!= NULL
, false, "invalid toggle button" );
190 const wxString stripped
= RemoveMarkup(markup
);
191 if ( stripped
.empty() && !markup
.empty() )
194 wxControl::SetLabel(stripped
);
196 if ( !HasFlag(wxBU_NOTEXT
) )
198 GtkLabel
* const label
= GTKGetLabel();
199 wxCHECK_MSG( label
, false, "no label in this toggle button?" );
201 GTKSetLabelWithMarkupForLabel(label
, markup
);
206 #endif // wxUSE_MARKUP
208 GtkLabel
*wxToggleButton::GTKGetLabel() const
210 GtkWidget
* child
= gtk_bin_get_child(GTK_BIN(m_widget
));
211 return GTK_LABEL(child
);
214 void wxToggleButton::DoApplyWidgetStyle(GtkRcStyle
*style
)
216 GTKApplyStyle(m_widget
, style
);
217 GTKApplyStyle(gtk_bin_get_child(GTK_BIN(m_widget
)), style
);
220 // Get the "best" size for this control.
221 wxSize
wxToggleButton::DoGetBestSize() const
223 wxSize
ret(wxAnyButton::DoGetBestSize());
225 if (!HasFlag(wxBU_EXACTFIT
))
227 if (ret
.x
< 80) ret
.x
= 80;
236 wxToggleButton::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
238 return GetDefaultAttributesFromGTKWidget(gtk_toggle_button_new
);
241 #endif // wxUSE_TOGGLEBTN