gedit r6103 - branches/printing/gedit



Author: pborelli
Date: Sun Jan 20 18:27:05 2008
New Revision: 6103
URL: http://svn.gnome.org/viewvc/gedit?rev=6103&view=rev

Log:
properly handle page setup and print settings: they are stored per doc, while the default is loaded serialized at the application level


Modified:
   branches/printing/gedit/gedit-app.c
   branches/printing/gedit/gedit-app.h
   branches/printing/gedit/gedit-commands-file-print.c
   branches/printing/gedit/gedit-print-job.c
   branches/printing/gedit/gedit-print-job.h
   branches/printing/gedit/gedit-tab.c
   branches/printing/gedit/gedit-tab.h

Modified: branches/printing/gedit/gedit-app.c
==============================================================================
--- branches/printing/gedit/gedit-app.c	(original)
+++ branches/printing/gedit/gedit-app.c	Sun Jan 20 18:27:05 2008
@@ -44,6 +44,10 @@
 #include "gedit-utils.h"
 #include "gedit-enum-types.h"
 
+
+#define GEDIT_PAGE_SETUP_FILE		"gedit-page-setup"
+#define GEDIT_PRINT_SETTINGS_FILE	"gedit-print-settings"
+
 #define GEDIT_APP_GET_PRIVATE(object)(G_TYPE_INSTANCE_GET_PRIVATE ((object), GEDIT_TYPE_APP, GeditAppPrivate))
 
 /* Properties */
@@ -57,7 +61,11 @@
 {
 	GList	          *windows;
 	GeditWindow       *active_window;
+
 	GeditLockdownMask  lockdown;
+
+	GtkPageSetup      *page_setup;
+	GtkPrintSettings  *print_settings;
 };
 
 G_DEFINE_TYPE(GeditApp, gedit_app, G_TYPE_OBJECT)
@@ -69,6 +77,11 @@
 
 	g_list_free (app->priv->windows);
 
+	if (app->priv->page_setup)
+		g_object_unref (app->priv->page_setup);
+	if (app->priv->print_settings)
+		g_object_unref (app->priv->print_settings);
+
 	G_OBJECT_CLASS (gedit_app_parent_class)->finalize (object);
 }
 
@@ -121,7 +134,8 @@
 	if (home != NULL)
 	{
 		return g_build_filename (home,
-					 ".gnome2/accels/"
+					 ".gnome2",
+					 "accels"
 					 "gedit",
 					 NULL);
 	}
@@ -130,21 +144,174 @@
 }
 
 static void
-gedit_app_init (GeditApp *app)
+load_accels (void)
 {
-	gchar *accel_file;
+	gchar *filename;
 
-	app->priv = GEDIT_APP_GET_PRIVATE (app);
+	filename = get_accel_file ();
+	if (filename != NULL)
+	{
+		gedit_debug_message (DEBUG_APP, "Loading keybindings from %s\n", filename);		
+		gtk_accel_map_load (filename);
+		g_free (filename);
+	}
+}
+
+static void
+save_accels (void)
+{
+	gchar *filename;
+
+	filename = get_accel_file ();
+	if (filename != NULL)
+	{
+		gedit_debug_message (DEBUG_APP, "Saving keybindings in %s\n", filename);		
+		gtk_accel_map_save (filename);
+		g_free (filename);
+	}
+}
+
+static gchar *
+get_page_setup_file (void)
+{
+	const gchar *home;
+
+	home = g_get_home_dir ();
+	if (home != NULL)
+	{
+		return g_build_filename (home,
+					 ".gnome2",
+					 "gedit",
+					 GEDIT_PAGE_SETUP_FILE,
+					 NULL);
+	}
+
+	return NULL;
+}
 
-	/* Load accels */
-	accel_file = get_accel_file ();
-	if (accel_file != NULL)
-	{
-		gedit_debug_message (DEBUG_APP, "Loading keybindings from %s\n", accel_file);		
-		gtk_accel_map_load (accel_file);
-		g_free (accel_file);
+static void
+load_page_setup (GeditApp *app)
+{
+	gchar *filename;
+	GError *error = NULL;
+
+	g_return_if_fail (app->priv->page_setup == NULL);
+
+	filename = get_page_setup_file ();
+
+	app->priv->page_setup = gtk_page_setup_new_from_file (filename,
+							      &error);
+	if (error)
+	{
+		/* TODO: ignore file not found error */
+		g_warning (error->message);
+		g_error_free (error);
+	}
+
+	g_free (filename);
+
+	/* fall back to default settings */
+	if (app->priv->page_setup == NULL)
+		app->priv->page_setup = gtk_page_setup_new ();
+}
+
+static void
+save_page_setup (GeditApp *app)
+{
+	gchar *filename;
+	GError *error = NULL;
+
+	if (app->priv->page_setup == NULL)
+		return;
+
+	filename = get_page_setup_file ();
+
+	gtk_page_setup_to_file (app->priv->page_setup,
+				filename,
+				&error);
+	if (error)
+	{
+		g_warning (error->message);
+		g_error_free (error);
+	}
+
+	g_free (filename);
+}
+
+static gchar *
+get_print_settings_file (void)
+{
+	const gchar *home;
+
+	home = g_get_home_dir ();
+	if (home != NULL)
+	{
+		return g_build_filename (home,
+					 ".gnome2",
+					 "gedit",
+					 GEDIT_PRINT_SETTINGS_FILE,
+					 NULL);
 	}
 
+	return NULL;
+}
+
+static void
+load_print_settings (GeditApp *app)
+{
+	gchar *filename;
+	GError *error = NULL;
+
+	g_return_if_fail (app->priv->print_settings == NULL);
+
+	filename = get_print_settings_file ();
+
+	app->priv->print_settings = gtk_print_settings_new_from_file (filename,
+								      &error);
+	if (error)
+	{
+		/* TODO: ignore file not found error */
+		g_warning (error->message);
+		g_error_free (error);
+	}
+
+	g_free (filename);
+
+	/* fall back to default settings */
+	if (app->priv->print_settings == NULL)
+		app->priv->print_settings = gtk_print_settings_new ();
+}
+
+static void
+save_print_settings (GeditApp *app)
+{
+	gchar *filename;
+	GError *error = NULL;
+
+	if (app->priv->print_settings == NULL)
+		return;
+
+	filename = get_print_settings_file ();
+
+	gtk_print_settings_to_file (app->priv->print_settings,
+				    filename,
+				    &error);
+	if (error)
+	{
+		g_warning (error->message);
+		g_error_free (error);
+	}
+
+	g_free (filename);
+}
+
+static void
+gedit_app_init (GeditApp *app)
+{
+	app->priv = GEDIT_APP_GET_PRIVATE (app);
+
+	load_accels ();
+
 	/* initial lockdown state */
 	app->priv->lockdown = gedit_prefs_manager_get_lockdown ();
 }
@@ -242,15 +409,11 @@
 */					      
 	if (app->priv->windows == NULL)
 	{
-		gchar *accel_file;
-		accel_file = get_accel_file ();
+		/* Last window is gone... save some settings and exit */
 
-		if (accel_file != NULL)
-		{
-			gedit_debug_message (DEBUG_APP, "Saveing keybindings in %s\n", accel_file);		
-			gtk_accel_map_save (accel_file);
-			g_free (accel_file);
-		}
+		save_accels ();
+
+		save_print_settings (app);
 
 		g_object_unref (app);
 	}
@@ -648,3 +811,54 @@
 
 	app_lockdown_changed (app);
 }
+
+/* Returns a copy */
+GtkPageSetup *
+_gedit_app_get_default_page_setup (GeditApp *app)
+{
+	g_return_val_if_fail (GEDIT_IS_APP (app), NULL);
+
+	if (app->priv->page_setup == NULL)
+		load_page_setup (app);
+
+	return gtk_page_setup_copy (app->priv->page_setup);
+}
+
+void
+_gedit_app_set_default_page_setup (GeditApp     *app,
+				   GtkPageSetup *page_setup)
+{
+	g_return_if_fail (GEDIT_IS_APP (app));
+	g_return_if_fail (GTK_IS_PAGE_SETUP (page_setup));
+
+	if (app->priv->page_setup != NULL)
+		g_object_unref (app->priv->page_setup);
+
+	app->priv->page_setup = page_setup;
+}
+
+/* Returns a copy */
+GtkPrintSettings *
+_gedit_app_get_default_print_settings (GeditApp *app)
+{
+	g_return_val_if_fail (GEDIT_IS_APP (app), NULL);
+
+	if (app->priv->print_settings == NULL)
+		load_print_settings (app);
+
+	return gtk_print_settings_copy (app->priv->print_settings);
+}
+
+void
+_gedit_app_set_default_print_settings (GeditApp         *app,
+				       GtkPrintSettings *settings)
+{
+	g_return_if_fail (GEDIT_IS_APP (app));
+	g_return_if_fail (GTK_IS_PRINT_SETTINGS (settings));
+
+	if (app->priv->print_settings != NULL)
+		g_object_unref (app->priv->print_settings);
+
+	app->priv->print_settings = settings;
+}
+

Modified: branches/printing/gedit/gedit-app.h
==============================================================================
--- branches/printing/gedit/gedit-app.h	(original)
+++ branches/printing/gedit/gedit-app.h	Sun Jan 20 18:27:05 2008
@@ -129,6 +129,13 @@
 void		 _gedit_window_set_lockdown		(GeditWindow         *window,
 							 GeditLockdownMask    lockdown);
 
+/* global print config */
+GtkPageSetup		*_gedit_app_get_default_page_setup	(GeditApp         *app);
+void			 _gedit_app_set_default_page_setup	(GeditApp         *app,
+								 GtkPageSetup     *page_setup);
+GtkPrintSettings	*_gedit_app_get_default_print_settings	(GeditApp         *app);
+void			 _gedit_app_set_default_print_settings	(GeditApp         *app,
+								 GtkPrintSettings *settings);
 
 G_END_DECLS
 

Modified: branches/printing/gedit/gedit-commands-file-print.c
==============================================================================
--- branches/printing/gedit/gedit-commands-file-print.c	(original)
+++ branches/printing/gedit/gedit-commands-file-print.c	Sun Jan 20 18:27:05 2008
@@ -39,31 +39,9 @@
 
 #include "gedit-commands.h"
 #include "gedit-window.h"
+#include "gedit-tab.h"
 #include "gedit-debug.h"
 
-// FIXME: should these be per window? per tab? global?
-static GtkPageSetup *page_setup = NULL;
-static GtkPrintSettings *settings = NULL;
-
-static void
-page_setup_done_cb (GtkPageSetup *setup,
-		    GeditTab     *tab)
-{
-	if (setup != NULL)
-	{
-		g_print ("Page setup done\n");
-
-		if (page_setup)
-			g_object_unref (page_setup);
-
-		page_setup = setup;
-	}
-	else
-	{
-		g_print ("Page setup dismissed\n");
-	}
-}
-
 void
 _gedit_cmd_file_page_setup (GtkAction   *action,
 			    GeditWindow *window)
@@ -76,11 +54,7 @@
 	if (tab == NULL)
 		return;
 
-	gtk_print_run_page_setup_dialog_async (GTK_WINDOW (window),
-		 			       page_setup,
-		 			       settings,
-					       (GtkPageSetupDoneFunc) page_setup_done_cb,
-					       tab);
+	_gedit_tab_page_setup (tab);
 }
 
 void
@@ -95,7 +69,7 @@
 	if (tab == NULL)
 		return;
 
-	_gedit_tab_print_preview (tab, page_setup);
+	_gedit_tab_print_preview (tab);
 }
 
 void
@@ -110,6 +84,6 @@
 	if (tab == NULL)
 		return;
 
-	_gedit_tab_print (tab, page_setup);
+	_gedit_tab_print (tab);
 }
 

Modified: branches/printing/gedit/gedit-print-job.c
==============================================================================
--- branches/printing/gedit/gedit-print-job.c	(original)
+++ branches/printing/gedit/gedit-print-job.c	Sun Jan 20 18:27:05 2008
@@ -43,8 +43,6 @@
 #include "gedit-marshal.h"
 #include "gedit-utils.h"
 
-#define GEDIT_PRINT_SETTINGS_KEY  "gedit-print-settings-key"
-#define GEDIT_PRINT_SETTINGS_FILE "gedit-print-settings"
 
 #define GEDIT_PRINT_JOB_GET_PRIVATE(object)(G_TYPE_INSTANCE_GET_PRIVATE ((object), GEDIT_TYPE_PRINT_JOB, GeditPrintJobPrivate))
 
@@ -87,98 +85,12 @@
 
 G_DEFINE_TYPE (GeditPrintJob, gedit_print_job, G_TYPE_OBJECT)
 
-static gchar *
-get_settings_filename (void)
-{
-	const gchar *home;
-
-	home = g_get_home_dir ();
-	if (home == NULL)
-	{
-		g_warning ("Could not get home directory\n");
-		return NULL;
-	}
-
-	return g_build_filename (home,
-				 ".gnome2",
-				 "gedit",
-				 GEDIT_PRINT_SETTINGS_FILE,
-				 NULL);
-}
-
-static void
-load_print_settings (GeditPrintJob *job)
-{
-	gchar *filename;
-	GError *error = NULL;
-
-	g_return_if_fail (job->priv->settings == NULL);
-
-	filename = get_settings_filename ();
-
-	job->priv->settings = gtk_print_settings_new_from_file (filename,
-								&error);
-	if (error)
-	{
-		/* TODO: ignore file not found error */
-		g_warning (error->message);
-		g_error_free (error);
-	}
-
-	g_free (filename);
-
-	/* fall back to default settings */
-	if (job->priv->settings == NULL)
-		job->priv->settings = gtk_print_settings_new ();
-}
-
-static void
-save_print_settings (GeditPrintJob *job)
-{
-	gchar *filename;
-	GError *error = NULL;
-
-	if (job->priv->settings == NULL)
-		return;
-
-	filename = get_settings_filename ();
-
-	gtk_print_settings_to_file (job->priv->settings,
-				    filename,
-				    &error);
-	if (error)
-	{
-		g_warning (error->message);
-		g_error_free (error);
-	}
-
-	g_free (filename);
-}
 
 static void
 set_view (GeditPrintJob *job, GeditView *view)
 {
-	gpointer data;
-
 	job->priv->view = view;
 	job->priv->doc = GEDIT_DOCUMENT (gtk_text_view_get_buffer (GTK_TEXT_VIEW (view)));
-
-	data = g_object_get_data (G_OBJECT (job->priv->doc),
-				  GEDIT_PRINT_SETTINGS_KEY);
-
-	if (data == NULL)	
-	{
-		load_print_settings (job);
-
-		g_object_set_data_full (G_OBJECT (job->priv->doc),
-					GEDIT_PRINT_SETTINGS_KEY,
-					g_object_ref (job->priv->settings),
-					(GDestroyNotify)g_object_unref);
-	}
-	else
-	{
-		job->priv->settings = GTK_PRINT_SETTINGS (data);
-	}
 }
 
 static void 
@@ -234,9 +146,6 @@
 	if (job->priv->operation != NULL)
 		g_object_unref (job->priv->operation);
 
-	if (job->priv->settings != NULL)
-		g_object_unref (job->priv->settings);
-
 	G_OBJECT_CLASS (gedit_print_job_parent_class)->finalize (object);
 }
 
@@ -494,7 +403,6 @@
 
 		case GTK_PRINT_OPERATION_RESULT_APPLY:
 			print_result = GEDIT_PRINT_JOB_RESULT_OK;
-			save_print_settings (job);
 			break;
 
 		case GTK_PRINT_OPERATION_RESULT_ERROR:
@@ -522,6 +430,7 @@
 gedit_print_job_print (GeditPrintJob            *job,
 		       GtkPrintOperationAction   action,
 		       GtkPageSetup             *page_setup,
+		       GtkPrintSettings         *settings,
 		       GtkWindow                *parent,
 		       GError                  **error)
 {
@@ -535,11 +444,12 @@
 	/* Check if we are previewing */
 	priv->is_preview = (action == GTK_PRINT_OPERATION_ACTION_PREVIEW);
 
-	/* Creare print operation */
+	/* Create print operation */
 	job->priv->operation = gtk_print_operation_new ();
 
-	gtk_print_operation_set_print_settings (priv->operation,
-						priv->settings);
+	if (settings)
+		gtk_print_operation_set_print_settings (priv->operation,
+							settings);
 
 	if (page_setup != NULL)
 		gtk_print_operation_set_default_page_setup (priv->operation,
@@ -631,4 +541,11 @@
 	return job->priv->progress;
 }
 
+GtkPrintSettings *
+gedit_print_job_get_print_settings (GeditPrintJob *job)
+{
+	g_return_val_if_fail (GEDIT_IS_PRINT_JOB (job), NULL);
+
+	return gtk_print_operation_get_print_settings (job->priv->operation);
+}
 

Modified: branches/printing/gedit/gedit-print-job.h
==============================================================================
--- branches/printing/gedit/gedit-print-job.h	(original)
+++ branches/printing/gedit/gedit-print-job.h	Sun Jan 20 18:27:05 2008
@@ -114,6 +114,7 @@
 GtkPrintOperationResult	 gedit_print_job_print			(GeditPrintJob            *job,
 								 GtkPrintOperationAction   action,
 								 GtkPageSetup             *page_setup,
+								 GtkPrintSettings         *settings,
 								 GtkWindow                *parent,
 								 GError                  **error);
 
@@ -125,6 +126,8 @@
 
 gdouble                  gedit_print_job_get_progress           (GeditPrintJob            *job);
 
+GtkPrintSettings        *gedit_print_job_get_print_settings     (GeditPrintJob            *job);
+
 G_END_DECLS
 
 #endif /* __GEDIT_PRINT_JOB_H__ */

Modified: branches/printing/gedit/gedit-tab.c
==============================================================================
--- branches/printing/gedit/gedit-tab.c	(original)
+++ branches/printing/gedit/gedit-tab.c	Sun Jan 20 18:27:05 2008
@@ -2136,6 +2136,51 @@
 	gedit_document_save_as (doc, uri, encoding, tab->priv->save_flags);
 }
 
+#define GEDIT_PAGE_SETUP_KEY "gedit-page-setup-key"
+#define GEDIT_PRINT_SETTINGS_KEY "gedit-print-settings-key"
+
+static GtkPageSetup *
+get_page_setup (GeditTab *tab)
+{
+	gpointer data;
+	GeditDocument *doc;
+
+	doc = gedit_tab_get_document (tab);
+
+	data = g_object_get_data (G_OBJECT (doc),
+				  GEDIT_PAGE_SETUP_KEY);
+
+	if (data == NULL)
+	{
+		return _gedit_app_get_default_page_setup (gedit_app_get_default());
+	}
+	else
+	{
+		return GTK_PAGE_SETUP (data);
+	}
+}
+
+static GtkPrintSettings *
+get_print_settings (GeditTab *tab)
+{
+	gpointer data;
+	GeditDocument *doc;
+
+	doc = gedit_tab_get_document (tab);
+
+	data = g_object_get_data (G_OBJECT (doc),
+				  GEDIT_PRINT_SETTINGS_KEY);
+
+	if (data == NULL)
+	{
+		return _gedit_app_get_default_print_settings (gedit_app_get_default());
+	}
+	else
+	{
+		return GTK_PRINT_SETTINGS (data);
+	}
+}
+
 /* FIXME: show the message area only if the operation will be "long" */
 static void
 printing_cb (GeditPrintJob       *job,
@@ -2179,6 +2224,27 @@
 
 	// TODO: check status and error
 
+	/* Save the print settings */ 
+	if (result == GTK_PRINT_OPERATION_RESULT_APPLY)
+	{
+		GeditDocument *doc;
+		GtkPrintSettings *settings;
+
+		doc = gedit_tab_get_document (tab);
+
+		settings = gedit_print_job_get_print_settings (job);
+
+		/* remember them for this document */
+		g_object_set_data_full (G_OBJECT (doc),
+					GEDIT_PRINT_SETTINGS_KEY,
+					g_object_ref (settings),
+					(GDestroyNotify)g_object_unref);
+
+		/* make them the default */
+		_gedit_app_set_default_print_settings (gedit_app_get_default (),
+						       settings);
+	}
+
 #if 0
 	if (tab->priv->print_preview != NULL)
 	{
@@ -2342,12 +2408,55 @@
 }
 
 static void
+page_setup_done_cb (GtkPageSetup *setup,
+		    GeditTab     *tab)
+{
+	if (setup != NULL)
+	{
+		GeditDocument *doc;
+
+		doc = gedit_tab_get_document (tab);
+
+		/* remember it for this document */
+		g_object_set_data_full (G_OBJECT (doc),
+					GEDIT_PAGE_SETUP_KEY,
+					g_object_ref (setup),
+					(GDestroyNotify)g_object_unref);
+
+		/* make it the default */
+		_gedit_app_set_default_page_setup (gedit_app_get_default(),
+						   setup);
+	}
+}
+
+void 
+_gedit_tab_page_setup (GeditTab *tab)
+{
+	GtkPageSetup *setup;
+	GtkPrintSettings *settings;
+
+	g_return_if_fail (GEDIT_IS_TAB (tab));
+
+	setup = get_page_setup (tab);
+	settings = get_print_settings (tab);
+
+	gtk_print_run_page_setup_dialog_async (GTK_WINDOW (gtk_widget_get_toplevel (GTK_WIDGET (tab))),
+		 			       setup,
+		 			       settings,
+					       (GtkPageSetupDoneFunc) page_setup_done_cb,
+					       tab);
+
+	/* CHECK: should we unref setup and settings? */
+}
+
+static void
 gedit_tab_print_or_print_preview (GeditTab                *tab,
-				  GtkPrintOperationAction  print_action,
-				  GtkPageSetup            *page_setup)
+				  GtkPrintOperationAction  print_action)
 {
 	GeditView *view;
 	gboolean is_preview;
+	GtkPageSetup *setup;
+	GtkPrintSettings *settings;
 	GtkPrintOperationResult res;
 	GError *error = NULL;
 
@@ -2382,9 +2491,13 @@
 	else
 		gedit_tab_set_state (tab, GEDIT_TAB_STATE_PRINTING);
 
+	setup = get_page_setup (tab);
+	settings = get_print_settings (tab);
+
 	res = gedit_print_job_print (tab->priv->print_job,
 				     print_action,
-				     page_setup,
+				     setup,
+				     settings,
 				     GTK_WINDOW (gtk_widget_get_toplevel (GTK_WIDGET (tab))),
 				     &error);
 
@@ -2400,25 +2513,29 @@
 }
 
 void 
-_gedit_tab_print (GeditTab     *tab,
-		  GtkPageSetup *page_setup)
+_gedit_tab_print (GeditTab     *tab)
 {
 	g_return_if_fail (GEDIT_IS_TAB (tab));
 
+	/* FIXME: currently we can have just one printoperation going on
+	 * at a given time, so before starting the print we close the preview.
+	 * Would be nice to handle it properly though */
+	if (tab->priv->state == GEDIT_TAB_STATE_SHOWING_PRINT_PREVIEW)
+	{
+		gtk_widget_destroy (tab->priv->print_preview);
+	}
+
 	gedit_tab_print_or_print_preview (tab,
-					  GTK_PRINT_OPERATION_ACTION_PRINT_DIALOG,
-					  page_setup);
+					  GTK_PRINT_OPERATION_ACTION_PRINT_DIALOG);
 }
 
 void
-_gedit_tab_print_preview (GeditTab     *tab,
-			  GtkPageSetup *page_setup)
+_gedit_tab_print_preview (GeditTab     *tab)
 {
 	g_return_if_fail (GEDIT_IS_TAB (tab));
 
 	gedit_tab_print_or_print_preview (tab,
-					  GTK_PRINT_OPERATION_ACTION_PREVIEW,
-					  page_setup);
+					  GTK_PRINT_OPERATION_ACTION_PREVIEW);
 }
 
 void 

Modified: branches/printing/gedit/gedit-tab.h
==============================================================================
--- branches/printing/gedit/gedit-tab.h	(original)
+++ branches/printing/gedit/gedit-tab.h	Sun Jan 20 18:27:05 2008
@@ -146,10 +146,9 @@
 						 const gchar         *uri,
 						 const GeditEncoding *encoding);
 
-void		 _gedit_tab_print		(GeditTab            *tab,
-						 GtkPageSetup        *page_setup);
-void		 _gedit_tab_print_preview	(GeditTab            *tab,
-						 GtkPageSetup        *page_setup);
+void		 _gedit_tab_page_setup		(GeditTab            *tab);
+void		 _gedit_tab_print		(GeditTab            *tab);
+void		 _gedit_tab_print_preview	(GeditTab            *tab);
 
 void		 _gedit_tab_mark_for_closing	(GeditTab	     *tab);
 



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