[gthumb/ext] ask whether to save the image before returning to the browser mode



commit 37db5262058bee65ae97309cca8e7f3b59006f69
Author: Paolo Bacchilega <paobac src gnome org>
Date:   Fri Sep 11 22:11:16 2009 +0200

    ask whether to save the image before returning to the browser mode

 extensions/image_viewer/gth-image-viewer-page.c    |   81 ++++++++++++--------
 extensions/red_eye_removal/gth-file-tool-red-eye.c |   14 ++--
 gthumb/gth-browser.c                               |   37 ++++++++-
 gthumb/gth-image-history.c                         |   26 ++++++-
 gthumb/gth-image-history.h                         |    3 +-
 gthumb/gth-sidebar.c                               |   13 +++-
 gthumb/gth-sidebar.h                               |    1 +
 gthumb/gth-toolbox.c                               |    7 ++
 gthumb/gth-toolbox.h                               |    1 +
 gthumb/gth-viewer-page.c                           |    8 ++
 gthumb/gth-viewer-page.h                           |    2 +
 11 files changed, 145 insertions(+), 48 deletions(-)
---
diff --git a/extensions/image_viewer/gth-image-viewer-page.c b/extensions/image_viewer/gth-image-viewer-page.c
index 412399e..0b3243d 100644
--- a/extensions/image_viewer/gth-image-viewer-page.c
+++ b/extensions/image_viewer/gth-image-viewer-page.c
@@ -188,7 +188,6 @@ image_ready_cb (GtkWidget          *widget,
 
 	g_file_info_set_attribute_boolean (self->priv->file_data->info, "gth::file::is-modified", FALSE);
 	gth_monitor_metadata_changed (gth_main_get_default_monitor (), self->priv->file_data);
-
 }
 
 
@@ -588,8 +587,11 @@ gth_image_viewer_page_real_view (GthViewerPage *base,
 	self = (GthImageViewerPage*) base;
 	g_return_if_fail (file_data != NULL);
 
-	if ((self->priv->file_data != NULL) && g_file_equal (file_data->file, self->priv->file_data->file))
+	if ((self->priv->file_data != NULL)
+	    && g_file_equal (file_data->file, self->priv->file_data->file))
+	{
 		return;
+	}
 
 	_g_object_unref (self->priv->file_data);
 	self->priv->file_data = gth_file_data_dup (file_data);
@@ -1004,6 +1006,50 @@ gth_image_viewer_page_real_save_as (GthViewerPage *base,
 
 
 static void
+_gth_image_viewer_page_set_pixbuf (GthImageViewerPage *self,
+				   GdkPixbuf          *pixbuf,
+				   gboolean            modified)
+{
+	GthFileData *file_data;
+	int          width;
+	int          height;
+	char        *size;
+
+	gth_image_viewer_set_pixbuf (GTH_IMAGE_VIEWER (self->priv->viewer), pixbuf);
+
+	file_data = gth_browser_get_current_file (GTH_BROWSER (self->priv->browser));
+
+	g_file_info_set_attribute_boolean (file_data->info, "gth::file::is-modified", modified);
+
+	width = gdk_pixbuf_get_width (pixbuf);
+	height = gdk_pixbuf_get_height (pixbuf);
+	g_file_info_set_attribute_int32 (file_data->info, "image::width", width);
+	g_file_info_set_attribute_int32 (file_data->info, "image::height", height);
+
+	size = g_strdup_printf ("%d x %d", width, height);
+	g_file_info_set_attribute_string (file_data->info, "image::size", size);
+
+	gth_monitor_metadata_changed (gth_main_get_default_monitor (), file_data);
+
+	g_free (size);
+}
+
+
+static void
+gth_image_viewer_page_real_revert (GthViewerPage *base)
+{
+	GthImageViewerPage *self = GTH_IMAGE_VIEWER_PAGE (base);
+	GthImageData       *idata;
+
+	idata = gth_image_history_revert (self->priv->history);
+	if (idata != NULL) {
+		_gth_image_viewer_page_set_pixbuf (self, idata->image, idata->unsaved);
+		gth_image_data_unref (idata);
+	}
+}
+
+
+static void
 gth_image_viewer_page_finalize (GObject *obj)
 {
 	GthImageViewerPage *self;
@@ -1042,6 +1088,7 @@ gth_viewer_page_interface_init (GthViewerPageIface *iface)
 	iface->can_save = gth_image_viewer_page_real_can_save;
 	iface->save = gth_image_viewer_page_real_save;
 	iface->save_as = gth_image_viewer_page_real_save_as;
+	iface->revert = gth_image_viewer_page_real_revert;
 }
 
 
@@ -1095,36 +1142,6 @@ gth_image_viewer_page_get_pixbuf (GthImageViewerPage *self)
 }
 
 
-static void
-_gth_image_viewer_page_set_pixbuf (GthImageViewerPage *self,
-				   GdkPixbuf          *pixbuf,
-				   gboolean            modified)
-{
-	GthFileData *file_data;
-	int          width;
-	int          height;
-	char        *size;
-
-	gth_image_viewer_set_pixbuf (GTH_IMAGE_VIEWER (self->priv->viewer), pixbuf);
-
-	file_data = gth_browser_get_current_file (GTH_BROWSER (self->priv->browser));
-
-	g_file_info_set_attribute_boolean (file_data->info, "gth::file::is-modified", modified);
-
-	width = gdk_pixbuf_get_width (pixbuf);
-	height = gdk_pixbuf_get_height (pixbuf);
-	g_file_info_set_attribute_int32 (file_data->info, "image::width", width);
-	g_file_info_set_attribute_int32 (file_data->info, "image::height", height);
-
-	size = g_strdup_printf ("%d x %d", width, height);
-	g_file_info_set_attribute_string (file_data->info, "image::size", size);
-
-	gth_monitor_metadata_changed (gth_main_get_default_monitor (), file_data);
-
-	g_free (size);
-}
-
-
 void
 gth_image_viewer_page_set_pixbuf (GthImageViewerPage *self,
 				  GdkPixbuf          *pixbuf)
diff --git a/extensions/red_eye_removal/gth-file-tool-red-eye.c b/extensions/red_eye_removal/gth-file-tool-red-eye.c
index a9e5b5c..3e73c5a 100644
--- a/extensions/red_eye_removal/gth-file-tool-red-eye.c
+++ b/extensions/red_eye_removal/gth-file-tool-red-eye.c
@@ -362,10 +362,10 @@ static GtkWidget *
 gth_file_tool_red_eye_get_options (GthFileTool *base)
 {
 	GthFileToolRedEye *self;
-	GtkWidget       *window;
-	GtkWidget       *viewer_page;
-	GtkWidget       *viewer;
-	GtkWidget       *options;
+	GtkWidget         *window;
+	GtkWidget         *viewer_page;
+	GtkWidget         *viewer;
+	GtkWidget         *options;
 
 	self = (GthFileToolRedEye *) base;
 
@@ -403,9 +403,9 @@ static void
 gth_file_tool_red_eye_destroy_options (GthFileTool *base)
 {
 	GthFileToolRedEye *self;
-	GtkWidget       *window;
-	GtkWidget       *viewer_page;
-	GtkWidget       *viewer;
+	GtkWidget         *window;
+	GtkWidget         *viewer_page;
+	GtkWidget         *viewer;
 
 	self = (GthFileToolRedEye *) base;
 
diff --git a/gthumb/gth-browser.c b/gthumb/gth-browser.c
index d3f1592..00095d7 100644
--- a/gthumb/gth-browser.c
+++ b/gthumb/gth-browser.c
@@ -1985,9 +1985,12 @@ _gth_browser_update_browser_ui (GthBrowser *browser,
 }
 
 
+/* --- _gth_browser_set_current_page --- */
+
+
 static void
-_gth_browser_set_current_page (GthWindow *window,
-			       int        page)
+_gth_browser_real_set_current_page (GthWindow *window,
+				    int        page)
 {
 	GthBrowser *browser = (GthBrowser *) window;
 
@@ -2003,6 +2006,35 @@ _gth_browser_set_current_page (GthWindow *window,
 
 
 static void
+set_current_page__file_saved_cb (GthBrowser *browser,
+				 gboolean    cancelled,
+				 gpointer    user_data)
+{
+	if (cancelled)
+		return;
+
+	if (browser->priv->current_file != NULL)
+		gth_viewer_page_revert (browser->priv->viewer_page);
+	_gth_browser_real_set_current_page (GTH_WINDOW (browser), GPOINTER_TO_INT (user_data));
+}
+
+
+static void
+_gth_browser_set_current_page (GthWindow *window,
+			       int        page)
+{
+	GthBrowser *browser = GTH_BROWSER (window);
+
+	if (gth_browser_get_file_modified (browser))
+		_gth_browser_ask_whether_to_save (browser,
+						  set_current_page__file_saved_cb,
+						  GINT_TO_POINTER (page));
+	else
+		_gth_browser_real_set_current_page (window, page);
+}
+
+
+static void
 gth_browser_init (GthBrowser *browser)
 {
 	int i;
@@ -3609,7 +3641,6 @@ gth_browser_go_home (GthBrowser *browser)
 	GFile *location;
 
 	location = g_file_new_for_uri (gth_pref_get_startup_location ());
-	gth_window_set_current_page (GTH_WINDOW (browser), GTH_BROWSER_PAGE_BROWSER);
 	gth_browser_go_to (browser, location, NULL);
 
 	g_object_unref (location);
diff --git a/gthumb/gth-image-history.c b/gthumb/gth-image-history.c
index 50b519e..f29edd9 100644
--- a/gthumb/gth-image-history.c
+++ b/gthumb/gth-image-history.c
@@ -49,11 +49,12 @@ gth_image_data_new (GdkPixbuf *image,
 }
 
 
-void
+GthImageData *
 gth_image_data_ref (GthImageData *idata)
 {
-	g_return_if_fail (idata != NULL);
+	g_return_val_if_fail (idata != NULL, NULL);
 	idata->ref++;
+	return idata;
 }
 
 
@@ -279,6 +280,27 @@ gth_image_history_undo (GthImageHistory *history,
 
 
 GthImageData *
+gth_image_history_revert (GthImageHistory *history)
+{
+	GthImageData *last_saved = NULL;
+	GList        *scan;
+
+	for (scan = history->priv->undo_history; scan; scan = scan->next) {
+		GthImageData *idata = scan->data;
+
+		if (idata->unsaved)
+			continue;
+
+		last_saved = gth_image_data_ref (idata);
+	}
+
+	gth_image_history_clear (history);
+
+	return last_saved;
+}
+
+
+GthImageData *
 gth_image_history_redo (GthImageHistory *history,
 			GdkPixbuf       *current_image,
 			gboolean         image_is_unsaved)
diff --git a/gthumb/gth-image-history.h b/gthumb/gth-image-history.h
index 8c8dbc3..9dfacdd 100644
--- a/gthumb/gth-image-history.h
+++ b/gthumb/gth-image-history.h
@@ -60,7 +60,7 @@ struct _GthImageHistoryClass {
 
 GthImageData *    gth_image_data_new           (GdkPixbuf       *image,
 						gboolean         unsaved);
-void              gth_image_data_ref           (GthImageData    *idata);
+GthImageData *    gth_image_data_ref           (GthImageData    *idata);
 void              gth_image_data_unref         (GthImageData    *idata);
 void              gth_image_data_list_free     (GList           *list);
 
@@ -78,6 +78,7 @@ GthImageData *    gth_image_history_redo       (GthImageHistory *history,
 void              gth_image_history_clear      (GthImageHistory *history);
 gboolean          gth_image_history_can_undo   (GthImageHistory *history);
 gboolean          gth_image_history_can_redo   (GthImageHistory *history);
+GthImageData *    gth_image_history_revert     (GthImageHistory *history);
 
 G_END_DECLS
 
diff --git a/gthumb/gth-sidebar.c b/gthumb/gth-sidebar.c
index 3aa966f..e63d12d 100644
--- a/gthumb/gth-sidebar.c
+++ b/gthumb/gth-sidebar.c
@@ -146,7 +146,7 @@ gth_sidebar_set_file (GthSidebar  *sidebar,
 	GList *children;
 	GList *scan;
 
-	/*gth_toolbox_deactivate_tool (GTH_TOOLBOX (sidebar->priv->toolbox)); FIXME */
+	gth_toolbox_deactivate_tool (GTH_TOOLBOX (sidebar->priv->toolbox));
 
 	children = gth_multipage_get_children (GTH_MULTIPAGE (sidebar->priv->properties));
 	for (scan = children; scan; scan = scan->next) {
@@ -165,8 +165,8 @@ gth_sidebar_set_file (GthSidebar  *sidebar,
 void
 gth_sidebar_show_properties (GthSidebar *sidebar)
 {
-	/*if (gtk_notebook_get_current_page (GTK_NOTEBOOK (sidebar)) == GTH_SIDEBAR_PAGE_TOOLS)
-		gth_toolbox_deactivate_tool (GTH_TOOLBOX (sidebar->priv->toolbox)); FIXME */
+	if (gtk_notebook_get_current_page (GTK_NOTEBOOK (sidebar)) == GTH_SIDEBAR_PAGE_TOOLS)
+		gth_toolbox_deactivate_tool (GTH_TOOLBOX (sidebar->priv->toolbox));
 	gtk_notebook_set_current_page (GTK_NOTEBOOK (sidebar), GTH_SIDEBAR_PAGE_PROPERTIES);
 }
 
@@ -178,6 +178,13 @@ gth_sidebar_show_tools (GthSidebar *sidebar)
 }
 
 
+gboolean
+gth_sidebar_is_tool_active (GthSidebar *sidebar)
+{
+	return gth_toolbox_is_tool_active (GTH_TOOLBOX (sidebar->priv->toolbox));
+}
+
+
 void
 gth_sidebar_update_sensitivity (GthSidebar *sidebar)
 {
diff --git a/gthumb/gth-sidebar.h b/gthumb/gth-sidebar.h
index 3b8d840..4fe45b1 100644
--- a/gthumb/gth-sidebar.h
+++ b/gthumb/gth-sidebar.h
@@ -70,6 +70,7 @@ void           gth_sidebar_set_file            (GthSidebar      *sidebar,
 						GthFileData     *file_data);
 void           gth_sidebar_show_properties     (GthSidebar      *sidebar);
 void           gth_sidebar_show_tools          (GthSidebar      *sidebar);
+gboolean       gth_sidebar_is_tool_active      (GthSidebar      *sidebar);
 void           gth_sidebar_update_sensitivity  (GthSidebar      *sidebar);
 
 GType          gth_property_view_get_type      (void);
diff --git a/gthumb/gth-toolbox.c b/gthumb/gth-toolbox.c
index 6693448..9714f8d 100644
--- a/gthumb/gth-toolbox.c
+++ b/gthumb/gth-toolbox.c
@@ -331,3 +331,10 @@ gth_toolbox_deactivate_tool (GthToolbox *toolbox)
 	if (toolbox->priv->active_tool != NULL)
 		gth_file_tool_hide_options (GTH_FILE_TOOL (toolbox->priv->active_tool));
 }
+
+
+gboolean
+gth_toolbox_is_tool_active (GthToolbox *toolbox)
+{
+	return toolbox->priv->active_tool != NULL;
+}
diff --git a/gthumb/gth-toolbox.h b/gthumb/gth-toolbox.h
index 1e56bb0..455a67d 100644
--- a/gthumb/gth-toolbox.h
+++ b/gthumb/gth-toolbox.h
@@ -53,6 +53,7 @@ GType          gth_toolbox_get_type              (void);
 GtkWidget *    gth_toolbox_new                   (const char  *name);
 void           gth_toolbox_update_sensitivity    (GthToolbox  *toolbox);
 void           gth_toolbox_deactivate_tool       (GthToolbox  *toolbox);
+gboolean       gth_toolbox_is_tool_active        (GthToolbox  *toolbox);
 
 G_END_DECLS
 
diff --git a/gthumb/gth-viewer-page.c b/gthumb/gth-viewer-page.c
index 792b57a..5176d39 100644
--- a/gthumb/gth-viewer-page.c
+++ b/gthumb/gth-viewer-page.c
@@ -131,6 +131,7 @@ gth_viewer_page_save (GthViewerPage *self,
 	GTH_VIEWER_PAGE_GET_INTERFACE (self)->save (self, file, func, data);
 }
 
+
 void
 gth_viewer_page_save_as (GthViewerPage  *self,
 			 FileSavedFunc   func,
@@ -138,3 +139,10 @@ gth_viewer_page_save_as (GthViewerPage  *self,
 {
 	GTH_VIEWER_PAGE_GET_INTERFACE (self)->save_as (self, func, data);
 }
+
+
+void
+gth_viewer_page_revert (GthViewerPage *self)
+{
+	GTH_VIEWER_PAGE_GET_INTERFACE (self)->revert (self);
+}
diff --git a/gthumb/gth-viewer-page.h b/gthumb/gth-viewer-page.h
index f4ef68c..4e36e57 100644
--- a/gthumb/gth-viewer-page.h
+++ b/gthumb/gth-viewer-page.h
@@ -67,6 +67,7 @@ struct _GthViewerPageIface {
 	void      (*save_as)             (GthViewerPage *self,
 					  FileSavedFunc  func,
 					  gpointer       data);
+	void      (*revert)              (GthViewerPage *self);
 };
 
 GType        gth_viewer_page_get_type            (void);
@@ -92,6 +93,7 @@ void         gth_viewer_page_save                (GthViewerPage  *self,
 void         gth_viewer_page_save_as             (GthViewerPage  *self,
 						  FileSavedFunc   func,
 						  gpointer        data);
+void         gth_viewer_page_revert              (GthViewerPage  *self);
 
 G_END_DECLS
 



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