Debian Codegraph @

generic/bltUnixDnd.c

raw symbols ◀ ▶ SCIP
3 Debian patches touch this file
1
2 /*
3 * bltUnixDnd.c --
4 *
5 * This module implements a drag-and-drop manager for the BLT
6 * Toolkit. Allows widgets to be registered as drag&drop sources
7 * and targets for handling "drag-and-drop" operations between
8 * Tcl/Tk applications.
9 *
10 * Copyright 1993-1998 Lucent Technologies, Inc.
11 *
12 * Permission to use, copy, modify, and distribute this software and
13 * its documentation for any purpose and without fee is hereby
14 * granted, provided that the above copyright notice appear in all
15 * copies and that both that the copyright notice and warranty
16 * disclaimer appear in supporting documentation, and that the names
17 * of Lucent Technologies any of their entities not be used in
18 * advertising or publicity pertaining to distribution of the software
19 * without specific, written prior permission.
20 *
21 * Lucent Technologies disclaims all warranties with regard to this
22 * software, including all implied warranties of merchantability and
23 * fitness. In no event shall Lucent Technologies be liable for any
24 * special, indirect or consequential damages or any damages
25 * whatsoever resulting from loss of use, data or profits, whether in
26 * an action of contract, negligence or other tortuous action, arising
27 * out of or in connection with the use or performance of this
28 * software.
29 *
30 * The "drag&drop" command was created by Michael J. McLennan.
31 */
32
33 #include "bltInt.h"
34
35 #ifndef NO_DRAGDROP
36
37 #include <bltHash.h>
38 #include <bltChain.h>
39
40
41 #include <X11/Xatom.h>
42 #include <X11/Xproto.h>
43
44 #define DND_THREAD_KEY "BLT Dnd Data"
45
46 #define DND_SELECTED (1<<0)
47 #define DND_INITIATED (1<<1)
48 #define DND_ACTIVE (DND_SELECTED | DND_INITIATED)
49 #define DND_IN_PACKAGE (1<<2) /* Indicates if a token package command is
50 * currently active. The user may invoke
51 * "update" or "tkwait" commands from within
52 * the package command script. This allows the
53 * "drag" operation to preempt itself. */
54 #define DND_VOIDED (1<<3)
55 #define DND_DELETED (1<<4)
56
57 #define PACK(lo,hi) (((hi) << 16) | ((lo) & 0x0000FFFF))
58 #define UNPACK(x,lo,hi) ((lo) = (x & 0x0000FFFF), (hi) = (x >> 16))
59
60 #define WATCH_ENTER (1<<0)
61 #define WATCH_LEAVE (1<<1)
62 #define WATCH_MOTION (1<<2)
63 #define WATCH_MASK (WATCH_ENTER | WATCH_LEAVE | WATCH_MOTION)
64
65 /* Source-to-Target Message Types */
66
67 #define ST_DRAG_ENTER 0x1001
68 #define ST_DRAG_LEAVE 0x1002
69 #define ST_DRAG_MOTION 0x1003
70 #define ST_DROP 0x1004
71
72 /* Target-to-Source Message Types */
73
74 #define TS_DRAG_STATUS 0x1005
75 #define TS_START_DROP 0x1006
76 #define TS_DROP_RESULT 0x1007
77
78 /* Indices of information fields in ClientMessage array. */
79
80 #define MESG_TYPE 0 /* Message type. */
81 #define MESG_WINDOW 1 /* Window id of remote. */
82 #define MESG_TIMESTAMP 2 /* Transaction timestamp. */
83 #define MESG_POINT 3 /* Root X-Y coordinate. */
84 #define MESG_STATE 4 /* Button and key state. */
85 #define MESG_RESPONSE 3 /* Response to drag/drop message. */
86 #define MESG_FORMAT 3 /* Format atom. */
87 #define MESG_PROPERTY 4 /* Index of button #/key state. */
88
89 /* Drop Status Values (actions included) */
90
91 #define DROP_CONTINUE -2
92 #define DROP_FAIL -1
93 #define DROP_CANCEL 0
94 #define DROP_OK 1
95 #define DROP_COPY 1
96 #define DROP_LINK 2
97 #define DROP_MOVE 3
98
99 #define PROP_WATCH_FLAGS 0
100 #define PROP_DATA_FORMATS 1
101 #define PROP_MAX_SIZE 1000 /* Maximum size of property. */
102
103 #define PROTO_BLT 0
104 #define PROTO_XDND 1
105
106 #define TOKEN_OFFSET 0
107 #define TOKEN_REDRAW (1<<0)
108
109 #define TOKEN_NORMAL 0
110 #define TOKEN_REJECT -1
111 #define TOKEN_ACTIVE 1
112 #define TOKEN_TIMEOUT 5000 /* 5 second timeout for drop requests. */
113
114 /*
115 * Each widget representing a drag & drop target is tagged with
116 * a "BltDndTarget" property in XA_STRING format. This property
117 * identifies the window as a target. It's formated as a Tcl list
118 * and contains the following information:
119 *
120 * "flags DATA_TYPE DATA_TYPE ..."
121 *
122 * "INTERP_NAME TARGET_NAME WINDOW_ID DATA_TYPE DATA_TYPE ..."
123 *
124 * INTERP_NAME Name of the target application's interpreter.
125 * TARGET_NAME Path name of widget registered as the drop target.
126 * WINDOW_ID Window Id of the target's communication window.
127 * Used to forward Enter/Leave/Motion event information
128 * to the target.
129 * DATA_TYPE One or more "types" handled by the target.
130 *
131 * When the user invokes the "drag" operation, the window hierarchy
132 * is progressively examined. Window information is cached during
133 * the operation, to minimize X server traffic. Windows carrying a
134 * "BltDndTarget" property are identified. When the token is dropped
135 * over a valid site, the drop information is sent to the application
136 * via the usual "send" command. If communication fails, the drag&drop
137 * facility automatically posts a rejection symbol on the token window.
138 */
139
140 /*
141 * Drop Protocol:
142 *
143 * Source Target
144 * ------ ------
145 * ButtonRelease-? event.
146 * Invokes blt::dnd drop
147 * +
148 * Send "drop" message to target (via
149 * ClientMessage). Contains X-Y, key/ --> Gets "drop" message.
150 * button state, source window XID. Invokes LeaveCmd proc.
151 * Gets property from source of
152 * ordered matching formats.
153 * +
154 * Invokes DropCmd proc. Arguments
155 * are X-Y coordinate, key/button
156 * state, transaction timestamp,
157 * list of matching formats.
158 * +
159 * Target selects format and invokes
160 * blt::dnd pull to transfer the data
161 * in the selected format.
162 * +
163 * Sends "drop start" message to
164 * source. Contains selected format
165 * Gets "drop start" message. <-- (as atom), ?action?, target window
166 * Invokes data handler for the ID, transaction timestamp.
167 * selected format. +
168 * + Waits for property to change on
169 * Places first packet of data in its window. Time out set for
170 * property on target window. --> no response.
171 * + +
172 * Waits for response property After each packet, sets zero-length
173 * change. Time out set for no resp. <-- property on source window.
174 * If non-zero length packet, error +
175 * occurred, packet is error message. Sends "drop finished" message.
176 * Contains transaction timestamp,
177 * Gets "drop finished" message. <-- status, ?action?.
178 * Invokes FinishCmd proc.
179 */
180
181 /* Configuration Parameters */
182
183 #define DEF_DND_BUTTON_BACKGROUND RGB_YELLOW
184 #define DEF_DND_BUTTON_BG_MONO STD_NORMAL_BG_MONO
185 #define DEF_DND_BUTTON_NUMBER "3"
186 #define DEF_DND_ENTER_COMMAND (char *)NULL
187 #define DEF_DND_LEAVE_COMMAND (char *)NULL
188 #define DEF_DND_MOTION_COMMAND (char *)NULL
189 #define DEF_DND_DROP_COMMAND (char *)NULL
190 #define DEF_DND_RESULT_COMMAND (char *)NULL
191 #define DEF_DND_PACKAGE_COMMAND (char *)NULL
192 #define DEF_DND_SELF_TARGET "no"
193 #define DEF_DND_SEND (char *)NULL
194 #define DEF_DND_IS_TARGET "no"
195 #define DEF_DND_IS_SOURCE "no"
196 #define DEF_DND_SITE_COMMAND (char *)NULL
197
198 #define DEF_DND_DRAG_THRESHOLD "0"
199 #define DEF_TOKEN_ACTIVE_BACKGROUND STD_ACTIVE_BACKGROUND
200 #define DEF_TOKEN_ACTIVE_BG_MONO STD_ACTIVE_BG_MONO
201 #define DEF_TOKEN_ACTIVE_BORDERWIDTH "3"
202 #define DEF_TOKEN_ACTIVE_RELIEF "sunken"
203 #define DEF_TOKEN_ANCHOR "se"
204 #define DEF_TOKEN_BACKGROUND STD_NORMAL_BACKGROUND
205 #define DEF_TOKEN_BG_MONO STD_NORMAL_BG_MONO
206 #define DEF_TOKEN_BORDERWIDTH "3"
207 #define DEF_TOKEN_CURSOR "top_left_arrow"
208 #define DEF_TOKEN_REJECT_BACKGROUND STD_NORMAL_BACKGROUND
209 #define DEF_TOKEN_REJECT_BG_MONO RGB_WHITE
210 #define DEF_TOKEN_REJECT_FOREGROUND RGB_RED
211 #define DEF_TOKEN_REJECT_FG_MONO RGB_BLACK
212 #define DEF_TOKEN_REJECT_STIPPLE_COLOR (char *)NULL
213 #define DEF_TOKEN_REJECT_STIPPLE_MONO RGB_GREY50
214 #define DEF_TOKEN_RELIEF "raised"
215
216 static int StringToCursors _ANSI_ARGS_((ClientData clientData,
217 Tcl_Interp *interp, Tk_Window tkwin, char *string, char *widgRec,
218 int offset));
219 static char *CursorsToString _ANSI_ARGS_((ClientData clientData, Tk_Window tkwin,
220 char *widgRec, int offset, Tcl_FreeProc **freeProcPtr));
221
222 Tk_CustomOption cursorsOption =
223 {
224 StringToCursors, CursorsToString, (ClientData)0
225 };
226
227 typedef struct {
228 Blt_HashTable dndTable; /* Hash table of dnd structures keyed by
229 * the address of the reference Tk window */
230 Tk_Window mainWindow;
231 Display *display;
232 Atom mesgAtom; /* Atom signifying a drag-and-drop message. */
233 Atom formatsAtom; /* Source formats property atom. */
234 Atom targetAtom; /* Target property atom. */
235 Atom commAtom; /* Communication property atom. */
236
237 #ifdef HAVE_XDND
238 Blt_HashTable handlerTable; /* Table of toplevel windows with XdndAware
239 * properties attached to them. */
240 Atom XdndActionListAtom;
241 Atom XdndAwareAtom;
242 Atom XdndEnterAtom;
243 Atom XdndFinishedAtom;
244 Atom XdndLeaveAtom;
245 Atom XdndPositionAtom;
246 Atom XdndSelectionAtom;
247 Atom XdndStatusAtom;
248 Atom XdndTypeListAtom;
249
250 Atom XdndActionCopyAtom;
251 Atom XdndActionMoveAtom;
252 Atom XdndActionLinkAtom;
253 Atom XdndActionAskAtom;
254 Atom XdndActionPrivateAtom;
255 Atom XdndActionDescriptionAtom;
256 #endif
257 } DndInterpData;
258
259
260 typedef struct {
261 Tcl_DString dString;
262 Window window; /* Source/Target window */
263 Display *display;
264 Atom commAtom; /* Data communication property atom. */
265 int packetSize;
266 Tcl_TimerToken timerToken;
267 int status; /* Status of transaction: CONTINUE, OK, FAIL,
268 * or TIMEOUT. */
269 int timestamp; /* Timestamp of the transaction. */
270 int offset;
271 int protocol; /* Drag-and-drop protocol used by the source:
272 * either PROTO_BLT or PROTO_XDND. */
273 } DropPending;
274
275 /*
276 * SubstDescriptors --
277 *
278 * Structure to hold letter-value pairs for percent substitutions.
279 */
280 typedef struct {
281 char letter; /* character like 'x' in "%x" */
282 char *value; /* value to be substituted in place of "%x" */
283 } SubstDescriptors;
284
285 /*
286 * Drag&Drop Registration Data
287 */
288 typedef struct {
289 Tk_Window tkwin; /* Window that embodies the token. NULL
290 * means that the window has been destroyed
291 * but the data structures haven't yet been
292 * cleaned up. */
293
294 Display *display; /* Display containing widget. Used, among
295 * other things, so that resources can be
296 * freed even after tkwin has gone away. */
297 Tcl_Interp *interp; /* Interpreter associated with widget. Used
298 * to delete widget command. */
299 Tk_3DBorder border; /* Structure used to draw 3-D border and
300 * background. NULL means no background
301 * or border. */
302 int borderWidth; /* Width of 3-D border (if any). */
303 int relief; /* 3-d effect: TK_RELIEF_RAISED etc. */
304
305 int flags; /* Various flags; see below for
306 * definitions. */
307
308 /* Token specific fields */
309 int x, y; /* Last position of token window */
310 int startX, startY;
311
312 int status; /* Indicates the current status of the token:
313 * 0 is normal, 1 is active. */
314 int lastStatus; /* Indicates the last status of the token. */
315 Tcl_TimerToken timerToken; /* Token for routine to hide tokenwin */
316 GC fillGC; /* GC used to draw rejection fg: (\) */
317 GC outlineGC; /* GC used to draw rejection bg: (\) */
318 int width, height;
319
320 /* User-configurable fields */
321
322 Tk_Anchor anchor; /* Position of token win relative to mouse */
323 Tk_3DBorder normalBorder; /* Border/background for token window */
324 Tk_3DBorder activeBorder; /* Border/background for token window */
325 int activeRelief;
326 int activeBorderWidth; /* Border width in pixels */
327 XColor *fillColor; /* Color used to draw rejection fg: (\) */
328 XColor *outlineColor; /* Color used to draw rejection bg: (\) */
329 Pixmap rejectStipple; /* Stipple used to draw rejection: (\) */
330 int reqWidth, reqHeight;
331
332 int nSteps;
333
334 } Token;
335
336 /*
337 * Winfo --
338 *
339 * This structure represents a window hierarchy examined during a single
340 * "drag" operation. It's used to cache information to reduce the round
341 * trip calls to the server needed to query window geometry information
342 * and grab the target property.
343 */
344 typedef struct WinfoStruct {
345 Window window; /* Window in hierarchy. */
346
347 int initialized; /* If zero, the rest of this structure's
348 * information hasn't been set. */
349
350 int x1, y1, x2, y2; /* Extents of the window (upper-left and
351 * lower-right corners). */
352
353 struct WinfoStruct *parentPtr; /* Parent node. NULL if root. Used to
354 * compute offset for X11 windows. */
355
356 Blt_Chain *chainPtr; /* List of this window's children. If NULL,
357 * there are no children. */
358
359 int isTarget; /* Indicates if this window is a drag&drop
360 * target. */
361 int lookedForProperty; /* Indicates if this window */
362
363 int eventFlags; /* Retrieved from the target's drag&drop
364 * property, indicates what kinds of pointer
365 * events should be relayed to the target via
366 * ClientMessages. Possible values are OR-ed
367 * combinations of the following bits:
368 * 001 Enter events.
369 * 010 Motion events.
370 * 100 Leave events.
371 */
372 char *matches;
373
374 } Winfo;
375
376 /*
377 * Dnd --
378 *
379 * This structure represents the drag&drop manager. It is associated
380 * with a widget as a drag&drop source, target, or both. It contains
381 * both the source and target components, since a widget can be both
382 * a drag source and a drop target.
383 */
384 typedef struct {
385 Tcl_Interp *interp; /* Interpreter associated with the drag&drop
386 * manager. */
387
388 Tk_Window tkwin; /* Tk window representing the drag&drop
389 * manager (can be source and/or target). */
390
391 Display *display; /* Display for drag&drop widget. Saved to free
392 * resources after window has been destroyed. */
393
394 int isSource; /* Indicates if this drag&drop manager can act
395 * as a drag source. */
396 int isTarget; /* Indicates if this drag&drop manager can act
397 * as a drop target. */
398
399 int targetPropertyExists; /* Indicates is the drop target property has
400 * been set. */
401
402 unsigned int flags; /* Various flags; see below for
403 * definitions. */
404 int timestamp; /* Id of the current drag&drop transaction. */
405
406 int x, y; /* Last known location of the mouse pointer. */
407
408 Blt_HashEntry *hashPtr;
409
410 DndInterpData *dataPtr;
411
412 /* Source component. */
413
414 Blt_HashTable getDataTable; /* Table of data handlers (converters)
415 * registered for this source. */
416
417 int reqButton; /* Button used to invoke drag operation. */
418
419 int button; /* Last button press detected. */
420 int keyState; /* Last key state. */
421
422 Tk_Cursor cursor; /* Cursor restored after dragging */
423
424 int selfTarget; /* Indicated if the source should drop onto
425 * itself. */
426
427 char **reqFormats; /* List of requested data formats. The
428 * list should be ordered with the more
429 * desireable formats first. You can also
430 * temporarily turn off a source by setting
431 * the value to the empty string. */
432
433 Winfo *rootPtr; /* Cached window information: Gathered
434 * and used during the "drag" operation
435 * to see if the mouse pointer is over a
436 * valid target. */
437
438 Winfo *windowPtr; /* Points to information about the last
439 * target the pointer was over. If NULL,
440 * the pointer was not over a valid target. */
441
442 char **packageCmd; /* Tcl command executed at start of the drag
443 * operation to initialize token. */
444
445 char **resultCmd; /* Tcl command executed at the end of the
446 * "drop" operation to indicate its status. */
447
448 char **siteCmd; /* Tcl command executed to update token
449 * window. */
450
451 Token *tokenPtr; /* Token used to provide special cursor. */
452
453
454 Tcl_TimerToken timerToken;
455
456 Tk_Cursor *cursors; /* Array of drag-and-drop cursors. */
457 int cursorPos;
458
459 int dragStart; /* Minimum number of pixels movement
460 * before B1-Motion is considered to
461 * start dragging. */
462
463 /* Target component. */
464
465 Blt_HashTable setDataTable; /* Table of data handlers (converters)
466 * registered for this target. */
467 char **enterCmd; /* Tcl proc called when the mouse enters the
468 * target. */
469 char **leaveCmd; /* Tcl proc called when the mouse leaves the
470 * target. */
471 char **motionCmd; /* Tcl proc called when the mouse is moved
472 * over the target. */
473 char **dropCmd; /* Tcl proc called when the mouse button
474 * is released over the target. */
475
476 char *matchingFormats;
477 int lastId; /* The last transaction id used. This is used
478 * to cache the above formats string. */
479
480 DropPending *pendingPtr; /* Points to structure containing information
481 * about a current drop in progress. If NULL,
482 * no drop is in progress. */
483
484 short int dropX, dropY; /* Location of the current drop. */
485 short int dragX, dragY; /* Starting position of token window */
486 } Dnd;
487
488
489 typedef struct {
490 Tk_Window tkwin; /* Toplevel window of the drop target. */
491 int refCount; /* # of targets referencing this structure. */
492 Dnd *dndPtr; /* Last drop target selected. Used the
493 * implement Enter/Leave events for targets.
494 * If NULL, indicates that no drop target was
495 * previously selected. */
496 int lastRepsonse; /* Indicates what the last response was. */
497 Window window; /* Window id of the top-level window (ie.
498 * the wrapper). */
499 char **formatArr; /* List of formats available from source.
500 * Must be pruned down to matching list. */
501 DndInterpData *dataPtr;
502 int x, y;
503
504 } XDndHandler;
505
506 extern Tk_CustomOption bltListOption;
507 extern Tk_CustomOption bltDistanceOption;
508
509 static Tk_ConfigSpec configSpecs[] =
510 {
511 {TK_CONFIG_CUSTOM, "-allowformats", "allowFormats", "AllowFormats",
512 DEF_DND_SEND, Tk_Offset(Dnd, reqFormats),
513 TK_CONFIG_NULL_OK, &bltListOption},
514 {TK_CONFIG_INT, "-button", "buttonNumber", "ButtonNumber",
515 DEF_DND_BUTTON_NUMBER, Tk_Offset(Dnd, reqButton), 0},
516 {TK_CONFIG_CUSTOM, "-dragthreshold", "dragThreshold", "DragThreshold",
517 DEF_DND_DRAG_THRESHOLD, Tk_Offset(Dnd, dragStart), 0,
518 &bltDistanceOption},
519 {TK_CONFIG_CUSTOM, "-cursors", "cursors", "cursors",
520 DEF_TOKEN_CURSOR, Tk_Offset(Dnd, cursors),
521 TK_CONFIG_NULL_OK, &cursorsOption },
522 {TK_CONFIG_CUSTOM, "-onenter", "onEnter", "OnEnter",
523 DEF_DND_ENTER_COMMAND, Tk_Offset(Dnd, enterCmd),
524 TK_CONFIG_NULL_OK, &bltListOption},
525 {TK_CONFIG_CUSTOM, "-onmotion", "onMotion", "OnMotion",
526 DEF_DND_MOTION_COMMAND, Tk_Offset(Dnd, motionCmd),
527 TK_CONFIG_NULL_OK, &bltListOption},
528 {TK_CONFIG_CUSTOM, "-onleave", "onLeave", "OnLeave",
529 DEF_DND_LEAVE_COMMAND, Tk_Offset(Dnd, leaveCmd),
530 TK_CONFIG_NULL_OK, &bltListOption},
531 {TK_CONFIG_CUSTOM, "-ondrop", "onDrop", "OnDrop",
532 DEF_DND_DROP_COMMAND, Tk_Offset(Dnd, dropCmd),
533 TK_CONFIG_NULL_OK, &bltListOption},
534 {TK_CONFIG_CUSTOM, "-package", "packageCommand", "PackageCommand",
535 DEF_DND_PACKAGE_COMMAND, Tk_Offset(Dnd, packageCmd),
536 TK_CONFIG_NULL_OK, &bltListOption },
537 {TK_CONFIG_CUSTOM, "-result", "result", "Result",
538 DEF_DND_RESULT_COMMAND, Tk_Offset(Dnd, resultCmd),
539 TK_CONFIG_NULL_OK, &bltListOption},
540 {TK_CONFIG_BOOLEAN, "-selftarget", "selfTarget", "SelfTarget",
541 DEF_DND_SELF_TARGET, Tk_Offset(Dnd, selfTarget), 0},
542 {TK_CONFIG_CUSTOM, "-site", "siteCommand", "Command",
543 DEF_DND_SITE_COMMAND, Tk_Offset(Dnd, siteCmd),
544 TK_CONFIG_NULL_OK, &bltListOption},
545 {TK_CONFIG_BOOLEAN, "-source", "source", "Source",
546 DEF_DND_IS_SOURCE, Tk_Offset(Dnd, isSource), 0},
547 {TK_CONFIG_BOOLEAN, "-target", "target", "Target",
548 DEF_DND_IS_TARGET, Tk_Offset(Dnd, isTarget), 0},
549 {TK_CONFIG_END, (char *)NULL, (char *)NULL, (char *)NULL, (char *)NULL,
550 0, 0},
551 };
552
553 static Tk_ConfigSpec tokenConfigSpecs[] =
554 {
555 {TK_CONFIG_BORDER, "-activebackground", "activeBackground",
556 "ActiveBackground", DEF_TOKEN_ACTIVE_BACKGROUND,
557 Tk_Offset(Token, activeBorder), TK_CONFIG_COLOR_ONLY},
558 {TK_CONFIG_BORDER, "-activebackground", "activeBackground",
559 "ActiveBackground", DEF_TOKEN_ACTIVE_BG_MONO,
560 Tk_Offset(Token, activeBorder), TK_CONFIG_MONO_ONLY},
561 {TK_CONFIG_RELIEF, "-activerelief", "activeRelief", "activeRelief",
562 DEF_TOKEN_ACTIVE_RELIEF, Tk_Offset(Token, activeRelief), 0},
563 {TK_CONFIG_ANCHOR, "-anchor", "anchor", "Anchor",
564 DEF_TOKEN_ANCHOR, Tk_Offset(Token, anchor), 0},
565 {TK_CONFIG_PIXELS, "-activeborderwidth", "activeBorderWidth",
566 "ActiveBorderWidth", DEF_TOKEN_ACTIVE_BORDERWIDTH,
567 Tk_Offset(Token, activeBorderWidth), 0},
568 {TK_CONFIG_BORDER, "-background", "background", "Background",
569 DEF_TOKEN_BACKGROUND, Tk_Offset(Token, normalBorder),
570 TK_CONFIG_COLOR_ONLY},
571 {TK_CONFIG_BORDER, "-background", "background", "Background",
572 DEF_TOKEN_BG_MONO, Tk_Offset(Token, normalBorder),
573 TK_CONFIG_MONO_ONLY},
574 {TK_CONFIG_PIXELS, "-borderwidth", "borderWidth", "BorderWidth",
575 DEF_TOKEN_BORDERWIDTH, Tk_Offset(Token, borderWidth), 0},
576 {TK_CONFIG_COLOR, "-outline", "outline", "Outline",
577 DEF_TOKEN_REJECT_BACKGROUND, Tk_Offset(Token, outlineColor),
578 TK_CONFIG_COLOR_ONLY},
579 {TK_CONFIG_COLOR, "-outline", "outline", "Outline",
580 DEF_TOKEN_REJECT_BG_MONO, Tk_Offset(Token, outlineColor),
581 TK_CONFIG_MONO_ONLY},
582 {TK_CONFIG_COLOR, "-fill", "fill", "Fill",
583 DEF_TOKEN_REJECT_FOREGROUND, Tk_Offset(Token, fillColor),
584 TK_CONFIG_COLOR_ONLY},
585 {TK_CONFIG_COLOR, "-fill", "fill", "Fill",
586 DEF_TOKEN_REJECT_BACKGROUND, Tk_Offset(Token, fillColor),
587 TK_CONFIG_MONO_ONLY},
588 {TK_CONFIG_BITMAP, "-rejectstipple", "rejectStipple", "Stipple",
589 DEF_TOKEN_REJECT_STIPPLE_COLOR, Tk_Offset(Token, rejectStipple),
590 TK_CONFIG_COLOR_ONLY | TK_CONFIG_NULL_OK},
591 {TK_CONFIG_BITMAP, "-rejectstipple", "rejectStipple", "Stipple",
592 DEF_TOKEN_REJECT_STIPPLE_MONO, Tk_Offset(Token, rejectStipple),
593 TK_CONFIG_MONO_ONLY},
594 {TK_CONFIG_RELIEF, "-relief", "relief", "Relief",
595 DEF_TOKEN_RELIEF, Tk_Offset(Token, relief), 0},
596 {TK_CONFIG_INT, "-width", "width", "Width",
597 (char *)NULL, Tk_Offset(Token, reqWidth), 0},
598 {TK_CONFIG_INT, "-height", "height", "Height",
599 (char *)NULL, Tk_Offset(Token, reqHeight), 0},
600 {TK_CONFIG_END, (char *)NULL, (char *)NULL, (char *)NULL, (char *)NULL,
601 0, 0},
602 };
603
604
605 /*
606 * Forward Declarations
607 */
608 static int DndCmd _ANSI_ARGS_((ClientData clientData, Tcl_Interp *interp,
609 int argc, char **argv));
610 static void TokenEventProc _ANSI_ARGS_((ClientData clientData,
611 XEvent *eventPtr));
612 static void MoveToken _ANSI_ARGS_((Dnd *dndPtr));
613 static void DisplayToken _ANSI_ARGS_((ClientData clientData));
614 static void HideToken _ANSI_ARGS_((Dnd *dndPtr));
615 static void DrawRejectSymbol _ANSI_ARGS_((Dnd *dndPtr));
616
617 static int GetDnd _ANSI_ARGS_((ClientData clientData, Tcl_Interp *interp,
618 char *name, Dnd **dndPtrPtr));
619 static Dnd *CreateDnd _ANSI_ARGS_((Tcl_Interp *interp, Tk_Window tkwin));
620 static void DestroyDnd _ANSI_ARGS_((DestroyData data));
621 static int DndEventProc _ANSI_ARGS_((ClientData clientData, XEvent *eventPtr));
622 static int ConfigureToken _ANSI_ARGS_((Tcl_Interp *interp, Dnd *dndPtr,
•623 int argc, CONST char **argv, int flags));
624
625 static Winfo *OverTarget _ANSI_ARGS_((Dnd *dndPtr));
626 static void AddTargetProperty _ANSI_ARGS_((Dnd *dndPtr));
627
628 static Winfo *InitRoot _ANSI_ARGS_((Dnd *dndPtr));
629 static void FreeWinfo _ANSI_ARGS_((Winfo *wr));
630 static void GetWinfo _ANSI_ARGS_((Display *display, Winfo * windowPtr));
631
632 static void CancelDrag _ANSI_ARGS_((Dnd *dndPtr));
633
634 /*
635 * ----------------------------------------------------------------------------
636 *
637 * StringToCursors --
638 *
639 * Converts the resize mode into its numeric representation. Valid
640 * mode strings are "none", "expand", "shrink", or "both".
641 *
642 * ----------------------------------------------------------------------------
643 */
644 /*ARGSUSED*/
645 static int
646 StringToCursors(clientData, interp, tkwin, string, widgRec, offset)
647 ClientData clientData; /* Not used. */
648 Tcl_Interp *interp; /* Interpreter to send results back to */
649 Tk_Window tkwin; /* Not used. */
650 char *string; /* String representing cursors. */
651 char *widgRec; /* Structure record */
652 int offset; /* Offset of field in record. */
653 {
654 Tk_Cursor **cursorPtrPtr = (Tk_Cursor **)(widgRec + offset);
655 int result = TCL_OK;
656 int nElems;
657 char **elemArr;
658
659 if (*cursorPtrPtr != NULL) {
660 Blt_Free(*cursorPtrPtr);
661 *cursorPtrPtr = NULL;
662 }
663 if (string == NULL) {
664 return TCL_OK;
665 }
666 if (Tcl_SplitList(interp, string, &nElems, &elemArr) != TCL_OK) {
667 return TCL_ERROR;
668 }
669 if (nElems > 0) {
670 Tk_Cursor *cursorArr;
671 register int i;
672
673 cursorArr = Blt_Calloc(nElems + 1, sizeof(Tk_Cursor));
674 for (i = 0; i < nElems; i++) {
675 cursorArr[i] = Tk_GetCursor(interp, tkwin, Tk_GetUid(elemArr[i]));
676 if (cursorArr[i] == None) {
677 *cursorPtrPtr = cursorArr;
678 result = TCL_ERROR;
679 break;
680 }
681 }
682 Blt_Free(elemArr);
683 *cursorPtrPtr = cursorArr;
684 }
685 return result;
686 }
687
688 /*
689 * ----------------------------------------------------------------------------
690 *
691 * CursorsToString --
692 *
693 * Returns resize mode string based upon the resize flags.
694 *
695 * Results:
696 * The resize mode string is returned.
697 *
698 * ----------------------------------------------------------------------------
699 */
700 /*ARGSUSED*/
701 static char *
702 CursorsToString(clientData, tkwin, widgRec, offset, freeProcPtr)
703 ClientData clientData; /* Not used. */
704 Tk_Window tkwin; /* Not used. */
705 char *widgRec; /* Cursor record */
706 int offset; /* Offset of record. */
707 Tcl_FreeProc **freeProcPtr; /* Memory deallocation scheme to use */
708 {
709 Tk_Cursor *cursorArr = *(Tk_Cursor **)(widgRec + offset);
710 Tk_Cursor *cursorPtr;
711 Tcl_DString dString;
712 char *result;
713
714 if (cursorArr == NULL) {
715 return "";
716 }
717 Tcl_DStringInit(&dString);
718 for (cursorPtr = cursorArr; *cursorPtr != NULL; cursorPtr++) {
719 Tcl_DStringAppendElement(&dString,
720 Tk_NameOfCursor(Tk_Display(tkwin), *cursorPtr));
721 }
722 result = Blt_Strdup(Tcl_DStringValue(&dString));
723 Tcl_DStringFree(&dString);
724 *freeProcPtr = (Tcl_FreeProc *)Blt_Free;
725 return result;
726 }
727
728
729 static char *
730 PrintList(list)
731 char **list;
732 {
733 int count;
734 char **p;
735
736 count = 0;
737 for(p = list; *p != NULL; p++) {
738 count++;
739 }
740 return Tcl_Merge(count, list);
741 }
742
743
744 /* ARGSUSED */
745 static int
746 XSendEventErrorProc(clientData, errEventPtr)
747 ClientData clientData;
748 XErrorEvent *errEventPtr;
749 {
750 int *errorPtr = clientData;
751
752 *errorPtr = TCL_ERROR;
753 return 0;
754 }
755
756 static void
757 SendClientMsg(display, window, mesgAtom, data0, data1, data2, data3, data4)
758 Display *display;
759 Window window;
760 Atom mesgAtom;
761 int data0, data1, data2, data3, data4;
762 {
763 XEvent event;
764 Tk_ErrorHandler handler;
765 int result;
766 int any = -1;
767
768 event.xclient.type = ClientMessage;
769 event.xclient.serial = 0;
770 event.xclient.send_event = True;
771 event.xclient.display = display;
772 event.xclient.window = window;
773 event.xclient.message_type = mesgAtom;
774 event.xclient.format = 32;
775 event.xclient.data.l[0] = data0;
776 event.xclient.data.l[1] = data1;
777 event.xclient.data.l[2] = data2;
778 event.xclient.data.l[3] = data3;
779 event.xclient.data.l[4] = data4;
780
781 result = TCL_OK;
782 handler = Tk_CreateErrorHandler(display, any, X_SendEvent, any,
783 XSendEventErrorProc, &result);
784 if (!XSendEvent(display, window, False, ClientMessage, &event)) {
785 result = TCL_ERROR;
786 }
787 Tk_DeleteErrorHandler(handler);
788 XSync(display, False);
789 if (result != TCL_OK) {
790 fprintf(stderr, "XSendEvent response to drop: Protocol failed\n");
791 }
792 }
793
794 /*
795 * ------------------------------------------------------------------------
796 *
797 * GetWindowZOrder --
798 *
799 * Returns a chain of the child windows according to their stacking
800 * order. The window ids are ordered from top to bottom.
801 *
802 * ------------------------------------------------------------------------
803 */
804 static Blt_Chain *
805 GetWindowZOrder(display, window)
806 Display *display;
807 Window window;
808 {
809 Blt_Chain *chainPtr;
810 Window *childArr;
811 unsigned int nChildren;
812 Window dummy;
813
814 chainPtr = NULL;
815 if ((XQueryTree(display, window, &dummy, &dummy, &childArr, &nChildren)) &&
816 (nChildren > 0)) {
817 register int i;
818
819 chainPtr = Blt_ChainCreate();
820 for (i = 0; i < nChildren; i++) {
821 /*
822 * XQuery returns windows in bottom to top order. We only care
823 * about the top window.
824 */
825 Blt_ChainPrepend(chainPtr, (ClientData)childArr[i]);
826 }
827 if (childArr != NULL) {
828 XFree((char *)childArr); /* done with list of kids */
829 }
830 }
831 return chainPtr;
832 }
833
834 static int
835 GetMaxPropertySize(display)
836 Display *display;
837 {
838 int size;
839
840 size = Blt_MaxRequestSize(display, sizeof(char));
841 size -= 32;
842 return size;
843 }
844
845 /*
846 * ------------------------------------------------------------------------
847 *
848 * GetProperty --
849 *
850 * Returns the data associated with the named property on the
851 * given window. All data is assumed to be 8-bit string data.
852 *
853 * ------------------------------------------------------------------------
854 */
855 static char *
856 GetProperty(display, window, atom)
857 Display *display;
858 Window window;
859 Atom atom;
860 {
861 char *data;
862 int result, format;
863 Atom typeAtom;
864 unsigned long nItems, bytesAfter;
865
866 if (window == None) {
867 return NULL;
868 }
869 data = NULL;
870 result = XGetWindowProperty(
871 display, /* Display of window. */
872 window, /* Window holding the property. */
873 atom, /* Name of property. */
874 0, /* Offset of data (for multiple reads). */
875 GetMaxPropertySize(display), /* Maximum number of items to read. */
876 False, /* If true, delete the property. */
877 XA_STRING, /* Desired type of property. */
878 &typeAtom, /* (out) Actual type of the property. */
879 &format, /* (out) Actual format of the property. */
880 &nItems, /* (out) # of items in specified format. */
881 &bytesAfter, /* (out) # of bytes remaining to be read. */
882 (unsigned char **)&data);
883 if ((result != Success) || (format != 8) || (typeAtom != XA_STRING)) {
884 if (data != NULL) {
885 XFree((char *)data);
886 data = NULL;
887 }
888 }
889 return data;
890 }
891
892 /*
893 * ------------------------------------------------------------------------
894 *
895 * SetProperty --
896 *
897 * Associates the given data with the a property on a given window.
898 * All data is assumed to be 8-bit string data.
899 *
900 * ------------------------------------------------------------------------
901 */
902 static void
903 SetProperty(tkwin, atom, data)
904 Tk_Window tkwin;
905 Atom atom;
906 char *data;
907 {
908 XChangeProperty(Tk_Display(tkwin), Tk_WindowId(tkwin), atom, XA_STRING,
909 8, PropModeReplace, (unsigned char *)data, strlen(data) + 1);
910 }
911
912 /*
913 * ------------------------------------------------------------------------
914 *
915 * GetWindowRegion --
916 *
917 * Queries for the upper-left and lower-right corners of the
918 * given window.
919 *
920 * Results:
921 * Returns if the window is currently viewable. The coordinates
922 * of the window are returned via parameters.
923 *
924 * ------------------------------------------------------------------------
925 */
926 static int
927 GetWindowRegion(display, windowPtr)
928 Display *display;
929 Winfo *windowPtr;
930 {
931 XWindowAttributes winAttrs;
932
933 if (XGetWindowAttributes(display, windowPtr->window, &winAttrs)) {
934 windowPtr->x1 = winAttrs.x;
935 windowPtr->y1 = winAttrs.y;
936 windowPtr->x2 = winAttrs.x + winAttrs.width - 1;
937 windowPtr->y2 = winAttrs.y + winAttrs.height - 1;
938 }
939 return (winAttrs.map_state == IsViewable);
940 }
941
942 /*
943 * ------------------------------------------------------------------------
944 *
945 * FindTopWindow --
946 *
947 * Searches for the topmost window at a given pair of X-Y coordinates.
948 *
949 * Results:
950 * Returns a pointer to the node representing the window containing
951 * the point. If one can't be found, NULL is returned.
952 *
953 * ------------------------------------------------------------------------
954 */
955 static Winfo *
956 FindTopWindow(dndPtr, x, y)
957 Dnd *dndPtr;
958 int x, y;
959 {
960 Winfo *rootPtr;
961 register Blt_ChainLink *linkPtr;
962 register Winfo *windowPtr;
963
964 rootPtr = dndPtr->rootPtr;
965 if (!rootPtr->initialized) {
966 GetWinfo(dndPtr->display, rootPtr);
967 }
968 if ((x < rootPtr->x1) || (x > rootPtr->x2) ||
969 (y < rootPtr->y1) || (y > rootPtr->y2)) {
970 return NULL; /* Point is not over window */
971 }
972 windowPtr = rootPtr;
973
974 /*
975 * The window list is ordered top to bottom, so stop when we find the
976 * first child that contains the X-Y coordinate. It will be the topmost
977 * window in that hierarchy. If none exists, then we already have the
978 * topmost window.
979 */
980 descend:
981 for (linkPtr = Blt_ChainFirstLink(rootPtr->chainPtr); linkPtr != NULL;
982 linkPtr = Blt_ChainNextLink(linkPtr)) {
983 rootPtr = Blt_ChainGetValue(linkPtr);
984 if (!rootPtr->initialized) {
985 GetWinfo(dndPtr->display, rootPtr);
986 }
987 if (rootPtr->window == Blt_GetRealWindowId(dndPtr->tokenPtr->tkwin)) {
988 continue; /* Don't examine the token window. */
989 }
990 if ((x >= rootPtr->x1) && (x <= rootPtr->x2) &&
991 (y >= rootPtr->y1) && (y <= rootPtr->y2)) {
992 /*
993 * Remember the last window containing the pointer and descend
994 * into its window hierarchy. We'll look for a child that also
995 * contains the pointer.
996 */
997 windowPtr = rootPtr;
998 goto descend;
999 }
1000 }
1001 return windowPtr;
1002 }
1003
1004 /*
1005 * ------------------------------------------------------------------------
1006 *
1007 * GetWidgetCursor --
1008 *
1009 * Queries a widget for its current cursor. The given window
1010 * may or may not be a Tk widget that has a -cursor option.
1011 *
1012 * Results:
1013 * Returns the current cursor of the widget.
1014 *
1015 * ------------------------------------------------------------------------
1016 */
1017 static Tk_Cursor
1018 GetWidgetCursor(interp, tkwin)
1019 Tcl_Interp *interp; /* Interpreter to evaluate widget command. */
1020 Tk_Window tkwin; /* Window of drag&drop source. */
1021 {
1022 Tk_Cursor cursor;
1023 Tcl_DString dString, savedResult;
1024
1025 cursor = None;
1026 Tcl_DStringInit(&dString);
1027 Blt_DStringAppendElements(&dString, Tk_PathName(tkwin), "cget", "-cursor",
1028 (char *)NULL);
1029 Tcl_DStringInit(&savedResult);
1030 Tcl_DStringGetResult(interp, &savedResult);
1031 if (Tcl_GlobalEval(interp, Tcl_DStringValue(&dString)) == TCL_OK) {
1032 char *name;
1033
1034 name = Tcl_GetStringResult(interp);
1035 if ((name != NULL) && (name[0] != '\0')) {
1036 cursor = Tk_GetCursor(interp, tkwin, Tk_GetUid(name));
1037 }
1038 }
1039 Tcl_DStringResult(interp, &savedResult);
1040 Tcl_DStringFree(&dString);
1041 return cursor;
1042 }
1043
1044 /*
1045 * ------------------------------------------------------------------------
1046 *
1047 * NameOfStatus --
1048 *
1049 * Converts a numeric drop result into its string representation.
1050 *
1051 * Results:
1052 * Returns a static string representing the drop result.
1053 *
1054 * ------------------------------------------------------------------------
1055 */
1056 static char *
1057 NameOfStatus(status)
1058 int status;
1059 {
1060 switch (status) {
1061 case DROP_OK:
1062 return "active";
1063 case DROP_CONTINUE:
1064 return "normal";
1065 case DROP_FAIL:
1066 return "reject";
1067 case DROP_CANCEL:
1068 return "cancel";
1069 default:
1070 return "unknown status value";
1071 }
1072 }
1073
1074 /*
1075 * ------------------------------------------------------------------------
1076 *
1077 * NameOfAction --
1078 *
1079 * Converts a numeric drop result into its string representation.
1080 *
1081 * Results:
1082 * Returns a static string representing the drop result.
1083 *
1084 * ------------------------------------------------------------------------
1085 */
1086 static char *
1087 NameOfAction(action)
1088 int action;
1089 {
1090 switch (action) {
1091 case DROP_COPY:
1092 return "copy";
1093 case DROP_CANCEL:
1094 return "cancel";
1095 case DROP_MOVE:
1096 return "move";
1097 break;
1098 case DROP_LINK:
1099 return "link";
1100 case DROP_FAIL:
1101 return "fail";
1102 default:
1103 return "unknown action";
1104 }
1105 }
1106
1107 /*
1108 * ------------------------------------------------------------------------
1109 *
1110 * GetAction --
1111 *
1112 * Converts a string to its numeric drop result value.
1113 *
1114 * Results:
1115 * Returns the drop result.
1116 *
1117 * ------------------------------------------------------------------------
1118 */
1119 static int
1120 GetAction(string)
1121 char *string;
1122 {
1123 char c;
1124
1125 c = string[0];
1126 if ((c == 'c') && (strcmp(string, "cancel") == 0)) {
1127 return DROP_CANCEL;
1128 } else if ((c == 'f') && (strcmp(string, "fail") == 0)) {
1129 return DROP_FAIL;
1130 } else if ((c == 'm') && (strcmp(string, "move") == 0)) {
1131 return DROP_MOVE;
1132 } else if ((c == 'l') && (strcmp(string, "link") == 0)) {
1133 return DROP_LINK;
1134 } else if ((c == 'c') && (strcmp(string, "copy") == 0)) {
1135 return DROP_COPY;
1136 } else {
1137 return DROP_COPY;
1138 }
1139 }
1140
1141 /*
1142 * ------------------------------------------------------------------------
1143 *
1144 * GetDragResult --
1145 *
1146 * Converts a string to its numeric drag result value.
1147 *
1148 * Results:
1149 * Returns the drag result.
1150 *
1151 * ------------------------------------------------------------------------
1152 */
1153 static int
1154 GetDragResult(interp, string)
1155 Tcl_Interp *interp;
1156 char *string;
1157 {
1158 char c;
1159 int bool;
1160
1161 c = string[0];
1162 if ((c == 'c') && (strcmp(string, "cancel") == 0)) {
1163 return DROP_CANCEL;
1164 } else if (Tcl_GetBoolean(interp, string, &bool) != TCL_OK) {
1165 Tcl_BackgroundError(interp);
1166 return DROP_CANCEL;
1167 }
1168 return bool;
1169 }
1170
1171 static void
1172 AnimateActiveCursor(clientData)
1173 ClientData clientData;
1174 {
1175 Dnd *dndPtr = clientData;
1176 Tk_Cursor cursor;
1177
1178 dndPtr->cursorPos++;
1179 cursor = dndPtr->cursors[dndPtr->cursorPos];
1180 if (cursor == None) {
1181 cursor = dndPtr->cursors[1];
1182 dndPtr->cursorPos = 1;
1183 }
1184 Tk_DefineCursor(dndPtr->tkwin, cursor);
1185 dndPtr->timerToken = Tcl_CreateTimerHandler(100, AnimateActiveCursor,
1186 dndPtr);
1187 }
1188
1189 static void
1190 StartActiveCursor(dndPtr)
1191 Dnd *dndPtr;
1192 {
1193 if (dndPtr->timerToken != NULL) {
1194 Tcl_DeleteTimerHandler(dndPtr->timerToken);
1195 }
1196 if (dndPtr->cursors != NULL) {
1197 Tk_Cursor cursor;
1198
1199 dndPtr->cursorPos = 1;
1200 cursor = dndPtr->cursors[1];
1201 if (cursor != None) {
1202 Tk_DefineCursor(dndPtr->tkwin, cursor);
1203 dndPtr->timerToken = Tcl_CreateTimerHandler(125,
1204 AnimateActiveCursor, dndPtr);
1205 }
1206 }
1207 }
1208
1209 static void
1210 StopActiveCursor(dndPtr)
1211 Dnd *dndPtr;
1212 {
1213 if (dndPtr->cursorPos > 0) {
1214 dndPtr->cursorPos = 0;
1215 }
1216 if (dndPtr->cursors != NULL) {
1217 Tk_DefineCursor(dndPtr->tkwin, dndPtr->cursors[0]);
1218 }
1219 if (dndPtr->timerToken != NULL) {
1220 Tcl_DeleteTimerHandler(dndPtr->timerToken);
1221 dndPtr->timerToken = NULL;
1222 }
1223 }
1224
1225 /*
1226 *----------------------------------------------------------------------
1227 *
1228 * EventuallyRedrawToken --
1229 *
1230 * Queues a request to redraw the widget at the next idle point.
1231 *
1232 * Results:
1233 * None.
1234 *
1235 * Side effects:
1236 * Information gets redisplayed. Right now we don't do selective
1237 * redisplays: the whole window will be redrawn.
1238 *
1239 *----------------------------------------------------------------------
1240 */
1241 static void
1242 EventuallyRedrawToken(dndPtr)
1243 Dnd *dndPtr;
1244 {
1245 Token *tokenPtr;
1246
1247 if (dndPtr->tokenPtr == NULL) {
1248 return;
1249 }
1250 tokenPtr = dndPtr->tokenPtr;
1251 if ((tokenPtr->tkwin != NULL) && (tokenPtr->tkwin != NULL) &&
1252 !(tokenPtr->flags & TOKEN_REDRAW)) {
1253 tokenPtr->flags |= TOKEN_REDRAW;
1254 Tcl_DoWhenIdle(DisplayToken, dndPtr);
1255 }
1256 }
1257
1258 /*
1259 * ------------------------------------------------------------------------
1260 *
1261 * RaiseToken --
1262 *
1263 * ------------------------------------------------------------------------
1264 */
1265 static void
1266 RaiseToken(dndPtr)
1267 Dnd *dndPtr;
1268 {
1269 Token *tokenPtr = dndPtr->tokenPtr;
1270
1271 if (dndPtr->flags & DND_INITIATED) {
1272 if ((Tk_Width(tokenPtr->tkwin) != Tk_ReqWidth(tokenPtr->tkwin)) ||
1273 (Tk_Height(tokenPtr->tkwin) != Tk_ReqHeight(tokenPtr->tkwin))) {
1274 Blt_ResizeToplevel(tokenPtr->tkwin,
1275 Tk_ReqWidth(tokenPtr->tkwin),
1276 Tk_ReqHeight(tokenPtr->tkwin));
1277 }
1278 Blt_MapToplevel(tokenPtr->tkwin);
1279 Blt_RaiseToplevel(tokenPtr->tkwin);
1280 }
1281 }
1282
1283
1284
1285 /*
1286 * ------------------------------------------------------------------------
1287 *
1288 * DisplayToken --
1289 *
1290 * ------------------------------------------------------------------------
1291 */
1292 static void
1293 DisplayToken(clientData)
1294 ClientData clientData;
1295 {
1296 Dnd *dndPtr = clientData;
1297 Token *tokenPtr = dndPtr->tokenPtr;
1298 int relief;
1299 Tk_3DBorder border;
1300 int borderWidth;
1301
1302 tokenPtr->flags &= ~TOKEN_REDRAW;
1303 if (tokenPtr->status == DROP_OK) {
1304 relief = tokenPtr->activeRelief;
1305 border = tokenPtr->activeBorder;
1306 borderWidth = tokenPtr->activeBorderWidth;
1307 if ((dndPtr->cursors != NULL) && (dndPtr->cursorPos == 0)) {
1308 StartActiveCursor(dndPtr);
1309 }
1310 } else {
1311 relief = tokenPtr->relief;
1312 border = tokenPtr->normalBorder;
1313 borderWidth = tokenPtr->borderWidth;
1314 StopActiveCursor(dndPtr);
1315 }
1316 Blt_Fill3DRectangle(tokenPtr->tkwin, Tk_WindowId(tokenPtr->tkwin), border,
1317 0, 0, Tk_Width(tokenPtr->tkwin), Tk_Height(tokenPtr->tkwin),
1318 borderWidth, relief);
1319 tokenPtr->lastStatus = tokenPtr->status;
1320 if (tokenPtr->status == DROP_FAIL) {
1321 DrawRejectSymbol(dndPtr);
1322 }
1323 }
1324
1325 /*
1326 * ------------------------------------------------------------------------
1327 *
1328 * FadeToken --
1329 *
1330 * Fades the token into the target.
1331 *
1332 * ------------------------------------------------------------------------
1333 */
1334 static void
1335 FadeToken(dndPtr)
1336 Dnd *dndPtr; /* Drag-and-drop manager (source). */
1337 {
1338 Token *tokenPtr = dndPtr->tokenPtr;
1339 int w, h;
1340 int dx, dy;
1341 Window window;
1342
1343 if (tokenPtr->status == DROP_FAIL) {
1344 tokenPtr->nSteps = 1;
1345 return;
1346 }
1347 if (tokenPtr->nSteps == 1) {
1348 HideToken(dndPtr);
1349 dndPtr->flags &= ~(DND_ACTIVE | DND_VOIDED);
1350 return;
1351 }
1352 if (tokenPtr->timerToken != NULL) {
1353 Tcl_DeleteTimerHandler(tokenPtr->timerToken);
1354 }
1355 tokenPtr->timerToken = Tcl_CreateTimerHandler(10,
1356 (Tcl_TimerProc *)FadeToken, dndPtr);
1357 tokenPtr->nSteps--;
1358
1359 w = Tk_ReqWidth(tokenPtr->tkwin) * tokenPtr->nSteps / 10;
1360 h = Tk_ReqHeight(tokenPtr->tkwin) * tokenPtr->nSteps / 10;
1361 if (w < 1) {
1362 w = 1;
1363 }
1364 if (h < 1) {
1365 h = 1;
1366 }
1367 dx = (Tk_ReqWidth(tokenPtr->tkwin) - w) / 2;
1368 dy = (Tk_ReqHeight(tokenPtr->tkwin) - h) / 2;
1369 window = Blt_GetRealWindowId(tokenPtr->tkwin);
1370 XMoveResizeWindow(dndPtr->display, window, tokenPtr->x + dx,
1371 tokenPtr->y + dy, (unsigned int)w, (unsigned int)h);
1372 tokenPtr->width = w, tokenPtr->height = h;
1373 }
1374
1375 /*
1376 * ------------------------------------------------------------------------
1377 *
1378 * SnapToken --
1379 *
1380 * Snaps the token back to the source.
1381 *
1382 * ------------------------------------------------------------------------
1383 */
1384 static void
1385 SnapToken(dndPtr)
1386 Dnd *dndPtr; /* drag&drop source window data */
1387 {
1388 Token *tokenPtr = dndPtr->tokenPtr;
1389
1390 if (tokenPtr->nSteps == 1) {
1391 HideToken(dndPtr);
1392 return;
1393 }
1394 if (tokenPtr->timerToken != NULL) {
1395 Tcl_DeleteTimerHandler(tokenPtr->timerToken);
1396 }
1397 tokenPtr->timerToken = Tcl_CreateTimerHandler(10,
1398 (Tcl_TimerProc *)SnapToken, dndPtr);
1399 tokenPtr->nSteps--;
1400 tokenPtr->x -= (tokenPtr->x - tokenPtr->startX) / tokenPtr->nSteps;
1401 tokenPtr->y -= (tokenPtr->y - tokenPtr->startY) / tokenPtr->nSteps;
1402 if ((tokenPtr->x != Tk_X(tokenPtr->tkwin)) ||
1403 (tokenPtr->y != Tk_Y(tokenPtr->tkwin))) {
1404 Tk_MoveToplevelWindow(tokenPtr->tkwin, tokenPtr->x, tokenPtr->y);
1405 }
1406 RaiseToken(dndPtr);
1407 }
1408
1409 /*
1410 * ------------------------------------------------------------------------
1411 *
1412 * HideToken --
1413 *
1414 * Unmaps the drag&drop token. Invoked directly at the end of a
1415 * successful communication, or after a delay if the communication
1416 * fails (allowing the user to see a graphical picture of failure).
1417 *
1418 * ------------------------------------------------------------------------
1419 */
1420 static void
1421 HideToken(dndPtr)
1422 Dnd *dndPtr;
1423 {
1424 Token *tokenPtr = dndPtr->tokenPtr;
1425
1426 if (tokenPtr->timerToken != NULL) {
1427 Tcl_DeleteTimerHandler(tokenPtr->timerToken);
1428 tokenPtr->timerToken = NULL;
1429 }
1430 if (dndPtr->flags & DND_INITIATED) {
1431 /* Reset the cursor back to its normal state. */
1432 StopActiveCursor(dndPtr);
1433 if (dndPtr->cursor == None) {
1434 Tk_UndefineCursor(dndPtr->tkwin);
1435 } else {
1436 Tk_DefineCursor(dndPtr->tkwin, dndPtr->cursor);
1437 }
1438 if (tokenPtr->tkwin != NULL) {
1439 Tk_UnmapWindow(tokenPtr->tkwin);
1440 Blt_ResizeToplevel(tokenPtr->tkwin,
1441 Tk_ReqWidth(tokenPtr->tkwin),
1442 Tk_ReqHeight(tokenPtr->tkwin));
1443 }
1444 }
1445 if (dndPtr->rootPtr != NULL) {
1446 FreeWinfo(dndPtr->rootPtr);
1447 dndPtr->rootPtr = NULL;
1448 }
1449 dndPtr->flags &= ~(DND_ACTIVE | DND_VOIDED);
1450 tokenPtr->status = DROP_CONTINUE;
1451 }
1452
1453 /*
1454 * ------------------------------------------------------------------------
1455 *
1456 * MorphToken --
1457 *
1458 * Fades the token into the target.
1459 *
1460 * ------------------------------------------------------------------------
1461 */
•1462 #if 0
1463 static void
1464 MorphToken(dndPtr)
1465 Dnd *dndPtr; /* Drag-and-drop manager (source). */
1466 {
1467 Token *tokenPtr = dndPtr->tokenPtr;
1468
1469 if (tokenPtr->status == DROP_FAIL) {
1470 tokenPtr->nSteps = 1;
1471 return;
1472 }
1473 if (tokenPtr->nSteps == 1) {
1474 HideToken(dndPtr);
1475 dndPtr->flags &= ~(DND_ACTIVE | DND_VOIDED);
1476 return;
1477 }
1478 if (tokenPtr->timerToken != NULL) {
1479 Tcl_DeleteTimerHandler(tokenPtr->timerToken);
1480 }
1481 tokenPtr->timerToken = Tcl_CreateTimerHandler(10,
1482 (Tcl_TimerProc *)MorphToken, dndPtr);
1483 tokenPtr->nSteps--;
1484
1485 if (dndPtr->flags & DROP_CANCEL) {
1486 tokenPtr->nSteps--;
1487 tokenPtr->x -= (tokenPtr->x - tokenPtr->startX) / tokenPtr->nSteps;
1488 tokenPtr->y -= (tokenPtr->y - tokenPtr->startY) / tokenPtr->nSteps;
1489 if ((tokenPtr->x != Tk_X(tokenPtr->tkwin)) ||
1490 (tokenPtr->y != Tk_Y(tokenPtr->tkwin))) {
1491 Tk_MoveToplevelWindow(tokenPtr->tkwin, tokenPtr->x, tokenPtr->y);
1492 }
1493 } else {
1494 int w, h;
1495 int dx, dy;
1496 Window window;
1497
1498 w = Tk_ReqWidth(tokenPtr->tkwin) * tokenPtr->nSteps / 10;
1499 h = Tk_ReqHeight(tokenPtr->tkwin) * tokenPtr->nSteps / 10;
1500 if (w < 1) {
1501 w = 1;
1502 }
1503 if (h < 1) {
1504 h = 1;
1505 }
1506 dx = (Tk_ReqWidth(tokenPtr->tkwin) - w) / 2;
1507 dy = (Tk_ReqHeight(tokenPtr->tkwin) - h) / 2;
1508 window = Blt_GetRealWindowId(tokenPtr->tkwin);
1509 XMoveResizeWindow(dndPtr->display, window, tokenPtr->x + dx,
1510 tokenPtr->y + dy, (unsigned int)w, (unsigned int)h);
1511 tokenPtr->width = w, tokenPtr->height = h;
1512 }
1513 RaiseToken(dndPtr);
1514 }
•1515 #endif
1516
1517 static void
1518 GetTokenPosition(dndPtr, x, y)
1519 Dnd *dndPtr;
1520 int x, y;
1521 {
1522 Token *tokenPtr = dndPtr->tokenPtr;
1523 int maxX, maxY;
1524 int vx, vy, dummy;
1525 Screen *screenPtr;
1526
1527 /* Adjust current location for virtual root windows. */
1528 Tk_GetVRootGeometry(dndPtr->tkwin, &vx, &vy, &dummy, &dummy);
1529 x += vx - TOKEN_OFFSET;
1530 y += vy - TOKEN_OFFSET;
1531
1532 screenPtr = Tk_Screen(tokenPtr->tkwin);
1533 maxX = WidthOfScreen(screenPtr) - Tk_Width(tokenPtr->tkwin);
1534 maxY = HeightOfScreen(screenPtr) - Tk_Height(tokenPtr->tkwin);
1535 Blt_TranslateAnchor(x, y, Tk_Width(tokenPtr->tkwin),
1536 Tk_Height(tokenPtr->tkwin), tokenPtr->anchor, &x, &y);
1537 if (x > maxX) {
1538 x = maxX;
1539 } else if (x < 0) {
1540 x = 0;
1541 }
1542 if (y > maxY) {
1543 y = maxY;
1544 } else if (y < 0) {
1545 y = 0;
1546 }
1547 tokenPtr->x = x, tokenPtr->y = y;
1548 }
1549
1550 /*
1551 * ------------------------------------------------------------------------
1552 *
1553 * MoveToken --
1554 *
1555 * Invoked during "drag" operations to move a token window to its
1556 * current "drag" coordinate.
1557 *
1558 * ------------------------------------------------------------------------
1559 */
1560 static void
1561 MoveToken(dndPtr)
1562 Dnd *dndPtr; /* drag&drop source window data */
1563 {
1564 Token *tokenPtr = dndPtr->tokenPtr;
1565
1566 GetTokenPosition(dndPtr, dndPtr->x, dndPtr->y);
1567 if ((tokenPtr->x != Tk_X(tokenPtr->tkwin)) ||
1568 (tokenPtr->y != Tk_Y(tokenPtr->tkwin))) {
1569 Tk_MoveToplevelWindow(tokenPtr->tkwin, tokenPtr->x, tokenPtr->y);
1570 }
1571 }
1572
1573
1574 /*
1575 * ------------------------------------------------------------------------
1576 *
1577 * ChangeToken --
1578 *
1579 * Invoked when the event loop is idle to determine whether or not
1580 * the current drag&drop token position is over another drag&drop
1581 * target.
1582 *
1583 * ------------------------------------------------------------------------
1584 */
1585 static void
1586 ChangeToken(dndPtr, status)
1587 Dnd *dndPtr;
1588 int status;
1589 {
1590 Token *tokenPtr = dndPtr->tokenPtr;
1591
1592 tokenPtr->status = status;
1593 EventuallyRedrawToken(dndPtr);
1594
1595 /*
1596 * If the source has a site command, then invoke it to
1597 * modify the appearance of the token window. Pass any
1598 * errors onto the drag&drop error handler.
1599 */
1600 if (dndPtr->siteCmd) {
1601 Tcl_Interp *interp = dndPtr->interp;
1602 Tcl_DString dString, savedResult;
1603 char **p;
1604
1605 Tcl_DStringInit(&dString);
1606 for (p = dndPtr->siteCmd; *p != NULL; p++) {
1607 Tcl_DStringAppendElement(&dString, *p);
1608 }
1609 Tcl_DStringAppendElement(&dString, Tk_PathName(dndPtr->tkwin));
1610 Tcl_DStringAppendElement(&dString, "timestamp");
1611 Tcl_DStringAppendElement(&dString, Blt_Utoa(dndPtr->timestamp));
1612 Tcl_DStringAppendElement(&dString, "status");
1613 Tcl_DStringAppendElement(&dString, NameOfStatus(status));
1614 Tcl_DStringInit(&savedResult);
1615 Tcl_DStringGetResult(interp, &savedResult);
1616 if (Tcl_GlobalEval(interp, Tcl_DStringValue(&dString)) != TCL_OK) {
1617 Tcl_BackgroundError(interp);
1618 }
1619 Tcl_DStringFree(&dString);
1620 Tcl_DStringResult(interp, &savedResult);
1621 }
1622 }
1623
1624 /*
1625 * ------------------------------------------------------------------------
1626 *
1627 * DrawRejectSymbol --
1628 *
1629 * Draws a rejection mark on the current drag&drop token, and arranges
1630 * for the token to be unmapped after a small delay.
1631 *
1632 * ------------------------------------------------------------------------
1633 */
1634 static void
1635 DrawRejectSymbol(dndPtr)
1636 Dnd *dndPtr;
1637 {
1638 Token *tokenPtr = dndPtr->tokenPtr;
1639 int divisor = 6; /* controls size of rejection symbol */
1640 int w, h, lineWidth, x, y, margin;
1641
1642 margin = 2 * tokenPtr->borderWidth;
1643 w = Tk_Width(tokenPtr->tkwin) - 2 * margin;
1644 h = Tk_Height(tokenPtr->tkwin) - 2 * margin;
1645 lineWidth = (w < h) ? w / divisor : h / divisor;
1646 lineWidth = (lineWidth < 1) ? 1 : lineWidth;
1647
1648 w = h = lineWidth * (divisor - 1);
1649 x = (Tk_Width(tokenPtr->tkwin) - w) / 2;
1650 y = (Tk_Height(tokenPtr->tkwin) - h) / 2;
1651
1652 /*
1653 * Draw the rejection symbol background (\) on the token window...
1654 */
1655 XSetLineAttributes(Tk_Display(tokenPtr->tkwin), tokenPtr->outlineGC,
1656 lineWidth + 2, LineSolid, CapButt, JoinBevel);
1657
1658 XDrawArc(Tk_Display(tokenPtr->tkwin), Tk_WindowId(tokenPtr->tkwin),
1659 tokenPtr->outlineGC, x, y, w, h, 0, 23040);
1660
1661 XDrawLine(Tk_Display(tokenPtr->tkwin), Tk_WindowId(tokenPtr->tkwin),
1662 tokenPtr->outlineGC, x + lineWidth, y + lineWidth, x + w - lineWidth,
1663 y + h - lineWidth);
1664
1665 /*
1666 * Draw the rejection symbol foreground (\) on the token window...
1667 */
1668 XSetLineAttributes(Tk_Display(tokenPtr->tkwin), tokenPtr->fillGC,
1669 lineWidth, LineSolid, CapButt, JoinBevel);
1670
1671 XDrawArc(Tk_Display(tokenPtr->tkwin), Tk_WindowId(tokenPtr->tkwin),
1672 tokenPtr->fillGC, x, y, w, h, 0, 23040);
1673
1674 XDrawLine(Tk_Display(tokenPtr->tkwin), Tk_WindowId(tokenPtr->tkwin),
1675 tokenPtr->fillGC, x + lineWidth, y + lineWidth, x + w - lineWidth,
1676 y + h - lineWidth);
1677
1678 tokenPtr->status = DROP_FAIL;
1679 /*
1680 * Arrange for token window to disappear eventually.
1681 */
1682 if (tokenPtr->timerToken != NULL) {
1683 Tcl_DeleteTimerHandler(tokenPtr->timerToken);
1684 }
1685 tokenPtr->timerToken = Tcl_CreateTimerHandler(1000,
1686 (Tcl_TimerProc *)HideToken, dndPtr);
1687 RaiseToken(dndPtr);
1688 dndPtr->flags &= ~(DND_ACTIVE | DND_VOIDED);
1689 }
1690
1691 /*
1692 * ------------------------------------------------------------------------
1693 *
1694 * CreateToken --
1695 *
1696 * Looks for a Source record in the hash table for drag&drop source
1697 * widgets. Creates a new record if the widget name is not already
1698 * registered. Returns a pointer to the desired record.
1699 *
1700 * ------------------------------------------------------------------------
1701 */
1702 static void
1703 DestroyToken(data)
1704 DestroyData data;
1705 {
1706 Dnd *dndPtr = (Dnd *)data;
1707 Token *tokenPtr = dndPtr->tokenPtr;
1708
1709 dndPtr->tokenPtr = NULL;
1710 if (tokenPtr == NULL) {
1711 return;
1712 }
1713 if (tokenPtr->flags & TOKEN_REDRAW) {
1714 Tcl_CancelIdleCall(DisplayToken, dndPtr);
1715 }
1716 Tk_FreeOptions(tokenConfigSpecs, (char *)tokenPtr, dndPtr->display, 0);
1717 if (tokenPtr->timerToken) {
1718 Tcl_DeleteTimerHandler(tokenPtr->timerToken);
1719 }
1720 if (tokenPtr->fillGC != NULL) {
1721 Tk_FreeGC(dndPtr->display, tokenPtr->fillGC);
1722 }
1723 if (tokenPtr->outlineGC != NULL) {
1724 Tk_FreeGC(dndPtr->display, tokenPtr->outlineGC);
1725 }
1726 if (tokenPtr->tkwin != NULL) {
1727 Tk_DeleteEventHandler(tokenPtr->tkwin,
1728 ExposureMask | StructureNotifyMask, TokenEventProc, dndPtr);
1729 Tk_DestroyWindow(tokenPtr->tkwin);
1730 }
1731 Blt_Free(tokenPtr);
1732 }
1733
1734 /*
1735 * ------------------------------------------------------------------------
1736 *
1737 * TokenEventProc --
1738 *
1739 * Invoked by the Tk dispatcher to handle widget events.
1740 * Manages redraws for the drag&drop token window.
1741 *
1742 * ------------------------------------------------------------------------
1743 */
1744 static void
1745 TokenEventProc(clientData, eventPtr)
1746 ClientData clientData; /* data associated with widget */
1747 XEvent *eventPtr; /* information about event */
1748 {
1749 Dnd *dndPtr = clientData;
1750 Token *tokenPtr = dndPtr->tokenPtr;
1751
1752 if ((eventPtr->type == Expose) && (eventPtr->xexpose.count == 0)) {
1753 if (tokenPtr->tkwin != NULL) {
1754 EventuallyRedrawToken(dndPtr);
1755 }
1756 } else if (eventPtr->type == DestroyNotify) {
1757 tokenPtr->tkwin = NULL;
1758 if (tokenPtr->flags & TOKEN_REDRAW) {
1759 tokenPtr->flags &= ~TOKEN_REDRAW;
1760 Tcl_CancelIdleCall(DisplayToken, dndPtr);
1761 }
1762 Tcl_EventuallyFree(dndPtr, DestroyToken);
1763 }
1764 }
1765
1766 /*
1767 * ------------------------------------------------------------------------
1768 *
1769 * CreateToken --
1770 *
1771 * Looks for a Source record in the hash table for drag&drop source
1772 * widgets. Creates a new record if the widget name is not already
1773 * registered. Returns a pointer to the desired record.
1774 *
1775 * ------------------------------------------------------------------------
1776 */
1777 static int
1778 CreateToken(interp, dndPtr)
1779 Tcl_Interp *interp;
1780 Dnd *dndPtr;
1781 {
1782 XSetWindowAttributes attrs;
1783 Tk_Window tkwin;
1784 unsigned int mask;
1785 Token *tokenPtr;
1786
1787 tokenPtr = Blt_Calloc(1, sizeof(Token));
1788 assert(tokenPtr);
1789 tokenPtr->anchor = TK_ANCHOR_SE;
1790 tokenPtr->relief = TK_RELIEF_RAISED;
1791 tokenPtr->activeRelief = TK_RELIEF_SUNKEN;
1792 tokenPtr->borderWidth = tokenPtr->activeBorderWidth = 3;
1793
1794 /* Create toplevel on parent's screen. */
1795 tkwin = Tk_CreateWindow(interp, dndPtr->tkwin, "dndtoken", "");
1796 if (tkwin == NULL) {
1797 Blt_Free(tokenPtr);
1798 return TCL_ERROR;
1799 }
1800 tokenPtr->tkwin = tkwin;
1801 Tk_SetClass(tkwin, "DndToken");
1802 Tk_CreateEventHandler(tkwin, ExposureMask | StructureNotifyMask,
1803 TokenEventProc, dndPtr);
1804 attrs.override_redirect = True;
1805 attrs.backing_store = WhenMapped;
1806 attrs.save_under = True;
1807 mask = CWOverrideRedirect | CWSaveUnder | CWBackingStore;
1808 Tk_ChangeWindowAttributes(tkwin, mask, &attrs);
1809 Tk_SetInternalBorder(tkwin, tokenPtr->borderWidth + 2);
1810 Tk_MakeWindowExist(tkwin);
1811 dndPtr->tokenPtr = tokenPtr;
1812 return TCL_OK;
1813 }
1814
1815 /*
1816 * ------------------------------------------------------------------------
1817 *
1818 * ConfigureToken --
1819 *
1820 * Called to process an (argc,argv) list to configure (or
1821 * reconfigure) a drag&drop source widget.
1822 *
1823 * ------------------------------------------------------------------------
1824 */
1825 static int
1826 ConfigureToken(interp, dndPtr, argc, argv, flags)
1827 Tcl_Interp *interp; /* current interpreter */
1828 Dnd *dndPtr; /* Drag&drop source widget record */
1829 int argc; /* number of arguments */
•1830 CONST char **argv; /* argument strings */
1831 int flags; /* flags controlling interpretation */
1832 {
1833 GC newGC;
1834 Token *tokenPtr = dndPtr->tokenPtr;
1835 XGCValues gcValues;
1836 unsigned long gcMask;
1837
1838 Tk_MakeWindowExist(tokenPtr->tkwin);
•1839 if (Blt_ConfigureWidget(interp, tokenPtr->tkwin, tokenConfigSpecs, argc,
1840 argv, (char *)tokenPtr, flags) != TCL_OK) {
1841 return TCL_ERROR;
1842 }
1843 /*
1844 * Set up the rejection outline GC for the token window...
1845 */
1846 gcValues.foreground = tokenPtr->outlineColor->pixel;
1847 gcValues.subwindow_mode = IncludeInferiors;
1848 gcValues.graphics_exposures = False;
1849 gcValues.line_style = LineSolid;
1850 gcValues.cap_style = CapButt;
1851 gcValues.join_style = JoinBevel;
1852
1853 gcMask = GCForeground | GCSubwindowMode | GCLineStyle |
1854 GCCapStyle | GCJoinStyle | GCGraphicsExposures;
1855 newGC = Tk_GetGC(dndPtr->tkwin, gcMask, &gcValues);
1856
1857 if (tokenPtr->outlineGC != NULL) {
1858 Tk_FreeGC(dndPtr->display, tokenPtr->outlineGC);
1859 }
1860 tokenPtr->outlineGC = newGC;
1861
1862 /*
1863 * Set up the rejection fill GC for the token window...
1864 */
1865 gcValues.foreground = tokenPtr->fillColor->pixel;
1866 if (tokenPtr->rejectStipple != None) {
1867 gcValues.stipple = tokenPtr->rejectStipple;
1868 gcValues.fill_style = FillStippled;
1869 gcMask |= GCStipple | GCFillStyle;
1870 }
1871 newGC = Tk_GetGC(dndPtr->tkwin, gcMask, &gcValues);
1872
1873 if (tokenPtr->fillGC != NULL) {
1874 Tk_FreeGC(dndPtr->display, tokenPtr->fillGC);
1875 }
1876 tokenPtr->fillGC = newGC;
1877
1878 if ((tokenPtr->reqWidth > 0) && (tokenPtr->reqHeight > 0)) {
1879 Tk_GeometryRequest(tokenPtr->tkwin, tokenPtr->reqWidth,
1880 tokenPtr->reqHeight);
1881 }
1882 /*
1883 * Reset the border width in case it has changed...
1884 */
1885 Tk_SetInternalBorder(tokenPtr->tkwin, tokenPtr->borderWidth + 2);
1886 return TCL_OK;
1887 }
1888
1889 static int
1890 GetFormattedData(dndPtr, format, timestamp, resultPtr)
1891 Dnd *dndPtr;
1892 char *format;
1893 int timestamp;
1894 Tcl_DString *resultPtr;
1895 {
1896 Tcl_Interp *interp = dndPtr->interp;
1897 Blt_HashEntry *hPtr;
1898 char **formatCmd;
1899 Tcl_DString savedResult;
1900 Tcl_DString dString;
1901 char **p;
1902 int x, y;
1903
1904 /* Find the data converter for the prescribed format. */
1905 hPtr = Blt_FindHashEntry(&(dndPtr->getDataTable), format);
1906 if (hPtr == NULL) {
1907 Tcl_AppendResult(interp, "can't find format \"", format,
1908 "\" in source \"", Tk_PathName(dndPtr->tkwin), "\"", (char *)NULL);
1909 return TCL_ERROR;
1910 }
1911 formatCmd = (char **)Blt_GetHashValue(hPtr);
1912 Tcl_DStringInit(&dString);
1913 for (p = formatCmd; *p != NULL; p++) {
1914 Tcl_DStringAppendElement(&dString, *p);
1915 }
1916 x = dndPtr->dragX - Blt_RootX(dndPtr->tkwin);
1917 y = dndPtr->dragY - Blt_RootY(dndPtr->tkwin);
1918 Tcl_DStringAppendElement(&dString, Tk_PathName(dndPtr->tkwin));
1919 Tcl_DStringAppendElement(&dString, "x");
1920 Tcl_DStringAppendElement(&dString, Blt_Itoa(x));
1921 Tcl_DStringAppendElement(&dString, "y");
1922 Tcl_DStringAppendElement(&dString, Blt_Itoa(y));
1923 Tcl_DStringAppendElement(&dString, "timestamp");
1924 Tcl_DStringAppendElement(&dString, Blt_Utoa(timestamp));
1925 Tcl_DStringAppendElement(&dString, "format");
1926 Tcl_DStringAppendElement(&dString, format);
1927 Tcl_DStringInit(&savedResult);
1928 Tcl_DStringGetResult(interp, &savedResult);
1929 if (Tcl_GlobalEval(interp, Tcl_DStringValue(&dString)) != TCL_OK) {
1930 Tcl_BackgroundError(interp);
1931 }
1932 Tcl_DStringFree(&dString);
1933 Tcl_DStringInit(resultPtr);
1934 Tcl_DStringGetResult(interp, resultPtr);
1935
1936 /* Restore the interpreter result. */
1937 Tcl_DStringResult(interp, &savedResult);
1938 return TCL_OK;
1939 }
1940
1941
1942 /*
1943 * ------------------------------------------------------------------------
1944 *
1945 * DestroyDnd --
1946 *
1947 * Free resources allocated for the drag&drop window.
1948 *
1949 * ------------------------------------------------------------------------
1950 */
1951 static void
1952 DestroyDnd(data)
1953 DestroyData data;
1954 {
1955 Dnd *dndPtr = (Dnd *)data;
1956 Blt_HashEntry *hPtr;
1957 Blt_HashSearch cursor;
1958 char **cmd;
1959
1960 Tk_FreeOptions(configSpecs, (char *)dndPtr, dndPtr->display, 0);
1961 Tk_DeleteGenericHandler(DndEventProc, dndPtr);
1962 for (hPtr = Blt_FirstHashEntry(&(dndPtr->getDataTable), &cursor);
1963 hPtr != NULL; hPtr = Blt_NextHashEntry(&cursor)) {
1964 cmd = (char **)Blt_GetHashValue(hPtr);
1965 if (cmd != NULL) {
1966 Blt_Free(cmd);
1967 }
1968 }
1969 Blt_DeleteHashTable(&(dndPtr->getDataTable));
1970
1971 for (hPtr = Blt_FirstHashEntry(&(dndPtr->setDataTable), &cursor);
1972 hPtr != NULL; hPtr = Blt_NextHashEntry(&cursor)) {
1973 cmd = (char **)Blt_GetHashValue(hPtr);
1974 if (cmd != NULL) {
1975 Blt_Free(cmd);
1976 }
1977 }
1978 Blt_DeleteHashTable(&(dndPtr->setDataTable));
1979 if (dndPtr->rootPtr != NULL) {
1980 FreeWinfo(dndPtr->rootPtr);
1981 }
1982 if (dndPtr->cursor != None) {
1983 Tk_FreeCursor(dndPtr->display, dndPtr->cursor);
1984 }
1985 if (dndPtr->reqFormats != NULL) {
1986 Blt_Free(dndPtr->reqFormats);
1987 }
1988 if (dndPtr->matchingFormats != NULL) {
1989 Blt_Free(dndPtr->matchingFormats);
1990 }
1991
1992 /* Now that the various commands are custom list options, we need
1993 * to explicitly free them. */
1994 if (dndPtr->motionCmd != NULL) {
1995 Blt_Free(dndPtr->motionCmd);
1996 }
1997 if (dndPtr->leaveCmd != NULL) {
1998 Blt_Free(dndPtr->leaveCmd);
1999 }
2000 if (dndPtr->enterCmd != NULL) {
2001 Blt_Free(dndPtr->enterCmd);
2002 }
2003 if (dndPtr->dropCmd != NULL) {
2004 Blt_Free(dndPtr->dropCmd);
2005 }
2006 if (dndPtr->resultCmd != NULL) {
2007 Blt_Free(dndPtr->resultCmd);
2008 }
2009 if (dndPtr->packageCmd != NULL) {
2010 Blt_Free(dndPtr->packageCmd);
2011 }
2012 if (dndPtr->siteCmd != NULL) {
2013 Blt_Free(dndPtr->siteCmd);
2014 }
2015
2016 if (dndPtr->hashPtr != NULL) {
2017 Blt_DeleteHashEntry(&(dndPtr->dataPtr->dndTable), dndPtr->hashPtr);
2018 }
2019 if (dndPtr->tokenPtr != NULL) {
2020 DestroyToken((DestroyData)dndPtr);
2021 }
2022 if (dndPtr->tkwin != NULL) {
2023 XDeleteProperty(dndPtr->display, Tk_WindowId(dndPtr->tkwin),
2024 dndPtr->dataPtr->targetAtom);
2025 XDeleteProperty(dndPtr->display, Tk_WindowId(dndPtr->tkwin),
2026 dndPtr->dataPtr->commAtom);
2027 }
2028 Blt_Free(dndPtr);
2029 }
2030
2031
2032 /*
2033 * ------------------------------------------------------------------------
2034 *
2035 * GetDnd --
2036 *
2037 * Looks for a Source record in the hash table for drag&drop source
2038 * widgets. Returns a pointer to the desired record.
2039 *
2040 * ------------------------------------------------------------------------
2041 */
2042 static int
2043 GetDnd(clientData, interp, pathName, dndPtrPtr)
2044 ClientData clientData;
2045 Tcl_Interp *interp;
2046 char *pathName; /* widget pathname for desired record */
2047 Dnd **dndPtrPtr;
2048 {
2049 DndInterpData *dataPtr = clientData;
2050 Blt_HashEntry *hPtr;
2051 Tk_Window tkwin;
2052
2053 tkwin = Tk_NameToWindow(interp, pathName, dataPtr->mainWindow);
2054 if (tkwin == NULL) {
2055 return TCL_ERROR;
2056 }
2057 hPtr = Blt_FindHashEntry(&(dataPtr->dndTable), (char *)tkwin);
2058 if (hPtr == NULL) {
2059 Tcl_AppendResult(interp, "window \"", pathName,
2060 "\" is not a drag&drop source/target", (char *)NULL);
2061 return TCL_ERROR;
2062 }
2063 *dndPtrPtr = (Dnd *)Blt_GetHashValue(hPtr);
2064 return TCL_OK;
2065 }
2066
2067 /*
2068 * ------------------------------------------------------------------------
2069 *
2070 * CreateDnd --
2071 *
2072 * Looks for a Source record in the hash table for drag&drop source
2073 * widgets. Creates a new record if the widget name is not already
2074 * registered. Returns a pointer to the desired record.
2075 *
2076 * ------------------------------------------------------------------------
2077 */
2078 static Dnd *
2079 CreateDnd(interp, tkwin)
2080 Tcl_Interp *interp;
2081 Tk_Window tkwin; /* widget for desired record */
2082 {
2083 Dnd *dndPtr;
2084
2085 dndPtr = Blt_Calloc(1, sizeof(Dnd));
2086 assert(dndPtr);
2087 dndPtr->interp = interp;
2088 dndPtr->display = Tk_Display(tkwin);
2089 dndPtr->tkwin = tkwin;
2090 Tk_MakeWindowExist(tkwin);
2091 Blt_InitHashTable(&(dndPtr->setDataTable), BLT_STRING_KEYS);
2092 Blt_InitHashTable(&(dndPtr->getDataTable), BLT_STRING_KEYS);
2093 Tk_CreateGenericHandler(DndEventProc, dndPtr);
2094 return dndPtr;
2095 }
2096
2097 static int
2098 ConfigureDnd(interp, dndPtr)
2099 Tcl_Interp *interp;
2100 Dnd *dndPtr;
2101 {
2102 Tcl_CmdInfo cmdInfo;
2103 Tcl_DString dString;
2104 int button, result;
2105
2106 if (!Tcl_GetCommandInfo(interp, "blt::DndInit", &cmdInfo)) {
2107 static char cmd[] = "source [file join $blt_library dnd.tcl]";
2108 /*
2109 * If the "DndInit" routine hasn't been sourced, do it now.
2110 */
2111 if (Tcl_GlobalEval(interp, cmd) != TCL_OK) {
2112 Tcl_AddErrorInfo(interp,
2113 "\n (while loading bindings for blt::drag&drop)");
2114 return TCL_ERROR;
2115 }
2116 }
2117 /*
2118 * Reset the target property if it's changed state or
2119 * added/subtracted one of its callback procedures.
2120 */
2121 if (Blt_ConfigModified(configSpecs, interp, "-target", "-onenter", "-onmotion",
2122 "-onleave", (char *)NULL)) {
2123 if (dndPtr->targetPropertyExists) {
2124 XDeleteProperty(dndPtr->display, Tk_WindowId(dndPtr->tkwin),
2125 dndPtr->dataPtr->targetAtom);
2126 dndPtr->targetPropertyExists = FALSE;
2127 }
2128 if (dndPtr->isTarget) {
2129 AddTargetProperty(dndPtr);
2130 dndPtr->targetPropertyExists = TRUE;
2131 }
2132 }
2133 if (dndPtr->isSource) {
2134 /* Check the button binding for valid range (0 or 1-5) */
2135 if ((dndPtr->reqButton < 0) || (dndPtr->reqButton > 5)) {
2136 Tcl_AppendResult(interp,
2137 "button must be 1-5, or 0 for no bindings",
2138 (char *)NULL);
2139 return TCL_ERROR;
2140 }
2141 button = dndPtr->reqButton;
2142 } else {
2143 button = 0;
2144 }
2145 Tcl_DStringInit(&dString);
2146 Blt_DStringAppendElements(&dString, "blt::DndInit",
2147 Tk_PathName(dndPtr->tkwin), Blt_Itoa(button), (char *)NULL);
2148 result = Tcl_GlobalEval(interp, Tcl_DStringValue(&dString));
2149 Tcl_DStringFree(&dString);
2150 if (result != TCL_OK) {
2151 return TCL_ERROR;
2152 }
2153 return TCL_OK;
2154 }
2155
2156
2157 /*
2158 *----------------------------------------------------------------------
2159 *
2160 * SendRestrictProc --
2161 *
2162 * This procedure filters incoming events when a "send" command
2163 * is outstanding. It defers all events except those containing
2164 * send commands and results.
2165 *
2166 * Results:
2167 * False is returned except for property-change events on a
2168 * commWindow.
2169 *
2170 * Side effects:
2171 * None.
2172 *
2173 *----------------------------------------------------------------------
2174 */
2175
2176 /* ARGSUSED */
2177 static Tk_RestrictAction
2178 SendRestrictProc(clientData, eventPtr)
2179 ClientData clientData; /* Drag-and-drop manager. */
2180 register XEvent *eventPtr; /* Event that just arrived. */
2181 {
2182 Dnd *dndPtr = clientData;
2183
2184 if (eventPtr->xproperty.window != Tk_WindowId(dndPtr->tkwin)) {
2185 return TK_PROCESS_EVENT; /* Event not in our window. */
2186 }
2187 if ((eventPtr->type == PropertyNotify) &&
2188 (eventPtr->xproperty.state == PropertyNewValue)) {
2189 return TK_PROCESS_EVENT; /* This is the one we want to process. */
2190 }
2191 if (eventPtr->type == Expose) {
2192 return TK_PROCESS_EVENT; /* Let expose events also get
2193 * handled. */
2194 }
2195 return TK_DEFER_EVENT; /* Defer everything else. */
2196 }
2197
2198 /*
2199 *----------------------------------------------------------------------
2200 *
2201 * SendTimerProc --
2202 *
2203 * Procedure called when the timer event elapses. Used to wait
2204 * between attempts checking for the designated window.
2205 *
2206 * Results:
2207 * None.
2208 *
2209 * Side Effects:
2210 * Sets a flag, indicating the timeout occurred.
2211 *
2212 *----------------------------------------------------------------------
2213 */
2214 static void
2215 SendTimerProc(clientData)
2216 ClientData clientData;
2217 {
2218 int *statusPtr = clientData;
2219
2220 /*
2221 * An unusually long amount of time has elapsed since the drag
2222 * start message was sent. Assume that the other party has died
2223 * and abort the operation.
2224 */
2225 *statusPtr = DROP_FAIL;
2226 }
2227
2228 #define WAIT_INTERVAL 2000 /* Twenty seconds. */
2229
2230 /*
2231 * ------------------------------------------------------------------------
2232 *
2233 * TargetPropertyEventProc --
2234 *
2235 * Invoked by the Tk dispatcher to handle widget events.
2236 * Manages redraws for the drag&drop token window.
2237 *
2238 * ------------------------------------------------------------------------
2239 */
2240 static void
2241 TargetPropertyEventProc(clientData, eventPtr)
2242 ClientData clientData; /* Data associated with transaction. */
2243 XEvent *eventPtr; /* information about event */
2244 {
2245 DropPending *pendingPtr = clientData;
2246 char *data;
2247 int result, format;
2248 Atom typeAtom;
2249 unsigned long nItems, bytesAfter;
2250
2251 #ifdef notdef
2252 fprintf(stderr, "TargetPropertyEventProc\n");
2253 #endif
2254 if ((eventPtr->type != PropertyNotify) ||
2255 (eventPtr->xproperty.atom != pendingPtr->commAtom) ||
2256 (eventPtr->xproperty.state != PropertyNewValue)) {
2257 return;
2258 }
2259 Tcl_DeleteTimerHandler(pendingPtr->timerToken);
2260 data = NULL;
2261 result = XGetWindowProperty(
2262 eventPtr->xproperty.display, /* Display of window. */
2263 eventPtr->xproperty.window, /* Window holding the property. */
2264 eventPtr->xproperty.atom, /* Name of property. */
2265 0, /* Offset of data (for multiple reads). */
2266 pendingPtr->packetSize, /* Maximum number of items to read. */
2267 False, /* If true, delete the property. */
2268 XA_STRING, /* Desired type of property. */
2269 &typeAtom, /* (out) Actual type of the property. */
2270 &format, /* (out) Actual format of the property. */
2271 &nItems, /* (out) # of items in specified format. */
2272 &bytesAfter, /* (out) # of bytes remaining to be read. */
2273 (unsigned char **)&data);
2274 #ifdef notdef
2275 fprintf(stderr,
2276 "TargetPropertyEventProc: result=%d, typeAtom=%d, format=%d, nItems=%d\n",
2277 result, typeAtom, format, nItems);
2278 #endif
2279 pendingPtr->status = DROP_FAIL;
2280 if ((result == Success) && (typeAtom == XA_STRING) && (format == 8)) {
2281 pendingPtr->status = DROP_OK;
2282 #ifdef notdef
2283 fprintf(stderr, "data found is (%s)\n", data);
2284 #endif
2285 Tcl_DStringAppend(&(pendingPtr->dString), data, -1);
2286 XFree(data);
2287 if (nItems == pendingPtr->packetSize) {
2288 /* Normally, we'll receive the data in one chunk. But if
2289 * more are required, reset the timer and go back into the
2290 * wait loop again. */
2291 pendingPtr->timerToken = Tcl_CreateTimerHandler(WAIT_INTERVAL,
2292 SendTimerProc, &pendingPtr->status);
2293 pendingPtr->status = DROP_CONTINUE;
2294 }
2295 }
2296 /* Set an empty, zero-length value on the source's property. This
2297 * acts as a handshake, indicating that the target received the
2298 * latest chunk. */
2299 #ifdef notdef
2300 fprintf(stderr, "TargetPropertyEventProc: set response property\n");
2301 #endif
2302 XChangeProperty(pendingPtr->display, pendingPtr->window,
2303 pendingPtr->commAtom, XA_STRING, 8, PropModeReplace,
2304 (unsigned char *)"", 0);
2305 }
2306
2307 #ifdef HAVE_XDND
2308
2309 static int
2310 XDndSelectionProc(clientData, interp, portion)
2311 ClientData clientData;
2312 Tcl_Interp *interp;
2313 char *portion;
2314 {
2315 DropPending *pendingPtr = clientData;
2316
2317 Tcl_DStringAppend(&(pendingPtr->dString), portion, -1);
2318 #ifdef notdef
2319 fprintf(stderr, "-> XDndGetSelectionProc\n");
2320 #endif
2321 return TCL_OK;
2322 }
2323
2324 #endif /* HAVE_XDND */
2325
2326 static void
2327 CompleteDataTransaction(dndPtr, format, pendingPtr)
2328 Dnd *dndPtr;
2329 char *format;
2330 DropPending *pendingPtr;
2331 {
2332 DndInterpData *dataPtr = dndPtr->dataPtr;
2333 Tk_Window tkwin;
2334 Atom formatAtom;
2335
2336 #ifdef notdef
2337 fprintf(stderr, "-> CompleteDataTransaction\n");
2338 #endif
2339 /* Check if the source is local to the application. */
2340 tkwin = Tk_IdToWindow(dndPtr->display, pendingPtr->window);
2341 if (tkwin != NULL) {
2342 Blt_HashEntry *hPtr;
2343
2344 hPtr = Blt_FindHashEntry(&(dndPtr->dataPtr->dndTable), (char *)tkwin);
2345 if (hPtr != NULL) {
2346 Dnd *srcPtr;
2347
2348 srcPtr = (Dnd *)Blt_GetHashValue(hPtr);
2349 GetFormattedData(srcPtr, format, pendingPtr->timestamp,
2350 &(pendingPtr->dString));
2351 }
2352 return;
2353 }
2354
2355 formatAtom = XInternAtom(pendingPtr->display, format, False);
2356
2357 if (pendingPtr->protocol == PROTO_XDND) {
2358 #ifdef HAVE_XDND
2359 if (Tk_GetSelection(dndPtr->interp, dndPtr->tkwin,
2360 dataPtr->XdndSelectionAtom, formatAtom, XDndSelectionProc,
2361 pendingPtr) != TCL_OK) {
2362 pendingPtr->status = DROP_FAIL;
2363 }
2364 #endif
2365 pendingPtr->status = DROP_OK;
2366 } else {
2367 Tk_RestrictProc *proc;
2368 ClientData arg;
2369
2370 SendClientMsg(pendingPtr->display, pendingPtr->window,
2371 dataPtr->mesgAtom,
2372 TS_START_DROP,
2373 (int)Tk_WindowId(dndPtr->tkwin),
2374 pendingPtr->timestamp,
2375 (int)formatAtom,
2376 (int)pendingPtr->commAtom);
2377
2378 pendingPtr->commAtom = dndPtr->dataPtr->commAtom;
2379 pendingPtr->status = DROP_CONTINUE;
2380 pendingPtr->display = dndPtr->display;
2381 proc = Tk_RestrictEvents(SendRestrictProc, dndPtr, &arg);
2382 Tk_CreateEventHandler(dndPtr->tkwin, PropertyChangeMask,
2383 TargetPropertyEventProc, pendingPtr);
2384 pendingPtr->timerToken = Tcl_CreateTimerHandler(WAIT_INTERVAL,
2385 SendTimerProc, &pendingPtr->status);
2386 /*
2387 * ----------------------------------------------------------
2388 *
2389 * Enter a loop processing X events until the all the data is
2390 * received or the source is declared to be dead (i.e. we
2391 * timeout). While waiting for a result, restrict handling to
2392 * just property-related events so that the transfer is
2393 * synchronous with respect to other events in the widget.
2394 *
2395 * ----------------------------------------------------------
2396 */
2397 while (pendingPtr->status == DROP_CONTINUE) {
2398 /* Wait for property event. */
2399 Tcl_DoOneEvent(TCL_ALL_EVENTS);
2400 }
2401 Tk_RestrictEvents(proc, arg, &arg);
2402 Tcl_DeleteTimerHandler(pendingPtr->timerToken);
2403 Tk_DeleteEventHandler(dndPtr->tkwin, PropertyChangeMask,
2404 TargetPropertyEventProc, pendingPtr);
2405 }
2406 #ifdef notdef
2407 fprintf(stderr, "<- CompleteDataTransaction\n");
2408 #endif
2409 }
2410
2411 /*
2412 * ------------------------------------------------------------------------
2413 *
2414 * SourcePropertyEventProc --
2415 *
2416 * Invoked by the Tk dispatcher when a PropertyNotify event occurs
2417 * on the source window. The event acts as a handshake between the
2418 * target and the source. The source acknowledges the target has
2419 * received the last packet of data and sends the next packet.
2420 *
2421 * Note a special case. If the data is divisible by the packetsize,
2422 * then an extra zero-length packet is sent to mark the end of the
2423 * data. A packetsize length packet indicates more is to follow.
2424 *
2425 * Normally the property is empty (zero-length). But if an
2426 * errored occurred on the target, it will contain the error
2427 * message.
2428 *
2429 * ------------------------------------------------------------------------
2430 */
2431 static void
2432 SourcePropertyEventProc(clientData, eventPtr)
2433 ClientData clientData; /* data associated with widget */
2434 XEvent *eventPtr; /* information about event */
2435 {
2436 DropPending *pendingPtr = clientData;
2437 char *data;
2438 int result, format;
2439 Atom typeAtom;
2440 unsigned long nItems, bytesAfter;
2441 int size, bytesLeft;
2442 unsigned char *p;
2443
2444 #ifdef notdef
2445 fprintf(stderr, "-> SourcePropertyEventProc\n");
2446 #endif
2447 if ((eventPtr->xproperty.atom != pendingPtr->commAtom)
2448 || (eventPtr->xproperty.state != PropertyNewValue)) {
2449 return;
2450 }
2451 Tcl_DeleteTimerHandler(pendingPtr->timerToken);
2452 data = NULL;
2453 result = XGetWindowProperty(
2454 eventPtr->xproperty.display, /* Display of window. */
2455 eventPtr->xproperty.window, /* Window holding the property. */
2456 eventPtr->xproperty.atom, /* Name of property. */
2457 0, /* Offset of data (for multiple reads). */
2458 pendingPtr->packetSize, /* Maximum number of items to read. */
2459 True, /* If true, delete the property. */
2460 XA_STRING, /* Desired type of property. */
2461 &typeAtom, /* (out) Actual type of the property. */
2462 &format, /* (out) Actual format of the property. */
2463 &nItems, /* (out) # of items in specified format. */
2464 &bytesAfter, /* (out) # of bytes remaining to be read. */
2465 (unsigned char **)&data);
2466
2467 if ((result != Success) || (typeAtom != XA_STRING) || (format != 8)) {
2468 pendingPtr->status = DROP_FAIL;
2469 #ifdef notdef
2470 fprintf(stderr, "<- SourcePropertyEventProc: wrong format\n");
2471 #endif
2472 return; /* Wrong data format. */
2473 }
2474 if (nItems > 0) {
2475 pendingPtr->status = DROP_FAIL;
2476 Tcl_DStringFree(&(pendingPtr->dString));
2477 Tcl_DStringAppend(&(pendingPtr->dString), data, -1);
2478 XFree(data);
2479 #ifdef notdef
2480 fprintf(stderr, "<- SourcePropertyEventProc: error\n");
2481 #endif
2482 return; /* Error occurred on target. */
2483 }
2484 bytesLeft = Tcl_DStringLength(&(pendingPtr->dString)) - pendingPtr->offset;
2485 if (bytesLeft <= 0) {
2486 #ifdef notdef
2487 fprintf(stderr, "<- SourcePropertyEventProc: done\n");
2488 #endif
2489 pendingPtr->status = DROP_OK;
2490 size = 0;
2491 } else {
2492 size = MIN(bytesLeft, pendingPtr->packetSize);
2493 pendingPtr->status = DROP_CONTINUE;
2494 }
2495 p = (unsigned char *)Tcl_DStringValue(&(pendingPtr->dString)) +
2496 pendingPtr->offset;
2497 XChangeProperty(pendingPtr->display, pendingPtr->window,
2498 pendingPtr->commAtom, XA_STRING, 8, PropModeReplace, p, size);
2499 pendingPtr->offset += size;
2500 pendingPtr->timerToken = Tcl_CreateTimerHandler(WAIT_INTERVAL,
2501 SendTimerProc, &pendingPtr->status);
2502 #ifdef notdef
2503 fprintf(stderr, "<- SourcePropertyEventProc\n");
2504 #endif
2505 }
2506
2507
2508 static void
2509 SendDataToTarget(dndPtr, pendingPtr)
2510 Dnd *dndPtr;
2511 DropPending *pendingPtr;
2512 {
2513 int size;
2514 Tk_RestrictProc *proc;
2515 ClientData arg;
2516
2517 #ifdef notdef
2518 fprintf(stderr, "-> SendDataToTarget\n");
2519 #endif
2520 Tk_CreateEventHandler(dndPtr->tkwin, PropertyChangeMask,
2521 SourcePropertyEventProc, pendingPtr);
2522 pendingPtr->timerToken = Tcl_CreateTimerHandler(WAIT_INTERVAL,
2523 SendTimerProc, &pendingPtr->status);
2524 size = MIN(Tcl_DStringLength(&pendingPtr->dString), pendingPtr->packetSize);
2525
2526 proc = Tk_RestrictEvents(SendRestrictProc, dndPtr, &arg);
2527
2528 /*
2529 * Setting the property starts the process. The target will
2530 * see the PropertyChange event and respond accordingly.
2531 */
2532 XChangeProperty(dndPtr->display, pendingPtr->window,
2533 pendingPtr->commAtom, XA_STRING, 8, PropModeReplace,
2534 (unsigned char *)Tcl_DStringValue(&(pendingPtr->dString)), size);
2535 pendingPtr->offset += size;
2536
2537 /*
2538 * Enter a loop processing X events until the result comes
2539 * in or the target is declared to be dead. While waiting
2540 * for a result, look only at property-related events so that
2541 * the handshake is synchronous with respect to other events in
2542 * the application.
2543 */
2544 pendingPtr->status = DROP_CONTINUE;
2545 while (pendingPtr->status == DROP_CONTINUE) {
2546 /* Wait for the property change event. */
2547 Tcl_DoOneEvent(TCL_ALL_EVENTS);
2548 }
2549 Tk_RestrictEvents(proc, arg, &arg);
2550 Tcl_DeleteTimerHandler(pendingPtr->timerToken);
2551 Tk_DeleteEventHandler(dndPtr->tkwin, PropertyChangeMask,
2552 SourcePropertyEventProc, pendingPtr);
2553 #ifdef notdef
2554 fprintf(stderr, "<- SendDataToTarget\n");
2555 #endif
2556 }
2557
2558 static void
2559 DoDrop(dndPtr, eventPtr)
2560 Dnd *dndPtr;
2561 XEvent *eventPtr;
2562 {
2563 DndInterpData *dataPtr = dndPtr->dataPtr;
2564 Token *tokenPtr = dndPtr->tokenPtr;
2565 Tcl_Interp *interp = dndPtr->interp;
2566 struct DropRequest {
2567 int mesg; /* TS_DROP_RESULT message. */
2568 Window window; /* Target window. */
2569 int timestamp; /* Transaction timestamp. */
2570 Atom formatAtom; /* Format requested. */
2571 } *dropPtr;
2572 char *format;
2573 DropPending pending;
2574
2575 if (tokenPtr->timerToken != NULL) {
2576 Tcl_DeleteTimerHandler(tokenPtr->timerToken);
2577 }
2578 dropPtr = (struct DropRequest *)eventPtr->xclient.data.l;
2579 format = XGetAtomName(dndPtr->display, dropPtr->formatAtom);
2580 #ifdef notdef
2581 fprintf(stderr, "DoDrop %s 0x%x\n", Tk_PathName(dndPtr->tkwin),
2582 dropPtr->window);
2583 #endif
2584 if (GetFormattedData(dndPtr, format, dropPtr->timestamp, &(pending.dString))
2585 != TCL_OK) {
2586 Tcl_BackgroundError(interp);
2587 /* Send empty string to break target's wait loop. */
2588 XChangeProperty(dndPtr->display, dropPtr->window, dataPtr->commAtom,
2589 XA_STRING, 8, PropModeReplace, (unsigned char *)"", 0);
2590 return;
2591 }
2592 pending.window = dropPtr->window;
2593 pending.display = dndPtr->display;
2594 pending.commAtom = dataPtr->commAtom;
2595 pending.offset = 0;
2596 pending.packetSize = GetMaxPropertySize(dndPtr->display);
2597 SendDataToTarget(dndPtr, &pending);
2598 Tcl_DStringFree(&(pending.dString));
2599 #ifdef notdef
2600 fprintf(stderr, "<- DoDrop\n");
2601 #endif
2602 }
2603
2604 static void
2605 DropFinished(dndPtr, eventPtr)
2606 Dnd *dndPtr;
2607 XEvent *eventPtr;
2608 {
2609 Tcl_Interp *interp = dndPtr->interp;
2610 Tcl_DString dString, savedResult;
2611 int result;
2612 char **p;
2613 struct DropResult {
2614 int mesg; /* TS_DROP_RESULT message. */
2615 Window window; /* Target window. */
2616 int timestamp; /* Transaction timestamp. */
2617 int result; /* Result of transaction. */
2618 } *dropPtr;
2619
2620 #ifdef notdef
2621 fprintf(stderr, "DropFinished\n");
2622 #endif
2623 dropPtr = (struct DropResult *)eventPtr->xclient.data.l;
2624
2625 Tcl_DStringInit(&dString);
2626 for (p = dndPtr->resultCmd; *p != NULL; p++) {
2627 Tcl_DStringAppendElement(&dString, *p);
2628 }
2629 Tcl_DStringAppendElement(&dString, Tk_PathName(dndPtr->tkwin));
2630 Tcl_DStringAppendElement(&dString, "action");
2631 Tcl_DStringAppendElement(&dString, NameOfAction(dropPtr->result));
2632 Tcl_DStringAppendElement(&dString, "timestamp");
2633 Tcl_DStringAppendElement(&dString, Blt_Utoa(dropPtr->timestamp));
2634
2635 Tcl_DStringInit(&savedResult);
2636 Tcl_DStringGetResult(interp, &savedResult);
2637 result = Tcl_GlobalEval(interp, Tcl_DStringValue(&dString));
2638 Tcl_DStringFree(&dString);
2639 if (result != TCL_OK) {
2640 Tcl_BackgroundError(interp);
2641 }
2642 Tcl_DStringResult(interp, &savedResult);
2643 }
2644
2645
2646 static void
2647 FreeFormats(dndPtr)
2648 Dnd *dndPtr;
2649 {
2650 if (dndPtr->matchingFormats != NULL) {
2651 Blt_Free(dndPtr->matchingFormats);
2652 dndPtr->matchingFormats = NULL;
2653 }
2654 dndPtr->lastId = None;
2655 }
2656
2657 static char *
2658 GetSourceFormats(dndPtr, window, timestamp)
2659 Dnd *dndPtr;
2660 Window window;
2661 int timestamp;
2662 {
2663 if (dndPtr->lastId != timestamp) {
2664 char *data;
2665
2666 FreeFormats(dndPtr);
2667 data = GetProperty(dndPtr->display, window,
2668 dndPtr->dataPtr->formatsAtom);
2669 if (data != NULL) {
2670 dndPtr->matchingFormats = Blt_Strdup(data);
2671 XFree(data);
2672 }
2673 dndPtr->lastId = timestamp;
2674 }
2675 if (dndPtr->matchingFormats == NULL) {
2676 return "";
2677 }
2678 return dndPtr->matchingFormats;
2679 }
2680
2681
2682 static int
2683 InvokeCallback(dndPtr, cmd, x, y, formats, button, keyState, timestamp)
2684 Dnd *dndPtr;
2685 char **cmd;
2686 int x, y;
2687 char *formats;
2688 int button, keyState, timestamp;
2689 {
2690 Tcl_DString dString, savedResult;
2691 Tcl_Interp *interp = dndPtr->interp;
2692 int result;
2693 char **p;
2694
2695 Tcl_DStringInit(&dString);
2696 for (p = cmd; *p != NULL; p++) {
2697 Tcl_DStringAppendElement(&dString, *p);
2698 }
2699 Tcl_DStringAppendElement(&dString, Tk_PathName(dndPtr->tkwin));
2700 x -= Blt_RootX(dndPtr->tkwin); /* Send coordinates relative to target. */
2701 y -= Blt_RootY(dndPtr->tkwin);
2702 Tcl_DStringAppendElement(&dString, "x");
2703 Tcl_DStringAppendElement(&dString, Blt_Itoa(x));
2704 Tcl_DStringAppendElement(&dString, "y");
2705 Tcl_DStringAppendElement(&dString, Blt_Itoa(y));
2706 Tcl_DStringAppendElement(&dString, "formats");
2707 if (formats == NULL) {
2708 formats = "";
2709 }
2710 Tcl_DStringAppendElement(&dString, formats);
2711 Tcl_DStringAppendElement(&dString, "button");
2712 Tcl_DStringAppendElement(&dString, Blt_Itoa(button));
2713 Tcl_DStringAppendElement(&dString, "state");
2714 Tcl_DStringAppendElement(&dString, Blt_Itoa(keyState));
2715 Tcl_DStringAppendElement(&dString, "timestamp");
2716 Tcl_DStringAppendElement(&dString, Blt_Utoa(timestamp));
2717 Tcl_Preserve(interp);
2718 Tcl_DStringInit(&savedResult);
2719 Tcl_DStringGetResult(interp, &savedResult);
2720 result = Tcl_GlobalEval(interp, Tcl_DStringValue(&dString));
2721 Tcl_DStringFree(&dString);
2722 if (result == TCL_OK) {
2723 result = GetDragResult(interp, Tcl_GetStringResult(interp));
2724 } else {
2725 result = DROP_CANCEL;
2726 Tcl_BackgroundError(interp);
2727 }
2728 Tcl_DStringResult(interp, &savedResult);
2729 Tcl_Release(interp);
2730 return result;
2731 }
2732
2733 /*
2734 * ------------------------------------------------------------------------
2735 *
2736 * AcceptDrop --
2737 *
2738 * Invokes a Tcl procedure to handle the target's side of the
2739 * drop. A Tcl procedure is invoked, either one designated for
2740 * this target by the user (-ondrop) or a default Tcl procedure.
2741 * It is passed the following arguments:
2742 *
2743 * widget The path name of the target.
2744 * x X-coordinate of the mouse relative to the
2745 * widget.
2746 * y Y-coordinate of the mouse relative to the
2747 * widget.
2748 * formats A list of data formats acceptable to both
2749 * the source and target.
2750 * button Button pressed.
2751 * state Key state.
2752 * timestamp Timestamp of transaction.
2753 * action Requested action from source.
2754 *
2755 * If the Tcl procedure returns "cancel", this indicates that the drop was
2756 * not accepted by the target and the reject symbol should be displayed.
2757 * Otherwise one of the following strings may be recognized:
2758 *
2759 * "cancel" Drop was canceled.
2760 * "copy" Source data has been successfully copied.
2761 * "link" Target has made a link to the data. It's
2762 * Ok for the source to remove it's association
2763 * with the data, but not to delete the data
2764 * itself.
2765 * "move" Source data has been successfully copied,
2766 * it's Ok for the source to delete its
2767 * association with the data and the data itself.
2768 *
2769 * The result is relayed back to the source via another client message.
2770 * The source may or may not be waiting for the result.
2771 *
2772 * Results:
2773 * None.
2774 *
2775 * Side Effects:
2776 * A Tcl procedure is invoked in the target to handle the drop event.
2777 * The result of the drop is sent (via another ClientMessage) to the
2778 * source.
2779 *
2780 * ------------------------------------------------------------------------
2781 */
2782 static int
2783 AcceptDrop(dndPtr, x, y, formats, button, keyState, timestamp)
2784 Dnd *dndPtr; /* Target where the drop event occurred. */
2785 int x, y;
2786 char *formats;
2787 int button, keyState, timestamp;
2788 {
2789 Tcl_Interp *interp = dndPtr->interp;
2790 char **cmd;
2791 Tcl_DString dString, savedResult;
2792 int result;
2793
2794 if (dndPtr->motionCmd != NULL) {
2795 result = InvokeCallback(dndPtr, dndPtr->motionCmd, x, y, formats,
2796 button, keyState, timestamp);
2797 if (result != DROP_OK) {
2798 return result;
2799 }
2800 }
2801 if (dndPtr->leaveCmd != NULL) {
2802 InvokeCallback(dndPtr, dndPtr->leaveCmd, x, y, formats, button,
2803 keyState, timestamp);
2804 }
2805 Tcl_DStringInit(&dString);
2806 cmd = dndPtr->dropCmd;
2807 if (cmd != NULL) {
2808 char **p;
2809
2810 for (p = cmd; *p != NULL; p++) {
2811 Tcl_DStringAppendElement(&dString, *p);
2812 }
2813 } else {
2814 Tcl_DStringAppendElement(&dString, "blt::DndStdDrop");
2815 }
2816 Tcl_DStringAppendElement(&dString, Tk_PathName(dndPtr->tkwin));
2817 dndPtr->dropX = x - Blt_RootX(dndPtr->tkwin);
2818 dndPtr->dropY = y - Blt_RootY(dndPtr->tkwin);
2819 Tcl_DStringAppendElement(&dString, "x");
2820 Tcl_DStringAppendElement(&dString, Blt_Itoa(dndPtr->dropX));
2821 Tcl_DStringAppendElement(&dString, "y");
2822 Tcl_DStringAppendElement(&dString, Blt_Itoa(dndPtr->dropY));
2823 Tcl_DStringAppendElement(&dString, "formats");
2824 Tcl_DStringAppendElement(&dString, formats);
2825 Tcl_DStringAppendElement(&dString, "button");
2826 Tcl_DStringAppendElement(&dString, Blt_Itoa(button));
2827 Tcl_DStringAppendElement(&dString, "state");
2828 Tcl_DStringAppendElement(&dString, Blt_Itoa(keyState));
2829 Tcl_DStringAppendElement(&dString, "timestamp");
2830 Tcl_DStringAppendElement(&dString, Blt_Utoa(timestamp));
2831 Tcl_Preserve(interp);
2832 Tcl_DStringInit(&savedResult);
2833 Tcl_DStringGetResult(interp, &savedResult);
2834 result = Tcl_GlobalEval(interp, Tcl_DStringValue(&dString));
2835 Tcl_DStringFree(&dString);
2836 if (result == TCL_OK) {
2837 result = GetAction(Tcl_GetStringResult(interp));
2838 } else {
2839 result = DROP_CANCEL;
2840 Tcl_BackgroundError(interp);
2841 }
2842 Tcl_DStringResult(interp, &savedResult);
2843 Tcl_Release(interp);
2844 return result;
2845 }
2846
2847 /*
2848 * ------------------------------------------------------------------------
2849 *
2850 * HandleDropEvent --
2851 *
2852 * Invokes a Tcl procedure to handle the target's side of the
2853 * drop. This routine is triggered via a client message from the
2854 * drag source indicating that the token was dropped over this
2855 * target. The fields of the incoming message are:
2856 *
2857 * data.l[0] Message type.
2858 * data.l[1] Window Id of the source.
2859 * data.l[2] Screen X-coordinate of the pointer.
2860 * data.l[3] Screen Y-coordinate of the pointer.
2861 * data.l[4] Id of the drag&drop transaction.
2862 *
2863 * A Tcl procedure is invoked, either one designated for this
2864 * target by the user (-ondrop) or a default Tcl procedure. It
2865 * is passed the following arguments:
2866 *
2867 * widget The path name of the target.
2868 * x X-coordinate of the mouse relative to the
2869 * widget.
2870 * y Y-coordinate of the mouse relative to the
2871 * widget.
2872 * formats A list of data formats acceptable to both
2873 * the source and target.
2874 *
2875 * If the Tcl procedure returns "cancel", this indicates that the drop was
2876 * not accepted by the target and the reject symbol should be displayed.
2877 * Otherwise one of the following strings may be recognized:
2878 *
2879 * "cancel" Drop was canceled.
2880 * "copy" Source data has been successfully copied.
2881 * "link" Target has made a link to the data. It's
2882 * Ok for the source to remove it's association
2883 * with the data, but not to delete the data
2884 * itself.
2885 * "move" Source data has been successfully copied,
2886 * it's Ok for the source to delete its
2887 * association with the data and the data itself.
2888 *
2889 * The result is relayed back to the source via another client message.
2890 * The source may or may not be waiting for the result.
2891 *
2892 * Results:
2893 * None.
2894 *
2895 * Side Effects:
2896 * A Tcl procedure is invoked in the target to handle the drop event.
2897 * The result of the drop is sent (via another ClientMessage) to the
2898 * source.
2899 *
2900 * ------------------------------------------------------------------------
2901 */
2902 static void
2903 HandleDropEvent(dndPtr, eventPtr)
2904 Dnd *dndPtr; /* Target where the drop event occurred. */
2905 XEvent *eventPtr; /* Message sent from the drag source. */
2906 {
2907 int button, keyState;
2908 int x, y;
2909 char *formats;
2910 int result;
2911 struct DropInfo {
2912 int mesg; /* TS_DROP message. */
2913 Window window; /* Source window. */
2914 int timestamp; /* Transaction timestamp. */
2915 int point; /* Root X-Y coordinate of pointer. */
2916 int flags; /* Button/keystate information. */
2917 } *dropPtr;
2918 DropPending pending;
2919
2920 dropPtr = (struct DropInfo *)eventPtr->xclient.data.l;
2921 UNPACK(dropPtr->point, x, y);
2922 UNPACK(dropPtr->flags, button, keyState);
2923
2924 /* Set up temporary bookkeeping for the drop transaction */
2925 memset (&pending, 0, sizeof(pending));
2926 pending.window = dropPtr->window;
2927 pending.display = eventPtr->xclient.display;
2928 pending.timestamp = dropPtr->timestamp;
2929 pending.protocol = PROTO_BLT;
2930 pending.packetSize = GetMaxPropertySize(pending.display);
2931 Tcl_DStringInit(&(pending.dString));
2932
2933 formats = GetSourceFormats(dndPtr, dropPtr->window, dropPtr->timestamp);
2934
2935 dndPtr->pendingPtr = &pending;
2936 result = AcceptDrop(dndPtr, x, y, formats, button, keyState,
2937 dropPtr->timestamp);
2938 dndPtr->pendingPtr = NULL;
2939
2940 /* Target-to-Source: Drop result message. */
2941 SendClientMsg(dndPtr->display, dropPtr->window, dndPtr->dataPtr->mesgAtom,
2942 TS_DROP_RESULT, (int)Tk_WindowId(dndPtr->tkwin), dropPtr->timestamp,
2943 result, 0);
2944 FreeFormats(dndPtr);
2945 }
2946
2947 /*
2948 * ------------------------------------------------------------------------
2949 *
2950 * HandleDragEvent --
2951 *
2952 * Invokes one of 3 Tcl procedures to handle the target's side of
2953 * the drag operation. This routine is triggered via a ClientMessage
2954 * from the drag source indicating that the token as either entered,
2955 * moved, or left this target. The source sends messages only if
2956 * Tcl procedures on the target have been defined to watch the
2957 * events. The message_type field can be either
2958 *
2959 * ST_DRAG_ENTER The mouse has entered the target.
2960 * ST_DRAG_MOTION The mouse has moved within the target.
2961 * ST_DRAG_LEAVE The mouse has left the target.
2962 *
2963 * The data fields are as follows:
2964 * data.l[0] Message type.
2965 * data.l[1] Window Id of the source.
2966 * data.l[2] Timestamp of the drag&drop transaction.
2967 * data.l[3] Root X-Y coordinate of the pointer.
2968 * data.l[4] Button and key state information.
2969 *
2970 * For any of the 3 Tcl procedures, the following arguments
2971 * are passed:
2972 *
2973 * widget The path name of the target.
2974 * x X-coordinate of the mouse in the widget.
2975 * y Y-coordinate of the mouse in the widget.
2976 * formats A list of data formats acceptable to both
2977 * the source and target.
2978 *
2979 * If the Tcl procedure returns "cancel", this indicates that the drag
2980 * operation has been canceled and the reject symbol should be displayed.
2981 * Otherwise it should return a boolean:
2982 *
2983 * true Target will accept drop.
2984 * false Target will not accept the drop.
2985 *
2986 * The purpose of the Enter and Leave procedure is to allow the
2987 * target to provide visual feedback that the drop can occur or not.
2988 * The Motion procedure is for cases where the drop area is a smaller
2989 * area within the target, such as a canvas item on a canvas. The
2990 * procedure can determine (based upon the X-Y coordinates) whether
2991 * the pointer is over the canvas item and return a value accordingly.
2992 *
2993 * The result of the Tcl procedure is then relayed back to the
2994 * source by a ClientMessage.
2995 *
2996 * Results:
2997 * None.
2998 *
2999 * Side Effects:
3000 * A Tcl procedure is invoked in the target to handle the drag event.
3001 * The result of the drag is sent (via another ClientMessage) to the
3002 * source.
3003 *
3004 * ------------------------------------------------------------------------
3005 */
3006 static void
3007 HandleDragEvent(dndPtr, eventPtr)
3008 Dnd *dndPtr; /* Target where the drag event occurred. */
3009 XEvent *eventPtr; /* Message sent from the drag source. */
3010 {
3011 char **cmd;
3012 int resp;
3013 int x, y;
3014 int button, keyState;
3015 char *formats;
3016 struct DragInfo {
3017 int mesg; /* Drag-and-drop message type. */
3018 Window window; /* Source window. */
3019 int timestamp; /* Transaction timestamp. */
3020 int point; /* Root X-Y coordinate of pointer. */
3021 int flags; /* Button/keystate information. */
3022 } *dragPtr;
3023
3024 dragPtr = (struct DragInfo *)eventPtr->xclient.data.l;
3025
3026 cmd = NULL;
3027 switch (dragPtr->mesg) {
3028 case ST_DRAG_ENTER:
3029 cmd = dndPtr->enterCmd;
3030 break;
3031 case ST_DRAG_MOTION:
3032 cmd = dndPtr->motionCmd;
3033 break;
3034 case ST_DRAG_LEAVE:
3035 cmd = dndPtr->leaveCmd;
3036 break;
3037 }
3038 if (cmd == NULL) {
3039 return; /* Nothing to do. */
3040 }
3041 UNPACK(dragPtr->point, x, y);
3042 UNPACK(dragPtr->flags, button, keyState);
3043 formats = GetSourceFormats(dndPtr, dragPtr->window, dragPtr->timestamp);
3044 resp = InvokeCallback(dndPtr, cmd, x, y, formats, button, keyState,
3045 dragPtr->timestamp);
3046
3047 /* Target-to-Source: Drag result message. */
3048 SendClientMsg(dndPtr->display, dragPtr->window, dndPtr->dataPtr->mesgAtom,
3049 TS_DRAG_STATUS, (int)Tk_WindowId(dndPtr->tkwin), dragPtr->timestamp,
3050 resp, 0);
3051 }
3052
3053 /*
3054 * ------------------------------------------------------------------------
3055 *
3056 * DndEventProc --
3057 *
3058 * Invoked by Tk_HandleEvent whenever a DestroyNotify event is received
3059 * on a registered drag&drop source widget.
3060 *
3061 * ------------------------------------------------------------------------
3062 */
3063 static int
3064 DndEventProc(clientData, eventPtr)
3065 ClientData clientData; /* Drag&drop record. */
3066 XEvent *eventPtr; /* Event description. */
3067 {
3068 Dnd *dndPtr = clientData;
3069
3070 if (eventPtr->xany.window != Tk_WindowId(dndPtr->tkwin)) {
3071 return 0;
3072 }
3073 if (eventPtr->type == DestroyNotify) {
3074 dndPtr->tkwin = NULL;
3075 dndPtr->flags |= DND_DELETED;
3076 Tcl_EventuallyFree(dndPtr, DestroyDnd);
3077 return 0; /* Other handlers have to see this event too.*/
3078 } else if (eventPtr->type == ButtonPress) {
3079 dndPtr->keyState = eventPtr->xbutton.state;
3080 dndPtr->button = eventPtr->xbutton.button;
3081 return 0;
3082 } else if (eventPtr->type == ButtonRelease) {
3083 dndPtr->keyState = eventPtr->xbutton.state;
3084 dndPtr->button = eventPtr->xbutton.button;
3085 return 0;
3086 } else if (eventPtr->type == MotionNotify) {
3087 dndPtr->keyState = eventPtr->xmotion.state;
3088 return 0;
3089 } else if ((eventPtr->type == ClientMessage) &&
3090 (eventPtr->xclient.message_type == dndPtr->dataPtr->mesgAtom)) {
3091 int result;
3092
3093 switch((unsigned int)eventPtr->xclient.data.l[0]) {
3094 case TS_START_DROP:
3095 DoDrop(dndPtr, eventPtr);
3096 return 1;
3097
3098 case TS_DROP_RESULT:
3099 result = eventPtr->xclient.data.l[MESG_RESPONSE];
3100 dndPtr->tokenPtr->status = result;
3101 if (result == DROP_CANCEL) {
3102 CancelDrag(dndPtr);
3103 } else if (result == DROP_FAIL) {
3104 EventuallyRedrawToken(dndPtr);
3105 } else {
3106 dndPtr->tokenPtr->nSteps = 10;
3107 FadeToken(dndPtr);
3108 }
3109 if (dndPtr->resultCmd != NULL) {
3110 DropFinished(dndPtr, eventPtr);
3111 }
3112 return 1;
3113
3114 case TS_DRAG_STATUS:
3115 result = eventPtr->xclient.data.l[MESG_RESPONSE];
3116 ChangeToken(dndPtr, result);
3117 return 1;
3118
3119 case ST_DROP:
3120 HandleDropEvent(dndPtr, eventPtr);
3121 return 1;
3122
3123 case ST_DRAG_ENTER:
3124 case ST_DRAG_MOTION:
3125 case ST_DRAG_LEAVE:
3126 HandleDragEvent(dndPtr, eventPtr);
3127 return 1;
3128 }
3129 }
3130 return 0;
3131 }
3132
3133 static void
3134 SendPointerMessage(dndPtr, eventType, windowPtr, x, y)
3135 Dnd *dndPtr; /* Source drag&drop manager. */
3136 int eventType; /* Type of event to relay. */
3137 Winfo *windowPtr; /* Generic window information. */
3138 int x, y; /* Root coordinates of mouse. */
3139 {
3140 /* Source-to-Target: Pointer event messages. */
3141 SendClientMsg(
3142 dndPtr->display, /* Display of recipient window. */
3143 windowPtr->window, /* Recipient window. */
3144 dndPtr->dataPtr->mesgAtom, /* Message type. */
3145 eventType, /* Data 1 */
3146 (int)Tk_WindowId(dndPtr->tkwin), /* Data 2 */
3147 dndPtr->timestamp, /* Data 3 */
3148 PACK(x, y), /* Data 4 */
3149 PACK(dndPtr->button, dndPtr->keyState)); /* Data 5 */
3150 /* Don't wait the response. */
3151 }
3152
3153 static void
3154 RelayEnterEvent(dndPtr, windowPtr, x, y)
3155 Dnd *dndPtr;
3156 Winfo *windowPtr;
3157 int x, y;
3158 {
3159 if ((windowPtr != NULL) && (windowPtr->eventFlags & WATCH_ENTER)) {
3160 SendPointerMessage(dndPtr, ST_DRAG_ENTER, windowPtr, x, y);
3161 }
3162 }
3163
3164 static void
3165 RelayLeaveEvent(dndPtr, windowPtr, x, y)
3166 Dnd *dndPtr;
3167 Winfo *windowPtr;
3168 int x, y;
3169 {
3170 if ((windowPtr != NULL) && (windowPtr->eventFlags & WATCH_LEAVE)) {
3171 SendPointerMessage(dndPtr, ST_DRAG_LEAVE, windowPtr, x, y);
3172 }
3173 }
3174
3175 static void
3176 RelayMotionEvent(dndPtr, windowPtr, x, y)
3177 Dnd *dndPtr;
3178 Winfo *windowPtr;
3179 int x, y;
3180 {
3181 if ((windowPtr != NULL) && (windowPtr->eventFlags & WATCH_MOTION)) {
3182 SendPointerMessage(dndPtr, ST_DRAG_MOTION, windowPtr, x, y);
3183 }
3184 }
3185
3186 static void
3187 RelayDropEvent(dndPtr, windowPtr, x, y)
3188 Dnd *dndPtr;
3189 Winfo *windowPtr;
3190 int x, y;
3191 {
3192 SendPointerMessage(dndPtr, ST_DROP, windowPtr, x, y);
3193 }
3194
3195 /*
3196 * ------------------------------------------------------------------------
3197 *
3198 * FreeWinfo --
3199 *
3200 * ------------------------------------------------------------------------
3201 */
3202 static void
3203 FreeWinfo(windowPtr)
3204 Winfo *windowPtr; /* window rep to be freed */
3205 {
3206 Winfo *childPtr;
3207 Blt_ChainLink *linkPtr;
3208
3209 for (linkPtr = Blt_ChainFirstLink(windowPtr->chainPtr); linkPtr != NULL;
3210 linkPtr = Blt_ChainNextLink(linkPtr)) {
3211 childPtr = Blt_ChainGetValue(linkPtr);
3212 FreeWinfo(childPtr); /* Recursively free children. */
3213 }
3214 if (windowPtr->matches != NULL) {
3215 Blt_Free(windowPtr->matches);
3216 }
3217 Blt_ChainDestroy(windowPtr->chainPtr);
3218 Blt_Free(windowPtr);
3219 }
3220
3221 /*
3222 * ------------------------------------------------------------------------
3223 *
3224 * GetWinfo --
3225 *
3226 * Invoked during "drag" operations. Digs into the root window
3227 * hierarchy and caches the window-related information.
3228 * If the current point lies over an uninitialized window (i.e.
3229 * one that already has an allocated Winfo structure, but has
3230 * not been filled in yet), this routine is called to query
3231 * window coordinates. If the window has any children, more
3232 * uninitialized Winfo structures are allocated. Further queries
3233 * will cause these structures to be initialized in turn.
3234 *
3235 * ------------------------------------------------------------------------
3236 */
3237 static void
3238 GetWinfo(display, windowPtr)
3239 Display *display;
3240 Winfo *windowPtr; /* window rep to be initialized */
3241 {
3242 int visible;
3243
3244 if (windowPtr->initialized) {
3245 return;
3246 }
3247 /* Query for the window coordinates. */
3248 visible = GetWindowRegion(display, windowPtr);
3249 if (visible) {
3250 Blt_ChainLink *linkPtr;
3251 Blt_Chain *chainPtr;
3252 Winfo *childPtr;
3253
3254 /* Add offset from parent's origin to coordinates */
3255 if (windowPtr->parentPtr != NULL) {
3256 windowPtr->x1 += windowPtr->parentPtr->x1;
3257 windowPtr->y1 += windowPtr->parentPtr->y1;
3258 windowPtr->x2 += windowPtr->parentPtr->x1;
3259 windowPtr->y2 += windowPtr->parentPtr->y1;
3260 }
3261 /*
3262 * Collect a list of child windows, sorted in z-order. The
3263 * topmost window will be first in the list.
3264 */
3265 chainPtr = GetWindowZOrder(display, windowPtr->window);
3266
3267 /* Add and initialize extra slots if needed. */
3268 for (linkPtr = Blt_ChainFirstLink(chainPtr); linkPtr != NULL;
3269 linkPtr = Blt_ChainNextLink(linkPtr)) {
3270 childPtr = Blt_Calloc(1, sizeof(Winfo));
3271 assert(childPtr);
3272 childPtr->initialized = FALSE;
3273 childPtr->window = (Window)Blt_ChainGetValue(linkPtr);
3274 childPtr->parentPtr = windowPtr;
3275 Blt_ChainSetValue(linkPtr, childPtr);
3276 }
3277 windowPtr->chainPtr = chainPtr;
3278 } else {
3279 /* If it's not viewable don't bother doing anything else. */
3280 windowPtr->x1 = windowPtr->y1 = windowPtr->x2 = windowPtr->y2 = -1;
3281 windowPtr->chainPtr = NULL;
3282 }
3283 windowPtr->initialized = TRUE;
3284 }
3285
3286 /*
3287 * ------------------------------------------------------------------------
3288 *
3289 * InitRoot --
3290 *
3291 * Invoked at the start of a "drag" operation to capture the
3292 * positions of all windows on the current root. Queries the
3293 * entire window hierarchy and determines the placement of each
3294 * window. Queries the "BltDndTarget" property info where
3295 * appropriate. This information is used during the drag
3296 * operation to determine when the drag&drop token is over a
3297 * valid drag&drop target.
3298 *
3299 * Results:
3300 * Returns the record for the root window, which contains records
3301 * for all other windows as children.
3302 *
3303 * ------------------------------------------------------------------------
3304 */
3305 static Winfo *
3306 InitRoot(dndPtr)
3307 Dnd *dndPtr;
3308 {
3309 Winfo *rootPtr;
3310
3311 rootPtr = Blt_Calloc(1, sizeof(Winfo));
3312 assert(rootPtr);
3313 rootPtr->window = DefaultRootWindow(dndPtr->display);
3314 dndPtr->windowPtr = NULL;
3315 GetWinfo(dndPtr->display, rootPtr);
3316 return rootPtr;
3317 }
3318
3319
3320 static int
3321 ParseProperty(interp, dndPtr, windowPtr, data)
3322 Tcl_Interp *interp;
3323 Dnd *dndPtr;
3324 Winfo *windowPtr;
3325 char *data;
3326 {
3327 int nElems;
3328 char **elemArr;
3329 int eventFlags;
3330 Tcl_DString dString;
3331 int count;
3332 register int i;
3333
3334 if (Tcl_SplitList(interp, data, &nElems, &elemArr) != TCL_OK) {
3335 return TCL_ERROR; /* Malformed property list. */
3336 }
3337 if (nElems < 1) {
3338 Tcl_AppendResult(interp, "Malformed property \"", data, "\"",
3339 (char *)NULL);
3340 goto error;
3341 }
3342 if (Tcl_GetInt(interp, elemArr[PROP_WATCH_FLAGS], &eventFlags) != TCL_OK) {
3343 goto error;
3344 }
3345
3346 /* target flags, type1, type2, ... */
3347 /*
3348 * The target property contains a list of possible formats.
3349 * Compare this with what formats the source is willing to
3350 * convert and compress the list down to just the matching
3351 * formats. It's up to the target to request the specific
3352 * type (or types) that it wants.
3353 */
3354 count = 0;
3355 Tcl_DStringInit(&dString);
3356 if (dndPtr->reqFormats == NULL) {
3357 Blt_HashEntry *hPtr;
3358 Blt_HashSearch cursor;
3359 char *fmt;
3360
3361 for (i = 1; i < nElems; i++) {
3362 for(hPtr = Blt_FirstHashEntry(&(dndPtr->getDataTable), &cursor);
3363 hPtr != NULL; hPtr = Blt_NextHashEntry(&cursor)) {
3364 fmt = Blt_GetHashKey(&(dndPtr->getDataTable), hPtr);
3365 if ((*fmt == elemArr[i][0]) && (strcmp(fmt, elemArr[i]) == 0)) {
3366 Tcl_DStringAppendElement(&dString, elemArr[i]);
3367 count++;
3368 break;
3369 }
3370 }
3371 }
3372 } else {
3373 register char **s;
3374
3375 for (i = 1; i < nElems; i++) {
3376 for (s = dndPtr->reqFormats; *s != NULL; s++) {
3377 if ((**s == elemArr[i][0]) && (strcmp(*s, elemArr[i]) == 0)) {
3378 Tcl_DStringAppendElement(&dString, elemArr[i]);
3379 count++;
3380 }
3381 }
3382 }
3383 }
3384 if (count == 0) {
3385 #ifdef notdef
3386 fprintf(stderr, "source/target mismatch: No matching types\n");
3387 #endif
3388 return TCL_BREAK;
3389 }
3390 if (eventFlags != 0) {
3391 SetProperty(dndPtr->tkwin, dndPtr->dataPtr->formatsAtom,
3392 Tcl_DStringValue(&dString));
3393 windowPtr->matches = NULL;
3394 } else {
3395 windowPtr->matches = Blt_Strdup(Tcl_DStringValue(&dString));
3396 }
3397 Tcl_DStringFree(&dString);
3398 windowPtr->eventFlags = eventFlags;
3399 return TCL_OK;
3400 error:
3401 Blt_Free(elemArr);
3402 return TCL_ERROR;
3403 }
3404
3405 /*
3406 * ------------------------------------------------------------------------
3407 *
3408 * OverTarget --
3409 *
3410 * Checks to see if a compatible drag&drop target exists at the
3411 * given position. A target is "compatible" if it is a drag&drop
3412 * window, and if it has a handler that is compatible with the
3413 * current source window.
3414 *
3415 * Results:
3416 * Returns a pointer to a structure describing the target, or NULL
3417 * if no compatible target is found.
3418 *
3419 * ------------------------------------------------------------------------
3420 */
3421 static Winfo *
3422 OverTarget(dndPtr)
3423 Dnd *dndPtr; /* drag&drop source window */
3424 {
3425 Tcl_Interp *interp = dndPtr->interp;
3426 int x, y;
3427 int vx, vy;
3428 int dummy;
3429 Winfo *windowPtr;
3430
3431 /*
3432 * If no window info has been been gathered yet for this target,
3433 * then abort this call. This probably means that the token is
3434 * moved before it has been properly built.
3435 */
3436 if (dndPtr->rootPtr == NULL) {
3437 fprintf(stderr, "rootPtr not initialized\n");
3438 return NULL;
3439 }
3440 /* Adjust current location for virtual root windows. */
3441 Tk_GetVRootGeometry(dndPtr->tkwin, &vx, &vy, &dummy, &dummy);
3442 x = dndPtr->x + vx;
3443 y = dndPtr->y + vy;
3444
3445 windowPtr = FindTopWindow(dndPtr, x, y);
3446 if (windowPtr == NULL) {
3447 return NULL; /* Not over a window. */
3448 }
3449 if ((!dndPtr->selfTarget) &&
3450 (Tk_WindowId(dndPtr->tkwin) == windowPtr->window)) {
3451 return NULL; /* If the self-target flag isn't set,
3452 * don't allow the source window to
3453 * drop onto itself. */
3454 }
3455 if (!windowPtr->lookedForProperty) {
3456 char *data;
3457 int result;
3458
3459 windowPtr->lookedForProperty = TRUE;
3460 /* See if this window has a "BltDndTarget" property. */
3461 data = GetProperty(dndPtr->display, windowPtr->window,
3462 dndPtr->dataPtr->targetAtom);
3463 if (data == NULL) {
3464 #ifdef notdef
3465 fprintf(stderr, "No property on 0x%x\n", windowPtr->window);
3466 #endif
3467 return NULL; /* No such property on window. */
3468 }
3469 result = ParseProperty(interp, dndPtr, windowPtr, data);
3470 XFree(data);
3471 if (result == TCL_BREAK) {
3472 #ifdef notdef
3473 fprintf(stderr, "No matching formats\n");
3474 #endif
3475 return NULL;
3476 }
3477 if (result != TCL_OK) {
3478 Tcl_BackgroundError(interp);
3479 return NULL; /* Malformed property list. */
3480 }
3481 windowPtr->isTarget = TRUE;
3482 }
3483 if (!windowPtr->isTarget) {
3484 return NULL;
3485 }
3486 return windowPtr;
3487
3488 }
3489
3490 /*
3491 * ------------------------------------------------------------------------
3492 *
3493 * AddTargetProperty --
3494 *
3495 * Attaches a drag&drop property to the given target window.
3496 * This property allows us to recognize the window later as a
3497 * valid target. It also stores important information including
3498 * the interpreter managing the target and the pathname of the
3499 * target window. Usually this routine is called when the target
3500 * is first registered or first exposed (so that the X-window
3501 * really exists).
3502 *
3503 * ------------------------------------------------------------------------
3504 */
3505 static void
3506 AddTargetProperty(dndPtr)
3507 Dnd *dndPtr; /* drag&drop target window data */
3508 {
3509 Tcl_DString dString;
3510 Blt_HashEntry *hPtr;
3511 unsigned int eventFlags;
3512 Blt_HashSearch cursor;
3513 char *fmt;
3514 char string[200];
3515
3516 Tcl_DStringInit(&dString);
3517 /*
3518 * Each target window's dnd property contains
3519 *
3520 * 1. Mouse event flags.
3521 * 2. List of all the data types that can be handled. If none
3522 * are listed, then all can be handled.
3523 */
3524 eventFlags = 0;
3525 if (dndPtr->enterCmd != NULL) {
3526 eventFlags |= WATCH_ENTER;
3527 }
3528 if (dndPtr->leaveCmd != NULL) {
3529 eventFlags |= WATCH_LEAVE;
3530 }
3531 if (dndPtr->motionCmd != NULL) {
3532 eventFlags |= WATCH_MOTION;
3533 }
3534 sprintf(string, "0x%x", eventFlags);
3535 Tcl_DStringAppendElement(&dString, string);
3536 for (hPtr = Blt_FirstHashEntry(&(dndPtr->setDataTable), &cursor);
3537 hPtr != NULL; hPtr = Blt_NextHashEntry(&cursor)) {
3538 fmt = Blt_GetHashKey(&(dndPtr->setDataTable), hPtr);
3539 Tcl_DStringAppendElement(&dString, fmt);
3540 }
3541 SetProperty(dndPtr->tkwin, dndPtr->dataPtr->targetAtom,
3542 Tcl_DStringValue(&dString));
3543 dndPtr->targetPropertyExists = TRUE;
3544 Tcl_DStringFree(&dString);
3545 }
3546
3547 static void
3548 CancelDrag(dndPtr)
3549 Dnd *dndPtr;
3550 {
3551 if (dndPtr->flags & DND_INITIATED) {
3552 dndPtr->tokenPtr->nSteps = 10;
3553 SnapToken(dndPtr);
3554 StopActiveCursor(dndPtr);
3555 if (dndPtr->cursor == None) {
3556 Tk_UndefineCursor(dndPtr->tkwin);
3557 } else {
3558 Tk_DefineCursor(dndPtr->tkwin, dndPtr->cursor);
3559 }
3560 }
3561 if (dndPtr->rootPtr != NULL) {
3562 FreeWinfo(dndPtr->rootPtr);
3563 dndPtr->rootPtr = NULL;
3564 }
3565 }
3566
3567 static int
3568 DragInit(dndPtr, x, y)
3569 Dnd *dndPtr;
3570 int x, y;
3571 {
3572 Token *tokenPtr = dndPtr->tokenPtr;
3573 int result;
3574 Winfo *newPtr;
3575
3576 assert((dndPtr->flags & DND_ACTIVE) == DND_SELECTED);
3577
3578 if (dndPtr->rootPtr != NULL) {
3579 FreeWinfo(dndPtr->rootPtr);
3580 }
3581 dndPtr->rootPtr = InitRoot(dndPtr); /* Reset information cache. */
3582 dndPtr->flags &= ~DND_VOIDED;
3583
3584 dndPtr->x = x; /* Save current location. */
3585 dndPtr->y = y;
3586 result = TRUE;
3587 Tcl_Preserve(dndPtr);
3588 if (dndPtr->packageCmd != NULL) {
3589 Tcl_DString dString, savedResult;
3590 Tcl_Interp *interp = dndPtr->interp;
3591 int status;
3592 char **p;
3593 int rx, ry;
3594
3595 Tcl_DStringInit(&dString);
3596 for (p = dndPtr->packageCmd; *p != NULL; p++) {
3597 Tcl_DStringAppendElement(&dString, *p);
3598 }
3599 Tcl_DStringAppendElement(&dString, Tk_PathName(dndPtr->tkwin));
3600 rx = dndPtr->dragX - Blt_RootX(dndPtr->tkwin);
3601 ry = dndPtr->dragY - Blt_RootY(dndPtr->tkwin);
3602 Tcl_DStringAppendElement(&dString, "x");
3603 Tcl_DStringAppendElement(&dString, Blt_Itoa(rx));
3604 Tcl_DStringAppendElement(&dString, "y");
3605 Tcl_DStringAppendElement(&dString, Blt_Itoa(ry));
3606 Tcl_DStringAppendElement(&dString, "button");
3607 Tcl_DStringAppendElement(&dString, Blt_Itoa(dndPtr->button));
3608 Tcl_DStringAppendElement(&dString, "state");
3609 Tcl_DStringAppendElement(&dString, Blt_Itoa(dndPtr->keyState));
3610 Tcl_DStringAppendElement(&dString, "timestamp");
3611 Tcl_DStringAppendElement(&dString, Blt_Utoa(dndPtr->timestamp));
3612 Tcl_DStringAppendElement(&dString, "token");
3613 Tcl_DStringAppendElement(&dString, Tk_PathName(tokenPtr->tkwin));
3614
3615 Tcl_DStringInit(&savedResult);
3616 Tcl_DStringGetResult(interp, &savedResult);
3617 dndPtr->flags |= DND_IN_PACKAGE;
3618 status = Tcl_GlobalEval(interp, Tcl_DStringValue(&dString));
3619 dndPtr->flags &= ~DND_IN_PACKAGE;
3620 if (status == TCL_OK) {
3621 result = GetDragResult(interp, Tcl_GetStringResult(interp));
3622 } else {
3623 Tcl_BackgroundError(interp);
3624 }
3625 Tcl_DStringFree(&dString);
3626 Tcl_DStringResult(interp, &savedResult);
3627 Tcl_DStringFree(&dString);
3628 if (status != TCL_OK) {
3629 HideToken(dndPtr);
3630 Tcl_Release(dndPtr);
3631 return TCL_ERROR;
3632 }
3633 }
3634 if (dndPtr->flags & DND_VOIDED) {
3635 HideToken(dndPtr);
3636 Tcl_Release(dndPtr);
3637 return TCL_RETURN;
3638 }
3639 if ((!result) || (dndPtr->flags & DND_DELETED)) {
3640 HideToken(dndPtr);
3641 Tcl_Release(dndPtr);
3642 return TCL_RETURN;
3643 }
3644 Tcl_Release(dndPtr);
3645
3646 if (dndPtr->cursor != None) {
3647 Tk_Cursor cursor;
3648
3649 /* Save the old cursor */
3650 cursor = GetWidgetCursor(dndPtr->interp, dndPtr->tkwin);
3651 if (dndPtr->cursor != None) {
3652 Tk_FreeCursor(dndPtr->display, dndPtr->cursor);
3653 }
3654 dndPtr->cursor = cursor;
3655 if (dndPtr->cursors != NULL) {
3656 /* Temporarily install the drag-and-drop cursor. */
3657 Tk_DefineCursor(dndPtr->tkwin, dndPtr->cursors[0]);
3658 }
3659 }
3660 if (Tk_WindowId(tokenPtr->tkwin) == None) {
3661 Tk_MakeWindowExist(tokenPtr->tkwin);
3662 }
3663 if (!Tk_IsMapped(tokenPtr->tkwin)) {
3664 Tk_MapWindow(tokenPtr->tkwin);
3665 }
3666 dndPtr->flags |= DND_INITIATED;
3667 newPtr = OverTarget(dndPtr);
3668 RelayEnterEvent(dndPtr, newPtr, x, y);
3669 dndPtr->windowPtr = newPtr;
3670 tokenPtr->status = (newPtr != NULL) ? DROP_OK : DROP_CONTINUE;
3671 if (tokenPtr->lastStatus != tokenPtr->status) {
3672 EventuallyRedrawToken(dndPtr);
3673 }
3674 MoveToken(dndPtr); /* Move token to current drag point. */
3675 RaiseToken(dndPtr);
3676 return TCL_OK;
3677 }
3678
3679 /*
3680 * ------------------------------------------------------------------------
3681 *
3682 * CancelOp --
3683 *
3684 * Cancels the current drag&drop operation for the source. Calling
3685 * this operation does not affect the transfer of data from the
3686 * source to the target, once the drop has been made. From the
3687 * source's point of view, the drag&drop operation is already over.
3688 *
3689 * Example: dnd cancel .widget
3690 *
3691 * Results:
3692 * A standard Tcl result.
3693 *
3694 * Side Effects:
3695 * Hides the token and sets a flag indicating that further "drag"
3696 * and "drop" operations should be ignored.
3697 *
3698 * ------------------------------------------------------------------------
3699 */
3700 /*ARGSUSED*/
3701 static int
3702 CancelOp(clientData, interp, argc, argv)
3703 ClientData clientData; /* Thread-specific data. */
3704 Tcl_Interp *interp;
3705 int argc; /* Not used. */
3706 char **argv;
3707 {
3708 Dnd *dndPtr;
3709
3710 if (GetDnd(clientData, interp, argv[2], &dndPtr) != TCL_OK) {
3711 return TCL_ERROR;
3712 }
3713 if (!dndPtr->isSource) {
3714 Tcl_AppendResult(interp, "widget \"", Tk_PathName(dndPtr->tkwin),
3715 "\" is not a registered drag&drop source.", (char *)NULL);
3716 return TCL_ERROR;
3717 }
3718 /* Send the target a Leave message so it can change back. */
3719 RelayLeaveEvent(dndPtr, dndPtr->windowPtr, 0, 0);
3720 CancelDrag(dndPtr);
3721 return TCL_OK;
3722 }
3723
3724 /*
3725 * ------------------------------------------------------------------------
3726 *
3727 * CgetOp --
3728 *
3729 * Called to process an (argc,argv) list to configure (or
3730 * reconfigure) a drag&drop widget.
3731 *
3732 * ------------------------------------------------------------------------
3733 */
3734 /* ARGSUSED*/
3735 static int
3736 CgetOp(clientData, interp, argc, argv)
3737 ClientData clientData; /* Thread-specific data. */
3738 Tcl_Interp *interp;
3739 int argc; /* Not used. */
3740 char **argv;
3741 {
3742 Dnd *dndPtr;
3743
3744 if (GetDnd(clientData, interp, argv[2], &dndPtr) != TCL_OK) {
3745 return TCL_ERROR;
3746 }
3747 return Tk_ConfigureValue(interp, dndPtr->tkwin, configSpecs, (char *)dndPtr,
3748 argv[3], 0);
3749 }
3750
3751 /*
3752 * ------------------------------------------------------------------------
3753 *
3754 * ConfigureOp --
3755 *
3756 * Called to process an (argc,argv) list to configure (or
3757 * reconfigure) a drag&drop widget.
3758 *
3759 * ------------------------------------------------------------------------
3760 */
3761 static int
3762 ConfigureOp(clientData, interp, argc, argv)
3763 ClientData clientData; /* Thread-specific data. */
3764 Tcl_Interp *interp; /* current interpreter */
3765 int argc; /* number of arguments */
•3766 CONST char **argv; /* argument strings */
3767 {
3768 Dnd *dndPtr;
3769 int flags;
3770
•3771 if (GetDnd(clientData, interp, (char *)argv[2], &dndPtr) != TCL_OK) {
3772 return TCL_ERROR;
3773 }
3774 flags = TK_CONFIG_ARGV_ONLY;
3775 if (argc == 3) {
3776 return Tk_ConfigureInfo(interp, dndPtr->tkwin, configSpecs,
3777 (char *)dndPtr, (char *)NULL, flags);
3778 } else if (argc == 4) {
3779 return Tk_ConfigureInfo(interp, dndPtr->tkwin, configSpecs,
3780 (char *)dndPtr, argv[3], flags);
3781 }
•3782 if (Blt_ConfigureWidget(interp, dndPtr->tkwin, configSpecs, argc - 3,
3783 argv + 3, (char *)dndPtr, flags) != TCL_OK) {
3784 return TCL_ERROR;
3785 }
3786 if (ConfigureDnd(interp, dndPtr) != TCL_OK) {
3787 return TCL_ERROR;
3788 }
3789 return TCL_OK;
3790 }
3791
3792 /*
3793 * ------------------------------------------------------------------------
3794 *
3795 * DeleteOp --
3796 *
3797 * Deletes the drag&drop manager from the widget. If a "-source"
3798 * or "-target" switch is present, only that component of the
3799 * drag&drop manager is shutdown. The manager is not deleted
3800 * unless both the target and source components are shutdown.
3801 *
3802 * Example: dnd delete .widget
3803 *
3804 * Results:
3805 * A standard Tcl result.
3806 *
3807 * Side Effects:
3808 * Deletes the drag&drop manager. Also the source and target window
3809 * properties are removed from the widget.
3810 *
3811 * ------------------------------------------------------------------------
3812 */
3813 static int
3814 DeleteOp(clientData, interp, argc, argv)
3815 ClientData clientData; /* Thread-specific data. */
3816 Tcl_Interp *interp;
3817 int argc;
3818 char **argv;
3819 {
3820 Dnd *dndPtr;
3821 register int i;
3822
3823 for(i = 3; i < argc; i++) {
3824 if (GetDnd(clientData, interp, argv[i], &dndPtr) != TCL_OK) {
3825 return TCL_ERROR;
3826 }
3827 dndPtr->flags |= DND_DELETED;
3828 Tcl_EventuallyFree(dndPtr, DestroyDnd);
3829 }
3830 return TCL_OK;
3831 }
3832
3833 /*
3834 * ------------------------------------------------------------------------
3835 *
3836 * SelectOp --
3837 *
3838 * Initializes a drag&drop transaction. Typically this operation
3839 * is called from a ButtonPress event on a source widget. The
3840 * window information cache is initialized, and the token is
3841 * initialized and displayed.
3842 *
3843 * Example: dnd pickup .widget x y
3844 *
3845 * Results:
3846 * A standard Tcl result.
3847 *
3848 * Side Effects:
3849 * The token is initialized and displayed. This may require invoking
3850 * a user-defined package command. The window information cache is
3851 * also initialized.
3852 *
3853 * ------------------------------------------------------------------------
3854 */
3855 /*ARGSUSED*/
3856 static int
3857 SelectOp(clientData, interp, argc, argv)
3858 ClientData clientData; /* Thread-specific data. */
3859 Tcl_Interp *interp;
3860 int argc; /* Not used. */
3861 char **argv;
3862 {
3863 Dnd *dndPtr;
3864 int x, y, timestamp;
3865 Token *tokenPtr;
3866
3867 if (GetDnd(clientData, interp, argv[2], &dndPtr) != TCL_OK) {
3868 return TCL_ERROR;
3869 }
3870 if (!dndPtr->isSource) {
3871 Tcl_AppendResult(interp, "widget \"", Tk_PathName(dndPtr->tkwin),
3872 "\" is not a registered drag&drop source.", (char *)NULL);
3873 return TCL_ERROR;
3874 }
3875 tokenPtr = dndPtr->tokenPtr;
3876 if (tokenPtr == NULL) {
3877 Tcl_AppendResult(interp, "no drag&drop token created for \"",
3878 argv[2], "\"", (char *)NULL);
3879 return TCL_ERROR;
3880 }
3881 if ((Tcl_GetInt(interp, argv[3], &x) != TCL_OK) ||
3882 (Tcl_GetInt(interp, argv[4], &y) != TCL_OK)) {
3883 return TCL_ERROR;
3884 }
3885 if (Tcl_GetInt(interp, argv[5], &timestamp) != TCL_OK) {
3886 return TCL_ERROR;
3887 }
3888 if (dndPtr->flags & (DND_IN_PACKAGE | DND_ACTIVE | DND_VOIDED)) {
3889 return TCL_OK;
3890 }
3891
3892 if (tokenPtr->timerToken != NULL) {
3893 HideToken(dndPtr); /* If the user selected again before the
3894 * token snap/melt has completed, first
3895 * disable the token timer callback. */
3896 }
3897 /* At this point, simply save the starting pointer location. */
3898 dndPtr->dragX = x, dndPtr->dragY = y;
3899 GetTokenPosition(dndPtr, x, y);
3900 tokenPtr->startX = tokenPtr->x;
3901 tokenPtr->startY = tokenPtr->y;
3902 dndPtr->timestamp = timestamp;
3903 dndPtr->flags |= DND_SELECTED;
3904
3905 if (dndPtr->dragStart == 0) {
3906 if (DragInit(dndPtr, x, y) == TCL_ERROR) {
3907 return TCL_ERROR;
3908 }
3909 }
3910 return TCL_OK;
3911 }
3912
3913 /*
3914 * ------------------------------------------------------------------------
3915 *
3916 * DragOp --
3917 *
3918 * Continues the drag&drop transaction. Typically this operation
3919 * is called from a button Motion event on a source widget. Pointer
3920 * event messages are possibly sent to the target, indicating Enter,
3921 * Leave, and Motion events.
3922 *
3923 * Example: dnd drag .widget x y
3924 *
3925 * Results:
3926 * A standard Tcl result.
3927 *
3928 * Side Effects:
3929 * Pointer events are relayed to the target (if the mouse is over
3930 * one).
3931 *
3932 * ------------------------------------------------------------------------
3933 */
3934 /*ARGSUSED*/
3935 static int
3936 DragOp(clientData, interp, argc, argv)
3937 ClientData clientData; /* Thread-specific data. */
3938 Tcl_Interp *interp;
3939 int argc; /* Not used. */
3940 char **argv;
3941 {
3942 Winfo *newPtr, *oldPtr;
3943 Dnd *dndPtr;
3944 int x, y;
3945
3946 if (GetDnd(clientData, interp, argv[2], &dndPtr) != TCL_OK) {
3947 return TCL_ERROR;
3948 }
3949 if (!dndPtr->isSource) {
3950 Tcl_AppendResult(interp, "widget \"", Tk_PathName(dndPtr->tkwin),
3951 "\" is not a registered drag&drop source.", (char *)NULL);
3952 return TCL_ERROR;
3953 }
3954 if (dndPtr->tokenPtr == NULL) {
3955 Tcl_AppendResult(interp, "no drag&drop token created for \"",
3956 argv[2], "\"", (char *)NULL);
3957 return TCL_ERROR;
3958 }
3959 if ((Tcl_GetInt(interp, argv[3], &x) != TCL_OK) ||
3960 (Tcl_GetInt(interp, argv[4], &y) != TCL_OK)) {
3961 return TCL_ERROR;
3962 }
3963
3964 if ((dndPtr->flags & DND_SELECTED) == 0) {
3965 return TCL_OK; /* Re-entered this routine. */
3966 }
3967 /*
3968 * The following code gets tricky because the package command may
3969 * call "update" or "tkwait". A motion event may then trigger
3970 * this routine, before the token has been initialized. Until the
3971 * package command finishes, no target messages are sent and drops
3972 * are silently ignored. Note that we do still track mouse
3973 * movements, so that when the package command completes, it will
3974 * have the latest pointer position.
3975 */
3976 dndPtr->x = x; /* Save current location. */
3977 dndPtr->y = y;
3978
3979 if (dndPtr->flags & DND_IN_PACKAGE) {
3980 return TCL_OK; /* Re-entered this routine. */
3981 }
3982 if ((dndPtr->flags & DND_INITIATED) == 0) {
3983 int dx, dy;
3984 int result;
3985
3986 dx = dndPtr->dragX - x;
3987 dy = dndPtr->dragY - y;
3988 if ((ABS(dx) < dndPtr->dragStart) && (ABS(dy) < dndPtr->dragStart)) {
3989 return TCL_OK;
3990 }
3991 result = DragInit(dndPtr, x, y);
3992 if (result == TCL_ERROR) {
3993 return TCL_ERROR;
3994 }
3995 if (result == TCL_RETURN) {
3996 return TCL_OK;
3997 }
3998 }
3999 if (dndPtr->flags & DND_VOIDED) {
4000 return TCL_OK;
4001 }
4002 oldPtr = dndPtr->windowPtr;
4003 newPtr = OverTarget(dndPtr);
4004 if (newPtr == oldPtr) {
4005 RelayMotionEvent(dndPtr, oldPtr, x, y);
4006 dndPtr->windowPtr = oldPtr;
4007 } else {
4008 RelayLeaveEvent(dndPtr, oldPtr, x, y);
4009 RelayEnterEvent(dndPtr, newPtr, x, y);
4010 dndPtr->windowPtr = newPtr;
4011 }
4012 dndPtr->tokenPtr->status = (newPtr != NULL) ? DROP_OK : DROP_CONTINUE;
4013 if (dndPtr->tokenPtr->lastStatus != dndPtr->tokenPtr->status) {
4014 EventuallyRedrawToken(dndPtr);
4015 }
4016 MoveToken(dndPtr); /* Move token to current drag point. */
4017 RaiseToken(dndPtr);
4018 return TCL_OK;
4019 }
4020
4021
4022 /*
4023 * ------------------------------------------------------------------------
4024 *
4025 * DropOp --
4026 *
4027 * Finishes the drag&drop transaction by dropping the data on the
4028 * selected target. Typically this operation is called from a
4029 * ButtonRelease event on a source widget. Note that a Leave message
4030 * is always sent to the target so that is can un-highlight itself.
4031 * The token is hidden and a drop message is sent to the target.
4032 *
4033 * Example: dnd drop .widget x y
4034 *
4035 * Results:
4036 * A standard Tcl result.
4037 *
4038 * Side Effects:
4039 * The token is hidden and a drop message is sent to the target.
4040 *
4041 * ------------------------------------------------------------------------
4042 */
4043 /*ARGSUSED*/
4044 static int
4045 DropOp(clientData, interp, argc, argv)
4046 ClientData clientData; /* Thread-specific data. */
4047 Tcl_Interp *interp;
4048 int argc; /* Not used. */
4049 char **argv;
4050 {
4051 Winfo *windowPtr;
4052 Dnd *dndPtr;
4053 int x, y;
4054
4055 if (GetDnd(clientData, interp, argv[2], &dndPtr) != TCL_OK) {
4056 return TCL_ERROR;
4057 }
4058 if (!dndPtr->isSource) {
4059 Tcl_AppendResult(interp, "widget \"", Tk_PathName(dndPtr->tkwin),
4060 "\" is not a registered drag&drop source.", (char *)NULL);
4061 return TCL_ERROR;
4062 }
4063 if ((Tcl_GetInt(interp, argv[3], &x) != TCL_OK) ||
4064 (Tcl_GetInt(interp, argv[4], &y) != TCL_OK)) {
4065 return TCL_ERROR;
4066 }
4067 dndPtr->x = x; /* Save drag&drop location */
4068 dndPtr->y = y;
4069 if ((dndPtr->flags & DND_INITIATED) == 0) {
4070 return TCL_OK; /* Never initiated any drag operation. */
4071 }
4072 if (dndPtr->flags & DND_VOIDED) {
4073 HideToken(dndPtr);
4074 return TCL_OK;
4075 }
4076 windowPtr = OverTarget(dndPtr);
4077 if (windowPtr != NULL) {
4078 if (windowPtr->matches != NULL) {
4079 SetProperty(dndPtr->tkwin, dndPtr->dataPtr->formatsAtom,
4080 windowPtr->matches);
4081 }
4082 MoveToken(dndPtr); /* Move token to current drag point. */
4083 RaiseToken(dndPtr);
4084 RelayDropEvent(dndPtr, windowPtr, x, y);
4085 #ifdef notdef
4086 tokenPtr->nSteps = 10;
4087 FadeToken(dndPtr);
4088 #endif
4089 } else {
4090 CancelDrag(dndPtr);
4091 }
4092 return TCL_OK;
4093 }
4094
4095 /*
4096 * ------------------------------------------------------------------------
4097 *
4098 * GetdataOp --
4099 *
4100 * Registers one or more data formats with a drag&drop source.
4101 * Each format has a Tcl command associated with it. This command
4102 * is automatically invoked whenever data is pulled from the source
4103 * to a target requesting the data in that particular format. The
4104 * purpose of the Tcl command is to get the data from in the
4105 * application. When the Tcl command is invoked, special percent
4106 * substitutions are made:
4107 *
4108 * %# Drag&drop transaction timestamp.
4109 * %W Source widget.
4110 *
4111 * If a converter (command) already exists for a format, it
4112 * overwrites the existing command.
4113 *
4114 * Example: dnd getdata .widget "color" { %W cget -bg }
4115 *
4116 * Results:
4117 * A standard Tcl result.
4118 *
4119 * ------------------------------------------------------------------------
4120 */
4121 static int
4122 GetdataOp(clientData, interp, argc, argv)
4123 ClientData clientData; /* Thread-specific data. */
4124 Tcl_Interp *interp;
4125 int argc;
4126 char **argv;
4127 {
4128 Dnd *dndPtr;
4129 Blt_HashEntry *hPtr;
4130 Blt_HashSearch cursor;
4131 int isNew, nElem;
4132 char **cmd;
4133 register int i;
4134
4135 if (GetDnd(clientData, interp, argv[2], &dndPtr) != TCL_OK) {
4136 return TCL_ERROR;
4137 }
4138 if (argc == 3) {
4139 /* Return list of source data formats */
4140 for (hPtr = Blt_FirstHashEntry(&(dndPtr->getDataTable), &cursor);
4141 hPtr != NULL; hPtr = Blt_NextHashEntry(&cursor)) {
4142 Tcl_AppendElement(interp,
4143 Blt_GetHashKey(&(dndPtr->getDataTable), hPtr));
4144 }
4145 return TCL_OK;
4146 }
4147
4148 if (argc == 4) {
4149 hPtr = Blt_FindHashEntry(&(dndPtr->getDataTable), argv[3]);
4150 if (hPtr == NULL) {
4151 Tcl_AppendResult(interp, "can't find handler for format \"",
4152 argv[3], "\" for source \"", Tk_PathName(dndPtr->tkwin), "\"",
4153 (char *)NULL);
4154 return TCL_ERROR;
4155 }
4156 cmd = (char **)Blt_GetHashValue(hPtr);
4157 if (cmd == NULL) {
4158 Tcl_SetResult(interp, "", TCL_STATIC);
4159 } else {
4160 Tcl_SetResult(interp, PrintList(cmd), TCL_DYNAMIC);
4161 }
4162 return TCL_OK;
4163 }
4164
4165 for (i = 3; i < argc; i += 2) {
4166 hPtr = Blt_CreateHashEntry(&(dndPtr->getDataTable), argv[i], &isNew);
4167 if (!isNew) {
4168 cmd = (char **)Blt_GetHashValue(hPtr);
4169 Blt_Free(cmd);
4170 }
4171 if (Tcl_SplitList(interp, argv[i + 1], &nElem, &cmd) != TCL_OK) {
4172 Blt_DeleteHashEntry(&(dndPtr->getDataTable), hPtr);
4173 return TCL_ERROR;
4174 }
4175 Blt_SetHashValue(hPtr, cmd);
4176 }
4177 return TCL_OK;
4178 }
4179
4180 /*
4181 * ------------------------------------------------------------------------
4182 *
4183 * NamesOp --
4184 *
4185 * Returns the names of all the drag&drop managers. If either
4186 * a "-source" or "-target" switch is present, only the names of
4187 * managers acting as sources or targets respectively are returned.
4188 * A pattern argument may also be given. Only those managers
4189 * matching the pattern are returned.
4190 *
4191 * Examples: dnd names
4192 * dnd names -source
4193 * dnd names -target
4194 * dnd names .*label
4195 * Results:
4196 * A standard Tcl result. A Tcl list of drag&drop manager
4197 * names is returned.
4198 *
4199 * ------------------------------------------------------------------------
4200 */
4201 static int
4202 NamesOp(clientData, interp, argc, argv)
4203 ClientData clientData; /* Thread-specific data. */
4204 Tcl_Interp *interp;
4205 int argc;
4206 char **argv;
4207 {
4208 DndInterpData *dataPtr = clientData;
4209 Blt_HashEntry *hPtr;
4210 Blt_HashSearch cursor;
4211 Dnd *dndPtr;
4212 int findSources, findTargets;
4213
4214 findSources = findTargets = TRUE;
4215 if (argc > 2) {
4216 if ((argv[2][0] == '-') && (strcmp(argv[2], "-source") == 0)) {
4217 findTargets = FALSE;
4218 argc--, argv++;
4219 } else if ((argv[2][0] == '-') && (strcmp(argv[2], "-target") == 0)) {
4220 findSources = FALSE;
4221 argc--, argv++;
4222 }
4223 }
4224 for (hPtr = Blt_FirstHashEntry(&(dataPtr->dndTable), &cursor);
4225 hPtr != NULL; hPtr = Blt_NextHashEntry(&cursor)) {
4226 dndPtr = (Dnd *)Blt_GetHashValue(hPtr);
4227 if ((argc > 3) &&
4228 (!Tcl_StringMatch(Tk_PathName(dndPtr->tkwin), argv[3]))) {
4229 continue;
4230 }
4231 if (((findSources) && (dndPtr->isSource)) ||
4232 ((findTargets) && (dndPtr->isTarget))) {
4233 Tcl_AppendElement(interp, Tk_PathName(dndPtr->tkwin));
4234 }
4235 }
4236 return TCL_OK;
4237 }
4238
4239 /*
4240 * ------------------------------------------------------------------------
4241 *
4242 * PullOp --
4243 *
4244 * Pulls the current data from the source in the given format.
4245 * application.
4246 *
4247 * dnd pull .widget format data
4248 *
4249 * Results:
4250 * A standard Tcl result.
4251 *
4252 * Side Effects:
4253 * Invokes the target's data converter to store the data.
4254 *
4255 * ------------------------------------------------------------------------
4256 */
4257 /*ARGSUSED*/
4258 static int
4259 PullOp(clientData, interp, argc, argv)
4260 ClientData clientData; /* Thread-specific data. */
4261 Tcl_Interp *interp;
4262 int argc; /* Not used. */
4263 char **argv;
4264 {
4265 Dnd *dndPtr; /* drag&drop source record */
4266 int result;
4267 char **formatCmd;
4268 Blt_HashEntry *hPtr;
4269
4270 if (GetDnd(clientData, interp, argv[2], &dndPtr) != TCL_OK) {
4271 return TCL_ERROR;
4272 }
4273 if (!dndPtr->isTarget) {
4274 Tcl_AppendResult(interp, "widget \"", Tk_PathName(dndPtr->tkwin),
4275 "\" is not a registered drag&drop target.", (char *)NULL);
4276 return TCL_ERROR;
4277 }
4278 hPtr = Blt_FindHashEntry(&(dndPtr->setDataTable), argv[3]);
4279 if (hPtr == NULL) {
4280 Tcl_AppendResult(interp, "can't find format \"", argv[3],
4281 "\" in target \"", Tk_PathName(dndPtr->tkwin), "\"", (char *)NULL);
4282 return TCL_ERROR;
4283 }
4284 formatCmd = (char **)Blt_GetHashValue(hPtr);
4285 if (dndPtr->pendingPtr == NULL) {
4286 Tcl_AppendResult(interp, "no drop in progress", (char *)NULL);
4287 return TCL_ERROR;
4288 }
4289
4290 CompleteDataTransaction(dndPtr, argv[3], dndPtr->pendingPtr);
4291 result = TCL_OK;
4292 if (Tcl_DStringLength(&(dndPtr->pendingPtr->dString)) > 0) {
4293 Tcl_DString dString, savedResult;
4294 char **p;
4295
4296 Tcl_DStringInit(&dString);
4297 for (p = formatCmd; *p != NULL; p++) {
4298 Tcl_DStringAppendElement(&dString, *p);
4299 }
4300 Tcl_DStringAppendElement(&dString, Tk_PathName(dndPtr->tkwin));
4301 Tcl_DStringAppendElement(&dString, "x");
4302 Tcl_DStringAppendElement(&dString, Blt_Itoa(dndPtr->dropX));
4303 Tcl_DStringAppendElement(&dString, "y");
4304 Tcl_DStringAppendElement(&dString, Blt_Itoa(dndPtr->dropY));
4305 Tcl_DStringAppendElement(&dString, "timestamp");
4306 Tcl_DStringAppendElement(&dString,
4307 Blt_Utoa(dndPtr->pendingPtr->timestamp));
4308 Tcl_DStringAppendElement(&dString, "format");
4309 Tcl_DStringAppendElement(&dString, argv[3]);
4310 Tcl_DStringAppendElement(&dString, "value");
4311 Tcl_DStringAppendElement(&dString,
4312 Tcl_DStringValue(&(dndPtr->pendingPtr->dString)));
4313 Tcl_DStringInit(&savedResult);
4314 Tcl_DStringGetResult(interp, &savedResult);
4315 if (Tcl_GlobalEval(interp, Tcl_DStringValue(&dString)) != TCL_OK) {
4316 Tcl_BackgroundError(interp);
4317 }
4318 Tcl_DStringResult(interp, &savedResult);
4319 Tcl_DStringFree(&dString);
4320 }
4321 return result;
4322 }
4323
4324 /*
4325 * ------------------------------------------------------------------------
4326 *
4327 * SetdataOp --
4328 *
4329 * Registers one or more data formats with a drag&drop target.
4330 * Each format has a Tcl command associated with it. This command
4331 * is automatically invoked whenever data arrives from a source
4332 * to be converted to that particular format. The purpose of the
4333 * command is to set the data somewhere in the application (either
4334 * using a Tcl command or variable). When the Tcl command is invoked,
4335 * special percent substitutions are made:
4336 *
4337 * %# Drag&drop transaction timestamp.
4338 * %W Target widget.
4339 * %v Data value transfered from the source to
4340 * be converted into the correct format.
4341 *
4342 * If a converter (command) already exists for a format, it
4343 * overwrites the existing command.
4344 *
4345 * Example: dnd setdata .widget color { . configure -bg %v }
4346 *
4347 * Results:
4348 * A standard Tcl result.
4349 *
4350 * ------------------------------------------------------------------------
4351 */
4352 static int
4353 SetdataOp(clientData, interp, argc, argv)
4354 ClientData clientData;
4355 Tcl_Interp *interp;
4356 int argc;
4357 char **argv;
4358 {
4359 Dnd *dndPtr;
4360 Blt_HashEntry *hPtr;
4361 Blt_HashSearch cursor;
4362 int isNew, nElem;
4363 char **cmd;
4364 int i;
4365
4366 if (GetDnd(clientData, interp, argv[2], &dndPtr) != TCL_OK) {
4367 return TCL_ERROR;
4368 }
4369 if (argc == 3) {
4370 /* Show target handler data formats */
4371 for (hPtr = Blt_FirstHashEntry(&(dndPtr->setDataTable), &cursor);
4372 hPtr != NULL; hPtr = Blt_NextHashEntry(&cursor)) {
4373 Tcl_AppendElement(interp,
4374 Blt_GetHashKey(&(dndPtr->setDataTable), hPtr));
4375 }
4376 return TCL_OK;
4377 }
4378 if (argc == 4) {
4379 hPtr = Blt_FindHashEntry(&(dndPtr->setDataTable), argv[3]);
4380 if (hPtr == NULL) {
4381 Tcl_AppendResult(interp, "can't find handler for format \"",
4382 argv[3], "\" for target \"", Tk_PathName(dndPtr->tkwin), "\"",
4383 (char *)NULL);
4384 return TCL_ERROR;
4385 }
4386 cmd = (char **)Blt_GetHashValue(hPtr);
4387 if (cmd == NULL) {
4388 Tcl_SetResult(interp, "", TCL_STATIC);
4389 } else {
4390 Tcl_SetResult(interp, PrintList(cmd), TCL_DYNAMIC);
4391 }
4392 return TCL_OK;
4393 }
4394 for (i = 3; i < argc; i += 2) {
4395 hPtr = Blt_CreateHashEntry(&(dndPtr->setDataTable), argv[i], &isNew);
4396 if (!isNew) {
4397 cmd = (char **)Blt_GetHashValue(hPtr);
4398 Blt_Free(cmd);
4399 }
4400 if (Tcl_SplitList(interp, argv[i + 1], &nElem, &cmd) != TCL_OK) {
4401 Blt_DeleteHashEntry(&(dndPtr->setDataTable), hPtr);
4402 return TCL_ERROR;
4403 }
4404 Blt_SetHashValue(hPtr, cmd);
4405 }
4406 AddTargetProperty(dndPtr);
4407 return TCL_OK;
4408 }
4409
4410 /*
4411 * ------------------------------------------------------------------------
4412 *
4413 * RegisterOp --
4414 *
4415 * dnd register .window
4416 * ------------------------------------------------------------------------
4417 */
4418 static int
4419 RegisterOp(clientData, interp, argc, argv)
4420 ClientData clientData;
4421 Tcl_Interp *interp;
4422 int argc;
•4423 CONST char **argv;
4424 {
4425 DndInterpData *dataPtr = clientData;
4426 Tk_Window tkwin;
4427 Blt_HashEntry *hPtr;
4428 Dnd *dndPtr;
4429 int isNew;
4430
4431 tkwin = Tk_NameToWindow(interp, argv[2], dataPtr->mainWindow);
4432 if (tkwin == NULL) {
4433 return TCL_ERROR;
4434 }
4435 hPtr = Blt_CreateHashEntry(&(dataPtr->dndTable), (char *)tkwin, &isNew);
4436 if (!isNew) {
4437 Tcl_AppendResult(interp, "\"", Tk_PathName(tkwin),
4438 "\" is already registered as a drag&drop manager", (char *)NULL);
4439 return TCL_ERROR;
4440 }
4441 dndPtr = CreateDnd(interp, tkwin);
4442 dndPtr->hashPtr = hPtr;
4443 dndPtr->dataPtr = dataPtr;
4444 Blt_SetHashValue(hPtr, dndPtr);
•4445 if (Blt_ConfigureWidget(interp, dndPtr->tkwin, configSpecs, argc - 3,
4446 argv + 3, (char *)dndPtr, 0) != TCL_OK) {
4447 return TCL_ERROR;
4448 }
4449 if (ConfigureDnd(interp, dndPtr) != TCL_OK) {
4450 return TCL_ERROR;
4451 }
4452 return TCL_OK;
4453 }
4454
4455 /*
4456 * ------------------------------------------------------------------------
4457 *
4458 * TokenWindowOp --
4459 *
4460 * ------------------------------------------------------------------------
4461 */
4462 static int
4463 TokenWindowOp(clientData, interp, argc, argv)
4464 ClientData clientData; /* Thread-specific data. */
4465 Tcl_Interp *interp;
4466 int argc;
•4467 CONST char **argv;
4468 {
4469 Dnd *dndPtr;
4470 int flags;
4471
•4472 if (GetDnd(clientData, interp, (char *)argv[3], &dndPtr) != TCL_OK) {
4473 return TCL_ERROR;
4474 }
4475 flags = 0;
4476 if (dndPtr->tokenPtr == NULL) {
4477 if (CreateToken(interp, dndPtr) != TCL_OK) {
4478 return TCL_ERROR;
4479 }
4480 } else {
4481 flags = TK_CONFIG_ARGV_ONLY;
4482 }
4483 if (ConfigureToken(interp, dndPtr, argc - 4, argv + 4, flags) != TCL_OK) {
4484 return TCL_ERROR;
4485 }
4486 Tcl_SetResult(interp, Tk_PathName(dndPtr->tokenPtr->tkwin), TCL_VOLATILE);
4487 return TCL_OK;
4488 }
4489
4490 /*
4491 * ------------------------------------------------------------------------
4492 *
4493 * TokenCgetOp --
4494 *
4495 * Called to process an (argc,argv) list to configure (or
4496 * reconfigure) a drag&drop widget.
4497 *
4498 * ------------------------------------------------------------------------
4499 */
4500 /* ARGSUSED*/
4501 static int
4502 TokenCgetOp(clientData, interp, argc, argv)
4503 ClientData clientData; /* Thread-specific data. */
4504 Tcl_Interp *interp;
4505 int argc; /* Not used. */
4506 char **argv;
4507 {
4508 Dnd *dndPtr;
4509
4510 if (GetDnd(clientData, interp, argv[3], &dndPtr) != TCL_OK) {
4511 return TCL_ERROR;
4512 }
4513 if (dndPtr->tokenPtr == NULL) {
4514 Tcl_AppendResult(interp, "no token created for \"", argv[3], "\"",
4515 (char *)NULL);
4516 return TCL_ERROR;
4517 }
4518 return Tk_ConfigureValue(interp, dndPtr->tokenPtr->tkwin, tokenConfigSpecs,
4519 (char *)dndPtr->tokenPtr, argv[4], TK_CONFIG_ARGV_ONLY);
4520 }
4521
4522 /*
4523 * ------------------------------------------------------------------------
4524 *
4525 * TokenConfigureOp --
4526 *
4527 * ------------------------------------------------------------------------
4528 */
4529 static int
4530 TokenConfigureOp(clientData, interp, argc, argv)
4531 ClientData clientData; /* Thread-specific data. */
4532 Tcl_Interp *interp;
4533 int argc;
•4534 CONST char **argv;
4535 {
4536 Token *tokenPtr;
4537 Dnd *dndPtr;
4538 int flags;
4539
•4540 if (GetDnd(clientData, interp, (char *)argv[3], &dndPtr) != TCL_OK) {
4541 return TCL_ERROR;
4542 }
4543 flags = TK_CONFIG_ARGV_ONLY;
4544 if (dndPtr->tokenPtr == NULL) {
4545 Tcl_AppendResult(interp, "no token created for \"", argv[3], "\"",
4546 (char *)NULL);
4547 return TCL_ERROR;
4548 }
4549 tokenPtr = dndPtr->tokenPtr;
4550 if (argc == 3) {
4551 return Tk_ConfigureInfo(interp, tokenPtr->tkwin, tokenConfigSpecs,
4552 (char *)tokenPtr, (char *)NULL, flags);
4553 } else if (argc == 4) {
4554 return Tk_ConfigureInfo(interp, tokenPtr->tkwin, tokenConfigSpecs,
4555 (char *)tokenPtr, argv[3], flags);
4556 }
4557 return ConfigureToken(interp, dndPtr, argc - 4, argv + 4, flags);
4558 }
4559
4560 static Blt_OpSpec tokenOps[] =
4561 {
4562 {"cget", 2, (Blt_Op)TokenCgetOp, 5, 5, "widget option",},
4563 {"configure", 2, (Blt_Op)TokenConfigureOp, 4, 0,
4564 "widget ?option value?...",},
4565 {"window", 5, (Blt_Op)TokenWindowOp, 4, 0,
4566 "widget ?option value?...",},
4567 };
4568
4569 static int nTokenOps = sizeof(tokenOps) / sizeof(Blt_OpSpec);
4570
4571 /*
4572 * ------------------------------------------------------------------------
4573 *
4574 * TokenOp --
4575 *
4576 * ------------------------------------------------------------------------
4577 */
4578 static int
4579 TokenOp(clientData, interp, argc, argv)
4580 ClientData clientData; /* Thread-specific data. */
4581 Tcl_Interp *interp;
4582 int argc;
4583 char **argv;
4584 {
4585 Blt_Op proc;
4586 int result;
4587
4588 proc = Blt_GetOp(interp, nTokenOps, tokenOps, BLT_OP_ARG2, argc, argv, 0);
4589 if (proc == NULL) {
4590 return TCL_ERROR;
4591 }
4592 result = (*proc) (clientData, interp, argc, argv);
4593 return result;
4594 }
4595
4596 static Blt_OpSpec dndOps[] =
4597 {
4598 {"cancel", 2, (Blt_Op)CancelOp, 3, 3, "widget",},
4599 {"cget", 2, (Blt_Op)CgetOp, 4, 4, "widget option",},
4600 {"configure", 4, (Blt_Op)ConfigureOp, 3, 0,
4601 "widget ?option value?...",},
4602 #ifdef notdef
4603 {"convert", 4, (Blt_Op)ConvertOp, 5, 5, "widget data format",},
4604 #endif
4605 {"delete", 2, (Blt_Op)DeleteOp, 3, 0,"?-source|-target? widget...",},
4606 {"drag", 3, (Blt_Op)DragOp, 3, 0, "widget x y ?token?",},
4607 {"drop", 3, (Blt_Op)DropOp, 3, 0, "widget x y ?token?",},
4608 {"getdata", 1, (Blt_Op)GetdataOp, 3, 0, "widget ?format command?",},
4609 {"names", 1, (Blt_Op)NamesOp, 2, 4, "?-source|-target? ?pattern?",},
4610 {"pull", 1, (Blt_Op)PullOp, 4, 4, "widget format",},
4611 {"register", 1, (Blt_Op)RegisterOp, 3, 0, "widget ?option value?...",},
4612 {"select", 3, (Blt_Op)SelectOp, 6, 6, "widget x y timestamp",},
4613 {"setdata", 3, (Blt_Op)SetdataOp, 3, 0, "widget ?format command?",},
4614 {"token", 5, (Blt_Op)TokenOp, 3, 0, "args...",},
4615 };
4616
4617 static int nDndOps = sizeof(dndOps) / sizeof(Blt_OpSpec);
4618
4619 /*
4620 * ------------------------------------------------------------------------
4621 *
4622 * DndCmd --
4623 *
4624 * Invoked by TCL whenever the user issues a drag&drop command.
4625 *
4626 * ------------------------------------------------------------------------
4627 */
4628 static int
4629 DndCmd(clientData, interp, argc, argv)
4630 ClientData clientData; /* Thread-specific data. */
4631 Tcl_Interp *interp; /* current interpreter */
4632 int argc; /* number of arguments */
4633 char **argv; /* argument strings */
4634 {
4635 Blt_Op proc;
4636 int result;
4637
4638 proc = Blt_GetOp(interp, nDndOps, dndOps, BLT_OP_ARG1, argc, argv, 0);
4639 if (proc == NULL) {
4640 return TCL_ERROR;
4641 }
4642 result = (*proc) (clientData, interp, argc, argv);
4643 return result;
4644 }
4645
4646 /*
4647 * -----------------------------------------------------------------------
4648 *
4649 * DndInterpDeleteProc --
4650 *
4651 * This is called when the interpreter hosting the "dnd" command is
4652 * destroyed.
4653 *
4654 * Results:
4655 * None.
4656 *
4657 * Side effects:
4658 * Destroys the hash table containing the drag&drop managers.
4659 *
4660 * ------------------------------------------------------------------------
4661 */
4662 /* ARGSUSED */
4663 static void
4664 DndInterpDeleteProc(clientData, interp)
4665 ClientData clientData; /* Thread-specific data. */
4666 Tcl_Interp *interp;
4667 {
4668 DndInterpData *dataPtr = clientData;
4669 Dnd *dndPtr;
4670 Blt_HashEntry *hPtr;
4671 Blt_HashSearch cursor;
4672
4673 for (hPtr = Blt_FirstHashEntry(&(dataPtr->dndTable), &cursor);
4674 hPtr != NULL; hPtr = Blt_NextHashEntry(&cursor)) {
4675 dndPtr = (Dnd *)Blt_GetHashValue(hPtr);
4676 dndPtr->hashPtr = NULL;
4677 DestroyDnd((DestroyData)dndPtr);
4678 }
4679 Blt_DeleteHashTable(&(dataPtr->dndTable));
4680 Tcl_DeleteAssocData(interp, DND_THREAD_KEY);
4681 Blt_Free(dataPtr);
4682 }
4683
4684 static DndInterpData *
4685 GetDndInterpData(interp)
4686 Tcl_Interp *interp;
4687 {
4688 DndInterpData *dataPtr;
4689 Tcl_InterpDeleteProc *proc;
4690
4691 dataPtr = (DndInterpData *)Tcl_GetAssocData(interp, DND_THREAD_KEY, &proc);
4692 if (dataPtr == NULL) {
4693 Display *display;
4694 Tk_Window tkwin;
4695
4696 dataPtr = Blt_Malloc(sizeof(DndInterpData));
4697 assert(dataPtr);
4698 tkwin = Tk_MainWindow(interp);
4699 display = Tk_Display(tkwin);
4700 dataPtr->mainWindow = tkwin;
4701 dataPtr->display = display;
4702 Tcl_SetAssocData(interp, DND_THREAD_KEY, DndInterpDeleteProc,
4703 dataPtr);
4704 Blt_InitHashTable(&(dataPtr->dndTable), BLT_ONE_WORD_KEYS);
4705 dataPtr->mesgAtom = XInternAtom(display, "BLT Dnd Message", False);
4706 dataPtr->targetAtom = XInternAtom(display, "BLT Dnd Target", False);
4707 dataPtr->formatsAtom = XInternAtom(display, "BLT Dnd Formats",False);
4708 dataPtr->commAtom = XInternAtom(display, "BLT Dnd CommData", False);
4709
4710 #ifdef HAVE_XDND
4711 dataPtr->XdndActionListAtom = XInternAtom(display, "XdndActionList",
4712 False);
4713 dataPtr->XdndAwareAtom = XInternAtom(display, "XdndAware", False);
4714 dataPtr->XdndEnterAtom = XInternAtom(display, "XdndEnter", False);
4715 dataPtr->XdndFinishedAtom = XInternAtom(display, "XdndFinished", False);
4716 dataPtr->XdndLeaveAtom = XInternAtom(display, "XdndLeave", False);
4717 dataPtr->XdndPositionAtom = XInternAtom(display, "XdndPosition", False);
4718 dataPtr->XdndSelectionAtom = XInternAtom(display, "XdndSelection",
4719 False);
4720 dataPtr->XdndStatusAtom = XInternAtom(display, "XdndStatus", False);
4721 dataPtr->XdndTypeListAtom = XInternAtom(display, "XdndTypeList", False);
4722 #endif /* HAVE_XDND */
4723 }
4724 return dataPtr;
4725 }
4726
4727 /*
4728 * ------------------------------------------------------------------------
4729 *
4730 * Blt_DndInit --
4731 *
4732 * Adds the drag&drop command to the given interpreter. Should
4733 * be invoked to properly install the command whenever a new
4734 * interpreter is created.
4735 *
4736 * ------------------------------------------------------------------------
4737 */
4738 int
4739 Blt_DndInit(interp)
4740 Tcl_Interp *interp; /* interpreter to be updated */
4741 {
4742 static Blt_CmdSpec cmdSpec =
4743 {
4744 "dnd", DndCmd
4745 };
4746 DndInterpData *dataPtr;
4747
4748 dataPtr = GetDndInterpData(interp);
4749 cmdSpec.clientData = dataPtr;
4750 if (Blt_InitCmd(interp, "blt", &cmdSpec) == NULL) {
4751 return TCL_ERROR;
4752 }
4753 return TCL_OK;
4754 }
4755
4756 #ifdef notdef
4757 /*
4758 * Registers bitmap outline of dragged data, used to indicate
4759 * what is being dragged by source. Bitmap is XOR-ed as cursor/token
4760 * is moved around the screen.
4761 */
4762 static void
4763 Blt_DndSetOutlineBitmap(tkwin, bitmap, x, y)
4764 Tk_Window tkwin;
4765 Pixmap bitmap;
4766 int x, y;
4767 {
4768
4769 }
4770 #endif
4771
4772 #ifdef HAVE_XDND
4773
4774 static void
4775 XDndFreeFormats(handlerPtr)
4776 XDndHandler *handlerPtr;
4777 {
4778 if (handlerPtr->formatArr != NULL) {
4779 char **p;
4780
4781 for (p = handlerPtr->formatArr; *p != NULL; p++) {
4782 XFree(*p);
4783 }
4784 Blt_Free(handlerPtr->formatArr);
4785 handlerPtr->formatArr = NULL;
4786 }
4787 }
4788
4789 static char **
4790 XDndGetFormats(handlerPtr, eventPtr)
4791 XDndHandler *handlerPtr;
4792 XEvent *eventPtr;
4793 {
4794 int flags;
4795 Window window;
4796 unsigned char *data;
4797 Atom typeAtom;
4798 Atom format;
4799 int nItems, bytesAfter;
4800 Atom *atomArr;
4801 char *nameArr[XDND_MAX_TYPES + 1];
4802 Display *display;
4803
4804 XDndFreeFormats(handlerPtr);
4805 display = eventPtr->xclient.display;
4806 window = eventPtr->xclient.data.l[0];
4807 flags = eventPtr->xclient.data.l[1];
4808 data = NULL;
4809 if (flags & 0x01) {
4810 result = XGetWindowProperty(
4811 display, /* Display of window. */
4812 window, /* Window holding the property. */
4813 handlerPtr->dataPtr->XdndTypeListAtom, /* Name of property. */
4814 0, /* Offset of data (for multiple reads). */
4815 XDND_MAX_TYPES, /* Maximum number of items to read. */
4816 False, /* If true, delete the property. */
4817 XA_ATOM, /* Desired type of property. */
4818 &typeAtom, /* (out) Actual type of the property. */
4819 &format, /* (out) Actual format of the property. */
4820 &nItems, /* (out) # of items in specified format. */
4821 &bytesAfter, /* (out) # of bytes remaining to be read. */
4822 (unsigned char **)&data);
4823 if ((result != Success) || (format != 32) || (typeAtom != XA_ATOM)) {
4824 if (data != NULL) {
4825 XFree((char *)data);
4826 return (char **)NULL;
4827 }
4828 }
4829 atomArr = (Atom *)data;
4830 nAtoms = nItems;
4831 } else {
4832 atomArr = &(eventPtr->xclient.data.l[2]);
4833 nAtoms = 3;
4834 }
4835 formatArr = Blt_Calloc(nAtoms + 1, sizeof(char *));
4836 for (i = 0; (i < nAtoms) && (atomArr[i] != None); i++) {
4837 formatArr[i] = XGetAtomName(display, atomArr[i]);
4838 }
4839 if (data != NULL) {
4840 XFree((char *)data);
4841 }
4842 handlerPtr->formatArr = formatArr;
4843 }
4844
4845 static char *
4846 GetMatchingFormats(dndPtr, formatArr)
4847 Dnd *dndPtr;
4848 char **formatArr;
4849 {
4850 int nMatches;
4851
4852 nMatches = 0;
4853 Tcl_DStringInit(&dString);
4854 for (p = formatArr; *p != NULL; p++) {
4855 for(hPtr = Blt_FirstHashEntry(&(dndPtr->setDataTable), &cursor);
4856 hPtr != NULL; hPtr = Blt_NextHashEntry(&cursor)) {
4857 fmt = Blt_GetHashKey(&(dndPtr->setDataTable), hPtr);
4858 if ((*fmt == **p) && (strcmp(fmt, *p) == 0)) {
4859 Tcl_DStringAppendElement(&dString, *p);
4860 nMatches++;
4861 break;
4862 }
4863 }
4864 }
4865 if (nMatches > 0) {
4866 char *string;
4867
4868 string = Blt_Strdup(Tcl_DStringValue(&dString));
4869 Tcl_DStringFree(&dString);
4870 return string;
4871 }
4872 return NULL;
4873 }
4874
4875 static void
4876 XDndPointerEvent(handlerPtr, eventPtr)
4877 XDndHandler *handlerPtr;
4878 XEvent *eventPtr;
4879 {
4880 Tk_Window tkwin;
4881 int flags;
4882 Atom action;
4883 Window window;
4884
4885 flags = 0;
4886 action = None;
4887 window = eventPtr->xclient.data.l[MESG_XDND_WINDOW];
4888 /*
4889 * If the XDND source has no formats specified, don't process any further.
4890 * Simply send a "no accept" action with the message.
4891 */
4892 if (handlerPtr->formatArr != NULL) {
4893 Dnd *newPtr, *oldPtr;
4894 int point;
4895 int button, keyState;
4896 int x, y;
4897 char *formats;
4898
4899 point = (int)eventPtr->xclient.data.l[MESG_XDND_POINT];
4900 UNPACK(point, x, y);
4901
4902 /*
4903 * See if the mouse pointer currently over a drop target. We first
4904 * determine what Tk window is under the mouse, and then check if
4905 * that window is registered as a drop target.
4906 */
4907 newPtr = NULL;
4908 tkwin = Tk_CoordsToWindow(x, y, handlerPtr->tkwin);
4909 if (tkwin != NULL) {
4910 Blt_HashEntry *hPtr;
4911
4912 hPtr = Blt_FindHashEntry(&(handlerPtr->dataPtr->dndTable),
4913 (char *)tkwin);
4914 if (hPtr != NULL) {
4915 newPtr = (Dnd *)Blt_GetHashValue(hPtr);
4916 if (!newPtr->isTarget) {
4917 newPtr = NULL; /* Not a DND target. */
4918 }
4919 formats = GetMatchingFormats(newPtr, handlerPtr->formatArr);
4920 if (formats == NULL) {
4921 newPtr = NULL; /* Source has no matching formats. */
4922 }
4923 }
4924 }
4925 button = keyState = 0;
4926 oldPtr = handlerPtr->dndPtr;
4927 resp = DROP_CANCEL;
4928 if (newPtr == oldPtr) {
4929 if ((oldPtr != NULL) && (oldPtr->motionCmd != NULL)) {
4930 resp = InvokeCallback(oldPtr, oldPtr->motionCmd, x, y, formats,
4931 button, keyState, dndPtr->timestamp);
4932 }
4933 } else {
4934 if ((oldPtr != NULL) && (oldPtr->leaveCmd != NULL)) {
4935 InvokeCallback(oldPtr, oldPtr->leaveCmd, x, y, formats, button,
4936 keyState, dndPtr->timestamp);
4937 }
4938 if ((newPtr != NULL) && (newPtr->enterCmd != NULL)) {
4939 resp = InvokeCallback(newPtr, newPtr->enterCmd, x, y, formats,
4940 button, keyState, dndPtr->timestamp);
4941 }
4942 handlerPtr->dndPtr = newPtr;
4943 /*
4944 * Save the current mouse position, since we get them from the
4945 * drop message.
4946 */
4947 newPtr->x = x;
4948 newPtr->y = y;
4949 }
4950 if (formats != NULL) {
4951 Blt_Free(formats);
4952 }
4953 flags = XDND_FLAGS_WANT_POSITION_MSGS;
4954 if (resp) {
4955 flags |= XDND_FLAGS_ACCEPT_DROP;
4956 action = handlerPtr->dataPtr->XdndActionCopyAtom;
4957 }
4958 }
4959 /* Target-to-Source: Drag result message. */
4960 SendClientMsg(handlerPtr->display, window,
4961 handlerPtr->dataPtr->XdndStatusAtom, handlerPtr->window,
4962 flags, 0, 0, action);
4963 }
4964
4965 static void
4966 XDndDropEvent(handlerPtr, eventPtr)
4967 XDndHandler *handlerPtr;
4968 XEvent *eventPtr;
4969 {
4970 Tk_Window tkwin;
4971 int flags;
4972 Atom action;
4973 Window window;
4974 int timestamp;
4975
4976 flags = 0;
4977 action = None;
4978 window = eventPtr->xclient.data.l[MESG_XDND_WINDOW];
4979 timestamp = eventPtr->xclient.data.l[MESG_XDND_TIMESTAMP];
4980
4981 /*
4982 * If no formats were specified for the XDND source or if the last
4983 * motion event did not place the mouse over a valid drop target,
4984 * don't process any further. Simply send a "no accept" action with
4985 * the message.
4986 */
4987 if ((handlerPtr->formatArr != NULL) && (handlerPtr->dndPtr != NULL)) {
4988 int button, keyState;
4989 Dnd *dndPtr = handlerPtr->dndPtr;
4990 DropPending pending;
4991 int resp;
4992
4993 button = keyState = 0; /* Protocol doesn't supply this information. */
4994
4995 /* Set up temporary bookkeeping for the drop transaction */
4996 memset (&pending, 0, sizeof(pending));
4997 pending.window = window;
4998 pending.display = eventPtr->xclient.display;
4999 pending.timestamp = timestamp;
5000 pending.protocol = PROTO_XDND;
5001 pending.packetSize = GetMaxPropertySize(pending.display);
5002 Tcl_DStringInit(&(pending.dString));
5003
5004 formats = GetMatchingFormats(handlerPtr->dndPtr, handlerPtr->formatArr);
5005 if (formats == NULL) {
5006 }
5007 dndPtr->pendingPtr = &pending;
5008 resp = AcceptDrop(dndPtr, dndPtr->x, dndPtr->y, formats, button,
5009 keyState, action, timestamp);
5010 dndPtr->pendingPtr = NULL;
5011 if (resp) {
5012 flags |= XDND_FLAGS_ACCEPT_DROP;
5013 action = handlerPtr->dataPtr->XdndActionCopyAtom;
5014 }
5015 }
5016 /* Target-to-Source: Drag result message. */
5017 SendClientMsg(handlerPtr->display, window,
5018 handlerPtr->dataPtr->XdndStatusAtom, handlerPtr->window,
5019 flags, 0, 0, action);
5020 }
5021
5022 /*
5023 * ------------------------------------------------------------------------
5024 *
5025 * XDndProtoEventProc --
5026 *
5027 * Invoked by Tk_HandleEvent whenever a DestroyNotify event is received
5028 * on a registered drag&drop source widget.
5029 *
5030 * ------------------------------------------------------------------------
5031 */
5032 static int
5033 XDndProtoEventProc(clientData, eventPtr)
5034 ClientData clientData; /* Drag&drop record. */
5035 XEvent *eventPtr; /* Event description. */
5036 {
5037 DndInterpData *dataPtr = clientData;
5038 Tk_Window tkwin;
5039 Blt_HashEntry *hPtr;
5040 XDndHandler *handlerPtr;
5041 int point;
5042 int x, y;
5043 Atom mesg;
5044
5045 if (eventPtr->type != ClientMessage) {
5046 return 0; /* Not a ClientMessage event. */
5047 }
5048 /* Was the recipient a registered toplevel window? */
5049 hPtr = Blt_FindHashEntry(&(dataPtr->handlerTable),
5050 (char *)eventPtr->xclient.window);
5051 if (hPtr == NULL) {
5052 return 0; /* No handler registered with window. */
5053 }
5054 handlerPtr = (XDndHandler *)Blt_GetHashValue(hPtr);
5055 mesg = eventPtr->xclient.message_type;
5056 if (mesg == dataPtr->XdndEnterAtom) {
5057 XDndGetFormats(handlerPtr, eventPtr);
5058 handlerPtr->dndPtr = NULL;
5059 } else if (mesg == dataPtr->XdndPositionAtom) {
5060 XDndPointerEvent(handlerPtr, eventPtr);
5061 } else if (mesg == dataPtr->XdndLeaveAtom) {
5062 XDndFreeFormats(handlerPtr); /* Free up any collected formats. */
5063 if (handlerPtr->dndPtr != NULL) {
5064 InvokeCallback(handlerPtr->dndPtr, handlerPtr->dndPtr->leaveCmd,
5065 -1, -1, NULL, 0, 0);
5066 /* Send leave event to drop target. */
5067 }
5068 } else if (mesg == dataPtr->XdndDropAtom) {
5069 XDndDropEvent(handlerPtr, eventPtr);
5070 } else {
5071 fprintf(stderr, "Unknown client message type = 0x%x\n", mesg);
5072 return 0; /* Unknown message type. */
5073 }
5074 return 1;
5075 }
5076
5077 static XDndHandler *
5078 XDndCreateHandler(dndPtr)
5079 Dnd *dndPtr;
5080 {
5081 Tk_Window tkwin;
5082 Window window;
5083 Blt_HashEntry *hPtr;
5084 int isNew;
5085 XDndHandler *handlerPtr;
5086
5087 /*
5088 * Find the containing toplevel of this window. See if an XDND
5089 * handler is already registered for it.
5090 */
5091 tkwin = Blt_GetToplevel(dndPtr->tkwin);
5092 window = Blt_GetRealWindowId(tkwin); /* Use the wrapper window as
5093 * the real toplevel window. */
5094 hPtr = Blt_CreateHashEntry(&(dataPtr->XDndHandlerTable), (char *)window,
5095 &isNew);
5096 if (!isNew) {
5097 handlerPtr = (XDndHandler *)Blt_GetHashEntry(hPtr);
5098 handlerPtr->refCount++;
5099 } else {
5100 handlerPtr = Blt_Malloc(sizeof(XDndHandler));
5101 handlerPtr->tkwin = tkwin;
5102 handlerPtr->dndPtr = NULL;
5103 handlerPtr->refCount = 1;
5104 handlerPtr->dataPtr = dataPtr;
5105 /* FIXME */
5106 SetProperty(window, dataPtr->XdndAwareAtom, "3");
5107 Blt_SetHashValue(hPtr, handlerPtr);
5108 }
5109 return handlerPtr;
5110 }
5111
5112 static void
5113 XDndDeleteHandler(dndPtr)
5114 Dnd *dndPtr;
5115 {
5116 Tk_Window tkwin;
5117 Window window;
5118 Blt_HashEntry *hPtr;
5119
5120 tkwin = Blt_GetToplevel(dndPtr->tkwin);
5121 window = Blt_GetRealWindowId(tkwin); /* Use the wrapper window as the real
5122 * toplevel window. */
5123 hPtr = Blt_FindHashEntry(&(dataPtr->XDndHandlerTable), (char *)window);
5124 if (hPtr != NULL) {
5125 XDndHandler *handlerPtr;
5126
5127 handlerPtr = (XDndHandler *)Blt_GetHashEntry(hPtr);
5128 handlerPtr->refCount--;
5129 if (handlerPtr->refCount == 0) {
5130 XDndFreeFormats(handlerPtr);
5131 XDeleteProperty(dndPtr->display, window,
5132 dndPtr->dataPtr->XdndAwareAtom);
5133 Blt_DeleteHashEntry(&(dataPtr->XDndHandlerTable), hPtr);
5134 Blt_Free(handlerPtr);
5135 }
5136 }
5137 }
5138
5139 #endif /* HAVE_XDND */
5140
5141 #endif /* NO_DRAGDROP */