root/dwt/widgets/FontDialog.d

Revision 246:fd9c62a2998e, 10.8 kB (checked in by Frank Benoit <benoit@tionex.de>, 5 months ago)

Updater SWT 3.4M7 to 3.4

Line 
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 dwt.widgets.FontDialog;
14
15 import dwt.DWT;
16 import dwt.DWTException;
17 import dwt.graphics.Font;
18 import dwt.graphics.FontData;
19 import dwt.graphics.PaletteData;
20 import dwt.graphics.RGB;
21 import dwt.internal.win32.OS;
22
23 import dwt.widgets.Dialog;
24 import dwt.widgets.Shell;
25 import dwt.widgets.Display;
26
27 import dwt.dwthelper.utils;
28
29 /**
30  * Instances of this class allow the user to select a font
31  * from all available fonts in the system.
32  * <dl>
33  * <dt><b>Styles:</b></dt>
34  * <dd>(none)</dd>
35  * <dt><b>Events:</b></dt>
36  * <dd>(none)</dd>
37  * </dl>
38  * <p>
39  * IMPORTANT: This class is intended to be subclassed <em>only</em>
40  * within the DWT implementation.
41  * </p>
42  *
43  * @see <a href="http://www.eclipse.org/swt/examples.php">DWT Example: ControlExample, Dialog tab</a>
44  * @see <a href="http://www.eclipse.org/swt/">Sample code and further information</a>
45  */
46 public class FontDialog : Dialog {
47     FontData fontData;
48     RGB rgb;
49
50 /**
51  * Constructs a new instance of this class given only its parent.
52  *
53  * @param parent a shell which will be the parent of the new instance
54  *
55  * @exception IllegalArgumentException <ul>
56  *    <li>ERROR_NULL_ARGUMENT - if the parent is null</li>
57  * </ul>
58  * @exception DWTException <ul>
59  *    <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the parent</li>
60  *    <li>ERROR_INVALID_SUBCLASS - if this class is not an allowed subclass</li>
61  * </ul>
62  */
63 public this (Shell parent) {
64     this (parent, DWT.APPLICATION_MODAL);
65 }
66
67 /**
68  * Constructs a new instance of this class given its parent
69  * and a style value describing its behavior and appearance.
70  * <p>
71  * The style value is either one of the style constants defined in
72  * class <code>DWT</code> which is applicable to instances of this
73  * class, or must be built by <em>bitwise OR</em>'ing together
74  * (that is, using the <code>int</code> "|" operator) two or more
75  * of those <code>DWT</code> style constants. The class description
76  * lists the style constants that are applicable to the class.
77  * Style bits are also inherited from superclasses.
78  * </p>
79  *
80  * @param parent a shell which will be the parent of the new instance
81  * @param style the style of dialog to construct
82  *
83  * @exception IllegalArgumentException <ul>
84  *    <li>ERROR_NULL_ARGUMENT - if the parent is null</li>
85  * </ul>
86  * @exception DWTException <ul>
87  *    <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the parent</li>
88  *    <li>ERROR_INVALID_SUBCLASS - if this class is not an allowed subclass</li>
89  * </ul>
90  */
91 public this (Shell parent, int style) {
92     super (parent, checkStyle (parent, style));
93     checkSubclass ();
94 }
95
96 /**
97  * Returns a FontData object describing the font that was
98  * selected in the dialog, or null if none is available.
99  *
100  * @return the FontData for the selected font, or null
101  * @deprecated use #getFontList ()
102  */
103 public FontData getFontData () {
104     return fontData;
105 }
106
107 /**
108  * Returns a FontData set describing the font that was
109  * selected in the dialog, or null if none is available.
110  *
111  * @return the FontData for the selected font, or null
112  * @since 2.1.1
113  */
114 public FontData [] getFontList () {
115     if (fontData is null) return null;
116     FontData [] result = new FontData [1];
117     result [0] = fontData;
118     return result;
119 }
120
121 /**
122  * Returns an RGB describing the color that was selected
123  * in the dialog, or null if none is available.
124  *
125  * @return the RGB value for the selected color, or null
126  *
127  * @see PaletteData#getRGBs
128  *
129  * @since 2.1
130  */
131 public RGB getRGB () {
132     return rgb;
133 }
134
135 /**
136  * Makes the dialog visible and brings it to the front
137  * of the display.
138  *
139  * @return a FontData object describing the font that was selected,
140  *         or null if the dialog was cancelled or an error occurred
141  *
142  * @exception DWTException <ul>
143  *    <li>ERROR_WIDGET_DISPOSED - if the dialog has been disposed</li>
144  *    <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the dialog</li>
145  * </ul>
146  */
147 public FontData open () {
148     static if (OS.IsWinCE) DWT.error (DWT.ERROR_NOT_IMPLEMENTED);
149
150     /* Get the owner HWND for the dialog */
151     HWND hwndOwner = parent.handle;
152     auto hwndParent = parent.handle;
153
154     /*
155     * Feature in Windows.  There is no API to set the orientation of a
156     * font dialog.  It is always inherited from the parent.  The fix is
157     * to create a hidden parent and set the orientation in the hidden
158     * parent for the dialog to inherit.
159     */
160     bool enabled = false;
161     if (!OS.IsWinCE && OS.WIN32_VERSION >= OS.VERSION(4, 10)) {
162         int dialogOrientation = style & (DWT.LEFT_TO_RIGHT | DWT.RIGHT_TO_LEFT);
163         int parentOrientation = parent.style & (DWT.LEFT_TO_RIGHT | DWT.RIGHT_TO_LEFT);
164         if (dialogOrientation !is parentOrientation) {
165             int exStyle = OS.WS_EX_NOINHERITLAYOUT;
166             if (dialogOrientation is DWT.RIGHT_TO_LEFT) exStyle |= OS.WS_EX_LAYOUTRTL;
167             hwndOwner = OS.CreateWindowEx (
168                 exStyle,
169                 Shell.DialogClass.ptr,
170                 null,
171                 0,
172                 OS.CW_USEDEFAULT, 0, OS.CW_USEDEFAULT, 0,
173                 hwndParent,
174                 null,
175                 OS.GetModuleHandle (null),
176                 null);
177             enabled = OS.IsWindowEnabled (hwndParent) !is 0;
178             if (enabled) OS.EnableWindow (hwndParent, false);
179         }
180     }
181
182     /* Open the dialog */
183     auto hHeap = OS.GetProcessHeap ();
184     CHOOSEFONT lpcf;
185     lpcf.lStructSize = CHOOSEFONT.sizeof;
186     lpcf.hwndOwner = hwndOwner;
187     lpcf.Flags = OS.CF_SCREENFONTS | OS.CF_EFFECTS;
188     auto lpLogFont = cast(LOGFONT*) OS.HeapAlloc (hHeap, OS.HEAP_ZERO_MEMORY, LOGFONT.sizeof);
189     if (fontData !is null /+&& fontData.data !is null+/) {
190         LOGFONT logFont = fontData.data;
191         int lfHeight = logFont.lfHeight;
192         auto hDC = OS.GetDC (null);
193         int pixels = -cast(int)(0.5f + (fontData.height * OS.GetDeviceCaps(hDC, OS.LOGPIXELSY) / 72));
194         OS.ReleaseDC (null, hDC);
195         logFont.lfHeight = pixels;
196         lpcf.Flags |= OS.CF_INITTOLOGFONTSTRUCT;
197         OS.MoveMemory (lpLogFont, &logFont, LOGFONT.sizeof);
198         logFont.lfHeight = lfHeight;
199     }
200     lpcf.lpLogFont = lpLogFont;
201     if (rgb !is null) {
202         int red = rgb.red & 0xFF;
203         int green = (rgb.green << 8) & 0xFF00;
204         int blue = (rgb.blue << 16) & 0xFF0000;
205         lpcf.rgbColors = red | green | blue;
206     }
207
208     /* Make the parent shell be temporary modal */
209     Dialog oldModal = null;
210     Display display = null;
211     if ((style & (DWT.APPLICATION_MODAL | DWT.SYSTEM_MODAL)) !is 0) {
212         display = parent.getDisplay ();
213         oldModal = display.getModalDialog ();
214         display.setModalDialog (this);
215     }
216
217     /* Open the dialog */
218     bool success = cast(bool) OS.ChooseFont (&lpcf);
219
220     /* Clear the temporary dialog modal parent */
221     if ((style & (DWT.APPLICATION_MODAL | DWT.SYSTEM_MODAL)) !is 0) {
222         display.setModalDialog (oldModal);
223     }
224
225     /* Compute the result */
226     if (success) {
227         LOGFONT* logFont = lpLogFont;
228         //OS.MoveMemory (logFont, lpLogFont, LOGFONT.sizeof);
229
230         /*
231          * This will not work on multiple screens or
232          * for printing. Should use DC for the proper device.
233          */
234         auto hDC = OS.GetDC(null);
235         int logPixelsY = OS.GetDeviceCaps(hDC, OS.LOGPIXELSY);
236         int pixels = 0;
237         if (logFont.lfHeight > 0) {
238             /*
239              * Feature in Windows. If the lfHeight of the LOGFONT structure
240              * is positive, the lfHeight measures the height of the entire
241              * cell, including internal leading, in logical units. Since the
242              * height of a font in points does not include the internal leading,
243              * we must subtract the internal leading, which requires a TEXTMETRIC,
244              * which in turn requires font creation.
245              */
246             auto hFont = OS.CreateFontIndirect(logFont);
247             auto oldFont = OS.SelectObject(hDC, hFont);
248             TEXTMETRIC lptm ;
249             OS.GetTextMetrics(hDC, &lptm);
250             OS.SelectObject(hDC, oldFont);
251             OS.DeleteObject(hFont);
252             pixels = logFont.lfHeight - lptm.tmInternalLeading;
253         } else {
254             pixels = -logFont.lfHeight;
255         }
256         OS.ReleaseDC(null, hDC);
257
258         float points = pixels * 72f /logPixelsY;
259         fontData = FontData.win32_new (logFont, points);
260         int red = lpcf.rgbColors & 0xFF;
261         int green = (lpcf.rgbColors >> 8) & 0xFF;
262         int blue = (lpcf.rgbColors >> 16) & 0xFF;
263         rgb = new RGB (red, green, blue);
264     }
265
266     /* Free the OS memory */
267     if (lpLogFont !is null) OS.HeapFree (hHeap, 0, lpLogFont);
268
269     /* Destroy the BIDI orientation window */
270     if (hwndParent !is hwndOwner) {
271         if (enabled) OS.EnableWindow (hwndParent, true);
272         OS.SetActiveWindow (hwndParent);
273         OS.DestroyWindow (hwndOwner);
274     }
275
276     /*
277     * This code is intentionally commented.  On some
278     * platforms, the owner window is repainted right
279     * away when a dialog window exits.  This behavior
280     * is currently unspecified.
281     */
282 //  if (hwndOwner !is 0) OS.UpdateWindow (hwndOwner);
283
284     if (!success) return null;
285     return fontData;
286 }
287
288 /**
289  * Sets a FontData object describing the font to be
290  * selected by default in the dialog, or null to let
291  * the platform choose one.
292  *
293  * @param fontData the FontData to use initially, or null
294  * @deprecated use #setFontList (FontData [])
295  */
296 public void setFontData (FontData fontData) {
297     this.fontData = fontData;
298 }
299
300 /**
301  * Sets the set of FontData objects describing the font to
302  * be selected by default in the dialog, or null to let
303  * the platform choose one.
304  *
305  * @param fontData the set of FontData objects to use initially, or null
306  *        to let the platform select a default when open() is called
307  *
308  * @see Font#getFontData
309  *
310  * @since 2.1.1
311  */
312 public void setFontList (FontData [] fontData) {
313     if (fontData !is null && fontData.length > 0) {
314         this.fontData = fontData [0];
315     } else {
316         this.fontData = null;
317     }
318 }
319
320 /**
321  * Sets the RGB describing the color to be selected by default
322  * in the dialog, or null to let the platform choose one.
323  *
324  * @param rgb the RGB value to use initially, or null to let
325  *        the platform select a default when open() is called
326  *
327  * @see PaletteData#getRGBs
328  *
329  * @since 2.1
330  */
331 public void setRGB (RGB rgb) {
332     this.rgb = rgb;
333 }
334
335 }
Note: See TracBrowser for help on using the browser.