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 static void gtk_togglebutton_clicked_callback(GtkWidget
*WXUNUSED(widget
), wxToggleButton
*cb
)
30 wxapp_install_idle_handler();
32 if (!cb
->m_hasVMT
|| g_blockEventsOnDrag
)
35 if (cb
->m_blockEvent
) return;
37 // Generate a wx event.
38 wxCommandEvent
event(wxEVT_COMMAND_TOGGLEBUTTON_CLICKED
, cb
->GetId());
39 event
.SetInt(cb
->GetValue());
40 event
.SetEventObject(cb
);
41 cb
->GetEventHandler()->ProcessEvent(event
);
44 IMPLEMENT_DYNAMIC_CLASS(wxToggleButton
, wxControl
)
45 DEFINE_EVENT_TYPE(wxEVT_COMMAND_TOGGLEBUTTON_CLICKED
)
47 // bool Create(wxWindow *parent, wxWindowID id, const wxString &label,
48 // const wxPoint &pos, const wxSize &size, long style,
49 // const wxValidator& validator, const wxString &name)
50 // Create the control.
51 bool wxToggleButton::Create(wxWindow
*parent
, wxWindowID id
,
52 const wxString
&label
, const wxPoint
&pos
,
53 const wxSize
&size
, long style
,
54 const wxValidator
& validator
,
58 m_acceptsFocus
= TRUE
;
62 if (!PreCreation(parent
, pos
, size
) ||
63 !CreateBase(parent
, id
, pos
, size
, style
, validator
, name
)) {
64 wxFAIL_MSG(wxT("wxToggleButton creation failed"));
68 wxControl::SetLabel(label
);
70 // Create the gtk widget.
71 m_widget
= gtk_toggle_button_new_with_label(m_label
.mbc_str());
73 gtk_signal_connect(GTK_OBJECT(m_widget
), "clicked",
74 GTK_SIGNAL_FUNC(gtk_togglebutton_clicked_callback
),
77 m_parent
->DoAddChild(this);
81 SetFont(parent
->GetFont());
83 wxSize
size_best(DoGetBestSize());
84 wxSize
new_size(size
);
86 new_size
.x
= size_best
.x
;
88 new_size
.y
= size_best
.y
;
89 if ((new_size
.x
!= size
.x
) || (new_size
.y
!= size
.y
))
90 SetSize(new_size
.x
, new_size
.y
);
92 SetBackgroundColour(parent
->GetBackgroundColour());
93 SetForegroundColour(parent
->GetForegroundColour());
100 // void SetValue(bool state)
101 // Set the value of the toggle button.
102 void wxToggleButton::SetValue(bool state
)
104 wxCHECK_RET(m_widget
!= NULL
, wxT("invalid toggle button"));
106 if (state
== GetValue())
111 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(m_widget
), state
);
113 m_blockEvent
= FALSE
;
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