From b4ebfc59bacaedb6bee926f67cb50f3bb5070cc1 Mon Sep 17 00:00:00 2001
From: Jonathan Wilkes <jon.w.wilkes@gmail.com>
Date: Sun, 18 Dec 2016 12:29:12 -0500
Subject: [PATCH] fix for #201: new subcanvas position isn't saved with patch

---
 pd/nw/pd_canvas.js |  4 ++++
 pd/src/g_canvas.c  | 21 +++++++++++++++------
 pd/src/g_editor.c  |  3 ---
 3 files changed, 19 insertions(+), 9 deletions(-)

diff --git a/pd/nw/pd_canvas.js b/pd/nw/pd_canvas.js
index 7c7aa77e2..265b8a399 100644
--- a/pd/nw/pd_canvas.js
+++ b/pd/nw/pd_canvas.js
@@ -630,6 +630,10 @@ var canvas_events = (function() {
     gui.Window.get().on("blur", function() {
         nw_window_blur_callback(name);
     });
+    gui.Window.get().on("move", function(x, y) {
+        var w = gui.Window.get();
+        pdgui.pdsend(name, "setbounds", x, y, x + w.width, y + w.height);
+    });
     // set minimum window size
     gui.Window.get().setMinimumSize(150, 100);
 
diff --git a/pd/src/g_canvas.c b/pd/src/g_canvas.c
index 1eaade008..83f872af0 100644
--- a/pd/src/g_canvas.c
+++ b/pd/src/g_canvas.c
@@ -51,7 +51,7 @@ t_canvas *canvas_whichfind;         /* last canvas we did a find in */
 static void canvas_start_dsp(void);
 static void canvas_stop_dsp(void);
 static void canvas_drawlines(t_canvas *x);
-void canvas_setbounds(t_canvas *x, int x1, int y1, int x2, int y2);
+void canvas_dosetbounds(t_canvas *x, int x1, int y1, int x2, int y2);
 void canvas_reflecttitle(t_canvas *x);
 static void canvas_addtolist(t_canvas *x);
 static void canvas_takeofflist(t_canvas *x);
@@ -423,7 +423,7 @@ t_canvas *canvas_new(void *dummy, t_symbol *sel, int argc, t_atom *argv)
     x->gl_y1 = 0;
     x->gl_x2 = 1;
     x->gl_y2 = 1;
-    canvas_setbounds(x, xloc, yloc, xloc + width, yloc + height);
+    canvas_dosetbounds(x, xloc, yloc, xloc + width, yloc + height);
     x->gl_owner = owner;
     x->gl_isclone = 0;
     x->gl_name = (*s->s_name ? s : 
@@ -609,9 +609,8 @@ int glist_isgraph(t_glist *x)
     return (x->gl_isgraph|(x->gl_hidetext<<1));
 }
 
-    /* This is sent from the GUI to inform a toplevel that its window has been
-    moved or resized. */
-void canvas_setbounds(t_canvas *x, int x1, int y1, int x2, int y2)
+/* bounds-setting for patch/subpatch windows */
+void canvas_dosetbounds(t_canvas *x, int x1, int y1, int x2, int y2)
 {
     //fprintf(stderr,"canvas_setbounds %d %d %d %d\n", x1, y1, x2, y2);
 
@@ -643,6 +642,14 @@ void canvas_setbounds(t_canvas *x, int x1, int y1, int x2, int y2)
     }
 }
 
+    /* public method to set the bounds for a patch/subpatch window from the
+       GUI. */
+static void canvas_setbounds(t_canvas *x, t_float left, t_float top,
+                             t_float right, t_float bottom)
+{
+    canvas_dosetbounds(x, (int)left, (int)top, (int)right, (int)bottom);
+}
+
 t_symbol *canvas_makebindsym(t_symbol *s)
 {
     char buf[MAXPDSTRING];
@@ -1247,7 +1254,7 @@ static void canvas_relocate(t_canvas *x, t_symbol *canvasgeom,
             /* for some reason this is initially called with cw=ch=1 so
             we just suppress that here. */
     if (cw > 5 && ch > 5)
-        canvas_setbounds(x, txpix, typix,
+        canvas_dosetbounds(x, txpix, typix,
             txpix + cw, typix + ch);
     /* readjust garrays (if any) */
     t_gobj *g;
@@ -2499,6 +2506,8 @@ void g_canvas_setup(void)
         A_DEFFLOAT, A_NULL);
     class_addmethod(canvas_class, (t_method)canvas_loadbang,
         gensym("loadbang"), A_NULL);
+    class_addmethod(canvas_class, (t_method)canvas_setbounds,
+        gensym("setbounds"), A_FLOAT, A_FLOAT, A_FLOAT, A_FLOAT, A_NULL);
     class_addmethod(canvas_class, (t_method)canvas_relocate,
         gensym("relocate"), A_SYMBOL, A_SYMBOL, A_NULL);
     class_addmethod(canvas_class, (t_method)canvas_vis,
diff --git a/pd/src/g_editor.c b/pd/src/g_editor.c
index e117ccfc7..436e113fe 100644
--- a/pd/src/g_editor.c
+++ b/pd/src/g_editor.c
@@ -68,7 +68,6 @@ static int screeny2;
 static int copiedfont;
 static void canvas_dofont(t_canvas *x, t_floatarg font, t_floatarg xresize,
     t_floatarg yresize);
-extern void canvas_setbounds(t_canvas *x, int x1, int y1, int x2, int y2);
 int canvas_apply_restore_original_position(t_canvas *x, int orig_pos);
 extern void canvas_draw_gop_resize_hooks(t_canvas *x);
 static void canvas_font(t_canvas *x, t_floatarg font, t_floatarg oldfont,
@@ -6237,8 +6236,6 @@ static void canvas_dopaste(t_canvas *x, t_binbuf *b)
             x->gl_screenx2 = screenx1 + screenx2;
             x->gl_screeny1 = screeny1;
             x->gl_screeny2 = screeny1 + screeny2;
-            //canvas_setbounds(x, screenx1, screeny1,
-            //    screenx1+screenx2, screeny1+screeny2);
             gui_vmess("gui_canvas_change_geometry", "xiiii",
                 x,
                 (int)(x->gl_screenx2 - x->gl_screenx1),
-- 
GitLab