52
49
GParamSpec *pspec);
53
static void xfce_clock_binary_size_request (GtkWidget *widget,
54
GtkRequisition *requisition);
50
static void xfce_clock_binary_finalize (GObject *object);
55
51
static gboolean xfce_clock_binary_expose_event (GtkWidget *widget,
56
52
GdkEventExpose *event);
53
static gboolean xfce_clock_binary_update (gpointer user_data);
66
68
struct _XfceClockBinaryClass
68
GtkImageClass __parent__;
70
GtkImageClass __parent__;
71
73
struct _XfceClockBinary
75
/* whether we draw seconds */
76
guint show_seconds : 1;
78
/* if this is a true binary clock */
79
guint true_binary : 1;
77
ClockPluginTimeout *timeout;
79
guint show_seconds : 1;
80
guint true_binary : 1;
81
guint show_inactive : 1;
84
G_DEFINE_TYPE (XfceClockBinary, xfce_clock_binary, GTK_TYPE_IMAGE);
87
XFCE_PANEL_DEFINE_TYPE (XfceClockBinary, xfce_clock_binary, GTK_TYPE_IMAGE)
89
92
xfce_clock_binary_class_init (XfceClockBinaryClass *klass)
91
GObjectClass *gobject_class;
92
GtkWidgetClass *gtkwidget_class;
94
gobject_class = G_OBJECT_CLASS (klass);
95
gobject_class->finalize = xfce_clock_binary_finalize;
96
gobject_class->set_property = xfce_clock_binary_set_property;
97
gobject_class->get_property = xfce_clock_binary_get_property;
99
gtkwidget_class = GTK_WIDGET_CLASS (klass);
100
gtkwidget_class->size_request = xfce_clock_binary_size_request;
101
gtkwidget_class->expose_event = xfce_clock_binary_expose_event;
104
* Whether we display seconds
106
g_object_class_install_property (gobject_class,
108
g_param_spec_boolean ("show-seconds", "show-seconds", "show-seconds",
109
FALSE, PANEL_PARAM_READWRITE));
112
* Whether this is a true binary clock
114
g_object_class_install_property (gobject_class,
116
g_param_spec_boolean ("true-binary", "true-binary", "true-binary",
117
FALSE, PANEL_PARAM_READWRITE));
94
GObjectClass *gobject_class;
95
GtkWidgetClass *gtkwidget_class;
97
gobject_class = G_OBJECT_CLASS (klass);
98
gobject_class->set_property = xfce_clock_binary_set_property;
99
gobject_class->get_property = xfce_clock_binary_get_property;
100
gobject_class->finalize = xfce_clock_binary_finalize;
102
gtkwidget_class = GTK_WIDGET_CLASS (klass);
103
gtkwidget_class->expose_event = xfce_clock_binary_expose_event;
105
g_object_class_install_property (gobject_class,
107
g_param_spec_double ("size-ratio", NULL, NULL,
108
-1, G_MAXDOUBLE, 1.0,
110
| G_PARAM_STATIC_STRINGS));
112
g_object_class_install_property (gobject_class,
114
g_param_spec_enum ("orientation", NULL, NULL,
115
GTK_TYPE_ORIENTATION,
116
GTK_ORIENTATION_HORIZONTAL,
118
| G_PARAM_STATIC_STRINGS));
120
g_object_class_install_property (gobject_class,
122
g_param_spec_boolean ("show-seconds", NULL, NULL,
125
| G_PARAM_STATIC_STRINGS));
127
g_object_class_install_property (gobject_class,
129
g_param_spec_boolean ("true-binary", NULL, NULL,
132
| G_PARAM_STATIC_STRINGS));
134
g_object_class_install_property (gobject_class,
136
g_param_spec_boolean ("show-inactive", NULL, NULL,
139
| G_PARAM_STATIC_STRINGS));
141
g_object_class_install_property (gobject_class,
143
g_param_spec_boolean ("show-grid", NULL, NULL,
146
| G_PARAM_STATIC_STRINGS));
143
169
const GValue *value,
144
170
GParamSpec *pspec)
146
XfceClockBinary *binary = XFCE_CLOCK_BINARY (object);
172
XfceClockBinary *binary = XFCE_CLOCK_BINARY (object);
150
case PROP_SHOW_SECONDS:
151
binary->show_seconds = g_value_get_boolean (value);
154
case PROP_TRUE_BINARY:
155
binary->true_binary = g_value_get_boolean (value);
159
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
176
case PROP_ORIENTATION:
179
case PROP_SHOW_SECONDS:
180
binary->show_seconds = g_value_get_boolean (value);
181
g_object_notify (object, "size-ratio");
184
case PROP_TRUE_BINARY:
185
binary->true_binary = g_value_get_boolean (value);
186
g_object_notify (object, "size-ratio");
189
case PROP_SHOW_INACTIVE:
190
binary->show_inactive = g_value_get_boolean (value);
194
binary->show_grid = g_value_get_boolean (value);
198
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
202
/* reschedule the timeout and resize */
203
clock_plugin_timeout_set_interval (binary->timeout,
204
binary->show_seconds ? CLOCK_INTERVAL_SECOND : CLOCK_INTERVAL_MINUTE);
205
gtk_widget_queue_resize (GTK_WIDGET (binary));
170
214
GParamSpec *pspec)
172
XfceClockBinary *binary = XFCE_CLOCK_BINARY (object);
176
case PROP_SHOW_SECONDS:
177
g_value_set_boolean (value, binary->show_seconds);
180
case PROP_TRUE_BINARY:
181
g_value_set_boolean (value, binary->true_binary);
185
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
193
xfce_clock_binary_size_request (GtkWidget *widget,
194
GtkRequisition *requisition)
198
XfceClockBinary *binary = XFCE_CLOCK_BINARY (widget);
200
/* get the current widget size */
201
gtk_widget_get_size_request (widget, &width, &height);
203
/* ratio of the clock */
204
if (binary->true_binary)
216
XfceClockBinary *binary = XFCE_CLOCK_BINARY (object);
221
case PROP_SHOW_SECONDS:
222
g_value_set_boolean (value, binary->show_seconds);
225
case PROP_TRUE_BINARY:
226
g_value_set_boolean (value, binary->true_binary);
229
case PROP_SHOW_INACTIVE:
230
g_value_set_boolean (value, binary->show_inactive);
234
g_value_set_boolean (value, binary->show_grid);
237
case PROP_SIZE_RATIO:
238
if (binary->true_binary)
206
239
ratio = binary->show_seconds ? 2.0 : 3.0;
210
241
ratio = binary->show_seconds ? 1.5 : 1.0;
213
/* set requisition based on the plugin orientation */
216
requisition->height = height;
217
requisition->width = height * ratio;
221
requisition->height = width / ratio;
222
requisition->width = width;
242
g_value_set_double (value, ratio);
246
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
254
xfce_clock_binary_finalize (GObject *object)
256
/* stop the timeout */
257
clock_plugin_timeout_free (XFCE_CLOCK_BINARY (object)->timeout);
259
(*G_OBJECT_CLASS (xfce_clock_binary_parent_class)->finalize) (object);
265
xfce_clock_binary_expose_event_true_binary (XfceClockBinary *binary,
267
GtkAllocation *alloc)
269
GdkColor *active, *inactive;
272
static gint binary_table[] = { 32, 16, 8, 4, 2, 1 };
273
gint col, cols = G_N_ELEMENTS (binary_table);
274
gint remain_h, remain_w;
275
gint offset_x, offset_y;
279
if (G_UNLIKELY (GTK_WIDGET_STATE (binary) == GTK_STATE_INSENSITIVE))
281
inactive = &(GTK_WIDGET (binary)->style->mid[GTK_STATE_INSENSITIVE]);
282
active = &(GTK_WIDGET (binary)->style->dark[GTK_STATE_INSENSITIVE]);
286
inactive = &(GTK_WIDGET (binary)->style->dark[GTK_STATE_NORMAL]);
287
active = &(GTK_WIDGET (binary)->style->dark[GTK_STATE_SELECTED]);
290
clock_plugin_get_localtime (&tm);
293
remain_h = alloc->height;
296
rows = binary->show_seconds ? 3 : 2;
297
for (row = 0; row < rows; row++)
299
/* get the time this row represents */
308
remain_w = alloc->width;
310
h = remain_h / (rows - row);
313
for (col = 0; col < cols; col++)
316
w = remain_w / (cols - col);
321
if (ticks >= binary_table[col])
323
gdk_cairo_set_source_color (cr, active);
324
ticks -= binary_table[col];
326
else if (binary->show_inactive)
328
gdk_cairo_set_source_color (cr, inactive);
336
cairo_rectangle (cr, x, offset_y, w - 1, h - 1);
348
xfce_clock_binary_expose_event_binary (XfceClockBinary *binary,
350
GtkAllocation *alloc)
352
GdkColor *active, *inactive;
353
static gint binary_table[] = { 80, 40, 20, 10, 8, 4, 2, 1 };
355
gint row, rows = G_N_ELEMENTS (binary_table) / 2;
358
gint remain_h, remain_w;
359
gint offset_x, offset_y;
363
if (G_UNLIKELY (GTK_WIDGET_STATE (binary) == GTK_STATE_INSENSITIVE))
365
inactive = &(GTK_WIDGET (binary)->style->mid[GTK_STATE_INSENSITIVE]);
366
active = &(GTK_WIDGET (binary)->style->dark[GTK_STATE_INSENSITIVE]);
370
inactive = &(GTK_WIDGET (binary)->style->dark[GTK_STATE_NORMAL]);
371
active = &(GTK_WIDGET (binary)->style->dark[GTK_STATE_SELECTED]);
374
clock_plugin_get_localtime (&tm);
376
remain_w = alloc->width;
379
/* make sure the cols are all equal */
380
cols = binary->show_seconds ? 6 : 4;
381
for (col = 0; col < cols; col++)
383
/* get the time this row represents */
392
remain_h = alloc->height;
394
w = remain_w / (cols - col);
397
for (row = 0; row < rows; row++)
400
h = remain_h / (rows - row);
405
digit = row + (4 * (col % 2));
406
if (ticks >= binary_table[digit])
408
gdk_cairo_set_source_color (cr, active);
409
ticks -= binary_table[digit];
411
else if (binary->show_inactive)
413
gdk_cairo_set_source_color (cr, inactive);
421
cairo_rectangle (cr, offset_x, y, w - 1, h - 1);
229
433
xfce_clock_binary_expose_event (GtkWidget *widget,
230
434
GdkEventExpose *event)
232
XfceClockBinary *binary = XFCE_CLOCK_BINARY (widget);
233
gdouble cw, ch, columns;
234
gint ticks, cells, decimal;
238
gint decimal_tb[] = {32, 16, 8, 4, 2, 1};
239
gint decimal_bcd[] = {80, 40, 20, 10, 8, 4, 2, 1};
241
GdkColor active, inactive;
244
g_return_val_if_fail (XFCE_CLOCK_IS_BINARY (binary), FALSE);
246
/* number of columns and cells */
247
columns = binary->show_seconds ? 3.0 : 2.0;
248
cells = binary->true_binary ? 6 : 8;
250
/* cell width and height */
251
if (binary->true_binary)
253
cw = widget->allocation.width / 6.0;
254
ch = widget->allocation.height / columns;
258
cw = widget->allocation.width / columns / 2.0;
259
ch = widget->allocation.height / 4.0;
263
radius = MIN (cw, ch) / 2.0 * 0.7;
266
inactive = widget->style->fg[GTK_STATE_NORMAL];
267
active = widget->style->bg[GTK_STATE_SELECTED];
269
/* get the cairo context */
270
cr = gdk_cairo_create (widget->window);
272
if (G_LIKELY (cr != NULL))
274
/* clip the drawing area */
275
gdk_cairo_rectangle (cr, &event->area);
278
/* get the current time */
279
xfce_clock_util_get_localtime (&tm);
281
/* walk the three or two time parts */
282
for (i = 0; i < columns; i++)
436
XfceClockBinary *binary = XFCE_CLOCK_BINARY (widget);
448
panel_return_val_if_fail (XFCE_CLOCK_IS_BINARY (binary), FALSE);
449
panel_return_val_if_fail (GDK_IS_WINDOW (widget->window), FALSE);
451
cr = gdk_cairo_create (widget->window);
452
if (G_LIKELY (cr != NULL))
454
/* clip the drawing region */
455
gdk_cairo_rectangle (cr, &event->area);
458
gtk_misc_get_padding (GTK_MISC (widget), &pad_x, &pad_y);
460
alloc = widget->allocation;
461
alloc.width -= 1 + 2 * pad_x;
462
alloc.height -= 1 + 2 * pad_y;
463
alloc.x += pad_x + 1;
464
alloc.y += pad_y + 1;
466
/* align columns and fix rounding */
467
cols = binary->true_binary ? 6 : (binary->show_seconds ? 6 : 4);
468
diff = alloc.width - (floor ((gdouble) alloc.width / cols) * cols);
472
/* align rows and fix rounding */
473
rows = binary->true_binary ? (binary->show_seconds ? 3 : 2) : 4;
474
diff = alloc.height - (floor ((gdouble) alloc.height / rows) * rows);
475
alloc.height -= diff;
478
if (binary->show_grid)
284
/* get the time of this column */
292
/* walk the binary columns */
293
for (j = 0; j < cells; j++)
295
if (binary->true_binary)
297
/* skip the columns we don't draw */
298
if (i == 0 && j == 0)
301
/* decimal representation of this cell */
302
decimal = decimal_tb[j];
304
/* center of the arc */
305
x = cw * (j + 0.5) + widget->allocation.x;
306
y = ch * (i + 0.5) + widget->allocation.y;
310
/* skip the columns we don't draw */
311
if ((j == 0) || (i == 0 && j == 1))
314
/* decimal representation of this cell */
315
decimal = decimal_bcd[j];
317
/* center of the arc */
318
x = cw * (i * 2 + (j < 4 ? 0 : 1) + 0.5) + widget->allocation.x;
319
y = ch * ((j >= 4 ? j - 4 : j) + 0.5) + widget->allocation.y;
322
/* if this binary values 'fits' in the time */
323
if (ticks >= decimal)
325
/* extract the decimal value from the time */
328
/* set the active color */
329
gdk_cairo_set_source_color (cr, &active);
333
/* set the inactive color */
334
gdk_cairo_set_source_color (cr, &inactive);
338
cairo_move_to (cr, x, y);
339
cairo_arc (cr, x, y, radius, 0, 2 * M_PI);
340
cairo_close_path (cr);
480
color = &(GTK_WIDGET (binary)->style->light[GTK_STATE_SELECTED]);
481
gdk_cairo_set_source_color (cr, color);
482
cairo_set_line_width (cr, 1);
484
remain_w = alloc.width;
485
remain_h = alloc.height;
489
cairo_rectangle (cr, x, y, alloc.width, alloc.height);
492
for (col = 0; col < cols - 1; col++)
494
w = remain_w / (cols - col);
495
x += w; remain_w -= w;
496
cairo_move_to (cr, x, alloc.y);
497
cairo_rel_line_to (cr, 0, alloc.height);
501
for (row = 0; row < rows - 1; row++)
503
h = remain_h / (rows - row);
504
y += h; remain_h -= h;
505
cairo_move_to (cr, alloc.x, y);
506
cairo_rel_line_to (cr, alloc.width, 0);
511
if (binary->true_binary)
512
xfce_clock_binary_expose_event_true_binary (binary, cr, &alloc);
514
xfce_clock_binary_expose_event_binary (binary, cr, &alloc);
525
xfce_clock_binary_update (gpointer user_data)
527
GtkWidget *widget = GTK_WIDGET (user_data);
529
panel_return_val_if_fail (XFCE_CLOCK_IS_BINARY (user_data), FALSE);
531
/* update if the widget if visible */
532
if (G_LIKELY (GTK_WIDGET_VISIBLE (widget)))
533
gtk_widget_queue_draw (widget);