
org.eclipse.swt.widgets.Tracker Maven / Gradle / Ivy
Show all versions of draw2d-rwt Show documentation
/*******************************************************************************
* Copyright (c) 2000, 2010 IBM Corporation 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:
* IBM Corporation - initial API and implementation
* Austin Riddle (Texas Center for Applied Technology) - rudimentary RAP port
*******************************************************************************/
package org.eclipse.swt.widgets;
import org.eclipse.swt.SWT;
import org.eclipse.swt.SWTException;
import org.eclipse.swt.dnd.*;
import org.eclipse.swt.events.ControlListener;
import org.eclipse.swt.events.KeyListener;
import org.eclipse.swt.graphics.*;
/**
* Instances of this class implement rubber banding rectangles that are
* drawn onto a parent Composite
or Display
.
* These rectangles can be specified to respond to mouse and key events
* by either moving or resizing themselves accordingly. Trackers are
* typically used to represent window geometries in a lightweight manner.
*
*
* - Styles:
* - LEFT, RIGHT, UP, DOWN, RESIZE
* - Events:
* - Move, Resize
*
*
* Note: Rectangle move behavior is assumed unless RESIZE is specified.
*
* IMPORTANT: This class is not intended to be subclassed.
*
*
* @see Tracker snippets
* @see Sample code and further information
* @noextend This class is not intended to be subclassed by clients.
*/
public class Tracker extends Widget {
Control parent;
boolean tracking, cancelled, stippled;
Rectangle [] rectangles = new Rectangle [0], proportions = rectangles;
Rectangle bounds;
int /*long*/ resizeCursor;
Cursor clientCursor;
int cursorOrientation = SWT.NONE;
boolean inEvent = false;
int /*long*/ hwndTransparent, hwndOpaque, oldTransparentProc, oldOpaqueProc;
int oldX, oldY;
Shell trackerShell;
/*
* The following values mirror step sizes on Windows
*/
final static int STEPSIZE_SMALL = 1;
final static int STEPSIZE_LARGE = 9;
private DropTarget trackerDropTarget;
private int internalStyle;
/**
* Constructs a new instance of this class given its parent
* and a style value describing its behavior and appearance.
*
* The style value is either one of the style constants defined in
* class SWT
which is applicable to instances of this
* class, or must be built by bitwise OR'ing together
* (that is, using the int
"|" operator) two or more
* of those SWT
style constants. The class description
* lists the style constants that are applicable to the class.
* Style bits are also inherited from superclasses.
*
*
* @param parent a widget which will be the parent of the new instance (cannot be null)
* @param style the style of widget to construct
*
* @exception IllegalArgumentException
* - ERROR_NULL_ARGUMENT - if the parent is null
*
* @exception SWTException
* - ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the parent
* - ERROR_INVALID_SUBCLASS - if this class is not an allowed subclass
*
*
* @see SWT#LEFT
* @see SWT#RIGHT
* @see SWT#UP
* @see SWT#DOWN
* @see SWT#RESIZE
* @see Widget#checkSubclass
* @see Widget#getStyle
*/
public Tracker (Composite parent, int style) {
super (parent, checkStyle (style));
this.parent = parent;
internalStyle = getStyle();
}
/**
* Constructs a new instance of this class given the display
* to create it on and a style value describing its behavior
* and appearance.
*
* The style value is either one of the style constants defined in
* class SWT
which is applicable to instances of this
* class, or must be built by bitwise OR'ing together
* (that is, using the int
"|" operator) two or more
* of those SWT
style constants. The class description
* lists the style constants that are applicable to the class.
* Style bits are also inherited from superclasses.
*
* Note: Currently, null can be passed in for the display argument.
* This has the effect of creating the tracker on the currently active
* display if there is one. If there is no current display, the
* tracker is created on a "default" display. Passing in null as
* the display argument is not considered to be good coding style,
* and may not be supported in a future release of SWT.
*
*
* @param display the display to create the tracker on
* @param style the style of control to construct
*
* @exception SWTException
* - ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the parent
* - ERROR_INVALID_SUBCLASS - if this class is not an allowed subclass
*
*
* @see SWT#LEFT
* @see SWT#RIGHT
* @see SWT#UP
* @see SWT#DOWN
* @see SWT#RESIZE
*/
//public Tracker (Display display, int style) {
// if (display == null) display = Display.getCurrent ();
// if (display == null) display = Display.getDefault ();
//// if (!display.isValidThread ()) {
//// error (SWT.ERROR_THREAD_INVALID_ACCESS);
//// }
// this.style = checkStyle (style);
// this.display = display;
//}
/**
* Adds the listener to the collection of listeners who will
* be notified when the control is moved or resized, by sending
* it one of the messages defined in the ControlListener
* interface.
*
* @param listener the listener which should be notified
*
* @exception IllegalArgumentException
* - ERROR_NULL_ARGUMENT - if the listener is null
*
* @exception SWTException
* - ERROR_WIDGET_DISPOSED - if the receiver has been disposed
* - ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver
*
*
* @see ControlListener
* @see #removeControlListener
*/
public void addControlListener (ControlListener listener) {
checkWidget ();
if (listener == null) error (SWT.ERROR_NULL_ARGUMENT);
TypedListener typedListener = new TypedListener (listener);
addListener (SWT.Resize, typedListener);
addListener (SWT.Move, typedListener);
}
/**
* Adds the listener to the collection of listeners who will
* be notified when keys are pressed and released on the system keyboard, by sending
* it one of the messages defined in the KeyListener
* interface.
*
* @param listener the listener which should be notified
*
* @exception IllegalArgumentException
* - ERROR_NULL_ARGUMENT - if the listener is null
*
* @exception SWTException
* - ERROR_WIDGET_DISPOSED - if the receiver has been disposed
* - ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver
*
*
* @see KeyListener
* @see #removeKeyListener
*/
public void addKeyListener (KeyListener listener) {
checkWidget ();
if (listener == null) error (SWT.ERROR_NULL_ARGUMENT);
TypedListener typedListener = new TypedListener (listener);
addListener (SWT.KeyUp,typedListener);
addListener (SWT.KeyDown,typedListener);
}
Point adjustMoveCursor () {
if (bounds == null) return null;
int newX = bounds.x + bounds.width / 2;
int newY = bounds.y;
// POINT pt = new POINT ();
// pt.x = newX; pt.y = newY;
// /*
// * Convert to screen coordinates iff needed
// */
// if (parent != null) {
// OS.ClientToScreen (parent.handle, pt);
// }
// OS.SetCursorPos (pt.x, pt.y);
// return new Point (pt.x, pt.y);
return new Point (0,0);
}
Point adjustResizeCursor () {
if (bounds == null) return null;
int newX, newY;
if ((cursorOrientation & SWT.LEFT) != 0) {
newX = bounds.x;
} else if ((cursorOrientation & SWT.RIGHT) != 0) {
newX = bounds.x + bounds.width;
} else {
newX = bounds.x + bounds.width / 2;
}
if ((cursorOrientation & SWT.UP) != 0) {
newY = bounds.y;
} else if ((cursorOrientation & SWT.DOWN) != 0) {
newY = bounds.y + bounds.height;
} else {
newY = bounds.y + bounds.height / 2;
}
// POINT pt = new POINT ();
// pt.x = newX; pt.y = newY;
// /*
// * Convert to screen coordinates iff needed
// */
// if (parent != null) {
// OS.ClientToScreen (parent.handle, pt);
// }
// OS.SetCursorPos (pt.x, pt.y);
//
// /*
// * If the client has not provided a custom cursor then determine
// * the appropriate resize cursor.
// */
if (clientCursor == null) {
int /*long*/ newCursor = 0;
// switch (cursorOrientation) {
// case SWT.UP:
// newCursor = OS.LoadCursor (0, OS.IDC_SIZENS);
// break;
// case SWT.DOWN:
// newCursor = OS.LoadCursor (0, OS.IDC_SIZENS);
// break;
// case SWT.LEFT:
// newCursor = OS.LoadCursor (0, OS.IDC_SIZEWE);
// break;
// case SWT.RIGHT:
// newCursor = OS.LoadCursor (0, OS.IDC_SIZEWE);
// break;
// case SWT.LEFT | SWT.UP:
// newCursor = OS.LoadCursor (0, OS.IDC_SIZENWSE);
// break;
// case SWT.RIGHT | SWT.DOWN:
// newCursor = OS.LoadCursor (0, OS.IDC_SIZENWSE);
// break;
// case SWT.LEFT | SWT.DOWN:
// newCursor = OS.LoadCursor (0, OS.IDC_SIZENESW);
// break;
// case SWT.RIGHT | SWT.UP:
// newCursor = OS.LoadCursor (0, OS.IDC_SIZENESW);
// break;
// default:
// newCursor = OS.LoadCursor (0, OS.IDC_SIZEALL);
// break;
// }
// OS.SetCursor (newCursor);
// if (resizeCursor != 0) {
// OS.DestroyCursor (resizeCursor);
// }
resizeCursor = newCursor;
}
// return new Point (pt.x, pt.y);
return new Point (0,0);
}
static int checkStyle (int style) {
if ((style & (SWT.LEFT | SWT.RIGHT | SWT.UP | SWT.DOWN)) == 0) {
style |= SWT.LEFT | SWT.RIGHT | SWT.UP | SWT.DOWN;
}
return style;
}
/**
* Stops displaying the tracker rectangles. Note that this is not considered
* to be a cancelation by the user.
*
* @exception SWTException
* - ERROR_WIDGET_DISPOSED - if the receiver has been disposed
* - ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver
*
*/
public void close () {
checkWidget ();
if (trackerShell != null) {
trackerShell.dispose();
trackerDropTarget.dispose();
trackerShell = null;
trackerDropTarget = null;
}
tracking = false;
}
Rectangle computeBounds () {
if (rectangles.length == 0) return null;
int xMin = rectangles [0].x;
int yMin = rectangles [0].y;
int xMax = rectangles [0].x + rectangles [0].width;
int yMax = rectangles [0].y + rectangles [0].height;
for (int i = 1; i < rectangles.length; i++) {
if (rectangles [i].x < xMin) xMin = rectangles [i].x;
if (rectangles [i].y < yMin) yMin = rectangles [i].y;
int rectRight = rectangles [i].x + rectangles [i].width;
if (rectRight > xMax) xMax = rectRight;
int rectBottom = rectangles [i].y + rectangles [i].height;
if (rectBottom > yMax) yMax = rectBottom;
}
return new Rectangle (xMin, yMin, xMax - xMin, yMax - yMin);
}
Rectangle [] computeProportions (Rectangle [] rects) {
Rectangle [] result = new Rectangle [rects.length];
bounds = computeBounds ();
if (bounds != null) {
for (int i = 0; i < rects.length; i++) {
int x = 0, y = 0, width = 0, height = 0;
if (bounds.width != 0) {
x = (rects [i].x - bounds.x) * 100 / bounds.width;
width = rects [i].width * 100 / bounds.width;
} else {
width = 100;
}
if (bounds.height != 0) {
y = (rects [i].y - bounds.y) * 100 / bounds.height;
height = rects [i].height * 100 / bounds.height;
} else {
height = 100;
}
result [i] = new Rectangle (x, y, width, height);
}
}
return result;
}
/**
* Draw the rectangles displayed by the tracker.
*/
void drawRectangles (Rectangle [] rects, boolean stippled) {
if (trackerShell == null || trackerShell.isDisposed()) {
trackerShell = new Shell(this.getDisplay(),SWT.NO_TRIM);
trackerShell.setAlpha( 140 );
trackerDropTarget = new DropTarget(trackerShell,DND.DROP_MOVE);
trackerDropTarget.setTransfer( new Transfer[]{TextTransfer.getInstance()} );
trackerShell.open();
}
// if (parent == null && !OS.IsWinCE && OS.WIN32_VERSION >= OS.VERSION (6, 0)) {
// RECT rect1 = new RECT();
// int bandWidth = stippled ? 3 : 1;
for (int i = 0; i < rects.length; i++) {
Rectangle rect = rects[i];
if (trackerShell != null && !trackerShell.isDisposed()) {
trackerShell.setBounds( rect );
}
// rect1.left = rect.x - bandWidth;
// rect1.top = rect.y - bandWidth;
// rect1.right = rect.x + rect.width + bandWidth * 2;
// rect1.bottom = rect.y + rect.height + bandWidth * 2;
// OS.RedrawWindow (hwndOpaque, rect1, 0, OS.RDW_INVALIDATE);
}
// return;
// }
// int bandWidth = 1;
// int /*long*/ hwndTrack = OS.GetDesktopWindow ();
// if (parent != null) hwndTrack = parent.handle;
// int /*long*/ hDC = OS.GetDCEx (hwndTrack, 0, OS.DCX_CACHE);
// int /*long*/ hBitmap = 0, hBrush = 0, oldBrush = 0;
// if (stippled) {
// bandWidth = 3;
// byte [] bits = {-86, 0, 85, 0, -86, 0, 85, 0, -86, 0, 85, 0, -86, 0, 85, 0};
// hBitmap = OS.CreateBitmap (8, 8, 1, 1, bits);
// hBrush = OS.CreatePatternBrush (hBitmap);
// oldBrush = OS.SelectObject (hDC, hBrush);
// }
// for (int i=0; iDisplay
then these are screen
* coordinates.
*
* @return the bounds of the Rectangles being drawn
*
* @exception SWTException
-
*
- ERROR_WIDGET_DISPOSED - if the receiver has been disposed *
- ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver *
true
if the rectangles are drawn with a stippled line, false
otherwise.
*
* @return the stippled effect of the rectangles
*
* @exception SWTException -
*
- ERROR_WIDGET_DISPOSED - if the receiver has been disposed *
- ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver *
true
if the user did not cancel the Tracker, false
otherwise
*
* @exception SWTException -
*
- ERROR_WIDGET_DISPOSED - if the receiver has been disposed *
- ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver *
-
*
- ERROR_NULL_ARGUMENT - if the listener is null *
-
*
- ERROR_WIDGET_DISPOSED - if the receiver has been disposed *
- ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver *
-
*
- ERROR_NULL_ARGUMENT - if the listener is null *
-
*
- ERROR_WIDGET_DISPOSED - if the receiver has been disposed *
- ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver *
Cursor
of the Tracker. If this cursor is null
* then the cursor reverts to the default.
*
* @param newCursor the new Cursor
to display
*
* @exception SWTException -
*
- ERROR_WIDGET_DISPOSED - if the receiver has been disposed *
- ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver *
-
*
- ERROR_NULL_ARGUMENT - if the set of rectangles is null or contains a null rectangle *
-
*
- ERROR_WIDGET_DISPOSED - if the receiver has been disposed *
- ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver *
true
if rectangle should appear stippled
*
* @exception SWTException -
*
- ERROR_WIDGET_DISPOSED - if the receiver has been disposed *
- ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver *