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.Pattern;
22 import org.eclipse.swt.graphics.Point;
23 import org.eclipse.swt.graphics.Rectangle;
24 import org.eclipse.swt.layout.GridLayout;
25 import org.eclipse.swt.widgets.Button;
26 import org.eclipse.swt.widgets.Composite;
27 import org.eclipse.swt.widgets.Menu;
28 
29 /**
30  * This tab demonstrates various line caps applicable to a line.
31  */
32 public class LineCapTab extends GraphicsTab {
33 
34 	Button colorButton;
35 	GraphicsBackground foreground;
36 	Menu menu;
37 
LineCapTab(GraphicsExample example)38 public LineCapTab(GraphicsExample example) {
39 	super(example);
40 }
41 
42 @Override
getCategory()43 public String getCategory() {
44 	return GraphicsExample.getResourceString("Lines"); //$NON-NLS-1$
45 }
46 
47 @Override
getText()48 public String getText() {
49 	return GraphicsExample.getResourceString("LineCap"); //$NON-NLS-1$
50 }
51 
52 @Override
getDescription()53 public String getDescription() {
54 	return GraphicsExample.getResourceString("LineCapDescription"); //$NON-NLS-1$
55 }
56 
57 @Override
dispose()58 public void dispose() {
59 	if (menu != null) {
60 		menu.dispose();
61 		menu = null;
62 	}
63 }
64 
65 @Override
createControlPanel(Composite parent)66 public void createControlPanel(Composite parent) {
67 
68 	Composite comp;
69 
70 	// create color button
71 	comp = new Composite(parent, SWT.NONE);
72 	comp.setLayout(new GridLayout());
73 
74 	ColorMenu cm = new ColorMenu();
75 	cm.setPatternItems(example.checkAdvancedGraphics());
76 	menu = cm.createMenu(parent.getParent(), gb -> {
77 		foreground = gb;
78 		colorButton.setImage(gb.getThumbNail());
79 		example.redraw();
80 	});
81 
82 	// initialize the foreground to the 3rd item in the menu (red)
83 	foreground = (GraphicsBackground)menu.getItem(2).getData();
84 
85 	// color button
86 	colorButton = new Button(comp, SWT.PUSH);
87 	colorButton.setText(GraphicsExample
88 			.getResourceString("Color")); //$NON-NLS-1$
89 	colorButton.setImage(foreground.getThumbNail());
90 	colorButton.addListener(SWT.Selection, event -> {
91 		final Button button = (Button) event.widget;
92 		final Composite parent1 = button.getParent();
93 		Rectangle bounds = button.getBounds();
94 		Point point = parent1.toDisplay(new Point(bounds.x, bounds.y));
95 		menu.setLocation(point.x, point.y + bounds.height);
96 		menu.setVisible(true);
97 	});
98 }
99 
100 @Override
paint(GC gc, int width, int height)101 public void paint(GC gc, int width, int height) {
102 	Device device = gc.getDevice();
103 
104 	// draw side lines
105 	gc.setLineWidth(1);
106 	gc.setLineStyle(SWT.LINE_DOT);
107 	gc.setForeground(device.getSystemColor(SWT.COLOR_BLACK));
108 	gc.drawLine(3*width/16, height/6, 3*width/16, 5*height/6);
109 	gc.drawLine(13*width/16, height/6, 13*width/16, 5*height/6);
110 	gc.setLineStyle(SWT.LINE_SOLID);
111 
112 	// draw labels
113 	Font font = new Font(device, getPlatformFont(), 20, SWT.NORMAL);
114 	gc.setFont(font);
115 
116 	String text = GraphicsExample.getResourceString("Flat"); //$NON-NLS-1$
117 	Point size = gc.stringExtent(text);
118 	gc.drawString(text, (width-size.x)/2, 3*height/12, true);
119 	text = GraphicsExample.getResourceString("Square"); //$NON-NLS-1$
120 	size = gc.stringExtent(text);
121 	gc.drawString(text, (width-size.x)/2, 5*height/12, true);
122 	text = GraphicsExample.getResourceString("Round"); //$NON-NLS-1$
123 	size = gc.stringExtent(text);
124 	gc.drawString(text, (width-size.x)/2, 7*height/12, true);
125 	font.dispose();
126 
127 	Pattern pattern = null;
128 	if (foreground.getBgColor1() != null) {
129 		gc.setForeground(foreground.getBgColor1());
130 	} else if (foreground.getBgImage() != null) {
131 		pattern = new Pattern(device, foreground.getBgImage());
132 		gc.setForegroundPattern(pattern);
133 	}
134 
135 	// draw lines with caps
136 	gc.setLineWidth(20);
137 	gc.setLineCap(SWT.CAP_FLAT);
138 	gc.drawLine(3*width/16, 2*height/6, 13*width/16, 2*height/6);
139 	gc.setLineCap(SWT.CAP_SQUARE);
140 	gc.drawLine(3*width/16, 3*height/6, 13*width/16, 3*height/6);
141 	gc.setLineCap(SWT.CAP_ROUND);
142 	gc.drawLine(3*width/16, 4*height/6, 13*width/16, 4*height/6);
143 
144 	if (pattern != null) pattern.dispose();
145 }
146 
147 /**
148  * Returns the name of a valid font for the resident platform.
149  */
getPlatformFont()150 static String getPlatformFont() {
151 	if(SWT.getPlatform() == "win32") {
152 		return "Arial";
153 	} else if (SWT.getPlatform() == "gtk") {
154 		return "Baekmuk Batang";
155 	} else {
156 		return "Verdana";
157 	}
158 }
159 }
160