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
;
27 extern wxWindowGTK
*g_delayedFocus
;
30 static void gtk_togglebutton_clicked_callback(GtkWidget
*WXUNUSED(widget
), wxToggleButton
*cb
)
33 wxapp_install_idle_handler();
35 if (!cb
->m_hasVMT
|| g_blockEventsOnDrag
)
38 if (cb
->m_blockEvent
) return;
40 // Generate a wx event.
41 wxCommandEvent
event(wxEVT_COMMAND_TOGGLEBUTTON_CLICKED
, cb
->GetId());
42 event
.SetInt(cb
->GetValue());
43 event
.SetEventObject(cb
);
44 cb
->GetEventHandler()->ProcessEvent(event
);
48 DEFINE_EVENT_TYPE(wxEVT_COMMAND_TOGGLEBUTTON_CLICKED
)
50 // ------------------------------------------------------------------------
51 // wxToggleBitmapButton
52 // ------------------------------------------------------------------------
54 IMPLEMENT_DYNAMIC_CLASS(wxToggleBitmapButton
, wxControl
)
56 bool wxToggleBitmapButton::Create(wxWindow
*parent
, wxWindowID id
,
57 const wxBitmap
&label
, const wxPoint
&pos
,
58 const wxSize
&size
, long style
,
59 const wxValidator
& validator
,
63 m_acceptsFocus
= TRUE
;
67 if (!PreCreation(parent
, pos
, size
) ||
68 !CreateBase(parent
, id
, pos
, size
, style
, validator
, name
))
70 wxFAIL_MSG(wxT("wxToggleBitmapButton creation failed"));
76 // Create the gtk widget.
77 m_widget
= gtk_toggle_button_new();
79 if (style
& wxNO_BORDER
)
80 gtk_button_set_relief( GTK_BUTTON(m_widget
), GTK_RELIEF_NONE
);
87 gtk_signal_connect(GTK_OBJECT(m_widget
), "clicked",
88 GTK_SIGNAL_FUNC(gtk_togglebutton_clicked_callback
),
91 m_parent
->DoAddChild(this);
98 // void SetValue(bool state)
99 // Set the value of the toggle button.
100 void wxToggleBitmapButton::SetValue(bool state
)
102 wxCHECK_RET(m_widget
!= NULL
, wxT("invalid toggle button"));
104 if (state
== GetValue())
109 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(m_widget
), state
);
111 m_blockEvent
= FALSE
;
114 // bool GetValue() const
115 // Get the value of the toggle button.
116 bool wxToggleBitmapButton::GetValue() const
118 wxCHECK_MSG(m_widget
!= NULL
, FALSE
, wxT("invalid toggle button"));
120 return GTK_TOGGLE_BUTTON(m_widget
)->active
;
123 void wxToggleBitmapButton::SetLabel(const wxBitmap
& label
)
125 wxCHECK_RET(m_widget
!= NULL
, wxT("invalid toggle button"));
128 InvalidateBestSize();
133 void wxToggleBitmapButton::OnSetBitmap()
135 if (!m_bitmap
.Ok()) return;
137 GdkBitmap
*mask
= (GdkBitmap
*) NULL
;
138 if (m_bitmap
.GetMask()) mask
= m_bitmap
.GetMask()->GetBitmap();
140 GtkWidget
*child
= BUTTON_CHILD(m_widget
);
144 GtkWidget
*pixmap
= gtk_pixmap_new(m_bitmap
.GetPixmap(), mask
);
145 gtk_widget_show(pixmap
);
146 gtk_container_add(GTK_CONTAINER(m_widget
), pixmap
);
149 { // subsequent bitmaps
150 GtkPixmap
*g_pixmap
= GTK_PIXMAP(child
);
151 gtk_pixmap_set(g_pixmap
, m_bitmap
.GetPixmap(), mask
);
155 bool wxToggleBitmapButton::Enable(bool enable
/*=TRUE*/)
157 if (!wxControl::Enable(enable
))
160 gtk_widget_set_sensitive(BUTTON_CHILD(m_widget
), enable
);
165 void wxToggleBitmapButton::DoApplyWidgetStyle(GtkRcStyle
*style
)
167 gtk_widget_modify_style(m_widget
, style
);
168 gtk_widget_modify_style(BUTTON_CHILD(m_widget
), style
);
171 bool wxToggleBitmapButton::IsOwnGtkWindow(GdkWindow
*window
)
173 return window
== TOGGLE_BUTTON_EVENT_WIN(m_widget
);
176 void wxToggleBitmapButton::OnInternalIdle()
178 wxCursor cursor
= m_cursor
;
180 if (g_globalCursor
.Ok())
181 cursor
= g_globalCursor
;
183 GdkWindow
*win
= TOGGLE_BUTTON_EVENT_WIN(m_widget
);
184 if ( win
&& cursor
.Ok() )
186 /* I now set the cursor the anew in every OnInternalIdle call
187 as setting the cursor in a parent window also effects the
188 windows above so that checking for the current cursor is
191 gdk_window_set_cursor(win
, cursor
.GetCursor());
194 if (wxUpdateUIEvent::CanUpdate(this))
195 UpdateWindowUI(wxUPDATE_UI_FROMIDLE
);
199 // Get the "best" size for this control.
200 wxSize
wxToggleBitmapButton::DoGetBestSize() const
206 int border
= HasFlag(wxNO_BORDER
) ? 4 : 10;
207 best
.x
= m_bitmap
.GetWidth()+border
;
208 best
.y
= m_bitmap
.GetHeight()+border
;
217 wxToggleBitmapButton::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
219 return GetDefaultAttributesFromGTKWidget(gtk_toggle_button_new
);
223 // ------------------------------------------------------------------------
225 // ------------------------------------------------------------------------
227 IMPLEMENT_DYNAMIC_CLASS(wxToggleButton
, wxControl
)
229 bool wxToggleButton::Create(wxWindow
*parent
, wxWindowID id
,
230 const wxString
&label
, const wxPoint
&pos
,
231 const wxSize
&size
, long style
,
232 const wxValidator
& validator
,
233 const wxString
&name
)
236 m_acceptsFocus
= TRUE
;
238 m_blockEvent
= FALSE
;
240 if (!PreCreation(parent
, pos
, size
) ||
241 !CreateBase(parent
, id
, pos
, size
, style
, validator
, name
)) {
242 wxFAIL_MSG(wxT("wxToggleButton creation failed"));
246 wxControl::SetLabel(label
);
248 // Create the gtk widget.
249 m_widget
= gtk_toggle_button_new_with_label( wxGTK_CONV( m_label
) );
251 gtk_signal_connect(GTK_OBJECT(m_widget
), "clicked",
252 GTK_SIGNAL_FUNC(gtk_togglebutton_clicked_callback
),
255 m_parent
->DoAddChild(this);
262 // void SetValue(bool state)
263 // Set the value of the toggle button.
264 void wxToggleButton::SetValue(bool state
)
266 wxCHECK_RET(m_widget
!= NULL
, wxT("invalid toggle button"));
268 if (state
== GetValue())
273 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(m_widget
), state
);
275 m_blockEvent
= FALSE
;
278 // bool GetValue() const
279 // Get the value of the toggle button.
280 bool wxToggleButton::GetValue() const
282 wxCHECK_MSG(m_widget
!= NULL
, FALSE
, wxT("invalid toggle button"));
284 return GTK_TOGGLE_BUTTON(m_widget
)->active
;
287 void wxToggleButton::SetLabel(const wxString
& label
)
289 wxCHECK_RET(m_widget
!= NULL
, wxT("invalid toggle button"));
291 wxControl::SetLabel(label
);
293 gtk_label_set(GTK_LABEL(BUTTON_CHILD(m_widget
)), wxGTK_CONV( GetLabel() ) );
296 bool wxToggleButton::Enable(bool enable
/*=TRUE*/)
298 if (!wxControl::Enable(enable
))
301 gtk_widget_set_sensitive(BUTTON_CHILD(m_widget
), enable
);
306 void wxToggleButton::DoApplyWidgetStyle(GtkRcStyle
*style
)
308 gtk_widget_modify_style(m_widget
, style
);
309 gtk_widget_modify_style(BUTTON_CHILD(m_widget
), style
);
312 bool wxToggleButton::IsOwnGtkWindow(GdkWindow
*window
)
314 return window
== TOGGLE_BUTTON_EVENT_WIN(m_widget
);
317 void wxToggleButton::OnInternalIdle()
319 wxCursor cursor
= m_cursor
;
321 if (g_globalCursor
.Ok())
322 cursor
= g_globalCursor
;
324 GdkWindow
*win
= TOGGLE_BUTTON_EVENT_WIN(m_widget
);
325 if ( win
&& cursor
.Ok() )
327 /* I now set the cursor the anew in every OnInternalIdle call
328 as setting the cursor in a parent window also effects the
329 windows above so that checking for the current cursor is
332 gdk_window_set_cursor(win
, cursor
.GetCursor());
335 if (wxUpdateUIEvent::CanUpdate(this))
336 UpdateWindowUI(wxUPDATE_UI_FROMIDLE
);
340 // Get the "best" size for this control.
341 wxSize
wxToggleButton::DoGetBestSize() const
343 wxSize
ret(wxControl::DoGetBestSize());
345 if (!HasFlag(wxBU_EXACTFIT
))
347 if (ret
.x
< 80) ret
.x
= 80;
356 wxToggleButton::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
358 return GetDefaultAttributesFromGTKWidget(gtk_toggle_button_new
);
361 #endif // wxUSE_TOGGLEBTN