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

Proposed by Marco Trevisan (Treviño)
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) Approve
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.
Revision history for this message
Ted Gould (ted) :
review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
=== modified file 'src/indicator-datetime.c'
--- src/indicator-datetime.c 2010-09-30 13:52:20 +0000
+++ src/indicator-datetime.c 2010-12-22 23:59:52 +0000
@@ -75,6 +75,7 @@
75 gboolean show_date;75 gboolean show_date;
76 gboolean show_day;76 gboolean show_day;
77 gchar * custom_string;77 gchar * custom_string;
78 gboolean custom_show_seconds;
7879
79 guint idle_measure;80 guint idle_measure;
80 gint max_width;81 gint max_width;
@@ -126,6 +127,18 @@
126#define INDICATOR_DATETIME_GET_PRIVATE(o) \127#define INDICATOR_DATETIME_GET_PRIVATE(o) \
127(G_TYPE_INSTANCE_GET_PRIVATE ((o), INDICATOR_DATETIME_TYPE, IndicatorDatetimePrivate))128(G_TYPE_INSTANCE_GET_PRIVATE ((o), INDICATOR_DATETIME_TYPE, IndicatorDatetimePrivate))
128129
130enum {
131 STRFTIME_MASK_NONE = 0, /* Hours or minutes as we always test those */
132 STRFTIME_MASK_SECONDS = 1 << 0, /* Seconds count */
133 STRFTIME_MASK_AMPM = 1 << 1, /* AM/PM counts */
134 STRFTIME_MASK_WEEK = 1 << 2, /* Day of the week maters (Sat, Sun, etc.) */
135 STRFTIME_MASK_DAY = 1 << 3, /* Day of the month counts (Feb 1st) */
136 STRFTIME_MASK_MONTH = 1 << 4, /* Which month matters */
137 STRFTIME_MASK_YEAR = 1 << 5, /* Which year matters */
138 /* Last entry, combines all previous */
139 STRFTIME_MASK_ALL = (STRFTIME_MASK_SECONDS | STRFTIME_MASK_AMPM | STRFTIME_MASK_WEEK | STRFTIME_MASK_DAY | STRFTIME_MASK_MONTH | STRFTIME_MASK_YEAR)
140};
141
129GType indicator_datetime_get_type (void);142GType indicator_datetime_get_type (void);
130143
131static void indicator_datetime_class_init (IndicatorDatetimeClass *klass);144static void indicator_datetime_class_init (IndicatorDatetimeClass *klass);
@@ -143,6 +156,7 @@
143static void guess_label_size (IndicatorDatetime * self);156static void guess_label_size (IndicatorDatetime * self);
144static void setup_timer (IndicatorDatetime * self, struct tm * ltime);157static void setup_timer (IndicatorDatetime * self, struct tm * ltime);
145static void update_time (DBusGProxy * proxy, gpointer user_data);158static void update_time (DBusGProxy * proxy, gpointer user_data);
159static gint generate_strftime_bitmask (const char *time_str);
146160
147/* Indicator Module Config */161/* Indicator Module Config */
148INDICATOR_SET_VERSION162INDICATOR_SET_VERSION
@@ -225,6 +239,7 @@
225 self->priv->show_date = FALSE;239 self->priv->show_date = FALSE;
226 self->priv->show_day = FALSE;240 self->priv->show_day = FALSE;
227 self->priv->custom_string = g_strdup(DEFAULT_TIME_FORMAT);241 self->priv->custom_string = g_strdup(DEFAULT_TIME_FORMAT);
242 self->priv->custom_show_seconds = FALSE;
228243
229 self->priv->time_string = generate_format_string(self);244 self->priv->time_string = generate_format_string(self);
230245
@@ -404,6 +419,7 @@
404 if (newval != self->priv->time_mode) {419 if (newval != self->priv->time_mode) {
405 update = TRUE;420 update = TRUE;
406 self->priv->time_mode = newval;421 self->priv->time_mode = newval;
422 setup_timer(self, NULL);
407 }423 }
408 break;424 break;
409 }425 }
@@ -440,6 +456,8 @@
440 self->priv->custom_string = NULL;456 self->priv->custom_string = NULL;
441 }457 }
442 self->priv->custom_string = g_strdup(newstr);458 self->priv->custom_string = g_strdup(newstr);
459 gint time_mask = generate_strftime_bitmask(newstr);
460 self->priv->custom_show_seconds = (time_mask & STRFTIME_MASK_SECONDS);
443 if (self->priv->time_mode == SETTINGS_TIME_CUSTOM) {461 if (self->priv->time_mode == SETTINGS_TIME_CUSTOM) {
444 update = TRUE;462 update = TRUE;
445 setup_timer(self, NULL);463 setup_timer(self, NULL);
@@ -539,9 +557,10 @@
539557
540 if (self->priv->label == NULL) return NULL;558 if (self->priv->label == NULL) return NULL;
541559
542 gchar longstr[128];560 gchar longstr[256];
543 time_t t;561 time_t t;
544 struct tm *ltime;562 struct tm *ltime;
563 gboolean use_markup;
545564
546 t = time(NULL);565 t = time(NULL);
547 ltime = localtime(&t);566 ltime = localtime(&t);
@@ -551,10 +570,18 @@
551 return NULL;570 return NULL;
552 }571 }
553572
554 strftime(longstr, 128, self->priv->time_string, ltime);573 strftime(longstr, 256, self->priv->time_string, ltime);
555 574
556 gchar * utf8 = g_locale_to_utf8(longstr, -1, NULL, NULL, NULL);575 gchar * utf8 = g_locale_to_utf8(longstr, -1, NULL, NULL, NULL);
557 gtk_label_set_label(self->priv->label, utf8);576
577 if (pango_parse_markup(utf8, -1, 0, NULL, NULL, NULL, NULL))
578 use_markup = TRUE;
579
580 if (use_markup)
581 gtk_label_set_markup(self->priv->label, utf8);
582 else
583 gtk_label_set_text(self->priv->label, utf8);
584
558 g_free(utf8);585 g_free(utf8);
559586
560 if (self->priv->idle_measure == 0) {587 if (self->priv->idle_measure == 0) {
@@ -595,7 +622,8 @@
595 self->priv->timer = 0;622 self->priv->timer = 0;
596 }623 }
597 624
598 if (self->priv->show_seconds) {625 if (self->priv->show_seconds ||
626 (self->priv->time_mode == SETTINGS_TIME_CUSTOM && self->priv->custom_show_seconds)) {
599 self->priv->timer = g_timeout_add_seconds(1, timer_func, self);627 self->priv->timer = g_timeout_add_seconds(1, timer_func, self);
600 } else {628 } else {
601 if (ltime == NULL) {629 if (ltime == NULL) {
@@ -617,7 +645,12 @@
617measure_string (GtkStyle * style, PangoContext * context, const gchar * string)645measure_string (GtkStyle * style, PangoContext * context, const gchar * string)
618{646{
619 PangoLayout * layout = pango_layout_new(context);647 PangoLayout * layout = pango_layout_new(context);
620 pango_layout_set_text(layout, string, -1);648
649 if (pango_parse_markup(string, -1, 0, NULL, NULL, NULL, NULL))
650 pango_layout_set_markup(layout, string, -1);
651 else
652 pango_layout_set_text(layout, string, -1);
653
621 pango_layout_set_font_description(layout, style->font_desc);654 pango_layout_set_font_description(layout, style->font_desc);
622655
623 gint width;656 gint width;
@@ -634,18 +667,6 @@
634 gint mask;667 gint mask;
635};668};
636669
637enum {
638 STRFTIME_MASK_NONE = 0, /* Hours or minutes as we always test those */
639 STRFTIME_MASK_SECONDS = 1 << 0, /* Seconds count */
640 STRFTIME_MASK_AMPM = 1 << 1, /* AM/PM counts */
641 STRFTIME_MASK_WEEK = 1 << 2, /* Day of the week maters (Sat, Sun, etc.) */
642 STRFTIME_MASK_DAY = 1 << 3, /* Day of the month counts (Feb 1st) */
643 STRFTIME_MASK_MONTH = 1 << 4, /* Which month matters */
644 STRFTIME_MASK_YEAR = 1 << 5, /* Which year matters */
645 /* Last entry, combines all previous */
646 STRFTIME_MASK_ALL = (STRFTIME_MASK_SECONDS | STRFTIME_MASK_AMPM | STRFTIME_MASK_WEEK | STRFTIME_MASK_DAY | STRFTIME_MASK_MONTH | STRFTIME_MASK_YEAR)
647};
648
649/* A table taken from the man page of strftime to what the different670/* A table taken from the man page of strftime to what the different
650 characters can effect. These are worst case in that we need to671 characters can effect. These are worst case in that we need to
651 test the length based on all these things to ensure that we have672 test the length based on all these things to ensure that we have
@@ -691,21 +712,21 @@
691 ensure that we can figure out which of the things we712 ensure that we can figure out which of the things we
692 need to check in determining the length. */713 need to check in determining the length. */
693static gint714static gint
694generate_strftime_bitmask (IndicatorDatetime * self)715generate_strftime_bitmask (const char *time_str)
695{716{
696 gint retval = 0;717 gint retval = 0;
697 glong strlength = g_utf8_strlen(self->priv->time_string, -1);718 glong strlength = g_utf8_strlen(time_str, -1);
698 gint i;719 gint i;
699 g_debug("Evaluating bitmask for '%s'", self->priv->time_string);720 g_debug("Evaluating bitmask for '%s'", time_str);
700721
701 for (i = 0; i < strlength; i++) {722 for (i = 0; i < strlength; i++) {
702 if (self->priv->time_string[i] == '%' && i + 1 < strlength) {723 if (time_str[i] == '%' && i + 1 < strlength) {
703 gchar evalchar = self->priv->time_string[i + 1];724 gchar evalchar = time_str[i + 1];
704725
705 /* If we're using alternate formats we need to skip those characters */726 /* If we're using alternate formats we need to skip those characters */
706 if (evalchar == 'E' || evalchar == 'O') {727 if (evalchar == 'E' || evalchar == 'O') {
707 if (i + 2 < strlength) {728 if (i + 2 < strlength) {
708 evalchar = self->priv->time_string[i + 2];729 evalchar = time_str[i + 2];
709 } else {730 } else {
710 continue;731 continue;
711 }732 }
@@ -799,7 +820,10 @@
799 GtkStyle * style = gtk_widget_get_style(GTK_WIDGET(self->priv->label));820 GtkStyle * style = gtk_widget_get_style(GTK_WIDGET(self->priv->label));
800 PangoContext * context = gtk_widget_get_pango_context(GTK_WIDGET(self->priv->label));821 PangoContext * context = gtk_widget_get_pango_context(GTK_WIDGET(self->priv->label));
801 gint * max_width = &(self->priv->max_width);822 gint * max_width = &(self->priv->max_width);
802 gint posibilitymask = generate_strftime_bitmask(self);823 gint posibilitymask = generate_strftime_bitmask(self->priv->time_string);
824
825 /* Reset max width */
826 *max_width = 0;
803827
804 /* Build the array of possibilities that we want to test */828 /* Build the array of possibilities that we want to test */
805 GArray * timevals = g_array_new(FALSE, TRUE, sizeof(struct tm));829 GArray * timevals = g_array_new(FALSE, TRUE, sizeof(struct tm));
@@ -808,9 +832,9 @@
808 g_debug("Checking against %d posible times", timevals->len);832 g_debug("Checking against %d posible times", timevals->len);
809 gint check_time;833 gint check_time;
810 for (check_time = 0; check_time < timevals->len; check_time++) {834 for (check_time = 0; check_time < timevals->len; check_time++) {
811 gchar longstr[128];835 gchar longstr[256];
812 strftime(longstr, 128, self->priv->time_string, &(g_array_index(timevals, struct tm, check_time)));836 strftime(longstr, 256, self->priv->time_string, &(g_array_index(timevals, struct tm, check_time)));
813 837
814 gchar * utf8 = g_locale_to_utf8(longstr, -1, NULL, NULL, NULL);838 gchar * utf8 = g_locale_to_utf8(longstr, -1, NULL, NULL, NULL);
815 gint length = measure_string(style, context, utf8);839 gint length = measure_string(style, context, utf8);
816 g_free(utf8);840 g_free(utf8);
@@ -840,6 +864,22 @@
840 return;864 return;
841}865}
842866
867
868static void
869update_text_gravity (GtkWidget *widget, GdkScreen *previous_screen, gpointer data)
870{
871 IndicatorDatetime * self = INDICATOR_DATETIME(data);
872 if (self->priv->label == NULL) return;
873
874 PangoLayout *layout;
875 PangoContext *context;
876
877 layout = gtk_label_get_layout (GTK_LABEL(self->priv->label));
878 context = pango_layout_get_context(layout);
879 pango_context_set_base_gravity(context, PANGO_GRAVITY_AUTO);
880}
881
882
843/* Tries to figure out what our format string should be. Lots883/* Tries to figure out what our format string should be. Lots
844 of translator comments in here. */884 of translator comments in here. */
845static gchar *885static gchar *
@@ -959,8 +999,10 @@
959 /* If there's not a label, we'll build ourselves one */999 /* If there's not a label, we'll build ourselves one */
960 if (self->priv->label == NULL) {1000 if (self->priv->label == NULL) {
961 self->priv->label = GTK_LABEL(gtk_label_new("Time"));1001 self->priv->label = GTK_LABEL(gtk_label_new("Time"));
1002 gtk_label_set_justify (GTK_LABEL(self->priv->label), GTK_JUSTIFY_CENTER);
962 g_object_ref(G_OBJECT(self->priv->label));1003 g_object_ref(G_OBJECT(self->priv->label));
963 g_signal_connect(G_OBJECT(self->priv->label), "style-set", G_CALLBACK(style_changed), self);1004 g_signal_connect(G_OBJECT(self->priv->label), "style-set", G_CALLBACK(style_changed), self);
1005 g_signal_connect(G_OBJECT(self->priv->label), "screen-changed", G_CALLBACK(update_text_gravity), self);
964 guess_label_size(self);1006 guess_label_size(self);
965 update_label(self);1007 update_label(self);
966 gtk_widget_show(GTK_WIDGET(self->priv->label));1008 gtk_widget_show(GTK_WIDGET(self->priv->label));

Subscribers

People subscribed via source and target branches