1 package org.eclipse.swt.snippets; 2 3 /******************************************************************************* 4 * Copyright (c) 2018 IBM Corporation and others. 5 * 6 * This program and the accompanying materials 7 * are made available under the terms of the Eclipse Public License 2.0 8 * which accompanies this distribution, and is available at 9 * https://www.eclipse.org/legal/epl-2.0/ 10 * 11 * SPDX-License-Identifier: EPL-2.0 12 * 13 * Contributors: 14 * IBM Corporation - initial API and implementation 15 *******************************************************************************/ 16 17 import org.eclipse.swt.*; 18 import org.eclipse.swt.custom.*; 19 import org.eclipse.swt.events.*; 20 /* 21 * Snippet to display/refresh Monitor DPI dynamically if supported by OS. 22 * 23 * For a list of all SWT example snippets see 24 * http://www.eclipse.org/swt/snippets/ 25 * 26 * @since 3.0 27 */ 28 import org.eclipse.swt.graphics.*; 29 import org.eclipse.swt.internal.*; 30 import org.eclipse.swt.layout.*; 31 import org.eclipse.swt.widgets.*; 32 33 /** 34 * Dynamic DPI with restart example snippet Display current monitors zoom 35 * 36 * For a list of all SWT example snippets see 37 * http://www.eclipse.org/swt/snippets/ 38 */ 39 public class Snippet373 { 40 private static final String IMAGE_100 = "eclipse16.png"; 41 private static final String IMAGE_150 = "eclipse24.png"; 42 private static final String IMAGE_200 = "eclipse32.png"; 43 44 private static final String IMAGES_ROOT = "bin/org/eclipse/swt/snippets/"; 45 46 private static final String IMAGE_PATH_100 = IMAGES_ROOT + IMAGE_100; 47 private static final String IMAGE_PATH_150 = IMAGES_ROOT + IMAGE_150; 48 private static final String IMAGE_PATH_200 = IMAGES_ROOT + IMAGE_200; 49 static final ImageFileNameProvider filenameProvider = zoom -> { 50 String path = null; 51 switch (zoom) { 52 case 150: 53 path = IMAGE_PATH_150; 54 break; 55 case 200: 56 path = IMAGE_PATH_200; 57 break; 58 default: 59 path = IMAGE_PATH_100; 60 } 61 return path; 62 }; 63 64 @SuppressWarnings("restriction") main(String[] args)65 public static void main(String[] args) { 66 System.setProperty("swt.autoScale", "quarter"); 67 Display display = new Display(); 68 final Image eclipse = new Image(display, filenameProvider); 69 final Image eclipseToolBar1 = new Image(display, filenameProvider); 70 final Image eclipseToolBar2 = new Image(display, filenameProvider); 71 final Image eclipseTableHeader = new Image(display, filenameProvider); 72 final Image eclipseTableItem = new Image(display, filenameProvider); 73 final Image eclipseTree1 = new Image(display, filenameProvider); 74 final Image eclipseTree2 = new Image(display, filenameProvider); 75 final Image eclipseCTab1 = new Image(display, filenameProvider); 76 final Image eclipseCTab2 = new Image(display, filenameProvider); 77 78 Shell shell = new Shell(display); 79 shell.setText("Snippet 373"); 80 shell.setImage(eclipse); 81 shell.setText("DynamicDPI @ " + DPIUtil.getDeviceZoom()); 82 shell.setLayout(new RowLayout(SWT.VERTICAL)); 83 shell.setLocation(100, 100); 84 shell.setSize(500, 600); 85 shell.addListener(SWT.ZoomChanged, new Listener() { 86 @Override 87 public void handleEvent(Event e) { 88 if (display.getPrimaryMonitor().equals(shell.getMonitor())) { 89 MessageBox box = new MessageBox(shell, SWT.PRIMARY_MODAL | SWT.OK | SWT.CANCEL); 90 box.setText(shell.getText()); 91 box.setMessage("DPI changed, do you want to exit & restart ?"); 92 e.doit = (box.open() == SWT.OK); 93 if (e.doit) { 94 shell.close(); 95 System.out.println("Program exit."); 96 } 97 } 98 } 99 }); 100 101 // Menu 102 Menu bar = new Menu(shell, SWT.BAR); 103 shell.setMenuBar(bar); 104 MenuItem fileItem = new MenuItem(bar, SWT.CASCADE); 105 fileItem.setText("&File"); 106 fileItem.setImage(eclipse); 107 Menu submenu = new Menu(shell, SWT.DROP_DOWN); 108 fileItem.setMenu(submenu); 109 MenuItem subItem = new MenuItem(submenu, SWT.PUSH); 110 subItem.addListener(SWT.Selection, e -> System.out.println("Select All")); 111 subItem.setText("Select &All\tCtrl+A"); 112 subItem.setAccelerator(SWT.MOD1 + 'A'); 113 subItem.setImage(eclipse); 114 115 // CTabFolder 116 CTabFolder folder = new CTabFolder(shell, SWT.BORDER); 117 for (int i = 0; i < 2; i++) { 118 CTabItem cTabItem = new CTabItem(folder, i % 2 == 0 ? SWT.CLOSE : SWT.NONE); 119 cTabItem.setText("Item " + i); 120 Text textMsg = new Text(folder, SWT.MULTI); 121 textMsg.setText("Content for Item " + i); 122 cTabItem.setControl(textMsg); 123 cTabItem.setImage((i % 2 == 1) ? eclipseCTab1 : eclipseCTab2); 124 } 125 126 // PerMonitorV2 setting 127 // Label label = new Label (shell, SWT.BORDER); 128 // label.setText("PerMonitorV2 value before:after:Error"); 129 // Text text = new Text(shell, SWT.BORDER); 130 // text.setText(DPIUtil.BEFORE + ":" + DPIUtil.AFTER + ":" + DPIUtil.RESULT); 131 132 // Composite for Label, Button, Tool-bar 133 Composite composite = new Composite(shell, SWT.BORDER); 134 composite.setLayout(new RowLayout(SWT.HORIZONTAL)); 135 136 // Label with Image 137 Label label1 = new Label(composite, SWT.BORDER); 138 label1.setImage(eclipse); 139 140 // Label with text only 141 Label label2 = new Label(composite, SWT.BORDER); 142 label2.setText("No Image"); 143 144 // Button with text + Old Image Constructor 145 Button oldButton1 = new Button(composite, SWT.PUSH); 146 oldButton1.setText("Old Img"); 147 oldButton1.setImage(new Image(display, IMAGE_PATH_100)); 148 149 // Button with Old Image Constructor 150 // Button oldButton2 = new Button(composite, SWT.PUSH); 151 // oldButton2.setImage(new Image(display, filenameProvider.getImagePath(100))); 152 153 // Button with Image 154 Button createDialog = new Button(composite, SWT.PUSH); 155 createDialog.setText("Child Dialog"); 156 createDialog.setImage(eclipse); 157 createDialog.addSelectionListener(new SelectionAdapter() { 158 159 @Override 160 public void widgetSelected(SelectionEvent event) { 161 final Shell dialog = new Shell(shell, SWT.DIALOG_TRIM | SWT.RESIZE); 162 dialog.setText("Child Dialog"); 163 RowLayout rowLayout = new RowLayout(SWT.VERTICAL); 164 dialog.setLayout(rowLayout); 165 Label label = new Label(dialog, SWT.BORDER); 166 label.setImage(eclipse); 167 Point location = shell.getLocation(); 168 dialog.setLocation(location.x + 250, location.y + 50); 169 dialog.pack(); 170 dialog.open(); 171 } 172 }); 173 174 // Toolbar with Image 175 ToolBar toolBar = new ToolBar(composite, SWT.FLAT | SWT.BORDER); 176 Rectangle clientArea = shell.getClientArea(); 177 toolBar.setLocation(clientArea.x, clientArea.y); 178 for (int i = 0; i < 2; i++) { 179 int style = i % 2 == 1 ? SWT.DROP_DOWN : SWT.PUSH; 180 ToolItem toolItem = new ToolItem(toolBar, style); 181 toolItem.setImage((i % 2 == 0) ? eclipseToolBar1 : eclipseToolBar2); 182 toolItem.setEnabled(i % 2 == 0); 183 } 184 toolBar.pack(); 185 186 Button button = new Button(shell, SWT.PUSH); 187 button.setText("Refresh-Current Monitor : Zoom"); 188 Text text1 = new Text(shell, SWT.BORDER); 189 Monitor monitor = button.getMonitor(); 190 text1.setText("" + monitor.getZoom()); 191 button.addMouseListener(new MouseAdapter() { 192 @Override 193 public void mouseDown(MouseEvent e) { 194 Monitor monitor = button.getMonitor(); 195 text1.setText("" + monitor.getZoom()); 196 } 197 }); 198 Button button2 = new Button(shell, SWT.PUSH); 199 button2.setText("Refresh-Both Monitors : Zoom"); 200 Text text2 = new Text(shell, SWT.BORDER); 201 Monitor[] monitors = display.getMonitors(); 202 StringBuilder text2String = new StringBuilder(); 203 for (int i = 0; i < monitors.length; i++) { 204 text2String.append(monitors[i].getZoom() + (i < (monitors.length - 1) ? " - " : "")); 205 } 206 text2.setText(text2String.toString()); 207 button2.addMouseListener(new MouseAdapter() { 208 @Override 209 public void mouseDown(MouseEvent e) { 210 Monitor[] monitors = display.getMonitors(); 211 StringBuilder text2String = new StringBuilder(); 212 for (int i = 0; i < monitors.length; i++) { 213 text2String.append(monitors[i].getZoom() + (i < (monitors.length - 1) ? " - " : "")); 214 } 215 text2.setText(text2String.toString()); 216 } 217 }); 218 219 // Table 220 Table table = new Table(shell, SWT.MULTI | SWT.BORDER | SWT.FULL_SELECTION); 221 table.setLinesVisible(true); 222 table.setHeaderVisible(true); 223 String titles[] = { "Title 1" }; 224 for (String title : titles) { 225 TableColumn column = new TableColumn(table, SWT.NONE); 226 column.setText(title); 227 column.setImage(eclipseTableHeader); 228 } 229 for (int i = 0; i < 1; i++) { 230 TableItem item = new TableItem(table, SWT.NONE); 231 item.setText(0, "Data " + i); 232 item.setImage(0, eclipseTableItem); 233 } 234 for (int i = 0; i < titles.length; i++) { 235 table.getColumn(i).pack(); 236 } 237 238 // Tree 239 final Tree tree = new Tree(shell, SWT.BORDER); 240 for (int i = 0; i < 1; i++) { 241 TreeItem iItem = new TreeItem(tree, 0); 242 iItem.setText("TreeItem (0) -" + i); 243 iItem.setImage(eclipseTree1); 244 TreeItem jItem = null; 245 for (int j = 0; j < 1; j++) { 246 jItem = new TreeItem(iItem, 0); 247 jItem.setText("TreeItem (1) -" + j); 248 jItem.setImage(eclipseTree2); 249 jItem.setExpanded(true); 250 } 251 tree.select(jItem); 252 } 253 254 // Shell Location 255 Monitor primary = display.getPrimaryMonitor(); 256 Rectangle bounds = primary.getBounds(); 257 Rectangle rect = shell.getBounds(); 258 int x = bounds.x + (bounds.width - rect.width) / 2; 259 int y = bounds.y + (bounds.height - rect.height) / 2; 260 shell.setLocation(x, y); 261 shell.open(); 262 while (!shell.isDisposed()) { 263 if (!display.readAndDispatch()) 264 display.sleep(); 265 } 266 display.dispose(); 267 } 268 } 269