Mercurial > projects > dwt-mac
changeset 99:d32621bf0f90
Ported dwt.dnd.DropTarget
author | Jacob Carlborg <doob@me.com> |
---|---|
date | Wed, 31 Dec 2008 13:16:18 +0100 |
parents | 5acb3346c926 |
children | c2f1ba00473b |
files | dwt/dnd/DNDListener.d dwt/dnd/DropTarget.d dwt/dwthelper/array.d dwt/dwthelper/utils.d |
diffstat | 4 files changed, 97 insertions(+), 74 deletions(-) [+] |
line wrap: on
line diff
--- a/dwt/dnd/DNDListener.d Tue Dec 30 20:58:04 2008 +0100 +++ b/dwt/dnd/DNDListener.d Wed Dec 31 13:16:18 2008 +0100 @@ -23,6 +23,7 @@ import dwt.dnd.DND; import dwt.dnd.DNDEvent; import dwt.dnd.DragSource; +import dwt.dnd.DragSourceEffect; import dwt.dnd.DragSourceEvent; import dwt.dnd.DragSourceListener; import dwt.dnd.DropTargetEvent;
--- a/dwt/dnd/DropTarget.d Tue Dec 30 20:58:04 2008 +0100 +++ b/dwt/dnd/DropTarget.d Wed Dec 31 13:16:18 2008 +0100 @@ -7,17 +7,17 @@ * * Contributors: * IBM Corporation - initial API and implementation + * + * Port to the D programming language: + * Jacob Carlborg <doob@me.com> *******************************************************************************/ module dwt.dnd.DropTarget; import dwt.dwthelper.utils; -import java.util.ArrayList; - import dwt.DWT; import dwt.DWTError; import dwt.DWTException; -import dwt.internal.Callback; import dwt.internal.cocoa.NSApplication; import dwt.internal.cocoa.NSArray; import dwt.internal.cocoa.NSCursor; @@ -40,6 +40,18 @@ import dwt.widgets.Tree; import dwt.widgets.Widget; +import dwt.dnd.DND; +import dwt.dnd.DNDEvent; +import dwt.dnd.DNDListener; +import dwt.dnd.DropTargetEffect; +import dwt.dnd.DropTargetListener; +import dwt.dnd.TableDropTargetEffect; +import dwt.dnd.Transfer; +import dwt.dnd.TransferData; +import dwt.dwthelper.array; +import dwt.internal.objc.cocoa.Cocoa; +import objc = dwt.internal.objc.runtime; + /** * * Class <code>DropTarget</code> defines the target object for a drag and drop transfer. @@ -95,26 +107,18 @@ * @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 DropTarget extends Widget { +public class DropTarget : Widget { - static Callback dropTarget2Args, dropTarget3Args; - static int /*long*/ proc2Args, proc3Args; - - static { - Class clazz = DropTarget.class; + static objc.IMP proc2Args, proc3Args; - dropTarget2Args = new Callback(clazz, "dropTargetProc", 2); - proc2Args = dropTarget2Args.getAddress(); - if (proc2Args is 0) DWT.error (DWT.ERROR_NO_MORE_CALLBACKS); - - dropTarget3Args = new Callback(clazz, "dropTargetProc", 3); - proc3Args = dropTarget3Args.getAddress(); - if (proc3Args is 0) DWT.error (DWT.ERROR_NO_MORE_CALLBACKS); + static this () { + proc2Args = cast(objc.IMP) &dropTargetProc2; + proc3Args = cast(objc.IMP) &dropTargetProc3; } Control control; Listener controlListener; - Transfer[] transferAgents = new Transfer[0]; + Transfer[] transferAgents; DropTargetEffect dropEffect; int feedback = DND.FEEDBACK_NONE; @@ -125,7 +129,7 @@ // workaround - There is no event for "operation changed" so track operation based on key state int keyOperation = -1; - static final String DEFAULT_DROP_TARGET_EFFECT = "DEFAULT_DROP_TARGET_EFFECT"; //$NON-NLS-1$ + static const String DEFAULT_DROP_TARGET_EFFECT = "DEFAULT_DROP_TARGET_EFFECT"; //$NON-NLS-1$ void addDragHandlers() { // Our strategy here is to dynamically add methods to the control's class that are required @@ -133,14 +137,14 @@ // the types with the Control's NSView and AppKit will call the methods in the protocol // when a drag goes over the view. - int /*long*/ cls = OS.object_getClass(control.view.id); + objc.Class cls = OS.object_getClass(control.view.id); - if (cls is 0) { + if (cls is null) { DND.error(DND.ERROR_CANNOT_INIT_DROP); } // If we already added it, no need to do it again. - int /*long*/ procPtr = OS.class_getMethodImplementation(cls, OS.sel_draggingEnded_); + objc.IMP procPtr = OS.class_getMethodImplementation(cls, OS.sel_draggingEnded_); if (procPtr is proc3Args) return; // Add the NSDraggingDestination callbacks @@ -202,8 +206,8 @@ } protected void checkSubclass () { - String name = getClass().getName (); - String validName = DropTarget.class.getName(); + String name = this.classinfo.name; + String validName = DropTarget.classinfo.name; if (!validName.equals(name)) { DND.error (DWT.ERROR_INVALID_SUBCLASS); } @@ -270,7 +274,7 @@ DNDEvent event = new DNDEvent(); event.widget = this; - event.time = (int)System.currentTimeMillis(); + event.time = cast(int)System.currentTimeMillis(); event.detail = DND.DROP_NONE; notifyListeners(DND.DragLeave, event); } @@ -349,7 +353,9 @@ * @see DND#DROP_MOVE * @see DND#DROP_LINK */ -public DropTarget(Control control, int style) { +public this(Control control, int style) { + transferAgents = new Transfer[0]; + super(control, checkStyle(style)); this.control = control; @@ -359,55 +365,55 @@ control.setData(DND.DROP_TARGET_KEY, this); - controlListener = new Listener () { + controlListener = new class () Listener { public void handleEvent (Event event) { - if (!DropTarget.this.isDisposed()) { - DropTarget.this.dispose(); + if (!this.outer.isDisposed()) { + this.outer.dispose(); } } }; control.addListener (DWT.Dispose, controlListener); - this.addListener(DWT.Dispose, new Listener() { + this.addListener(DWT.Dispose, new class () Listener { public void handleEvent (Event event) { onDispose(); } }); Object effect = control.getData(DEFAULT_DROP_TARGET_EFFECT); - if (effect instanceof DropTargetEffect) { - dropEffect = (DropTargetEffect) effect; - } else if (control instanceof Table) { - dropEffect = new TableDropTargetEffect((Table) control); - } else if (control instanceof Tree) { - dropEffect = new TreeDropTargetEffect((Tree) control); + if (cast(DropTargetEffect) effect) { + dropEffect = cast(DropTargetEffect) effect; + } else if (cast(Table) control) { + dropEffect = new TableDropTargetEffect(cast(Table) control); + } else if (cast(Tree) control) { + dropEffect = new TreeDropTargetEffect(cast(Tree) control); } addDragHandlers(); } -static int /*long*/ dropTargetProc(int /*long*/ id, int /*long*/ sel) { +static objc.id dropTargetProc2(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; - DropTarget dt = (DropTarget)widget.getData(DND.DROP_TARGET_KEY); - if (dt is null) return 0; + if (widget is null) return null; + DropTarget dt = cast(DropTarget)widget.getData(DND.DROP_TARGET_KEY); + if (dt is null) return null; if (sel is OS.sel_wantsPeriodicDraggingUpdates) { return dt.wantsPeriodicDraggingUpdates() ? 1 : 0; } - return 0; + return null; } -static int /*long*/ dropTargetProc(int /*long*/ id, int /*long*/ sel, int /*long*/ arg0) { +static objc.id dropTargetProc3(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; - DropTarget dt = (DropTarget)widget.getData(DND.DROP_TARGET_KEY); - if (dt is null) return 0; + if (widget is null) return null; + DropTarget dt = cast(DropTarget)widget.getData(DND.DROP_TARGET_KEY); + if (dt is null) return null; // arg0 is _always_ the sender, and implements NSDraggingInfo. // Looks like an NSObject for our purposes, though. @@ -420,10 +426,10 @@ } else if (sel is OS.sel_draggingExited_) { dt.draggingExited(sender); } else if (sel is OS.sel_performDragOperation_) { - return dt.performDragOperation(sender) ? 1 : 0; + return dt.performDragOperation(sender) ? cast(objc.id) 1 : null; } - return 0; + return null; } /** @@ -458,17 +464,17 @@ */ public DropTargetListener[] getDropListeners() { Listener[] listeners = getListeners(DND.DragEnter); - int length = listeners.length; - DropTargetListener[] dropListeners = new DropTargetListener[length]; + int length_ = listeners.length; + DropTargetListener[] dropListeners = new DropTargetListener[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) { - dropListeners[count] = (DropTargetListener) ((DNDListener) listener).getEventListener(); + if (cast(DNDListener) listener) { + dropListeners[count] = cast(DropTargetListener) (cast(DNDListener) listener).getEventListener(); count++; } } - if (count is length) return dropListeners; + if (count is length_) return dropListeners; DropTargetListener[] result = new DropTargetListener[count]; System.arraycopy(dropListeners, 0, result, 0, count); return result; @@ -497,7 +503,7 @@ // correct Cocoa behavior. Control + Option or Command is NSDragOperationGeneric, // or DND.DROP_DEFAULT in the DWT. NSEvent currEvent = NSApplication.sharedApplication().currentEvent(); - int /*long*/ modifiers = currEvent.modifierFlags(); + NSUInteger modifiers = currEvent.modifierFlags(); bool option = (modifiers & OS.NSAlternateKeyMask) is OS.NSAlternateKeyMask; bool control = (modifiers & OS.NSControlKeyMask) is OS.NSControlKeyMask; if (control && option) return DND.DROP_DEFAULT; @@ -530,7 +536,7 @@ } int opToOsOp(int operation) { - int osOperation = 0; + NSDragOperation osOperation = 0; if ((operation & DND.DROP_COPY) !is 0){ osOperation |= OS.NSDragOperationCopy; } @@ -543,10 +549,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; @@ -569,12 +575,12 @@ bool performDragOperation(NSObject sender) { DNDEvent event = new DNDEvent(); event.widget = this; - event.time = (int)System.currentTimeMillis(); + event.time = cast(int)System.currentTimeMillis(); if (dropEffect !is null) { NSPoint mouseLocation = sender.draggingLocation(); NSPoint globalLoc = sender.draggingDestinationWindow().convertBaseToScreen(mouseLocation); - event.item = dropEffect.getItem((int)globalLoc.x, (int)globalLoc.y); + event.item = dropEffect.getItem(cast(int)globalLoc.x, cast(int)globalLoc.y); } event.detail = DND.DROP_NONE; @@ -725,7 +731,7 @@ // get allowed operations int style = getStyle(); - int /*long*/ allowedActions = draggingState.draggingSourceOperationMask(); + NSDragOperation allowedActions = draggingState.draggingSourceOperationMask(); int operations = osOpToOp(allowedActions) & style; if (operations is DND.DROP_NONE) return false; @@ -746,9 +752,9 @@ NSArray draggedTypes = dragPBoard.types(); if (draggedTypes is null) return false; - int /*long*/ draggedTypeCount = draggedTypes.count(); + NSUInteger draggedTypeCount = draggedTypes.count(); - TransferData[] dataTypes = new TransferData[(int)draggedTypeCount]; + TransferData[] dataTypes = new TransferData[draggedTypeCount]; int index = -1; for (int i = 0; i < draggedTypeCount; i++) { id draggedType = draggedTypes.objectAtIndex(i); @@ -776,13 +782,13 @@ NSPoint mouse = draggingState.draggingLocation(); NSPoint globalMouse = draggingState.draggingDestinationWindow().convertBaseToScreen(mouse); NSArray screens = NSScreen.screens(); - NSRect screenRect = new NSScreen(screens.objectAtIndex(0)).frame(); + NSRect screenRect = (new NSScreen(screens.objectAtIndex(0))).frame(); globalMouse.y = screenRect.height - globalMouse.y; event.widget = this; - event.x = (int)globalMouse.x; - event.y = (int)globalMouse.y; - event.time = (int)System.currentTimeMillis(); + event.x = cast(int)globalMouse.x; + event.y = cast(int)globalMouse.y; + event.time = cast(int)System.currentTimeMillis(); event.feedback = DND.FEEDBACK_SELECT; event.dataTypes = dataTypes; event.dataType = dataTypes[0]; @@ -815,7 +821,7 @@ // Register the types as valid drop types in Cocoa. // Accumulate all of the transfer types into a list. - ArrayList typeStrings = new ArrayList(); + String[] typeStrings; for (int i = 0; i < this.transferAgents.length; i++) { String[] types = transferAgents[i].getTypeNames(); @@ -830,7 +836,7 @@ NSMutableArray nsTypeStrings = NSMutableArray.arrayWithCapacity(typeStringCount); for (int i = 0; i < typeStringCount; i++) { - nsTypeStrings.addObject(NSString.stringWith((String)typeStrings.get(i))); + nsTypeStrings.addObject(NSString.stringWith(cast(String)typeStrings.get(i))); } control.view.registerForDraggedTypes(nsTypeStrings);
--- a/dwt/dwthelper/array.d Tue Dec 30 20:58:04 2008 +0100 +++ b/dwt/dwthelper/array.d Wed Dec 31 13:16:18 2008 +0100 @@ -26,7 +26,6 @@ * Returns: the modified array * * Throws: AssertException if the length of the array is 0 - * Throws: AssertException if the element is null */ T[] add (T) (ref T[] arr, T element) in @@ -48,7 +47,6 @@ * Returns: the modified array * * Throws: AssertException if the length of the array is 0 - * Throws: AssertException if the element is null */ alias add addElement; @@ -60,12 +58,15 @@ * index = the index of the element to get * * Returns: the element at the specified index + * + * Throws: AssertException if the length of the array is 0 + * AssertException if the $(D_CODE index) is greater than the length of the array */ -T elementAt (T) (T[] arr, int index) +T elementAt (T) (T[] arr, size_t index) in { assert(arr.length > 0); - assert(index > -1 && index < arr.length); + assert(index < arr.length); } body { @@ -73,6 +74,20 @@ } /** + * Gets the element at the specified position in the array + * + * Params: + * arr = the array to get the element from + * index = the index of the element to get + * + * Returns: the element at the specified position + * + * Throws: AssertException if the length of the array is 0 + * AssertException if the $(D_CODE index) is greater than the length of the array + */ +alias elementAt get; + +/** * Returns the number of elements in the specified array. * * Params: @@ -97,7 +112,7 @@ * Returns: the element that was removed or $(D_CODE null) * * Throws: AssertException if the length of the array is 0 - * Throws: AssertException if the $(D_CODE index) argument is + * AssertException if the $(D_CODE index) argument is * negative or not less than the length of this array. */ T remove (T) (ref T[] arr, int index)