KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > swt > widgets > FontDialog


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 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  *******************************************************************************/

11 package org.eclipse.swt.widgets;
12
13
14 import org.eclipse.swt.internal.win32.*;
15 import org.eclipse.swt.*;
16 import org.eclipse.swt.graphics.*;
17
18 /**
19  * Instances of this class allow the user to select a font
20  * from all available fonts in the system.
21  * <dl>
22  * <dt><b>Styles:</b></dt>
23  * <dd>(none)</dd>
24  * <dt><b>Events:</b></dt>
25  * <dd>(none)</dd>
26  * </dl>
27  * <p>
28  * IMPORTANT: This class is intended to be subclassed <em>only</em>
29  * within the SWT implementation.
30  * </p>
31  */

32 public class FontDialog extends Dialog {
33     FontData fontData;
34     RGB rgb;
35     
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 /**
54  * Constructs a new instance of this class given its parent
55  * and a style value describing its behavior and appearance.
56  * <p>
57  * The style value is either one of the style constants defined in
58  * class <code>SWT</code> which is applicable to instances of this
59  * class, or must be built by <em>bitwise OR</em>'ing together
60  * (that is, using the <code>int</code> "|" operator) two or more
61  * of those <code>SWT</code> style constants. The class description
62  * lists the style constants that are applicable to the class.
63  * Style bits are also inherited from superclasses.
64  * </p>
65  *
66  * @param parent a shell which will be the parent of the new instance
67  * @param style the style of dialog to construct
68  *
69  * @exception IllegalArgumentException <ul>
70  * <li>ERROR_NULL_ARGUMENT - if the parent is null</li>
71  * </ul>
72  * @exception SWTException <ul>
73  * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the parent</li>
74  * <li>ERROR_INVALID_SUBCLASS - if this class is not an allowed subclass</li>
75  * </ul>
76  */

77 public FontDialog (Shell parent, int style) {
78     super (parent, style);
79     checkSubclass ();
80 }
81
82 /**
83  * Returns a FontData object describing the font that was
84  * selected in the dialog, or null if none is available.
85  *
86  * @return the FontData for the selected font, or null
87  * @deprecated use #getFontList ()
88  */

89 public FontData getFontData () {
90     return fontData;
91 }
92
93 /**
94  * Returns a FontData set 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  * @since 2.1.1
99  */

100 public FontData [] getFontList () {
101     if (fontData == null) return null;
102     FontData [] result = new FontData [1];
103     result [0] = fontData;
104     return result;
105 }
106
107 /**
108  * Returns an RGB describing the color that was selected
109  * in the dialog, or null if none is available.
110  *
111  * @return the RGB value for the selected color, or null
112  *
113  * @see PaletteData#getRGBs
114  *
115  * @since 2.1
116  */

117 public RGB getRGB () {
118     return rgb;
119 }
120
121 /**
122  * Makes the dialog visible and brings it to the front
123  * of the display.
124  *
125  * @return a FontData object describing the font that was selected,
126  * or null if the dialog was cancelled or an error occurred
127  *
128  * @exception SWTException <ul>
129  * <li>ERROR_WIDGET_DISPOSED - if the dialog has been disposed</li>
130  * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the dialog</li>
131  * </ul>
132  */

133 public FontData open () {
134     if (OS.IsWinCE) SWT.error (SWT.ERROR_NOT_IMPLEMENTED);
135     
136     /* Get the owner HWND for the dialog */
137     int hwndOwner = 0;
138     if (parent != null) hwndOwner = parent.handle;
139
140     /* Open the dialog */
141     int hHeap = OS.GetProcessHeap ();
142     CHOOSEFONT lpcf = new CHOOSEFONT ();
143     lpcf.lStructSize = CHOOSEFONT.sizeof;
144     lpcf.hwndOwner = hwndOwner;
145     lpcf.Flags = OS.CF_SCREENFONTS | OS.CF_EFFECTS;
146     int lpLogFont = OS.HeapAlloc (hHeap, OS.HEAP_ZERO_MEMORY, LOGFONT.sizeof);
147     if (fontData != null && fontData.data != null) {
148         LOGFONT logFont = fontData.data;
149         int lfHeight = logFont.lfHeight;
150         int hDC = OS.GetDC (0);
151         int pixels = -(int)(0.5f + (fontData.height * OS.GetDeviceCaps(hDC, OS.LOGPIXELSY) / 72));
152         OS.ReleaseDC (0, hDC);
153         logFont.lfHeight = pixels;
154         lpcf.Flags |= OS.CF_INITTOLOGFONTSTRUCT;
155         OS.MoveMemory (lpLogFont, logFont, LOGFONT.sizeof);
156         logFont.lfHeight = lfHeight;
157     }
158     lpcf.lpLogFont = lpLogFont;
159     if (rgb != null) {
160         int red = rgb.red & 0xFF;
161         int green = (rgb.green << 8) & 0xFF00;
162         int blue = (rgb.blue << 16) & 0xFF0000;
163         lpcf.rgbColors = red | green | blue;
164     }
165     
166     /* Make the parent shell be temporary modal */
167     Shell oldModal = null;
168     Display display = null;
169     if ((style & (SWT.APPLICATION_MODAL | SWT.SYSTEM_MODAL)) != 0) {
170         display = parent.getDisplay ();
171         oldModal = display.getModalDialogShell ();
172         display.setModalDialogShell (parent);
173     }
174
175     /* Open the dialog */
176     boolean success = OS.ChooseFont (lpcf);
177     
178     /* Clear the temporary dialog modal parent */
179     if ((style & (SWT.APPLICATION_MODAL | SWT.SYSTEM_MODAL)) != 0) {
180         display.setModalDialogShell (oldModal);
181     }
182     
183     /* Compute the result */
184     if (success) {
185         LOGFONT logFont = OS.IsUnicode ? (LOGFONT) new LOGFONTW () : new LOGFONTA ();
186         OS.MoveMemory (logFont, lpLogFont, LOGFONT.sizeof);
187
188         /*
189          * This will not work on multiple screens or
190          * for printing. Should use DC for the proper device.
191          */

192         int hDC = OS.GetDC(0);
193         int logPixelsY = OS.GetDeviceCaps(hDC, OS.LOGPIXELSY);
194         int pixels = 0;
195         if (logFont.lfHeight > 0) {
196             /*
197              * Feature in Windows. If the lfHeight of the LOGFONT structure
198              * is positive, the lfHeight measures the height of the entire
199              * cell, including internal leading, in logical units. Since the
200              * height of a font in points does not include the internal leading,
201              * we must subtract the internal leading, which requires a TEXTMETRIC,
202              * which in turn requires font creation.
203              */

204             int hFont = OS.CreateFontIndirect(logFont);
205             int oldFont = OS.SelectObject(hDC, hFont);
206             TEXTMETRIC lptm = OS.IsUnicode ? (TEXTMETRIC) new TEXTMETRICW () : new TEXTMETRICA ();
207             OS.GetTextMetrics(hDC, lptm);
208             OS.SelectObject(hDC, oldFont);
209             OS.DeleteObject(hFont);
210             pixels = logFont.lfHeight - lptm.tmInternalLeading;
211         } else {
212             pixels = -logFont.lfHeight;
213         }
214         OS.ReleaseDC(0, hDC);
215
216         float points = pixels * 72f /logPixelsY;
217         fontData = FontData.win32_new (logFont, points);
218         int red = lpcf.rgbColors & 0xFF;
219         int green = (lpcf.rgbColors >> 8) & 0xFF;
220         int blue = (lpcf.rgbColors >> 16) & 0xFF;
221         rgb = new RGB (red, green, blue);
222     }
223         
224     /* Free the OS memory */
225     if (lpLogFont != 0) OS.HeapFree (hHeap, 0, lpLogFont);
226
227     /*
228     * This code is intentionally commented. On some
229     * platforms, the owner window is repainted right
230     * away when a dialog window exits. This behavior
231     * is currently unspecified.
232     */

233 // if (hwndOwner != 0) OS.UpdateWindow (hwndOwner);
234

235     if (!success) return null;
236     return fontData;
237 }
238
239 /**
240  * Sets a FontData object describing the font to be
241  * selected by default in the dialog, or null to let
242  * the platform choose one.
243  *
244  * @param fontData the FontData to use initially, or null
245  * @deprecated use #setFontList (FontData [])
246  */

247 public void setFontData (FontData fontData) {
248     this.fontData = fontData;
249 }
250
251 /**
252  * Sets the set of FontData objects describing the font to
253  * be selected by default in the dialog, or null to let
254  * the platform choose one.
255  *
256  * @param fontData the set of FontData objects to use initially, or null
257  * to let the platform select a default when open() is called
258  *
259  * @see Font#getFontData
260  *
261  * @since 2.1.1
262  */

263 public void setFontList (FontData [] fontData) {
264     if (fontData != null && fontData.length > 0) {
265         this.fontData = fontData [0];
266     } else {
267         this.fontData = null;
268     }
269 }
270
271 /**
272  * Sets the RGB describing the color to be selected by default
273  * in the dialog, or null to let the platform choose one.
274  *
275  * @param rgb the RGB value to use initially, or null to let
276  * the platform select a default when open() is called
277  *
278  * @see PaletteData#getRGBs
279  *
280  * @since 2.1
281  */

282 public void setRGB (RGB rgb) {
283     this.rgb = rgb;
284 }
285
286 }
287
Popular Tags