[nautilus] bookmarks-window: cleanup NautilusBookmarksWindow



commit fbcfa7c445b066dd35bec1dbd61445400aa4e73e
Author: Cosimo Cecchi <cosimoc gnome org>
Date:   Tue Apr 24 11:24:56 2012 -0400

    bookmarks-window: cleanup NautilusBookmarksWindow
    
    Make this transient to the window that spawned it, and destroy it with
    its parent.

 src/nautilus-bookmarks-window.c |  923 ++++++++++++++++-----------------------
 src/nautilus-bookmarks-window.h |    7 +-
 src/nautilus-window-bookmarks.c |   13 +-
 3 files changed, 385 insertions(+), 558 deletions(-)
---
diff --git a/src/nautilus-bookmarks-window.c b/src/nautilus-bookmarks-window.c
index 839af9a..d994813 100644
--- a/src/nautilus-bookmarks-window.c
+++ b/src/nautilus-bookmarks-window.c
@@ -63,56 +63,6 @@ static int		     row_deleted_signal_id;
 static int                   row_activated_signal_id;
 static int                   button_pressed_signal_id;
 static int                   key_pressed_signal_id;
-static int                   jump_button_signal_id;
-
-/* forward declarations */
-static guint    get_selected_row                            (void);
-static gboolean get_selection_exists                        (void);
-static void     name_or_uri_field_activate                  (NautilusEntry        *entry);
-static void     on_bookmark_list_changed                    (NautilusBookmarkList *list,
-							     gpointer              user_data);
-static void     on_name_field_changed                       (GtkEditable          *editable,
-							     gpointer              user_data);
-static void     on_remove_button_clicked                    (GtkButton            *button,
-							     gpointer              user_data);
-static void     on_jump_button_clicked                      (GtkButton            *button,
-							     gpointer              user_data);
-static void	on_row_changed				    (GtkListStore	  *store,
-							     GtkTreePath	  *path,
-							     GtkTreeIter	  *iter,
-							     gpointer		   user_data);
-static void	on_row_deleted				    (GtkListStore	  *store,
-							     GtkTreePath	  *path,
-							     gpointer		   user_data);
-static void	on_row_activated			    (GtkTreeView	  *view,
-							     GtkTreePath	  *path,
-                                                             GtkTreeViewColumn    *column,
-							     gpointer		   user_data);
-static gboolean	on_button_pressed                           (GtkTreeView	  *view,
-                                                             GdkEventButton       *event,
-							     gpointer		   user_data);
-static gboolean	on_key_pressed                              (GtkTreeView	  *view,
-                                                             GdkEventKey          *event,
-							     gpointer		   user_data);
-static void     on_selection_changed                        (GtkTreeSelection     *treeselection,
-							     gpointer              user_data);
-
-static gboolean on_text_field_focus_out_event               (GtkWidget            *widget,
-							     GdkEventFocus        *event,
-							     gpointer              user_data);
-static void     on_uri_field_changed                        (GtkEditable          *editable,
-							     gpointer              user_data);
-static gboolean on_window_delete_event                      (GtkWidget            *widget,
-							     GdkEvent             *event,
-							     gpointer              user_data);
-static void     on_window_hide_event                        (GtkWidget            *widget,
-							     gpointer              user_data);
-static void     on_window_destroy_event                     (GtkWidget            *widget,
-							     gpointer              user_data);
-static void     repopulate                                  (void);
-static void     set_up_close_accelerator                    (GtkWidget            *window);
-static void	open_selected_bookmark 			    (gpointer   user_data, GdkScreen *screen);
-static void	update_bookmark_from_text		    (void);
 
 /* We store a pointer to the bookmark in a column so when an item is moved
    with DnD we know which item it is. However we have to be careful to keep
@@ -125,20 +75,56 @@ static void	update_bookmark_from_text		    (void);
 #define BOOKMARK_LIST_COLUMN_STYLE		3
 #define BOOKMARK_LIST_COLUMN_COUNT		4
 
-/* layout constants */
-
-/* Keep window from shrinking down ridiculously small; numbers are somewhat arbitrary */
-#define BOOKMARKS_WINDOW_MIN_WIDTH	300
-#define BOOKMARKS_WINDOW_MIN_HEIGHT	100
-
 /* Larger size initially; user can stretch or shrink (but not shrink below min) */
 #define BOOKMARKS_WINDOW_INITIAL_WIDTH	500
 #define BOOKMARKS_WINDOW_INITIAL_HEIGHT	200
 
+static gboolean
+get_selection_exists (void)
+{
+	return gtk_tree_selection_get_selected (bookmark_selection, NULL, NULL);
+}
+
+static guint
+get_selected_row (void)
+{
+	GtkTreeIter       iter;
+	GtkTreePath      *path;
+	GtkTreeModel     *model;
+	gint		 *indices, row;
+	
+	g_assert (get_selection_exists());
+	
+	model = GTK_TREE_MODEL (bookmark_list_store);
+	gtk_tree_selection_get_selected (bookmark_selection,
+					 &model,
+					 &iter);
+	
+	path = gtk_tree_model_get_path (model, &iter);
+	indices = gtk_tree_path_get_indices (path);
+	row = indices[0];
+	gtk_tree_path_free (path);
+	return row;
+}
+
+static NautilusBookmark *
+get_selected_bookmark (void)
+{
+	g_return_val_if_fail(NAUTILUS_IS_BOOKMARK_LIST(bookmarks), NULL);
+
+	if (!get_selection_exists())
+		return NULL;
+
+	if (nautilus_bookmark_list_length (bookmarks) < 1)
+		return NULL;
+
+	return nautilus_bookmark_list_item_at(bookmarks, get_selected_row ());
+}
+
 static void
-nautilus_bookmarks_window_response_callback (GtkDialog *dialog,
-					     int response_id,
-					     gpointer callback_data)
+nautilus_bookmarks_window_response_cb (GtkDialog *dialog,
+				       int response_id,
+				       gpointer callback_data)
 {
 	if (response_id == GTK_RESPONSE_HELP) {
 		GError *error = NULL;
@@ -164,10 +150,23 @@ nautilus_bookmarks_window_response_callback (GtkDialog *dialog,
 			g_error_free (error);
 		}
 	} else if (response_id == GTK_RESPONSE_CLOSE) {
-		gtk_widget_hide (GTK_WIDGET (dialog));
+		gtk_widget_destroy (GTK_WIDGET (dialog));
         }
 }
 
+static int
+nautilus_bookmarks_window_key_press_event_cb (GtkWindow *window, 
+					      GdkEventKey *event, 
+					      gpointer user_data)
+{
+	if (event->state & GDK_CONTROL_MASK && event->keyval == GDK_KEY_w) {
+		gtk_widget_destroy (GTK_WIDGET (window));
+		return TRUE;
+	}
+
+	return FALSE;
+}
+
 static GtkListStore *
 create_bookmark_store (void)
 {
@@ -193,6 +192,50 @@ setup_empty_list (void)
 }
 
 static void
+on_selection_changed (GtkTreeSelection *treeselection,
+		      gpointer user_data)
+{
+	NautilusBookmark *selected;
+	const char *name = NULL;
+	char *entry_text = NULL;
+	GFile *location;
+
+	g_assert (GTK_IS_ENTRY (name_field));
+	g_assert (GTK_IS_ENTRY (uri_field));
+
+	selected = get_selected_bookmark ();
+
+	if (selected) {
+		name = nautilus_bookmark_get_name (selected);
+		location = nautilus_bookmark_get_location (selected);
+		entry_text = g_file_get_parse_name (location);
+
+		g_object_unref (location);
+	}
+	
+	/* Set the sensitivity of widgets that require a selection */
+	gtk_widget_set_sensitive (remove_button, selected != NULL);
+        gtk_widget_set_sensitive (jump_button, selected != NULL);
+	gtk_widget_set_sensitive (name_field, selected != NULL);
+	gtk_widget_set_sensitive (uri_field, selected != NULL);
+
+	g_signal_handler_block (name_field, name_field_changed_signal_id);
+	nautilus_entry_set_text (NAUTILUS_ENTRY (name_field),
+				 name ? name : "");
+	g_signal_handler_unblock (name_field, name_field_changed_signal_id);
+
+	g_signal_handler_block (uri_field, uri_field_changed_signal_id);
+	nautilus_entry_set_text (NAUTILUS_ENTRY (uri_field),
+				 entry_text ? entry_text : "");
+	g_signal_handler_unblock (uri_field, uri_field_changed_signal_id);
+
+	text_changed = FALSE;
+	name_text_changed = FALSE;
+
+	g_free (entry_text);
+}
+
+static void
 bookmarks_set_empty (gboolean empty)
 {
 	GtkTreeIter iter;
@@ -218,241 +261,102 @@ bookmarks_set_empty (gboolean empty)
 }
 
 static void
-edit_bookmarks_dialog_reset_signals (gpointer data,
-				     GObject *obj)
-{
-	g_signal_handler_disconnect (jump_button,
-				     jump_button_signal_id);
-	g_signal_handler_disconnect (bookmark_list_widget,
-				     row_activated_signal_id);
-	jump_button_signal_id =
-		g_signal_connect (jump_button, "clicked",
-				  G_CALLBACK (on_jump_button_clicked), NULL);
-	row_activated_signal_id =
-		g_signal_connect (bookmark_list_widget, "row_activated",
-				  G_CALLBACK (on_row_activated), NULL);
-}
-
-/**
- * create_bookmarks_window:
- * 
- * Create a new bookmark-editing window. 
- * @list: The NautilusBookmarkList that this window will edit.
- *
- * Return value: A pointer to the new window.
- **/
-GtkWindow *
-create_bookmarks_window (NautilusBookmarkList *list, GObject *undo_manager_source)
+repopulate (void)
 {
-	GtkWidget         *window;
-	GtkTreeViewColumn *col;
-	GtkCellRenderer   *rend;
-	GtkBuilder        *builder;
-
-	bookmarks = list;
-
-	builder = gtk_builder_new ();
-	if (!gtk_builder_add_from_resource (builder,
-					    "/org/gnome/nautilus/nautilus-bookmarks-window.ui",
-					    NULL)) {
-		return NULL;
-	}
+	NautilusBookmark *selected;
+	GtkListStore *store;
+	GtkTreePath *path;
+	GtkTreeRowReference *reference;
+	guint index;
 
-	window = (GtkWidget *)gtk_builder_get_object (builder, "bookmarks_dialog");
-	bookmark_list_widget = (GtkTreeView *)gtk_builder_get_object (builder, "bookmark_tree_view");
-	remove_button = (GtkWidget *)gtk_builder_get_object (builder, "bookmark_delete_button");
-	jump_button = (GtkWidget *)gtk_builder_get_object (builder, "bookmark_jump_button");
+	g_assert (GTK_IS_TREE_VIEW (bookmark_list_widget));
+	g_assert (NAUTILUS_IS_BOOKMARK_LIST (bookmarks));
+	
+	store = GTK_LIST_STORE (bookmark_list_store);
 
-	set_up_close_accelerator (window);
+	selected = get_selected_bookmark ();
 
-	gtk_window_set_wmclass (GTK_WINDOW (window), "bookmarks", "Nautilus");
-	gtk_window_set_default_size (GTK_WINDOW (window), 
-				     BOOKMARKS_WINDOW_INITIAL_WIDTH, 
-				     BOOKMARKS_WINDOW_INITIAL_HEIGHT);
+	g_signal_handler_block (bookmark_selection,
+				selection_changed_id);
+	g_signal_handler_block (bookmark_list_store,
+				row_deleted_signal_id);
+        g_signal_handler_block (bookmark_list_widget,
+                                row_activated_signal_id);
+        g_signal_handler_block (bookmark_list_widget,
+                                key_pressed_signal_id);
+        g_signal_handler_block (bookmark_list_widget,
+                                button_pressed_signal_id);
 
-	g_object_weak_ref (G_OBJECT (undo_manager_source), edit_bookmarks_dialog_reset_signals, 
-			   undo_manager_source);
+	gtk_list_store_clear (store);
 	
-	bookmark_list_widget = GTK_TREE_VIEW (gtk_builder_get_object (builder, "bookmark_tree_view"));
+	g_signal_handler_unblock (bookmark_list_widget,
+				  row_activated_signal_id);
+        g_signal_handler_unblock (bookmark_list_widget,
+                                  key_pressed_signal_id);
+        g_signal_handler_unblock (bookmark_list_widget,
+                                  button_pressed_signal_id);
+	g_signal_handler_unblock (bookmark_list_store,
+				  row_deleted_signal_id);
+	g_signal_handler_unblock (bookmark_selection,
+				  selection_changed_id);
+	
+	/* Fill the list in with the bookmark names. */
+	g_signal_handler_block (store, row_changed_signal_id);
 
-	rend = gtk_cell_renderer_pixbuf_new ();
-	col = gtk_tree_view_column_new_with_attributes ("Icon", 
-							rend,
-							"gicon", 
-							BOOKMARK_LIST_COLUMN_ICON,
-							NULL);
-	gtk_tree_view_append_column (bookmark_list_widget,
-				     GTK_TREE_VIEW_COLUMN (col));
-	gtk_tree_view_column_set_fixed_width (GTK_TREE_VIEW_COLUMN (col),
-					      NAUTILUS_ICON_SIZE_SMALLER);
+	reference = NULL;
 
-	rend = gtk_cell_renderer_text_new ();
-	g_object_set (rend,
-		      "ellipsize", PANGO_ELLIPSIZE_END,
-		      "ellipsize-set", TRUE,
-		      NULL);
+	for (index = 0; index < nautilus_bookmark_list_length (bookmarks); ++index) {
+		NautilusBookmark *bookmark;
+		const char       *bookmark_name;
+		GIcon            *bookmark_icon;
+		GtkTreeIter       iter;
 
-	col = gtk_tree_view_column_new_with_attributes ("Icon", 
-							rend,
-							"text", 
-							BOOKMARK_LIST_COLUMN_NAME,
-							"style",
-							BOOKMARK_LIST_COLUMN_STYLE,
-							NULL);
-	gtk_tree_view_append_column (bookmark_list_widget,
-				     GTK_TREE_VIEW_COLUMN (col));
-	
-	bookmark_list_store = create_bookmark_store ();
-	setup_empty_list ();
-	gtk_tree_view_set_model (bookmark_list_widget,
-				 GTK_TREE_MODEL (bookmark_empty_list_store));
-	
-	bookmark_selection =
-		GTK_TREE_SELECTION (gtk_tree_view_get_selection (bookmark_list_widget));
+		bookmark = nautilus_bookmark_list_item_at (bookmarks, index);
+		bookmark_name = nautilus_bookmark_get_name (bookmark);
+		bookmark_icon = nautilus_bookmark_get_icon (bookmark);
 
-	name_field = nautilus_entry_new ();
-	
-	gtk_widget_show (name_field);
-	gtk_box_pack_start (GTK_BOX (gtk_builder_get_object (builder, "bookmark_name_placeholder")),
-			    name_field, TRUE, TRUE, 0);
-	
-	gtk_label_set_mnemonic_widget (
-		GTK_LABEL (gtk_builder_get_object (builder, "bookmark_name_label")),
-		name_field);
+		gtk_list_store_append (store, &iter);
+		gtk_list_store_set (store, &iter, 
+				    BOOKMARK_LIST_COLUMN_ICON, bookmark_icon,
+				    BOOKMARK_LIST_COLUMN_NAME, bookmark_name,
+				    BOOKMARK_LIST_COLUMN_BOOKMARK, bookmark,
+				    BOOKMARK_LIST_COLUMN_STYLE, PANGO_STYLE_NORMAL,
+				    -1);
 
-	uri_field = nautilus_entry_new ();
-	gtk_widget_show (uri_field);
-	gtk_box_pack_start (GTK_BOX (gtk_builder_get_object (builder, "bookmark_location_placeholder")),
-			    uri_field, TRUE, TRUE, 0);
+		if (bookmark == selected) {
+			/* save old selection */
+			GtkTreePath *path;
 
-	gtk_label_set_mnemonic_widget (
-		GTK_LABEL (gtk_builder_get_object (builder, "bookmark_location_label")),
-		uri_field);
+			path = gtk_tree_model_get_path (GTK_TREE_MODEL (store), &iter);
+			reference = gtk_tree_row_reference_new (GTK_TREE_MODEL (store), path);
+			gtk_tree_path_free (path);
+		}
 
-	bookmark_list_changed_signal_id =
-		g_signal_connect (bookmarks, "changed",
-				  G_CALLBACK (on_bookmark_list_changed), NULL);
-	row_changed_signal_id =
-		g_signal_connect (bookmark_list_store, "row_changed",
-				  G_CALLBACK (on_row_changed), NULL);
-	row_deleted_signal_id =
-		g_signal_connect (bookmark_list_store, "row_deleted",
-				  G_CALLBACK (on_row_deleted), NULL);
-        row_activated_signal_id =
-                g_signal_connect (bookmark_list_widget, "row_activated",
-                                  G_CALLBACK (on_row_activated), undo_manager_source);
-        button_pressed_signal_id =
-                g_signal_connect (bookmark_list_widget, "button_press_event",
-                                  G_CALLBACK (on_button_pressed), NULL);
-        key_pressed_signal_id =
-                g_signal_connect (bookmark_list_widget, "key_press_event",
-                                  G_CALLBACK (on_key_pressed), NULL);
-	selection_changed_id =
-		g_signal_connect (bookmark_selection, "changed",
-				  G_CALLBACK (on_selection_changed), NULL);	
+		g_object_unref (bookmark_icon);
+	}
 
-	g_signal_connect (window, "delete_event",
-			  G_CALLBACK (on_window_delete_event), NULL);
-	g_signal_connect (window, "hide",
-			  G_CALLBACK (on_window_hide_event), NULL);                    	    
-	g_signal_connect (window, "destroy",
-			  G_CALLBACK (on_window_destroy_event), NULL);
-	g_signal_connect (window, "response",
-			  G_CALLBACK (nautilus_bookmarks_window_response_callback), NULL);
+	g_signal_handler_unblock (store, row_changed_signal_id);
 
-	name_field_changed_signal_id =
-		g_signal_connect (name_field, "changed",
-				  G_CALLBACK (on_name_field_changed), NULL);
-                      		    
-	g_signal_connect (name_field, "focus_out_event",
-			  G_CALLBACK (on_text_field_focus_out_event), NULL);                            
-	g_signal_connect (name_field, "activate",
-			  G_CALLBACK (name_or_uri_field_activate), NULL);
-
-	uri_field_changed_signal_id = 
-		g_signal_connect (uri_field, "changed",
-				  G_CALLBACK (on_uri_field_changed), NULL);
-                      		    
-	g_signal_connect (uri_field, "focus_out_event",
-			  G_CALLBACK (on_text_field_focus_out_event), NULL);
-	g_signal_connect (uri_field, "activate",
-			  G_CALLBACK (name_or_uri_field_activate), NULL);
-	g_signal_connect (remove_button, "clicked",
-			  G_CALLBACK (on_remove_button_clicked), NULL);
-	jump_button_signal_id = 
-		g_signal_connect (jump_button, "clicked",
-				  G_CALLBACK (on_jump_button_clicked), undo_manager_source);
-
-	gtk_tree_selection_set_mode (bookmark_selection, GTK_SELECTION_BROWSE);
-	
-	/* Fill in list widget with bookmarks, must be after signals are wired up. */
-	repopulate();
-
-	g_object_unref (builder);
-	
-	return GTK_WINDOW (window);
-}
-
-void
-edit_bookmarks_dialog_set_signals (GObject *undo_manager_source)
-{
-
-	g_signal_handler_disconnect (jump_button,
-				     jump_button_signal_id);
-	g_signal_handler_disconnect (bookmark_list_widget,
-				     row_activated_signal_id);
-
-	jump_button_signal_id =
-		g_signal_connect (jump_button, "clicked",
-				  G_CALLBACK (on_jump_button_clicked), undo_manager_source);
-	row_activated_signal_id =
-		g_signal_connect (bookmark_list_widget, "row_activated",
-				  G_CALLBACK (on_row_activated), undo_manager_source);
-
-	g_object_weak_ref (G_OBJECT (undo_manager_source), edit_bookmarks_dialog_reset_signals,
-			   undo_manager_source);
-}
-
-static NautilusBookmark *
-get_selected_bookmark (void)
-{
-	g_return_val_if_fail(NAUTILUS_IS_BOOKMARK_LIST(bookmarks), NULL);
+	if (reference != NULL) {
+		/* restore old selection */
 
-	if (!get_selection_exists())
-		return NULL;
+		/* bookmarks_set_empty() will call the selection change handler,
+ 		 * so we block it here in case of selection change.
+ 		 */
+		g_signal_handler_block (bookmark_selection, selection_changed_id);
 
-	if (nautilus_bookmark_list_length (bookmarks) < 1)
-		return NULL;
+		g_assert (index != 0);
+		g_assert (gtk_tree_row_reference_valid (reference));
 
-	return nautilus_bookmark_list_item_at(bookmarks, get_selected_row ());
-}
+		path = gtk_tree_row_reference_get_path (reference);
+		gtk_tree_selection_select_path (bookmark_selection, path);
+		gtk_tree_row_reference_free (reference);
+		gtk_tree_path_free (path);
 
-static guint
-get_selected_row (void)
-{
-	GtkTreeIter       iter;
-	GtkTreePath      *path;
-	GtkTreeModel     *model;
-	gint		 *indices, row;
-	
-	g_assert (get_selection_exists());
-	
-	model = GTK_TREE_MODEL (bookmark_list_store);
-	gtk_tree_selection_get_selected (bookmark_selection,
-					 &model,
-					 &iter);
-	
-	path = gtk_tree_model_get_path (model, &iter);
-	indices = gtk_tree_path_get_indices (path);
-	row = indices[0];
-	gtk_tree_path_free (path);
-	return row;
-}
+		g_signal_handler_unblock (bookmark_selection, selection_changed_id);
+	}
 
-static gboolean
-get_selection_exists (void)
-{
-	return gtk_tree_selection_get_selected (bookmark_selection, NULL, NULL);
+	bookmarks_set_empty (index == 0);	  
 }
 
 static void
@@ -491,10 +395,9 @@ on_name_field_changed (GtkEditable *editable,
 }
 
 static void
-open_selected_bookmark (gpointer user_data, GdkScreen *screen)
+open_selected_bookmark (NautilusWindow *window)
 {
 	NautilusBookmark *selected;
-	NautilusWindow *window;
 	GFile *location;
 	
 	selected = get_selected_bookmark ();
@@ -508,7 +411,6 @@ open_selected_bookmark (gpointer user_data, GdkScreen *screen)
 		return;
 	}
 
-	window = user_data;
 	nautilus_window_go_to (window, location);
 
 	g_object_unref (location);
@@ -518,10 +420,7 @@ static void
 on_jump_button_clicked (GtkButton *button,
                         gpointer   user_data)
 {
-	GdkScreen *screen;
-
-	screen = gtk_widget_get_screen (GTK_WIDGET (button));
-	open_selected_bookmark (user_data, screen);
+	open_selected_bookmark (user_data);
 }
 
 static void
@@ -617,6 +516,70 @@ on_row_changed (GtkListStore *store,
 	}
 }
 
+static void
+update_bookmark_from_text (void)
+{
+	NautilusBookmark *bookmark, *bookmark_in_list;
+	const char *name;
+	GIcon *icon;
+	guint selected_row;
+	GtkTreeIter iter;
+	GFile *location;
+
+	g_assert (GTK_IS_ENTRY (name_field));
+	g_assert (GTK_IS_ENTRY (uri_field));
+
+	if (!text_changed ||
+	    gtk_entry_get_text_length (GTK_ENTRY (uri_field)) == 0) {
+		return;
+	}
+
+	location = g_file_parse_name 
+		(gtk_entry_get_text (GTK_ENTRY (uri_field)));
+		
+	bookmark = nautilus_bookmark_new (location,
+					  name_text_changed ? gtk_entry_get_text (GTK_ENTRY (name_field)) : NULL,
+					  NULL);
+		
+	g_object_unref (location);
+
+	selected_row = get_selected_row ();
+
+	/* turn off list updating 'cuz otherwise the list-reordering code runs
+	 * after repopulate(), thus reordering the correctly-ordered list.
+	 */
+	g_signal_handler_block (bookmarks, 
+				bookmark_list_changed_signal_id);
+	nautilus_bookmark_list_delete_item_at (bookmarks, selected_row);
+	nautilus_bookmark_list_insert_item (bookmarks, bookmark, selected_row);
+	g_signal_handler_unblock (bookmarks, 
+				  bookmark_list_changed_signal_id);
+	g_object_unref (bookmark);
+
+	/* We also have to update the bookmark pointer in the list
+	   store. */
+	gtk_tree_selection_get_selected (bookmark_selection,
+					 NULL, &iter);
+	g_signal_handler_block (bookmark_list_store,
+				row_changed_signal_id);
+
+	bookmark_in_list = nautilus_bookmark_list_item_at (bookmarks,
+							   selected_row);
+
+	name = nautilus_bookmark_get_name (bookmark_in_list);
+	icon = nautilus_bookmark_get_icon (bookmark_in_list);
+
+	gtk_list_store_set (bookmark_list_store, &iter,
+			    BOOKMARK_LIST_COLUMN_BOOKMARK, bookmark_in_list,
+			    BOOKMARK_LIST_COLUMN_NAME, name,
+			    BOOKMARK_LIST_COLUMN_ICON, icon,
+			    -1);
+	g_signal_handler_unblock (bookmark_list_store,
+				  row_changed_signal_id);
+
+	g_object_unref (icon);
+}
+
 /* The update_bookmark_from_text() calls in the
  * on_button_pressed() and on_key_pressed() handlers
  * of the tree view are a hack.
@@ -660,10 +623,7 @@ on_row_activated (GtkTreeView       *view,
                   GtkTreeViewColumn *column,
                   gpointer           user_data)
 {
-	GdkScreen *screen;
-
-	screen = gtk_widget_get_screen (GTK_WIDGET (view));
-	open_selected_bookmark (user_data, screen);
+	open_selected_bookmark (user_data);
 }
 
 static void
@@ -681,116 +641,6 @@ on_row_deleted (GtkListStore *store,
 	g_signal_handler_unblock (bookmarks, bookmark_list_changed_signal_id);
 }
 
-static void
-on_selection_changed (GtkTreeSelection *treeselection,
-		      gpointer user_data)
-{
-	NautilusBookmark *selected;
-	const char *name = NULL;
-	char *entry_text = NULL;
-	GFile *location;
-
-	g_assert (GTK_IS_ENTRY (name_field));
-	g_assert (GTK_IS_ENTRY (uri_field));
-
-	selected = get_selected_bookmark ();
-
-	if (selected) {
-		name = nautilus_bookmark_get_name (selected);
-		location = nautilus_bookmark_get_location (selected);
-		entry_text = g_file_get_parse_name (location);
-
-		g_object_unref (location);
-	}
-	
-	/* Set the sensitivity of widgets that require a selection */
-	gtk_widget_set_sensitive (remove_button, selected != NULL);
-        gtk_widget_set_sensitive (jump_button, selected != NULL);
-	gtk_widget_set_sensitive (name_field, selected != NULL);
-	gtk_widget_set_sensitive (uri_field, selected != NULL);
-
-	g_signal_handler_block (name_field, name_field_changed_signal_id);
-	nautilus_entry_set_text (NAUTILUS_ENTRY (name_field),
-				 name ? name : "");
-	g_signal_handler_unblock (name_field, name_field_changed_signal_id);
-
-	g_signal_handler_block (uri_field, uri_field_changed_signal_id);
-	nautilus_entry_set_text (NAUTILUS_ENTRY (uri_field),
-				 entry_text ? entry_text : "");
-	g_signal_handler_unblock (uri_field, uri_field_changed_signal_id);
-
-	text_changed = FALSE;
-	name_text_changed = FALSE;
-
-	g_free (entry_text);
-}
-
-
-static void
-update_bookmark_from_text (void)
-{
-	if (text_changed) {
-		NautilusBookmark *bookmark, *bookmark_in_list;
-		const char *name;
-		GIcon *icon;
-		guint selected_row;
-		GtkTreeIter iter;
-		GFile *location;
-
-		g_assert (GTK_IS_ENTRY (name_field));
-		g_assert (GTK_IS_ENTRY (uri_field));
-
-		if (gtk_entry_get_text_length (GTK_ENTRY (uri_field)) == 0) {
-			return;
-		}
-
-		location = g_file_parse_name 
-			(gtk_entry_get_text (GTK_ENTRY (uri_field)));
-		
-		bookmark = nautilus_bookmark_new (location,
-						  name_text_changed ? gtk_entry_get_text (GTK_ENTRY (name_field)) : NULL,
-						  NULL);
-		
-		g_object_unref (location);
-
-		selected_row = get_selected_row ();
-
-		/* turn off list updating 'cuz otherwise the list-reordering code runs
-		 * after repopulate(), thus reordering the correctly-ordered list.
-		 */
-		g_signal_handler_block (bookmarks, 
-					bookmark_list_changed_signal_id);
-		nautilus_bookmark_list_delete_item_at (bookmarks, selected_row);
-		nautilus_bookmark_list_insert_item (bookmarks, bookmark, selected_row);
-		g_signal_handler_unblock (bookmarks, 
-					  bookmark_list_changed_signal_id);
-		g_object_unref (bookmark);
-
-		/* We also have to update the bookmark pointer in the list
-		   store. */
-		gtk_tree_selection_get_selected (bookmark_selection,
-						 NULL, &iter);
-		g_signal_handler_block (bookmark_list_store,
-					row_changed_signal_id);
-
-		bookmark_in_list = nautilus_bookmark_list_item_at (bookmarks,
-								   selected_row);
-
-		name = nautilus_bookmark_get_name (bookmark_in_list);
-		icon = nautilus_bookmark_get_icon (bookmark_in_list);
-
-		gtk_list_store_set (bookmark_list_store, &iter,
-				    BOOKMARK_LIST_COLUMN_BOOKMARK, bookmark_in_list,
-				    BOOKMARK_LIST_COLUMN_NAME, name,
-				    BOOKMARK_LIST_COLUMN_ICON, icon,
-				    -1);
-		g_signal_handler_unblock (bookmark_list_store,
-					  row_changed_signal_id);
-
-		g_object_unref (icon);
-	}
-}
-
 static gboolean
 on_text_field_focus_out_event (GtkWidget *widget,
 			       GdkEventFocus *event,
@@ -821,172 +671,157 @@ on_uri_field_changed (GtkEditable *editable,
 	text_changed = TRUE;
 }
 
-static gboolean
-on_window_delete_event (GtkWidget *widget,
-			GdkEvent *event,
-			gpointer user_data)
-{
-	gtk_widget_hide (widget);
-	return TRUE;
-}
-
-static gboolean
-restore_geometry (gpointer data)
+/**
+ * nautilus_bookmarks_window_new:
+ * 
+ * Create a new bookmark-editing window. 
+ * @list: The NautilusBookmarkList that this window will edit.
+ *
+ * Return value: A pointer to the new window.
+ **/
+GtkWindow *
+nautilus_bookmarks_window_new (NautilusWindow *parent_window,
+			       NautilusBookmarkList *list)
 {
-	g_assert (GTK_IS_WINDOW (data));
+	GtkWindow         *window;
+	GtkTreeViewColumn *col;
+	GtkCellRenderer   *rend;
+	GtkBuilder        *builder;
 
-	nautilus_bookmarks_window_restore_geometry (GTK_WIDGET (data));
+	bookmarks = list;
 
-	/* Don't call this again */
-	return FALSE;
-}
+	builder = gtk_builder_new ();
+	if (!gtk_builder_add_from_resource (builder,
+					    "/org/gnome/nautilus/nautilus-bookmarks-window.ui",
+					    NULL)) {
+		return NULL;
+	}
 
-static void
-on_window_hide_event (GtkWidget *widget,
-		      gpointer user_data)
-{
-	nautilus_bookmarks_window_save_geometry (GTK_WINDOW (widget));
+	window = GTK_WINDOW (gtk_builder_get_object (builder, "bookmarks_dialog"));
 
-	/* Disable undo for entry widgets */
-	nautilus_undo_unregister (G_OBJECT (name_field));
-	nautilus_undo_unregister (G_OBJECT (uri_field));
+	gtk_window_set_wmclass (window, "bookmarks", "Nautilus");
+	gtk_window_set_default_size (window, 
+				     BOOKMARKS_WINDOW_INITIAL_WIDTH, 
+				     BOOKMARKS_WINDOW_INITIAL_HEIGHT);
+	gtk_window_set_application (window,
+				    gtk_window_get_application (GTK_WINDOW (parent_window)));
+	gtk_window_set_transient_for (window, GTK_WINDOW (parent_window));
+	gtk_window_set_destroy_with_parent (window, TRUE);
+	gtk_window_set_screen (window, gtk_window_get_screen (GTK_WINDOW (window)));
+
+	g_signal_connect (window, "key-press-event",
+			  G_CALLBACK (nautilus_bookmarks_window_key_press_event_cb), NULL);
+	g_signal_connect (window, "response",
+			  G_CALLBACK (nautilus_bookmarks_window_response_cb), NULL);
 
-	/* restore_geometry only works after window is hidden */
-	g_idle_add (restore_geometry, widget);
-}
+	bookmark_list_widget = GTK_TREE_VIEW (gtk_builder_get_object (builder, "bookmark_tree_view"));
+	remove_button = GTK_WIDGET (gtk_builder_get_object (builder, "bookmark_delete_button"));
+	jump_button = GTK_WIDGET (gtk_builder_get_object (builder, "bookmark_jump_button"));
 
-static void
-on_window_destroy_event (GtkWidget *widget,
-		      	 gpointer user_data)
-{
-	g_object_unref (bookmark_list_store);
-	g_object_unref (bookmark_empty_list_store);
-	g_source_remove_by_user_data (widget);
-}
+	rend = gtk_cell_renderer_pixbuf_new ();
+	col = gtk_tree_view_column_new_with_attributes ("Icon", 
+							rend,
+							"gicon", 
+							BOOKMARK_LIST_COLUMN_ICON,
+							NULL);
+	gtk_tree_view_append_column (bookmark_list_widget,
+				     GTK_TREE_VIEW_COLUMN (col));
+	gtk_tree_view_column_set_fixed_width (GTK_TREE_VIEW_COLUMN (col),
+					      NAUTILUS_ICON_SIZE_SMALLER);
 
-static void
-repopulate (void)
-{
-	NautilusBookmark *selected;
-	GtkListStore *store;
-	GtkTreePath *path;
-	GtkTreeRowReference *reference;
-	guint index;
+	rend = gtk_cell_renderer_text_new ();
+	g_object_set (rend,
+		      "ellipsize", PANGO_ELLIPSIZE_END,
+		      "ellipsize-set", TRUE,
+		      NULL);
 
-	g_assert (GTK_IS_TREE_VIEW (bookmark_list_widget));
-	g_assert (NAUTILUS_IS_BOOKMARK_LIST (bookmarks));
+	col = gtk_tree_view_column_new_with_attributes ("Icon", 
+							rend,
+							"text", 
+							BOOKMARK_LIST_COLUMN_NAME,
+							"style",
+							BOOKMARK_LIST_COLUMN_STYLE,
+							NULL);
+	gtk_tree_view_append_column (bookmark_list_widget,
+				     GTK_TREE_VIEW_COLUMN (col));
 	
-	store = GTK_LIST_STORE (bookmark_list_store);
-
-	selected = get_selected_bookmark ();
-
-	g_signal_handler_block (bookmark_selection,
-				selection_changed_id);
-	g_signal_handler_block (bookmark_list_store,
-				row_deleted_signal_id);
-        g_signal_handler_block (bookmark_list_widget,
-                                row_activated_signal_id);
-        g_signal_handler_block (bookmark_list_widget,
-                                key_pressed_signal_id);
-        g_signal_handler_block (bookmark_list_widget,
-                                button_pressed_signal_id);
+	bookmark_list_store = create_bookmark_store ();
+	setup_empty_list ();
+	gtk_tree_view_set_model (bookmark_list_widget,
+				 GTK_TREE_MODEL (bookmark_empty_list_store));
+	
+	bookmark_selection =
+		GTK_TREE_SELECTION (gtk_tree_view_get_selection (bookmark_list_widget));
 
-	gtk_list_store_clear (store);
+	name_field = nautilus_entry_new ();
 	
-	g_signal_handler_unblock (bookmark_list_widget,
-				  row_activated_signal_id);
-        g_signal_handler_unblock (bookmark_list_widget,
-                                  key_pressed_signal_id);
-        g_signal_handler_unblock (bookmark_list_widget,
-                                  button_pressed_signal_id);
-	g_signal_handler_unblock (bookmark_list_store,
-				  row_deleted_signal_id);
-	g_signal_handler_unblock (bookmark_selection,
-				  selection_changed_id);
+	gtk_widget_show (name_field);
+	gtk_box_pack_start (GTK_BOX (gtk_builder_get_object (builder, "bookmark_name_placeholder")),
+			    name_field, TRUE, TRUE, 0);
 	
-	/* Fill the list in with the bookmark names. */
-	g_signal_handler_block (store, row_changed_signal_id);
-
-	reference = NULL;
-
-	for (index = 0; index < nautilus_bookmark_list_length (bookmarks); ++index) {
-		NautilusBookmark *bookmark;
-		const char       *bookmark_name;
-		GIcon            *bookmark_icon;
-		GtkTreeIter       iter;
-
-		bookmark = nautilus_bookmark_list_item_at (bookmarks, index);
-		bookmark_name = nautilus_bookmark_get_name (bookmark);
-		bookmark_icon = nautilus_bookmark_get_icon (bookmark);
-
-		gtk_list_store_append (store, &iter);
-		gtk_list_store_set (store, &iter, 
-				    BOOKMARK_LIST_COLUMN_ICON, bookmark_icon,
-				    BOOKMARK_LIST_COLUMN_NAME, bookmark_name,
-				    BOOKMARK_LIST_COLUMN_BOOKMARK, bookmark,
-				    BOOKMARK_LIST_COLUMN_STYLE, PANGO_STYLE_NORMAL,
-				    -1);
-
-		if (bookmark == selected) {
-			/* save old selection */
-			GtkTreePath *path;
-
-			path = gtk_tree_model_get_path (GTK_TREE_MODEL (store), &iter);
-			reference = gtk_tree_row_reference_new (GTK_TREE_MODEL (store), path);
-			gtk_tree_path_free (path);
-		}
-
-		g_object_unref (bookmark_icon);
-	}
-
-	g_signal_handler_unblock (store, row_changed_signal_id);
-
-	if (reference != NULL) {
-		/* restore old selection */
-
-		/* bookmarks_set_empty() will call the selection change handler,
- 		 * so we block it here in case of selection change.
- 		 */
-		g_signal_handler_block (bookmark_selection, selection_changed_id);
-
-		g_assert (index != 0);
-		g_assert (gtk_tree_row_reference_valid (reference));
+	gtk_label_set_mnemonic_widget (
+		GTK_LABEL (gtk_builder_get_object (builder, "bookmark_name_label")),
+		name_field);
 
-		path = gtk_tree_row_reference_get_path (reference);
-		gtk_tree_selection_select_path (bookmark_selection, path);
-		gtk_tree_row_reference_free (reference);
-		gtk_tree_path_free (path);
+	uri_field = nautilus_entry_new ();
+	gtk_widget_show (uri_field);
+	gtk_box_pack_start (GTK_BOX (gtk_builder_get_object (builder, "bookmark_location_placeholder")),
+			    uri_field, TRUE, TRUE, 0);
 
-		g_signal_handler_unblock (bookmark_selection, selection_changed_id);
-	}
+	gtk_label_set_mnemonic_widget (
+		GTK_LABEL (gtk_builder_get_object (builder, "bookmark_location_label")),
+		uri_field);
 
-	bookmarks_set_empty (index == 0);	  
-}
+	bookmark_list_changed_signal_id =
+		g_signal_connect (bookmarks, "changed",
+				  G_CALLBACK (on_bookmark_list_changed), NULL);
+	row_changed_signal_id =
+		g_signal_connect (bookmark_list_store, "row_changed",
+				  G_CALLBACK (on_row_changed), NULL);
+	row_deleted_signal_id =
+		g_signal_connect (bookmark_list_store, "row_deleted",
+				  G_CALLBACK (on_row_deleted), NULL);
+        row_activated_signal_id =
+                g_signal_connect (bookmark_list_widget, "row_activated",
+                                  G_CALLBACK (on_row_activated), parent_window);
+        button_pressed_signal_id =
+                g_signal_connect (bookmark_list_widget, "button_press_event",
+                                  G_CALLBACK (on_button_pressed), NULL);
+        key_pressed_signal_id =
+                g_signal_connect (bookmark_list_widget, "key_press_event",
+                                  G_CALLBACK (on_key_pressed), NULL);
+	selection_changed_id =
+		g_signal_connect (bookmark_selection, "changed",
+				  G_CALLBACK (on_selection_changed), NULL);	
 
-static int
-handle_close_accelerator (GtkWindow *window, 
-			  GdkEventKey *event, 
-			  gpointer user_data)
-{
-	g_assert (GTK_IS_WINDOW (window));
-	g_assert (event != NULL);
-	g_assert (user_data == NULL);
+	name_field_changed_signal_id =
+		g_signal_connect (name_field, "changed",
+				  G_CALLBACK (on_name_field_changed), NULL);
+                      		    
+	g_signal_connect (name_field, "focus_out_event",
+			  G_CALLBACK (on_text_field_focus_out_event), NULL);                            
+	g_signal_connect (name_field, "activate",
+			  G_CALLBACK (name_or_uri_field_activate), NULL);
 
-	if (event->state & GDK_CONTROL_MASK && event->keyval == GDK_KEY_w) {
-		gtk_widget_hide (GTK_WIDGET (window));
-		return TRUE;
-	}
+	uri_field_changed_signal_id = 
+		g_signal_connect (uri_field, "changed",
+				  G_CALLBACK (on_uri_field_changed), NULL);
+                      		    
+	g_signal_connect (uri_field, "focus_out_event",
+			  G_CALLBACK (on_text_field_focus_out_event), NULL);
+	g_signal_connect (uri_field, "activate",
+			  G_CALLBACK (name_or_uri_field_activate), NULL);
+	g_signal_connect (remove_button, "clicked",
+			  G_CALLBACK (on_remove_button_clicked), NULL);
+	g_signal_connect (jump_button, "clicked",
+			  G_CALLBACK (on_jump_button_clicked), parent_window);
 
-	return FALSE;
-}
+	gtk_tree_selection_set_mode (bookmark_selection, GTK_SELECTION_BROWSE);
+	
+	/* Fill in list widget with bookmarks, must be after signals are wired up. */
+	repopulate();
 
-static void
-set_up_close_accelerator (GtkWidget *window)
-{
-	/* Note that we don't call eel_gtk_window_set_up_close_accelerator
-	 * here because we have to handle saving geometry before hiding the
-	 * window.
-	 */
-	g_signal_connect (window, "key_press_event",
-			  G_CALLBACK (handle_close_accelerator), NULL);
+	g_object_unref (builder);
+	
+	return GTK_WINDOW (window);
 }
diff --git a/src/nautilus-bookmarks-window.h b/src/nautilus-bookmarks-window.h
index 03c5de6..08ddd48 100644
--- a/src/nautilus-bookmarks-window.h
+++ b/src/nautilus-bookmarks-window.h
@@ -29,10 +29,11 @@
 #define NAUTILUS_BOOKMARKS_WINDOW_H
 
 #include <gtk/gtk.h>
+
+#include "nautilus-window.h"
 #include "nautilus-bookmark-list.h"
 
-GtkWindow *create_bookmarks_window                 (NautilusBookmarkList *bookmarks,
-						    GObject              *undo_manager_source);
-void	   edit_bookmarks_dialog_set_signals	   (GObject 		 *undo_manager_source);
+GtkWindow *nautilus_bookmarks_window_new (NautilusWindow       *parent_window,
+					  NautilusBookmarkList *bookmarks);
 
 #endif /* NAUTILUS_BOOKMARKS_WINDOW_H */
diff --git a/src/nautilus-window-bookmarks.c b/src/nautilus-window-bookmarks.c
index ecfd49e..5d973b9 100644
--- a/src/nautilus-window-bookmarks.c
+++ b/src/nautilus-window-bookmarks.c
@@ -102,15 +102,9 @@ show_bogus_bookmark_window (NautilusWindow *window,
 static GtkWindow *
 get_or_create_bookmarks_window (NautilusWindow *window)
 {
-	GObject *undo_manager_source;
-
-	undo_manager_source = G_OBJECT (window);
-
 	if (bookmarks_window == NULL) {
-		bookmarks_window = create_bookmarks_window (window->details->bookmark_list,
-		                                            undo_manager_source);
-	} else {
-		edit_bookmarks_dialog_set_signals (undo_manager_source);
+		bookmarks_window = nautilus_bookmarks_window_new (window, window->details->bookmark_list);
+		g_object_add_weak_pointer (G_OBJECT (bookmarks_window), (gpointer *) &bookmarks_window);
 	}
 
 	return bookmarks_window;
@@ -144,9 +138,6 @@ nautilus_window_edit_bookmarks (NautilusWindow *window)
 	GtkWindow *dialog;
 
 	dialog = get_or_create_bookmarks_window (window);
-
-	gtk_window_set_screen (
-		dialog, gtk_window_get_screen (GTK_WINDOW (window)));
         gtk_window_present (dialog);
 }
 



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]