~ubuntu-branches/ubuntu/precise/xfce4-panel/precise

« back to all changes in this revision

Viewing changes to plugins/clock/clock-digital.c

  • Committer: Bazaar Package Importer
  • Author(s): Lionel Le Folgoc
  • Date: 2010-12-04 15:45:53 UTC
  • mto: (4.1.3 experimental)
  • mto: This revision was merged to the branch mainline in revision 50.
  • Revision ID: james.westby@ubuntu.com-20101204154553-f452gq02eiksf09f
Tags: upstream-4.7.5
ImportĀ upstreamĀ versionĀ 4.7.5

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* $Id$ */
2
1
/*
3
 
 * Copyright (c) 2007 Nick Schermer <nick@xfce.org>
 
2
 * Copyright (c) 2007-2009 Nick Schermer <nick@xfce.org>
4
3
 *
5
4
 * This program is free software; you can redistribute it and/or modify it
6
5
 * under the terms of the GNU General Public License as published by the Free
32
31
 
33
32
 
34
33
 
35
 
/* class functions */
36
 
static void xfce_clock_digital_finalize     (GObject               *object);
37
 
static void xfce_clock_digital_set_property (GObject               *object,
38
 
                                             guint                  prop_id,
39
 
                                             const GValue          *value,
40
 
                                             GParamSpec            *pspec);
41
 
static void xfce_clock_digital_get_property (GObject               *object,
42
 
                                             guint                  prop_id,
43
 
                                             GValue                *value,
44
 
                                             GParamSpec            *pspec);
 
34
static void     xfce_clock_digital_set_property (GObject               *object,
 
35
                                                 guint                  prop_id,
 
36
                                                 const GValue          *value,
 
37
                                                 GParamSpec            *pspec);
 
38
static void     xfce_clock_digital_get_property (GObject               *object,
 
39
                                                 guint                  prop_id,
 
40
                                                 GValue                *value,
 
41
                                                 GParamSpec            *pspec);
 
42
static void     xfce_clock_digital_finalize     (GObject               *object);
 
43
static gboolean xfce_clock_digital_update       (gpointer               user_data);
45
44
 
46
45
 
47
46
 
48
47
enum
49
48
{
50
 
    PROP_0,
51
 
    PROP_DIGITAL_FORMAT
 
49
 PROP_0,
 
50
 PROP_DIGITAL_FORMAT,
 
51
 PROP_SIZE_RATIO,
 
52
  PROP_ORIENTATION
52
53
};
53
54
 
54
55
struct _XfceClockDigitalClass
55
56
{
56
 
    GtkLabelClass __parent__;
 
57
 GtkLabelClass __parent__;
57
58
};
58
59
 
59
60
struct _XfceClockDigital
60
61
{
61
 
    GtkLabel  __parent__;
62
 
 
63
 
    /* current clock format */
64
 
    gchar    *format;
 
62
  GtkLabel __parent__;
 
63
 
 
64
  ClockPluginTimeout *timeout;
 
65
 
 
66
  gchar *format;
65
67
};
66
68
 
67
69
 
68
70
 
69
 
G_DEFINE_TYPE (XfceClockDigital, xfce_clock_digital, GTK_TYPE_LABEL);
 
71
XFCE_PANEL_DEFINE_TYPE (XfceClockDigital, xfce_clock_digital, GTK_TYPE_LABEL)
70
72
 
71
73
 
72
74
 
73
75
static void
74
76
xfce_clock_digital_class_init (XfceClockDigitalClass *klass)
75
77
{
76
 
    GObjectClass *gobject_class;
77
 
 
78
 
    gobject_class = G_OBJECT_CLASS (klass);
79
 
    gobject_class->finalize = xfce_clock_digital_finalize;
80
 
    gobject_class->set_property = xfce_clock_digital_set_property;
81
 
    gobject_class->get_property = xfce_clock_digital_get_property;
82
 
 
83
 
    /**
84
 
     * Digital clock format
85
 
     **/
86
 
    g_object_class_install_property (gobject_class,
87
 
                                     PROP_DIGITAL_FORMAT,
88
 
                                     g_param_spec_string ("digital-format", "digital-format", "digital-format",
89
 
                                                          NULL, PANEL_PARAM_READWRITE));
 
78
  GObjectClass *gobject_class;
 
79
 
 
80
  gobject_class = G_OBJECT_CLASS (klass);
 
81
  gobject_class->finalize = xfce_clock_digital_finalize;
 
82
  gobject_class->set_property = xfce_clock_digital_set_property;
 
83
  gobject_class->get_property = xfce_clock_digital_get_property;
 
84
 
 
85
  g_object_class_install_property (gobject_class,
 
86
                                   PROP_SIZE_RATIO,
 
87
                                   g_param_spec_double ("size-ratio", NULL, NULL,
 
88
                                                        -1, G_MAXDOUBLE, 0.0,
 
89
                                                        G_PARAM_READABLE
 
90
                                                        | G_PARAM_STATIC_STRINGS));
 
91
 
 
92
  g_object_class_install_property (gobject_class,
 
93
                                   PROP_ORIENTATION,
 
94
                                   g_param_spec_enum ("orientation", NULL, NULL,
 
95
                                                      GTK_TYPE_ORIENTATION,
 
96
                                                      GTK_ORIENTATION_HORIZONTAL,
 
97
                                                      G_PARAM_WRITABLE
 
98
                                                      | G_PARAM_STATIC_STRINGS));
 
99
 
 
100
  g_object_class_install_property (gobject_class,
 
101
                                   PROP_DIGITAL_FORMAT,
 
102
                                   g_param_spec_string ("digital-format", NULL, NULL,
 
103
                                                        DEFAULT_DIGITAL_FORMAT,
 
104
                                                        G_PARAM_READWRITE
 
105
                                                        | G_PARAM_STATIC_STRINGS));
90
106
}
91
107
 
92
108
 
94
110
static void
95
111
xfce_clock_digital_init (XfceClockDigital *digital)
96
112
{
97
 
    /* init */
98
 
    digital->format = NULL;
99
 
 
100
 
    /* center text */
101
 
    gtk_label_set_justify (GTK_LABEL (digital), GTK_JUSTIFY_CENTER);
102
 
}
103
 
 
104
 
 
105
 
 
106
 
static void
107
 
xfce_clock_digital_finalize (GObject *object)
108
 
{
109
 
    XfceClockDigital *digital = XFCE_CLOCK_DIGITAL (object);
110
 
 
111
 
    /* cleanup */
112
 
    g_free (digital->format);
113
 
 
114
 
    (*G_OBJECT_CLASS (xfce_clock_digital_parent_class)->finalize) (object);
 
113
  digital->format = g_strdup (DEFAULT_DIGITAL_FORMAT);
 
114
  digital->timeout = clock_plugin_timeout_new (clock_plugin_interval_from_format (digital->format),
 
115
                                               xfce_clock_digital_update,
 
116
                                               digital);
 
117
 
 
118
  gtk_label_set_justify (GTK_LABEL (digital), GTK_JUSTIFY_CENTER);
115
119
}
116
120
 
117
121
 
122
126
                                 const GValue *value,
123
127
                                 GParamSpec   *pspec)
124
128
{
125
 
    XfceClockDigital *digital = XFCE_CLOCK_DIGITAL (object);
 
129
  XfceClockDigital *digital = XFCE_CLOCK_DIGITAL (object);
126
130
 
127
 
    switch (prop_id)
 
131
  switch (prop_id)
128
132
    {
129
 
        case PROP_DIGITAL_FORMAT:
130
 
            /* cleanup */
131
 
            g_free (digital->format);
132
 
 
133
 
            /* set new format */
134
 
            digital->format = g_value_dup_string (value);
135
 
 
136
 
            /* update the widget */
137
 
            xfce_clock_digital_update (digital);
138
 
            break;
139
 
 
140
 
        default:
141
 
            G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
142
 
            break;
 
133
    case PROP_ORIENTATION:
 
134
      gtk_label_set_angle (GTK_LABEL (object),
 
135
          g_value_get_enum (value) == GTK_ORIENTATION_HORIZONTAL ?
 
136
          0 : 270);
 
137
      break;
 
138
 
 
139
    case PROP_DIGITAL_FORMAT:
 
140
      g_free (digital->format);
 
141
      digital->format = g_value_dup_string (value);
 
142
      break;
 
143
 
 
144
    default:
 
145
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
 
146
      break;
143
147
    }
 
148
 
 
149
  /* reschedule the timeout and redraw */
 
150
  clock_plugin_timeout_set_interval (digital->timeout,
 
151
      clock_plugin_interval_from_format (digital->format));
 
152
  xfce_clock_digital_update (digital);
144
153
}
145
154
 
146
155
 
151
160
                                 GValue     *value,
152
161
                                 GParamSpec *pspec)
153
162
{
154
 
    XfceClockDigital *digital = XFCE_CLOCK_DIGITAL (object);
 
163
  XfceClockDigital *digital = XFCE_CLOCK_DIGITAL (object);
155
164
 
156
 
    switch (prop_id)
 
165
  switch (prop_id)
157
166
    {
158
 
        case PROP_DIGITAL_FORMAT:
159
 
            g_value_set_string (value, digital->format);
160
 
            break;
161
 
 
162
 
        default:
163
 
            G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
164
 
            break;
 
167
    case PROP_DIGITAL_FORMAT:
 
168
      g_value_set_string (value, digital->format);
 
169
      break;
 
170
 
 
171
    case PROP_SIZE_RATIO:
 
172
      g_value_set_double (value, -1.0);
 
173
      break;
 
174
 
 
175
    default:
 
176
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
 
177
      break;
165
178
    }
166
179
}
167
180
 
168
181
 
169
182
 
 
183
static void
 
184
xfce_clock_digital_finalize (GObject *object)
 
185
{
 
186
  XfceClockDigital *digital = XFCE_CLOCK_DIGITAL (object);
 
187
 
 
188
  /* stop the timeout */
 
189
  clock_plugin_timeout_free (digital->timeout);
 
190
 
 
191
  g_free (digital->format);
 
192
 
 
193
  (*G_OBJECT_CLASS (xfce_clock_digital_parent_class)->finalize) (object);
 
194
}
 
195
 
 
196
 
 
197
 
 
198
static gboolean
 
199
xfce_clock_digital_update (gpointer user_data)
 
200
{
 
201
  XfceClockDigital *digital = XFCE_CLOCK_DIGITAL (user_data);
 
202
  gchar            *string;
 
203
  struct tm         tm;
 
204
 
 
205
  panel_return_val_if_fail (XFCE_CLOCK_IS_DIGITAL (digital), FALSE);
 
206
 
 
207
  /* get the local time */
 
208
  clock_plugin_get_localtime (&tm);
 
209
 
 
210
  /* set time string */
 
211
  string = clock_plugin_strdup_strftime (digital->format, &tm);
 
212
  gtk_label_set_markup (GTK_LABEL (digital), string);
 
213
  g_free (string);
 
214
 
 
215
  return TRUE;
 
216
}
 
217
 
 
218
 
 
219
 
170
220
GtkWidget *
171
221
xfce_clock_digital_new (void)
172
222
{
173
 
    return g_object_new (XFCE_CLOCK_TYPE_DIGITAL, NULL);
174
 
}
175
 
 
176
 
 
177
 
 
178
 
gboolean
179
 
xfce_clock_digital_update (gpointer user_data)
180
 
{
181
 
    XfceClockDigital *digital = XFCE_CLOCK_DIGITAL (user_data);
182
 
    gchar            *string;
183
 
    struct tm         tm;
184
 
 
185
 
    g_return_val_if_fail (XFCE_CLOCK_IS_DIGITAL (digital), FALSE);
186
 
 
187
 
    /* get the local time */
188
 
    xfce_clock_util_get_localtime (&tm);
189
 
 
190
 
    /* get the string */
191
 
    string = xfce_clock_util_strdup_strftime (digital->format ? digital->format : DEFAULT_DIGITAL_FORMAT, &tm);
192
 
 
193
 
    /* set the new label */
194
 
    gtk_label_set_markup (GTK_LABEL (digital), string);
195
 
 
196
 
    /* cleanup */
197
 
    g_free (string);
198
 
 
199
 
    return TRUE;
 
223
  return g_object_new (XFCE_CLOCK_TYPE_DIGITAL, NULL);
200
224
}