1 /////////////////////////////////////////////////////////////////////////////
2 // Name: wx/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"
16 #include "wx/tglbtn.h"
17 #include "wx/button.h"
21 // FIXME: Use GtkImage instead of GtkPixmap.
22 #include <gtk/gtkversion.h>
23 #ifdef GTK_DISABLE_DEPRECATED
24 #undef GTK_DISABLE_DEPRECATED
27 #include "wx/gtk/private.h"
29 extern void wxapp_install_idle_handler();
31 extern bool g_blockEventsOnDrag
;
32 extern wxCursor g_globalCursor
;
35 static void gtk_togglebutton_clicked_callback(GtkWidget
*WXUNUSED(widget
), wxToggleButton
*cb
)
38 wxapp_install_idle_handler();
40 if (!cb
->m_hasVMT
|| g_blockEventsOnDrag
)
43 if (cb
->m_blockEvent
) return;
45 // Generate a wx event.
46 wxCommandEvent
event(wxEVT_COMMAND_TOGGLEBUTTON_CLICKED
, cb
->GetId());
47 event
.SetInt(cb
->GetValue());
48 event
.SetEventObject(cb
);
49 cb
->GetEventHandler()->ProcessEvent(event
);
53 DEFINE_EVENT_TYPE(wxEVT_COMMAND_TOGGLEBUTTON_CLICKED
)
55 // ------------------------------------------------------------------------
56 // wxToggleBitmapButton
57 // ------------------------------------------------------------------------
59 IMPLEMENT_DYNAMIC_CLASS(wxToggleBitmapButton
, wxControl
)
61 bool wxToggleBitmapButton::Create(wxWindow
*parent
, wxWindowID id
,
62 const wxBitmap
&label
, const wxPoint
&pos
,
63 const wxSize
&size
, long style
,
64 const wxValidator
& validator
,
68 m_acceptsFocus
= true;
72 if (!PreCreation(parent
, pos
, size
) ||
73 !CreateBase(parent
, id
, pos
, size
, style
, validator
, name
))
75 wxFAIL_MSG(wxT("wxToggleBitmapButton creation failed"));
81 // Create the gtk widget.
82 m_widget
= gtk_toggle_button_new();
84 if (style
& wxNO_BORDER
)
85 gtk_button_set_relief( GTK_BUTTON(m_widget
), GTK_RELIEF_NONE
);
92 g_signal_connect (m_widget
, "clicked",
93 G_CALLBACK (gtk_togglebutton_clicked_callback
),
96 m_parent
->DoAddChild(this);
103 // void SetValue(bool state)
104 // Set the value of the toggle button.
105 void wxToggleBitmapButton::SetValue(bool state
)
107 wxCHECK_RET(m_widget
!= NULL
, wxT("invalid toggle button"));
109 if (state
== GetValue())
114 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(m_widget
), state
);
116 m_blockEvent
= false;
119 // bool GetValue() const
120 // Get the value of the toggle button.
121 bool wxToggleBitmapButton::GetValue() const
123 wxCHECK_MSG(m_widget
!= NULL
, false, wxT("invalid toggle button"));
125 return GTK_TOGGLE_BUTTON(m_widget
)->active
;
128 void wxToggleBitmapButton::SetLabel(const wxBitmap
& label
)
130 wxCHECK_RET(m_widget
!= NULL
, wxT("invalid toggle button"));
133 InvalidateBestSize();
138 void wxToggleBitmapButton::OnSetBitmap()
140 if (!m_bitmap
.Ok()) return;
142 GdkBitmap
*mask
= (GdkBitmap
*) NULL
;
143 if (m_bitmap
.GetMask()) mask
= m_bitmap
.GetMask()->GetBitmap();
145 GtkWidget
*child
= GTK_BIN(m_widget
)->child
;
149 GtkWidget
*pixmap
= gtk_pixmap_new(m_bitmap
.GetPixmap(), mask
);
150 gtk_widget_show(pixmap
);
151 gtk_container_add(GTK_CONTAINER(m_widget
), pixmap
);
154 { // subsequent bitmaps
155 GtkPixmap
*g_pixmap
= GTK_PIXMAP(child
);
156 gtk_pixmap_set(g_pixmap
, m_bitmap
.GetPixmap(), mask
);
160 bool wxToggleBitmapButton::Enable(bool enable
/*=true*/)
162 if (!wxControl::Enable(enable
))
165 gtk_widget_set_sensitive(GTK_BIN(m_widget
)->child
, enable
);
170 void wxToggleBitmapButton::DoApplyWidgetStyle(GtkRcStyle
*style
)
172 gtk_widget_modify_style(m_widget
, style
);
173 gtk_widget_modify_style(GTK_BIN(m_widget
)->child
, style
);
176 bool wxToggleBitmapButton::IsOwnGtkWindow(GdkWindow
*window
)
178 return window
== GTK_BUTTON(m_widget
)->event_window
;
181 void wxToggleBitmapButton::OnInternalIdle()
183 wxCursor cursor
= m_cursor
;
185 if (g_globalCursor
.Ok())
186 cursor
= g_globalCursor
;
188 GdkWindow
*win
= GTK_BUTTON(m_widget
)->event_window
;
189 if ( win
&& cursor
.Ok() )
191 /* I now set the cursor the anew in every OnInternalIdle call
192 as setting the cursor in a parent window also effects the
193 windows above so that checking for the current cursor is
196 gdk_window_set_cursor(win
, cursor
.GetCursor());
199 if (wxUpdateUIEvent::CanUpdate(this))
200 UpdateWindowUI(wxUPDATE_UI_FROMIDLE
);
204 // Get the "best" size for this control.
205 wxSize
wxToggleBitmapButton::DoGetBestSize() const
211 int border
= HasFlag(wxNO_BORDER
) ? 4 : 10;
212 best
.x
= m_bitmap
.GetWidth()+border
;
213 best
.y
= m_bitmap
.GetHeight()+border
;
222 wxToggleBitmapButton::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
224 return GetDefaultAttributesFromGTKWidget(gtk_toggle_button_new
);
228 // ------------------------------------------------------------------------
230 // ------------------------------------------------------------------------
232 IMPLEMENT_DYNAMIC_CLASS(wxToggleButton
, wxControl
)
234 bool wxToggleButton::Create(wxWindow
*parent
, wxWindowID id
,
235 const wxString
&label
, const wxPoint
&pos
,
236 const wxSize
&size
, long style
,
237 const wxValidator
& validator
,
238 const wxString
&name
)
241 m_acceptsFocus
= true;
243 m_blockEvent
= false;
245 if (!PreCreation(parent
, pos
, size
) ||
246 !CreateBase(parent
, id
, pos
, size
, style
, validator
, name
)) {
247 wxFAIL_MSG(wxT("wxToggleButton creation failed"));
251 wxControl::SetLabel(label
);
253 // Create the gtk widget.
254 m_widget
= gtk_toggle_button_new_with_label( wxGTK_CONV( m_label
) );
256 g_signal_connect (m_widget
, "clicked",
257 G_CALLBACK (gtk_togglebutton_clicked_callback
),
260 m_parent
->DoAddChild(this);
267 // void SetValue(bool state)
268 // Set the value of the toggle button.
269 void wxToggleButton::SetValue(bool state
)
271 wxCHECK_RET(m_widget
!= NULL
, wxT("invalid toggle button"));
273 if (state
== GetValue())
278 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(m_widget
), state
);
280 m_blockEvent
= false;
283 // bool GetValue() const
284 // Get the value of the toggle button.
285 bool wxToggleButton::GetValue() const
287 wxCHECK_MSG(m_widget
!= NULL
, false, wxT("invalid toggle button"));
289 return GTK_TOGGLE_BUTTON(m_widget
)->active
;
292 void wxToggleButton::SetLabel(const wxString
& label
)
294 wxCHECK_RET(m_widget
!= NULL
, wxT("invalid toggle button"));
296 wxControl::SetLabel(label
);
298 gtk_label_set_text(GTK_LABEL(GTK_BIN(m_widget
)->child
), wxGTK_CONV(GetLabel()));
301 bool wxToggleButton::Enable(bool enable
/*=true*/)
303 if (!wxControl::Enable(enable
))
306 gtk_widget_set_sensitive(GTK_BIN(m_widget
)->child
, enable
);
311 void wxToggleButton::DoApplyWidgetStyle(GtkRcStyle
*style
)
313 gtk_widget_modify_style(m_widget
, style
);
314 gtk_widget_modify_style(GTK_BIN(m_widget
)->child
, style
);
317 bool wxToggleButton::IsOwnGtkWindow(GdkWindow
*window
)
319 return window
== GTK_BUTTON(m_widget
)->event_window
;
322 void wxToggleButton::OnInternalIdle()
324 wxCursor cursor
= m_cursor
;
326 if (g_globalCursor
.Ok())
327 cursor
= g_globalCursor
;
329 GdkWindow
*win
= GTK_BUTTON(m_widget
)->event_window
;
330 if ( win
&& cursor
.Ok() )
332 /* I now set the cursor the anew in every OnInternalIdle call
333 as setting the cursor in a parent window also effects the
334 windows above so that checking for the current cursor is
337 gdk_window_set_cursor(win
, cursor
.GetCursor());
340 if (wxUpdateUIEvent::CanUpdate(this))
341 UpdateWindowUI(wxUPDATE_UI_FROMIDLE
);
345 // Get the "best" size for this control.
346 wxSize
wxToggleButton::DoGetBestSize() const
348 wxSize
ret(wxControl::DoGetBestSize());
350 if (!HasFlag(wxBU_EXACTFIT
))
352 if (ret
.x
< 80) ret
.x
= 80;
361 wxToggleButton::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
363 return GetDefaultAttributesFromGTKWidget(gtk_toggle_button_new
);
366 #endif // wxUSE_TOGGLEBTN