From 98bc8bd8dddde8ef78362097afa5b67f5ab42ccb Mon Sep 17 00:00:00 2001
From: Ivica Ico Bukvic <ico@vt.edu>
Date: Sat, 26 Mar 2011 15:58:00 -0400
Subject: [PATCH] Pd-0.42.5-extended-l2ork-dev-20110326.tar.bz2

---
 src/g_canvas.c | 15 +++++++++++++++
 src/g_canvas.h |  1 +
 src/g_editor.c | 37 +++++++++++++++++++------------------
 src/m_class.c  |  3 +++
 src/m_pd.h     |  2 +-
 src/pd.tk      |  5 +++--
 src/s_inter.c  |  2 +-
 7 files changed, 43 insertions(+), 22 deletions(-)

diff --git a/src/g_canvas.c b/src/g_canvas.c
index 797d1a0e4..66591eb26 100644
--- a/src/g_canvas.c
+++ b/src/g_canvas.c
@@ -351,6 +351,11 @@ void linetraverser_skipobject(t_linetraverser *t)
 /* -------------------- the canvas object -------------------------- */
 int glist_valid = 10000;
 
+//static void canvas_manual_pd_free(t_canvas *x) {
+//	sys_flushtogui();
+//	pd_free(&x->gl_pd);
+//}
+
 void glist_init(t_glist *x)
 {
         /* zero out everyone except "pd" field */
@@ -377,6 +382,12 @@ t_canvas *canvas_new(void *dummy, t_symbol *sel, int argc, t_atom *argv)
     // jsarlo
     x->gl_magic_glass = magicGlass_new((int)x);
     // end jsarlo
+
+	//if we are root canvas set the clock for script based destructor of the window
+	//if (!owner) {
+	//	x->gl_destroy = clock_new(x, (t_method)canvas_manual_pd_free);
+	//}
+
     x->gl_obj.te_type = T_OBJECT;
     if (!owner)
         canvas_addtolist(x);
@@ -808,6 +819,10 @@ void canvas_free(t_canvas *x)
     if (x->gl_magic_glass)
       magicGlass_free(x->gl_magic_glass);
     // end jsarlo
+
+	//delete clock for gl_destroy
+	//if (x->gl_destroy) clock_free(x->gl_destroy);
+
     canvas_noundo(x);
     if (canvas_editing == x)
         canvas_editing = 0;
diff --git a/src/g_canvas.h b/src/g_canvas.h
index 2501ebbd6..be574f249 100644
--- a/src/g_canvas.h
+++ b/src/g_canvas.h
@@ -193,6 +193,7 @@ struct _glist
     // jsarlo
     t_magicGlass *gl_magic_glass;   /* magic glass object */
     // end jsarlo
+	//t_clock  *gl_destroy;			/* for script-based closing of the patch */
 };
 
 #define gl_gobj gl_obj.te_g
diff --git a/src/g_editor.c b/src/g_editor.c
index 40393b8d3..e73bfbe97 100644
--- a/src/g_editor.c
+++ b/src/g_editor.c
@@ -2735,17 +2735,13 @@ void glob_verifyquit(void *dummy, t_floatarg f)
     else glob_quit(0);
 }
 
+void canvas_dofree(t_gobj *dummy, t_glist *x)
+{
+	pd_free(&x->gl_pd);
+}
+
     /* close a window (or possibly quit Pd), checking for dirty flags.
     The "force" parameter is interpreted as follows:
-
-		FOR INTERNAL USE (using it explicitly via pd messages induces
-		a crash because pd_free is called before the previous function
-		has run its course and thus you end up with hard-to-trace crash)
-	   -1 - request from GUI to close, no verification
-			(after it returns back from tcl/tk side of things to avoid
-			freeing before the last method has run its course)
-
-		OFFICIAL USE
         0 - request from GUI to close, verifying whether clean or dirty
         1 - request from GUI to close, no verification
         2 - verified - mark this one clean, then continue as in 1
@@ -2755,9 +2751,7 @@ void canvas_menuclose(t_canvas *x, t_floatarg fforce)
 {
     int force = fforce;
     t_glist *g;
-	if (force == -1)
-		pd_free(&x->gl_pd);
-    else if (x->gl_owner && (force == 0 || force == 1))
+	if (x->gl_owner && (force == 0 || force == 1))
         canvas_vis(x, 0);   /* if subpatch, just invis it */
     else if (force == 0)    
     {
@@ -2790,12 +2784,17 @@ void canvas_menuclose(t_canvas *x, t_floatarg fforce)
                 canvas_getrootfor(x), canvas_getrootfor(x)->gl_name->s_name, x);
         }
 */
-        else //pd_free(&x->gl_pd);
-			sys_vgui("pd {.x%lx menuclose -1;}\n", x);
+        else pd_free(&x->gl_pd);
+			//sys_queuegui(x, x, canvas_dofree);
+			//clock_delay(x->gl_destroy, 0);
     }
-    else if (force == 1)
+    else if (force == 1) {
         //pd_free(&x->gl_pd);
-		sys_vgui("pd {.x%lx menuclose -1;}\n", x);
+		//sys_vgui("pd {.x%lx menuclose -1;}\n", x);
+		//sys_vgui("menu_close .x%lx\n", x);
+		sys_queuegui(x, x, canvas_dofree);
+		//clock_delay(x->gl_destroy, 0);
+	}
     else if (force == 2)
     {
         canvas_dirty(x, 0);
@@ -2817,8 +2816,10 @@ void canvas_menuclose(t_canvas *x, t_floatarg fforce)
             //         canvas_getrootfor(x), g);
             return;
         }
-        else //pd_free(&x->gl_pd);
-			sys_vgui("pd {.x%lx menuclose -1;}\n", x);
+        else pd_free(&x->gl_pd);
+			//sys_vgui("pd {.x%lx menuclose -1;}\n", x);
+			//sys_queuegui(x, x, canvas_dofree);
+			//clock_delay(x->gl_destroy, 0);
     }
     else if (force == 3)
     {
diff --git a/src/m_class.c b/src/m_class.c
index 3278f85da..abb9561cc 100644
--- a/src/m_class.c
+++ b/src/m_class.c
@@ -669,6 +669,8 @@ void pd_typedmess(t_pd *x, t_symbol *s, int argc, t_atom *argv)
     int narg = 0;
     t_pd *bonzo;
     
+	//fprinf(stderr,"\nstart %s %d\n", s->s_name, c->c_nmethod);
+
         /* check for messages that are handled by fixed slots in the class
         structure.  We don't catch "pointer" though so that sending "pointer"
         to pd_objectmaker doesn't require that we supply a pointer value. */
@@ -709,6 +711,7 @@ void pd_typedmess(t_pd *x, t_symbol *s, int argc, t_atom *argv)
         if (m->me_name == s)
 		//if (m && m->me_name == s)
     {
+		//fprintf(stderr,"me_name %s\n", m->me_name);
         wp = m->me_arg;
         if (*wp == A_GIMME)
         {
diff --git a/src/m_pd.h b/src/m_pd.h
index a1cd59fd1..b61e370a2 100644
--- a/src/m_pd.h
+++ b/src/m_pd.h
@@ -11,7 +11,7 @@ extern "C" {
 #define PD_MAJOR_VERSION 0
 #define PD_MINOR_VERSION 42
 #define PD_BUGFIX_VERSION 5
-#define PD_TEST_VERSION "extended-l2ork-20110324"
+#define PD_TEST_VERSION "extended-l2ork-20110326"
 
 /* old name for "MSW" flag -- we have to take it for the sake of many old
 "nmakefiles" for externs, which will define NT and not MSW */
diff --git a/src/pd.tk b/src/pd.tk
index a1b43e55c..066472b38 100644
--- a/src/pd.tk
+++ b/src/pd.tk
@@ -851,7 +851,7 @@ proc pdtk_check {canvas x message default} {
     if {! [string compare $answer yes]}  {
 		pd $message
 		if {$canvas eq "."} {
-			focus $canvassegfault due to the way iemgui's implementation of universal color
+			focus $canvas
 		} else {
 			menu_close $canvas
 		}
@@ -1545,6 +1545,7 @@ proc menu_print {name} {
 }
 
 proc menu_close {name} {
+	#puts stderr menu_close
 	if {$name == ".texteditor.text"} { 
 		set topname [string trimright $name .text]
 		texteditor_send $name
@@ -1989,7 +1990,7 @@ proc pdtk_canvas_new {name width height geometry editable} {
         -xscrollcommand "$name.scrollhort set" \
         -scrollregion [concat 0 0 $width $height]
 
-	pdtk_standardkeybindings $name.c
+	#pdtk_standardkeybindings $name.c
 
 	if {[info tclversion] >= 8.5 && $pd_nt == 0} {
 		if {$::menu($name) == 1} {
diff --git a/src/s_inter.c b/src/s_inter.c
index 6d0191a47..47eb6a530 100644
--- a/src/s_inter.c
+++ b/src/s_inter.c
@@ -716,7 +716,7 @@ void sys_gui(char *s)
     sys_vgui("%s", s);
 }
 
-static int sys_flushtogui( void)
+int sys_flushtogui( void)
 {
     int writesize = sys_guibufhead - sys_guibuftail, nwrote = 0;
     if (writesize > 0)
-- 
GitLab