Browse Source

Adds methods to ask for insertion indexes or character indexes in the textpane

Fixes issue 3836: Link hitboxing needs to ignore insertion indexing and use character indexing

Change-Id: I24c2cadccc3a95125b7d714602094de377d191d2
Reviewed-on: http://gerrit.dmdirc.com/952
Automatic-Compile: DMDirc Local Commits <dmdirc@googlemail.com>
Reviewed-by: Shane Mc Cormack <shane@dmdirc.com>
tags/0.6.3^0
Greboid 14 years ago
parent
commit
6592de93c8

+ 1
- 1
src/com/dmdirc/addons/ui_swing/components/frames/TextFrame.java View File

@@ -890,7 +890,7 @@ public abstract class TextFrame extends JInternalFrame implements Window,
890 890
         SwingUtilities.convertPointFromScreen(point, this);
891 891
         if (e.getSource() == getTextPane() && point != null) {
892 892
             final LineInfo lineInfo = getTextPane().getClickPosition(textPane.
893
-                    getMousePosition());
893
+                    getMousePosition(), false);
894 894
             final ClickType clickType = getTextPane().getClickType(lineInfo);
895 895
             final String attribute = (String) getTextPane().
896 896
                     getAttributeValueAtPoint(lineInfo);

+ 16
- 1
src/com/dmdirc/addons/ui_swing/textpane/TextPane.java View File

@@ -200,7 +200,22 @@ public final class TextPane extends JComponent implements AdjustmentListener,
200 200
      * @return line number, line part, position in whole line
201 201
      */
202 202
     public LineInfo getClickPosition(final Point point) {
203
-        return canvas.getClickPosition(point);
203
+        return canvas.getClickPosition(point, true);
204
+    }
205
+
206
+    /**
207
+     *
208
+     * Returns the line information from a mouse click inside the textpane.
209
+     *
210
+     * @param point mouse position
211
+     * @param selection Are we selecting text?
212
+     *
213
+     * @return line number, line part, position in whole line
214
+     *
215
+     * @since 0.6.3
216
+     */
217
+    public LineInfo getClickPosition(final Point point, final boolean selection) {
218
+        return canvas.getClickPosition(point, selection);
204 219
     }
205 220
 
206 221
     /**

+ 12
- 7
src/com/dmdirc/addons/ui_swing/textpane/TextPaneCanvas.java View File

@@ -443,7 +443,7 @@ class TextPaneCanvas extends JPanel implements MouseInputListener,
443 443
         final int start;
444 444
         final int end;
445 445
 
446
-        final LineInfo lineInfo = getClickPosition(getMousePosition());
446
+        final LineInfo lineInfo = getClickPosition(getMousePosition(), true);
447 447
 
448 448
         if (lineInfo.getLine() != -1) {
449 449
             clickedText = document.getLine(lineInfo.getLine()).getText();
@@ -686,7 +686,7 @@ class TextPaneCanvas extends JPanel implements MouseInputListener,
686 686
 
687 687
     /** Checks for a link under the cursor and sets appropriately. */
688 688
     private void checkForLink() {
689
-        final LineInfo lineInfo = getClickPosition(getMousePosition());
689
+        final LineInfo lineInfo = getClickPosition(getMousePosition(), false);
690 690
 
691 691
         if (lineInfo.getLine() != -1 && document.getLine(lineInfo.getLine()) !=
692 692
                 null) {
@@ -752,7 +752,7 @@ class TextPaneCanvas extends JPanel implements MouseInputListener,
752 752
                             bounds.getHeight() - DOUBLE_SIDE_PADDING - 1);
753 753
                 }
754 754
             }
755
-            final LineInfo info = getClickPosition(point);
755
+            final LineInfo info = getClickPosition(point, true);
756 756
             if (info.getLine() == -1 && info.getPart() == -1 && contains(point)) {
757 757
                 info.setLine(0);
758 758
                 info.setPart(0);
@@ -776,10 +776,11 @@ class TextPaneCanvas extends JPanel implements MouseInputListener,
776 776
      * Returns the line information from a mouse click inside the textpane.
777 777
      *
778 778
      * @param point mouse position
779
+     * @param selection Are we selecting text?
779 780
      *
780 781
      * @return line number, line part, position in whole line
781 782
      */
782
-    public LineInfo getClickPosition(final Point point) {
783
+    public LineInfo getClickPosition(final Point point, final boolean selection) {
783 784
         int lineNumber = -1;
784 785
         int linePart = -1;
785 786
         int pos = 0;
@@ -793,7 +794,7 @@ class TextPaneCanvas extends JPanel implements MouseInputListener,
793 794
             }
794 795
 
795 796
             pos = getHitPosition(lineNumber, linePart, (int) point.getX(),
796
-                    (int) point.getY());
797
+                    (int) point.getY(), selection);
797 798
         }
798 799
 
799 800
         return new LineInfo(lineNumber, linePart, pos);
@@ -810,7 +811,7 @@ class TextPaneCanvas extends JPanel implements MouseInputListener,
810 811
      * @return Hit position
811 812
      */
812 813
     private int getHitPosition(final int lineNumber, final int linePart,
813
-            final int x, final int y) {
814
+            final int x, final int y, final boolean selection) {
814 815
         int pos = 0;
815 816
 
816 817
         for (Map.Entry<Rectangle, TextLayout> entry : positions.entrySet()) {
@@ -821,7 +822,11 @@ class TextPaneCanvas extends JPanel implements MouseInputListener,
821 822
                         linePart) {
822 823
                     final TextHitInfo hit = entry.getValue().hitTestChar(x -
823 824
                             DOUBLE_SIDE_PADDING, y);
824
-                    pos += hit.getInsertionIndex();
825
+                    if (selection) {
826
+                        pos += hit.getInsertionIndex();
827
+                    } else {
828
+                        pos += hit.getCharIndex();
829
+                    }
825 830
                 }
826 831
             }
827 832
         }

Loading…
Cancel
Save