Mercurial > projects > dwt-mac
annotate dwt/widgets/FontDialog.d @ 54:96d20dc95b8c
Updated FontDialog to 3.514
author | Jacob Carlborg <doob@me.com> |
---|---|
date | Mon, 08 Dec 2008 21:27:18 +0100 |
parents | d8635bb48c7c |
children | c74ba20de292 |
rev | line source |
---|---|
45 | 1 /******************************************************************************* |
2 * Copyright (c) 2000, 2008 IBM Corporation and others. | |
0
380af2bdd8e5
Upload of whole dwt tree
Jacob Carlborg <doob@me.com> <jacob.carlborg@gmail.com>
parents:
diff
changeset
|
3 * All rights reserved. This program and the accompanying materials |
380af2bdd8e5
Upload of whole dwt tree
Jacob Carlborg <doob@me.com> <jacob.carlborg@gmail.com>
parents:
diff
changeset
|
4 * are made available under the terms of the Eclipse Public License v1.0 |
380af2bdd8e5
Upload of whole dwt tree
Jacob Carlborg <doob@me.com> <jacob.carlborg@gmail.com>
parents:
diff
changeset
|
5 * which accompanies this distribution, and is available at |
380af2bdd8e5
Upload of whole dwt tree
Jacob Carlborg <doob@me.com> <jacob.carlborg@gmail.com>
parents:
diff
changeset
|
6 * http://www.eclipse.org/legal/epl-v10.html |
380af2bdd8e5
Upload of whole dwt tree
Jacob Carlborg <doob@me.com> <jacob.carlborg@gmail.com>
parents:
diff
changeset
|
7 * |
380af2bdd8e5
Upload of whole dwt tree
Jacob Carlborg <doob@me.com> <jacob.carlborg@gmail.com>
parents:
diff
changeset
|
8 * Contributors: |
380af2bdd8e5
Upload of whole dwt tree
Jacob Carlborg <doob@me.com> <jacob.carlborg@gmail.com>
parents:
diff
changeset
|
9 * IBM Corporation - initial API and implementation |
37
642f460a0908
Fixed a lot of compile errors, a "hello world" app compiles now
Jacob Carlborg <doob@me.com> <jacob.carlborg@gmail.com>
parents:
7
diff
changeset
|
10 * |
642f460a0908
Fixed a lot of compile errors, a "hello world" app compiles now
Jacob Carlborg <doob@me.com> <jacob.carlborg@gmail.com>
parents:
7
diff
changeset
|
11 * Port to the D programming language: |
45 | 12 * Jacob Carlborg <doob@me.com> |
0
380af2bdd8e5
Upload of whole dwt tree
Jacob Carlborg <doob@me.com> <jacob.carlborg@gmail.com>
parents:
diff
changeset
|
13 *******************************************************************************/ |
380af2bdd8e5
Upload of whole dwt tree
Jacob Carlborg <doob@me.com> <jacob.carlborg@gmail.com>
parents:
diff
changeset
|
14 module dwt.widgets.FontDialog; |
380af2bdd8e5
Upload of whole dwt tree
Jacob Carlborg <doob@me.com> <jacob.carlborg@gmail.com>
parents:
diff
changeset
|
15 |
380af2bdd8e5
Upload of whole dwt tree
Jacob Carlborg <doob@me.com> <jacob.carlborg@gmail.com>
parents:
diff
changeset
|
16 |
380af2bdd8e5
Upload of whole dwt tree
Jacob Carlborg <doob@me.com> <jacob.carlborg@gmail.com>
parents:
diff
changeset
|
17 import dwt.DWT; |
380af2bdd8e5
Upload of whole dwt tree
Jacob Carlborg <doob@me.com> <jacob.carlborg@gmail.com>
parents:
diff
changeset
|
18 import dwt.DWTException; |
380af2bdd8e5
Upload of whole dwt tree
Jacob Carlborg <doob@me.com> <jacob.carlborg@gmail.com>
parents:
diff
changeset
|
19 import dwt.graphics.Font; |
380af2bdd8e5
Upload of whole dwt tree
Jacob Carlborg <doob@me.com> <jacob.carlborg@gmail.com>
parents:
diff
changeset
|
20 import dwt.graphics.FontData; |
380af2bdd8e5
Upload of whole dwt tree
Jacob Carlborg <doob@me.com> <jacob.carlborg@gmail.com>
parents:
diff
changeset
|
21 import dwt.graphics.PaletteData; |
380af2bdd8e5
Upload of whole dwt tree
Jacob Carlborg <doob@me.com> <jacob.carlborg@gmail.com>
parents:
diff
changeset
|
22 import dwt.graphics.RGB; |
380af2bdd8e5
Upload of whole dwt tree
Jacob Carlborg <doob@me.com> <jacob.carlborg@gmail.com>
parents:
diff
changeset
|
23 import dwt.internal.cocoa.NSApplication; |
380af2bdd8e5
Upload of whole dwt tree
Jacob Carlborg <doob@me.com> <jacob.carlborg@gmail.com>
parents:
diff
changeset
|
24 import dwt.internal.cocoa.NSFont; |
380af2bdd8e5
Upload of whole dwt tree
Jacob Carlborg <doob@me.com> <jacob.carlborg@gmail.com>
parents:
diff
changeset
|
25 import dwt.internal.cocoa.NSFontPanel; |
380af2bdd8e5
Upload of whole dwt tree
Jacob Carlborg <doob@me.com> <jacob.carlborg@gmail.com>
parents:
diff
changeset
|
26 import dwt.internal.cocoa.NSString; |
380af2bdd8e5
Upload of whole dwt tree
Jacob Carlborg <doob@me.com> <jacob.carlborg@gmail.com>
parents:
diff
changeset
|
27 import dwt.internal.cocoa.OS; |
380af2bdd8e5
Upload of whole dwt tree
Jacob Carlborg <doob@me.com> <jacob.carlborg@gmail.com>
parents:
diff
changeset
|
28 import dwt.internal.cocoa.SWTPanelDelegate; |
380af2bdd8e5
Upload of whole dwt tree
Jacob Carlborg <doob@me.com> <jacob.carlborg@gmail.com>
parents:
diff
changeset
|
29 |
37
642f460a0908
Fixed a lot of compile errors, a "hello world" app compiles now
Jacob Carlborg <doob@me.com> <jacob.carlborg@gmail.com>
parents:
7
diff
changeset
|
30 import dwt.dwthelper.utils; |
54 | 31 import dwt.internal.objc.cocoa.Cocoa; |
37
642f460a0908
Fixed a lot of compile errors, a "hello world" app compiles now
Jacob Carlborg <doob@me.com> <jacob.carlborg@gmail.com>
parents:
7
diff
changeset
|
32 import dwt.widgets.Dialog; |
642f460a0908
Fixed a lot of compile errors, a "hello world" app compiles now
Jacob Carlborg <doob@me.com> <jacob.carlborg@gmail.com>
parents:
7
diff
changeset
|
33 import dwt.widgets.Display; |
642f460a0908
Fixed a lot of compile errors, a "hello world" app compiles now
Jacob Carlborg <doob@me.com> <jacob.carlborg@gmail.com>
parents:
7
diff
changeset
|
34 import dwt.widgets.Shell; |
642f460a0908
Fixed a lot of compile errors, a "hello world" app compiles now
Jacob Carlborg <doob@me.com> <jacob.carlborg@gmail.com>
parents:
7
diff
changeset
|
35 |
0
380af2bdd8e5
Upload of whole dwt tree
Jacob Carlborg <doob@me.com> <jacob.carlborg@gmail.com>
parents:
diff
changeset
|
36 /** |
380af2bdd8e5
Upload of whole dwt tree
Jacob Carlborg <doob@me.com> <jacob.carlborg@gmail.com>
parents:
diff
changeset
|
37 * Instances of this class allow the user to select a font |
380af2bdd8e5
Upload of whole dwt tree
Jacob Carlborg <doob@me.com> <jacob.carlborg@gmail.com>
parents:
diff
changeset
|
38 * from all available fonts in the system. |
380af2bdd8e5
Upload of whole dwt tree
Jacob Carlborg <doob@me.com> <jacob.carlborg@gmail.com>
parents:
diff
changeset
|
39 * <dl> |
380af2bdd8e5
Upload of whole dwt tree
Jacob Carlborg <doob@me.com> <jacob.carlborg@gmail.com>
parents:
diff
changeset
|
40 * <dt><b>Styles:</b></dt> |
380af2bdd8e5
Upload of whole dwt tree
Jacob Carlborg <doob@me.com> <jacob.carlborg@gmail.com>
parents:
diff
changeset
|
41 * <dd>(none)</dd> |
380af2bdd8e5
Upload of whole dwt tree
Jacob Carlborg <doob@me.com> <jacob.carlborg@gmail.com>
parents:
diff
changeset
|
42 * <dt><b>Events:</b></dt> |
380af2bdd8e5
Upload of whole dwt tree
Jacob Carlborg <doob@me.com> <jacob.carlborg@gmail.com>
parents:
diff
changeset
|
43 * <dd>(none)</dd> |
380af2bdd8e5
Upload of whole dwt tree
Jacob Carlborg <doob@me.com> <jacob.carlborg@gmail.com>
parents:
diff
changeset
|
44 * </dl> |
380af2bdd8e5
Upload of whole dwt tree
Jacob Carlborg <doob@me.com> <jacob.carlborg@gmail.com>
parents:
diff
changeset
|
45 * <p> |
380af2bdd8e5
Upload of whole dwt tree
Jacob Carlborg <doob@me.com> <jacob.carlborg@gmail.com>
parents:
diff
changeset
|
46 * IMPORTANT: This class is intended to be subclassed <em>only</em> |
380af2bdd8e5
Upload of whole dwt tree
Jacob Carlborg <doob@me.com> <jacob.carlborg@gmail.com>
parents:
diff
changeset
|
47 * within the DWT implementation. |
380af2bdd8e5
Upload of whole dwt tree
Jacob Carlborg <doob@me.com> <jacob.carlborg@gmail.com>
parents:
diff
changeset
|
48 * </p> |
45 | 49 * |
50 * @see <a href="http://www.eclipse.org/swt/examples.php">DWT Example: ControlExample, Dialog tab</a> | |
51 * @see <a href="http://www.eclipse.org/swt/">Sample code and further information</a> | |
0
380af2bdd8e5
Upload of whole dwt tree
Jacob Carlborg <doob@me.com> <jacob.carlborg@gmail.com>
parents:
diff
changeset
|
52 */ |
3 | 53 public class FontDialog : Dialog { |
0
380af2bdd8e5
Upload of whole dwt tree
Jacob Carlborg <doob@me.com> <jacob.carlborg@gmail.com>
parents:
diff
changeset
|
54 FontData fontData; |
380af2bdd8e5
Upload of whole dwt tree
Jacob Carlborg <doob@me.com> <jacob.carlborg@gmail.com>
parents:
diff
changeset
|
55 RGB rgb; |
45 | 56 bool selected; |
0
380af2bdd8e5
Upload of whole dwt tree
Jacob Carlborg <doob@me.com> <jacob.carlborg@gmail.com>
parents:
diff
changeset
|
57 int fontID, fontSize; |
37
642f460a0908
Fixed a lot of compile errors, a "hello world" app compiles now
Jacob Carlborg <doob@me.com> <jacob.carlborg@gmail.com>
parents:
7
diff
changeset
|
58 |
54 | 59 /** |
60 * Constructs a new instance of this class given only its parent. | |
61 * | |
62 * @param parent a shell which will be the parent of the new instance | |
63 * | |
64 * @exception IllegalArgumentException <ul> | |
65 * <li>ERROR_NULL_ARGUMENT - if the parent is null</li> | |
66 * </ul> | |
67 * @exception DWTException <ul> | |
68 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the parent</li> | |
69 * <li>ERROR_INVALID_SUBCLASS - if this class is not an allowed subclass</li> | |
70 * </ul> | |
71 */ | |
72 public this (Shell parent) { | |
73 this (parent, DWT.APPLICATION_MODAL); | |
74 } | |
75 | |
76 /** | |
77 * Constructs a new instance of this class given its parent | |
78 * and a style value describing its behavior and appearance. | |
79 * <p> | |
80 * The style value is either one of the style constants defined in | |
81 * class <code>DWT</code> which is applicable to instances of this | |
82 * class, or must be built by <em>bitwise OR</em>'ing together | |
83 * (that is, using the <code>int</code> "|" operator) two or more | |
84 * of those <code>DWT</code> style constants. The class description | |
85 * lists the style constants that are applicable to the class. | |
86 * Style bits are also inherited from superclasses. | |
87 * </p> | |
88 * | |
89 * @param parent a shell which will be the parent of the new instance | |
90 * @param style the style of dialog to construct | |
91 * | |
92 * @exception IllegalArgumentException <ul> | |
93 * <li>ERROR_NULL_ARGUMENT - if the parent is null</li> | |
94 * </ul> | |
95 * @exception DWTException <ul> | |
96 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the parent</li> | |
97 * <li>ERROR_INVALID_SUBCLASS - if this class is not an allowed subclass</li> | |
98 * </ul> | |
99 */ | |
37
642f460a0908
Fixed a lot of compile errors, a "hello world" app compiles now
Jacob Carlborg <doob@me.com> <jacob.carlborg@gmail.com>
parents:
7
diff
changeset
|
100 public this (Shell parent, int style) { |
45 | 101 super (parent, checkStyle (parent, style)); |
37
642f460a0908
Fixed a lot of compile errors, a "hello world" app compiles now
Jacob Carlborg <doob@me.com> <jacob.carlborg@gmail.com>
parents:
7
diff
changeset
|
102 checkSubclass (); |
642f460a0908
Fixed a lot of compile errors, a "hello world" app compiles now
Jacob Carlborg <doob@me.com> <jacob.carlborg@gmail.com>
parents:
7
diff
changeset
|
103 } |
642f460a0908
Fixed a lot of compile errors, a "hello world" app compiles now
Jacob Carlborg <doob@me.com> <jacob.carlborg@gmail.com>
parents:
7
diff
changeset
|
104 |
45 | 105 void changeFont(objc.id id, objc.SEL sel, objc.id arg0) { |
106 selected = true; | |
37
642f460a0908
Fixed a lot of compile errors, a "hello world" app compiles now
Jacob Carlborg <doob@me.com> <jacob.carlborg@gmail.com>
parents:
7
diff
changeset
|
107 } |
642f460a0908
Fixed a lot of compile errors, a "hello world" app compiles now
Jacob Carlborg <doob@me.com> <jacob.carlborg@gmail.com>
parents:
7
diff
changeset
|
108 |
642f460a0908
Fixed a lot of compile errors, a "hello world" app compiles now
Jacob Carlborg <doob@me.com> <jacob.carlborg@gmail.com>
parents:
7
diff
changeset
|
109 /** |
54 | 110 * Returns a FontData object describing the font that was |
111 * selected in the dialog, or null if none is available. | |
112 * | |
113 * @return the FontData for the selected font, or null | |
114 * @deprecated use #getFontList () | |
115 */ | |
116 public FontData getFontData () { | |
117 return fontData; | |
118 } | |
119 | |
120 /** | |
121 * Returns a FontData set describing the font that was | |
122 * selected in the dialog, or null if none is available. | |
123 * | |
124 * @return the FontData for the selected font, or null | |
125 * @since 2.1.1 | |
126 */ | |
127 public FontData [] getFontList () { | |
128 if (fontData is null) return null; | |
129 FontData [] result = new FontData [1]; | |
130 result [0] = fontData; | |
131 return result; | |
132 } | |
133 | |
134 /** | |
135 * Returns an RGB describing the color that was selected | |
136 * in the dialog, or null if none is available. | |
137 * | |
138 * @return the RGB value for the selected color, or null | |
139 * | |
140 * @see PaletteData#getRGBs | |
141 * | |
142 * @since 2.1 | |
143 */ | |
144 public RGB getRGB () { | |
145 return rgb; | |
146 } | |
147 | |
148 //int fontProc (int nextHandler, int theEvent, int userData) { | |
149 // int kind = OS.GetEventKind (theEvent); | |
150 // switch (kind) { | |
151 // case OS.kEventFontPanelClosed: | |
152 // open = false; | |
153 // break; | |
154 // case OS.kEventFontSelection: | |
155 // int [] fontID = new int [1]; | |
156 // if (OS.GetEventParameter (theEvent, OS.kEventParamATSUFontID, OS.typeUInt32, null, 4, null, fontID) is OS.noErr) { | |
157 // this.fontID = fontID [0]; | |
158 // } | |
159 // int [] fontSize = new int [1]; | |
160 // if (OS.GetEventParameter (theEvent, OS.kEventParamATSUFontSize, OS.typeFixed, null, 4, null, fontSize) is OS.noErr) { | |
161 // this.fontSize = fontSize [0]; | |
162 // } | |
163 // RGBColor color = new RGBColor (); | |
164 // int [] actualSize = new int [1]; | |
165 // if (OS.GetEventParameter (theEvent, OS.kEventParamFontColor, OS.typeRGBColor, null, RGBColor.sizeof, actualSize, color) is OS.noErr) { | |
166 // int red = (color.red >> 8) & 0xFF; | |
167 // int green = (color.green >> 8) & 0xFF; | |
168 // int blue = (color.blue >> 8) & 0xFF; | |
169 // rgb = new RGB (red, green, blue); | |
170 // } else { | |
171 // int [] dict = new int [1]; | |
172 // if (OS.GetEventParameter (theEvent, OS.kEventParamDictionary, OS.typeCFDictionaryRef, null, 4, actualSize, dict) is OS.noErr) { | |
173 // int [] attrib = new int [1]; | |
174 // if (OS.CFDictionaryGetValueIfPresent (dict [0], OS.kFontPanelAttributesKey (), attrib)) { | |
175 // int [] tags = new int [1]; | |
176 // int [] sizes = new int [1]; | |
177 // int [] values = new int [1]; | |
178 // if (OS.CFDictionaryGetValueIfPresent (attrib [0], OS.kFontPanelAttributeTagsKey (), tags) && | |
179 // OS.CFDictionaryGetValueIfPresent (attrib [0], OS.kFontPanelAttributeSizesKey (), sizes) && | |
180 // OS.CFDictionaryGetValueIfPresent (attrib [0], OS.kFontPanelAttributeValuesKey (), values) | |
181 // ) { | |
182 // int count = OS.CFDataGetLength (tags [0]) / 4; | |
183 // int tagPtr = OS.CFDataGetBytePtr (tags[0]); | |
184 // int sizePtr = OS.CFDataGetBytePtr (sizes [0]); | |
185 // int [] tag = new int [1]; | |
186 // int [] size = new int [1]; | |
187 // int valueOffset = 0; | |
188 // for (int i = 0 ; i < count ; i++) { | |
189 // OS.memmove (tag, tagPtr + (i * 4), 4); | |
190 // OS.memmove (size, sizePtr + (i * 4), 4); | |
191 // if (tag [0] is OS.kATSUColorTag && size[0] is RGBColor.sizeof) { | |
192 // int valuePtr = OS.CFDataGetBytePtr (values [0]); | |
193 // OS.memmove (color, valuePtr + valueOffset, RGBColor.sizeof); | |
194 // int red = (color.red >> 8) & 0xFF; | |
195 // int green = (color.green >> 8) & 0xFF; | |
196 // int blue = (color.blue >> 8) & 0xFF; | |
197 // rgb = new RGB (red, green, blue); | |
198 // break ; | |
199 // } | |
200 // valueOffset = size[0]; | |
201 // } | |
202 // } | |
203 // } | |
204 // } | |
205 // } | |
206 // break; | |
207 // } | |
208 // return OS.noErr; | |
209 //} | |
210 | |
211 /** | |
212 * Makes the dialog visible and brings it to the front | |
213 * of the display. | |
214 * | |
215 * @return a FontData object describing the font that was selected, | |
216 * or null if the dialog was cancelled or an error occurred | |
217 * | |
218 * @exception DWTException <ul> | |
219 * <li>ERROR_WIDGET_DISPOSED - if the dialog has been disposed</li> | |
220 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the dialog</li> | |
221 * </ul> | |
222 */ | |
223 public FontData open () { | |
45 | 224 Display display = parent !is null ? parent.display : Display.getCurrent (); |
54 | 225 NSFontPanel panel = NSFontPanel.sharedFontPanel(); |
226 panel.setTitle(NSString.stringWith(title !is null ? title : "")); | |
45 | 227 bool create = fontData !is null; |
228 Font font = create ? new Font(display, fontData) : display.getSystemFont(); | |
229 panel.setPanelFont(font.handle, false); | |
54 | 230 SWTPanelDelegate delegate_ = cast(SWTPanelDelegate)(new SWTPanelDelegate()).alloc().init(); |
231 void* jniRef = OS.NewGlobalRef(this); | |
232 if (jniRef is null) DWT.error(DWT.ERROR_NO_HANDLES); | |
233 OS.object_setInstanceVariable(delegate_.id, Display.SWT_OBJECT, jniRef); | |
234 panel.setDelegate(delegate_); | |
235 fontData = null; | |
45 | 236 selected = false; |
54 | 237 panel.orderFront(null); |
45 | 238 NSApplication.sharedApplication().runModalForWindow(panel); |
239 if (selected) { | |
240 NSFont nsFont = panel.panelConvertFont(font.handle); | |
241 if (nsFont !is null) { | |
242 fontData = Font.cocoa_new(display, nsFont).getFontData()[0]; | |
243 } | |
244 } | |
54 | 245 panel.setDelegate(null); |
246 delegate_.release(); | |
45 | 247 if (create) font.dispose(); |
54 | 248 return fontData; |
249 } | |
250 | |
251 /** | |
252 * Sets a FontData object describing the font to be | |
253 * selected by default in the dialog, or null to let | |
254 * the platform choose one. | |
255 * | |
256 * @param fontData the FontData to use initially, or null | |
257 * @deprecated use #setFontList (FontData []) | |
258 */ | |
259 public void setFontData (FontData fontData) { | |
260 this.fontData = fontData; | |
261 } | |
262 | |
263 /** | |
264 * Sets the set of FontData objects describing the font to | |
265 * be selected by default in the dialog, or null to let | |
266 * the platform choose one. | |
267 * | |
268 * @param fontData the set of FontData objects to use initially, or null | |
269 * to let the platform select a default when open() is called | |
270 * | |
271 * @see Font#getFontData | |
272 * | |
273 * @since 2.1.1 | |
274 */ | |
275 public void setFontList (FontData [] fontData) { | |
276 if (fontData !is null && fontData.length > 0) { | |
277 this.fontData = fontData [0]; | |
278 } else { | |
279 this.fontData = null; | |
0
380af2bdd8e5
Upload of whole dwt tree
Jacob Carlborg <doob@me.com> <jacob.carlborg@gmail.com>
parents:
diff
changeset
|
280 } |
54 | 281 } |
282 | |
283 /** | |
284 * Sets the RGB describing the color to be selected by default | |
285 * in the dialog, or null to let the platform choose one. | |
286 * | |
287 * @param rgb the RGB value to use initially, or null to let | |
288 * the platform select a default when open() is called | |
289 * | |
290 * @see PaletteData#getRGBs | |
291 * | |
292 * @since 2.1 | |
293 */ | |
294 public void setRGB (RGB rgb) { | |
295 this.rgb = rgb; | |
296 } | |
37
642f460a0908
Fixed a lot of compile errors, a "hello world" app compiles now
Jacob Carlborg <doob@me.com> <jacob.carlborg@gmail.com>
parents:
7
diff
changeset
|
297 |
45 | 298 void windowWillClose(objc.id id, objc.SEL sel, objc.id sender) { |
54 | 299 NSApplication.sharedApplication().stop(null); |
300 } | |
301 | |
302 } |