7 #include <X11/cursorfont.h>
9 #include <X11/extensions/shape.h>
14 #define XDND_DESTINATION_RESPONSE_MAX_DELAY 10000
15 #define MIN_X_MOVE_OFFSET 5
16 #define MIN_Y_MOVE_OFFSET 5
17 #define MAX_SLIDEBACK_ITER 15
19 #define VERSION_INFO(dragInfo) dragInfo->protocolVersion
20 #define XDND_PROPERTY_FORMAT 32
21 #define XDND_ACTION_DESCRIPTION_FORMAT 8
23 #define XDND_SOURCE_INFO(dragInfo) dragInfo->sourceInfo
24 #define XDND_DEST_WIN(dragInfo) dragInfo->sourceInfo->destinationWindow
25 #define XDND_SOURCE_ACTION(dragInfo) dragInfo->sourceAction
26 #define XDND_DEST_ACTION(dragInfo) dragInfo->destinationAction
27 #define XDND_SOURCE_VIEW(dragInfo) dragInfo->sourceInfo->sourceView
28 #define XDND_SOURCE_STATE(dragInfo) dragInfo->sourceInfo->state
29 #define XDND_SELECTION_PROCS(dragInfo) dragInfo->sourceInfo->selectionProcs
30 #define XDND_DRAG_ICON(dragInfo) dragInfo->sourceInfo->icon
31 #define XDND_MOUSE_OFFSET(dragInfo) dragInfo->sourceInfo->mouseOffset
32 #define XDND_DRAG_CURSOR(dragInfo) dragInfo->sourceInfo->dragCursor
33 #define XDND_DRAG_ICON_POS(dragInfo) dragInfo->sourceInfo->imageLocation
34 #define XDND_NO_POS_ZONE(dragInfo) dragInfo->sourceInfo->noPositionMessageZone
35 #define XDND_TIMESTAMP(dragInfo) dragInfo->timestamp
36 #define XDND_3_TYPES(dragInfo) dragInfo->sourceInfo->firstThreeTypes
37 #define XDND_SOURCE_VIEW_STORED(dragInfo) dragInfo->sourceInfo != NULL \
38 && dragInfo->sourceInfo->sourceView != NULL
41 static WMHandlerID dndSourceTimer
= NULL
;
44 static void* idleState(WMView
*srcView
, XClientMessageEvent
*event
,
45 WMDraggingInfo
*info
);
46 static void* dropAllowedState(WMView
*srcView
, XClientMessageEvent
*event
,
47 WMDraggingInfo
*info
);
48 static void* finishDropState(WMView
*srcView
, XClientMessageEvent
*event
,
49 WMDraggingInfo
*info
);
53 stateName(W_DndState
*state
)
56 return "no state defined";
58 if (state
== idleState
)
61 if (state
== dropAllowedState
)
62 return "dropAllowedState";
64 if (state
== finishDropState
)
65 return "finishDropState";
67 return "unknown state";
73 sourceScreen(WMDraggingInfo
*info
)
75 return W_VIEW_SCREEN(XDND_SOURCE_VIEW(info
));
80 endDragProcess(WMDraggingInfo
*info
, Bool deposited
)
82 WMView
*view
= XDND_SOURCE_VIEW(info
);
83 WMScreen
*scr
= W_VIEW_SCREEN(XDND_SOURCE_VIEW(info
));
85 /* free selection handler while view exists */
86 WMDeleteSelectionHandler(view
,
87 scr
->xdndSelectionAtom
,
89 wfree(XDND_SELECTION_PROCS(info
));
91 if (XDND_DRAG_CURSOR(info
) != None
) {
92 XFreeCursor(scr
->display
,
93 XDND_DRAG_CURSOR(info
));
94 XDND_DRAG_CURSOR(info
) = None
;
97 if (view
->dragSourceProcs
->endedDrag
!= NULL
) {
98 /* this can destroy source view (with a "move" action for example) */
99 view
->dragSourceProcs
->endedDrag(view
, &XDND_DRAG_ICON_POS(info
),
103 /* clear remaining draggging infos */
104 wfree(XDND_SOURCE_INFO(info
));
105 XDND_SOURCE_INFO(info
) = NULL
;
109 /* ----- drag cursor ----- */
111 initDragCursor(WMDraggingInfo
*info
)
113 WMScreen
*scr
= sourceScreen(info
);
114 XColor cursorFgColor
, cursorBgColor
;
117 cursorFgColor
.red
= 0x4500;
118 cursorFgColor
.green
= 0xb000;
119 cursorFgColor
.blue
= 0x4500;
122 cursorBgColor
.red
= 0xffff;
123 cursorBgColor
.green
= 0xffff;
124 cursorBgColor
.blue
= 0xffff;
126 XDND_DRAG_CURSOR(info
) = XCreateFontCursor(scr
->display
, XC_left_ptr
);
127 XRecolorCursor(scr
->display
,
128 XDND_DRAG_CURSOR(info
),
132 XFlush(scr
->display
);
136 recolorCursor(WMDraggingInfo
*info
, Bool dropIsAllowed
)
138 WMScreen
*scr
= sourceScreen(info
);
141 XDefineCursor(scr
->display
,
143 XDND_DRAG_CURSOR(info
));
145 XDefineCursor(scr
->display
,
150 XFlush(scr
->display
);
152 /* ----- end of drag cursor ----- */
155 /* ----- selection procs ----- */
157 convertSelection(WMView
*view
, Atom selection
, Atom target
,
158 void *cdata
, Atom
*type
)
164 scr
= W_VIEW_SCREEN(view
);
165 typeName
= XGetAtomName(scr
->display
, target
);
169 if (view
->dragSourceProcs
->fetchDragData
!= NULL
) {
170 data
= view
->dragSourceProcs
->fetchDragData(
177 if (typeName
!= NULL
)
185 selectionLost(WMView
*view
, Atom selection
, void *cdata
)
187 wwarning("DND selection lost during drag operation...");
192 selectionDone(WMView
*view
, Atom selection
, Atom target
, void *cdata
)
195 printf("selection done\n");
198 /* ----- end of selection procs ----- */
201 /* ----- visual part ----- */
204 makeDragIcon(WMScreen
*scr
, WMPixmap
*pixmap
)
209 XSetWindowAttributes attribs
;
213 pixmap
= scr
->defaultObjectIcon
;
216 size
= WMGetPixmapSize(pixmap
);
217 pix
= pixmap
->pixmap
;
220 flags
= CWSaveUnder
|CWBackPixmap
|CWOverrideRedirect
|CWColormap
;
221 attribs
.save_under
= True
;
222 attribs
.background_pixmap
= pix
;
223 attribs
.override_redirect
= True
;
224 attribs
.colormap
= scr
->colormap
;
226 window
= XCreateWindow(scr
->display
, scr
->rootWin
, 0, 0, size
.width
,
227 size
.height
, 0, scr
->depth
, InputOutput
,
228 scr
->visual
, flags
, &attribs
);
233 XShapeCombineMask(scr
->display
, window
, ShapeBounding
, 0, 0, mask
,
243 slideWindow(Display
*dpy
, Window win
, int srcX
, int srcY
, int dstX
, int dstY
)
249 iterations
= WMIN(MAX_SLIDEBACK_ITER
,
250 WMAX(abs(dstX
-srcX
), abs(dstY
-srcY
)));
255 dx
= (double)(dstX
-srcX
)/iterations
;
256 dy
= (double)(dstY
-srcY
)/iterations
;
258 for (i
= 0; i
<= iterations
; i
++) {
259 XMoveWindow(dpy
, win
, x
, y
);
271 getInitialDragImageCoord(int viewCoord
, int mouseCoord
,
272 int viewSize
, int iconSize
)
274 if (iconSize
>= viewSize
) {
275 /* center icon coord on view */
276 return viewCoord
- iconSize
/2;
278 /* try to center icon on mouse pos */
280 if (mouseCoord
- iconSize
/2 <= viewCoord
)
281 /* if icon was centered on mouse, it would be off view
282 thus, put icon left (resp. top) side
283 at view (resp. top) side */
286 else if (mouseCoord
+ iconSize
/2 >= viewCoord
+ viewSize
)
287 /* if icon was centered on mouse, it would be off view
288 thus, put icon right (resp. bottom) side
289 at view right (resp. bottom) side */
290 return viewCoord
+ viewSize
- iconSize
;
293 return mouseCoord
- iconSize
/2;
299 initDragImagePos(WMView
*view
, WMDraggingInfo
*info
, XEvent
*event
)
301 WMSize iconSize
= WMGetPixmapSize(view
->dragImage
);
302 WMSize viewSize
= WMGetViewSize(view
);
306 XTranslateCoordinates(W_VIEW_SCREEN(view
)->display
,
307 WMViewXID(view
), W_VIEW_SCREEN(view
)->rootWin
,
308 0, 0, &(viewPos
.x
), &(viewPos
.y
),
312 XDND_DRAG_ICON_POS(info
).x
=
313 getInitialDragImageCoord(viewPos
.x
, event
->xmotion
.x_root
,
314 viewSize
.width
, iconSize
.width
);
316 XDND_DRAG_ICON_POS(info
).y
=
317 getInitialDragImageCoord(viewPos
.y
, event
->xmotion
.y_root
,
318 viewSize
.height
, iconSize
.height
);
320 /* set mouse offset relative to icon */
321 XDND_MOUSE_OFFSET(info
).x
=
322 event
->xmotion
.x_root
- XDND_DRAG_ICON_POS(info
).x
;
323 XDND_MOUSE_OFFSET(info
).y
=
324 event
->xmotion
.y_root
- XDND_DRAG_ICON_POS(info
).y
;
329 refreshDragImage(WMView
*view
, WMDraggingInfo
*info
)
331 WMScreen
*scr
= W_VIEW_SCREEN(view
);
333 XMoveWindow(scr
->display
, XDND_DRAG_ICON(info
),
334 XDND_DRAG_ICON_POS(info
).x
,
335 XDND_DRAG_ICON_POS(info
).y
);
340 startDragImage(WMView
*view
, WMDraggingInfo
*info
, XEvent
* event
)
342 WMScreen
*scr
= W_VIEW_SCREEN(view
);
344 XDND_DRAG_ICON(info
) = makeDragIcon(scr
, view
->dragImage
);
345 initDragImagePos(view
, info
, event
);
346 refreshDragImage(view
, info
);
347 XMapRaised(scr
->display
, XDND_DRAG_ICON(info
));
349 initDragCursor(info
);
354 endDragImage(WMDraggingInfo
*info
, Bool slideBack
)
356 WMView
*view
= XDND_SOURCE_VIEW(info
);
357 Display
*dpy
= W_VIEW_SCREEN(view
)->display
;
363 XTranslateCoordinates(W_VIEW_SCREEN(view
)->display
,
364 WMViewXID(view
), W_VIEW_SCREEN(view
)->rootWin
,
365 0, 0, &(toLocation
.x
), &(toLocation
.y
),
368 slideWindow(dpy
, XDND_DRAG_ICON(info
),
369 XDND_DRAG_ICON_POS(info
).x
,
370 XDND_DRAG_ICON_POS(info
).y
,
375 XDestroyWindow(dpy
, XDND_DRAG_ICON(info
));
378 /* ----- end of visual part ----- */
381 /* ----- messages ----- */
383 /* send a DnD message to the destination window */
385 sendDnDClientMessage(WMDraggingInfo
*info
, Atom message
,
391 Display
*dpy
= sourceScreen(info
)->display
;
392 Window srcWin
= WMViewXID(XDND_SOURCE_VIEW(info
));
393 Window destWin
= XDND_DEST_WIN(info
);
395 if (! W_SendDnDClientMessage(dpy
,
404 recolorCursor(info
, False
);
405 endDragImage(info
, True
);
406 endDragProcess(info
, False
);
415 sendEnterMessage(WMDraggingInfo
*info
)
417 WMScreen
*scr
= sourceScreen(info
);
420 data1
= (VERSION_INFO(info
) << 24)|1; /* 1: support of type list */
422 return sendDnDClientMessage(info
, scr
->xdndEnterAtom
,
424 XDND_3_TYPES(info
)[0],
425 XDND_3_TYPES(info
)[1],
426 XDND_3_TYPES(info
)[2]);
431 // this functon doesn't return something in all cases.
432 // control reaches end of non-void function. fix this -Dan */
434 sendPositionMessage(WMDraggingInfo
*info
, WMPoint
*mousePos
)
436 WMScreen
*scr
= sourceScreen(info
);
437 WMRect
*noPosZone
= &(XDND_NO_POS_ZONE(info
));
439 if (noPosZone
->size
.width
!= 0 || noPosZone
->size
.height
!= 0) {
440 if (mousePos
->x
< noPosZone
->pos
.x
441 || mousePos
->x
> (noPosZone
->pos
.x
+ noPosZone
->size
.width
)
442 || mousePos
->y
< noPosZone
->pos
.y
443 || mousePos
->y
> (noPosZone
->pos
.y
+ noPosZone
->size
.width
)) {
444 /* send position if out of zone defined by destination */
445 return sendDnDClientMessage(info
, scr
->xdndPositionAtom
,
447 mousePos
->x
<<16|mousePos
->y
,
448 XDND_TIMESTAMP(info
),
449 XDND_SOURCE_ACTION(info
));
452 /* send position on each move */
453 return sendDnDClientMessage(info
, scr
->xdndPositionAtom
,
455 mousePos
->x
<<16|mousePos
->y
,
456 XDND_TIMESTAMP(info
),
457 XDND_SOURCE_ACTION(info
));
463 sendLeaveMessage(WMDraggingInfo
*info
)
465 WMScreen
*scr
= sourceScreen(info
);
467 return sendDnDClientMessage(info
, scr
->xdndLeaveAtom
,
473 sendDropMessage(WMDraggingInfo
*info
)
475 WMScreen
*scr
= sourceScreen(info
);
477 return sendDnDClientMessage(info
,
480 XDND_TIMESTAMP(info
),
484 /* ----- end of messages ----- */
488 getTypeAtomList(WMScreen
*scr
, WMView
*view
, int* count
)
494 types
= view
->dragSourceProcs
->dropDataTypes(view
);
497 *count
= WMGetArrayItemCount(types
);
499 typeAtoms
= wmalloc((*count
)*sizeof(Atom
));
500 for (i
=0; i
< *count
; i
++) {
501 typeAtoms
[i
] = XInternAtom(scr
->display
,
502 WMGetFromArray(types
, i
),
506 /* WMFreeArray(types); */
510 /* WMFreeArray(types); */
514 typeAtoms
= wmalloc(sizeof(Atom
));
522 registerDropTypes(WMScreen
*scr
, WMView
*view
, WMDraggingInfo
*info
)
527 typeList
= getTypeAtomList(scr
, view
, &count
);
529 /* store the first 3 types */
530 for(i
=0; i
< 3 && i
< count
; i
++)
531 XDND_3_TYPES(info
)[i
] = typeList
[i
];
534 XDND_3_TYPES(info
)[i
] = None
;
537 /* store the entire type list */
538 XChangeProperty(scr
->display
,
540 scr
->xdndTypeListAtom
,
542 XDND_PROPERTY_FORMAT
,
544 (unsigned char*) typeList
,
550 registerOperationList(WMScreen
*scr
, WMView
*view
, WMArray
* operationArray
)
553 WMDragOperationType operation
;
554 int count
= WMGetArrayItemCount(operationArray
);
557 actionList
= wmalloc(sizeof(Atom
)*count
);
559 for(i
=0; i
< count
; i
++) {
560 operation
= WMGetDragOperationItemType(
561 WMGetFromArray(operationArray
, i
));
562 actionList
[i
] = W_OperationToAction(scr
, operation
);
565 XChangeProperty(scr
->display
,
567 scr
->xdndActionListAtom
,
569 XDND_PROPERTY_FORMAT
,
571 (unsigned char*) actionList
,
576 registerDescriptionList(WMScreen
*scr
, WMView
*view
, WMArray
* operationArray
)
578 char *text
, *textListItem
, *textList
;
579 int count
= WMGetArrayItemCount(operationArray
);
583 /* size of XA_STRING info */
584 for(i
=0; i
< count
; i
++) {
585 size
+= strlen(WMGetDragOperationItemText(
586 WMGetFromArray(operationArray
, i
))) + 1; /* +1 = +NULL */
589 /* create text list */
590 textList
= wmalloc(size
);
591 textListItem
= textList
;
593 for(i
=0; i
< count
; i
++) {
594 text
= WMGetDragOperationItemText(WMGetFromArray(operationArray
, i
));
595 strcpy(textListItem
, text
);
597 /* to next text offset */
598 textListItem
= &(textListItem
[strlen(textListItem
) + 1]);
601 XChangeProperty(scr
->display
,
603 scr
->xdndActionDescriptionAtom
,
605 XDND_ACTION_DESCRIPTION_FORMAT
,
607 (unsigned char*) textList
,
611 /* called if wanted operation is WDOperationAsk */
613 registerSupportedOperations(WMView
*view
)
615 WMScreen
*scr
= W_VIEW_SCREEN(view
);
616 WMArray
* operationArray
;
618 operationArray
= view
->dragSourceProcs
->askedOperations(view
);
620 registerOperationList(scr
, view
, operationArray
);
621 registerDescriptionList(scr
, view
, operationArray
);
623 /* WMFreeArray(operationArray); */
628 initSourceDragInfo(WMView
*sourceView
, WMDraggingInfo
*info
)
632 XDND_SOURCE_INFO(info
) = (W_DragSourceInfo
*) wmalloc(sizeof(W_DragSourceInfo
));
634 XDND_SOURCE_VIEW(info
) = sourceView
;
635 XDND_DEST_WIN(info
) = None
;
636 XDND_DRAG_ICON(info
) = None
;
638 XDND_SOURCE_ACTION(info
) = W_OperationToAction(
639 W_VIEW_SCREEN(sourceView
),
640 sourceView
->dragSourceProcs
->wantedDropOperation(sourceView
));
642 XDND_DEST_ACTION(info
) = None
;
644 XDND_SOURCE_STATE(info
) = idleState
;
646 emptyZone
.pos
= wmkpoint(0, 0);
647 emptyZone
.size
= wmksize(0, 0);
648 XDND_NO_POS_ZONE(info
) = emptyZone
;
653 Returned array is destroyed after dropDataTypes call
656 defDropDataTypes(WMView
*self
)
662 static WMDragOperationType
663 defWantedDropOperation(WMView
*self
)
665 return WDOperationNone
;
670 Must be defined if wantedDropOperation return WDOperationAsk
672 Return a WMDragOperationItem array (destroyed after call).
673 A WMDragOperationItem links a label to an operation.
675 defAskedOperations(WMView *self); */
679 defAcceptDropOperation(WMView
*self
, WMDragOperationType allowedOperation
)
686 defBeganDrag(WMView
*self
, WMPoint
*point
)
692 defEndedDrag(WMView
*self
, WMPoint
*point
, Bool deposited
)
698 Returned data is not destroyed
701 defFetchDragData(WMView
*self
, char *type
)
708 WMSetViewDragSourceProcs(WMView
*view
, WMDragSourceProcs
*procs
)
710 if (view
->dragSourceProcs
)
711 wfree(view
->dragSourceProcs
);
712 view
->dragSourceProcs
= wmalloc(sizeof(WMDragSourceProcs
));
714 *view
->dragSourceProcs
= *procs
;
716 if (procs
->dropDataTypes
== NULL
)
717 view
->dragSourceProcs
->dropDataTypes
= defDropDataTypes
;
719 if (procs
->wantedDropOperation
== NULL
)
720 view
->dragSourceProcs
->wantedDropOperation
= defWantedDropOperation
;
723 Note: askedOperations can be NULL, if wantedDropOperation never returns
727 if (procs
->acceptDropOperation
== NULL
)
728 view
->dragSourceProcs
->acceptDropOperation
= defAcceptDropOperation
;
730 if (procs
->beganDrag
== NULL
)
731 view
->dragSourceProcs
->beganDrag
= defBeganDrag
;
733 if (procs
->endedDrag
== NULL
)
734 view
->dragSourceProcs
->endedDrag
= defEndedDrag
;
736 if (procs
->fetchDragData
== NULL
)
737 view
->dragSourceProcs
->fetchDragData
= defFetchDragData
;
742 isXdndAware(WMScreen
*scr
, Window win
)
746 unsigned long count
, remain
;
747 unsigned char *winXdndVersion
;
752 XGetWindowProperty(scr
->display
, win
, scr
->xdndAwareAtom
,
753 0, 1, False
, XA_ATOM
, &type
, &format
,
754 &count
, &remain
, &winXdndVersion
);
757 || format
!= XDND_PROPERTY_FORMAT
758 || count
== 0 || !winXdndVersion
) {
760 XFree(winXdndVersion
);
764 XFree(winXdndVersion
);
765 return (count
== 1); /* xdnd version is set */
770 windowChildren(Display
*dpy
, Window win
, unsigned *nchildren
)
775 if (!XQueryTree(dpy
, win
, &foo
, &bar
, &children
, nchildren
)) {
783 lookForAwareWindow(WMScreen
*scr
, WMPoint
*mousePos
, Window win
)
788 /* Since xdnd v3, only the toplevel window should be aware */
789 if (isXdndAware(scr
, win
))
792 /* inspect child under pointer */
793 if (XTranslateCoordinates(scr
->display
, scr
->rootWin
, win
,
794 mousePos
->x
, mousePos
->y
, &tmpx
, &tmpy
,
799 return lookForAwareWindow(scr
, mousePos
, child
);
807 findDestination(WMDraggingInfo
*info
, WMPoint
*mousePos
)
809 WMScreen
*scr
= sourceScreen(info
);
811 Window
*children
= windowChildren(scr
->display
, scr
->rootWin
, &nchildren
);
813 XWindowAttributes attr
;
815 if (isXdndAware(scr
, scr
->rootWin
))
818 /* exclude drag icon (and upper) from search */
819 for (i
= nchildren
-1; i
>= 0; i
--) {
820 if (children
[i
] == XDND_DRAG_ICON(info
)) {
827 /* root window has no child under drag icon, and is not xdnd aware. */
831 /* inspecting children, from upper to lower */
832 for (; i
>= 0; i
--) {
833 if (XGetWindowAttributes(scr
->display
, children
[i
], &attr
)
834 && attr
.map_state
== IsViewable
835 && mousePos
->x
>= attr
.x
836 && mousePos
->y
>= attr
.y
837 && mousePos
->x
< attr
.x
+ attr
.width
838 && mousePos
->y
< attr
.y
+ attr
.height
) {
839 return lookForAwareWindow(scr
, mousePos
, children
[i
]);
843 /* No child window under drag pointer */
849 initMotionProcess(WMView
*view
, WMDraggingInfo
*info
,
850 XEvent
*event
, WMPoint
*startLocation
)
852 WMScreen
*scr
= W_VIEW_SCREEN(view
);
854 /* take ownership of XdndSelection */
855 XDND_SELECTION_PROCS(info
) =
856 (WMSelectionProcs
*) wmalloc(sizeof(WMSelectionProcs
));
857 XDND_SELECTION_PROCS(info
)->convertSelection
= convertSelection
;
858 XDND_SELECTION_PROCS(info
)->selectionLost
= selectionLost
;
859 XDND_SELECTION_PROCS(info
)->selectionDone
= selectionDone
;
860 XDND_TIMESTAMP(info
) = event
->xmotion
.time
;
862 if (!WMCreateSelectionHandler(view
, scr
->xdndSelectionAtom
,
864 XDND_SELECTION_PROCS(info
), NULL
)) {
865 wwarning("could not get ownership or DND selection");
869 registerDropTypes(scr
, view
, info
);
871 if (XDND_SOURCE_ACTION(info
) == W_VIEW_SCREEN(view
)->xdndActionAsk
)
872 registerSupportedOperations(view
);
874 if (view
->dragSourceProcs
->beganDrag
!= NULL
) {
875 view
->dragSourceProcs
->beganDrag(view
, startLocation
);
881 processMotion(WMDraggingInfo
*info
, Window windowUnderDrag
, WMPoint
*mousePos
)
883 /* // WMScreen *scr = sourceScreen(info); */
884 Window newDestination
= findDestination(info
, mousePos
);
886 W_DragSourceStopTimer();
888 if (newDestination
!= XDND_DEST_WIN(info
)) {
889 recolorCursor(info
, False
);
891 if (XDND_DEST_WIN(info
) != None
) {
892 /* leaving a xdnd window */
893 sendLeaveMessage(info
);
896 XDND_DEST_WIN(info
) = newDestination
;
897 XDND_SOURCE_STATE(info
) = idleState
;
898 XDND_DEST_ACTION(info
) = None
;
899 XDND_NO_POS_ZONE(info
).size
.width
= 0;
900 XDND_NO_POS_ZONE(info
).size
.height
= 0;
902 if (newDestination
!= None
) {
903 /* entering a xdnd window */
904 if (! sendEnterMessage(info
)) {
905 XDND_DEST_WIN(info
) = None
;
909 W_DragSourceStartTimer(info
);
912 if (XDND_DEST_WIN(info
) != None
) {
913 if (! sendPositionMessage(info
, mousePos
)) {
914 XDND_DEST_WIN(info
) = None
;
918 W_DragSourceStartTimer(info
);
925 processButtonRelease(WMDraggingInfo
*info
)
927 if (XDND_SOURCE_STATE(info
) == dropAllowedState
) {
929 W_DragSourceStopTimer();
931 if (! sendDropMessage(info
))
934 W_DragSourceStartTimer(info
);
937 if (XDND_DEST_WIN(info
) != None
)
938 sendLeaveMessage(info
);
940 W_DragSourceStopTimer();
947 WMIsDraggingFromView(WMView
*view
)
949 WMDraggingInfo
*info
= &W_VIEW_SCREEN(view
)->dragInfo
;
951 return ( XDND_SOURCE_INFO(info
) != NULL
952 && XDND_SOURCE_STATE(info
) != finishDropState
);
953 /* return W_VIEW_SCREEN(view)->dragInfo.sourceInfo != NULL; */
958 WMDragImageFromView(WMView
*view
, XEvent
*event
)
960 WMDraggingInfo
*info
= &W_VIEW_SCREEN(view
)->dragInfo
;
961 WMPoint mouseLocation
;
963 switch(event
->type
) {
965 if (event
->xbutton
.button
== Button1
) {
968 XPeekEvent(event
->xbutton
.display
, &nextEvent
);
970 /* Initialize only if a drag really begins (avoid clicks) */
971 if (nextEvent
.type
== MotionNotify
) {
972 initSourceDragInfo(view
, info
);
979 if (WMIsDraggingFromView(view
)) {
980 Bool dropBegan
= processButtonRelease(info
);
982 recolorCursor(info
, False
);
984 endDragImage(info
, False
);
985 XDND_SOURCE_STATE(info
) = finishDropState
;
988 endDragImage(info
, True
);
989 endDragProcess(info
,False
);
995 if (WMIsDraggingFromView(view
)) {
996 mouseLocation
= wmkpoint(event
->xmotion
.x_root
,
997 event
->xmotion
.y_root
);
999 if (abs(XDND_DRAG_ICON_POS(info
).x
- mouseLocation
.x
) >=
1001 || abs(XDND_DRAG_ICON_POS(info
).y
- mouseLocation
.y
) >=
1002 MIN_Y_MOVE_OFFSET
) {
1003 if (XDND_DRAG_ICON(info
) == None
) {
1004 initMotionProcess(view
, info
, event
, &mouseLocation
);
1005 startDragImage(view
, info
, event
);
1007 XDND_DRAG_ICON_POS(info
).x
=
1008 mouseLocation
.x
- XDND_MOUSE_OFFSET(info
).x
;
1009 XDND_DRAG_ICON_POS(info
).y
=
1010 mouseLocation
.y
- XDND_MOUSE_OFFSET(info
).y
;
1012 refreshDragImage(view
, info
);
1014 event
->xmotion
.window
,
1024 /* Minimal mouse events handler: no right or double-click detection,
1025 only drag is supported */
1027 dragImageHandler(XEvent
*event
, void *cdata
)
1029 WMView
*view
= (WMView
*)cdata
;
1031 WMDragImageFromView(view
, event
);
1035 /* ----- source states ----- */
1039 traceStatusMsg(Display
*dpy
, XClientMessageEvent
*statusEvent
)
1041 printf("Xdnd status message:\n");
1043 if (statusEvent
->data
.l
[1] & 0x2UL
)
1044 printf("send position on every move\n");
1047 x
= statusEvent
->data
.l
[2] >> 16;
1048 y
= statusEvent
->data
.l
[2] & 0xFFFFL
;
1049 w
= statusEvent
->data
.l
[3] >> 16;
1050 h
= statusEvent
->data
.l
[3] & 0xFFFFL
;
1052 printf("send position out of ((%d,%d) , (%d,%d))\n",
1056 if (statusEvent
->data
.l
[1] & 0x1L
)
1057 printf("allowed action: %s\n",
1058 XGetAtomName(dpy
, statusEvent
->data
.l
[4]));
1060 printf("no action allowed\n");
1066 storeDropAction(WMDraggingInfo
*info
, Atom destAction
)
1068 WMView
* sourceView
= XDND_SOURCE_VIEW(info
);
1069 WMScreen
*scr
= W_VIEW_SCREEN(sourceView
);
1071 if (sourceView
->dragSourceProcs
->acceptDropOperation
!= NULL
) {
1072 if (sourceView
->dragSourceProcs
->acceptDropOperation(
1074 W_ActionToOperation(scr
, destAction
)))
1075 XDND_DEST_ACTION(info
) = destAction
;
1077 XDND_DEST_ACTION(info
) = None
;
1079 XDND_DEST_ACTION(info
) = destAction
;
1085 storeStatusMessageInfos(WMDraggingInfo
*info
, XClientMessageEvent
*statusEvent
)
1087 WMRect
* noPosZone
= &(XDND_NO_POS_ZONE(info
));
1091 traceStatusMsg(sourceScreen(info
)->display
, statusEvent
);
1094 if (statusEvent
->data
.l
[1] & 0x2UL
) {
1095 /* bit 1 set: destination wants position messages on every move */
1096 noPosZone
->size
.width
= 0;
1097 noPosZone
->size
.height
= 0;
1099 /* don't send another position message while in given rectangle */
1100 noPosZone
->pos
.x
= statusEvent
->data
.l
[2] >> 16;
1101 noPosZone
->pos
.y
= statusEvent
->data
.l
[2] & 0xFFFFL
;
1102 noPosZone
->size
.width
= statusEvent
->data
.l
[3] >> 16;
1103 noPosZone
->size
.height
= statusEvent
->data
.l
[3] & 0xFFFFL
;
1106 if ((statusEvent
->data
.l
[1] & 0x1L
) || statusEvent
->data
.l
[4] != None
) {
1107 /* destination accept drop */
1108 storeDropAction(info
, statusEvent
->data
.l
[4]);
1110 XDND_DEST_ACTION(info
) = None
;
1116 idleState(WMView
*view
, XClientMessageEvent
*event
, WMDraggingInfo
*info
)
1119 Atom destMsg
= event
->message_type
;
1121 scr
= W_VIEW_SCREEN(view
);
1123 if (destMsg
== scr
->xdndStatusAtom
) {
1124 storeStatusMessageInfos(info
, event
);
1126 if (XDND_DEST_ACTION(info
) != None
) {
1127 recolorCursor(info
, True
);
1128 W_DragSourceStartTimer(info
);
1129 return dropAllowedState
;
1132 recolorCursor(info
, False
);
1137 if (destMsg
== scr
->xdndFinishedAtom
) {
1138 wwarning("received xdndFinishedAtom before drop began");
1141 W_DragSourceStartTimer(info
);
1147 dropAllowedState(WMView
*view
, XClientMessageEvent
*event
, WMDraggingInfo
*info
)
1149 WMScreen
*scr
= W_VIEW_SCREEN(view
);
1150 Atom destMsg
= event
->message_type
;
1152 if (destMsg
== scr
->xdndStatusAtom
) {
1153 storeStatusMessageInfos(info
, event
);
1155 if (XDND_DEST_ACTION(info
) == None
) {
1157 recolorCursor(info
, False
);
1162 W_DragSourceStartTimer(info
);
1163 return dropAllowedState
;
1168 finishDropState(WMView
*view
, XClientMessageEvent
*event
, WMDraggingInfo
*info
)
1170 WMScreen
*scr
= W_VIEW_SCREEN(view
);
1171 Atom destMsg
= event
->message_type
;
1173 if (destMsg
== scr
->xdndFinishedAtom
) {
1174 endDragProcess(info
, True
);
1178 W_DragSourceStartTimer(info
);
1179 return finishDropState
;
1181 /* ----- end of source states ----- */
1184 /* ----- source timer ----- */
1186 dragSourceResponseTimeOut(void *source
)
1188 WMView
*view
= (WMView
*)source
;
1189 WMDraggingInfo
*info
= &(W_VIEW_SCREEN(view
)->dragInfo
);
1191 wwarning("delay for drag destination response expired");
1192 sendLeaveMessage(info
);
1194 recolorCursor(info
, False
);
1195 if (XDND_SOURCE_STATE(info
) == finishDropState
) {
1197 endDragImage(info
, True
);
1198 endDragProcess(info
, False
);
1200 XDND_SOURCE_STATE(info
) = idleState
;
1205 W_DragSourceStopTimer()
1207 if (dndSourceTimer
!= NULL
) {
1208 WMDeleteTimerHandler(dndSourceTimer
);
1209 dndSourceTimer
= NULL
;
1214 W_DragSourceStartTimer(WMDraggingInfo
*info
)
1216 W_DragSourceStopTimer();
1218 dndSourceTimer
= WMAddTimerHandler(
1219 XDND_DESTINATION_RESPONSE_MAX_DELAY
,
1220 dragSourceResponseTimeOut
,
1221 XDND_SOURCE_VIEW(info
));
1224 /* ----- End of Destination timer ----- */
1228 W_DragSourceStateHandler(WMDraggingInfo
*info
, XClientMessageEvent
*event
)
1231 W_DndState
* newState
;
1233 if (XDND_SOURCE_VIEW_STORED(info
)) {
1234 view
= XDND_SOURCE_VIEW(info
);
1237 printf("current source state: %s\n",
1238 stateName(XDND_SOURCE_STATE(info
)));
1241 newState
= (W_DndState
*) XDND_SOURCE_STATE(info
)(view
, event
, info
);
1245 printf("new source state: %s\n", stateName(newState
));
1248 if (newState
!= NULL
)
1249 XDND_SOURCE_STATE(info
) = newState
;
1250 /* else drop finished, and info has been flushed */
1255 void WMSetViewDragImage(WMView
* view
, WMPixmap
*dragImage
)
1257 if (view
->dragImage
!= NULL
)
1258 WMReleasePixmap(view
->dragImage
);
1260 view
->dragImage
= WMRetainPixmap(dragImage
);
1264 void WMReleaseViewDragImage(WMView
* view
)
1266 if (view
->dragImage
!= NULL
)
1267 WMReleasePixmap(view
->dragImage
);
1271 /* Create a drag handler, associating drag event masks with dragEventProc */
1273 WMCreateDragHandler(WMView
*view
, WMEventProc
*dragEventProc
, void *clientData
)
1275 WMCreateEventHandler(view
,
1276 ButtonPressMask
|ButtonReleaseMask
|Button1MotionMask
,
1277 dragEventProc
, clientData
);
1282 WMDeleteDragHandler(WMView
*view
, WMEventProc
*dragEventProc
, void *clientData
)
1284 WMDeleteEventHandler(view
,
1285 ButtonPressMask
|ButtonReleaseMask
|Button1MotionMask
,
1286 dragEventProc
, clientData
);
1290 /* set default drag handler for view */
1292 WMSetViewDraggable(WMView
*view
, WMDragSourceProcs
*dragSourceProcs
,
1293 WMPixmap
*dragImage
)
1295 wassertr(dragImage
!= NULL
);
1296 view
->dragImage
= WMRetainPixmap(dragImage
);
1298 WMSetViewDragSourceProcs(view
, dragSourceProcs
);
1300 WMCreateDragHandler(view
, dragImageHandler
, view
);
1305 WMUnsetViewDraggable(WMView
*view
)
1307 if (view
->dragSourceProcs
) {
1308 wfree(view
->dragSourceProcs
);
1309 view
->dragSourceProcs
= NULL
;
1312 WMReleaseViewDragImage(view
);
1314 WMDeleteDragHandler(view
, dragImageHandler
, view
);