Mercurial > projects > dwt-mac
changeset 95:52007db1276d
Ported dwt.dnd.DragSource
author | Jacob Carlborg <doob@me.com> |
---|---|
date | Tue, 30 Dec 2008 20:51:17 +0100 |
parents | 70a562fb34e4 |
children | bc175cd0484a |
files | dwt/dnd/DragSource.d dwt/dnd/DragSourceAdapter.d dwt/dnd/DragSourceListener.d dwt/internal/cocoa/OS.d |
diffstat | 4 files changed, 122 insertions(+), 105 deletions(-) [+] |
line wrap: on
line diff
--- a/dwt/dnd/DragSource.d Tue Dec 30 19:12:22 2008 +0100 +++ b/dwt/dnd/DragSource.d Tue Dec 30 20:51:17 2008 +0100 @@ -7,6 +7,9 @@ * * Contributors: * IBM Corporation - initial API and implementation + * + * Port to the D programming language: + * Jacob Carlborg <doob@me.com> *******************************************************************************/ module dwt.dnd.DragSource; @@ -18,7 +21,6 @@ import dwt.DWTException; import dwt.graphics.Image; import dwt.internal.C; -import dwt.internal.Callback; import dwt.internal.cocoa.NSApplication; import dwt.internal.cocoa.NSArray; import dwt.internal.cocoa.NSData; @@ -40,6 +42,15 @@ import dwt.widgets.Table; import dwt.widgets.Tree; import dwt.widgets.Widget; + +import dwt.dnd.DND; +import dwt.dnd.DragSourceEffect; +import dwt.dnd.DragSourceListener; +import dwt.dnd.Transfer; +import dwt.dnd.TreeDragSourceEffect; +import dwt.internal.objc.cocoa.Cocoa; +import objc = dwt.internal.objc.runtime; + /** * * <code>DragSource</code> defines the source object for a drag and drop transfer. @@ -120,45 +131,43 @@ * @see <a href="http://www.eclipse.org/swt/examples.php">DWT Example: DNDExample</a> * @see <a href="http://www.eclipse.org/swt/">Sample code and further information</a> */ -public class DragSource extends Widget { +public class DragSource : Widget { - static Callback dragSource2Args, dragSource3Args, dragSource4Args, dragSource5Args; - static final String DWT_OBJECT = "DWT_OBJECT"; + //static Callback dragSource2Args, dragSource3Args, dragSource4Args, dragSource5Args; + static const String DWT_OBJECT = "DWT_OBJECT"; - static { + static this () { String className = "SWTDragSourceDelegate"; // TODO: These should either move out of Display or be accessible to this class. String types = "*"; - int size = C.PTR_SIZEOF, align = C.PTR_SIZEOF is 4 ? 2 : 3; - - Class clazz = DragSource.class; - - dragSource2Args = new Callback(clazz, "dragSourceProc", 2); - int /*long*/ proc2 = dragSource2Args.getAddress(); - if (proc2 is 0) DWT.error (DWT.ERROR_NO_MORE_CALLBACKS); - - dragSource3Args = new Callback(clazz, "dragSourceProc", 3); - int /*long*/ proc3 = dragSource3Args.getAddress(); - if (proc3 is 0) DWT.error (DWT.ERROR_NO_MORE_CALLBACKS); + int size = C.PTR_SIZEOF, align_ = C.PTR_SIZEOF is 4 ? 2 : 3; - dragSource4Args = new Callback(clazz, "dragSourceProc", 4); - int /*long*/ proc4 = dragSource4Args.getAddress(); - if (proc4 is 0) DWT.error (DWT.ERROR_NO_MORE_CALLBACKS); + objc.IMP proc2 = cast(objc.IMP) &dragSourceProc2; + objc.IMP proc3 = cast(objc.IMP) &dragSourceProc3; + objc.IMP proc4 = cast(objc.IMP) &dragSourceProc4; + objc.IMP proc5 = cast(objc.IMP) &dragSourceProc5; - dragSource5Args = new Callback(clazz, "dragSourceProc", 5); - int /*long*/ proc5 = dragSource5Args.getAddress(); - if (proc5 is 0) DWT.error (DWT.ERROR_NO_MORE_CALLBACKS); + objc.Class cls = OS.objc_allocateClassPair(cast(objc.Class) OS.class_NSObject, className, 0); + OS.class_addIvar(cls, DWT_OBJECT, size, cast(byte)align_, types); - int /*long*/ cls = OS.objc_allocateClassPair(OS.class_NSObject, className, 0); - OS.class_addIvar(cls, DWT_OBJECT, size, (byte)align, types); - - int /*long*/ draggedImage_endedAt_operationProc = OS.draggedImage_endedAt_operation_CALLBACK(proc5); + objc.IMP draggedImage_endedAt_operationProc = OS.draggedImage_endedAt_operation_CALLBACK(proc5); // Add the NSDraggingSource callbacks OS.class_addMethod(cls, OS.sel_draggingSourceOperationMaskForLocal_, proc3, "@:c"); - OS.class_addMethod(cls, OS.sel_draggedImage_beganAt_, proc4, "@:@{NSPoint=ff}"); - OS.class_addMethod(cls, OS.sel_draggedImage_endedAt_operation_, draggedImage_endedAt_operationProc, "@:@{NSPoint=ff}I"); + + static if ((void*).sizeof > int.sizeof) /* 64bit target */ + { + OS.class_addMethod(cls, OS.sel_draggedImage_beganAt_, proc4, "@:@{NSPoint=dd}"); + OS.class_addMethod(cls, OS.sel_draggedImage_endedAt_operation_, draggedImage_endedAt_operationProc, "@:@{NSPoint=dd}Q"); + } + + else + { + OS.class_addMethod(cls, OS.sel_draggedImage_beganAt_, proc4, "@:@{NSPoint=ff}"); + OS.class_addMethod(cls, OS.sel_draggedImage_endedAt_operation_, draggedImage_endedAt_operationProc, "@:@{NSPoint=ff}I"); + } + OS.class_addMethod(cls, OS.sel_ignoreModifierKeysWhileDragging, proc3, "@:"); // Add the NSPasteboard delegate callback @@ -179,7 +188,7 @@ static final String DEFAULT_DRAG_SOURCE_EFFECT = "DEFAULT_DRAG_SOURCE_EFFECT"; //$NON-NLS-1$ bool dragStarted = false; - private int /*long*/ delegateJniRef; + private void* delegateJniRef; /** * Creates a new <code>DragSource</code> to handle dragging from the specified <code>Control</code>. @@ -210,7 +219,7 @@ * @see DND#DROP_MOVE * @see DND#DROP_LINK */ -public DragSource(Control control, int style) { +public this(Control control, int style) { super (control, checkStyle(style)); this.control = control; if (control.getData(DND.DRAG_SOURCE_KEY) !is null) { @@ -218,16 +227,16 @@ } control.setData(DND.DRAG_SOURCE_KEY, this); - controlListener = new Listener () { + controlListener = new class () Listener { public void handleEvent (Event event) { if (event.type is DWT.Dispose) { - if (!DragSource.this.isDisposed()) { - DragSource.this.dispose(); + if (!this.outer.isDisposed()) { + this.outer.dispose(); } } if (event.type is DWT.DragDetect) { - if (!DragSource.this.isDisposed()) { - DragSource.this.drag(event); + if (!this.outer.isDisposed()) { + this.outer.drag(event); } } } @@ -235,24 +244,24 @@ control.addListener (DWT.Dispose, controlListener); control.addListener (DWT.DragDetect, controlListener); - this.addListener(DWT.Dispose, new Listener() { + this.addListener(DWT.Dispose, new class () Listener { public void handleEvent(Event e) { onDispose(); } }); Object effect = control.getData(DEFAULT_DRAG_SOURCE_EFFECT); - if (effect instanceof DragSourceEffect) { - dragEffect = (DragSourceEffect) effect; - } else if (control instanceof Tree) { - dragEffect = new TreeDragSourceEffect((Tree) control); - } else if (control instanceof Table) { - dragEffect = new TableDragSourceEffect((Table) control); + if (cast(DragSourceEffect) effect) { + dragEffect = cast(DragSourceEffect) effect; + } else if (cast(Tree) control) { + dragEffect = new TreeDragSourceEffect(cast(Tree) control); + } else if (cast(Table) control) { + dragEffect = new TableDragSourceEffect(cast(Table) control); } // Create a delegate, but then stuff a pointer back to this object so callbacks will have // access to this object's data. - dragSourceDelegate = (SWTDragSourceDelegate)new SWTDragSourceDelegate().alloc().init(); + dragSourceDelegate = cast(SWTDragSourceDelegate)(new SWTDragSourceDelegate()).alloc().init(); delegateJniRef = OS.NewGlobalRef(this); if (delegateJniRef is 0) DWT.error(DWT.ERROR_NO_HANDLES); OS.object_setInstanceVariable(dragSourceDelegate.id, DWT_OBJECT, delegateJniRef); @@ -298,8 +307,8 @@ } protected void checkSubclass () { - String name = getClass().getName (); - String validName = DragSource.class.getName(); + String name = this.classinfo.name; + String validName = DragSource.classinfo.name; if (!validName.equals(name)) { DND.error (DWT.ERROR_INVALID_SUBCLASS); } @@ -376,7 +385,7 @@ if (!dragStarted) { event = new DNDEvent(); event.widget = this; - event.time = (int)System.currentTimeMillis(); + event.time = cast(int)System.currentTimeMillis(); event.doit = false; event.detail = DND.DROP_NONE; notifyListeners(DND.DragEnd, event); @@ -390,11 +399,11 @@ this.dragStarted = true; } -void draggedImage_endedAt_operation(NSImage image, NSPoint location, int /*long*/ operation) { +void draggedImage_endedAt_operation(NSImage image, NSPoint location, objc.id operation) { int swtOperation = osOpToOp(operation); Event event = new DNDEvent(); event.widget = this; - event.time = (int)System.currentTimeMillis(); + event.time = cast(int)System.currentTimeMillis(); event.doit = swtOperation !is DND.DROP_NONE; event.detail = swtOperation; notifyListeners(DND.DragEnd, event); @@ -407,52 +416,52 @@ // Drag operations are same for local or remote drags. return dragOperations; } - -static int /*long*/ dragSourceProc(int /*long*/ id, int /*long*/ sel) { +extern (C) { +static objc.id dragSourceProc2(objc.id id, objc.SEL sel) { Display display = Display.findDisplay(Thread.currentThread()); - if (display is null || display.isDisposed()) return 0; + if (display is null || display.isDisposed()) return null; Widget widget = display.findWidget(id); - if (widget is null) return 0; - DragSource ds = (DragSource)widget.getData(DND.DRAG_SOURCE_KEY); - if (ds is null) return 0; + if (widget is null) return null; + DragSource ds = cast(DragSource)widget.getData(DND.DRAG_SOURCE_KEY); + if (ds is null) return null; if (sel is OS.sel_ignoreModifierKeysWhileDragging) { - return (ds.ignoreModifierKeysWhileDragging() ? 1 : 0); + return (ds.ignoreModifierKeysWhileDragging() ? cast(objc.id ) 1 : null); } - return 0; + return null; } -static int /*long*/ dragSourceProc(int /*long*/ id, int /*long*/ sel, int /*long*/ arg0) { +static objc.id dragSourceProc3(objc.id id, objc.SEL sel, objc.id arg0) { Display display = Display.findDisplay(Thread.currentThread()); - if (display is null || display.isDisposed()) return 0; + if (display is null || display.isDisposed()) return null; Widget widget = display.findWidget(id); - if (widget is null) return 0; + if (widget is null) return null; DragSource ds = null; - if (widget instanceof DragSource) - ds = (DragSource)widget; + if (cast(DragSource) widget) + ds = cast(DragSource)widget; - if (ds is null) return 0; + if (ds is null) return null; if (sel is OS.sel_draggingSourceOperationMaskForLocal_) { - return ds.draggingSourceOperationMaskForLocal(arg0 is 1); + return ds.draggingSourceOperationMaskForLocal(arg0 is cast(objc.id) 1); } - return 0; + return null; } -static int /*long*/ dragSourceProc(int /*long*/ id, int /*long*/ sel, int /*long*/ arg0, int /*long*/ arg1) { +static objc.id dragSourceProc4(objc.id id, objc.id sel, objc.id arg0, objc.id arg1) { Display display = Display.findDisplay(Thread.currentThread()); - if (display is null || display.isDisposed()) return 0; + if (display is null || display.isDisposed()) return null; Widget widget = display.findWidget(id); - if (widget is null) return 0; + if (widget is null) return null; DragSource ds = null; - if (widget instanceof DragSource) - ds = (DragSource)widget; + if (cast(DragSource) widget) + ds = cast(DragSource)widget; - if (ds is null) return 0; + if (ds is null) return null; if (sel is OS.sel_draggedImage_beganAt_) { NSImage image = new NSImage(arg0); @@ -464,20 +473,20 @@ ds.pasteboard_provideDataForType(pb, type); } - return 0; + return null; } -static int /*long*/ dragSourceProc(int /*long*/ id, int /*long*/ sel, int /*long*/ arg0, int /*long*/ arg1, int /*long*/ arg2) { +static objc.id dragSourceProc5(objc.id id, objc.id sel, objc.id arg0, objc.id arg1, objc.id arg2) { Display display = Display.findDisplay(Thread.currentThread()); - if (display is null || display.isDisposed()) return 0; + if (display is null || display.isDisposed()) return null; Widget widget = display.findWidget(id); - if (widget is null) return 0; + if (widget is null) return null; DragSource ds = null; - if (widget instanceof DragSource) - ds = (DragSource)widget; + if (cast(DragSource) widget) + ds = cast(DragSource)widget; - if (ds is null) return 0; + if (ds is null) return null; if (sel is OS.sel_draggedImage_endedAt_operation_) { NSImage image = new NSImage(arg0); @@ -486,8 +495,8 @@ ds.draggedImage_endedAt_operation(image, point, arg2); } - return 0; -} + return null; +}} /** * Returns the Control which is registered for this DragSource. This is the control that the @@ -521,17 +530,17 @@ */ public DragSourceListener[] getDragListeners() { Listener[] listeners = getListeners(DND.DragStart); - int length = listeners.length; - DragSourceListener[] dragListeners = new DragSourceListener[length]; + int length_ = listeners.length; + DragSourceListener[] dragListeners = new DragSourceListener[length_]; int count = 0; - for (int i = 0; i < length; i++) { + for (int i = 0; i < length_; i++) { Listener listener = listeners[i]; - if (listener instanceof DNDListener) { - dragListeners[count] = (DragSourceListener) ((DNDListener) listener).getEventListener(); + if (cast(DNDListener) listener) { + dragListeners[count] = cast(DragSourceListener) (cast(DNDListener) listener).getEventListener(); count++; } } - if (count is length) return dragListeners; + if (count is length_) return dragListeners; DragSourceListener[] result = new DragSourceListener[count]; System.arraycopy(dragListeners, 0, result, 0, count); return result; @@ -582,7 +591,7 @@ } int opToOsOp(int operation) { - int osOperation = 0; + NSDragOperation osOperation = 0; if ((operation & DND.DROP_COPY) !is 0){ osOperation |= OS.NSDragOperationCopy; } @@ -595,10 +604,10 @@ if ((operation & DND.DROP_TARGET_MOVE) !is 0) { osOperation |= OS.NSDragOperationDelete; } - return osOperation; + return cast(int) osOperation; } -int osOpToOp(int /*long*/ osOperation){ +int osOpToOp(NSDragOperation osOperation){ int operation = 0; if ((osOperation & OS.NSDragOperationCopy) !is 0){ operation |= DND.DROP_COPY; @@ -624,7 +633,7 @@ transferData.type = Transfer.registerType(dataType.getString()); DNDEvent event = new DNDEvent(); event.widget = this; - event.time = (int)System.currentTimeMillis(); + event.time = cast(int)System.currentTimeMillis(); event.dataType = transferData; notifyListeners(DND.DragSetData, event); Transfer transfer = null; @@ -644,14 +653,14 @@ if (dataType.isEqual(OS.NSStringPboardType) || dataType.isEqual(OS.NSHTMLPboardType) || dataType.isEqual(OS.NSRTFPboardType)) { - pasteboard.setString((NSString) tdata, dataType); + pasteboard.setString(cast(NSString) tdata, dataType); } else if (dataType.isEqual(OS.NSURLPboardType)) { - NSURL url = (NSURL) tdata; + NSURL url = cast(NSURL) tdata; url.writeToPasteboard(pasteboard); } else if (dataType.isEqual(OS.NSFilenamesPboardType)) { - pasteboard.setPropertyList((NSArray) tdata, dataType); + pasteboard.setPropertyList(cast(NSArray) tdata, dataType); } else { - pasteboard.setData((NSData) tdata, dataType); + pasteboard.setData(cast(NSData) tdata, dataType); } }
--- a/dwt/dnd/DragSourceAdapter.d Tue Dec 30 19:12:22 2008 +0100 +++ b/dwt/dnd/DragSourceAdapter.d Tue Dec 30 20:51:17 2008 +0100 @@ -7,12 +7,18 @@ * * Contributors: * IBM Corporation - initial API and implementation + * + * Port to the D programming language: + * Jacob Carlborg <doob@me.com> *******************************************************************************/ module dwt.dnd.DragSourceAdapter; import dwt.dwthelper.utils; +import dwt.dnd.DragSourceEvent; +import dwt.dnd.DragSourceListener; + /** * This adapter class provides default implementations for the * methods described by the <code>DragSourceListener</code> interface. @@ -25,7 +31,7 @@ * @see DragSourceEvent * @see <a href="http://www.eclipse.org/swt/">Sample code and further information</a> */ -public class DragSourceAdapter implements DragSourceListener { +public class DragSourceAdapter : DragSourceListener { /** * This implementation of <code>dragStart</code> permits the drag operation to start.
--- a/dwt/dnd/DragSourceListener.d Tue Dec 30 19:12:22 2008 +0100 +++ b/dwt/dnd/DragSourceListener.d Tue Dec 30 20:51:17 2008 +0100 @@ -14,6 +14,8 @@ import dwt.internal.DWTEventListener; +import dwt.dnd.DragSourceEvent; + /** * The <code>DragSourceListener</code> class provides event notification to the application for DragSource events. * @@ -26,7 +28,7 @@ * <b>move</b> operation, the application must remove the data that was transferred.</p> * */ -public interface DragSourceListener extends DWTEventListener { +public interface DragSourceListener : DWTEventListener { /** * The user has begun the actions required to drag the widget. This event gives the application
--- a/dwt/internal/cocoa/OS.d Tue Dec 30 19:12:22 2008 +0100 +++ b/dwt/internal/cocoa/OS.d Tue Dec 30 20:51:17 2008 +0100 @@ -2885,16 +2885,16 @@ public static const int NSDoubleType = 6; public static const wchar NSDownArrowFunctionKey = 63233; public static const int NSDownTextMovement = 22; -public static const int NSDragOperationAll = 15; -public static const int NSDragOperationAll_Obsolete = 15; -public static const int NSDragOperationCopy = 1; -public static const int NSDragOperationDelete = 32; -public static const int NSDragOperationEvery = -1; -public static const int NSDragOperationGeneric = 4; -public static const int NSDragOperationLink = 2; -public static const int NSDragOperationMove = 16; -public static const int NSDragOperationNone = 0; -public static const int NSDragOperationPrivate = 8; +alias Cocoa.NSDragOperation.NSDragOperationAll_Obsolete NSDragOperationAll; +alias Cocoa.NSDragOperation.NSDragOperationAll_Obsolete NSDragOperationAll_Obsolete; +alias Cocoa.NSDragOperation.NSDragOperationCopy NSDragOperationCopy; +alias Cocoa.NSDragOperation.NSDragOperationDelete NSDragOperationDelete; +alias Cocoa.NSDragOperation.NSDragOperationEvery NSDragOperationEvery; +alias Cocoa.NSDragOperation.NSDragOperationGeneric NSDragOperationGeneric; +alias Cocoa.NSDragOperation.NSDragOperationLink NSDragOperationLink; +alias Cocoa.NSDragOperation.NSDragOperationMove NSDragOperationMove; +alias Cocoa.NSDragOperation.NSDragOperationNone NSDragOperationNone; +alias Cocoa.NSDragOperation.NSDragOperationPrivate NSDragOperationPrivate; public static const int NSDrawerClosedState = 0; public static const int NSDrawerClosingState = 3; public static const int NSDrawerOpenState = 2;