1 /*******************************************************************************
2  * Copyright (c) 2006, 2016 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 
15 package org.eclipse.swt.examples.graphics;
16 
17 import org.eclipse.swt.SWT;
18 import org.eclipse.swt.graphics.Device;
19 import org.eclipse.swt.graphics.Font;
20 import org.eclipse.swt.graphics.GC;
21 import org.eclipse.swt.graphics.Path;
22 import org.eclipse.swt.graphics.Pattern;
23 import org.eclipse.swt.graphics.Point;
24 import org.eclipse.swt.graphics.Rectangle;
25 import org.eclipse.swt.layout.GridLayout;
26 import org.eclipse.swt.widgets.Button;
27 import org.eclipse.swt.widgets.Combo;
28 import org.eclipse.swt.widgets.Composite;
29 import org.eclipse.swt.widgets.Label;
30 import org.eclipse.swt.widgets.Menu;
31 
32 /**
33  * This tab demonstrates how to apply a path clipping and the effects of
34  * applying one.
35  */
36 public class PathClippingTab extends GraphicsTab {
37 
38 	private Combo clippingCb;
39 	private Button colorButton;
40 	private GraphicsBackground background;
41 	private Menu menu;
42 
PathClippingTab(GraphicsExample example)43 public PathClippingTab(GraphicsExample example) {
44 	super(example);
45 }
46 
47 @Override
getCategory()48 public String getCategory() {
49 	return GraphicsExample.getResourceString("Clipping"); //$NON-NLS-1$
50 }
51 
52 @Override
getText()53 public String getText() {
54 	return GraphicsExample.getResourceString("PathClipping"); //$NON-NLS-1$
55 }
56 
57 @Override
getDescription()58 public String getDescription() {
59 	return GraphicsExample.getResourceString("PathClippingDesc"); //$NON-NLS-1$
60 }
61 
62 @Override
dispose()63 public void dispose() {
64 	if (menu != null) {
65 		menu.dispose();
66 		menu = null;
67 	}
68 }
69 
70 /**
71  * Creates the widgets used to control the drawing.
72  */
73 @Override
createControlPanel(Composite parent)74 public void createControlPanel(Composite parent) {
75 
76 	// create drop down combo for choosing clipping
77 	Composite comp = new Composite(parent, SWT.NONE);
78 	comp.setLayout(new GridLayout(2, false));
79 
80 	new Label(comp, SWT.CENTER).setText(GraphicsExample
81 				.getResourceString("Clipping")); //$NON-NLS-1$
82 	clippingCb = new Combo(comp, SWT.DROP_DOWN);
83 	clippingCb.add(GraphicsExample.getResourceString("Circles")); //$NON-NLS-1$
84 	clippingCb.add(GraphicsExample.getResourceString("Rectangle")); //$NON-NLS-1$
85 	clippingCb.add(GraphicsExample.getResourceString("Oval")); //$NON-NLS-1$
86 	clippingCb.add(GraphicsExample.getResourceString("Word")); //$NON-NLS-1$
87 	clippingCb.add(GraphicsExample.getResourceString("Star")); //$NON-NLS-1$
88 	clippingCb.add(GraphicsExample.getResourceString("Triangles")); //$NON-NLS-1$
89 	clippingCb.add(GraphicsExample.getResourceString("Default")); //$NON-NLS-1$
90 	clippingCb.select(0);
91 	clippingCb.addListener(SWT.Selection, event -> example.redraw());
92 
93 	// color menu
94 	ColorMenu cm = new ColorMenu();
95 	cm.setPatternItems(example.checkAdvancedGraphics());
96 	menu = cm.createMenu(parent.getParent(), gb -> {
97 		background = gb;
98 		colorButton.setImage(gb.getThumbNail());
99 		example.redraw();
100 	});
101 
102 	// initialize the background to the 5th item in the menu (blue)
103 	background =(GraphicsBackground)menu.getItem(4).getData();
104 
105 	// color button
106 	comp = new Composite(parent, SWT.NONE);
107 	comp.setLayout(new GridLayout(2, false));
108 
109 	colorButton = new Button(comp, SWT.PUSH);
110 	colorButton.setText(GraphicsExample
111 			.getResourceString("Color")); //$NON-NLS-1$
112 	colorButton.setImage(background.getThumbNail());
113 	colorButton.addListener(SWT.Selection, event -> {
114 		final Button button = (Button) event.widget;
115 		final Composite parent1 = button.getParent();
116 		Rectangle bounds = button.getBounds();
117 		Point point = parent1.toDisplay(new Point(bounds.x, bounds.y));
118 		menu.setLocation(point.x, point.y + bounds.height);
119 		menu.setVisible(true);
120 	});
121 }
122 @Override
paint(GC gc, int width, int height)123 public void paint(GC gc, int width, int height) {
124 	if (!example.checkAdvancedGraphics()) return;
125 	Device device = gc.getDevice();
126 
127 	int clipping = clippingCb.getSelectionIndex();
128 	switch (clipping) {
129 
130 	case 0:		// circles
131 		Path path = new Path(device);
132 		path.addArc((width-width/5)/2, (height-height/5)/2, width/5, height/5, 0, 360);
133 		path.addArc(5*(width-width/8)/12, 4*(height-height/8)/12, width/8, height/8, 0, 360);
134 		path.addArc(7*(width-width/8)/12, 8*(height-height/8)/12, width/8, height/8, 0, 360);
135 		path.addArc(6*(width-width/12)/12, 3*(height-height/12)/12, width/12, height/12, 0, 360);
136 		path.addArc(6*(width-width/12)/12, 9*(height-height/12)/12, width/12, height/12, 0, 360);
137 		path.addArc(11.5f*(width-width/18)/20, 5*(height-height/18)/18, width/18, height/18, 0, 360);
138 		path.addArc(8.5f*(width-width/18)/20, 13*(height-height/18)/18, width/18, height/18, 0, 360);
139 		gc.setClipping(path);
140 		path.dispose();
141 		break;
142 	case 1:		// rectangle
143 		path = new Path(device);
144 		path.addRectangle(100, 100, width-200, height-200);
145 		path.addRectangle(120, 120, width-240, height-240);
146 		path.addRectangle(140, 140, width-280, height-280);
147 		gc.setClipping(path);
148 		path.dispose();
149 		break;
150 	case 2:		// circle
151 		path = new Path(device);
152 		path.addArc(100, 100, width-200, height-200, 0, 360);
153 		path.addArc((width-width/5)/2, (height-height/5)/2, width/5, height/5, 0, 360);
154 		path.addArc(5*(width-width/8)/12, 4*(height-height/8)/12, width/8, height/8, 0, 360);
155 		path.addArc(7*(width-width/8)/12, 8*(height-height/8)/12, width/8, height/8, 0, 360);
156 		path.addArc(6*(width-width/12)/12, 3*(height-height/12)/12, width/12, height/12, 0, 360);
157 		path.addArc(6*(width-width/12)/12, 9*(height-height/12)/12, width/12, height/12, 0, 360);
158 		path.addArc(11.5f*(width-width/18)/20, 5*(height-height/18)/18, width/18, height/18, 0, 360);
159 		path.addArc(8.5f*(width-width/18)/20, 13*(height-height/18)/18, width/18, height/18, 0, 360);
160 		gc.setClipping(path);
161 		path.dispose();
162 		break;
163 	case 3:		// word
164 		path = new Path(device);
165 		String text = GraphicsExample.getResourceString("SWT"); //$NON-NLS-1$
166 		Font font = new Font(device, "Times", 200, SWT.NORMAL);
167 		gc.setFont(font);
168 		Point size = gc.stringExtent(text);
169 		path.addString(text, (width-size.x)/2, (height-size.y)/2, font);
170 		font.dispose();
171 		gc.setClipping(path);
172 		path.dispose();
173 		break;
174 	case 4:		// star
175 		path = new Path(device);
176 		path.lineTo(width/2, 0);
177 		path.lineTo(5*width/8, height/3);
178 		path.lineTo(width, height/3);
179 		path.lineTo(3*width/4, 10*height/16);
180 		path.lineTo(7*width/8, height);
181 		path.lineTo(width/2, 3*height/4);
182 		path.lineTo(width/8, height);
183 		path.lineTo(width/4, 10*height/16);
184 		path.lineTo(0, height/3);
185 		path.lineTo(3*width/8, height/3);
186 		path.lineTo(width/2, 0);
187 
188 		Path ovalPath = new Path(device);
189 		ovalPath.addArc(90, 90, width-180, height-180, 0, 360);
190 
191 		path.addPath(ovalPath);
192 		gc.setClipping(path);
193 		ovalPath.dispose();
194 		path.dispose();
195 		break;
196 	case 5:		// triangles
197 		path = new Path(device);
198 		path.addRectangle(0, 0, width, height);
199 		path.lineTo(width/4, 0);
200 		path.lineTo(width/4, height/2);
201 		path.lineTo(0, height/2);
202 		path.lineTo(width/4, 0);
203 
204 		Path path2 = new Path(device);
205 		path2.lineTo(width/2, 0);
206 		path2.lineTo(width/4, height/2);
207 		path2.lineTo(3*width/4, height/2);
208 		path2.lineTo(width/2, 0);
209 
210 		Path path3 = new Path(device);
211 		path3.lineTo(3*width/4, 0);
212 		path3.lineTo(3*width/4, height/2);
213 		path3.lineTo(width, height/2);
214 		path3.lineTo(3*width/4, 0);
215 
216 		Path path4 = new Path(device);
217 		path4.lineTo(0, height);
218 		path4.lineTo(width/4, height/2);
219 		path4.lineTo(width/2, height);
220 		path4.lineTo(0, height);
221 
222 		Path path5 = new Path(device);
223 		path5.lineTo(width/2, height);
224 		path5.lineTo(3*width/4, height/2);
225 		path5.lineTo(width, height);
226 		path5.lineTo(width/2, height);
227 
228 		path.addPath(path2);
229 		path.addPath(path3);
230 		path.addPath(path4);
231 		path.addPath(path5);
232 		gc.setClipping(path);
233 
234 		path5.dispose();
235 		path4.dispose();
236 		path3.dispose();
237 		path2.dispose();
238 		path.dispose();
239 		break;
240 	case 6:		// default
241 		gc.setClipping(0, 0, width, height);
242 		break;
243 	}
244 
245 	Pattern pattern = null;
246 	if (background.getBgColor1() != null) {
247 		gc.setBackground(background.getBgColor1());
248 	} else if (background.getBgImage() != null) {
249 		pattern = new Pattern(device, background.getBgImage());
250 		gc.setBackgroundPattern(pattern);
251 	}
252 	gc.fillRectangle(0, 0, width, height);
253 	if (pattern != null) pattern.dispose();
254 }
255 }
256