From 8f080e890301c82b4be607528e156ffbebf48eab Mon Sep 17 00:00:00 2001 From: Eric Koegel Date: Fri, 13 Jan 2012 21:36:42 +0300 Subject: [PATCH] Adds a root menu option to arrange icons. This patch adds a menu option to arrange the desktop icons. First, it sorts and adds all the special icons such as the trash, home folder, and any volumes. Next, it sorts and adds all the regular file icons. For bug 2833. --- src/xfdesktop-file-icon-manager.c | 16 ++++++ src/xfdesktop-icon-view.c | 103 +++++++++++++++++++++++++++++++++++++ src/xfdesktop-icon-view.h | 2 + 3 files changed, 121 insertions(+), 0 deletions(-) diff --git a/src/xfdesktop-file-icon-manager.c b/src/xfdesktop-file-icon-manager.c index d3ca2eb..928f009 100644 --- a/src/xfdesktop-file-icon-manager.c +++ b/src/xfdesktop-file-icon-manager.c @@ -858,6 +858,14 @@ xfdesktop_file_icon_menu_delete(GtkWidget *widget, } static void +xfdesktop_file_icon_menu_arrange_icons(GtkWidget *widget, + gpointer user_data) +{ + XfdesktopFileIconManager *fmanager = XFDESKTOP_FILE_ICON_MANAGER(user_data); + xfdesktop_icon_view_sort_icons(fmanager->priv->icon_view); +} + +static void xfdesktop_file_icon_menu_properties(GtkWidget *widget, gpointer user_data) { @@ -1691,6 +1699,14 @@ xfdesktop_file_icon_manager_populate_context_menu(XfceDesktop *desktop, : G_CALLBACK(xfdesktop_file_icon_menu_properties), fmanager); } + + /* Add an arrange icons option to the menu */ + mi = gtk_image_menu_item_new_with_mnemonic(_("_Arrange Desktop Icons")); + gtk_widget_show(mi); + gtk_menu_shell_append(GTK_MENU_SHELL(menu), mi); + g_signal_connect(G_OBJECT(mi), "activate", + G_CALLBACK(xfdesktop_file_icon_menu_arrange_icons), + fmanager); } /* don't free |selected|. the menu deactivated handler does that */ diff --git a/src/xfdesktop-icon-view.c b/src/xfdesktop-icon-view.c index 0736006..8b7f476 100644 --- a/src/xfdesktop-icon-view.c +++ b/src/xfdesktop-icon-view.c @@ -47,7 +47,9 @@ #endif #include "xfdesktop-icon-view.h" +#include "xfdesktop-file-icon-manager.h" #include "xfdesktop-marshal.h" +#include "xfdesktop-volume-icon.h" #include #include @@ -1440,6 +1442,107 @@ xfdesktop_icon_view_drag_data_received(GtkWidget *widget, info, time_); } +static gint +xfdesktop_icon_view_compare_icons(gconstpointer *a, + gconstpointer *b) +{ + XfdesktopIcon *a_icon, *b_icon; + gchar *a_str, *b_str; + gint ret; + + a_icon = XFDESKTOP_ICON(a); + b_icon = XFDESKTOP_ICON(b); + + a_str = g_strdup(xfdesktop_icon_peek_label(a_icon)); + b_str = g_strdup(xfdesktop_icon_peek_label(b_icon)); + + ret = g_strcmp0(a_str, b_str); + + g_free(a_str); + g_free(b_str); + + return ret; +} + +void +xfdesktop_icon_view_sort_icons(XfdesktopIconView *icon_view) +{ + GList *l = NULL; + GList *special_icons = NULL; + GList *regular_icons = NULL; + guint16 row = 0; + guint16 col = -1; /* start at -1 because we'll increment it */ + + /* 1: Remove all the icons from the desktop + * 2: Sort and place special icons + * 3: Sort and place all the regular icons + */ + for(l = icon_view->priv->icons; l; l = l->next) { + guint16 old_row, old_col; + + /* clear out old position and either add it to our + * special_icons or regular_icons list + */ + xfdesktop_icon_view_invalidate_icon(icon_view, l->data, FALSE); + if(xfdesktop_icon_get_position(l->data, &old_row, &old_col)) + xfdesktop_grid_set_position_free(icon_view, old_row, old_col); + + if(XFDESKTOP_IS_SPECIAL_FILE_ICON(l->data) || + XFDESKTOP_IS_VOLUME_ICON(l->data)) { + special_icons = g_list_insert_sorted(special_icons, + l->data, + (GCompareFunc)xfdesktop_icon_view_compare_icons); + } else { + regular_icons = g_list_insert_sorted(regular_icons, + l->data, + (GCompareFunc)xfdesktop_icon_view_compare_icons); + } + } + + /* Append the special_icons */ + for(l = special_icons; l; l = l->next) { + + /* Find the next available spot for an icon */ + do { + if(row + 1 >= icon_view->priv->nrows) { + ++col; + row = 0; + } else { + ++row; + } + } while(!xfdesktop_grid_is_free_position(icon_view, row, col)); + + /* set new position */ + xfdesktop_icon_set_position(l->data, row, col); + xfdesktop_grid_unset_position_free(icon_view, l->data); + + xfdesktop_icon_view_invalidate_icon(icon_view, l->data, TRUE); + } + + /* Append the regular_icons */ + for(l = regular_icons; l; l = l->next) { + + /* Find the next available spot for an icon */ + do { + if(row + 1 >= icon_view->priv->nrows) { + ++col; + row = 0; + } else { + ++row; + } + } while(!xfdesktop_grid_is_free_position(icon_view, row, col)); + + /* set new position */ + xfdesktop_icon_set_position(l->data, row, col); + xfdesktop_grid_unset_position_free(icon_view, l->data); + + xfdesktop_icon_view_invalidate_icon(icon_view, l->data, TRUE); + } + + g_list_free(special_icons); + g_list_free(regular_icons); +} + static void xfdesktop_icon_view_icon_theme_changed(GtkIconTheme *icon_theme, gpointer user_data) diff --git a/src/xfdesktop-icon-view.h b/src/xfdesktop-icon-view.h index 4961044..95bddd4 100644 --- a/src/xfdesktop-icon-view.h +++ b/src/xfdesktop-icon-view.h @@ -118,6 +118,8 @@ gdouble xfdesktop_icon_view_get_font_size(XfdesktopIconView *icon_view); GtkWidget *xfdesktop_icon_view_get_window_widget(XfdesktopIconView *icon_view); +void xfdesktop_icon_view_sort_icons(XfdesktopIconView *icon_view); + #if defined(DEBUG) && DEBUG > 0 guint _xfdesktop_icon_view_n_items(XfdesktopIconView *icon_view); #endif -- 1.7.5.4