From 605721b378c98564a27755a564969b3b247e9e27 Mon Sep 17 00:00:00 2001 From: Konloch Date: Wed, 21 Jul 2021 05:10:40 -0700 Subject: [PATCH] Mouse Wheel Zoom On All Scrollable Windows Plugin Writer, Consoles and the Hex-Viewer can now be zoomed in/out and support font control --- .../gui/components/SearchableJTextArea.java | 51 +++++++++++++++++++ .../components/SearchableRSyntaxTextArea.java | 34 +++++++++---- .../gui/hexviewer/JHexEditor.java | 48 ++++++++++++++--- 3 files changed, 115 insertions(+), 18 deletions(-) diff --git a/src/main/java/the/bytecode/club/bytecodeviewer/gui/components/SearchableJTextArea.java b/src/main/java/the/bytecode/club/bytecodeviewer/gui/components/SearchableJTextArea.java index fae5a3ed..60ffa51b 100644 --- a/src/main/java/the/bytecode/club/bytecodeviewer/gui/components/SearchableJTextArea.java +++ b/src/main/java/the/bytecode/club/bytecodeviewer/gui/components/SearchableJTextArea.java @@ -1,5 +1,6 @@ package the.bytecode.club.bytecodeviewer.gui.components; +import the.bytecode.club.bytecodeviewer.BytecodeViewer; import the.bytecode.club.bytecodeviewer.GlobalHotKeys; import the.bytecode.club.bytecodeviewer.resources.IconResources; import the.bytecode.club.bytecodeviewer.gui.components.listeners.PressKeyListener; @@ -10,7 +11,9 @@ import the.bytecode.club.bytecodeviewer.util.JTextAreaUtils; import javax.swing.*; import java.awt.*; +import java.awt.event.InputEvent; import java.awt.event.KeyEvent; +import java.awt.event.MouseWheelListener; /*************************************************************************** * Bytecode Viewer (BCV) - Java & Android Reverse Engineering Suite * @@ -78,6 +81,19 @@ public class SearchableJTextArea extends JTextArea GlobalHotKeys.keyPressed(keyEvent); })); + + final Font newFont = getFont().deriveFont((float) BytecodeViewer.viewer.getFontSize()); + + //set number-bar font + setFont(newFont); + + SwingUtilities.invokeLater(()-> { + //attach CTRL + Mouse Wheel Zoom + attachCtrlMouseWheelZoom(); + + //set text font + setFont(newFont); + }); } public void search(String search, boolean forwardSearchDirection, boolean caseSensitiveSearch) @@ -90,6 +106,41 @@ public class SearchableJTextArea extends JTextArea JTextAreaUtils.highlight(this, pattern, caseSensitiveSearch); } + public void attachCtrlMouseWheelZoom() + { + //get the existing scroll event + MouseWheelListener ogListener = scrollPane.getMouseWheelListeners().length > 0 ? + scrollPane.getMouseWheelListeners()[0] : null; + + //remove the existing event + if(ogListener != null) + scrollPane.removeMouseWheelListener(ogListener); + + //add a new event + scrollPane.addMouseWheelListener(e -> + { + if (getText().isEmpty()) + return; + + if ((e.getModifiersEx() & InputEvent.CTRL_DOWN_MASK) != 0) + { + Font font = getFont(); + int size = font.getSize(); + + if (e.getWheelRotation() > 0) //Up + setFont(new Font(font.getName(), font.getStyle(), --size >= 2 ? --size : 2)); + else //Down + setFont(new Font(font.getName(), font.getStyle(), ++size)); + + e.consume(); + } + else if(ogListener != null) + { + ogListener.mouseWheelMoved(e); + } + }); + } + public JScrollPane getScrollPane() { return scrollPane; diff --git a/src/main/java/the/bytecode/club/bytecodeviewer/gui/components/SearchableRSyntaxTextArea.java b/src/main/java/the/bytecode/club/bytecodeviewer/gui/components/SearchableRSyntaxTextArea.java index 91b31e21..33063924 100644 --- a/src/main/java/the/bytecode/club/bytecodeviewer/gui/components/SearchableRSyntaxTextArea.java +++ b/src/main/java/the/bytecode/club/bytecodeviewer/gui/components/SearchableRSyntaxTextArea.java @@ -2,6 +2,7 @@ package the.bytecode.club.bytecodeviewer.gui.components; import org.fife.ui.rsyntaxtextarea.RSyntaxTextArea; import org.fife.ui.rtextarea.RTextScrollPane; +import the.bytecode.club.bytecodeviewer.BytecodeViewer; import the.bytecode.club.bytecodeviewer.Configuration; import the.bytecode.club.bytecodeviewer.GlobalHotKeys; import the.bytecode.club.bytecodeviewer.resources.IconResources; @@ -58,15 +59,7 @@ public class SearchableRSyntaxTextArea extends RSyntaxTextArea public SearchableRSyntaxTextArea() { - if(Configuration.lafTheme == LAFTheme.DARK || Configuration.lafTheme == LAFTheme.SOLARIZED_DARK) - { - //this fixes the white border on the jScrollBar panes - scrollPane.getHorizontalScrollBar().setBackground(darkScrollBackground); - scrollPane.getHorizontalScrollBar().setForeground(darkScrollForeground); - scrollPane.getVerticalScrollBar().setBackground(darkScrollBackground); - scrollPane.getVerticalScrollBar().setForeground(darkScrollForeground); - } - else if(Configuration.lafTheme == LAFTheme.HIGH_CONTRAST_DARK) + if(Configuration.lafTheme == LAFTheme.HIGH_CONTRAST_DARK) { //this fixes the white border on the jScrollBar panes scrollPane.getHorizontalScrollBar().setBackground(blackScrollBackground); @@ -74,6 +67,14 @@ public class SearchableRSyntaxTextArea extends RSyntaxTextArea scrollPane.getVerticalScrollBar().setBackground(blackScrollBackground); scrollPane.getVerticalScrollBar().setForeground(blackScrollForeground); } + else if(Configuration.lafTheme.isDark()) + { + //this fixes the white border on the jScrollBar panes + scrollPane.getHorizontalScrollBar().setBackground(darkScrollBackground); + scrollPane.getHorizontalScrollBar().setForeground(darkScrollForeground); + scrollPane.getVerticalScrollBar().setBackground(darkScrollBackground); + scrollPane.getVerticalScrollBar().setForeground(darkScrollForeground); + } setAntiAliasingEnabled(true); @@ -113,8 +114,19 @@ public class SearchableRSyntaxTextArea extends RSyntaxTextArea GlobalHotKeys.keyPressed(keyEvent); })); - //attach CTRL + Mouse Wheel Zoom - SwingUtilities.invokeLater(this::attachCtrlMouseWheelZoom); + final Font newFont = getFont().deriveFont((float) BytecodeViewer.viewer.getFontSize()); + + //set number-bar font + setFont(newFont); + + SwingUtilities.invokeLater(()-> { + //attach CTRL + Mouse Wheel Zoom + attachCtrlMouseWheelZoom(); + + //set text font + setFont(newFont); + }); + } public void search(String search, boolean forwardSearchDirection, boolean caseSensitiveSearch) diff --git a/src/main/java/the/bytecode/club/bytecodeviewer/gui/hexviewer/JHexEditor.java b/src/main/java/the/bytecode/club/bytecodeviewer/gui/hexviewer/JHexEditor.java index c5c11c34..db381ba5 100644 --- a/src/main/java/the/bytecode/club/bytecodeviewer/gui/hexviewer/JHexEditor.java +++ b/src/main/java/the/bytecode/club/bytecodeviewer/gui/hexviewer/JHexEditor.java @@ -9,13 +9,7 @@ import java.awt.Font; import java.awt.FontMetrics; import java.awt.Graphics; import java.awt.Rectangle; -import java.awt.event.AdjustmentEvent; -import java.awt.event.AdjustmentListener; -import java.awt.event.FocusEvent; -import java.awt.event.FocusListener; -import java.awt.event.KeyEvent; -import java.awt.event.MouseWheelEvent; -import java.awt.event.MouseWheelListener; +import java.awt.event.*; import javax.swing.*; /** @@ -82,6 +76,9 @@ public class JHexEditor extends JPanel implements FocusListener, AdjustmentListe this.setLayout(new BorderLayout(1, 1)); this.add(panel, BorderLayout.CENTER); + + //attach CTRL + Mouse Wheel Zoom + SwingUtilities.invokeLater(this::attachCtrlMouseWheelZoom); } @Override @@ -103,6 +100,43 @@ public class JHexEditor extends JPanel implements FocusListener, AdjustmentListe sb.setValueIsAdjusting(true); super.paint(g); } + + public void attachCtrlMouseWheelZoom() + { + //get the existing scroll event + MouseWheelListener ogListener = getMouseWheelListeners().length > 0 ? + getMouseWheelListeners()[0] : null; + + //remove the existing event + if(ogListener != null) + removeMouseWheelListener(ogListener); + + //add a new event + addMouseWheelListener(e -> + { + if ((e.getModifiersEx() & InputEvent.CTRL_DOWN_MASK) != 0) + { + int size = font.getSize(); + + Font newFont; + if (e.getWheelRotation() > 0) //Up + newFont = new Font(font.getName(), font.getStyle(), --size >= 2 ? --size : 2); + else //Down + newFont = new Font(font.getName(), font.getStyle(), ++size); + + setFont(newFont); + hex.setFont(newFont); + ascii.setFont(newFont); + font = newFont; + + e.consume(); + } + else if(ogListener != null) + { + ogListener.mouseWheelMoved(e); + } + }); + } protected int getInicio() { return inicio;