From a5cb457f5cb0dc9a3c80d1aa5402bb66c6b1af80 Mon Sep 17 00:00:00 2001
From: Jonathan Wilkes <jon.w.wilkes@gmail.com>
Date: Fri, 4 Nov 2016 23:11:43 -0400
Subject: [PATCH] port unauthorized/grid to the new GUI

---
 externals/unauthorized/grid/grid.c | 798 +++++++++++++++--------------
 pd/nw/pdgui.js                     | 108 ++++
 2 files changed, 535 insertions(+), 371 deletions(-)

diff --git a/externals/unauthorized/grid/grid.c b/externals/unauthorized/grid/grid.c
index d32fcf706..57179b62f 100644
--- a/externals/unauthorized/grid/grid.c
+++ b/externals/unauthorized/grid/grid.c
@@ -6,7 +6,6 @@
 /* grid control object : two dimensionnal grid                                 */
 /* thanks to Thomas Musil, Miller Puckette, Guenther Geiger and Krzystof Czaja */
 
-
 #include <stdlib.h>
 #include <string.h>
 #include <stdio.h>
@@ -63,30 +62,38 @@ static char   *grid_version = "grid: version 0.8, written by Yves Degoyon (ydego
 /* drawing functions */
 static void grid_draw_update(t_grid *x, t_glist *glist)
 {
- t_canvas *canvas=glist_getcanvas(glist);
- t_int xpoint=x->x_current, ypoint=x->y_current;
+    t_canvas *canvas=glist_getcanvas(glist);
+    t_int xpoint=x->x_current, ypoint=x->y_current;
 
-    // later : try to figure out what's this test for ??  
-    // if (glist_isvisible(glist))
-    // {
-       // delete previous point if existing
-       if (x->x_point)  
-       {
-          GRID_SYS_VGUI3(".x%lx.c delete %lxPOINT\n", canvas, x);
-       }
-        
-       if ( x->x_current < text_xpix(&x->x_obj, glist) ) xpoint = text_xpix(&x->x_obj, glist);
-       if ( x->x_current > text_xpix(&x->x_obj, glist) + x->x_width - pointsize ) 
-			xpoint = text_xpix(&x->x_obj, glist) + x->x_width - pointsize;
-       if ( x->y_current < text_ypix(&x->x_obj, glist) ) ypoint = text_ypix(&x->x_obj, glist);
-       if ( x->y_current > text_ypix(&x->x_obj, glist) + x->x_height - pointsize ) 
-			ypoint = text_ypix(&x->x_obj, glist) + x->x_height - pointsize;
-       // draw the selected point
-       GRID_SYS_VGUI7(".x%lx.c create rectangle %d %d %d %d -fill #FF0000 -tags %lxPOINT\n",
-	     canvas, xpoint, ypoint, xpoint+pointsize, ypoint+pointsize, x);
-       x->x_point = 1;
-    // }  
-    // else 
+    if (glist_isvisible(glist))
+    {
+        // delete previous point if existing
+        //if (x->x_point)
+        //{
+        //   GRID_SYS_VGUI3(".x%lx.c delete %lxPOINT\n", canvas, x);
+        //}
+
+        if (x->x_current < text_xpix(&x->x_obj, glist))
+            xpoint = text_xpix(&x->x_obj, glist);
+        if (x->x_current > text_xpix(&x->x_obj, glist) + x->x_width - pointsize)
+            xpoint = text_xpix(&x->x_obj, glist) + x->x_width - pointsize;
+        if (x->y_current < text_ypix(&x->x_obj, glist))
+            ypoint = text_ypix(&x->x_obj, glist);
+        if (x->y_current > text_ypix(&x->x_obj, glist) + x->x_height - pointsize)
+            ypoint = text_ypix(&x->x_obj, glist) + x->x_height - pointsize;
+        // draw the selected point
+        //GRID_SYS_VGUI7(".x%lx.c create rectangle %d %d %d %d "
+        //               "-fill #FF0000 -tags %lxPOINT\n",
+        //    canvas, xpoint, ypoint, xpoint+pointsize, ypoint+pointsize, x);
+        /* move the point */
+        gui_vmess("gui_grid_point", "xxii",
+            canvas,
+            x,
+            xpoint - text_xpix(&x->x_obj, glist),
+            ypoint - text_ypix(&x->x_obj, glist));
+        x->x_point = 1;
+    }
+    // else
     // {
     //    post( "grid : position updated in an invisible grid" );
     // }
@@ -94,142 +101,178 @@ static void grid_draw_update(t_grid *x, t_glist *glist)
 
 static void grid_draw_new(t_grid *x, t_glist *glist)
 {
-  t_canvas *canvas=glist_getcanvas(glist);
-
-    GRID_SYS_VGUI8(".x%lx.c create rectangle %d %d %d %d -fill %s -tags %lxGRID\n",
-	     canvas, text_xpix(&x->x_obj, glist), text_ypix(&x->x_obj, glist),
-	     text_xpix(&x->x_obj, glist) + x->x_width, text_ypix(&x->x_obj, glist) + x->x_height,
-	     x->x_bgcolor, x);
-    GRID_SYS_VGUI7(".x%lx.c create rectangle %d %d %d %d -tags %lxo0\n",
-	     canvas, text_xpix(&x->x_obj, glist), text_ypix(&x->x_obj, glist) + x->x_height+1,
-	     text_xpix(&x->x_obj, glist)+7, text_ypix(&x->x_obj, glist) + x->x_height+2,
-	     x);
-    GRID_SYS_VGUI7(".x%lx.c create rectangle %d %d %d %d -tags %lxo1\n",
-	     canvas, text_xpix(&x->x_obj, glist)+x->x_width-7, text_ypix(&x->x_obj, glist) + x->x_height+1,
-	     text_xpix(&x->x_obj, glist)+x->x_width, text_ypix(&x->x_obj, glist) + x->x_height+2,
-	     x);
-
-    if ( x->x_grid ) 
-    {
-       int xlpos = text_xpix(&x->x_obj, glist)+x->x_width/x->x_xlines;
-       int ylpos = text_ypix(&x->x_obj, glist)+x->x_height/x->x_ylines;
-       int xcount = 1;
-       int ycount = 1;
-       while ( xlpos < text_xpix(&x->x_obj, glist)+x->x_width )
-       {
-         GRID_SYS_VGUI9(".x%lx.c create line %d %d %d %d -fill #FFFFFF -tags %lxLINE%d%d\n",
-	     canvas, xlpos, text_ypix(&x->x_obj, glist),
-	     xlpos, text_ypix(&x->x_obj, glist)+x->x_height,
-	     x, xcount, 0 );
-         xlpos+=x->x_width/x->x_xlines;
-         xcount++;
-       }
-       while ( ylpos < text_ypix(&x->x_obj, glist)+x->x_height )
-       {
-         GRID_SYS_VGUI9(".x%lx.c create line %d %d %d %d -fill #FFFFFF -tags %lxLINE%d%d\n",
-	     canvas, text_xpix(&x->x_obj, glist), ylpos,
-	     text_xpix(&x->x_obj, glist)+x->x_width, ylpos,
-	     x, 0, ycount);
-         ylpos+=x->x_height/x->x_ylines;
-         ycount++;
-       }
-    }
+    t_canvas *canvas=glist_getcanvas(glist);
+    //GRID_SYS_VGUI8(".x%lx.c create rectangle %d %d %d %d "
+    //               "-fill %s -tags %lxGRID\n",
+    //    canvas, text_xpix(&x->x_obj, glist), text_ypix(&x->x_obj, glist),
+    //    text_xpix(&x->x_obj, glist) + x->x_width,
+    //    text_ypix(&x->x_obj, glist) + x->x_height,
+    //    x->x_bgcolor, x);
+    //GRID_SYS_VGUI7(".x%lx.c create rectangle %d %d %d %d -tags %lxo0\n",
+    //    canvas, text_xpix(&x->x_obj, glist),
+    //    text_ypix(&x->x_obj, glist) + x->x_height+1,
+    //    text_xpix(&x->x_obj, glist)+7,
+    //    text_ypix(&x->x_obj, glist) + x->x_height+2, x);
+    //GRID_SYS_VGUI7(".x%lx.c create rectangle %d %d %d %d -tags %lxo1\n",
+    //    canvas, text_xpix(&x->x_obj, glist) + x->x_width - 7,
+    //    text_ypix(&x->x_obj, glist) + x->x_height + 1,
+    //    text_xpix(&x->x_obj, glist) + x->x_width,
+    //    text_ypix(&x->x_obj, glist) + x->x_height + 2,
+    //    x);
+    //if (x->x_grid)
+    //{
+    //    int xlpos = text_xpix(&x->x_obj, glist)+x->x_width/x->x_xlines;
+    //    int ylpos = text_ypix(&x->x_obj, glist)+x->x_height/x->x_ylines;
+    //    int xcount = 1;
+    //    int ycount = 1;
+    //    while ( xlpos < text_xpix(&x->x_obj, glist)+x->x_width )
+    //    {
+    //        GRID_SYS_VGUI9(".x%lx.c create line %d %d %d %d "
+    //                       "-fill #FFFFFF -tags %lxLINE%d%d\n",
+    //            canvas, xlpos, text_ypix(&x->x_obj, glist),
+    //            xlpos, text_ypix(&x->x_obj, glist) + x->x_height,
+    //            x, xcount, 0);
+    //        xlpos += x->x_width / x->x_xlines;
+    //        xcount++;
+    //    }
+    //    while (ylpos < text_ypix(&x->x_obj, glist)+x->x_height)
+    //    {
+    //        GRID_SYS_VGUI9(".x%lx.c create line %d %d %d %d "
+    //                       "-fill #FFFFFF -tags %lxLINE%d%d\n",
+    //            canvas, text_xpix(&x->x_obj, glist), ylpos,
+    //            text_xpix(&x->x_obj, glist)+x->x_width, ylpos,
+    //            x, 0, ycount);
+    //        ylpos += x->x_height / x->x_ylines;
+    //        ycount++;
+    //    }
+    //}
+
+    // gui_gobj_new, "xx type text_xpix text_ypix istoplevel"
+    gui_vmess("gui_gobj_new", "xxsiii",
+        canvas,
+        x,
+        "obj",
+        text_xpix(&x->x_obj, glist),
+        text_ypix(&x->x_obj, glist),
+        glist_istoplevel(glist));
+    gui_vmess("gui_grid_draw_bg", "xxiisiii",
+        canvas,
+        x,
+        x->x_width,
+        x->x_height,
+        x->x_bgcolor,
+        x->x_grid,
+        x->x_xlines,
+        x->x_ylines);
     canvas_fixlinesfor( canvas, (t_text*)x );
 }
 
 static void grid_draw_move(t_grid *x, t_glist *glist)
 {
-  t_canvas *canvas=glist_getcanvas(glist);
-
-    GRID_SYS_VGUI7(".x%lx.c coords %lxGRID %d %d %d %d\n",
-	     canvas, x,
-	     text_xpix(&x->x_obj, glist), text_ypix(&x->x_obj, glist),
-	     text_xpix(&x->x_obj, glist)+x->x_width, text_ypix(&x->x_obj, glist)+x->x_height);
-    GRID_SYS_VGUI7(".x%lx.c coords %lxo0 %d %d %d %d\n",
-	     canvas, x,
-	     text_xpix(&x->x_obj, glist), text_ypix(&x->x_obj, glist) + x->x_height+1,
-	     text_xpix(&x->x_obj, glist)+7, text_ypix(&x->x_obj, glist) + x->x_height+2 );
-    GRID_SYS_VGUI7(".x%lx.c coords %lxo1 %d %d %d %d\n",
-	     canvas, x,
-	     text_xpix(&x->x_obj, glist)+x->x_width-7, text_ypix(&x->x_obj, glist) + x->x_height+1,
-	     text_xpix(&x->x_obj, glist)+x->x_width, text_ypix(&x->x_obj, glist) + x->x_height+2 );
-    if ( x->x_point ) 
-    {
-       grid_draw_update(x, glist);
-    }
-    if ( x->x_grid ) 
+    t_canvas *canvas=glist_getcanvas(glist);
+
+    //GRID_SYS_VGUI7(".x%lx.c coords %lxGRID %d %d %d %d\n",
+    //    canvas, x,
+    //    text_xpix(&x->x_obj, glist), text_ypix(&x->x_obj, glist),
+    //    text_xpix(&x->x_obj, glist)+x->x_width,
+    //    text_ypix(&x->x_obj, glist)+x->x_height);
+    //GRID_SYS_VGUI7(".x%lx.c coords %lxo0 %d %d %d %d\n",
+    //    canvas, x,
+    //    text_xpix(&x->x_obj, glist),
+    //    text_ypix(&x->x_obj, glist) + x->x_height + 1,
+    //    text_xpix(&x->x_obj, glist) + 7,
+    //    text_ypix(&x->x_obj, glist) + x->x_height + 2);
+    //GRID_SYS_VGUI7(".x%lx.c coords %lxo1 %d %d %d %d\n",
+    //    canvas, x,
+    //    text_xpix(&x->x_obj, glist) + x->x_width - 7,
+    //    text_ypix(&x->x_obj, glist) + x->x_height + 1,
+    //    text_xpix(&x->x_obj, glist) + x->x_width,
+    //    text_ypix(&x->x_obj, glist) + x->x_height + 2);
+    if (x->x_point)
     {
-       int xlpos = text_xpix(&x->x_obj, glist)+x->x_width/x->x_xlines;
-       int ylpos = text_ypix(&x->x_obj, glist)+x->x_height/x->x_ylines;
-       int xcount = 1;
-       int ycount = 1;
-       while ( xlpos < text_xpix(&x->x_obj, glist)+x->x_width )
-       {
-         GRID_SYS_VGUI9(".x%lx.c coords %lxLINE%d%d %d %d %d %d\n",
-	     canvas, x, xcount, 0, xlpos, text_ypix(&x->x_obj, glist),
-	     xlpos, text_ypix(&x->x_obj, glist) + x->x_height);
-         xlpos+=x->x_width/x->x_xlines;
-         xcount++;
-       }
-       while ( ylpos < text_ypix(&x->x_obj, glist)+x->x_height )
-       {
-         GRID_SYS_VGUI9(".x%lx.c coords %lxLINE%d%d %d %d %d %d\n",
-	     canvas, x, 0, ycount, text_xpix(&x->x_obj, glist), ylpos,
-	     text_xpix(&x->x_obj, glist) + x->x_width, ylpos);
-         ylpos+=x->x_height/x->x_ylines;
-         ycount++;
-       }
+        grid_draw_update(x, glist);
     }
+    //if (x->x_grid)
+    //{
+    //    int xlpos = text_xpix(&x->x_obj, glist)+x->x_width/x->x_xlines;
+    //    int ylpos = text_ypix(&x->x_obj, glist)+x->x_height/x->x_ylines;
+    //    int xcount = 1;
+    //    int ycount = 1;
+    //    while (xlpos < text_xpix(&x->x_obj, glist) + x->x_width)
+    //    {
+    //        GRID_SYS_VGUI9(".x%lx.c coords %lxLINE%d%d %d %d %d %d\n",
+    //            canvas, x, xcount, 0, xlpos, text_ypix(&x->x_obj, glist),
+    //            xlpos, text_ypix(&x->x_obj, glist) + x->x_height);
+    //            xlpos+=x->x_width/x->x_xlines;
+    //        xcount++;
+    //    }
+    //    while (ylpos < text_ypix(&x->x_obj, glist) + x->x_height)
+    //    {
+    //        GRID_SYS_VGUI9(".x%lx.c coords %lxLINE%d%d %d %d %d %d\n",
+    //            canvas, x, 0, ycount, text_xpix(&x->x_obj, glist), ylpos,
+    //            text_xpix(&x->x_obj, glist) + x->x_width, ylpos);
+    //            ylpos+=x->x_height/x->x_ylines;
+    //        ycount++;
+    //    }
+    //}
     canvas_fixlinesfor( canvas, (t_text*)x );
 }
 
-static void grid_draw_erase(t_grid* x,t_glist* glist)
+static void grid_draw_erase(t_grid* x, t_glist* glist)
 {
-  t_canvas *canvas=glist_getcanvas(glist);
-  int i;
-
-    GRID_SYS_VGUI3(".x%lx.c delete %lxGRID\n", canvas, x);
-    GRID_SYS_VGUI3(".x%lx.c delete %lxo0\n", canvas, x);
-    GRID_SYS_VGUI3(".x%lx.c delete %lxo1\n", canvas, x);
-    if (x->x_grid)  
-    {
-       for (i=1; i<x->x_xlines; i++ )
-       {
-           GRID_SYS_VGUI4(".x%lx.c delete %lxLINE%d0\n", canvas, x, i);
-       }
-       for (i=1; i<x->x_ylines; i++ )
-       {
-           GRID_SYS_VGUI4(".x%lx.c delete %lxLINE0%d\n", canvas, x, i);
-       }
-    }
-    if (x->x_point)  
-    {
-          GRID_SYS_VGUI3(".x%lx.c delete %lxPOINT\n", canvas, x);
-          x->x_point = 0;
-    }
+    t_canvas *canvas = glist_getcanvas(glist);
+
+    gui_vmess("gui_gobj_erase", "xx",
+        canvas, x);
+    //int i;
+
+    //GRID_SYS_VGUI3(".x%lx.c delete %lxGRID\n", canvas, x);
+    //GRID_SYS_VGUI3(".x%lx.c delete %lxo0\n", canvas, x);
+    //GRID_SYS_VGUI3(".x%lx.c delete %lxo1\n", canvas, x);
+    //if (x->x_grid)
+    //{
+    //    for (i=1; i<x->x_xlines; i++)
+    //    {
+    //        GRID_SYS_VGUI4(".x%lx.c delete %lxLINE%d0\n", canvas, x, i);
+    //    }
+    //    for (i=1; i<x->x_ylines; i++)
+    //    {
+    //        GRID_SYS_VGUI4(".x%lx.c delete %lxLINE0%d\n", canvas, x, i);
+    //    }
+    //}
+    //if (x->x_point)
+    //{
+    //    GRID_SYS_VGUI3(".x%lx.c delete %lxPOINT\n", canvas, x);
+    //    x->x_point = 0;
+    //}
 }
 
 static void grid_draw_select(t_grid* x,t_glist* glist)
 {
-  t_canvas *canvas=glist_getcanvas(glist);
-
-    if(x->x_selected)
+    t_canvas *canvas = glist_getcanvas(glist);
+    if (x->x_selected)
     {
-	pd_bind(&x->x_obj.ob_pd, x->x_name);
+        pd_bind(&x->x_obj.ob_pd, x->x_name);
         /* sets the item in blue */
-	GRID_SYS_VGUI3(".x%lx.c itemconfigure %lxGRID -outline #0000FF\n", canvas, x);
+        //GRID_SYS_VGUI3(".x%lx.c itemconfigure %lxGRID "
+        //               "-outline #0000FF\n",
+        //    canvas, x);
+        gui_vmess("gui_gobj_select", "xx", canvas, x);
     }
     else
     {
-	pd_unbind(&x->x_obj.ob_pd, x->x_name);
-	GRID_SYS_VGUI3(".x%lx.c itemconfigure %lxGRID -outline #000000\n", canvas, x);
+        pd_unbind(&x->x_obj.ob_pd, x->x_name);
+        //GRID_SYS_VGUI3(".x%lx.c itemconfigure %lxGRID "
+        //               "-outline #000000\n",
+        //    canvas, x);
+        gui_vmess("gui_gobj_deselect", "xx", canvas, x);
     }
 }
 
 static void grid_output_current(t_grid* x)
 {
-  t_float xvalue, yvalue;
-  t_float xmodstep, ymodstep;
+    t_float xvalue, yvalue;
+    t_float xmodstep, ymodstep;
 
     xvalue = x->x_min + (x->x_current - text_xpix(&x->x_obj, x->x_glist)) * (x->x_max-x->x_min) / x->x_width ;
     if (xvalue < x->x_min ) xvalue = x->x_min;
@@ -243,14 +286,14 @@ static void grid_output_current(t_grid* x)
     if (yvalue > x->y_max ) yvalue = x->y_max;
     ymodstep = ((float)((int)(yvalue*10000) % (int)(x->x_ystep*10000))/10000.);
     yvalue = yvalue - ymodstep;
-    outlet_float( x->x_youtlet, yvalue );
+    outlet_float(x->x_youtlet, yvalue);
 }
 
 /* ------------------------ grid widgetbehaviour----------------------------- */
 
 
 static void grid_getrect(t_gobj *z, t_glist *owner,
-			    int *xp1, int *yp1, int *xp2, int *yp2)
+    int *xp1, int *yp1, int *xp2, int *yp2)
 {
    t_grid* x = (t_grid*)z;
 
@@ -262,91 +305,93 @@ static void grid_getrect(t_gobj *z, t_glist *owner,
 
 static void grid_save(t_gobj *z, t_binbuf *b)
 {
-   t_grid *x = (t_grid *)z;
+    t_grid *x = (t_grid *)z;
 
-   // post( "saving grid : %s", x->x_name->s_name );
-   binbuf_addv(b, "ssiissiffiffiffiiff", gensym("#X"),gensym("obj"),
-		(t_int)x->x_obj.te_xpix, (t_int)x->x_obj.te_ypix,
-		atom_getsymbol(binbuf_getvec(x->x_obj.te_binbuf)),
+    // post( "saving grid : %s", x->x_name->s_name );
+    binbuf_addv(b, "ssiissiffiffiffiiff", gensym("#X"),gensym("obj"),
+    (t_int)x->x_obj.te_xpix, (t_int)x->x_obj.te_ypix,
+        atom_getsymbol(binbuf_getvec(x->x_obj.te_binbuf)),
         x->x_name, x->x_width, x->x_min,
-		x->x_max, x->x_height,
-                x->y_min, x->y_max,
-                x->x_grid, x->x_xstep, 
-                x->x_ystep, x->x_xlines, x->x_ylines, 
-                x->x_current, x->y_current );
-   binbuf_addv(b, ";");
+        x->x_max, x->x_height,
+        x->y_min, x->y_max,
+        x->x_grid, x->x_xstep, 
+        x->x_ystep, x->x_xlines, x->x_ylines, 
+        x->x_current, x->y_current );
+    binbuf_addv(b, ";");
 }
 
 static void grid_properties(t_gobj *z, t_glist *owner)
 {
-   char buf[800];
-   t_grid *x=(t_grid *)z;
-
-   sprintf(buf, "pdtk_grid_dialog %%s %s %d %.2f %.2f %d %.2f %.2f %d %.2f %.2f %d %d\n",
-            x->x_name->s_name, x->x_width, x->x_min, x->x_max, x->x_height, 
-            x->y_min, x->y_max, x->x_grid, x->x_xstep, x->x_ystep,
-            x->x_xlines, x->x_ylines );
-   // post("grid_properties : %s", buf );
-   gfxstub_new(&x->x_obj.ob_pd, x, buf);
+    char buf[800];
+    t_grid *x=(t_grid *)z;
+
+    sprintf(buf, "pdtk_grid_dialog %%s %s %d %.2f %.2f %d %.2f %.2f %d %.2f %.2f %d %d\n",
+        x->x_name->s_name, x->x_width, x->x_min, x->x_max, x->x_height, 
+        x->y_min, x->y_max, x->x_grid, x->x_xstep, x->x_ystep,
+        x->x_xlines, x->x_ylines );
+        // post("grid_properties : %s", buf );
+    gfxstub_new(&x->x_obj.ob_pd, x, buf);
 }
 
 static void grid_select(t_gobj *z, t_glist *glist, int selected)
 {
-   t_grid *x = (t_grid *)z;
+    t_grid *x = (t_grid *)z;
 
-   x->x_selected = selected;
-   grid_draw_select( x, glist );
+    x->x_selected = selected;
+    grid_draw_select(x, glist);
 }
 
 static void grid_vis(t_gobj *z, t_glist *glist, int vis)
 {
-   t_grid *x = (t_grid *)z;
-
-   if (vis)
-   {
-      grid_draw_new( x, glist );
-      grid_draw_update( x, glist );
-      grid_output_current(x);
-   }
-   else
-   {
-      grid_draw_erase( x, glist );
-   }
+    t_grid *x = (t_grid *)z;
+
+    if (vis)
+    {
+        grid_draw_new( x, glist );
+        grid_draw_update( x, glist );
+        grid_output_current(x);
+    }
+    else
+    {
+        grid_draw_erase( x, glist );
+    }
 }
 
 static void grid_dialog(t_grid *x, t_symbol *s, int argc, t_atom *argv)
 {
-   if ( !x ) {
-     post( "grid : error :tried to set properties on an unexisting object" );
-   }
-   if ( argc != 12 )
-   {
-      post( "grid : error in the number of arguments ( %d instead of 12 )", argc );
-      return;
-   }
-      if ( argv[0].a_type != A_SYMBOL || argv[1].a_type != A_FLOAT ||
-        argv[2].a_type != A_FLOAT || argv[3].a_type != A_FLOAT ||
-        argv[4].a_type != A_FLOAT || argv[5].a_type != A_FLOAT ||
-        argv[6].a_type != A_FLOAT || argv[7].a_type != A_FLOAT || 
-        argv[8].a_type != A_FLOAT || argv[9].a_type != A_FLOAT || 
-        argv[10].a_type != A_FLOAT || argv[11].a_type != A_FLOAT ) {
-      post( "grid : wrong arguments" );
-      return;
-   }
-   x->x_name = argv[0].a_w.w_symbol;
-   x->x_width = (int)argv[1].a_w.w_float;
-   x->x_min = argv[2].a_w.w_float;
-   x->x_max = argv[3].a_w.w_float;
-   x->x_height = (int)argv[4].a_w.w_float;
-   x->y_min = argv[5].a_w.w_float;
-   x->y_max = argv[6].a_w.w_float;
-   x->x_grid = argv[7].a_w.w_float;
-   x->x_xstep = argv[8].a_w.w_float;
-   x->x_ystep = argv[9].a_w.w_float;
-   x->x_xlines = argv[10].a_w.w_float;
-   x->x_ylines = argv[11].a_w.w_float;
-   grid_draw_erase(x, x->x_glist);
-   grid_draw_new(x, x->x_glist);
+    if (!x)
+    {
+        post( "grid : error :tried to set properties on an unexisting object" );
+    }
+    if (argc != 12)
+    {
+        post( "grid : error in the number of arguments ( %d instead of 12 )",
+            argc );
+        return;
+    }
+        if ( argv[0].a_type != A_SYMBOL || argv[1].a_type != A_FLOAT ||
+            argv[2].a_type != A_FLOAT || argv[3].a_type != A_FLOAT ||
+            argv[4].a_type != A_FLOAT || argv[5].a_type != A_FLOAT ||
+            argv[6].a_type != A_FLOAT || argv[7].a_type != A_FLOAT || 
+            argv[8].a_type != A_FLOAT || argv[9].a_type != A_FLOAT || 
+            argv[10].a_type != A_FLOAT || argv[11].a_type != A_FLOAT ) {
+        post( "grid : wrong arguments" );
+        return;
+    }
+    x->x_name = argv[0].a_w.w_symbol;
+    x->x_width = (int)argv[1].a_w.w_float;
+    x->x_min = argv[2].a_w.w_float;
+    x->x_max = argv[3].a_w.w_float;
+    x->x_height = (int)argv[4].a_w.w_float;
+    x->y_min = argv[5].a_w.w_float;
+    x->y_max = argv[6].a_w.w_float;
+    x->x_grid = argv[7].a_w.w_float;
+    x->x_xstep = argv[8].a_w.w_float;
+    x->x_ystep = argv[9].a_w.w_float;
+    x->x_xlines = argv[10].a_w.w_float;
+    x->x_ylines = argv[11].a_w.w_float;
+    grid_draw_erase(x, x->x_glist);
+    grid_draw_new(x, x->x_glist);
 }
 
 static void grid_delete(t_gobj *z, t_glist *glist)
@@ -366,9 +411,9 @@ static void grid_displace(t_gobj *z, t_glist *glist, int dx, int dy)
     x->x_current += dx;
     x->x_obj.te_ypix += dy;
     x->y_current += dy;
-    if(xold != text_xpix(&x->x_obj, glist) || yold != text_ypix(&x->x_obj, glist))
+    if (xold != text_xpix(&x->x_obj, glist) || yold != text_ypix(&x->x_obj, glist))
     {
-	grid_draw_move(x, x->x_glist);
+	//grid_draw_move(x, x->x_glist);
     }
 }
 
@@ -389,19 +434,19 @@ static void grid_motion(t_grid *x, t_floatarg dx, t_floatarg dy)
 }
 
 static int grid_click(t_gobj *z, struct _glist *glist,
-			    int xpix, int ypix, int shift, int alt, int dbl, int doit)
+    int xpix, int ypix, int shift, int alt, int dbl, int doit)
 {
     t_grid* x = (t_grid *)z;
 
     // post( "grid_click doit=%d x=%d y=%d", doit, xpix, ypix );
     if ( doit) 
     {
-      x->x_current = xpix;
-      x->y_current = ypix;
-      grid_output_current(x);
-      grid_draw_update(x, glist);
-      glist_grab(glist, &x->x_obj.te_g, (t_glistmotionfn)grid_motion,
-	       0, xpix, ypix);
+        x->x_current = xpix;
+        x->y_current = ypix;
+        grid_output_current(x);
+        grid_draw_update(x, glist);
+        glist_grab(glist, &x->x_obj.te_g, (t_glistmotionfn)grid_motion,
+            0, xpix, ypix);
     }
     return (1);
 }
@@ -411,16 +456,16 @@ static void grid_goto(t_grid *x, t_floatarg newx, t_floatarg newy)
     int xold = x->x_current;
     int yold = x->y_current;
 
-    if ( newx > x->x_width-1 ) newx = x->x_width-1;
-    if ( newx < 0 ) newx = 0;
-    if ( newy > x->x_height-1 ) newy = x->x_height-1;
-    if ( newy < 0 ) newy = 0;
+    if (newx > x->x_width-1) newx = x->x_width-1;
+    if (newx < 0) newx = 0;
+    if (newy > x->x_height-1) newy = x->x_height-1;
+    if (newy < 0) newy = 0;
 
     // post( "grid_set x=%f y=%f", newx, newy );
 
     x->x_current = newx + text_xpix(&x->x_obj, x->x_glist);
     x->y_current = newy + text_ypix(&x->x_obj, x->x_glist);
-    if(xold != x->x_current || yold != x->y_current)
+    if (xold != x->x_current || yold != x->y_current)
     {
         grid_output_current(x);
         grid_draw_update(x, x->x_glist);
@@ -428,21 +473,22 @@ static void grid_goto(t_grid *x, t_floatarg newx, t_floatarg newy)
 }
 
 
-static void grid_new_color(t_grid *x, t_floatarg color1, t_floatarg color2, t_floatarg color3)
+static void grid_new_color(t_grid *x, t_floatarg color1,
+    t_floatarg color2, t_floatarg color3)
 {
     char col1[10], col2[10], col3[10];
 
-    if ( ( color1 < 0 ) || ( color1 > 255 ) )
+    if ((color1 < 0) || (color1 > 255))
     {
-        post( "wrong color component : %d", (int) color1 );
+        post("wrong color component : %d", (int)color1);
     }
-    if ( ( color2 < 0 ) || ( color2 > 255 ) )
+    if ((color2 < 0) || (color2 > 255))
     {
-        post( "wrong color component : %d", (int) color2 );
+        post("wrong color component : %d", (int)color2);
     }
-    if ( ( color3 < 0 ) || ( color3 > 255 ) )
+    if ((color3 < 0) || (color3 > 255))
     {
-        post( "wrong color component : %d", (int) color3 );
+        post("wrong color component : %d", (int)color3);
     }
 
     if (color1 < 17)
@@ -461,118 +507,122 @@ static void grid_new_color(t_grid *x, t_floatarg color1, t_floatarg color2, t_fl
         sprintf(col3,"%X",(int) color3);
     sprintf( x->x_bgcolor, "#%s%s%s", col1, col2, col3);
 
-    grid_draw_erase( x, x->x_glist);
-    grid_draw_new( x, x->x_glist );
+    grid_draw_erase(x, x->x_glist);
+    grid_draw_new(x, x->x_glist);
 }
 
 static void grid_values(t_grid* x, t_floatarg xvalue, t_floatarg yvalue)
 {
-  int xold = x->x_current;
-  int yold = x->y_current;
+    int xold = x->x_current;
+    int yold = x->y_current;
 
-  if (xvalue < x->x_min ) xvalue = x->x_min;
-  if (xvalue > x->x_max ) xvalue = x->x_max;
+    if (xvalue < x->x_min ) xvalue = x->x_min;
+    if (xvalue > x->x_max ) xvalue = x->x_max;
 
-  x->x_current = text_xpix(&x->x_obj, x->x_glist) + ((xvalue - x->x_min) / x->x_max) * x->x_width;
+    x->x_current = text_xpix(&x->x_obj, x->x_glist) + ((xvalue - x->x_min) / x->x_max) * x->x_width;
 
-  if (yvalue < x->y_min ) yvalue = x->y_min;
-  if (yvalue > x->y_max ) yvalue = x->y_max;
+    if (yvalue < x->y_min ) yvalue = x->y_min;
+    if (yvalue > x->y_max ) yvalue = x->y_max;
 
-  x->y_current =  text_ypix(&x->x_obj, x->x_glist) + (1 - ((yvalue - x->y_min) / x->y_max)) * x->x_height;
+    x->y_current =  text_ypix(&x->x_obj, x->x_glist) + (1 - ((yvalue - x->y_min) / x->y_max)) * x->x_height;
 
-  if(xold != x->x_current || yold != x->y_current) {
+    if (xold != x->x_current || yold != x->y_current)
+    {
         grid_output_current(x);
         grid_draw_update(x, x->x_glist);
-  }
+    }
 }
 
 static void grid_xvalues(t_grid* x, t_floatarg xvalue, t_floatarg yvalue)
 {
-  int xold = x->x_current;
-  int yold = x->y_current;
+    int xold = x->x_current;
+    int yold = x->y_current;
 
-  if (xvalue < x->x_min ) xvalue = x->x_min;
-  if (xvalue > x->x_max ) xvalue = x->x_max;
+    if (xvalue < x->x_min ) xvalue = x->x_min;
+    if (xvalue > x->x_max ) xvalue = x->x_max;
 
-  x->x_current = text_xpix(&x->x_obj, x->x_glist) + ((xvalue - x->x_min) / x->x_max) * x->x_width;
+    x->x_current = text_xpix(&x->x_obj, x->x_glist) + ((xvalue - x->x_min) / x->x_max) * x->x_width;
 
-  if (yvalue < x->y_min ) yvalue = x->y_min;
-  if (yvalue > x->y_max ) yvalue = x->y_max;
+    if (yvalue < x->y_min ) yvalue = x->y_min;
+    if (yvalue > x->y_max ) yvalue = x->y_max;
 
-  x->y_current =  text_ypix(&x->x_obj, x->x_glist) + (1 - ((yvalue - x->y_min) / x->y_max)) * x->x_height;
+    x->y_current =  text_ypix(&x->x_obj, x->x_glist) + (1 - ((yvalue - x->y_min) / x->y_max)) * x->x_height;
 
-  if(xold != x->x_current || yold != x->y_current) {
+    if (xold != x->x_current || yold != x->y_current)
+    {
         grid_draw_update(x, x->x_glist);
-  }
+    }
 }
 
 static void grid_valuemotion(t_grid* x, t_floatarg dx, t_floatarg dy)
 {
-  int xold = x->x_current;
-  int yold = x->y_current;
-  t_float xvalue, yvalue;
+    int xold = x->x_current;
+    int yold = x->y_current;
+    t_float xvalue, yvalue;
 
-  xvalue = x->x_min + (x->x_current - text_xpix(&x->x_obj, x->x_glist)) * (x->x_max-x->x_min) / x->x_width ;
-  if (xvalue < x->x_min ) xvalue = x->x_min;
-  if (xvalue > x->x_max ) xvalue = x->x_max;
+    xvalue = x->x_min + (x->x_current - text_xpix(&x->x_obj, x->x_glist)) * (x->x_max-x->x_min) / x->x_width ;
+    if (xvalue < x->x_min ) xvalue = x->x_min;
+    if (xvalue > x->x_max ) xvalue = x->x_max;
 
-  yvalue = x->y_max - (x->y_current - text_ypix(&x->x_obj, x->x_glist) ) * (x->y_max-x->y_min) / x->x_height ;
-  if (yvalue < x->y_min ) yvalue = x->y_min;
-  if (yvalue > x->y_max ) yvalue = x->y_max;
+    yvalue = x->y_max - (x->y_current - text_ypix(&x->x_obj, x->x_glist) ) * (x->y_max-x->y_min) / x->x_height ;
+    if (yvalue < x->y_min ) yvalue = x->y_min;
+    if (yvalue > x->y_max ) yvalue = x->y_max;
 
-  xvalue += dx;
-  yvalue += dy;
+    xvalue += dx;
+    yvalue += dy;
 
-  if (xvalue < x->x_min ) xvalue = x->x_min;
-  if (xvalue > x->x_max ) xvalue = x->x_max;
+    if (xvalue < x->x_min ) xvalue = x->x_min;
+    if (xvalue > x->x_max ) xvalue = x->x_max;
 
-  x->x_current = text_xpix(&x->x_obj, x->x_glist) + ((xvalue - x->x_min) / x->x_max) * x->x_width;
+    x->x_current = text_xpix(&x->x_obj, x->x_glist) + ((xvalue - x->x_min) / x->x_max) * x->x_width;
 
-  if (yvalue < x->y_min ) yvalue = x->y_min;
-  if (yvalue > x->y_max ) yvalue = x->y_max;
+    if (yvalue < x->y_min ) yvalue = x->y_min;
+    if (yvalue > x->y_max ) yvalue = x->y_max;
 
-  x->y_current =  text_ypix(&x->x_obj, x->x_glist) + (1 - ((yvalue - x->y_min) / x->y_max)) * x->x_height;
+    x->y_current =  text_ypix(&x->x_obj, x->x_glist) + (1 - ((yvalue - x->y_min) / x->y_max)) * x->x_height;
 
-  if(xold != x->x_current || yold != x->y_current) {
+    if (xold != x->x_current || yold != x->y_current)
+    {
         grid_output_current(x);
         grid_draw_update(x, x->x_glist);
-  }
+    }
 }
 
 static void grid_xvaluemotion(t_grid* x, t_floatarg dx, t_floatarg dy)
 {
-  int xold = x->x_current;
-  int yold = x->y_current;
-  t_float xvalue, yvalue;
+    int xold = x->x_current;
+    int yold = x->y_current;
+    t_float xvalue, yvalue;
 
-  xvalue = x->x_min + (x->x_current - text_xpix(&x->x_obj, x->x_glist)) * (x->x_max-x->x_min) / x->x_width ;
-  if (xvalue < x->x_min ) xvalue = x->x_min;
-  if (xvalue > x->x_max ) xvalue = x->x_max;
+    xvalue = x->x_min + (x->x_current - text_xpix(&x->x_obj, x->x_glist)) * (x->x_max-x->x_min) / x->x_width;
+    if (xvalue < x->x_min) xvalue = x->x_min;
+    if (xvalue > x->x_max) xvalue = x->x_max;
 
-  yvalue = x->y_max - (x->y_current - text_ypix(&x->x_obj, x->x_glist) ) * (x->y_max-x->y_min) / x->x_height ;
-  if (yvalue < x->y_min ) yvalue = x->y_min;
-  if (yvalue > x->y_max ) yvalue = x->y_max;
+    yvalue = x->y_max - (x->y_current - text_ypix(&x->x_obj, x->x_glist) ) * (x->y_max-x->y_min) / x->x_height ;
+    if (yvalue < x->y_min ) yvalue = x->y_min;
+    if (yvalue > x->y_max ) yvalue = x->y_max;
 
-  xvalue += dx;
-  yvalue += dy;
+    xvalue += dx;
+    yvalue += dy;
 
-  if (xvalue < x->x_min ) xvalue = x->x_min;
-  if (xvalue > x->x_max ) xvalue = x->x_max;
+    if (xvalue < x->x_min) xvalue = x->x_min;
+    if (xvalue > x->x_max) xvalue = x->x_max;
 
-  x->x_current = text_xpix(&x->x_obj, x->x_glist) + ((xvalue - x->x_min) / x->x_max) * x->x_width;
+    x->x_current = text_xpix(&x->x_obj, x->x_glist) + ((xvalue - x->x_min) / x->x_max) * x->x_width;
 
-  if (yvalue < x->y_min ) yvalue = x->y_min;
-  if (yvalue > x->y_max ) yvalue = x->y_max;
+    if (yvalue < x->y_min) yvalue = x->y_min;
+    if (yvalue > x->y_max) yvalue = x->y_max;
 
-  x->y_current =  text_ypix(&x->x_obj, x->x_glist) + (1 - ((yvalue - x->y_min) / x->y_max)) * x->x_height;
+    x->y_current = text_ypix(&x->x_obj, x->x_glist) + (1 - ((yvalue - x->y_min) / x->y_max)) * x->x_height;
 
-  if(xold != x->x_current || yold != x->y_current) {
+    if (xold != x->x_current || yold != x->y_current)
+    {
         grid_draw_update(x, x->x_glist);
-  }
+    }
 }
 
 static void grid_bang(t_grid *x) {
-  grid_output_current(x);
+    grid_output_current(x);
 }
 
 static t_grid *grid_new(t_symbol *s, int argc, t_atom *argv)
@@ -585,84 +635,87 @@ static t_grid *grid_new(t_symbol *s, int argc, t_atom *argv)
 
     x = (t_grid *)pd_new(grid_class);
     // new grid created from the gui 
-    if ( argc != 0 )
+    if (argc != 0)
     {
-      if ( argc != 14 )
-      {
-        post( "grid : error in the number of arguments ( %d instead of 14 )", argc );
-        return NULL;
-      }
-      if ( argv[0].a_type != A_SYMBOL || argv[1].a_type != A_FLOAT ||
-        argv[2].a_type != A_FLOAT || argv[3].a_type != A_FLOAT ||
-        argv[4].a_type != A_FLOAT || argv[5].a_type != A_FLOAT ||
-        argv[6].a_type != A_FLOAT || argv[7].a_type != A_FLOAT || 
-        argv[8].a_type != A_FLOAT || argv[9].a_type != A_FLOAT || 
-        argv[10].a_type != A_FLOAT || argv[11].a_type != A_FLOAT || 
-        argv[12].a_type != A_FLOAT || argv[13].a_type != A_FLOAT ) {
-        post( "grid : wrong arguments" );
-        return NULL;
-      }
-
-      // update grid count
-      if (!strncmp((str = argv[0].a_w.w_symbol->s_name), "grid", 5)
-    	 && (zz = atoi(str + 5)) > gridcount) 
-      {
-        gridcount = zz;
-      }
-      x->x_name = argv[0].a_w.w_symbol;
-      pd_bind(&x->x_obj.ob_pd, x->x_name);
-      x->x_width = argv[1].a_w.w_float;
-      x->x_min = argv[2].a_w.w_float;
-      x->x_max = argv[3].a_w.w_float;
-      x->x_height = argv[4].a_w.w_float;
-      x->y_min = argv[5].a_w.w_float;
-      x->y_max = argv[6].a_w.w_float;
-      x->x_grid = argv[7].a_w.w_float;
-      x->x_xstep = argv[8].a_w.w_float;
-      x->x_ystep = argv[9].a_w.w_float;
-      x->x_xlines = argv[10].a_w.w_float;
-      x->x_ylines = argv[11].a_w.w_float;
-      x->x_current = argv[12].a_w.w_float;
-      x->y_current = argv[13].a_w.w_float;
-      x->x_point = 1;
+        if ( argc != 14 )
+        {
+            post( "grid : error in the number of arguments "
+                  "( %d instead of 14 )",
+                argc );
+            return NULL;
+        }
+        if (argv[0].a_type != A_SYMBOL || argv[1].a_type != A_FLOAT ||
+            argv[2].a_type != A_FLOAT || argv[3].a_type != A_FLOAT ||
+            argv[4].a_type != A_FLOAT || argv[5].a_type != A_FLOAT ||
+            argv[6].a_type != A_FLOAT || argv[7].a_type != A_FLOAT ||
+            argv[8].a_type != A_FLOAT || argv[9].a_type != A_FLOAT ||
+            argv[10].a_type != A_FLOAT || argv[11].a_type != A_FLOAT ||
+            argv[12].a_type != A_FLOAT || argv[13].a_type != A_FLOAT )
+        {
+            post( "grid : wrong arguments" );
+            return NULL;
+        }
+
+        // update grid count
+        if (!strncmp((str = argv[0].a_w.w_symbol->s_name), "grid", 5)
+            && (zz = atoi(str + 5)) > gridcount)
+        {
+            gridcount = zz;
+        }
+        x->x_name = argv[0].a_w.w_symbol;
+        pd_bind(&x->x_obj.ob_pd, x->x_name);
+        x->x_width = argv[1].a_w.w_float;
+        x->x_min = argv[2].a_w.w_float;
+        x->x_max = argv[3].a_w.w_float;
+        x->x_height = argv[4].a_w.w_float;
+        x->y_min = argv[5].a_w.w_float;
+        x->y_max = argv[6].a_w.w_float;
+        x->x_grid = argv[7].a_w.w_float;
+        x->x_xstep = argv[8].a_w.w_float;
+        x->x_ystep = argv[9].a_w.w_float;
+        x->x_xlines = argv[10].a_w.w_float;
+        x->x_ylines = argv[11].a_w.w_float;
+        x->x_current = argv[12].a_w.w_float;
+        x->y_current = argv[13].a_w.w_float;
+        x->x_point = 1;
     }
     else
     {
-      char buf[40];
-
-      sprintf(buf, "grid%d", ++gridcount);
-      s = gensym(buf);    	
-
-      x->x_name = s;
-      pd_bind(&x->x_obj.ob_pd, x->x_name);
-
-      x->x_width = DEFAULT_GRID_WIDTH;
-      x->x_min = 0;
-      x->x_max = DEFAULT_GRID_WIDTH - 1;
-      x->x_height = DEFAULT_GRID_HEIGHT;
-      x->y_min = 0;
-      x->y_max = DEFAULT_GRID_HEIGHT - 1;
-      x->x_grid = 1;	
-      x->x_xstep = 1.0;	
-      x->x_ystep = 1.0;	
-      x->x_xlines = DEFAULT_GRID_NBLINES;	
-      x->x_ylines = DEFAULT_GRID_NBLINES;	
-      x->x_current = 0;
-      x->y_current = 0;	
-
+        char buf[40];
+
+        sprintf(buf, "grid%d", ++gridcount);
+        s = gensym(buf);
+
+        x->x_name = s;
+        pd_bind(&x->x_obj.ob_pd, x->x_name);
+
+        x->x_width = DEFAULT_GRID_WIDTH;
+        x->x_min = 0;
+        x->x_max = DEFAULT_GRID_WIDTH - 1;
+        x->x_height = DEFAULT_GRID_HEIGHT;
+        x->y_min = 0;
+        x->y_max = DEFAULT_GRID_HEIGHT - 1;
+        x->x_grid = 1;
+        x->x_xstep = 1.0;
+        x->x_ystep = 1.0;
+        x->x_xlines = DEFAULT_GRID_NBLINES;
+        x->x_ylines = DEFAULT_GRID_NBLINES;
+        x->x_current = 0;
+        x->y_current = 0;
     }
 
     // common fields for new and restored grids
-    x->x_point = 0;	
-    x->x_selected = 0;	
+    x->x_point = 0;
+    x->x_selected = 0;
     x->x_glist = (t_glist *) canvas_getcurrent();
-    x->x_xoutlet = outlet_new(&x->x_obj, &s_float ); 
-    x->x_youtlet = outlet_new(&x->x_obj, &s_float ); 
+    x->x_xoutlet = outlet_new(&x->x_obj, &s_float );
+    x->x_youtlet = outlet_new(&x->x_obj, &s_float );
 
     x->x_bgcolor = (char*)getbytes(12);
-    strcpy( x->x_bgcolor, "#123589" ); 
+    strcpy( x->x_bgcolor, "#123589" );
 
-    // post( "grid_new name : %s width: %d height : %d", x->x_name->s_name, x->x_width, x->x_height );
+    // post( "grid_new name : %s width: %d height : %d",
+    // x->x_name->s_name, x->x_width, x->x_height );
 
     return (x);
 }
@@ -674,26 +727,30 @@ static void grid_free(t_grid *x)
 
 void grid_setup(void)
 {
-#include "grid.tk2c"
+//#include "grid.tk2c"
     post ( grid_version );
     grid_class = class_new(gensym("grid"), (t_newmethod)grid_new,
-			      (t_method)grid_free, sizeof(t_grid), 0, A_GIMME, 0);
-    class_addmethod(grid_class, (t_method)grid_click, gensym("click"),
-		    A_FLOAT, A_FLOAT, A_FLOAT, A_FLOAT, A_FLOAT, 0);
-    class_addmethod(grid_class, (t_method)grid_motion, gensym("motion"),
-		    A_FLOAT, A_FLOAT, 0);
-    class_addmethod(grid_class, (t_method)grid_bang, gensym("bang"), 0);
-    class_addmethod(grid_class, (t_method)grid_values, gensym("values"),
-                    A_FLOAT, A_FLOAT, 0);
-    class_addmethod(grid_class, (t_method)grid_valuemotion, gensym("valuemotion"),
-                    A_FLOAT, A_FLOAT, 0);
-    class_addmethod(grid_class, (t_method)grid_xvalues, gensym("xvalues"),
-                    A_FLOAT, A_FLOAT, 0);
-    class_addmethod(grid_class, (t_method)grid_xvaluemotion, gensym("xvaluemotion"),
-                    A_FLOAT, A_FLOAT, 0);
-    class_addmethod(grid_class, (t_method)grid_goto, gensym("goto"), A_FLOAT, A_FLOAT, 0);
-    class_addmethod(grid_class, (t_method)grid_dialog, gensym("dialog"), A_GIMME, 0);
-    class_addmethod(grid_class, (t_method)grid_new_color, gensym("color"), A_FLOAT, A_FLOAT, A_FLOAT, 0);
+        (t_method)grid_free, sizeof(t_grid), 0, A_GIMME, 0);
+    class_addmethod(grid_class, (t_method)grid_click,
+        gensym("click"), A_FLOAT, A_FLOAT, A_FLOAT, A_FLOAT, A_FLOAT, 0);
+    class_addmethod(grid_class, (t_method)grid_motion,
+        gensym("motion"), A_FLOAT, A_FLOAT, 0);
+    class_addmethod(grid_class, (t_method)grid_bang,
+        gensym("bang"), 0);
+    class_addmethod(grid_class, (t_method)grid_values,
+        gensym("values"), A_FLOAT, A_FLOAT, 0);
+    class_addmethod(grid_class, (t_method)grid_valuemotion,
+        gensym("valuemotion"), A_FLOAT, A_FLOAT, 0);
+    class_addmethod(grid_class, (t_method)grid_xvalues,
+        gensym("xvalues"), A_FLOAT, A_FLOAT, 0);
+    class_addmethod(grid_class, (t_method)grid_xvaluemotion,
+        gensym("xvaluemotion"), A_FLOAT, A_FLOAT, 0);
+    class_addmethod(grid_class, (t_method)grid_goto,
+        gensym("goto"), A_FLOAT, A_FLOAT, 0);
+    class_addmethod(grid_class, (t_method)grid_dialog,
+        gensym("dialog"), A_GIMME, 0);
+    class_addmethod(grid_class, (t_method)grid_new_color,
+        gensym("color"), A_FLOAT, A_FLOAT, A_FLOAT, 0);
     grid_widgetbehavior.w_getrectfn =    grid_getrect;
     grid_widgetbehavior.w_displacefn =   grid_displace;
     grid_widgetbehavior.w_selectfn =     grid_select;
@@ -711,5 +768,4 @@ void grid_setup(void)
 #endif
 
     class_setwidget(grid_class, &grid_widgetbehavior);
-
 }
diff --git a/pd/nw/pdgui.js b/pd/nw/pdgui.js
index 7a498963a..23a991769 100644
--- a/pd/nw/pdgui.js
+++ b/pd/nw/pdgui.js
@@ -3322,6 +3322,114 @@ function gui_scope_displace(cid, tag, dx, dy) {
     gui_text_displace(cid, tag, dx, dy);
 }
 
+// unauthorized/grid
+
+function make_grid_data(w, h, x_l, y_l) {
+    var d, x, y, offset;
+    d = [];
+    offset = Math.floor(w / x_l);
+    if (offset > 0) {
+        for (x = 0; x < w; x += offset) {
+            d = d.concat(["M", x, 0, x, h]); // vertical line
+        }
+    } else {
+        post("Warning: too many gridlines");
+    }
+    offset = Math.floor(h / y_l);
+    if (offset > 0) {
+        for (y = 0; y < w; y += offset) {
+            d = d.concat(["M", 0, y, w, y]); // horizontal line
+        }
+    } else {
+        post("Warning: too many gridlines");
+    }
+    return d.join(" ");
+}
+
+function gui_grid_draw_bg(cid, tag, w, h, bg_color, has_grid, x_l, y_l) {
+    var g = get_gobj(cid, tag),
+        border, // for inheriting gui preset colors
+        bg,
+        out_0,
+        out_1,
+        grid,
+        grid_d_string,
+        point,
+        point_size = 5;
+    bg = create_item(cid, "rect", {
+        width: w,
+        height: h,
+        fill: bg_color,
+        class: "bg"
+    });
+    border = create_item(cid, "path", {
+        d: ["M", 0, 0, w, 0,
+            "M", 0, h, w, h,
+            "M", 0, 0, 0, h,
+            "M", w, 0, w, h
+           ].join(" "),
+        width: w,
+        height: h,
+        fill: "none",
+        stroke: "black",
+        "stroke-width": 1,
+        class: "border" // now we can inherit the css border styles
+    });
+    out_0 = create_item(cid, "rect", {
+        y: h + 1,
+        width: 7,
+        height: 1,
+        fill: "none",
+        stroke: "black",
+        "stroke-width": 1,
+        class: "out_0"
+    });
+    out_1 = create_item(cid, "rect", {
+        x: w - 7,
+        y: h + 1,
+        width: 7,
+        height: 1,
+        fill: "none",
+        stroke: "black",
+        "stroke-width": 1,
+        class: "out_1"
+    });
+    if (has_grid === 1) {
+        grid_d_string = make_grid_data(w, h, x_l, y_l);
+        grid = create_item(cid, "path", {
+            d: grid_d_string,
+            stroke: "white",
+            "stroke-width": 1,
+            class: "grid"
+        });
+    }
+    point = create_item(cid, "rect", {
+        style: "visibility: none;",
+        width: 5,
+        height: 5,
+        fill: "#ff0000",
+        stroke: "black",
+        "stroke-width": 1,
+        class: "point"
+    });
+    g.appendChild(bg);
+    g.appendChild(out_0);
+    g.appendChild(out_1);
+    g.appendChild(grid);
+    g.appendChild(point);
+    g.appendChild(border);
+}
+
+function gui_grid_point(cid, tag, x, y) {
+    var g = get_gobj(cid, tag),
+        p = g.querySelector(".point");
+    configure_item(p, {
+        x: x,
+        y: y,
+        style: "visibility: visible;"
+    });
+}
+
 function add_popup(cid, popup) {
     popup_menu[cid] = popup;
 }
-- 
GitLab