Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 26a2234a5c680c6dbc4ce5496c5d9cae9e2b65e4 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
/*******************************************************************************
 * Copyright (c) 2006, 2016 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/

package org.eclipse.swt.examples.graphics;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Device;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.FontData;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Pattern;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.Spinner;

/**
 * This tab demonstrates various text fonts. It allows the user to specify font
 * parameters such as face, style and size.
 */
public class CustomFontTab extends GraphicsTab {

	String text = GraphicsExample.getResourceString("SWT");
	GraphicsBackground fontForeground;
	Combo fontFaceCb, fontStyleCb;
	Spinner fontPointSpinner;
	Button colorButton;
	List<String> fontNames;
	int [] styleValues;
	String [] fontStyles;
	Menu menu;

public CustomFontTab(GraphicsExample example) {
	super(example);

	// create list of fonts for this platform
	FontData [] fontData = Display.getCurrent().getFontList(null, true);
	fontNames = new ArrayList<>();
	for (FontData element : fontData) {
		// remove duplicates and sort
		String nextName = element.getName();
		if (!fontNames.contains(nextName)) {
			int j = 0;
			while(j < fontNames.size() && nextName.compareTo(fontNames.get(j)) > 0) {
				j++;
			}
			fontNames.add(j, nextName);
		}
	}
	fontStyles = new String [] {
			GraphicsExample.getResourceString("Regular"), //$NON-NLS-1$
			GraphicsExample.getResourceString("Italic"), //$NON-NLS-1$
			GraphicsExample.getResourceString("Bold"), //$NON-NLS-1$
			GraphicsExample.getResourceString("BoldItalic") //$NON-NLS-1$
	};
	styleValues = new int [] {SWT.NORMAL, SWT.ITALIC, SWT.BOLD, SWT.BOLD | SWT.ITALIC};
}

@Override
public String getCategory() {
	return GraphicsExample.getResourceString("Font"); //$NON-NLS-1$
}

@Override
public String getText() {
	return GraphicsExample.getResourceString("CustomFont"); //$NON-NLS-1$
}

@Override
public String getDescription() {
	return GraphicsExample.getResourceString("CustomFontDescription"); //$NON-NLS-1$
}

@Override
public void dispose() {
	if (menu != null) {
		menu.dispose();
		menu = null;
	}
}

@Override
public void createControlPanel(Composite parent) {

	Composite mainComp = new Composite(parent, SWT.NONE);
	mainComp.setLayout(new RowLayout());

	// create combo for font face
	Composite comp = new Composite(mainComp, SWT.NONE);
	comp.setLayout(new GridLayout(2, false));

	new Label(comp, SWT.LEFT).setText(GraphicsExample.getResourceString("FontFace")); //$NON-NLS-1$
	fontFaceCb = new Combo(comp, SWT.DROP_DOWN);
	for (String name : fontNames) {
		fontFaceCb.add(name);
	}
	fontFaceCb.select(0);
	fontFaceCb.addListener(SWT.Selection, event -> example.redraw());

	// create combo for font style
	comp = new Composite(mainComp, SWT.NONE);
	comp.setLayout(new GridLayout(2, false));

	new Label(comp, SWT.LEFT).setText(GraphicsExample.getResourceString("FontStyle")); //$NON-NLS-1$
	fontStyleCb = new Combo(comp, SWT.DROP_DOWN);
	for (String fontStyle : fontStyles) {
		fontStyleCb.add(fontStyle);
	}
	fontStyleCb.select(0);
	fontStyleCb.addListener(SWT.Selection, event -> example.redraw());

	// create spinner for font size (points)
	comp = new Composite(mainComp, SWT.NONE);
	comp.setLayout(new GridLayout(2, false));

	new Label(comp, SWT.LEFT).setText(GraphicsExample.getResourceString("FontSize")); //$NON-NLS-1$
	fontPointSpinner = new Spinner(comp, SWT.BORDER | SWT.WRAP);
	fontPointSpinner.setMinimum(1);
	fontPointSpinner.setMaximum(1000);
	fontPointSpinner.setSelection(200);
	fontPointSpinner.addListener(SWT.Selection, event -> example.redraw());

	ColorMenu cm = new ColorMenu();
	cm.setColorItems(true);
	cm.setPatternItems(example.checkAdvancedGraphics());
	menu = cm.createMenu(parent.getParent(), gb -> {
		fontForeground = gb;
		colorButton.setImage(gb.getThumbNail());
		example.redraw();
	});

	// initialize the background to the 2nd item in the menu (black)
	fontForeground = (GraphicsBackground)menu.getItem(1).getData();

	// create color button
	comp = new Composite(parent, SWT.NONE);
	comp.setLayout(new GridLayout());

	colorButton = new Button(comp, SWT.PUSH);
	colorButton.setText(GraphicsExample.getResourceString("Color")); //$NON-NLS-1$
	colorButton.setImage(fontForeground.getThumbNail());
	colorButton.addListener(SWT.Selection, event -> {
		final Button button = (Button) event.widget;
		final Composite parent1 = button.getParent();
		Rectangle bounds = button.getBounds();
		Point point = parent1.toDisplay(new Point(bounds.x, bounds.y));
		menu.setLocation(point.x, point.y + bounds.height);
		menu.setVisible(true);
	});
}

@Override
public void paint(GC gc, int width, int height) {
	if (!example.checkAdvancedGraphics()) return;
	Device device = gc.getDevice();

	String fontFace = fontNames.get(fontFaceCb.getSelectionIndex());
	int points = fontPointSpinner.getSelection();
	int style = styleValues[fontStyleCb.getSelectionIndex()];

	Font font = new Font(device, fontFace, points, style);
	gc.setFont(font);
	gc.setTextAntialias(SWT.ON);

	Point size = gc.stringExtent(text);
	int textWidth = size.x;
	int textHeight = size.y;

	Pattern pattern = null;
	if (fontForeground.getBgColor1() != null) {
		gc.setForeground(fontForeground.getBgColor1());
	} else if (fontForeground.getBgImage() != null) {
		pattern = new Pattern(device, fontForeground.getBgImage());
		gc.setForegroundPattern(pattern);
	}

	gc.drawString(text, (width-textWidth)/2, (height-textHeight)/2, true);

	font.dispose();
	if (pattern != null) pattern.dispose();
}

}

Back to the top