Search in sources :

Example 31 with ITextViewerExtension5

use of org.eclipse.jface.text.ITextViewerExtension5 in project eclipse.platform.text by eclipse.

the class LineNumberRulerColumn method handleMouseScrolled.

/**
 * Handles mouse scrolled events on the ruler by forwarding them to the text widget.
 *
 * @param e the mouse event
 * @since 3.10
 */
void handleMouseScrolled(MouseEvent e) {
    if (fCachedTextViewer instanceof ITextViewerExtension5) {
        ITextViewerExtension5 extension = (ITextViewerExtension5) fCachedTextViewer;
        StyledText textWidget = fCachedTextViewer.getTextWidget();
        int topIndex = textWidget.getTopIndex();
        int newTopIndex = Math.max(0, topIndex - e.count);
        fCachedTextViewer.setTopIndex(extension.widgetLine2ModelLine(newTopIndex));
    } else {
        int topIndex = fCachedTextViewer.getTopIndex();
        int newTopIndex = Math.max(0, topIndex - e.count);
        fCachedTextViewer.setTopIndex(newTopIndex);
    }
}
Also used : StyledText(org.eclipse.swt.custom.StyledText) ITextViewerExtension5(org.eclipse.jface.text.ITextViewerExtension5) Point(org.eclipse.swt.graphics.Point)

Example 32 with ITextViewerExtension5

use of org.eclipse.jface.text.ITextViewerExtension5 in project eclipse.platform.text by eclipse.

the class OverviewRuler method toLineNumbers.

/**
 * Translates a given y-coordinate of this ruler into the corresponding
 * document lines. The number of lines depends on the concrete scaling
 * given as the ration between the height of this ruler and the length
 * of the document.
 *
 * @param y_coordinate the y-coordinate
 * @param annotationRect <code>true</code> to only consider the position of a drawn annotation rectangle,
 * 	<code>false</code> to consider the whole line
 * @return the corresponding document lines as {firstLine, lastLine}, or {-1, -1} if no lines correspond to the y-coordinate
 */
private int[] toLineNumbers(int y_coordinate, boolean annotationRect) {
    // this is the inverse of #computeY(int, WidgetInfos)
    WidgetInfos infos = new WidgetInfos(fTextViewer.getTextWidget(), fCanvas);
    if (y_coordinate >= infos.writable || y_coordinate >= infos.bounds.height || y_coordinate < 0)
        return new int[] { -1, -1 };
    if (annotationRect && ANNOTATION_HEIGHT >= infos.bounds.height / infos.maxLines)
        annotationRect = false;
    int[] lines = new int[2];
    int[] pixels;
    int pixelEnd = Math.min(infos.bounds.height, y_coordinate + ANNOTATION_HEIGHT / 2 + 1);
    if (annotationRect) {
        pixels = new int[] { pixelEnd };
    } else {
        int pixelStart = Math.max(y_coordinate - ANNOTATION_HEIGHT / 2 + 1, 0);
        pixels = new int[] { pixelStart, pixelEnd };
    }
    if (infos.bounds.height > infos.writable || infos.invisibleLines <= 0) {
        // yy = Math.max(0, (2 * startLine + 1) * infos.writable / (infos.maxLines * 2) - infos.bounds.y);
        for (int i = 0; i < pixels.length; i++) lines[i] = (int) ((pixels[i] + infos.bounds.y) * infos.maxLines / (double) infos.writable);
        if (DEBUG_TO_DOCUMENT_LINE_NUMBER)
            // $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
            System.out.println("static y: " + y_coordinate + " => [" + lines[0] + ", " + lines[1] + "]");
    } else if (y_coordinate < infos.thumbHeight / 2) {
        // yy= (int) (startLine * infos.thumbHeight / infos.visibleLines);
        for (int i = 0; i < pixels.length; i++) lines[i] = (int) (pixels[i] * infos.visibleLines / infos.thumbHeight);
        if (DEBUG_TO_DOCUMENT_LINE_NUMBER)
            // $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
            System.out.println("start  y: " + y_coordinate + " => [" + lines[0] + ", " + lines[1] + "]");
    } else if (infos.bounds.height - 1 - infos.thumbHeight / 2 < y_coordinate) {
        // yy= (int) (infos.bounds.height-1 - (double)infos.thumbHeight / 2 + (startLine - (infos.maxLines - infos.visibleLines / 2) + 1) * infos.thumbHeight / infos.visibleLines);
        for (int i = 0; i < pixels.length; i++) lines[i] = (int) ((pixels[i] - (infos.bounds.height - 1) + (double) infos.thumbHeight / 2) * infos.visibleLines / infos.thumbHeight - 1 + (infos.maxLines - infos.visibleLines / 2));
        if (DEBUG_TO_DOCUMENT_LINE_NUMBER)
            // $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
            System.out.println("end    y: " + y_coordinate + " => [" + lines[0] + ", " + lines[1] + "]");
    } else {
        // yy= (int) ((double)infos.thumbHeight/2 + (startLine + 1 - infos.visibleLines / 2) * (infos.bounds.height-1 - infos.thumbHeight) / infos.invisibleLines);
        for (int i = 0; i < pixels.length; i++) lines[i] = (int) ((pixels[i] - (double) infos.thumbHeight / 2) * infos.invisibleLines / (infos.bounds.height - 1 - infos.thumbHeight) - 1 + infos.visibleLines / 2);
        if (DEBUG_TO_DOCUMENT_LINE_NUMBER)
            // $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
            System.out.println("middle y: " + y_coordinate + " => [" + lines[0] + ", " + lines[1] + "]");
    }
    if (y_coordinate < ANNOTATION_HEIGHT && y_coordinate < infos.bounds.y)
        lines[0] = 0;
    else if (lines[0] < 0)
        lines[0] = 0;
    if (annotationRect) {
        int y0 = computeY(lines[0], infos);
        if (y_coordinate < y0 || y0 + ANNOTATION_HEIGHT < y_coordinate) {
            lines[0] = -1;
            lines[1] = -1;
            return lines;
        } else {
            lines[1] = lines[0];
        }
    }
    if (lines[1] > infos.maxLines)
        lines[1] = infos.maxLines;
    if (fTextViewer instanceof ITextViewerExtension5) {
        ITextViewerExtension5 extension = (ITextViewerExtension5) fTextViewer;
        lines[0] = extension.widgetLine2ModelLine(lines[0]);
        lines[1] = extension.widgetLine2ModelLine(lines[1]);
    } else {
        try {
            IRegion visible = fTextViewer.getVisibleRegion();
            int lineNumber = fTextViewer.getDocument().getLineOfOffset(visible.getOffset());
            lines[0] += lineNumber;
            lines[1] += lineNumber;
        } catch (BadLocationException x) {
        }
    }
    if (DEBUG_TO_DOCUMENT_LINE_NUMBER)
        // $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
        System.out.println("result: [" + lines[0] + ", " + lines[1] + "]");
    return lines;
}
Also used : ITextViewerExtension5(org.eclipse.jface.text.ITextViewerExtension5) Point(org.eclipse.swt.graphics.Point) IRegion(org.eclipse.jface.text.IRegion) BadLocationException(org.eclipse.jface.text.BadLocationException)

Aggregations

ITextViewerExtension5 (org.eclipse.jface.text.ITextViewerExtension5)32 IRegion (org.eclipse.jface.text.IRegion)22 Point (org.eclipse.swt.graphics.Point)21 BadLocationException (org.eclipse.jface.text.BadLocationException)11 Region (org.eclipse.jface.text.Region)10 IDocument (org.eclipse.jface.text.IDocument)9 StyledText (org.eclipse.swt.custom.StyledText)9 Position (org.eclipse.jface.text.Position)5 Rectangle (org.eclipse.swt.graphics.Rectangle)5 ITextViewer (org.eclipse.jface.text.ITextViewer)2 ISourceViewer (org.eclipse.jface.text.source.ISourceViewer)2 GC (org.eclipse.swt.graphics.GC)2 Image (org.eclipse.swt.graphics.Image)2 Entry (java.util.Map.Entry)1 IFindReplaceTargetExtension (org.eclipse.jface.text.IFindReplaceTargetExtension)1 ITextSelection (org.eclipse.jface.text.ITextSelection)1 ITextViewerExtension (org.eclipse.jface.text.ITextViewerExtension)1 IAnnotationModel (org.eclipse.jface.text.source.IAnnotationModel)1 ILineRange (org.eclipse.jface.text.source.ILineRange)1 LineRange (org.eclipse.jface.text.source.LineRange)1