]> git.saurik.com Git - wxWidgets.git/blob - src/gtk1/tglbtn.cpp
expose find window method
[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: Rocketeer license
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 static void gtk_togglebutton_clicked_callback(GtkWidget *WXUNUSED(widget), wxToggleButton *cb)
30 {
31 if (g_isIdle)
32 wxapp_install_idle_handler();
33
34 if (!cb->m_hasVMT || g_blockEventsOnDrag)
35 return;
36
37 if (cb->m_blockEvent) return;
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
46 DEFINE_EVENT_TYPE(wxEVT_COMMAND_TOGGLEBUTTON_CLICKED)
47
48 // ------------------------------------------------------------------------
49 // wxToggleBitmapButton
50 // ------------------------------------------------------------------------
51
52 IMPLEMENT_DYNAMIC_CLASS(wxToggleBitmapButton, wxControl)
53
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,
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 {
82 wxSize newSize = size;
83 int border = (style & wxNO_BORDER) ? 4 : 10;
84 if (newSize.x == -1)
85 newSize.x = m_bitmap.GetWidth()+border;
86 if (newSize.y == -1)
87 newSize.y = m_bitmap.GetHeight()+border;
88 SetSize( newSize.x, newSize.y );
89 OnSetBitmap();
90 }
91
92 gtk_signal_connect(GTK_OBJECT(m_widget), "clicked",
93 GTK_SIGNAL_FUNC(gtk_togglebutton_clicked_callback),
94 (gpointer *)this);
95
96 m_parent->DoAddChild(this);
97
98 PostCreation();
99 InheritAttributes();
100
101 Show( TRUE );
102
103 return TRUE;
104 }
105
106 // void SetValue(bool state)
107 // Set the value of the toggle button.
108 void wxToggleBitmapButton::SetValue(bool state)
109 {
110 wxCHECK_RET(m_widget != NULL, wxT("invalid toggle button"));
111
112 if (state == GetValue())
113 return;
114
115 m_blockEvent = TRUE;
116
117 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(m_widget), state);
118
119 m_blockEvent = FALSE;
120 }
121
122 // bool GetValue() const
123 // Get the value of the toggle button.
124 bool wxToggleBitmapButton::GetValue() const
125 {
126 wxCHECK_MSG(m_widget != NULL, FALSE, wxT("invalid toggle button"));
127
128 return GTK_TOGGLE_BUTTON(m_widget)->active;
129 }
130
131 void wxToggleBitmapButton::SetLabel(const wxBitmap& label)
132 {
133 wxCHECK_RET(m_widget != NULL, wxT("invalid toggle button"));
134
135 m_bitmap = label;
136
137 OnSetBitmap();
138 }
139
140 void wxToggleBitmapButton::OnSetBitmap()
141 {
142 if (!m_bitmap.Ok()) return;
143
144 GdkBitmap *mask = (GdkBitmap *) NULL;
145 if (m_bitmap.GetMask()) mask = m_bitmap.GetMask()->GetBitmap();
146
147 GtkWidget *child = BUTTON_CHILD(m_widget);
148 if (child == NULL)
149 {
150 // initial bitmap
151 GtkWidget *pixmap = gtk_pixmap_new(m_bitmap.GetPixmap(), mask);
152 gtk_widget_show(pixmap);
153 gtk_container_add(GTK_CONTAINER(m_widget), pixmap);
154 }
155 else
156 { // subsequent bitmaps
157 GtkPixmap *g_pixmap = GTK_PIXMAP(child);
158 gtk_pixmap_set(g_pixmap, m_bitmap.GetPixmap(), mask);
159 }
160 }
161
162 bool wxToggleBitmapButton::Enable(bool enable /*=TRUE*/)
163 {
164 if (!wxControl::Enable(enable))
165 return FALSE;
166
167 gtk_widget_set_sensitive(BUTTON_CHILD(m_widget), enable);
168
169 return TRUE;
170 }
171
172 void wxToggleBitmapButton::ApplyWidgetStyle()
173 {
174 SetWidgetStyle();
175 gtk_widget_set_style(m_widget, m_widgetStyle);
176 gtk_widget_set_style(BUTTON_CHILD(m_widget), m_widgetStyle);
177 }
178
179 bool wxToggleBitmapButton::IsOwnGtkWindow(GdkWindow *window)
180 {
181 return window == TOGGLE_BUTTON_EVENT_WIN(m_widget);
182 }
183
184 void wxToggleBitmapButton::OnInternalIdle()
185 {
186 wxCursor cursor = m_cursor;
187
188 if (g_globalCursor.Ok())
189 cursor = g_globalCursor;
190
191 GdkWindow *win = TOGGLE_BUTTON_EVENT_WIN(m_widget);
192 if ( win && cursor.Ok() )
193 {
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
197 not possible. */
198
199 gdk_window_set_cursor(win, cursor.GetCursor());
200 }
201
202 if (wxUpdateUIEvent::CanUpdate(this))
203 UpdateWindowUI(wxUPDATE_UI_FROMIDLE);
204 }
205
206 // wxSize DoGetBestSize() const
207 // Get the "best" size for this control.
208 wxSize wxToggleBitmapButton::DoGetBestSize() const
209 {
210 wxSize ret(wxControl::DoGetBestSize());
211
212 if (!HasFlag(wxBU_EXACTFIT))
213 {
214 if (ret.x < 80) ret.x = 80;
215 }
216
217
218 return ret;
219 }
220 // ------------------------------------------------------------------------
221 // wxToggleButton
222 // ------------------------------------------------------------------------
223
224 IMPLEMENT_DYNAMIC_CLASS(wxToggleButton, wxControl)
225
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)
231 {
232 m_needParent = TRUE;
233 m_acceptsFocus = TRUE;
234
235 m_blockEvent = FALSE;
236
237 if (!PreCreation(parent, pos, size) ||
238 !CreateBase(parent, id, pos, size, style, validator, name )) {
239 wxFAIL_MSG(wxT("wxToggleButton creation failed"));
240 return FALSE;
241 }
242
243 wxControl::SetLabel(label);
244
245 // Create the gtk widget.
246 m_widget = gtk_toggle_button_new_with_label( wxGTK_CONV( m_label ) );
247
248 gtk_signal_connect(GTK_OBJECT(m_widget), "clicked",
249 GTK_SIGNAL_FUNC(gtk_togglebutton_clicked_callback),
250 (gpointer *)this);
251
252 m_parent->DoAddChild(this);
253
254 PostCreation();
255 InheritAttributes();
256
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);
265
266 Show(TRUE);
267
268 return TRUE;
269 }
270
271 // void SetValue(bool state)
272 // Set the value of the toggle button.
273 void wxToggleButton::SetValue(bool state)
274 {
275 wxCHECK_RET(m_widget != NULL, wxT("invalid toggle button"));
276
277 if (state == GetValue())
278 return;
279
280 m_blockEvent = TRUE;
281
282 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(m_widget), state);
283
284 m_blockEvent = FALSE;
285 }
286
287 // bool GetValue() const
288 // Get the value of the toggle button.
289 bool wxToggleButton::GetValue() const
290 {
291 wxCHECK_MSG(m_widget != NULL, FALSE, wxT("invalid toggle button"));
292
293 return GTK_TOGGLE_BUTTON(m_widget)->active;
294 }
295
296 void wxToggleButton::SetLabel(const wxString& label)
297 {
298 wxCHECK_RET(m_widget != NULL, wxT("invalid toggle button"));
299
300 wxControl::SetLabel(label);
301
302 gtk_label_set(GTK_LABEL(BUTTON_CHILD(m_widget)), wxGTK_CONV( GetLabel() ) );
303 }
304
305 bool wxToggleButton::Enable(bool enable /*=TRUE*/)
306 {
307 if (!wxControl::Enable(enable))
308 return FALSE;
309
310 gtk_widget_set_sensitive(BUTTON_CHILD(m_widget), enable);
311
312 return TRUE;
313 }
314
315 void wxToggleButton::ApplyWidgetStyle()
316 {
317 SetWidgetStyle();
318 gtk_widget_set_style(m_widget, m_widgetStyle);
319 gtk_widget_set_style(BUTTON_CHILD(m_widget), m_widgetStyle);
320 }
321
322 bool wxToggleButton::IsOwnGtkWindow(GdkWindow *window)
323 {
324 return window == TOGGLE_BUTTON_EVENT_WIN(m_widget);
325 }
326
327 void wxToggleButton::OnInternalIdle()
328 {
329 wxCursor cursor = m_cursor;
330
331 if (g_globalCursor.Ok())
332 cursor = g_globalCursor;
333
334 GdkWindow *win = TOGGLE_BUTTON_EVENT_WIN(m_widget);
335 if ( win && cursor.Ok() )
336 {
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
340 not possible. */
341
342 gdk_window_set_cursor(win, cursor.GetCursor());
343 }
344
345 if (wxUpdateUIEvent::CanUpdate(this))
346 UpdateWindowUI(wxUPDATE_UI_FROMIDLE);
347 }
348
349 // wxSize DoGetBestSize() const
350 // Get the "best" size for this control.
351 wxSize wxToggleButton::DoGetBestSize() const
352 {
353 wxSize ret(wxControl::DoGetBestSize());
354
355 if (!HasFlag(wxBU_EXACTFIT))
356 {
357 if (ret.x < 80) ret.x = 80;
358 }
359
360
361 return ret;
362 }
363
364 #endif // wxUSE_TOGGLEBTN
365