From 5753e0733f4504a3b10160843c6e3bef649b8333 Mon Sep 17 00:00:00 2001
From: Albert Graef <aggraef@gmail.com>
Date: Wed, 16 Nov 2016 16:24:38 +0100
Subject: [PATCH] Add fit to width/height zoom operations.

---
 pd/nw/locales/de/translation.json |  4 ++++
 pd/nw/locales/en/translation.json |  4 ++++
 pd/nw/pd_canvas.js                | 16 +++++++++++++++-
 pd/nw/pd_menus.js                 | 12 ++++++++++++
 pd/nw/pdgui.js                    | 23 +++++++++++++++--------
 5 files changed, 50 insertions(+), 9 deletions(-)

diff --git a/pd/nw/locales/de/translation.json b/pd/nw/locales/de/translation.json
index 0dc5423ab..6e8998428 100644
--- a/pd/nw/locales/de/translation.json
+++ b/pd/nw/locales/de/translation.json
@@ -167,6 +167,10 @@
     "zoomreset_tt": "Setze die Anzeige des Patches auf die Originalgröße zurück",
     "zoomoptimal": "Optimale Größe",
     "zoomoptimal_tt": "Wählt die optimale Größe, mit der der gesamte Patch angezeigt werden kann (soweit möglich)",
+    "zoomhoriz": "An Breite anpassen",
+    "zoomhoriz_tt": "Wählt die optimale Größe, um den Patch in voller Breite anzuzeigen",
+    "zoomvert": "An Höhe anpassen",
+    "zoomvert_tt": "Wählt die optimale Größe, um den Patch in voller Höhe anzuzeigen",
     "fullscreen": "Vollbild",
 
     "put": "Hinzufügen",
diff --git a/pd/nw/locales/en/translation.json b/pd/nw/locales/en/translation.json
index 7f4cf14fa..ea02b5377 100644
--- a/pd/nw/locales/en/translation.json
+++ b/pd/nw/locales/en/translation.json
@@ -167,6 +167,10 @@
     "zoomreset_tt": "Reset the zoom to the original level",
     "zoomoptimal": "Optimal Zoom",
     "zoomoptimal_tt": "Change the zoom to the optimal level which makes the entire patch visible (as far as possible)",
+    "zoomhoriz": "Fit to Width",
+    "zoomhoriz_tt": "Change the zoom level to make the patch fit horizontally",
+    "zoomvert": "Fit to Height",
+    "zoomvert_tt": "Change the zoom level to make the patch fit vertically",
     "fullscreen": "Fullscreen",
 
     "put": "Put",
diff --git a/pd/nw/pd_canvas.js b/pd/nw/pd_canvas.js
index 261b0eb43..0b582fd9b 100644
--- a/pd/nw/pd_canvas.js
+++ b/pd/nw/pd_canvas.js
@@ -1189,7 +1189,21 @@ function nw_create_patch_window_menus(gui, w, name) {
     minit(m.view.optimalzoom, {
         enabled: true,
         click: function () {
-            pdgui.gui_canvas_optimal_zoom(name);
+            pdgui.gui_canvas_optimal_zoom(name, 1, 1);
+            pdgui.gui_canvas_get_scroll(name);
+        }
+    });
+    minit(m.view.horizzoom, {
+        enabled: true,
+        click: function () {
+            pdgui.gui_canvas_optimal_zoom(name, 1, 0);
+            pdgui.gui_canvas_get_scroll(name);
+        }
+    });
+    minit(m.view.vertzoom, {
+        enabled: true,
+        click: function () {
+            pdgui.gui_canvas_optimal_zoom(name, 0, 1);
             pdgui.gui_canvas_get_scroll(name);
         }
     });
diff --git a/pd/nw/pd_menus.js b/pd/nw/pd_menus.js
index 0f8778589..9342be984 100644
--- a/pd/nw/pd_menus.js
+++ b/pd/nw/pd_menus.js
@@ -332,6 +332,18 @@ function create_menu(gui, type) {
             modifiers: cmd_or_ctrl + "+alt",
             tooltip: l("menu.zoomoptimal_tt")
 	}));
+	view_menu.append(m.view.horizzoom = new gui.MenuItem({
+            label: l("menu.zoomhoriz"),
+            key: "w",
+            modifiers: cmd_or_ctrl + "+alt",
+            tooltip: l("menu.zoomhoriz_tt")
+	}));
+	view_menu.append(m.view.vertzoom = new gui.MenuItem({
+            label: l("menu.zoomvert"),
+            key: "h",
+            modifiers: cmd_or_ctrl + "+alt",
+            tooltip: l("menu.zoomvert_tt")
+	}));
     }
     view_menu.append(new gui.MenuItem({ type: "separator" }));
     view_menu.append(m.view.fullscreen = new gui.MenuItem({
diff --git a/pd/nw/pdgui.js b/pd/nw/pdgui.js
index 171241810..f88d4c8b6 100644
--- a/pd/nw/pdgui.js
+++ b/pd/nw/pdgui.js
@@ -4647,7 +4647,7 @@ function gui_canvas_get_scroll(cid) {
 
 exports.gui_canvas_get_scroll = gui_canvas_get_scroll;
 
-function do_optimalzoom(cid) {
+function do_optimalzoom(cid, hflag, vflag) {
     // determine an optimal zoom level that makes the entire patch fit within
     // the window
     if (!patchwin[cid]) { return; }
@@ -4662,11 +4662,18 @@ function do_optimalzoom(cid) {
     var zx = 0, zy = 0;
     if (width>0) zx = Math.floor(Math.log(min_width/width)/Math.log(1.2));
     if (height>0) zy = Math.floor(Math.log(min_height/height)/Math.log(1.2));
-    // Optimal zoom is the minimum of the horizontal and vertical zoom
-    // values. This gives us the offset to the current zoom level. We
-    // then need to clamp the resulting new zoom level to the valid
-    // zoom level range of -8..+7.
-    var actz = patchwin[cid].zoomLevel, z = actz+Math.min(zx, zy);
+    // Optimal zoom is the minimum of the horizontal and/or the vertical zoom
+    // values, depending on the h and v flags. This gives us the offset to the
+    // current zoom level. We then need to clamp the resulting new zoom level
+    // to the valid zoom level range of -8..+7.
+    var actz = patchwin[cid].zoomLevel, z = 0;
+    if (hflag && vflag)
+	z = Math.min(zx, zy);
+    else if (hflag)
+	z = zx;
+    else if (vflag)
+	z = zy;
+    z += actz;
     if (z < -8) z = -8; if (z > 7) z = 7;
     //post("bbox: "+width+"x"+height+"+"+x+"+"+y+" window size: "+min_width+"x"+min_height+" current zoom level: "+actz+" optimal zoom level: "+z);
     if (z != actz) {
@@ -4680,9 +4687,9 @@ var optimalzoom_var = {};
 // use a smaller value here, so that we're done before a subsequent
 // call to do_getscroll updates the viewport. XXXREVIEW: Hopefully
 // 100 msec are enough for do_optimalzoom to finish.
-function gui_canvas_optimal_zoom(cid) {
+function gui_canvas_optimal_zoom(cid, h, v) {
     clearTimeout(optimalzoom_var[cid]);
-    optimalzoom_var[cid] = setTimeout(do_optimalzoom, 150, cid);
+    optimalzoom_var[cid] = setTimeout(do_optimalzoom, 150, cid, h, v);
 }
 
 exports.gui_canvas_optimal_zoom = gui_canvas_optimal_zoom;
-- 
GitLab