Changed the order some header files are loaded, as it seems to cause havoc
[wmaker-crm.git] / WINGs / dragsource.c
blob50ecdf063d0e5cdb34e581d30fe7d766b48decab
3 #include "wconfig.h"
4 #include "WINGsP.h"
6 #include <X11/Xatom.h>
7 #include <X11/cursorfont.h>
8 #ifdef SHAPE
9 #include <X11/extensions/shape.h>
10 #endif
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);
51 #ifdef XDND_DEBUG
52 static const char*
53 stateName(W_DndState *state)
55 if (state == NULL)
56 return "no state defined";
58 if (state == idleState)
59 return "idleState";
61 if (state == dropAllowedState)
62 return "dropAllowedState";
64 if (state == finishDropState)
65 return "finishDropState";
67 return "unknown state";
69 #endif
72 static WMScreen*
73 sourceScreen(WMDraggingInfo *info)
75 return W_VIEW_SCREEN(XDND_SOURCE_VIEW(info));
79 static void
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,
88 CurrentTime);
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),
100 deposited);
103 /* clear remaining draggging infos */
104 wfree(XDND_SOURCE_INFO(info));
105 XDND_SOURCE_INFO(info) = NULL;
109 /* ----- drag cursor ----- */
110 static void
111 initDragCursor(WMDraggingInfo *info)
113 WMScreen *scr = sourceScreen(info);
114 XColor cursorFgColor, cursorBgColor;
116 /* green */
117 cursorFgColor.red = 0x4500;
118 cursorFgColor.green = 0xb000;
119 cursorFgColor.blue = 0x4500;
121 /* white */
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),
129 &cursorFgColor,
130 &cursorBgColor);
132 XFlush(scr->display);
135 static void
136 recolorCursor(WMDraggingInfo *info, Bool dropIsAllowed)
138 WMScreen *scr = sourceScreen(info);
140 if (dropIsAllowed) {
141 XDefineCursor(scr->display,
142 scr->rootWin,
143 XDND_DRAG_CURSOR(info));
144 } else {
145 XDefineCursor(scr->display,
146 scr->rootWin,
147 scr->defaultCursor);
150 XFlush(scr->display);
152 /* ----- end of drag cursor ----- */
155 /* ----- selection procs ----- */
156 static WMData*
157 convertSelection(WMView *view, Atom selection, Atom target,
158 void *cdata, Atom *type)
160 WMScreen *scr;
161 WMData *data;
162 char *typeName;
164 scr = W_VIEW_SCREEN(view);
165 typeName = XGetAtomName(scr->display, target);
167 *type = target;
169 if (view->dragSourceProcs->fetchDragData != NULL) {
170 data = view->dragSourceProcs->fetchDragData(
171 view,
172 typeName);
173 } else {
174 data = NULL;
177 if (typeName != NULL)
178 XFree(typeName);
180 return data;
184 static void
185 selectionLost(WMView *view, Atom selection, void *cdata)
187 wwarning("DND selection lost during drag operation...");
191 static void
192 selectionDone(WMView *view, Atom selection, Atom target, void *cdata)
194 #ifdef XDND_DEBUG
195 printf("selection done\n");
196 #endif
198 /* ----- end of selection procs ----- */
201 /* ----- visual part ----- */
203 static Window
204 makeDragIcon(WMScreen *scr, WMPixmap *pixmap)
206 Window window;
207 WMSize size;
208 unsigned long flags;
209 XSetWindowAttributes attribs;
210 Pixmap pix, mask;
212 if (!pixmap) {
213 pixmap = scr->defaultObjectIcon;
216 size = WMGetPixmapSize(pixmap);
217 pix = pixmap->pixmap;
218 mask = pixmap->mask;
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);
230 #ifdef SHAPE
232 if (mask) {
233 XShapeCombineMask(scr->display, window, ShapeBounding, 0, 0, mask,
234 ShapeSet);
236 #endif
238 return window;
242 static void
243 slideWindow(Display *dpy, Window win, int srcX, int srcY, int dstX, int dstY)
245 double x, y, dx, dy;
246 int i;
247 int iterations;
249 iterations = WMIN(MAX_SLIDEBACK_ITER,
250 WMAX(abs(dstX-srcX), abs(dstY-srcY)));
252 x = srcX;
253 y = 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);
260 XFlush(dpy);
262 wusleep(800);
264 x += dx;
265 y += dy;
270 static int
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;
277 } else {
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 */
284 return viewCoord;
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;
292 else
293 return mouseCoord - iconSize/2;
298 static void
299 initDragImagePos(WMView *view, WMDraggingInfo *info, XEvent *event)
301 WMSize iconSize = WMGetPixmapSize(view->dragImage);
302 WMSize viewSize = WMGetViewSize(view);
303 WMPoint viewPos;
304 Window foo;
306 XTranslateCoordinates(W_VIEW_SCREEN(view)->display,
307 WMViewXID(view), W_VIEW_SCREEN(view)->rootWin,
308 0, 0, &(viewPos.x), &(viewPos.y),
309 &foo);
311 /* set icon pos */
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;
328 static void
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);
339 static void
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);
353 static void
354 endDragImage(WMDraggingInfo *info, Bool slideBack)
356 WMView *view = XDND_SOURCE_VIEW(info);
357 Display *dpy = W_VIEW_SCREEN(view)->display;
359 if (slideBack) {
360 WMPoint toLocation;
361 Window foo;
363 XTranslateCoordinates(W_VIEW_SCREEN(view)->display,
364 WMViewXID(view), W_VIEW_SCREEN(view)->rootWin,
365 0, 0, &(toLocation.x), &(toLocation.y),
366 &foo);
368 slideWindow(dpy, XDND_DRAG_ICON(info),
369 XDND_DRAG_ICON_POS(info).x,
370 XDND_DRAG_ICON_POS(info).y,
371 toLocation.x,
372 toLocation.y);
375 XDestroyWindow(dpy, XDND_DRAG_ICON(info));
378 /* ----- end of visual part ----- */
381 /* ----- messages ----- */
383 /* send a DnD message to the destination window */
384 static Bool
385 sendDnDClientMessage(WMDraggingInfo *info, Atom message,
386 unsigned long data1,
387 unsigned long data2,
388 unsigned long data3,
389 unsigned long data4)
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,
396 destWin,
397 message,
398 srcWin,
399 data1,
400 data2,
401 data3,
402 data4)) {
403 /* drop failed */
404 recolorCursor(info, False);
405 endDragImage(info, True);
406 endDragProcess(info, False);
407 return False;
410 return True;
414 static Bool
415 sendEnterMessage(WMDraggingInfo *info)
417 WMScreen *scr = sourceScreen(info);
418 unsigned long data1;
420 data1 = (VERSION_INFO(info) << 24)|1; /* 1: support of type list */
422 return sendDnDClientMessage(info, scr->xdndEnterAtom,
423 data1,
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 */
433 static Bool
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));
451 } else {
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));
462 static Bool
463 sendLeaveMessage(WMDraggingInfo *info)
465 WMScreen *scr = sourceScreen(info);
467 return sendDnDClientMessage(info, scr->xdndLeaveAtom,
468 0, 0, 0, 0);
472 static Bool
473 sendDropMessage(WMDraggingInfo *info)
475 WMScreen *scr = sourceScreen(info);
477 return sendDnDClientMessage(info,
478 scr->xdndDropAtom,
480 XDND_TIMESTAMP(info),
481 0, 0);
484 /* ----- end of messages ----- */
487 static Atom*
488 getTypeAtomList(WMScreen *scr, WMView *view, int* count)
490 WMArray* types;
491 Atom* typeAtoms;
492 int i;
494 types = view->dragSourceProcs->dropDataTypes(view);
496 if (types != NULL) {
497 *count = WMGetArrayItemCount(types);
498 if (*count > 0) {
499 typeAtoms = wmalloc((*count)*sizeof(Atom));
500 for (i=0; i < *count; i++) {
501 typeAtoms[i] = XInternAtom(scr->display,
502 WMGetFromArray(types, i),
503 False);
506 /* WMFreeArray(types); */
507 return typeAtoms;
510 /* WMFreeArray(types); */
513 *count = 1;
514 typeAtoms = wmalloc(sizeof(Atom));
515 *typeAtoms = None;
517 return typeAtoms;
521 static void
522 registerDropTypes(WMScreen *scr, WMView *view, WMDraggingInfo *info)
524 Atom* typeList;
525 int i, count;
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];
533 for(; i < 3; i++)
534 XDND_3_TYPES(info)[i] = None;
537 /* store the entire type list */
538 XChangeProperty(scr->display,
539 WMViewXID(view),
540 scr->xdndTypeListAtom,
541 XA_ATOM,
542 XDND_PROPERTY_FORMAT,
543 PropModeReplace,
544 (unsigned char*) typeList,
545 count);
549 static void
550 registerOperationList(WMScreen *scr, WMView *view, WMArray* operationArray)
552 Atom* actionList;
553 WMDragOperationType operation;
554 int count = WMGetArrayItemCount(operationArray);
555 int i;
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,
566 WMViewXID(view),
567 scr->xdndActionListAtom,
568 XA_ATOM,
569 XDND_PROPERTY_FORMAT,
570 PropModeReplace,
571 (unsigned char*) actionList,
572 count);
575 static void
576 registerDescriptionList(WMScreen *scr, WMView *view, WMArray* operationArray)
578 char *text, *textListItem, *textList;
579 int count = WMGetArrayItemCount(operationArray);
580 int i;
581 int size = 0;
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,
602 WMViewXID(view),
603 scr->xdndActionDescriptionAtom,
604 XA_STRING,
605 XDND_ACTION_DESCRIPTION_FORMAT,
606 PropModeReplace,
607 (unsigned char*) textList,
608 size);
611 /* called if wanted operation is WDOperationAsk */
612 static void
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); */
627 static void
628 initSourceDragInfo(WMView *sourceView, WMDraggingInfo *info)
630 WMRect emptyZone;
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
655 static WMArray*
656 defDropDataTypes(WMView *self)
658 return NULL;
662 static WMDragOperationType
663 defWantedDropOperation(WMView *self)
665 return WDOperationNone;
670 Must be defined if wantedDropOperation return WDOperationAsk
671 (useless otherwise).
672 Return a WMDragOperationItem array (destroyed after call).
673 A WMDragOperationItem links a label to an operation.
674 static WMArray*
675 defAskedOperations(WMView *self); */
678 static Bool
679 defAcceptDropOperation(WMView *self, WMDragOperationType allowedOperation)
681 return False;
685 static void
686 defBeganDrag(WMView *self, WMPoint *point)
691 static void
692 defEndedDrag(WMView *self, WMPoint *point, Bool deposited)
698 Returned data is not destroyed
700 static WMData*
701 defFetchDragData(WMView *self, char *type)
703 return NULL;
707 void
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
724 WDOperationAsk.
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;
741 static Bool
742 isXdndAware(WMScreen *scr, Window win)
744 Atom type;
745 int format;
746 unsigned long count, remain;
747 unsigned char *winXdndVersion;
749 if (win == None)
750 return False;
752 XGetWindowProperty(scr->display, win, scr->xdndAwareAtom,
753 0, 1, False, XA_ATOM, &type, &format,
754 &count, &remain, &winXdndVersion);
756 if (type != XA_ATOM
757 || format != XDND_PROPERTY_FORMAT
758 || count == 0 || !winXdndVersion) {
759 if (winXdndVersion)
760 XFree(winXdndVersion);
761 return False;
764 XFree(winXdndVersion);
765 return (count == 1); /* xdnd version is set */
769 static Window*
770 windowChildren(Display *dpy, Window win, unsigned *nchildren)
772 Window *children;
773 Window foo, bar;
775 if (!XQueryTree(dpy, win, &foo, &bar, &children, nchildren)) {
776 *nchildren = 0;
777 return NULL;
778 } else
779 return children;
782 static Window
783 lookForAwareWindow(WMScreen *scr, WMPoint *mousePos, Window win)
785 int tmpx, tmpy;
786 Window child;
788 /* Since xdnd v3, only the toplevel window should be aware */
789 if (isXdndAware(scr, win))
790 return win;
792 /* inspect child under pointer */
793 if (XTranslateCoordinates(scr->display, scr->rootWin, win,
794 mousePos->x, mousePos->y, &tmpx, &tmpy,
795 &child)) {
796 if (child == None)
797 return None;
798 else
799 return lookForAwareWindow(scr, mousePos, child);
802 return None;
806 static Window
807 findDestination(WMDraggingInfo *info, WMPoint *mousePos)
809 WMScreen *scr = sourceScreen(info);
810 unsigned nchildren;
811 Window *children = windowChildren(scr->display, scr->rootWin, &nchildren);
812 int i;
813 XWindowAttributes attr;
815 if (isXdndAware(scr, scr->rootWin))
816 return 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)) {
821 i--;
822 break;
826 if (i < 0) {
827 /* root window has no child under drag icon, and is not xdnd aware. */
828 return None;
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 */
844 return None;
848 static void
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,
863 CurrentTime,
864 XDND_SELECTION_PROCS(info), NULL)) {
865 wwarning("could not get ownership or DND selection");
866 return;
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);
880 static void
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;
906 return;
909 W_DragSourceStartTimer(info);
911 } else {
912 if (XDND_DEST_WIN(info) != None) {
913 if (! sendPositionMessage(info, mousePos)) {
914 XDND_DEST_WIN(info) = None;
915 return;
918 W_DragSourceStartTimer(info);
924 static Bool
925 processButtonRelease(WMDraggingInfo *info)
927 if (XDND_SOURCE_STATE(info) == dropAllowedState) {
928 /* begin drop */
929 W_DragSourceStopTimer();
931 if (! sendDropMessage(info))
932 return False;
934 W_DragSourceStartTimer(info);
935 return True;
936 } else {
937 if (XDND_DEST_WIN(info) != None)
938 sendLeaveMessage(info);
940 W_DragSourceStopTimer();
941 return False;
946 Bool
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; */
957 void
958 WMDragImageFromView(WMView *view, XEvent *event)
960 WMDraggingInfo *info = &W_VIEW_SCREEN(view)->dragInfo;
961 WMPoint mouseLocation;
963 switch(event->type) {
964 case ButtonPress:
965 if (event->xbutton.button == Button1) {
966 XEvent nextEvent;
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);
976 break;
978 case ButtonRelease:
979 if (WMIsDraggingFromView(view)) {
980 Bool dropBegan = processButtonRelease(info);
982 recolorCursor(info, False);
983 if (dropBegan) {
984 endDragImage(info, False);
985 XDND_SOURCE_STATE(info) = finishDropState;
986 } else {
987 /* drop failed */
988 endDragImage(info, True);
989 endDragProcess(info,False);
992 break;
994 case MotionNotify:
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) >=
1000 MIN_X_MOVE_OFFSET
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);
1006 } else {
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);
1013 processMotion(info,
1014 event->xmotion.window,
1015 &mouseLocation);
1019 break;
1024 /* Minimal mouse events handler: no right or double-click detection,
1025 only drag is supported */
1026 static void
1027 dragImageHandler(XEvent *event, void *cdata)
1029 WMView *view = (WMView*)cdata;
1031 WMDragImageFromView(view, event);
1035 /* ----- source states ----- */
1037 #ifdef XDND_DEBUG
1038 static void
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");
1045 else {
1046 int x, y, w, h;
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",
1053 x, y, x+w, y+h);
1056 if (statusEvent->data.l[1] & 0x1L)
1057 printf("allowed action: %s\n",
1058 XGetAtomName(dpy, statusEvent->data.l[4]));
1059 else
1060 printf("no action allowed\n");
1062 #endif
1065 static void
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(
1073 sourceView,
1074 W_ActionToOperation(scr, destAction)))
1075 XDND_DEST_ACTION(info) = destAction;
1076 else
1077 XDND_DEST_ACTION(info) = None;
1078 } else {
1079 XDND_DEST_ACTION(info) = destAction;
1084 static void
1085 storeStatusMessageInfos(WMDraggingInfo *info, XClientMessageEvent *statusEvent)
1087 WMRect* noPosZone = &(XDND_NO_POS_ZONE(info));
1089 #ifdef XDND_DEBUG
1091 traceStatusMsg(sourceScreen(info)->display, statusEvent);
1092 #endif
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;
1098 } else {
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]);
1109 } else {
1110 XDND_DEST_ACTION(info) = None;
1115 static void*
1116 idleState(WMView *view, XClientMessageEvent *event, WMDraggingInfo *info)
1118 WMScreen *scr;
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;
1130 } else {
1131 /* drop denied */
1132 recolorCursor(info, False);
1133 return idleState;
1137 if (destMsg == scr->xdndFinishedAtom) {
1138 wwarning("received xdndFinishedAtom before drop began");
1141 W_DragSourceStartTimer(info);
1142 return idleState;
1146 static void*
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) {
1156 /* drop denied */
1157 recolorCursor(info, False);
1158 return idleState;
1162 W_DragSourceStartTimer(info);
1163 return dropAllowedState;
1167 static void*
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);
1175 return NULL;
1178 W_DragSourceStartTimer(info);
1179 return finishDropState;
1181 /* ----- end of source states ----- */
1184 /* ----- source timer ----- */
1185 static void
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) {
1196 /* drop failed */
1197 endDragImage(info, True);
1198 endDragProcess(info, False);
1199 } else {
1200 XDND_SOURCE_STATE(info) = idleState;
1204 void
1205 W_DragSourceStopTimer()
1207 if (dndSourceTimer != NULL) {
1208 WMDeleteTimerHandler(dndSourceTimer);
1209 dndSourceTimer = NULL;
1213 void
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 ----- */
1227 void
1228 W_DragSourceStateHandler(WMDraggingInfo *info, XClientMessageEvent *event)
1230 WMView *view;
1231 W_DndState* newState;
1233 if (XDND_SOURCE_VIEW_STORED(info)) {
1234 view = XDND_SOURCE_VIEW(info);
1235 #ifdef XDND_DEBUG
1237 printf("current source state: %s\n",
1238 stateName(XDND_SOURCE_STATE(info)));
1239 #endif
1241 newState = (W_DndState*) XDND_SOURCE_STATE(info)(view, event, info);
1243 #ifdef XDND_DEBUG
1245 printf("new source state: %s\n", stateName(newState));
1246 #endif
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 */
1272 void
1273 WMCreateDragHandler(WMView *view, WMEventProc *dragEventProc, void *clientData)
1275 WMCreateEventHandler(view,
1276 ButtonPressMask|ButtonReleaseMask|Button1MotionMask,
1277 dragEventProc, clientData);
1281 void
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 */
1291 void
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);
1304 void
1305 WMUnsetViewDraggable(WMView *view)
1307 if (view->dragSourceProcs) {
1308 wfree(view->dragSourceProcs);
1309 view->dragSourceProcs = NULL;
1312 WMReleaseViewDragImage(view);
1314 WMDeleteDragHandler(view, dragImageHandler, view);