1 #include "../src/config.h"
4 #include <X11/cursorfont.h>
6 #include <X11/extensions/shape.h>
12 #define XDND_DESTINATION_RESPONSE_MAX_DELAY 10000
13 #define MIN_X_MOVE_OFFSET 5
14 #define MIN_Y_MOVE_OFFSET 5
15 #define MAX_SLIDEBACK_ITER 15
17 #define VERSION_INFO(dragInfo) dragInfo->protocolVersion
18 #define XDND_PROPERTY_FORMAT 32
19 #define XDND_ACTION_DESCRIPTION_FORMAT 8
21 #define XDND_SOURCE_INFO(dragInfo) dragInfo->sourceInfo
22 #define XDND_DEST_WIN(dragInfo) dragInfo->sourceInfo->destinationWindow
23 #define XDND_SOURCE_ACTION(dragInfo) dragInfo->sourceAction
24 #define XDND_DEST_ACTION(dragInfo) dragInfo->destinationAction
25 #define XDND_SOURCE_VIEW(dragInfo) dragInfo->sourceInfo->sourceView
26 #define XDND_SOURCE_STATE(dragInfo) dragInfo->sourceInfo->state
27 #define XDND_SELECTION_PROCS(dragInfo) dragInfo->sourceInfo->selectionProcs
28 #define XDND_DRAG_ICON(dragInfo) dragInfo->sourceInfo->icon
29 #define XDND_MOUSE_OFFSET(dragInfo) dragInfo->sourceInfo->mouseOffset
30 #define XDND_DRAG_CURSOR(dragInfo) dragInfo->sourceInfo->dragCursor
31 #define XDND_DRAG_ICON_POS(dragInfo) dragInfo->sourceInfo->imageLocation
32 #define XDND_NO_POS_ZONE(dragInfo) dragInfo->sourceInfo->noPositionMessageZone
33 #define XDND_TIMESTAMP(dragInfo) dragInfo->timestamp
34 #define XDND_3_TYPES(dragInfo) dragInfo->sourceInfo->firstThreeTypes
35 #define XDND_SOURCE_VIEW_STORED(dragInfo) dragInfo->sourceInfo != NULL \
36 && dragInfo->sourceInfo->sourceView != NULL
39 static WMHandlerID dndSourceTimer
= NULL
;
42 static void* idleState(WMView
*srcView
, XClientMessageEvent
*event
,
43 WMDraggingInfo
*info
);
44 static void* dropAllowedState(WMView
*srcView
, XClientMessageEvent
*event
,
45 WMDraggingInfo
*info
);
46 static void* finishDropState(WMView
*srcView
, XClientMessageEvent
*event
,
47 WMDraggingInfo
*info
);
51 stateName(W_DndState
*state
)
54 return "no state defined";
56 if (state
== idleState
)
59 if (state
== dropAllowedState
)
60 return "dropAllowedState";
62 if (state
== finishDropState
)
63 return "finishDropState";
65 return "unknown state";
71 sourceScreen(WMDraggingInfo
*info
)
73 return W_VIEW_SCREEN(XDND_SOURCE_VIEW(info
));
78 endDragProcess(WMDraggingInfo
*info
, Bool deposited
)
80 WMView
*view
= XDND_SOURCE_VIEW(info
);
81 WMScreen
*scr
= W_VIEW_SCREEN(XDND_SOURCE_VIEW(info
));
83 /* free selection handler while view exists */
84 WMDeleteSelectionHandler(view
,
85 scr
->xdndSelectionAtom
,
87 wfree(XDND_SELECTION_PROCS(info
));
89 if (XDND_DRAG_CURSOR(info
) != None
) {
90 XFreeCursor(scr
->display
,
91 XDND_DRAG_CURSOR(info
));
92 XDND_DRAG_CURSOR(info
) = None
;
95 if (view
->dragSourceProcs
->endedDrag
!= NULL
) {
96 /* this can destroy source view (with a "move" action for example) */
97 view
->dragSourceProcs
->endedDrag(view
, &XDND_DRAG_ICON_POS(info
),
101 /* clear remaining draggging infos */
102 wfree(XDND_SOURCE_INFO(info
));
103 XDND_SOURCE_INFO(info
) = NULL
;
107 /* ----- drag cursor ----- */
109 initDragCursor(WMDraggingInfo
*info
)
111 WMScreen
*scr
= sourceScreen(info
);
112 XColor cursorFgColor
, cursorBgColor
;
115 cursorFgColor
.red
= 0x4500;
116 cursorFgColor
.green
= 0xb000;
117 cursorFgColor
.blue
= 0x4500;
120 cursorBgColor
.red
= 0xffff;
121 cursorBgColor
.green
= 0xffff;
122 cursorBgColor
.blue
= 0xffff;
124 XDND_DRAG_CURSOR(info
) = XCreateFontCursor(scr
->display
, XC_left_ptr
);
125 XRecolorCursor(scr
->display
,
126 XDND_DRAG_CURSOR(info
),
130 XFlush(scr
->display
);
134 recolorCursor(WMDraggingInfo
*info
, Bool dropIsAllowed
)
136 WMScreen
*scr
= sourceScreen(info
);
139 XDefineCursor(scr
->display
,
141 XDND_DRAG_CURSOR(info
));
143 XDefineCursor(scr
->display
,
148 XFlush(scr
->display
);
150 /* ----- end of drag cursor ----- */
153 /* ----- selection procs ----- */
155 convertSelection(WMView
*view
, Atom selection
, Atom target
,
156 void *cdata
, Atom
*type
)
162 scr
= W_VIEW_SCREEN(view
);
163 typeName
= XGetAtomName(scr
->display
, target
);
167 if (view
->dragSourceProcs
->fetchDragData
!= NULL
) {
168 data
= view
->dragSourceProcs
->fetchDragData(
175 if (typeName
!= NULL
)
183 selectionLost(WMView
*view
, Atom selection
, void *cdata
)
185 wwarning("DND selection lost during drag operation...");
190 selectionDone(WMView
*view
, Atom selection
, Atom target
, void *cdata
)
193 printf("selection done\n");
196 /* ----- end of selection procs ----- */
199 /* ----- visual part ----- */
202 makeDragIcon(WMScreen
*scr
, WMPixmap
*pixmap
)
207 XSetWindowAttributes attribs
;
211 pixmap
= scr
->defaultObjectIcon
;
214 size
= WMGetPixmapSize(pixmap
);
215 pix
= pixmap
->pixmap
;
218 flags
= CWSaveUnder
|CWBackPixmap
|CWOverrideRedirect
|CWColormap
;
219 attribs
.save_under
= True
;
220 attribs
.background_pixmap
= pix
;
221 attribs
.override_redirect
= True
;
222 attribs
.colormap
= scr
->colormap
;
224 window
= XCreateWindow(scr
->display
, scr
->rootWin
, 0, 0, size
.width
,
225 size
.height
, 0, scr
->depth
, InputOutput
,
226 scr
->visual
, flags
, &attribs
);
231 XShapeCombineMask(scr
->display
, window
, ShapeBounding
, 0, 0, mask
,
241 slideWindow(Display
*dpy
, Window win
, int srcX
, int srcY
, int dstX
, int dstY
)
247 iterations
= WMIN(MAX_SLIDEBACK_ITER
,
248 WMAX(abs(dstX
-srcX
), abs(dstY
-srcY
)));
253 dx
= (double)(dstX
-srcX
)/iterations
;
254 dy
= (double)(dstY
-srcY
)/iterations
;
256 for (i
= 0; i
<= iterations
; i
++) {
257 XMoveWindow(dpy
, win
, x
, y
);
269 getInitialDragImageCoord(int viewCoord
, int mouseCoord
,
270 int viewSize
, int iconSize
)
272 if (iconSize
>= viewSize
) {
273 /* center icon coord on view */
274 return viewCoord
- iconSize
/2;
276 /* try to center icon on mouse pos */
278 if (mouseCoord
- iconSize
/2 <= viewCoord
)
279 /* if icon was centered on mouse, it would be off view
280 thus, put icon left (resp. top) side
281 at view (resp. top) side */
284 else if (mouseCoord
+ iconSize
/2 >= viewCoord
+ viewSize
)
285 /* if icon was centered on mouse, it would be off view
286 thus, put icon right (resp. bottom) side
287 at view right (resp. bottom) side */
288 return viewCoord
+ viewSize
- iconSize
;
291 return mouseCoord
- iconSize
/2;
297 initDragImagePos(WMView
*view
, WMDraggingInfo
*info
, XEvent
*event
)
299 WMSize iconSize
= WMGetPixmapSize(view
->dragImage
);
300 WMSize viewSize
= WMGetViewSize(view
);
304 XTranslateCoordinates(W_VIEW_SCREEN(view
)->display
,
305 WMViewXID(view
), W_VIEW_SCREEN(view
)->rootWin
,
306 0, 0, &(viewPos
.x
), &(viewPos
.y
),
310 XDND_DRAG_ICON_POS(info
).x
=
311 getInitialDragImageCoord(viewPos
.x
, event
->xmotion
.x_root
,
312 viewSize
.width
, iconSize
.width
);
314 XDND_DRAG_ICON_POS(info
).y
=
315 getInitialDragImageCoord(viewPos
.y
, event
->xmotion
.y_root
,
316 viewSize
.height
, iconSize
.height
);
318 /* set mouse offset relative to icon */
319 XDND_MOUSE_OFFSET(info
).x
=
320 event
->xmotion
.x_root
- XDND_DRAG_ICON_POS(info
).x
;
321 XDND_MOUSE_OFFSET(info
).y
=
322 event
->xmotion
.y_root
- XDND_DRAG_ICON_POS(info
).y
;
327 refreshDragImage(WMView
*view
, WMDraggingInfo
*info
)
329 WMScreen
*scr
= W_VIEW_SCREEN(view
);
331 XMoveWindow(scr
->display
, XDND_DRAG_ICON(info
),
332 XDND_DRAG_ICON_POS(info
).x
,
333 XDND_DRAG_ICON_POS(info
).y
);
338 startDragImage(WMView
*view
, WMDraggingInfo
*info
, XEvent
* event
)
340 WMScreen
*scr
= W_VIEW_SCREEN(view
);
342 XDND_DRAG_ICON(info
) = makeDragIcon(scr
, view
->dragImage
);
343 initDragImagePos(view
, info
, event
);
344 refreshDragImage(view
, info
);
345 XMapRaised(scr
->display
, XDND_DRAG_ICON(info
));
347 initDragCursor(info
);
352 endDragImage(WMDraggingInfo
*info
, Bool slideBack
)
354 WMView
*view
= XDND_SOURCE_VIEW(info
);
355 Display
*dpy
= W_VIEW_SCREEN(view
)->display
;
361 XTranslateCoordinates(W_VIEW_SCREEN(view
)->display
,
362 WMViewXID(view
), W_VIEW_SCREEN(view
)->rootWin
,
363 0, 0, &(toLocation
.x
), &(toLocation
.y
),
366 slideWindow(dpy
, XDND_DRAG_ICON(info
),
367 XDND_DRAG_ICON_POS(info
).x
,
368 XDND_DRAG_ICON_POS(info
).y
,
373 XDestroyWindow(dpy
, XDND_DRAG_ICON(info
));
376 /* ----- end of visual part ----- */
379 /* ----- messages ----- */
381 /* send a DnD message to the destination window */
383 sendDnDClientMessage(WMDraggingInfo
*info
, Atom message
,
389 Display
*dpy
= sourceScreen(info
)->display
;
390 Window srcWin
= WMViewXID(XDND_SOURCE_VIEW(info
));
391 Window destWin
= XDND_DEST_WIN(info
);
393 if (! W_SendDnDClientMessage(dpy
,
402 recolorCursor(info
, False
);
403 endDragImage(info
, True
);
404 endDragProcess(info
, False
);
413 sendEnterMessage(WMDraggingInfo
*info
)
415 WMScreen
*scr
= sourceScreen(info
);
418 data1
= (VERSION_INFO(info
) << 24)|1; /* 1: support of type list */
420 return sendDnDClientMessage(info
, scr
->xdndEnterAtom
,
422 XDND_3_TYPES(info
)[0],
423 XDND_3_TYPES(info
)[1],
424 XDND_3_TYPES(info
)[2]);
429 sendPositionMessage(WMDraggingInfo
*info
, WMPoint
*mousePos
)
431 WMScreen
*scr
= sourceScreen(info
);
432 WMRect
*noPosZone
= &(XDND_NO_POS_ZONE(info
));
434 if (noPosZone
->size
.width
!= 0 || noPosZone
->size
.height
!= 0) {
435 if (mousePos
->x
< noPosZone
->pos
.x
436 || mousePos
->x
> (noPosZone
->pos
.x
+ noPosZone
->size
.width
)
437 || mousePos
->y
< noPosZone
->pos
.y
438 || mousePos
->y
> (noPosZone
->pos
.y
+ noPosZone
->size
.width
)) {
439 /* send position if out of zone defined by destination */
440 return sendDnDClientMessage(info
, scr
->xdndPositionAtom
,
442 mousePos
->x
<<16|mousePos
->y
,
443 XDND_TIMESTAMP(info
),
444 XDND_SOURCE_ACTION(info
));
447 /* send position on each move */
448 return sendDnDClientMessage(info
, scr
->xdndPositionAtom
,
450 mousePos
->x
<<16|mousePos
->y
,
451 XDND_TIMESTAMP(info
),
452 XDND_SOURCE_ACTION(info
));
458 sendLeaveMessage(WMDraggingInfo
*info
)
460 WMScreen
*scr
= sourceScreen(info
);
462 return sendDnDClientMessage(info
, scr
->xdndLeaveAtom
,
468 sendDropMessage(WMDraggingInfo
*info
)
470 WMScreen
*scr
= sourceScreen(info
);
472 return sendDnDClientMessage(info
,
475 XDND_TIMESTAMP(info
),
479 /* ----- end of messages ----- */
483 getTypeAtomList(WMScreen
*scr
, WMView
*view
, int* count
)
489 types
= view
->dragSourceProcs
->dropDataTypes(view
);
492 *count
= WMGetArrayItemCount(types
);
494 typeAtoms
= wmalloc((*count
)*sizeof(Atom
));
495 for (i
=0; i
< *count
; i
++) {
496 typeAtoms
[i
] = XInternAtom(scr
->display
,
497 WMGetFromArray(types
, i
),
501 /* WMFreeArray(types); */
505 /* WMFreeArray(types); */
509 typeAtoms
= wmalloc(sizeof(Atom
));
517 registerDropTypes(WMScreen
*scr
, WMView
*view
, WMDraggingInfo
*info
)
522 typeList
= getTypeAtomList(scr
, view
, &count
);
524 /* store the first 3 types */
525 for(i
=0; i
< 3 && i
< count
; i
++)
526 XDND_3_TYPES(info
)[i
] = typeList
[i
];
529 XDND_3_TYPES(info
)[i
] = None
;
532 /* store the entire type list */
533 XChangeProperty(scr
->display
,
535 scr
->xdndTypeListAtom
,
537 XDND_PROPERTY_FORMAT
,
539 (unsigned char*) typeList
,
545 registerOperationList(WMScreen
*scr
, WMView
*view
, WMArray
* operationArray
)
548 WMDragOperationType operation
;
549 int count
= WMGetArrayItemCount(operationArray
);
552 actionList
= wmalloc(sizeof(Atom
)*count
);
554 for(i
=0; i
< count
; i
++) {
555 operation
= WMGetDragOperationItemType(
556 WMGetFromArray(operationArray
, i
));
557 actionList
[i
] = W_OperationToAction(scr
, operation
);
560 XChangeProperty(scr
->display
,
562 scr
->xdndActionListAtom
,
564 XDND_PROPERTY_FORMAT
,
566 (unsigned char*) actionList
,
571 registerDescriptionList(WMScreen
*scr
, WMView
*view
, WMArray
* operationArray
)
573 char *text
, *textListItem
, *textList
;
574 int count
= WMGetArrayItemCount(operationArray
);
578 /* size of XA_STRING info */
579 for(i
=0; i
< count
; i
++) {
580 size
+= strlen(WMGetDragOperationItemText(
581 WMGetFromArray(operationArray
, i
))) + 1; /* +1 = +NULL */
584 /* create text list */
585 textList
= wmalloc(size
);
586 textListItem
= textList
;
588 for(i
=0; i
< count
; i
++) {
589 text
= WMGetDragOperationItemText(WMGetFromArray(operationArray
, i
));
590 strcpy(textListItem
, text
);
592 /* to next text offset */
593 textListItem
= &(textListItem
[strlen(textListItem
) + 1]);
596 XChangeProperty(scr
->display
,
598 scr
->xdndActionDescriptionAtom
,
600 XDND_ACTION_DESCRIPTION_FORMAT
,
602 (unsigned char*) textList
,
606 /* called if wanted operation is WDOperationAsk */
608 registerSupportedOperations(WMView
*view
)
610 WMScreen
*scr
= W_VIEW_SCREEN(view
);
611 WMArray
* operationArray
;
613 operationArray
= view
->dragSourceProcs
->askedOperations(view
);
615 registerOperationList(scr
, view
, operationArray
);
616 registerDescriptionList(scr
, view
, operationArray
);
618 /* WMFreeArray(operationArray); */
623 initSourceDragInfo(WMView
*sourceView
, WMDraggingInfo
*info
)
627 XDND_SOURCE_INFO(info
) = (W_DragSourceInfo
*) wmalloc(sizeof(W_DragSourceInfo
));
629 XDND_SOURCE_VIEW(info
) = sourceView
;
630 XDND_DEST_WIN(info
) = None
;
631 XDND_DRAG_ICON(info
) = None
;
633 XDND_SOURCE_ACTION(info
) = W_OperationToAction(
634 W_VIEW_SCREEN(sourceView
),
635 sourceView
->dragSourceProcs
->wantedDropOperation(sourceView
));
637 XDND_DEST_ACTION(info
) = None
;
639 XDND_SOURCE_STATE(info
) = idleState
;
641 emptyZone
.pos
= wmkpoint(0, 0);
642 emptyZone
.size
= wmksize(0, 0);
643 XDND_NO_POS_ZONE(info
) = emptyZone
;
648 Returned array is destroyed after dropDataTypes call
651 defDropDataTypes(WMView
*self
)
657 static WMDragOperationType
658 defWantedDropOperation(WMView
*self
)
660 return WDOperationNone
;
665 Must be defined if wantedDropOperation return WDOperationAsk
667 Return a WMDragOperationItem array (destroyed after call).
668 A WMDragOperationItem links a label to an operation.
670 defAskedOperations(WMView *self); */
674 defAcceptDropOperation(WMView
*self
, WMDragOperationType allowedOperation
)
681 defBeganDrag(WMView
*self
, WMPoint
*point
)
687 defEndedDrag(WMView
*self
, WMPoint
*point
, Bool deposited
)
693 Returned data is not destroyed
696 defFetchDragData(WMView
*self
, char *type
)
703 WMSetViewDragSourceProcs(WMView
*view
, WMDragSourceProcs
*procs
)
705 if (view
->dragSourceProcs
)
706 wfree(view
->dragSourceProcs
);
707 view
->dragSourceProcs
= wmalloc(sizeof(WMDragSourceProcs
));
709 *view
->dragSourceProcs
= *procs
;
711 if (procs
->dropDataTypes
== NULL
)
712 view
->dragSourceProcs
->dropDataTypes
= defDropDataTypes
;
714 if (procs
->wantedDropOperation
== NULL
)
715 view
->dragSourceProcs
->wantedDropOperation
= defWantedDropOperation
;
718 Note: askedOperations can be NULL, if wantedDropOperation never returns
722 if (procs
->acceptDropOperation
== NULL
)
723 view
->dragSourceProcs
->acceptDropOperation
= defAcceptDropOperation
;
725 if (procs
->beganDrag
== NULL
)
726 view
->dragSourceProcs
->beganDrag
= defBeganDrag
;
728 if (procs
->endedDrag
== NULL
)
729 view
->dragSourceProcs
->endedDrag
= defEndedDrag
;
731 if (procs
->fetchDragData
== NULL
)
732 view
->dragSourceProcs
->fetchDragData
= defFetchDragData
;
737 isXdndAware(WMScreen
*scr
, Window win
)
741 unsigned long count
, remain
;
742 unsigned char *winXdndVersion
;
743 unsigned char *proxy
;
748 XGetWindowProperty(scr
->display
, win
, scr
->xdndAwareAtom
,
749 0, 1, False
, XA_ATOM
, &type
, &format
,
750 &count
, &remain
, &winXdndVersion
);
753 || format
!= XDND_PROPERTY_FORMAT
754 || count
== 0 || !winXdndVersion
) {
756 XFree(winXdndVersion
);
760 XFree(winXdndVersion
);
761 return (count
== 1); /* xdnd version is set */
766 windowChildren(Display
*dpy
, Window win
, unsigned *nchildren
)
771 if (!XQueryTree(dpy
, win
, &foo
, &bar
, &children
, nchildren
)) {
779 lookForAwareWindow(WMScreen
*scr
, WMPoint
*mousePos
, Window win
)
784 /* Since xdnd v3, only the toplevel window should be aware */
785 if (isXdndAware(scr
, win
))
788 /* inspect child under pointer */
789 if (XTranslateCoordinates(scr
->display
, scr
->rootWin
, win
,
790 mousePos
->x
, mousePos
->y
, &tmpx
, &tmpy
,
795 return lookForAwareWindow(scr
, mousePos
, child
);
803 findDestination(WMDraggingInfo
*info
, WMPoint
*mousePos
)
805 WMScreen
*scr
= sourceScreen(info
);
807 Window
*children
= windowChildren(scr
->display
, scr
->rootWin
, &nchildren
);
809 XWindowAttributes attr
;
811 if (isXdndAware(scr
, scr
->rootWin
))
814 /* exclude drag icon (and upper) from search */
815 for (i
= nchildren
-1; i
>= 0; i
--) {
816 if (children
[i
] == XDND_DRAG_ICON(info
)) {
823 /* root window has no child under drag icon, and is not xdnd aware. */
827 /* inspecting children, from upper to lower */
828 for (; i
>= 0; i
--) {
829 if (XGetWindowAttributes(scr
->display
, children
[i
], &attr
)
830 && attr
.map_state
== IsViewable
831 && mousePos
->x
>= attr
.x
832 && mousePos
->y
>= attr
.y
833 && mousePos
->x
< attr
.x
+ attr
.width
834 && mousePos
->y
< attr
.y
+ attr
.height
) {
835 return lookForAwareWindow(scr
, mousePos
, children
[i
]);
839 /* No child window under drag pointer */
845 initMotionProcess(WMView
*view
, WMDraggingInfo
*info
,
846 XEvent
*event
, WMPoint
*startLocation
)
848 WMScreen
*scr
= W_VIEW_SCREEN(view
);
850 /* take ownership of XdndSelection */
851 XDND_SELECTION_PROCS(info
) =
852 (WMSelectionProcs
*) wmalloc(sizeof(WMSelectionProcs
));
853 XDND_SELECTION_PROCS(info
)->convertSelection
= convertSelection
;
854 XDND_SELECTION_PROCS(info
)->selectionLost
= selectionLost
;
855 XDND_SELECTION_PROCS(info
)->selectionDone
= selectionDone
;
856 XDND_TIMESTAMP(info
) = event
->xmotion
.time
;
858 if (!WMCreateSelectionHandler(view
, scr
->xdndSelectionAtom
,
860 XDND_SELECTION_PROCS(info
), NULL
)) {
861 wwarning("could not get ownership or DND selection");
865 registerDropTypes(scr
, view
, info
);
867 if (XDND_SOURCE_ACTION(info
) == W_VIEW_SCREEN(view
)->xdndActionAsk
)
868 registerSupportedOperations(view
);
870 if (view
->dragSourceProcs
->beganDrag
!= NULL
) {
871 view
->dragSourceProcs
->beganDrag(view
, startLocation
);
877 processMotion(WMDraggingInfo
*info
, Window windowUnderDrag
, WMPoint
*mousePos
)
879 WMScreen
*scr
= sourceScreen(info
);
880 Window newDestination
= findDestination(info
, mousePos
);
882 W_DragSourceStopTimer();
884 if (newDestination
!= XDND_DEST_WIN(info
)) {
885 recolorCursor(info
, False
);
887 if (XDND_DEST_WIN(info
) != None
) {
888 /* leaving a xdnd window */
889 sendLeaveMessage(info
);
892 XDND_DEST_WIN(info
) = newDestination
;
893 XDND_SOURCE_STATE(info
) = idleState
;
894 XDND_DEST_ACTION(info
) = None
;
895 XDND_NO_POS_ZONE(info
).size
.width
= 0;
896 XDND_NO_POS_ZONE(info
).size
.height
= 0;
898 if (newDestination
!= None
) {
899 /* entering a xdnd window */
900 if (! sendEnterMessage(info
)) {
901 XDND_DEST_WIN(info
) = None
;
905 W_DragSourceStartTimer(info
);
908 if (XDND_DEST_WIN(info
) != None
) {
909 if (! sendPositionMessage(info
, mousePos
)) {
910 XDND_DEST_WIN(info
) = None
;
914 W_DragSourceStartTimer(info
);
921 processButtonRelease(WMDraggingInfo
*info
)
923 if (XDND_SOURCE_STATE(info
) == dropAllowedState
) {
925 W_DragSourceStopTimer();
927 if (! sendDropMessage(info
))
930 W_DragSourceStartTimer(info
);
933 if (XDND_DEST_WIN(info
) != None
)
934 sendLeaveMessage(info
);
936 W_DragSourceStopTimer();
943 WMIsDraggingFromView(WMView
*view
)
945 WMDraggingInfo
*info
= &W_VIEW_SCREEN(view
)->dragInfo
;
947 return ( XDND_SOURCE_INFO(info
) != NULL
948 && XDND_SOURCE_STATE(info
) != finishDropState
);
949 /* return W_VIEW_SCREEN(view)->dragInfo.sourceInfo != NULL; */
954 WMDragImageFromView(WMView
*view
, XEvent
*event
)
956 WMDraggingInfo
*info
= &W_VIEW_SCREEN(view
)->dragInfo
;
957 WMPoint mouseLocation
;
959 switch(event
->type
) {
961 if (event
->xbutton
.button
== Button1
) {
964 XPeekEvent(event
->xbutton
.display
, &nextEvent
);
966 /* Initialize only if a drag really begins (avoid clicks) */
967 if (nextEvent
.type
== MotionNotify
) {
968 initSourceDragInfo(view
, info
);
975 if (WMIsDraggingFromView(view
)) {
976 Bool dropBegan
= processButtonRelease(info
);
978 recolorCursor(info
, False
);
980 endDragImage(info
, False
);
981 XDND_SOURCE_STATE(info
) = finishDropState
;
984 endDragImage(info
, True
);
985 endDragProcess(info
,False
);
991 if (WMIsDraggingFromView(view
)) {
992 mouseLocation
= wmkpoint(event
->xmotion
.x_root
,
993 event
->xmotion
.y_root
);
995 if (abs(XDND_DRAG_ICON_POS(info
).x
- mouseLocation
.x
) >=
997 || abs(XDND_DRAG_ICON_POS(info
).y
- mouseLocation
.y
) >=
999 if (XDND_DRAG_ICON(info
) == None
) {
1000 initMotionProcess(view
, info
, event
, &mouseLocation
);
1001 startDragImage(view
, info
, event
);
1003 XDND_DRAG_ICON_POS(info
).x
=
1004 mouseLocation
.x
- XDND_MOUSE_OFFSET(info
).x
;
1005 XDND_DRAG_ICON_POS(info
).y
=
1006 mouseLocation
.y
- XDND_MOUSE_OFFSET(info
).y
;
1008 refreshDragImage(view
, info
);
1010 event
->xmotion
.window
,
1020 /* Minimal mouse events handler: no right or double-click detection,
1021 only drag is supported */
1023 dragImageHandler(XEvent
*event
, void *cdata
)
1025 WMView
*view
= (WMView
*)cdata
;
1027 WMDragImageFromView(view
, event
);
1031 /* ----- source states ----- */
1035 traceStatusMsg(Display
*dpy
, XClientMessageEvent
*statusEvent
)
1037 printf("Xdnd status message:\n");
1039 if (statusEvent
->data
.l
[1] & 0x2UL
)
1040 printf("send position on every move\n");
1043 x
= statusEvent
->data
.l
[2] >> 16;
1044 y
= statusEvent
->data
.l
[2] & 0xFFFFL
;
1045 w
= statusEvent
->data
.l
[3] >> 16;
1046 h
= statusEvent
->data
.l
[3] & 0xFFFFL
;
1048 printf("send position out of ((%d,%d) , (%d,%d))\n",
1052 if (statusEvent
->data
.l
[1] & 0x1L
)
1053 printf("allowed action: %s\n",
1054 XGetAtomName(dpy
, statusEvent
->data
.l
[4]));
1056 printf("no action allowed\n");
1061 storeDropAction(WMDraggingInfo
*info
, Atom destAction
)
1063 WMView
* sourceView
= XDND_SOURCE_VIEW(info
);
1064 WMScreen
*scr
= W_VIEW_SCREEN(sourceView
);
1066 if (sourceView
->dragSourceProcs
->acceptDropOperation
!= NULL
) {
1067 if (sourceView
->dragSourceProcs
->acceptDropOperation(
1069 W_ActionToOperation(scr
, destAction
)))
1070 XDND_DEST_ACTION(info
) = destAction
;
1072 XDND_DEST_ACTION(info
) = None
;
1074 XDND_DEST_ACTION(info
) = destAction
;
1080 storeStatusMessageInfos(WMDraggingInfo
*info
, XClientMessageEvent
*statusEvent
)
1082 WMRect
* noPosZone
= &(XDND_NO_POS_ZONE(info
));
1086 traceStatusMsg(sourceScreen(info
)->display
, statusEvent
);
1089 if (statusEvent
->data
.l
[1] & 0x2UL
) {
1090 /* bit 1 set: destination wants position messages on every move */
1091 noPosZone
->size
.width
= 0;
1092 noPosZone
->size
.height
= 0;
1094 /* don't send another position message while in given rectangle */
1095 noPosZone
->pos
.x
= statusEvent
->data
.l
[2] >> 16;
1096 noPosZone
->pos
.y
= statusEvent
->data
.l
[2] & 0xFFFFL
;
1097 noPosZone
->size
.width
= statusEvent
->data
.l
[3] >> 16;
1098 noPosZone
->size
.height
= statusEvent
->data
.l
[3] & 0xFFFFL
;
1101 if ((statusEvent
->data
.l
[1] & 0x1L
) || statusEvent
->data
.l
[4] != None
) {
1102 /* destination accept drop */
1103 storeDropAction(info
, statusEvent
->data
.l
[4]);
1105 XDND_DEST_ACTION(info
) = None
;
1111 idleState(WMView
*view
, XClientMessageEvent
*event
, WMDraggingInfo
*info
)
1114 Atom destMsg
= event
->message_type
;
1116 scr
= W_VIEW_SCREEN(view
);
1118 if (destMsg
== scr
->xdndStatusAtom
) {
1119 storeStatusMessageInfos(info
, event
);
1121 if (XDND_DEST_ACTION(info
) != None
) {
1122 recolorCursor(info
, True
);
1123 W_DragSourceStartTimer(info
);
1124 return dropAllowedState
;
1127 recolorCursor(info
, False
);
1132 if (destMsg
== scr
->xdndFinishedAtom
) {
1133 wwarning("received xdndFinishedAtom before drop began");
1136 W_DragSourceStartTimer(info
);
1142 dropAllowedState(WMView
*view
, XClientMessageEvent
*event
, WMDraggingInfo
*info
)
1144 WMScreen
*scr
= W_VIEW_SCREEN(view
);
1145 Atom destMsg
= event
->message_type
;
1147 if (destMsg
== scr
->xdndStatusAtom
) {
1148 storeStatusMessageInfos(info
, event
);
1150 if (XDND_DEST_ACTION(info
) == None
) {
1152 recolorCursor(info
, False
);
1157 W_DragSourceStartTimer(info
);
1158 return dropAllowedState
;
1163 finishDropState(WMView
*view
, XClientMessageEvent
*event
, WMDraggingInfo
*info
)
1165 WMScreen
*scr
= W_VIEW_SCREEN(view
);
1166 Atom destMsg
= event
->message_type
;
1168 if (destMsg
== scr
->xdndFinishedAtom
) {
1169 endDragProcess(info
, True
);
1173 W_DragSourceStartTimer(info
);
1174 return finishDropState
;
1176 /* ----- end of source states ----- */
1179 /* ----- source timer ----- */
1181 dragSourceResponseTimeOut(void *source
)
1183 WMView
*view
= (WMView
*)source
;
1184 WMDraggingInfo
*info
= &(W_VIEW_SCREEN(view
)->dragInfo
);
1186 wwarning("delay for drag destination response expired");
1187 sendLeaveMessage(info
);
1189 recolorCursor(info
, False
);
1190 if (XDND_SOURCE_STATE(info
) == finishDropState
) {
1192 endDragImage(info
, True
);
1193 endDragProcess(info
, False
);
1195 XDND_SOURCE_STATE(info
) = idleState
;
1200 W_DragSourceStopTimer()
1202 if (dndSourceTimer
!= NULL
) {
1203 WMDeleteTimerHandler(dndSourceTimer
);
1204 dndSourceTimer
= NULL
;
1209 W_DragSourceStartTimer(WMDraggingInfo
*info
)
1211 W_DragSourceStopTimer();
1213 dndSourceTimer
= WMAddTimerHandler(
1214 XDND_DESTINATION_RESPONSE_MAX_DELAY
,
1215 dragSourceResponseTimeOut
,
1216 XDND_SOURCE_VIEW(info
));
1219 /* ----- End of Destination timer ----- */
1223 W_DragSourceStateHandler(WMDraggingInfo
*info
, XClientMessageEvent
*event
)
1226 W_DndState
* newState
;
1228 if (XDND_SOURCE_VIEW_STORED(info
)) {
1229 view
= XDND_SOURCE_VIEW(info
);
1232 printf("current source state: %s\n",
1233 stateName(XDND_SOURCE_STATE(info
)));
1236 newState
= (W_DndState
*) XDND_SOURCE_STATE(info
)(view
, event
, info
);
1240 printf("new source state: %s\n", stateName(newState
));
1243 if (newState
!= NULL
)
1244 XDND_SOURCE_STATE(info
) = newState
;
1245 /* else drop finished, and info has been flushed */
1250 void WMSetViewDragImage(WMView
* view
, WMPixmap
*dragImage
)
1252 if (view
->dragImage
!= NULL
)
1253 WMReleasePixmap(view
->dragImage
);
1255 view
->dragImage
= WMRetainPixmap(dragImage
);
1259 void WMReleaseViewDragImage(WMView
* view
)
1261 if (view
->dragImage
!= NULL
)
1262 WMReleasePixmap(view
->dragImage
);
1266 /* Create a drag handler, associating drag event masks with dragEventProc */
1268 WMCreateDragHandler(WMView
*view
, WMEventProc
*dragEventProc
, void *clientData
)
1270 WMCreateEventHandler(view
,
1271 ButtonPressMask
|ButtonReleaseMask
|Button1MotionMask
,
1272 dragEventProc
, clientData
);
1277 WMDeleteDragHandler(WMView
*view
, WMEventProc
*dragEventProc
, void *clientData
)
1279 WMDeleteEventHandler(view
,
1280 ButtonPressMask
|ButtonReleaseMask
|Button1MotionMask
,
1281 dragEventProc
, clientData
);
1285 /* set default drag handler for view */
1287 WMSetViewDraggable(WMView
*view
, WMDragSourceProcs
*dragSourceProcs
,
1288 WMPixmap
*dragImage
)
1290 wassertr(dragImage
!= NULL
);
1291 view
->dragImage
= WMRetainPixmap(dragImage
);
1293 WMSetViewDragSourceProcs(view
, dragSourceProcs
);
1295 WMCreateDragHandler(view
, dragImageHandler
, view
);
1300 WMUnsetViewDraggable(WMView
*view
)
1302 if (view
->dragSourceProcs
) {
1303 wfree(view
->dragSourceProcs
);
1304 view
->dragSourceProcs
= NULL
;
1307 WMReleaseViewDragImage(view
);
1309 WMDeleteDragHandler(view
, dragImageHandler
, view
);