aboutsummaryrefslogtreecommitdiffstats
path: root/applets/clock/clock.c
blob: 62a85c45e9ec3d223b99c2cc62b3f8aa84fef20d (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
/* 
 * (C) 2006 OpenedHand Ltd.
 *
 * Author: Jorn Baayen <jorn@openedhand.com>
 *
 * Licensed under the GPL v2 or greater.
 */

#include <gtk/gtklabel.h>
#include <time.h>
#include <matchbox-panel/mb-panel.h>

/* Applet destroyed */
static void
destroy_cb (GtkLabel *label)
{
        guint timeout_id;
        
        /* Remove timeout */
        timeout_id =
                GPOINTER_TO_UINT
                        (g_object_get_data (G_OBJECT (label), "timeout-id"));

        g_source_remove (timeout_id);
}

/* Called every minute */
static gboolean
timeout (GtkLabel *label)
{
        time_t t;
        char str[6];

        /* Update label */
        t = time (NULL);
        strftime (str, 6, "%H:%M", localtime (&t));
        
        gtk_label_set_text (label, str);

        /* Keep going */
        return TRUE;
}

/* Called on the next minute after applet creation */
static gboolean
initial_timeout (GtkLabel *label)
{
        guint timeout_id;
        
        /* Update label */
        timeout (label);
        
        /* Install a new timeout that is called every minute */
        timeout_id = g_timeout_add (60 * 1000,
                                    (GSourceFunc) timeout,
                                    label);
        
        g_object_set_data (G_OBJECT (label),
                           "timeout-id",
                           GUINT_TO_POINTER (timeout_id));
        
        /* Don't call this again */
        return FALSE;
}

/* GtkStyle set */
static void
style_set_cb (GtkWidget *widget,
              GtkStyle  *style,
              gpointer   user_data)
{
        GtkRequisition requisition;

        gtk_widget_size_request (widget, &requisition);

        /* Do we have enough space for a horizontal clock? */
        if (GPOINTER_TO_INT (user_data) < requisition.width) {
                /* No: Rotate label 90 degrees */
                gtk_label_set_angle (GTK_LABEL (widget), 90.0);
        }
}

G_MODULE_EXPORT GtkWidget *
mb_panel_applet_create (const char *id,
                        int         panel_width,
                        int         panel_height)
{
        GtkWidget *label;
        guint timeout_id;
        time_t t;
        struct tm *local_time;

        /* Create label */
        label = gtk_label_new (NULL);

        gtk_widget_set_name (label, "MatchboxPanelClock");

        timeout (GTK_LABEL (label));

        g_signal_connect (label,
                          "destroy",
                          G_CALLBACK (destroy_cb),
                          NULL);

        /* Set up a timeout to be called when we hit the next minute */
        t = time (NULL);
        local_time = localtime (&t);
        
        timeout_id = g_timeout_add ((60 - local_time->tm_sec) * 1000,
                                    (GSourceFunc) initial_timeout,
                                    label);
        
        g_object_set_data (G_OBJECT (label),
                           "timeout-id",
                           GUINT_TO_POINTER (timeout_id));

        /* Is this a vertical panel? */
        if (panel_width < panel_height) {
                /* Yes: Connect to 'style-set' signal */
                g_signal_connect (label,
                                  "style-set",
                                  G_CALLBACK (style_set_cb),
                                  GINT_TO_POINTER (panel_width));

                gtk_misc_set_padding (GTK_MISC (label), 0, 5);
        } else
                gtk_misc_set_padding (GTK_MISC (label), 5, 0);

        /* Show! */
        gtk_widget_show (label);

        return label;
};