diff -r e8e63152f320 -r 2a9601315dfc javauis/eswt_qt/org.eclipse.swt/Eclipse SWT/qt/org/eclipse/swt/graphics/GC.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/javauis/eswt_qt/org.eclipse.swt/Eclipse SWT/qt/org/eclipse/swt/graphics/GC.java Mon May 03 12:27:20 2010 +0300 @@ -0,0 +1,2155 @@ +/******************************************************************************* + * Copyright (c) 2000, 2007 IBM Corporation and others. + * Portion Copyright (c) 2008 Nokia Corporation and/or its subsidiary(-ies). + * 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 + * Nokia Corporation - Qt implementation + *******************************************************************************/ +package org.eclipse.swt.graphics; + +import org.eclipse.swt.SWT; +import org.eclipse.swt.internal.qt.GCData; +import org.eclipse.swt.internal.qt.OS; +import org.eclipse.swt.internal.qt.graphics.GraphicsContext; +import org.eclipse.swt.widgets.Control; +import org.eclipse.swt.widgets.Display; + +/** + * Class GC is where all of the drawing capabilities that are + * supported by SWT are located. Instances are used to draw on either an + * Image, a Control, or directly on a + * Display. + *
+ *
Styles:
+ *
LEFT_TO_RIGHT, RIGHT_TO_LEFT
+ *
+ * + *

+ * The SWT drawing coordinate system is the two-dimensional space with the + * origin (0,0) at the top left corner of the drawing area and with (x,y) values + * increasing to the right and downward respectively. + *

+ * + *

+ * Application code must explicitly invoke the GC.dispose() method + * to release the operating system resources managed by each instance when those + * instances are no longer required. This is particularly important on + * Windows95 and Windows98 where the operating system has a limited number of + * device contexts available. + *

+ * + *

+ * Note: Only one of LEFT_TO_RIGHT and RIGHT_TO_LEFT may be specified. + *

+ * + * @see org.eclipse.swt.events.PaintEvent + */ +public final class GC { + +// FontMetrics +private final static int FM_ASCENT = 0; +private final static int FM_AVERAGE_CHAR_WIDTH = 1; +private final static int FM_DESCENT = 2; +private final static int FM_HEIGHT = 3; +private final static int FM_LEADING = 4; +private final static int FM_DATA_COUNT = 5; + +// Configuration flags +final static int FOREGROUND = 1 << 0; +final static int BACKGROUND = 1 << 1; +final static int FONT = 1 << 2; +final static int LINE_STYLE = 1 << 3; +final static int LINE_CAP = 1 << 4; +final static int LINE_JOIN = 1 << 5; +final static int LINE_WIDTH = 1 << 6; +final static int LINE_MITERLIMIT = 1 << 7; +final static int BACKGROUND_BG = 1 << 8; +final static int DRAW_OFFSET = 1 << 9; +final static int DRAW = FOREGROUND | LINE_WIDTH | LINE_STYLE | LINE_CAP + | LINE_JOIN | LINE_MITERLIMIT | DRAW_OFFSET; +final static int FILL = BACKGROUND; + +static int checkStyle(int style) { + if ((style & SWT.LEFT_TO_RIGHT) != 0) { + style &= ~SWT.RIGHT_TO_LEFT; + } + return style & (SWT.LEFT_TO_RIGHT | SWT.RIGHT_TO_LEFT); +} + +int handle; +GCData data; +Color customForeground; +Color customBackground; + +Drawable drawable; + +/** + * Prevents uninitialized instances from being created outside the package. + */ +GC() { +} + +/** + * Constructs a new instance of this class which has been configured to draw on + * the specified drawable. Sets the foreground color, background color and font + * in the GC to match those in the drawable. + *

+ * You must dispose the graphics context when it is no longer required. + *

+ * + * @param drawable + * the drawable to draw on + * @exception IllegalArgumentException + * + * @exception SWTError + * + */ +public GC(Drawable drawable) { + this(drawable, 0); +} + +/** + * Constructs a new instance of this class which has been configured to draw on + * the specified drawable. Sets the foreground color, background color and font + * in the GC to match those in the drawable. + *

+ * You must dispose the graphics context when it is no longer required. + *

+ * + * @param drawable + * the drawable to draw on + * @param style + * the style of GC to construct + * + * @exception IllegalArgumentException + * + * @exception SWTError + * + * + */ +public GC(Drawable drawable, int style) { + if (drawable == null) { + SWT.error(SWT.ERROR_NULL_ARGUMENT); + } + + GCData data = new GCData(); + data.style = checkStyle(style); + + int gcHandle = drawable.internal_new_GC(data); + try { + init(drawable, data, gcHandle); + } catch (Error e) { + drawable.internal_dispose_GC(data); + throw e; + } +} + +/* + * Here it's checked based on the data.state flags which gc attributes need to + * be configured. State flags are set when configuration is up-to-date and + * cleared when configuration is invalidated. + */ +void checkGC(int mask) { + int state = data.state; + if ((state & mask) == mask) + return; + state = (state ^ mask) & mask; // take out bits not in mask + data.state |= mask; + + if((data.state & FOREGROUND) != 0) { + if ((data.style & SWT.MIRRORED) != 0) { + int width = OS.QPaintDevice_width(data.drawable); + data.internalGc.resetTransform(); + data.internalGc.translate(width - 1, 0); + data.internalGc.scale(-1, 1); + } + } +} + +/** + * Copies a rectangular area of the receiver at the specified position into the + * image, which must be of type SWT.BITMAP. + * + * @param image + * the image to copy into + * @param x + * the x coordinate in the receiver of the area to be copied + * @param y + * the y coordinate in the receiver of the area to be copied + * + * @exception IllegalArgumentException + * + * @exception SWTException + * + */ +public void copyArea(Image image, int x, int y) { + if (handle == 0) { + SWT.error(SWT.ERROR_GRAPHIC_DISPOSED); + } + if (image == null) { + SWT.error(SWT.ERROR_NULL_ARGUMENT); + } + if (image.type != SWT.BITMAP || image.isDisposed()) { + SWT.error(SWT.ERROR_INVALID_ARGUMENT); + } + + // Reset all mirroring transformation and calculate the mirrored coordinates + // manually. + if ((data.style & SWT.MIRRORED) != 0) { + data.internalGc.resetTransform(); + data.state = ~FOREGROUND; + int clientWidth = OS.QPaintDevice_width(data.drawable); + x = clientWidth - x - image.getBounds().width; + } + + data.internalGc.copyArea(image.getImage(), x, y); +} + +/** + * Copies a rectangular area of the receiver at the source position onto the + * receiver at the destination position. + * + * @param srcX + * the x coordinate in the receiver of the area to be copied + * @param srcY + * the y coordinate in the receiver of the area to be copied + * @param width + * the width of the area to copy + * @param height + * the height of the area to copy + * @param destX + * the x coordinate in the receiver of the area to copy to + * @param destY + * the y coordinate in the receiver of the area to copy to + * + * @exception SWTException + * + */ +public void copyArea(int srcX, int srcY, int width, int height, int destX, + int destY) { + copyArea(srcX, srcY, width, height, destX, destY, true); +} + +/** + * Copies a rectangular area of the receiver at the source + * position onto the receiver at the destination position. + * + * @param srcX the x coordinate in the receiver of the area to be copied + * @param srcY the y coordinate in the receiver of the area to be copied + * @param width the width of the area to copy + * @param height the height of the area to copy + * @param destX the x coordinate in the receiver of the area to copy to + * @param destY the y coordinate in the receiver of the area to copy to + * @param paint if true paint events will be generated for old and obscured areas + * + * @exception SWTException + * + * @since 3.1 + */ +public void copyArea(int srcX, int srcY, int width, int height, int destX, int destY, boolean paint) { + if (handle == 0) { + SWT.error(SWT.ERROR_GRAPHIC_DISPOSED); + } + if (width <= 0 || height <= 0) { + return; + } + int deltaX = destX - srcX; + int deltaY = destY - srcY; + if (deltaX == 0 && deltaY == 0) { + return; + } + + // Reset all mirroring transformation and calculate the mirrored coordinates + // manually. + if ((data.style & SWT.MIRRORED) != 0) { + data.internalGc.resetTransform(); + data.state = ~FOREGROUND; + int clientWidth = OS.QPaintDevice_width(data.drawable); + srcX = clientWidth - srcX - width; + destX = clientWidth - destX - width; + } + data.internalGc.copyArea(srcX, srcY, width, height, destX, destY, paint); +} + +/** + * Disposes of the operating system resources associated with the graphics + * context. Applications must dispose of all the GCs which they allocate. + * + * @exception SWTError + *