media: use GIcons instead of rendering pixbufs ourselves

This simplifies the code a bit, and avoid unnecesary work.
This commit is contained in:
Cosimo Cecchi 2010-11-14 14:38:27 +01:00
parent b70172e707
commit 5f6c1d2b50

View file

@ -47,7 +47,7 @@ enum {
}; };
enum { enum {
COLUMN_AUTORUN_PIXBUF, COLUMN_AUTORUN_GICON,
COLUMN_AUTORUN_NAME, COLUMN_AUTORUN_NAME,
COLUMN_AUTORUN_APP_INFO, COLUMN_AUTORUN_APP_INFO,
COLUMN_AUTORUN_X_CONTENT_TYPE, COLUMN_AUTORUN_X_CONTENT_TYPE,
@ -416,8 +416,7 @@ prepare_combo_box (CcMediaPanel *self,
GAppInfo *default_app_info; GAppInfo *default_app_info;
GtkListStore *list_store; GtkListStore *list_store;
GtkTreeIter iter; GtkTreeIter iter;
GdkPixbuf *pixbuf; GIcon *icon;
int icon_size, width, height;
int set_active; int set_active;
int n; int n;
int num_apps; int num_apps;
@ -433,9 +432,6 @@ prepare_combo_box (CcMediaPanel *self,
&pref_start_app, &pref_ignore, &pref_open_folder); &pref_start_app, &pref_ignore, &pref_open_folder);
pref_ask = !pref_start_app && !pref_ignore && !pref_open_folder; pref_ask = !pref_start_app && !pref_ignore && !pref_open_folder;
gtk_icon_size_lookup (GTK_ICON_SIZE_MENU, &width, &height);
icon_size = MAX (width, height);
set_active = -1; set_active = -1;
data = NULL; data = NULL;
new_data = TRUE; new_data = TRUE;
@ -445,7 +441,7 @@ prepare_combo_box (CcMediaPanel *self,
num_apps = g_list_length (app_info_list); num_apps = g_list_length (app_info_list);
list_store = gtk_list_store_new (5, list_store = gtk_list_store_new (5,
GDK_TYPE_PIXBUF, G_TYPE_ICON,
G_TYPE_STRING, G_TYPE_STRING,
G_TYPE_APP_INFO, G_TYPE_APP_INFO,
G_TYPE_STRING, G_TYPE_STRING,
@ -454,71 +450,58 @@ prepare_combo_box (CcMediaPanel *self,
/* no apps installed */ /* no apps installed */
if (num_apps == 0) { if (num_apps == 0) {
gtk_list_store_append (list_store, &iter); gtk_list_store_append (list_store, &iter);
pixbuf = gtk_icon_theme_load_icon (gtk_icon_theme_get_default (), icon = g_themed_icon_new (GTK_STOCK_DIALOG_ERROR);
GTK_STOCK_DIALOG_ERROR,
icon_size,
0,
NULL);
/* TODO: integrate with PackageKit-gnome to find applications */ /* TODO: integrate with PackageKit-gnome to find applications */
gtk_list_store_set (list_store, &iter, gtk_list_store_set (list_store, &iter,
COLUMN_AUTORUN_PIXBUF, pixbuf, COLUMN_AUTORUN_GICON, icon,
COLUMN_AUTORUN_NAME, _("No applications found"), COLUMN_AUTORUN_NAME, _("No applications found"),
COLUMN_AUTORUN_APP_INFO, NULL, COLUMN_AUTORUN_APP_INFO, NULL,
COLUMN_AUTORUN_X_CONTENT_TYPE, x_content_type, COLUMN_AUTORUN_X_CONTENT_TYPE, x_content_type,
COLUMN_AUTORUN_ITEM_TYPE, AUTORUN_ASK, COLUMN_AUTORUN_ITEM_TYPE, AUTORUN_ASK,
-1); -1);
g_object_unref (pixbuf); g_object_unref (icon);
} else { } else {
gtk_list_store_append (list_store, &iter); gtk_list_store_append (list_store, &iter);
pixbuf = gtk_icon_theme_load_icon (gtk_icon_theme_get_default (), icon = g_themed_icon_new (GTK_STOCK_DIALOG_QUESTION);
GTK_STOCK_DIALOG_QUESTION,
icon_size,
0,
NULL);
gtk_list_store_set (list_store, &iter, gtk_list_store_set (list_store, &iter,
COLUMN_AUTORUN_PIXBUF, pixbuf, COLUMN_AUTORUN_GICON, icon,
COLUMN_AUTORUN_NAME, _("Ask what to do"), COLUMN_AUTORUN_NAME, _("Ask what to do"),
COLUMN_AUTORUN_APP_INFO, NULL, COLUMN_AUTORUN_APP_INFO, NULL,
COLUMN_AUTORUN_X_CONTENT_TYPE, x_content_type, COLUMN_AUTORUN_X_CONTENT_TYPE, x_content_type,
COLUMN_AUTORUN_ITEM_TYPE, AUTORUN_ASK, COLUMN_AUTORUN_ITEM_TYPE, AUTORUN_ASK,
-1); -1);
g_object_unref (pixbuf); g_object_unref (icon);
gtk_list_store_append (list_store, &iter); gtk_list_store_append (list_store, &iter);
pixbuf = gtk_icon_theme_load_icon (gtk_icon_theme_get_default (), icon = g_themed_icon_new (GTK_STOCK_CLOSE);
GTK_STOCK_CLOSE,
icon_size,
0,
NULL);
gtk_list_store_set (list_store, &iter, gtk_list_store_set (list_store, &iter,
COLUMN_AUTORUN_PIXBUF, pixbuf, COLUMN_AUTORUN_GICON, icon,
COLUMN_AUTORUN_NAME, _("Do Nothing"), COLUMN_AUTORUN_NAME, _("Do Nothing"),
COLUMN_AUTORUN_APP_INFO, NULL, COLUMN_AUTORUN_APP_INFO, NULL,
COLUMN_AUTORUN_X_CONTENT_TYPE, x_content_type, COLUMN_AUTORUN_X_CONTENT_TYPE, x_content_type,
COLUMN_AUTORUN_ITEM_TYPE, AUTORUN_IGNORE, COLUMN_AUTORUN_ITEM_TYPE, AUTORUN_IGNORE,
-1); -1);
g_object_unref (pixbuf); g_object_unref (icon);
gtk_list_store_append (list_store, &iter); gtk_list_store_append (list_store, &iter);
pixbuf = gtk_icon_theme_load_icon (gtk_icon_theme_get_default (), icon = g_themed_icon_new ("folder-open");
"folder-open",
icon_size,
0,
NULL);
gtk_list_store_set (list_store, &iter, gtk_list_store_set (list_store, &iter,
COLUMN_AUTORUN_PIXBUF, pixbuf, COLUMN_AUTORUN_GICON, icon,
COLUMN_AUTORUN_NAME, _("Open Folder"), COLUMN_AUTORUN_NAME, _("Open Folder"),
COLUMN_AUTORUN_APP_INFO, NULL, COLUMN_AUTORUN_APP_INFO, NULL,
COLUMN_AUTORUN_X_CONTENT_TYPE, x_content_type, COLUMN_AUTORUN_X_CONTENT_TYPE, x_content_type,
COLUMN_AUTORUN_ITEM_TYPE, AUTORUN_OPEN_FOLDER, COLUMN_AUTORUN_ITEM_TYPE, AUTORUN_OPEN_FOLDER,
-1); -1);
g_object_unref (pixbuf); g_object_unref (icon);
gtk_list_store_append (list_store, &iter); gtk_list_store_append (list_store, &iter);
gtk_list_store_set (list_store, &iter, gtk_list_store_set (list_store, &iter,
COLUMN_AUTORUN_PIXBUF, NULL, COLUMN_AUTORUN_GICON, NULL,
COLUMN_AUTORUN_NAME, NULL, COLUMN_AUTORUN_NAME, NULL,
COLUMN_AUTORUN_APP_INFO, NULL, COLUMN_AUTORUN_APP_INFO, NULL,
COLUMN_AUTORUN_X_CONTENT_TYPE, NULL, COLUMN_AUTORUN_X_CONTENT_TYPE, NULL,
@ -526,8 +509,6 @@ prepare_combo_box (CcMediaPanel *self,
-1); -1);
for (l = app_info_list, n = 4; l != NULL; l = l->next, n++) { for (l = app_info_list, n = 4; l != NULL; l = l->next, n++) {
GIcon *icon;
GtkIconInfo *icon_info;
char *open_string; char *open_string;
GAppInfo *app_info = l->data; GAppInfo *app_info = l->data;
@ -537,27 +518,17 @@ prepare_combo_box (CcMediaPanel *self,
*/ */
icon = g_app_info_get_icon (app_info); icon = g_app_info_get_icon (app_info);
icon_info = gtk_icon_theme_lookup_by_gicon (gtk_icon_theme_get_default (),
icon,
icon_size,
GTK_ICON_LOOKUP_GENERIC_FALLBACK |
GTK_ICON_LOOKUP_FORCE_SIZE);
pixbuf = gtk_icon_info_load_icon (icon_info, NULL);
gtk_icon_info_free (icon_info);
open_string = g_strdup_printf (_("Open %s"), g_app_info_get_display_name (app_info)); open_string = g_strdup_printf (_("Open %s"), g_app_info_get_display_name (app_info));
gtk_list_store_append (list_store, &iter); gtk_list_store_append (list_store, &iter);
gtk_list_store_set (list_store, &iter, gtk_list_store_set (list_store, &iter,
COLUMN_AUTORUN_PIXBUF, pixbuf, COLUMN_AUTORUN_GICON, icon,
COLUMN_AUTORUN_NAME, open_string, COLUMN_AUTORUN_NAME, open_string,
COLUMN_AUTORUN_APP_INFO, app_info, COLUMN_AUTORUN_APP_INFO, app_info,
COLUMN_AUTORUN_X_CONTENT_TYPE, x_content_type, COLUMN_AUTORUN_X_CONTENT_TYPE, x_content_type,
COLUMN_AUTORUN_ITEM_TYPE, AUTORUN_APP, COLUMN_AUTORUN_ITEM_TYPE, AUTORUN_APP,
-1); -1);
if (pixbuf != NULL) {
g_object_unref (pixbuf);
}
g_free (open_string); g_free (open_string);
if (g_app_info_equal (app_info, default_app_info)) { if (g_app_info_equal (app_info, default_app_info)) {
@ -568,7 +539,7 @@ prepare_combo_box (CcMediaPanel *self,
gtk_list_store_append (list_store, &iter); gtk_list_store_append (list_store, &iter);
gtk_list_store_set (list_store, &iter, gtk_list_store_set (list_store, &iter,
COLUMN_AUTORUN_PIXBUF, NULL, COLUMN_AUTORUN_GICON, NULL,
COLUMN_AUTORUN_NAME, NULL, COLUMN_AUTORUN_NAME, NULL,
COLUMN_AUTORUN_APP_INFO, NULL, COLUMN_AUTORUN_APP_INFO, NULL,
COLUMN_AUTORUN_X_CONTENT_TYPE, NULL, COLUMN_AUTORUN_X_CONTENT_TYPE, NULL,
@ -576,19 +547,16 @@ prepare_combo_box (CcMediaPanel *self,
-1); -1);
gtk_list_store_append (list_store, &iter); gtk_list_store_append (list_store, &iter);
pixbuf = gtk_icon_theme_load_icon (gtk_icon_theme_get_default (), icon = g_themed_icon_new ("application-x-executable");
"application-x-executable",
icon_size,
0,
NULL);
gtk_list_store_set (list_store, &iter, gtk_list_store_set (list_store, &iter,
COLUMN_AUTORUN_PIXBUF, pixbuf, COLUMN_AUTORUN_GICON, icon,
COLUMN_AUTORUN_NAME, _("Open with other Application..."), COLUMN_AUTORUN_NAME, _("Open with other Application..."),
COLUMN_AUTORUN_APP_INFO, NULL, COLUMN_AUTORUN_APP_INFO, NULL,
COLUMN_AUTORUN_X_CONTENT_TYPE, x_content_type, COLUMN_AUTORUN_X_CONTENT_TYPE, x_content_type,
COLUMN_AUTORUN_ITEM_TYPE, AUTORUN_OTHER_APP, COLUMN_AUTORUN_ITEM_TYPE, AUTORUN_OTHER_APP,
-1); -1);
g_object_unref (pixbuf); g_object_unref (icon);
if (default_app_info != NULL) { if (default_app_info != NULL) {
g_object_unref (default_app_info); g_object_unref (default_app_info);
@ -603,7 +571,7 @@ prepare_combo_box (CcMediaPanel *self,
renderer = gtk_cell_renderer_pixbuf_new (); renderer = gtk_cell_renderer_pixbuf_new ();
gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combo_box), renderer, FALSE); gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combo_box), renderer, FALSE);
gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combo_box), renderer, gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combo_box), renderer,
"pixbuf", COLUMN_AUTORUN_PIXBUF, "gicon", COLUMN_AUTORUN_GICON,
NULL); NULL);
renderer = gtk_cell_renderer_text_new (); renderer = gtk_cell_renderer_text_new ();
gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combo_box), renderer, TRUE); gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combo_box), renderer, TRUE);
@ -683,9 +651,9 @@ media_panel_setup (CcMediaPanel *self)
other_type_combo_box = GTK_WIDGET (gtk_builder_get_object (builder, "media_other_type_combobox")); other_type_combo_box = GTK_WIDGET (gtk_builder_get_object (builder, "media_other_type_combobox"));
other_type_list_store = gtk_list_store_new (3, other_type_list_store = gtk_list_store_new (3,
GDK_TYPE_PIXBUF, G_TYPE_ICON,
G_TYPE_STRING, G_TYPE_STRING,
G_TYPE_STRING); G_TYPE_STRING);
gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (other_type_list_store), gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (other_type_list_store),
@ -698,9 +666,6 @@ media_panel_setup (CcMediaPanel *self)
char *content_type = l->data; char *content_type = l->data;
char *description; char *description;
GIcon *icon; GIcon *icon;
GtkIconInfo *icon_info;
GdkPixbuf *pixbuf;
int icon_size, width, height;
if (!g_str_has_prefix (content_type, "x-content/")) if (!g_str_has_prefix (content_type, "x-content/"))
continue; continue;
@ -710,54 +675,39 @@ media_panel_setup (CcMediaPanel *self)
} }
} }
gtk_icon_size_lookup (GTK_ICON_SIZE_MENU, &width, &height);
icon_size = MAX (width, height);
pixbuf = NULL;
description = g_content_type_get_description (content_type); description = g_content_type_get_description (content_type);
gtk_list_store_append (other_type_list_store, &iter); gtk_list_store_append (other_type_list_store, &iter);
icon = g_content_type_get_icon (content_type); icon = g_content_type_get_icon (content_type);
if (icon != NULL) {
icon_info = gtk_icon_theme_lookup_by_gicon (gtk_icon_theme_get_default (),
icon, icon_size,
GTK_ICON_LOOKUP_GENERIC_FALLBACK |
GTK_ICON_LOOKUP_FORCE_SIZE);
g_object_unref (icon);
if (icon_info != NULL) { gtk_list_store_set (other_type_list_store, &iter,
pixbuf = gtk_icon_info_load_icon (icon_info, NULL); 0, icon,
gtk_icon_info_free (icon_info); 1, description,
} 2, content_type,
}
gtk_list_store_set (other_type_list_store, &iter,
0, pixbuf,
1, description,
2, content_type,
-1); -1);
if (pixbuf != NULL)
g_object_unref (pixbuf);
g_free (description); g_free (description);
g_object_unref (icon);
skip: skip:
; ;
} }
g_list_foreach (content_types, (GFunc) g_free, NULL);
g_list_free (content_types);
gtk_combo_box_set_model (GTK_COMBO_BOX (other_type_combo_box), GTK_TREE_MODEL (other_type_list_store)); g_list_free_full (content_types, g_free);
gtk_combo_box_set_model (GTK_COMBO_BOX (other_type_combo_box),
GTK_TREE_MODEL (other_type_list_store));
renderer = gtk_cell_renderer_pixbuf_new (); renderer = gtk_cell_renderer_pixbuf_new ();
gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (other_type_combo_box), renderer, FALSE); gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (other_type_combo_box), renderer, FALSE);
gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (other_type_combo_box), renderer, gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (other_type_combo_box), renderer,
"pixbuf", 0, "gicon", 0,
NULL); NULL);
renderer = gtk_cell_renderer_text_new (); renderer = gtk_cell_renderer_text_new ();
gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (other_type_combo_box), renderer, TRUE); gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (other_type_combo_box), renderer, TRUE);
gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (other_type_combo_box), renderer, gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (other_type_combo_box), renderer,
"text", 1, "text", 1,
NULL); NULL);
g_signal_connect (G_OBJECT (other_type_combo_box), g_signal_connect (other_type_combo_box,
"changed", "changed",
G_CALLBACK (other_type_combo_box_changed), G_CALLBACK (other_type_combo_box_changed),
self); self);