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
8 // Copyright: (c) 2000 Johnny C. Norris II
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // For compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
17 #include "wx/tglbtn.h"
20 #include "wx/button.h"
23 #include "wx/gtk1/private.h"
25 extern void wxapp_install_idle_handler();
27 extern bool g_blockEventsOnDrag
;
28 extern wxCursor g_globalCursor
;
31 static void gtk_togglebutton_clicked_callback(GtkWidget
*WXUNUSED(widget
), wxToggleButton
*cb
)
34 wxapp_install_idle_handler();
36 if (!cb
->m_hasVMT
|| g_blockEventsOnDrag
)
39 if (cb
->m_blockEvent
) return;
41 // Generate a wx event.
42 wxCommandEvent
event(wxEVT_TOGGLEBUTTON
, cb
->GetId());
43 event
.SetInt(cb
->GetValue());
44 event
.SetEventObject(cb
);
45 cb
->HandleWindowEvent(event
);
49 wxDEFINE_EVENT( wxEVT_TOGGLEBUTTON
, wxCommandEvent
);
51 // ------------------------------------------------------------------------
52 // wxToggleBitmapButton
53 // ------------------------------------------------------------------------
55 IMPLEMENT_DYNAMIC_CLASS(wxToggleBitmapButton
, wxControl
)
57 bool wxToggleBitmapButton::Create(wxWindow
*parent
, wxWindowID id
,
58 const wxBitmap
&label
, const wxPoint
&pos
,
59 const wxSize
&size
, long style
,
60 const wxValidator
& validator
,
64 m_acceptsFocus
= true;
68 if (!PreCreation(parent
, pos
, size
) ||
69 !CreateBase(parent
, id
, pos
, size
, style
, validator
, name
))
71 wxFAIL_MSG(wxT("wxToggleBitmapButton creation failed"));
77 // Create the gtk widget.
78 m_widget
= gtk_toggle_button_new();
80 if (style
& wxNO_BORDER
)
81 gtk_button_set_relief( GTK_BUTTON(m_widget
), GTK_RELIEF_NONE
);
88 gtk_signal_connect(GTK_OBJECT(m_widget
), "clicked",
89 GTK_SIGNAL_FUNC(gtk_togglebutton_clicked_callback
),
92 m_parent
->DoAddChild(this);
99 // void SetValue(bool state)
100 // Set the value of the toggle button.
101 void wxToggleBitmapButton::SetValue(bool state
)
103 wxCHECK_RET(m_widget
!= NULL
, wxT("invalid toggle button"));
105 if (state
== GetValue())
110 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(m_widget
), state
);
112 m_blockEvent
= false;
115 // bool GetValue() const
116 // Get the value of the toggle button.
117 bool wxToggleBitmapButton::GetValue() const
119 wxCHECK_MSG(m_widget
!= NULL
, false, wxT("invalid toggle button"));
121 return GTK_TOGGLE_BUTTON(m_widget
)->active
;
124 void wxToggleBitmapButton::SetLabel(const wxBitmap
& label
)
126 wxCHECK_RET(m_widget
!= NULL
, wxT("invalid toggle button"));
129 InvalidateBestSize();
134 void wxToggleBitmapButton::OnSetBitmap()
136 if (!m_bitmap
.IsOk()) return;
138 GdkBitmap
*mask
= NULL
;
139 if (m_bitmap
.GetMask()) mask
= m_bitmap
.GetMask()->GetBitmap();
141 GtkWidget
*child
= BUTTON_CHILD(m_widget
);
145 GtkWidget
*pixmap
= gtk_pixmap_new(m_bitmap
.GetPixmap(), mask
);
146 gtk_widget_show(pixmap
);
147 gtk_container_add(GTK_CONTAINER(m_widget
), pixmap
);
150 { // subsequent bitmaps
151 GtkPixmap
*g_pixmap
= GTK_PIXMAP(child
);
152 gtk_pixmap_set(g_pixmap
, m_bitmap
.GetPixmap(), mask
);
156 bool wxToggleBitmapButton::Enable(bool enable
/*=true*/)
158 if (!wxControl::Enable(enable
))
161 gtk_widget_set_sensitive(BUTTON_CHILD(m_widget
), enable
);
166 void wxToggleBitmapButton::DoApplyWidgetStyle(GtkRcStyle
*style
)
168 gtk_widget_modify_style(m_widget
, style
);
169 gtk_widget_modify_style(BUTTON_CHILD(m_widget
), style
);
172 bool wxToggleBitmapButton::IsOwnGtkWindow(GdkWindow
*window
)
174 return window
== TOGGLE_BUTTON_EVENT_WIN(m_widget
);
177 void wxToggleBitmapButton::OnInternalIdle()
179 wxCursor cursor
= m_cursor
;
181 if (g_globalCursor
.IsOk())
182 cursor
= g_globalCursor
;
184 GdkWindow
*win
= TOGGLE_BUTTON_EVENT_WIN(m_widget
);
185 if ( win
&& cursor
.IsOk() )
187 /* I now set the cursor the anew in every OnInternalIdle call
188 as setting the cursor in a parent window also effects the
189 windows above so that checking for the current cursor is
192 gdk_window_set_cursor(win
, cursor
.GetCursor());
195 if (wxUpdateUIEvent::CanUpdate(this))
196 UpdateWindowUI(wxUPDATE_UI_FROMIDLE
);
200 // Get the "best" size for this control.
201 wxSize
wxToggleBitmapButton::DoGetBestSize() const
207 int border
= HasFlag(wxNO_BORDER
) ? 4 : 10;
208 best
.x
= m_bitmap
.GetWidth()+border
;
209 best
.y
= m_bitmap
.GetHeight()+border
;
218 wxToggleBitmapButton::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
220 return GetDefaultAttributesFromGTKWidget(gtk_toggle_button_new
);
224 // ------------------------------------------------------------------------
226 // ------------------------------------------------------------------------
228 IMPLEMENT_DYNAMIC_CLASS(wxToggleButton
, wxControl
)
230 bool wxToggleButton::Create(wxWindow
*parent
, wxWindowID id
,
231 const wxString
&label
, const wxPoint
&pos
,
232 const wxSize
&size
, long style
,
233 const wxValidator
& validator
,
234 const wxString
&name
)
237 m_acceptsFocus
= true;
239 m_blockEvent
= false;
241 if (!PreCreation(parent
, pos
, size
) ||
242 !CreateBase(parent
, id
, pos
, size
, style
, validator
, name
)) {
243 wxFAIL_MSG(wxT("wxToggleButton creation failed"));
247 wxControl::SetLabel(label
);
249 // Create the gtk widget.
250 m_widget
= gtk_toggle_button_new_with_label( wxGTK_CONV( m_label
) );
252 gtk_signal_connect(GTK_OBJECT(m_widget
), "clicked",
253 GTK_SIGNAL_FUNC(gtk_togglebutton_clicked_callback
),
256 m_parent
->DoAddChild(this);
263 // void SetValue(bool state)
264 // Set the value of the toggle button.
265 void wxToggleButton::SetValue(bool state
)
267 wxCHECK_RET(m_widget
!= NULL
, wxT("invalid toggle button"));
269 if (state
== GetValue())
274 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(m_widget
), state
);
276 m_blockEvent
= false;
279 // bool GetValue() const
280 // Get the value of the toggle button.
281 bool wxToggleButton::GetValue() const
283 wxCHECK_MSG(m_widget
!= NULL
, false, wxT("invalid toggle button"));
285 return GTK_TOGGLE_BUTTON(m_widget
)->active
;
288 void wxToggleButton::SetLabel(const wxString
& label
)
290 wxCHECK_RET(m_widget
!= NULL
, wxT("invalid toggle button"));
292 wxControl::SetLabel(label
);
294 gtk_label_set(GTK_LABEL(BUTTON_CHILD(m_widget
)), wxGTK_CONV( GetLabel() ) );
297 bool wxToggleButton::Enable(bool enable
/*=true*/)
299 if (!wxControl::Enable(enable
))
302 gtk_widget_set_sensitive(BUTTON_CHILD(m_widget
), enable
);
307 void wxToggleButton::DoApplyWidgetStyle(GtkRcStyle
*style
)
309 gtk_widget_modify_style(m_widget
, style
);
310 gtk_widget_modify_style(BUTTON_CHILD(m_widget
), style
);
313 bool wxToggleButton::IsOwnGtkWindow(GdkWindow
*window
)
315 return window
== TOGGLE_BUTTON_EVENT_WIN(m_widget
);
318 void wxToggleButton::OnInternalIdle()
320 wxCursor cursor
= m_cursor
;
322 if (g_globalCursor
.IsOk())
323 cursor
= g_globalCursor
;
325 GdkWindow
*win
= TOGGLE_BUTTON_EVENT_WIN(m_widget
);
326 if ( win
&& cursor
.IsOk() )
328 /* I now set the cursor the anew in every OnInternalIdle call
329 as setting the cursor in a parent window also effects the
330 windows above so that checking for the current cursor is
333 gdk_window_set_cursor(win
, cursor
.GetCursor());
336 if (wxUpdateUIEvent::CanUpdate(this))
337 UpdateWindowUI(wxUPDATE_UI_FROMIDLE
);
341 // Get the "best" size for this control.
342 wxSize
wxToggleButton::DoGetBestSize() const
344 wxSize
ret(wxControl::DoGetBestSize());
346 if (!HasFlag(wxBU_EXACTFIT
))
348 if (ret
.x
< 80) ret
.x
= 80;
357 wxToggleButton::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
359 return GetDefaultAttributesFromGTKWidget(gtk_toggle_button_new
);
362 #endif // wxUSE_TOGGLEBTN