Skip to content

Commit c1b8100

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 c6a29a9 commit c1b8100

File tree

3 files changed

+233
-0
lines changed

3 files changed

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

0 commit comments

Comments
 (0)