comparison org.eclipse.swt.win32.win32.x86/src/org/eclipse/swt/graphics/Font.d @ 0:6dd524f61e62

add dwt win and basic java stuff
author Frank Benoit <benoit@tionex.de>
date Mon, 02 Mar 2009 14:44:16 +0100
parents
children 6bf2837c50fe
comparison
equal deleted inserted replaced
-1:000000000000 0:6dd524f61e62
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.Font;
14
15 import org.eclipse.swt.SWT;
16 import org.eclipse.swt.SWTError;
17 import org.eclipse.swt.SWTException;
18 import org.eclipse.swt.internal.win32.OS;
19
20 import org.eclipse.swt.graphics.Resource;
21 import org.eclipse.swt.graphics.FontData;
22 import org.eclipse.swt.graphics.Device;
23
24 import tango.text.convert.Format;
25 import java.lang.all;
26 //import tango.stdc.stringz;
27
28 /**
29 * Instances of this class manage operating system resources that
30 * define how text looks when it is displayed. Fonts may be constructed
31 * by providing a device and either name, size and style information
32 * or a <code>FontData</code> object which encapsulates this data.
33 * <p>
34 * Application code must explicitly invoke the <code>Font.dispose()</code>
35 * method to release the operating system resources managed by each instance
36 * when those instances are no longer required.
37 * </p>
38 *
39 * @see FontData
40 * @see <a href="http://www.eclipse.org/swt/snippets/#font">Font snippets</a>
41 * @see <a href="http://www.eclipse.org/swt/examples.php">SWT Examples: GraphicsExample, PaintExample</a>
42 * @see <a href="http://www.eclipse.org/swt/">Sample code and further information</a>
43 */
44
45 public final class Font : Resource {
46
47 alias Resource.init_ init_;
48 /**
49 * the handle to the OS font resource
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 HFONT handle;
59
60 /**
61 * Prevents uninitialized instances from being created outside the package.
62 */
63 this(Device device) {
64 super(device);
65 }
66
67 /**
68 * Constructs a new font given a device and font data
69 * which describes the desired font's appearance.
70 * <p>
71 * You must dispose the font when it is no longer required.
72 * </p>
73 *
74 * @param device the device to create the font on
75 * @param fd the FontData that describes the desired font (must not be null)
76 *
77 * @exception IllegalArgumentException <ul>
78 * <li>ERROR_NULL_ARGUMENT - if device is null and there is no current device</li>
79 * <li>ERROR_NULL_ARGUMENT - if the fd argument is null</li>
80 * </ul>
81 * @exception SWTError <ul>
82 * <li>ERROR_NO_HANDLES - if a font could not be created from the given font data</li>
83 * </ul>
84 */
85 public this(Device device, FontData fd) {
86 super(device);
87 init_(fd);
88 init_();
89 }
90
91 /**
92 * Constructs a new font given a device and an array
93 * of font data which describes the desired font's
94 * appearance.
95 * <p>
96 * You must dispose the font when it is no longer required.
97 * </p>
98 *
99 * @param device the device to create the font on
100 * @param fds the array of FontData that describes the desired font (must not be null)
101 *
102 * @exception IllegalArgumentException <ul>
103 * <li>ERROR_NULL_ARGUMENT - if device is null and there is no current device</li>
104 * <li>ERROR_NULL_ARGUMENT - if the fds argument is null</li>
105 * <li>ERROR_INVALID_ARGUMENT - if the length of fds is zero</li>
106 * <li>ERROR_NULL_ARGUMENT - if any fd in the array is null</li>
107 * </ul>
108 * @exception SWTError <ul>
109 * <li>ERROR_NO_HANDLES - if a font could not be created from the given font data</li>
110 * </ul>
111 *
112 * @since 2.1
113 */
114 public this(Device device, FontData[] fds) {
115 super(device);
116 if (fds is null) SWT.error(SWT.ERROR_NULL_ARGUMENT);
117 if (fds.length is 0) SWT.error(SWT.ERROR_INVALID_ARGUMENT);
118 for (int i=0; i<fds.length; i++) {
119 if (fds[i] is null) SWT.error(SWT.ERROR_INVALID_ARGUMENT);
120 }
121 init_(fds[0]);
122 init_();
123 }
124
125 /**
126 * Constructs a new font given a device, a font name,
127 * the height of the desired font in points, and a font
128 * style.
129 * <p>
130 * You must dispose the font when it is no longer required.
131 * </p>
132 *
133 * @param device the device to create the font on
134 * @param name the name of the font (must not be null)
135 * @param height the font height in points
136 * @param style a bit or combination of NORMAL, BOLD, ITALIC
137 *
138 * @exception IllegalArgumentException <ul>
139 * <li>ERROR_NULL_ARGUMENT - if device is null and there is no current device</li>
140 * <li>ERROR_NULL_ARGUMENT - if the name argument is null</li>
141 * <li>ERROR_INVALID_ARGUMENT - if the height is negative</li>
142 * </ul>
143 * @exception SWTError <ul>
144 * <li>ERROR_NO_HANDLES - if a font could not be created from the given arguments</li>
145 * </ul>
146 */
147 public this(Device device, String name, int height, int style) {
148 super(device);
149 if (name is null) SWT.error(SWT.ERROR_NULL_ARGUMENT);
150 init_(new FontData (name, height, style));
151 init_();
152 }
153
154 /*public*/ this(Device device, String name, float height, int style) {
155 super(device);
156 if (name is null) SWT.error(SWT.ERROR_NULL_ARGUMENT);
157 init_(new FontData (name, height, style));
158 init_();
159 }
160 void destroy() {
161 OS.DeleteObject(handle);
162 handle = null;
163 }
164
165 /**
166 * Compares the argument to the receiver, and returns true
167 * if they represent the <em>same</em> object using a class
168 * specific comparison.
169 *
170 * @param object the object to compare with this object
171 * @return <code>true</code> if the object is the same as this object and <code>false</code> otherwise
172 *
173 * @see #hashCode
174 */
175 public int opEquals(Object object) {
176 if (object is this) return true;
177 if ( auto font = cast(Font)object ){
178 return device is font.device && handle is font.handle;
179 }
180 return false;
181 }
182
183 /**
184 * Returns an array of <code>FontData</code>s representing the receiver.
185 * On Windows, only one FontData will be returned per font. On X however,
186 * a <code>Font</code> object <em>may</em> be composed of multiple X
187 * fonts. To support this case, we return an array of font data objects.
188 *
189 * @return an array of font data objects describing the receiver
190 *
191 * @exception SWTException <ul>
192 * <li>ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed</li>
193 * </ul>
194 */
195 public FontData[] getFontData() {
196 if (isDisposed()) SWT.error(SWT.ERROR_GRAPHIC_DISPOSED);
197 LOGFONT logFont;
198 OS.GetObject(handle, LOGFONT.sizeof, &logFont);
199 return [ cast(FontData) FontData.win32_new(&logFont, device.computePoints(&logFont, handle))];
200 }
201
202 /**
203 * Returns an integer hash code for the receiver. Any two
204 * objects that return <code>true</code> when passed to
205 * <code>equals</code> must return the same value for this
206 * method.
207 *
208 * @return the receiver's hash
209 *
210 * @see #equals
211 */
212 override public hash_t toHash () {
213 return cast(hash_t)handle;
214 }
215
216 void init_ (FontData fd) {
217 if (fd is null) SWT.error(SWT.ERROR_NULL_ARGUMENT);
218 LOGFONT* logFont = &fd.data;
219 int lfHeight = logFont.lfHeight;
220 logFont.lfHeight = device.computePixels(fd.height);
221 handle = OS.CreateFontIndirect(logFont);
222 logFont.lfHeight = lfHeight;
223 if (handle is null) SWT.error(SWT.ERROR_NO_HANDLES);
224 }
225
226 /**
227 * Returns <code>true</code> if the font has been disposed,
228 * and <code>false</code> otherwise.
229 * <p>
230 * This method gets the dispose state for the font.
231 * When a font has been disposed, it is an error to
232 * invoke any other method using the font.
233 *
234 * @return <code>true</code> when the font is disposed and <code>false</code> otherwise
235 */
236 override public bool isDisposed() {
237 return handle is null;
238 }
239
240 /**
241 * Returns a string containing a concise, human-readable
242 * description of the receiver.
243 *
244 * @return a string representation of the receiver
245 */
246 override public String toString () {
247 if (isDisposed()) return "Font {*DISPOSED*}";
248 return Format( "Font {{{}}", handle );
249 //
250 // LOGFONT logFont;
251 // OS.GetObject(handle, LOGFONT.sizeof, &logFont);
252 // char[] name = .TCHARzToStr( logFont.lfFaceName.ptr, -1 );
253 //
254 // return Format( "Font {{{}, {}}", handle, name );
255 }
256
257 /**
258 * Invokes platform specific functionality to allocate a new font.
259 * <p>
260 * <b>IMPORTANT:</b> This method is <em>not</em> part of the public
261 * API for <code>Font</code>. It is marked public only so that it
262 * can be shared within the packages provided by SWT. It is not
263 * available on all platforms, and should never be called from
264 * application code.
265 * </p>
266 *
267 * @param device the device on which to allocate the color
268 * @param handle the handle for the font
269 * @return a new font object containing the specified device and handle
270 */
271 public static Font win32_new(Device device, HFONT handle) {
272 Font font = new Font(device);
273 font.disposeChecking = false;
274 font.handle = handle;
275 return font;
276 }
277
278 }