From 9c46c111666900bc30bf3a5062148f3f6dfc0e37 Mon Sep 17 00:00:00 2001 From: Ted Gould Date: Thu, 5 Aug 2010 11:18:07 -0500 Subject: Storing the guide and using it to bound the size of the label. --- src/indicator-application.c | 56 ++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 55 insertions(+), 1 deletion(-) diff --git a/src/indicator-application.c b/src/indicator-application.c index 5a6bfaf..1fd043d 100644 --- a/src/indicator-application.c +++ b/src/indicator-application.c @@ -88,6 +88,7 @@ struct _ApplicationEntry { gboolean old_service; gchar * dbusobject; gchar * dbusaddress; + gchar * guide; }; #define INDICATOR_APPLICATION_GET_PRIVATE(o) \ @@ -431,6 +432,46 @@ application_added_search (gconstpointer a, gconstpointer b) return -1; } +/* Does a quick meausre of how big the string is in + pixels with a Pango layout */ +static gint +measure_string (GtkStyle * style, PangoContext * context, const gchar * string) +{ + PangoLayout * layout = pango_layout_new(context); + pango_layout_set_text(layout, string, -1); + pango_layout_set_font_description(layout, style->font_desc); + + gint width; + pango_layout_get_pixel_size(layout, &width, NULL); + g_object_unref(layout); + return width; +} + +/* Try to get a good guess at what a maximum width of the entire + string would be. */ +static void +guess_label_size (ApplicationEntry * app) +{ + /* This is during startup. */ + if (app->entry.label == NULL) return; + + GtkStyle * style = gtk_widget_get_style(GTK_WIDGET(app->entry.label)); + PangoContext * context = gtk_widget_get_pango_context(GTK_WIDGET(app->entry.label)); + + gint length = measure_string(style, context, gtk_label_get_text(app->entry.label)); + + if (app->guide != NULL) { + gint guidelen = measure_string(style, context, app->guide); + if (guidelen > length) { + length = guidelen; + } + } + + gtk_widget_set_size_request(GTK_WIDGET(app->entry.label), length, -1); + + return; +} + /* Here we respond to new applications by building up the ApplicationEntry and signaling the indicator host that we've got a new indicator. */ @@ -465,6 +506,7 @@ application_added (DBusGProxy * proxy, const gchar * iconname, gint position, co app->dbusaddress = g_strdup(dbusaddress); app->dbusobject = g_strdup(dbusobject); + app->guide = NULL; /* We make a long name using the suffix, and if that icon is available we want to use it. Otherwise we'll @@ -484,7 +526,16 @@ application_added (DBusGProxy * proxy, const gchar * iconname, gint position, co app->entry.label = GTK_LABEL(gtk_label_new(label)); gtk_widget_show(GTK_WIDGET(app->entry.label)); - /* TODO: Use guide to size the label better */ + if (app->guide != NULL) { + g_free(app->guide); + app->guide = NULL; + } + + if (guide != NULL) { + app->guide = g_strdup(guide); + } + + guess_label_size(app); } app->entry.menu = GTK_MENU(dbusmenu_gtkmenu_new((gchar *)dbusaddress, (gchar *)dbusobject)); @@ -529,6 +580,9 @@ application_removed (DBusGProxy * proxy, gint position, IndicatorApplication * a if (app->dbusobject != NULL) { g_free(app->dbusobject); } + if (app->guide != NULL) { + g_free(app->guide); + } if (app->entry.image != NULL) { g_object_unref(G_OBJECT(app->entry.image)); } -- cgit v1.2.3 From 62a5f41b117ec2f8a2c5258522951fe94acfffea Mon Sep 17 00:00:00 2001 From: Ted Gould Date: Thu, 5 Aug 2010 11:25:25 -0500 Subject: Adding updating the guide and label size to the label update signal --- src/indicator-application.c | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/indicator-application.c b/src/indicator-application.c index 1fd043d..461d32e 100644 --- a/src/indicator-application.c +++ b/src/indicator-application.c @@ -617,6 +617,17 @@ application_label_changed (DBusGProxy * proxy, gint position, const gchar * labe /* TODO: Handle the case where we didn't have a label */ } + if (app->guide != NULL) { + g_free(app->guide); + app->guide = NULL; + } + + if (guide != NULL) { + app->guide = g_strdup(guide); + } + + guess_label_size(app); + return; } -- cgit v1.2.3 From a644617fdbc2257a9e6219532a5a6bfaf6decc90 Mon Sep 17 00:00:00 2001 From: Ted Gould Date: Thu, 5 Aug 2010 12:08:51 -0500 Subject: Building a label in the case that we don't have one already. --- src/indicator-application.c | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/src/indicator-application.c b/src/indicator-application.c index 461d32e..788c5e9 100644 --- a/src/indicator-application.c +++ b/src/indicator-application.c @@ -587,7 +587,6 @@ application_removed (DBusGProxy * proxy, gint position, IndicatorApplication * a g_object_unref(G_OBJECT(app->entry.image)); } if (app->entry.label != NULL) { - g_warning("Odd, an application indicator with a label?"); g_object_unref(G_OBJECT(app->entry.label)); } if (app->entry.menu != NULL) { @@ -610,11 +609,19 @@ application_label_changed (DBusGProxy * proxy, gint position, const gchar * labe g_warning("Unable to find application at position: %d", position); return; } + + /* TODO: Be able to remove labels */ if (app->entry.label != NULL) { gtk_label_set_text(app->entry.label, label); } else { - /* TODO: Handle the case where we didn't have a label */ + app->entry.label = GTK_LABEL(gtk_label_new(label)); + gtk_widget_show(GTK_WIDGET(app->entry.label)); + + /* We tell listeners that it got removed and readded so + that we can get them to re-look at the new label. */ + g_signal_emit(G_OBJECT(application), INDICATOR_OBJECT_SIGNAL_ENTRY_REMOVED_ID, 0, &(app->entry), TRUE); + g_signal_emit(G_OBJECT(application), INDICATOR_OBJECT_SIGNAL_ENTRY_ADDED_ID, 0, &(app->entry), TRUE); } if (app->guide != NULL) { @@ -622,7 +629,7 @@ application_label_changed (DBusGProxy * proxy, gint position, const gchar * labe app->guide = NULL; } - if (guide != NULL) { + if (guide != NULL && guide[0] != '\0') { app->guide = g_strdup(guide); } -- cgit v1.2.3 From 08a2c880e92c9ac143e83c862853c07068661fb9 Mon Sep 17 00:00:00 2001 From: Ted Gould Date: Thu, 5 Aug 2010 12:20:25 -0500 Subject: Some slight refactoring and comments along with the ability to remove the entries and recreate them. --- src/indicator-application.c | 35 +++++++++++++++++++++++++---------- 1 file changed, 25 insertions(+), 10 deletions(-) diff --git a/src/indicator-application.c b/src/indicator-application.c index 788c5e9..a4933ac 100644 --- a/src/indicator-application.c +++ b/src/indicator-application.c @@ -610,20 +610,34 @@ application_label_changed (DBusGProxy * proxy, gint position, const gchar * labe return; } - /* TODO: Be able to remove labels */ - - if (app->entry.label != NULL) { - gtk_label_set_text(app->entry.label, label); + if (label == NULL || label[0] == '\0') { + /* No label, let's see if we need to delete the old one */ + if (app->entry.label != NULL) { + g_object_unref(G_OBJECT(app->entry.label)); + app->entry.label = NULL; + + /* We tell listeners that it got removed and readded so + that we can get them to remove the old label. */ + g_signal_emit(G_OBJECT(application), INDICATOR_OBJECT_SIGNAL_ENTRY_REMOVED_ID, 0, &(app->entry), TRUE); + g_signal_emit(G_OBJECT(application), INDICATOR_OBJECT_SIGNAL_ENTRY_ADDED_ID, 0, &(app->entry), TRUE); + } } else { - app->entry.label = GTK_LABEL(gtk_label_new(label)); - gtk_widget_show(GTK_WIDGET(app->entry.label)); + /* We've got a label, is this just an update or is + it a new thing. */ + if (app->entry.label != NULL) { + gtk_label_set_text(app->entry.label, label); + } else { + app->entry.label = GTK_LABEL(gtk_label_new(label)); + gtk_widget_show(GTK_WIDGET(app->entry.label)); - /* We tell listeners that it got removed and readded so - that we can get them to re-look at the new label. */ - g_signal_emit(G_OBJECT(application), INDICATOR_OBJECT_SIGNAL_ENTRY_REMOVED_ID, 0, &(app->entry), TRUE); - g_signal_emit(G_OBJECT(application), INDICATOR_OBJECT_SIGNAL_ENTRY_ADDED_ID, 0, &(app->entry), TRUE); + /* We tell listeners that it got removed and readded so + that we can get them to re-look at the new label. */ + g_signal_emit(G_OBJECT(application), INDICATOR_OBJECT_SIGNAL_ENTRY_REMOVED_ID, 0, &(app->entry), TRUE); + g_signal_emit(G_OBJECT(application), INDICATOR_OBJECT_SIGNAL_ENTRY_ADDED_ID, 0, &(app->entry), TRUE); + } } + /* Copy the guide if we have one */ if (app->guide != NULL) { g_free(app->guide); app->guide = NULL; @@ -633,6 +647,7 @@ application_label_changed (DBusGProxy * proxy, gint position, const gchar * labe app->guide = g_strdup(guide); } + /* Protected against not having a label */ guess_label_size(app); return; -- cgit v1.2.3 From 38074f5f2b53b969b9bdd1759ac6c94cfec68956 Mon Sep 17 00:00:00 2001 From: Ted Gould Date: Thu, 5 Aug 2010 12:20:56 -0500 Subject: Not really sure what this TODO meant, so I'm removing it. --- src/indicator-application.c | 1 - 1 file changed, 1 deletion(-) diff --git a/src/indicator-application.c b/src/indicator-application.c index a4933ac..939537e 100644 --- a/src/indicator-application.c +++ b/src/indicator-application.c @@ -548,7 +548,6 @@ application_added (DBusGProxy * proxy, const gchar * iconname, gint position, co priv->applications = g_list_insert(priv->applications, app, position); - /* TODO: Need to deal with position here somehow */ g_signal_emit(G_OBJECT(application), INDICATOR_OBJECT_SIGNAL_ENTRY_ADDED_ID, 0, &(app->entry), TRUE); return; } -- cgit v1.2.3 From b17e62540f4ac630d7af7386409779271f3a2a57 Mon Sep 17 00:00:00 2001 From: Ted Gould Date: Thu, 5 Aug 2010 13:23:12 -0500 Subject: Reshuffling the signaling and making sure to ref the label. --- src/indicator-application.c | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/src/indicator-application.c b/src/indicator-application.c index 939537e..2b32a1b 100644 --- a/src/indicator-application.c +++ b/src/indicator-application.c @@ -524,6 +524,7 @@ application_added (DBusGProxy * proxy, const gchar * iconname, gint position, co app->entry.label = NULL; } else { app->entry.label = GTK_LABEL(gtk_label_new(label)); + g_object_ref(G_OBJECT(app->entry.label)); gtk_widget_show(GTK_WIDGET(app->entry.label)); if (app->guide != NULL) { @@ -603,6 +604,7 @@ application_label_changed (DBusGProxy * proxy, gint position, const gchar * labe { IndicatorApplicationPrivate * priv = INDICATOR_APPLICATION_GET_PRIVATE(application); ApplicationEntry * app = (ApplicationEntry *)g_list_nth_data(priv->applications, position); + gboolean signal_reload = FALSE; if (app == NULL) { g_warning("Unable to find application at position: %d", position); @@ -615,10 +617,7 @@ application_label_changed (DBusGProxy * proxy, gint position, const gchar * labe g_object_unref(G_OBJECT(app->entry.label)); app->entry.label = NULL; - /* We tell listeners that it got removed and readded so - that we can get them to remove the old label. */ - g_signal_emit(G_OBJECT(application), INDICATOR_OBJECT_SIGNAL_ENTRY_REMOVED_ID, 0, &(app->entry), TRUE); - g_signal_emit(G_OBJECT(application), INDICATOR_OBJECT_SIGNAL_ENTRY_ADDED_ID, 0, &(app->entry), TRUE); + signal_reload = TRUE; } } else { /* We've got a label, is this just an update or is @@ -627,12 +626,10 @@ application_label_changed (DBusGProxy * proxy, gint position, const gchar * labe gtk_label_set_text(app->entry.label, label); } else { app->entry.label = GTK_LABEL(gtk_label_new(label)); + g_object_ref(G_OBJECT(app->entry.label)); gtk_widget_show(GTK_WIDGET(app->entry.label)); - /* We tell listeners that it got removed and readded so - that we can get them to re-look at the new label. */ - g_signal_emit(G_OBJECT(application), INDICATOR_OBJECT_SIGNAL_ENTRY_REMOVED_ID, 0, &(app->entry), TRUE); - g_signal_emit(G_OBJECT(application), INDICATOR_OBJECT_SIGNAL_ENTRY_ADDED_ID, 0, &(app->entry), TRUE); + signal_reload = TRUE; } } @@ -649,6 +646,13 @@ application_label_changed (DBusGProxy * proxy, gint position, const gchar * labe /* Protected against not having a label */ guess_label_size(app); + if (signal_reload) { + /* Telling the listener that this has been removed, and then + readded to make it reparse the entry. */ + g_signal_emit(G_OBJECT(application), INDICATOR_OBJECT_SIGNAL_ENTRY_REMOVED_ID, 0, &(app->entry), TRUE); + g_signal_emit(G_OBJECT(application), INDICATOR_OBJECT_SIGNAL_ENTRY_ADDED_ID, 0, &(app->entry), TRUE); + } + return; } -- cgit v1.2.3 From 54c6103dc570dbbee0ba8402d3c7156ac40dc5a4 Mon Sep 17 00:00:00 2001 From: Ted Gould Date: Thu, 5 Aug 2010 13:56:56 -0500 Subject: Showing and hiding the entries with removal. --- src/indicator-application.c | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/src/indicator-application.c b/src/indicator-application.c index 2b32a1b..31c3aa9 100644 --- a/src/indicator-application.c +++ b/src/indicator-application.c @@ -649,7 +649,28 @@ application_label_changed (DBusGProxy * proxy, gint position, const gchar * labe if (signal_reload) { /* Telling the listener that this has been removed, and then readded to make it reparse the entry. */ + if (app->entry.label != NULL) { + gtk_widget_hide(GTK_WIDGET(app->entry.label)); + } + + if (app->entry.image != NULL) { + gtk_widget_hide(GTK_WIDGET(app->entry.image)); + } + + if (app->entry.menu != NULL) { + gtk_menu_detach(app->entry.menu); + } + g_signal_emit(G_OBJECT(application), INDICATOR_OBJECT_SIGNAL_ENTRY_REMOVED_ID, 0, &(app->entry), TRUE); + + if (app->entry.label != NULL) { + gtk_widget_show(GTK_WIDGET(app->entry.label)); + } + + if (app->entry.image != NULL) { + gtk_widget_show(GTK_WIDGET(app->entry.image)); + } + g_signal_emit(G_OBJECT(application), INDICATOR_OBJECT_SIGNAL_ENTRY_ADDED_ID, 0, &(app->entry), TRUE); } -- cgit v1.2.3