gtkwindowgroup.c 6.89 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
/* GTK - The GIMP Toolkit
 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
 */

/*
 * Modified by the GTK+ Team and others 1997-2000.  See the AUTHORS
 * file for a list of people on the GTK+ Team.  See the ChangeLog
 * files for a list of changes.  These files are distributed with
 * GTK+ at ftp://ftp.gtk.org/pub/gtk/. 
 */

#include "config.h"

#include "gtkmain.h"
#include "gtkwindowprivate.h"
#include "gtkwindowgroup.h"
30
#include "gtkprivate.h"
31
32
33


/**
Matthias Clasen's avatar
Matthias Clasen committed
34
 * GtkWindowGroup:
35
 *
Matthias Clasen's avatar
Matthias Clasen committed
36
37
38
39
 * `GtkWindowGroup` makes group of windows behave like separate applications.
 *
 * It achieves this by limiting the effect of GTK grabs and modality
 * to windows in the same group.
Matthias Clasen's avatar
Matthias Clasen committed
40
41
42
43
44
 *
 * A window can be a member in at most one window group at a time.
 * Windows that have not been explicitly assigned to a group are
 * implicitly treated like windows of the default window group.
 *
Matthias Clasen's avatar
Matthias Clasen committed
45
46
 * `GtkWindowGroup` objects are referenced by each window in the group,
 * so once you have added all windows to a `GtkWindowGroup`, you can drop
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
 * the initial reference to the window group with g_object_unref(). If the
 * windows in the window group are subsequently destroyed, then they will
 * be removed from the window group and drop their references on the window
 * group; when all window have been removed, the window group will be
 * freed.
 */

typedef struct _GtkDeviceGrabInfo GtkDeviceGrabInfo;
struct _GtkDeviceGrabInfo
{
  GtkWidget *widget;
  GdkDevice *device;
  guint block_others : 1;
};

struct _GtkWindowGroupPrivate
{
  GSList *grabs;
};

G_DEFINE_TYPE_WITH_PRIVATE (GtkWindowGroup, gtk_window_group, G_TYPE_OBJECT)

static void
gtk_window_group_init (GtkWindowGroup *group)
{
  group->priv = gtk_window_group_get_instance_private (group);
}

static void
gtk_window_group_class_init (GtkWindowGroupClass *klass)
{
}


/**
 * gtk_window_group_new:
Matthias Clasen's avatar
Matthias Clasen committed
83
84
 *
 * Creates a new `GtkWindowGroup` object.
85
86
 *
 * Modality of windows only affects windows
Matthias Clasen's avatar
Matthias Clasen committed
87
 * within the same `GtkWindowGroup`.
88
 * 
Matthias Clasen's avatar
Matthias Clasen committed
89
 * Returns: a new `GtkWindowGroup`.
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
 **/
GtkWindowGroup *
gtk_window_group_new (void)
{
  return g_object_new (GTK_TYPE_WINDOW_GROUP, NULL);
}

static void
window_group_cleanup_grabs (GtkWindowGroup *group,
                            GtkWindow      *window)
{
  GtkWindowGroupPrivate *priv;
  GSList *tmp_list;
  GSList *to_remove = NULL;

  priv = group->priv;

  tmp_list = priv->grabs;
  while (tmp_list)
    {
110
      if (gtk_widget_get_root (tmp_list->data) == (GtkRoot*) window)
111
112
113
114
115
116
117
118
119
120
121
122
123
124
        to_remove = g_slist_prepend (to_remove, g_object_ref (tmp_list->data));
      tmp_list = tmp_list->next;
    }

  while (to_remove)
    {
      gtk_grab_remove (to_remove->data);
      g_object_unref (to_remove->data);
      to_remove = g_slist_delete_link (to_remove, to_remove);
    }
}

/**
 * gtk_window_group_add_window:
Matthias Clasen's avatar
Matthias Clasen committed
125
126
127
128
129
 * @window_group: a `GtkWindowGroup`
 * @window: the `GtkWindow` to add
 *
 * Adds a window to a `GtkWindowGroup`.
 */
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
void
gtk_window_group_add_window (GtkWindowGroup *window_group,
                             GtkWindow      *window)
{
  GtkWindowGroup *old_group;

  g_return_if_fail (GTK_IS_WINDOW_GROUP (window_group));
  g_return_if_fail (GTK_IS_WINDOW (window));

  old_group = _gtk_window_get_window_group (window);

  if (old_group != window_group)
    {
      g_object_ref (window);
      g_object_ref (window_group);

      if (old_group)
        gtk_window_group_remove_window (old_group, window);
      else
        window_group_cleanup_grabs (gtk_window_get_group (NULL), window);

      _gtk_window_set_window_group (window, window_group);

      g_object_unref (window);
    }
}

/**
 * gtk_window_group_remove_window:
Matthias Clasen's avatar
Matthias Clasen committed
159
160
161
162
163
 * @window_group: a `GtkWindowGroup`
 * @window: the `GtkWindow` to remove
 *
 * Removes a window from a `GtkWindowGroup`.
 */
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
void
gtk_window_group_remove_window (GtkWindowGroup *window_group,
                                GtkWindow      *window)
{
  g_return_if_fail (GTK_IS_WINDOW_GROUP (window_group));
  g_return_if_fail (GTK_IS_WINDOW (window));
  g_return_if_fail (_gtk_window_get_window_group (window) == window_group);

  g_object_ref (window);

  window_group_cleanup_grabs (window_group, window);
  _gtk_window_set_window_group (window, NULL);

  g_object_unref (window_group);
  g_object_unref (window);
}

/**
 * gtk_window_group_list_windows:
Matthias Clasen's avatar
Matthias Clasen committed
183
 * @window_group: a `GtkWindowGroup`
184
 *
Matthias Clasen's avatar
Matthias Clasen committed
185
 * Returns a list of the `GtkWindows` that belong to @window_group.
186
187
188
 *
 * Returns: (element-type GtkWindow) (transfer container): A
 *   newly-allocated list of windows inside the group.
Matthias Clasen's avatar
Matthias Clasen committed
189
 */
190
191
192
193
194
195
196
197
198
199
200
201
202
203
GList *
gtk_window_group_list_windows (GtkWindowGroup *window_group)
{
  GList *toplevels, *toplevel, *group_windows;

  g_return_val_if_fail (GTK_IS_WINDOW_GROUP (window_group), NULL);

  group_windows = NULL;
  toplevels = gtk_window_list_toplevels ();

  for (toplevel = toplevels; toplevel; toplevel = toplevel->next)
    {
      GtkWindow *window = toplevel->data;

204
      if (window_group == gtk_window_get_group (window))
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
        group_windows = g_list_prepend (group_windows, window);
    }

  g_list_free (toplevels);

  return g_list_reverse (group_windows);
}

GtkWidget *
gtk_window_group_get_current_grab (GtkWindowGroup *window_group)
{
  g_return_val_if_fail (GTK_IS_WINDOW_GROUP (window_group), NULL);

  if (window_group->priv->grabs)
    return GTK_WIDGET (window_group->priv->grabs->data);
  return NULL;
}

223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
static void
revoke_implicit_grabs (GtkWindowGroup *window_group,
                       GdkDevice      *device,
                       GtkWidget      *grab_widget)
{
  GList *windows, *l;

  windows = gtk_window_group_list_windows (window_group);

  for (l = windows; l; l = l->next)
    {
      gtk_window_maybe_revoke_implicit_grab (l->data,
                                             device,
                                             grab_widget);
    }

  g_list_free (windows);
}

242
243
244
245
246
247
248
249
void
_gtk_window_group_add_grab (GtkWindowGroup *window_group,
                            GtkWidget      *widget)
{
  GtkWindowGroupPrivate *priv;

  priv = window_group->priv;
  priv->grabs = g_slist_prepend (priv->grabs, widget);
250
251

  revoke_implicit_grabs (window_group, NULL, widget);
252
253
254
255
256
257
258
259
260
261
262
}

void
_gtk_window_group_remove_grab (GtkWindowGroup *window_group,
                               GtkWidget      *widget)
{
  GtkWindowGroupPrivate *priv;

  priv = window_group->priv;
  priv->grabs = g_slist_remove (priv->grabs, widget);
}