← Back to team overview

ayatana-commits team mailing list archive

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

 

Treviño (Marco Trevisan) has proposed merging lp:~3v1n0/indicator-datetime/clock-label-improvements into lp:indicator-datetime.

Requested reviews:
  Indicator Applet Developers (indicator-applet-developers)

For more details, see:
https://code.launchpad.net/~3v1n0/indicator-datetime/clock-label-improvements/+merge/44534

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!).
-- 
https://code.launchpad.net/~3v1n0/indicator-datetime/clock-label-improvements/+merge/44534
Your team ayatana-commits is subscribed to branch lp:indicator-datetime.
=== 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 @@
 	gboolean show_date;
 	gboolean show_day;
 	gchar * custom_string;
+	gboolean custom_show_seconds;
 
 	guint idle_measure;
 	gint  max_width;
@@ -126,6 +127,18 @@
 #define INDICATOR_DATETIME_GET_PRIVATE(o) \
 (G_TYPE_INSTANCE_GET_PRIVATE ((o), INDICATOR_DATETIME_TYPE, IndicatorDatetimePrivate))
 
+enum {
+	STRFTIME_MASK_NONE    = 0,      /* Hours or minutes as we always test those */
+	STRFTIME_MASK_SECONDS = 1 << 0, /* Seconds count */
+	STRFTIME_MASK_AMPM    = 1 << 1, /* AM/PM counts */
+	STRFTIME_MASK_WEEK    = 1 << 2, /* Day of the week maters (Sat, Sun, etc.) */
+	STRFTIME_MASK_DAY     = 1 << 3, /* Day of the month counts (Feb 1st) */
+	STRFTIME_MASK_MONTH   = 1 << 4, /* Which month matters */
+	STRFTIME_MASK_YEAR    = 1 << 5, /* Which year matters */
+	/* Last entry, combines all previous */
+	STRFTIME_MASK_ALL     = (STRFTIME_MASK_SECONDS | STRFTIME_MASK_AMPM | STRFTIME_MASK_WEEK | STRFTIME_MASK_DAY | STRFTIME_MASK_MONTH | STRFTIME_MASK_YEAR)
+};
+
 GType indicator_datetime_get_type (void);
 
 static void indicator_datetime_class_init (IndicatorDatetimeClass *klass);
@@ -143,6 +156,7 @@
 static void guess_label_size              (IndicatorDatetime * self);
 static void setup_timer                   (IndicatorDatetime * self, struct tm * ltime);
 static void update_time                   (DBusGProxy * proxy, gpointer user_data);
+static gint generate_strftime_bitmask     (const char *time_str);
 
 /* Indicator Module Config */
 INDICATOR_SET_VERSION
@@ -225,6 +239,7 @@
 	self->priv->show_date = FALSE;
 	self->priv->show_day = FALSE;
 	self->priv->custom_string = g_strdup(DEFAULT_TIME_FORMAT);
+	self->priv->custom_show_seconds = FALSE;
 
 	self->priv->time_string = generate_format_string(self);
 
@@ -404,6 +419,7 @@
 		if (newval != self->priv->time_mode) {
 			update = TRUE;
 			self->priv->time_mode = newval;
+			setup_timer(self, NULL);			
 		}
 		break;
 	}
@@ -440,6 +456,8 @@
 				self->priv->custom_string = NULL;
 			}
 			self->priv->custom_string = g_strdup(newstr);
+			gint time_mask = generate_strftime_bitmask(newstr);
+			self->priv->custom_show_seconds = (time_mask & STRFTIME_MASK_SECONDS);
 			if (self->priv->time_mode == SETTINGS_TIME_CUSTOM) {
 				update = TRUE;
 				setup_timer(self, NULL);
@@ -539,9 +557,10 @@
 
 	if (self->priv->label == NULL) return NULL;
 
-	gchar longstr[128];
+	gchar longstr[256];
 	time_t t;
 	struct tm *ltime;
+	gboolean use_markup;
 
 	t = time(NULL);
 	ltime = localtime(&t);
@@ -551,10 +570,18 @@
 		return NULL;
 	}
 
-	strftime(longstr, 128, self->priv->time_string, ltime);
+	strftime(longstr, 256, self->priv->time_string, ltime);
 	
 	gchar * utf8 = g_locale_to_utf8(longstr, -1, NULL, NULL, NULL);
-	gtk_label_set_label(self->priv->label, utf8);
+
+	if (pango_parse_markup(utf8, -1, 0, NULL, NULL, NULL, NULL))
+		use_markup = TRUE;
+
+	if (use_markup)
+		gtk_label_set_markup(self->priv->label, utf8);
+	else
+		gtk_label_set_text(self->priv->label, utf8);
+
 	g_free(utf8);
 
 	if (self->priv->idle_measure == 0) {
@@ -595,7 +622,8 @@
 		self->priv->timer = 0;
 	}
 	
-	if (self->priv->show_seconds) {
+	if (self->priv->show_seconds ||
+		(self->priv->time_mode == SETTINGS_TIME_CUSTOM && self->priv->custom_show_seconds)) {
 		self->priv->timer = g_timeout_add_seconds(1, timer_func, self);
 	} else {
 		if (ltime == NULL) {
@@ -617,7 +645,12 @@
 measure_string (GtkStyle * style, PangoContext * context, const gchar * string)
 {
 	PangoLayout * layout = pango_layout_new(context);
-	pango_layout_set_text(layout, string, -1);
+
+	if (pango_parse_markup(string, -1, 0, NULL, NULL, NULL, NULL))
+		pango_layout_set_markup(layout, string, -1);
+	else
+		pango_layout_set_text(layout, string, -1);
+
 	pango_layout_set_font_description(layout, style->font_desc);
 
 	gint width;
@@ -634,18 +667,6 @@
 	gint mask;
 };
 
-enum {
-	STRFTIME_MASK_NONE    = 0,      /* Hours or minutes as we always test those */
-	STRFTIME_MASK_SECONDS = 1 << 0, /* Seconds count */
-	STRFTIME_MASK_AMPM    = 1 << 1, /* AM/PM counts */
-	STRFTIME_MASK_WEEK    = 1 << 2, /* Day of the week maters (Sat, Sun, etc.) */
-	STRFTIME_MASK_DAY     = 1 << 3, /* Day of the month counts (Feb 1st) */
-	STRFTIME_MASK_MONTH   = 1 << 4, /* Which month matters */
-	STRFTIME_MASK_YEAR    = 1 << 5, /* Which year matters */
-	/* Last entry, combines all previous */
-	STRFTIME_MASK_ALL     = (STRFTIME_MASK_SECONDS | STRFTIME_MASK_AMPM | STRFTIME_MASK_WEEK | STRFTIME_MASK_DAY | STRFTIME_MASK_MONTH | STRFTIME_MASK_YEAR)
-};
-
 /* A table taken from the man page of strftime to what the different
    characters can effect.  These are worst case in that we need to
    test the length based on all these things to ensure that we have
@@ -691,21 +712,21 @@
    ensure that we can figure out which of the things we
    need to check in determining the length. */
 static gint
-generate_strftime_bitmask (IndicatorDatetime * self)
+generate_strftime_bitmask (const char *time_str)
 {
 	gint retval = 0;
-	glong strlength = g_utf8_strlen(self->priv->time_string, -1);
+	glong strlength = g_utf8_strlen(time_str, -1);
 	gint i;
-	g_debug("Evaluating bitmask for '%s'", self->priv->time_string);
+	g_debug("Evaluating bitmask for '%s'", time_str);
 
 	for (i = 0; i < strlength; i++) {
-		if (self->priv->time_string[i] == '%' && i + 1 < strlength) {
-			gchar evalchar = self->priv->time_string[i + 1];
+		if (time_str[i] == '%' && i + 1 < strlength) {
+			gchar evalchar = time_str[i + 1];
 
 			/* If we're using alternate formats we need to skip those characters */
 			if (evalchar == 'E' || evalchar == 'O') {
 				if (i + 2 < strlength) {
-					evalchar = self->priv->time_string[i + 2];
+					evalchar = time_str[i + 2];
 				} else {
 					continue;
 				}
@@ -799,7 +820,10 @@
 	GtkStyle * style = gtk_widget_get_style(GTK_WIDGET(self->priv->label));
 	PangoContext * context = gtk_widget_get_pango_context(GTK_WIDGET(self->priv->label));
 	gint * max_width = &(self->priv->max_width);
-	gint posibilitymask = generate_strftime_bitmask(self);
+	gint posibilitymask = generate_strftime_bitmask(self->priv->time_string);
+
+	/* Reset max width */
+	*max_width = 0;
 
 	/* Build the array of possibilities that we want to test */
 	GArray * timevals = g_array_new(FALSE, TRUE, sizeof(struct tm));
@@ -808,9 +832,9 @@
 	g_debug("Checking against %d posible times", timevals->len);
 	gint check_time;
 	for (check_time = 0; check_time < timevals->len; check_time++) {
-		gchar longstr[128];
-		strftime(longstr, 128, self->priv->time_string, &(g_array_index(timevals, struct tm, check_time)));
-		
+		gchar longstr[256];
+		strftime(longstr, 256, self->priv->time_string, &(g_array_index(timevals, struct tm, check_time)));
+
 		gchar * utf8 = g_locale_to_utf8(longstr, -1, NULL, NULL, NULL);
 		gint length = measure_string(style, context, utf8);
 		g_free(utf8);
@@ -840,6 +864,22 @@
 	return;
 }
 
+
+static void
+update_text_gravity (GtkWidget *widget, GdkScreen *previous_screen, gpointer data)
+{
+	IndicatorDatetime * self = INDICATOR_DATETIME(data);
+	if (self->priv->label == NULL) return;
+
+	PangoLayout  *layout;
+	PangoContext *context;
+
+	layout = gtk_label_get_layout (GTK_LABEL(self->priv->label));
+	context = pango_layout_get_context(layout);
+	pango_context_set_base_gravity(context, PANGO_GRAVITY_AUTO);
+}
+
+
 /* Tries to figure out what our format string should be.  Lots
    of translator comments in here. */
 static gchar *
@@ -959,8 +999,10 @@
 	/* If there's not a label, we'll build ourselves one */
 	if (self->priv->label == NULL) {
 		self->priv->label = GTK_LABEL(gtk_label_new("Time"));
+		gtk_label_set_justify (GTK_LABEL(self->priv->label), GTK_JUSTIFY_CENTER);
 		g_object_ref(G_OBJECT(self->priv->label));
 		g_signal_connect(G_OBJECT(self->priv->label), "style-set", G_CALLBACK(style_changed), self);
+		g_signal_connect(G_OBJECT(self->priv->label), "screen-changed", G_CALLBACK(update_text_gravity), self);
 		guess_label_size(self);
 		update_label(self);
 		gtk_widget_show(GTK_WIDGET(self->priv->label));


Follow ups