]> git.saurik.com Git - wxWidgets.git/blame - src/gtk/tglbtn.cpp
I admit I have no good solution for wxPrinter::Setup(wxWindow*)
[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;
9f884528 126 InvalidateBestSize();
4f856067
RR
127
128 OnSetBitmap();
129}
130
131void wxToggleBitmapButton::OnSetBitmap()
132{
133 if (!m_bitmap.Ok()) return;
134
135 GdkBitmap *mask = (GdkBitmap *) NULL;
136 if (m_bitmap.GetMask()) mask = m_bitmap.GetMask()->GetBitmap();
137
138 GtkWidget *child = BUTTON_CHILD(m_widget);
139 if (child == NULL)
140 {
141 // initial bitmap
142 GtkWidget *pixmap = gtk_pixmap_new(m_bitmap.GetPixmap(), mask);
143 gtk_widget_show(pixmap);
144 gtk_container_add(GTK_CONTAINER(m_widget), pixmap);
145 }
146 else
147 { // subsequent bitmaps
148 GtkPixmap *g_pixmap = GTK_PIXMAP(child);
149 gtk_pixmap_set(g_pixmap, m_bitmap.GetPixmap(), mask);
150 }
151}
152
153bool wxToggleBitmapButton::Enable(bool enable /*=TRUE*/)
154{
155 if (!wxControl::Enable(enable))
156 return FALSE;
157
158 gtk_widget_set_sensitive(BUTTON_CHILD(m_widget), enable);
159
160 return TRUE;
161}
162
f40fdaa3 163void wxToggleBitmapButton::DoApplyWidgetStyle(GtkRcStyle *style)
4f856067 164{
f40fdaa3
VS
165 gtk_widget_modify_style(m_widget, style);
166 gtk_widget_modify_style(BUTTON_CHILD(m_widget), style);
4f856067
RR
167}
168
169bool wxToggleBitmapButton::IsOwnGtkWindow(GdkWindow *window)
170{
171 return window == TOGGLE_BUTTON_EVENT_WIN(m_widget);
172}
173
174void wxToggleBitmapButton::OnInternalIdle()
175{
176 wxCursor cursor = m_cursor;
177
178 if (g_globalCursor.Ok())
179 cursor = g_globalCursor;
180
181 GdkWindow *win = TOGGLE_BUTTON_EVENT_WIN(m_widget);
182 if ( win && cursor.Ok() )
183 {
184 /* I now set the cursor the anew in every OnInternalIdle call
185 as setting the cursor in a parent window also effects the
186 windows above so that checking for the current cursor is
187 not possible. */
188
189 gdk_window_set_cursor(win, cursor.GetCursor());
190 }
191
192 if (wxUpdateUIEvent::CanUpdate(this))
193 UpdateWindowUI(wxUPDATE_UI_FROMIDLE);
194}
195
9f884528 196
4f856067
RR
197// Get the "best" size for this control.
198wxSize wxToggleBitmapButton::DoGetBestSize() const
199{
abdeb9e7
RD
200 wxSize best;
201
202 if (m_bitmap.Ok())
4f856067 203 {
abdeb9e7
RD
204 int border = HasFlag(wxNO_BORDER) ? 4 : 10;
205 best.x = m_bitmap.GetWidth()+border;
206 best.y = m_bitmap.GetHeight()+border;
4f856067 207 }
9f884528 208 CacheBestSize(best);
abdeb9e7 209 return best;
4f856067 210}
9d522606
RD
211
212
213// static
214wxVisualAttributes
215wxToggleBitmapButton::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
216{
217 return GetDefaultAttributesFromGTKWidget(gtk_toggle_button_new);
218}
219
220
4f856067
RR
221// ------------------------------------------------------------------------
222// wxToggleButton
223// ------------------------------------------------------------------------
224
225IMPLEMENT_DYNAMIC_CLASS(wxToggleButton, wxControl)
226
1db8dc4a
VZ
227bool wxToggleButton::Create(wxWindow *parent, wxWindowID id,
228 const wxString &label, const wxPoint &pos,
229 const wxSize &size, long style,
230 const wxValidator& validator,
231 const wxString &name)
232{
233 m_needParent = TRUE;
234 m_acceptsFocus = TRUE;
9864c56d
RR
235
236 m_blockEvent = FALSE;
1db8dc4a
VZ
237
238 if (!PreCreation(parent, pos, size) ||
239 !CreateBase(parent, id, pos, size, style, validator, name )) {
240 wxFAIL_MSG(wxT("wxToggleButton creation failed"));
241 return FALSE;
242 }
243
244 wxControl::SetLabel(label);
245
246 // Create the gtk widget.
fab591c5 247 m_widget = gtk_toggle_button_new_with_label( wxGTK_CONV( m_label ) );
1db8dc4a
VZ
248
249 gtk_signal_connect(GTK_OBJECT(m_widget), "clicked",
250 GTK_SIGNAL_FUNC(gtk_togglebutton_clicked_callback),
251 (gpointer *)this);
252
253 m_parent->DoAddChild(this);
254
abdeb9e7 255 PostCreation(size);
1db8dc4a
VZ
256
257 return TRUE;
258}
259
260// void SetValue(bool state)
261// Set the value of the toggle button.
262void wxToggleButton::SetValue(bool state)
263{
264 wxCHECK_RET(m_widget != NULL, wxT("invalid toggle button"));
265
266 if (state == GetValue())
267 return;
268
9864c56d 269 m_blockEvent = TRUE;
1db8dc4a 270
e2762ff0 271 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(m_widget), state);
1db8dc4a 272
9864c56d 273 m_blockEvent = FALSE;
1db8dc4a
VZ
274}
275
276// bool GetValue() const
277// Get the value of the toggle button.
278bool wxToggleButton::GetValue() const
279{
280 wxCHECK_MSG(m_widget != NULL, FALSE, wxT("invalid toggle button"));
281
282 return GTK_TOGGLE_BUTTON(m_widget)->active;
283}
284
1db8dc4a
VZ
285void wxToggleButton::SetLabel(const wxString& label)
286{
8ab696e0 287 wxCHECK_RET(m_widget != NULL, wxT("invalid toggle button"));
1db8dc4a 288
8ab696e0 289 wxControl::SetLabel(label);
1db8dc4a 290
fab591c5 291 gtk_label_set(GTK_LABEL(BUTTON_CHILD(m_widget)), wxGTK_CONV( GetLabel() ) );
1db8dc4a
VZ
292}
293
1db8dc4a
VZ
294bool wxToggleButton::Enable(bool enable /*=TRUE*/)
295{
8ab696e0
RR
296 if (!wxControl::Enable(enable))
297 return FALSE;
1db8dc4a 298
9e691f46 299 gtk_widget_set_sensitive(BUTTON_CHILD(m_widget), enable);
1db8dc4a 300
8ab696e0 301 return TRUE;
1db8dc4a
VZ
302}
303
f40fdaa3 304void wxToggleButton::DoApplyWidgetStyle(GtkRcStyle *style)
1db8dc4a 305{
f40fdaa3
VS
306 gtk_widget_modify_style(m_widget, style);
307 gtk_widget_modify_style(BUTTON_CHILD(m_widget), style);
1db8dc4a
VZ
308}
309
1db8dc4a
VZ
310bool wxToggleButton::IsOwnGtkWindow(GdkWindow *window)
311{
9e691f46 312 return window == TOGGLE_BUTTON_EVENT_WIN(m_widget);
1db8dc4a
VZ
313}
314
1db8dc4a
VZ
315void wxToggleButton::OnInternalIdle()
316{
8ab696e0
RR
317 wxCursor cursor = m_cursor;
318
319 if (g_globalCursor.Ok())
320 cursor = g_globalCursor;
1db8dc4a 321
9e691f46
VZ
322 GdkWindow *win = TOGGLE_BUTTON_EVENT_WIN(m_widget);
323 if ( win && cursor.Ok() )
324 {
1db8dc4a
VZ
325 /* I now set the cursor the anew in every OnInternalIdle call
326 as setting the cursor in a parent window also effects the
327 windows above so that checking for the current cursor is
328 not possible. */
329
9e691f46 330 gdk_window_set_cursor(win, cursor.GetCursor());
8ab696e0 331 }
1db8dc4a 332
e39af974
JS
333 if (wxUpdateUIEvent::CanUpdate(this))
334 UpdateWindowUI(wxUPDATE_UI_FROMIDLE);
1db8dc4a
VZ
335}
336
9f884528 337
1db8dc4a
VZ
338// Get the "best" size for this control.
339wxSize wxToggleButton::DoGetBestSize() const
340{
8ab696e0
RR
341 wxSize ret(wxControl::DoGetBestSize());
342
343 if (!HasFlag(wxBU_EXACTFIT))
344 {
345 if (ret.x < 80) ret.x = 80;
346 }
347
9f884528
RD
348 CacheBestSize(ret);
349 return ret;
1db8dc4a
VZ
350}
351
9d522606
RD
352// static
353wxVisualAttributes
354wxToggleButton::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
355{
356 return GetDefaultAttributesFromGTKWidget(gtk_toggle_button_new);
357}
358
1db8dc4a
VZ
359#endif // wxUSE_TOGGLEBTN
360