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