File: wrapper-plug-x11.c

package info (click to toggle)
xfce4-panel 4.20.7-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 17,220 kB
  • sloc: ansic: 61,756; sh: 5,515; makefile: 1,330; python: 128; xml: 101; sed: 16
file content (279 lines) | stat: -rw-r--r-- 8,771 bytes parent folder | download | duplicates (3)
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
/*
 * Copyright (C) 2022 Gaƫl Bonithon <gael@xfce.org>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; 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 "wrapper-plug-x11.h"
#include "wrapper-plug.h"

#include "common/panel-private.h"



static void
wrapper_plug_x11_finalize (GObject *object);
static void
wrapper_plug_x11_iface_init (WrapperPlugInterface *iface);
static void
wrapper_plug_x11_child_size_allocate (GtkWidget *widget,
                                      GtkAllocation *allocation);
static void
wrapper_plug_x11_proxy_provider_signal (WrapperPlug *plug,
                                        XfcePanelPluginProviderSignal provider_signal,
                                        XfcePanelPluginProvider *provider);
static void
wrapper_plug_x11_proxy_remote_event_result (WrapperPlug *plug,
                                            guint handle,
                                            gboolean result);
static void
wrapper_plug_x11_set_background_color (WrapperPlug *plug,
                                       const gchar *color);
static void
wrapper_plug_x11_set_background_image (WrapperPlug *plug,
                                       const gchar *image);
static void
wrapper_plug_x11_set_geometry (WrapperPlug *plug,
                               const GdkRectangle *geometry);



struct _WrapperPlugX11
{
  GtkPlug __parent__;

  GDBusProxy *proxy;

  /* background information */
  GtkStyleProvider *style_provider;
  gchar *image;
};



G_DEFINE_FINAL_TYPE_WITH_CODE (WrapperPlugX11, wrapper_plug_x11, GTK_TYPE_PLUG,
                               G_IMPLEMENT_INTERFACE (WRAPPER_TYPE_PLUG,
                                                      wrapper_plug_x11_iface_init))



static void
wrapper_plug_x11_class_init (WrapperPlugX11Class *klass)
{
  GObjectClass *gobject_class;

  gobject_class = G_OBJECT_CLASS (klass);
  gobject_class->finalize = wrapper_plug_x11_finalize;

  /* GtkWidget::size-allocate is flagged G_SIGNAL_RUN_FIRST so we need to overwrite it */
  g_signal_override_class_handler ("size-allocate", XFCE_TYPE_PANEL_PLUGIN,
                                   G_CALLBACK (wrapper_plug_x11_child_size_allocate));
}



static void
wrapper_plug_x11_init (WrapperPlugX11 *plug)
{
  GdkVisual *visual = NULL;
  GdkScreen *screen;
  GtkStyleContext *context;

  gtk_widget_set_name (GTK_WIDGET (plug), "XfcePanelWindowWrapper");

  /* set the colormap */
  screen = gtk_window_get_screen (GTK_WINDOW (plug));
  visual = gdk_screen_get_rgba_visual (screen);
  if (visual != NULL)
    gtk_widget_set_visual (GTK_WIDGET (plug), visual);

  /* set the panel class */
  context = gtk_widget_get_style_context (GTK_WIDGET (plug));
  gtk_style_context_add_class (context, "panel");
  gtk_style_context_add_class (context, "xfce4-panel");

  gtk_drag_dest_unset (GTK_WIDGET (plug));

  /* add the style provider */
  plug->style_provider = GTK_STYLE_PROVIDER (gtk_css_provider_new ());

  gtk_style_context_add_provider (context, plug->style_provider,
                                  GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);
}



static void
wrapper_plug_x11_finalize (GObject *object)
{
  WrapperPlugX11 *plug = WRAPPER_PLUG_X11 (object);

  g_object_unref (plug->style_provider);
  g_free (plug->image);

  G_OBJECT_CLASS (wrapper_plug_x11_parent_class)->finalize (object);
}



static void
wrapper_plug_x11_child_size_allocate (GtkWidget *widget,
                                      GtkAllocation *allocation)
{
  GtkRequisition size;

  /* avoid allocation warnings */
  gtk_widget_get_preferred_size (widget, NULL, &size);
  if (allocation->width <= 1 && size.width > 1)
    allocation->width = size.width;
  if (allocation->height <= 1 && size.height > 1)
    allocation->height = size.height;

  g_signal_chain_from_overridden_handler (widget, allocation);
}



static void
wrapper_plug_x11_iface_init (WrapperPlugInterface *iface)
{
  iface->proxy_provider_signal = wrapper_plug_x11_proxy_provider_signal;
  iface->proxy_remote_event_result = wrapper_plug_x11_proxy_remote_event_result;
  iface->set_background_color = wrapper_plug_x11_set_background_color;
  iface->set_background_image = wrapper_plug_x11_set_background_image;
  iface->set_geometry = wrapper_plug_x11_set_geometry;
}



static void
wrapper_plug_x11_proxy_provider_signal (WrapperPlug *plug,
                                        XfcePanelPluginProviderSignal provider_signal,
                                        XfcePanelPluginProvider *provider)
{
  wrapper_plug_proxy_method_call_sync (WRAPPER_PLUG_X11 (plug)->proxy, "ProviderSignal",
                                       g_variant_new ("(u)", provider_signal));
}



static void
wrapper_plug_x11_proxy_remote_event_result (WrapperPlug *plug,
                                            guint handle,
                                            gboolean result)
{
  wrapper_plug_proxy_method_call_sync (WRAPPER_PLUG_X11 (plug)->proxy, "RemoteEventResult",
                                       g_variant_new ("(ub)", handle, result));
}



static void
wrapper_plug_x11_set_background_color (WrapperPlug *plug,
                                       const gchar *color)
{
  WrapperPlugX11 *xplug = WRAPPER_PLUG_X11 (plug);
  GdkRGBA rgba;
  gchar *css, *str;

  /* interpret NULL color as user requesting the system theme, so reset the css here */
  if (color == NULL)
    {
      gtk_css_provider_load_from_data (GTK_CSS_PROVIDER (xplug->style_provider), "", -1, NULL);
      g_free (xplug->image);
      xplug->image = NULL;
      return;
    }

  if (gdk_rgba_parse (&rgba, color))
    {
      str = gdk_rgba_to_string (&rgba);
      css = g_strdup_printf ("* { background: %s; }", str);
      gtk_css_provider_load_from_data (GTK_CSS_PROVIDER (xplug->style_provider), css, -1, NULL);
      g_free (css);
      g_free (str);
    }
}



static void
wrapper_plug_x11_set_background_image (WrapperPlug *plug,
                                       const gchar *image)
{
  WrapperPlugX11 *xplug = WRAPPER_PLUG_X11 (plug);
  GdkRectangle geom = { 0 };

  g_free (xplug->image);
  xplug->image = g_strdup (image);

  /*
   * Normally, socket geometry should always be retrieved in this way (or better still via
   * `gdk_window_get_position()`), but in practice this is only suitable for initialization.
   * This is because :
   * - The coordinates returned by this function may be outdated, and you'll need to mouse
   *   over the plugin to trigger a socket allocation that updates this data (reproducible,
   *   for example, by switching panel mode from vertical to deskbar).
   * - It's even worse with `gdk_window_get_position()`.
   * - The socket window scale factor is not updated when the UI scale factor is changed at
   *   runtime.
   * For all these reasons, sending geometry via D-Bus is simpler and more reliable.
   */
  gdk_window_get_geometry (gtk_plug_get_socket_window (GTK_PLUG (plug)), &geom.x, &geom.y, NULL, NULL);
  wrapper_plug_x11_set_geometry (plug, &geom);
}



static void
wrapper_plug_x11_set_geometry (WrapperPlug *plug,
                               const GdkRectangle *geometry)
{
  WrapperPlugX11 *xplug = WRAPPER_PLUG_X11 (plug);

  /* do not scale background image with the panel */
  gint scale_factor = gtk_widget_get_scale_factor (GTK_WIDGET (plug));
  gchar *css_url = g_strdup_printf ("url(\"%s\")", xplug->image);
  for (gint i = 1; i < scale_factor; i++)
    {
      gchar *temp = g_strdup_printf ("%s,url(\"%s\")", css_url, xplug->image);
      g_free (css_url);
      css_url = temp;
    }
  gchar *css = g_strdup_printf ("* { background: -gtk-scaled(%s) %dpx %dpx; }",
                                css_url, -geometry->x, -geometry->y);
  gtk_css_provider_load_from_data (GTK_CSS_PROVIDER (xplug->style_provider), css, -1, NULL);
  g_free (css);
  g_free (css_url);
}



GtkWidget *
wrapper_plug_x11_new (Window socket_id,
                      GDBusProxy *proxy)
{
  WrapperPlugX11 *plug;

  plug = g_object_new (WRAPPER_TYPE_PLUG_X11, NULL);
  gtk_plug_construct (GTK_PLUG (plug), socket_id);
  plug->proxy = proxy;

  return GTK_WIDGET (plug);
}