Mercurial > projects > dwt-win
annotate dwt/graphics/Font.d @ 334:c49e17d48b76
Fix problems at GC run at main()'s end.
author | Frank Benoit <benoit@tionex.de> |
---|---|
date | Wed, 18 Feb 2009 16:14:31 +0100 |
parents | fd9c62a2998e |
children |
rev | line source |
---|---|
23 | 1 /******************************************************************************* |
246 | 2 * Copyright (c) 2000, 2008 IBM Corporation and others. |
15 | 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 | |
23 | 10 * Port to the D programming language: |
11 * Frank Benoit <benoit@tionex.de> | |
15 | 12 *******************************************************************************/ |
13 module dwt.graphics.Font; | |
14 | |
15 import dwt.DWT; | |
16 import dwt.DWTError; | |
17 import dwt.DWTException; | |
18 import dwt.internal.win32.OS; | |
19 | |
20 import dwt.graphics.Resource; | |
21 import dwt.graphics.FontData; | |
23 | 22 import dwt.graphics.Device; |
15 | 23 |
24 import tango.text.convert.Format; | |
212
ab60f3309436
reverted the char[] to String and use the an alias.
Frank Benoit <benoit@tionex.de>
parents:
203
diff
changeset
|
25 import dwt.dwthelper.utils; |
23 | 26 //import tango.stdc.stringz; |
15 | 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> | |
23 | 34 * Application code must explicitly invoke the <code>Font.dispose()</code> |
15 | 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 | |
246 | 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">DWT Examples: GraphicsExample, PaintExample</a> | |
42 * @see <a href="http://www.eclipse.org/swt/">Sample code and further information</a> | |
15 | 43 */ |
44 | |
45 public final class Font : Resource { | |
23 | 46 |
213 | 47 alias Resource.init_ init_; |
15 | 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 DWT | |
53 * public API. It is marked public only so that it can be shared | |
54 * within the packages provided by DWT. It is not available on all | |
55 * platforms and should never be accessed from application code. | |
56 * </p> | |
57 */ | |
58 public HFONT handle; | |
23 | 59 |
15 | 60 /** |
61 * Prevents uninitialized instances from being created outside the package. | |
62 */ | |
213 | 63 this(Device device) { |
64 super(device); | |
15 | 65 } |
66 | |
23 | 67 /** |
15 | 68 * Constructs a new font given a device and font data |
69 * which describes the desired font's appearance. | |
70 * <p> | |
23 | 71 * You must dispose the font when it is no longer required. |
15 | 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) | |
23 | 76 * |
15 | 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 DWTError <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) { | |
213 | 86 super(device); |
87 init_(fd); | |
88 init_(); | |
15 | 89 } |
90 | |
23 | 91 /** |
15 | 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> | |
23 | 96 * You must dispose the font when it is no longer required. |
15 | 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) | |
23 | 101 * |
15 | 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 DWTError <ul> | |
109 * <li>ERROR_NO_HANDLES - if a font could not be created from the given font data</li> | |
110 * </ul> | |
23 | 111 * |
15 | 112 * @since 2.1 |
113 */ | |
114 public this(Device device, FontData[] fds) { | |
213 | 115 super(device); |
15 | 116 if (fds is null) DWT.error(DWT.ERROR_NULL_ARGUMENT); |
117 if (fds.length is 0) DWT.error(DWT.ERROR_INVALID_ARGUMENT); | |
118 for (int i=0; i<fds.length; i++) { | |
119 if (fds[i] is null) DWT.error(DWT.ERROR_INVALID_ARGUMENT); | |
120 } | |
213 | 121 init_(fds[0]); |
122 init_(); | |
15 | 123 } |
124 | |
23 | 125 /** |
15 | 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> | |
23 | 130 * You must dispose the font when it is no longer required. |
15 | 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 | |
23 | 137 * |
15 | 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 DWTError <ul> | |
144 * <li>ERROR_NO_HANDLES - if a font could not be created from the given arguments</li> | |
145 * </ul> | |
146 */ | |
212
ab60f3309436
reverted the char[] to String and use the an alias.
Frank Benoit <benoit@tionex.de>
parents:
203
diff
changeset
|
147 public this(Device device, String name, int height, int style) { |
213 | 148 super(device); |
15 | 149 if (name is null) DWT.error(DWT.ERROR_NULL_ARGUMENT); |
213 | 150 init_(new FontData (name, height, style)); |
151 init_(); | |
15 | 152 } |
153 | |
212
ab60f3309436
reverted the char[] to String and use the an alias.
Frank Benoit <benoit@tionex.de>
parents:
203
diff
changeset
|
154 /*public*/ this(Device device, String name, float height, int style) { |
213 | 155 super(device); |
15 | 156 if (name is null) DWT.error(DWT.ERROR_NULL_ARGUMENT); |
213 | 157 init_(new FontData (name, height, style)); |
158 init_(); | |
15 | 159 } |
213 | 160 void destroy() { |
15 | 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. | |
23 | 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 | |
15 | 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 DWTException <ul> | |
192 * <li>ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed</li> | |
193 * </ul> | |
194 */ | |
195 public FontData[] getFontData() { | |
196 if (isDisposed()) DWT.error(DWT.ERROR_GRAPHIC_DISPOSED); | |
334
c49e17d48b76
Fix problems at GC run at main()'s end.
Frank Benoit <benoit@tionex.de>
parents:
246
diff
changeset
|
197 LOGFONT logFont; |
c49e17d48b76
Fix problems at GC run at main()'s end.
Frank Benoit <benoit@tionex.de>
parents:
246
diff
changeset
|
198 OS.GetObject(handle, LOGFONT.sizeof, &logFont); |
c49e17d48b76
Fix problems at GC run at main()'s end.
Frank Benoit <benoit@tionex.de>
parents:
246
diff
changeset
|
199 return [ cast(FontData) FontData.win32_new(&logFont, device.computePoints(&logFont, handle))]; |
15 | 200 } |
201 | |
202 /** | |
23 | 203 * Returns an integer hash code for the receiver. Any two |
204 * objects that return <code>true</code> when passed to | |
15 | 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 */ | |
48
9a64a7781bab
Added override and alias, first chunk. Thanks torhu for doing this patch.
Frank Benoit <benoit@tionex.de>
parents:
23
diff
changeset
|
212 override public hash_t toHash () { |
15 | 213 return cast(hash_t)handle; |
214 } | |
215 | |
213 | 216 void init_ (FontData fd) { |
15 | 217 if (fd is null) DWT.error(DWT.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) DWT.error(DWT.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 */ | |
48
9a64a7781bab
Added override and alias, first chunk. Thanks torhu for doing this patch.
Frank Benoit <benoit@tionex.de>
parents:
23
diff
changeset
|
236 override public bool isDisposed() { |
15 | 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 */ | |
212
ab60f3309436
reverted the char[] to String and use the an alias.
Frank Benoit <benoit@tionex.de>
parents:
203
diff
changeset
|
246 override public String toString () { |
15 | 247 if (isDisposed()) return "Font {*DISPOSED*}"; |
248 return Format( "Font {{{}}", handle ); | |
334
c49e17d48b76
Fix problems at GC run at main()'s end.
Frank Benoit <benoit@tionex.de>
parents:
246
diff
changeset
|
249 // |
c49e17d48b76
Fix problems at GC run at main()'s end.
Frank Benoit <benoit@tionex.de>
parents:
246
diff
changeset
|
250 // LOGFONT logFont; |
c49e17d48b76
Fix problems at GC run at main()'s end.
Frank Benoit <benoit@tionex.de>
parents:
246
diff
changeset
|
251 // OS.GetObject(handle, LOGFONT.sizeof, &logFont); |
c49e17d48b76
Fix problems at GC run at main()'s end.
Frank Benoit <benoit@tionex.de>
parents:
246
diff
changeset
|
252 // char[] name = .TCHARzToStr( logFont.lfFaceName.ptr, -1 ); |
c49e17d48b76
Fix problems at GC run at main()'s end.
Frank Benoit <benoit@tionex.de>
parents:
246
diff
changeset
|
253 // |
c49e17d48b76
Fix problems at GC run at main()'s end.
Frank Benoit <benoit@tionex.de>
parents:
246
diff
changeset
|
254 // return Format( "Font {{{}, {}}", handle, name ); |
15 | 255 } |
256 | |
23 | 257 /** |
15 | 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 DWT. 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) { | |
213 | 272 Font font = new Font(device); |
334
c49e17d48b76
Fix problems at GC run at main()'s end.
Frank Benoit <benoit@tionex.de>
parents:
246
diff
changeset
|
273 font.disposeChecking = false; |
15 | 274 font.handle = handle; |
275 return font; | |
276 } | |
277 | |
278 } |