]> git.saurik.com Git - wxWidgets.git/blame - src/gtk/tglbtn.cpp
Don't have to explicitly set the background any more, the default will
[wxWidgets.git] / src / gtk / tglbtn.cpp
CommitLineData
1db8dc4a
VZ
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
6// Modified by:
7// Created: 08.02.01
8// RCS-ID: $Id$
9// Copyright: (c) 2000 Johnny C. Norris II
10// License: Rocketeer license
11/////////////////////////////////////////////////////////////////////////////
12
14f355c2
VS
13// For compilers that support precompilation, includes "wx.h".
14#include "wx/wxprec.h"
15
1db8dc4a 16#include "wx/tglbtn.h"
8ab696e0 17#include "wx/button.h"
1db8dc4a
VZ
18
19#if wxUSE_TOGGLEBTN
20
9e691f46 21#include "wx/gtk/private.h"
1db8dc4a
VZ
22
23extern void wxapp_install_idle_handler();
24extern bool g_isIdle;
25extern bool g_blockEventsOnDrag;
26extern wxCursor g_globalCursor;
d7fa7eaa 27extern wxWindowGTK *g_delayedFocus;
1db8dc4a 28
9864c56d 29static void gtk_togglebutton_clicked_callback(GtkWidget *WXUNUSED(widget), wxToggleButton *cb)
1db8dc4a
VZ
30{
31 if (g_isIdle)
32 wxapp_install_idle_handler();
33
34 if (!cb->m_hasVMT || g_blockEventsOnDrag)
35 return;
9864c56d
RR
36
37 if (cb->m_blockEvent) return;
1db8dc4a
VZ
38
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);
44}
45
1db8dc4a
VZ
46DEFINE_EVENT_TYPE(wxEVT_COMMAND_TOGGLEBUTTON_CLICKED)
47
4f856067
RR
48// ------------------------------------------------------------------------
49// wxToggleBitmapButton
50// ------------------------------------------------------------------------
51
52IMPLEMENT_DYNAMIC_CLASS(wxToggleBitmapButton, wxControl)
53
54bool wxToggleBitmapButton::Create(wxWindow *parent, wxWindowID id,
55 const wxBitmap &label, const wxPoint &pos,
56 const wxSize &size, long style,
57 const wxValidator& validator,
58 const wxString &name)
59{
60 m_needParent = TRUE;
61 m_acceptsFocus = TRUE;
62
63 m_blockEvent = FALSE;
64
65 if (!PreCreation(parent, pos, size) ||
66 !CreateBase(parent, id, pos, size, style, validator, name ))
67 {
68 wxFAIL_MSG(wxT("wxToggleBitmapButton creation failed"));
69 return FALSE;
70 }
71
72 m_bitmap = label;
73
74 // Create the gtk widget.
75 m_widget = gtk_toggle_button_new();
76
77 if (style & wxNO_BORDER)
78 gtk_button_set_relief( GTK_BUTTON(m_widget), GTK_RELIEF_NONE );
79
80 if (m_bitmap.Ok())
81 {
4f856067
RR
82 OnSetBitmap();
83 }
84
85 gtk_signal_connect(GTK_OBJECT(m_widget), "clicked",
86 GTK_SIGNAL_FUNC(gtk_togglebutton_clicked_callback),
87 (gpointer *)this);
88
89 m_parent->DoAddChild(this);
90
abdeb9e7 91 PostCreation(size);
4f856067
RR
92
93 return TRUE;
94}
95
96// void SetValue(bool state)
97// Set the value of the toggle button.
98void wxToggleBitmapButton::SetValue(bool state)
99{
100 wxCHECK_RET(m_widget != NULL, wxT("invalid toggle button"));
101
102 if (state == GetValue())
103 return;
104
105 m_blockEvent = TRUE;
106
107 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(m_widget), state);
108
109 m_blockEvent = FALSE;
110}
111
112// bool GetValue() const
113// Get the value of the toggle button.
114bool wxToggleBitmapButton::GetValue() const
115{
116 wxCHECK_MSG(m_widget != NULL, FALSE, wxT("invalid toggle button"));
117
118 return GTK_TOGGLE_BUTTON(m_widget)->active;
119}
120
121void wxToggleBitmapButton::SetLabel(const wxBitmap& label)
122{
123 wxCHECK_RET(m_widget != NULL, wxT("invalid toggle button"));
124
125 m_bitmap = label;
126
127 OnSetBitmap();
128}
129
130void wxToggleBitmapButton::OnSetBitmap()
131{
132 if (!m_bitmap.Ok()) return;
133
134 GdkBitmap *mask = (GdkBitmap *) NULL;
135 if (m_bitmap.GetMask()) mask = m_bitmap.GetMask()->GetBitmap();
136
137 GtkWidget *child = BUTTON_CHILD(m_widget);
138 if (child == NULL)
139 {
140 // initial bitmap
141 GtkWidget *pixmap = gtk_pixmap_new(m_bitmap.GetPixmap(), mask);
142 gtk_widget_show(pixmap);
143 gtk_container_add(GTK_CONTAINER(m_widget), pixmap);
144 }
145 else
146 { // subsequent bitmaps
147 GtkPixmap *g_pixmap = GTK_PIXMAP(child);
148 gtk_pixmap_set(g_pixmap, m_bitmap.GetPixmap(), mask);
149 }
150}
151
152bool wxToggleBitmapButton::Enable(bool enable /*=TRUE*/)
153{
154 if (!wxControl::Enable(enable))
155 return FALSE;
156
157 gtk_widget_set_sensitive(BUTTON_CHILD(m_widget), enable);
158
159 return TRUE;
160}
161
f40fdaa3 162void wxToggleBitmapButton::DoApplyWidgetStyle(GtkRcStyle *style)
4f856067 163{
f40fdaa3
VS
164 gtk_widget_modify_style(m_widget, style);
165 gtk_widget_modify_style(BUTTON_CHILD(m_widget), style);
4f856067
RR
166}
167
168bool wxToggleBitmapButton::IsOwnGtkWindow(GdkWindow *window)
169{
170 return window == TOGGLE_BUTTON_EVENT_WIN(m_widget);
171}
172
173void wxToggleBitmapButton::OnInternalIdle()
174{
175 wxCursor cursor = m_cursor;
176
177 if (g_globalCursor.Ok())
178 cursor = g_globalCursor;
179
180 GdkWindow *win = TOGGLE_BUTTON_EVENT_WIN(m_widget);
181 if ( win && cursor.Ok() )
182 {
183 /* I now set the cursor the anew in every OnInternalIdle call
184 as setting the cursor in a parent window also effects the
185 windows above so that checking for the current cursor is
186 not possible. */
187
188 gdk_window_set_cursor(win, cursor.GetCursor());
189 }
190
191 if (wxUpdateUIEvent::CanUpdate(this))
192 UpdateWindowUI(wxUPDATE_UI_FROMIDLE);
193}
194
195// wxSize DoGetBestSize() const
196// Get the "best" size for this control.
197wxSize wxToggleBitmapButton::DoGetBestSize() const
198{
abdeb9e7
RD
199 wxSize best;
200
201 if (m_bitmap.Ok())
4f856067 202 {
abdeb9e7
RD
203 int border = HasFlag(wxNO_BORDER) ? 4 : 10;
204 best.x = m_bitmap.GetWidth()+border;
205 best.y = m_bitmap.GetHeight()+border;
4f856067 206 }
abdeb9e7 207 return best;
4f856067 208}
9d522606
RD
209
210
211// static
212wxVisualAttributes
213wxToggleBitmapButton::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
214{
215 return GetDefaultAttributesFromGTKWidget(gtk_toggle_button_new);
216}
217
218
4f856067
RR
219// ------------------------------------------------------------------------
220// wxToggleButton
221// ------------------------------------------------------------------------
222
223IMPLEMENT_DYNAMIC_CLASS(wxToggleButton, wxControl)
224
1db8dc4a
VZ
225bool wxToggleButton::Create(wxWindow *parent, wxWindowID id,
226 const wxString &label, const wxPoint &pos,
227 const wxSize &size, long style,
228 const wxValidator& validator,
229 const wxString &name)
230{
231 m_needParent = TRUE;
232 m_acceptsFocus = TRUE;
9864c56d
RR
233
234 m_blockEvent = FALSE;
1db8dc4a
VZ
235
236 if (!PreCreation(parent, pos, size) ||
237 !CreateBase(parent, id, pos, size, style, validator, name )) {
238 wxFAIL_MSG(wxT("wxToggleButton creation failed"));
239 return FALSE;
240 }
241
242 wxControl::SetLabel(label);
243
244 // Create the gtk widget.
fab591c5 245 m_widget = gtk_toggle_button_new_with_label( wxGTK_CONV( m_label ) );
1db8dc4a
VZ
246
247 gtk_signal_connect(GTK_OBJECT(m_widget), "clicked",
248 GTK_SIGNAL_FUNC(gtk_togglebutton_clicked_callback),
249 (gpointer *)this);
250
251 m_parent->DoAddChild(this);
252
abdeb9e7 253 PostCreation(size);
1db8dc4a
VZ
254
255 return TRUE;
256}
257
258// void SetValue(bool state)
259// Set the value of the toggle button.
260void wxToggleButton::SetValue(bool state)
261{
262 wxCHECK_RET(m_widget != NULL, wxT("invalid toggle button"));
263
264 if (state == GetValue())
265 return;
266
9864c56d 267 m_blockEvent = TRUE;
1db8dc4a 268
e2762ff0 269 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(m_widget), state);
1db8dc4a 270
9864c56d 271 m_blockEvent = FALSE;
1db8dc4a
VZ
272}
273
274// bool GetValue() const
275// Get the value of the toggle button.
276bool wxToggleButton::GetValue() const
277{
278 wxCHECK_MSG(m_widget != NULL, FALSE, wxT("invalid toggle button"));
279
280 return GTK_TOGGLE_BUTTON(m_widget)->active;
281}
282
1db8dc4a
VZ
283void wxToggleButton::SetLabel(const wxString& label)
284{
8ab696e0 285 wxCHECK_RET(m_widget != NULL, wxT("invalid toggle button"));
1db8dc4a 286
8ab696e0 287 wxControl::SetLabel(label);
1db8dc4a 288
fab591c5 289 gtk_label_set(GTK_LABEL(BUTTON_CHILD(m_widget)), wxGTK_CONV( GetLabel() ) );
1db8dc4a
VZ
290}
291
1db8dc4a
VZ
292bool wxToggleButton::Enable(bool enable /*=TRUE*/)
293{
8ab696e0
RR
294 if (!wxControl::Enable(enable))
295 return FALSE;
1db8dc4a 296
9e691f46 297 gtk_widget_set_sensitive(BUTTON_CHILD(m_widget), enable);
1db8dc4a 298
8ab696e0 299 return TRUE;
1db8dc4a
VZ
300}
301
f40fdaa3 302void wxToggleButton::DoApplyWidgetStyle(GtkRcStyle *style)
1db8dc4a 303{
f40fdaa3
VS
304 gtk_widget_modify_style(m_widget, style);
305 gtk_widget_modify_style(BUTTON_CHILD(m_widget), style);
1db8dc4a
VZ
306}
307
1db8dc4a
VZ
308bool wxToggleButton::IsOwnGtkWindow(GdkWindow *window)
309{
9e691f46 310 return window == TOGGLE_BUTTON_EVENT_WIN(m_widget);
1db8dc4a
VZ
311}
312
1db8dc4a
VZ
313void wxToggleButton::OnInternalIdle()
314{
8ab696e0
RR
315 wxCursor cursor = m_cursor;
316
317 if (g_globalCursor.Ok())
318 cursor = g_globalCursor;
1db8dc4a 319
9e691f46
VZ
320 GdkWindow *win = TOGGLE_BUTTON_EVENT_WIN(m_widget);
321 if ( win && cursor.Ok() )
322 {
1db8dc4a
VZ
323 /* I now set the cursor the anew in every OnInternalIdle call
324 as setting the cursor in a parent window also effects the
325 windows above so that checking for the current cursor is
326 not possible. */
327
9e691f46 328 gdk_window_set_cursor(win, cursor.GetCursor());
8ab696e0 329 }
1db8dc4a 330
e39af974
JS
331 if (wxUpdateUIEvent::CanUpdate(this))
332 UpdateWindowUI(wxUPDATE_UI_FROMIDLE);
1db8dc4a
VZ
333}
334
335// wxSize DoGetBestSize() const
336// Get the "best" size for this control.
337wxSize wxToggleButton::DoGetBestSize() const
338{
8ab696e0
RR
339 wxSize ret(wxControl::DoGetBestSize());
340
341 if (!HasFlag(wxBU_EXACTFIT))
342 {
343 if (ret.x < 80) ret.x = 80;
344 }
345
1db8dc4a
VZ
346
347 return ret;
348}
349
9d522606
RD
350// static
351wxVisualAttributes
352wxToggleButton::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
353{
354 return GetDefaultAttributesFromGTKWidget(gtk_toggle_button_new);
355}
356
1db8dc4a
VZ
357#endif // wxUSE_TOGGLEBTN
358