[gthumb/ext] fixed undo/redo system



commit 59fb8167ac1979b8516a96350c2463fff8a99752
Author: Paolo Bacchilega <paobac src gnome org>
Date:   Sun Sep 13 23:13:03 2009 +0200

    fixed undo/redo system

 .../file_tools/gth-file-tool-adjust-colors.c       |   14 +---
 extensions/file_tools/gth-file-tool-desaturate.c   |    2 -
 extensions/image_viewer/gth-image-viewer-page.c    |   36 ++++++----
 extensions/image_viewer/gth-image-viewer-page.h    |    1 +
 gthumb/gth-image-history.c                         |   77 +++++++++++---------
 gthumb/gth-image-history.h                         |    9 +--
 6 files changed, 71 insertions(+), 68 deletions(-)
---
diff --git a/extensions/file_tools/gth-file-tool-adjust-colors.c b/extensions/file_tools/gth-file-tool-adjust-colors.c
index 1fa88b8..45d56a0 100644
--- a/extensions/file_tools/gth-file-tool-adjust-colors.c
+++ b/extensions/file_tools/gth-file-tool-adjust-colors.c
@@ -398,9 +398,6 @@ adjust_colors_release (GthPixbufTask *pixop,
 {
 	AdjustData *data = pixop->data;
 
-	if (error == NULL)
-		gth_image_viewer_page_set_pixbuf (GTH_IMAGE_VIEWER_PAGE (data->viewer_page), pixop->dest, TRUE);
-
 	g_object_unref (data->viewer_page);
 	pixbuf_cache_free (data->cache);
 	g_free (data->hs);
@@ -441,8 +438,8 @@ static void
 cancel_button_clicked_cb (GtkButton               *button,
 			  GthFileToolAdjustColors *self)
 {
-	GtkWidget   *window;
-	GthFileData *current_file;
+	GtkWidget *window;
+	GtkWidget *viewer_page;
 
 	if (self->priv->apply_event != 0) {
 		g_source_remove (self->priv->apply_event);
@@ -450,11 +447,8 @@ cancel_button_clicked_cb (GtkButton               *button,
 	}
 
 	window = gth_file_tool_get_window (GTH_FILE_TOOL (self));
-	current_file = gth_browser_get_current_file (GTH_BROWSER (window));
-	if (current_file != NULL) {
-		g_file_info_set_attribute_boolean (current_file->info, "gth::file::is-modified", FALSE);
-		gth_monitor_metadata_changed (gth_main_get_default_monitor (), current_file);
-	}
+	viewer_page = gth_browser_get_viewer_page (GTH_BROWSER (window));
+	gth_image_viewer_page_reset (GTH_IMAGE_VIEWER_PAGE (viewer_page));
 
 	gth_file_tool_hide_options (GTH_FILE_TOOL (self));
 }
diff --git a/extensions/file_tools/gth-file-tool-desaturate.c b/extensions/file_tools/gth-file-tool-desaturate.c
index b093f0b..fd74532 100644
--- a/extensions/file_tools/gth-file-tool-desaturate.c
+++ b/extensions/file_tools/gth-file-tool-desaturate.c
@@ -48,10 +48,8 @@ desaturate_step (GthPixbufTask *pixop)
 
 	max = MAX (pixop->src_pixel[RED_PIX], pixop->src_pixel[GREEN_PIX]);
 	max = MAX (max, pixop->src_pixel[BLUE_PIX]);
-
 	min = MIN (pixop->src_pixel[RED_PIX], pixop->src_pixel[GREEN_PIX]);
 	min = MIN (min, pixop->src_pixel[BLUE_PIX]);
-
 	lightness = (max + min) / 2;
 
 	pixop->dest_pixel[RED_PIX]   = lightness;
diff --git a/extensions/image_viewer/gth-image-viewer-page.c b/extensions/image_viewer/gth-image-viewer-page.c
index 5df5828..eb82cb5 100644
--- a/extensions/image_viewer/gth-image-viewer-page.c
+++ b/extensions/image_viewer/gth-image-viewer-page.c
@@ -185,6 +185,9 @@ image_ready_cb (GtkWidget          *widget,
 		GthImageViewerPage *self)
 {
 	gth_image_history_clear (self->priv->history);
+	gth_image_history_add_image (self->priv->history,
+				     gth_image_viewer_get_current_pixbuf (GTH_IMAGE_VIEWER (self->priv->viewer)),
+				     FALSE);
 
 	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);
@@ -1148,9 +1151,7 @@ gth_image_viewer_page_set_pixbuf (GthImageViewerPage *self,
 				  gboolean            add_to_history)
 {
 	if (add_to_history)
-		gth_image_history_add_image (self->priv->history,
-					     gth_image_viewer_page_get_pixbuf (self),
-					     gth_browser_get_file_modified (GTH_BROWSER (self->priv->browser)));
+		gth_image_history_add_image (self->priv->history, pixbuf, TRUE);
 	_gth_image_viewer_page_set_pixbuf (self, pixbuf, TRUE);
 }
 
@@ -1160,13 +1161,9 @@ gth_image_viewer_page_undo (GthImageViewerPage *self)
 {
 	GthImageData *idata;
 
-	idata = gth_image_history_undo (self->priv->history,
-					gth_image_viewer_page_get_pixbuf (self),
-					gth_browser_get_file_modified (GTH_BROWSER (self->priv->browser)));
-	if (idata != NULL) {
+	idata = gth_image_history_undo (self->priv->history);
+	if (idata != NULL)
 		_gth_image_viewer_page_set_pixbuf (self, idata->image, idata->unsaved);
-		gth_image_data_unref (idata);
-	}
 }
 
 
@@ -1175,13 +1172,9 @@ gth_image_viewer_page_redo (GthImageViewerPage *self)
 {
 	GthImageData *idata;
 
-	idata = gth_image_history_redo (self->priv->history,
-					gth_image_viewer_page_get_pixbuf (self),
-					gth_browser_get_file_modified (GTH_BROWSER (self->priv->browser)));
-	if (idata != NULL) {
+	idata = gth_image_history_redo (self->priv->history);
+	if (idata != NULL)
 		_gth_image_viewer_page_set_pixbuf (self, idata->image, idata->unsaved);
-		gth_image_data_unref (idata);
-	}
 }
 
 
@@ -1190,3 +1183,16 @@ gth_image_viewer_page_get_history (GthImageViewerPage *self)
 {
 	return self->priv->history;
 }
+
+
+void
+gth_image_viewer_page_reset (GthImageViewerPage *self)
+{
+	GthImageData *last_image;
+
+	last_image = gth_image_history_get_last (self->priv->history);
+	if (last_image == NULL)
+		return;
+
+	_gth_image_viewer_page_set_pixbuf (self, last_image->image, last_image->unsaved);
+}
diff --git a/extensions/image_viewer/gth-image-viewer-page.h b/extensions/image_viewer/gth-image-viewer-page.h
index 22a826b..39852ee 100644
--- a/extensions/image_viewer/gth-image-viewer-page.h
+++ b/extensions/image_viewer/gth-image-viewer-page.h
@@ -56,6 +56,7 @@ void               gth_image_viewer_page_set_pixbuf       (GthImageViewerPage *p
 void               gth_image_viewer_page_undo             (GthImageViewerPage *page);
 void               gth_image_viewer_page_redo             (GthImageViewerPage *page);
 GthImageHistory *  gth_image_viewer_page_get_history      (GthImageViewerPage *self);
+void               gth_image_viewer_page_reset            (GthImageViewerPage *self);
 
 G_END_DECLS
 
diff --git a/gthumb/gth-image-history.c b/gthumb/gth-image-history.c
index f29edd9..fb144e0 100644
--- a/gthumb/gth-image-history.c
+++ b/gthumb/gth-image-history.c
@@ -41,8 +41,7 @@ gth_image_data_new (GdkPixbuf *image,
 	idata = g_new0 (GthImageData, 1);
 
 	idata->ref = 1;
-	g_object_ref (image);
-	idata->image = image;
+	idata->image = g_object_ref (image);
 	idata->unsaved = unsaved;
 
 	return idata;
@@ -259,65 +258,42 @@ gth_image_history_add_image (GthImageHistory *history,
 
 
 GthImageData *
-gth_image_history_undo (GthImageHistory *history,
-			GdkPixbuf       *current_image,
-			gboolean         image_is_unsaved)
+gth_image_history_undo (GthImageHistory *history)
 {
 	GthImageData *idata;
 
-	if (history->priv->undo_history == NULL)
+	if ((history->priv->undo_history == NULL) || (history->priv->undo_history->next == NULL))
 		return NULL;
 
-	add_image_to_redo_history (history, current_image, image_is_unsaved);
 	idata = remove_first_image (&(history->priv->undo_history));
+	add_image_to_redo_history (history, idata->image, idata->unsaved);
+	gth_image_data_unref (idata);
 
 	g_signal_emit (G_OBJECT (history),
 		       gth_image_history_signals[CHANGED],
 		       0);
 
-	return idata;
-}
-
-
-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;
+	return (GthImageData *) history->priv->undo_history->data;
 }
 
 
 GthImageData *
-gth_image_history_redo (GthImageHistory *history,
-			GdkPixbuf       *current_image,
-			gboolean         image_is_unsaved)
+gth_image_history_redo (GthImageHistory *history)
 {
 	GthImageData *idata;
 
 	if (history->priv->redo_history == NULL)
 		return NULL;
 
-	add_image_to_undo_history (history, current_image, image_is_unsaved);
 	idata = remove_first_image (&(history->priv->redo_history));
+	add_image_to_undo_history (history, idata->image, idata->unsaved);
+	gth_image_data_unref (idata);
 
 	g_signal_emit (G_OBJECT (history),
 		       gth_image_history_signals[CHANGED],
 		       0);
 
-	return idata;
+	return (GthImageData *) history->priv->undo_history->data;
 }
 
 
@@ -335,7 +311,7 @@ gth_image_history_clear (GthImageHistory *history)
 gboolean
 gth_image_history_can_undo (GthImageHistory *history)
 {
-	return history->priv->undo_history != NULL;
+	return (history->priv->undo_history != NULL) && (history->priv->undo_history->next != NULL);
 }
 
 
@@ -344,3 +320,34 @@ gth_image_history_can_redo (GthImageHistory *history)
 {
 	return history->priv->redo_history != NULL;
 }
+
+
+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_get_last (GthImageHistory *history)
+{
+	if (history->priv->undo_history == NULL)
+		return NULL;
+	else
+		return (GthImageData *) history->priv->undo_history->data;
+}
diff --git a/gthumb/gth-image-history.h b/gthumb/gth-image-history.h
index 9dfacdd..d2cb081 100644
--- a/gthumb/gth-image-history.h
+++ b/gthumb/gth-image-history.h
@@ -69,16 +69,13 @@ GthImageHistory * gth_image_history_new        (void);
 void              gth_image_history_add_image  (GthImageHistory *history,
 						GdkPixbuf       *image,
 						gboolean         unsaved);
-GthImageData *    gth_image_history_undo       (GthImageHistory *history,
-						GdkPixbuf       *current_image,
-						gboolean         image_is_unsaved);
-GthImageData *    gth_image_history_redo       (GthImageHistory *history,
-						GdkPixbuf       *current_image,
-						gboolean         image_is_unsaved);
+GthImageData *    gth_image_history_undo       (GthImageHistory *history);
+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);
+GthImageData *    gth_image_history_get_last   (GthImageHistory *history);
 
 G_END_DECLS
 



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