]> git.saurik.com Git - wxWidgets.git/blob - src/gtk1/tglbtn.cpp
Patch 1173507 by Stas Sergeev. Allow FindFirst to work with patterns
[wxWidgets.git] / src / gtk1 / tglbtn.cpp
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: wxWindows licence
11 /////////////////////////////////////////////////////////////////////////////
12
13 // For compilers that support precompilation, includes "wx.h".
14 #include "wx/wxprec.h"
15
16 #include "wx/tglbtn.h"
17 #include "wx/button.h"
18
19 #if wxUSE_TOGGLEBTN
20
21 #include "wx/gtk/private.h"
22
23 extern void wxapp_install_idle_handler();
24 extern bool g_isIdle;
25 extern bool g_blockEventsOnDrag;
26 extern wxCursor g_globalCursor;
27 extern wxWindowGTK *g_delayedFocus;
28
29 extern "C" {
30 static void gtk_togglebutton_clicked_callback(GtkWidget *WXUNUSED(widget), wxToggleButton *cb)
31 {
32 if (g_isIdle)
33 wxapp_install_idle_handler();
34
35 if (!cb->m_hasVMT || g_blockEventsOnDrag)
36 return;
37
38 if (cb->m_blockEvent) return;
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 }
46 }
47
48 DEFINE_EVENT_TYPE(wxEVT_COMMAND_TOGGLEBUTTON_CLICKED)
49
50 // ------------------------------------------------------------------------
51 // wxToggleBitmapButton
52 // ------------------------------------------------------------------------
53
54 IMPLEMENT_DYNAMIC_CLASS(wxToggleBitmapButton, wxControl)
55
56 bool 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 {
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
93 PostCreation(size);
94
95 return TRUE;
96 }
97
98 // void SetValue(bool state)
99 // Set the value of the toggle button.
100 void 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.
116 bool 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
123 void wxToggleBitmapButton::SetLabel(const wxBitmap& label)
124 {
125 wxCHECK_RET(m_widget != NULL, wxT("invalid toggle button"));
126
127 m_bitmap = label;
128 InvalidateBestSize();
129
130 OnSetBitmap();
131 }
132
133 void 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
155 bool 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
165 void wxToggleBitmapButton::DoApplyWidgetStyle(GtkRcStyle *style)
166 {
167 gtk_widget_modify_style(m_widget, style);
168 gtk_widget_modify_style(BUTTON_CHILD(m_widget), style);
169 }
170
171 bool wxToggleBitmapButton::IsOwnGtkWindow(GdkWindow *window)
172 {
173 return window == TOGGLE_BUTTON_EVENT_WIN(m_widget);
174 }
175
176 void 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
198
199 // Get the "best" size for this control.
200 wxSize wxToggleBitmapButton::DoGetBestSize() const
201 {
202 wxSize best;
203
204 if (m_bitmap.Ok())
205 {
206 int border = HasFlag(wxNO_BORDER) ? 4 : 10;
207 best.x = m_bitmap.GetWidth()+border;
208 best.y = m_bitmap.GetHeight()+border;
209 }
210 CacheBestSize(best);
211 return best;
212 }
213
214
215 // static
216 wxVisualAttributes
217 wxToggleBitmapButton::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
218 {
219 return GetDefaultAttributesFromGTKWidget(gtk_toggle_button_new);
220 }
221
222
223 // ------------------------------------------------------------------------
224 // wxToggleButton
225 // ------------------------------------------------------------------------
226
227 IMPLEMENT_DYNAMIC_CLASS(wxToggleButton, wxControl)
228
229 bool 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;
237
238 m_blockEvent = FALSE;
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.
249 m_widget = gtk_toggle_button_new_with_label( wxGTK_CONV( m_label ) );
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
257 PostCreation(size);
258
259 return TRUE;
260 }
261
262 // void SetValue(bool state)
263 // Set the value of the toggle button.
264 void wxToggleButton::SetValue(bool state)
265 {
266 wxCHECK_RET(m_widget != NULL, wxT("invalid toggle button"));
267
268 if (state == GetValue())
269 return;
270
271 m_blockEvent = TRUE;
272
273 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(m_widget), state);
274
275 m_blockEvent = FALSE;
276 }
277
278 // bool GetValue() const
279 // Get the value of the toggle button.
280 bool 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
287 void wxToggleButton::SetLabel(const wxString& label)
288 {
289 wxCHECK_RET(m_widget != NULL, wxT("invalid toggle button"));
290
291 wxControl::SetLabel(label);
292
293 gtk_label_set(GTK_LABEL(BUTTON_CHILD(m_widget)), wxGTK_CONV( GetLabel() ) );
294 }
295
296 bool wxToggleButton::Enable(bool enable /*=TRUE*/)
297 {
298 if (!wxControl::Enable(enable))
299 return FALSE;
300
301 gtk_widget_set_sensitive(BUTTON_CHILD(m_widget), enable);
302
303 return TRUE;
304 }
305
306 void wxToggleButton::DoApplyWidgetStyle(GtkRcStyle *style)
307 {
308 gtk_widget_modify_style(m_widget, style);
309 gtk_widget_modify_style(BUTTON_CHILD(m_widget), style);
310 }
311
312 bool wxToggleButton::IsOwnGtkWindow(GdkWindow *window)
313 {
314 return window == TOGGLE_BUTTON_EVENT_WIN(m_widget);
315 }
316
317 void wxToggleButton::OnInternalIdle()
318 {
319 wxCursor cursor = m_cursor;
320
321 if (g_globalCursor.Ok())
322 cursor = g_globalCursor;
323
324 GdkWindow *win = TOGGLE_BUTTON_EVENT_WIN(m_widget);
325 if ( win && cursor.Ok() )
326 {
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
332 gdk_window_set_cursor(win, cursor.GetCursor());
333 }
334
335 if (wxUpdateUIEvent::CanUpdate(this))
336 UpdateWindowUI(wxUPDATE_UI_FROMIDLE);
337 }
338
339
340 // Get the "best" size for this control.
341 wxSize wxToggleButton::DoGetBestSize() const
342 {
343 wxSize ret(wxControl::DoGetBestSize());
344
345 if (!HasFlag(wxBU_EXACTFIT))
346 {
347 if (ret.x < 80) ret.x = 80;
348 }
349
350 CacheBestSize(ret);
351 return ret;
352 }
353
354 // static
355 wxVisualAttributes
356 wxToggleButton::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
357 {
358 return GetDefaultAttributesFromGTKWidget(gtk_toggle_button_new);
359 }
360
361 #endif // wxUSE_TOGGLEBTN
362