[gedit] Added line ending configuration in save dialog



commit 6c11ec395400ad72a7f65df2e92d1588b0e87ca6
Author: Jesse van den Kieboom <jesse icecrew nl>
Date:   Sat Jan 23 18:40:06 2010 +0100

    Added line ending configuration in save dialog

 gedit/gedit-commands-file.c       |   10 ++-
 gedit/gedit-file-chooser-dialog.c |  201 +++++++++++++++++++++++++++++++++----
 gedit/gedit-file-chooser-dialog.h |   17 ++-
 gedit/gedit-tab.c                 |   12 ++-
 gedit/gedit-tab.h                 |    3 +-
 5 files changed, 215 insertions(+), 28 deletions(-)
---
diff --git a/gedit/gedit-commands-file.c b/gedit/gedit-commands-file.c
index b8f6de3..f055d8a 100644
--- a/gedit/gedit-commands-file.c
+++ b/gedit/gedit-commands-file.c
@@ -605,6 +605,7 @@ save_dialog_response_cb (GeditFileChooserDialog *dialog,
 	GeditTab *tab;
 	gpointer data;
 	GSList *tabs_to_save_as;
+	GeditDocumentNewlineType newline_type;
 
 	gedit_debug (DEBUG_COMMANDS);
 
@@ -622,6 +623,7 @@ save_dialog_response_cb (GeditFileChooserDialog *dialog,
 	g_return_if_fail (file != NULL);
 
 	encoding = gedit_file_chooser_dialog_get_encoding (dialog);
+	newline_type = gedit_file_chooser_dialog_get_newline_type (dialog);
 
 	gtk_widget_destroy (GTK_WIDGET (dialog));
 
@@ -649,7 +651,7 @@ save_dialog_response_cb (GeditFileChooserDialog *dialog,
 
 		// FIXME: pass the GFile to tab when api is there
 		uri = g_file_get_uri (file);
-		_gedit_tab_save_as (tab, uri, encoding);
+		_gedit_tab_save_as (tab, uri, encoding, newline_type);
 		g_free (uri);
 	}
 
@@ -739,6 +741,7 @@ file_save_as (GeditTab    *tab,
 	GFile *file;
 	gboolean uri_set = FALSE;
 	const GeditEncoding *encoding;
+	GeditDocumentNewlineType newline_type;
 
 	g_return_if_fail (GEDIT_IS_TAB (tab));
 	g_return_if_fail (GEDIT_IS_WINDOW (window));
@@ -812,9 +815,14 @@ file_save_as (GeditTab    *tab,
 	encoding = gedit_document_get_encoding (doc);
 	g_return_if_fail (encoding != NULL);
 
+	newline_type = gedit_document_get_newline_type (doc);
+
 	gedit_file_chooser_dialog_set_encoding (GEDIT_FILE_CHOOSER_DIALOG (save_dialog),
 						encoding);
 
+	gedit_file_chooser_dialog_set_newline_type (GEDIT_FILE_CHOOSER_DIALOG (save_dialog),
+	                                            newline_type);
+
 	g_object_set_data (G_OBJECT (save_dialog),
 			   GEDIT_TAB_TO_SAVE_AS,
 			   tab);
diff --git a/gedit/gedit-file-chooser-dialog.c b/gedit/gedit-file-chooser-dialog.c
index 755ac0b..a03ed30 100644
--- a/gedit/gedit-file-chooser-dialog.c
+++ b/gedit/gedit-file-chooser-dialog.c
@@ -45,6 +45,7 @@
 #include "gedit-language-manager.h"
 #include "gedit-prefs-manager-app.h"
 #include "gedit-debug.h"
+#include "gedit-enum-types.h"
 
 #define GEDIT_FILE_CHOOSER_DIALOG_GET_PRIVATE(object)(G_TYPE_INSTANCE_GET_PRIVATE ((object), GEDIT_TYPE_FILE_CHOOSER_DIALOG, GeditFileChooserDialogPrivate))
 
@@ -54,6 +55,11 @@
 struct _GeditFileChooserDialogPrivate
 {
 	GtkWidget *option_menu;
+	GtkWidget *extra_widget;
+
+	GtkWidget *newline_label;
+	GtkWidget *newline_combo;
+	GtkListStore *newline_store;
 };
 
 G_DEFINE_TYPE(GeditFileChooserDialog, gedit_file_chooser_dialog, GTK_TYPE_FILE_CHOOSER_DIALOG)
@@ -69,37 +75,140 @@ gedit_file_chooser_dialog_class_init (GeditFileChooserDialogClass *klass)
 static void
 create_option_menu (GeditFileChooserDialog *dialog)
 {
-	GtkWidget *hbox;
 	GtkWidget *label;
 	GtkWidget *menu;
 
-	hbox = gtk_hbox_new (FALSE, 6);
-
 	label = gtk_label_new_with_mnemonic (_("C_haracter Coding:"));
+	gtk_misc_set_alignment (GTK_MISC (label), 0, 0.5);
+
 	menu = gedit_encodings_combo_box_new (
 		gtk_file_chooser_get_action (GTK_FILE_CHOOSER (dialog)) == GTK_FILE_CHOOSER_ACTION_SAVE);
 
 	gtk_label_set_mnemonic_widget (GTK_LABEL (label), menu);
-	gtk_box_pack_start (GTK_BOX (hbox),
-			    label,
-			    FALSE,
-			    FALSE,
-			    0);
-
-	gtk_box_pack_end (GTK_BOX (hbox),
-			  menu,
-			  TRUE,
-			  TRUE,
-			  0);
 
-	gtk_file_chooser_set_extra_widget (GTK_FILE_CHOOSER (dialog),
-					   hbox);
-	gtk_widget_show_all (hbox);
+	gtk_box_pack_start (GTK_BOX (dialog->priv->extra_widget),
+	                    label,
+	                    FALSE,
+	                    TRUE,
+	                    0);
+
+	gtk_box_pack_start (GTK_BOX (dialog->priv->extra_widget),
+	                    menu,
+	                    TRUE,
+	                    TRUE,
+	                    0);
+
+	gtk_widget_show (label);
+	gtk_widget_show (menu);
 
 	dialog->priv->option_menu = menu;
 }
 
 static void
+update_newline_visibility (GeditFileChooserDialog *dialog)
+{
+	gboolean issave = gtk_file_chooser_get_action (GTK_FILE_CHOOSER (dialog)) ==
+	                  GTK_FILE_CHOOSER_ACTION_SAVE;
+
+	gtk_widget_set_visible (dialog->priv->newline_label, issave);
+	gtk_widget_set_visible (dialog->priv->newline_combo, issave);
+}
+
+static void
+newline_combo_append (GtkComboBox              *combo,
+                      GtkListStore             *store,
+                      GtkTreeIter              *iter,
+                      const gchar              *label,
+                      GeditDocumentNewlineType  newline_type)
+{
+	gtk_list_store_append (store, iter);
+	gtk_list_store_set (store, iter, 0, label, 1, newline_type, -1);
+
+	if (newline_type == GEDIT_DOCUMENT_NEWLINE_TYPE_DEFAULT)
+	{
+		gtk_combo_box_set_active_iter (combo, iter);
+	}
+}
+
+static void
+create_newline_combo (GeditFileChooserDialog *dialog)
+{
+	GtkWidget *label, *combo;
+	GtkListStore *store;
+	GtkCellRenderer *renderer;
+	GtkTreeIter iter;
+
+	label = gtk_label_new_with_mnemonic (_("L_ine Ending:"));
+	gtk_misc_set_alignment (GTK_MISC (label), 0, 0.5);
+
+	store = gtk_list_store_new (2, G_TYPE_STRING, GEDIT_TYPE_DOCUMENT_NEWLINE_TYPE);
+	combo = gtk_combo_box_new_with_model (GTK_TREE_MODEL (store));
+	renderer = gtk_cell_renderer_text_new ();
+
+	gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combo),
+	                            renderer,
+	                            TRUE);
+
+	gtk_cell_layout_add_attribute (GTK_CELL_LAYOUT (combo),
+	                               renderer,
+	                               "text",
+	                               0);
+
+	newline_combo_append (GTK_COMBO_BOX (combo),
+	                      store,
+	                      &iter,
+	                      _("Unix/Linux"),
+	                      GEDIT_DOCUMENT_NEWLINE_TYPE_LF);
+
+	newline_combo_append (GTK_COMBO_BOX (combo),
+	                      store,
+	                      &iter,
+	                      _("Mac OS Classic"),
+	                      GEDIT_DOCUMENT_NEWLINE_TYPE_CR);
+
+	newline_combo_append (GTK_COMBO_BOX (combo),
+	                      store,
+	                      &iter,
+	                      _("Windows"),
+	                      GEDIT_DOCUMENT_NEWLINE_TYPE_CR_LF);
+
+	gtk_label_set_mnemonic_widget (GTK_LABEL (label), combo);
+
+	gtk_box_pack_start (GTK_BOX (dialog->priv->extra_widget),
+	                    label,
+	                    FALSE,
+	                    TRUE,
+	                    0);
+
+	gtk_box_pack_start (GTK_BOX (dialog->priv->extra_widget),
+	                    combo,
+	                    TRUE,
+	                    TRUE,
+	                    0);
+
+	dialog->priv->newline_combo = combo;
+	dialog->priv->newline_label = label;
+	dialog->priv->newline_store = store;
+
+	update_newline_visibility (dialog);
+}
+
+static void
+create_extra_widget (GeditFileChooserDialog *dialog)
+{
+	dialog->priv->extra_widget = gtk_hbox_new (FALSE, 6);
+
+	gtk_widget_show (dialog->priv->extra_widget);
+
+	create_option_menu (dialog);
+	create_newline_combo (dialog);
+
+	gtk_file_chooser_set_extra_widget (GTK_FILE_CHOOSER (dialog),
+					   dialog->priv->extra_widget);
+
+}
+
+static void
 action_changed (GeditFileChooserDialog *dialog,
 		GParamSpec	       *pspec,
 		gpointer		data)
@@ -125,6 +234,8 @@ action_changed (GeditFileChooserDialog *dialog,
 		default:
 			gtk_widget_hide (dialog->priv->option_menu);
 	}
+
+	update_newline_visibility (dialog);
 }
 
 static void
@@ -263,7 +374,7 @@ gedit_file_chooser_dialog_new_valist (const gchar          *title,
 			       "select-multiple", action == GTK_FILE_CHOOSER_ACTION_OPEN,
 			       NULL);
 
-	create_option_menu (GEDIT_FILE_CHOOSER_DIALOG (result));
+	create_extra_widget (GEDIT_FILE_CHOOSER_DIALOG (result));
 
 	g_signal_connect (result,
 			  "notify::action",
@@ -388,3 +499,57 @@ gedit_file_chooser_dialog_get_encoding (GeditFileChooserDialog *dialog)
 	return gedit_encodings_combo_box_get_selected_encoding (
 				GEDIT_ENCODINGS_COMBO_BOX (dialog->priv->option_menu));
 }
+
+void
+gedit_file_chooser_dialog_set_newline_type (GeditFileChooserDialog  *dialog,
+					    GeditDocumentNewlineType newline_type)
+{
+	GtkTreeIter iter;
+	GtkTreeModel *model;
+
+	g_return_if_fail (GEDIT_IS_FILE_CHOOSER_DIALOG (dialog));
+	g_return_if_fail (gtk_file_chooser_get_action (GTK_FILE_CHOOSER (dialog)) == GTK_FILE_CHOOSER_ACTION_SAVE);
+
+	model = GTK_TREE_MODEL (dialog->priv->newline_store);
+
+	if (!gtk_tree_model_get_iter_first (model, &iter))
+	{
+		return;
+	}
+
+	do
+	{
+		GeditDocumentNewlineType nt;
+
+		gtk_tree_model_get (model, &iter, 1, &nt, -1);
+
+		if (newline_type == nt)
+		{
+			gtk_combo_box_set_active_iter (GTK_COMBO_BOX (dialog->priv->newline_combo),
+			                               &iter);
+			break;
+		}
+	} while (gtk_tree_model_iter_next (model, &iter));
+}
+
+GeditDocumentNewlineType
+gedit_file_chooser_dialog_get_newline_type (GeditFileChooserDialog *dialog)
+{
+	GtkTreeIter iter;
+	GeditDocumentNewlineType newline_type;
+
+	g_return_val_if_fail (GEDIT_IS_FILE_CHOOSER_DIALOG (dialog), GEDIT_DOCUMENT_NEWLINE_TYPE_DEFAULT);
+	g_return_val_if_fail (gtk_file_chooser_get_action (GTK_FILE_CHOOSER (dialog)) == GTK_FILE_CHOOSER_ACTION_SAVE,
+	                      GEDIT_DOCUMENT_NEWLINE_TYPE_DEFAULT);
+
+	gtk_combo_box_get_active_iter (GTK_COMBO_BOX (dialog->priv->newline_combo),
+	                               &iter);
+
+	gtk_tree_model_get (GTK_TREE_MODEL (dialog->priv->newline_store),
+	                    &iter,
+	                    1,
+	                    &newline_type,
+	                    -1);
+
+	return newline_type;
+}
diff --git a/gedit/gedit-file-chooser-dialog.h b/gedit/gedit-file-chooser-dialog.h
index 5900a34..090045c 100644
--- a/gedit/gedit-file-chooser-dialog.h
+++ b/gedit/gedit-file-chooser-dialog.h
@@ -34,6 +34,8 @@
 #include <gtk/gtk.h>
 
 #include <gedit/gedit-encodings.h>
+#include <gedit/gedit-enum-types.h>
+#include <gedit/gedit-document.h>
 
 G_BEGIN_DECLS
 
@@ -69,13 +71,18 @@ GtkWidget	*gedit_file_chooser_dialog_new		(const gchar            *title,
 							 const GeditEncoding    *encoding,
 							 const gchar            *first_button_text,
 							 ...);
- 
+
 void		 gedit_file_chooser_dialog_set_encoding (GeditFileChooserDialog *dialog,
 							 const GeditEncoding    *encoding);
-							 
-const GeditEncoding 
-		*gedit_file_chooser_dialog_get_encoding (GeditFileChooserDialog *dialog);						 
-							 
+
+const GeditEncoding
+		*gedit_file_chooser_dialog_get_encoding (GeditFileChooserDialog *dialog);
+
+void		 gedit_file_chooser_dialog_set_newline_type (GeditFileChooserDialog  *dialog,
+							     GeditDocumentNewlineType newline_type);
+
+GeditDocumentNewlineType
+		 gedit_file_chooser_dialog_get_newline_type (GeditFileChooserDialog *dialog);
 
 G_END_DECLS
 
diff --git a/gedit/gedit-tab.c b/gedit/gedit-tab.c
index ba1d2ee..5789033 100644
--- a/gedit/gedit-tab.c
+++ b/gedit/gedit-tab.c
@@ -2177,9 +2177,10 @@ gedit_tab_auto_save (GeditTab *tab)
 }
 
 void
-_gedit_tab_save_as (GeditTab            *tab,
-		    const gchar         *uri,
-		    const GeditEncoding *encoding)
+_gedit_tab_save_as (GeditTab                 *tab,
+                    const gchar              *uri,
+                    const GeditEncoding      *encoding,
+                    GeditDocumentNewlineType  newline_type)
 {
 	GeditDocument *doc;
 	GeditDocumentSaveFlags save_flags;
@@ -2224,6 +2225,11 @@ _gedit_tab_save_as (GeditTab            *tab,
 	if (tab->priv->auto_save_timeout > 0)
 		remove_auto_save_timeout (tab);
 
+	/* FIXME: this should behave the same as encoding, setting it here
+	   makes it persistent (if save fails, it's remembered). It's not
+	   a very big deal, but would be nice to have them follow the
+	   same pattern. This can be changed once we break API for 3.0 */
+	gedit_document_set_newline_type (doc, newline_type);
 	gedit_document_save_as (doc, uri, encoding, tab->priv->save_flags);
 }
 
diff --git a/gedit/gedit-tab.h b/gedit/gedit-tab.h
index 43d90d2..0d0d9d1 100644
--- a/gedit/gedit-tab.h
+++ b/gedit/gedit-tab.h
@@ -144,7 +144,8 @@ void		 _gedit_tab_revert		(GeditTab            *tab);
 void		 _gedit_tab_save		(GeditTab            *tab);
 void		 _gedit_tab_save_as		(GeditTab            *tab,
 						 const gchar         *uri,
-						 const GeditEncoding *encoding);
+						 const GeditEncoding *encoding,
+						 GeditDocumentNewlineType newline_type);
 
 void		 _gedit_tab_print		(GeditTab            *tab);
 void		 _gedit_tab_print_preview	(GeditTab            *tab);



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