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 // License: 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"
24 #include "wx/gtk/private.h"
26 extern bool g_blockEventsOnDrag
;
29 static void gtk_togglebutton_clicked_callback(GtkWidget
*WXUNUSED(widget
), wxToggleButton
*cb
)
32 wxapp_install_idle_handler();
34 if (!cb
->m_hasVMT
|| g_blockEventsOnDrag
)
37 if (cb
->m_blockEvent
) return;
39 // Generate a wx event.
40 wxCommandEvent
event(wxEVT_COMMAND_TOGGLEBUTTON_CLICKED
, cb
->GetId());
41 event
.SetInt(cb
->GetValue());
42 event
.SetEventObject(cb
);
43 cb
->GetEventHandler()->ProcessEvent(event
);
47 DEFINE_EVENT_TYPE(wxEVT_COMMAND_TOGGLEBUTTON_CLICKED
)
49 // ------------------------------------------------------------------------
50 // wxToggleBitmapButton
51 // ------------------------------------------------------------------------
53 IMPLEMENT_DYNAMIC_CLASS(wxToggleBitmapButton
, wxControl
)
55 bool wxToggleBitmapButton::Create(wxWindow
*parent
, wxWindowID id
,
56 const wxBitmap
&label
, const wxPoint
&pos
,
57 const wxSize
&size
, long style
,
58 const wxValidator
& validator
,
65 if (!PreCreation(parent
, pos
, size
) ||
66 !CreateBase(parent
, id
, pos
, size
, style
, validator
, name
))
68 wxFAIL_MSG(wxT("wxToggleBitmapButton creation failed"));
72 // Create the gtk widget.
73 m_widget
= gtk_toggle_button_new();
75 if (style
& wxNO_BORDER
)
76 gtk_button_set_relief( GTK_BUTTON(m_widget
), GTK_RELIEF_NONE
);
81 g_signal_connect (m_widget
, "clicked",
82 G_CALLBACK (gtk_togglebutton_clicked_callback
),
85 m_parent
->DoAddChild(this);
92 // void SetValue(bool state)
93 // Set the value of the toggle button.
94 void wxToggleBitmapButton::SetValue(bool state
)
96 wxCHECK_RET(m_widget
!= NULL
, wxT("invalid toggle button"));
98 if (state
== GetValue())
103 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(m_widget
), state
);
105 m_blockEvent
= false;
108 // bool GetValue() const
109 // Get the value of the toggle button.
110 bool wxToggleBitmapButton::GetValue() const
112 wxCHECK_MSG(m_widget
!= NULL
, false, wxT("invalid toggle button"));
114 return gtk_toggle_button_get_active((GtkToggleButton
*)m_widget
);
117 void wxToggleBitmapButton::SetLabel(const wxBitmap
& label
)
119 wxCHECK_RET(m_widget
!= NULL
, wxT("invalid toggle button"));
122 InvalidateBestSize();
127 void wxToggleBitmapButton::OnSetBitmap()
129 if (!m_bitmap
.Ok()) return;
131 GtkWidget
* image
= ((GtkBin
*)m_widget
)->child
;
135 image
= gtk_image_new_from_pixbuf(m_bitmap
.GetPixbuf());
136 gtk_widget_show(image
);
137 gtk_container_add((GtkContainer
*)m_widget
, image
);
140 { // subsequent bitmaps
141 gtk_image_set_from_pixbuf((GtkImage
*)image
, m_bitmap
.GetPixbuf());
145 bool wxToggleBitmapButton::Enable(bool enable
/*=true*/)
147 if (!wxControl::Enable(enable
))
150 gtk_widget_set_sensitive(GTK_BIN(m_widget
)->child
, enable
);
155 void wxToggleBitmapButton::DoApplyWidgetStyle(GtkRcStyle
*style
)
157 gtk_widget_modify_style(m_widget
, style
);
158 gtk_widget_modify_style(GTK_BIN(m_widget
)->child
, style
);
162 wxToggleBitmapButton::GTKGetWindow(wxArrayGdkWindows
& WXUNUSED(windows
)) const
164 return GTK_BUTTON(m_widget
)->event_window
;
167 // Get the "best" size for this control.
168 wxSize
wxToggleBitmapButton::DoGetBestSize() const
174 int border
= HasFlag(wxNO_BORDER
) ? 4 : 10;
175 best
.x
= m_bitmap
.GetWidth()+border
;
176 best
.y
= m_bitmap
.GetHeight()+border
;
185 wxToggleBitmapButton::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
187 return GetDefaultAttributesFromGTKWidget(gtk_toggle_button_new
);
191 // ------------------------------------------------------------------------
193 // ------------------------------------------------------------------------
195 IMPLEMENT_DYNAMIC_CLASS(wxToggleButton
, wxControl
)
197 bool wxToggleButton::Create(wxWindow
*parent
, wxWindowID id
,
198 const wxString
&label
, const wxPoint
&pos
,
199 const wxSize
&size
, long style
,
200 const wxValidator
& validator
,
201 const wxString
&name
)
205 m_blockEvent
= false;
207 if (!PreCreation(parent
, pos
, size
) ||
208 !CreateBase(parent
, id
, pos
, size
, style
, validator
, name
))
210 wxFAIL_MSG(wxT("wxToggleButton creation failed"));
214 // Create the gtk widget.
215 m_widget
= gtk_toggle_button_new_with_mnemonic("");
219 g_signal_connect (m_widget
, "clicked",
220 G_CALLBACK (gtk_togglebutton_clicked_callback
),
223 m_parent
->DoAddChild(this);
230 // void SetValue(bool state)
231 // Set the value of the toggle button.
232 void wxToggleButton::SetValue(bool state
)
234 wxCHECK_RET(m_widget
!= NULL
, wxT("invalid toggle button"));
236 if (state
== GetValue())
241 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(m_widget
), state
);
243 m_blockEvent
= false;
246 // bool GetValue() const
247 // Get the value of the toggle button.
248 bool wxToggleButton::GetValue() const
250 wxCHECK_MSG(m_widget
!= NULL
, false, wxT("invalid toggle button"));
252 return GTK_TOGGLE_BUTTON(m_widget
)->active
;
255 void wxToggleButton::SetLabel(const wxString
& label
)
257 wxCHECK_RET(m_widget
!= NULL
, wxT("invalid toggle button"));
259 wxControl::SetLabel(label
);
261 const wxString labelGTK
= GTKConvertMnemonics(label
);
263 gtk_button_set_label(GTK_BUTTON(m_widget
), wxGTK_CONV(labelGTK
));
265 ApplyWidgetStyle( false );
268 bool wxToggleButton::Enable(bool enable
/*=true*/)
270 if (!wxControl::Enable(enable
))
273 gtk_widget_set_sensitive(GTK_BIN(m_widget
)->child
, enable
);
278 void wxToggleButton::DoApplyWidgetStyle(GtkRcStyle
*style
)
280 gtk_widget_modify_style(m_widget
, style
);
281 gtk_widget_modify_style(GTK_BIN(m_widget
)->child
, style
);
285 wxToggleButton::GTKGetWindow(wxArrayGdkWindows
& WXUNUSED(windows
)) const
287 return GTK_BUTTON(m_widget
)->event_window
;
290 // Get the "best" size for this control.
291 wxSize
wxToggleButton::DoGetBestSize() const
293 wxSize
ret(wxControl::DoGetBestSize());
295 if (!HasFlag(wxBU_EXACTFIT
))
297 if (ret
.x
< 80) ret
.x
= 80;
306 wxToggleButton::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
308 return GetDefaultAttributesFromGTKWidget(gtk_toggle_button_new
);
311 #endif // wxUSE_TOGGLEBTN