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]);
428 // this functon doesn't return something in all cases.
429 // control reaches end of non-void function. fix this -Dan
431 sendPositionMessage(WMDraggingInfo
*info
, WMPoint
*mousePos
)
433 WMScreen
*scr
= sourceScreen(info
);
434 WMRect
*noPosZone
= &(XDND_NO_POS_ZONE(info
));
436 if (noPosZone
->size
.width
!= 0 || noPosZone
->size
.height
!= 0) {
437 if (mousePos
->x
< noPosZone
->pos
.x
438 || mousePos
->x
> (noPosZone
->pos
.x
+ noPosZone
->size
.width
)
439 || mousePos
->y
< noPosZone
->pos
.y
440 || mousePos
->y
> (noPosZone
->pos
.y
+ noPosZone
->size
.width
)) {
441 /* send position if out of zone defined by destination */
442 return sendDnDClientMessage(info
, scr
->xdndPositionAtom
,
444 mousePos
->x
<<16|mousePos
->y
,
445 XDND_TIMESTAMP(info
),
446 XDND_SOURCE_ACTION(info
));
449 /* send position on each move */
450 return sendDnDClientMessage(info
, scr
->xdndPositionAtom
,
452 mousePos
->x
<<16|mousePos
->y
,
453 XDND_TIMESTAMP(info
),
454 XDND_SOURCE_ACTION(info
));
460 sendLeaveMessage(WMDraggingInfo
*info
)
462 WMScreen
*scr
= sourceScreen(info
);
464 return sendDnDClientMessage(info
, scr
->xdndLeaveAtom
,
470 sendDropMessage(WMDraggingInfo
*info
)
472 WMScreen
*scr
= sourceScreen(info
);
474 return sendDnDClientMessage(info
,
477 XDND_TIMESTAMP(info
),
481 /* ----- end of messages ----- */
485 getTypeAtomList(WMScreen
*scr
, WMView
*view
, int* count
)
491 types
= view
->dragSourceProcs
->dropDataTypes(view
);
494 *count
= WMGetArrayItemCount(types
);
496 typeAtoms
= wmalloc((*count
)*sizeof(Atom
));
497 for (i
=0; i
< *count
; i
++) {
498 typeAtoms
[i
] = XInternAtom(scr
->display
,
499 WMGetFromArray(types
, i
),
503 /* WMFreeArray(types); */
507 /* WMFreeArray(types); */
511 typeAtoms
= wmalloc(sizeof(Atom
));
519 registerDropTypes(WMScreen
*scr
, WMView
*view
, WMDraggingInfo
*info
)
524 typeList
= getTypeAtomList(scr
, view
, &count
);
526 /* store the first 3 types */
527 for(i
=0; i
< 3 && i
< count
; i
++)
528 XDND_3_TYPES(info
)[i
] = typeList
[i
];
531 XDND_3_TYPES(info
)[i
] = None
;
534 /* store the entire type list */
535 XChangeProperty(scr
->display
,
537 scr
->xdndTypeListAtom
,
539 XDND_PROPERTY_FORMAT
,
541 (unsigned char*) typeList
,
547 registerOperationList(WMScreen
*scr
, WMView
*view
, WMArray
* operationArray
)
550 WMDragOperationType operation
;
551 int count
= WMGetArrayItemCount(operationArray
);
554 actionList
= wmalloc(sizeof(Atom
)*count
);
556 for(i
=0; i
< count
; i
++) {
557 operation
= WMGetDragOperationItemType(
558 WMGetFromArray(operationArray
, i
));
559 actionList
[i
] = W_OperationToAction(scr
, operation
);
562 XChangeProperty(scr
->display
,
564 scr
->xdndActionListAtom
,
566 XDND_PROPERTY_FORMAT
,
568 (unsigned char*) actionList
,
573 registerDescriptionList(WMScreen
*scr
, WMView
*view
, WMArray
* operationArray
)
575 char *text
, *textListItem
, *textList
;
576 int count
= WMGetArrayItemCount(operationArray
);
580 /* size of XA_STRING info */
581 for(i
=0; i
< count
; i
++) {
582 size
+= strlen(WMGetDragOperationItemText(
583 WMGetFromArray(operationArray
, i
))) + 1; /* +1 = +NULL */
586 /* create text list */
587 textList
= wmalloc(size
);
588 textListItem
= textList
;
590 for(i
=0; i
< count
; i
++) {
591 text
= WMGetDragOperationItemText(WMGetFromArray(operationArray
, i
));
592 strcpy(textListItem
, text
);
594 /* to next text offset */
595 textListItem
= &(textListItem
[strlen(textListItem
) + 1]);
598 XChangeProperty(scr
->display
,
600 scr
->xdndActionDescriptionAtom
,
602 XDND_ACTION_DESCRIPTION_FORMAT
,
604 (unsigned char*) textList
,
608 /* called if wanted operation is WDOperationAsk */
610 registerSupportedOperations(WMView
*view
)
612 WMScreen
*scr
= W_VIEW_SCREEN(view
);
613 WMArray
* operationArray
;
615 operationArray
= view
->dragSourceProcs
->askedOperations(view
);
617 registerOperationList(scr
, view
, operationArray
);
618 registerDescriptionList(scr
, view
, operationArray
);
620 /* WMFreeArray(operationArray); */
625 initSourceDragInfo(WMView
*sourceView
, WMDraggingInfo
*info
)
629 XDND_SOURCE_INFO(info
) = (W_DragSourceInfo
*) wmalloc(sizeof(W_DragSourceInfo
));
631 XDND_SOURCE_VIEW(info
) = sourceView
;
632 XDND_DEST_WIN(info
) = None
;
633 XDND_DRAG_ICON(info
) = None
;
635 XDND_SOURCE_ACTION(info
) = W_OperationToAction(
636 W_VIEW_SCREEN(sourceView
),
637 sourceView
->dragSourceProcs
->wantedDropOperation(sourceView
));
639 XDND_DEST_ACTION(info
) = None
;
641 XDND_SOURCE_STATE(info
) = idleState
;
643 emptyZone
.pos
= wmkpoint(0, 0);
644 emptyZone
.size
= wmksize(0, 0);
645 XDND_NO_POS_ZONE(info
) = emptyZone
;
650 Returned array is destroyed after dropDataTypes call
653 defDropDataTypes(WMView
*self
)
659 static WMDragOperationType
660 defWantedDropOperation(WMView
*self
)
662 return WDOperationNone
;
667 Must be defined if wantedDropOperation return WDOperationAsk
669 Return a WMDragOperationItem array (destroyed after call).
670 A WMDragOperationItem links a label to an operation.
672 defAskedOperations(WMView *self); */
676 defAcceptDropOperation(WMView
*self
, WMDragOperationType allowedOperation
)
683 defBeganDrag(WMView
*self
, WMPoint
*point
)
689 defEndedDrag(WMView
*self
, WMPoint
*point
, Bool deposited
)
695 Returned data is not destroyed
698 defFetchDragData(WMView
*self
, char *type
)
705 WMSetViewDragSourceProcs(WMView
*view
, WMDragSourceProcs
*procs
)
707 if (view
->dragSourceProcs
)
708 wfree(view
->dragSourceProcs
);
709 view
->dragSourceProcs
= wmalloc(sizeof(WMDragSourceProcs
));
711 *view
->dragSourceProcs
= *procs
;
713 if (procs
->dropDataTypes
== NULL
)
714 view
->dragSourceProcs
->dropDataTypes
= defDropDataTypes
;
716 if (procs
->wantedDropOperation
== NULL
)
717 view
->dragSourceProcs
->wantedDropOperation
= defWantedDropOperation
;
720 Note: askedOperations can be NULL, if wantedDropOperation never returns
724 if (procs
->acceptDropOperation
== NULL
)
725 view
->dragSourceProcs
->acceptDropOperation
= defAcceptDropOperation
;
727 if (procs
->beganDrag
== NULL
)
728 view
->dragSourceProcs
->beganDrag
= defBeganDrag
;
730 if (procs
->endedDrag
== NULL
)
731 view
->dragSourceProcs
->endedDrag
= defEndedDrag
;
733 if (procs
->fetchDragData
== NULL
)
734 view
->dragSourceProcs
->fetchDragData
= defFetchDragData
;
739 isXdndAware(WMScreen
*scr
, Window win
)
743 unsigned long count
, remain
;
744 unsigned char *winXdndVersion
;
749 XGetWindowProperty(scr
->display
, win
, scr
->xdndAwareAtom
,
750 0, 1, False
, XA_ATOM
, &type
, &format
,
751 &count
, &remain
, &winXdndVersion
);
754 || format
!= XDND_PROPERTY_FORMAT
755 || count
== 0 || !winXdndVersion
) {
757 XFree(winXdndVersion
);
761 XFree(winXdndVersion
);
762 return (count
== 1); /* xdnd version is set */
767 windowChildren(Display
*dpy
, Window win
, unsigned *nchildren
)
772 if (!XQueryTree(dpy
, win
, &foo
, &bar
, &children
, nchildren
)) {
780 lookForAwareWindow(WMScreen
*scr
, WMPoint
*mousePos
, Window win
)
785 /* Since xdnd v3, only the toplevel window should be aware */
786 if (isXdndAware(scr
, win
))
789 /* inspect child under pointer */
790 if (XTranslateCoordinates(scr
->display
, scr
->rootWin
, win
,
791 mousePos
->x
, mousePos
->y
, &tmpx
, &tmpy
,
796 return lookForAwareWindow(scr
, mousePos
, child
);
804 findDestination(WMDraggingInfo
*info
, WMPoint
*mousePos
)
806 WMScreen
*scr
= sourceScreen(info
);
808 Window
*children
= windowChildren(scr
->display
, scr
->rootWin
, &nchildren
);
810 XWindowAttributes attr
;
812 if (isXdndAware(scr
, scr
->rootWin
))
815 /* exclude drag icon (and upper) from search */
816 for (i
= nchildren
-1; i
>= 0; i
--) {
817 if (children
[i
] == XDND_DRAG_ICON(info
)) {
824 /* root window has no child under drag icon, and is not xdnd aware. */
828 /* inspecting children, from upper to lower */
829 for (; i
>= 0; i
--) {
830 if (XGetWindowAttributes(scr
->display
, children
[i
], &attr
)
831 && attr
.map_state
== IsViewable
832 && mousePos
->x
>= attr
.x
833 && mousePos
->y
>= attr
.y
834 && mousePos
->x
< attr
.x
+ attr
.width
835 && mousePos
->y
< attr
.y
+ attr
.height
) {
836 return lookForAwareWindow(scr
, mousePos
, children
[i
]);
840 /* No child window under drag pointer */
846 initMotionProcess(WMView
*view
, WMDraggingInfo
*info
,
847 XEvent
*event
, WMPoint
*startLocation
)
849 WMScreen
*scr
= W_VIEW_SCREEN(view
);
851 /* take ownership of XdndSelection */
852 XDND_SELECTION_PROCS(info
) =
853 (WMSelectionProcs
*) wmalloc(sizeof(WMSelectionProcs
));
854 XDND_SELECTION_PROCS(info
)->convertSelection
= convertSelection
;
855 XDND_SELECTION_PROCS(info
)->selectionLost
= selectionLost
;
856 XDND_SELECTION_PROCS(info
)->selectionDone
= selectionDone
;
857 XDND_TIMESTAMP(info
) = event
->xmotion
.time
;
859 if (!WMCreateSelectionHandler(view
, scr
->xdndSelectionAtom
,
861 XDND_SELECTION_PROCS(info
), NULL
)) {
862 wwarning("could not get ownership or DND selection");
866 registerDropTypes(scr
, view
, info
);
868 if (XDND_SOURCE_ACTION(info
) == W_VIEW_SCREEN(view
)->xdndActionAsk
)
869 registerSupportedOperations(view
);
871 if (view
->dragSourceProcs
->beganDrag
!= NULL
) {
872 view
->dragSourceProcs
->beganDrag(view
, startLocation
);
878 processMotion(WMDraggingInfo
*info
, Window windowUnderDrag
, WMPoint
*mousePos
)
880 /* // WMScreen *scr = sourceScreen(info); */
881 Window newDestination
= findDestination(info
, mousePos
);
883 W_DragSourceStopTimer();
885 if (newDestination
!= XDND_DEST_WIN(info
)) {
886 recolorCursor(info
, False
);
888 if (XDND_DEST_WIN(info
) != None
) {
889 /* leaving a xdnd window */
890 sendLeaveMessage(info
);
893 XDND_DEST_WIN(info
) = newDestination
;
894 XDND_SOURCE_STATE(info
) = idleState
;
895 XDND_DEST_ACTION(info
) = None
;
896 XDND_NO_POS_ZONE(info
).size
.width
= 0;
897 XDND_NO_POS_ZONE(info
).size
.height
= 0;
899 if (newDestination
!= None
) {
900 /* entering a xdnd window */
901 if (! sendEnterMessage(info
)) {
902 XDND_DEST_WIN(info
) = None
;
906 W_DragSourceStartTimer(info
);
909 if (XDND_DEST_WIN(info
) != None
) {
910 if (! sendPositionMessage(info
, mousePos
)) {
911 XDND_DEST_WIN(info
) = None
;
915 W_DragSourceStartTimer(info
);
922 processButtonRelease(WMDraggingInfo
*info
)
924 if (XDND_SOURCE_STATE(info
) == dropAllowedState
) {
926 W_DragSourceStopTimer();
928 if (! sendDropMessage(info
))
931 W_DragSourceStartTimer(info
);
934 if (XDND_DEST_WIN(info
) != None
)
935 sendLeaveMessage(info
);
937 W_DragSourceStopTimer();
944 WMIsDraggingFromView(WMView
*view
)
946 WMDraggingInfo
*info
= &W_VIEW_SCREEN(view
)->dragInfo
;
948 return ( XDND_SOURCE_INFO(info
) != NULL
949 && XDND_SOURCE_STATE(info
) != finishDropState
);
950 /* return W_VIEW_SCREEN(view)->dragInfo.sourceInfo != NULL; */
955 WMDragImageFromView(WMView
*view
, XEvent
*event
)
957 WMDraggingInfo
*info
= &W_VIEW_SCREEN(view
)->dragInfo
;
958 WMPoint mouseLocation
;
960 switch(event
->type
) {
962 if (event
->xbutton
.button
== Button1
) {
965 XPeekEvent(event
->xbutton
.display
, &nextEvent
);
967 /* Initialize only if a drag really begins (avoid clicks) */
968 if (nextEvent
.type
== MotionNotify
) {
969 initSourceDragInfo(view
, info
);
976 if (WMIsDraggingFromView(view
)) {
977 Bool dropBegan
= processButtonRelease(info
);
979 recolorCursor(info
, False
);
981 endDragImage(info
, False
);
982 XDND_SOURCE_STATE(info
) = finishDropState
;
985 endDragImage(info
, True
);
986 endDragProcess(info
,False
);
992 if (WMIsDraggingFromView(view
)) {
993 mouseLocation
= wmkpoint(event
->xmotion
.x_root
,
994 event
->xmotion
.y_root
);
996 if (abs(XDND_DRAG_ICON_POS(info
).x
- mouseLocation
.x
) >=
998 || abs(XDND_DRAG_ICON_POS(info
).y
- mouseLocation
.y
) >=
1000 if (XDND_DRAG_ICON(info
) == None
) {
1001 initMotionProcess(view
, info
, event
, &mouseLocation
);
1002 startDragImage(view
, info
, event
);
1004 XDND_DRAG_ICON_POS(info
).x
=
1005 mouseLocation
.x
- XDND_MOUSE_OFFSET(info
).x
;
1006 XDND_DRAG_ICON_POS(info
).y
=
1007 mouseLocation
.y
- XDND_MOUSE_OFFSET(info
).y
;
1009 refreshDragImage(view
, info
);
1011 event
->xmotion
.window
,
1021 /* Minimal mouse events handler: no right or double-click detection,
1022 only drag is supported */
1024 dragImageHandler(XEvent
*event
, void *cdata
)
1026 WMView
*view
= (WMView
*)cdata
;
1028 WMDragImageFromView(view
, event
);
1032 /* ----- source states ----- */
1036 traceStatusMsg(Display
*dpy
, XClientMessageEvent
*statusEvent
)
1038 printf("Xdnd status message:\n");
1040 if (statusEvent
->data
.l
[1] & 0x2UL
)
1041 printf("send position on every move\n");
1044 x
= statusEvent
->data
.l
[2] >> 16;
1045 y
= statusEvent
->data
.l
[2] & 0xFFFFL
;
1046 w
= statusEvent
->data
.l
[3] >> 16;
1047 h
= statusEvent
->data
.l
[3] & 0xFFFFL
;
1049 printf("send position out of ((%d,%d) , (%d,%d))\n",
1053 if (statusEvent
->data
.l
[1] & 0x1L
)
1054 printf("allowed action: %s\n",
1055 XGetAtomName(dpy
, statusEvent
->data
.l
[4]));
1057 printf("no action allowed\n");
1063 storeDropAction(WMDraggingInfo
*info
, Atom destAction
)
1065 WMView
* sourceView
= XDND_SOURCE_VIEW(info
);
1066 WMScreen
*scr
= W_VIEW_SCREEN(sourceView
);
1068 if (sourceView
->dragSourceProcs
->acceptDropOperation
!= NULL
) {
1069 if (sourceView
->dragSourceProcs
->acceptDropOperation(
1071 W_ActionToOperation(scr
, destAction
)))
1072 XDND_DEST_ACTION(info
) = destAction
;
1074 XDND_DEST_ACTION(info
) = None
;
1076 XDND_DEST_ACTION(info
) = destAction
;
1082 storeStatusMessageInfos(WMDraggingInfo
*info
, XClientMessageEvent
*statusEvent
)
1084 WMRect
* noPosZone
= &(XDND_NO_POS_ZONE(info
));
1088 traceStatusMsg(sourceScreen(info
)->display
, statusEvent
);
1091 if (statusEvent
->data
.l
[1] & 0x2UL
) {
1092 /* bit 1 set: destination wants position messages on every move */
1093 noPosZone
->size
.width
= 0;
1094 noPosZone
->size
.height
= 0;
1096 /* don't send another position message while in given rectangle */
1097 noPosZone
->pos
.x
= statusEvent
->data
.l
[2] >> 16;
1098 noPosZone
->pos
.y
= statusEvent
->data
.l
[2] & 0xFFFFL
;
1099 noPosZone
->size
.width
= statusEvent
->data
.l
[3] >> 16;
1100 noPosZone
->size
.height
= statusEvent
->data
.l
[3] & 0xFFFFL
;
1103 if ((statusEvent
->data
.l
[1] & 0x1L
) || statusEvent
->data
.l
[4] != None
) {
1104 /* destination accept drop */
1105 storeDropAction(info
, statusEvent
->data
.l
[4]);
1107 XDND_DEST_ACTION(info
) = None
;
1113 idleState(WMView
*view
, XClientMessageEvent
*event
, WMDraggingInfo
*info
)
1116 Atom destMsg
= event
->message_type
;
1118 scr
= W_VIEW_SCREEN(view
);
1120 if (destMsg
== scr
->xdndStatusAtom
) {
1121 storeStatusMessageInfos(info
, event
);
1123 if (XDND_DEST_ACTION(info
) != None
) {
1124 recolorCursor(info
, True
);
1125 W_DragSourceStartTimer(info
);
1126 return dropAllowedState
;
1129 recolorCursor(info
, False
);
1134 if (destMsg
== scr
->xdndFinishedAtom
) {
1135 wwarning("received xdndFinishedAtom before drop began");
1138 W_DragSourceStartTimer(info
);
1144 dropAllowedState(WMView
*view
, XClientMessageEvent
*event
, WMDraggingInfo
*info
)
1146 WMScreen
*scr
= W_VIEW_SCREEN(view
);
1147 Atom destMsg
= event
->message_type
;
1149 if (destMsg
== scr
->xdndStatusAtom
) {
1150 storeStatusMessageInfos(info
, event
);
1152 if (XDND_DEST_ACTION(info
) == None
) {
1154 recolorCursor(info
, False
);
1159 W_DragSourceStartTimer(info
);
1160 return dropAllowedState
;
1165 finishDropState(WMView
*view
, XClientMessageEvent
*event
, WMDraggingInfo
*info
)
1167 WMScreen
*scr
= W_VIEW_SCREEN(view
);
1168 Atom destMsg
= event
->message_type
;
1170 if (destMsg
== scr
->xdndFinishedAtom
) {
1171 endDragProcess(info
, True
);
1175 W_DragSourceStartTimer(info
);
1176 return finishDropState
;
1178 /* ----- end of source states ----- */
1181 /* ----- source timer ----- */
1183 dragSourceResponseTimeOut(void *source
)
1185 WMView
*view
= (WMView
*)source
;
1186 WMDraggingInfo
*info
= &(W_VIEW_SCREEN(view
)->dragInfo
);
1188 wwarning("delay for drag destination response expired");
1189 sendLeaveMessage(info
);
1191 recolorCursor(info
, False
);
1192 if (XDND_SOURCE_STATE(info
) == finishDropState
) {
1194 endDragImage(info
, True
);
1195 endDragProcess(info
, False
);
1197 XDND_SOURCE_STATE(info
) = idleState
;
1202 W_DragSourceStopTimer()
1204 if (dndSourceTimer
!= NULL
) {
1205 WMDeleteTimerHandler(dndSourceTimer
);
1206 dndSourceTimer
= NULL
;
1211 W_DragSourceStartTimer(WMDraggingInfo
*info
)
1213 W_DragSourceStopTimer();
1215 dndSourceTimer
= WMAddTimerHandler(
1216 XDND_DESTINATION_RESPONSE_MAX_DELAY
,
1217 dragSourceResponseTimeOut
,
1218 XDND_SOURCE_VIEW(info
));
1221 /* ----- End of Destination timer ----- */
1225 W_DragSourceStateHandler(WMDraggingInfo
*info
, XClientMessageEvent
*event
)
1228 W_DndState
* newState
;
1230 if (XDND_SOURCE_VIEW_STORED(info
)) {
1231 view
= XDND_SOURCE_VIEW(info
);
1234 printf("current source state: %s\n",
1235 stateName(XDND_SOURCE_STATE(info
)));
1238 newState
= (W_DndState
*) XDND_SOURCE_STATE(info
)(view
, event
, info
);
1242 printf("new source state: %s\n", stateName(newState
));
1245 if (newState
!= NULL
)
1246 XDND_SOURCE_STATE(info
) = newState
;
1247 /* else drop finished, and info has been flushed */
1252 void WMSetViewDragImage(WMView
* view
, WMPixmap
*dragImage
)
1254 if (view
->dragImage
!= NULL
)
1255 WMReleasePixmap(view
->dragImage
);
1257 view
->dragImage
= WMRetainPixmap(dragImage
);
1261 void WMReleaseViewDragImage(WMView
* view
)
1263 if (view
->dragImage
!= NULL
)
1264 WMReleasePixmap(view
->dragImage
);
1268 /* Create a drag handler, associating drag event masks with dragEventProc */
1270 WMCreateDragHandler(WMView
*view
, WMEventProc
*dragEventProc
, void *clientData
)
1272 WMCreateEventHandler(view
,
1273 ButtonPressMask
|ButtonReleaseMask
|Button1MotionMask
,
1274 dragEventProc
, clientData
);
1279 WMDeleteDragHandler(WMView
*view
, WMEventProc
*dragEventProc
, void *clientData
)
1281 WMDeleteEventHandler(view
,
1282 ButtonPressMask
|ButtonReleaseMask
|Button1MotionMask
,
1283 dragEventProc
, clientData
);
1287 /* set default drag handler for view */
1289 WMSetViewDraggable(WMView
*view
, WMDragSourceProcs
*dragSourceProcs
,
1290 WMPixmap
*dragImage
)
1292 wassertr(dragImage
!= NULL
);
1293 view
->dragImage
= WMRetainPixmap(dragImage
);
1295 WMSetViewDragSourceProcs(view
, dragSourceProcs
);
1297 WMCreateDragHandler(view
, dragImageHandler
, view
);
1302 WMUnsetViewDraggable(WMView
*view
)
1304 if (view
->dragSourceProcs
) {
1305 wfree(view
->dragSourceProcs
);
1306 view
->dragSourceProcs
= NULL
;
1309 WMReleaseViewDragImage(view
);
1311 WMDeleteDragHandler(view
, dragImageHandler
, view
);