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