Mercurial > projects > dwt-linux
annotate dwt/graphics/Pattern.d @ 259:c0d810de7093
Update SWT 3.4M7 to 3.4
author | Frank Benoit <benoit@tionex.de> |
---|---|
date | Sun, 29 Jun 2008 14:33:38 +0200 |
parents | ce446666f5a2 |
children |
rev | line source |
---|---|
72 | 1 /******************************************************************************* |
259 | 2 * Copyright (c) 2000, 2008 IBM Corporation and others. |
18 | 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 | |
72 | 10 * Port to the D programming language: |
11 * Frank Benoit <benoit@tionex.de> | |
18 | 12 *******************************************************************************/ |
13 module dwt.graphics.Pattern; | |
14 | |
238 | 15 import dwt.dwthelper.utils; |
16 | |
21 | 17 |
71 | 18 import dwt.DWT; |
21 | 19 import dwt.graphics.Resource; |
20 import dwt.graphics.Color; | |
21 import dwt.graphics.GC; | |
22 import dwt.graphics.Device; | |
23 import dwt.graphics.Image; | |
24 import dwt.internal.cairo.Cairo; | |
25 | |
26 import tango.text.convert.Format; | |
18 | 27 |
28 /** | |
29 * Instances of this class represent patterns to use while drawing. Patterns | |
30 * can be specified either as bitmaps or gradients. | |
31 * <p> | |
32 * Application code must explicitly invoke the <code>Pattern.dispose()</code> | |
33 * method to release the operating system resources managed by each instance | |
34 * when those instances are no longer required. | |
35 * </p> | |
36 * <p> | |
37 * This class requires the operating system's advanced graphics subsystem | |
38 * which may not be available on some platforms. | |
39 * </p> | |
40 * | |
259 | 41 * @see <a href="http://www.eclipse.org/swt/snippets/#path">Path, Pattern snippets</a> |
42 * @see <a href="http://www.eclipse.org/swt/examples.php">DWT Example: GraphicsExample</a> | |
43 * @see <a href="http://www.eclipse.org/swt/">Sample code and further information</a> | |
44 * | |
18 | 45 * @since 3.1 |
46 */ | |
21 | 47 public class Pattern : Resource { |
18 | 48 |
59
8cec8f536af3
All D sources complete gvim retab (4 spaces), bug with Composite moveAbove/moveBelow fixed.
Frank Benoit <benoit@tionex.de>
parents:
33
diff
changeset
|
49 /** |
8cec8f536af3
All D sources complete gvim retab (4 spaces), bug with Composite moveAbove/moveBelow fixed.
Frank Benoit <benoit@tionex.de>
parents:
33
diff
changeset
|
50 * the OS resource for the Pattern |
8cec8f536af3
All D sources complete gvim retab (4 spaces), bug with Composite moveAbove/moveBelow fixed.
Frank Benoit <benoit@tionex.de>
parents:
33
diff
changeset
|
51 * (Warning: This field is platform dependent) |
8cec8f536af3
All D sources complete gvim retab (4 spaces), bug with Composite moveAbove/moveBelow fixed.
Frank Benoit <benoit@tionex.de>
parents:
33
diff
changeset
|
52 * <p> |
71 | 53 * <b>IMPORTANT:</b> This field is <em>not</em> part of the DWT |
59
8cec8f536af3
All D sources complete gvim retab (4 spaces), bug with Composite moveAbove/moveBelow fixed.
Frank Benoit <benoit@tionex.de>
parents:
33
diff
changeset
|
54 * public API. It is marked public only so that it can be shared |
71 | 55 * within the packages provided by DWT. It is not available on all |
59
8cec8f536af3
All D sources complete gvim retab (4 spaces), bug with Composite moveAbove/moveBelow fixed.
Frank Benoit <benoit@tionex.de>
parents:
33
diff
changeset
|
56 * platforms and should never be accessed from application code. |
8cec8f536af3
All D sources complete gvim retab (4 spaces), bug with Composite moveAbove/moveBelow fixed.
Frank Benoit <benoit@tionex.de>
parents:
33
diff
changeset
|
57 * </p> |
8cec8f536af3
All D sources complete gvim retab (4 spaces), bug with Composite moveAbove/moveBelow fixed.
Frank Benoit <benoit@tionex.de>
parents:
33
diff
changeset
|
58 */ |
8cec8f536af3
All D sources complete gvim retab (4 spaces), bug with Composite moveAbove/moveBelow fixed.
Frank Benoit <benoit@tionex.de>
parents:
33
diff
changeset
|
59 public cairo_pattern_t* handle; |
18 | 60 |
240 | 61 cairo_surface_t * surface; |
62 | |
18 | 63 /** |
64 * Constructs a new Pattern given an image. Drawing with the resulting | |
65 * pattern will cause the image to be tiled over the resulting area. | |
66 * <p> | |
67 * This operation requires the operating system's advanced | |
68 * graphics subsystem which may not be available on some | |
69 * platforms. | |
70 * </p> | |
71 * | |
72 * @param device the device on which to allocate the pattern | |
73 * @param image the image that the pattern will draw | |
74 * | |
75 * @exception IllegalArgumentException <ul> | |
76 * <li>ERROR_NULL_ARGUMENT - if the device is null and there is no current device, or the image is null</li> | |
77 * <li>ERROR_INVALID_ARGUMENT - if the image has been disposed</li> | |
78 * </ul> | |
71 | 79 * @exception DWTException <ul> |
18 | 80 * <li>ERROR_NO_GRAPHICS_LIBRARY - if advanced graphics are not available</li> |
81 * </ul> | |
71 | 82 * @exception DWTError <ul> |
18 | 83 * <li>ERROR_NO_HANDLES if a handle for the pattern could not be obtained</li> |
84 * </ul> | |
85 * | |
86 * @see #dispose() | |
87 */ | |
21 | 88 public this(Device device, Image image) { |
240 | 89 super(device); |
71 | 90 if (image is null) DWT.error(DWT.ERROR_NULL_ARGUMENT); |
91 if (image.isDisposed()) DWT.error(DWT.ERROR_INVALID_ARGUMENT); | |
240 | 92 this.device.checkCairo(); |
59
8cec8f536af3
All D sources complete gvim retab (4 spaces), bug with Composite moveAbove/moveBelow fixed.
Frank Benoit <benoit@tionex.de>
parents:
33
diff
changeset
|
93 image.createSurface(); |
8cec8f536af3
All D sources complete gvim retab (4 spaces), bug with Composite moveAbove/moveBelow fixed.
Frank Benoit <benoit@tionex.de>
parents:
33
diff
changeset
|
94 handle = Cairo.cairo_pattern_create_for_surface(image.surface); |
71 | 95 if (handle is null) DWT.error(DWT.ERROR_NO_HANDLES); |
59
8cec8f536af3
All D sources complete gvim retab (4 spaces), bug with Composite moveAbove/moveBelow fixed.
Frank Benoit <benoit@tionex.de>
parents:
33
diff
changeset
|
96 Cairo.cairo_pattern_set_extend(handle, Cairo.CAIRO_EXTEND_REPEAT); |
240 | 97 surface = image.surface; |
98 init_(); | |
18 | 99 } |
100 | |
101 /** | |
102 * Constructs a new Pattern that represents a linear, two color | |
103 * gradient. Drawing with the pattern will cause the resulting area to be | |
104 * tiled with the gradient specified by the arguments. | |
105 * <p> | |
106 * This operation requires the operating system's advanced | |
107 * graphics subsystem which may not be available on some | |
108 * platforms. | |
109 * </p> | |
110 * | |
111 * @param device the device on which to allocate the pattern | |
112 * @param x1 the x coordinate of the starting corner of the gradient | |
113 * @param y1 the y coordinate of the starting corner of the gradient | |
114 * @param x2 the x coordinate of the ending corner of the gradient | |
115 * @param y2 the y coordinate of the ending corner of the gradient | |
116 * @param color1 the starting color of the gradient | |
117 * @param color2 the ending color of the gradient | |
118 * | |
119 * @exception IllegalArgumentException <ul> | |
120 * <li>ERROR_NULL_ARGUMENT - if the device is null and there is no current device, | |
121 * or if either color1 or color2 is null</li> | |
122 * <li>ERROR_INVALID_ARGUMENT - if either color1 or color2 has been disposed</li> | |
123 * </ul> | |
71 | 124 * @exception DWTException <ul> |
18 | 125 * <li>ERROR_NO_GRAPHICS_LIBRARY - if advanced graphics are not available</li> |
126 * </ul> | |
71 | 127 * @exception DWTError <ul> |
18 | 128 * <li>ERROR_NO_HANDLES if a handle for the pattern could not be obtained</li> |
129 * </ul> | |
130 * | |
131 * @see #dispose() | |
132 */ | |
21 | 133 public this(Device device, float x1, float y1, float x2, float y2, Color color1, Color color2) { |
59
8cec8f536af3
All D sources complete gvim retab (4 spaces), bug with Composite moveAbove/moveBelow fixed.
Frank Benoit <benoit@tionex.de>
parents:
33
diff
changeset
|
134 this(device, x1, y1, x2, y2, color1, 0xFF, color2, 0xFF); |
18 | 135 } |
136 /** | |
137 * Constructs a new Pattern that represents a linear, two color | |
138 * gradient. Drawing with the pattern will cause the resulting area to be | |
139 * tiled with the gradient specified by the arguments. | |
140 * <p> | |
141 * This operation requires the operating system's advanced | |
142 * graphics subsystem which may not be available on some | |
143 * platforms. | |
144 * </p> | |
145 * | |
146 * @param device the device on which to allocate the pattern | |
147 * @param x1 the x coordinate of the starting corner of the gradient | |
148 * @param y1 the y coordinate of the starting corner of the gradient | |
149 * @param x2 the x coordinate of the ending corner of the gradient | |
150 * @param y2 the y coordinate of the ending corner of the gradient | |
151 * @param color1 the starting color of the gradient | |
152 * @param alpha1 the starting alpha value of the gradient | |
153 * @param color2 the ending color of the gradient | |
154 * @param alpha2 the ending alpha value of the gradient | |
155 * | |
156 * @exception IllegalArgumentException <ul> | |
157 * <li>ERROR_NULL_ARGUMENT - if the device is null and there is no current device, | |
158 * or if either color1 or color2 is null</li> | |
159 * <li>ERROR_INVALID_ARGUMENT - if either color1 or color2 has been disposed</li> | |
160 * </ul> | |
71 | 161 * @exception DWTException <ul> |
18 | 162 * <li>ERROR_NO_GRAPHICS_LIBRARY - if advanced graphics are not available</li> |
163 * </ul> | |
71 | 164 * @exception DWTError <ul> |
18 | 165 * <li>ERROR_NO_HANDLES if a handle for the pattern could not be obtained</li> |
166 * </ul> | |
167 * | |
168 * @see #dispose() | |
169 * | |
170 * @since 3.2 | |
171 */ | |
21 | 172 public this(Device device, float x1, float y1, float x2, float y2, Color color1, int alpha1, Color color2, int alpha2) { |
240 | 173 super(device); |
71 | 174 if (color1 is null) DWT.error(DWT.ERROR_NULL_ARGUMENT); |
175 if (color1.isDisposed()) DWT.error(DWT.ERROR_INVALID_ARGUMENT); | |
176 if (color2 is null) DWT.error(DWT.ERROR_NULL_ARGUMENT); | |
177 if (color2.isDisposed()) DWT.error(DWT.ERROR_INVALID_ARGUMENT); | |
240 | 178 this.device.checkCairo(); |
59
8cec8f536af3
All D sources complete gvim retab (4 spaces), bug with Composite moveAbove/moveBelow fixed.
Frank Benoit <benoit@tionex.de>
parents:
33
diff
changeset
|
179 handle = Cairo.cairo_pattern_create_linear(x1, y1, x2, y2); |
71 | 180 if (handle is null) DWT.error(DWT.ERROR_NO_HANDLES); |
59
8cec8f536af3
All D sources complete gvim retab (4 spaces), bug with Composite moveAbove/moveBelow fixed.
Frank Benoit <benoit@tionex.de>
parents:
33
diff
changeset
|
181 GC.setCairoPatternColor(handle, 0, color1, alpha1); |
8cec8f536af3
All D sources complete gvim retab (4 spaces), bug with Composite moveAbove/moveBelow fixed.
Frank Benoit <benoit@tionex.de>
parents:
33
diff
changeset
|
182 GC.setCairoPatternColor(handle, 1, color2, alpha2); |
8cec8f536af3
All D sources complete gvim retab (4 spaces), bug with Composite moveAbove/moveBelow fixed.
Frank Benoit <benoit@tionex.de>
parents:
33
diff
changeset
|
183 Cairo.cairo_pattern_set_extend(handle, Cairo.CAIRO_EXTEND_REPEAT); |
240 | 184 init_(); |
18 | 185 } |
186 | |
240 | 187 void destroy() { |
59
8cec8f536af3
All D sources complete gvim retab (4 spaces), bug with Composite moveAbove/moveBelow fixed.
Frank Benoit <benoit@tionex.de>
parents:
33
diff
changeset
|
188 Cairo.cairo_pattern_destroy(handle); |
8cec8f536af3
All D sources complete gvim retab (4 spaces), bug with Composite moveAbove/moveBelow fixed.
Frank Benoit <benoit@tionex.de>
parents:
33
diff
changeset
|
189 handle = null; |
240 | 190 surface = null; |
18 | 191 } |
192 | |
193 /** | |
194 * Returns <code>true</code> if the Pattern has been disposed, | |
195 * and <code>false</code> otherwise. | |
196 * <p> | |
197 * This method gets the dispose state for the Pattern. | |
198 * When a Pattern has been disposed, it is an error to | |
199 * invoke any other method using the Pattern. | |
200 * | |
201 * @return <code>true</code> when the Pattern is disposed, and <code>false</code> otherwise | |
202 */ | |
150
f2e04420fd6c
reworked overrides and superclass aliases
Frank Benoit <benoit@tionex.de>
parents:
72
diff
changeset
|
203 public override bool isDisposed() { |
59
8cec8f536af3
All D sources complete gvim retab (4 spaces), bug with Composite moveAbove/moveBelow fixed.
Frank Benoit <benoit@tionex.de>
parents:
33
diff
changeset
|
204 return handle is null; |
18 | 205 } |
206 | |
207 /** | |
208 * Returns a string containing a concise, human-readable | |
209 * description of the receiver. | |
210 * | |
211 * @return a string representation of the receiver | |
212 */ | |
238 | 213 public override String toString() { |
59
8cec8f536af3
All D sources complete gvim retab (4 spaces), bug with Composite moveAbove/moveBelow fixed.
Frank Benoit <benoit@tionex.de>
parents:
33
diff
changeset
|
214 if (isDisposed()) return "Pattern {*DISPOSED*}"; |
8cec8f536af3
All D sources complete gvim retab (4 spaces), bug with Composite moveAbove/moveBelow fixed.
Frank Benoit <benoit@tionex.de>
parents:
33
diff
changeset
|
215 return Format( "Pattern {{{}}", handle ); |
18 | 216 } |
217 | |
218 } |