Re: Patch: Recent visit folder selection in gtk2 filechooser

2007-05-09 Thread Jochen Baier
Jochen Baier wrote:
> hi,
>
> the attached patch will add a combobox to the gtk2 filechooser to easy
> switch between recent visit folder. (=folder you opened a file)
> maybe worth to add to gvim.
>
> screenshot: http://www.jochen-baier.de/vim_recent.jpg
>
> regards jochen
>   
> 

patch is obsolete, gtk+ will include something similar in the future
versions of the filechooser widget

regards jochen


Patch: Recent visit folder selection in gtk2 filechooser

2007-04-24 Thread Jochen Baier
hi,

the attached patch will add a combobox to the gtk2 filechooser to easy
switch between recent visit folder. (=folder you opened a file)
maybe worth to add to gvim.

screenshot: http://www.jochen-baier.de/vim_recent.jpg

regards jochen
Index: src/gui_gtk.c
===
--- src/gui_gtk.c	(revision 242)
+++ src/gui_gtk.c	(working copy)
@@ -1195,7 +1195,415 @@
 # define USE_FILE_CHOOSER
 #endif
 
-#ifndef USE_FILE_CHOOSER
+#if defined USE_FILE_CHOOSER 
+/*struct saving recent visit folder*/
+typedef struct _File_Chooser File_Chooser;
+struct _File_Chooser
+{
+	GtkListStore *store;
+	GtkTreeModel *model;
+	GtkWidget *filechooserdialog;
+	GtkWidget *save_button;
+	GtkWidget *open_button;
+	GtkWidget *combo;
+};
+
+static void file_chooser_path_selected (GtkComboBox *combobox,
+	File_Chooser *file_chooser);  
+
+/*return iter if path is in combo list*/
+	static gboolean
+file_chooser_get_iter_for_path (File_Chooser *file_chooser,
+	GtkTreeIter *iter, gchar *new_path)
+{
+	gchar *old_path=NULL;
+	gboolean valid=FALSE;
+	gboolean exist=FALSE;
+
+	valid = gtk_tree_model_get_iter_first (file_chooser->model, iter);
+	while (valid)
+	{
+		old_path=NULL;
+		gtk_tree_model_get (file_chooser->model, iter, 0,
+			&old_path, -1);
+
+		if (strcmp (new_path, old_path) == 0)
+		{
+			g_free (old_path);
+			exist=TRUE;
+			break;
+		}
+		g_free (old_path);
+		valid = gtk_tree_model_iter_next (file_chooser->model, iter);
+	}
+  
+	return exist;
+}
+
+	static void
+file_chooser_append_path (File_Chooser *file_chooser, gchar *new_path)
+{
+	GtkTreeIter iter;
+
+	if (!file_chooser_get_iter_for_path (file_chooser, &iter, new_path))
+	{
+		gtk_list_store_append (file_chooser->store, &iter);
+		gtk_list_store_set (file_chooser->store, &iter,
+			0, new_path, -1);
+	}
+   
+}
+
+/*current folder inside file_chooser changed*/
+	static void
+file_chooser_folder_changed (GtkFileChooser *chooser,
+	File_Chooser *file_chooser)
+{
+	GtkTreeIter iter;
+	GtkTreeIter exist_iter;
+	gchar *combo_path=NULL;
+	gchar *chooser_path=NULL;
+	gboolean ret=FALSE;
+
+	/*if combox_path == chooser_path do nothing*/
+	chooser_path=gtk_file_chooser_get_current_folder (
+		GTK_FILE_CHOOSER(file_chooser->filechooserdialog));
+
+	if (gtk_combo_box_get_active_iter (GTK_COMBO_BOX(
+			file_chooser->combo), &iter))
+{
+		gtk_tree_model_get (file_chooser->model, &iter, 0,
+			&combo_path, -1);
+
+		if (strcmp (combo_path, chooser_path) == 0)
+		{
+			ret=TRUE;
+		}
+
+		g_free (combo_path);
+	}
+
+	if (ret)
+	{
+		g_free (chooser_path);
+		return;
+	}
+
+	/*if path is known, show it in the combo, if not clean the combo*/
+	g_signal_handlers_disconnect_by_func(file_chooser->combo,
+		file_chooser_path_selected, file_chooser);
+	if (file_chooser_get_iter_for_path (file_chooser, 
+		&exist_iter, chooser_path)) 
+	{
+
+		gtk_combo_box_set_active_iter (
+			GTK_COMBO_BOX(file_chooser->combo), &exist_iter);
+	}
+	else
+	{
+		gtk_combo_box_set_active (
+			GTK_COMBO_BOX(file_chooser->combo), -1);
+		gtk_entry_set_text (GTK_ENTRY
+			(GTK_BIN (file_chooser->combo)->child), "");
+	}
+
+	g_signal_connect ((gpointer) file_chooser->combo, "changed",
+		G_CALLBACK (file_chooser_path_selected), file_chooser);
+	g_free (chooser_path);
+}
+
+/*user changed path in the combo box*/
+	static void
+file_chooser_path_selected (GtkComboBox *combobox,
+	File_Chooser *file_chooser)
+{
+	GtkTreeIter iter;
+	gboolean valid;
+	gchar *path=NULL;
+
+	if (gtk_combo_box_get_active_iter (combobox, &iter))
+	{
+		gtk_tree_model_get (file_chooser->model, &iter, 0,
+			&path, -1);
+		gtk_file_chooser_set_current_folder (GTK_FILE_CHOOSER(
+			file_chooser->filechooserdialog), path);
+		g_free(path);
+	}
+
+}
+
+	static void
+file_chooser_destroyed (gpointer data)
+{
+	File_Chooser *file_chooser = (File_Chooser*) data;
+	gtk_list_store_clear (file_chooser->store);
+	g_object_unref (file_chooser->model);
+	g_free (file_chooser);
+}
+
+/*mark active item italic*/
+	static void
+file_chooser_mark_active_item (GtkCellLayout   *cell_layout,
+	GtkCellRenderer *cell,
+	GtkTreeModel*tree_model,
+	GtkTreeIter *iter,
+	gpointerdata)
+{
+	GtkTreePath *path;
+	GtkTreeIter active_iter;
+	gboolean cursive=FALSE;
+
+	File_Chooser *file_chooser = (File_Chooser*) data;
+
+	path = gtk_tree_model_get_path (tree_model, iter);
+
+	if (gtk_combo_box_get_active_iter (
+		GTK_COMBO_BOX(file_chooser->combo), &active_iter))
+	{
+		GtkTreePath* active_path= gtk_tree_model_get_path (
+			tree_model, &active_iter);
+
+		if (gtk_tree_path_compare (path,active_path)==0)
+			cursive=TRUE;
+
+		gtk_tree_path_free (active_path);
+	}
+
+	if (cursive)
+		g_object_set (cell, "style", PANGO_STYLE_ITALIC, NULL);
+	else
+		g_object_set (cell, "style", PANGO_STYLE_NORMAL, NULL);
+
+	gtk_tree_path_free (path);
+}
+
+	static File_Chooser*
+init_file_chooser (GtkWidget *parent)
+{
+	File_Chooser *file_chooser = g_new (File_Chooser, 1);
+
+	GtkWidge