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 #include "wx/gtk/private.h"
23 extern void wxapp_install_idle_handler();
25 extern bool g_blockEventsOnDrag
;
26 extern wxCursor g_globalCursor
;
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
,
62 m_acceptsFocus
= true;
66 if (!PreCreation(parent
, pos
, size
) ||
67 !CreateBase(parent
, id
, pos
, size
, style
, validator
, name
))
69 wxFAIL_MSG(wxT("wxToggleBitmapButton creation failed"));
75 // Create the gtk widget.
76 m_widget
= gtk_toggle_button_new();
78 if (style
& wxNO_BORDER
)
79 gtk_button_set_relief( GTK_BUTTON(m_widget
), GTK_RELIEF_NONE
);
86 g_signal_connect (m_widget
, "clicked",
87 G_CALLBACK (gtk_togglebutton_clicked_callback
),
90 m_parent
->DoAddChild(this);
97 // void SetValue(bool state)
98 // Set the value of the toggle button.
99 void wxToggleBitmapButton::SetValue(bool state
)
101 wxCHECK_RET(m_widget
!= NULL
, wxT("invalid toggle button"));
103 if (state
== GetValue())
108 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(m_widget
), state
);
110 m_blockEvent
= false;
113 // bool GetValue() const
114 // Get the value of the toggle button.
115 bool wxToggleBitmapButton::GetValue() const
117 wxCHECK_MSG(m_widget
!= NULL
, false, wxT("invalid toggle button"));
119 return GTK_TOGGLE_BUTTON(m_widget
)->active
;
122 void wxToggleBitmapButton::SetLabel(const wxBitmap
& label
)
124 wxCHECK_RET(m_widget
!= NULL
, wxT("invalid toggle button"));
127 InvalidateBestSize();
132 void wxToggleBitmapButton::OnSetBitmap()
134 if (!m_bitmap
.Ok()) return;
136 GdkBitmap
*mask
= (GdkBitmap
*) NULL
;
137 if (m_bitmap
.GetMask()) mask
= m_bitmap
.GetMask()->GetBitmap();
139 GtkWidget
*child
= BUTTON_CHILD(m_widget
);
143 GtkWidget
*pixmap
= gtk_pixmap_new(m_bitmap
.GetPixmap(), mask
);
144 gtk_widget_show(pixmap
);
145 gtk_container_add(GTK_CONTAINER(m_widget
), pixmap
);
148 { // subsequent bitmaps
149 GtkPixmap
*g_pixmap
= GTK_PIXMAP(child
);
150 gtk_pixmap_set(g_pixmap
, m_bitmap
.GetPixmap(), mask
);
154 bool wxToggleBitmapButton::Enable(bool enable
/*=true*/)
156 if (!wxControl::Enable(enable
))
159 gtk_widget_set_sensitive(BUTTON_CHILD(m_widget
), enable
);
164 void wxToggleBitmapButton::DoApplyWidgetStyle(GtkRcStyle
*style
)
166 gtk_widget_modify_style(m_widget
, style
);
167 gtk_widget_modify_style(BUTTON_CHILD(m_widget
), style
);
170 bool wxToggleBitmapButton::IsOwnGtkWindow(GdkWindow
*window
)
172 return window
== TOGGLE_BUTTON_EVENT_WIN(m_widget
);
175 void wxToggleBitmapButton::OnInternalIdle()
177 wxCursor cursor
= m_cursor
;
179 if (g_globalCursor
.Ok())
180 cursor
= g_globalCursor
;
182 GdkWindow
*win
= TOGGLE_BUTTON_EVENT_WIN(m_widget
);
183 if ( win
&& cursor
.Ok() )
185 /* I now set the cursor the anew in every OnInternalIdle call
186 as setting the cursor in a parent window also effects the
187 windows above so that checking for the current cursor is
190 gdk_window_set_cursor(win
, cursor
.GetCursor());
193 if (wxUpdateUIEvent::CanUpdate(this))
194 UpdateWindowUI(wxUPDATE_UI_FROMIDLE
);
198 // Get the "best" size for this control.
199 wxSize
wxToggleBitmapButton::DoGetBestSize() const
205 int border
= HasFlag(wxNO_BORDER
) ? 4 : 10;
206 best
.x
= m_bitmap
.GetWidth()+border
;
207 best
.y
= m_bitmap
.GetHeight()+border
;
216 wxToggleBitmapButton::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
218 return GetDefaultAttributesFromGTKWidget(gtk_toggle_button_new
);
222 // ------------------------------------------------------------------------
224 // ------------------------------------------------------------------------
226 IMPLEMENT_DYNAMIC_CLASS(wxToggleButton
, wxControl
)
228 bool wxToggleButton::Create(wxWindow
*parent
, wxWindowID id
,
229 const wxString
&label
, const wxPoint
&pos
,
230 const wxSize
&size
, long style
,
231 const wxValidator
& validator
,
232 const wxString
&name
)
235 m_acceptsFocus
= true;
237 m_blockEvent
= false;
239 if (!PreCreation(parent
, pos
, size
) ||
240 !CreateBase(parent
, id
, pos
, size
, style
, validator
, name
)) {
241 wxFAIL_MSG(wxT("wxToggleButton creation failed"));
245 wxControl::SetLabel(label
);
247 // Create the gtk widget.
248 m_widget
= gtk_toggle_button_new_with_label( wxGTK_CONV( m_label
) );
250 g_signal_connect (m_widget
, "clicked",
251 G_CALLBACK (gtk_togglebutton_clicked_callback
),
254 m_parent
->DoAddChild(this);
261 // void SetValue(bool state)
262 // Set the value of the toggle button.
263 void wxToggleButton::SetValue(bool state
)
265 wxCHECK_RET(m_widget
!= NULL
, wxT("invalid toggle button"));
267 if (state
== GetValue())
272 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(m_widget
), state
);
274 m_blockEvent
= false;
277 // bool GetValue() const
278 // Get the value of the toggle button.
279 bool wxToggleButton::GetValue() const
281 wxCHECK_MSG(m_widget
!= NULL
, false, wxT("invalid toggle button"));
283 return GTK_TOGGLE_BUTTON(m_widget
)->active
;
286 void wxToggleButton::SetLabel(const wxString
& label
)
288 wxCHECK_RET(m_widget
!= NULL
, wxT("invalid toggle button"));
290 wxControl::SetLabel(label
);
292 gtk_label_set(GTK_LABEL(BUTTON_CHILD(m_widget
)), wxGTK_CONV( GetLabel() ) );
295 bool wxToggleButton::Enable(bool enable
/*=true*/)
297 if (!wxControl::Enable(enable
))
300 gtk_widget_set_sensitive(BUTTON_CHILD(m_widget
), enable
);
305 void wxToggleButton::DoApplyWidgetStyle(GtkRcStyle
*style
)
307 gtk_widget_modify_style(m_widget
, style
);
308 gtk_widget_modify_style(BUTTON_CHILD(m_widget
), style
);
311 bool wxToggleButton::IsOwnGtkWindow(GdkWindow
*window
)
313 return window
== TOGGLE_BUTTON_EVENT_WIN(m_widget
);
316 void wxToggleButton::OnInternalIdle()
318 wxCursor cursor
= m_cursor
;
320 if (g_globalCursor
.Ok())
321 cursor
= g_globalCursor
;
323 GdkWindow
*win
= TOGGLE_BUTTON_EVENT_WIN(m_widget
);
324 if ( win
&& cursor
.Ok() )
326 /* I now set the cursor the anew in every OnInternalIdle call
327 as setting the cursor in a parent window also effects the
328 windows above so that checking for the current cursor is
331 gdk_window_set_cursor(win
, cursor
.GetCursor());
334 if (wxUpdateUIEvent::CanUpdate(this))
335 UpdateWindowUI(wxUPDATE_UI_FROMIDLE
);
339 // Get the "best" size for this control.
340 wxSize
wxToggleButton::DoGetBestSize() const
342 wxSize
ret(wxControl::DoGetBestSize());
344 if (!HasFlag(wxBU_EXACTFIT
))
346 if (ret
.x
< 80) ret
.x
= 80;
355 wxToggleButton::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
357 return GetDefaultAttributesFromGTKWidget(gtk_toggle_button_new
);
360 #endif // wxUSE_TOGGLEBTN