From e48c0ba2787d996c9879df0ef096f792c6928d2f Mon Sep 17 00:00:00 2001 From: Michael Scharf Date: Thu, 11 Oct 2007 16:42:39 +0000 Subject: [PATCH] bug 205879: [terminal] clicking into the terminal creates a selection of one character https://bugs.eclipse.org/bugs/show_bug.cgi?id=205879 The user has to drag at least one character to make a selection --- .../terminal/textcanvas/ITextCanvasModel.java | 37 +++++++------- .../terminal/textcanvas/TextCanvas.java | 51 +++++++++++++------ 2 files changed, 55 insertions(+), 33 deletions(-) diff --git a/terminal/org.eclipse.tm.terminal/src/org/eclipse/tm/internal/terminal/textcanvas/ITextCanvasModel.java b/terminal/org.eclipse.tm.terminal/src/org/eclipse/tm/internal/terminal/textcanvas/ITextCanvasModel.java index bb2eac1599b..75d9ef3a430 100644 --- a/terminal/org.eclipse.tm.terminal/src/org/eclipse/tm/internal/terminal/textcanvas/ITextCanvasModel.java +++ b/terminal/org.eclipse.tm.terminal/src/org/eclipse/tm/internal/terminal/textcanvas/ITextCanvasModel.java @@ -1,11 +1,11 @@ /******************************************************************************* * Copyright (c) 2007 Wind River Systems, Inc. and others. - * All rights reserved. This program and the accompanying materials - * are made available under the terms of the Eclipse Public License v1.0 - * which accompanies this distribution, and is available at - * http://www.eclipse.org/legal/epl-v10.html - * - * Contributors: + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: * Michael Scharf (Wind River) - initial API and implementation *******************************************************************************/ package org.eclipse.tm.internal.terminal.textcanvas; @@ -16,10 +16,10 @@ import org.eclipse.tm.terminal.model.ITerminalTextDataReadOnly; public interface ITextCanvasModel { void addCellCanvasModelListener(ITextCanvasModelListener listener); void removeCellCanvasModelListener(ITextCanvasModelListener listener); - + ITerminalTextDataReadOnly getTerminalText(); /** - * This is is + * This is is * @param startLine * @param startCol * @param height @@ -36,48 +36,49 @@ public interface ITextCanvasModel { * @param visible */ void setCursorEnabled(boolean visible); - + /** * @return true if the cursor is shown. */ boolean isCursorEnabled(); - + /** - * @return the line of the cursor + * @return the line of the cursor */ int getCursorLine(); /** * @return the column of the cursor */ int getCursorColumn(); - + /** * @return the start of the selection or null if nothing is selected - * {@link Point#x} is the column and {@link Point#y} is the line. + * {@link Point#x} is the column and {@link Point#y} is the line. */ Point getSelectionStart(); /** * @return the end of the selection or null if nothing is selected - * {@link Point#x} is the column and {@link Point#y} is the line. + * {@link Point#x} is the column and {@link Point#y} is the line. */ Point getSelectionEnd(); - + Point getSelectionAnchor(); - + void setSelectionAnchor(Point anchor); /** + * Sets the selection. A negative startLine clears the selection. * @param startLine * @param endLine * @param startColumn * @param endColumn */ void setSelection(int startLine, int endLine, int startColumn, int endColumn); - + /** * @param line * @return true if line is part of the selection */ boolean hasLineSelection(int line); - + String getSelectedText(); } \ No newline at end of file diff --git a/terminal/org.eclipse.tm.terminal/src/org/eclipse/tm/internal/terminal/textcanvas/TextCanvas.java b/terminal/org.eclipse.tm.terminal/src/org/eclipse/tm/internal/terminal/textcanvas/TextCanvas.java index 032e8d8c2a3..50eab90ac4c 100644 --- a/terminal/org.eclipse.tm.terminal/src/org/eclipse/tm/internal/terminal/textcanvas/TextCanvas.java +++ b/terminal/org.eclipse.tm.terminal/src/org/eclipse/tm/internal/terminal/textcanvas/TextCanvas.java @@ -1,11 +1,11 @@ /******************************************************************************* * Copyright (c) 2007 Wind River Systems, Inc. and others. - * All rights reserved. This program and the accompanying materials - * are made available under the terms of the Eclipse Public License v1.0 - * which accompanies this distribution, and is available at - * http://www.eclipse.org/legal/epl-v10.html - * - * Contributors: + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: * Michael Scharf (Wind River) - initial API and implementation *******************************************************************************/ package org.eclipse.tm.internal.terminal.textcanvas; @@ -37,6 +37,7 @@ public class TextCanvas extends GridCanvas { private boolean fScrollLock; private Point fDraggingStart; private Point fDraggingEnd; + private boolean fHasSelection; private ResizeListener fResizeListener; private int fMinColumns=20; private int fMinLines=4; @@ -56,7 +57,7 @@ public class TextCanvas extends GridCanvas { setCellHeight(fCellRenderer.getCellHeight()); calculateGrid(); - + } public void rangeChanged(int col, int line, int width, int height) { repaintRange(col,line,width,height); @@ -84,6 +85,7 @@ public class TextCanvas extends GridCanvas { public void mouseDown(MouseEvent e) { if(e.button==1) { // left button fDraggingStart=screenPointToCell(e.x, e.y); + fHasSelection=false; if((e.stateMask&SWT.SHIFT)!=0) { Point anchor=fCellCanvasModel.getSelectionAnchor(); if(anchor!=null) @@ -94,9 +96,13 @@ public class TextCanvas extends GridCanvas { fDraggingEnd=null; } } - public void mouseUp(MouseEvent e) { + public void mouseUp(MouseEvent e) { if(e.button==1) { // left button - setSelection(screenPointToCell(e.x, e.y)); + updateHasSelection(e); + if(fHasSelection) + setSelection(screenPointToCell(e.x, e.y)); + else + fCellCanvasModel.setSelection(-1,-1,-1,-1); fDraggingStart=null; } } @@ -105,6 +111,7 @@ public class TextCanvas extends GridCanvas { public void mouseMove(MouseEvent e) { if (fDraggingStart != null) { + updateHasSelection(e); setSelection(screenPointToCell(e.x, e.y)); } } @@ -113,6 +120,20 @@ public class TextCanvas extends GridCanvas { setHorizontalBarVisible(false); } + /** + * The user has to drag the mouse to at least one character to make a selection. + * Once this is done, even a one char selection is OK. + * + * @param e + */ + private void updateHasSelection(MouseEvent e) { + if(fDraggingStart!=null) { + Point p=screenPointToCell(e.x, e.y); + if(fDraggingStart.x!=p.x||fDraggingStart.y!=p.y) + fHasSelection=true; + } + } + void setSelection(Point p) { if (fDraggingStart !=null && !p.equals(fDraggingEnd)) { fDraggingEnd = p; @@ -143,7 +164,7 @@ public class TextCanvas extends GridCanvas { public ILinelRenderer getCellRenderer() { return fCellRenderer; } - + public int getMinColumns() { return fMinColumns; } @@ -212,15 +233,15 @@ public class TextCanvas extends GridCanvas { } } /** - * + * * @return true if the cursor should be shown on output.... */ public boolean isScrollLock() { return fScrollLock; } /** - * If set then if the size changes - * @param scrollLock + * If set then if the size changes + * @param scrollLock */ public void setScrollLock(boolean scrollLock) { fScrollLock=scrollLock; @@ -232,7 +253,7 @@ public class TextCanvas extends GridCanvas { } protected void drawLine(GC gc, int line, int x, int y, int colFirst, int colLast) { fCellRenderer.drawLine(fCellCanvasModel, gc,line,x,y,colFirst, colLast); - + } protected void visibleCellRectangleChanged(int x, int y, int width, int height) { fCellCanvasModel.setVisibleRectangle(y,x,height,width); @@ -278,6 +299,6 @@ public class TextCanvas extends GridCanvas { throw new IllegalArgumentException("There can be at most one listener at the moment!"); //$NON-NLS-1$ fResizeListener=listener; } - + }