Java Code Examples for org.eclipse.swt.custom.StyledText#getLocationAtOffset()

The following examples show how to use org.eclipse.swt.custom.StyledText#getLocationAtOffset() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: RenameRefactoringPopup.java    From xtext-eclipse with Eclipse Public License 2.0 6 votes vote down vote up
protected Point computePopupLocation() {
	if (popup == null || popup.isDisposed())
		return null;

	LinkedPosition position = renameLinkedMode.getCurrentLinkedPosition();
	if (position == null)
		return null;
	ISourceViewer viewer = editor.getInternalSourceViewer();
	ITextViewerExtension5 viewer5 = (ITextViewerExtension5) viewer;
	int widgetOffset = viewer5.modelOffset2WidgetOffset(position.offset);

	StyledText textWidget = viewer.getTextWidget();
	Point pos = textWidget.getLocationAtOffset(widgetOffset);
	Point pSize = getExtent();
	pSize.y += HAH + 1;
	pos.x -= HAO;
	pos.y += textWidget.getLineHeight(widgetOffset);
	Point dPos = textWidget.toDisplay(pos);
	Rectangle displayBounds = textWidget.getDisplay().getClientArea();
	Rectangle dPopupRect = Geometry.createRectangle(dPos, pSize);
	Geometry.moveInside(dPopupRect, displayBounds);
	return new Point(dPopupRect.x, dPopupRect.y);
}
 
Example 2
Source File: JDTQuickMenuCreator.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
private Point computeWordStart() {
	ITextSelection selection= (ITextSelection)fEditor.getSelectionProvider().getSelection();
	IRegion textRegion= JavaWordFinder.findWord(fEditor.getViewer().getDocument(), selection.getOffset());
	if (textRegion == null)
		return null;

	IRegion widgetRegion= modelRange2WidgetRange(textRegion);
	if (widgetRegion == null)
		return null;

	int start= widgetRegion.getOffset();

	StyledText styledText= fEditor.getViewer().getTextWidget();
	Point result= styledText.getLocationAtOffset(start);
	result.y+= styledText.getLineHeight(start);

	if (!styledText.getClientArea().contains(result))
		return null;
	return result;
}
 
Example 3
Source File: CommandElementsProvider.java    From APICloud-Studio with GNU General Public License v3.0 5 votes vote down vote up
public Point getCommandElementsPopupLocation()
{
	Object control = textEditor.getAdapter(Control.class);
	if (control instanceof StyledText)
	{
		StyledText textWidget = (StyledText) control;
		int caretOffset = textWidget.getCaretOffset();
		Point locationAtOffset = textWidget.getLocationAtOffset(caretOffset);
		locationAtOffset = textWidget.toDisplay(locationAtOffset.x,
				locationAtOffset.y + textWidget.getLineHeight(caretOffset) + 2);
		return locationAtOffset;
	}
	return null;
}
 
Example 4
Source File: LineBackgroundPainter.java    From APICloud-Studio with GNU General Public License v3.0 4 votes vote down vote up
private Rectangle getLineRectangle(Position position)
{
	if (position == null)
	{
		return null;
	}

	// if the position that is about to be drawn was deleted then we can't
	if (position.isDeleted())
	{
		return null;
	}

	int widgetOffset = 0;
	if (fViewer instanceof ITextViewerExtension5)
	{

		ITextViewerExtension5 extension = (ITextViewerExtension5) fViewer;
		widgetOffset = extension.modelOffset2WidgetOffset(position.getOffset());
		if (widgetOffset == -1)
		{
			return null;
		}
	}
	else
	{

		IRegion visible = fViewer.getVisibleRegion();
		widgetOffset = position.getOffset() - visible.getOffset();
		if (widgetOffset < 0 || visible.getLength() < widgetOffset)
		{
			return null;
		}
	}

	StyledText textWidget = fViewer.getTextWidget();
	// check for https://bugs.eclipse.org/bugs/show_bug.cgi?id=64898
	// this is a guard against the symptoms but not the actual solution
	if (0 <= widgetOffset && widgetOffset <= textWidget.getCharCount())
	{
		Point upperLeft = textWidget.getLocationAtOffset(widgetOffset);
		int width = textWidget.getClientArea().width + textWidget.getHorizontalPixel();
		int height = textWidget.getLineHeight(widgetOffset);
		return new Rectangle(0, upperLeft.y, width, height);
	}

	return null;
}
 
Example 5
Source File: TextVerticalLinesIndentGuide.java    From Pydev with Eclipse Public License 1.0 4 votes vote down vote up
private void computeLine(String string, int firstCharPosition, StyledText styledText, int line, int lineHeight,
        SortedMap<Integer, List<VerticalLinesToDraw>> lineToVerticalLinesToDraw) {
    int lineOffset = -1;

    String spaces = string.substring(0, firstCharPosition);
    int level = 0;
    int whitespacesFound = 0;
    int tabWidthUsed = getTabWidth();
    for (int j = 0; j < firstCharPosition - 1; j++) { //-1 because we don't want to cover for the column where a non whitespace char is.
        char c = spaces.charAt(j);
        if (c == '\t') {
            level++;
            whitespacesFound = 0;
        } else {
            //whitespace (not tab)
            whitespacesFound++;
            if (whitespacesFound % tabWidthUsed == 0) {
                level++;
                whitespacesFound = 0;
            }
        }
        if (level > 0) {
            Point point1;

            if (lineOffset == -1) {
                lineOffset = styledText.getOffsetAtLine(line);
            }
            point1 = styledText.getLocationAtOffset(lineOffset + j + 1);
            int xCoord = point1.x + 3;

            VerticalLinesToDraw verticalLinesToDraw = new VerticalLinesToDraw(xCoord,
                    point1.y, xCoord, point1.y + lineHeight);

            List<VerticalLinesToDraw> lst = lineToVerticalLinesToDraw.get(line);
            if (lst == null) {
                lst = new ArrayList<VerticalLinesToDraw>();
                lineToVerticalLinesToDraw.put(line, lst);
            }
            lst.add(verticalLinesToDraw);

            level--;
        }
    }
}
 
Example 6
Source File: RemoteCursorStrategy.java    From saros with GNU General Public License v2.0 4 votes vote down vote up
/**
 * {@inheritDoc}
 *
 * @param annotation An RemoteCursorAnnotation passed by the {@link AnnotationPainter}
 * @param offset offset of the end of the Selection
 * @param length always 0, will be ignored
 */
@Override
public void draw(
    Annotation annotation, GC gc, StyledText textWidget, int offset, int length, Color color) {
  Point currentCursorPosition = textWidget.getLocationAtOffset(offset);

  // clearing mode
  if (gc == null) {
    /*
     * Redraw the surrounding area of the cursor. Because we draw a line
     * with a width larger than 1, we have to clear the area around the
     * actual coordinates (start a bit more left, and extend a bit to
     * the right).
     */
    textWidget.redraw(
        currentCursorPosition.x - CURSOR_WIDTH / 2,
        currentCursorPosition.y,
        CURSOR_WIDTH + 1,
        textWidget.getLineHeight(),
        false);

    return;
  }

  final Color oldBackground = gc.getBackground();
  final Color oldForeground = gc.getForeground();

  /*
   * Draw the cursor line
   */
  gc.setBackground(color);
  gc.setForeground(color);

  gc.setLineWidth(CURSOR_WIDTH);
  gc.drawLine(
      currentCursorPosition.x,
      currentCursorPosition.y,
      currentCursorPosition.x,
      currentCursorPosition.y + textWidget.getLineHeight());

  // set back the colors like they were before
  gc.setBackground(oldBackground);
  gc.setForeground(oldForeground);
}