From aae52e18f0d0df94f7d95ba4bebafdee534d87e1 Mon Sep 17 00:00:00 2001
From: Jonathan Wilkes <jon.w.wilkes@gmail.com>
Date: Tue, 10 Nov 2015 21:47:25 -0500
Subject: [PATCH] add menu items for "Parent Window" and "Visible Parent
 Window", more cleanup of menus

---
 pd/nw/index.js                    | 64 -------------------------------
 pd/nw/locales/en/translation.json |  2 +
 pd/nw/pd_canvas.js                | 12 +++++-
 pd/src/g_editor.c                 | 29 +++++++++-----
 4 files changed, 32 insertions(+), 75 deletions(-)

diff --git a/pd/nw/index.js b/pd/nw/index.js
index 7643a18b2..95195b20c 100644
--- a/pd/nw/index.js
+++ b/pd/nw/index.js
@@ -231,18 +231,6 @@ function console_find_keydown(elem, evt) {
     }
 }
 
-function pdmenu_copy () {
-    alert("Please implement pdmenu_copy"); 
-}
-
-function pdmenu_selectall () {
-    alert("Please implement pdmenu_selectall"); 
-}
-
-function pdmenu_preferences () {
-    alert("Please implement pdmenu_preferences"); 
-}
-
 function pdmenu_next_win () {
     alert("Please implement pdmenu_preferences"); 
 }
@@ -251,38 +239,6 @@ function pdmenu_previous_win () {
     alert("Please implement pdmenu_preferences"); 
 }
 
-function pdmenu_parent_win () {
-    alert("Please implement pdmenu_preferences"); 
-}
-
-function pdmenu_console_win () {
-    alert("Please implement pdmenu_preferences"); 
-}
-
-function pdmenu_audio_on () {
-    alert("Please implement pdmenu_preferences"); 
-}
-
-function pdmenu_audio_off () {
-    alert("Please implement pdmenu_preferences"); 
-}
-
-function pdmenu_test_audio () {
-    alert("Please implement pdmenu_preferences"); 
-}
-
-function pdmenu_load_meter () {
-    alert("Please implement pdmenu_preferences"); 
-}
-
-function pdmenu_about_pd () {
-    alert("Please implement pdmenu_preferences"); 
-}
-
-function pdmenu_manual () {
-    alert("Please implement pdmenu_preferences"); 
-}
-
 function pdmenu_help_browser () {
     alert("Please implement pdmenu_preferences"); 
 }
@@ -568,26 +524,6 @@ function nw_create_pd_window_menus () {
         tooltip: l("menu.prevwin_tt")
     }));
 
-    winmanMenu.append(new gui.MenuItem({
-        type: "separator"
-    }));
-
-    winmanMenu.append(new gui.MenuItem({
-        label: l("menu.parentwin"),
-        click: pdmenu_parent_win,
-        //key: "a",
-        //modifiers: "ctrl",
-        tooltip: l("menu.parentwin_tt")
-    }));
-
-    winmanMenu.append(new gui.MenuItem({
-        label: l("menu.pdwin"),
-        click: pdmenu_console_win,
-        //key: "a",
-        //modifiers: "ctrl",
-        tooltip: l("menu.pdwin_tt")
-    }));
-
     // Media menu
     var mediaMenu = new gui.Menu();
 
diff --git a/pd/nw/locales/en/translation.json b/pd/nw/locales/en/translation.json
index 1f083886a..747380ff9 100644
--- a/pd/nw/locales/en/translation.json
+++ b/pd/nw/locales/en/translation.json
@@ -200,6 +200,8 @@
     "prevwin_tt": "Give focus to the previous window in the stacking order",
     "parentwin": "Parent Window",
     "parentwin_tt": "give focus to the parent window of the current window",
+    "visible_parentwin": "Visible Parent Window",
+    "visible_parentwin_tt": "give focus to the closest ancestor of this window that is visible",
     "pdwin": "Pd Window",
     "pdwin_tt": "Give focus to the main Pd window",
 
diff --git a/pd/nw/pd_canvas.js b/pd/nw/pd_canvas.js
index d5a4f9826..396484d6f 100644
--- a/pd/nw/pd_canvas.js
+++ b/pd/nw/pd_canvas.js
@@ -1238,10 +1238,20 @@ function nw_create_patch_window_menus(name) {
 
     winmanMenu.append(new gui.MenuItem({
         label: l("menu.parentwin"),
-        click: menu_generic,
+        click: function() {
+            pdgui.pdsend(name, "findparent", 0);
+        },
         tooltip: l("menu.parentwin_tt"),
     }));
 
+    winmanMenu.append(new gui.MenuItem({
+        label: l("menu.visible_parentwin"),
+        click: function() {
+            pdgui.pdsend(name, "findparent", 1);
+        },
+        tooltip: l("menu.visible_parentwin_tt"),
+    }));
+
     winmanMenu.append(new gui.MenuItem({
         label: l("menu.pdwin"),
         click: function() {
diff --git a/pd/src/g_editor.c b/pd/src/g_editor.c
index 9a08d364c..e4dce60f6 100644
--- a/pd/src/g_editor.c
+++ b/pd/src/g_editor.c
@@ -5744,19 +5744,28 @@ static void canvas_find_again(t_canvas *x)
 }
 
 /* following function serves mainly as a helper function for tcl/tk
- and focuses first visible parent (not the immediate parent) */
-static void canvas_find_parent(t_canvas *x)
+   f = 0: show immediate parent
+   f = 1: show visible parent */
+static void canvas_find_parent(t_canvas *x, t_floatarg f)
 {
     if (x->gl_owner)
     {
-        t_glist *owner = x->gl_owner;
-        while (!glist_isvisible(owner) &&
-               !owner->gl_havewindow && owner->gl_owner)
-            owner = owner->gl_owner;
-        if (glist_isvisible(owner) && owner->gl_havewindow)
-            canvas_vis(owner, 1);
+        if (f != 0) /* find visible parent */
+        {
+            t_glist *owner = x->gl_owner;
+            while (!glist_isvisible(owner) &&
+                   !owner->gl_havewindow && owner->gl_owner)
+                owner = owner->gl_owner;
+            if (glist_isvisible(owner) && owner->gl_havewindow)
+                canvas_vis(owner, 1);
+        }
+        else /* find immediate parent */
+        {
+            canvas_vis(glist_getcanvas(x->gl_owner), 1);
+        }
     }
-    else {
+    else
+    {
         sys_gui("menu_raise_console\n");
     }
 }
@@ -7783,7 +7792,7 @@ void g_editor_setup(void)
     class_addmethod(canvas_class, (t_method)canvas_find_again,
         gensym("findagain"), A_NULL);
     class_addmethod(canvas_class, (t_method)canvas_find_parent,
-        gensym("findparent"), A_NULL);
+        gensym("findparent"), A_DEFFLOAT, A_NULL);
     class_addmethod(canvas_class, (t_method)canvas_done_popup,
         gensym("done-popup"), A_FLOAT, A_FLOAT, A_FLOAT, A_NULL);
     class_addmethod(canvas_class, (t_method)canvas_donecanvasdialog,
-- 
GitLab