Skip to content

Commit bcd7122

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 6e4241d commit bcd7122

File tree

3 files changed

+228
-0
lines changed

3 files changed

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

0 commit comments

Comments
 (0)