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 : Cairo;
25 import org.eclipse.swt.internal.gtk.OS;
26 
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  *
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">SWT Example: GraphicsExample</a>
43  * @see <a href="http://www.eclipse.org/swt/">Sample code and further information</a>
44  *
45  * @since 3.1
46  */
47 public class Pattern : Resource {
48 
49     /**
50      * the OS resource for the Pattern
51      * (Warning: This field is platform dependent)
52      * <p>
53      * <b>IMPORTANT:</b> This field is <em>not</em> part of the SWT
54      * public API. It is marked public only so that it can be shared
55      * within the packages provided by SWT. It is not available on all
56      * platforms and should never be accessed from application code.
57      * </p>
58      */
59     public org.eclipse.swt.internal.gtk.OS.cairo_pattern_t* handle;
60 
61     org.eclipse.swt.internal.gtk.OS.cairo_surface_t * surface;
62 
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>
79  * @exception SWTException <ul>
80  *    <li>ERROR_NO_GRAPHICS_LIBRARY - if advanced graphics are not available</li>
81  * </ul>
82  * @exception SWTError <ul>
83  *    <li>ERROR_NO_HANDLES if a handle for the pattern could not be obtained</li>
84  * </ul>
85  *
86  * @see #dispose()
87  */
88 public this(Device device, Image image) {
89     super(device);
90     if (image is null) SWT.error(SWT.ERROR_NULL_ARGUMENT);
91     if (image.isDisposed()) SWT.error(SWT.ERROR_INVALID_ARGUMENT);
92     this.device.checkCairo();
93     image.createSurface();
94     handle = Cairo.cairo_pattern_create_for_surface(image.surface);
95     if (handle is null) SWT.error(SWT.ERROR_NO_HANDLES);
96     Cairo.cairo_pattern_set_extend(handle, Cairo.CAIRO_EXTEND_REPEAT);
97     surface = image.surface;
98     init_();
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>
124  * @exception SWTException <ul>
125  *    <li>ERROR_NO_GRAPHICS_LIBRARY - if advanced graphics are not available</li>
126  * </ul>
127  * @exception SWTError <ul>
128  *    <li>ERROR_NO_HANDLES if a handle for the pattern could not be obtained</li>
129  * </ul>
130  *
131  * @see #dispose()
132  */
133 public this(Device device, float x1, float y1, float x2, float y2, Color color1, Color color2) {
134     this(device, x1, y1, x2, y2, color1, 0xFF, color2, 0xFF);
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>
161  * @exception SWTException <ul>
162  *    <li>ERROR_NO_GRAPHICS_LIBRARY - if advanced graphics are not available</li>
163  * </ul>
164  * @exception SWTError <ul>
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  */
172 public this(Device device, float x1, float y1, float x2, float y2, Color color1, int alpha1, Color color2, int alpha2) {
173     super(device);
174     if (color1 is null) SWT.error(SWT.ERROR_NULL_ARGUMENT);
175     if (color1.isDisposed()) SWT.error(SWT.ERROR_INVALID_ARGUMENT);
176     if (color2 is null) SWT.error(SWT.ERROR_NULL_ARGUMENT);
177     if (color2.isDisposed()) SWT.error(SWT.ERROR_INVALID_ARGUMENT);
178     this.device.checkCairo();
179     handle = Cairo.cairo_pattern_create_linear(x1, y1, x2, y2);
180     if (handle is null) SWT.error(SWT.ERROR_NO_HANDLES);
181     GC.setCairoPatternColor(handle, 0, color1, alpha1);
182     GC.setCairoPatternColor(handle, 1, color2, alpha2);
183     Cairo.cairo_pattern_set_extend(handle, Cairo.CAIRO_EXTEND_REPEAT);
184     init_();
185 }
186 
187 override
188 void destroy() {
189     Cairo.cairo_pattern_destroy(handle);
190     handle = null;
191     surface = null;
192 }
193 
194 /**
195  * Returns <code>true</code> if the Pattern has been disposed,
196  * and <code>false</code> otherwise.
197  * <p>
198  * This method gets the dispose state for the Pattern.
199  * When a Pattern has been disposed, it is an error to
200  * invoke any other method using the Pattern.
201  *
202  * @return <code>true</code> when the Pattern is disposed, and <code>false</code> otherwise
203  */
204 public override bool isDisposed() {
205     return handle is null;
206 }
207 
208 /**
209  * Returns a string containing a concise, human-readable
210  * description of the receiver.
211  *
212  * @return a string representation of the receiver
213  */
214 public override String toString() {
215     if (isDisposed()) return "Pattern {*DISPOSED*}";
216     return Format( "Pattern {{{}}", handle );
217 }
218 
219 }