Skip to content

Commit 99ed41b

Browse files
Refactoring 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 778d04b commit 99ed41b

File tree

1 file changed

+117
-102
lines changed
  • bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/graphics

1 file changed

+117
-102
lines changed

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

Lines changed: 117 additions & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -135,35 +135,7 @@ public final class Cursor extends Resource {
135135
*/
136136
public Cursor(Device device, int style) {
137137
this(device);
138-
long lpCursorName = 0;
139-
switch (style) {
140-
case SWT.CURSOR_HAND: lpCursorName = OS.IDC_HAND; break;
141-
case SWT.CURSOR_ARROW: lpCursorName = OS.IDC_ARROW; break;
142-
case SWT.CURSOR_WAIT: lpCursorName = OS.IDC_WAIT; break;
143-
case SWT.CURSOR_CROSS: lpCursorName = OS.IDC_CROSS; break;
144-
case SWT.CURSOR_APPSTARTING: lpCursorName = OS.IDC_APPSTARTING; break;
145-
case SWT.CURSOR_HELP: lpCursorName = OS.IDC_HELP; break;
146-
case SWT.CURSOR_SIZEALL: lpCursorName = OS.IDC_SIZEALL; break;
147-
case SWT.CURSOR_SIZENESW: lpCursorName = OS.IDC_SIZENESW; break;
148-
case SWT.CURSOR_SIZENS: lpCursorName = OS.IDC_SIZENS; break;
149-
case SWT.CURSOR_SIZENWSE: lpCursorName = OS.IDC_SIZENWSE; break;
150-
case SWT.CURSOR_SIZEWE: lpCursorName = OS.IDC_SIZEWE; break;
151-
case SWT.CURSOR_SIZEN: lpCursorName = OS.IDC_SIZENS; break;
152-
case SWT.CURSOR_SIZES: lpCursorName = OS.IDC_SIZENS; break;
153-
case SWT.CURSOR_SIZEE: lpCursorName = OS.IDC_SIZEWE; break;
154-
case SWT.CURSOR_SIZEW: lpCursorName = OS.IDC_SIZEWE; break;
155-
case SWT.CURSOR_SIZENE: lpCursorName = OS.IDC_SIZENESW; break;
156-
case SWT.CURSOR_SIZESE: lpCursorName = OS.IDC_SIZENWSE; break;
157-
case SWT.CURSOR_SIZESW: lpCursorName = OS.IDC_SIZENESW; break;
158-
case SWT.CURSOR_SIZENW: lpCursorName = OS.IDC_SIZENWSE; break;
159-
case SWT.CURSOR_UPARROW: lpCursorName = OS.IDC_UPARROW; break;
160-
case SWT.CURSOR_IBEAM: lpCursorName = OS.IDC_IBEAM; break;
161-
case SWT.CURSOR_NO: lpCursorName = OS.IDC_NO; break;
162-
default:
163-
SWT.error(SWT.ERROR_INVALID_ARGUMENT);
164-
}
165-
handle = OS.LoadCursor(0, lpCursorName);
166-
if (handle == 0) SWT.error(SWT.ERROR_NO_HANDLES);
138+
this.handle = setupCursorFromStyle(style);
167139
init();
168140
}
169141

@@ -207,34 +179,7 @@ public Cursor(Device device, ImageData source, ImageData mask, int hotspotX, int
207179
this.hotspotX = hotspotX;
208180
this.hotspotY = hotspotY;
209181
this.imageDataProvider = null;
210-
if (source == null) SWT.error(SWT.ERROR_NULL_ARGUMENT);
211-
if (mask == null) {
212-
if (source.getTransparencyType() != SWT.TRANSPARENCY_MASK) {
213-
SWT.error(SWT.ERROR_NULL_ARGUMENT);
214-
}
215-
mask = source.getTransparencyMask();
216-
}
217-
/* Check the bounds. Mask must be the same size as source */
218-
if (mask.width != source.width || mask.height != source.height) {
219-
SWT.error(SWT.ERROR_INVALID_ARGUMENT);
220-
}
221-
/* Check the hotspots */
222-
if (hotspotX >= source.width || hotspotX < 0 ||
223-
hotspotY >= source.height || hotspotY < 0) {
224-
SWT.error(SWT.ERROR_INVALID_ARGUMENT);
225-
}
226-
/* Convert depth to 1 */
227-
mask = ImageData.convertMask(mask);
228-
source = ImageData.convertMask(source);
229-
230-
/* Make sure source and mask scanline pad is 2 */
231-
byte[] sourceData = ImageData.convertPad(source.data, source.width, source.height, source.depth, source.scanlinePad, 2);
232-
byte[] maskData = ImageData.convertPad(mask.data, mask.width, mask.height, mask.depth, mask.scanlinePad, 2);
233-
234-
/* Create the cursor */
235-
long hInst = OS.GetModuleHandle(null);
236-
handle = OS.CreateCursor(hInst, hotspotX, hotspotY, source.width, source.height, sourceData, maskData);
237-
if (handle == 0) SWT.error(SWT.ERROR_NO_HANDLES);
182+
this.handle = setupCursorFromImageData(source, mask, hotspotX, hotspotY);
238183
init();
239184
this.device.registerResourceWithZoomSupport(this);
240185
}
@@ -275,10 +220,89 @@ public Cursor(Device device, ImageData source, int hotspotX, int hotspotY) {
275220
this.hotspotX = hotspotX;
276221
this.hotspotY = hotspotY;
277222
this.imageDataProvider = null;
278-
setupCursorFromImageData(source);
223+
this.handle = setupCursorFromImageData(device, source, hotspotX, hotspotY);
224+
isIcon = true;
225+
init();
226+
this.device.registerResourceWithZoomSupport(this);
227+
}
228+
229+
/**
230+
* Constructs a new cursor given a device, image describing
231+
* the desired cursor appearance, and the x and y coordinates of
232+
* the <em>hotspot</em> (that is, the point within the area
233+
* covered by the cursor which is considered to be where the
234+
* on-screen pointer is "pointing").
235+
* <p>
236+
* You must dispose the cursor when it is no longer required.
237+
* </p>
238+
*
239+
* @param device the device on which to allocate the cursor
240+
* @param imageDataProvider the ImageDataProvider for the cursor
241+
* @param hotspotX the x coordinate of the cursor's hotspot
242+
* @param hotspotY the y coordinate of the cursor's hotspot
243+
*
244+
* @exception IllegalArgumentException <ul>
245+
* <li>ERROR_NULL_ARGUMENT - if device is null and there is no current device</li>
246+
* <li>ERROR_NULL_ARGUMENT - if the image is null</li>
247+
* <li>ERROR_INVALID_ARGUMENT - if the hotspot is outside the bounds of the
248+
* image</li>
249+
* </ul>
250+
* @exception SWTError <ul>
251+
* <li>ERROR_NO_HANDLES - if a handle could not be obtained for cursor creation</li>
252+
* </ul>
253+
*
254+
* @see #dispose()
255+
*
256+
* @since 3.131
257+
*/
258+
public Cursor(Device device, ImageDataProvider imageDataProvider, int hotspotX, int hotspotY) {
259+
super(device);
260+
if (imageDataProvider == null) SWT.error(SWT.ERROR_NULL_ARGUMENT);
261+
this.imageDataProvider = imageDataProvider;
262+
this.source = imageDataProvider.getImageData(100);
263+
this.mask = null;
264+
this.hotspotX = hotspotX;
265+
this.hotspotY = hotspotY;
266+
this.handle = setupCursorFromImageData(device, this.source, hotspotX, hotspotY);
267+
isIcon = true;
268+
init();
269+
this.device.registerResourceWithZoomSupport(this);
270+
}
271+
272+
private static long setupCursorFromStyle(int style) {
273+
long lpCursorName = 0;
274+
switch (style) {
275+
case SWT.CURSOR_HAND: lpCursorName = OS.IDC_HAND; break;
276+
case SWT.CURSOR_ARROW: lpCursorName = OS.IDC_ARROW; break;
277+
case SWT.CURSOR_WAIT: lpCursorName = OS.IDC_WAIT; break;
278+
case SWT.CURSOR_CROSS: lpCursorName = OS.IDC_CROSS; break;
279+
case SWT.CURSOR_APPSTARTING: lpCursorName = OS.IDC_APPSTARTING; break;
280+
case SWT.CURSOR_HELP: lpCursorName = OS.IDC_HELP; break;
281+
case SWT.CURSOR_SIZEALL: lpCursorName = OS.IDC_SIZEALL; break;
282+
case SWT.CURSOR_SIZENESW: lpCursorName = OS.IDC_SIZENESW; break;
283+
case SWT.CURSOR_SIZENS: lpCursorName = OS.IDC_SIZENS; break;
284+
case SWT.CURSOR_SIZENWSE: lpCursorName = OS.IDC_SIZENWSE; break;
285+
case SWT.CURSOR_SIZEWE: lpCursorName = OS.IDC_SIZEWE; break;
286+
case SWT.CURSOR_SIZEN: lpCursorName = OS.IDC_SIZENS; break;
287+
case SWT.CURSOR_SIZES: lpCursorName = OS.IDC_SIZENS; break;
288+
case SWT.CURSOR_SIZEE: lpCursorName = OS.IDC_SIZEWE; break;
289+
case SWT.CURSOR_SIZEW: lpCursorName = OS.IDC_SIZEWE; break;
290+
case SWT.CURSOR_SIZENE: lpCursorName = OS.IDC_SIZENESW; break;
291+
case SWT.CURSOR_SIZESE: lpCursorName = OS.IDC_SIZENWSE; break;
292+
case SWT.CURSOR_SIZESW: lpCursorName = OS.IDC_SIZENESW; break;
293+
case SWT.CURSOR_SIZENW: lpCursorName = OS.IDC_SIZENWSE; break;
294+
case SWT.CURSOR_UPARROW: lpCursorName = OS.IDC_UPARROW; break;
295+
case SWT.CURSOR_IBEAM: lpCursorName = OS.IDC_IBEAM; break;
296+
case SWT.CURSOR_NO: lpCursorName = OS.IDC_NO; break;
297+
default:
298+
SWT.error(SWT.ERROR_INVALID_ARGUMENT);
299+
}
300+
long handle = OS.LoadCursor(0, lpCursorName);
301+
if (handle == 0) SWT.error(SWT.ERROR_NO_HANDLES);
302+
return handle;
279303
}
280304

281-
private void setupCursorFromImageData(ImageData source) {
305+
private static long setupCursorFromImageData(Device device, ImageData source, int hotspotX, int hotspotY) {
282306
if (source == null) SWT.error(SWT.ERROR_NULL_ARGUMENT);
283307
/* Check the hotspots */
284308
if (hotspotX >= source.width || hotspotX < 0 ||
@@ -333,7 +357,7 @@ private void setupCursorFromImageData(ImageData source) {
333357
if (hMask == 0) SWT.error(SWT.ERROR_NO_HANDLES);
334358
} else {
335359
ImageData mask = source.getTransparencyMask();
336-
long [] result = Image.initIcon(this.device, source, mask);
360+
long [] result = Image.initIcon(device, source, mask);
337361
hBitmap = result[0];
338362
hMask = result[1];
339363
}
@@ -344,53 +368,44 @@ private void setupCursorFromImageData(ImageData source) {
344368
info.hbmMask = hMask;
345369
info.xHotspot = hotspotX;
346370
info.yHotspot = hotspotY;
347-
handle = OS.CreateIconIndirect(info);
371+
long handle = OS.CreateIconIndirect(info);
348372
OS.DeleteObject(hBitmap);
349373
OS.DeleteObject(hMask);
350374
if (handle == 0) SWT.error(SWT.ERROR_NO_HANDLES);
351-
isIcon = true;
352-
init();
353-
this.device.registerResourceWithZoomSupport(this);
375+
376+
return handle;
354377
}
355378

356-
/**
357-
* Constructs a new cursor given a device, image describing
358-
* the desired cursor appearance, and the x and y coordinates of
359-
* the <em>hotspot</em> (that is, the point within the area
360-
* covered by the cursor which is considered to be where the
361-
* on-screen pointer is "pointing").
362-
* <p>
363-
* You must dispose the cursor when it is no longer required.
364-
* </p>
365-
*
366-
* @param device the device on which to allocate the cursor
367-
* @param imageDataProvider the ImageDataProvider for the cursor
368-
* @param hotspotX the x coordinate of the cursor's hotspot
369-
* @param hotspotY the y coordinate of the cursor's hotspot
370-
*
371-
* @exception IllegalArgumentException <ul>
372-
* <li>ERROR_NULL_ARGUMENT - if device is null and there is no current device</li>
373-
* <li>ERROR_NULL_ARGUMENT - if the image is null</li>
374-
* <li>ERROR_INVALID_ARGUMENT - if the hotspot is outside the bounds of the
375-
* image</li>
376-
* </ul>
377-
* @exception SWTError <ul>
378-
* <li>ERROR_NO_HANDLES - if a handle could not be obtained for cursor creation</li>
379-
* </ul>
380-
*
381-
* @see #dispose()
382-
*
383-
* @since 3.131
384-
*/
385-
public Cursor(Device device, ImageDataProvider imageDataProvider, int hotspotX, int hotspotY) {
386-
super(device);
387-
if (imageDataProvider == null) SWT.error(SWT.ERROR_NULL_ARGUMENT);
388-
this.imageDataProvider = imageDataProvider;
389-
this.source = imageDataProvider.getImageData(100);
390-
this.mask = null;
391-
this.hotspotX = hotspotX;
392-
this.hotspotY = hotspotY;
393-
setupCursorFromImageData(this.source);
379+
private static long setupCursorFromImageData(ImageData source, ImageData mask, int hotspotX, int hotspotY) {
380+
if (source == null) SWT.error(SWT.ERROR_NULL_ARGUMENT);
381+
if (mask == null) {
382+
if (source.getTransparencyType() != SWT.TRANSPARENCY_MASK) {
383+
SWT.error(SWT.ERROR_NULL_ARGUMENT);
384+
}
385+
mask = source.getTransparencyMask();
386+
}
387+
/* Check the bounds. Mask must be the same size as source */
388+
if (mask.width != source.width || mask.height != source.height) {
389+
SWT.error(SWT.ERROR_INVALID_ARGUMENT);
390+
}
391+
/* Check the hotspots */
392+
if (hotspotX >= source.width || hotspotX < 0 ||
393+
hotspotY >= source.height || hotspotY < 0) {
394+
SWT.error(SWT.ERROR_INVALID_ARGUMENT);
395+
}
396+
/* Convert depth to 1 */
397+
mask = ImageData.convertMask(mask);
398+
source = ImageData.convertMask(source);
399+
400+
/* Make sure source and mask scanline pad is 2 */
401+
byte[] sourceData = ImageData.convertPad(source.data, source.width, source.height, source.depth, source.scanlinePad, 2);
402+
byte[] maskData = ImageData.convertPad(mask.data, mask.width, mask.height, mask.depth, mask.scanlinePad, 2);
403+
404+
/* Create the cursor */
405+
long hInst = OS.GetModuleHandle(null);
406+
long handle = OS.CreateCursor(hInst, hotspotX, hotspotY, source.width, source.height, sourceData, maskData);
407+
if (handle == 0) SWT.error(SWT.ERROR_NO_HANDLES);
408+
return handle;
394409
}
395410

396411
/**

0 commit comments

Comments
 (0)