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 DEFINE_EVENT_TYPE(wxEVT_COMMAND_TOGGLEBUTTON_CLICKED
)
48 // ------------------------------------------------------------------------
49 // wxToggleBitmapButton
50 // ------------------------------------------------------------------------
52 IMPLEMENT_DYNAMIC_CLASS(wxToggleBitmapButton
, wxControl
)
54 bool wxToggleBitmapButton::Create(wxWindow
*parent
, wxWindowID id
,
55 const wxBitmap
&label
, const wxPoint
&pos
,
56 const wxSize
&size
, long style
,
57 const wxValidator
& validator
,
61 m_acceptsFocus
= TRUE
;
65 if (!PreCreation(parent
, pos
, size
) ||
66 !CreateBase(parent
, id
, pos
, size
, style
, validator
, name
))
68 wxFAIL_MSG(wxT("wxToggleBitmapButton creation failed"));
74 // Create the gtk widget.
75 m_widget
= gtk_toggle_button_new();
77 if (style
& wxNO_BORDER
)
78 gtk_button_set_relief( GTK_BUTTON(m_widget
), GTK_RELIEF_NONE
);
82 wxSize newSize
= size
;
83 int border
= (style
& wxNO_BORDER
) ? 4 : 10;
85 newSize
.x
= m_bitmap
.GetWidth()+border
;
87 newSize
.y
= m_bitmap
.GetHeight()+border
;
88 SetSize( newSize
.x
, newSize
.y
);
92 gtk_signal_connect(GTK_OBJECT(m_widget
), "clicked",
93 GTK_SIGNAL_FUNC(gtk_togglebutton_clicked_callback
),
96 m_parent
->DoAddChild(this);
106 // void SetValue(bool state)
107 // Set the value of the toggle button.
108 void wxToggleBitmapButton::SetValue(bool state
)
110 wxCHECK_RET(m_widget
!= NULL
, wxT("invalid toggle button"));
112 if (state
== GetValue())
117 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(m_widget
), state
);
119 m_blockEvent
= FALSE
;
122 // bool GetValue() const
123 // Get the value of the toggle button.
124 bool wxToggleBitmapButton::GetValue() const
126 wxCHECK_MSG(m_widget
!= NULL
, FALSE
, wxT("invalid toggle button"));
128 return GTK_TOGGLE_BUTTON(m_widget
)->active
;
131 void wxToggleBitmapButton::SetLabel(const wxBitmap
& label
)
133 wxCHECK_RET(m_widget
!= NULL
, wxT("invalid toggle button"));
140 void wxToggleBitmapButton::OnSetBitmap()
142 if (!m_bitmap
.Ok()) return;
144 GdkBitmap
*mask
= (GdkBitmap
*) NULL
;
145 if (m_bitmap
.GetMask()) mask
= m_bitmap
.GetMask()->GetBitmap();
147 GtkWidget
*child
= BUTTON_CHILD(m_widget
);
151 GtkWidget
*pixmap
= gtk_pixmap_new(m_bitmap
.GetPixmap(), mask
);
152 gtk_widget_show(pixmap
);
153 gtk_container_add(GTK_CONTAINER(m_widget
), pixmap
);
156 { // subsequent bitmaps
157 GtkPixmap
*g_pixmap
= GTK_PIXMAP(child
);
158 gtk_pixmap_set(g_pixmap
, m_bitmap
.GetPixmap(), mask
);
162 bool wxToggleBitmapButton::Enable(bool enable
/*=TRUE*/)
164 if (!wxControl::Enable(enable
))
167 gtk_widget_set_sensitive(BUTTON_CHILD(m_widget
), enable
);
172 void wxToggleBitmapButton::ApplyWidgetStyle()
175 gtk_widget_set_style(m_widget
, m_widgetStyle
);
176 gtk_widget_set_style(BUTTON_CHILD(m_widget
), m_widgetStyle
);
179 bool wxToggleBitmapButton::IsOwnGtkWindow(GdkWindow
*window
)
181 return window
== TOGGLE_BUTTON_EVENT_WIN(m_widget
);
184 void wxToggleBitmapButton::OnInternalIdle()
186 wxCursor cursor
= m_cursor
;
188 if (g_globalCursor
.Ok())
189 cursor
= g_globalCursor
;
191 GdkWindow
*win
= TOGGLE_BUTTON_EVENT_WIN(m_widget
);
192 if ( win
&& cursor
.Ok() )
194 /* I now set the cursor the anew in every OnInternalIdle call
195 as setting the cursor in a parent window also effects the
196 windows above so that checking for the current cursor is
199 gdk_window_set_cursor(win
, cursor
.GetCursor());
202 if (wxUpdateUIEvent::CanUpdate(this))
203 UpdateWindowUI(wxUPDATE_UI_FROMIDLE
);
206 // wxSize DoGetBestSize() const
207 // Get the "best" size for this control.
208 wxSize
wxToggleBitmapButton::DoGetBestSize() const
210 wxSize
ret(wxControl::DoGetBestSize());
212 if (!HasFlag(wxBU_EXACTFIT
))
214 if (ret
.x
< 80) ret
.x
= 80;
220 // ------------------------------------------------------------------------
222 // ------------------------------------------------------------------------
224 IMPLEMENT_DYNAMIC_CLASS(wxToggleButton
, wxControl
)
226 bool wxToggleButton::Create(wxWindow
*parent
, wxWindowID id
,
227 const wxString
&label
, const wxPoint
&pos
,
228 const wxSize
&size
, long style
,
229 const wxValidator
& validator
,
230 const wxString
&name
)
233 m_acceptsFocus
= TRUE
;
235 m_blockEvent
= FALSE
;
237 if (!PreCreation(parent
, pos
, size
) ||
238 !CreateBase(parent
, id
, pos
, size
, style
, validator
, name
)) {
239 wxFAIL_MSG(wxT("wxToggleButton creation failed"));
243 wxControl::SetLabel(label
);
245 // Create the gtk widget.
246 m_widget
= gtk_toggle_button_new_with_label( wxGTK_CONV( m_label
) );
248 gtk_signal_connect(GTK_OBJECT(m_widget
), "clicked",
249 GTK_SIGNAL_FUNC(gtk_togglebutton_clicked_callback
),
252 m_parent
->DoAddChild(this);
257 wxSize
size_best(DoGetBestSize());
258 wxSize
new_size(size
);
259 if (new_size
.x
== -1)
260 new_size
.x
= size_best
.x
;
261 if (new_size
.y
== -1)
262 new_size
.y
= size_best
.y
;
263 if ((new_size
.x
!= size
.x
) || (new_size
.y
!= size
.y
))
264 SetSize(new_size
.x
, new_size
.y
);
271 // void SetValue(bool state)
272 // Set the value of the toggle button.
273 void wxToggleButton::SetValue(bool state
)
275 wxCHECK_RET(m_widget
!= NULL
, wxT("invalid toggle button"));
277 if (state
== GetValue())
282 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(m_widget
), state
);
284 m_blockEvent
= FALSE
;
287 // bool GetValue() const
288 // Get the value of the toggle button.
289 bool wxToggleButton::GetValue() const
291 wxCHECK_MSG(m_widget
!= NULL
, FALSE
, wxT("invalid toggle button"));
293 return GTK_TOGGLE_BUTTON(m_widget
)->active
;
296 void wxToggleButton::SetLabel(const wxString
& label
)
298 wxCHECK_RET(m_widget
!= NULL
, wxT("invalid toggle button"));
300 wxControl::SetLabel(label
);
302 gtk_label_set(GTK_LABEL(BUTTON_CHILD(m_widget
)), wxGTK_CONV( GetLabel() ) );
305 bool wxToggleButton::Enable(bool enable
/*=TRUE*/)
307 if (!wxControl::Enable(enable
))
310 gtk_widget_set_sensitive(BUTTON_CHILD(m_widget
), enable
);
315 void wxToggleButton::ApplyWidgetStyle()
318 gtk_widget_set_style(m_widget
, m_widgetStyle
);
319 gtk_widget_set_style(BUTTON_CHILD(m_widget
), m_widgetStyle
);
322 bool wxToggleButton::IsOwnGtkWindow(GdkWindow
*window
)
324 return window
== TOGGLE_BUTTON_EVENT_WIN(m_widget
);
327 void wxToggleButton::OnInternalIdle()
329 wxCursor cursor
= m_cursor
;
331 if (g_globalCursor
.Ok())
332 cursor
= g_globalCursor
;
334 GdkWindow
*win
= TOGGLE_BUTTON_EVENT_WIN(m_widget
);
335 if ( win
&& cursor
.Ok() )
337 /* I now set the cursor the anew in every OnInternalIdle call
338 as setting the cursor in a parent window also effects the
339 windows above so that checking for the current cursor is
342 gdk_window_set_cursor(win
, cursor
.GetCursor());
345 if (wxUpdateUIEvent::CanUpdate(this))
346 UpdateWindowUI(wxUPDATE_UI_FROMIDLE
);
349 // wxSize DoGetBestSize() const
350 // Get the "best" size for this control.
351 wxSize
wxToggleButton::DoGetBestSize() const
353 wxSize
ret(wxControl::DoGetBestSize());
355 if (!HasFlag(wxBU_EXACTFIT
))
357 if (ret
.x
< 80) ret
.x
= 80;
364 #endif // wxUSE_TOGGLEBTN