1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk1/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"
24 #include "wx/gtk1/private.h"
26 extern void wxapp_install_idle_handler();
28 extern bool g_blockEventsOnDrag
;
29 extern wxCursor g_globalCursor
;
32 static void gtk_togglebutton_clicked_callback(GtkWidget
*WXUNUSED(widget
), wxToggleButton
*cb
)
35 wxapp_install_idle_handler();
37 if (!cb
->m_hasVMT
|| g_blockEventsOnDrag
)
40 if (cb
->m_blockEvent
) return;
42 // Generate a wx event.
43 wxCommandEvent
event(wxEVT_TOGGLEBUTTON
, cb
->GetId());
44 event
.SetInt(cb
->GetValue());
45 event
.SetEventObject(cb
);
46 cb
->HandleWindowEvent(event
);
50 wxDEFINE_EVENT( wxEVT_TOGGLEBUTTON
, wxCommandEvent
);
52 // ------------------------------------------------------------------------
53 // wxToggleBitmapButton
54 // ------------------------------------------------------------------------
56 IMPLEMENT_DYNAMIC_CLASS(wxToggleBitmapButton
, wxControl
)
58 bool wxToggleBitmapButton::Create(wxWindow
*parent
, wxWindowID id
,
59 const wxBitmap
&label
, const wxPoint
&pos
,
60 const wxSize
&size
, long style
,
61 const wxValidator
& validator
,
65 m_acceptsFocus
= true;
69 if (!PreCreation(parent
, pos
, size
) ||
70 !CreateBase(parent
, id
, pos
, size
, style
, validator
, name
))
72 wxFAIL_MSG(wxT("wxToggleBitmapButton creation failed"));
78 // Create the gtk widget.
79 m_widget
= gtk_toggle_button_new();
81 if (style
& wxNO_BORDER
)
82 gtk_button_set_relief( GTK_BUTTON(m_widget
), GTK_RELIEF_NONE
);
89 gtk_signal_connect(GTK_OBJECT(m_widget
), "clicked",
90 GTK_SIGNAL_FUNC(gtk_togglebutton_clicked_callback
),
93 m_parent
->DoAddChild(this);
100 // void SetValue(bool state)
101 // Set the value of the toggle button.
102 void wxToggleBitmapButton::SetValue(bool state
)
104 wxCHECK_RET(m_widget
!= NULL
, wxT("invalid toggle button"));
106 if (state
== GetValue())
111 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(m_widget
), state
);
113 m_blockEvent
= false;
116 // bool GetValue() const
117 // Get the value of the toggle button.
118 bool wxToggleBitmapButton::GetValue() const
120 wxCHECK_MSG(m_widget
!= NULL
, false, wxT("invalid toggle button"));
122 return GTK_TOGGLE_BUTTON(m_widget
)->active
;
125 void wxToggleBitmapButton::SetLabel(const wxBitmap
& label
)
127 wxCHECK_RET(m_widget
!= NULL
, wxT("invalid toggle button"));
130 InvalidateBestSize();
135 void wxToggleBitmapButton::OnSetBitmap()
137 if (!m_bitmap
.IsOk()) return;
139 GdkBitmap
*mask
= NULL
;
140 if (m_bitmap
.GetMask()) mask
= m_bitmap
.GetMask()->GetBitmap();
142 GtkWidget
*child
= BUTTON_CHILD(m_widget
);
146 GtkWidget
*pixmap
= gtk_pixmap_new(m_bitmap
.GetPixmap(), mask
);
147 gtk_widget_show(pixmap
);
148 gtk_container_add(GTK_CONTAINER(m_widget
), pixmap
);
151 { // subsequent bitmaps
152 GtkPixmap
*g_pixmap
= GTK_PIXMAP(child
);
153 gtk_pixmap_set(g_pixmap
, m_bitmap
.GetPixmap(), mask
);
157 bool wxToggleBitmapButton::Enable(bool enable
/*=true*/)
159 if (!wxControl::Enable(enable
))
162 gtk_widget_set_sensitive(BUTTON_CHILD(m_widget
), enable
);
167 void wxToggleBitmapButton::DoApplyWidgetStyle(GtkRcStyle
*style
)
169 gtk_widget_modify_style(m_widget
, style
);
170 gtk_widget_modify_style(BUTTON_CHILD(m_widget
), style
);
173 bool wxToggleBitmapButton::IsOwnGtkWindow(GdkWindow
*window
)
175 return window
== TOGGLE_BUTTON_EVENT_WIN(m_widget
);
178 void wxToggleBitmapButton::OnInternalIdle()
180 wxCursor cursor
= m_cursor
;
182 if (g_globalCursor
.IsOk())
183 cursor
= g_globalCursor
;
185 GdkWindow
*win
= TOGGLE_BUTTON_EVENT_WIN(m_widget
);
186 if ( win
&& cursor
.IsOk() )
188 /* I now set the cursor the anew in every OnInternalIdle call
189 as setting the cursor in a parent window also effects the
190 windows above so that checking for the current cursor is
193 gdk_window_set_cursor(win
, cursor
.GetCursor());
196 if (wxUpdateUIEvent::CanUpdate(this))
197 UpdateWindowUI(wxUPDATE_UI_FROMIDLE
);
201 // Get the "best" size for this control.
202 wxSize
wxToggleBitmapButton::DoGetBestSize() const
208 int border
= HasFlag(wxNO_BORDER
) ? 4 : 10;
209 best
.x
= m_bitmap
.GetWidth()+border
;
210 best
.y
= m_bitmap
.GetHeight()+border
;
219 wxToggleBitmapButton::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
221 return GetDefaultAttributesFromGTKWidget(gtk_toggle_button_new
);
225 // ------------------------------------------------------------------------
227 // ------------------------------------------------------------------------
229 IMPLEMENT_DYNAMIC_CLASS(wxToggleButton
, wxControl
)
231 bool wxToggleButton::Create(wxWindow
*parent
, wxWindowID id
,
232 const wxString
&label
, const wxPoint
&pos
,
233 const wxSize
&size
, long style
,
234 const wxValidator
& validator
,
235 const wxString
&name
)
238 m_acceptsFocus
= true;
240 m_blockEvent
= false;
242 if (!PreCreation(parent
, pos
, size
) ||
243 !CreateBase(parent
, id
, pos
, size
, style
, validator
, name
)) {
244 wxFAIL_MSG(wxT("wxToggleButton creation failed"));
248 wxControl::SetLabel(label
);
250 // Create the gtk widget.
251 m_widget
= gtk_toggle_button_new_with_label( wxGTK_CONV( m_label
) );
253 gtk_signal_connect(GTK_OBJECT(m_widget
), "clicked",
254 GTK_SIGNAL_FUNC(gtk_togglebutton_clicked_callback
),
257 m_parent
->DoAddChild(this);
264 // void SetValue(bool state)
265 // Set the value of the toggle button.
266 void wxToggleButton::SetValue(bool state
)
268 wxCHECK_RET(m_widget
!= NULL
, wxT("invalid toggle button"));
270 if (state
== GetValue())
275 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(m_widget
), state
);
277 m_blockEvent
= false;
280 // bool GetValue() const
281 // Get the value of the toggle button.
282 bool wxToggleButton::GetValue() const
284 wxCHECK_MSG(m_widget
!= NULL
, false, wxT("invalid toggle button"));
286 return GTK_TOGGLE_BUTTON(m_widget
)->active
;
289 void wxToggleButton::SetLabel(const wxString
& label
)
291 wxCHECK_RET(m_widget
!= NULL
, wxT("invalid toggle button"));
293 wxControl::SetLabel(label
);
295 gtk_label_set(GTK_LABEL(BUTTON_CHILD(m_widget
)), wxGTK_CONV( GetLabel() ) );
298 bool wxToggleButton::Enable(bool enable
/*=true*/)
300 if (!wxControl::Enable(enable
))
303 gtk_widget_set_sensitive(BUTTON_CHILD(m_widget
), enable
);
308 void wxToggleButton::DoApplyWidgetStyle(GtkRcStyle
*style
)
310 gtk_widget_modify_style(m_widget
, style
);
311 gtk_widget_modify_style(BUTTON_CHILD(m_widget
), style
);
314 bool wxToggleButton::IsOwnGtkWindow(GdkWindow
*window
)
316 return window
== TOGGLE_BUTTON_EVENT_WIN(m_widget
);
319 void wxToggleButton::OnInternalIdle()
321 wxCursor cursor
= m_cursor
;
323 if (g_globalCursor
.IsOk())
324 cursor
= g_globalCursor
;
326 GdkWindow
*win
= TOGGLE_BUTTON_EVENT_WIN(m_widget
);
327 if ( win
&& cursor
.IsOk() )
329 /* I now set the cursor the anew in every OnInternalIdle call
330 as setting the cursor in a parent window also effects the
331 windows above so that checking for the current cursor is
334 gdk_window_set_cursor(win
, cursor
.GetCursor());
337 if (wxUpdateUIEvent::CanUpdate(this))
338 UpdateWindowUI(wxUPDATE_UI_FROMIDLE
);
342 // Get the "best" size for this control.
343 wxSize
wxToggleButton::DoGetBestSize() const
345 wxSize
ret(wxControl::DoGetBestSize());
347 if (!HasFlag(wxBU_EXACTFIT
))
349 if (ret
.x
< 80) ret
.x
= 80;
358 wxToggleButton::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
360 return GetDefaultAttributesFromGTKWidget(gtk_toggle_button_new
);
363 #endif // wxUSE_TOGGLEBTN