Merge lp:~3v1n0/indicator-datetime/clock-label-improvements into lp:indicator-datetime/0.3

Proposed by Marco Trevisan (Treviño) on 2010-12-22
Status: Merged
Merged at revision: 31
Proposed branch: lp:~3v1n0/indicator-datetime/clock-label-improvements
Merge into: lp:indicator-datetime/0.3
Diff against target: 226 lines (+69/-27)
1 file modified
src/indicator-datetime.c (+69/-27)
To merge this branch: bzr merge lp:~3v1n0/indicator-datetime/clock-label-improvements
Reviewer Review Type Date Requested Status
Ted Gould (community) 2010-12-22 Approve on 2011-01-14
Review via email: mp+44534@code.launchpad.net

Description of the change

Added some fixes for the datetime indicator label, making it centering, update on screen changes, and support the pango markup.
It also now detects if the custom strings need to be updated each second or each minute (before it was not possible to use a custom string showing seconds!).

To post a comment you must log in.
Ted Gould (ted) :
review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'src/indicator-datetime.c'
2--- src/indicator-datetime.c 2010-09-30 13:52:20 +0000
3+++ src/indicator-datetime.c 2010-12-22 23:59:52 +0000
4@@ -75,6 +75,7 @@
5 gboolean show_date;
6 gboolean show_day;
7 gchar * custom_string;
8+ gboolean custom_show_seconds;
9
10 guint idle_measure;
11 gint max_width;
12@@ -126,6 +127,18 @@
13 #define INDICATOR_DATETIME_GET_PRIVATE(o) \
14 (G_TYPE_INSTANCE_GET_PRIVATE ((o), INDICATOR_DATETIME_TYPE, IndicatorDatetimePrivate))
15
16+enum {
17+ STRFTIME_MASK_NONE = 0, /* Hours or minutes as we always test those */
18+ STRFTIME_MASK_SECONDS = 1 << 0, /* Seconds count */
19+ STRFTIME_MASK_AMPM = 1 << 1, /* AM/PM counts */
20+ STRFTIME_MASK_WEEK = 1 << 2, /* Day of the week maters (Sat, Sun, etc.) */
21+ STRFTIME_MASK_DAY = 1 << 3, /* Day of the month counts (Feb 1st) */
22+ STRFTIME_MASK_MONTH = 1 << 4, /* Which month matters */
23+ STRFTIME_MASK_YEAR = 1 << 5, /* Which year matters */
24+ /* Last entry, combines all previous */
25+ STRFTIME_MASK_ALL = (STRFTIME_MASK_SECONDS | STRFTIME_MASK_AMPM | STRFTIME_MASK_WEEK | STRFTIME_MASK_DAY | STRFTIME_MASK_MONTH | STRFTIME_MASK_YEAR)
26+};
27+
28 GType indicator_datetime_get_type (void);
29
30 static void indicator_datetime_class_init (IndicatorDatetimeClass *klass);
31@@ -143,6 +156,7 @@
32 static void guess_label_size (IndicatorDatetime * self);
33 static void setup_timer (IndicatorDatetime * self, struct tm * ltime);
34 static void update_time (DBusGProxy * proxy, gpointer user_data);
35+static gint generate_strftime_bitmask (const char *time_str);
36
37 /* Indicator Module Config */
38 INDICATOR_SET_VERSION
39@@ -225,6 +239,7 @@
40 self->priv->show_date = FALSE;
41 self->priv->show_day = FALSE;
42 self->priv->custom_string = g_strdup(DEFAULT_TIME_FORMAT);
43+ self->priv->custom_show_seconds = FALSE;
44
45 self->priv->time_string = generate_format_string(self);
46
47@@ -404,6 +419,7 @@
48 if (newval != self->priv->time_mode) {
49 update = TRUE;
50 self->priv->time_mode = newval;
51+ setup_timer(self, NULL);
52 }
53 break;
54 }
55@@ -440,6 +456,8 @@
56 self->priv->custom_string = NULL;
57 }
58 self->priv->custom_string = g_strdup(newstr);
59+ gint time_mask = generate_strftime_bitmask(newstr);
60+ self->priv->custom_show_seconds = (time_mask & STRFTIME_MASK_SECONDS);
61 if (self->priv->time_mode == SETTINGS_TIME_CUSTOM) {
62 update = TRUE;
63 setup_timer(self, NULL);
64@@ -539,9 +557,10 @@
65
66 if (self->priv->label == NULL) return NULL;
67
68- gchar longstr[128];
69+ gchar longstr[256];
70 time_t t;
71 struct tm *ltime;
72+ gboolean use_markup;
73
74 t = time(NULL);
75 ltime = localtime(&t);
76@@ -551,10 +570,18 @@
77 return NULL;
78 }
79
80- strftime(longstr, 128, self->priv->time_string, ltime);
81+ strftime(longstr, 256, self->priv->time_string, ltime);
82
83 gchar * utf8 = g_locale_to_utf8(longstr, -1, NULL, NULL, NULL);
84- gtk_label_set_label(self->priv->label, utf8);
85+
86+ if (pango_parse_markup(utf8, -1, 0, NULL, NULL, NULL, NULL))
87+ use_markup = TRUE;
88+
89+ if (use_markup)
90+ gtk_label_set_markup(self->priv->label, utf8);
91+ else
92+ gtk_label_set_text(self->priv->label, utf8);
93+
94 g_free(utf8);
95
96 if (self->priv->idle_measure == 0) {
97@@ -595,7 +622,8 @@
98 self->priv->timer = 0;
99 }
100
101- if (self->priv->show_seconds) {
102+ if (self->priv->show_seconds ||
103+ (self->priv->time_mode == SETTINGS_TIME_CUSTOM && self->priv->custom_show_seconds)) {
104 self->priv->timer = g_timeout_add_seconds(1, timer_func, self);
105 } else {
106 if (ltime == NULL) {
107@@ -617,7 +645,12 @@
108 measure_string (GtkStyle * style, PangoContext * context, const gchar * string)
109 {
110 PangoLayout * layout = pango_layout_new(context);
111- pango_layout_set_text(layout, string, -1);
112+
113+ if (pango_parse_markup(string, -1, 0, NULL, NULL, NULL, NULL))
114+ pango_layout_set_markup(layout, string, -1);
115+ else
116+ pango_layout_set_text(layout, string, -1);
117+
118 pango_layout_set_font_description(layout, style->font_desc);
119
120 gint width;
121@@ -634,18 +667,6 @@
122 gint mask;
123 };
124
125-enum {
126- STRFTIME_MASK_NONE = 0, /* Hours or minutes as we always test those */
127- STRFTIME_MASK_SECONDS = 1 << 0, /* Seconds count */
128- STRFTIME_MASK_AMPM = 1 << 1, /* AM/PM counts */
129- STRFTIME_MASK_WEEK = 1 << 2, /* Day of the week maters (Sat, Sun, etc.) */
130- STRFTIME_MASK_DAY = 1 << 3, /* Day of the month counts (Feb 1st) */
131- STRFTIME_MASK_MONTH = 1 << 4, /* Which month matters */
132- STRFTIME_MASK_YEAR = 1 << 5, /* Which year matters */
133- /* Last entry, combines all previous */
134- STRFTIME_MASK_ALL = (STRFTIME_MASK_SECONDS | STRFTIME_MASK_AMPM | STRFTIME_MASK_WEEK | STRFTIME_MASK_DAY | STRFTIME_MASK_MONTH | STRFTIME_MASK_YEAR)
135-};
136-
137 /* A table taken from the man page of strftime to what the different
138 characters can effect. These are worst case in that we need to
139 test the length based on all these things to ensure that we have
140@@ -691,21 +712,21 @@
141 ensure that we can figure out which of the things we
142 need to check in determining the length. */
143 static gint
144-generate_strftime_bitmask (IndicatorDatetime * self)
145+generate_strftime_bitmask (const char *time_str)
146 {
147 gint retval = 0;
148- glong strlength = g_utf8_strlen(self->priv->time_string, -1);
149+ glong strlength = g_utf8_strlen(time_str, -1);
150 gint i;
151- g_debug("Evaluating bitmask for '%s'", self->priv->time_string);
152+ g_debug("Evaluating bitmask for '%s'", time_str);
153
154 for (i = 0; i < strlength; i++) {
155- if (self->priv->time_string[i] == '%' && i + 1 < strlength) {
156- gchar evalchar = self->priv->time_string[i + 1];
157+ if (time_str[i] == '%' && i + 1 < strlength) {
158+ gchar evalchar = time_str[i + 1];
159
160 /* If we're using alternate formats we need to skip those characters */
161 if (evalchar == 'E' || evalchar == 'O') {
162 if (i + 2 < strlength) {
163- evalchar = self->priv->time_string[i + 2];
164+ evalchar = time_str[i + 2];
165 } else {
166 continue;
167 }
168@@ -799,7 +820,10 @@
169 GtkStyle * style = gtk_widget_get_style(GTK_WIDGET(self->priv->label));
170 PangoContext * context = gtk_widget_get_pango_context(GTK_WIDGET(self->priv->label));
171 gint * max_width = &(self->priv->max_width);
172- gint posibilitymask = generate_strftime_bitmask(self);
173+ gint posibilitymask = generate_strftime_bitmask(self->priv->time_string);
174+
175+ /* Reset max width */
176+ *max_width = 0;
177
178 /* Build the array of possibilities that we want to test */
179 GArray * timevals = g_array_new(FALSE, TRUE, sizeof(struct tm));
180@@ -808,9 +832,9 @@
181 g_debug("Checking against %d posible times", timevals->len);
182 gint check_time;
183 for (check_time = 0; check_time < timevals->len; check_time++) {
184- gchar longstr[128];
185- strftime(longstr, 128, self->priv->time_string, &(g_array_index(timevals, struct tm, check_time)));
186-
187+ gchar longstr[256];
188+ strftime(longstr, 256, self->priv->time_string, &(g_array_index(timevals, struct tm, check_time)));
189+
190 gchar * utf8 = g_locale_to_utf8(longstr, -1, NULL, NULL, NULL);
191 gint length = measure_string(style, context, utf8);
192 g_free(utf8);
193@@ -840,6 +864,22 @@
194 return;
195 }
196
197+
198+static void
199+update_text_gravity (GtkWidget *widget, GdkScreen *previous_screen, gpointer data)
200+{
201+ IndicatorDatetime * self = INDICATOR_DATETIME(data);
202+ if (self->priv->label == NULL) return;
203+
204+ PangoLayout *layout;
205+ PangoContext *context;
206+
207+ layout = gtk_label_get_layout (GTK_LABEL(self->priv->label));
208+ context = pango_layout_get_context(layout);
209+ pango_context_set_base_gravity(context, PANGO_GRAVITY_AUTO);
210+}
211+
212+
213 /* Tries to figure out what our format string should be. Lots
214 of translator comments in here. */
215 static gchar *
216@@ -959,8 +999,10 @@
217 /* If there's not a label, we'll build ourselves one */
218 if (self->priv->label == NULL) {
219 self->priv->label = GTK_LABEL(gtk_label_new("Time"));
220+ gtk_label_set_justify (GTK_LABEL(self->priv->label), GTK_JUSTIFY_CENTER);
221 g_object_ref(G_OBJECT(self->priv->label));
222 g_signal_connect(G_OBJECT(self->priv->label), "style-set", G_CALLBACK(style_changed), self);
223+ g_signal_connect(G_OBJECT(self->priv->label), "screen-changed", G_CALLBACK(update_text_gravity), self);
224 guess_label_size(self);
225 update_label(self);
226 gtk_widget_show(GTK_WIDGET(self->priv->label));

Subscribers

People subscribed via source and target branches