1 /* GTK - The GIMP Toolkit
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18 /*
19 * Modified by the GTK+ Team and others 1997-2000. See the AUTHORS
20 * file for a list of people on the GTK+ Team. See the ChangeLog
21 * files for a list of changes. These files are distributed with
22 * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
23 */
24
25 #include "config.h"
26
27 #include "gtkmain.h"
28 #include "gtkwindowprivate.h"
29 #include "gtkwindowgroup.h"
30
31
32 /**
33 * SECTION:gtkwindowgroup
34 * @Short_description: Limit the effect of grabs
35 * @Title: GtkWindowGroup
36 *
37 * A #GtkWindowGroup restricts the effect of grabs to windows
38 * in the same group, thereby making window groups almost behave
39 * like separate applications.
40 *
41 * A window can be a member in at most one window group at a time.
42 * Windows that have not been explicitly assigned to a group are
43 * implicitly treated like windows of the default window group.
44 *
45 * GtkWindowGroup objects are referenced by each window in the group,
46 * so once you have added all windows to a GtkWindowGroup, you can drop
47 * the initial reference to the window group with g_object_unref(). If the
48 * windows in the window group are subsequently destroyed, then they will
49 * be removed from the window group and drop their references on the window
50 * group; when all window have been removed, the window group will be
51 * freed.
52 */
53
54 typedef struct _GtkDeviceGrabInfo GtkDeviceGrabInfo;
55 struct _GtkDeviceGrabInfo
56 {
57 GtkWidget *widget;
58 GdkDevice *device;
59 guint block_others : 1;
60 };
61
62 struct _GtkWindowGroupPrivate
63 {
64 GSList *grabs;
65 GSList *device_grabs;
66 };
67
G_DEFINE_TYPE_WITH_PRIVATE(GtkWindowGroup,gtk_window_group,G_TYPE_OBJECT)68 G_DEFINE_TYPE_WITH_PRIVATE (GtkWindowGroup, gtk_window_group, G_TYPE_OBJECT)
69
70 static void
71 gtk_window_group_init (GtkWindowGroup *group)
72 {
73 group->priv = gtk_window_group_get_instance_private (group);
74 }
75
76 static void
gtk_window_group_class_init(GtkWindowGroupClass * klass)77 gtk_window_group_class_init (GtkWindowGroupClass *klass)
78 {
79 }
80
81
82 /**
83 * gtk_window_group_new:
84 *
85 * Creates a new #GtkWindowGroup object. Grabs added with
86 * gtk_grab_add() only affect windows within the same #GtkWindowGroup.
87 *
88 * Returns: a new #GtkWindowGroup.
89 **/
90 GtkWindowGroup *
gtk_window_group_new(void)91 gtk_window_group_new (void)
92 {
93 return g_object_new (GTK_TYPE_WINDOW_GROUP, NULL);
94 }
95
96 static void
window_group_cleanup_grabs(GtkWindowGroup * group,GtkWindow * window)97 window_group_cleanup_grabs (GtkWindowGroup *group,
98 GtkWindow *window)
99 {
100 GtkWindowGroupPrivate *priv;
101 GtkDeviceGrabInfo *info;
102 GSList *tmp_list;
103 GSList *to_remove = NULL;
104
105 priv = group->priv;
106
107 tmp_list = priv->grabs;
108 while (tmp_list)
109 {
110 if (gtk_widget_get_toplevel (tmp_list->data) == (GtkWidget*) window)
111 to_remove = g_slist_prepend (to_remove, g_object_ref (tmp_list->data));
112 tmp_list = tmp_list->next;
113 }
114
115 while (to_remove)
116 {
117 gtk_grab_remove (to_remove->data);
118 g_object_unref (to_remove->data);
119 to_remove = g_slist_delete_link (to_remove, to_remove);
120 }
121
122 tmp_list = priv->device_grabs;
123
124 while (tmp_list)
125 {
126 info = tmp_list->data;
127
128 if (gtk_widget_get_toplevel (info->widget) == (GtkWidget *) window)
129 to_remove = g_slist_prepend (to_remove, info);
130
131 tmp_list = tmp_list->next;
132 }
133
134 while (to_remove)
135 {
136 info = to_remove->data;
137
138 gtk_device_grab_remove (info->widget, info->device);
139 to_remove = g_slist_delete_link (to_remove, to_remove);
140 }
141 }
142
143 /**
144 * gtk_window_group_add_window:
145 * @window_group: a #GtkWindowGroup
146 * @window: the #GtkWindow to add
147 *
148 * Adds a window to a #GtkWindowGroup.
149 **/
150 void
gtk_window_group_add_window(GtkWindowGroup * window_group,GtkWindow * window)151 gtk_window_group_add_window (GtkWindowGroup *window_group,
152 GtkWindow *window)
153 {
154 GtkWindowGroup *old_group;
155
156 g_return_if_fail (GTK_IS_WINDOW_GROUP (window_group));
157 g_return_if_fail (GTK_IS_WINDOW (window));
158
159 old_group = _gtk_window_get_window_group (window);
160
161 if (old_group != window_group)
162 {
163 g_object_ref (window);
164 g_object_ref (window_group);
165
166 if (old_group)
167 gtk_window_group_remove_window (old_group, window);
168 else
169 window_group_cleanup_grabs (gtk_window_get_group (NULL), window);
170
171 _gtk_window_set_window_group (window, window_group);
172
173 g_object_unref (window);
174 }
175 }
176
177 /**
178 * gtk_window_group_remove_window:
179 * @window_group: a #GtkWindowGroup
180 * @window: the #GtkWindow to remove
181 *
182 * Removes a window from a #GtkWindowGroup.
183 **/
184 void
gtk_window_group_remove_window(GtkWindowGroup * window_group,GtkWindow * window)185 gtk_window_group_remove_window (GtkWindowGroup *window_group,
186 GtkWindow *window)
187 {
188 g_return_if_fail (GTK_IS_WINDOW_GROUP (window_group));
189 g_return_if_fail (GTK_IS_WINDOW (window));
190 g_return_if_fail (_gtk_window_get_window_group (window) == window_group);
191
192 g_object_ref (window);
193
194 window_group_cleanup_grabs (window_group, window);
195 _gtk_window_set_window_group (window, NULL);
196
197 g_object_unref (window_group);
198 g_object_unref (window);
199 }
200
201 /**
202 * gtk_window_group_list_windows:
203 * @window_group: a #GtkWindowGroup
204 *
205 * Returns a list of the #GtkWindows that belong to @window_group.
206 *
207 * Returns: (element-type GtkWindow) (transfer container): A
208 * newly-allocated list of windows inside the group.
209 *
210 * Since: 2.14
211 **/
212 GList *
gtk_window_group_list_windows(GtkWindowGroup * window_group)213 gtk_window_group_list_windows (GtkWindowGroup *window_group)
214 {
215 GList *toplevels, *toplevel, *group_windows;
216
217 g_return_val_if_fail (GTK_IS_WINDOW_GROUP (window_group), NULL);
218
219 group_windows = NULL;
220 toplevels = gtk_window_list_toplevels ();
221
222 for (toplevel = toplevels; toplevel; toplevel = toplevel->next)
223 {
224 GtkWindow *window = toplevel->data;
225
226 if (window_group == _gtk_window_get_window_group (window))
227 group_windows = g_list_prepend (group_windows, window);
228 }
229
230 g_list_free (toplevels);
231
232 return g_list_reverse (group_windows);
233 }
234
235 /**
236 * gtk_window_group_get_current_grab:
237 * @window_group: a #GtkWindowGroup
238 *
239 * Gets the current grab widget of the given group,
240 * see gtk_grab_add().
241 *
242 * Returns: (transfer none): the current grab widget of the group
243 *
244 * Since: 2.22
245 */
246 GtkWidget *
gtk_window_group_get_current_grab(GtkWindowGroup * window_group)247 gtk_window_group_get_current_grab (GtkWindowGroup *window_group)
248 {
249 g_return_val_if_fail (GTK_IS_WINDOW_GROUP (window_group), NULL);
250
251 if (window_group->priv->grabs)
252 return GTK_WIDGET (window_group->priv->grabs->data);
253 return NULL;
254 }
255
256 void
_gtk_window_group_add_grab(GtkWindowGroup * window_group,GtkWidget * widget)257 _gtk_window_group_add_grab (GtkWindowGroup *window_group,
258 GtkWidget *widget)
259 {
260 GtkWindowGroupPrivate *priv;
261
262 priv = window_group->priv;
263 priv->grabs = g_slist_prepend (priv->grabs, widget);
264 }
265
266 void
_gtk_window_group_remove_grab(GtkWindowGroup * window_group,GtkWidget * widget)267 _gtk_window_group_remove_grab (GtkWindowGroup *window_group,
268 GtkWidget *widget)
269 {
270 GtkWindowGroupPrivate *priv;
271
272 priv = window_group->priv;
273 priv->grabs = g_slist_remove (priv->grabs, widget);
274 }
275
276 void
_gtk_window_group_add_device_grab(GtkWindowGroup * window_group,GtkWidget * widget,GdkDevice * device,gboolean block_others)277 _gtk_window_group_add_device_grab (GtkWindowGroup *window_group,
278 GtkWidget *widget,
279 GdkDevice *device,
280 gboolean block_others)
281 {
282 GtkWindowGroupPrivate *priv;
283 GtkDeviceGrabInfo *info;
284
285 priv = window_group->priv;
286
287 info = g_slice_new0 (GtkDeviceGrabInfo);
288 info->widget = widget;
289 info->device = device;
290 info->block_others = block_others;
291
292 priv->device_grabs = g_slist_prepend (priv->device_grabs, info);
293 }
294
295 void
_gtk_window_group_remove_device_grab(GtkWindowGroup * window_group,GtkWidget * widget,GdkDevice * device)296 _gtk_window_group_remove_device_grab (GtkWindowGroup *window_group,
297 GtkWidget *widget,
298 GdkDevice *device)
299 {
300 GtkWindowGroupPrivate *priv;
301 GtkDeviceGrabInfo *info;
302 GSList *list, *node = NULL;
303 GdkDevice *other_device;
304
305 priv = window_group->priv;
306 other_device = gdk_device_get_associated_device (device);
307 list = priv->device_grabs;
308
309 while (list)
310 {
311 info = list->data;
312
313 if (info->widget == widget &&
314 (info->device == device ||
315 info->device == other_device))
316 {
317 node = list;
318 break;
319 }
320
321 list = list->next;
322 }
323
324 if (node)
325 {
326 info = node->data;
327
328 priv->device_grabs = g_slist_delete_link (priv->device_grabs, node);
329 g_slice_free (GtkDeviceGrabInfo, info);
330 }
331 }
332
333 /**
334 * gtk_window_group_get_current_device_grab:
335 * @window_group: a #GtkWindowGroup
336 * @device: a #GdkDevice
337 *
338 * Returns the current grab widget for @device, or %NULL if none.
339 *
340 * Returns: (nullable) (transfer none): The grab widget, or %NULL
341 *
342 * Since: 3.0
343 */
344 GtkWidget *
gtk_window_group_get_current_device_grab(GtkWindowGroup * window_group,GdkDevice * device)345 gtk_window_group_get_current_device_grab (GtkWindowGroup *window_group,
346 GdkDevice *device)
347 {
348 GtkWindowGroupPrivate *priv;
349 GtkDeviceGrabInfo *info;
350 GdkDevice *other_device;
351 GSList *list;
352
353 g_return_val_if_fail (GTK_IS_WINDOW_GROUP (window_group), NULL);
354 g_return_val_if_fail (GDK_IS_DEVICE (device), NULL);
355
356 priv = window_group->priv;
357 list = priv->device_grabs;
358 other_device = gdk_device_get_associated_device (device);
359
360 while (list)
361 {
362 info = list->data;
363 list = list->next;
364
365 if (info->device == device ||
366 info->device == other_device)
367 return info->widget;
368 }
369
370 return NULL;
371 }
372
373 gboolean
_gtk_window_group_widget_is_blocked_for_device(GtkWindowGroup * window_group,GtkWidget * widget,GdkDevice * device)374 _gtk_window_group_widget_is_blocked_for_device (GtkWindowGroup *window_group,
375 GtkWidget *widget,
376 GdkDevice *device)
377 {
378 GtkWindowGroupPrivate *priv;
379 GtkDeviceGrabInfo *info;
380 GdkDevice *other_device;
381 GSList *list;
382
383 priv = window_group->priv;
384 other_device = gdk_device_get_associated_device (device);
385 list = priv->device_grabs;
386
387 while (list)
388 {
389 info = list->data;
390 list = list->next;
391
392 /* Look for blocking grabs on other device pairs
393 * that have the passed widget within the GTK+ grab.
394 */
395 if (info->block_others &&
396 info->device != device &&
397 info->device != other_device &&
398 (info->widget == widget ||
399 gtk_widget_is_ancestor (widget, info->widget)))
400 return TRUE;
401 }
402
403 return FALSE;
404 }
405