Mercurial > projects > dwt-addons
comparison dwtx/jface/resource/DerivedImageDescriptor.d @ 4:c87617952847
some JFace modules
author | Frank Benoit <benoit@tionex.de> |
---|---|
date | Fri, 28 Mar 2008 17:08:33 +0100 |
parents | |
children | ea8ff534f622 |
comparison
equal
deleted
inserted
replaced
3:6518c18a01f7 | 4:c87617952847 |
---|---|
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 dwtx.jface.resource.DerivedImageDescriptor; | |
14 | |
15 import dwtx.jface.resource.ImageDescriptor; | |
16 import dwtx.jface.resource.DeviceResourceException; | |
17 | |
18 import dwt.DWT; | |
19 import dwt.DWTException; | |
20 import dwt.graphics.Device; | |
21 import dwt.graphics.Image; | |
22 import dwt.graphics.ImageData; | |
23 import dwt.widgets.Display; | |
24 | |
25 import dwt.dwthelper.utils; | |
26 | |
27 /** | |
28 * An image descriptor which creates images based on another ImageDescriptor, but with | |
29 * additional DWT flags. Note that this is only intended for compatibility. | |
30 * | |
31 * @since 3.1 | |
32 */ | |
33 final class DerivedImageDescriptor : ImageDescriptor { | |
34 | |
35 private ImageDescriptor original; | |
36 private int flags; | |
37 | |
38 /** | |
39 * Create a new image descriptor | |
40 * @param original the original one | |
41 * @param swtFlags flags to be used when image is created {@link Image#Image(Device, Image, int)} | |
42 * @see DWT#IMAGE_COPY | |
43 * @see DWT#IMAGE_DISABLE | |
44 * @see DWT#IMAGE_GRAY | |
45 */ | |
46 public this(ImageDescriptor original, int swtFlags) { | |
47 this.original = original; | |
48 flags = swtFlags; | |
49 } | |
50 | |
51 public Object createResource(Device device) { | |
52 try { | |
53 return internalCreateImage(device); | |
54 } catch (DWTException e) { | |
55 throw new DeviceResourceException(this, e); | |
56 } | |
57 } | |
58 | |
59 public Image createImage(Device device) { | |
60 return internalCreateImage(device); | |
61 } | |
62 | |
63 public override hash_t toHash() { | |
64 return original.toHash() + flags; | |
65 } | |
66 | |
67 public override int opEquals(Object arg0) { | |
68 if ( auto desc = cast(DerivedImageDescriptor)arg0 ) { | |
69 return desc.original is original && flags is desc.flags; | |
70 } | |
71 | |
72 return false; | |
73 } | |
74 | |
75 /** | |
76 * Creates a new Image on the given device. Note that we defined a new | |
77 * method rather than overloading createImage since this needs to be | |
78 * called by getImageData(), and we want to be absolutely certain not | |
79 * to cause infinite recursion if the base class gets refactored. | |
80 * | |
81 * @param device device to create the image on | |
82 * @return a newly allocated Image. Must be disposed by calling image.dispose(). | |
83 */ | |
84 private final Image internalCreateImage(Device device) { | |
85 Image originalImage = original.createImage(device); | |
86 Image result = new Image(device, originalImage, flags); | |
87 original.destroyResource(originalImage); | |
88 return result; | |
89 } | |
90 | |
91 public ImageData getImageData() { | |
92 Image image = internalCreateImage(Display.getCurrent()); | |
93 ImageData result = image.getImageData(); | |
94 image.dispose(); | |
95 return result; | |
96 } | |
97 | |
98 } |