gedit r6086 - branches/printing/gedit



Author: pborelli
Date: Mon Jan 14 12:06:19 2008
New Revision: 6086
URL: http://svn.gnome.org/viewvc/gedit?rev=6086&view=rev

Log:
make sure the gtk preview is terminated when destroying the preview widget so that the "done" signal is emitted and the state machine goes on properly.
Also commit some code for print settings I had in my tree (not used yet).


Modified:
   branches/printing/gedit/gedit-print-job.c
   branches/printing/gedit/gedit-tab.c

Modified: branches/printing/gedit/gedit-print-job.c
==============================================================================
--- branches/printing/gedit/gedit-print-job.c	(original)
+++ branches/printing/gedit/gedit-print-job.c	Mon Jan 14 12:06:19 2008
@@ -43,16 +43,21 @@
 #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))
 
 struct _GeditPrintJobPrivate
 {
 	GeditView                *view;
 	GeditDocument            *doc;
-	
+
 	GtkPrintOperation        *operation;
 	GtkSourcePrintCompositor *compositor;
 
+	GtkPrintSettings         *settings;
+
 	GtkWidget                *preview;
 
 	GeditPrintJobStatus       status;
@@ -80,11 +85,98 @@
 
 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
-set_view (GeditPrintJob *job, GeditView *view) 
+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 
@@ -140,6 +232,9 @@
 	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);
 }
 
@@ -199,10 +294,6 @@
 	g_type_class_add_private (object_class, sizeof (GeditPrintJobPrivate));
 }
 
-// Print setting and page setup vanno settati per document e fall back al file salvato
-// Vedi buffer_set
-#define GEDIT_PRINT_CONFIG "gedit-print-config-key"
-
 static void
 create_compositor (GeditPrintJob *job)
 {
@@ -282,6 +373,13 @@
 	g_signal_emit (job, print_job_signals[SHOW_PREVIEW], 0, job->priv->preview);
 }
 
+static void
+preview_destroyed (GtkWidget                *preview,
+		   GtkPrintOperationPreview *gtk_preview)
+{
+	gtk_print_operation_preview_end_preview (gtk_preview);
+}
+
 static gboolean 
 preview_cb (GtkPrintOperation        *op,
 	    GtkPrintOperationPreview *gtk_preview,
@@ -296,6 +394,12 @@
 				G_CALLBACK (preview_ready),
 				job);
 
+	/* FIXME: should this go in the preview widget itself? */
+	g_signal_connect (job->priv->preview,
+			  "destroy",
+			  G_CALLBACK (preview_destroyed),
+			  gtk_preview);
+
 	return TRUE;
 }
 
@@ -375,17 +479,17 @@
 		case GTK_PRINT_OPERATION_RESULT_CANCEL:
 			print_result = GEDIT_PRINT_JOB_RESULT_CANCEL;
 			break;
-		
+
 		case GTK_PRINT_OPERATION_RESULT_APPLY:
 			print_result = GEDIT_PRINT_JOB_RESULT_OK;
-			// TODO: save settings
+			save_print_settings (job);
 			break;
-			
+
 		case GTK_PRINT_OPERATION_RESULT_ERROR:
 			print_result = GEDIT_PRINT_JOB_RESULT_ERROR;
 			gtk_print_operation_get_error (operation, &error);
 			break;
-			
+
 		default:
 			g_return_if_reached ();
 	}
@@ -400,14 +504,6 @@
 	
 	g_object_unref (job);
 }
-	      
-static GtkPrintSettings *
-get_print_settings (GeditPrintJob  *job,
-		    GError        **error)
-{
-	/* TODO */
-	return NULL;
-}
 
 static GtkPageSetup *
 get_page_setup (GeditPrintJob  *job,
@@ -425,24 +521,22 @@
 		       GError                  **error)
 {
 	gchar            *job_name;
-	GtkPrintSettings *settings;
 	GtkPageSetup     *page_setup;
 	
 	g_return_val_if_fail (job->priv->compositor == NULL, GTK_PRINT_OPERATION_RESULT_ERROR);
 	
 	/* Get print setting and page_setup */
-	settings = get_print_settings (job, error);
 	page_setup = get_page_setup (job, error);
 	
 	/* Creare print operation */
 	job->priv->operation = gtk_print_operation_new ();
-	
-	if (settings != NULL) 
-		gtk_print_operation_set_print_settings (job->priv->operation, settings);
-	    
+
+	gtk_print_operation_set_print_settings (job->priv->operation,
+						job->priv->settings);
+
 	if (page_setup != NULL)
 		gtk_print_operation_set_default_page_setup (job->priv->operation, page_setup);
-		
+	
 	job_name = gedit_document_get_short_name_for_display (job->priv->doc);
 	
 	gtk_print_operation_set_job_name (job->priv->operation, job_name);
@@ -471,7 +565,6 @@
 			  "end-print", 
 			  G_CALLBACK (end_print_cb),
 			  job);
-
 	g_signal_connect (job->priv->operation,
 			  "done", 
 			  G_CALLBACK (done_cb),

Modified: branches/printing/gedit/gedit-tab.c
==============================================================================
--- branches/printing/gedit/gedit-tab.c	(original)
+++ branches/printing/gedit/gedit-tab.c	Mon Jan 14 12:06:19 2008
@@ -2160,10 +2160,22 @@
 		  GeditTab            *tab)
 {
 	GeditView *view;
-	
-	g_return_if_fail (GEDIT_IS_PROGRESS_MESSAGE_AREA (tab->priv->message_area));
 
-	set_message_area (tab, NULL); /* destroy the message area */
+	g_return_if_fail (tab->priv->state == GEDIT_TAB_STATE_PRINT_PREVIEWING ||
+			  tab->priv->state == GEDIT_TAB_STATE_SHOWING_PRINT_PREVIEW ||
+			  tab->priv->state == GEDIT_TAB_STATE_PRINTING);
+
+	if (tab->priv->state == GEDIT_TAB_STATE_SHOWING_PRINT_PREVIEW)
+	{
+		/* print preview has been destroyed... */
+		tab->priv->print_preview = NULL;
+	}
+	else
+	{
+		g_return_if_fail (GEDIT_IS_PROGRESS_MESSAGE_AREA (tab->priv->message_area));
+
+		set_message_area (tab, NULL); /* destroy the message area */
+	}
 
 	// TODO: check status and error
 
@@ -2181,10 +2193,12 @@
 
 	view = gedit_tab_get_view (tab);
 	gtk_widget_grab_focus (GTK_WIDGET (view));
-	
+
  	g_object_unref (tab->priv->print_job);
+	tab->priv->print_job = NULL;
 }
 
+#if 0
 static void
 print_preview_destroyed (GtkWidget *preview,
 			 GeditTab  *tab)
@@ -2210,6 +2224,7 @@
 		g_return_if_fail (tab->priv->state == GEDIT_TAB_STATE_PRINTING);
 	}	
 }
+#endif
 
 static void
 show_preview_cb (GeditPrintJob       *job,
@@ -2230,11 +2245,12 @@
 	gtk_widget_show (tab->priv->print_preview);
 	gtk_widget_grab_focus (tab->priv->print_preview);
 
+/* when the preview gets destroyed we get "done" signal
 	g_signal_connect (tab->priv->print_preview,
 			  "destroy",
 			  G_CALLBACK (print_preview_destroyed),
 			  tab);	
-
+*/
 	gedit_tab_set_state (tab, GEDIT_TAB_STATE_SHOWING_PRINT_PREVIEW);
 }
 



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