summaryrefslogtreecommitdiff
path: root/plugins/clock/clock-binary.c
blob: fed0aa301e33416770e49aef6b18c2c664af8ce2 (plain)
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
/* $Id$ */
/*
 * Copyright (c) 2007 Nick Schermer <nick@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., 59 Temple
 * Place, Suite 330, Boston, MA  02111-1307  USA
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#ifdef HAVE_TIME_H
#include <time.h>
#endif
#ifdef HAVE_MATH_H
#include <math.h>
#endif

#ifndef M_PI
#define M_PI 3.141592654
#endif

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

#include "clock.h"
#include "clock-binary.h"



/* class functions */
static void      xfce_clock_binary_finalize      (GObject              *object);
static void      xfce_clock_binary_set_property  (GObject              *object,
                                                  guint                 prop_id,
                                                  const GValue         *value,
                                                  GParamSpec           *pspec);
static void      xfce_clock_binary_get_property  (GObject              *object,
                                                  guint                 prop_id,
                                                  GValue               *value,
                                                  GParamSpec           *pspec);
static void      xfce_clock_binary_size_request  (GtkWidget            *widget,
                                                  GtkRequisition       *requisition);
static gboolean  xfce_clock_binary_expose_event  (GtkWidget            *widget,
                                                  GdkEventExpose       *event);


enum
{
    PROP_0,
    PROP_SHOW_SECONDS,
    PROP_TRUE_BINARY
};

struct _XfceClockBinaryClass
{
    GtkImageClass __parent__;
};

struct _XfceClockBinary
{
    GtkImage  __parent__;

    /* whether we draw seconds */
    guint     show_seconds : 1;

    /* if this is a true binary clock */
    guint     true_binary : 1;
};



G_DEFINE_TYPE (XfceClockBinary, xfce_clock_binary, GTK_TYPE_IMAGE);



static void
xfce_clock_binary_class_init (XfceClockBinaryClass *klass)
{
    GObjectClass   *gobject_class;
    GtkWidgetClass *gtkwidget_class;

    gobject_class = G_OBJECT_CLASS (klass);
    gobject_class->finalize = xfce_clock_binary_finalize;
    gobject_class->set_property = xfce_clock_binary_set_property;
    gobject_class->get_property = xfce_clock_binary_get_property;

    gtkwidget_class = GTK_WIDGET_CLASS (klass);
    gtkwidget_class->size_request = xfce_clock_binary_size_request;
    gtkwidget_class->expose_event = xfce_clock_binary_expose_event;

    /**
     * Whether we display seconds
     **/
    g_object_class_install_property (gobject_class,
                                     PROP_SHOW_SECONDS,
                                     g_param_spec_boolean ("show-seconds", "show-seconds", "show-seconds",
                                                           FALSE, PANEL_PARAM_READWRITE));

    /**
     * Whether this is a true binary clock
     **/
    g_object_class_install_property (gobject_class,
                                     PROP_TRUE_BINARY,
                                     g_param_spec_boolean ("true-binary", "true-binary", "true-binary",
                                                           FALSE, PANEL_PARAM_READWRITE));
}



static void
xfce_clock_binary_init (XfceClockBinary *binary)
{
    /* init */
    binary->show_seconds = FALSE;
    binary->true_binary = FALSE;
}



static void
xfce_clock_binary_finalize (GObject *object)
{
    (*G_OBJECT_CLASS (xfce_clock_binary_parent_class)->finalize) (object);
}



static void
xfce_clock_binary_set_property (GObject      *object,
                                guint         prop_id,
                                const GValue *value,
                                GParamSpec   *pspec)
{
    XfceClockBinary *binary = XFCE_CLOCK_BINARY (object);

    switch (prop_id)
    {
        case PROP_SHOW_SECONDS:
            binary->show_seconds = g_value_get_boolean (value);
            break;

        case PROP_TRUE_BINARY:
            binary->true_binary = g_value_get_boolean (value);
            break;

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



static void
xfce_clock_binary_get_property (GObject    *object,
                                guint       prop_id,
                                GValue     *value,
                                GParamSpec *pspec)
{
    XfceClockBinary *binary = XFCE_CLOCK_BINARY (object);

    switch (prop_id)
    {
        case PROP_SHOW_SECONDS:
            g_value_set_boolean (value, binary->show_seconds);
            break;

        case PROP_TRUE_BINARY:
            g_value_set_boolean (value, binary->true_binary);
            break;

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



static void
xfce_clock_binary_size_request (GtkWidget      *widget,
                                GtkRequisition *requisition)
{
    gint             width, height;
    gdouble          ratio;
    XfceClockBinary *binary = XFCE_CLOCK_BINARY (widget);

    /* get the current widget size */
    gtk_widget_get_size_request (widget, &width, &height);

    /* ratio of the clock */
    if (binary->true_binary)
    {
        ratio = binary->show_seconds ? 2.0 : 3.0;
    }
    else
    {
        ratio = binary->show_seconds ? 1.5 : 1.0;
    }

    /* set requisition based on the plugin orientation */
    if (width == -1)
    {
        requisition->height = height;
        requisition->width = height * ratio;
    }
    else
    {
        requisition->height = width / ratio;
        requisition->width = width;
    }
}



static gboolean
xfce_clock_binary_expose_event (GtkWidget      *widget,
                                GdkEventExpose *event)
{
    XfceClockBinary *binary = XFCE_CLOCK_BINARY (widget);
    gdouble          cw, ch, columns;
    gint             ticks, cells, decimal;
    gdouble          radius;
    gdouble          x, y;
    gint             i, j;
    gint             decimal_tb[] = {32, 16, 8, 4, 2, 1};
    gint             decimal_bcd[] = {80, 40, 20, 10, 8, 4, 2, 1};
    cairo_t         *cr;
    GdkColor         active, inactive;
    struct tm        tm;

    g_return_val_if_fail (XFCE_CLOCK_IS_BINARY (binary), FALSE);

    /* number of columns and cells */
    columns = binary->show_seconds ? 3.0 : 2.0;
    cells = binary->true_binary ? 6 : 8;

    /* cell width and height */
    if (binary->true_binary)
    {
        cw = widget->allocation.width / 6.0;
        ch = widget->allocation.height / columns;
    }
    else /* bcd clock */
    {
        cw = widget->allocation.width / columns / 2.0;
        ch = widget->allocation.height / 4.0;
    }

    /* arc radius */
    radius = MIN (cw, ch) / 2.0 * 0.7;

    /* get colors */
    inactive = widget->style->fg[GTK_STATE_NORMAL];
    active = widget->style->bg[GTK_STATE_SELECTED];

    /* get the cairo context */
    cr = gdk_cairo_create (widget->window);

    if (G_LIKELY (cr != NULL))
    {
        /* clip the drawing area */
        gdk_cairo_rectangle (cr, &event->area);
        cairo_clip (cr);
        
        /* get the current time */
        xfce_clock_util_get_localtime (&tm);

        /* walk the three or two time parts */
        for (i = 0; i < columns; i++)
        {
            /* get the time of this column */
            if (i == 0)
                ticks = tm.tm_hour;
            else if (i == 1)
                ticks = tm.tm_min;
            else
                ticks = tm.tm_sec;

            /* walk the binary columns */
            for (j = 0; j < cells; j++)
            {
                if (binary->true_binary)
                {
                    /* skip the columns we don't draw */
                    if (i == 0 && j == 0)
                        continue;

                    /* decimal representation of this cell */
                    decimal = decimal_tb[j];

                    /* center of the arc */
                    x = cw * (j + 0.5) + widget->allocation.x;
                    y = ch * (i + 0.5) + widget->allocation.y;
                }
                else /* bcd clock */
                {
                    /* skip the columns we don't draw */
                    if ((j == 0) || (i == 0 && j == 1))
                        continue;

                    /* decimal representation of this cell */
                    decimal = decimal_bcd[j];

                    /* center of the arc */
                    x = cw * (i * 2 + (j < 4 ? 0 : 1) + 0.5) + widget->allocation.x;
                    y = ch * ((j >= 4 ? j - 4 : j) + 0.5) + widget->allocation.y;
                }

                /* if this binary values 'fits' in the time */
                if (ticks >= decimal)
                {
                    /* extract the decimal value from the time */
                    ticks -= decimal;

                    /* set the active color */
                    gdk_cairo_set_source_color (cr, &active);
                }
                else
                {
                    /* set the inactive color */
                    gdk_cairo_set_source_color (cr, &inactive);
                }

                /* draw the arc */
                cairo_move_to (cr, x, y);
                cairo_arc (cr, x, y, radius, 0, 2 * M_PI);
                cairo_close_path (cr);

                /* fill the arc */
                cairo_fill (cr);
            }
        }

        /* cleanup */
        cairo_destroy (cr);
    }

    return FALSE;
}



GtkWidget *
xfce_clock_binary_new (void)
{
    return g_object_new (XFCE_CLOCK_TYPE_BINARY, NULL);
}



gboolean
xfce_clock_binary_update (gpointer user_data)
{
    GtkWidget *widget = GTK_WIDGET (user_data);

    /* update if the widget if visible */
    if (G_LIKELY (GTK_WIDGET_VISIBLE (widget)))
        gtk_widget_queue_draw (widget);

    return TRUE;
}