6 #include <X11/cursorfont.h>
8 #include <X11/extensions/shape.h>
11 #define XDND_DESTINATION_RESPONSE_MAX_DELAY 10000
12 #define MIN_X_MOVE_OFFSET 5
13 #define MIN_Y_MOVE_OFFSET 5
14 #define MAX_SLIDEBACK_ITER 15
16 #define XDND_PROPERTY_FORMAT 32
17 #define XDND_ACTION_DESCRIPTION_FORMAT 8
19 #define XDND_DEST_VERSION(dragInfo) dragInfo->protocolVersion
20 #define XDND_SOURCE_INFO(dragInfo) dragInfo->sourceInfo
21 #define XDND_DEST_WIN(dragInfo) dragInfo->sourceInfo->destinationWindow
22 #define XDND_SOURCE_ACTION(dragInfo) dragInfo->sourceAction
23 #define XDND_DEST_ACTION(dragInfo) dragInfo->destinationAction
24 #define XDND_SOURCE_VIEW(dragInfo) dragInfo->sourceInfo->sourceView
25 #define XDND_SOURCE_STATE(dragInfo) dragInfo->sourceInfo->state
26 #define XDND_SELECTION_PROCS(dragInfo) dragInfo->sourceInfo->selectionProcs
27 #define XDND_DRAG_ICON(dragInfo) dragInfo->sourceInfo->icon
28 #define XDND_MOUSE_OFFSET(dragInfo) dragInfo->sourceInfo->mouseOffset
29 #define XDND_DRAG_CURSOR(dragInfo) dragInfo->sourceInfo->dragCursor
30 #define XDND_DRAG_ICON_POS(dragInfo) dragInfo->sourceInfo->imageLocation
31 #define XDND_NO_POS_ZONE(dragInfo) dragInfo->sourceInfo->noPositionMessageZone
32 #define XDND_TIMESTAMP(dragInfo) dragInfo->timestamp
33 #define XDND_3_TYPES(dragInfo) dragInfo->sourceInfo->firstThreeTypes
34 #define XDND_SOURCE_VIEW_STORED(dragInfo) dragInfo->sourceInfo != NULL \
35 && dragInfo->sourceInfo->sourceView != NULL
37 static WMHandlerID dndSourceTimer
= NULL
;
39 static void *idleState(WMView
* srcView
, XClientMessageEvent
* event
, WMDraggingInfo
* info
);
40 static void *dropAllowedState(WMView
* srcView
, XClientMessageEvent
* event
, WMDraggingInfo
* info
);
41 static void *finishDropState(WMView
* srcView
, XClientMessageEvent
* event
, WMDraggingInfo
* info
);
44 static const char *stateName(W_DndState
* state
)
47 return "no state defined";
49 if (state
== idleState
)
52 if (state
== dropAllowedState
)
53 return "dropAllowedState";
55 if (state
== finishDropState
)
56 return "finishDropState";
58 return "unknown state";
62 static WMScreen
*sourceScreen(WMDraggingInfo
* info
)
64 return W_VIEW_SCREEN(XDND_SOURCE_VIEW(info
));
67 static void endDragProcess(WMDraggingInfo
* info
, Bool deposited
)
69 WMView
*view
= XDND_SOURCE_VIEW(info
);
70 WMScreen
*scr
= W_VIEW_SCREEN(XDND_SOURCE_VIEW(info
));
72 /* free selection handler while view exists */
73 WMDeleteSelectionHandler(view
, scr
->xdndSelectionAtom
, CurrentTime
);
74 wfree(XDND_SELECTION_PROCS(info
));
76 if (XDND_DRAG_CURSOR(info
) != None
) {
77 XFreeCursor(scr
->display
, XDND_DRAG_CURSOR(info
));
78 XDND_DRAG_CURSOR(info
) = None
;
81 if (view
->dragSourceProcs
->endedDrag
!= NULL
) {
82 /* this can destroy source view (with a "move" action for example) */
83 view
->dragSourceProcs
->endedDrag(view
, &XDND_DRAG_ICON_POS(info
), deposited
);
86 /* clear remaining draggging infos */
87 wfree(XDND_SOURCE_INFO(info
));
88 XDND_SOURCE_INFO(info
) = NULL
;
91 /* ----- drag cursor ----- */
92 static void initDragCursor(WMDraggingInfo
* info
)
94 WMScreen
*scr
= sourceScreen(info
);
95 XColor cursorFgColor
, cursorBgColor
;
98 cursorFgColor
.red
= 0x4500;
99 cursorFgColor
.green
= 0xb000;
100 cursorFgColor
.blue
= 0x4500;
103 cursorBgColor
.red
= 0xffff;
104 cursorBgColor
.green
= 0xffff;
105 cursorBgColor
.blue
= 0xffff;
107 XDND_DRAG_CURSOR(info
) = XCreateFontCursor(scr
->display
, XC_left_ptr
);
108 XRecolorCursor(scr
->display
, XDND_DRAG_CURSOR(info
), &cursorFgColor
, &cursorBgColor
);
110 XFlush(scr
->display
);
113 static void recolorCursor(WMDraggingInfo
* info
, Bool dropIsAllowed
)
115 WMScreen
*scr
= sourceScreen(info
);
118 XDefineCursor(scr
->display
, scr
->rootWin
, XDND_DRAG_CURSOR(info
));
120 XDefineCursor(scr
->display
, scr
->rootWin
, scr
->defaultCursor
);
123 XFlush(scr
->display
);
126 /* ----- end of drag cursor ----- */
128 /* ----- selection procs ----- */
129 static WMData
*convertSelection(WMView
* view
, Atom selection
, Atom target
, void *cdata
, Atom
* type
)
135 scr
= W_VIEW_SCREEN(view
);
136 typeName
= XGetAtomName(scr
->display
, target
);
140 if (view
->dragSourceProcs
->fetchDragData
!= NULL
) {
141 data
= view
->dragSourceProcs
->fetchDragData(view
, typeName
);
146 if (typeName
!= NULL
)
152 static void selectionLost(WMView
* view
, Atom selection
, void *cdata
)
154 wwarning("DND selection lost during drag operation...");
157 static void selectionDone(WMView
* view
, Atom selection
, Atom target
, void *cdata
)
160 printf("selection done\n");
164 /* ----- end of selection procs ----- */
166 /* ----- visual part ----- */
168 static Window
makeDragIcon(WMScreen
* scr
, WMPixmap
* pixmap
)
173 XSetWindowAttributes attribs
;
177 pixmap
= scr
->defaultObjectIcon
;
180 size
= WMGetPixmapSize(pixmap
);
181 pix
= pixmap
->pixmap
;
184 flags
= CWSaveUnder
| CWBackPixmap
| CWOverrideRedirect
| CWColormap
;
185 attribs
.save_under
= True
;
186 attribs
.background_pixmap
= pix
;
187 attribs
.override_redirect
= True
;
188 attribs
.colormap
= scr
->colormap
;
190 window
= XCreateWindow(scr
->display
, scr
->rootWin
, 0, 0, size
.width
,
191 size
.height
, 0, scr
->depth
, InputOutput
, scr
->visual
, flags
, &attribs
);
196 XShapeCombineMask(scr
->display
, window
, ShapeBounding
, 0, 0, mask
, ShapeSet
);
203 static void slideWindow(Display
* dpy
, Window win
, int srcX
, int srcY
, int dstX
, int dstY
)
209 iterations
= WMIN(MAX_SLIDEBACK_ITER
, WMAX(abs(dstX
- srcX
), abs(dstY
- srcY
)));
214 dx
= (double)(dstX
- srcX
) / iterations
;
215 dy
= (double)(dstY
- srcY
) / iterations
;
217 for (i
= 0; i
<= iterations
; i
++) {
218 XMoveWindow(dpy
, win
, x
, y
);
228 static int getInitialDragImageCoord(int viewCoord
, int mouseCoord
, int viewSize
, int iconSize
)
230 if (iconSize
>= viewSize
) {
231 /* center icon coord on view */
232 return viewCoord
- iconSize
/ 2;
234 /* try to center icon on mouse pos */
236 if (mouseCoord
- iconSize
/ 2 <= viewCoord
)
237 /* if icon was centered on mouse, it would be off view
238 thus, put icon left (resp. top) side
239 at view (resp. top) side */
242 else if (mouseCoord
+ iconSize
/ 2 >= viewCoord
+ viewSize
)
243 /* if icon was centered on mouse, it would be off view
244 thus, put icon right (resp. bottom) side
245 at view right (resp. bottom) side */
246 return viewCoord
+ viewSize
- iconSize
;
249 return mouseCoord
- iconSize
/ 2;
254 static void initDragImagePos(WMView
* view
, WMDraggingInfo
* info
, XEvent
* event
)
256 WMSize iconSize
= WMGetPixmapSize(view
->dragImage
);
257 WMSize viewSize
= WMGetViewSize(view
);
261 XTranslateCoordinates(W_VIEW_SCREEN(view
)->display
,
262 WMViewXID(view
), W_VIEW_SCREEN(view
)->rootWin
,
263 0, 0, &(viewPos
.x
), &(viewPos
.y
), &foo
);
266 XDND_DRAG_ICON_POS(info
).x
=
267 getInitialDragImageCoord(viewPos
.x
, event
->xmotion
.x_root
, viewSize
.width
, iconSize
.width
);
269 XDND_DRAG_ICON_POS(info
).y
=
270 getInitialDragImageCoord(viewPos
.y
, event
->xmotion
.y_root
, viewSize
.height
, iconSize
.height
);
272 /* set mouse offset relative to icon */
273 XDND_MOUSE_OFFSET(info
).x
= event
->xmotion
.x_root
- XDND_DRAG_ICON_POS(info
).x
;
274 XDND_MOUSE_OFFSET(info
).y
= event
->xmotion
.y_root
- XDND_DRAG_ICON_POS(info
).y
;
277 static void refreshDragImage(WMView
* view
, WMDraggingInfo
* info
)
279 WMScreen
*scr
= W_VIEW_SCREEN(view
);
281 XMoveWindow(scr
->display
, XDND_DRAG_ICON(info
), XDND_DRAG_ICON_POS(info
).x
, XDND_DRAG_ICON_POS(info
).y
);
284 static void startDragImage(WMView
* view
, WMDraggingInfo
* info
, XEvent
* event
)
286 WMScreen
*scr
= W_VIEW_SCREEN(view
);
288 XDND_DRAG_ICON(info
) = makeDragIcon(scr
, view
->dragImage
);
289 initDragImagePos(view
, info
, event
);
290 refreshDragImage(view
, info
);
291 XMapRaised(scr
->display
, XDND_DRAG_ICON(info
));
293 initDragCursor(info
);
296 static void endDragImage(WMDraggingInfo
* info
, Bool slideBack
)
298 WMView
*view
= XDND_SOURCE_VIEW(info
);
299 Display
*dpy
= W_VIEW_SCREEN(view
)->display
;
305 XTranslateCoordinates(W_VIEW_SCREEN(view
)->display
,
306 WMViewXID(view
), W_VIEW_SCREEN(view
)->rootWin
,
307 0, 0, &(toLocation
.x
), &(toLocation
.y
), &foo
);
309 slideWindow(dpy
, XDND_DRAG_ICON(info
),
310 XDND_DRAG_ICON_POS(info
).x
, XDND_DRAG_ICON_POS(info
).y
, toLocation
.x
, toLocation
.y
);
313 XDestroyWindow(dpy
, XDND_DRAG_ICON(info
));
316 /* ----- end of visual part ----- */
318 /* ----- messages ----- */
320 /* send a DnD message to the destination window */
322 sendDnDClientMessage(WMDraggingInfo
* info
, Atom message
,
323 unsigned long data1
, unsigned long data2
, unsigned long data3
, unsigned long data4
)
325 Display
*dpy
= sourceScreen(info
)->display
;
326 Window srcWin
= WMViewXID(XDND_SOURCE_VIEW(info
));
327 Window destWin
= XDND_DEST_WIN(info
);
329 if (!W_SendDnDClientMessage(dpy
, destWin
, message
, srcWin
, data1
, data2
, data3
, data4
)) {
331 recolorCursor(info
, False
);
332 endDragImage(info
, True
);
333 endDragProcess(info
, False
);
340 static Bool
sendEnterMessage(WMDraggingInfo
* info
)
342 WMScreen
*scr
= sourceScreen(info
);
343 unsigned long version
;
345 if (XDND_DEST_VERSION(info
) > 2) {
346 if (XDND_DEST_VERSION(info
) < XDND_VERSION
)
347 version
= XDND_DEST_VERSION(info
);
349 version
= XDND_VERSION
;
354 return sendDnDClientMessage(info
, scr
->xdndEnterAtom
, (version
<< 24) | 1, /* 1: support of type list */
355 XDND_3_TYPES(info
)[0], XDND_3_TYPES(info
)[1], XDND_3_TYPES(info
)[2]);
358 static Bool
sendPositionMessage(WMDraggingInfo
* info
, WMPoint
* mousePos
)
360 WMScreen
*scr
= sourceScreen(info
);
361 WMRect
*noPosZone
= &(XDND_NO_POS_ZONE(info
));
363 if (noPosZone
->size
.width
!= 0 || noPosZone
->size
.height
!= 0) {
364 if (mousePos
->x
< noPosZone
->pos
.x
|| mousePos
->x
> (noPosZone
->pos
.x
+ noPosZone
->size
.width
)
365 || mousePos
->y
< noPosZone
->pos
.y
|| mousePos
->y
> (noPosZone
->pos
.y
+ noPosZone
->size
.height
)) {
366 /* send position if out of zone defined by destination */
367 return sendDnDClientMessage(info
, scr
->xdndPositionAtom
,
369 mousePos
->x
<< 16 | mousePos
->y
,
370 XDND_TIMESTAMP(info
), XDND_SOURCE_ACTION(info
));
373 /* Nothing to send, always succeed */
378 /* send position on each move */
379 return sendDnDClientMessage(info
, scr
->xdndPositionAtom
,
381 mousePos
->x
<< 16 | mousePos
->y
,
382 XDND_TIMESTAMP(info
), XDND_SOURCE_ACTION(info
));
385 static Bool
sendLeaveMessage(WMDraggingInfo
* info
)
387 WMScreen
*scr
= sourceScreen(info
);
389 return sendDnDClientMessage(info
, scr
->xdndLeaveAtom
, 0, 0, 0, 0);
392 static Bool
sendDropMessage(WMDraggingInfo
* info
)
394 WMScreen
*scr
= sourceScreen(info
);
396 return sendDnDClientMessage(info
, scr
->xdndDropAtom
, 0, XDND_TIMESTAMP(info
), 0, 0);
399 /* ----- end of messages ----- */
401 static Atom
*getTypeAtomList(WMScreen
* scr
, WMView
* view
, int *count
)
407 types
= view
->dragSourceProcs
->dropDataTypes(view
);
410 *count
= WMGetArrayItemCount(types
);
412 typeAtoms
= wmalloc((*count
) * sizeof(Atom
));
413 for (i
= 0; i
< *count
; i
++) {
414 typeAtoms
[i
] = XInternAtom(scr
->display
, WMGetFromArray(types
, i
), False
);
417 /* WMFreeArray(types); */
421 /* WMFreeArray(types); */
425 typeAtoms
= wmalloc(sizeof(Atom
));
431 static void registerDropTypes(WMScreen
* scr
, WMView
* view
, WMDraggingInfo
* info
)
436 typeList
= getTypeAtomList(scr
, view
, &count
);
438 /* store the first 3 types */
439 for (i
= 0; i
< 3 && i
< count
; i
++)
440 XDND_3_TYPES(info
)[i
] = typeList
[i
];
443 XDND_3_TYPES(info
)[i
] = None
;
445 /* store the entire type list */
446 XChangeProperty(scr
->display
,
448 scr
->xdndTypeListAtom
,
449 XA_ATOM
, XDND_PROPERTY_FORMAT
, PropModeReplace
, (unsigned char *)typeList
, count
);
452 static void registerOperationList(WMScreen
* scr
, WMView
* view
, WMArray
* operationArray
)
455 WMDragOperationType operation
;
456 int count
= WMGetArrayItemCount(operationArray
);
459 actionList
= wmalloc(sizeof(Atom
) * count
);
461 for (i
= 0; i
< count
; i
++) {
462 operation
= WMGetDragOperationItemType(WMGetFromArray(operationArray
, i
));
463 actionList
[i
] = W_OperationToAction(scr
, operation
);
466 XChangeProperty(scr
->display
,
468 scr
->xdndActionListAtom
,
469 XA_ATOM
, XDND_PROPERTY_FORMAT
, PropModeReplace
, (unsigned char *)actionList
, count
);
472 static void registerDescriptionList(WMScreen
* scr
, WMView
* view
, WMArray
* operationArray
)
474 char *text
, *textListItem
, *textList
;
475 int count
= WMGetArrayItemCount(operationArray
);
479 /* size of XA_STRING info */
480 for (i
= 0; i
< count
; i
++) {
481 size
+= strlen(WMGetDragOperationItemText(WMGetFromArray(operationArray
, i
))) + 1 /* NULL */;
484 /* create text list */
485 textList
= wmalloc(size
);
486 textListItem
= textList
;
488 for (i
= 0; i
< count
; i
++) {
489 text
= WMGetDragOperationItemText(WMGetFromArray(operationArray
, i
));
490 wstrlcpy(textListItem
, text
, size
);
492 /* to next text offset */
493 textListItem
= &(textListItem
[strlen(textListItem
) + 1]);
496 XChangeProperty(scr
->display
,
498 scr
->xdndActionDescriptionAtom
,
500 XDND_ACTION_DESCRIPTION_FORMAT
, PropModeReplace
, (unsigned char *)textList
, size
);
503 /* called if wanted operation is WDOperationAsk */
504 static void registerSupportedOperations(WMView
* view
)
506 WMScreen
*scr
= W_VIEW_SCREEN(view
);
507 WMArray
*operationArray
;
509 operationArray
= view
->dragSourceProcs
->askedOperations(view
);
511 registerOperationList(scr
, view
, operationArray
);
512 registerDescriptionList(scr
, view
, operationArray
);
514 /* WMFreeArray(operationArray); */
517 static void initSourceDragInfo(WMView
* sourceView
, WMDraggingInfo
* info
)
521 XDND_SOURCE_INFO(info
) = (W_DragSourceInfo
*) wmalloc(sizeof(W_DragSourceInfo
));
523 XDND_SOURCE_VIEW(info
) = sourceView
;
524 XDND_DEST_WIN(info
) = None
;
525 XDND_DRAG_ICON(info
) = None
;
527 XDND_SOURCE_ACTION(info
) = W_OperationToAction(W_VIEW_SCREEN(sourceView
),
528 sourceView
->dragSourceProcs
->
529 wantedDropOperation(sourceView
));
531 XDND_DEST_ACTION(info
) = None
;
533 XDND_SOURCE_STATE(info
) = idleState
;
535 emptyZone
.pos
= wmkpoint(0, 0);
536 emptyZone
.size
= wmksize(0, 0);
537 XDND_NO_POS_ZONE(info
) = emptyZone
;
541 Returned array is destroyed after dropDataTypes call
543 static WMArray
*defDropDataTypes(WMView
* self
)
548 static WMDragOperationType
defWantedDropOperation(WMView
* self
)
550 return WDOperationNone
;
554 Must be defined if wantedDropOperation return WDOperationAsk
556 Return a WMDragOperationItem array (destroyed after call).
557 A WMDragOperationItem links a label to an operation.
559 defAskedOperations(WMView *self); */
561 static Bool
defAcceptDropOperation(WMView
* self
, WMDragOperationType allowedOperation
)
566 static void defBeganDrag(WMView
* self
, WMPoint
* point
)
570 static void defEndedDrag(WMView
* self
, WMPoint
* point
, Bool deposited
)
575 Returned data is not destroyed
577 static WMData
*defFetchDragData(WMView
* self
, char *type
)
582 void WMSetViewDragSourceProcs(WMView
* view
, WMDragSourceProcs
* procs
)
584 if (view
->dragSourceProcs
)
585 wfree(view
->dragSourceProcs
);
586 view
->dragSourceProcs
= wmalloc(sizeof(WMDragSourceProcs
));
588 *view
->dragSourceProcs
= *procs
;
590 if (procs
->dropDataTypes
== NULL
)
591 view
->dragSourceProcs
->dropDataTypes
= defDropDataTypes
;
593 if (procs
->wantedDropOperation
== NULL
)
594 view
->dragSourceProcs
->wantedDropOperation
= defWantedDropOperation
;
597 Note: askedOperations can be NULL, if wantedDropOperation never returns
601 if (procs
->acceptDropOperation
== NULL
)
602 view
->dragSourceProcs
->acceptDropOperation
= defAcceptDropOperation
;
604 if (procs
->beganDrag
== NULL
)
605 view
->dragSourceProcs
->beganDrag
= defBeganDrag
;
607 if (procs
->endedDrag
== NULL
)
608 view
->dragSourceProcs
->endedDrag
= defEndedDrag
;
610 if (procs
->fetchDragData
== NULL
)
611 view
->dragSourceProcs
->fetchDragData
= defFetchDragData
;
614 static Bool
isXdndAware(WMScreen
* scr
, Window win
)
618 unsigned long count
, remain
;
619 unsigned char *winXdndVersion
;
624 XGetWindowProperty(scr
->display
, win
, scr
->xdndAwareAtom
,
625 0, 1, False
, XA_ATOM
, &type
, &format
, &count
, &remain
, &winXdndVersion
);
627 if (type
!= XA_ATOM
|| format
!= XDND_PROPERTY_FORMAT
|| count
== 0 || !winXdndVersion
) {
629 XFree(winXdndVersion
);
633 XFree(winXdndVersion
);
634 return (count
== 1); /* xdnd version is set */
637 static Window
*windowChildren(Display
* dpy
, Window win
, unsigned *nchildren
)
642 if (!XQueryTree(dpy
, win
, &foo
, &bar
, &children
, nchildren
)) {
649 static Window
lookForAwareWindow(WMScreen
* scr
, WMPoint
* mousePos
, Window win
)
654 /* Since xdnd v3, only the toplevel window should be aware */
655 if (isXdndAware(scr
, win
))
658 /* inspect child under pointer */
659 if (XTranslateCoordinates(scr
->display
, scr
->rootWin
, win
, mousePos
->x
, mousePos
->y
, &tmpx
, &tmpy
, &child
)) {
663 return lookForAwareWindow(scr
, mousePos
, child
);
669 static Window
findDestination(WMDraggingInfo
* info
, WMPoint
* mousePos
)
671 WMScreen
*scr
= sourceScreen(info
);
673 Window
*children
= windowChildren(scr
->display
, scr
->rootWin
, &nchildren
);
675 XWindowAttributes attr
;
677 if (isXdndAware(scr
, scr
->rootWin
))
680 /* exclude drag icon (and upper) from search */
681 for (i
= nchildren
- 1; i
>= 0; i
--) {
682 if (children
[i
] == XDND_DRAG_ICON(info
)) {
689 /* root window has no child under drag icon, and is not xdnd aware. */
693 /* inspecting children, from upper to lower */
694 for (; i
>= 0; i
--) {
695 if (XGetWindowAttributes(scr
->display
, children
[i
], &attr
)
696 && attr
.map_state
== IsViewable
697 && mousePos
->x
>= attr
.x
698 && mousePos
->y
>= attr
.y
699 && mousePos
->x
< attr
.x
+ attr
.width
&& mousePos
->y
< attr
.y
+ attr
.height
) {
700 return lookForAwareWindow(scr
, mousePos
, children
[i
]);
704 /* No child window under drag pointer */
708 static void storeDestinationProtocolVersion(WMDraggingInfo
* info
)
712 unsigned long count
, remain
;
713 unsigned char *winXdndVersion
;
714 WMScreen
*scr
= W_VIEW_SCREEN(XDND_SOURCE_VIEW(info
));
716 wassertr(XDND_DEST_WIN(info
) != None
);
718 if (XGetWindowProperty(scr
->display
, XDND_DEST_WIN(info
),
720 0, 1, False
, XA_ATOM
, &type
, &format
,
721 &count
, &remain
, &winXdndVersion
) == Success
) {
722 XDND_DEST_VERSION(info
) = *winXdndVersion
;
723 XFree(winXdndVersion
);
725 XDND_DEST_VERSION(info
) = 0;
726 wwarning("failed to read XDND version of drop target");
730 static void initMotionProcess(WMView
* view
, WMDraggingInfo
* info
, XEvent
* event
, WMPoint
* startLocation
)
732 WMScreen
*scr
= W_VIEW_SCREEN(view
);
734 /* take ownership of XdndSelection */
735 XDND_SELECTION_PROCS(info
) = (WMSelectionProcs
*) wmalloc(sizeof(WMSelectionProcs
));
736 XDND_SELECTION_PROCS(info
)->convertSelection
= convertSelection
;
737 XDND_SELECTION_PROCS(info
)->selectionLost
= selectionLost
;
738 XDND_SELECTION_PROCS(info
)->selectionDone
= selectionDone
;
739 XDND_TIMESTAMP(info
) = event
->xmotion
.time
;
741 if (!WMCreateSelectionHandler(view
, scr
->xdndSelectionAtom
, CurrentTime
, XDND_SELECTION_PROCS(info
), NULL
)) {
742 wwarning("could not get ownership or DND selection");
746 registerDropTypes(scr
, view
, info
);
748 if (XDND_SOURCE_ACTION(info
) == W_VIEW_SCREEN(view
)->xdndActionAsk
)
749 registerSupportedOperations(view
);
751 if (view
->dragSourceProcs
->beganDrag
!= NULL
) {
752 view
->dragSourceProcs
->beganDrag(view
, startLocation
);
756 static void processMotion(WMDraggingInfo
* info
, WMPoint
* mousePos
)
758 Window newDestination
= findDestination(info
, mousePos
);
760 W_DragSourceStopTimer();
762 if (newDestination
!= XDND_DEST_WIN(info
)) {
763 recolorCursor(info
, False
);
765 if (XDND_DEST_WIN(info
) != None
) {
766 /* leaving a xdnd window */
767 sendLeaveMessage(info
);
770 XDND_DEST_WIN(info
) = newDestination
;
771 XDND_DEST_ACTION(info
) = None
;
772 XDND_NO_POS_ZONE(info
).size
.width
= 0;
773 XDND_NO_POS_ZONE(info
).size
.height
= 0;
775 if (newDestination
!= None
) {
776 /* entering a xdnd window */
777 XDND_SOURCE_STATE(info
) = idleState
;
778 storeDestinationProtocolVersion(info
);
780 if (!sendEnterMessage(info
)) {
781 XDND_DEST_WIN(info
) = None
;
785 W_DragSourceStartTimer(info
);
787 XDND_SOURCE_STATE(info
) = NULL
;
790 if (XDND_DEST_WIN(info
) != None
) {
791 if (!sendPositionMessage(info
, mousePos
)) {
792 XDND_DEST_WIN(info
) = None
;
796 W_DragSourceStartTimer(info
);
801 static Bool
processButtonRelease(WMDraggingInfo
* info
)
803 if (XDND_SOURCE_STATE(info
) == dropAllowedState
) {
805 W_DragSourceStopTimer();
807 if (!sendDropMessage(info
))
810 W_DragSourceStartTimer(info
);
813 if (XDND_DEST_WIN(info
) != None
)
814 sendLeaveMessage(info
);
816 W_DragSourceStopTimer();
821 Bool
WMIsDraggingFromView(WMView
* view
)
823 WMDraggingInfo
*info
= &W_VIEW_SCREEN(view
)->dragInfo
;
825 return (XDND_SOURCE_INFO(info
) != NULL
&& XDND_SOURCE_STATE(info
) != finishDropState
);
826 /* return W_VIEW_SCREEN(view)->dragInfo.sourceInfo != NULL; */
829 void WMDragImageFromView(WMView
* view
, XEvent
* event
)
831 WMDraggingInfo
*info
= &W_VIEW_SCREEN(view
)->dragInfo
;
832 WMPoint mouseLocation
;
834 switch (event
->type
) {
836 if (event
->xbutton
.button
== Button1
) {
839 XPeekEvent(event
->xbutton
.display
, &nextEvent
);
841 /* Initialize only if a drag really begins (avoid clicks) */
842 if (nextEvent
.type
== MotionNotify
) {
843 initSourceDragInfo(view
, info
);
850 if (WMIsDraggingFromView(view
)) {
851 Bool dropBegan
= processButtonRelease(info
);
853 recolorCursor(info
, False
);
855 endDragImage(info
, False
);
856 XDND_SOURCE_STATE(info
) = finishDropState
;
859 endDragImage(info
, True
);
860 endDragProcess(info
, False
);
866 if (WMIsDraggingFromView(view
)) {
867 mouseLocation
= wmkpoint(event
->xmotion
.x_root
, event
->xmotion
.y_root
);
869 if (abs(XDND_DRAG_ICON_POS(info
).x
- mouseLocation
.x
) >=
871 || abs(XDND_DRAG_ICON_POS(info
).y
- mouseLocation
.y
) >= MIN_Y_MOVE_OFFSET
) {
872 if (XDND_DRAG_ICON(info
) == None
) {
873 initMotionProcess(view
, info
, event
, &mouseLocation
);
874 startDragImage(view
, info
, event
);
876 XDND_DRAG_ICON_POS(info
).x
= mouseLocation
.x
- XDND_MOUSE_OFFSET(info
).x
;
877 XDND_DRAG_ICON_POS(info
).y
= mouseLocation
.y
- XDND_MOUSE_OFFSET(info
).y
;
879 refreshDragImage(view
, info
);
880 processMotion(info
, &mouseLocation
);
888 /* Minimal mouse events handler: no right or double-click detection,
889 only drag is supported */
890 static void dragImageHandler(XEvent
* event
, void *cdata
)
892 WMView
*view
= (WMView
*) cdata
;
894 WMDragImageFromView(view
, event
);
897 /* ----- source states ----- */
900 static void traceStatusMsg(Display
* dpy
, XClientMessageEvent
* statusEvent
)
902 printf("Xdnd status message:\n");
904 if (statusEvent
->data
.l
[1] & 0x2UL
)
905 printf("\tsend position on every move\n");
908 x
= statusEvent
->data
.l
[2] >> 16;
909 y
= statusEvent
->data
.l
[2] & 0xFFFFL
;
910 w
= statusEvent
->data
.l
[3] >> 16;
911 h
= statusEvent
->data
.l
[3] & 0xFFFFL
;
913 printf("\tsend position out of ((%d,%d) , (%d,%d))\n", x
, y
, x
+ w
, y
+ h
);
916 if (statusEvent
->data
.l
[1] & 0x1L
)
917 printf("\tallowed action: %s\n", XGetAtomName(dpy
, statusEvent
->data
.l
[4]));
919 printf("\tno action allowed\n");
923 static void storeDropAction(WMDraggingInfo
* info
, Atom destAction
)
925 WMView
*sourceView
= XDND_SOURCE_VIEW(info
);
926 WMScreen
*scr
= W_VIEW_SCREEN(sourceView
);
928 if (sourceView
->dragSourceProcs
->acceptDropOperation
!= NULL
) {
929 if (sourceView
->dragSourceProcs
->acceptDropOperation(sourceView
,
930 W_ActionToOperation(scr
, destAction
)))
931 XDND_DEST_ACTION(info
) = destAction
;
933 XDND_DEST_ACTION(info
) = None
;
935 XDND_DEST_ACTION(info
) = destAction
;
939 static void storeStatusMessageInfos(WMDraggingInfo
* info
, XClientMessageEvent
* statusEvent
)
941 WMRect
*noPosZone
= &(XDND_NO_POS_ZONE(info
));
945 traceStatusMsg(sourceScreen(info
)->display
, statusEvent
);
948 if (statusEvent
->data
.l
[1] & 0x2UL
) {
949 /* bit 1 set: destination wants position messages on every move */
950 noPosZone
->size
.width
= 0;
951 noPosZone
->size
.height
= 0;
953 /* don't send another position message while in given rectangle */
954 noPosZone
->pos
.x
= statusEvent
->data
.l
[2] >> 16;
955 noPosZone
->pos
.y
= statusEvent
->data
.l
[2] & 0xFFFFL
;
956 noPosZone
->size
.width
= statusEvent
->data
.l
[3] >> 16;
957 noPosZone
->size
.height
= statusEvent
->data
.l
[3] & 0xFFFFL
;
960 if ((statusEvent
->data
.l
[1] & 0x1L
) || statusEvent
->data
.l
[4] != None
) {
961 /* destination accept drop */
962 storeDropAction(info
, statusEvent
->data
.l
[4]);
964 XDND_DEST_ACTION(info
) = None
;
968 static void *idleState(WMView
* view
, XClientMessageEvent
* event
, WMDraggingInfo
* info
)
971 Atom destMsg
= event
->message_type
;
973 scr
= W_VIEW_SCREEN(view
);
975 if (destMsg
== scr
->xdndStatusAtom
) {
976 storeStatusMessageInfos(info
, event
);
978 if (XDND_DEST_ACTION(info
) != None
) {
979 recolorCursor(info
, True
);
980 W_DragSourceStartTimer(info
);
981 return dropAllowedState
;
984 recolorCursor(info
, False
);
989 if (destMsg
== scr
->xdndFinishedAtom
) {
990 wwarning("received xdndFinishedAtom before drop began");
993 W_DragSourceStartTimer(info
);
997 static void *dropAllowedState(WMView
* view
, XClientMessageEvent
* event
, WMDraggingInfo
* info
)
999 WMScreen
*scr
= W_VIEW_SCREEN(view
);
1000 Atom destMsg
= event
->message_type
;
1002 if (destMsg
== scr
->xdndStatusAtom
) {
1003 storeStatusMessageInfos(info
, event
);
1005 if (XDND_DEST_ACTION(info
) == None
) {
1007 recolorCursor(info
, False
);
1012 W_DragSourceStartTimer(info
);
1013 return dropAllowedState
;
1016 static void *finishDropState(WMView
* view
, XClientMessageEvent
* event
, WMDraggingInfo
* info
)
1018 WMScreen
*scr
= W_VIEW_SCREEN(view
);
1019 Atom destMsg
= event
->message_type
;
1021 if (destMsg
== scr
->xdndFinishedAtom
) {
1022 endDragProcess(info
, True
);
1026 W_DragSourceStartTimer(info
);
1027 return finishDropState
;
1030 /* ----- end of source states ----- */
1032 /* ----- source timer ----- */
1033 static void dragSourceResponseTimeOut(void *source
)
1035 WMView
*view
= (WMView
*) source
;
1036 WMDraggingInfo
*info
= &(W_VIEW_SCREEN(view
)->dragInfo
);
1038 wwarning("delay for drag destination response expired");
1039 sendLeaveMessage(info
);
1041 recolorCursor(info
, False
);
1042 if (XDND_SOURCE_STATE(info
) == finishDropState
) {
1044 endDragImage(info
, True
);
1045 endDragProcess(info
, False
);
1047 XDND_SOURCE_STATE(info
) = idleState
;
1051 void W_DragSourceStopTimer()
1053 if (dndSourceTimer
!= NULL
) {
1054 WMDeleteTimerHandler(dndSourceTimer
);
1055 dndSourceTimer
= NULL
;
1059 void W_DragSourceStartTimer(WMDraggingInfo
* info
)
1061 W_DragSourceStopTimer();
1063 dndSourceTimer
= WMAddTimerHandler(XDND_DESTINATION_RESPONSE_MAX_DELAY
,
1064 dragSourceResponseTimeOut
, XDND_SOURCE_VIEW(info
));
1067 /* ----- End of Destination timer ----- */
1069 void W_DragSourceStateHandler(WMDraggingInfo
* info
, XClientMessageEvent
* event
)
1072 W_DndState
*newState
;
1074 if (XDND_SOURCE_VIEW_STORED(info
)) {
1075 if (XDND_SOURCE_STATE(info
) != NULL
) {
1076 view
= XDND_SOURCE_VIEW(info
);
1079 printf("current source state: %s\n", stateName(XDND_SOURCE_STATE(info
)));
1082 newState
= (W_DndState
*) XDND_SOURCE_STATE(info
) (view
, event
, info
);
1086 printf("new source state: %s\n", stateName(newState
));
1089 if (newState
!= NULL
)
1090 XDND_SOURCE_STATE(info
) = newState
;
1091 /* else drop finished, and info has been flushed */
1095 wwarning("received DnD message without having a target");
1099 void WMSetViewDragImage(WMView
* view
, WMPixmap
* dragImage
)
1101 if (view
->dragImage
!= NULL
)
1102 WMReleasePixmap(view
->dragImage
);
1104 view
->dragImage
= WMRetainPixmap(dragImage
);
1107 void WMReleaseViewDragImage(WMView
* view
)
1109 if (view
->dragImage
!= NULL
)
1110 WMReleasePixmap(view
->dragImage
);
1113 /* Create a drag handler, associating drag event masks with dragEventProc */
1114 void WMCreateDragHandler(WMView
* view
, WMEventProc
* dragEventProc
, void *clientData
)
1116 WMCreateEventHandler(view
,
1117 ButtonPressMask
| ButtonReleaseMask
| Button1MotionMask
, dragEventProc
, clientData
);
1120 void WMDeleteDragHandler(WMView
* view
, WMEventProc
* dragEventProc
, void *clientData
)
1122 WMDeleteEventHandler(view
,
1123 ButtonPressMask
| ButtonReleaseMask
| Button1MotionMask
, dragEventProc
, clientData
);
1126 /* set default drag handler for view */
1127 void WMSetViewDraggable(WMView
* view
, WMDragSourceProcs
* dragSourceProcs
, WMPixmap
* dragImage
)
1129 wassertr(dragImage
!= NULL
);
1130 view
->dragImage
= WMRetainPixmap(dragImage
);
1132 WMSetViewDragSourceProcs(view
, dragSourceProcs
);
1134 WMCreateDragHandler(view
, dragImageHandler
, view
);
1137 void WMUnsetViewDraggable(WMView
* view
)
1139 if (view
->dragSourceProcs
) {
1140 wfree(view
->dragSourceProcs
);
1141 view
->dragSourceProcs
= NULL
;
1144 WMReleaseViewDragImage(view
);
1146 WMDeleteDragHandler(view
, dragImageHandler
, view
);