diff options
author | Niraj Modi | 2014-12-16 08:33:33 +0000 |
---|---|---|
committer | Niraj Modi | 2014-12-16 08:33:33 +0000 |
commit | 1cdfeeaccc53c178b93c72e34f5379dd6eea654c (patch) | |
tree | 45dc5f05a7493f29eb4e39eed0d25a90b44b7104 | |
parent | 5618fd34cc76c821b34579f5f5ba31f8e4de2c80 (diff) | |
download | eclipse.platform.swt-1cdfeeaccc53c178b93c72e34f5379dd6eea654c.tar.gz eclipse.platform.swt-1cdfeeaccc53c178b93c72e34f5379dd6eea654c.tar.xz eclipse.platform.swt-1cdfeeaccc53c178b93c72e34f5379dd6eea654c.zip |
Bug 435384 - Add support for 'transparent' colors
- replaced transparent boolean with int alpha
Change-Id: I95ee45b2526e4d5bd729d68d0b30b334c9f5ae9c
Signed-off-by: Niraj Modi <niraj.modi@in.ibm.com>
3 files changed, 102 insertions, 69 deletions
diff --git a/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/graphics/Color.java b/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/graphics/Color.java index 668bf54cac..624f3d11fd 100644 --- a/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/graphics/Color.java +++ b/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/graphics/Color.java @@ -46,7 +46,7 @@ public final class Color extends Resource { * @noreference This field is not intended to be referenced by clients. */ public int handle; - public boolean transparent; + int alpha = 255; /** * Prevents uninitialized instances from being created outside the package. @@ -81,7 +81,38 @@ Color(Device device) { */ public Color (Device device, int red, int green, int blue) { super(device); - init(red, green, blue); + init(red, green, blue, 255); + init(); +} + +/** + * Constructs a new instance of this class given a device and the + * desired red, green, blue & alpha values expressed as ints in the range + * 0 to 255 (where 0 is black and 255 is full brightness). On limited + * color devices, the color instance created by this call may not have + * the same RGB values as the ones specified by the arguments. The + * RGB values on the returned instance will be the color values of + * the operating system color. + * <p> + * You must dispose the color when it is no longer required. + * </p> + * + * @param device the device on which to allocate the color + * @param red the amount of red in the color + * @param green the amount of green in the color + * @param blue the amount of blue in the color + * @param alpha the amount of alpha in the color + * + * @exception IllegalArgumentException <ul> + * <li>ERROR_NULL_ARGUMENT - if device is null and there is no current device</li> + * <li>ERROR_INVALID_ARGUMENT - if the red, green, blue or alpha argument is not between 0 and 255</li> + * </ul> + * + * @see #dispose + */ +public Color (Device device, int red, int green, int blue, int alpha) { + super(device); + init(red, green, blue, alpha); init(); } @@ -110,14 +141,14 @@ public Color (Device device, int red, int green, int blue) { public Color (Device device, RGB rgb) { super(device); if (rgb == null) SWT.error(SWT.ERROR_NULL_ARGUMENT); - init(rgb.red, rgb.green, rgb.blue); + init(rgb.red, rgb.green, rgb.blue, 255); init(); } /** * Constructs a new instance of this class given a device, an - * <code>RGB</code> describing the desired red, green and blue values - * and transparent boolean as true or false. + * <code>RGB</code> describing the desired red, green and blue values, + * alpha specifying the level of transparency. * On limited color devices, the color instance created by this call * may not have the same RGB values as the ones specified by the * argument. The RGB values on the returned instance will be the color @@ -128,18 +159,19 @@ public Color (Device device, RGB rgb) { * * @param device the device on which to allocate the color * @param rgb the RGB values of the desired color + * @param alpha the alpha value of the desired color * * @exception IllegalArgumentException <ul> * <li>ERROR_NULL_ARGUMENT - if device is null and there is no current device</li> * <li>ERROR_NULL_ARGUMENT - if the rgb argument is null</li> - * <li>ERROR_INVALID_ARGUMENT - if the red, green or blue components of the argument are not between 0 and 255</li> + * <li>ERROR_INVALID_ARGUMENT - if the red, green, blue or alpha components of the argument are not between 0 and 255</li> * </ul> * * @see #dispose */ -public Color(Device device, RGB rgb, boolean transparent) { +public Color(Device device, RGB rgb, int alpha) { this(device, rgb); - this.transparent = transparent; + this.alpha = alpha; } void destroy() { @@ -174,7 +206,20 @@ public boolean equals (Object object) { if (object == this) return true; if (!(object instanceof Color)) return false; Color color = (Color) object; - return device == color.device && (handle & 0xFFFFFF) == (color.handle & 0xFFFFFF); + return device == color.device && (handle & 0xFFFFFF) == (color.handle & 0xFFFFFF) && (alpha == color.alpha); +} + +/** + * Returns the amount of alpha in the color, from 0 to 255. + * + * @return the alpha component of the color + * + * @exception SWTException <ul> + * <li>ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed</li> + * </ul> + */ +public int getAlpha () { + return this.alpha; } /** @@ -255,18 +300,20 @@ public int hashCode () { * @param red the amount of red in the color * @param green the amount of green in the color * @param blue the amount of blue in the color + * @param alpha the amount of alpha in the color * * @exception IllegalArgumentException <ul> - * <li>ERROR_INVALID_ARGUMENT - if the red, green or blue argument is not between 0 and 255</li> + * <li>ERROR_INVALID_ARGUMENT - if the red, green, blue or alpha argument is not between 0 and 255</li> * </ul> * * @see #dispose */ -void init(int red, int green, int blue) { - if (red > 255 || red < 0 || green > 255 || green < 0 || blue > 255 || blue < 0) { +void init(int red, int green, int blue, int alpha) { + if (red > 255 || red < 0 || green > 255 || green < 0 || blue > 255 || blue < 0 || alpha > 255 || alpha < 0) { SWT.error(SWT.ERROR_INVALID_ARGUMENT); } handle = (red & 0xFF) | ((green & 0xFF) << 8) | ((blue & 0xFF) << 16); + this.alpha = alpha; /* If this is not a palette-based device, return */ long /*int*/ hPal = device.hPalette; @@ -322,17 +369,6 @@ public boolean isDisposed() { } /** - * Returns <code>true</code> if the color is transparent, - * and <code>false</code> otherwise. - * <p> - * - * @return <code>true</code> when the color is transparent and <code>false</code> otherwise - */ -public boolean isTransparent() { - return this.transparent; -} - -/** * Returns a string containing a concise, human-readable * description of the receiver. * @@ -360,8 +396,30 @@ public String toString () { * @noreference This method is not intended to be referenced by clients. */ public static Color win32_new(Device device, int handle) { + return win32_new(device, handle, 255); +} + +/** + * Invokes platform specific functionality to allocate a new color. + * <p> + * <b>IMPORTANT:</b> This method is <em>not</em> part of the public + * API for <code>Color</code>. It is marked public only so that it + * can be shared within the packages provided by SWT. It is not + * available on all platforms, and should never be called from + * application code. + * </p> + * + * @param device the device on which to allocate the color + * @param handle the handle for the color + * @param alpha the int for the alpha content in the color + * @return a new color object containing the specified device and handle + * + * @noreference This method is not intended to be referenced by clients. + */ +public static Color win32_new(Device device, int handle, int alpha) { Color color = new Color(device); color.handle = handle; + color.alpha = alpha; return color; } diff --git a/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/Control.java b/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/Control.java index 15dc3138e6..886aaf1a8f 100644 --- a/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/Control.java +++ b/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/Control.java @@ -67,8 +67,7 @@ public abstract class Control extends Widget implements Drawable { Image backgroundImage, transparentBackgroundImage; Region region; Font font; - int drawCount, foreground, background, transparentBackground; - boolean isTransparentBackground; + int drawCount, foreground, background, transparentBackground, backgroundAlpha = 255; /** * Prevents uninitialized instances from being created outside the package. @@ -542,8 +541,8 @@ void checkBackground () { Composite composite = parent; do { int mode = composite.backgroundMode; - if (mode != 0 || isTransparentBackground) { - if (mode == SWT.INHERIT_DEFAULT || isTransparentBackground) { + if (mode != 0 || backgroundAlpha == 0) { + if (mode == SWT.INHERIT_DEFAULT || backgroundAlpha == 0) { Control control = this; do { if ((control.state & THEME_BACKGROUND) == 0) { @@ -1167,9 +1166,8 @@ public Accessible getAccessible () { */ public Color getBackground () { checkWidget (); - if (isTransparentBackground) { - Color color = Color.win32_new (display, transparentBackground); - color.transparent = true; + if (backgroundAlpha == 0) { + Color color = Color.win32_new (display, transparentBackground, 0); return color; } else { @@ -1193,7 +1191,7 @@ public Color getBackground () { */ public Image getBackgroundImage () { checkWidget (); - if (isTransparentBackground) { + if (backgroundAlpha == 0) { return transparentBackgroundImage; } else { @@ -3023,7 +3021,7 @@ public void setBackground (Color color) { checkWidget (); _setBackground (color); if (color != null) { - setBackgroundTransparent (color.isTransparent ()); + setBackgroundTransparency (color.getAlpha()); } } @@ -3038,19 +3036,19 @@ private void _setBackground (Color color) { updateBackgroundColor (); } -private void setBackgroundTransparent (boolean transparent) { - if (transparent) { +private void setBackgroundTransparency (int alpha) { + if (alpha == 0) { // clear background if (backgroundImage != null) { transparentBackgroundImage = getBackgroundImage (); setBackgroundImage (null); - } + } if (getBackground () != null) { transparentBackground = getBackgroundPixel (); _setBackground (null); } } - isTransparentBackground = transparent; + this.backgroundAlpha = alpha; this.updateBackgroundMode (); } diff --git a/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet365.java b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet365.java index d218b32796..aac26c5f68 100644 --- a/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet365.java +++ b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet365.java @@ -11,7 +11,6 @@ package org.eclipse.swt.snippets;
import org.eclipse.swt.*;
-import org.eclipse.swt.custom.*;
import org.eclipse.swt.events.*;
import org.eclipse.swt.graphics.*;
import org.eclipse.swt.layout.*;
@@ -36,29 +35,6 @@ public class Snippet365 { static Button push;
static Text text;
- public static void main1(String[] args) {
- Display display = new Display();
- Shell shell = new Shell(display);
- shell.setLayout(new GridLayout());
- CTabFolder folder = new CTabFolder(shell, SWT.BORDER);
- folder.setBackground(
- new Color[] { display.getSystemColor(SWT.COLOR_RED), display.getSystemColor(SWT.COLOR_YELLOW) },
- new int[] { 90 }, true);
-
- CTabItem item = new CTabItem(folder, SWT.CLOSE);
- item.setText("Item");
-
- Composite comp = new Composite(folder, SWT.NONE);
- comp.setLayout(new RowLayout(SWT.HORIZONTAL));
- shell.pack();
- shell.open();
- while (!shell.isDisposed()) {
- if (!display.readAndDispatch())
- display.sleep();
- }
- display.dispose();
- }
-
public static void main(String[] args) {
final Display display = new Display();
final Shell shell = new Shell(display);
@@ -98,19 +74,20 @@ public class Snippet365 { @Override
public void widgetSelected(SelectionEvent e) {
boolean transparent = ((Button) e.getSource()).getSelection();
- composite.setBackground(new Color(display, composite.getBackground().getRGB(), transparent));
+ int alpha = transparent ? 0 : 255;
+ composite.setBackground(new Color(display, composite.getBackground().getRGB(), alpha));
buttonCheckBox.setBackground(new Color(Display.getDefault(), buttonCheckBox.getBackground().getRGB(),
- transparent));
- bar.setBackground(new Color(display, bar.getBackground().getRGB(), transparent));
- list.setBackground(new Color(display, list.getBackground().getRGB(), transparent));
- label.setBackground(new Color(display, label.getBackground().getRGB(), transparent));
- radio.setBackground(new Color(display, radio.getBackground().getRGB(), transparent));
+ alpha));
+ bar.setBackground(new Color(display, bar.getBackground().getRGB(), alpha));
+ list.setBackground(new Color(display, list.getBackground().getRGB(), alpha));
+ label.setBackground(new Color(display, label.getBackground().getRGB(), alpha));
+ radio.setBackground(new Color(display, radio.getBackground().getRGB(), alpha));
if (transparent)
- check.setBackground(new Color(display, check.getBackground().getRGB(), transparent));
+ check.setBackground(new Color(display, check.getBackground().getRGB(), alpha));
else
check.setBackgroundImage(check.getBackgroundImage());
- push.setBackground(new Color(display, push.getBackground().getRGB(), transparent));
- text.setBackground(new Color(display, text.getBackground().getRGB(), transparent));
+ push.setBackground(new Color(display, push.getBackground().getRGB(), alpha));
+ text.setBackground(new Color(display, text.getBackground().getRGB(), alpha));
}
@Override
|