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
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
|
/*
* Copyright (C) 2002 Red Hat, Inc.
* Copyright (C) 2003-2006 Vincent Untz
* Copyright (C) 2007 Christian Persch
* Copyright (C) 2017 Colomban Wendling <cwendling@hypra.fr>
*
* 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 St, Fifth Floor, Boston, MA
* 02110-1301, USA.
*/
/* Well, actuall'y is the Tray itself, the container for the items. But
* NaTray is already taken for the XEMBED part, so for now it's called NaGrid,
* don't make a big deal out of it. */
#include <config.h>
#include <gtk/gtk.h>
#include "na-grid.h"
#include "system-tray/na-tray.h"
#include "status-notifier/sn-host-v0.h"
#define MIN_ICON_SIZE_DEFAULT 24
typedef struct
{
GtkOrientation orientation;
gint index;
NaGrid *grid;
} SortData;
struct _NaGrid
{
GtkGrid parent;
gint icon_padding;
gint icon_size;
gint min_icon_size;
gint cols;
gint rows;
gint length;
GSList *hosts;
GSList *items;
};
enum
{
PROP_0,
PROP_ICON_PADDING,
PROP_ICON_SIZE
};
G_DEFINE_TYPE (NaGrid, na_grid, GTK_TYPE_GRID)
static gint
compare_items (gconstpointer a,
gconstpointer b)
{
NaItem *item1;
NaItem *item2;
NaItemCategory c1;
NaItemCategory c2;
const gchar *id1;
const gchar *id2;
item1 = (NaItem *) a;
item2 = (NaItem *) b;
c1 = na_item_get_category (item1);
c2 = na_item_get_category (item2);
if (c1 < c2)
return -1;
else if (c1 > c2)
return 1;
id1 = na_item_get_id (item1);
id2 = na_item_get_id (item2);
return g_strcmp0 (id1, id2);
}
static void
sort_items (GtkWidget *item,
SortData *data)
{
gint col, row, left_attach, top_attach;
/* row / col number depends on whether we are horizontal or vertical */
if (data->orientation == GTK_ORIENTATION_HORIZONTAL)
{
col = data->index / data->grid->rows;
row = data->index % data->grid->rows;
}
else
{
row = data->index / data->grid->cols;
col = data->index % data->grid->cols;
}
/* only update item position if it has changed from current */
gtk_container_child_get (GTK_CONTAINER (data->grid),
item,
"left-attach", &left_attach,
"top-attach", &top_attach,
NULL);
if (left_attach != col || top_attach != row)
{
gtk_container_child_set (GTK_CONTAINER (data->grid),
item,
"left-attach", col,
"top-attach", row,
NULL);
}
/* increment to index of next item */
data->index++;
}
static void
refresh_grid (NaGrid *self)
{
GtkOrientation orientation;
GtkAllocation allocation;
gint rows, cols, length;
orientation = gtk_orientable_get_orientation (GTK_ORIENTABLE (self));
gtk_widget_get_allocation (GTK_WIDGET (self), &allocation);
length = g_slist_length (self->items);
if (orientation == GTK_ORIENTATION_HORIZONTAL)
{
gtk_grid_set_row_homogeneous (GTK_GRID (self), TRUE);
gtk_grid_set_column_homogeneous (GTK_GRID (self), FALSE);
rows = MAX (1, allocation.height / self->min_icon_size);
cols = MAX (1, length / rows);
if (length % rows)
cols++;
}
else
{
gtk_grid_set_row_homogeneous (GTK_GRID (self), FALSE);
gtk_grid_set_column_homogeneous (GTK_GRID (self), TRUE);
cols = MAX (1, allocation.width / self->min_icon_size);
rows = MAX (1, length / cols);
if (length % cols)
rows++;
}
if (self->cols != cols || self->rows != rows || self->length != length)
{
self->cols = cols;
self->rows = rows;
self->length = length;
SortData data;
data.orientation = gtk_orientable_get_orientation (GTK_ORIENTABLE (self));
data.index = 0;
data.grid = self;
g_slist_foreach (self->items,
(GFunc) sort_items,
&data);
}
}
void
na_grid_set_min_icon_size (NaGrid *grid,
gint min_icon_size)
{
g_return_if_fail (NA_IS_GRID (grid));
grid->min_icon_size = min_icon_size;
refresh_grid (grid);
}
static void
item_added_cb (NaHost *host,
NaItem *item,
NaGrid *self)
{
g_return_if_fail (NA_IS_HOST (host));
g_return_if_fail (NA_IS_ITEM (item));
g_return_if_fail (NA_IS_GRID (self));
g_object_bind_property (self, "orientation",
item, "orientation",
G_BINDING_SYNC_CREATE);
self->items = g_slist_prepend (self->items, item);
gtk_widget_set_hexpand (GTK_WIDGET (item), TRUE);
gtk_widget_set_vexpand (GTK_WIDGET (item), TRUE);
gtk_grid_attach (GTK_GRID (self),
GTK_WIDGET (item),
self->cols - 1,
self->rows - 1,
1, 1);
self->items = g_slist_sort (self->items, compare_items);
refresh_grid (self);
}
static void
item_removed_cb (NaHost *host,
NaItem *item,
NaGrid *self)
{
g_return_if_fail (NA_IS_HOST (host));
g_return_if_fail (NA_IS_ITEM (item));
g_return_if_fail (NA_IS_GRID (self));
gtk_container_remove (GTK_CONTAINER (self), GTK_WIDGET (item));
self->items = g_slist_remove (self->items, item);
refresh_grid (self);
}
static void
na_grid_init (NaGrid *self)
{
self->icon_padding = 0;
self->icon_size = 0;
self->min_icon_size = MIN_ICON_SIZE_DEFAULT;
self->cols = 1;
self->rows = 1;
self->length = 0;
self->hosts = NULL;
self->items = NULL;
gtk_grid_set_row_homogeneous (GTK_GRID (self), TRUE);
gtk_grid_set_column_homogeneous (GTK_GRID (self), TRUE);
}
static void
add_host (NaGrid *self,
NaHost *host)
{
self->hosts = g_slist_prepend (self->hosts, host);
g_object_bind_property (self, "icon-padding", host, "icon-padding",
G_BINDING_DEFAULT | G_BINDING_SYNC_CREATE);
g_object_bind_property (self, "icon-size", host, "icon-size",
G_BINDING_DEFAULT | G_BINDING_SYNC_CREATE);
g_signal_connect_object (host, "item-added",
G_CALLBACK (item_added_cb), self, 0);
g_signal_connect_object (host, "item-removed",
G_CALLBACK (item_removed_cb), self, 0);
}
static void
na_grid_style_updated (GtkWidget *widget)
{
NaGrid *self = NA_GRID (widget);
GtkStyleContext *context;
GSList *node;
if (GTK_WIDGET_CLASS (na_grid_parent_class)->style_updated)
GTK_WIDGET_CLASS (na_grid_parent_class)->style_updated (widget);
context = gtk_widget_get_style_context (widget);
for (node = self->hosts; node; node = node->next)
{
gtk_style_context_save (context);
na_host_style_updated (node->data, context);
gtk_style_context_restore (context);
}
}
/* Custom drawing because system-tray items need weird stuff. */
static gboolean
na_grid_draw (GtkWidget *grid,
cairo_t *cr)
{
GList *child;
GList *children = gtk_container_get_children (GTK_CONTAINER (grid));
for (child = children; child; child = child->next)
{
if (! NA_IS_ITEM (child->data) ||
! na_item_draw_on_parent (child->data, grid, cr))
{
if (gtk_widget_is_drawable (child->data) &&
gtk_cairo_should_draw_window (cr, gtk_widget_get_window (child->data)))
gtk_container_propagate_draw (GTK_CONTAINER (grid), child->data, cr);
}
}
g_list_free (children);
return TRUE;
}
static void
na_grid_realize (GtkWidget *widget)
{
NaGrid *self = NA_GRID (widget);
GdkScreen *screen;
GtkOrientation orientation;
NaHost *tray_host;
GTK_WIDGET_CLASS (na_grid_parent_class)->realize (widget);
/* Instantiate the hosts now we have a screen */
screen = gtk_widget_get_screen (GTK_WIDGET (self));
orientation = gtk_orientable_get_orientation (GTK_ORIENTABLE (self));
tray_host = na_tray_new_for_screen (screen, orientation);
g_object_bind_property (self, "orientation",
tray_host, "orientation",
G_BINDING_DEFAULT);
add_host (self, tray_host);
add_host (self, sn_host_v0_new ());
}
static void
na_grid_unrealize (GtkWidget *widget)
{
NaGrid *self = NA_GRID (widget);
if (self->hosts != NULL)
{
g_slist_free_full (self->hosts, g_object_unref);
self->hosts = NULL;
}
g_clear_pointer (&self->items, g_slist_free);
GTK_WIDGET_CLASS (na_grid_parent_class)->unrealize (widget);
}
static void
na_grid_size_allocate (GtkWidget *widget,
GtkAllocation *allocation)
{
GTK_WIDGET_CLASS (na_grid_parent_class)->size_allocate (widget, allocation);
refresh_grid (NA_GRID (widget));
}
static void
na_grid_get_property (GObject *object,
guint property_id,
GValue *value,
GParamSpec *pspec)
{
NaGrid *self = NA_GRID (object);
switch (property_id)
{
case PROP_ICON_PADDING:
g_value_set_int (value, self->icon_padding);
break;
case PROP_ICON_SIZE:
g_value_set_int (value, self->icon_size);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
break;
}
}
static void
na_grid_set_property (GObject *object,
guint property_id,
const GValue *value,
GParamSpec *pspec)
{
NaGrid *self = NA_GRID (object);
switch (property_id)
{
case PROP_ICON_PADDING:
self->icon_padding = g_value_get_int (value);
break;
case PROP_ICON_SIZE:
self->icon_size = g_value_get_int (value);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
break;
}
}
static void
na_grid_class_init (NaGridClass *klass)
{
GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
gobject_class->get_property = na_grid_get_property;
gobject_class->set_property = na_grid_set_property;
widget_class->draw = na_grid_draw;
widget_class->realize = na_grid_realize;
widget_class->unrealize = na_grid_unrealize;
widget_class->style_updated = na_grid_style_updated;
widget_class->size_allocate = na_grid_size_allocate;
g_object_class_install_property (gobject_class, PROP_ICON_PADDING,
g_param_spec_int ("icon-padding",
"Padding around icons",
"Padding that should be put around icons, in pixels",
0, G_MAXINT, 0,
G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
g_object_class_install_property (gobject_class, PROP_ICON_SIZE,
g_param_spec_int ("icon-size",
"Icon size",
"If non-zero, hardcodes the size of the icons in pixels",
0, G_MAXINT, 0,
G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
}
GtkWidget *
na_grid_new (GtkOrientation orientation)
{
return g_object_new (NA_TYPE_GRID,
"orientation", orientation,
NULL);
}
void
na_grid_force_redraw (NaGrid *grid)
{
GSList *node;
for (node = grid->hosts; node; node = node->next)
na_host_force_redraw (node->data);
}
|