Skip to content

Commit e8aadbb

Browse files
ShahzaibIbrahimHeikoKlare
authored andcommitted
[Win32] Refactor the Cursor constructors
Extracting out the OS handle creation logic into separate methods. Intention is to make them reusable if someone want to create only a OS handle within a Cursor class.
1 parent 4ee25eb commit e8aadbb

File tree

1 file changed

+28
-13
lines changed
  • bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/graphics

1 file changed

+28
-13
lines changed

bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/graphics/Cursor.java

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,11 @@ public final class Cursor extends Resource {
135135
*/
136136
public Cursor(Device device, int style) {
137137
this(device);
138+
this.handle = setupCursorFromStyle(style);
139+
init();
140+
}
141+
142+
private static long setupCursorFromStyle(int style) {
138143
long lpCursorName = 0;
139144
switch (style) {
140145
case SWT.CURSOR_HAND: lpCursorName = OS.IDC_HAND; break;
@@ -162,9 +167,9 @@ public Cursor(Device device, int style) {
162167
default:
163168
SWT.error(SWT.ERROR_INVALID_ARGUMENT);
164169
}
165-
handle = OS.LoadCursor(0, lpCursorName);
170+
long handle = OS.LoadCursor(0, lpCursorName);
166171
if (handle == 0) SWT.error(SWT.ERROR_NO_HANDLES);
167-
init();
172+
return handle;
168173
}
169174

170175
/**
@@ -207,6 +212,12 @@ public Cursor(Device device, ImageData source, ImageData mask, int hotspotX, int
207212
this.hotspotX = hotspotX;
208213
this.hotspotY = hotspotY;
209214
this.imageDataProvider = null;
215+
this.handle = setupCursorFromImageData(source, mask, hotspotX, hotspotY);
216+
init();
217+
this.device.registerResourceWithZoomSupport(this);
218+
}
219+
220+
private static long setupCursorFromImageData(ImageData source, ImageData mask, int hotspotX, int hotspotY) {
210221
if (source == null) SWT.error(SWT.ERROR_NULL_ARGUMENT);
211222
if (mask == null) {
212223
if (source.getTransparencyType() != SWT.TRANSPARENCY_MASK) {
@@ -233,10 +244,9 @@ public Cursor(Device device, ImageData source, ImageData mask, int hotspotX, int
233244

234245
/* Create the cursor */
235246
long hInst = OS.GetModuleHandle(null);
236-
handle = OS.CreateCursor(hInst, hotspotX, hotspotY, source.width, source.height, sourceData, maskData);
247+
long handle = OS.CreateCursor(hInst, hotspotX, hotspotY, source.width, source.height, sourceData, maskData);
237248
if (handle == 0) SWT.error(SWT.ERROR_NO_HANDLES);
238-
init();
239-
this.device.registerResourceWithZoomSupport(this);
249+
return handle;
240250
}
241251

242252
/**
@@ -275,10 +285,13 @@ public Cursor(Device device, ImageData source, int hotspotX, int hotspotY) {
275285
this.hotspotX = hotspotX;
276286
this.hotspotY = hotspotY;
277287
this.imageDataProvider = null;
278-
setupCursorFromImageData(source);
288+
this.handle = setupCursorFromImageData(device, source, hotspotX, hotspotY);
289+
isIcon = true;
290+
init();
291+
this.device.registerResourceWithZoomSupport(this);
279292
}
280293

281-
private void setupCursorFromImageData(ImageData source) {
294+
private static long setupCursorFromImageData(Device device, ImageData source, int hotspotX, int hotspotY) {
282295
if (source == null) SWT.error(SWT.ERROR_NULL_ARGUMENT);
283296
/* Check the hotspots */
284297
if (hotspotX >= source.width || hotspotX < 0 ||
@@ -333,7 +346,7 @@ private void setupCursorFromImageData(ImageData source) {
333346
if (hMask == 0) SWT.error(SWT.ERROR_NO_HANDLES);
334347
} else {
335348
ImageData mask = source.getTransparencyMask();
336-
long [] result = Image.initIcon(this.device, source, mask);
349+
long [] result = Image.initIcon(device, source, mask);
337350
hBitmap = result[0];
338351
hMask = result[1];
339352
}
@@ -344,13 +357,12 @@ private void setupCursorFromImageData(ImageData source) {
344357
info.hbmMask = hMask;
345358
info.xHotspot = hotspotX;
346359
info.yHotspot = hotspotY;
347-
handle = OS.CreateIconIndirect(info);
360+
long handle = OS.CreateIconIndirect(info);
348361
OS.DeleteObject(hBitmap);
349362
OS.DeleteObject(hMask);
350363
if (handle == 0) SWT.error(SWT.ERROR_NO_HANDLES);
351-
isIcon = true;
352-
init();
353-
this.device.registerResourceWithZoomSupport(this);
364+
365+
return handle;
354366
}
355367

356368
/**
@@ -390,7 +402,10 @@ public Cursor(Device device, ImageDataProvider imageDataProvider, int hotspotX,
390402
this.mask = null;
391403
this.hotspotX = hotspotX;
392404
this.hotspotY = hotspotY;
393-
setupCursorFromImageData(this.source);
405+
this.handle = setupCursorFromImageData(device, this.source, hotspotX, hotspotY);
406+
isIcon = true;
407+
init();
408+
this.device.registerResourceWithZoomSupport(this);
394409
}
395410

396411
/**

0 commit comments

Comments
 (0)