Skip to content

Commit 5e7f5e7

Browse files
Add Snippet 386: Demonstrates Cursor constructors and DPI scaling in SWT
This snippet demonstrates several ways to construct and use cursors in an SWT application, including system cursors, custom image cursors, and DPI-aware cursors using ImageDataProvider. The user interface allows interactive selection of the cursor construction method, and updates the shell’s cursor in real time based on the current choice. The snippet displays the current system zoom level (DPI) and the expected cursor size, and draws reference ticks to help visualize scaling effects. It also responds to system DPI changes, ensuring accurate display and behavior at different zoom levels. The code is structured for clarity and modularity, making it suitable for direct use with the SWT Snippet Explorer.
1 parent 508ad4a commit 5e7f5e7

File tree

3 files changed

+216
-0
lines changed

3 files changed

+216
-0
lines changed

examples/org.eclipse.swt.snippets/Snippets.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ To contribute a new snippet, [create a snippet contribution as a pull request](h
125125
- [create a color cursor from a source and a mask](https://github.com/eclipse-platform/eclipse.platform.swt/tree/master/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet119.java)[(preview)](https://github.com/eclipse-platform/eclipse.platform.swt/blob/master/examples/org.eclipse.swt.snippets/previews/Snippet119.png "Preview for Snippet 119")
126126
- [create a color cursor from an image file](https://github.com/eclipse-platform/eclipse.platform.swt/tree/master/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet118.java)[(preview)](https://github.com/eclipse-platform/eclipse.platform.swt/blob/master/examples/org.eclipse.swt.snippets/previews/Snippet118.png "Preview for Snippet 118")
127127
- [hide the Cursor over a control](https://github.com/eclipse-platform/eclipse.platform.swt/tree/master/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet242.java)[(preview)](https://github.com/eclipse-platform/eclipse.platform.swt/blob/master/examples/org.eclipse.swt.snippets/previews/Snippet242.png "Preview for Snippet 242")
128+
- [demonstrates various ways to construct and use custom cursors in an SWT application](https://github.com/eclipse-platform/eclipse.platform.swt/tree/master/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet386.java)[(preview)](https://github.com/eclipse-platform/eclipse.platform.swt/blob/master/examples/org.eclipse.swt.snippets/previews/Snippet386.png "Preview for Snippet 386")
128129

129130
### **DateTime**
130131
- [create a DateTime calendar and a DateTime time](https://github.com/eclipse-platform/eclipse.platform.swt/tree/master/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet250.java)[(preview)](https://github.com/eclipse-platform/eclipse.platform.swt/blob/master/examples/org.eclipse.swt.snippets/previews/Snippet250.png "Preview for Snippet 250")
16.2 KB
Loading
Lines changed: 215 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,215 @@
1+
/*******************************************************************************
2+
* Copyright (c) 2025 Yatta Solutions
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+
* Yatta Solutions - initial API and implementation
13+
*******************************************************************************/
14+
package org.eclipse.swt.snippets;
15+
16+
import org.eclipse.swt.*;
17+
import org.eclipse.swt.graphics.*;
18+
import org.eclipse.swt.layout.*;
19+
import org.eclipse.swt.widgets.*;
20+
21+
/**
22+
* Demonstrates various ways to construct and use custom cursors in an SWT application.
23+
* <p>
24+
* This example provides a graphical user interface that allows users to select from several
25+
* different {@link org.eclipse.swt.graphics.Cursor} constructors, showing the resulting cursor
26+
* in real time on the application shell. It also dynamically updates to reflect changes in
27+
* system DPI scaling (zoom), displaying the expected cursor size and drawing a set of
28+
* reference ticks to help visualize scaling.
29+
* <p>
30+
* Features of this snippet include:
31+
* <ul>
32+
* <li>Combo box to choose between multiple cursor constructors, including system, custom image, and
33+
* DPI-aware providers.</li>
34+
* <li>Live update of the shell cursor based on the user's selection.</li>
35+
* <li>Display of current system zoom level and expected cursor size for visual reference.</li>
36+
* <li>Painted tick marks and labels to visualize scaling and cursor positioning at different zooms.</li>
37+
* <li>DPI change listener to update UI when system zoom changes.</li>
38+
* </ul>
39+
*
40+
* This snippet is intended for educational and demonstration purposes to aid understanding of
41+
* cursor creation and DPI handling in SWT.
42+
*
43+
* <p>
44+
* For a list of all SWT example snippets see
45+
* http://www.eclipse.org/swt/snippets/
46+
* </p>
47+
*/
48+
public class Snippet386 {
49+
50+
private static final int IMAGE_SIZE_IN_POINTS = 16;
51+
52+
public static void main(String[] args) {
53+
System.setProperty("swt.autoScale.updateOnRuntime", "true");
54+
Display display = new Display();
55+
Shell shell = createShell(display);
56+
57+
Label zoomLabel = createZoomLabel(shell);
58+
Combo combo = createConstructorCombo(shell);
59+
60+
addZoomChangedListener(shell, zoomLabel);
61+
Group section = new Group(shell, SWT.NONE);
62+
section.setText("Scale");
63+
section.setLayoutData(new GridData(SWT.FILL, SWT.TOP, true, true));
64+
section.setLayout(new GridLayout(2, false));
65+
66+
GridData gd = new GridData(SWT.FILL, SWT.TOP, true, false);
67+
gd.heightHint = 100; // fixed height in pixels
68+
section.setLayoutData(gd);
69+
addPaintTicks(section);
70+
71+
CursorManager cursorManager = new CursorManager(display, shell, combo);
72+
combo.addListener(SWT.Selection, e -> cursorManager.updateCursor());
73+
cursorManager.updateCursor();
74+
75+
shell.setSize(400,400);
76+
shell.open();
77+
78+
eventLoop(display, shell);
79+
display.dispose();
80+
}
81+
82+
private static Shell createShell(Display display) {
83+
Shell shell = new Shell(display);
84+
shell.setText("Snippet 386");
85+
shell.setLayout(new GridLayout(1, false));
86+
87+
Label label = new Label(shell, SWT.NONE);
88+
label.setText("Choose Cursor Constructor:");
89+
return shell;
90+
}
91+
92+
private static Combo createConstructorCombo(Composite parent) {
93+
Combo combo = new Combo(parent, SWT.READ_ONLY);
94+
combo.setItems("Cursor(Device, int)", "Cursor(Device, ImageData, ImageData, int, int)",
95+
"Cursor(Device, ImageData, int, int)", "Cursor(Device, ImageDataProvider, int, int)");
96+
combo.select(0);
97+
return combo;
98+
}
99+
100+
private static Label createZoomLabel(Shell parent) {
101+
Label zoomLabel = new Label(parent, SWT.NONE);
102+
zoomLabel.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
103+
setZoomLabelText(parent, zoomLabel);
104+
return zoomLabel;
105+
}
106+
107+
private static void setZoomLabelText(Shell shell, Label label) {
108+
int zoom = shell.getMonitor().getZoom();
109+
int expectedCursorSize = Math.round(IMAGE_SIZE_IN_POINTS * (zoom / 100f));
110+
label.setText("Current zoom: " + zoom + "% \nExpected Cursor Size = " + expectedCursorSize);
111+
}
112+
113+
private static void addZoomChangedListener(Shell shell, Label zoomLabel) {
114+
shell.addListener(SWT.Resize, event -> {
115+
setZoomLabelText(shell, zoomLabel);
116+
shell.layout();
117+
});
118+
}
119+
120+
private static void addPaintTicks(Composite composite) {
121+
composite.addPaintListener(event -> {
122+
drawTicks(composite, event.gc);
123+
});
124+
}
125+
126+
private static void drawTicks(Composite shell, GC gc) {
127+
int deviceZoom = shell.getMonitor().getZoom();
128+
float devScale = deviceZoom / 100f;
129+
Point client = shell.getSize();
130+
int xPos = (int) ((client.x / 2) - (6 * (IMAGE_SIZE_IN_POINTS / devScale)));
131+
int tickHeight = 10;
132+
int yPos = 20;
133+
134+
for (int tickIndex = 0; tickIndex < 6; tickIndex++) {
135+
xPos += (IMAGE_SIZE_IN_POINTS / devScale);
136+
int yOffset = (tickIndex % 3 == 1) ? 20 : (tickIndex % 3 == 2) ? 40 : 0;
137+
138+
gc.drawLine(xPos, yPos, xPos, yPos + tickHeight);
139+
gc.drawText(Integer.toString(tickIndex * IMAGE_SIZE_IN_POINTS), xPos - 5,
140+
yPos + 12 + yOffset);
141+
}
142+
}
143+
144+
private static void eventLoop(Display display, Shell shell) {
145+
while (!shell.isDisposed()) {
146+
if (!display.readAndDispatch())
147+
display.sleep();
148+
}
149+
}
150+
public static ImageData createSolidColorImageData(int size, RGB color) {
151+
PaletteData palette = new PaletteData(0xFF0000, 0x00FF00, 0x0000FF);
152+
ImageData imageData = new ImageData(size, size, 24, palette);
153+
154+
int pixel = palette.getPixel(color);
155+
for (int y = 0; y < size; y++) {
156+
for (int x = 0; x < size; x++) {
157+
imageData.setPixel(x, y, pixel);
158+
}
159+
}
160+
return imageData;
161+
}
162+
163+
private static class CursorManager {
164+
private final Display display;
165+
private final Shell shell;
166+
private final Combo combo;
167+
168+
CursorManager(Display display, Shell shell, Combo combo) {
169+
this.display = display;
170+
this.shell = shell;
171+
this.combo = combo;
172+
}
173+
174+
void updateCursor() {
175+
int selection = combo.getSelectionIndex();
176+
Cursor oldCursor = shell.getCursor();
177+
if (oldCursor != null && !oldCursor.isDisposed()) {
178+
oldCursor.dispose();
179+
}
180+
Cursor cursor = createCursor(selection);
181+
if (cursor != null) {
182+
shell.setCursor(cursor);
183+
}
184+
}
185+
186+
private Cursor createCursor(int selection) {
187+
switch (selection) {
188+
case 0:
189+
return new Cursor(display, SWT.CURSOR_HAND);
190+
case 1: {
191+
RGB blue = new RGB(0, 0, 255);
192+
ImageData source = new ImageData(IMAGE_SIZE_IN_POINTS, IMAGE_SIZE_IN_POINTS, 1,
193+
new PaletteData(new RGB[] { blue }));
194+
ImageData mask = new ImageData(IMAGE_SIZE_IN_POINTS, IMAGE_SIZE_IN_POINTS, 1,
195+
new PaletteData(new RGB[] { blue }));
196+
return new Cursor(display, source, mask, 0, 0);
197+
}
198+
199+
case 2:
200+
RGB red = new RGB(255, 0, 0);
201+
return new Cursor(display, createSolidColorImageData(IMAGE_SIZE_IN_POINTS, red), 0, 0);
202+
203+
case 3: {
204+
RGB green = new RGB(0, 255, 0);
205+
ImageDataProvider provider = zoom -> {
206+
return createSolidColorImageData(IMAGE_SIZE_IN_POINTS * zoom / 100, green);
207+
};
208+
return new Cursor(display, provider, 0, 0);
209+
}
210+
default:
211+
return null;
212+
}
213+
}
214+
}
215+
}

0 commit comments

Comments
 (0)