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: Rocketeer license
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
;
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
);
46 IMPLEMENT_DYNAMIC_CLASS(wxToggleButton
, wxControl
)
47 DEFINE_EVENT_TYPE(wxEVT_COMMAND_TOGGLEBUTTON_CLICKED
)
49 bool wxToggleButton::Create(wxWindow
*parent
, wxWindowID id
,
50 const wxString
&label
, const wxPoint
&pos
,
51 const wxSize
&size
, long style
,
52 const wxValidator
& validator
,
56 m_acceptsFocus
= TRUE
;
60 if (!PreCreation(parent
, pos
, size
) ||
61 !CreateBase(parent
, id
, pos
, size
, style
, validator
, name
)) {
62 wxFAIL_MSG(wxT("wxToggleButton creation failed"));
66 wxControl::SetLabel(label
);
68 // Create the gtk widget.
69 m_widget
= gtk_toggle_button_new_with_label( wxGTK_CONV( m_label
) );
71 gtk_signal_connect(GTK_OBJECT(m_widget
), "clicked",
72 GTK_SIGNAL_FUNC(gtk_togglebutton_clicked_callback
),
75 m_parent
->DoAddChild(this);
79 SetFont(parent
->GetFont());
81 wxSize
size_best(DoGetBestSize());
82 wxSize
new_size(size
);
84 new_size
.x
= size_best
.x
;
86 new_size
.y
= size_best
.y
;
87 if ((new_size
.x
!= size
.x
) || (new_size
.y
!= size
.y
))
88 SetSize(new_size
.x
, new_size
.y
);
90 SetBackgroundColour(parent
->GetBackgroundColour());
91 SetForegroundColour(parent
->GetForegroundColour());
98 // void SetValue(bool state)
99 // Set the value of the toggle button.
100 void wxToggleButton::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 wxToggleButton::GetValue() const
118 wxCHECK_MSG(m_widget
!= NULL
, FALSE
, wxT("invalid toggle button"));
120 return GTK_TOGGLE_BUTTON(m_widget
)->active
;
123 void wxToggleButton::SetLabel(const wxString
& label
)
125 wxCHECK_RET(m_widget
!= NULL
, wxT("invalid toggle button"));
127 wxControl::SetLabel(label
);
129 gtk_label_set(GTK_LABEL(BUTTON_CHILD(m_widget
)), wxGTK_CONV( GetLabel() ) );
132 bool wxToggleButton::Enable(bool enable
/*=TRUE*/)
134 if (!wxControl::Enable(enable
))
137 gtk_widget_set_sensitive(BUTTON_CHILD(m_widget
), enable
);
142 void wxToggleButton::ApplyWidgetStyle()
145 gtk_widget_set_style(m_widget
, m_widgetStyle
);
146 gtk_widget_set_style(BUTTON_CHILD(m_widget
), m_widgetStyle
);
149 bool wxToggleButton::IsOwnGtkWindow(GdkWindow
*window
)
151 return window
== TOGGLE_BUTTON_EVENT_WIN(m_widget
);
154 void wxToggleButton::OnInternalIdle()
156 wxCursor cursor
= m_cursor
;
158 if (g_globalCursor
.Ok())
159 cursor
= g_globalCursor
;
161 GdkWindow
*win
= TOGGLE_BUTTON_EVENT_WIN(m_widget
);
162 if ( win
&& cursor
.Ok() )
164 /* I now set the cursor the anew in every OnInternalIdle call
165 as setting the cursor in a parent window also effects the
166 windows above so that checking for the current cursor is
169 gdk_window_set_cursor(win
, cursor
.GetCursor());
172 if (wxUpdateUIEvent::CanUpdate(this))
173 UpdateWindowUI(wxUPDATE_UI_FROMIDLE
);
176 // wxSize DoGetBestSize() const
177 // Get the "best" size for this control.
178 wxSize
wxToggleButton::DoGetBestSize() const
180 wxSize
ret(wxControl::DoGetBestSize());
182 if (!HasFlag(wxBU_EXACTFIT
))
184 if (ret
.x
< 80) ret
.x
= 80;
191 #endif // wxUSE_TOGGLEBTN