changeset 1490:b924293cf6c8

Desktop files window: make columns sortable, replace Apply and OK buttons by one Save button which is made sensitive when needed.
author zas_
date Mon, 30 Mar 2009 17:05:31 +0000
parents ac7ae6fc6d49
children f057c235426f
files src/desktop_file.c
diffstat 1 files changed, 124 insertions(+), 23 deletions(-) [+]
line wrap: on
line diff
--- a/src/desktop_file.c	Sun Mar 29 20:06:21 2009 +0000
+++ b/src/desktop_file.c	Mon Mar 30 17:05:31 2009 +0000
@@ -34,7 +34,10 @@
 {
 	GtkWidget *window;
 	GtkWidget *entry;
+	GtkWidget *save_button;
 	GtkTextBuffer *buffer;
+	gchar *desktop_name;
+	gboolean modified;
 };
 
 
@@ -78,7 +81,8 @@
 static void editor_window_close_cb(GtkWidget *widget, gpointer data)
 {
 	EditorWindow *ew = data;
-	
+
+	g_free(ew->desktop_name);
 	gtk_widget_destroy(ew->window);
 	g_free(ew);
 }
@@ -89,16 +93,40 @@
 	return TRUE;
 }
 
-static void editor_window_apply_cb(GtkWidget *widget, gpointer data)
+static void editor_window_save_cb(GtkWidget *widget, gpointer data)
+{
+	EditorWindow *ew = data;
+
+	if (ew->modified)
+		{
+		editor_window_save(ew);
+		}
+}
+
+static void editor_window_text_modified_cb(GtkWidget *widget, gpointer data)
 {
 	EditorWindow *ew = data;
-	editor_window_save(ew);
+	
+	if (gtk_text_buffer_get_modified(ew->buffer))
+		{
+		gtk_widget_set_sensitive(ew->save_button, TRUE);
+		ew->modified = TRUE;
+		}
 }
-static void editor_window_ok_cb(GtkWidget *widget, gpointer data)
+
+static void editor_window_entry_changed_cb(GtkWidget *widget, gpointer data)
 {
 	EditorWindow *ew = data;
-	if (!editor_window_save(ew)) return;
-	editor_window_close_cb(widget, ew);
+	const gchar *content = gtk_entry_get_text(GTK_ENTRY(ew->entry));
+	gboolean modified = (!ew->desktop_name && *content);
+
+	if (!modified)
+		{
+		modified = strcmp(ew->desktop_name, content);
+		}
+	
+	gtk_widget_set_sensitive(ew->save_button, modified);
+	ew->modified = modified;
 }
 
 static void editor_window_new(const gchar *src_path, const gchar *desktop_name)
@@ -137,12 +165,14 @@
 
 	ew->entry = gtk_entry_new();
 	gtk_box_pack_start(GTK_BOX(hbox), ew->entry, TRUE, TRUE, 0);
+	ew->desktop_name = NULL;
 	if (desktop_name)
 		{
 		gtk_entry_set_text(GTK_ENTRY(ew->entry), desktop_name);
+		ew->desktop_name = g_strdup(desktop_name);
 		}
 	gtk_widget_show(ew->entry);
-	
+	g_signal_connect(G_OBJECT(ew->entry), "changed", G_CALLBACK(editor_window_entry_changed_cb), ew);
 
 	button_hbox = gtk_hbutton_box_new();
 	gtk_button_box_set_layout(GTK_BUTTON_BOX(button_hbox), GTK_BUTTONBOX_END);
@@ -150,20 +180,13 @@
 	gtk_box_pack_end(GTK_BOX(hbox), button_hbox, FALSE, FALSE, 0);
 	gtk_widget_show(button_hbox);
 
-	button = pref_button_new(NULL, GTK_STOCK_OK, NULL, FALSE,
-				 G_CALLBACK(editor_window_ok_cb), ew);
-	gtk_container_add(GTK_CONTAINER(button_hbox), button);
-	GTK_WIDGET_SET_FLAGS(button, GTK_CAN_DEFAULT);
-	gtk_widget_grab_default(button);
-	gtk_widget_show(button);
-
-	ct_button = button;
-
-	button = pref_button_new(NULL, GTK_STOCK_APPLY, NULL, FALSE,
-				 G_CALLBACK(editor_window_apply_cb), ew);
-	gtk_container_add(GTK_CONTAINER(button_hbox), button);
-	GTK_WIDGET_SET_FLAGS(button, GTK_CAN_DEFAULT);
-	gtk_widget_show(button);
+	ew->save_button = pref_button_new(NULL, GTK_STOCK_SAVE, NULL, FALSE,
+				 G_CALLBACK(editor_window_save_cb), ew);
+	gtk_container_add(GTK_CONTAINER(button_hbox), ew->save_button);
+	GTK_WIDGET_SET_FLAGS(ew->save_button, GTK_CAN_DEFAULT);
+	gtk_widget_set_sensitive(ew->save_button, FALSE);
+	gtk_widget_show(ew->save_button);
+	ct_button = ew->save_button;
 
 	button = pref_button_new(NULL, GTK_STOCK_CANCEL, NULL, FALSE,
 				 G_CALLBACK(editor_window_close_cb), ew);
@@ -193,7 +216,9 @@
 		{
 		gtk_text_buffer_set_text(ew->buffer, text, size);
 		}
-
+	gtk_text_buffer_set_modified(ew->buffer, FALSE);
+	g_signal_connect(G_OBJECT(ew->buffer), "modified-changed",
+			 G_CALLBACK(editor_window_text_modified_cb), ew);
 
 	gtk_widget_show(ew->window);
 }
@@ -237,6 +262,61 @@
 	editor_window_new(DESKTOP_FILE_TEMPLATE, _("new.desktop"));
 }
 
+
+static gint editor_list_window_sort_cb(GtkTreeModel *model, GtkTreeIter *a, GtkTreeIter *b, gpointer data)
+{
+	gint n = GPOINTER_TO_INT(data);
+	gint ret = 0;
+
+	switch (n)
+		{
+		case DESKTOP_FILE_COLUMN_KEY:
+		case DESKTOP_FILE_COLUMN_NAME:
+		case DESKTOP_FILE_COLUMN_PATH:
+			{
+			gchar *s1, *s2;
+
+			gtk_tree_model_get(model, a, n, &s1, -1);
+			gtk_tree_model_get(model, b, n, &s2, -1);
+
+			if (!s1 || !s2)
+				{
+			  	if (!s1 && !s2) break;
+			  	ret = s1 ? 1 : -1;
+				}
+			else
+				{
+			  	ret = g_utf8_collate(s1, s2);
+				}
+
+			g_free(s1);
+			g_free(s2);
+			}
+			break;
+	
+		case DESKTOP_FILE_COLUMN_HIDDEN:
+			{
+			gint *v1, *v2;
+
+			gtk_tree_model_get(model, a, n, &v1, -1);
+			gtk_tree_model_get(model, b, n, &v2, -1);
+
+			if (v1 == v2)
+				ret = 0;
+			else if (v1 < v2)
+				ret = 1;
+			else
+				ret = -1;
+			}
+			break;
+
+    		default:
+       			g_return_val_if_reached(0);
+		}
+
+	return ret;
+}
+
 static void editor_list_window_create(void)
 {
 	GtkWidget *win_vbox;
@@ -246,7 +326,9 @@
 	GtkCellRenderer *renderer;
 	GtkTreeSelection *selection;
 	GtkTreeViewColumn *column;
-
+	GtkTreeModel *store;
+	GtkTreeSortable *sortable;
+	
 	editor_list_window = window_new(GTK_WINDOW_TOPLEVEL, "editors", PIXBUF_INLINE_ICON_CONFIG, NULL, _("Editors"));
 	gtk_window_set_type_hint(GTK_WINDOW(editor_list_window), GDK_WINDOW_TYPE_HINT_DIALOG);
 	g_signal_connect(G_OBJECT(editor_list_window), "delete_event",
@@ -304,6 +386,7 @@
 	gtk_tree_view_column_pack_start(column, renderer, FALSE);
 	gtk_tree_view_column_add_attribute(column, renderer, "text", DESKTOP_FILE_COLUMN_KEY);
 	gtk_tree_view_append_column(GTK_TREE_VIEW(editor_list_view), column);
+	gtk_tree_view_column_set_sort_column_id(column, DESKTOP_FILE_COLUMN_KEY);
 
 	column = gtk_tree_view_column_new();
 	gtk_tree_view_column_set_title(column, _("Hidden"));
@@ -312,6 +395,7 @@
 	gtk_tree_view_column_pack_start(column, renderer, FALSE);
 	gtk_tree_view_column_add_attribute(column, renderer, "active", DESKTOP_FILE_COLUMN_HIDDEN);
 	gtk_tree_view_append_column(GTK_TREE_VIEW(editor_list_view), column);
+	gtk_tree_view_column_set_sort_column_id(column, DESKTOP_FILE_COLUMN_HIDDEN);
 
 	column = gtk_tree_view_column_new();
 	gtk_tree_view_column_set_title(column, _("Name"));
@@ -320,6 +404,7 @@
 	gtk_tree_view_column_pack_start(column, renderer, FALSE);
 	gtk_tree_view_column_add_attribute(column, renderer, "text", DESKTOP_FILE_COLUMN_NAME);
 	gtk_tree_view_append_column(GTK_TREE_VIEW(editor_list_view), column);
+	gtk_tree_view_column_set_sort_column_id(column, DESKTOP_FILE_COLUMN_NAME);
 
 	column = gtk_tree_view_column_new();
 	gtk_tree_view_column_set_title(column, _("Path"));
@@ -328,6 +413,22 @@
 	gtk_tree_view_column_pack_start(column, renderer, FALSE);
 	gtk_tree_view_column_add_attribute(column, renderer, "text", DESKTOP_FILE_COLUMN_PATH);
 	gtk_tree_view_append_column(GTK_TREE_VIEW(editor_list_view), column);
+	gtk_tree_view_column_set_sort_column_id(column, DESKTOP_FILE_COLUMN_PATH);
+
+	/* set up sorting */
+	store = gtk_tree_view_get_model(GTK_TREE_VIEW(editor_list_view));
+	sortable = GTK_TREE_SORTABLE(store);
+	gtk_tree_sortable_set_sort_func(sortable, DESKTOP_FILE_COLUMN_KEY, editor_list_window_sort_cb,
+					GINT_TO_POINTER(DESKTOP_FILE_COLUMN_KEY), NULL);
+	gtk_tree_sortable_set_sort_func(sortable, DESKTOP_FILE_COLUMN_HIDDEN, editor_list_window_sort_cb,
+					GINT_TO_POINTER(DESKTOP_FILE_COLUMN_HIDDEN), NULL);
+	gtk_tree_sortable_set_sort_func(sortable, DESKTOP_FILE_COLUMN_NAME, editor_list_window_sort_cb,
+					GINT_TO_POINTER(DESKTOP_FILE_COLUMN_NAME), NULL);
+	gtk_tree_sortable_set_sort_func(sortable, DESKTOP_FILE_COLUMN_PATH, editor_list_window_sort_cb,
+					GINT_TO_POINTER(DESKTOP_FILE_COLUMN_PATH), NULL);
+
+	/* set initial sort order */
+    	gtk_tree_sortable_set_sort_column_id(sortable, DESKTOP_FILE_COLUMN_KEY, GTK_SORT_ASCENDING);
 
 	gtk_container_add(GTK_CONTAINER(scrolled), editor_list_view);
 	gtk_widget_show(editor_list_view);