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