1 /*******************************************************************************
2 * Copyright (c) 2000, 2014 IBM Corporation and others.
4 * This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License 2.0
6 * which accompanies this distribution, and is available at
7 * https://www.eclipse.org/legal/epl-2.0/
9 * SPDX-License-Identifier: EPL-2.0
12 * IBM Corporation - initial API and implementation
13 *******************************************************************************/
14 package org.eclipse.swt.widgets;
17 import org.eclipse.swt.*;
18 import org.eclipse.swt.graphics.*;
19 import org.eclipse.swt.internal.win32.*;
22 * Instances of this class allow the user to select a font
23 * from all available fonts in the system.
25 * <dt><b>Styles:</b></dt>
27 * <dt><b>Events:</b></dt>
31 * IMPORTANT: This class is <em>not</em> intended to be subclassed.
34 * @see <a href="http://www.eclipse.org/swt/examples.php">SWT Example: ControlExample, Dialog tab</a>
35 * @see <a href="http://www.eclipse.org/swt/">Sample code and further information</a>
36 * @noextend This class is not intended to be subclassed by clients.
38 public class FontDialog extends Dialog {
41 boolean effectsVisible = true;
44 * Constructs a new instance of this class given only its parent.
46 * @param parent a shell which will be the parent of the new instance
48 * @exception IllegalArgumentException <ul>
49 * <li>ERROR_NULL_ARGUMENT - if the parent is null</li>
51 * @exception SWTException <ul>
52 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the parent</li>
53 * <li>ERROR_INVALID_SUBCLASS - if this class is not an allowed subclass</li>
56 public FontDialog (Shell parent) {
57 this (parent, SWT.APPLICATION_MODAL);
61 * Constructs a new instance of this class given its parent
62 * and a style value describing its behavior and appearance.
64 * The style value is either one of the style constants defined in
65 * class <code>SWT</code> which is applicable to instances of this
66 * class, or must be built by <em>bitwise OR</em>'ing together
67 * (that is, using the <code>int</code> "|" operator) two or more
68 * of those <code>SWT</code> style constants. The class description
69 * lists the style constants that are applicable to the class.
70 * Style bits are also inherited from superclasses.
73 * @param parent a shell which will be the parent of the new instance
74 * @param style the style of dialog to construct
76 * @exception IllegalArgumentException <ul>
77 * <li>ERROR_NULL_ARGUMENT - if the parent is null</li>
79 * @exception SWTException <ul>
80 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the parent</li>
81 * <li>ERROR_INVALID_SUBCLASS - if this class is not an allowed subclass</li>
84 public FontDialog (Shell parent, int style) {
85 super (parent, checkStyle (parent, style));
90 * Returns <code>true</code> if the dialog's effects selection controls
91 * are visible, and <code>false</code> otherwise.
93 * If the platform's font dialog does not have any effects selection controls,
94 * then this method always returns false.
97 * @return <code>true</code> if the dialog's effects selection controls
98 * are visible and <code>false</code> otherwise
102 public boolean getEffectsVisible () {
103 return effectsVisible;
107 * Returns a FontData object describing the font that was
108 * selected in the dialog, or null if none is available.
110 * @return the FontData for the selected font, or null
111 * @deprecated use #getFontList ()
114 public FontData getFontData () {
119 * Returns a FontData set describing the font that was
120 * selected in the dialog, or null if none is available.
122 * @return the FontData for the selected font, or null
125 public FontData [] getFontList () {
126 if (fontData == null) return null;
127 FontData [] result = new FontData [1];
128 result [0] = fontData;
133 * Returns an RGB describing the color that was selected
134 * in the dialog, or null if none is available.
136 * @return the RGB value for the selected color, or null
138 * @see PaletteData#getRGBs
142 public RGB getRGB () {
147 * Makes the dialog visible and brings it to the front
150 * @return a FontData object describing the font that was selected,
151 * or null if the dialog was cancelled or an error occurred
153 * @exception SWTException <ul>
154 * <li>ERROR_WIDGET_DISPOSED - if the dialog has been disposed</li>
155 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the dialog</li>
158 public FontData open () {
159 /* Get the owner HWND for the dialog */
160 long hwndOwner = parent.handle;
161 long hwndParent = parent.handle;
164 * Feature in Windows. There is no API to set the orientation of a
165 * font dialog. It is always inherited from the parent. The fix is
166 * to create a hidden parent and set the orientation in the hidden
167 * parent for the dialog to inherit.
169 boolean enabled = false;
170 int dialogOrientation = style & (SWT.LEFT_TO_RIGHT | SWT.RIGHT_TO_LEFT);
171 int parentOrientation = parent.style & (SWT.LEFT_TO_RIGHT | SWT.RIGHT_TO_LEFT);
172 if (dialogOrientation != parentOrientation) {
173 int exStyle = OS.WS_EX_NOINHERITLAYOUT;
174 if (dialogOrientation == SWT.RIGHT_TO_LEFT) exStyle |= OS.WS_EX_LAYOUTRTL;
175 hwndOwner = OS.CreateWindowEx (
180 OS.CW_USEDEFAULT, 0, OS.CW_USEDEFAULT, 0,
183 OS.GetModuleHandle (null),
185 enabled = OS.IsWindowEnabled (hwndParent);
186 if (enabled) OS.EnableWindow (hwndParent, false);
189 /* Open the dialog */
190 long hHeap = OS.GetProcessHeap ();
191 CHOOSEFONT lpcf = new CHOOSEFONT ();
192 lpcf.lStructSize = CHOOSEFONT.sizeof;
193 lpcf.hwndOwner = hwndOwner;
194 lpcf.Flags = OS.CF_SCREENFONTS;
195 if (effectsVisible) {
196 lpcf.Flags |= OS.CF_EFFECTS;
199 long lpLogFont = OS.HeapAlloc (hHeap, OS.HEAP_ZERO_MEMORY, LOGFONT.sizeof);
200 if (fontData != null && fontData.data != null) {
201 LOGFONT logFont = fontData.data;
202 int lfHeight = logFont.lfHeight;
203 long hDC = OS.GetDC (0);
204 int pixels = -(int)(0.5f + (fontData.height * OS.GetDeviceCaps(hDC, OS.LOGPIXELSY) / 72));
205 OS.ReleaseDC (0, hDC);
206 logFont.lfHeight = pixels;
207 lpcf.Flags |= OS.CF_INITTOLOGFONTSTRUCT;
208 OS.MoveMemory (lpLogFont, logFont, LOGFONT.sizeof);
209 logFont.lfHeight = lfHeight;
211 lpcf.lpLogFont = lpLogFont;
213 int red = rgb.red & 0xFF;
214 int green = (rgb.green << 8) & 0xFF00;
215 int blue = (rgb.blue << 16) & 0xFF0000;
216 lpcf.rgbColors = red | green | blue;
219 /* Make the parent shell be temporary modal */
220 Dialog oldModal = null;
221 Display display = parent.getDisplay ();
222 if ((style & (SWT.APPLICATION_MODAL | SWT.SYSTEM_MODAL)) != 0) {
223 oldModal = display.getModalDialog ();
224 display.setModalDialog (this);
227 display.externalEventLoop = true;
228 display.sendPreExternalEventDispatchEvent ();
229 /* Open the dialog */
230 boolean success = OS.ChooseFont (lpcf);
231 display.externalEventLoop = false;
232 display.sendPostExternalEventDispatchEvent ();
234 /* Clear the temporary dialog modal parent */
235 if ((style & (SWT.APPLICATION_MODAL | SWT.SYSTEM_MODAL)) != 0) {
236 display.setModalDialog (oldModal);
239 /* Compute the result */
241 LOGFONT logFont = new LOGFONT ();
242 OS.MoveMemory (logFont, lpLogFont, LOGFONT.sizeof);
245 * This will not work on multiple screens or
246 * for printing. Should use DC for the proper device.
248 long hDC = OS.GetDC(0);
249 int logPixelsY = OS.GetDeviceCaps(hDC, OS.LOGPIXELSY);
251 if (logFont.lfHeight > 0) {
253 * Feature in Windows. If the lfHeight of the LOGFONT structure
254 * is positive, the lfHeight measures the height of the entire
255 * cell, including internal leading, in logical units. Since the
256 * height of a font in points does not include the internal leading,
257 * we must subtract the internal leading, which requires a TEXTMETRIC,
258 * which in turn requires font creation.
260 long hFont = OS.CreateFontIndirect(logFont);
261 long oldFont = OS.SelectObject(hDC, hFont);
262 TEXTMETRIC lptm = new TEXTMETRIC ();
263 OS.GetTextMetrics(hDC, lptm);
264 OS.SelectObject(hDC, oldFont);
265 OS.DeleteObject(hFont);
266 pixels = logFont.lfHeight - lptm.tmInternalLeading;
268 pixels = -logFont.lfHeight;
270 OS.ReleaseDC(0, hDC);
272 float points = pixels * 72f /logPixelsY;
273 fontData = FontData.win32_new (logFont, points);
274 if (effectsVisible) {
275 int red = lpcf.rgbColors & 0xFF;
276 int green = (lpcf.rgbColors >> 8) & 0xFF;
277 int blue = (lpcf.rgbColors >> 16) & 0xFF;
278 rgb = new RGB (red, green, blue);
282 /* Free the OS memory */
283 if (lpLogFont != 0) OS.HeapFree (hHeap, 0, lpLogFont);
285 /* Destroy the BIDI orientation window */
286 if (hwndParent != hwndOwner) {
287 if (enabled) OS.EnableWindow (hwndParent, true);
288 OS.SetActiveWindow (hwndParent);
289 OS.DestroyWindow (hwndOwner);
293 * This code is intentionally commented. On some
294 * platforms, the owner window is repainted right
295 * away when a dialog window exits. This behavior
296 * is currently unspecified.
298 // if (hwndOwner != 0) OS.UpdateWindow (hwndOwner);
300 if (!success) return null;
305 * Sets the effects selection controls in the dialog visible if the
306 * argument is <code>true</code>, and invisible otherwise.
308 * By default the effects selection controls are displayed if the
309 * platform font dialog supports effects selection.
312 * @param visible whether or not the dialog will show the effects selection controls
316 public void setEffectsVisible(boolean visible) {
317 effectsVisible = visible;
321 * Sets a FontData object describing the font to be
322 * selected by default in the dialog, or null to let
323 * the platform choose one.
325 * @param fontData the FontData to use initially, or null
326 * @deprecated use #setFontList (FontData [])
329 public void setFontData (FontData fontData) {
330 this.fontData = fontData;
334 * Sets the set of FontData objects describing the font to
335 * be selected by default in the dialog, or null to let
336 * the platform choose one.
338 * @param fontData the set of FontData objects to use initially, or null
339 * to let the platform select a default when open() is called
341 * @see Font#getFontData
345 public void setFontList (FontData [] fontData) {
346 if (fontData != null && fontData.length > 0) {
347 this.fontData = fontData [0];
349 this.fontData = null;
354 * Sets the RGB describing the color to be selected by default
355 * in the dialog, or null to let the platform choose one.
357 * @param rgb the RGB value to use initially, or null to let
358 * the platform select a default when open() is called
360 * @see PaletteData#getRGBs
364 public void setRGB (RGB rgb) {