Skip to content

Commit

Permalink
Bringing the required changes to DropTarget.java
Browse files Browse the repository at this point in the history
In the method dropTargetProc(long id, long sel, long arg0) the widget
was being recognised as label instead of the widget that actually has
the drop Target key. As soon as the first drop target key is acquired
the area is recognised as a Drop Target area and the Drag handlers are
added.

Also a new function called handleLabels( Control c, long cls) is
also added to bring the feature of droping on to the top of a label that
has an image view.This functions takes a control as a parameter to go up
till its parent to find the label which has an image view and add drag
handlers for that.
Fixes: #649
  • Loading branch information
elsazac committed Jan 31, 2024
1 parent 3e43811 commit 1d9e062
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ public class DropTarget extends Widget {
Transfer[] transferAgents = new Transfer[0];
DropTargetEffect dropEffect;
int feedback = DND.FEEDBACK_NONE;
boolean labelDragHandlersAdded =true;

// Track application selections
TransferData selectedDataType;
Expand All @@ -115,30 +116,42 @@ public class DropTarget extends Widget {
static final String DEFAULT_DROP_TARGET_EFFECT = "DEFAULT_DROP_TARGET_EFFECT"; //$NON-NLS-1$
static final String IS_ACTIVE = "org.eclipse.swt.internal.isActive"; //$NON-NLS-1$

public void handleLabels(Control c, long cls) {
// If the content view can be an image view, then add the dragging methods to the image view too.
// This is used by Label so that dragging can work even when the Label has an image set on it.
if (!labelDragHandlersAdded) {
return;
}
if (c instanceof Label) {
addDragHandlers(cls); // adding the handlers to label class
long imageView = 0;
if (((imageView = OS.objc_msgSend(c.view.id, OS.sel_getImageView)) != 0)) {
cls = OS.object_getClass(imageView);
addDragHandlers(cls);
labelDragHandlersAdded = false;
}
} else if (c instanceof Composite) {
Control[] cal = ((Composite) c).getChildren();
for (Control child : cal) {
handleLabels(child, cls);
}
}
}
void addDragHandlers() {
// Our strategy here is to dynamically add methods to the control's class that are required
// by NSDraggingDestination. Then, when setTransfer is called, we just register
// the types with the Control's NSView and AppKit will call the methods in the protocol
// when a drag goes over the view.

long cls = OS.object_getClass(control.view.id);

if (cls == 0) {
DND.error(DND.ERROR_CANNOT_INIT_DROP);
}

handleLabels(control,cls);
// If we already added it, no need to do it again.
long procPtr = OS.class_getMethodImplementation(cls, OS.sel_draggingEntered_);
if (procPtr == proc3Args) return;
addDragHandlers(cls);

// If the content view can be image view, then add the dragging methods to image view too.
// This is used by Label so that dragging can work even when the Label has an image set on it.
long imageView = 0;
if ((imageView = OS.objc_msgSend(control.view.id, OS.sel_getImageView)) != 0) {
cls = OS.object_getClass(imageView);
addDragHandlers(cls);
}
}

void addDragHandlers (long cls) {
Expand Down Expand Up @@ -434,7 +447,20 @@ static long dropTargetProc(long id, long sel, long arg0) {
if (display == null || display.isDisposed()) return 0;
Widget widget = display.findWidget(id);
if (widget == null) return 0;
DropTarget dt = (DropTarget)widget.getData(DND.DROP_TARGET_KEY);
DropTarget dt = (DropTarget) widget.getData(DND.DROP_TARGET_KEY);
if (dt == null && widget instanceof Label) {
while (widget != null && !(widget instanceof Shell)) {
dt = (DropTarget) widget.getData(DND.DROP_TARGET_KEY);
if (dt != null) {
break;
}
if (widget instanceof Control) {
Composite widgetParent = ((Control) widget).getParent();
widget = widgetParent;
}
else break;
}
}
if (dt == null) return 0;

// arg0 is _always_ the sender, and implements NSDraggingInfo.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,12 @@ void drawBackground (long id, NSGraphicsContext context, NSRect rect) {

@Override
long imageView() {
return imageView.id;
if (imageView != null) {
return imageView.id;
}
else {
return 0L;
}
};

@Override
Expand Down

0 comments on commit 1d9e062

Please sign in to comment.