reverted the last test commit (duh)
[wmaker-crm.git] / WINGs / dragsource.c
blobb08ee783644d5ef93f15b02391cfe0b395cfce73
1 #include "../src/config.h"
3 #include <X11/Xatom.h>
4 #include <X11/cursorfont.h>
5 #ifdef SHAPE
6 #include <X11/extensions/shape.h>
7 #endif
9 #include "WINGsP.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);
49 #ifdef XDND_DEBUG
50 static const char*
51 stateName(W_DndState *state)
53 if (state == NULL)
54 return "no state defined";
56 if (state == idleState)
57 return "idleState";
59 if (state == dropAllowedState)
60 return "dropAllowedState";
62 if (state == finishDropState)
63 return "finishDropState";
65 return "unknown state";
67 #endif
70 static WMScreen*
71 sourceScreen(WMDraggingInfo *info)
73 return W_VIEW_SCREEN(XDND_SOURCE_VIEW(info));
77 static void
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,
86 CurrentTime);
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),
98 deposited);
101 /* clear remaining draggging infos */
102 wfree(XDND_SOURCE_INFO(info));
103 XDND_SOURCE_INFO(info) = NULL;
107 /* ----- drag cursor ----- */
108 static void
109 initDragCursor(WMDraggingInfo *info)
111 WMScreen *scr = sourceScreen(info);
112 XColor cursorFgColor, cursorBgColor;
114 /* green */
115 cursorFgColor.red = 0x4500;
116 cursorFgColor.green = 0xb000;
117 cursorFgColor.blue = 0x4500;
119 /* white */
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),
127 &cursorFgColor,
128 &cursorBgColor);
130 XFlush(scr->display);
133 static void
134 recolorCursor(WMDraggingInfo *info, Bool dropIsAllowed)
136 WMScreen *scr = sourceScreen(info);
138 if (dropIsAllowed) {
139 XDefineCursor(scr->display,
140 scr->rootWin,
141 XDND_DRAG_CURSOR(info));
142 } else {
143 XDefineCursor(scr->display,
144 scr->rootWin,
145 scr->defaultCursor);
148 XFlush(scr->display);
150 /* ----- end of drag cursor ----- */
153 /* ----- selection procs ----- */
154 static WMData*
155 convertSelection(WMView *view, Atom selection, Atom target,
156 void *cdata, Atom *type)
158 WMScreen *scr;
159 WMData *data;
160 char *typeName;
162 scr = W_VIEW_SCREEN(view);
163 typeName = XGetAtomName(scr->display, target);
165 *type = target;
167 if (view->dragSourceProcs->fetchDragData != NULL) {
168 data = view->dragSourceProcs->fetchDragData(
169 view,
170 typeName);
171 } else {
172 data = NULL;
175 if (typeName != NULL)
176 XFree(typeName);
178 return data;
182 static void
183 selectionLost(WMView *view, Atom selection, void *cdata)
185 wwarning("DND selection lost during drag operation...");
189 static void
190 selectionDone(WMView *view, Atom selection, Atom target, void *cdata)
192 #ifdef XDND_DEBUG
193 printf("selection done\n");
194 #endif
196 /* ----- end of selection procs ----- */
199 /* ----- visual part ----- */
201 static Window
202 makeDragIcon(WMScreen *scr, WMPixmap *pixmap)
204 Window window;
205 WMSize size;
206 unsigned long flags;
207 XSetWindowAttributes attribs;
208 Pixmap pix, mask;
210 if (!pixmap) {
211 pixmap = scr->defaultObjectIcon;
214 size = WMGetPixmapSize(pixmap);
215 pix = pixmap->pixmap;
216 mask = pixmap->mask;
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);
228 #ifdef SHAPE
230 if (mask) {
231 XShapeCombineMask(scr->display, window, ShapeBounding, 0, 0, mask,
232 ShapeSet);
234 #endif
236 return window;
240 static void
241 slideWindow(Display *dpy, Window win, int srcX, int srcY, int dstX, int dstY)
243 double x, y, dx, dy;
244 int i;
245 int iterations;
247 iterations = WMIN(MAX_SLIDEBACK_ITER,
248 WMAX(abs(dstX-srcX), abs(dstY-srcY)));
250 x = srcX;
251 y = 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);
258 XFlush(dpy);
260 wusleep(800);
262 x += dx;
263 y += dy;
268 static int
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;
275 } else {
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 */
282 return viewCoord;
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;
290 else
291 return mouseCoord - iconSize/2;
296 static void
297 initDragImagePos(WMView *view, WMDraggingInfo *info, XEvent *event)
299 WMSize iconSize = WMGetPixmapSize(view->dragImage);
300 WMSize viewSize = WMGetViewSize(view);
301 WMPoint viewPos;
302 Window foo;
304 XTranslateCoordinates(W_VIEW_SCREEN(view)->display,
305 WMViewXID(view), W_VIEW_SCREEN(view)->rootWin,
306 0, 0, &(viewPos.x), &(viewPos.y),
307 &foo);
309 /* set icon pos */
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;
326 static void
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);
337 static void
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);
351 static void
352 endDragImage(WMDraggingInfo *info, Bool slideBack)
354 WMView *view = XDND_SOURCE_VIEW(info);
355 Display *dpy = W_VIEW_SCREEN(view)->display;
357 if (slideBack) {
358 WMPoint toLocation;
359 Window foo;
361 XTranslateCoordinates(W_VIEW_SCREEN(view)->display,
362 WMViewXID(view), W_VIEW_SCREEN(view)->rootWin,
363 0, 0, &(toLocation.x), &(toLocation.y),
364 &foo);
366 slideWindow(dpy, XDND_DRAG_ICON(info),
367 XDND_DRAG_ICON_POS(info).x,
368 XDND_DRAG_ICON_POS(info).y,
369 toLocation.x,
370 toLocation.y);
373 XDestroyWindow(dpy, XDND_DRAG_ICON(info));
376 /* ----- end of visual part ----- */
379 /* ----- messages ----- */
381 /* send a DnD message to the destination window */
382 static Bool
383 sendDnDClientMessage(WMDraggingInfo *info, Atom message,
384 unsigned long data1,
385 unsigned long data2,
386 unsigned long data3,
387 unsigned long data4)
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,
394 destWin,
395 message,
396 srcWin,
397 data1,
398 data2,
399 data3,
400 data4)) {
401 /* drop failed */
402 recolorCursor(info, False);
403 endDragImage(info, True);
404 endDragProcess(info, False);
405 return False;
408 return True;
412 static Bool
413 sendEnterMessage(WMDraggingInfo *info)
415 WMScreen *scr = sourceScreen(info);
416 unsigned long data1;
418 data1 = (VERSION_INFO(info) << 24)|1; /* 1: support of type list */
420 return sendDnDClientMessage(info, scr->xdndEnterAtom,
421 data1,
422 XDND_3_TYPES(info)[0],
423 XDND_3_TYPES(info)[1],
424 XDND_3_TYPES(info)[2]);
428 static Bool
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));
446 } else {
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));
457 static Bool
458 sendLeaveMessage(WMDraggingInfo *info)
460 WMScreen *scr = sourceScreen(info);
462 return sendDnDClientMessage(info, scr->xdndLeaveAtom,
463 0, 0, 0, 0);
467 static Bool
468 sendDropMessage(WMDraggingInfo *info)
470 WMScreen *scr = sourceScreen(info);
472 return sendDnDClientMessage(info,
473 scr->xdndDropAtom,
475 XDND_TIMESTAMP(info),
476 0, 0);
479 /* ----- end of messages ----- */
482 static Atom*
483 getTypeAtomList(WMScreen *scr, WMView *view, int* count)
485 WMArray* types;
486 Atom* typeAtoms;
487 int i;
489 types = view->dragSourceProcs->dropDataTypes(view);
491 if (types != NULL) {
492 *count = WMGetArrayItemCount(types);
493 if (*count > 0) {
494 typeAtoms = wmalloc((*count)*sizeof(Atom));
495 for (i=0; i < *count; i++) {
496 typeAtoms[i] = XInternAtom(scr->display,
497 WMGetFromArray(types, i),
498 False);
501 /* WMFreeArray(types); */
502 return typeAtoms;
505 /* WMFreeArray(types); */
508 *count = 1;
509 typeAtoms = wmalloc(sizeof(Atom));
510 *typeAtoms = None;
512 return typeAtoms;
516 static void
517 registerDropTypes(WMScreen *scr, WMView *view, WMDraggingInfo *info)
519 Atom* typeList;
520 int i, count;
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];
528 for(; i < 3; i++)
529 XDND_3_TYPES(info)[i] = None;
532 /* store the entire type list */
533 XChangeProperty(scr->display,
534 WMViewXID(view),
535 scr->xdndTypeListAtom,
536 XA_ATOM,
537 XDND_PROPERTY_FORMAT,
538 PropModeReplace,
539 (unsigned char*) typeList,
540 count);
544 static void
545 registerOperationList(WMScreen *scr, WMView *view, WMArray* operationArray)
547 Atom* actionList;
548 WMDragOperationType operation;
549 int count = WMGetArrayItemCount(operationArray);
550 int i;
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,
561 WMViewXID(view),
562 scr->xdndActionListAtom,
563 XA_ATOM,
564 XDND_PROPERTY_FORMAT,
565 PropModeReplace,
566 (unsigned char*) actionList,
567 count);
570 static void
571 registerDescriptionList(WMScreen *scr, WMView *view, WMArray* operationArray)
573 char *text, *textListItem, *textList;
574 int count = WMGetArrayItemCount(operationArray);
575 int i;
576 int size = 0;
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,
597 WMViewXID(view),
598 scr->xdndActionDescriptionAtom,
599 XA_STRING,
600 XDND_ACTION_DESCRIPTION_FORMAT,
601 PropModeReplace,
602 (unsigned char*) textList,
603 size);
606 /* called if wanted operation is WDOperationAsk */
607 static void
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); */
622 static void
623 initSourceDragInfo(WMView *sourceView, WMDraggingInfo *info)
625 WMRect emptyZone;
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
650 static WMArray*
651 defDropDataTypes(WMView *self)
653 return NULL;
657 static WMDragOperationType
658 defWantedDropOperation(WMView *self)
660 return WDOperationNone;
665 Must be defined if wantedDropOperation return WDOperationAsk
666 (useless otherwise).
667 Return a WMDragOperationItem array (destroyed after call).
668 A WMDragOperationItem links a label to an operation.
669 static WMArray*
670 defAskedOperations(WMView *self); */
673 static Bool
674 defAcceptDropOperation(WMView *self, WMDragOperationType allowedOperation)
676 return False;
680 static void
681 defBeganDrag(WMView *self, WMPoint *point)
686 static void
687 defEndedDrag(WMView *self, WMPoint *point, Bool deposited)
693 Returned data is not destroyed
695 static WMData*
696 defFetchDragData(WMView *self, char *type)
698 return NULL;
702 void
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
719 WDOperationAsk.
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;
736 static Bool
737 isXdndAware(WMScreen *scr, Window win)
739 Atom type;
740 int format;
741 unsigned long count, remain;
742 unsigned char *winXdndVersion;
743 unsigned char *proxy;
745 if (win == None)
746 return False;
748 XGetWindowProperty(scr->display, win, scr->xdndAwareAtom,
749 0, 1, False, XA_ATOM, &type, &format,
750 &count, &remain, &winXdndVersion);
752 if (type != XA_ATOM
753 || format != XDND_PROPERTY_FORMAT
754 || count == 0 || !winXdndVersion) {
755 if (winXdndVersion)
756 XFree(winXdndVersion);
757 return False;
760 XFree(winXdndVersion);
761 return (count == 1); /* xdnd version is set */
765 static Window*
766 windowChildren(Display *dpy, Window win, unsigned *nchildren)
768 Window *children;
769 Window foo, bar;
771 if (!XQueryTree(dpy, win, &foo, &bar, &children, nchildren)) {
772 *nchildren = 0;
773 return NULL;
774 } else
775 return children;
778 static Window
779 lookForAwareWindow(WMScreen *scr, WMPoint *mousePos, Window win)
781 int tmpx, tmpy;
782 Window child;
784 /* Since xdnd v3, only the toplevel window should be aware */
785 if (isXdndAware(scr, win))
786 return win;
788 /* inspect child under pointer */
789 if (XTranslateCoordinates(scr->display, scr->rootWin, win,
790 mousePos->x, mousePos->y, &tmpx, &tmpy,
791 &child)) {
792 if (child == None)
793 return None;
794 else
795 return lookForAwareWindow(scr, mousePos, child);
798 return None;
802 static Window
803 findDestination(WMDraggingInfo *info, WMPoint *mousePos)
805 WMScreen *scr = sourceScreen(info);
806 unsigned nchildren;
807 Window *children = windowChildren(scr->display, scr->rootWin, &nchildren);
808 int i;
809 XWindowAttributes attr;
811 if (isXdndAware(scr, scr->rootWin))
812 return 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)) {
817 i--;
818 break;
822 if (i < 0) {
823 /* root window has no child under drag icon, and is not xdnd aware. */
824 return None;
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 */
840 return None;
844 static void
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,
859 CurrentTime,
860 XDND_SELECTION_PROCS(info), NULL)) {
861 wwarning("could not get ownership or DND selection");
862 return;
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);
876 static void
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;
902 return;
905 W_DragSourceStartTimer(info);
907 } else {
908 if (XDND_DEST_WIN(info) != None) {
909 if (! sendPositionMessage(info, mousePos)) {
910 XDND_DEST_WIN(info) = None;
911 return;
914 W_DragSourceStartTimer(info);
920 static Bool
921 processButtonRelease(WMDraggingInfo *info)
923 if (XDND_SOURCE_STATE(info) == dropAllowedState) {
924 /* begin drop */
925 W_DragSourceStopTimer();
927 if (! sendDropMessage(info))
928 return False;
930 W_DragSourceStartTimer(info);
931 return True;
932 } else {
933 if (XDND_DEST_WIN(info) != None)
934 sendLeaveMessage(info);
936 W_DragSourceStopTimer();
937 return False;
942 Bool
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; */
953 void
954 WMDragImageFromView(WMView *view, XEvent *event)
956 WMDraggingInfo *info = &W_VIEW_SCREEN(view)->dragInfo;
957 WMPoint mouseLocation;
959 switch(event->type) {
960 case ButtonPress:
961 if (event->xbutton.button == Button1) {
962 XEvent nextEvent;
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);
972 break;
974 case ButtonRelease:
975 if (WMIsDraggingFromView(view)) {
976 Bool dropBegan = processButtonRelease(info);
978 recolorCursor(info, False);
979 if (dropBegan) {
980 endDragImage(info, False);
981 XDND_SOURCE_STATE(info) = finishDropState;
982 } else {
983 /* drop failed */
984 endDragImage(info, True);
985 endDragProcess(info,False);
988 break;
990 case MotionNotify:
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) >=
996 MIN_X_MOVE_OFFSET
997 || abs(XDND_DRAG_ICON_POS(info).y - mouseLocation.y) >=
998 MIN_Y_MOVE_OFFSET) {
999 if (XDND_DRAG_ICON(info) == None) {
1000 initMotionProcess(view, info, event, &mouseLocation);
1001 startDragImage(view, info, event);
1002 } else {
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);
1009 processMotion(info,
1010 event->xmotion.window,
1011 &mouseLocation);
1015 break;
1020 /* Minimal mouse events handler: no right or double-click detection,
1021 only drag is supported */
1022 static void
1023 dragImageHandler(XEvent *event, void *cdata)
1025 WMView *view = (WMView*)cdata;
1027 WMDragImageFromView(view, event);
1031 /* ----- source states ----- */
1033 #ifdef XDND_DEBUG
1034 static void
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");
1041 else {
1042 int x, y, w, h;
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",
1049 x, y, x+w, y+h);
1052 if (statusEvent->data.l[1] & 0x1L)
1053 printf("allowed action: %s\n",
1054 XGetAtomName(dpy, statusEvent->data.l[4]));
1055 else
1056 printf("no action allowed\n");
1058 #endif
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(
1068 sourceView,
1069 W_ActionToOperation(scr, destAction)))
1070 XDND_DEST_ACTION(info) = destAction;
1071 else
1072 XDND_DEST_ACTION(info) = None;
1073 } else {
1074 XDND_DEST_ACTION(info) = destAction;
1079 static void
1080 storeStatusMessageInfos(WMDraggingInfo *info, XClientMessageEvent *statusEvent)
1082 WMRect* noPosZone = &(XDND_NO_POS_ZONE(info));
1084 #ifdef XDND_DEBUG
1086 traceStatusMsg(sourceScreen(info)->display, statusEvent);
1087 #endif
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;
1093 } else {
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]);
1104 } else {
1105 XDND_DEST_ACTION(info) = None;
1110 static void*
1111 idleState(WMView *view, XClientMessageEvent *event, WMDraggingInfo *info)
1113 WMScreen *scr;
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;
1125 } else {
1126 /* drop denied */
1127 recolorCursor(info, False);
1128 return idleState;
1132 if (destMsg == scr->xdndFinishedAtom) {
1133 wwarning("received xdndFinishedAtom before drop began");
1136 W_DragSourceStartTimer(info);
1137 return idleState;
1141 static void*
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) {
1151 /* drop denied */
1152 recolorCursor(info, False);
1153 return idleState;
1157 W_DragSourceStartTimer(info);
1158 return dropAllowedState;
1162 static void*
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);
1170 return NULL;
1173 W_DragSourceStartTimer(info);
1174 return finishDropState;
1176 /* ----- end of source states ----- */
1179 /* ----- source timer ----- */
1180 static void
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) {
1191 /* drop failed */
1192 endDragImage(info, True);
1193 endDragProcess(info, False);
1194 } else {
1195 XDND_SOURCE_STATE(info) = idleState;
1199 void
1200 W_DragSourceStopTimer()
1202 if (dndSourceTimer != NULL) {
1203 WMDeleteTimerHandler(dndSourceTimer);
1204 dndSourceTimer = NULL;
1208 void
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 ----- */
1222 void
1223 W_DragSourceStateHandler(WMDraggingInfo *info, XClientMessageEvent *event)
1225 WMView *view;
1226 W_DndState* newState;
1228 if (XDND_SOURCE_VIEW_STORED(info)) {
1229 view = XDND_SOURCE_VIEW(info);
1230 #ifdef XDND_DEBUG
1232 printf("current source state: %s\n",
1233 stateName(XDND_SOURCE_STATE(info)));
1234 #endif
1236 newState = (W_DndState*) XDND_SOURCE_STATE(info)(view, event, info);
1238 #ifdef XDND_DEBUG
1240 printf("new source state: %s\n", stateName(newState));
1241 #endif
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 */
1267 void
1268 WMCreateDragHandler(WMView *view, WMEventProc *dragEventProc, void *clientData)
1270 WMCreateEventHandler(view,
1271 ButtonPressMask|ButtonReleaseMask|Button1MotionMask,
1272 dragEventProc, clientData);
1276 void
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 */
1286 void
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);
1299 void
1300 WMUnsetViewDraggable(WMView *view)
1302 if (view->dragSourceProcs) {
1303 wfree(view->dragSourceProcs);
1304 view->dragSourceProcs = NULL;
1307 WMReleaseViewDragImage(view);
1309 WMDeleteDragHandler(view, dragImageHandler, view);