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.GC;
20 import org.eclipse.swt.graphics.Point;
21 import org.eclipse.swt.graphics.Rectangle;
22 import org.eclipse.swt.graphics.Region;
23 import org.eclipse.swt.layout.GridLayout;
24 import org.eclipse.swt.widgets.Button;
25 import org.eclipse.swt.widgets.Combo;
26 import org.eclipse.swt.widgets.Composite;
27 import org.eclipse.swt.widgets.Label;
28 import org.eclipse.swt.widgets.Menu;
29
30 /**
31 * This tab demonstrates how to apply a region clipping and the effects of
32 * applying one.
33 */
34 public class RegionClippingTab extends GraphicsTab {
35
36 private Combo clippingCb;
37 private Button colorButton1, colorButton2;
38 private Menu menu1, menu2;
39 private GraphicsBackground colorGB1, colorGB2;
40
RegionClippingTab(GraphicsExample example)41 public RegionClippingTab(GraphicsExample example) {
42 super(example);
43 }
44
45 @Override
getCategory()46 public String getCategory() {
47 return GraphicsExample.getResourceString("Clipping"); //$NON-NLS-1$
48 }
49
50 @Override
getText()51 public String getText() {
52 return GraphicsExample.getResourceString("RegionClipping"); //$NON-NLS-1$
53 }
54
55 @Override
getDescription()56 public String getDescription() {
57 return GraphicsExample.getResourceString("RegionClippingDescription"); //$NON-NLS-1$
58 }
59
60 @Override
dispose()61 public void dispose() {
62 if (menu1 != null) {
63 menu1.dispose();
64 menu1 = null;
65 }
66 if (menu2 != null) {
67 menu2.dispose();
68 menu2 = null;
69 }
70 }
71
72 /**
73 * Creates the widgets used to control the drawing.
74 */
75 @Override
createControlPanel(Composite parent)76 public void createControlPanel(Composite parent) {
77 // create drop down combo for choosing clipping
78 Composite comp = new Composite(parent, SWT.NONE);
79 comp.setLayout(new GridLayout(2, false));
80
81 new Label(comp, SWT.CENTER).setText(GraphicsExample
82 .getResourceString("Clipping")); //$NON-NLS-1$
83 clippingCb = new Combo(comp, SWT.DROP_DOWN);
84 clippingCb.add(GraphicsExample.getResourceString("Region1")); //$NON-NLS-1$
85 clippingCb.add(GraphicsExample.getResourceString("Region2")); //$NON-NLS-1$
86 clippingCb.add(GraphicsExample.getResourceString("Add")); //$NON-NLS-1$
87 clippingCb.add(GraphicsExample.getResourceString("Sub")); //$NON-NLS-1$
88 clippingCb.add(GraphicsExample.getResourceString("Inter")); //$NON-NLS-1$
89 clippingCb.select(0);
90 clippingCb.addListener(SWT.Selection, event -> example.redraw());
91
92 // color menu
93 ColorMenu cm = new ColorMenu();
94 menu1 = cm.createMenu(parent.getParent(), gb -> {
95 colorGB1 = gb;
96 colorButton1.setImage(gb.getThumbNail());
97 example.redraw();
98 });
99 menu2 = cm.createMenu(parent.getParent(), gb -> {
100 colorGB2 = gb;
101 colorButton2.setImage(gb.getThumbNail());
102 example.redraw();
103 });
104
105 // initialize the color to blue
106 colorGB1 = (GraphicsBackground)menu1.getItem(4).getData();
107 // initialize the color to red
108 colorGB2 = (GraphicsBackground)menu2.getItem(2).getData();
109
110 // color button 1
111 comp = new Composite(parent, SWT.NONE);
112 comp.setLayout(new GridLayout(2, false));
113
114 colorButton1 = new Button(comp, SWT.PUSH);
115 colorButton1.setText(GraphicsExample
116 .getResourceString("Color1")); //$NON-NLS-1$
117 colorButton1.setImage(colorGB1.getThumbNail());
118 colorButton1.addListener(SWT.Selection, event -> {
119 final Button button = (Button) event.widget;
120 final Composite parent1 = button.getParent();
121 Rectangle bounds = button.getBounds();
122 Point point = parent1.toDisplay(new Point(bounds.x, bounds.y));
123 menu1.setLocation(point.x, point.y + bounds.height);
124 menu1.setVisible(true);
125 });
126
127 // color button 2
128 comp = new Composite(parent, SWT.NONE);
129 comp.setLayout(new GridLayout(2, false));
130
131 colorButton2 = new Button(comp, SWT.PUSH);
132 colorButton2.setText(GraphicsExample
133 .getResourceString("Color2")); //$NON-NLS-1$
134 colorButton2.setImage(colorGB2.getThumbNail());
135 colorButton2.addListener(SWT.Selection, event -> {
136 final Button button = (Button) event.widget;
137 final Composite parent1 = button.getParent();
138 Rectangle bounds = button.getBounds();
139 Point point = parent1.toDisplay(new Point(bounds.x, bounds.y));
140 menu2.setLocation(point.x, point.y + bounds.height);
141 menu2.setVisible(true);
142 });
143 }
144
145 @Override
paint(GC gc, int width, int height)146 public void paint(GC gc, int width, int height) {
147 if (!example.checkAdvancedGraphics()) return;
148 Device device = gc.getDevice();
149
150 // array of coordinate points of polygon 1 (region 1)
151 int [] polygon1 = new int [] {10, height/2, 9*width/16, 10, 9*width/16, height-10};
152 Region region1 = new Region(device);
153 region1.add(polygon1);
154
155 // array of coordinate points of polygon 2 (region 2)
156 int [] polygon2 = new int [] {
157 9*width/16, 10,
158 9*width/16, height/8,
159 7*width/16, 2*height/8,
160 9*width/16, 3*height/8,
161 7*width/16, 4*height/8,
162 9*width/16, 5*height/8,
163 7*width/16, 6*height/8,
164 9*width/16, 7*height/8,
165 9*width/16, height-10,
166 width-10, height/2
167 };
168 Region region2 = new Region(device);
169 region2.add(polygon2);
170
171 gc.setAlpha(127);
172
173 int clippingIndex = clippingCb.getSelectionIndex();
174
175 switch (clippingIndex) {
176 case 0:
177 // region 1
178 gc.setClipping(region1);
179 gc.setBackground(colorGB1.getBgColor1());
180 gc.fillPolygon(polygon1);
181 break;
182 case 1:
183 // region 2
184 gc.setClipping(region2);
185 gc.setBackground(colorGB2.getBgColor1());
186 gc.fillPolygon(polygon2);
187 break;
188 case 2:
189 // add
190 region1.add(region2);
191 break;
192 case 3:
193 // sub
194 region1.subtract(region2);
195 break;
196 case 4:
197 // intersect
198 region1.intersect(region2);
199 break;
200 }
201
202 if (clippingIndex > 1) {
203 gc.setClipping(region1);
204
205 gc.setBackground(colorGB1.getBgColor1());
206 gc.fillPolygon(polygon1);
207
208 gc.setBackground(colorGB2.getBgColor1());
209 gc.fillPolygon(polygon2);
210 }
211
212 region1.dispose();
213 region2.dispose();
214 }
215 }
216