comparison dwt/widgets/FontDialog.d @ 37:642f460a0908

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