aboutsummaryrefslogtreecommitdiffstats
path: root/applets/clock.c
blob: 8f381c7e28cb2689732a61e74bd6cfeb05f1f289 (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
/* 
 * (C) 2006 OpenedHand Ltd.
 *
 * Author: Jorn Baayen <jorn@openedhand.com>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library 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
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

#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);
        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 (MIN (panel_width, panel_height) == panel_width) {
                /* Yes: Connect to 'style-set' signal */
                g_signal_connect (label,
                                  "style-set",
                                  G_CALLBACK (style_set_cb),
                                  GINT_TO_POINTER (panel_width));
        }

        /* Show! */
        gtk_widget_show (label);

        return label;
};