comparison 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
comparison
equal deleted inserted replaced
53:6914d8d302de 54:96d20dc95b8c
26 import dwt.internal.cocoa.NSString; 26 import dwt.internal.cocoa.NSString;
27 import dwt.internal.cocoa.OS; 27 import dwt.internal.cocoa.OS;
28 import dwt.internal.cocoa.SWTPanelDelegate; 28 import dwt.internal.cocoa.SWTPanelDelegate;
29 29
30 import dwt.dwthelper.utils; 30 import dwt.dwthelper.utils;
31 import dwt.internal.cocoa.NSInteger; 31 import dwt.internal.objc.cocoa.Cocoa;
32 import dwt.widgets.Dialog; 32 import dwt.widgets.Dialog;
33 import dwt.widgets.Display; 33 import dwt.widgets.Display;
34 import dwt.widgets.Shell; 34 import dwt.widgets.Shell;
35 35
36 /** 36 /**
54 FontData fontData; 54 FontData fontData;
55 RGB rgb; 55 RGB rgb;
56 bool selected; 56 bool selected;
57 int fontID, fontSize; 57 int fontID, fontSize;
58 58
59 /** 59 /**
60 * Constructs a new instance of this class given only its parent. 60 * Constructs a new instance of this class given only its parent.
61 * 61 *
62 * @param parent a shell which will be the parent of the new instance 62 * @param parent a shell which will be the parent of the new instance
63 * 63 *
64 * @exception IllegalArgumentException <ul> 64 * @exception IllegalArgumentException <ul>
65 * <li>ERROR_NULL_ARGUMENT - if the parent is null</li> 65 * <li>ERROR_NULL_ARGUMENT - if the parent is null</li>
66 * </ul> 66 * </ul>
67 * @exception DWTException <ul> 67 * @exception DWTException <ul>
68 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the parent</li> 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> 69 * <li>ERROR_INVALID_SUBCLASS - if this class is not an allowed subclass</li>
70 * </ul> 70 * </ul>
71 */ 71 */
72 public this (Shell parent) { 72 public this (Shell parent) {
73 this (parent, DWT.APPLICATION_MODAL); 73 this (parent, DWT.APPLICATION_MODAL);
74 } 74 }
75 75
76 /** 76 /**
77 * Constructs a new instance of this class given its parent 77 * Constructs a new instance of this class given its parent
78 * and a style value describing its behavior and appearance. 78 * and a style value describing its behavior and appearance.
79 * <p> 79 * <p>
80 * The style value is either one of the style constants defined in 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 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 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 83 * (that is, using the <code>int</code> "|" operator) two or more
84 * of those <code>DWT</code> style constants. The class description 84 * of those <code>DWT</code> style constants. The class description
85 * lists the style constants that are applicable to the class. 85 * lists the style constants that are applicable to the class.
86 * Style bits are also inherited from superclasses. 86 * Style bits are also inherited from superclasses.
87 * </p> 87 * </p>
88 * 88 *
89 * @param parent a shell which will be the parent of the new instance 89 * @param parent a shell which will be the parent of the new instance
90 * @param style the style of dialog to construct 90 * @param style the style of dialog to construct
91 * 91 *
92 * @exception IllegalArgumentException <ul> 92 * @exception IllegalArgumentException <ul>
93 * <li>ERROR_NULL_ARGUMENT - if the parent is null</li> 93 * <li>ERROR_NULL_ARGUMENT - if the parent is null</li>
94 * </ul> 94 * </ul>
95 * @exception DWTException <ul> 95 * @exception DWTException <ul>
96 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the parent</li> 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> 97 * <li>ERROR_INVALID_SUBCLASS - if this class is not an allowed subclass</li>
98 * </ul> 98 * </ul>
99 */ 99 */
100 public this (Shell parent, int style) { 100 public this (Shell parent, int style) {
101 super (parent, checkStyle (parent, style)); 101 super (parent, checkStyle (parent, style));
102 checkSubclass (); 102 checkSubclass ();
103 } 103 }
104 104
105 void changeFont(objc.id id, objc.SEL sel, objc.id arg0) { 105 void changeFont(objc.id id, objc.SEL sel, objc.id arg0) {
106 selected = true; 106 selected = true;
107 } 107 }
108 108
109 /** 109 /**
110 * Returns a FontData object describing the font that was 110 * Returns a FontData object describing the font that was
111 * selected in the dialog, or null if none is available. 111 * selected in the dialog, or null if none is available.
112 * 112 *
113 * @return the FontData for the selected font, or null 113 * @return the FontData for the selected font, or null
114 * @deprecated use #getFontList () 114 * @deprecated use #getFontList ()
115 */ 115 */
116 public FontData getFontData () { 116 public FontData getFontData () {
117 return fontData; 117 return fontData;
118 } 118 }
119 119
120 /** 120 /**
121 * Returns a FontData set describing the font that was 121 * Returns a FontData set describing the font that was
122 * selected in the dialog, or null if none is available. 122 * selected in the dialog, or null if none is available.
123 * 123 *
124 * @return the FontData for the selected font, or null 124 * @return the FontData for the selected font, or null
125 * @since 2.1.1 125 * @since 2.1.1
126 */ 126 */
127 public FontData [] getFontList () { 127 public FontData [] getFontList () {
128 if (fontData is null) return null; 128 if (fontData is null) return null;
129 FontData [] result = new FontData [1]; 129 FontData [] result = new FontData [1];
130 result [0] = fontData; 130 result [0] = fontData;
131 return result; 131 return result;
132 } 132 }
133 133
134 /** 134 /**
135 * Returns an RGB describing the color that was selected 135 * Returns an RGB describing the color that was selected
136 * in the dialog, or null if none is available. 136 * in the dialog, or null if none is available.
137 * 137 *
138 * @return the RGB value for the selected color, or null 138 * @return the RGB value for the selected color, or null
139 * 139 *
140 * @see PaletteData#getRGBs 140 * @see PaletteData#getRGBs
141 * 141 *
142 * @since 2.1 142 * @since 2.1
143 */ 143 */
144 public RGB getRGB () { 144 public RGB getRGB () {
145 return rgb; 145 return rgb;
146 } 146 }
147 147
148 //int fontProc (int nextHandler, int theEvent, int userData) { 148 //int fontProc (int nextHandler, int theEvent, int userData) {
149 // int kind = OS.GetEventKind (theEvent); 149 // int kind = OS.GetEventKind (theEvent);
150 // switch (kind) { 150 // switch (kind) {
151 // case OS.kEventFontPanelClosed: 151 // case OS.kEventFontPanelClosed:
152 // open = false; 152 // open = false;
153 // break; 153 // break;
154 // case OS.kEventFontSelection: 154 // case OS.kEventFontSelection:
155 // int [] fontID = new int [1]; 155 // int [] fontID = new int [1];
156 // if (OS.GetEventParameter (theEvent, OS.kEventParamATSUFontID, OS.typeUInt32, null, 4, null, fontID) is OS.noErr) { 156 // if (OS.GetEventParameter (theEvent, OS.kEventParamATSUFontID, OS.typeUInt32, null, 4, null, fontID) is OS.noErr) {
157 // this.fontID = fontID [0]; 157 // this.fontID = fontID [0];
158 // } 158 // }
159 // int [] fontSize = new int [1]; 159 // int [] fontSize = new int [1];
160 // if (OS.GetEventParameter (theEvent, OS.kEventParamATSUFontSize, OS.typeFixed, null, 4, null, fontSize) is OS.noErr) { 160 // if (OS.GetEventParameter (theEvent, OS.kEventParamATSUFontSize, OS.typeFixed, null, 4, null, fontSize) is OS.noErr) {
161 // this.fontSize = fontSize [0]; 161 // this.fontSize = fontSize [0];
162 // } 162 // }
163 // RGBColor color = new RGBColor (); 163 // RGBColor color = new RGBColor ();
164 // int [] actualSize = new int [1]; 164 // int [] actualSize = new int [1];
165 // if (OS.GetEventParameter (theEvent, OS.kEventParamFontColor, OS.typeRGBColor, null, RGBColor.sizeof, actualSize, color) is OS.noErr) { 165 // if (OS.GetEventParameter (theEvent, OS.kEventParamFontColor, OS.typeRGBColor, null, RGBColor.sizeof, actualSize, color) is OS.noErr) {
166 // int red = (color.red >> 8) & 0xFF; 166 // int red = (color.red >> 8) & 0xFF;
167 // int green = (color.green >> 8) & 0xFF; 167 // int green = (color.green >> 8) & 0xFF;
168 // int blue = (color.blue >> 8) & 0xFF; 168 // int blue = (color.blue >> 8) & 0xFF;
169 // rgb = new RGB (red, green, blue); 169 // rgb = new RGB (red, green, blue);
170 // } else { 170 // } else {
171 // int [] dict = new int [1]; 171 // int [] dict = new int [1];
172 // if (OS.GetEventParameter (theEvent, OS.kEventParamDictionary, OS.typeCFDictionaryRef, null, 4, actualSize, dict) is OS.noErr) { 172 // if (OS.GetEventParameter (theEvent, OS.kEventParamDictionary, OS.typeCFDictionaryRef, null, 4, actualSize, dict) is OS.noErr) {
173 // int [] attrib = new int [1]; 173 // int [] attrib = new int [1];
174 // if (OS.CFDictionaryGetValueIfPresent (dict [0], OS.kFontPanelAttributesKey (), attrib)) { 174 // if (OS.CFDictionaryGetValueIfPresent (dict [0], OS.kFontPanelAttributesKey (), attrib)) {
175 // int [] tags = new int [1]; 175 // int [] tags = new int [1];
176 // int [] sizes = new int [1]; 176 // int [] sizes = new int [1];
177 // int [] values = new int [1]; 177 // int [] values = new int [1];
178 // if (OS.CFDictionaryGetValueIfPresent (attrib [0], OS.kFontPanelAttributeTagsKey (), tags) && 178 // if (OS.CFDictionaryGetValueIfPresent (attrib [0], OS.kFontPanelAttributeTagsKey (), tags) &&
179 // OS.CFDictionaryGetValueIfPresent (attrib [0], OS.kFontPanelAttributeSizesKey (), sizes) && 179 // OS.CFDictionaryGetValueIfPresent (attrib [0], OS.kFontPanelAttributeSizesKey (), sizes) &&
180 // OS.CFDictionaryGetValueIfPresent (attrib [0], OS.kFontPanelAttributeValuesKey (), values) 180 // OS.CFDictionaryGetValueIfPresent (attrib [0], OS.kFontPanelAttributeValuesKey (), values)
181 // ) { 181 // ) {
182 // int count = OS.CFDataGetLength (tags [0]) / 4; 182 // int count = OS.CFDataGetLength (tags [0]) / 4;
183 // int tagPtr = OS.CFDataGetBytePtr (tags[0]); 183 // int tagPtr = OS.CFDataGetBytePtr (tags[0]);
184 // int sizePtr = OS.CFDataGetBytePtr (sizes [0]); 184 // int sizePtr = OS.CFDataGetBytePtr (sizes [0]);
185 // int [] tag = new int [1]; 185 // int [] tag = new int [1];
186 // int [] size = new int [1]; 186 // int [] size = new int [1];
187 // int valueOffset = 0; 187 // int valueOffset = 0;
188 // for (int i = 0 ; i < count ; i++) { 188 // for (int i = 0 ; i < count ; i++) {
189 // OS.memmove (tag, tagPtr + (i * 4), 4); 189 // OS.memmove (tag, tagPtr + (i * 4), 4);
190 // OS.memmove (size, sizePtr + (i * 4), 4); 190 // OS.memmove (size, sizePtr + (i * 4), 4);
191 // if (tag [0] is OS.kATSUColorTag && size[0] is RGBColor.sizeof) { 191 // if (tag [0] is OS.kATSUColorTag && size[0] is RGBColor.sizeof) {
192 // int valuePtr = OS.CFDataGetBytePtr (values [0]); 192 // int valuePtr = OS.CFDataGetBytePtr (values [0]);
193 // OS.memmove (color, valuePtr + valueOffset, RGBColor.sizeof); 193 // OS.memmove (color, valuePtr + valueOffset, RGBColor.sizeof);
194 // int red = (color.red >> 8) & 0xFF; 194 // int red = (color.red >> 8) & 0xFF;
195 // int green = (color.green >> 8) & 0xFF; 195 // int green = (color.green >> 8) & 0xFF;
196 // int blue = (color.blue >> 8) & 0xFF; 196 // int blue = (color.blue >> 8) & 0xFF;
197 // rgb = new RGB (red, green, blue); 197 // rgb = new RGB (red, green, blue);
198 // break ; 198 // break ;
199 // } 199 // }
200 // valueOffset = size[0]; 200 // valueOffset = size[0];
201 // } 201 // }
202 // } 202 // }
203 // } 203 // }
204 // } 204 // }
205 // } 205 // }
206 // break; 206 // break;
207 // } 207 // }
208 // return OS.noErr; 208 // return OS.noErr;
209 //} 209 //}
210 210
211 /** 211 /**
212 * Makes the dialog visible and brings it to the front 212 * Makes the dialog visible and brings it to the front
213 * of the display. 213 * of the display.
214 * 214 *
215 * @return a FontData object describing the font that was selected, 215 * @return a FontData object describing the font that was selected,
216 * or null if the dialog was cancelled or an error occurred 216 * or null if the dialog was cancelled or an error occurred
217 * 217 *
218 * @exception DWTException <ul> 218 * @exception DWTException <ul>
219 * <li>ERROR_WIDGET_DISPOSED - if the dialog has been disposed</li> 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> 220 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the dialog</li>
221 * </ul> 221 * </ul>
222 */ 222 */
223 public FontData open () { 223 public FontData open () {
224 Display display = parent !is null ? parent.display : Display.getCurrent (); 224 Display display = parent !is null ? parent.display : Display.getCurrent ();
225 NSFontPanel panel = NSFontPanel.sharedFontPanel(); 225 NSFontPanel panel = NSFontPanel.sharedFontPanel();
226 panel.setTitle(NSString.stringWith(title !is null ? title : "")); 226 panel.setTitle(NSString.stringWith(title !is null ? title : ""));
227 bool create = fontData !is null; 227 bool create = fontData !is null;
228 Font font = create ? new Font(display, fontData) : display.getSystemFont(); 228 Font font = create ? new Font(display, fontData) : display.getSystemFont();
229 panel.setPanelFont(font.handle, false); 229 panel.setPanelFont(font.handle, false);
230 SWTPanelDelegate delegate_ = cast(SWTPanelDelegate)(new SWTPanelDelegate()).alloc().init(); 230 SWTPanelDelegate delegate_ = cast(SWTPanelDelegate)(new SWTPanelDelegate()).alloc().init();
231 char* jniRef = OS.NewGlobalRef(this); 231 void* jniRef = OS.NewGlobalRef(this);
232 if (jniRef is null) DWT.error(DWT.ERROR_NO_HANDLES); 232 if (jniRef is null) DWT.error(DWT.ERROR_NO_HANDLES);
233 OS.object_setInstanceVariable(delegate.id, Display.DWT_OBJECT, jniRef); 233 OS.object_setInstanceVariable(delegate_.id, Display.SWT_OBJECT, jniRef);
234 panel.setDelegate(delegate_); 234 panel.setDelegate(delegate_);
235 fontData = null; 235 fontData = null;
236 selected = false; 236 selected = false;
237 panel.orderFront(null); 237 panel.orderFront(null);
238 NSApplication.sharedApplication().runModalForWindow(panel); 238 NSApplication.sharedApplication().runModalForWindow(panel);
239 if (selected) { 239 if (selected) {
240 NSFont nsFont = panel.panelConvertFont(font.handle); 240 NSFont nsFont = panel.panelConvertFont(font.handle);
241 if (nsFont !is null) { 241 if (nsFont !is null) {
242 fontData = Font.cocoa_new(display, nsFont).getFontData()[0]; 242 fontData = Font.cocoa_new(display, nsFont).getFontData()[0];
243 } 243 }
244 } 244 }
245 panel.setDelegate(null); 245 panel.setDelegate(null);
246 delegate_.release(); 246 delegate_.release();
247 OS.DeleteGlobalRef(jniRef);
248 if (create) font.dispose(); 247 if (create) font.dispose();
249 return fontData; 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;
250 } 280 }
251 281 }
252 /** 282
253 * Sets a FontData object describing the font to be 283 /**
254 * selected by default in the dialog, or null to let 284 * Sets the RGB describing the color to be selected by default
255 * the platform choose one. 285 * in the dialog, or null to let the platform choose one.
256 * 286 *
257 * @param fontData the FontData to use initially, or null 287 * @param rgb the RGB value to use initially, or null to let
258 * @deprecated use #setFontList (FontData []) 288 * the platform select a default when open() is called
259 */ 289 *
260 public void setFontData (FontData fontData) { 290 * @see PaletteData#getRGBs
261 this.fontData = fontData; 291 *
262 } 292 * @since 2.1
263 293 */
264 /** 294 public void setRGB (RGB rgb) {
265 * Sets the set of FontData objects describing the font to 295 this.rgb = rgb;
266 * be selected by default in the dialog, or null to let 296 }
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;
281 }
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 }
298 297
299 void windowWillClose(objc.id id, objc.SEL sel, objc.id sender) { 298 void windowWillClose(objc.id id, objc.SEL sel, objc.id sender) {
300 NSApplication.sharedApplication().stop(null); 299 NSApplication.sharedApplication().stop(null);
301 } 300 }
302 301
303 } 302 }