1
/* Copyright (c) 2012 Andrzej <ndrwrdck@gmail.com>
3
* This program is free software; you can redistribute it and/or modify
4
* it under the terms of the GNU General Public License as published by
5
* the Free Software Foundation; either version 2 of the License, or
6
* (at your option) any later version.
8
* This program is distributed in the hope that it will be useful,
9
* but WITHOUT ANY WARRANTY; without even the implied warranty of
10
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
* GNU Library General Public License for more details.
13
* You should have received a copy of the GNU General Public License
14
* along with this program; if not, write to the Free Software
15
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21
#include <libxfce4panel/libxfce4panel.h>
22
#include <libindicator/indicator-object.h>
24
#include "indicator-box.h"
25
#include "indicator-button.h"
27
static void xfce_indicator_box_finalize (GObject *object);
28
static void xfce_indicator_box_get_property (GObject *object,
32
static void xfce_indicator_box_set_property (GObject *object,
36
static void xfce_indicator_box_add (GtkContainer *container,
38
static void xfce_indicator_box_remove (GtkContainer *container,
40
static void xfce_indicator_box_forall (GtkContainer *container,
41
gboolean include_internals,
43
gpointer callback_data);
44
static GType xfce_indicator_box_child_type (GtkContainer *container);
45
static void xfce_indicator_box_size_request (GtkWidget *widget,
46
GtkRequisition *requisition);
47
static void xfce_indicator_box_size_allocate (GtkWidget *widget,
48
GtkAllocation *allocation);
49
static gint xfce_indicator_box_get_row_size (XfceIndicatorBox *box);
58
G_DEFINE_TYPE (XfceIndicatorBox, xfce_indicator_box, GTK_TYPE_CONTAINER)
61
xfce_indicator_box_class_init (XfceIndicatorBoxClass *klass)
63
GObjectClass *gobject_class;
64
GtkWidgetClass *gtkwidget_class;
65
GtkContainerClass *gtkcontainer_class;
67
gobject_class = G_OBJECT_CLASS (klass);
68
gobject_class->finalize = xfce_indicator_box_finalize;
69
gobject_class->get_property = xfce_indicator_box_get_property;
70
gobject_class->set_property = xfce_indicator_box_set_property;
72
gtkwidget_class = GTK_WIDGET_CLASS (klass);
73
gtkwidget_class->size_request = xfce_indicator_box_size_request;
74
gtkwidget_class->size_allocate = xfce_indicator_box_size_allocate;
76
gtkcontainer_class = GTK_CONTAINER_CLASS (klass);
77
gtkcontainer_class->add = xfce_indicator_box_add;
78
gtkcontainer_class->remove = xfce_indicator_box_remove;
79
gtkcontainer_class->forall = xfce_indicator_box_forall;
80
gtkcontainer_class->child_type = xfce_indicator_box_child_type;
82
g_object_class_install_property (gobject_class,
84
g_param_spec_uint ("icon-size-max",
89
EXO_PARAM_READWRITE));
95
xfce_indicator_box_init (XfceIndicatorBox *box)
97
GTK_WIDGET_SET_FLAGS (box, GTK_NO_WINDOW);
99
gtk_widget_set_can_focus(GTK_WIDGET(box), TRUE);
100
gtk_container_set_border_width(GTK_CONTAINER(box), 0);
102
box->children = NULL;
105
box->icon_size_max = 24;
106
box->panel_size = 16;
107
box->panel_orientation = GTK_ORIENTATION_HORIZONTAL;
108
box->orientation = GTK_ORIENTATION_HORIZONTAL;
114
xfce_indicator_box_finalize (GObject *object)
116
XfceIndicatorBox *box = XFCE_INDICATOR_BOX (object);
118
if (box->children != NULL)
120
g_slist_free (box->children);
121
g_debug ("Not all icons have been removed from the indicator icon box.");
124
G_OBJECT_CLASS (xfce_indicator_box_parent_class)->finalize (object);
130
xfce_indicator_box_get_property (GObject *object,
135
XfceIndicatorBox *box = XFCE_INDICATOR_BOX (object);
140
case PROP_ICON_SIZE_MAX:
141
g_value_set_uint (value, box->icon_size_max);
145
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
153
xfce_indicator_box_set_property (GObject *object,
158
XfceIndicatorBox *box = XFCE_INDICATOR_BOX (object);
161
XfceIndicatorButton *child;
166
case PROP_ICON_SIZE_MAX:
167
val = g_value_get_uint (value);
168
if (box->icon_size_max != val)
170
box->icon_size_max = val;
171
size = xfce_indicator_box_get_row_size (box);
172
for (li = box->children; li != NULL; li = li->next)
174
child = XFCE_INDICATOR_BUTTON (li->data);
175
g_return_if_fail (XFCE_IS_INDICATOR_BUTTON (child));
176
xfce_indicator_button_set_size (child, box->panel_size, size);
178
gtk_widget_queue_resize (GTK_WIDGET (box));
183
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
191
xfce_indicator_box_set_orientation (XfceIndicatorBox *box,
192
GtkOrientation panel_orientation,
193
GtkOrientation orientation)
195
gboolean needs_update = FALSE;
196
XfceIndicatorButton *child;
199
g_return_if_fail (XFCE_IS_INDICATOR_BOX (box));
201
if (box->orientation != orientation)
203
box->orientation = orientation;
207
if (box->panel_orientation != panel_orientation)
209
box->panel_orientation = panel_orientation;
215
for (li = box->children; li != NULL; li = li->next)
217
child = XFCE_INDICATOR_BUTTON (li->data);
218
g_return_if_fail (XFCE_IS_INDICATOR_BUTTON (child));
219
xfce_indicator_button_set_orientation (child, panel_orientation, orientation);
221
gtk_widget_queue_resize (GTK_WIDGET (box));
228
xfce_indicator_box_set_size (XfceIndicatorBox *box,
232
gboolean needs_update = FALSE;
233
XfceIndicatorButton *child;
237
g_return_if_fail (XFCE_IS_INDICATOR_BOX (box));
239
if (box->nrows != nrows)
245
if (box->panel_size != panel_size)
247
box->panel_size = panel_size;
253
size = xfce_indicator_box_get_row_size (box);
254
for (li = box->children; li != NULL; li = li->next)
256
child = XFCE_INDICATOR_BUTTON (li->data);
257
g_return_if_fail (XFCE_IS_INDICATOR_BUTTON (child));
258
xfce_indicator_button_set_size (child, panel_size, size);
260
gtk_widget_queue_resize (GTK_WIDGET (box));
267
xfce_indicator_box_new ()
269
XfceIndicatorBox *box = g_object_new (XFCE_TYPE_INDICATOR_BOX, NULL);
270
return GTK_WIDGET (box);
276
xfce_indicator_box_add (GtkContainer *container,
279
XfceIndicatorBox *box = XFCE_INDICATOR_BOX (container);
280
XfceIndicatorButton *button = XFCE_INDICATOR_BUTTON (child);
283
g_return_if_fail (XFCE_IS_INDICATOR_BOX (box));
284
g_return_if_fail (GTK_IS_WIDGET (child));
285
g_return_if_fail (child->parent == NULL);
287
box->children = g_slist_append (box->children, child);
289
gtk_widget_set_parent (child, GTK_WIDGET (box));
290
xfce_indicator_button_set_orientation (button, box->panel_orientation, box->orientation);
291
size = xfce_indicator_box_get_row_size (box);
292
xfce_indicator_button_set_size (button, box->panel_size, size);
294
gtk_widget_queue_resize (GTK_WIDGET (container));
300
xfce_indicator_box_remove (GtkContainer *container,
303
XfceIndicatorBox *box = XFCE_INDICATOR_BOX (container);
306
/* search the child */
307
li = g_slist_find (box->children, child);
308
if (G_LIKELY (li != NULL))
310
g_assert (GTK_WIDGET (li->data) == child);
312
/* unparent widget */
313
box->children = g_slist_remove_link (box->children, li);
314
gtk_widget_unparent (child);
316
/* resize, so we update has-hidden */
317
gtk_widget_queue_resize (GTK_WIDGET (container));
324
xfce_indicator_box_forall (GtkContainer *container,
325
gboolean include_internals,
326
GtkCallback callback,
327
gpointer callback_data)
329
XfceIndicatorBox *box = XFCE_INDICATOR_BOX (container);
332
/* run callback for all children */
333
for (li = box->children; li != NULL; li = lnext)
336
(*callback) (GTK_WIDGET (li->data), callback_data);
343
xfce_indicator_box_child_type (GtkContainer *container)
345
return GTK_TYPE_WIDGET;
351
xfce_indicator_box_size_request (GtkWidget *widget,
352
GtkRequisition *requisition)
354
XfceIndicatorBox *box = XFCE_INDICATOR_BOX (widget);
356
GtkRequisition child_req;
365
panel_size = box->panel_size;
369
//nrows = MAX (box->nrows,
370
// box->panel_size / xfce_indicator_box_get_row_size (box));
371
nrows = box->panel_size / xfce_indicator_box_get_row_size (box);
373
for (li = box->children; li != NULL; li = li->next)
375
child = GTK_WIDGET (li->data);
376
g_return_if_fail (XFCE_IS_INDICATOR_BUTTON (child));
378
gtk_widget_size_request (child, &child_req);
379
has_label = (xfce_indicator_button_get_label (XFCE_INDICATOR_BUTTON (child)) != NULL);
381
/* wrap rows if column is overflowing or a label is encountered */
382
if (row > 0 && (has_label || row >= nrows))
390
MAX (length, (box->panel_orientation == GTK_ORIENTATION_HORIZONTAL) ? child_req.width :child_req.height);
392
if (has_label || row >= nrows)
406
if (box->panel_orientation == GTK_ORIENTATION_HORIZONTAL)
408
requisition->width = x;
409
requisition->height = panel_size;
413
requisition->width = panel_size;
414
requisition->height = x;
416
/* g_debug ("indicator-box size request: w=%d h=%d", requisition->width, requisition->height); */
422
xfce_indicator_box_size_allocate (GtkWidget *widget,
423
GtkAllocation *allocation)
425
XfceIndicatorBox *box = XFCE_INDICATOR_BOX (widget);
427
GtkAllocation child_alloc;
428
GtkRequisition child_req;
429
gint panel_size, size;
445
//nrows = MAX (box->nrows, box->panel_size / box->icon_size_max);
446
nrows = box->panel_size / xfce_indicator_box_get_row_size (box);
447
panel_size = box->panel_size;
448
size = panel_size / nrows;
450
for (li = box->children; li != NULL; li = li->next)
452
child = GTK_WIDGET (li->data);
453
g_return_if_fail (XFCE_IS_INDICATOR_BUTTON (child));
455
gtk_widget_get_child_requisition (child, &child_req);
457
has_label = (xfce_indicator_button_get_label (XFCE_INDICATOR_BUTTON (child)) != NULL);
459
/* wrap rows if column is overflowing or a label is encountered */
460
if (row > 0 && (has_label || row >= nrows))
468
width = (has_label) ? panel_size : size;
469
length = MAX (length,
470
(box->panel_orientation == GTK_ORIENTATION_HORIZONTAL) ? child_req.width :child_req.height);
472
if (box->panel_orientation == GTK_ORIENTATION_HORIZONTAL)
474
child_alloc.x = x0 + x;
475
child_alloc.y = y0 + y;
476
child_alloc.width = length;
477
child_alloc.height = width;
481
child_alloc.x = x0 + y;
482
child_alloc.y = y0 + x;
483
child_alloc.width = width;
484
child_alloc.height = length;
487
/* g_debug ("indicator-box size allocate: x=%d y=%d w=%d h=%d", */
488
/* child_alloc.x, child_alloc.y, child_alloc.width, child_alloc.height); */
490
gtk_widget_size_allocate (child, &child_alloc);
492
if (has_label || row >= nrows)
510
xfce_indicator_box_get_row_size (XfceIndicatorBox *box)
512
gint border_thickness;
515
g_return_val_if_fail (XFCE_IS_INDICATOR_BOX (box), 24);
517
style = gtk_widget_get_style (GTK_WIDGET (box));
518
border_thickness = 2 * MAX (style->xthickness, style->ythickness) + 2;
520
return MIN (box->panel_size / box->nrows, box->icon_size_max + border_thickness);