From 3e677f2c47512f0816f6b666127991e87b4c1d43 Mon Sep 17 00:00:00 2001
From: Albert Graef <aggraef@gmail.com>
Date: Tue, 8 Sep 2020 12:17:15 +0200
Subject: [PATCH] Added the logic to conditionally show the editmode background

---
 pd/nw/dialog_prefs.html |  6 ++++--
 pd/nw/pd_canvas.js      |  6 +++++-
 pd/nw/pdgui.js          | 39 ++++++++++++++++++++++++++++++++++-----
 pd/src/g_editor.c       |  4 +++-
 4 files changed, 46 insertions(+), 9 deletions(-)

diff --git a/pd/nw/dialog_prefs.html b/pd/nw/dialog_prefs.html
index 4cd2179d6..9a8b89d7d 100644
--- a/pd/nw/dialog_prefs.html
+++ b/pd/nw/dialog_prefs.html
@@ -762,8 +762,8 @@ function apply(save_prefs) {
     );
 
     // Send the gui prefs (currently just the name of the gui preset, the
-    // status of the save-zoom toggle and various options related to the help
-    // browser) to Pd
+    // status of the show_grid and save_zoom toggles and various options
+    // related to the help browser) to Pd
     pdgui.pdsend("pd gui-prefs",
         get_gui_preset(),
         get_bool_elem("show_grid"),
@@ -773,6 +773,8 @@ function apply(save_prefs) {
         get_bool_elem("browser_init"),
         get_autopatch_yoffset()
     );
+    // Update the grid on all open windows.
+    pdgui.update_grid(get_bool_elem("show_grid"));
 
     // Send the startup config data to Pd
     pdgui.pdsend.apply(null, ["pd path-dialog", startup_use_stdpath, startup_verbose].concat(get_path_array()));
diff --git a/pd/nw/pd_canvas.js b/pd/nw/pd_canvas.js
index 55c3c8ada..9e074b544 100644
--- a/pd/nw/pd_canvas.js
+++ b/pd/nw/pd_canvas.js
@@ -1455,6 +1455,10 @@ function set_edit_menu_modals(state) {
     canvas_menu.edit.font.enabled = state;
 }
 
+function get_editmode_checkbox() {
+    return canvas_menu.edit.editmode.checked;
+}
+
 function set_editmode_checkbox(state) {
     canvas_menu.edit.editmode.checked = state;
 }
@@ -2107,4 +2111,4 @@ function init_menu_font_size(size) {
 // LATER: we can use this to also update the undo state appropriately
 function update_menu_items(cid) {
     pdgui.pdsend(cid, "updatemenu");
-}
\ No newline at end of file
+}
diff --git a/pd/nw/pdgui.js b/pd/nw/pdgui.js
index 1cbcb031e..3fd48f16d 100644
--- a/pd/nw/pdgui.js
+++ b/pd/nw/pdgui.js
@@ -1283,9 +1283,11 @@ function canvas_set_editmode(cid, state) {
         w.set_editmode_checkbox(state !== 0 ? true : false);
         if (state !== 0) {
             patchsvg.classList.add("editmode");
-            //post("editmode:" + gui_editmode_svg_background);
-            patchwin[cid].window.document.body.style.setProperty("background-image",
-                gui_editmode_svg_background);
+            if (showgrid[cid]) {
+                //post("editmode:" + gui_editmode_svg_background);
+                patchwin[cid].window.document.body.style.setProperty
+                ("background-image", gui_editmode_svg_background);
+            }
         } else {
             patchsvg.classList.remove("editmode");
             patchwin[cid].window.document.body.style.setProperty("background-image",
@@ -1300,6 +1302,25 @@ function gui_canvas_set_editmode(cid, state) {
     canvas_set_editmode(cid, state);
 }
 
+function update_grid(grid) {
+    // Update the grid background of all canvas windows when the corresponding
+    // option in the gui prefs changes.
+    var bg = grid != 0 ? gui_editmode_svg_background : "none";
+    for (var cid in patchwin) {
+	gui(cid).get_elem("patchsvg", function(patchsvg, w) {
+            var editmode = w.get_editmode_checkbox();
+	    if (editmode) {
+                patchwin[cid].window.document.body.style.setProperty
+                ("background-image", bg);
+	    }
+	});
+    }
+    // Also update the showgrid flags.
+    set_showgrid(grid);
+}
+
+exports.update_grid = update_grid;
+
 // requires nw.js API (Menuitem)
 function gui_canvas_set_cordinspector(cid, state) {
     patchwin[cid].window.set_cord_inspector_checkbox(state !== 0 ? true : false);
@@ -1556,6 +1577,7 @@ var scroll = {},
     redo = {},
     font = {},
     doscroll = {},
+    showgrid = {},
     last_loaded, // last loaded canvas
     last_focused, // last focused canvas (doesn't include Pd window or dialogs)
     loading = {},
@@ -1566,6 +1588,12 @@ var scroll = {},
     var patchwin = {}; // object filled with cid: [Window object] pairs
     var dialogwin = {}; // object filled with did: [Window object] pairs
 
+var set_showgrid = function(grid) {
+    for (var cid in showgrid) {
+	showgrid[cid] = grid;
+    }
+}
+
 exports.get_patchwin = function(name) {
     return patchwin[name];
 }
@@ -1725,7 +1753,7 @@ function create_window(cid, type, width, height, xpos, ypos, attr_array) {
 }
 
 // create a new canvas
-function gui_canvas_new(cid, width, height, geometry, zoom, editmode, name, dir, dirty_flag, hide_scroll, hide_menu, has_toplevel_scalars, cargs) {
+function gui_canvas_new(cid, width, height, geometry, grid, zoom, editmode, name, dir, dirty_flag, hide_scroll, hide_menu, has_toplevel_scalars, cargs) {
     // hack for buggy tcl popups... should go away for node-webkit
     //reset_ctrl_on_popup_window
     
@@ -1752,6 +1780,7 @@ function gui_canvas_new(cid, width, height, geometry, zoom, editmode, name, dir,
     redo[cid] = false;
     font[cid] = 10;
     doscroll[cid] = 0;
+    showgrid[cid] = grid != 0;
     toplevel_scalars[cid] = has_toplevel_scalars;
     // geometry is just the x/y screen offset "+xoff+yoff"
     geometry = geometry.slice(1);   // remove the leading "+"
@@ -6780,4 +6809,4 @@ function gui_osx_dialog_appearance(id)
     var close_button = dialogwin[id].window.document.getElementById("titlebar_close_button");
     close_button.style.setProperty("line-height", "14px");
     close_button.style.setProperty("border-radius", "10px");
-}
\ No newline at end of file
+}
diff --git a/pd/src/g_editor.c b/pd/src/g_editor.c
index 5e30db534..8ac701f44 100644
--- a/pd/src/g_editor.c
+++ b/pd/src/g_editor.c
@@ -2692,6 +2692,7 @@ void canvas_vis(t_canvas *x, t_floatarg f)
 
     t_gobj *g;
     t_int properties;
+    extern int sys_grid;
 
     int flag = (f != 0);
     if (flag)
@@ -2747,11 +2748,12 @@ void canvas_vis(t_canvas *x, t_floatarg f)
                We may need to expand this to include scalars, as well. */
             canvas_create_editor(x);
             canvas_args_to_string(argsbuf, x);
-            gui_vmess("gui_canvas_new", "xiisiissiiiis",
+            gui_vmess("gui_canvas_new", "xiisiiissiiiis",
                 x,
                 (int)(x->gl_screenx2 - x->gl_screenx1),
                 (int)(x->gl_screeny2 - x->gl_screeny1),
                 geobuf,
+                sys_grid,
                 x->gl_zoom,
                 x->gl_edit,
                 x->gl_name->s_name,
-- 
GitLab