From f75669d01e9968a95be469cd6c520b177ae87dec Mon Sep 17 00:00:00 2001
From: Jonathan Wilkes <jon.w.wilkes@gmail.com>
Date: Tue, 27 Oct 2015 21:44:56 -0400
Subject: [PATCH] first draft of functionality for opening pd patches from the
 menu

---
 pd/nw/index.js   | 17 +++++++++++++----
 pd/nw/pdgui.js   | 34 ++++++++++++++++++++++++++++++++++
 pd/src/s_inter.c | 10 ++++++++--
 3 files changed, 55 insertions(+), 6 deletions(-)

diff --git a/pd/nw/index.js b/pd/nw/index.js
index 4548018bf..6ac9353f9 100644
--- a/pd/nw/index.js
+++ b/pd/nw/index.js
@@ -1,11 +1,14 @@
 "use strict";
 var nw = require("nw.gui");
-console.log(nw.App.argv);
+console.log("foo is foo");
+console.log("gurgle is " + nw.App.argv);
 var pdgui = require("./pdgui.js");
 var port_no = nw.App.argv[0]; // fed to us by the Pd process
+var gui_dir = nw.App.argv[3];
 var pwd = process.env.PWD;
 pdgui.set_port(port_no);
 pdgui.set_pwd(pwd);
+pdgui.set_gui_dir(gui_dir);
 pdgui.set_pd_window(this);
 pdgui.set_app_quitfn(app_quit);
 
@@ -593,7 +596,9 @@ function nw_create_pd_window_menus () {
 
     mediaMenu.append(new nw.MenuItem({
         label: l("menu.test"),
-        click: pdmenu_test_audio,
+        click: function() {
+            pdgui.pd_doc_open("doc/7.stuff/tools", "testtone.pd");
+        },
         //key: "a",
         //modifiers: "ctrl",
         tooltip: l("menu.test_tt")
@@ -601,7 +606,9 @@ function nw_create_pd_window_menus () {
 
     mediaMenu.append(new nw.MenuItem({
         label: l("menu.loadmeter"),
-        click: pdmenu_load_meter,
+        click: function() {
+            pdgui.pd_doc_open("doc/7.stuff/tools", "load-meter.pd");
+        },
         //key: "a",
         //modifiers: "ctrl",
         tooltip: l("menu.loadmeter_tt")
@@ -619,7 +626,9 @@ function nw_create_pd_window_menus () {
     // Help sub-entries
     helpMenu.append(new nw.MenuItem({
         label: l("menu.about"),
-        click: pdmenu_about_pd,
+        click: function() {
+//            pd_doc_open("doc/1.manual", "1.introduc
+        },
         //key: "c",
         //modifiers: "ctrl",
         tooltip: l("menu.about_tt")
diff --git a/pd/nw/pdgui.js b/pd/nw/pdgui.js
index 9b51a128d..b68c9862b 100644
--- a/pd/nw/pdgui.js
+++ b/pd/nw/pdgui.js
@@ -3,6 +3,7 @@
 // Modules
 
 var pwd;
+var gui_dir;
 
 exports.set_pwd = function(pwd_string) {
     pwd = pwd_string;
@@ -12,6 +13,14 @@ exports.get_pwd = function() {
     return pwd;
 }
 
+exports.set_gui_dir = function(dir_string) {
+    gui_dir = path.normalize(path.join(dir_string, ".."));
+}
+
+exports.get_gui_dir = function() {
+    return gui_dir;
+}
+
 var fs = require("fs");     // for fs.existsSync
 var path = require("path"); // for path.dirname path.extname path.join
 
@@ -506,6 +515,31 @@ function gui_open_files_via_unique(filenames)
     }
 }
 
+// Think about renaming this and pd_doc_open...
+
+// Open a file-- html, text, or Pd.
+function doc_open (dir, basename) {
+    // Just Pd files at the moment... we'll add the others later
+    var norm_path = path.normalize(dir);
+    if (basename.slice(-4) === ".txt"
+        || basename.slice(-2) === ".c") {
+        open_text(path.join(norm_path, basename));
+    } else if (basename.slice(-5) === ".html"
+               || basename.slice(-4) === ".htm"
+               || basename.slice(-4) === ".pdf") {
+        open_html(path.join(norm_path, basename));
+    } else {
+        pdsend("pd open", enquote(basename), enquote(norm_path));
+    }
+}
+
+// Open a file relative to the main directory where "doc/" and "extra/" live
+function pd_doc_open(dir, basename) {
+    doc_open(path.join(gui_dir, dir), basename);
+}
+
+exports.pd_doc_open = pd_doc_open;
+
 function gui_build_filelist(file) {
     startup_files.push(file);
 }
diff --git a/pd/src/s_inter.c b/pd/src/s_inter.c
index 94e78a98e..8b5306ae2 100644
--- a/pd/src/s_inter.c
+++ b/pd/src/s_inter.c
@@ -1287,13 +1287,19 @@ int sys_startgui(const char *guidir)
 
 fprintf(stderr, "guidir is %s\n", guidir);
 
+            /* For some reason, the nw binary doesn't give you access to
+               the first argument-- this is the path to the directory where
+               package.json lives (or the zip file if it's compressed). So
+               we add it again as the last argument to make sure we can fetch
+               it on the GUI side. */
             sprintf(cmdbuf,
-                "%s/nw/nw %s %d localhost %s\n",
+                "%s/nw/nw %s %d localhost %s %s\n",
                 guidir,
                 guidir,
 //                "/home/user/pd-nw/pd/nw",
                 portno,
-                (sys_k12_mode ? "pd-l2ork-k12" : "pd-l2ork"));
+                (sys_k12_mode ? "pd-l2ork-k12" : "pd-l2ork"),
+                guidir);
 
 #endif
             sys_guicmd = cmdbuf;
-- 
GitLab