]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.eclipse.swt.win32.win32.x86_64/src/org/eclipse/swt/widgets/FontDialog.java
Remove invalid SHA-256-Digests
[simantics/platform.git] / bundles / org.eclipse.swt.win32.win32.x86_64 / src / org / eclipse / swt / widgets / FontDialog.java
1 /*******************************************************************************
2  * Copyright (c) 2000, 2014 IBM Corporation and others.
3  *
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/
8  *
9  * SPDX-License-Identifier: EPL-2.0
10  *
11  * Contributors:
12  *     IBM Corporation - initial API and implementation
13  *******************************************************************************/
14 package org.eclipse.swt.widgets;
15
16
17 import org.eclipse.swt.*;
18 import org.eclipse.swt.graphics.*;
19 import org.eclipse.swt.internal.win32.*;
20
21 /**
22  * Instances of this class allow the user to select a font
23  * from all available fonts in the system.
24  * <dl>
25  * <dt><b>Styles:</b></dt>
26  * <dd>(none)</dd>
27  * <dt><b>Events:</b></dt>
28  * <dd>(none)</dd>
29  * </dl>
30  * <p>
31  * IMPORTANT: This class is <em>not</em> intended to be subclassed.
32  * </p>
33  *
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.
37  */
38 public class FontDialog extends Dialog {
39         FontData fontData;
40         RGB rgb;
41         boolean effectsVisible = true;
42
43 /**
44  * Constructs a new instance of this class given only its parent.
45  *
46  * @param parent a shell which will be the parent of the new instance
47  *
48  * @exception IllegalArgumentException <ul>
49  *    <li>ERROR_NULL_ARGUMENT - if the parent is null</li>
50  * </ul>
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>
54  * </ul>
55  */
56 public FontDialog (Shell parent) {
57         this (parent, SWT.APPLICATION_MODAL);
58 }
59
60 /**
61  * Constructs a new instance of this class given its parent
62  * and a style value describing its behavior and appearance.
63  * <p>
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.
71  * </p>
72  *
73  * @param parent a shell which will be the parent of the new instance
74  * @param style the style of dialog to construct
75  *
76  * @exception IllegalArgumentException <ul>
77  *    <li>ERROR_NULL_ARGUMENT - if the parent is null</li>
78  * </ul>
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>
82  * </ul>
83  */
84 public FontDialog (Shell parent, int style) {
85         super (parent, checkStyle (parent, style));
86         checkSubclass ();
87 }
88
89 /**
90  * Returns <code>true</code> if the dialog's effects selection controls
91  * are visible, and <code>false</code> otherwise.
92  * <p>
93  * If the platform's font dialog does not have any effects selection controls,
94  * then this method always returns false.
95  * </p>
96  *
97  * @return <code>true</code> if the dialog's effects selection controls
98  * are visible and <code>false</code> otherwise
99  *
100  * @since 3.8
101  */
102 public boolean getEffectsVisible () {
103         return effectsVisible;
104 }
105
106 /**
107  * Returns a FontData object describing the font that was
108  * selected in the dialog, or null if none is available.
109  *
110  * @return the FontData for the selected font, or null
111  * @deprecated use #getFontList ()
112  */
113 @Deprecated
114 public FontData getFontData () {
115         return fontData;
116 }
117
118 /**
119  * Returns a FontData set describing the font that was
120  * selected in the dialog, or null if none is available.
121  *
122  * @return the FontData for the selected font, or null
123  * @since 2.1.1
124  */
125 public FontData [] getFontList () {
126         if (fontData == null) return null;
127         FontData [] result = new FontData [1];
128         result [0] = fontData;
129         return result;
130 }
131
132 /**
133  * Returns an RGB describing the color that was selected
134  * in the dialog, or null if none is available.
135  *
136  * @return the RGB value for the selected color, or null
137  *
138  * @see PaletteData#getRGBs
139  *
140  * @since 2.1
141  */
142 public RGB getRGB () {
143         return rgb;
144 }
145
146 /**
147  * Makes the dialog visible and brings it to the front
148  * of the display.
149  *
150  * @return a FontData object describing the font that was selected,
151  *         or null if the dialog was cancelled or an error occurred
152  *
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>
156  * </ul>
157  */
158 public FontData open () {
159         /* Get the owner HWND for the dialog */
160         long hwndOwner = parent.handle;
161         long hwndParent = parent.handle;
162
163         /*
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.
168         */
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 (
176                         exStyle,
177                         Shell.DialogClass,
178                         null,
179                         0,
180                         OS.CW_USEDEFAULT, 0, OS.CW_USEDEFAULT, 0,
181                         hwndParent,
182                         0,
183                         OS.GetModuleHandle (null),
184                         null);
185                 enabled = OS.IsWindowEnabled (hwndParent);
186                 if (enabled) OS.EnableWindow (hwndParent, false);
187         }
188
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;
197         }
198
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;
210         }
211         lpcf.lpLogFont = lpLogFont;
212         if (rgb != null) {
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;
217         }
218
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);
225         }
226
227         display.externalEventLoop = true;
228         display.sendPreExternalEventDispatchEvent ();
229         /* Open the dialog */
230         boolean success = OS.ChooseFont (lpcf);
231         display.externalEventLoop = false;
232         display.sendPostExternalEventDispatchEvent ();
233
234         /* Clear the temporary dialog modal parent */
235         if ((style & (SWT.APPLICATION_MODAL | SWT.SYSTEM_MODAL)) != 0) {
236                 display.setModalDialog (oldModal);
237         }
238
239         /* Compute the result */
240         if (success) {
241                 LOGFONT logFont = new LOGFONT ();
242                 OS.MoveMemory (logFont, lpLogFont, LOGFONT.sizeof);
243
244                 /*
245                  * This will not work on multiple screens or
246                  * for printing. Should use DC for the proper device.
247                  */
248                 long hDC = OS.GetDC(0);
249                 int logPixelsY = OS.GetDeviceCaps(hDC, OS.LOGPIXELSY);
250                 int pixels = 0;
251                 if (logFont.lfHeight > 0) {
252                         /*
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.
259                          */
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;
267                 } else {
268                         pixels = -logFont.lfHeight;
269                 }
270                 OS.ReleaseDC(0, hDC);
271
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);
279                 }
280         }
281
282         /* Free the OS memory */
283         if (lpLogFont != 0) OS.HeapFree (hHeap, 0, lpLogFont);
284
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);
290         }
291
292         /*
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.
297         */
298 //      if (hwndOwner != 0) OS.UpdateWindow (hwndOwner);
299
300         if (!success) return null;
301         return fontData;
302 }
303
304 /**
305  * Sets the effects selection controls in the dialog visible if the
306  * argument is <code>true</code>, and invisible otherwise.
307  * <p>
308  * By default the effects selection controls are displayed if the
309  * platform font dialog supports effects selection.
310  * </p>
311  *
312  * @param visible whether or not the dialog will show the effects selection controls
313  *
314  * @since 3.8
315  */
316 public void setEffectsVisible(boolean visible) {
317         effectsVisible = visible;
318 }
319
320 /**
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.
324  *
325  * @param fontData the FontData to use initially, or null
326  * @deprecated use #setFontList (FontData [])
327  */
328 @Deprecated
329 public void setFontData (FontData fontData) {
330         this.fontData = fontData;
331 }
332
333 /**
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.
337  *
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
340  *
341  * @see Font#getFontData
342  *
343  * @since 2.1.1
344  */
345 public void setFontList (FontData [] fontData) {
346         if (fontData != null && fontData.length > 0) {
347                 this.fontData = fontData [0];
348         } else {
349                 this.fontData = null;
350         }
351 }
352
353 /**
354  * Sets the RGB describing the color to be selected by default
355  * in the dialog, or null to let the platform choose one.
356  *
357  * @param rgb the RGB value to use initially, or null to let
358  *        the platform select a default when open() is called
359  *
360  * @see PaletteData#getRGBs
361  *
362  * @since 2.1
363  */
364 public void setRGB (RGB rgb) {
365         this.rgb = rgb;
366 }
367
368 }