Skip to content

Commit

Permalink
Convert to hidpi API added in Java 9
Browse files Browse the repository at this point in the history
  • Loading branch information
bobbylight committed Nov 21, 2024
1 parent b4870e6 commit f4d0f78
Show file tree
Hide file tree
Showing 13 changed files with 101 additions and 127 deletions.
17 changes: 7 additions & 10 deletions RSyntaxTextArea/src/main/java/org/fife/print/RPrintUtilities.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,7 @@
*/
package org.fife.print;

import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.*;
import java.awt.print.PageFormat;
import java.awt.print.Printable;

Expand Down Expand Up @@ -368,7 +365,7 @@ public static int printDocumentWordWrap(Graphics g, JTextComponent textComponent
fm = g.getFontMetrics();
int fontHeight = fm.getHeight();

final int lineLengthInPixels = (int)pageFormat.getImageableWidth();
final double lineLengthInPixels = pageFormat.getImageableWidth();
final int maxLinesPerPage = (int)pageFormat.getImageableHeight() / fontHeight;

final int startingLineNumber = maxLinesPerPage * pageIndex;
Expand Down Expand Up @@ -416,7 +413,7 @@ public static int printDocumentWordWrap(Graphics g, JTextComponent textComponent
currentLineSeg = removeEndingWhitespace(currentLineSeg);

// Figure out how long the line is, in pixels.
int currentLineLengthInPixels = Utilities.getTabbedTextWidth(currentLineSeg, fm, 0, tabExpander, 0);
float currentLineLengthInPixels = Utilities.getTabbedTextWidth(currentLineSeg, fm, 0f, tabExpander, 0);

//System.err.println("'" + currentLineSeg + "' - " + currentLineLengthInPixels + "/" +
// LINE_LENGTH_IN_PIXELS);
Expand Down Expand Up @@ -470,7 +467,7 @@ public static int printDocumentWordWrap(Graphics g, JTextComponent textComponent
return Printable.NO_SUCH_PAGE;
}
currentLineLengthInPixels = Utilities.
getTabbedTextWidth(currentLineSeg, fm, 0, tabExpander, 0);
getTabbedTextWidth(currentLineSeg, fm, 0f, tabExpander, 0);
} while (currentLineLengthInPixels <= lineLengthInPixels);
currentPos--;

Expand All @@ -488,7 +485,7 @@ public static int printDocumentWordWrap(Graphics g, JTextComponent textComponent
return Printable.NO_SUCH_PAGE;
}

currentLineLengthInPixels = Utilities.getTabbedTextWidth(currentLineSeg, fm, 0, tabExpander, 0);
currentLineLengthInPixels = Utilities.getTabbedTextWidth(currentLineSeg, fm, 0f, tabExpander, 0);
} // End of while (currentLineLengthInPixels > LINE_LENGTH_IN_PIXELS).

startingOffset += currentPos; // Where to start (offset from line's start), since this line wraps.
Expand All @@ -497,8 +494,8 @@ public static int printDocumentWordWrap(Graphics g, JTextComponent textComponent

numPrintedLines++;
if (numPrintedLines>startingLineNumber) {
//g.drawString(currentLineSeg.toString(), xOffset,y);
Utilities.drawTabbedText(currentLineSeg, xOffset,y, g, tabExpander, 0);
Graphics2D g2d = (Graphics2D)g;
Utilities.drawTabbedText(currentLineSeg, (float)xOffset,y, g2d, tabExpander, 0);
y += fontHeight;
if (numPrintedLines==startingLineNumber+maxLinesPerPage) {
return Printable.PAGE_EXISTS;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;
import javax.swing.Icon;
import javax.swing.text.BadLocationException;
import javax.swing.text.Document;
Expand Down Expand Up @@ -203,8 +202,7 @@ private void paintComponentWrapped(Graphics g) {
//boolean currentLineHighlighted = textArea.getHighlightCurrentLine();
Document doc = textArea.getDocument();
Element root = doc.getDefaultRootElement();
int topPosition = textArea.viewToModel(
new Point(visibleRect.x,visibleRect.y));
int topPosition = textArea.viewToModel2D(visibleRect.getLocation());
int topLine = root.getElementIndex(topPosition);

int topY = visibleRect.y;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@

import java.awt.Color;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.geom.Rectangle2D;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.net.URL;
Expand Down Expand Up @@ -433,7 +433,7 @@ public ToolTipInfo getToolTipText(MouseEvent e) {
if (noticeHighlightPairs!=null) {

Point p = e.getPoint();
int pos = textArea.viewToModel(p);
int pos = textArea.viewToModel2D(p);

for (NoticeHighlightPair pair : noticeHighlightPairs) {
ParserNotice notice = pair.notice;
Expand Down Expand Up @@ -543,8 +543,7 @@ private boolean noticeContainsPosition(ParserNotice notice, int offs){
* @return Whether the parser notice actually contains the specified point
* in the view.
*/
private boolean noticeContainsPointInView(ParserNotice notice,
Point p) {
private boolean noticeContainsPointInView(ParserNotice notice, Point p) {

try {

Expand All @@ -567,20 +566,21 @@ private boolean noticeContainsPointInView(ParserNotice notice,
end = elem.getEndOffset() - 1;
}

Rectangle r1 = textArea.modelToView(start);
Rectangle r2 = textArea.modelToView(end);
if (r1.y!=r2.y) {
Rectangle2D r1 = textArea.modelToView2D(start);
Rectangle2D r2 = textArea.modelToView2D(end);
if (r1.getY() != r2.getY()) {
// If the notice spans multiple lines, give them the benefit
// of the doubt. This is only "wrong" if the user is in empty
// space "to the right" of the error marker when it ends at the
// end of a line anyway.
return true;
}

r1.y--; // Be a tiny bit lenient.
r1.height += 2; // Ditto
return p.x>=r1.x && p.x<(r2.x+r2.width) &&
p.y>=r1.y && p.y<(r1.y+r1.height);
// Be a tiny bit lenient with y/height
r1.setRect(r1.getX(), r1.getY() - 1, r1.getWidth(), r1.getHeight() + 2);

return p.x >= r1.getX() && p.x < (r2.getX() + r2.getWidth()) &&
p.y >= r1.getY() && p.y < (r1.getY() + r1.getHeight());

} catch (BadLocationException ble) { // Never occurs
// Give them the benefit of the doubt, should 99% of the time be
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import java.awt.*;
import java.awt.datatransfer.Clipboard;
import java.awt.event.*;
import java.awt.geom.Point2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.net.MalformedURLException;
Expand Down Expand Up @@ -848,10 +849,10 @@ protected final void doBracketMatching() {
(bracketInfo.y!=lastBracketMatchPos ||
bracketInfo.x!=lastCaretBracketPos)) {
try {
match = modelToView(bracketInfo.y);
match = modelToView2D(bracketInfo.y).getBounds();
if (match!=null) { // Happens if we're not yet visible
if (getPaintMatchedBracketPair()) {
dotRect = modelToView(bracketInfo.x);
dotRect = modelToView2D(bracketInfo.x).getBounds();
}
else {
dotRect = null;
Expand Down Expand Up @@ -1604,7 +1605,7 @@ public boolean getPaintTabLines() {
boolean getPaintTokenBackgrounds(int line, float y) {
//System.out.println(y + ", " + getCurrentCaretY() + "-" + (getCurrentCaretY() + getLineHeight()));
int iy = (int)y;
int curCaretY = getCurrentCaretY();
double curCaretY = getCurrentCaretY();
return iy<curCaretY || iy>=curCaretY+getLineHeight() ||
!getHighlightCurrentLine();
}
Expand Down Expand Up @@ -2189,7 +2190,7 @@ public boolean isWhitespaceVisible() {
* @param offs The position in the model.
* @return The token, or <code>null</code> if no token is at that
* position.
* @see #viewToToken(Point)
* @see #viewToToken(Point2D)
*/
public Token modelToToken(int offs) {
if (offs>=0) {
Expand Down Expand Up @@ -3354,14 +3355,14 @@ public void undoLastAction() {
* position.
* @see #modelToToken(int)
*/
public Token viewToToken(Point p) {
public Token viewToToken(Point2D p) {
/*
* TODO: This is a little inefficient. This should convert view
* coordinates to the underlying token (if any). The way things currently
* are, we're calling getTokenListForLine() twice (once in viewToModel()
* and once here).
*/
return modelToToken(viewToModel(p));
return modelToToken(viewToModel2D(p));
}

/**
Expand Down Expand Up @@ -3563,7 +3564,7 @@ public void mouseMoved(MouseEvent e) {
c2 = Cursor.getPredefinedCursor(Cursor.HAND_CURSOR);
}
else if (t!=null && linkGenerator!=null) {
int offs = viewToModel(e.getPoint());
int offs = viewToModel2D(e.getPoint());
LinkGeneratorResult newResult = linkGenerator.
isLinkAtOffset(RSyntaxTextArea.this, offs);
if (newResult!=null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.geom.Rectangle2D;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

Expand Down Expand Up @@ -169,11 +170,11 @@ public void dispose() {
void fixSize() {

Dimension d;
Rectangle r;
Rectangle2D r;
try {

// modelToView call is required for this hack, never remove!
r = textArea.modelToView(textArea.getDocument().getLength()-1);
r = textArea.modelToView2D(textArea.getDocument().getLength()-1);

// Ensure the text area doesn't start out too tall or wide.
d = textArea.getPreferredSize();
Expand All @@ -191,9 +192,9 @@ void fixSize() {

// if the new textArea width causes our text to wrap, we must
// compute a new preferred size to get all our physical lines.
r = textArea.modelToView(textArea.getDocument().getLength()-1);
if (r.y+r.height>d.height) {
d.height = r.y + r.height + 5;
r = textArea.modelToView2D(textArea.getDocument().getLength()-1);
if (r.getMaxY() > d.height) {
d.height = (int)r.getMaxY() + 5;
if(ft.getMaxSize() != null) {
d.height = Math.min(d.height, maxWindowH);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ private Fold findOpenFoldClosestTo(Point p) {

RSyntaxTextArea rsta = (RSyntaxTextArea)textArea;
if (rsta.isCodeFoldingEnabled()) { // Should always be true
int offs = rsta.viewToModel(p); // TODO: Optimize me
int offs = rsta.viewToModel2D(p); // TODO: Optimize me
if (offs>-1) {
try {
int line = rsta.getLineOfOffset(offs);
Expand Down Expand Up @@ -357,7 +357,7 @@ public String getToolTipText(MouseEvent e) {
RSyntaxTextArea rsta = (RSyntaxTextArea)textArea;
if (rsta.isCodeFoldingEnabled()) {
FoldManager fm = rsta.getFoldManager();
int pos = rsta.viewToModel(new Point(0, e.getY()));
int pos = rsta.viewToModel2D(new Point(0, e.getY()));
if (pos>=0) { // Not -1
int line;
try {
Expand Down Expand Up @@ -601,8 +601,7 @@ private void paintComponentWrapped(Graphics g) {
View v = ui.getRootView(textArea).getView(0);
Document doc = textArea.getDocument();
Element root = doc.getDefaultRootElement();
int topPosition = textArea.viewToModel(
new Point(visibleRect.x,visibleRect.y));
int topPosition = textArea.viewToModel2D(visibleRect.getLocation());
int topLine = root.getElementIndex(topPosition);
int cellHeight = textArea.getLineHeight();
FoldManager fm = ((RSyntaxTextArea)textArea).getFoldManager();
Expand Down Expand Up @@ -712,7 +711,7 @@ private int rowAtPoint(Point p) {
int line = 0;

try {
int offs = textArea.viewToModel(p);
int offs = textArea.viewToModel2D(p);
if (offs>-1) {
line = textArea.getLineOfOffset(offs);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -507,7 +507,7 @@ public int getSpacingBetweenLineNumbersAndFoldIndicator() {
*/
public GutterIconInfo[] getTrackingIcons(Point p)
throws BadLocationException {
int offs = textArea.viewToModel(new Point(0, p.y));
int offs = textArea.viewToModel2D(new Point(0, p.y));
int line = textArea.getLineOfOffset(offs);
return iconArea.getTrackingIcons(line);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Insets;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.geom.Point2D;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
Expand Down Expand Up @@ -552,8 +552,8 @@ private void paintComponentWrapped(Graphics g) {
Document doc = textArea.getDocument();
Element root = doc.getDefaultRootElement();
int lineCount = root.getElementCount();
int topPosition = textArea.viewToModel(
new Point(visibleRect.x,visibleRect.y));
int topPosition = textArea.viewToModel2D(
new Point2D.Double(visibleRect.x, visibleRect.y));
int topLine = root.getElementIndex(topPosition);

// Compute the y at which to begin painting text, taking into account
Expand Down Expand Up @@ -857,8 +857,8 @@ public void updateUI() {
* @return The corresponding line in the editor.
* @throws BadLocationException ble If an error occurs.
*/
private int viewToModelLine(Point p) throws BadLocationException {
int offs = textArea.viewToModel(p);
private int viewToModelLine(Point2D p) throws BadLocationException {
int offs = textArea.viewToModel2D(p);
return offs>-1 ? textArea.getLineOfOffset(offs) : -1;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ public void mouseClicked(MouseEvent e) {
@Override
public void mouseDragged(MouseEvent e) {
if (mouseDragStartOffset>-1) {
int pos = textArea.viewToModel(new Point(0, e.getY()));
int pos = textArea.viewToModel2D(new Point(0, e.getY()));
if (pos>=0) { // Not -1
textArea.setCaretPosition(mouseDragStartOffset);
textArea.moveCaretPosition(pos);
Expand Down Expand Up @@ -323,7 +323,7 @@ public void mousePressed(MouseEvent e) {
return;
}
if (e.getButton()==MouseEvent.BUTTON1) {
int pos = textArea.viewToModel(new Point(0, e.getY()));
int pos = textArea.viewToModel2D(new Point(0, e.getY()));
if (pos>=0) { // Not -1
textArea.setCaretPosition(pos);
}
Expand Down Expand Up @@ -521,8 +521,7 @@ private void paintWrappedLineNumbers(Graphics g, Rectangle visibleRect) {
Document doc = textArea.getDocument();
Element root = doc.getDefaultRootElement();
int lineCount = root.getElementCount();
int topPosition = textArea.viewToModel(
new Point(visibleRect.x,visibleRect.y));
int topPosition = textArea.viewToModel2D(visibleRect.getLocation());
int topLine = root.getElementIndex(topPosition);
FoldManager fm = null;
if (textArea instanceof RSyntaxTextArea) {
Expand Down
Loading

0 comments on commit f4d0f78

Please sign in to comment.