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 #include "wx/tglbtn.h"
20 extern void wxapp_install_idle_handler();
22 extern bool g_blockEventsOnDrag
;
23 extern wxCursor g_globalCursor
;
25 // void gtk_togglebutton_clicked_callback(GtkWidget *widget, wxToggleButton *cb)
26 // Callback function given to gtk.
27 void wxToggleButton::gtk_togglebutton_clicked_callback(GtkWidget
*WXUNUSED(widget
), wxToggleButton
*cb
)
30 wxapp_install_idle_handler();
32 if (!cb
->m_hasVMT
|| g_blockEventsOnDrag
)
35 // Generate a wx event.
36 wxCommandEvent
event(wxEVT_COMMAND_TOGGLEBUTTON_CLICKED
, cb
->GetId());
37 event
.SetInt(cb
->GetValue());
38 event
.SetEventObject(cb
);
39 cb
->GetEventHandler()->ProcessEvent(event
);
42 IMPLEMENT_DYNAMIC_CLASS(wxToggleButton
, wxControl
)
43 DEFINE_EVENT_TYPE(wxEVT_COMMAND_TOGGLEBUTTON_CLICKED
)
45 // bool Create(wxWindow *parent, wxWindowID id, const wxString &label,
46 // const wxPoint &pos, const wxSize &size, long style,
47 // const wxValidator& validator, const wxString &name)
48 // Create the control.
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
;
58 if (!PreCreation(parent
, pos
, size
) ||
59 !CreateBase(parent
, id
, pos
, size
, style
, validator
, name
)) {
60 wxFAIL_MSG(wxT("wxToggleButton creation failed"));
64 wxControl::SetLabel(label
);
66 // Create the gtk widget.
67 m_widget
= gtk_toggle_button_new_with_label(m_label
.mbc_str());
69 gtk_signal_connect(GTK_OBJECT(m_widget
), "clicked",
70 GTK_SIGNAL_FUNC(gtk_togglebutton_clicked_callback
),
73 m_parent
->DoAddChild(this);
77 SetFont(parent
->GetFont());
79 wxSize
size_best(DoGetBestSize());
80 wxSize
new_size(size
);
82 new_size
.x
= size_best
.x
;
84 new_size
.y
= size_best
.y
;
85 if ((new_size
.x
!= size
.x
) || (new_size
.y
!= size
.y
))
86 SetSize(new_size
.x
, new_size
.y
);
88 SetBackgroundColour(parent
->GetBackgroundColour());
89 SetForegroundColour(parent
->GetForegroundColour());
96 // void SetValue(bool state)
97 // Set the value of the toggle button.
98 void wxToggleButton::SetValue(bool state
)
100 wxCHECK_RET(m_widget
!= NULL
, wxT("invalid toggle button"));
102 if (state
== GetValue())
105 gtk_signal_disconnect_by_func(GTK_OBJECT(m_widget
),
106 GTK_SIGNAL_FUNC(gtk_togglebutton_clicked_callback
),
109 gtk_toggle_button_set_state(GTK_TOGGLE_BUTTON(m_widget
), state
);
111 gtk_signal_connect(GTK_OBJECT(m_widget
), "clicked",
112 GTK_SIGNAL_FUNC(gtk_togglebutton_clicked_callback
),
116 // bool GetValue() const
117 // Get the value of the toggle button.
118 bool wxToggleButton::GetValue() const
120 wxCHECK_MSG(m_widget
!= NULL
, FALSE
, wxT("invalid toggle button"));
122 return GTK_TOGGLE_BUTTON(m_widget
)->active
;
125 // void SetLabel(const wxString& label)
126 // Set the button's label.
127 void wxToggleButton::SetLabel(const wxString
& label
)
129 wxCHECK_RET(m_widget
!= NULL
, wxT("invalid toggle button"));
131 wxControl::SetLabel(label
);
133 gtk_label_set(GTK_LABEL(GTK_BUTTON(m_widget
)->child
),
134 GetLabel().mbc_str());
137 // bool Enable(bool enable)
138 // Enable (or disable) the control.
139 bool wxToggleButton::Enable(bool enable
/*=TRUE*/)
141 if (!wxControl::Enable(enable
))
144 gtk_widget_set_sensitive(GTK_BUTTON(m_widget
)->child
, enable
);
149 // void ApplyWidgetStyle()
150 // I don't really know what this does.
151 void wxToggleButton::ApplyWidgetStyle()
154 gtk_widget_set_style(m_widget
, m_widgetStyle
);
155 gtk_widget_set_style(GTK_BUTTON(m_widget
)->child
, m_widgetStyle
);
158 // bool IsOwnGtkWindow(GdkWindow *window)
159 // I'm not really sure what this is for, either.
160 bool wxToggleButton::IsOwnGtkWindow(GdkWindow
*window
)
162 return (window
== GTK_TOGGLE_BUTTON(m_widget
)->event_window
);
165 // void OnInternalIdle()
166 // Apparently gtk cursors are difficult to deal with.
167 void wxToggleButton::OnInternalIdle()
169 wxCursor cursor
= m_cursor
;
170 if (g_globalCursor
.Ok())
171 cursor
= g_globalCursor
;
173 if (GTK_TOGGLE_BUTTON(m_widget
)->event_window
&& cursor
.Ok()) {
174 /* I now set the cursor the anew in every OnInternalIdle call
175 as setting the cursor in a parent window also effects the
176 windows above so that checking for the current cursor is
179 gdk_window_set_cursor(GTK_TOGGLE_BUTTON(m_widget
)->event_window
,
186 // wxSize DoGetBestSize() const
187 // Get the "best" size for this control.
188 wxSize
wxToggleButton::DoGetBestSize() const
190 wxSize
ret(wxControl::DoGetBestSize());
197 #endif // wxUSE_TOGGLEBTN