1 /******************************************************************************* 2 * Copyright (c) 2000, 2008 IBM Corporation and others. 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 10 * Port to the D programming language: 11 * Frank Benoit <benoit@tionex.de> 12 *******************************************************************************/ 13 module org.eclipse.swt.widgets.FontDialog; 14 15 import org.eclipse.swt.SWT; 16 import org.eclipse.swt.SWTException; 17 import org.eclipse.swt.graphics.Font; 18 import org.eclipse.swt.graphics.FontData; 19 import org.eclipse.swt.graphics.PaletteData; 20 import org.eclipse.swt.graphics.RGB; 21 import org.eclipse.swt.internal.gtk.OS; 22 import org.eclipse.swt.widgets.Dialog; 23 import org.eclipse.swt.widgets.Shell; 24 import org.eclipse.swt.widgets.Display; 25 26 import java.lang.all; 27 28 /** 29 * Instances of this class allow the user to select a font 30 * from all available fonts in the system. 31 * <dl> 32 * <dt><b>Styles:</b></dt> 33 * <dd>(none)</dd> 34 * <dt><b>Events:</b></dt> 35 * <dd>(none)</dd> 36 * </dl> 37 * <p> 38 * IMPORTANT: This class is intended to be subclassed <em>only</em> 39 * within the SWT implementation. 40 * </p> 41 * 42 * @see <a href="http://www.eclipse.org/swt/examples.php">SWT Example: ControlExample, Dialog tab</a> 43 * @see <a href="http://www.eclipse.org/swt/">Sample code and further information</a> 44 */ 45 public class FontDialog : Dialog { 46 FontData fontData; 47 RGB rgb; 48 /** 49 * Constructs a new instance of this class given only its parent. 50 * 51 * @param parent a shell which will be the parent of the new instance 52 * 53 * @exception IllegalArgumentException <ul> 54 * <li>ERROR_NULL_ARGUMENT - if the parent is null</li> 55 * </ul> 56 * @exception SWTException <ul> 57 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the parent</li> 58 * <li>ERROR_INVALID_SUBCLASS - if this class is not an allowed subclass</li> 59 * </ul> 60 */ 61 public this (Shell parent) { 62 this (parent, SWT.APPLICATION_MODAL); 63 } 64 /** 65 * Constructs a new instance of this class given its parent 66 * and a style value describing its behavior and appearance. 67 * <p> 68 * The style value is either one of the style constants defined in 69 * class <code>SWT</code> which is applicable to instances of this 70 * class, or must be built by <em>bitwise OR</em>'ing together 71 * (that is, using the <code>int</code> "|" operator) two or more 72 * of those <code>SWT</code> style constants. The class description 73 * lists the style constants that are applicable to the class. 74 * Style bits are also inherited from superclasses. 75 * </p> 76 * 77 * @param parent a shell which will be the parent of the new instance 78 * @param style the style of dialog to construct 79 * 80 * @exception IllegalArgumentException <ul> 81 * <li>ERROR_NULL_ARGUMENT - if the parent is null</li> 82 * </ul> 83 * @exception SWTException <ul> 84 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the parent</li> 85 * <li>ERROR_INVALID_SUBCLASS - if this class is not an allowed subclass</li> 86 * </ul> 87 */ 88 public this (Shell parent, int style) { 89 super (parent, checkStyle (parent, style)); 90 checkSubclass (); 91 } 92 93 /** 94 * Returns a FontData object describing the font that was 95 * selected in the dialog, or null if none is available. 96 * 97 * @return the FontData for the selected font, or null 98 * @deprecated use #getFontList () 99 */ 100 public FontData getFontData () { 101 return fontData; 102 } 103 104 /** 105 * Returns a FontData set describing the font that was 106 * selected in the dialog, or null if none is available. 107 * 108 * @return the FontData for the selected font, or null 109 * @since 2.1.1 110 */ 111 public FontData [] getFontList () { 112 if (fontData is null) return null; 113 FontData [] result = new FontData [1]; 114 result [0] = fontData; 115 return result; 116 } 117 118 /** 119 * Returns an RGB describing the color that was selected 120 * in the dialog, or null if none is available. 121 * 122 * @return the RGB value for the selected color, or null 123 * 124 * @see PaletteData#getRGBs 125 * 126 * @since 2.1 127 */ 128 public RGB getRGB () { 129 return rgb; 130 } 131 132 /** 133 * Makes the dialog visible and brings it to the front 134 * of the display. 135 * 136 * @return a FontData object describing the font that was selected, 137 * or null if the dialog was cancelled or an error occurred 138 * 139 * @exception SWTException <ul> 140 * <li>ERROR_WIDGET_DISPOSED - if the dialog has been disposed</li> 141 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the dialog</li> 142 * </ul> 143 */ 144 public FontData open () { 145 GtkWidget* handle; 146 char* titleBytes; 147 titleBytes = toStringz(title); 148 Display display = parent !is null ? parent.getDisplay (): Display.getCurrent (); 149 handle = OS.gtk_font_selection_dialog_new (titleBytes); 150 if (parent !is null) { 151 auto shellHandle = parent.topHandle (); 152 OS.gtk_window_set_transient_for(handle, shellHandle); 153 auto pixbufs = OS.gtk_window_get_icon_list (shellHandle); 154 if (pixbufs !is null) { 155 OS.gtk_window_set_icon_list (handle, pixbufs); 156 OS.g_list_free (pixbufs); 157 } 158 } 159 if (fontData !is null) { 160 Font font = new Font (display, fontData); 161 auto fontName = OS.pango_font_description_to_string (font.handle); 162 font.dispose(); 163 OS.gtk_font_selection_dialog_set_font_name (handle, fontName); 164 OS.g_free (fontName); 165 } 166 display.addIdleProc (); 167 Dialog oldModal = null; 168 if (OS.gtk_window_get_modal (handle)) { 169 oldModal = display.getModalDialog (); 170 display.setModalDialog (this); 171 } 172 int signalId = 0; 173 ptrdiff_t hookId = 0; 174 CallbackData emissionData; 175 emissionData.display = display; 176 emissionData.data = handle; 177 if ((style & SWT.RIGHT_TO_LEFT) !is 0) { 178 signalId = OS.g_signal_lookup (OS.map.ptr, OS.GTK_TYPE_WIDGET()); 179 hookId = OS.g_signal_add_emission_hook (signalId, 0, &Display.emissionFunc, &emissionData, null); 180 } 181 int response = OS.gtk_dialog_run (handle); 182 if ((style & SWT.RIGHT_TO_LEFT) !is 0) { 183 OS.g_signal_remove_emission_hook (signalId, hookId); 184 } 185 if (OS.gtk_window_get_modal (handle)) { 186 display.setModalDialog (oldModal); 187 } 188 bool success = response is OS.GTK_RESPONSE_OK; 189 if (success) { 190 auto fontName = OS.gtk_font_selection_dialog_get_font_name (handle); 191 auto fontDesc = OS.pango_font_description_from_string (fontName); 192 OS.g_free (fontName); 193 Font font = Font.gtk_new (display, fontDesc); 194 fontData = font.getFontData () [0]; 195 OS.pango_font_description_free (fontDesc); 196 } 197 display.removeIdleProc (); 198 OS.gtk_widget_destroy(handle); 199 if (!success) return null; 200 return fontData; 201 } 202 /** 203 * Sets a FontData object describing the font to be 204 * selected by default in the dialog, or null to let 205 * the platform choose one. 206 * 207 * @param fontData the FontData to use initially, or null 208 * @deprecated use #setFontList (FontData []) 209 */ 210 public void setFontData (FontData fontData) { 211 this.fontData = fontData; 212 } 213 214 /** 215 * Sets the set of FontData objects describing the font to 216 * be selected by default in the dialog, or null to let 217 * the platform choose one. 218 * 219 * @param fontData the set of FontData objects to use initially, or null 220 * to let the platform select a default when open() is called 221 * 222 * @see Font#getFontData 223 * 224 * @since 2.1.1 225 */ 226 public void setFontList (FontData [] fontData) { 227 if (fontData !is null && fontData.length > 0) { 228 this.fontData = fontData [0]; 229 } else { 230 this.fontData = null; 231 } 232 } 233 /** 234 * Sets the RGB describing the color to be selected by default 235 * in the dialog, or null to let the platform choose one. 236 * 237 * @param rgb the RGB value to use initially, or null to let 238 * the platform select a default when open() is called 239 * 240 * @see PaletteData#getRGBs 241 * 242 * @since 2.1 243 */ 244 public void setRGB (RGB rgb) { 245 this.rgb = rgb; 246 } 247 }