source: trunk/tests/SWT/java/SWT008_02.java

Last change on this file was 12, checked in by lpino, 18 years ago
  • Initial commit
File size: 8.2 KB
Line 
1/*
2* SWT008_02.java
3*/
4
5/*
6* Copyright (c) 2002, 2004 EclipseOS2 Team.
7* This file is made available under the terms of the Common Public License v1.0
8* which accompanies this distribution, and is available at
9* http://www.eclipse.org/legal/cpl-v10.html
10*/
11
12import org.eclipse.swt.*;
13import org.eclipse.swt.widgets.*;
14import org.eclipse.swt.events.*;
15import org.eclipse.swt.graphics.*;
16import org.eclipse.swt.layout.*;
17
18/**
19* A simple testcase that allows to call the font dialog changing the font of the text displaying
20 * the fonts attribute.
21*/
22
23public class SWT008_02 extends SWTTestCase {
24 private Font font;
25 boolean drawCheckers = false;
26 Class fmHndCls;
27 String platform;
28 private FontData newFont;
29 Canvas canvas;
30 Image background = null;
31
32 static {
33 STEP = "008";
34 TEST = "02";
35 DESC = "Font Dialog";
36 }
37
38 public static void main (String [] args) {
39 go (new SWT008_02 ());
40 }
41
42 Shell createTopShell (Display display) {
43 return new Shell (display,SWT.SHELL_TRIM | SWT.NO_REDRAW_RESIZE);
44 }
45
46 void initComponents () {
47 super.initComponents();
48 //Initiates the font with SystemFont
49 newFont = ((Font)(Display.getDefault().getSystemFont())).getFontData()[0];
50 canvas = new Canvas (shell, 0);
51 canvas.addPaintListener(new PaintListener () {
52 public void paintControl(PaintEvent event) {
53 GC gc = event.gc;
54
55 Rectangle r = new Rectangle (event.x, event.y, event.width, event.height);
56 gc.setClipping (r);
57
58 drawGrid (gc,
59 (r.x / 30) * 20, (r.y / 30) * 20,
60 r.width + 30, r.height + 30, 10);
61
62 int y = 0;
63 y = drawFont (gc, newFont, 0, 0);
64 }
65 });
66
67 Composite bottom = new Composite (shell, 0);
68 Composite group1 = new Composite (bottom, SWT.NO_BACKGROUND);
69 group1.setLayout (new FillLayout (SWT.HORIZONTAL));
70
71 shell.setLayout(new GridLayout(2, false));
72 Button button = new Button(group1, SWT.PUSH);
73 button.setText("Font...");
74 button.addSelectionListener(new SelectionAdapter() {
75 public void widgetSelected(SelectionEvent event) {
76 FontDialog dlg = new FontDialog(shell);
77 FontData ret = null;
78 FontData defaultFont = ((Font)(Display.getDefault().getSystemFont())).getFontData()[0];
79 dlg.setFontData(defaultFont);
80 ret = dlg.open();
81 if (ret == null){
82 return;
83 }
84 else{
85 newFont = ret;
86 System.out.println("Height = " + newFont.getHeight());
87 System.out.println("Name = " + newFont.getName());
88 System.out.println("Style = " + newFont.getStyle());
89 canvas.redraw();
90 }
91 }
92 });
93
94 FormData fdata = new FormData();
95 fdata.right = new FormAttachment (100, 0);
96 fdata.bottom = new FormAttachment (100, 0);
97 group1.setLayoutData (fdata);
98 FormLayout formLayout = new FormLayout ();
99 bottom.setLayout (formLayout);
100
101 GridData gdata = new GridData (GridData.HORIZONTAL_ALIGN_FILL | GridData.GRAB_HORIZONTAL | GridData.VERTICAL_ALIGN_FILL | GridData.GRAB_VERTICAL);
102 canvas.setLayoutData (gdata);
103 gdata = new GridData (GridData.HORIZONTAL_ALIGN_FILL | GridData.GRAB_HORIZONTAL | GridData.VERTICAL_ALIGN_CENTER);
104 bottom.setLayoutData (gdata);
105
106 GridLayout gridLayout = new GridLayout (1, false);
107 // Win32 workaround: this forces canvas bounds to be properly
108 // calculated for the maximized state, since windows in Windows
109 // cannot be maximized when not visible, in that sense that their
110 // real size won't change until the window is actually shown.
111
112 Rectangle dr = display.getBounds ();
113 Rectangle sr = new Rectangle (0, 0, 640, 100);
114 sr.x = (dr.width-sr.width)/2;
115 sr.y = (dr.height-sr.height)/2;
116 shell.setBounds (sr);
117
118 }
119 void drawGrid (GC gc, int x0, int y0, int width, int height, int step) {
120 if (drawCheckers) {
121 if (background == null) {
122 int w = 100, h = 100;
123 Display display = Display.getDefault();
124 background = new Image (display, w, h);
125 GC bgc = new GC (background);
126 bgc.setBackground (display.getSystemColor (SWT.COLOR_WHITE));
127 bgc.fillRectangle(0, 0, 100, 100);
128 bgc.fillRectangle(100, 100, 100, 100);
129 bgc.setBackground (display.getSystemColor (SWT.COLOR_GRAY));
130 bgc.fillRectangle(100, 0, 100, 100);
131 bgc.fillRectangle(0, 100, 100, 100);
132 bgc.dispose();
133 }
134 Rectangle r = background.getBounds();
135 for (int y = 0; y <= height; y += r.height)
136 for (int x = 0; x <= width; x += r.width)
137 gc.drawImage (background, x0 + x, y0 + y);
138 } else {
139 gc.fillRectangle (gc.getClipping());
140 }
141 }
142 int drawFont (GC gc, FontData fd, int x0, int y0) {
143// Font f = fonts [idx];
144// FontData fd = f.getFontData() [0];
145 String str = " " + fd.getHeight() + ".";
146 String name;
147 name = fd.getName();
148 str += name.length() == 0 ? "<uninitialized>" : name;
149 int style = fd.getStyle();
150 if (style != SWT.NORMAL) {
151 str += " [";
152 if ((style & SWT.BOLD) != 0) str += "B";
153 if ((style & SWT.ITALIC) != 0) str += "I";
154 str += "]";
155 }
156
157 gc.setForeground (display.getSystemColor (SWT.COLOR_BLACK));
158 gc.setBackground (display.getSystemColor (SWT.COLOR_CYAN));
159 Font f = new Font(display, fd);
160 gc.setFont (f);
161 FontMetrics fm = gc.getFontMetrics();
162 Point box;
163
164 str += " " + getAdditionalFontInfo (fd, fm);
165 gc.drawString (str, x0, y0, true);
166 box = gc.stringExtent (str);
167
168 return fm.getHeight();
169 }
170
171 String getAdditionalFontInfo (FontData fd, FontMetrics fm) {
172 if (fmHndCls == null) {
173 fmHndCls = fm.handle.getClass();
174 platform = SWT.getPlatform();
175 }
176
177 int lMatch = 0;
178 int actualHeight = 0;
179 String faceName = "<not_available>";
180
181 Point dpi = display.getDPI();
182 actualHeight =
183 Math.round ((float)((fm.getAscent() + fm.getDescent()) * 72) / dpi.y);
184
185 if (platform.equals ("pm")) {
186 short fsDefn;
187 short sNominalPointSize;
188 byte[] szFacename = null;
189 try {
190 lMatch = fmHndCls.getField ("lMatch").getInt (fm.handle);
191 fsDefn = fmHndCls.getField ("fsDefn").getShort (fm.handle);
192 sNominalPointSize = fmHndCls.getField ("sNominalPointSize").getShort (fm.handle);
193 szFacename = (byte[])fmHndCls.getField ("szFacename").get (fm.handle);
194
195 if ((fsDefn & 0x0001 /* OS.FM_DEFN_OUTLINE */) == 0) {
196 actualHeight = sNominalPointSize / 10;
197 }
198 int i = 0;
199 while (szFacename [i] != 0) i++;
200 faceName = new String (szFacename, 0, i);
201 } catch (NoSuchFieldException x) {
202 } catch (IllegalAccessException x) {
203 }
204 }
205
206 String str = "[" +
207 actualHeight + "." +
208 faceName + ";" +
209 fm.getHeight() + "=" +
210 fm.getLeading() + "+" + fm.getAscent() + "+" + fm.getDescent() + ";" +
211 fm.getAverageCharWidth() + ";" +
212 lMatch +
213 "] ";
214
215 return str;
216 }
217
218}
219
Note: See TracBrowser for help on using the repository browser.