1
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*-
3
* Copyright (C) 2007 William Jon McCann <mccann@jhu.edu>
4
* Copyright (C) 2008 Red Hat, Inc.
6
* This program is free software; you can redistribute it and/or modify
7
* it under the terms of the GNU General Public License as published by
8
* the Free Software Foundation; either version 2 of the License, or
9
* (at your option) any later version.
11
* This program is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
* GNU General Public License for more details.
16
* You should have received a copy of the GNU General Public License
17
* along with this program; if not, write to the Free Software
18
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20
* Written by: William Jon McCann <mccann@jhu.edu>
21
* Ray Strode <rstrode@redhat.com>
35
#include <glib/gi18n.h>
36
#include <glib/gstdio.h>
39
#include "gdm-clock-widget.h"
41
#define GDM_CLOCK_WIDGET_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), GDM_TYPE_CLOCK_WIDGET, GdmClockWidgetPrivate))
43
struct GdmClockWidgetPrivate
48
guint update_clock_id;
49
guint should_show_seconds : 1;
50
guint should_show_date : 1;
53
static void gdm_clock_widget_class_init (GdmClockWidgetClass *klass);
54
static void gdm_clock_widget_init (GdmClockWidget *clock_widget);
55
static void gdm_clock_widget_finalize (GObject *object);
56
static gboolean update_timeout_cb (GdmClockWidget *clock);
58
G_DEFINE_TYPE (GdmClockWidget, gdm_clock_widget, GTK_TYPE_ALIGNMENT)
61
update_time_format (GdmClockWidget *clock)
66
if (clock->priv->should_show_date && clock->priv->should_show_seconds) {
67
/* translators: This is the time format to use when both
68
* the date and time with seconds are being shown together.
70
clock_format = _("%a %b %e, %l:%M:%S %p");
71
tooltip_format = NULL;
72
} else if (clock->priv->should_show_date && !clock->priv->should_show_seconds) {
73
/* translators: This is the time format to use when both
74
* the date and time without seconds are being shown together.
76
clock_format = _("%a %b %e, %l:%M %p");
78
tooltip_format = NULL;
79
} else if (!clock->priv->should_show_date && clock->priv->should_show_seconds) {
80
/* translators: This is the time format to use when there is
81
* no date, just weekday and time with seconds.
83
clock_format = _("%a %l:%M:%S %p");
85
/* translators: This is the time format to use for the date
87
tooltip_format = _("%x");
89
/* translators: This is the time format to use when there is
90
* no date, just weekday and time without seconds.
92
clock_format = _("%a %l:%M %p");
94
tooltip_format = _("%x");
97
g_free (clock->priv->time_format);
98
clock->priv->time_format = g_locale_from_utf8 (clock_format, -1, NULL, NULL, NULL);
100
g_free (clock->priv->tooltip_format);
102
if (tooltip_format != NULL) {
103
clock->priv->tooltip_format = g_locale_from_utf8 (tooltip_format, -1, NULL, NULL, NULL);
105
clock->priv->tooltip_format = NULL;
110
update_clock (GtkLabel *label,
111
const char *clock_format,
112
const char *tooltip_format)
122
g_warning ("Unable to get broken down local time");
125
if (strftime (buf, sizeof (buf), clock_format, tm) == 0) {
126
g_warning ("Couldn't format time: %s", clock_format);
129
utf8 = g_locale_to_utf8 (buf, -1, NULL, NULL, NULL);
130
gtk_label_set_text (label, utf8);
133
if (tooltip_format != NULL) {
134
if (strftime (buf, sizeof (buf), tooltip_format, tm) == 0) {
135
g_warning ("Couldn't format tooltip date: %s", tooltip_format);
138
utf8 = g_locale_to_utf8 (buf, -1, NULL, NULL, NULL);
139
gtk_widget_set_tooltip_text (GTK_WIDGET (label), utf8);
142
gtk_widget_set_has_tooltip (GTK_WIDGET (label), FALSE);
147
set_clock_timeout (GdmClockWidget *clock,
153
if (clock->priv->update_clock_id > 0) {
154
g_source_remove (clock->priv->update_clock_id);
155
clock->priv->update_clock_id = 0;
158
g_get_current_time (&tv);
159
timeouttime = (G_USEC_PER_SEC - tv.tv_usec) / 1000 + 1;
161
/* timeout of one minute if we don't care about the seconds */
162
if (! clock->priv->should_show_seconds) {
163
timeouttime += 1000 * (59 - now % 60);
166
clock->priv->update_clock_id = g_timeout_add (timeouttime,
167
(GSourceFunc)update_timeout_cb,
172
update_timeout_cb (GdmClockWidget *clock)
178
if (clock->priv->label != NULL) {
179
update_clock (GTK_LABEL (clock->priv->label),
180
clock->priv->time_format,
181
clock->priv->tooltip_format);
184
set_clock_timeout (clock, new_time);
190
remove_timeout (GdmClockWidget *clock)
192
if (clock->priv->update_clock_id > 0) {
193
g_source_remove (clock->priv->update_clock_id);
194
clock->priv->update_clock_id = 0;
199
gdm_clock_widget_size_request (GtkWidget *widget,
200
GtkRequisition *requisition)
202
PangoFontMetrics *metrics;
203
PangoContext *context;
208
if (GTK_WIDGET_CLASS (gdm_clock_widget_parent_class)->size_request) {
209
GTK_WIDGET_CLASS (gdm_clock_widget_parent_class)->size_request (widget, requisition);
212
gtk_widget_ensure_style (widget);
213
context = gtk_widget_get_pango_context (widget);
214
metrics = pango_context_get_metrics (context,
215
widget->style->font_desc,
216
pango_context_get_language (context));
218
ascent = pango_font_metrics_get_ascent (metrics);
219
descent = pango_font_metrics_get_descent (metrics);
220
padding = PANGO_PIXELS (ascent + descent) / 2.0;
221
requisition->height += padding;
223
pango_font_metrics_unref (metrics);
227
gdm_clock_widget_class_init (GdmClockWidgetClass *klass)
229
GObjectClass *object_class;
230
GtkWidgetClass *widget_class;
232
object_class = G_OBJECT_CLASS (klass);
233
widget_class = GTK_WIDGET_CLASS (klass);
235
object_class->finalize = gdm_clock_widget_finalize;
236
widget_class->size_request = gdm_clock_widget_size_request;
238
g_type_class_add_private (klass, sizeof (GdmClockWidgetPrivate));
242
gdm_clock_widget_init (GdmClockWidget *widget)
246
widget->priv = GDM_CLOCK_WIDGET_GET_PRIVATE (widget);
248
box = gtk_hbox_new (FALSE, 6);
249
gtk_widget_show (box);
250
gtk_container_add (GTK_CONTAINER (widget), box);
252
widget->priv->label = gtk_label_new ("");
254
gtk_widget_show (widget->priv->label);
255
gtk_box_pack_start (GTK_BOX (box), widget->priv->label, FALSE, FALSE, 0);
257
update_time_format (widget);
258
update_timeout_cb (widget);
262
gdm_clock_widget_finalize (GObject *object)
264
GdmClockWidget *clock_widget;
266
g_return_if_fail (object != NULL);
267
g_return_if_fail (GDM_IS_CLOCK_WIDGET (object));
269
clock_widget = GDM_CLOCK_WIDGET (object);
271
g_return_if_fail (clock_widget->priv != NULL);
273
remove_timeout (clock_widget);
275
G_OBJECT_CLASS (gdm_clock_widget_parent_class)->finalize (object);
279
gdm_clock_widget_new (void)
283
object = g_object_new (GDM_TYPE_CLOCK_WIDGET,
286
return GTK_WIDGET (object);