Source code: org/eclipse/swt/widgets/FontDialog.java
1 /*******************************************************************************
2 * Copyright (c) 2000, 2004 IBM Corporation and others.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Common Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/cpl-v10.html
7 *
8 * Contributors:
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
11 package org.eclipse.swt.widgets;
12
13
14 import org.eclipse.swt.*;
15 import org.eclipse.swt.internal.*;
16 import org.eclipse.swt.internal.gtk.*;
17 import org.eclipse.swt.graphics.*;
18
19 /**
20 * Instances of this class allow the user to select a font
21 * from all available fonts in the system.
22 * <dl>
23 * <dt><b>Styles:</b></dt>
24 * <dd>(none)</dd>
25 * <dt><b>Events:</b></dt>
26 * <dd>(none)</dd>
27 * </dl>
28 * <p>
29 * IMPORTANT: This class is intended to be subclassed <em>only</em>
30 * within the SWT implementation.
31 * </p>
32 */
33 public class FontDialog extends Dialog {
34 FontData fontData;
35 RGB rgb;
36 /**
37 * Constructs a new instance of this class given only its parent.
38 *
39 * @param parent a shell which will be the parent of the new instance
40 *
41 * @exception IllegalArgumentException <ul>
42 * <li>ERROR_NULL_ARGUMENT - if the parent is null</li>
43 * </ul>
44 * @exception SWTException <ul>
45 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the parent</li>
46 * <li>ERROR_INVALID_SUBCLASS - if this class is not an allowed subclass</li>
47 * </ul>
48 */
49 public FontDialog (Shell parent) {
50 this (parent, SWT.PRIMARY_MODAL);
51 }
52 /**
53 * Constructs a new instance of this class given its parent
54 * and a style value describing its behavior and appearance.
55 * <p>
56 * The style value is either one of the style constants defined in
57 * class <code>SWT</code> which is applicable to instances of this
58 * class, or must be built by <em>bitwise OR</em>'ing together
59 * (that is, using the <code>int</code> "|" operator) two or more
60 * of those <code>SWT</code> style constants. The class description
61 * lists the style constants that are applicable to the class.
62 * Style bits are also inherited from superclasses.
63 * </p>
64 *
65 * @param parent a shell which will be the parent of the new instance
66 * @param style the style of dialog to construct
67 *
68 * @exception IllegalArgumentException <ul>
69 * <li>ERROR_NULL_ARGUMENT - if the parent is null</li>
70 * </ul>
71 * @exception SWTException <ul>
72 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the parent</li>
73 * <li>ERROR_INVALID_SUBCLASS - if this class is not an allowed subclass</li>
74 * </ul>
75 */
76 public FontDialog (Shell parent, int style) {
77 super (parent, style);
78 checkSubclass ();
79 }
80
81 /**
82 * Returns a FontData object describing the font that was
83 * selected in the dialog, or null if none is available.
84 *
85 * @return the FontData for the selected font, or null
86 * @deprecated use #getFontList ()
87 */
88 public FontData getFontData () {
89 return fontData;
90 }
91
92 /**
93 * Returns a FontData set describing the font that was
94 * selected in the dialog, or null if none is available.
95 *
96 * @return the FontData for the selected font, or null
97 * @since 2.1.1
98 */
99 public FontData [] getFontList () {
100 if (fontData == null) return null;
101 FontData [] result = new FontData [1];
102 result [0] = fontData;
103 return result;
104 }
105
106 /**
107 * Returns the currently selected color in the receiver.
108 *
109 * @return the RGB value for the selected color, may be null
110 *
111 * @see PaletteData#getRGBs
112 *
113 * @since 2.1
114 */
115 public RGB getRGB () {
116 return rgb;
117 }
118
119 /**
120 * Makes the dialog visible and brings it to the front
121 * of the display.
122 *
123 * @return a FontData object describing the font that was selected,
124 * or null if the dialog was cancelled or an error occurred
125 *
126 * @exception SWTException <ul>
127 * <li>ERROR_WIDGET_DISPOSED - if the dialog has been disposed</li>
128 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the dialog</li>
129 * </ul>
130 */
131 public FontData open () {
132 long /*int*/ handle;
133 byte [] titleBytes;
134 titleBytes = Converter.wcsToMbcs (null, title, true);
135 handle = OS.gtk_font_selection_dialog_new (titleBytes);
136 if (parent!=null) {
137 OS.gtk_window_set_transient_for(handle, parent.topHandle());
138 }
139 if (fontData != null) {
140 Display display = parent != null ? parent.display : Display.getCurrent ();
141 Font font = new Font (display, fontData);
142 long /*int*/ fontName = OS.pango_font_description_to_string (font.handle);
143 int length = OS.strlen (fontName);
144 byte [] buffer = new byte [length + 1];
145 OS.memmove (buffer, fontName, length);
146 font.dispose();
147 OS.g_free (fontName);
148 OS.gtk_font_selection_dialog_set_font_name (handle, buffer);
149 }
150 int response = OS.gtk_dialog_run(handle);
151 boolean success = response == OS.GTK_RESPONSE_OK;
152 if (success) {
153 long /*int*/ fontName = OS.gtk_font_selection_dialog_get_font_name (handle);
154 int length = OS.strlen (fontName);
155 byte [] buffer = new byte [length + 1];
156 OS.memmove (buffer, fontName, length);
157 long /*int*/ fontDesc = OS.pango_font_description_from_string (buffer);
158 Display display = parent != null ? parent.display : Display.getCurrent ();
159 Font font = Font.gtk_new (display, fontDesc);
160 fontData = font.getFontData () [0];
161 OS.pango_font_description_free (fontDesc);
162 }
163 OS.gtk_widget_destroy(handle);
164 if (!success) return null;
165 return fontData;
166 }
167 /**
168 * Sets a FontData object describing the font to be
169 * selected by default in the dialog, or null to let
170 * the platform choose one.
171 *
172 * @param fontData the FontData to use initially, or null
173 * @deprecated use #setFontList (FontData [])
174 */
175 public void setFontData (FontData fontData) {
176 this.fontData = fontData;
177 }
178
179 /**
180 * Sets a set of FontData objects describing the font to
181 * be selected by default in the dialog, or null to let
182 * the platform choose one.
183 *
184 * @param fontData the set of FontData objects to use initially, or null
185 * @since 2.1.1
186 */
187 public void setFontList (FontData [] fontData) {
188 if (fontData != null && fontData.length > 0) {
189 this.fontData = fontData [0];
190 } else {
191 this.fontData = null;
192 }
193 }
194 /**
195 * Sets the receiver's selected color to be the argument.
196 *
197 * @param rgb the new RGB value for the selected color, may be
198 * null to let the platform to select a default when
199 * open() is called
200 *
201 * @see PaletteData#getRGBs
202 *
203 * @since 2.1
204 */
205 public void setRGB (RGB rgb) {
206 this.rgb = rgb;
207 }
208 }