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"
14 #include "wx/button.h"
18 #include "wx/gtk/private.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 wxToggleButton::Create(wxWindow
*parent
, wxWindowID id
,
48 const wxString
&label
, const wxPoint
&pos
,
49 const wxSize
&size
, long style
,
50 const wxValidator
& validator
,
54 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())
107 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(m_widget
), state
);
109 m_blockEvent
= FALSE
;
112 // bool GetValue() const
113 // Get the value of the toggle button.
114 bool wxToggleButton::GetValue() const
116 wxCHECK_MSG(m_widget
!= NULL
, FALSE
, wxT("invalid toggle button"));
118 return GTK_TOGGLE_BUTTON(m_widget
)->active
;
121 void wxToggleButton::SetLabel(const wxString
& label
)
123 wxCHECK_RET(m_widget
!= NULL
, wxT("invalid toggle button"));
125 wxControl::SetLabel(label
);
127 gtk_label_set(GTK_LABEL(BUTTON_CHILD(m_widget
)), GetLabel().mbc_str());
130 bool wxToggleButton::Enable(bool enable
/*=TRUE*/)
132 if (!wxControl::Enable(enable
))
135 gtk_widget_set_sensitive(BUTTON_CHILD(m_widget
), enable
);
140 void wxToggleButton::ApplyWidgetStyle()
143 gtk_widget_set_style(m_widget
, m_widgetStyle
);
144 gtk_widget_set_style(BUTTON_CHILD(m_widget
), m_widgetStyle
);
147 bool wxToggleButton::IsOwnGtkWindow(GdkWindow
*window
)
149 return window
== TOGGLE_BUTTON_EVENT_WIN(m_widget
);
152 void wxToggleButton::OnInternalIdle()
154 wxCursor cursor
= m_cursor
;
156 if (g_globalCursor
.Ok())
157 cursor
= g_globalCursor
;
159 GdkWindow
*win
= TOGGLE_BUTTON_EVENT_WIN(m_widget
);
160 if ( win
&& cursor
.Ok() )
162 /* I now set the cursor the anew in every OnInternalIdle call
163 as setting the cursor in a parent window also effects the
164 windows above so that checking for the current cursor is
167 gdk_window_set_cursor(win
, cursor
.GetCursor());
173 // wxSize DoGetBestSize() const
174 // Get the "best" size for this control.
175 wxSize
wxToggleButton::DoGetBestSize() const
177 wxSize
ret(wxControl::DoGetBestSize());
179 if (!HasFlag(wxBU_EXACTFIT
))
181 if (ret
.x
< 80) ret
.x
= 80;
188 #endif // wxUSE_TOGGLEBTN