Mercurial > projects > dwt2
comparison org.eclipse.jface/src/org/eclipse/jface/resource/DerivedImageDescriptor.d @ 12:bc29606a740c
Added dwt-addons in original directory structure of eclipse.org
author | Frank Benoit <benoit@tionex.de> |
---|---|
date | Sat, 14 Mar 2009 18:23:29 +0100 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
11:43904fec5dca | 12:bc29606a740c |
---|---|
1 /******************************************************************************* | |
2 * Copyright (c) 2005, 2006 IBM Corporation and others. | |
3 * All rights reserved. This program and the accompanying materials | |
4 * are made available under the terms of the Eclipse Public License v1.0 | |
5 * which accompanies this distribution, and is available at | |
6 * http://www.eclipse.org/legal/epl-v10.html | |
7 * | |
8 * Contributors: | |
9 * IBM Corporation - initial API and implementation | |
10 * Port to the D programming language: | |
11 * Frank Benoit <benoit@tionex.de> | |
12 *******************************************************************************/ | |
13 module org.eclipse.jface.resource.DerivedImageDescriptor; | |
14 | |
15 import org.eclipse.jface.resource.ImageDescriptor; | |
16 import org.eclipse.jface.resource.DeviceResourceException; | |
17 | |
18 import org.eclipse.swt.SWT; | |
19 import org.eclipse.swt.SWTException; | |
20 import org.eclipse.swt.graphics.Device; | |
21 import org.eclipse.swt.graphics.Image; | |
22 import org.eclipse.swt.graphics.ImageData; | |
23 import org.eclipse.swt.widgets.Display; | |
24 | |
25 import java.lang.all; | |
26 | |
27 /** | |
28 * An image descriptor which creates images based on another ImageDescriptor, but with | |
29 * additional SWT flags. Note that this is only intended for compatibility. | |
30 * | |
31 * @since 3.1 | |
32 */ | |
33 final class DerivedImageDescriptor : ImageDescriptor { | |
34 alias ImageDescriptor.createImage createImage; | |
35 | |
36 private ImageDescriptor original; | |
37 private int flags; | |
38 | |
39 /** | |
40 * Create a new image descriptor | |
41 * @param original the original one | |
42 * @param swtFlags flags to be used when image is created {@link Image#Image(Device, Image, int)} | |
43 * @see SWT#IMAGE_COPY | |
44 * @see SWT#IMAGE_DISABLE | |
45 * @see SWT#IMAGE_GRAY | |
46 */ | |
47 public this(ImageDescriptor original, int swtFlags) { | |
48 this.original = original; | |
49 flags = swtFlags; | |
50 } | |
51 | |
52 public override Object createResource(Device device) { | |
53 try { | |
54 return internalCreateImage(device); | |
55 } catch (SWTException e) { | |
56 throw new DeviceResourceException(this, e); | |
57 } | |
58 } | |
59 | |
60 public override Image createImage(Device device) { | |
61 return internalCreateImage(device); | |
62 } | |
63 | |
64 public override hash_t toHash() { | |
65 return original.toHash() + flags; | |
66 } | |
67 | |
68 public override int opEquals(Object arg0) { | |
69 if ( auto desc = cast(DerivedImageDescriptor)arg0 ) { | |
70 return desc.original is original && flags is desc.flags; | |
71 } | |
72 | |
73 return false; | |
74 } | |
75 | |
76 /** | |
77 * Creates a new Image on the given device. Note that we defined a new | |
78 * method rather than overloading createImage since this needs to be | |
79 * called by getImageData(), and we want to be absolutely certain not | |
80 * to cause infinite recursion if the base class gets refactored. | |
81 * | |
82 * @param device device to create the image on | |
83 * @return a newly allocated Image. Must be disposed by calling image.dispose(). | |
84 */ | |
85 private final Image internalCreateImage(Device device) { | |
86 Image originalImage = original.createImage(device); | |
87 Image result = new Image(device, originalImage, flags); | |
88 original.destroyResource(originalImage); | |
89 return result; | |
90 } | |
91 | |
92 public override ImageData getImageData() { | |
93 Image image = internalCreateImage(Display.getCurrent()); | |
94 ImageData result = image.getImageData(); | |
95 image.dispose(); | |
96 return result; | |
97 } | |
98 | |
99 } |