File: panel-utils.c

package info (click to toggle)
xfce4-panel 4.20.7-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 17,220 kB
  • sloc: ansic: 61,756; sh: 5,515; makefile: 1,330; python: 128; xml: 101; sed: 16
file content (415 lines) | stat: -rw-r--r-- 11,867 bytes parent folder | download
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
/*
 * Copyright (C) 2009-2010 Nick Schermer <nick@xfce.org>
 *
 * 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.1 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, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include "panel-private.h"
#include "panel-utils.h"

#include <libxfce4ui/libxfce4ui.h>



typedef struct _PanelUtilsGtkLabelData
{
  const gchar *label_text;
  GtkLabel *label;
} PanelUtilsGtkLabelData;



void
_panel_utils_weak_notify (gpointer data,
                          GObject *where_the_object_was)
{
  if (XFCE_IS_PANEL_PLUGIN (data))
    xfce_panel_plugin_unblock_menu (data);
  else
    g_object_unref (data);
}



static void
panel_utils_help_button_clicked (GtkWidget *button,
                                 XfcePanelPlugin *panel_plugin)
{
  GtkWidget *toplevel;

  panel_return_if_fail (XFCE_IS_PANEL_PLUGIN (panel_plugin));
  panel_return_if_fail (GTK_IS_WIDGET (button));

  toplevel = gtk_widget_get_toplevel (button);
  panel_utils_show_help (GTK_WINDOW (toplevel),
                         xfce_panel_plugin_get_name (panel_plugin),
                         NULL);
}



static void
panel_utils_block_autohide (XfcePanelPlugin *panel_plugin)
{
  panel_return_if_fail (XFCE_IS_PANEL_PLUGIN (panel_plugin));

  xfce_panel_plugin_block_autohide (panel_plugin, TRUE);
}



static void
panel_utils_unblock_autohide (XfcePanelPlugin *panel_plugin)
{
  panel_return_if_fail (XFCE_IS_PANEL_PLUGIN (panel_plugin));

  xfce_panel_plugin_block_autohide (panel_plugin, FALSE);
}



GtkBuilder *
panel_utils_builder_new (XfcePanelPlugin *panel_plugin,
                         const gchar *resource,
                         GObject **dialog_return)
{
  GError *error = NULL;
  GtkBuilder *builder;
  GObject *dialog, *button;

  panel_return_val_if_fail (XFCE_IS_PANEL_PLUGIN (panel_plugin), NULL);

  builder = gtk_builder_new ();
  gtk_builder_set_translation_domain (builder, GETTEXT_PACKAGE);
  if (gtk_builder_add_from_resource (builder, resource, &error))
    {
      dialog = gtk_builder_get_object (builder, "dialog");
      if (G_LIKELY (dialog != NULL))
        {
          g_object_weak_ref (G_OBJECT (dialog), _panel_utils_weak_notify, builder);
          xfce_panel_plugin_take_window (panel_plugin, GTK_WINDOW (dialog));

          xfce_panel_plugin_block_menu (panel_plugin);
          g_object_weak_ref (G_OBJECT (dialog), _panel_utils_weak_notify, panel_plugin);

          g_signal_connect_swapped (dialog, "show",
                                    G_CALLBACK (panel_utils_block_autohide), panel_plugin);
          g_signal_connect_swapped (dialog, "hide",
                                    G_CALLBACK (panel_utils_unblock_autohide), panel_plugin);

          button = gtk_builder_get_object (builder, "close-button");
          if (G_LIKELY (button != NULL))
            g_signal_connect_swapped (G_OBJECT (button), "clicked",
                                      G_CALLBACK (gtk_widget_destroy), dialog);

          button = gtk_builder_get_object (builder, "help-button");
          if (G_LIKELY (button != NULL))
            g_signal_connect (G_OBJECT (button), "clicked",
                              G_CALLBACK (panel_utils_help_button_clicked), panel_plugin);

          if (G_LIKELY (dialog_return != NULL))
            *dialog_return = dialog;

          return builder;
        }
      else
        {
          g_set_error_literal (&error, 0, 0, "No widget with the name \"dialog\" found");
        }
    }

  g_critical ("Failed to construct the builder for plugin %s-%d: %s.",
              xfce_panel_plugin_get_name (panel_plugin),
              xfce_panel_plugin_get_unique_id (panel_plugin),
              error->message);
  g_error_free (error);
  g_object_unref (G_OBJECT (builder));

  return NULL;
}



void
panel_utils_show_help (GtkWindow *parent,
                       const gchar *page,
                       const gchar *offset)
{
  xfce_dialog_show_help (parent, "xfce4-panel", page, offset);
}



gboolean
panel_utils_device_grab (GtkWidget *widget)
{
  GdkScreen *screen = gtk_widget_get_screen (widget);
  GdkDisplay *display = gdk_screen_get_display (screen);
  GdkSeat *seat = gdk_display_get_default_seat (display);
  GdkWindow *window = gdk_window_get_effective_toplevel (gtk_widget_get_window (widget));

  return xfce_gdk_device_grab (seat, window, GDK_SEAT_CAPABILITY_ALL, NULL);
}



void
panel_utils_set_atk_info (GtkWidget *widget,
                          const gchar *name,
                          const gchar *description)
{
  static gboolean initialized = FALSE;
  static gboolean atk_enabled = TRUE;
  AtkObject *object;

  panel_return_if_fail (GTK_IS_WIDGET (widget));

  if (atk_enabled)
    {
      object = gtk_widget_get_accessible (widget);

      if (!initialized)
        {
          initialized = TRUE;
          atk_enabled = GTK_IS_ACCESSIBLE (object);

          if (!atk_enabled)
            return;
        }

      if (name != NULL)
        atk_object_set_name (object, name);

      if (description != NULL)
        atk_object_set_description (object, description);
    }
}



static gboolean
destroy_later (gpointer widget)
{
  gtk_widget_destroy (GTK_WIDGET (widget));
  g_object_unref (G_OBJECT (widget));
  return FALSE;
}



void
panel_utils_destroy_later (GtkWidget *widget)
{
  panel_return_if_fail (GTK_IS_WIDGET (widget));

  g_idle_add_full (G_PRIORITY_HIGH, destroy_later, widget, NULL);
  g_object_ref_sink (G_OBJECT (widget));
}



/*
 * We need to do this when GTK refuses to do it itself, for example to bring back a window
 * that has been moved off-screen, see https://github.com/wmww/gtk-layer-shell/issues/143.
 * This manual intervention should only be done if necessary though, as it can have side
 * effects. It is not performed systematically in Gtk Layer Shell for this reason, and, for
 * example, it causes the pointer to re-enter the autohide window when moved off screen,
 * which, coupled with widget_remap() below, can cause the panel to flicker.
 */
void
panel_utils_wl_surface_commit (GtkWidget *widget)
{
#ifdef ENABLE_WAYLAND
  GdkWindow *window = gtk_widget_get_window (widget);
  if (window != NULL)
    {
      /* yes, it can be null when the window is not */
      struct wl_surface *wl_surface = gdk_wayland_window_get_wl_surface (window);
      if (wl_surface != NULL)
        wl_surface_commit (wl_surface);
    }
#endif
}



/*
 * Like wl_surface_commit() above: use sparingly. It is about forcing GTK and/or the
 * compositor to take into account a request (resizing, layer change), but this can
 * have side effects.
 */
void
panel_utils_widget_remap (GtkWidget *widget)
{
  if (gtk_widget_get_visible (widget))
    {
      gtk_widget_hide (GTK_WIDGET (widget));
      gtk_widget_show (GTK_WIDGET (widget));
    }
}



static void
panel_utils_gtk_dialog_find_label_by_text_cb (GtkWidget *widget,
                                              gpointer data)
{
  PanelUtilsGtkLabelData *label_data = data;

  panel_return_if_fail (widget);
  panel_return_if_fail (label_data && label_data->label_text);

  if (GTK_IS_LABEL (widget) && g_strcmp0 (label_data->label_text, gtk_label_get_text (GTK_LABEL (widget))) == 0)
    {
      if (label_data->label)
        g_warning ("%s: Found multiple labels with text value '%s'", G_STRFUNC, label_data->label_text);
      else
        label_data->label = GTK_LABEL (widget);
    }
  else if (GTK_IS_BOX (widget))
    gtk_container_foreach (GTK_CONTAINER (widget), panel_utils_gtk_dialog_find_label_by_text_cb, data);
}



/*
 * Recursively searches the given GtkDialog and obtains a GtkLabel that has the given text.
 */
GtkLabel *
panel_utils_gtk_dialog_find_label_by_text (GtkDialog *dialog,
                                           const gchar *label_text)
{
  PanelUtilsGtkLabelData *label_data;
  GtkLabel *label;

  panel_return_val_if_fail (GTK_IS_DIALOG (dialog), NULL);

  label_data = g_new0 (PanelUtilsGtkLabelData, 1);
  label_data->label_text = label_text;
  label_data->label = NULL;

  gtk_container_foreach (GTK_CONTAINER (dialog), panel_utils_gtk_dialog_find_label_by_text_cb, label_data);
  if (label_data->label == NULL)
    g_warning ("%s: Could not find a label with the given text '%s'", G_STRFUNC, label_text);

  label = label_data->label;
  g_free (label_data);
  return label;
}



gint
panel_utils_compare_xfw_gdk_monitors (gconstpointer a,
                                      gconstpointer b)
{
  return xfw_monitor_get_gdk_monitor ((XfwMonitor *) a) == b ? 0 : 1;
}



GdkMonitor *
panel_utils_get_monitor_at_widget (GtkWidget *widget)
{
  GdkDisplay *display = gdk_display_get_default ();
  GdkWindow *window = gtk_widget_get_window (widget);
  if (window != NULL)
    return gdk_display_get_monitor_at_window (display, window);

  return gdk_display_get_monitor (display, 0);
}



GList *
panel_utils_list_workspace_groups_for_monitor (XfwScreen *xfw_screen,
                                               GdkMonitor *monitor)
{
  XfwWorkspaceManager *manager = xfw_screen_get_workspace_manager (xfw_screen);
  GList *groups = NULL;
  for (GList *lp = xfw_workspace_manager_list_workspace_groups (manager); lp != NULL; lp = lp->next)
    if (g_list_find_custom (xfw_workspace_group_get_monitors (lp->data), monitor, panel_utils_compare_xfw_gdk_monitors))
      groups = g_list_prepend (groups, lp->data);

  return g_list_reverse (groups);
}



GList *
panel_utils_list_workspaces_for_monitor (XfwScreen *xfw_screen,
                                         GdkMonitor *monitor)
{
  GList *groups = panel_utils_list_workspace_groups_for_monitor (xfw_screen, monitor);
  GList *workspaces = NULL;
  for (GList *lp = groups; lp != NULL; lp = lp->next)
    for (GList *lq = xfw_workspace_group_list_workspaces (lp->data); lq != NULL; lq = lq->next)
      workspaces = g_list_prepend (workspaces, lq->data);

  g_list_free (groups);
  return g_list_reverse (workspaces);
}



XfwWorkspace *
panel_utils_get_active_workspace_for_monitor (XfwScreen *xfw_screen,
                                              GdkMonitor *monitor)
{
  GList *groups = panel_utils_list_workspace_groups_for_monitor (xfw_screen, monitor);
  XfwWorkspace *workspace = NULL;
  for (GList *lp = groups; lp != NULL; lp = lp->next)
    {
      workspace = xfw_workspace_group_get_active_workspace (lp->data);
      if (workspace != NULL)
        break;
    }

  g_list_free (groups);
  return workspace;
}



guint
panel_utils_get_workspace_count_for_monitor (XfwScreen *xfw_screen,
                                             GdkMonitor *monitor)
{
  GList *groups = panel_utils_list_workspace_groups_for_monitor (xfw_screen, monitor);
  guint count = 0;
  for (GList *lp = groups; lp != NULL; lp = lp->next)
    count += xfw_workspace_group_get_workspace_count (lp->data);

  g_list_free (groups);
  return count;
}



gint
panel_utils_get_workspace_number_for_monitor (XfwScreen *xfw_screen,
                                              GdkMonitor *monitor,
                                              XfwWorkspace *workspace)
{
  GList *workspaces = panel_utils_list_workspaces_for_monitor (xfw_screen, monitor);
  gint number = g_list_index (workspaces, workspace);

  g_list_free (workspaces);
  return number;
}