[Glade-devel] Cut/Paste with Undo/Redo patch.

Archit Baweja bighead@users.sourceforge.net
Fri, 4 Apr 2003 02:51:30 -0500


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Hi

Here's the patch which has been waiting in my source tree for over 6 months.
Works. Couple of inefficiences and I also noticed a segfault not relating to
clipboard from my initial observations but I don't know really. Will look into
it. Anyways heres the patch.

Feast on it, tear it apart, criticize it and more importantly debug it if u
want to (although I will do all those over the weekend). Have fun!

Cheers!
Archit Baweja


===File ~/Projects/gnome2/glade3/glade3-clipboard-and-undo.patch===
? .cvsignore
? autom4te-2.53.cache
? glade-3.desktop
? glade3-clipboard-and-undo.patch
? stamp-h1
? src/glade-3
Index: ChangeLog
===================================================================
RCS file: /cvs/gnome/glade3/ChangeLog,v
retrieving revision 1.103
diff -u -r1.103 ChangeLog
- --- ChangeLog	2 Apr 2003 19:52:08 -0000	1.103
+++ ChangeLog	4 Apr 2003 07:45:23 -0000
@@ -1,3 +1,17 @@
+2003-04-04  Archit Baweja  <bighead@users.sourceforge.net>
+
+	* src/glade-project-window.c (gpw_cut_cb): use glade_command_cut().
+	(gpw_paste_cb): use glade_command_paste().
+
+	* src/glade-placeholder.c (glade_placeholder_paste_cb): likewise.
+
+	* src/glade-widget.c (glade_widget_cut): likewise.
+
+2002-05-26  Archit Baweja  <bighead@users.sourceforge.net>
+
+	* src/glade-command.c (glade_command_cut_paste_*): cut/paste through
+	the undo/redo system.
+
 2003-04-02  Joaquin Cuenca Abela  <e98cuenc@yahoo.com>
 
 	* src/glade-menu-editor.c: Fix the segfault when adding a new menu item.
Index: src/glade-clipboard.c
===================================================================
RCS file: /cvs/gnome/glade3/src/glade-clipboard.c,v
retrieving revision 1.7
diff -u -r1.7 glade-clipboard.c
- --- src/glade-clipboard.c	13 Mar 2003 23:12:03 -0000	1.7
+++ src/glade-clipboard.c	4 Apr 2003 07:45:23 -0000
@@ -4,7 +4,7 @@
  * Copyright (C) 2001 The GNOME Foundation.
  *
  * Author(s):
- - *      Archit Baweja <bighead@crosswinds.net>
+ *      Archit Baweja <bighead@users.sourceforge.net>
  *
  * This program is free software; you can redistribute it and/or
  * modify it under the terms of the GNU General Public License
Index: src/glade-command.c
===================================================================
RCS file: /cvs/gnome/glade3/src/glade-command.c,v
retrieving revision 1.4
diff -u -r1.4 glade-command.c
- --- src/glade-command.c	9 Mar 2003 20:37:46 -0000	1.4
+++ src/glade-command.c	4 Apr 2003 07:45:23 -0000
@@ -18,6 +18,7 @@
  *
  * Authors:
  *   Joaquín Cuenca Abela <e98cuenc@yahoo.com>
+ *   Archit Baweja <bighead@users.sourceforge.net>
  */
 #include <gtk/gtk.h>
 #include <string.h>
@@ -31,6 +32,7 @@
 #include "glade-property.h"
 #include "glade-debug.h"
 #include "glade-placeholder.h"
+#include "glade-clipboard.h"
 #include "glade.h"
 
 #define GLADE_COMMAND_TYPE		(glade_command_get_type ())
@@ -608,4 +610,132 @@
 glade_command_create (GladeWidget *widget)
 {
 	glade_command_create_delete_common (widget, TRUE);
+}
+
+/**
+ * Cut/Paste
+ *
+ * Following is the code to extend the GladeCommand Undo/Redo system to 
+ * Clipboard functions.
+ **/
+typedef struct {
+	GladeCommand parent;
+
+	GladeClipboard *clipboard;
+	GladeWidget *widget;
+	GladePlaceholder *placeholder;
+	gboolean cut;
+} GladeCommandCutPaste;
+
+
+GLADE_MAKE_COMMAND (GladeCommandCutPaste, glade_command_cut_paste);
+#define GLADE_COMMAND_CUT_PASTE_TYPE		(glade_command_cut_paste_get_type ())
+#define GLADE_COMMAND_CUT_PASTE(o)	  	(G_TYPE_CHECK_INSTANCE_CAST ((o), GLADE_COMMAND_CUT_PASTE_TYPE, GladeCommandCutPaste))
+#define GLADE_COMMAND_CUT_PASTE_CLASS(k)	(G_TYPE_CHECK_CLASS_CAST ((k), GLADE_COMMAND_CUT_PASTE_TYPE, GladeCommandCutPasteClass))
+#define IS_GLADE_COMMAND_CUT_PASTE(o)		(G_TYPE_CHECK_INSTANCE_TYPE ((o), GLADE_COMMAND_CUT_PASTE_TYPE))
+#define IS_GLADE_COMMAND_CUT_PASTE_CLASS(k)	(G_TYPE_CHECK_CLASS_TYPE ((k), GLADE_COMMAND_CREATE_DELETE_TYPE))
+
+static gboolean
+glade_command_cut_paste_undo (GladeCommand *cmd)
+{
+	return glade_command_cut_paste_execute (cmd);
+}
+
+static gboolean
+glade_command_paste_execute (GladeCommandCutPaste *me)
+{
+	glade_clipboard_paste (me->clipboard, me->placeholder);
+
+	return TRUE;
+}
+
+static gboolean
+glade_command_cut_execute (GladeCommandCutPaste *me)
+{
+	glade_clipboard_cut (me->clipboard, me->widget);
+
+	return TRUE;
+}
+
+/**
+ * Execute the cmd and revert it.  Ie, after the execution of this
+ * function cmd will point to the undo action
+ */
+static gboolean
+glade_command_cut_paste_execute (GladeCommand *cmd)
+{
+	GladeCommandCutPaste *me = (GladeCommandCutPaste *) cmd;
+	gboolean retval;
+	
+	if (me->cut)
+		retval = glade_command_cut_execute (me);
+	else
+		retval = glade_command_paste_execute (me);
+
+	me->cut = !me->cut;
+	return retval;
+}
+
+static void
+glade_command_cut_paste_finalize (GObject *obj)
+{
+	GladeCommandCutPaste *cmd = GLADE_COMMAND_CUT_PASTE (obj);
+	g_object_unref (cmd->widget);
+        glade_command_finalize (obj);
+}
+
+static gboolean
+glade_command_cut_paste_unifies (GladeCommand *this, GladeCommand *other)
+{
+	return FALSE;
+}
+
+static void
+glade_command_cut_paste_collapse (GladeCommand *this, GladeCommand *other)
+{
+	g_return_if_reached ();
+}
+
+static void
+glade_command_cut_paste_common (GladeWidget *widget,
+				GladePlaceholder *placeholder,
+				gboolean cut)
+{
+	GladeCommandCutPaste *me;
+	GladeCommand *cmd;
+	GladeProject *project;
+	GladeProjectWindow *gpw;
+
+	me = (GladeCommandCutPaste *) g_object_new (GLADE_COMMAND_CUT_PASTE_TYPE, NULL);
+	cmd = (GladeCommand *) me;
+	
+	project = glade_project_window_get_project ();
+	gpw = glade_project_window_get ();
+
+	me->cut = cut;
+	me->widget = widget;
+	me->placeholder = placeholder;
+	me->clipboard = gpw->clipboard;
+	
+	cmd->description = g_strdup_printf (_("%s %s"), cut ? "Cut" : "Paste", widget->name);
+	
+	g_debug(("Pushing: %s\n", cmd->description));
+
+	/*
+	 * Push it onto the undo stack only on success
+	 */
+	if (glade_command_cut_paste_execute (GLADE_COMMAND (me)))
+		glade_command_push_undo (project, GLADE_COMMAND (me));
+}
+
+void
+glade_command_paste (GladeWidget *widget, GladePlaceholder *placeholder)
+{
+	glade_command_cut_paste_common (widget, placeholder, FALSE);
+}
+
+void
+glade_command_cut (GladeWidget *widget)
+{
+	glade_command_cut_paste_common (widget, NULL, TRUE);
 }
Index: src/glade-command.h
===================================================================
RCS file: /cvs/gnome/glade3/src/glade-command.h,v
retrieving revision 1.2
diff -u -r1.2 glade-command.h
- --- src/glade-command.h	11 Apr 2002 08:01:44 -0000	1.2
+++ src/glade-command.h	4 Apr 2003 07:45:23 -0000
@@ -10,7 +10,11 @@
 const gchar* glade_command_get_description (GList *l);
 
 void glade_command_set_property (GObject *obj, const gchar* name, const GValue* value);
+
 void glade_command_delete (GladeWidget *widget);
 void glade_command_create (GladeWidget *widget);
+
+void glade_command_cut   (GladeWidget *widget);
+void glade_command_paste (GladeWidget *widget, GladePlaceholder *placeholder);
 
 #endif /* GLADE_COMMAND_H */
Index: src/glade-placeholder.c
===================================================================
RCS file: /cvs/gnome/glade3/src/glade-placeholder.c,v
retrieving revision 1.25
diff -u -r1.25 glade-placeholder.c
- --- src/glade-placeholder.c	2 May 2002 09:11:09 -0000	1.25
+++ src/glade-placeholder.c	4 Apr 2003 07:45:23 -0000
@@ -794,12 +794,13 @@
 void
 glade_placeholder_paste_cb (GtkWidget *button, gpointer data)
 {
- -	GladePlaceholder *placeholder = GTK_WIDGET (data);
 	GladeProjectWindow *gpw;
- -	GladeClipboard *clipboard;
 
 	gpw = glade_project_window_get ();
- -	clipboard = gpw->clipboard;
 
- -	glade_clipboard_paste (clipboard, placeholder);
+	/*
+	 * The data parameter is the placeholder we have to replace with the
+	 * widget.
+	 */
+	glade_command_paste (gpw->active_widget, GTK_WIDGET (data));
 }
Index: src/glade-project-window.c
===================================================================
RCS file: /cvs/gnome/glade3/src/glade-project-window.c,v
retrieving revision 1.36
diff -u -r1.36 glade-project-window.c
- --- src/glade-project-window.c	2 Apr 2003 19:06:28 -0000	1.36
+++ src/glade-project-window.c	4 Apr 2003 07:45:23 -0000
@@ -193,7 +193,7 @@
 	widget = gpw->active_widget;
 
 	if (widget)
- -		glade_clipboard_cut (gpw->clipboard, widget);
+		glade_command_cut (widget);
 }
 
 static void
@@ -202,7 +202,7 @@
 	GladeProjectWindow *gpw;
 
 	gpw = glade_project_window_get ();
- -	glade_clipboard_paste (gpw->clipboard, gpw->active_placeholder);
+	glade_command_paste (gpw->active_widget, gpw->active_placeholder);
 }
 
 static void
Index: src/glade-widget.c
===================================================================
RCS file: /cvs/gnome/glade3/src/glade-widget.c,v
retrieving revision 1.35
diff -u -r1.35 glade-widget.c
- --- src/glade-widget.c	2 Apr 2003 19:52:10 -0000	1.35
+++ src/glade-widget.c	4 Apr 2003 07:45:24 -0000
@@ -1114,8 +1114,7 @@
 
 	gpw = glade_project_window_get ();
 	clipboard = gpw->clipboard;
- -
- -	glade_clipboard_cut (clipboard, widget);
+	glade_command_cut (widget);
 }
 
 void
============================================================
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.0.7 (GNU/Linux)
Comment: Processed by Mailcrypt 3.5.6 and Gnu Privacy Guard <http://www.gnupg.org/>

iD8DBQE+jTmC2rWNPKmGjMcRAgjmAKCcGfehSNjMZuawzuHRob05oANA/ACfdf/w
IO/Cv3pPzgY7b2hPs9c9RFQ=
=agWE
-----END PGP SIGNATURE-----