File: separator.c

package info (click to toggle)
xfce4-panel 4.20.6-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 17,212 kB
  • sloc: ansic: 61,588; sh: 5,515; makefile: 1,330; python: 128; xml: 101; sed: 16
file content (392 lines) | stat: -rw-r--r-- 12,259 bytes parent folder | download | duplicates (2)
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
/*
 * Copyright (c) 2005-2007 Jasper Huijsmans <jasper@xfce.org>
 * Copyright (C) 2007-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 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 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 "separator.h"

#include "common/panel-private.h"
#include "common/panel-utils.h"
#include "common/panel-xfconf.h"
#include "libxfce4panel/xfce-panel-plugin-provider.h"

#include <gtk/gtk.h>
#include <libxfce4ui/libxfce4ui.h>


#define SEPARATOR_OFFSET (0.15)
#define SEPARATOR_SIZE (8)
#define DOTS_OFFSET (4)
#define DOTS_SIZE (3)
#define HANDLE_SIZE (4)



static void
separator_plugin_get_property (GObject *object,
                               guint prop_id,
                               GValue *value,
                               GParamSpec *pspec);
static void
separator_plugin_set_property (GObject *object,
                               guint prop_id,
                               const GValue *value,
                               GParamSpec *pspec);
static gboolean
separator_plugin_draw (GtkWidget *widget,
                       cairo_t *cr);
static void
separator_plugin_construct (XfcePanelPlugin *panel_plugin);
static gboolean
separator_plugin_size_changed (XfcePanelPlugin *panel_plugin,
                               gint size);
static void
separator_plugin_configure_plugin (XfcePanelPlugin *panel_plugin);
static void
separator_plugin_orientation_changed (XfcePanelPlugin *panel_plugin,
                                      GtkOrientation orientation);



typedef enum _SeparatorPluginStyle
{
  SEPARATOR_PLUGIN_STYLE_TRANSPARENT = 0,
  SEPARATOR_PLUGIN_STYLE_SEPARATOR,
  SEPARATOR_PLUGIN_STYLE_HANDLE,
  SEPARATOR_PLUGIN_STYLE_DOTS,
  SEPARATOR_PLUGIN_STYLE_WRAP, /* not used in 4.10, nrows property is now used */

  /* defines */
  SEPARATOR_PLUGIN_STYLE_MIN = SEPARATOR_PLUGIN_STYLE_TRANSPARENT,
  SEPARATOR_PLUGIN_STYLE_MAX = SEPARATOR_PLUGIN_STYLE_WRAP,
  SEPARATOR_PLUGIN_STYLE_DEFAULT = SEPARATOR_PLUGIN_STYLE_SEPARATOR
} SeparatorPluginStyle;

struct _SeparatorPlugin
{
  /* parent type */
  XfcePanelPlugin __parent__;

  /* separator style */
  SeparatorPluginStyle style;
};

enum
{
  PROP_0,
  PROP_STYLE,
  PROP_EXPAND
};



/* define the plugin */
XFCE_PANEL_DEFINE_PLUGIN (SeparatorPlugin, separator_plugin)



static void
separator_plugin_class_init (SeparatorPluginClass *klass)
{
  XfcePanelPluginClass *plugin_class;
  GObjectClass *gobject_class;
  GtkWidgetClass *widget_class;

  gobject_class = G_OBJECT_CLASS (klass);
  gobject_class->set_property = separator_plugin_set_property;
  gobject_class->get_property = separator_plugin_get_property;

  widget_class = GTK_WIDGET_CLASS (klass);
  widget_class->draw = separator_plugin_draw;

  plugin_class = XFCE_PANEL_PLUGIN_CLASS (klass);
  plugin_class->construct = separator_plugin_construct;
  plugin_class->size_changed = separator_plugin_size_changed;
  plugin_class->configure_plugin = separator_plugin_configure_plugin;
  plugin_class->orientation_changed = separator_plugin_orientation_changed;

  g_object_class_install_property (gobject_class,
                                   PROP_STYLE,
                                   g_param_spec_uint ("style",
                                                      NULL, NULL,
                                                      SEPARATOR_PLUGIN_STYLE_MIN,
                                                      SEPARATOR_PLUGIN_STYLE_MAX,
                                                      SEPARATOR_PLUGIN_STYLE_DEFAULT,
                                                      G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));

  g_object_class_install_property (gobject_class,
                                   PROP_EXPAND,
                                   g_param_spec_boolean ("expand",
                                                         NULL, NULL,
                                                         FALSE,
                                                         G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
}



static void
separator_plugin_init (SeparatorPlugin *plugin)
{
  plugin->style = SEPARATOR_PLUGIN_STYLE_DEFAULT;
}



static void
separator_plugin_get_property (GObject *object,
                               guint prop_id,
                               GValue *value,
                               GParamSpec *pspec)
{
  SeparatorPlugin *plugin = SEPARATOR_PLUGIN (object);
  gboolean expand;

  switch (prop_id)
    {
    case PROP_STYLE:
      g_value_set_uint (value, plugin->style);
      break;

    case PROP_EXPAND:
      expand = xfce_panel_plugin_get_expand (XFCE_PANEL_PLUGIN (plugin));
      g_value_set_boolean (value, expand);
      break;

    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
    }
}



static void
separator_plugin_set_property (GObject *object,
                               guint prop_id,
                               const GValue *value,
                               GParamSpec *pspec)
{
  SeparatorPlugin *plugin = SEPARATOR_PLUGIN (object);

  switch (prop_id)
    {
    case PROP_STYLE:
      plugin->style = g_value_get_uint (value);

      /* old property */
      if (plugin->style == SEPARATOR_PLUGIN_STYLE_WRAP)
        plugin->style = SEPARATOR_PLUGIN_STYLE_DEFAULT;

      gtk_widget_queue_draw (GTK_WIDGET (object));
      break;

    case PROP_EXPAND:
      xfce_panel_plugin_set_expand (XFCE_PANEL_PLUGIN (plugin),
                                    g_value_get_boolean (value));
      break;

    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
    }
}



static gboolean
separator_plugin_draw (GtkWidget *widget,
                       cairo_t *cr)
{
  SeparatorPlugin *plugin = SEPARATOR_PLUGIN (widget);
  GtkAllocation alloc;
  gdouble x, y;
  guint dotcount, i;
  GtkStyleContext *ctx;
  GdkRGBA fg_rgba;

  gtk_widget_get_allocation (widget, &alloc);

  ctx = gtk_widget_get_style_context (widget);
  gtk_style_context_get_color (ctx, gtk_widget_get_state_flags (widget), &fg_rgba);
  /* Tone down the foreground color a bit for the separators */
  fg_rgba.alpha = 0.5;
  gdk_cairo_set_source_rgba (cr, &fg_rgba);

  switch (plugin->style)
    {
    case SEPARATOR_PLUGIN_STYLE_TRANSPARENT:
    case SEPARATOR_PLUGIN_STYLE_WRAP:
      /* do nothing */
      break;

    case SEPARATOR_PLUGIN_STYLE_SEPARATOR:

      if (xfce_panel_plugin_get_orientation (XFCE_PANEL_PLUGIN (plugin)) == GTK_ORIENTATION_HORIZONTAL)
        {
          gtk_render_line (ctx, cr,
                           (gdouble) (alloc.width - 1.0) / 2.0,
                           (gdouble) alloc.height * SEPARATOR_OFFSET,
                           (gdouble) (alloc.width - 1.0) / 2.0,
                           (gdouble) alloc.height * (1.0 - SEPARATOR_OFFSET));
        }
      else
        {
          gtk_render_line (ctx, cr,
                           (gdouble) alloc.width * SEPARATOR_OFFSET,
                           (gdouble) (alloc.height - 1.0) / 2.0,
                           (gdouble) alloc.width * (1.0 - SEPARATOR_OFFSET),
                           (gdouble) (alloc.height - 1.0) / 2.0);
        }
      break;

    case SEPARATOR_PLUGIN_STYLE_HANDLE:
      x = (alloc.width - HANDLE_SIZE) / 2;
      y = (alloc.height - HANDLE_SIZE) / 2;
      cairo_set_line_width (cr, 1.5);
      /* draw the handle */
      for (i = 0; i < 3; i++)
        {
          if (xfce_panel_plugin_get_orientation (XFCE_PANEL_PLUGIN (plugin)) == GTK_ORIENTATION_HORIZONTAL)
            {
              cairo_move_to (cr, x, y + (i * HANDLE_SIZE) - (HANDLE_SIZE / 2));
              cairo_line_to (cr, x + HANDLE_SIZE, y + (i * HANDLE_SIZE) - (HANDLE_SIZE / 2));
            }
          else
            {
              cairo_move_to (cr, x + (i * HANDLE_SIZE) - (HANDLE_SIZE / 2), y);
              cairo_line_to (cr, x + (i * HANDLE_SIZE) - (HANDLE_SIZE / 2), y + HANDLE_SIZE);
            }
          cairo_stroke (cr);
        }
      break;

    case SEPARATOR_PLUGIN_STYLE_DOTS:
      x = (alloc.width - DOTS_SIZE) / 2;
      y = (alloc.height - DOTS_SIZE) / 2;
      if (xfce_panel_plugin_get_orientation (XFCE_PANEL_PLUGIN (plugin)) == GTK_ORIENTATION_HORIZONTAL)
        {
          dotcount = MAX (alloc.height / (DOTS_SIZE + DOTS_OFFSET), 1);
          y = (alloc.height / (double) dotcount - DOTS_SIZE) / 2;
        }
      else
        {
          dotcount = MAX (alloc.width / (DOTS_SIZE + DOTS_OFFSET), 1);
          x = (alloc.width / (double) dotcount - DOTS_SIZE) / 2;
        }

      /* draw the dots */
      for (i = 0; i < dotcount; i++)
        {
          if (xfce_panel_plugin_get_orientation (XFCE_PANEL_PLUGIN (plugin)) == GTK_ORIENTATION_HORIZONTAL)
            cairo_arc (cr, x, y + (i * (alloc.height / (double) dotcount)) + (DOTS_SIZE / 2),
                       DOTS_SIZE / 2, 0, 2 * 3.14);
          else
            cairo_arc (cr, x + (i * (alloc.width / (double) dotcount)) + (DOTS_SIZE / 2), y,
                       DOTS_SIZE / 2, 0, 2 * 3.14);
          cairo_fill (cr);
        }
      break;
    }

  return FALSE;
}



static void
separator_plugin_construct (XfcePanelPlugin *panel_plugin)
{
  SeparatorPlugin *plugin = SEPARATOR_PLUGIN (panel_plugin);
  const PanelProperty properties[] = {
    { "style", G_TYPE_UINT },
    { "expand", G_TYPE_BOOLEAN },
    { NULL }
  };

  /* show the properties dialog */
  xfce_panel_plugin_menu_show_configure (XFCE_PANEL_PLUGIN (plugin));

  /* connect all properties */
  panel_properties_bind (NULL, G_OBJECT (plugin),
                         xfce_panel_plugin_get_property_base (panel_plugin),
                         properties, FALSE);

  /* make sure the plugin is drawn */
  gtk_widget_queue_draw (GTK_WIDGET (panel_plugin));
}



static gboolean
separator_plugin_size_changed (XfcePanelPlugin *panel_plugin,
                               gint size)
{
  /* set the minimum separator size */
  if (xfce_panel_plugin_get_orientation (panel_plugin) == GTK_ORIENTATION_HORIZONTAL)
    gtk_widget_set_size_request (GTK_WIDGET (panel_plugin),
                                 SEPARATOR_SIZE, size);
  else
    gtk_widget_set_size_request (GTK_WIDGET (panel_plugin),
                                 size, SEPARATOR_SIZE);

  return TRUE;
}



static void
separator_plugin_configure_plugin (XfcePanelPlugin *panel_plugin)
{
  SeparatorPlugin *plugin = SEPARATOR_PLUGIN (panel_plugin);
  GtkBuilder *builder;
  GObject *dialog;
  GObject *style, *expand;

  panel_return_if_fail (SEPARATOR_IS_PLUGIN (plugin));

  /* setup the dialog */
  builder = panel_utils_builder_new (panel_plugin, "/org/xfce/panel/separator-dialog.glade", &dialog);
  if (G_UNLIKELY (builder == NULL))
    return;

  style = gtk_builder_get_object (builder, "style");
  g_object_bind_property (G_OBJECT (plugin), "style",
                          G_OBJECT (style), "active",
                          G_BINDING_SYNC_CREATE | G_BINDING_BIDIRECTIONAL);

  expand = gtk_builder_get_object (builder, "expand");
  g_object_bind_property (G_OBJECT (plugin), "expand",
                          G_OBJECT (expand), "active",
                          G_BINDING_SYNC_CREATE | G_BINDING_BIDIRECTIONAL);

  gtk_widget_show (GTK_WIDGET (dialog));
}



static void
separator_plugin_orientation_changed (XfcePanelPlugin *panel_plugin,
                                      GtkOrientation orientation)
{
  /* for a size change to set the widget size request properly */
  separator_plugin_size_changed (panel_plugin,
                                 xfce_panel_plugin_get_size (panel_plugin));
}