Debian Codegraph @

generic/bltUnixDnd.c

raw symbols ◀ ▶
Source reconstructed at patch state 1 of 3 — not the installed (fully patched) source.
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, 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 static void
1463 MorphToken(dndPtr)
1464 Dnd *dndPtr; /* Drag-and-drop manager (source). */
1465 {
1466 Token *tokenPtr = dndPtr->tokenPtr;
1467
1468 if (tokenPtr->status == DROP_FAIL) {
1469 tokenPtr->nSteps = 1;
1470 return;
1471 }
1472 if (tokenPtr->nSteps == 1) {
1473 HideToken(dndPtr);
1474 dndPtr->flags &= ~(DND_ACTIVE | DND_VOIDED);
1475 return;
1476 }
1477 if (tokenPtr->timerToken != NULL) {
1478 Tcl_DeleteTimerHandler(tokenPtr->timerToken);
1479 }
1480 tokenPtr->timerToken = Tcl_CreateTimerHandler(10,
1481 (Tcl_TimerProc *)MorphToken, dndPtr);
1482 tokenPtr->nSteps--;
1483
1484 if (dndPtr->flags & DROP_CANCEL) {
1485 tokenPtr->nSteps--;
1486 tokenPtr->x -= (tokenPtr->x - tokenPtr->startX) / tokenPtr->nSteps;
1487 tokenPtr->y -= (tokenPtr->y - tokenPtr->startY) / tokenPtr->nSteps;
1488 if ((tokenPtr->x != Tk_X(tokenPtr->tkwin)) ||
1489 (tokenPtr->y != Tk_Y(tokenPtr->tkwin))) {
1490 Tk_MoveToplevelWindow(tokenPtr->tkwin, tokenPtr->x, tokenPtr->y);
1491 }
1492 } else {
1493 int w, h;
1494 int dx, dy;
1495 Window window;
1496
1497 w = Tk_ReqWidth(tokenPtr->tkwin) * tokenPtr->nSteps / 10;
1498 h = Tk_ReqHeight(tokenPtr->tkwin) * tokenPtr->nSteps / 10;
1499 if (w < 1) {
1500 w = 1;
1501 }
1502 if (h < 1) {
1503 h = 1;
1504 }
1505 dx = (Tk_ReqWidth(tokenPtr->tkwin) - w) / 2;
1506 dy = (Tk_ReqHeight(tokenPtr->tkwin) - h) / 2;
1507 window = Blt_GetRealWindowId(tokenPtr->tkwin);
1508 XMoveResizeWindow(dndPtr->display, window, tokenPtr->x + dx,
1509 tokenPtr->y + dy, (unsigned int)w, (unsigned int)h);
1510 tokenPtr->width = w, tokenPtr->height = h;
1511 }
1512 RaiseToken(dndPtr);
1513 }
1514
1515 static void
1516 GetTokenPosition(dndPtr, x, y)
1517 Dnd *dndPtr;
1518 int x, y;
1519 {
1520 Token *tokenPtr = dndPtr->tokenPtr;
1521 int maxX, maxY;
1522 int vx, vy, dummy;
1523 Screen *screenPtr;
1524
1525 /* Adjust current location for virtual root windows. */
1526 Tk_GetVRootGeometry(dndPtr->tkwin, &vx, &vy, &dummy, &dummy);
1527 x += vx - TOKEN_OFFSET;
1528 y += vy - TOKEN_OFFSET;
1529
1530 screenPtr = Tk_Screen(tokenPtr->tkwin);
1531 maxX = WidthOfScreen(screenPtr) - Tk_Width(tokenPtr->tkwin);
1532 maxY = HeightOfScreen(screenPtr) - Tk_Height(tokenPtr->tkwin);
1533 Blt_TranslateAnchor(x, y, Tk_Width(tokenPtr->tkwin),
1534 Tk_Height(tokenPtr->tkwin), tokenPtr->anchor, &x, &y);
1535 if (x > maxX) {
1536 x = maxX;
1537 } else if (x < 0) {
1538 x = 0;
1539 }
1540 if (y > maxY) {
1541 y = maxY;
1542 } else if (y < 0) {
1543 y = 0;
1544 }
1545 tokenPtr->x = x, tokenPtr->y = y;
1546 }
1547
1548 /*
1549 * ------------------------------------------------------------------------
1550 *
1551 * MoveToken --
1552 *
1553 * Invoked during "drag" operations to move a token window to its
1554 * current "drag" coordinate.
1555 *
1556 * ------------------------------------------------------------------------
1557 */
1558 static void
1559 MoveToken(dndPtr)
1560 Dnd *dndPtr; /* drag&drop source window data */
1561 {
1562 Token *tokenPtr = dndPtr->tokenPtr;
1563
1564 GetTokenPosition(dndPtr, dndPtr->x, dndPtr->y);
1565 if ((tokenPtr->x != Tk_X(tokenPtr->tkwin)) ||
1566 (tokenPtr->y != Tk_Y(tokenPtr->tkwin))) {
1567 Tk_MoveToplevelWindow(tokenPtr->tkwin, tokenPtr->x, tokenPtr->y);
1568 }
1569 }
1570
1571
1572 /*
1573 * ------------------------------------------------------------------------
1574 *
1575 * ChangeToken --
1576 *
1577 * Invoked when the event loop is idle to determine whether or not
1578 * the current drag&drop token position is over another drag&drop
1579 * target.
1580 *
1581 * ------------------------------------------------------------------------
1582 */
1583 static void
1584 ChangeToken(dndPtr, status)
1585 Dnd *dndPtr;
1586 int status;
1587 {
1588 Token *tokenPtr = dndPtr->tokenPtr;
1589
1590 tokenPtr->status = status;
1591 EventuallyRedrawToken(dndPtr);
1592
1593 /*
1594 * If the source has a site command, then invoke it to
1595 * modify the appearance of the token window. Pass any
1596 * errors onto the drag&drop error handler.
1597 */
1598 if (dndPtr->siteCmd) {
1599 Tcl_Interp *interp = dndPtr->interp;
1600 Tcl_DString dString, savedResult;
1601 char **p;
1602
1603 Tcl_DStringInit(&dString);
1604 for (p = dndPtr->siteCmd; *p != NULL; p++) {
1605 Tcl_DStringAppendElement(&dString, *p);
1606 }
1607 Tcl_DStringAppendElement(&dString, Tk_PathName(dndPtr->tkwin));
1608 Tcl_DStringAppendElement(&dString, "timestamp");
1609 Tcl_DStringAppendElement(&dString, Blt_Utoa(dndPtr->timestamp));
1610 Tcl_DStringAppendElement(&dString, "status");
1611 Tcl_DStringAppendElement(&dString, NameOfStatus(status));
1612 Tcl_DStringInit(&savedResult);
1613 Tcl_DStringGetResult(interp, &savedResult);
1614 if (Tcl_GlobalEval(interp, Tcl_DStringValue(&dString)) != TCL_OK) {
1615 Tcl_BackgroundError(interp);
1616 }
1617 Tcl_DStringFree(&dString);
1618 Tcl_DStringResult(interp, &savedResult);
1619 }
1620 }
1621
1622 /*
1623 * ------------------------------------------------------------------------
1624 *
1625 * DrawRejectSymbol --
1626 *
1627 * Draws a rejection mark on the current drag&drop token, and arranges
1628 * for the token to be unmapped after a small delay.
1629 *
1630 * ------------------------------------------------------------------------
1631 */
1632 static void
1633 DrawRejectSymbol(dndPtr)
1634 Dnd *dndPtr;
1635 {
1636 Token *tokenPtr = dndPtr->tokenPtr;
1637 int divisor = 6; /* controls size of rejection symbol */
1638 int w, h, lineWidth, x, y, margin;
1639
1640 margin = 2 * tokenPtr->borderWidth;
1641 w = Tk_Width(tokenPtr->tkwin) - 2 * margin;
1642 h = Tk_Height(tokenPtr->tkwin) - 2 * margin;
1643 lineWidth = (w < h) ? w / divisor : h / divisor;
1644 lineWidth = (lineWidth < 1) ? 1 : lineWidth;
1645
1646 w = h = lineWidth * (divisor - 1);
1647 x = (Tk_Width(tokenPtr->tkwin) - w) / 2;
1648 y = (Tk_Height(tokenPtr->tkwin) - h) / 2;
1649
1650 /*
1651 * Draw the rejection symbol background (\) on the token window...
1652 */
1653 XSetLineAttributes(Tk_Display(tokenPtr->tkwin), tokenPtr->outlineGC,
1654 lineWidth + 2, LineSolid, CapButt, JoinBevel);
1655
1656 XDrawArc(Tk_Display(tokenPtr->tkwin), Tk_WindowId(tokenPtr->tkwin),
1657 tokenPtr->outlineGC, x, y, w, h, 0, 23040);
1658
1659 XDrawLine(Tk_Display(tokenPtr->tkwin), Tk_WindowId(tokenPtr->tkwin),
1660 tokenPtr->outlineGC, x + lineWidth, y + lineWidth, x + w - lineWidth,
1661 y + h - lineWidth);
1662
1663 /*
1664 * Draw the rejection symbol foreground (\) on the token window...
1665 */
1666 XSetLineAttributes(Tk_Display(tokenPtr->tkwin), tokenPtr->fillGC,
1667 lineWidth, LineSolid, CapButt, JoinBevel);
1668
1669 XDrawArc(Tk_Display(tokenPtr->tkwin), Tk_WindowId(tokenPtr->tkwin),
1670 tokenPtr->fillGC, x, y, w, h, 0, 23040);
1671
1672 XDrawLine(Tk_Display(tokenPtr->tkwin), Tk_WindowId(tokenPtr->tkwin),
1673 tokenPtr->fillGC, x + lineWidth, y + lineWidth, x + w - lineWidth,
1674 y + h - lineWidth);
1675
1676 tokenPtr->status = DROP_FAIL;
1677 /*
1678 * Arrange for token window to disappear eventually.
1679 */
1680 if (tokenPtr->timerToken != NULL) {
1681 Tcl_DeleteTimerHandler(tokenPtr->timerToken);
1682 }
1683 tokenPtr->timerToken = Tcl_CreateTimerHandler(1000,
1684 (Tcl_TimerProc *)HideToken, dndPtr);
1685 RaiseToken(dndPtr);
1686 dndPtr->flags &= ~(DND_ACTIVE | DND_VOIDED);
1687 }
1688
1689 /*
1690 * ------------------------------------------------------------------------
1691 *
1692 * CreateToken --
1693 *
1694 * Looks for a Source record in the hash table for drag&drop source
1695 * widgets. Creates a new record if the widget name is not already
1696 * registered. Returns a pointer to the desired record.
1697 *
1698 * ------------------------------------------------------------------------
1699 */
1700 static void
1701 DestroyToken(data)
1702 DestroyData data;
1703 {
1704 Dnd *dndPtr = (Dnd *)data;
1705 Token *tokenPtr = dndPtr->tokenPtr;
1706
1707 dndPtr->tokenPtr = NULL;
1708 if (tokenPtr == NULL) {
1709 return;
1710 }
1711 if (tokenPtr->flags & TOKEN_REDRAW) {
1712 Tcl_CancelIdleCall(DisplayToken, dndPtr);
1713 }
1714 Tk_FreeOptions(tokenConfigSpecs, (char *)tokenPtr, dndPtr->display, 0);
1715 if (tokenPtr->timerToken) {
1716 Tcl_DeleteTimerHandler(tokenPtr->timerToken);
1717 }
1718 if (tokenPtr->fillGC != NULL) {
1719 Tk_FreeGC(dndPtr->display, tokenPtr->fillGC);
1720 }
1721 if (tokenPtr->outlineGC != NULL) {
1722 Tk_FreeGC(dndPtr->display, tokenPtr->outlineGC);
1723 }
1724 if (tokenPtr->tkwin != NULL) {
1725 Tk_DeleteEventHandler(tokenPtr->tkwin,
1726 ExposureMask | StructureNotifyMask, TokenEventProc, dndPtr);
1727 Tk_DestroyWindow(tokenPtr->tkwin);
1728 }
1729 Blt_Free(tokenPtr);
1730 }
1731
1732 /*
1733 * ------------------------------------------------------------------------
1734 *
1735 * TokenEventProc --
1736 *
1737 * Invoked by the Tk dispatcher to handle widget events.
1738 * Manages redraws for the drag&drop token window.
1739 *
1740 * ------------------------------------------------------------------------
1741 */
1742 static void
1743 TokenEventProc(clientData, eventPtr)
1744 ClientData clientData; /* data associated with widget */
1745 XEvent *eventPtr; /* information about event */
1746 {
1747 Dnd *dndPtr = clientData;
1748 Token *tokenPtr = dndPtr->tokenPtr;
1749
1750 if ((eventPtr->type == Expose) && (eventPtr->xexpose.count == 0)) {
1751 if (tokenPtr->tkwin != NULL) {
1752 EventuallyRedrawToken(dndPtr);
1753 }
1754 } else if (eventPtr->type == DestroyNotify) {
1755 tokenPtr->tkwin = NULL;
1756 if (tokenPtr->flags & TOKEN_REDRAW) {
1757 tokenPtr->flags &= ~TOKEN_REDRAW;
1758 Tcl_CancelIdleCall(DisplayToken, dndPtr);
1759 }
1760 Tcl_EventuallyFree(dndPtr, DestroyToken);
1761 }
1762 }
1763
1764 /*
1765 * ------------------------------------------------------------------------
1766 *
1767 * CreateToken --
1768 *
1769 * Looks for a Source record in the hash table for drag&drop source
1770 * widgets. Creates a new record if the widget name is not already
1771 * registered. Returns a pointer to the desired record.
1772 *
1773 * ------------------------------------------------------------------------
1774 */
1775 static int
1776 CreateToken(interp, dndPtr)
1777 Tcl_Interp *interp;
1778 Dnd *dndPtr;
1779 {
1780 XSetWindowAttributes attrs;
1781 Tk_Window tkwin;
1782 unsigned int mask;
1783 Token *tokenPtr;
1784
1785 tokenPtr = Blt_Calloc(1, sizeof(Token));
1786 assert(tokenPtr);
1787 tokenPtr->anchor = TK_ANCHOR_SE;
1788 tokenPtr->relief = TK_RELIEF_RAISED;
1789 tokenPtr->activeRelief = TK_RELIEF_SUNKEN;
1790 tokenPtr->borderWidth = tokenPtr->activeBorderWidth = 3;
1791
1792 /* Create toplevel on parent's screen. */
1793 tkwin = Tk_CreateWindow(interp, dndPtr->tkwin, "dndtoken", "");
1794 if (tkwin == NULL) {
1795 Blt_Free(tokenPtr);
1796 return TCL_ERROR;
1797 }
1798 tokenPtr->tkwin = tkwin;
1799 Tk_SetClass(tkwin, "DndToken");
1800 Tk_CreateEventHandler(tkwin, ExposureMask | StructureNotifyMask,
1801 TokenEventProc, dndPtr);
1802 attrs.override_redirect = True;
1803 attrs.backing_store = WhenMapped;
1804 attrs.save_under = True;
1805 mask = CWOverrideRedirect | CWSaveUnder | CWBackingStore;
1806 Tk_ChangeWindowAttributes(tkwin, mask, &attrs);
1807 Tk_SetInternalBorder(tkwin, tokenPtr->borderWidth + 2);
1808 Tk_MakeWindowExist(tkwin);
1809 dndPtr->tokenPtr = tokenPtr;
1810 return TCL_OK;
1811 }
1812
1813 /*
1814 * ------------------------------------------------------------------------
1815 *
1816 * ConfigureToken --
1817 *
1818 * Called to process an (argc,argv) list to configure (or
1819 * reconfigure) a drag&drop source widget.
1820 *
1821 * ------------------------------------------------------------------------
1822 */
1823 static int
1824 ConfigureToken(interp, dndPtr, argc, argv, flags)
1825 Tcl_Interp *interp; /* current interpreter */
1826 Dnd *dndPtr; /* Drag&drop source widget record */
1827 int argc; /* number of arguments */
1828 char **argv; /* argument strings */
1829 int flags; /* flags controlling interpretation */
1830 {
1831 GC newGC;
1832 Token *tokenPtr = dndPtr->tokenPtr;
1833 XGCValues gcValues;
1834 unsigned long gcMask;
1835
1836 Tk_MakeWindowExist(tokenPtr->tkwin);
1837 if (Blt_ConfigureWidget(interp, tokenPtr->tkwin, tokenConfigSpecs, argc,
1838 argv, (char *)tokenPtr, flags) != TCL_OK) {
1839 return TCL_ERROR;
1840 }
1841 /*
1842 * Set up the rejection outline GC for the token window...
1843 */
1844 gcValues.foreground = tokenPtr->outlineColor->pixel;
1845 gcValues.subwindow_mode = IncludeInferiors;
1846 gcValues.graphics_exposures = False;
1847 gcValues.line_style = LineSolid;
1848 gcValues.cap_style = CapButt;
1849 gcValues.join_style = JoinBevel;
1850
1851 gcMask = GCForeground | GCSubwindowMode | GCLineStyle |
1852 GCCapStyle | GCJoinStyle | GCGraphicsExposures;
1853 newGC = Tk_GetGC(dndPtr->tkwin, gcMask, &gcValues);
1854
1855 if (tokenPtr->outlineGC != NULL) {
1856 Tk_FreeGC(dndPtr->display, tokenPtr->outlineGC);
1857 }
1858 tokenPtr->outlineGC = newGC;
1859
1860 /*
1861 * Set up the rejection fill GC for the token window...
1862 */
1863 gcValues.foreground = tokenPtr->fillColor->pixel;
1864 if (tokenPtr->rejectStipple != None) {
1865 gcValues.stipple = tokenPtr->rejectStipple;
1866 gcValues.fill_style = FillStippled;
1867 gcMask |= GCStipple | GCFillStyle;
1868 }
1869 newGC = Tk_GetGC(dndPtr->tkwin, gcMask, &gcValues);
1870
1871 if (tokenPtr->fillGC != NULL) {
1872 Tk_FreeGC(dndPtr->display, tokenPtr->fillGC);
1873 }
1874 tokenPtr->fillGC = newGC;
1875
1876 if ((tokenPtr->reqWidth > 0) && (tokenPtr->reqHeight > 0)) {
1877 Tk_GeometryRequest(tokenPtr->tkwin, tokenPtr->reqWidth,
1878 tokenPtr->reqHeight);
1879 }
1880 /*
1881 * Reset the border width in case it has changed...
1882 */
1883 Tk_SetInternalBorder(tokenPtr->tkwin, tokenPtr->borderWidth + 2);
1884 return TCL_OK;
1885 }
1886
1887 static int
1888 GetFormattedData(dndPtr, format, timestamp, resultPtr)
1889 Dnd *dndPtr;
1890 char *format;
1891 int timestamp;
1892 Tcl_DString *resultPtr;
1893 {
1894 Tcl_Interp *interp = dndPtr->interp;
1895 Blt_HashEntry *hPtr;
1896 char **formatCmd;
1897 Tcl_DString savedResult;
1898 Tcl_DString dString;
1899 char **p;
1900 int x, y;
1901
1902 /* Find the data converter for the prescribed format. */
1903 hPtr = Blt_FindHashEntry(&(dndPtr->getDataTable), format);
1904 if (hPtr == NULL) {
1905 Tcl_AppendResult(interp, "can't find format \"", format,
1906 "\" in source \"", Tk_PathName(dndPtr->tkwin), "\"", (char *)NULL);
1907 return TCL_ERROR;
1908 }
1909 formatCmd = (char **)Blt_GetHashValue(hPtr);
1910 Tcl_DStringInit(&dString);
1911 for (p = formatCmd; *p != NULL; p++) {
1912 Tcl_DStringAppendElement(&dString, *p);
1913 }
1914 x = dndPtr->dragX - Blt_RootX(dndPtr->tkwin);
1915 y = dndPtr->dragY - Blt_RootY(dndPtr->tkwin);
1916 Tcl_DStringAppendElement(&dString, Tk_PathName(dndPtr->tkwin));
1917 Tcl_DStringAppendElement(&dString, "x");
1918 Tcl_DStringAppendElement(&dString, Blt_Itoa(x));
1919 Tcl_DStringAppendElement(&dString, "y");
1920 Tcl_DStringAppendElement(&dString, Blt_Itoa(y));
1921 Tcl_DStringAppendElement(&dString, "timestamp");
1922 Tcl_DStringAppendElement(&dString, Blt_Utoa(timestamp));
1923 Tcl_DStringAppendElement(&dString, "format");
1924 Tcl_DStringAppendElement(&dString, format);
1925 Tcl_DStringInit(&savedResult);
1926 Tcl_DStringGetResult(interp, &savedResult);
1927 if (Tcl_GlobalEval(interp, Tcl_DStringValue(&dString)) != TCL_OK) {
1928 Tcl_BackgroundError(interp);
1929 }
1930 Tcl_DStringFree(&dString);
1931 Tcl_DStringInit(resultPtr);
1932 Tcl_DStringGetResult(interp, resultPtr);
1933
1934 /* Restore the interpreter result. */
1935 Tcl_DStringResult(interp, &savedResult);
1936 return TCL_OK;
1937 }
1938
1939
1940 /*
1941 * ------------------------------------------------------------------------
1942 *
1943 * DestroyDnd --
1944 *
1945 * Free resources allocated for the drag&drop window.
1946 *
1947 * ------------------------------------------------------------------------
1948 */
1949 static void
1950 DestroyDnd(data)
1951 DestroyData data;
1952 {
1953 Dnd *dndPtr = (Dnd *)data;
1954 Blt_HashEntry *hPtr;
1955 Blt_HashSearch cursor;
1956 char **cmd;
1957
1958 Tk_FreeOptions(configSpecs, (char *)dndPtr, dndPtr->display, 0);
1959 Tk_DeleteGenericHandler(DndEventProc, dndPtr);
1960 for (hPtr = Blt_FirstHashEntry(&(dndPtr->getDataTable), &cursor);
1961 hPtr != NULL; hPtr = Blt_NextHashEntry(&cursor)) {
1962 cmd = (char **)Blt_GetHashValue(hPtr);
1963 if (cmd != NULL) {
1964 Blt_Free(cmd);
1965 }
1966 }
1967 Blt_DeleteHashTable(&(dndPtr->getDataTable));
1968
1969 for (hPtr = Blt_FirstHashEntry(&(dndPtr->setDataTable), &cursor);
1970 hPtr != NULL; hPtr = Blt_NextHashEntry(&cursor)) {
1971 cmd = (char **)Blt_GetHashValue(hPtr);
1972 if (cmd != NULL) {
1973 Blt_Free(cmd);
1974 }
1975 }
1976 Blt_DeleteHashTable(&(dndPtr->setDataTable));
1977 if (dndPtr->rootPtr != NULL) {
1978 FreeWinfo(dndPtr->rootPtr);
1979 }
1980 if (dndPtr->cursor != None) {
1981 Tk_FreeCursor(dndPtr->display, dndPtr->cursor);
1982 }
1983 if (dndPtr->reqFormats != NULL) {
1984 Blt_Free(dndPtr->reqFormats);
1985 }
1986 if (dndPtr->matchingFormats != NULL) {
1987 Blt_Free(dndPtr->matchingFormats);
1988 }
1989
1990 /* Now that the various commands are custom list options, we need
1991 * to explicitly free them. */
1992 if (dndPtr->motionCmd != NULL) {
1993 Blt_Free(dndPtr->motionCmd);
1994 }
1995 if (dndPtr->leaveCmd != NULL) {
1996 Blt_Free(dndPtr->leaveCmd);
1997 }
1998 if (dndPtr->enterCmd != NULL) {
1999 Blt_Free(dndPtr->enterCmd);
2000 }
2001 if (dndPtr->dropCmd != NULL) {
2002 Blt_Free(dndPtr->dropCmd);
2003 }
2004 if (dndPtr->resultCmd != NULL) {
2005 Blt_Free(dndPtr->resultCmd);
2006 }
2007 if (dndPtr->packageCmd != NULL) {
2008 Blt_Free(dndPtr->packageCmd);
2009 }
2010 if (dndPtr->siteCmd != NULL) {
2011 Blt_Free(dndPtr->siteCmd);
2012 }
2013
2014 if (dndPtr->hashPtr != NULL) {
2015 Blt_DeleteHashEntry(&(dndPtr->dataPtr->dndTable), dndPtr->hashPtr);
2016 }
2017 if (dndPtr->tokenPtr != NULL) {
2018 DestroyToken((DestroyData)dndPtr);
2019 }
2020 if (dndPtr->tkwin != NULL) {
2021 XDeleteProperty(dndPtr->display, Tk_WindowId(dndPtr->tkwin),
2022 dndPtr->dataPtr->targetAtom);
2023 XDeleteProperty(dndPtr->display, Tk_WindowId(dndPtr->tkwin),
2024 dndPtr->dataPtr->commAtom);
2025 }
2026 Blt_Free(dndPtr);
2027 }
2028
2029
2030 /*
2031 * ------------------------------------------------------------------------
2032 *
2033 * GetDnd --
2034 *
2035 * Looks for a Source record in the hash table for drag&drop source
2036 * widgets. Returns a pointer to the desired record.
2037 *
2038 * ------------------------------------------------------------------------
2039 */
2040 static int
2041 GetDnd(clientData, interp, pathName, dndPtrPtr)
2042 ClientData clientData;
2043 Tcl_Interp *interp;
2044 char *pathName; /* widget pathname for desired record */
2045 Dnd **dndPtrPtr;
2046 {
2047 DndInterpData *dataPtr = clientData;
2048 Blt_HashEntry *hPtr;
2049 Tk_Window tkwin;
2050
2051 tkwin = Tk_NameToWindow(interp, pathName, dataPtr->mainWindow);
2052 if (tkwin == NULL) {
2053 return TCL_ERROR;
2054 }
2055 hPtr = Blt_FindHashEntry(&(dataPtr->dndTable), (char *)tkwin);
2056 if (hPtr == NULL) {
2057 Tcl_AppendResult(interp, "window \"", pathName,
2058 "\" is not a drag&drop source/target", (char *)NULL);
2059 return TCL_ERROR;
2060 }
2061 *dndPtrPtr = (Dnd *)Blt_GetHashValue(hPtr);
2062 return TCL_OK;
2063 }
2064
2065 /*
2066 * ------------------------------------------------------------------------
2067 *
2068 * CreateDnd --
2069 *
2070 * Looks for a Source record in the hash table for drag&drop source
2071 * widgets. Creates a new record if the widget name is not already
2072 * registered. Returns a pointer to the desired record.
2073 *
2074 * ------------------------------------------------------------------------
2075 */
2076 static Dnd *
2077 CreateDnd(interp, tkwin)
2078 Tcl_Interp *interp;
2079 Tk_Window tkwin; /* widget for desired record */
2080 {
2081 Dnd *dndPtr;
2082
2083 dndPtr = Blt_Calloc(1, sizeof(Dnd));
2084 assert(dndPtr);
2085 dndPtr->interp = interp;
2086 dndPtr->display = Tk_Display(tkwin);
2087 dndPtr->tkwin = tkwin;
2088 Tk_MakeWindowExist(tkwin);
2089 Blt_InitHashTable(&(dndPtr->setDataTable), BLT_STRING_KEYS);
2090 Blt_InitHashTable(&(dndPtr->getDataTable), BLT_STRING_KEYS);
2091 Tk_CreateGenericHandler(DndEventProc, dndPtr);
2092 return dndPtr;
2093 }
2094
2095 static int
2096 ConfigureDnd(interp, dndPtr)
2097 Tcl_Interp *interp;
2098 Dnd *dndPtr;
2099 {
2100 Tcl_CmdInfo cmdInfo;
2101 Tcl_DString dString;
2102 int button, result;
2103
2104 if (!Tcl_GetCommandInfo(interp, "blt::DndInit", &cmdInfo)) {
2105 static char cmd[] = "source [file join $blt_library dnd.tcl]";
2106 /*
2107 * If the "DndInit" routine hasn't been sourced, do it now.
2108 */
2109 if (Tcl_GlobalEval(interp, cmd) != TCL_OK) {
2110 Tcl_AddErrorInfo(interp,
2111 "\n (while loading bindings for blt::drag&drop)");
2112 return TCL_ERROR;
2113 }
2114 }
2115 /*
2116 * Reset the target property if it's changed state or
2117 * added/subtracted one of its callback procedures.
2118 */
2119 if (Blt_ConfigModified(configSpecs, interp, "-target", "-onenter", "-onmotion",
2120 "-onleave", (char *)NULL)) {
2121 if (dndPtr->targetPropertyExists) {
2122 XDeleteProperty(dndPtr->display, Tk_WindowId(dndPtr->tkwin),
2123 dndPtr->dataPtr->targetAtom);
2124 dndPtr->targetPropertyExists = FALSE;
2125 }
2126 if (dndPtr->isTarget) {
2127 AddTargetProperty(dndPtr);
2128 dndPtr->targetPropertyExists = TRUE;
2129 }
2130 }
2131 if (dndPtr->isSource) {
2132 /* Check the button binding for valid range (0 or 1-5) */
2133 if ((dndPtr->reqButton < 0) || (dndPtr->reqButton > 5)) {
2134 Tcl_AppendResult(interp,
2135 "button must be 1-5, or 0 for no bindings",
2136 (char *)NULL);
2137 return TCL_ERROR;
2138 }
2139 button = dndPtr->reqButton;
2140 } else {
2141 button = 0;
2142 }
2143 Tcl_DStringInit(&dString);
2144 Blt_DStringAppendElements(&dString, "blt::DndInit",
2145 Tk_PathName(dndPtr->tkwin), Blt_Itoa(button), (char *)NULL);
2146 result = Tcl_GlobalEval(interp, Tcl_DStringValue(&dString));
2147 Tcl_DStringFree(&dString);
2148 if (result != TCL_OK) {
2149 return TCL_ERROR;
2150 }
2151 return TCL_OK;
2152 }
2153
2154
2155 /*
2156 *----------------------------------------------------------------------
2157 *
2158 * SendRestrictProc --
2159 *
2160 * This procedure filters incoming events when a "send" command
2161 * is outstanding. It defers all events except those containing
2162 * send commands and results.
2163 *
2164 * Results:
2165 * False is returned except for property-change events on a
2166 * commWindow.
2167 *
2168 * Side effects:
2169 * None.
2170 *
2171 *----------------------------------------------------------------------
2172 */
2173
2174 /* ARGSUSED */
2175 static Tk_RestrictAction
2176 SendRestrictProc(clientData, eventPtr)
2177 ClientData clientData; /* Drag-and-drop manager. */
2178 register XEvent *eventPtr; /* Event that just arrived. */
2179 {
2180 Dnd *dndPtr = clientData;
2181
2182 if (eventPtr->xproperty.window != Tk_WindowId(dndPtr->tkwin)) {
2183 return TK_PROCESS_EVENT; /* Event not in our window. */
2184 }
2185 if ((eventPtr->type == PropertyNotify) &&
2186 (eventPtr->xproperty.state == PropertyNewValue)) {
2187 return TK_PROCESS_EVENT; /* This is the one we want to process. */
2188 }
2189 if (eventPtr->type == Expose) {
2190 return TK_PROCESS_EVENT; /* Let expose events also get
2191 * handled. */
2192 }
2193 return TK_DEFER_EVENT; /* Defer everything else. */
2194 }
2195
2196 /*
2197 *----------------------------------------------------------------------
2198 *
2199 * SendTimerProc --
2200 *
2201 * Procedure called when the timer event elapses. Used to wait
2202 * between attempts checking for the designated window.
2203 *
2204 * Results:
2205 * None.
2206 *
2207 * Side Effects:
2208 * Sets a flag, indicating the timeout occurred.
2209 *
2210 *----------------------------------------------------------------------
2211 */
2212 static void
2213 SendTimerProc(clientData)
2214 ClientData clientData;
2215 {
2216 int *statusPtr = clientData;
2217
2218 /*
2219 * An unusually long amount of time has elapsed since the drag
2220 * start message was sent. Assume that the other party has died
2221 * and abort the operation.
2222 */
2223 *statusPtr = DROP_FAIL;
2224 }
2225
2226 #define WAIT_INTERVAL 2000 /* Twenty seconds. */
2227
2228 /*
2229 * ------------------------------------------------------------------------
2230 *
2231 * TargetPropertyEventProc --
2232 *
2233 * Invoked by the Tk dispatcher to handle widget events.
2234 * Manages redraws for the drag&drop token window.
2235 *
2236 * ------------------------------------------------------------------------
2237 */
2238 static void
2239 TargetPropertyEventProc(clientData, eventPtr)
2240 ClientData clientData; /* Data associated with transaction. */
2241 XEvent *eventPtr; /* information about event */
2242 {
2243 DropPending *pendingPtr = clientData;
2244 char *data;
2245 int result, format;
2246 Atom typeAtom;
2247 unsigned long nItems, bytesAfter;
2248
2249 #ifdef notdef
2250 fprintf(stderr, "TargetPropertyEventProc\n");
2251 #endif
2252 if ((eventPtr->type != PropertyNotify) ||
2253 (eventPtr->xproperty.atom != pendingPtr->commAtom) ||
2254 (eventPtr->xproperty.state != PropertyNewValue)) {
2255 return;
2256 }
2257 Tcl_DeleteTimerHandler(pendingPtr->timerToken);
2258 data = NULL;
2259 result = XGetWindowProperty(
2260 eventPtr->xproperty.display, /* Display of window. */
2261 eventPtr->xproperty.window, /* Window holding the property. */
2262 eventPtr->xproperty.atom, /* Name of property. */
2263 0, /* Offset of data (for multiple reads). */
2264 pendingPtr->packetSize, /* Maximum number of items to read. */
2265 False, /* If true, delete the property. */
2266 XA_STRING, /* Desired type of property. */
2267 &typeAtom, /* (out) Actual type of the property. */
2268 &format, /* (out) Actual format of the property. */
2269 &nItems, /* (out) # of items in specified format. */
2270 &bytesAfter, /* (out) # of bytes remaining to be read. */
2271 (unsigned char **)&data);
2272 #ifdef notdef
2273 fprintf(stderr,
2274 "TargetPropertyEventProc: result=%d, typeAtom=%d, format=%d, nItems=%d\n",
2275 result, typeAtom, format, nItems);
2276 #endif
2277 pendingPtr->status = DROP_FAIL;
2278 if ((result == Success) && (typeAtom == XA_STRING) && (format == 8)) {
2279 pendingPtr->status = DROP_OK;
2280 #ifdef notdef
2281 fprintf(stderr, "data found is (%s)\n", data);
2282 #endif
2283 Tcl_DStringAppend(&(pendingPtr->dString), data, -1);
2284 XFree(data);
2285 if (nItems == pendingPtr->packetSize) {
2286 /* Normally, we'll receive the data in one chunk. But if
2287 * more are required, reset the timer and go back into the
2288 * wait loop again. */
2289 pendingPtr->timerToken = Tcl_CreateTimerHandler(WAIT_INTERVAL,
2290 SendTimerProc, &pendingPtr->status);
2291 pendingPtr->status = DROP_CONTINUE;
2292 }
2293 }
2294 /* Set an empty, zero-length value on the source's property. This
2295 * acts as a handshake, indicating that the target received the
2296 * latest chunk. */
2297 #ifdef notdef
2298 fprintf(stderr, "TargetPropertyEventProc: set response property\n");
2299 #endif
2300 XChangeProperty(pendingPtr->display, pendingPtr->window,
2301 pendingPtr->commAtom, XA_STRING, 8, PropModeReplace,
2302 (unsigned char *)"", 0);
2303 }
2304
2305 #ifdef HAVE_XDND
2306
2307 static int
2308 XDndSelectionProc(clientData, interp, portion)
2309 ClientData clientData;
2310 Tcl_Interp *interp;
2311 char *portion;
2312 {
2313 DropPending *pendingPtr = clientData;
2314
2315 Tcl_DStringAppend(&(pendingPtr->dString), portion, -1);
2316 #ifdef notdef
2317 fprintf(stderr, "-> XDndGetSelectionProc\n");
2318 #endif
2319 return TCL_OK;
2320 }
2321
2322 #endif /* HAVE_XDND */
2323
2324 static void
2325 CompleteDataTransaction(dndPtr, format, pendingPtr)
2326 Dnd *dndPtr;
2327 char *format;
2328 DropPending *pendingPtr;
2329 {
2330 DndInterpData *dataPtr = dndPtr->dataPtr;
2331 Tk_Window tkwin;
2332 Atom formatAtom;
2333
2334 #ifdef notdef
2335 fprintf(stderr, "-> CompleteDataTransaction\n");
2336 #endif
2337 /* Check if the source is local to the application. */
2338 tkwin = Tk_IdToWindow(dndPtr->display, pendingPtr->window);
2339 if (tkwin != NULL) {
2340 Blt_HashEntry *hPtr;
2341
2342 hPtr = Blt_FindHashEntry(&(dndPtr->dataPtr->dndTable), (char *)tkwin);
2343 if (hPtr != NULL) {
2344 Dnd *srcPtr;
2345
2346 srcPtr = (Dnd *)Blt_GetHashValue(hPtr);
2347 GetFormattedData(srcPtr, format, pendingPtr->timestamp,
2348 &(pendingPtr->dString));
2349 }
2350 return;
2351 }
2352
2353 formatAtom = XInternAtom(pendingPtr->display, format, False);
2354
2355 if (pendingPtr->protocol == PROTO_XDND) {
2356 #ifdef HAVE_XDND
2357 if (Tk_GetSelection(dndPtr->interp, dndPtr->tkwin,
2358 dataPtr->XdndSelectionAtom, formatAtom, XDndSelectionProc,
2359 pendingPtr) != TCL_OK) {
2360 pendingPtr->status = DROP_FAIL;
2361 }
2362 #endif
2363 pendingPtr->status = DROP_OK;
2364 } else {
2365 Tk_RestrictProc *proc;
2366 ClientData arg;
2367
2368 SendClientMsg(pendingPtr->display, pendingPtr->window,
2369 dataPtr->mesgAtom,
2370 TS_START_DROP,
2371 (int)Tk_WindowId(dndPtr->tkwin),
2372 pendingPtr->timestamp,
2373 (int)formatAtom,
2374 (int)pendingPtr->commAtom);
2375
2376 pendingPtr->commAtom = dndPtr->dataPtr->commAtom;
2377 pendingPtr->status = DROP_CONTINUE;
2378 pendingPtr->display = dndPtr->display;
2379 proc = Tk_RestrictEvents(SendRestrictProc, dndPtr, &arg);
2380 Tk_CreateEventHandler(dndPtr->tkwin, PropertyChangeMask,
2381 TargetPropertyEventProc, pendingPtr);
2382 pendingPtr->timerToken = Tcl_CreateTimerHandler(WAIT_INTERVAL,
2383 SendTimerProc, &pendingPtr->status);
2384 /*
2385 * ----------------------------------------------------------
2386 *
2387 * Enter a loop processing X events until the all the data is
2388 * received or the source is declared to be dead (i.e. we
2389 * timeout). While waiting for a result, restrict handling to
2390 * just property-related events so that the transfer is
2391 * synchronous with respect to other events in the widget.
2392 *
2393 * ----------------------------------------------------------
2394 */
2395 while (pendingPtr->status == DROP_CONTINUE) {
2396 /* Wait for property event. */
2397 Tcl_DoOneEvent(TCL_ALL_EVENTS);
2398 }
2399 Tk_RestrictEvents(proc, arg, &arg);
2400 Tcl_DeleteTimerHandler(pendingPtr->timerToken);
2401 Tk_DeleteEventHandler(dndPtr->tkwin, PropertyChangeMask,
2402 TargetPropertyEventProc, pendingPtr);
2403 }
2404 #ifdef notdef
2405 fprintf(stderr, "<- CompleteDataTransaction\n");
2406 #endif
2407 }
2408
2409 /*
2410 * ------------------------------------------------------------------------
2411 *
2412 * SourcePropertyEventProc --
2413 *
2414 * Invoked by the Tk dispatcher when a PropertyNotify event occurs
2415 * on the source window. The event acts as a handshake between the
2416 * target and the source. The source acknowledges the target has
2417 * received the last packet of data and sends the next packet.
2418 *
2419 * Note a special case. If the data is divisible by the packetsize,
2420 * then an extra zero-length packet is sent to mark the end of the
2421 * data. A packetsize length packet indicates more is to follow.
2422 *
2423 * Normally the property is empty (zero-length). But if an
2424 * errored occurred on the target, it will contain the error
2425 * message.
2426 *
2427 * ------------------------------------------------------------------------
2428 */
2429 static void
2430 SourcePropertyEventProc(clientData, eventPtr)
2431 ClientData clientData; /* data associated with widget */
2432 XEvent *eventPtr; /* information about event */
2433 {
2434 DropPending *pendingPtr = clientData;
2435 char *data;
2436 int result, format;
2437 Atom typeAtom;
2438 unsigned long nItems, bytesAfter;
2439 int size, bytesLeft;
2440 unsigned char *p;
2441
2442 #ifdef notdef
2443 fprintf(stderr, "-> SourcePropertyEventProc\n");
2444 #endif
2445 if ((eventPtr->xproperty.atom != pendingPtr->commAtom)
2446 || (eventPtr->xproperty.state != PropertyNewValue)) {
2447 return;
2448 }
2449 Tcl_DeleteTimerHandler(pendingPtr->timerToken);
2450 data = NULL;
2451 result = XGetWindowProperty(
2452 eventPtr->xproperty.display, /* Display of window. */
2453 eventPtr->xproperty.window, /* Window holding the property. */
2454 eventPtr->xproperty.atom, /* Name of property. */
2455 0, /* Offset of data (for multiple reads). */
2456 pendingPtr->packetSize, /* Maximum number of items to read. */
2457 True, /* If true, delete the property. */
2458 XA_STRING, /* Desired type of property. */
2459 &typeAtom, /* (out) Actual type of the property. */
2460 &format, /* (out) Actual format of the property. */
2461 &nItems, /* (out) # of items in specified format. */
2462 &bytesAfter, /* (out) # of bytes remaining to be read. */
2463 (unsigned char **)&data);
2464
2465 if ((result != Success) || (typeAtom != XA_STRING) || (format != 8)) {
2466 pendingPtr->status = DROP_FAIL;
2467 #ifdef notdef
2468 fprintf(stderr, "<- SourcePropertyEventProc: wrong format\n");
2469 #endif
2470 return; /* Wrong data format. */
2471 }
2472 if (nItems > 0) {
2473 pendingPtr->status = DROP_FAIL;
2474 Tcl_DStringFree(&(pendingPtr->dString));
2475 Tcl_DStringAppend(&(pendingPtr->dString), data, -1);
2476 XFree(data);
2477 #ifdef notdef
2478 fprintf(stderr, "<- SourcePropertyEventProc: error\n");
2479 #endif
2480 return; /* Error occurred on target. */
2481 }
2482 bytesLeft = Tcl_DStringLength(&(pendingPtr->dString)) - pendingPtr->offset;
2483 if (bytesLeft <= 0) {
2484 #ifdef notdef
2485 fprintf(stderr, "<- SourcePropertyEventProc: done\n");
2486 #endif
2487 pendingPtr->status = DROP_OK;
2488 size = 0;
2489 } else {
2490 size = MIN(bytesLeft, pendingPtr->packetSize);
2491 pendingPtr->status = DROP_CONTINUE;
2492 }
2493 p = (unsigned char *)Tcl_DStringValue(&(pendingPtr->dString)) +
2494 pendingPtr->offset;
2495 XChangeProperty(pendingPtr->display, pendingPtr->window,
2496 pendingPtr->commAtom, XA_STRING, 8, PropModeReplace, p, size);
2497 pendingPtr->offset += size;
2498 pendingPtr->timerToken = Tcl_CreateTimerHandler(WAIT_INTERVAL,
2499 SendTimerProc, &pendingPtr->status);
2500 #ifdef notdef
2501 fprintf(stderr, "<- SourcePropertyEventProc\n");
2502 #endif
2503 }
2504
2505
2506 static void
2507 SendDataToTarget(dndPtr, pendingPtr)
2508 Dnd *dndPtr;
2509 DropPending *pendingPtr;
2510 {
2511 int size;
2512 Tk_RestrictProc *proc;
2513 ClientData arg;
2514
2515 #ifdef notdef
2516 fprintf(stderr, "-> SendDataToTarget\n");
2517 #endif
2518 Tk_CreateEventHandler(dndPtr->tkwin, PropertyChangeMask,
2519 SourcePropertyEventProc, pendingPtr);
2520 pendingPtr->timerToken = Tcl_CreateTimerHandler(WAIT_INTERVAL,
2521 SendTimerProc, &pendingPtr->status);
2522 size = MIN(Tcl_DStringLength(&pendingPtr->dString), pendingPtr->packetSize);
2523
2524 proc = Tk_RestrictEvents(SendRestrictProc, dndPtr, &arg);
2525
2526 /*
2527 * Setting the property starts the process. The target will
2528 * see the PropertyChange event and respond accordingly.
2529 */
2530 XChangeProperty(dndPtr->display, pendingPtr->window,
2531 pendingPtr->commAtom, XA_STRING, 8, PropModeReplace,
2532 (unsigned char *)Tcl_DStringValue(&(pendingPtr->dString)), size);
2533 pendingPtr->offset += size;
2534
2535 /*
2536 * Enter a loop processing X events until the result comes
2537 * in or the target is declared to be dead. While waiting
2538 * for a result, look only at property-related events so that
2539 * the handshake is synchronous with respect to other events in
2540 * the application.
2541 */
2542 pendingPtr->status = DROP_CONTINUE;
2543 while (pendingPtr->status == DROP_CONTINUE) {
2544 /* Wait for the property change event. */
2545 Tcl_DoOneEvent(TCL_ALL_EVENTS);
2546 }
2547 Tk_RestrictEvents(proc, arg, &arg);
2548 Tcl_DeleteTimerHandler(pendingPtr->timerToken);
2549 Tk_DeleteEventHandler(dndPtr->tkwin, PropertyChangeMask,
2550 SourcePropertyEventProc, pendingPtr);
2551 #ifdef notdef
2552 fprintf(stderr, "<- SendDataToTarget\n");
2553 #endif
2554 }
2555
2556 static void
2557 DoDrop(dndPtr, eventPtr)
2558 Dnd *dndPtr;
2559 XEvent *eventPtr;
2560 {
2561 DndInterpData *dataPtr = dndPtr->dataPtr;
2562 Token *tokenPtr = dndPtr->tokenPtr;
2563 Tcl_Interp *interp = dndPtr->interp;
2564 struct DropRequest {
2565 int mesg; /* TS_DROP_RESULT message. */
2566 Window window; /* Target window. */
2567 int timestamp; /* Transaction timestamp. */
2568 Atom formatAtom; /* Format requested. */
2569 } *dropPtr;
2570 char *format;
2571 DropPending pending;
2572
2573 if (tokenPtr->timerToken != NULL) {
2574 Tcl_DeleteTimerHandler(tokenPtr->timerToken);
2575 }
2576 dropPtr = (struct DropRequest *)eventPtr->xclient.data.l;
2577 format = XGetAtomName(dndPtr->display, dropPtr->formatAtom);
2578 #ifdef notdef
2579 fprintf(stderr, "DoDrop %s 0x%x\n", Tk_PathName(dndPtr->tkwin),
2580 dropPtr->window);
2581 #endif
2582 if (GetFormattedData(dndPtr, format, dropPtr->timestamp, &(pending.dString))
2583 != TCL_OK) {
2584 Tcl_BackgroundError(interp);
2585 /* Send empty string to break target's wait loop. */
2586 XChangeProperty(dndPtr->display, dropPtr->window, dataPtr->commAtom,
2587 XA_STRING, 8, PropModeReplace, (unsigned char *)"", 0);
2588 return;
2589 }
2590 pending.window = dropPtr->window;
2591 pending.display = dndPtr->display;
2592 pending.commAtom = dataPtr->commAtom;
2593 pending.offset = 0;
2594 pending.packetSize = GetMaxPropertySize(dndPtr->display);
2595 SendDataToTarget(dndPtr, &pending);
2596 Tcl_DStringFree(&(pending.dString));
2597 #ifdef notdef
2598 fprintf(stderr, "<- DoDrop\n");
2599 #endif
2600 }
2601
2602 static void
2603 DropFinished(dndPtr, eventPtr)
2604 Dnd *dndPtr;
2605 XEvent *eventPtr;
2606 {
2607 Tcl_Interp *interp = dndPtr->interp;
2608 Tcl_DString dString, savedResult;
2609 int result;
2610 char **p;
2611 struct DropResult {
2612 int mesg; /* TS_DROP_RESULT message. */
2613 Window window; /* Target window. */
2614 int timestamp; /* Transaction timestamp. */
2615 int result; /* Result of transaction. */
2616 } *dropPtr;
2617
2618 #ifdef notdef
2619 fprintf(stderr, "DropFinished\n");
2620 #endif
2621 dropPtr = (struct DropResult *)eventPtr->xclient.data.l;
2622
2623 Tcl_DStringInit(&dString);
2624 for (p = dndPtr->resultCmd; *p != NULL; p++) {
2625 Tcl_DStringAppendElement(&dString, *p);
2626 }
2627 Tcl_DStringAppendElement(&dString, Tk_PathName(dndPtr->tkwin));
2628 Tcl_DStringAppendElement(&dString, "action");
2629 Tcl_DStringAppendElement(&dString, NameOfAction(dropPtr->result));
2630 Tcl_DStringAppendElement(&dString, "timestamp");
2631 Tcl_DStringAppendElement(&dString, Blt_Utoa(dropPtr->timestamp));
2632
2633 Tcl_DStringInit(&savedResult);
2634 Tcl_DStringGetResult(interp, &savedResult);
2635 result = Tcl_GlobalEval(interp, Tcl_DStringValue(&dString));
2636 Tcl_DStringFree(&dString);
2637 if (result != TCL_OK) {
2638 Tcl_BackgroundError(interp);
2639 }
2640 Tcl_DStringResult(interp, &savedResult);
2641 }
2642
2643
2644 static void
2645 FreeFormats(dndPtr)
2646 Dnd *dndPtr;
2647 {
2648 if (dndPtr->matchingFormats != NULL) {
2649 Blt_Free(dndPtr->matchingFormats);
2650 dndPtr->matchingFormats = NULL;
2651 }
2652 dndPtr->lastId = None;
2653 }
2654
2655 static char *
2656 GetSourceFormats(dndPtr, window, timestamp)
2657 Dnd *dndPtr;
2658 Window window;
2659 int timestamp;
2660 {
2661 if (dndPtr->lastId != timestamp) {
2662 char *data;
2663
2664 FreeFormats(dndPtr);
2665 data = GetProperty(dndPtr->display, window,
2666 dndPtr->dataPtr->formatsAtom);
2667 if (data != NULL) {
2668 dndPtr->matchingFormats = Blt_Strdup(data);
2669 XFree(data);
2670 }
2671 dndPtr->lastId = timestamp;
2672 }
2673 if (dndPtr->matchingFormats == NULL) {
2674 return "";
2675 }
2676 return dndPtr->matchingFormats;
2677 }
2678
2679
2680 static int
2681 InvokeCallback(dndPtr, cmd, x, y, formats, button, keyState, timestamp)
2682 Dnd *dndPtr;
2683 char **cmd;
2684 int x, y;
2685 char *formats;
2686 int button, keyState, timestamp;
2687 {
2688 Tcl_DString dString, savedResult;
2689 Tcl_Interp *interp = dndPtr->interp;
2690 int result;
2691 char **p;
2692
2693 Tcl_DStringInit(&dString);
2694 for (p = cmd; *p != NULL; p++) {
2695 Tcl_DStringAppendElement(&dString, *p);
2696 }
2697 Tcl_DStringAppendElement(&dString, Tk_PathName(dndPtr->tkwin));
2698 x -= Blt_RootX(dndPtr->tkwin); /* Send coordinates relative to target. */
2699 y -= Blt_RootY(dndPtr->tkwin);
2700 Tcl_DStringAppendElement(&dString, "x");
2701 Tcl_DStringAppendElement(&dString, Blt_Itoa(x));
2702 Tcl_DStringAppendElement(&dString, "y");
2703 Tcl_DStringAppendElement(&dString, Blt_Itoa(y));
2704 Tcl_DStringAppendElement(&dString, "formats");
2705 if (formats == NULL) {
2706 formats = "";
2707 }
2708 Tcl_DStringAppendElement(&dString, formats);
2709 Tcl_DStringAppendElement(&dString, "button");
2710 Tcl_DStringAppendElement(&dString, Blt_Itoa(button));
2711 Tcl_DStringAppendElement(&dString, "state");
2712 Tcl_DStringAppendElement(&dString, Blt_Itoa(keyState));
2713 Tcl_DStringAppendElement(&dString, "timestamp");
2714 Tcl_DStringAppendElement(&dString, Blt_Utoa(timestamp));
2715 Tcl_Preserve(interp);
2716 Tcl_DStringInit(&savedResult);
2717 Tcl_DStringGetResult(interp, &savedResult);
2718 result = Tcl_GlobalEval(interp, Tcl_DStringValue(&dString));
2719 Tcl_DStringFree(&dString);
2720 if (result == TCL_OK) {
2721 result = GetDragResult(interp, Tcl_GetStringResult(interp));
2722 } else {
2723 result = DROP_CANCEL;
2724 Tcl_BackgroundError(interp);
2725 }
2726 Tcl_DStringResult(interp, &savedResult);
2727 Tcl_Release(interp);
2728 return result;
2729 }
2730
2731 /*
2732 * ------------------------------------------------------------------------
2733 *
2734 * AcceptDrop --
2735 *
2736 * Invokes a Tcl procedure to handle the target's side of the
2737 * drop. A Tcl procedure is invoked, either one designated for
2738 * this target by the user (-ondrop) or a default Tcl procedure.
2739 * It is passed the following arguments:
2740 *
2741 * widget The path name of the target.
2742 * x X-coordinate of the mouse relative to the
2743 * widget.
2744 * y Y-coordinate of the mouse relative to the
2745 * widget.
2746 * formats A list of data formats acceptable to both
2747 * the source and target.
2748 * button Button pressed.
2749 * state Key state.
2750 * timestamp Timestamp of transaction.
2751 * action Requested action from source.
2752 *
2753 * If the Tcl procedure returns "cancel", this indicates that the drop was
2754 * not accepted by the target and the reject symbol should be displayed.
2755 * Otherwise one of the following strings may be recognized:
2756 *
2757 * "cancel" Drop was canceled.
2758 * "copy" Source data has been successfully copied.
2759 * "link" Target has made a link to the data. It's
2760 * Ok for the source to remove it's association
2761 * with the data, but not to delete the data
2762 * itself.
2763 * "move" Source data has been successfully copied,
2764 * it's Ok for the source to delete its
2765 * association with the data and the data itself.
2766 *
2767 * The result is relayed back to the source via another client message.
2768 * The source may or may not be waiting for the result.
2769 *
2770 * Results:
2771 * None.
2772 *
2773 * Side Effects:
2774 * A Tcl procedure is invoked in the target to handle the drop event.
2775 * The result of the drop is sent (via another ClientMessage) to the
2776 * source.
2777 *
2778 * ------------------------------------------------------------------------
2779 */
2780 static int
2781 AcceptDrop(dndPtr, x, y, formats, button, keyState, timestamp)
2782 Dnd *dndPtr; /* Target where the drop event occurred. */
2783 int x, y;
2784 char *formats;
2785 int button, keyState, timestamp;
2786 {
2787 Tcl_Interp *interp = dndPtr->interp;
2788 char **cmd;
2789 Tcl_DString dString, savedResult;
2790 int result;
2791
2792 if (dndPtr->motionCmd != NULL) {
2793 result = InvokeCallback(dndPtr, dndPtr->motionCmd, x, y, formats,
2794 button, keyState, timestamp);
2795 if (result != DROP_OK) {
2796 return result;
2797 }
2798 }
2799 if (dndPtr->leaveCmd != NULL) {
2800 InvokeCallback(dndPtr, dndPtr->leaveCmd, x, y, formats, button,
2801 keyState, timestamp);
2802 }
2803 Tcl_DStringInit(&dString);
2804 cmd = dndPtr->dropCmd;
2805 if (cmd != NULL) {
2806 char **p;
2807
2808 for (p = cmd; *p != NULL; p++) {
2809 Tcl_DStringAppendElement(&dString, *p);
2810 }
2811 } else {
2812 Tcl_DStringAppendElement(&dString, "blt::DndStdDrop");
2813 }
2814 Tcl_DStringAppendElement(&dString, Tk_PathName(dndPtr->tkwin));
2815 dndPtr->dropX = x - Blt_RootX(dndPtr->tkwin);
2816 dndPtr->dropY = y - Blt_RootY(dndPtr->tkwin);
2817 Tcl_DStringAppendElement(&dString, "x");
2818 Tcl_DStringAppendElement(&dString, Blt_Itoa(dndPtr->dropX));
2819 Tcl_DStringAppendElement(&dString, "y");
2820 Tcl_DStringAppendElement(&dString, Blt_Itoa(dndPtr->dropY));
2821 Tcl_DStringAppendElement(&dString, "formats");
2822 Tcl_DStringAppendElement(&dString, formats);
2823 Tcl_DStringAppendElement(&dString, "button");
2824 Tcl_DStringAppendElement(&dString, Blt_Itoa(button));
2825 Tcl_DStringAppendElement(&dString, "state");
2826 Tcl_DStringAppendElement(&dString, Blt_Itoa(keyState));
2827 Tcl_DStringAppendElement(&dString, "timestamp");
2828 Tcl_DStringAppendElement(&dString, Blt_Utoa(timestamp));
2829 Tcl_Preserve(interp);
2830 Tcl_DStringInit(&savedResult);
2831 Tcl_DStringGetResult(interp, &savedResult);
2832 result = Tcl_GlobalEval(interp, Tcl_DStringValue(&dString));
2833 Tcl_DStringFree(&dString);
2834 if (result == TCL_OK) {
2835 result = GetAction(Tcl_GetStringResult(interp));
2836 } else {
2837 result = DROP_CANCEL;
2838 Tcl_BackgroundError(interp);
2839 }
2840 Tcl_DStringResult(interp, &savedResult);
2841 Tcl_Release(interp);
2842 return result;
2843 }
2844
2845 /*
2846 * ------------------------------------------------------------------------
2847 *
2848 * HandleDropEvent --
2849 *
2850 * Invokes a Tcl procedure to handle the target's side of the
2851 * drop. This routine is triggered via a client message from the
2852 * drag source indicating that the token was dropped over this
2853 * target. The fields of the incoming message are:
2854 *
2855 * data.l[0] Message type.
2856 * data.l[1] Window Id of the source.
2857 * data.l[2] Screen X-coordinate of the pointer.
2858 * data.l[3] Screen Y-coordinate of the pointer.
2859 * data.l[4] Id of the drag&drop transaction.
2860 *
2861 * A Tcl procedure is invoked, either one designated for this
2862 * target by the user (-ondrop) or a default Tcl procedure. It
2863 * is passed the following arguments:
2864 *
2865 * widget The path name of the target.
2866 * x X-coordinate of the mouse relative to the
2867 * widget.
2868 * y Y-coordinate of the mouse relative to the
2869 * widget.
2870 * formats A list of data formats acceptable to both
2871 * the source and target.
2872 *
2873 * If the Tcl procedure returns "cancel", this indicates that the drop was
2874 * not accepted by the target and the reject symbol should be displayed.
2875 * Otherwise one of the following strings may be recognized:
2876 *
2877 * "cancel" Drop was canceled.
2878 * "copy" Source data has been successfully copied.
2879 * "link" Target has made a link to the data. It's
2880 * Ok for the source to remove it's association
2881 * with the data, but not to delete the data
2882 * itself.
2883 * "move" Source data has been successfully copied,
2884 * it's Ok for the source to delete its
2885 * association with the data and the data itself.
2886 *
2887 * The result is relayed back to the source via another client message.
2888 * The source may or may not be waiting for the result.
2889 *
2890 * Results:
2891 * None.
2892 *
2893 * Side Effects:
2894 * A Tcl procedure is invoked in the target to handle the drop event.
2895 * The result of the drop is sent (via another ClientMessage) to the
2896 * source.
2897 *
2898 * ------------------------------------------------------------------------
2899 */
2900 static void
2901 HandleDropEvent(dndPtr, eventPtr)
2902 Dnd *dndPtr; /* Target where the drop event occurred. */
2903 XEvent *eventPtr; /* Message sent from the drag source. */
2904 {
2905 int button, keyState;
2906 int x, y;
2907 char *formats;
2908 int result;
2909 struct DropInfo {
2910 int mesg; /* TS_DROP message. */
2911 Window window; /* Source window. */
2912 int timestamp; /* Transaction timestamp. */
2913 int point; /* Root X-Y coordinate of pointer. */
2914 int flags; /* Button/keystate information. */
2915 } *dropPtr;
2916 DropPending pending;
2917
2918 dropPtr = (struct DropInfo *)eventPtr->xclient.data.l;
2919 UNPACK(dropPtr->point, x, y);
2920 UNPACK(dropPtr->flags, button, keyState);
2921
2922 /* Set up temporary bookkeeping for the drop transaction */
2923 memset (&pending, 0, sizeof(pending));
2924 pending.window = dropPtr->window;
2925 pending.display = eventPtr->xclient.display;
2926 pending.timestamp = dropPtr->timestamp;
2927 pending.protocol = PROTO_BLT;
2928 pending.packetSize = GetMaxPropertySize(pending.display);
2929 Tcl_DStringInit(&(pending.dString));
2930
2931 formats = GetSourceFormats(dndPtr, dropPtr->window, dropPtr->timestamp);
2932
2933 dndPtr->pendingPtr = &pending;
2934 result = AcceptDrop(dndPtr, x, y, formats, button, keyState,
2935 dropPtr->timestamp);
2936 dndPtr->pendingPtr = NULL;
2937
2938 /* Target-to-Source: Drop result message. */
2939 SendClientMsg(dndPtr->display, dropPtr->window, dndPtr->dataPtr->mesgAtom,
2940 TS_DROP_RESULT, (int)Tk_WindowId(dndPtr->tkwin), dropPtr->timestamp,
2941 result, 0);
2942 FreeFormats(dndPtr);
2943 }
2944
2945 /*
2946 * ------------------------------------------------------------------------
2947 *
2948 * HandleDragEvent --
2949 *
2950 * Invokes one of 3 Tcl procedures to handle the target's side of
2951 * the drag operation. This routine is triggered via a ClientMessage
2952 * from the drag source indicating that the token as either entered,
2953 * moved, or left this target. The source sends messages only if
2954 * Tcl procedures on the target have been defined to watch the
2955 * events. The message_type field can be either
2956 *
2957 * ST_DRAG_ENTER The mouse has entered the target.
2958 * ST_DRAG_MOTION The mouse has moved within the target.
2959 * ST_DRAG_LEAVE The mouse has left the target.
2960 *
2961 * The data fields are as follows:
2962 * data.l[0] Message type.
2963 * data.l[1] Window Id of the source.
2964 * data.l[2] Timestamp of the drag&drop transaction.
2965 * data.l[3] Root X-Y coordinate of the pointer.
2966 * data.l[4] Button and key state information.
2967 *
2968 * For any of the 3 Tcl procedures, the following arguments
2969 * are passed:
2970 *
2971 * widget The path name of the target.
2972 * x X-coordinate of the mouse in the widget.
2973 * y Y-coordinate of the mouse in the widget.
2974 * formats A list of data formats acceptable to both
2975 * the source and target.
2976 *
2977 * If the Tcl procedure returns "cancel", this indicates that the drag
2978 * operation has been canceled and the reject symbol should be displayed.
2979 * Otherwise it should return a boolean:
2980 *
2981 * true Target will accept drop.
2982 * false Target will not accept the drop.
2983 *
2984 * The purpose of the Enter and Leave procedure is to allow the
2985 * target to provide visual feedback that the drop can occur or not.
2986 * The Motion procedure is for cases where the drop area is a smaller
2987 * area within the target, such as a canvas item on a canvas. The
2988 * procedure can determine (based upon the X-Y coordinates) whether
2989 * the pointer is over the canvas item and return a value accordingly.
2990 *
2991 * The result of the Tcl procedure is then relayed back to the
2992 * source by a ClientMessage.
2993 *
2994 * Results:
2995 * None.
2996 *
2997 * Side Effects:
2998 * A Tcl procedure is invoked in the target to handle the drag event.
2999 * The result of the drag is sent (via another ClientMessage) to the
3000 * source.
3001 *
3002 * ------------------------------------------------------------------------
3003 */
3004 static void
3005 HandleDragEvent(dndPtr, eventPtr)
3006 Dnd *dndPtr; /* Target where the drag event occurred. */
3007 XEvent *eventPtr; /* Message sent from the drag source. */
3008 {
3009 char **cmd;
3010 int resp;
3011 int x, y;
3012 int button, keyState;
3013 char *formats;
3014 struct DragInfo {
3015 int mesg; /* Drag-and-drop message type. */
3016 Window window; /* Source window. */
3017 int timestamp; /* Transaction timestamp. */
3018 int point; /* Root X-Y coordinate of pointer. */
3019 int flags; /* Button/keystate information. */
3020 } *dragPtr;
3021
3022 dragPtr = (struct DragInfo *)eventPtr->xclient.data.l;
3023
3024 cmd = NULL;
3025 switch (dragPtr->mesg) {
3026 case ST_DRAG_ENTER:
3027 cmd = dndPtr->enterCmd;
3028 break;
3029 case ST_DRAG_MOTION:
3030 cmd = dndPtr->motionCmd;
3031 break;
3032 case ST_DRAG_LEAVE:
3033 cmd = dndPtr->leaveCmd;
3034 break;
3035 }
3036 if (cmd == NULL) {
3037 return; /* Nothing to do. */
3038 }
3039 UNPACK(dragPtr->point, x, y);
3040 UNPACK(dragPtr->flags, button, keyState);
3041 formats = GetSourceFormats(dndPtr, dragPtr->window, dragPtr->timestamp);
3042 resp = InvokeCallback(dndPtr, cmd, x, y, formats, button, keyState,
3043 dragPtr->timestamp);
3044
3045 /* Target-to-Source: Drag result message. */
3046 SendClientMsg(dndPtr->display, dragPtr->window, dndPtr->dataPtr->mesgAtom,
3047 TS_DRAG_STATUS, (int)Tk_WindowId(dndPtr->tkwin), dragPtr->timestamp,
3048 resp, 0);
3049 }
3050
3051 /*
3052 * ------------------------------------------------------------------------
3053 *
3054 * DndEventProc --
3055 *
3056 * Invoked by Tk_HandleEvent whenever a DestroyNotify event is received
3057 * on a registered drag&drop source widget.
3058 *
3059 * ------------------------------------------------------------------------
3060 */
3061 static int
3062 DndEventProc(clientData, eventPtr)
3063 ClientData clientData; /* Drag&drop record. */
3064 XEvent *eventPtr; /* Event description. */
3065 {
3066 Dnd *dndPtr = clientData;
3067
3068 if (eventPtr->xany.window != Tk_WindowId(dndPtr->tkwin)) {
3069 return 0;
3070 }
3071 if (eventPtr->type == DestroyNotify) {
3072 dndPtr->tkwin = NULL;
3073 dndPtr->flags |= DND_DELETED;
3074 Tcl_EventuallyFree(dndPtr, DestroyDnd);
3075 return 0; /* Other handlers have to see this event too.*/
3076 } else if (eventPtr->type == ButtonPress) {
3077 dndPtr->keyState = eventPtr->xbutton.state;
3078 dndPtr->button = eventPtr->xbutton.button;
3079 return 0;
3080 } else if (eventPtr->type == ButtonRelease) {
3081 dndPtr->keyState = eventPtr->xbutton.state;
3082 dndPtr->button = eventPtr->xbutton.button;
3083 return 0;
3084 } else if (eventPtr->type == MotionNotify) {
3085 dndPtr->keyState = eventPtr->xmotion.state;
3086 return 0;
3087 } else if ((eventPtr->type == ClientMessage) &&
3088 (eventPtr->xclient.message_type == dndPtr->dataPtr->mesgAtom)) {
3089 int result;
3090
3091 switch((unsigned int)eventPtr->xclient.data.l[0]) {
3092 case TS_START_DROP:
3093 DoDrop(dndPtr, eventPtr);
3094 return 1;
3095
3096 case TS_DROP_RESULT:
3097 result = eventPtr->xclient.data.l[MESG_RESPONSE];
3098 dndPtr->tokenPtr->status = result;
3099 if (result == DROP_CANCEL) {
3100 CancelDrag(dndPtr);
3101 } else if (result == DROP_FAIL) {
3102 EventuallyRedrawToken(dndPtr);
3103 } else {
3104 dndPtr->tokenPtr->nSteps = 10;
3105 FadeToken(dndPtr);
3106 }
3107 if (dndPtr->resultCmd != NULL) {
3108 DropFinished(dndPtr, eventPtr);
3109 }
3110 return 1;
3111
3112 case TS_DRAG_STATUS:
3113 result = eventPtr->xclient.data.l[MESG_RESPONSE];
3114 ChangeToken(dndPtr, result);
3115 return 1;
3116
3117 case ST_DROP:
3118 HandleDropEvent(dndPtr, eventPtr);
3119 return 1;
3120
3121 case ST_DRAG_ENTER:
3122 case ST_DRAG_MOTION:
3123 case ST_DRAG_LEAVE:
3124 HandleDragEvent(dndPtr, eventPtr);
3125 return 1;
3126 }
3127 }
3128 return 0;
3129 }
3130
3131 static void
3132 SendPointerMessage(dndPtr, eventType, windowPtr, x, y)
3133 Dnd *dndPtr; /* Source drag&drop manager. */
3134 int eventType; /* Type of event to relay. */
3135 Winfo *windowPtr; /* Generic window information. */
3136 int x, y; /* Root coordinates of mouse. */
3137 {
3138 /* Source-to-Target: Pointer event messages. */
3139 SendClientMsg(
3140 dndPtr->display, /* Display of recipient window. */
3141 windowPtr->window, /* Recipient window. */
3142 dndPtr->dataPtr->mesgAtom, /* Message type. */
3143 eventType, /* Data 1 */
3144 (int)Tk_WindowId(dndPtr->tkwin), /* Data 2 */
3145 dndPtr->timestamp, /* Data 3 */
3146 PACK(x, y), /* Data 4 */
3147 PACK(dndPtr->button, dndPtr->keyState)); /* Data 5 */
3148 /* Don't wait the response. */
3149 }
3150
3151 static void
3152 RelayEnterEvent(dndPtr, windowPtr, x, y)
3153 Dnd *dndPtr;
3154 Winfo *windowPtr;
3155 int x, y;
3156 {
3157 if ((windowPtr != NULL) && (windowPtr->eventFlags & WATCH_ENTER)) {
3158 SendPointerMessage(dndPtr, ST_DRAG_ENTER, windowPtr, x, y);
3159 }
3160 }
3161
3162 static void
3163 RelayLeaveEvent(dndPtr, windowPtr, x, y)
3164 Dnd *dndPtr;
3165 Winfo *windowPtr;
3166 int x, y;
3167 {
3168 if ((windowPtr != NULL) && (windowPtr->eventFlags & WATCH_LEAVE)) {
3169 SendPointerMessage(dndPtr, ST_DRAG_LEAVE, windowPtr, x, y);
3170 }
3171 }
3172
3173 static void
3174 RelayMotionEvent(dndPtr, windowPtr, x, y)
3175 Dnd *dndPtr;
3176 Winfo *windowPtr;
3177 int x, y;
3178 {
3179 if ((windowPtr != NULL) && (windowPtr->eventFlags & WATCH_MOTION)) {
3180 SendPointerMessage(dndPtr, ST_DRAG_MOTION, windowPtr, x, y);
3181 }
3182 }
3183
3184 static void
3185 RelayDropEvent(dndPtr, windowPtr, x, y)
3186 Dnd *dndPtr;
3187 Winfo *windowPtr;
3188 int x, y;
3189 {
3190 SendPointerMessage(dndPtr, ST_DROP, windowPtr, x, y);
3191 }
3192
3193 /*
3194 * ------------------------------------------------------------------------
3195 *
3196 * FreeWinfo --
3197 *
3198 * ------------------------------------------------------------------------
3199 */
3200 static void
3201 FreeWinfo(windowPtr)
3202 Winfo *windowPtr; /* window rep to be freed */
3203 {
3204 Winfo *childPtr;
3205 Blt_ChainLink *linkPtr;
3206
3207 for (linkPtr = Blt_ChainFirstLink(windowPtr->chainPtr); linkPtr != NULL;
3208 linkPtr = Blt_ChainNextLink(linkPtr)) {
3209 childPtr = Blt_ChainGetValue(linkPtr);
3210 FreeWinfo(childPtr); /* Recursively free children. */
3211 }
3212 if (windowPtr->matches != NULL) {
3213 Blt_Free(windowPtr->matches);
3214 }
3215 Blt_ChainDestroy(windowPtr->chainPtr);
3216 Blt_Free(windowPtr);
3217 }
3218
3219 /*
3220 * ------------------------------------------------------------------------
3221 *
3222 * GetWinfo --
3223 *
3224 * Invoked during "drag" operations. Digs into the root window
3225 * hierarchy and caches the window-related information.
3226 * If the current point lies over an uninitialized window (i.e.
3227 * one that already has an allocated Winfo structure, but has
3228 * not been filled in yet), this routine is called to query
3229 * window coordinates. If the window has any children, more
3230 * uninitialized Winfo structures are allocated. Further queries
3231 * will cause these structures to be initialized in turn.
3232 *
3233 * ------------------------------------------------------------------------
3234 */
3235 static void
3236 GetWinfo(display, windowPtr)
3237 Display *display;
3238 Winfo *windowPtr; /* window rep to be initialized */
3239 {
3240 int visible;
3241
3242 if (windowPtr->initialized) {
3243 return;
3244 }
3245 /* Query for the window coordinates. */
3246 visible = GetWindowRegion(display, windowPtr);
3247 if (visible) {
3248 Blt_ChainLink *linkPtr;
3249 Blt_Chain *chainPtr;
3250 Winfo *childPtr;
3251
3252 /* Add offset from parent's origin to coordinates */
3253 if (windowPtr->parentPtr != NULL) {
3254 windowPtr->x1 += windowPtr->parentPtr->x1;
3255 windowPtr->y1 += windowPtr->parentPtr->y1;
3256 windowPtr->x2 += windowPtr->parentPtr->x1;
3257 windowPtr->y2 += windowPtr->parentPtr->y1;
3258 }
3259 /*
3260 * Collect a list of child windows, sorted in z-order. The
3261 * topmost window will be first in the list.
3262 */
3263 chainPtr = GetWindowZOrder(display, windowPtr->window);
3264
3265 /* Add and initialize extra slots if needed. */
3266 for (linkPtr = Blt_ChainFirstLink(chainPtr); linkPtr != NULL;
3267 linkPtr = Blt_ChainNextLink(linkPtr)) {
3268 childPtr = Blt_Calloc(1, sizeof(Winfo));
3269 assert(childPtr);
3270 childPtr->initialized = FALSE;
3271 childPtr->window = (Window)Blt_ChainGetValue(linkPtr);
3272 childPtr->parentPtr = windowPtr;
3273 Blt_ChainSetValue(linkPtr, childPtr);
3274 }
3275 windowPtr->chainPtr = chainPtr;
3276 } else {
3277 /* If it's not viewable don't bother doing anything else. */
3278 windowPtr->x1 = windowPtr->y1 = windowPtr->x2 = windowPtr->y2 = -1;
3279 windowPtr->chainPtr = NULL;
3280 }
3281 windowPtr->initialized = TRUE;
3282 }
3283
3284 /*
3285 * ------------------------------------------------------------------------
3286 *
3287 * InitRoot --
3288 *
3289 * Invoked at the start of a "drag" operation to capture the
3290 * positions of all windows on the current root. Queries the
3291 * entire window hierarchy and determines the placement of each
3292 * window. Queries the "BltDndTarget" property info where
3293 * appropriate. This information is used during the drag
3294 * operation to determine when the drag&drop token is over a
3295 * valid drag&drop target.
3296 *
3297 * Results:
3298 * Returns the record for the root window, which contains records
3299 * for all other windows as children.
3300 *
3301 * ------------------------------------------------------------------------
3302 */
3303 static Winfo *
3304 InitRoot(dndPtr)
3305 Dnd *dndPtr;
3306 {
3307 Winfo *rootPtr;
3308
3309 rootPtr = Blt_Calloc(1, sizeof(Winfo));
3310 assert(rootPtr);
3311 rootPtr->window = DefaultRootWindow(dndPtr->display);
3312 dndPtr->windowPtr = NULL;
3313 GetWinfo(dndPtr->display, rootPtr);
3314 return rootPtr;
3315 }
3316
3317
3318 static int
3319 ParseProperty(interp, dndPtr, windowPtr, data)
3320 Tcl_Interp *interp;
3321 Dnd *dndPtr;
3322 Winfo *windowPtr;
3323 char *data;
3324 {
3325 int nElems;
3326 char **elemArr;
3327 int eventFlags;
3328 Tcl_DString dString;
3329 int count;
3330 register int i;
3331
3332 if (Tcl_SplitList(interp, data, &nElems, &elemArr) != TCL_OK) {
3333 return TCL_ERROR; /* Malformed property list. */
3334 }
3335 if (nElems < 1) {
3336 Tcl_AppendResult(interp, "Malformed property \"", data, "\"",
3337 (char *)NULL);
3338 goto error;
3339 }
3340 if (Tcl_GetInt(interp, elemArr[PROP_WATCH_FLAGS], &eventFlags) != TCL_OK) {
3341 goto error;
3342 }
3343
3344 /* target flags, type1, type2, ... */
3345 /*
3346 * The target property contains a list of possible formats.
3347 * Compare this with what formats the source is willing to
3348 * convert and compress the list down to just the matching
3349 * formats. It's up to the target to request the specific
3350 * type (or types) that it wants.
3351 */
3352 count = 0;
3353 Tcl_DStringInit(&dString);
3354 if (dndPtr->reqFormats == NULL) {
3355 Blt_HashEntry *hPtr;
3356 Blt_HashSearch cursor;
3357 char *fmt;
3358
3359 for (i = 1; i < nElems; i++) {
3360 for(hPtr = Blt_FirstHashEntry(&(dndPtr->getDataTable), &cursor);
3361 hPtr != NULL; hPtr = Blt_NextHashEntry(&cursor)) {
3362 fmt = Blt_GetHashKey(&(dndPtr->getDataTable), hPtr);
3363 if ((*fmt == elemArr[i][0]) && (strcmp(fmt, elemArr[i]) == 0)) {
3364 Tcl_DStringAppendElement(&dString, elemArr[i]);
3365 count++;
3366 break;
3367 }
3368 }
3369 }
3370 } else {
3371 register char **s;
3372
3373 for (i = 1; i < nElems; i++) {
3374 for (s = dndPtr->reqFormats; *s != NULL; s++) {
3375 if ((**s == elemArr[i][0]) && (strcmp(*s, elemArr[i]) == 0)) {
3376 Tcl_DStringAppendElement(&dString, elemArr[i]);
3377 count++;
3378 }
3379 }
3380 }
3381 }
3382 if (count == 0) {
3383 #ifdef notdef
3384 fprintf(stderr, "source/target mismatch: No matching types\n");
3385 #endif
3386 return TCL_BREAK;
3387 }
3388 if (eventFlags != 0) {
3389 SetProperty(dndPtr->tkwin, dndPtr->dataPtr->formatsAtom,
3390 Tcl_DStringValue(&dString));
3391 windowPtr->matches = NULL;
3392 } else {
3393 windowPtr->matches = Blt_Strdup(Tcl_DStringValue(&dString));
3394 }
3395 Tcl_DStringFree(&dString);
3396 windowPtr->eventFlags = eventFlags;
3397 return TCL_OK;
3398 error:
3399 Blt_Free(elemArr);
3400 return TCL_ERROR;
3401 }
3402
3403 /*
3404 * ------------------------------------------------------------------------
3405 *
3406 * OverTarget --
3407 *
3408 * Checks to see if a compatible drag&drop target exists at the
3409 * given position. A target is "compatible" if it is a drag&drop
3410 * window, and if it has a handler that is compatible with the
3411 * current source window.
3412 *
3413 * Results:
3414 * Returns a pointer to a structure describing the target, or NULL
3415 * if no compatible target is found.
3416 *
3417 * ------------------------------------------------------------------------
3418 */
3419 static Winfo *
3420 OverTarget(dndPtr)
3421 Dnd *dndPtr; /* drag&drop source window */
3422 {
3423 Tcl_Interp *interp = dndPtr->interp;
3424 int x, y;
3425 int vx, vy;
3426 int dummy;
3427 Winfo *windowPtr;
3428
3429 /*
3430 * If no window info has been been gathered yet for this target,
3431 * then abort this call. This probably means that the token is
3432 * moved before it has been properly built.
3433 */
3434 if (dndPtr->rootPtr == NULL) {
3435 fprintf(stderr, "rootPtr not initialized\n");
3436 return NULL;
3437 }
3438 /* Adjust current location for virtual root windows. */
3439 Tk_GetVRootGeometry(dndPtr->tkwin, &vx, &vy, &dummy, &dummy);
3440 x = dndPtr->x + vx;
3441 y = dndPtr->y + vy;
3442
3443 windowPtr = FindTopWindow(dndPtr, x, y);
3444 if (windowPtr == NULL) {
3445 return NULL; /* Not over a window. */
3446 }
3447 if ((!dndPtr->selfTarget) &&
3448 (Tk_WindowId(dndPtr->tkwin) == windowPtr->window)) {
3449 return NULL; /* If the self-target flag isn't set,
3450 * don't allow the source window to
3451 * drop onto itself. */
3452 }
3453 if (!windowPtr->lookedForProperty) {
3454 char *data;
3455 int result;
3456
3457 windowPtr->lookedForProperty = TRUE;
3458 /* See if this window has a "BltDndTarget" property. */
3459 data = GetProperty(dndPtr->display, windowPtr->window,
3460 dndPtr->dataPtr->targetAtom);
3461 if (data == NULL) {
3462 #ifdef notdef
3463 fprintf(stderr, "No property on 0x%x\n", windowPtr->window);
3464 #endif
3465 return NULL; /* No such property on window. */
3466 }
3467 result = ParseProperty(interp, dndPtr, windowPtr, data);
3468 XFree(data);
3469 if (result == TCL_BREAK) {
3470 #ifdef notdef
3471 fprintf(stderr, "No matching formats\n");
3472 #endif
3473 return NULL;
3474 }
3475 if (result != TCL_OK) {
3476 Tcl_BackgroundError(interp);
3477 return NULL; /* Malformed property list. */
3478 }
3479 windowPtr->isTarget = TRUE;
3480 }
3481 if (!windowPtr->isTarget) {
3482 return NULL;
3483 }
3484 return windowPtr;
3485
3486 }
3487
3488 /*
3489 * ------------------------------------------------------------------------
3490 *
3491 * AddTargetProperty --
3492 *
3493 * Attaches a drag&drop property to the given target window.
3494 * This property allows us to recognize the window later as a
3495 * valid target. It also stores important information including
3496 * the interpreter managing the target and the pathname of the
3497 * target window. Usually this routine is called when the target
3498 * is first registered or first exposed (so that the X-window
3499 * really exists).
3500 *
3501 * ------------------------------------------------------------------------
3502 */
3503 static void
3504 AddTargetProperty(dndPtr)
3505 Dnd *dndPtr; /* drag&drop target window data */
3506 {
3507 Tcl_DString dString;
3508 Blt_HashEntry *hPtr;
3509 unsigned int eventFlags;
3510 Blt_HashSearch cursor;
3511 char *fmt;
3512 char string[200];
3513
3514 Tcl_DStringInit(&dString);
3515 /*
3516 * Each target window's dnd property contains
3517 *
3518 * 1. Mouse event flags.
3519 * 2. List of all the data types that can be handled. If none
3520 * are listed, then all can be handled.
3521 */
3522 eventFlags = 0;
3523 if (dndPtr->enterCmd != NULL) {
3524 eventFlags |= WATCH_ENTER;
3525 }
3526 if (dndPtr->leaveCmd != NULL) {
3527 eventFlags |= WATCH_LEAVE;
3528 }
3529 if (dndPtr->motionCmd != NULL) {
3530 eventFlags |= WATCH_MOTION;
3531 }
3532 sprintf(string, "0x%x", eventFlags);
3533 Tcl_DStringAppendElement(&dString, string);
3534 for (hPtr = Blt_FirstHashEntry(&(dndPtr->setDataTable), &cursor);
3535 hPtr != NULL; hPtr = Blt_NextHashEntry(&cursor)) {
3536 fmt = Blt_GetHashKey(&(dndPtr->setDataTable), hPtr);
3537 Tcl_DStringAppendElement(&dString, fmt);
3538 }
3539 SetProperty(dndPtr->tkwin, dndPtr->dataPtr->targetAtom,
3540 Tcl_DStringValue(&dString));
3541 dndPtr->targetPropertyExists = TRUE;
3542 Tcl_DStringFree(&dString);
3543 }
3544
3545 static void
3546 CancelDrag(dndPtr)
3547 Dnd *dndPtr;
3548 {
3549 if (dndPtr->flags & DND_INITIATED) {
3550 dndPtr->tokenPtr->nSteps = 10;
3551 SnapToken(dndPtr);
3552 StopActiveCursor(dndPtr);
3553 if (dndPtr->cursor == None) {
3554 Tk_UndefineCursor(dndPtr->tkwin);
3555 } else {
3556 Tk_DefineCursor(dndPtr->tkwin, dndPtr->cursor);
3557 }
3558 }
3559 if (dndPtr->rootPtr != NULL) {
3560 FreeWinfo(dndPtr->rootPtr);
3561 dndPtr->rootPtr = NULL;
3562 }
3563 }
3564
3565 static int
3566 DragInit(dndPtr, x, y)
3567 Dnd *dndPtr;
3568 int x, y;
3569 {
3570 Token *tokenPtr = dndPtr->tokenPtr;
3571 int result;
3572 Winfo *newPtr;
3573
3574 assert((dndPtr->flags & DND_ACTIVE) == DND_SELECTED);
3575
3576 if (dndPtr->rootPtr != NULL) {
3577 FreeWinfo(dndPtr->rootPtr);
3578 }
3579 dndPtr->rootPtr = InitRoot(dndPtr); /* Reset information cache. */
3580 dndPtr->flags &= ~DND_VOIDED;
3581
3582 dndPtr->x = x; /* Save current location. */
3583 dndPtr->y = y;
3584 result = TRUE;
3585 Tcl_Preserve(dndPtr);
3586 if (dndPtr->packageCmd != NULL) {
3587 Tcl_DString dString, savedResult;
3588 Tcl_Interp *interp = dndPtr->interp;
3589 int status;
3590 char **p;
3591 int rx, ry;
3592
3593 Tcl_DStringInit(&dString);
3594 for (p = dndPtr->packageCmd; *p != NULL; p++) {
3595 Tcl_DStringAppendElement(&dString, *p);
3596 }
3597 Tcl_DStringAppendElement(&dString, Tk_PathName(dndPtr->tkwin));
3598 rx = dndPtr->dragX - Blt_RootX(dndPtr->tkwin);
3599 ry = dndPtr->dragY - Blt_RootY(dndPtr->tkwin);
3600 Tcl_DStringAppendElement(&dString, "x");
3601 Tcl_DStringAppendElement(&dString, Blt_Itoa(rx));
3602 Tcl_DStringAppendElement(&dString, "y");
3603 Tcl_DStringAppendElement(&dString, Blt_Itoa(ry));
3604 Tcl_DStringAppendElement(&dString, "button");
3605 Tcl_DStringAppendElement(&dString, Blt_Itoa(dndPtr->button));
3606 Tcl_DStringAppendElement(&dString, "state");
3607 Tcl_DStringAppendElement(&dString, Blt_Itoa(dndPtr->keyState));
3608 Tcl_DStringAppendElement(&dString, "timestamp");
3609 Tcl_DStringAppendElement(&dString, Blt_Utoa(dndPtr->timestamp));
3610 Tcl_DStringAppendElement(&dString, "token");
3611 Tcl_DStringAppendElement(&dString, Tk_PathName(tokenPtr->tkwin));
3612
3613 Tcl_DStringInit(&savedResult);
3614 Tcl_DStringGetResult(interp, &savedResult);
3615 dndPtr->flags |= DND_IN_PACKAGE;
3616 status = Tcl_GlobalEval(interp, Tcl_DStringValue(&dString));
3617 dndPtr->flags &= ~DND_IN_PACKAGE;
3618 if (status == TCL_OK) {
3619 result = GetDragResult(interp, Tcl_GetStringResult(interp));
3620 } else {
3621 Tcl_BackgroundError(interp);
3622 }
3623 Tcl_DStringFree(&dString);
3624 Tcl_DStringResult(interp, &savedResult);
3625 Tcl_DStringFree(&dString);
3626 if (status != TCL_OK) {
3627 HideToken(dndPtr);
3628 Tcl_Release(dndPtr);
3629 return TCL_ERROR;
3630 }
3631 }
3632 if (dndPtr->flags & DND_VOIDED) {
3633 HideToken(dndPtr);
3634 Tcl_Release(dndPtr);
3635 return TCL_RETURN;
3636 }
3637 if ((!result) || (dndPtr->flags & DND_DELETED)) {
3638 HideToken(dndPtr);
3639 Tcl_Release(dndPtr);
3640 return TCL_RETURN;
3641 }
3642 Tcl_Release(dndPtr);
3643
3644 if (dndPtr->cursor != None) {
3645 Tk_Cursor cursor;
3646
3647 /* Save the old cursor */
3648 cursor = GetWidgetCursor(dndPtr->interp, dndPtr->tkwin);
3649 if (dndPtr->cursor != None) {
3650 Tk_FreeCursor(dndPtr->display, dndPtr->cursor);
3651 }
3652 dndPtr->cursor = cursor;
3653 if (dndPtr->cursors != NULL) {
3654 /* Temporarily install the drag-and-drop cursor. */
3655 Tk_DefineCursor(dndPtr->tkwin, dndPtr->cursors[0]);
3656 }
3657 }
3658 if (Tk_WindowId(tokenPtr->tkwin) == None) {
3659 Tk_MakeWindowExist(tokenPtr->tkwin);
3660 }
3661 if (!Tk_IsMapped(tokenPtr->tkwin)) {
3662 Tk_MapWindow(tokenPtr->tkwin);
3663 }
3664 dndPtr->flags |= DND_INITIATED;
3665 newPtr = OverTarget(dndPtr);
3666 RelayEnterEvent(dndPtr, newPtr, x, y);
3667 dndPtr->windowPtr = newPtr;
3668 tokenPtr->status = (newPtr != NULL) ? DROP_OK : DROP_CONTINUE;
3669 if (tokenPtr->lastStatus != tokenPtr->status) {
3670 EventuallyRedrawToken(dndPtr);
3671 }
3672 MoveToken(dndPtr); /* Move token to current drag point. */
3673 RaiseToken(dndPtr);
3674 return TCL_OK;
3675 }
3676
3677 /*
3678 * ------------------------------------------------------------------------
3679 *
3680 * CancelOp --
3681 *
3682 * Cancels the current drag&drop operation for the source. Calling
3683 * this operation does not affect the transfer of data from the
3684 * source to the target, once the drop has been made. From the
3685 * source's point of view, the drag&drop operation is already over.
3686 *
3687 * Example: dnd cancel .widget
3688 *
3689 * Results:
3690 * A standard Tcl result.
3691 *
3692 * Side Effects:
3693 * Hides the token and sets a flag indicating that further "drag"
3694 * and "drop" operations should be ignored.
3695 *
3696 * ------------------------------------------------------------------------
3697 */
3698 /*ARGSUSED*/
3699 static int
3700 CancelOp(clientData, interp, argc, argv)
3701 ClientData clientData; /* Thread-specific data. */
3702 Tcl_Interp *interp;
3703 int argc; /* Not used. */
3704 char **argv;
3705 {
3706 Dnd *dndPtr;
3707
3708 if (GetDnd(clientData, interp, argv[2], &dndPtr) != TCL_OK) {
3709 return TCL_ERROR;
3710 }
3711 if (!dndPtr->isSource) {
3712 Tcl_AppendResult(interp, "widget \"", Tk_PathName(dndPtr->tkwin),
3713 "\" is not a registered drag&drop source.", (char *)NULL);
3714 return TCL_ERROR;
3715 }
3716 /* Send the target a Leave message so it can change back. */
3717 RelayLeaveEvent(dndPtr, dndPtr->windowPtr, 0, 0);
3718 CancelDrag(dndPtr);
3719 return TCL_OK;
3720 }
3721
3722 /*
3723 * ------------------------------------------------------------------------
3724 *
3725 * CgetOp --
3726 *
3727 * Called to process an (argc,argv) list to configure (or
3728 * reconfigure) a drag&drop widget.
3729 *
3730 * ------------------------------------------------------------------------
3731 */
3732 /* ARGSUSED*/
3733 static int
3734 CgetOp(clientData, interp, argc, argv)
3735 ClientData clientData; /* Thread-specific data. */
3736 Tcl_Interp *interp;
3737 int argc; /* Not used. */
3738 char **argv;
3739 {
3740 Dnd *dndPtr;
3741
3742 if (GetDnd(clientData, interp, argv[2], &dndPtr) != TCL_OK) {
3743 return TCL_ERROR;
3744 }
3745 return Tk_ConfigureValue(interp, dndPtr->tkwin, configSpecs, (char *)dndPtr,
3746 argv[3], 0);
3747 }
3748
3749 /*
3750 * ------------------------------------------------------------------------
3751 *
3752 * ConfigureOp --
3753 *
3754 * Called to process an (argc,argv) list to configure (or
3755 * reconfigure) a drag&drop widget.
3756 *
3757 * ------------------------------------------------------------------------
3758 */
3759 static int
3760 ConfigureOp(clientData, interp, argc, argv)
3761 ClientData clientData; /* Thread-specific data. */
3762 Tcl_Interp *interp; /* current interpreter */
3763 int argc; /* number of arguments */
3764 char **argv; /* argument strings */
3765 {
3766 Dnd *dndPtr;
3767 int flags;
3768
3769 if (GetDnd(clientData, interp, argv[2], &dndPtr) != TCL_OK) {
3770 return TCL_ERROR;
3771 }
3772 flags = TK_CONFIG_ARGV_ONLY;
3773 if (argc == 3) {
3774 return Tk_ConfigureInfo(interp, dndPtr->tkwin, configSpecs,
3775 (char *)dndPtr, (char *)NULL, flags);
3776 } else if (argc == 4) {
3777 return Tk_ConfigureInfo(interp, dndPtr->tkwin, configSpecs,
3778 (char *)dndPtr, argv[3], flags);
3779 }
3780 if (Blt_ConfigureWidget(interp, dndPtr->tkwin, configSpecs, argc - 3,
3781 argv + 3, (char *)dndPtr, flags) != TCL_OK) {
3782 return TCL_ERROR;
3783 }
3784 if (ConfigureDnd(interp, dndPtr) != TCL_OK) {
3785 return TCL_ERROR;
3786 }
3787 return TCL_OK;
3788 }
3789
3790 /*
3791 * ------------------------------------------------------------------------
3792 *
3793 * DeleteOp --
3794 *
3795 * Deletes the drag&drop manager from the widget. If a "-source"
3796 * or "-target" switch is present, only that component of the
3797 * drag&drop manager is shutdown. The manager is not deleted
3798 * unless both the target and source components are shutdown.
3799 *
3800 * Example: dnd delete .widget
3801 *
3802 * Results:
3803 * A standard Tcl result.
3804 *
3805 * Side Effects:
3806 * Deletes the drag&drop manager. Also the source and target window
3807 * properties are removed from the widget.
3808 *
3809 * ------------------------------------------------------------------------
3810 */
3811 static int
3812 DeleteOp(clientData, interp, argc, argv)
3813 ClientData clientData; /* Thread-specific data. */
3814 Tcl_Interp *interp;
3815 int argc;
3816 char **argv;
3817 {
3818 Dnd *dndPtr;
3819 register int i;
3820
3821 for(i = 3; i < argc; i++) {
3822 if (GetDnd(clientData, interp, argv[i], &dndPtr) != TCL_OK) {
3823 return TCL_ERROR;
3824 }
3825 dndPtr->flags |= DND_DELETED;
3826 Tcl_EventuallyFree(dndPtr, DestroyDnd);
3827 }
3828 return TCL_OK;
3829 }
3830
3831 /*
3832 * ------------------------------------------------------------------------
3833 *
3834 * SelectOp --
3835 *
3836 * Initializes a drag&drop transaction. Typically this operation
3837 * is called from a ButtonPress event on a source widget. The
3838 * window information cache is initialized, and the token is
3839 * initialized and displayed.
3840 *
3841 * Example: dnd pickup .widget x y
3842 *
3843 * Results:
3844 * A standard Tcl result.
3845 *
3846 * Side Effects:
3847 * The token is initialized and displayed. This may require invoking
3848 * a user-defined package command. The window information cache is
3849 * also initialized.
3850 *
3851 * ------------------------------------------------------------------------
3852 */
3853 /*ARGSUSED*/
3854 static int
3855 SelectOp(clientData, interp, argc, argv)
3856 ClientData clientData; /* Thread-specific data. */
3857 Tcl_Interp *interp;
3858 int argc; /* Not used. */
3859 char **argv;
3860 {
3861 Dnd *dndPtr;
3862 int x, y, timestamp;
3863 Token *tokenPtr;
3864
3865 if (GetDnd(clientData, interp, argv[2], &dndPtr) != TCL_OK) {
3866 return TCL_ERROR;
3867 }
3868 if (!dndPtr->isSource) {
3869 Tcl_AppendResult(interp, "widget \"", Tk_PathName(dndPtr->tkwin),
3870 "\" is not a registered drag&drop source.", (char *)NULL);
3871 return TCL_ERROR;
3872 }
3873 tokenPtr = dndPtr->tokenPtr;
3874 if (tokenPtr == NULL) {
3875 Tcl_AppendResult(interp, "no drag&drop token created for \"",
3876 argv[2], "\"", (char *)NULL);
3877 return TCL_ERROR;
3878 }
3879 if ((Tcl_GetInt(interp, argv[3], &x) != TCL_OK) ||
3880 (Tcl_GetInt(interp, argv[4], &y) != TCL_OK)) {
3881 return TCL_ERROR;
3882 }
3883 if (Tcl_GetInt(interp, argv[5], &timestamp) != TCL_OK) {
3884 return TCL_ERROR;
3885 }
3886 if (dndPtr->flags & (DND_IN_PACKAGE | DND_ACTIVE | DND_VOIDED)) {
3887 return TCL_OK;
3888 }
3889
3890 if (tokenPtr->timerToken != NULL) {
3891 HideToken(dndPtr); /* If the user selected again before the
3892 * token snap/melt has completed, first
3893 * disable the token timer callback. */
3894 }
3895 /* At this point, simply save the starting pointer location. */
3896 dndPtr->dragX = x, dndPtr->dragY = y;
3897 GetTokenPosition(dndPtr, x, y);
3898 tokenPtr->startX = tokenPtr->x;
3899 tokenPtr->startY = tokenPtr->y;
3900 dndPtr->timestamp = timestamp;
3901 dndPtr->flags |= DND_SELECTED;
3902
3903 if (dndPtr->dragStart == 0) {
3904 if (DragInit(dndPtr, x, y) == TCL_ERROR) {
3905 return TCL_ERROR;
3906 }
3907 }
3908 return TCL_OK;
3909 }
3910
3911 /*
3912 * ------------------------------------------------------------------------
3913 *
3914 * DragOp --
3915 *
3916 * Continues the drag&drop transaction. Typically this operation
3917 * is called from a button Motion event on a source widget. Pointer
3918 * event messages are possibly sent to the target, indicating Enter,
3919 * Leave, and Motion events.
3920 *
3921 * Example: dnd drag .widget x y
3922 *
3923 * Results:
3924 * A standard Tcl result.
3925 *
3926 * Side Effects:
3927 * Pointer events are relayed to the target (if the mouse is over
3928 * one).
3929 *
3930 * ------------------------------------------------------------------------
3931 */
3932 /*ARGSUSED*/
3933 static int
3934 DragOp(clientData, interp, argc, argv)
3935 ClientData clientData; /* Thread-specific data. */
3936 Tcl_Interp *interp;
3937 int argc; /* Not used. */
3938 char **argv;
3939 {
3940 Winfo *newPtr, *oldPtr;
3941 Dnd *dndPtr;
3942 int x, y;
3943
3944 if (GetDnd(clientData, interp, argv[2], &dndPtr) != TCL_OK) {
3945 return TCL_ERROR;
3946 }
3947 if (!dndPtr->isSource) {
3948 Tcl_AppendResult(interp, "widget \"", Tk_PathName(dndPtr->tkwin),
3949 "\" is not a registered drag&drop source.", (char *)NULL);
3950 return TCL_ERROR;
3951 }
3952 if (dndPtr->tokenPtr == NULL) {
3953 Tcl_AppendResult(interp, "no drag&drop token created for \"",
3954 argv[2], "\"", (char *)NULL);
3955 return TCL_ERROR;
3956 }
3957 if ((Tcl_GetInt(interp, argv[3], &x) != TCL_OK) ||
3958 (Tcl_GetInt(interp, argv[4], &y) != TCL_OK)) {
3959 return TCL_ERROR;
3960 }
3961
3962 if ((dndPtr->flags & DND_SELECTED) == 0) {
3963 return TCL_OK; /* Re-entered this routine. */
3964 }
3965 /*
3966 * The following code gets tricky because the package command may
3967 * call "update" or "tkwait". A motion event may then trigger
3968 * this routine, before the token has been initialized. Until the
3969 * package command finishes, no target messages are sent and drops
3970 * are silently ignored. Note that we do still track mouse
3971 * movements, so that when the package command completes, it will
3972 * have the latest pointer position.
3973 */
3974 dndPtr->x = x; /* Save current location. */
3975 dndPtr->y = y;
3976
3977 if (dndPtr->flags & DND_IN_PACKAGE) {
3978 return TCL_OK; /* Re-entered this routine. */
3979 }
3980 if ((dndPtr->flags & DND_INITIATED) == 0) {
3981 int dx, dy;
3982 int result;
3983
3984 dx = dndPtr->dragX - x;
3985 dy = dndPtr->dragY - y;
3986 if ((ABS(dx) < dndPtr->dragStart) && (ABS(dy) < dndPtr->dragStart)) {
3987 return TCL_OK;
3988 }
3989 result = DragInit(dndPtr, x, y);
3990 if (result == TCL_ERROR) {
3991 return TCL_ERROR;
3992 }
3993 if (result == TCL_RETURN) {
3994 return TCL_OK;
3995 }
3996 }
3997 if (dndPtr->flags & DND_VOIDED) {
3998 return TCL_OK;
3999 }
4000 oldPtr = dndPtr->windowPtr;
4001 newPtr = OverTarget(dndPtr);
4002 if (newPtr == oldPtr) {
4003 RelayMotionEvent(dndPtr, oldPtr, x, y);
4004 dndPtr->windowPtr = oldPtr;
4005 } else {
4006 RelayLeaveEvent(dndPtr, oldPtr, x, y);
4007 RelayEnterEvent(dndPtr, newPtr, x, y);
4008 dndPtr->windowPtr = newPtr;
4009 }
4010 dndPtr->tokenPtr->status = (newPtr != NULL) ? DROP_OK : DROP_CONTINUE;
4011 if (dndPtr->tokenPtr->lastStatus != dndPtr->tokenPtr->status) {
4012 EventuallyRedrawToken(dndPtr);
4013 }
4014 MoveToken(dndPtr); /* Move token to current drag point. */
4015 RaiseToken(dndPtr);
4016 return TCL_OK;
4017 }
4018
4019
4020 /*
4021 * ------------------------------------------------------------------------
4022 *
4023 * DropOp --
4024 *
4025 * Finishes the drag&drop transaction by dropping the data on the
4026 * selected target. Typically this operation is called from a
4027 * ButtonRelease event on a source widget. Note that a Leave message
4028 * is always sent to the target so that is can un-highlight itself.
4029 * The token is hidden and a drop message is sent to the target.
4030 *
4031 * Example: dnd drop .widget x y
4032 *
4033 * Results:
4034 * A standard Tcl result.
4035 *
4036 * Side Effects:
4037 * The token is hidden and a drop message is sent to the target.
4038 *
4039 * ------------------------------------------------------------------------
4040 */
4041 /*ARGSUSED*/
4042 static int
4043 DropOp(clientData, interp, argc, argv)
4044 ClientData clientData; /* Thread-specific data. */
4045 Tcl_Interp *interp;
4046 int argc; /* Not used. */
4047 char **argv;
4048 {
4049 Winfo *windowPtr;
4050 Dnd *dndPtr;
4051 int x, y;
4052
4053 if (GetDnd(clientData, interp, argv[2], &dndPtr) != TCL_OK) {
4054 return TCL_ERROR;
4055 }
4056 if (!dndPtr->isSource) {
4057 Tcl_AppendResult(interp, "widget \"", Tk_PathName(dndPtr->tkwin),
4058 "\" is not a registered drag&drop source.", (char *)NULL);
4059 return TCL_ERROR;
4060 }
4061 if ((Tcl_GetInt(interp, argv[3], &x) != TCL_OK) ||
4062 (Tcl_GetInt(interp, argv[4], &y) != TCL_OK)) {
4063 return TCL_ERROR;
4064 }
4065 dndPtr->x = x; /* Save drag&drop location */
4066 dndPtr->y = y;
4067 if ((dndPtr->flags & DND_INITIATED) == 0) {
4068 return TCL_OK; /* Never initiated any drag operation. */
4069 }
4070 if (dndPtr->flags & DND_VOIDED) {
4071 HideToken(dndPtr);
4072 return TCL_OK;
4073 }
4074 windowPtr = OverTarget(dndPtr);
4075 if (windowPtr != NULL) {
4076 if (windowPtr->matches != NULL) {
4077 SetProperty(dndPtr->tkwin, dndPtr->dataPtr->formatsAtom,
4078 windowPtr->matches);
4079 }
4080 MoveToken(dndPtr); /* Move token to current drag point. */
4081 RaiseToken(dndPtr);
4082 RelayDropEvent(dndPtr, windowPtr, x, y);
4083 #ifdef notdef
4084 tokenPtr->nSteps = 10;
4085 FadeToken(dndPtr);
4086 #endif
4087 } else {
4088 CancelDrag(dndPtr);
4089 }
4090 return TCL_OK;
4091 }
4092
4093 /*
4094 * ------------------------------------------------------------------------
4095 *
4096 * GetdataOp --
4097 *
4098 * Registers one or more data formats with a drag&drop source.
4099 * Each format has a Tcl command associated with it. This command
4100 * is automatically invoked whenever data is pulled from the source
4101 * to a target requesting the data in that particular format. The
4102 * purpose of the Tcl command is to get the data from in the
4103 * application. When the Tcl command is invoked, special percent
4104 * substitutions are made:
4105 *
4106 * %# Drag&drop transaction timestamp.
4107 * %W Source widget.
4108 *
4109 * If a converter (command) already exists for a format, it
4110 * overwrites the existing command.
4111 *
4112 * Example: dnd getdata .widget "color" { %W cget -bg }
4113 *
4114 * Results:
4115 * A standard Tcl result.
4116 *
4117 * ------------------------------------------------------------------------
4118 */
4119 static int
4120 GetdataOp(clientData, interp, argc, argv)
4121 ClientData clientData; /* Thread-specific data. */
4122 Tcl_Interp *interp;
4123 int argc;
4124 char **argv;
4125 {
4126 Dnd *dndPtr;
4127 Blt_HashEntry *hPtr;
4128 Blt_HashSearch cursor;
4129 int isNew, nElem;
4130 char **cmd;
4131 register int i;
4132
4133 if (GetDnd(clientData, interp, argv[2], &dndPtr) != TCL_OK) {
4134 return TCL_ERROR;
4135 }
4136 if (argc == 3) {
4137 /* Return list of source data formats */
4138 for (hPtr = Blt_FirstHashEntry(&(dndPtr->getDataTable), &cursor);
4139 hPtr != NULL; hPtr = Blt_NextHashEntry(&cursor)) {
4140 Tcl_AppendElement(interp,
4141 Blt_GetHashKey(&(dndPtr->getDataTable), hPtr));
4142 }
4143 return TCL_OK;
4144 }
4145
4146 if (argc == 4) {
4147 hPtr = Blt_FindHashEntry(&(dndPtr->getDataTable), argv[3]);
4148 if (hPtr == NULL) {
4149 Tcl_AppendResult(interp, "can't find handler for format \"",
4150 argv[3], "\" for source \"", Tk_PathName(dndPtr->tkwin), "\"",
4151 (char *)NULL);
4152 return TCL_ERROR;
4153 }
4154 cmd = (char **)Blt_GetHashValue(hPtr);
4155 if (cmd == NULL) {
4156 Tcl_SetResult(interp, "", TCL_STATIC);
4157 } else {
4158 Tcl_SetResult(interp, PrintList(cmd), TCL_DYNAMIC);
4159 }
4160 return TCL_OK;
4161 }
4162
4163 for (i = 3; i < argc; i += 2) {
4164 hPtr = Blt_CreateHashEntry(&(dndPtr->getDataTable), argv[i], &isNew);
4165 if (!isNew) {
4166 cmd = (char **)Blt_GetHashValue(hPtr);
4167 Blt_Free(cmd);
4168 }
4169 if (Tcl_SplitList(interp, argv[i + 1], &nElem, &cmd) != TCL_OK) {
4170 Blt_DeleteHashEntry(&(dndPtr->getDataTable), hPtr);
4171 return TCL_ERROR;
4172 }
4173 Blt_SetHashValue(hPtr, cmd);
4174 }
4175 return TCL_OK;
4176 }
4177
4178 /*
4179 * ------------------------------------------------------------------------
4180 *
4181 * NamesOp --
4182 *
4183 * Returns the names of all the drag&drop managers. If either
4184 * a "-source" or "-target" switch is present, only the names of
4185 * managers acting as sources or targets respectively are returned.
4186 * A pattern argument may also be given. Only those managers
4187 * matching the pattern are returned.
4188 *
4189 * Examples: dnd names
4190 * dnd names -source
4191 * dnd names -target
4192 * dnd names .*label
4193 * Results:
4194 * A standard Tcl result. A Tcl list of drag&drop manager
4195 * names is returned.
4196 *
4197 * ------------------------------------------------------------------------
4198 */
4199 static int
4200 NamesOp(clientData, interp, argc, argv)
4201 ClientData clientData; /* Thread-specific data. */
4202 Tcl_Interp *interp;
4203 int argc;
4204 char **argv;
4205 {
4206 DndInterpData *dataPtr = clientData;
4207 Blt_HashEntry *hPtr;
4208 Blt_HashSearch cursor;
4209 Dnd *dndPtr;
4210 int findSources, findTargets;
4211
4212 findSources = findTargets = TRUE;
4213 if (argc > 2) {
4214 if ((argv[2][0] == '-') && (strcmp(argv[2], "-source") == 0)) {
4215 findTargets = FALSE;
4216 argc--, argv++;
4217 } else if ((argv[2][0] == '-') && (strcmp(argv[2], "-target") == 0)) {
4218 findSources = FALSE;
4219 argc--, argv++;
4220 }
4221 }
4222 for (hPtr = Blt_FirstHashEntry(&(dataPtr->dndTable), &cursor);
4223 hPtr != NULL; hPtr = Blt_NextHashEntry(&cursor)) {
4224 dndPtr = (Dnd *)Blt_GetHashValue(hPtr);
4225 if ((argc > 3) &&
4226 (!Tcl_StringMatch(Tk_PathName(dndPtr->tkwin), argv[3]))) {
4227 continue;
4228 }
4229 if (((findSources) && (dndPtr->isSource)) ||
4230 ((findTargets) && (dndPtr->isTarget))) {
4231 Tcl_AppendElement(interp, Tk_PathName(dndPtr->tkwin));
4232 }
4233 }
4234 return TCL_OK;
4235 }
4236
4237 /*
4238 * ------------------------------------------------------------------------
4239 *
4240 * PullOp --
4241 *
4242 * Pulls the current data from the source in the given format.
4243 * application.
4244 *
4245 * dnd pull .widget format data
4246 *
4247 * Results:
4248 * A standard Tcl result.
4249 *
4250 * Side Effects:
4251 * Invokes the target's data converter to store the data.
4252 *
4253 * ------------------------------------------------------------------------
4254 */
4255 /*ARGSUSED*/
4256 static int
4257 PullOp(clientData, interp, argc, argv)
4258 ClientData clientData; /* Thread-specific data. */
4259 Tcl_Interp *interp;
4260 int argc; /* Not used. */
4261 char **argv;
4262 {
4263 Dnd *dndPtr; /* drag&drop source record */
4264 int result;
4265 char **formatCmd;
4266 Blt_HashEntry *hPtr;
4267
4268 if (GetDnd(clientData, interp, argv[2], &dndPtr) != TCL_OK) {
4269 return TCL_ERROR;
4270 }
4271 if (!dndPtr->isTarget) {
4272 Tcl_AppendResult(interp, "widget \"", Tk_PathName(dndPtr->tkwin),
4273 "\" is not a registered drag&drop target.", (char *)NULL);
4274 return TCL_ERROR;
4275 }
4276 hPtr = Blt_FindHashEntry(&(dndPtr->setDataTable), argv[3]);
4277 if (hPtr == NULL) {
4278 Tcl_AppendResult(interp, "can't find format \"", argv[3],
4279 "\" in target \"", Tk_PathName(dndPtr->tkwin), "\"", (char *)NULL);
4280 return TCL_ERROR;
4281 }
4282 formatCmd = (char **)Blt_GetHashValue(hPtr);
4283 if (dndPtr->pendingPtr == NULL) {
4284 Tcl_AppendResult(interp, "no drop in progress", (char *)NULL);
4285 return TCL_ERROR;
4286 }
4287
4288 CompleteDataTransaction(dndPtr, argv[3], dndPtr->pendingPtr);
4289 result = TCL_OK;
4290 if (Tcl_DStringLength(&(dndPtr->pendingPtr->dString)) > 0) {
4291 Tcl_DString dString, savedResult;
4292 char **p;
4293
4294 Tcl_DStringInit(&dString);
4295 for (p = formatCmd; *p != NULL; p++) {
4296 Tcl_DStringAppendElement(&dString, *p);
4297 }
4298 Tcl_DStringAppendElement(&dString, Tk_PathName(dndPtr->tkwin));
4299 Tcl_DStringAppendElement(&dString, "x");
4300 Tcl_DStringAppendElement(&dString, Blt_Itoa(dndPtr->dropX));
4301 Tcl_DStringAppendElement(&dString, "y");
4302 Tcl_DStringAppendElement(&dString, Blt_Itoa(dndPtr->dropY));
4303 Tcl_DStringAppendElement(&dString, "timestamp");
4304 Tcl_DStringAppendElement(&dString,
4305 Blt_Utoa(dndPtr->pendingPtr->timestamp));
4306 Tcl_DStringAppendElement(&dString, "format");
4307 Tcl_DStringAppendElement(&dString, argv[3]);
4308 Tcl_DStringAppendElement(&dString, "value");
4309 Tcl_DStringAppendElement(&dString,
4310 Tcl_DStringValue(&(dndPtr->pendingPtr->dString)));
4311 Tcl_DStringInit(&savedResult);
4312 Tcl_DStringGetResult(interp, &savedResult);
4313 if (Tcl_GlobalEval(interp, Tcl_DStringValue(&dString)) != TCL_OK) {
4314 Tcl_BackgroundError(interp);
4315 }
4316 Tcl_DStringResult(interp, &savedResult);
4317 Tcl_DStringFree(&dString);
4318 }
4319 return result;
4320 }
4321
4322 /*
4323 * ------------------------------------------------------------------------
4324 *
4325 * SetdataOp --
4326 *
4327 * Registers one or more data formats with a drag&drop target.
4328 * Each format has a Tcl command associated with it. This command
4329 * is automatically invoked whenever data arrives from a source
4330 * to be converted to that particular format. The purpose of the
4331 * command is to set the data somewhere in the application (either
4332 * using a Tcl command or variable). When the Tcl command is invoked,
4333 * special percent substitutions are made:
4334 *
4335 * %# Drag&drop transaction timestamp.
4336 * %W Target widget.
4337 * %v Data value transfered from the source to
4338 * be converted into the correct format.
4339 *
4340 * If a converter (command) already exists for a format, it
4341 * overwrites the existing command.
4342 *
4343 * Example: dnd setdata .widget color { . configure -bg %v }
4344 *
4345 * Results:
4346 * A standard Tcl result.
4347 *
4348 * ------------------------------------------------------------------------
4349 */
4350 static int
4351 SetdataOp(clientData, interp, argc, argv)
4352 ClientData clientData;
4353 Tcl_Interp *interp;
4354 int argc;
4355 char **argv;
4356 {
4357 Dnd *dndPtr;
4358 Blt_HashEntry *hPtr;
4359 Blt_HashSearch cursor;
4360 int isNew, nElem;
4361 char **cmd;
4362 int i;
4363
4364 if (GetDnd(clientData, interp, argv[2], &dndPtr) != TCL_OK) {
4365 return TCL_ERROR;
4366 }
4367 if (argc == 3) {
4368 /* Show target handler data formats */
4369 for (hPtr = Blt_FirstHashEntry(&(dndPtr->setDataTable), &cursor);
4370 hPtr != NULL; hPtr = Blt_NextHashEntry(&cursor)) {
4371 Tcl_AppendElement(interp,
4372 Blt_GetHashKey(&(dndPtr->setDataTable), hPtr));
4373 }
4374 return TCL_OK;
4375 }
4376 if (argc == 4) {
4377 hPtr = Blt_FindHashEntry(&(dndPtr->setDataTable), argv[3]);
4378 if (hPtr == NULL) {
4379 Tcl_AppendResult(interp, "can't find handler for format \"",
4380 argv[3], "\" for target \"", Tk_PathName(dndPtr->tkwin), "\"",
4381 (char *)NULL);
4382 return TCL_ERROR;
4383 }
4384 cmd = (char **)Blt_GetHashValue(hPtr);
4385 if (cmd == NULL) {
4386 Tcl_SetResult(interp, "", TCL_STATIC);
4387 } else {
4388 Tcl_SetResult(interp, PrintList(cmd), TCL_DYNAMIC);
4389 }
4390 return TCL_OK;
4391 }
4392 for (i = 3; i < argc; i += 2) {
4393 hPtr = Blt_CreateHashEntry(&(dndPtr->setDataTable), argv[i], &isNew);
4394 if (!isNew) {
4395 cmd = (char **)Blt_GetHashValue(hPtr);
4396 Blt_Free(cmd);
4397 }
4398 if (Tcl_SplitList(interp, argv[i + 1], &nElem, &cmd) != TCL_OK) {
4399 Blt_DeleteHashEntry(&(dndPtr->setDataTable), hPtr);
4400 return TCL_ERROR;
4401 }
4402 Blt_SetHashValue(hPtr, cmd);
4403 }
4404 AddTargetProperty(dndPtr);
4405 return TCL_OK;
4406 }
4407
4408 /*
4409 * ------------------------------------------------------------------------
4410 *
4411 * RegisterOp --
4412 *
4413 * dnd register .window
4414 * ------------------------------------------------------------------------
4415 */
4416 static int
4417 RegisterOp(clientData, interp, argc, argv)
4418 ClientData clientData;
4419 Tcl_Interp *interp;
4420 int argc;
4421 char **argv;
4422 {
4423 DndInterpData *dataPtr = clientData;
4424 Tk_Window tkwin;
4425 Blt_HashEntry *hPtr;
4426 Dnd *dndPtr;
4427 int isNew;
4428
4429 tkwin = Tk_NameToWindow(interp, argv[2], dataPtr->mainWindow);
4430 if (tkwin == NULL) {
4431 return TCL_ERROR;
4432 }
4433 hPtr = Blt_CreateHashEntry(&(dataPtr->dndTable), (char *)tkwin, &isNew);
4434 if (!isNew) {
4435 Tcl_AppendResult(interp, "\"", Tk_PathName(tkwin),
4436 "\" is already registered as a drag&drop manager", (char *)NULL);
4437 return TCL_ERROR;
4438 }
4439 dndPtr = CreateDnd(interp, tkwin);
4440 dndPtr->hashPtr = hPtr;
4441 dndPtr->dataPtr = dataPtr;
4442 Blt_SetHashValue(hPtr, dndPtr);
4443 if (Blt_ConfigureWidget(interp, dndPtr->tkwin, configSpecs, argc - 3,
4444 argv + 3, (char *)dndPtr, 0) != TCL_OK) {
4445 return TCL_ERROR;
4446 }
4447 if (ConfigureDnd(interp, dndPtr) != TCL_OK) {
4448 return TCL_ERROR;
4449 }
4450 return TCL_OK;
4451 }
4452
4453 /*
4454 * ------------------------------------------------------------------------
4455 *
4456 * TokenWindowOp --
4457 *
4458 * ------------------------------------------------------------------------
4459 */
4460 static int
4461 TokenWindowOp(clientData, interp, argc, argv)
4462 ClientData clientData; /* Thread-specific data. */
4463 Tcl_Interp *interp;
4464 int argc;
4465 char **argv;
4466 {
4467 Dnd *dndPtr;
4468 int flags;
4469
4470 if (GetDnd(clientData, interp, argv[3], &dndPtr) != TCL_OK) {
4471 return TCL_ERROR;
4472 }
4473 flags = 0;
4474 if (dndPtr->tokenPtr == NULL) {
4475 if (CreateToken(interp, dndPtr) != TCL_OK) {
4476 return TCL_ERROR;
4477 }
4478 } else {
4479 flags = TK_CONFIG_ARGV_ONLY;
4480 }
4481 if (ConfigureToken(interp, dndPtr, argc - 4, argv + 4, flags) != TCL_OK) {
4482 return TCL_ERROR;
4483 }
4484 Tcl_SetResult(interp, Tk_PathName(dndPtr->tokenPtr->tkwin), TCL_VOLATILE);
4485 return TCL_OK;
4486 }
4487
4488 /*
4489 * ------------------------------------------------------------------------
4490 *
4491 * TokenCgetOp --
4492 *
4493 * Called to process an (argc,argv) list to configure (or
4494 * reconfigure) a drag&drop widget.
4495 *
4496 * ------------------------------------------------------------------------
4497 */
4498 /* ARGSUSED*/
4499 static int
4500 TokenCgetOp(clientData, interp, argc, argv)
4501 ClientData clientData; /* Thread-specific data. */
4502 Tcl_Interp *interp;
4503 int argc; /* Not used. */
4504 char **argv;
4505 {
4506 Dnd *dndPtr;
4507
4508 if (GetDnd(clientData, interp, argv[3], &dndPtr) != TCL_OK) {
4509 return TCL_ERROR;
4510 }
4511 if (dndPtr->tokenPtr == NULL) {
4512 Tcl_AppendResult(interp, "no token created for \"", argv[3], "\"",
4513 (char *)NULL);
4514 return TCL_ERROR;
4515 }
4516 return Tk_ConfigureValue(interp, dndPtr->tokenPtr->tkwin, tokenConfigSpecs,
4517 (char *)dndPtr->tokenPtr, argv[4], TK_CONFIG_ARGV_ONLY);
4518 }
4519
4520 /*
4521 * ------------------------------------------------------------------------
4522 *
4523 * TokenConfigureOp --
4524 *
4525 * ------------------------------------------------------------------------
4526 */
4527 static int
4528 TokenConfigureOp(clientData, interp, argc, argv)
4529 ClientData clientData; /* Thread-specific data. */
4530 Tcl_Interp *interp;
4531 int argc;
4532 char **argv;
4533 {
4534 Token *tokenPtr;
4535 Dnd *dndPtr;
4536 int flags;
4537
4538 if (GetDnd(clientData, interp, argv[3], &dndPtr) != TCL_OK) {
4539 return TCL_ERROR;
4540 }
4541 flags = TK_CONFIG_ARGV_ONLY;
4542 if (dndPtr->tokenPtr == NULL) {
4543 Tcl_AppendResult(interp, "no token created for \"", argv[3], "\"",
4544 (char *)NULL);
4545 return TCL_ERROR;
4546 }
4547 tokenPtr = dndPtr->tokenPtr;
4548 if (argc == 3) {
4549 return Tk_ConfigureInfo(interp, tokenPtr->tkwin, tokenConfigSpecs,
4550 (char *)tokenPtr, (char *)NULL, flags);
4551 } else if (argc == 4) {
4552 return Tk_ConfigureInfo(interp, tokenPtr->tkwin, tokenConfigSpecs,
4553 (char *)tokenPtr, argv[3], flags);
4554 }
4555 return ConfigureToken(interp, dndPtr, argc - 4, argv + 4, flags);
4556 }
4557
4558 static Blt_OpSpec tokenOps[] =
4559 {
4560 {"cget", 2, (Blt_Op)TokenCgetOp, 5, 5, "widget option",},
4561 {"configure", 2, (Blt_Op)TokenConfigureOp, 4, 0,
4562 "widget ?option value?...",},
4563 {"window", 5, (Blt_Op)TokenWindowOp, 4, 0,
4564 "widget ?option value?...",},
4565 };
4566
4567 static int nTokenOps = sizeof(tokenOps) / sizeof(Blt_OpSpec);
4568
4569 /*
4570 * ------------------------------------------------------------------------
4571 *
4572 * TokenOp --
4573 *
4574 * ------------------------------------------------------------------------
4575 */
4576 static int
4577 TokenOp(clientData, interp, argc, argv)
4578 ClientData clientData; /* Thread-specific data. */
4579 Tcl_Interp *interp;
4580 int argc;
4581 char **argv;
4582 {
4583 Blt_Op proc;
4584 int result;
4585
4586 proc = Blt_GetOp(interp, nTokenOps, tokenOps, BLT_OP_ARG2, argc, argv, 0);
4587 if (proc == NULL) {
4588 return TCL_ERROR;
4589 }
4590 result = (*proc) (clientData, interp, argc, argv);
4591 return result;
4592 }
4593
4594 static Blt_OpSpec dndOps[] =
4595 {
4596 {"cancel", 2, (Blt_Op)CancelOp, 3, 3, "widget",},
4597 {"cget", 2, (Blt_Op)CgetOp, 4, 4, "widget option",},
4598 {"configure", 4, (Blt_Op)ConfigureOp, 3, 0,
4599 "widget ?option value?...",},
4600 #ifdef notdef
4601 {"convert", 4, (Blt_Op)ConvertOp, 5, 5, "widget data format",},
4602 #endif
4603 {"delete", 2, (Blt_Op)DeleteOp, 3, 0,"?-source|-target? widget...",},
4604 {"drag", 3, (Blt_Op)DragOp, 3, 0, "widget x y ?token?",},
4605 {"drop", 3, (Blt_Op)DropOp, 3, 0, "widget x y ?token?",},
4606 {"getdata", 1, (Blt_Op)GetdataOp, 3, 0, "widget ?format command?",},
4607 {"names", 1, (Blt_Op)NamesOp, 2, 4, "?-source|-target? ?pattern?",},
4608 {"pull", 1, (Blt_Op)PullOp, 4, 4, "widget format",},
4609 {"register", 1, (Blt_Op)RegisterOp, 3, 0, "widget ?option value?...",},
4610 {"select", 3, (Blt_Op)SelectOp, 6, 6, "widget x y timestamp",},
4611 {"setdata", 3, (Blt_Op)SetdataOp, 3, 0, "widget ?format command?",},
4612 {"token", 5, (Blt_Op)TokenOp, 3, 0, "args...",},
4613 };
4614
4615 static int nDndOps = sizeof(dndOps) / sizeof(Blt_OpSpec);
4616
4617 /*
4618 * ------------------------------------------------------------------------
4619 *
4620 * DndCmd --
4621 *
4622 * Invoked by TCL whenever the user issues a drag&drop command.
4623 *
4624 * ------------------------------------------------------------------------
4625 */
4626 static int
4627 DndCmd(clientData, interp, argc, argv)
4628 ClientData clientData; /* Thread-specific data. */
4629 Tcl_Interp *interp; /* current interpreter */
4630 int argc; /* number of arguments */
4631 char **argv; /* argument strings */
4632 {
4633 Blt_Op proc;
4634 int result;
4635
4636 proc = Blt_GetOp(interp, nDndOps, dndOps, BLT_OP_ARG1, argc, argv, 0);
4637 if (proc == NULL) {
4638 return TCL_ERROR;
4639 }
4640 result = (*proc) (clientData, interp, argc, argv);
4641 return result;
4642 }
4643
4644 /*
4645 * -----------------------------------------------------------------------
4646 *
4647 * DndInterpDeleteProc --
4648 *
4649 * This is called when the interpreter hosting the "dnd" command is
4650 * destroyed.
4651 *
4652 * Results:
4653 * None.
4654 *
4655 * Side effects:
4656 * Destroys the hash table containing the drag&drop managers.
4657 *
4658 * ------------------------------------------------------------------------
4659 */
4660 /* ARGSUSED */
4661 static void
4662 DndInterpDeleteProc(clientData, interp)
4663 ClientData clientData; /* Thread-specific data. */
4664 Tcl_Interp *interp;
4665 {
4666 DndInterpData *dataPtr = clientData;
4667 Dnd *dndPtr;
4668 Blt_HashEntry *hPtr;
4669 Blt_HashSearch cursor;
4670
4671 for (hPtr = Blt_FirstHashEntry(&(dataPtr->dndTable), &cursor);
4672 hPtr != NULL; hPtr = Blt_NextHashEntry(&cursor)) {
4673 dndPtr = (Dnd *)Blt_GetHashValue(hPtr);
4674 dndPtr->hashPtr = NULL;
4675 DestroyDnd((DestroyData)dndPtr);
4676 }
4677 Blt_DeleteHashTable(&(dataPtr->dndTable));
4678 Tcl_DeleteAssocData(interp, DND_THREAD_KEY);
4679 Blt_Free(dataPtr);
4680 }
4681
4682 static DndInterpData *
4683 GetDndInterpData(interp)
4684 Tcl_Interp *interp;
4685 {
4686 DndInterpData *dataPtr;
4687 Tcl_InterpDeleteProc *proc;
4688
4689 dataPtr = (DndInterpData *)Tcl_GetAssocData(interp, DND_THREAD_KEY, &proc);
4690 if (dataPtr == NULL) {
4691 Display *display;
4692 Tk_Window tkwin;
4693
4694 dataPtr = Blt_Malloc(sizeof(DndInterpData));
4695 assert(dataPtr);
4696 tkwin = Tk_MainWindow(interp);
4697 display = Tk_Display(tkwin);
4698 dataPtr->mainWindow = tkwin;
4699 dataPtr->display = display;
4700 Tcl_SetAssocData(interp, DND_THREAD_KEY, DndInterpDeleteProc,
4701 dataPtr);
4702 Blt_InitHashTable(&(dataPtr->dndTable), BLT_ONE_WORD_KEYS);
4703 dataPtr->mesgAtom = XInternAtom(display, "BLT Dnd Message", False);
4704 dataPtr->targetAtom = XInternAtom(display, "BLT Dnd Target", False);
4705 dataPtr->formatsAtom = XInternAtom(display, "BLT Dnd Formats",False);
4706 dataPtr->commAtom = XInternAtom(display, "BLT Dnd CommData", False);
4707
4708 #ifdef HAVE_XDND
4709 dataPtr->XdndActionListAtom = XInternAtom(display, "XdndActionList",
4710 False);
4711 dataPtr->XdndAwareAtom = XInternAtom(display, "XdndAware", False);
4712 dataPtr->XdndEnterAtom = XInternAtom(display, "XdndEnter", False);
4713 dataPtr->XdndFinishedAtom = XInternAtom(display, "XdndFinished", False);
4714 dataPtr->XdndLeaveAtom = XInternAtom(display, "XdndLeave", False);
4715 dataPtr->XdndPositionAtom = XInternAtom(display, "XdndPosition", False);
4716 dataPtr->XdndSelectionAtom = XInternAtom(display, "XdndSelection",
4717 False);
4718 dataPtr->XdndStatusAtom = XInternAtom(display, "XdndStatus", False);
4719 dataPtr->XdndTypeListAtom = XInternAtom(display, "XdndTypeList", False);
4720 #endif /* HAVE_XDND */
4721 }
4722 return dataPtr;
4723 }
4724
4725 /*
4726 * ------------------------------------------------------------------------
4727 *
4728 * Blt_DndInit --
4729 *
4730 * Adds the drag&drop command to the given interpreter. Should
4731 * be invoked to properly install the command whenever a new
4732 * interpreter is created.
4733 *
4734 * ------------------------------------------------------------------------
4735 */
4736 int
4737 Blt_DndInit(interp)
4738 Tcl_Interp *interp; /* interpreter to be updated */
4739 {
4740 static Blt_CmdSpec cmdSpec =
4741 {
4742 "dnd", DndCmd
4743 };
4744 DndInterpData *dataPtr;
4745
4746 dataPtr = GetDndInterpData(interp);
4747 cmdSpec.clientData = dataPtr;
4748 if (Blt_InitCmd(interp, "blt", &cmdSpec) == NULL) {
4749 return TCL_ERROR;
4750 }
4751 return TCL_OK;
4752 }
4753
4754 #ifdef notdef
4755 /*
4756 * Registers bitmap outline of dragged data, used to indicate
4757 * what is being dragged by source. Bitmap is XOR-ed as cursor/token
4758 * is moved around the screen.
4759 */
4760 static void
4761 Blt_DndSetOutlineBitmap(tkwin, bitmap, x, y)
4762 Tk_Window tkwin;
4763 Pixmap bitmap;
4764 int x, y;
4765 {
4766
4767 }
4768 #endif
4769
4770 #ifdef HAVE_XDND
4771
4772 static void
4773 XDndFreeFormats(handlerPtr)
4774 XDndHandler *handlerPtr;
4775 {
4776 if (handlerPtr->formatArr != NULL) {
4777 char **p;
4778
4779 for (p = handlerPtr->formatArr; *p != NULL; p++) {
4780 XFree(*p);
4781 }
4782 Blt_Free(handlerPtr->formatArr);
4783 handlerPtr->formatArr = NULL;
4784 }
4785 }
4786
4787 static char **
4788 XDndGetFormats(handlerPtr, eventPtr)
4789 XDndHandler *handlerPtr;
4790 XEvent *eventPtr;
4791 {
4792 int flags;
4793 Window window;
4794 unsigned char *data;
4795 Atom typeAtom;
4796 Atom format;
4797 int nItems, bytesAfter;
4798 Atom *atomArr;
4799 char *nameArr[XDND_MAX_TYPES + 1];
4800 Display *display;
4801
4802 XDndFreeFormats(handlerPtr);
4803 display = eventPtr->xclient.display;
4804 window = eventPtr->xclient.data.l[0];
4805 flags = eventPtr->xclient.data.l[1];
4806 data = NULL;
4807 if (flags & 0x01) {
4808 result = XGetWindowProperty(
4809 display, /* Display of window. */
4810 window, /* Window holding the property. */
4811 handlerPtr->dataPtr->XdndTypeListAtom, /* Name of property. */
4812 0, /* Offset of data (for multiple reads). */
4813 XDND_MAX_TYPES, /* Maximum number of items to read. */
4814 False, /* If true, delete the property. */
4815 XA_ATOM, /* Desired type of property. */
4816 &typeAtom, /* (out) Actual type of the property. */
4817 &format, /* (out) Actual format of the property. */
4818 &nItems, /* (out) # of items in specified format. */
4819 &bytesAfter, /* (out) # of bytes remaining to be read. */
4820 (unsigned char **)&data);
4821 if ((result != Success) || (format != 32) || (typeAtom != XA_ATOM)) {
4822 if (data != NULL) {
4823 XFree((char *)data);
4824 return (char **)NULL;
4825 }
4826 }
4827 atomArr = (Atom *)data;
4828 nAtoms = nItems;
4829 } else {
4830 atomArr = &(eventPtr->xclient.data.l[2]);
4831 nAtoms = 3;
4832 }
4833 formatArr = Blt_Calloc(nAtoms + 1, sizeof(char *));
4834 for (i = 0; (i < nAtoms) && (atomArr[i] != None); i++) {
4835 formatArr[i] = XGetAtomName(display, atomArr[i]);
4836 }
4837 if (data != NULL) {
4838 XFree((char *)data);
4839 }
4840 handlerPtr->formatArr = formatArr;
4841 }
4842
4843 static char *
4844 GetMatchingFormats(dndPtr, formatArr)
4845 Dnd *dndPtr;
4846 char **formatArr;
4847 {
4848 int nMatches;
4849
4850 nMatches = 0;
4851 Tcl_DStringInit(&dString);
4852 for (p = formatArr; *p != NULL; p++) {
4853 for(hPtr = Blt_FirstHashEntry(&(dndPtr->setDataTable), &cursor);
4854 hPtr != NULL; hPtr = Blt_NextHashEntry(&cursor)) {
4855 fmt = Blt_GetHashKey(&(dndPtr->setDataTable), hPtr);
4856 if ((*fmt == **p) && (strcmp(fmt, *p) == 0)) {
4857 Tcl_DStringAppendElement(&dString, *p);
4858 nMatches++;
4859 break;
4860 }
4861 }
4862 }
4863 if (nMatches > 0) {
4864 char *string;
4865
4866 string = Blt_Strdup(Tcl_DStringValue(&dString));
4867 Tcl_DStringFree(&dString);
4868 return string;
4869 }
4870 return NULL;
4871 }
4872
4873 static void
4874 XDndPointerEvent(handlerPtr, eventPtr)
4875 XDndHandler *handlerPtr;
4876 XEvent *eventPtr;
4877 {
4878 Tk_Window tkwin;
4879 int flags;
4880 Atom action;
4881 Window window;
4882
4883 flags = 0;
4884 action = None;
4885 window = eventPtr->xclient.data.l[MESG_XDND_WINDOW];
4886 /*
4887 * If the XDND source has no formats specified, don't process any further.
4888 * Simply send a "no accept" action with the message.
4889 */
4890 if (handlerPtr->formatArr != NULL) {
4891 Dnd *newPtr, *oldPtr;
4892 int point;
4893 int button, keyState;
4894 int x, y;
4895 char *formats;
4896
4897 point = (int)eventPtr->xclient.data.l[MESG_XDND_POINT];
4898 UNPACK(point, x, y);
4899
4900 /*
4901 * See if the mouse pointer currently over a drop target. We first
4902 * determine what Tk window is under the mouse, and then check if
4903 * that window is registered as a drop target.
4904 */
4905 newPtr = NULL;
4906 tkwin = Tk_CoordsToWindow(x, y, handlerPtr->tkwin);
4907 if (tkwin != NULL) {
4908 Blt_HashEntry *hPtr;
4909
4910 hPtr = Blt_FindHashEntry(&(handlerPtr->dataPtr->dndTable),
4911 (char *)tkwin);
4912 if (hPtr != NULL) {
4913 newPtr = (Dnd *)Blt_GetHashValue(hPtr);
4914 if (!newPtr->isTarget) {
4915 newPtr = NULL; /* Not a DND target. */
4916 }
4917 formats = GetMatchingFormats(newPtr, handlerPtr->formatArr);
4918 if (formats == NULL) {
4919 newPtr = NULL; /* Source has no matching formats. */
4920 }
4921 }
4922 }
4923 button = keyState = 0;
4924 oldPtr = handlerPtr->dndPtr;
4925 resp = DROP_CANCEL;
4926 if (newPtr == oldPtr) {
4927 if ((oldPtr != NULL) && (oldPtr->motionCmd != NULL)) {
4928 resp = InvokeCallback(oldPtr, oldPtr->motionCmd, x, y, formats,
4929 button, keyState, dndPtr->timestamp);
4930 }
4931 } else {
4932 if ((oldPtr != NULL) && (oldPtr->leaveCmd != NULL)) {
4933 InvokeCallback(oldPtr, oldPtr->leaveCmd, x, y, formats, button,
4934 keyState, dndPtr->timestamp);
4935 }
4936 if ((newPtr != NULL) && (newPtr->enterCmd != NULL)) {
4937 resp = InvokeCallback(newPtr, newPtr->enterCmd, x, y, formats,
4938 button, keyState, dndPtr->timestamp);
4939 }
4940 handlerPtr->dndPtr = newPtr;
4941 /*
4942 * Save the current mouse position, since we get them from the
4943 * drop message.
4944 */
4945 newPtr->x = x;
4946 newPtr->y = y;
4947 }
4948 if (formats != NULL) {
4949 Blt_Free(formats);
4950 }
4951 flags = XDND_FLAGS_WANT_POSITION_MSGS;
4952 if (resp) {
4953 flags |= XDND_FLAGS_ACCEPT_DROP;
4954 action = handlerPtr->dataPtr->XdndActionCopyAtom;
4955 }
4956 }
4957 /* Target-to-Source: Drag result message. */
4958 SendClientMsg(handlerPtr->display, window,
4959 handlerPtr->dataPtr->XdndStatusAtom, handlerPtr->window,
4960 flags, 0, 0, action);
4961 }
4962
4963 static void
4964 XDndDropEvent(handlerPtr, eventPtr)
4965 XDndHandler *handlerPtr;
4966 XEvent *eventPtr;
4967 {
4968 Tk_Window tkwin;
4969 int flags;
4970 Atom action;
4971 Window window;
4972 int timestamp;
4973
4974 flags = 0;
4975 action = None;
4976 window = eventPtr->xclient.data.l[MESG_XDND_WINDOW];
4977 timestamp = eventPtr->xclient.data.l[MESG_XDND_TIMESTAMP];
4978
4979 /*
4980 * If no formats were specified for the XDND source or if the last
4981 * motion event did not place the mouse over a valid drop target,
4982 * don't process any further. Simply send a "no accept" action with
4983 * the message.
4984 */
4985 if ((handlerPtr->formatArr != NULL) && (handlerPtr->dndPtr != NULL)) {
4986 int button, keyState;
4987 Dnd *dndPtr = handlerPtr->dndPtr;
4988 DropPending pending;
4989 int resp;
4990
4991 button = keyState = 0; /* Protocol doesn't supply this information. */
4992
4993 /* Set up temporary bookkeeping for the drop transaction */
4994 memset (&pending, 0, sizeof(pending));
4995 pending.window = window;
4996 pending.display = eventPtr->xclient.display;
4997 pending.timestamp = timestamp;
4998 pending.protocol = PROTO_XDND;
4999 pending.packetSize = GetMaxPropertySize(pending.display);
5000 Tcl_DStringInit(&(pending.dString));
5001
5002 formats = GetMatchingFormats(handlerPtr->dndPtr, handlerPtr->formatArr);
5003 if (formats == NULL) {
5004 }
5005 dndPtr->pendingPtr = &pending;
5006 resp = AcceptDrop(dndPtr, dndPtr->x, dndPtr->y, formats, button,
5007 keyState, action, timestamp);
5008 dndPtr->pendingPtr = NULL;
5009 if (resp) {
5010 flags |= XDND_FLAGS_ACCEPT_DROP;
5011 action = handlerPtr->dataPtr->XdndActionCopyAtom;
5012 }
5013 }
5014 /* Target-to-Source: Drag result message. */
5015 SendClientMsg(handlerPtr->display, window,
5016 handlerPtr->dataPtr->XdndStatusAtom, handlerPtr->window,
5017 flags, 0, 0, action);
5018 }
5019
5020 /*
5021 * ------------------------------------------------------------------------
5022 *
5023 * XDndProtoEventProc --
5024 *
5025 * Invoked by Tk_HandleEvent whenever a DestroyNotify event is received
5026 * on a registered drag&drop source widget.
5027 *
5028 * ------------------------------------------------------------------------
5029 */
5030 static int
5031 XDndProtoEventProc(clientData, eventPtr)
5032 ClientData clientData; /* Drag&drop record. */
5033 XEvent *eventPtr; /* Event description. */
5034 {
5035 DndInterpData *dataPtr = clientData;
5036 Tk_Window tkwin;
5037 Blt_HashEntry *hPtr;
5038 XDndHandler *handlerPtr;
5039 int point;
5040 int x, y;
5041 Atom mesg;
5042
5043 if (eventPtr->type != ClientMessage) {
5044 return 0; /* Not a ClientMessage event. */
5045 }
5046 /* Was the recipient a registered toplevel window? */
5047 hPtr = Blt_FindHashEntry(&(dataPtr->handlerTable),
5048 (char *)eventPtr->xclient.window);
5049 if (hPtr == NULL) {
5050 return 0; /* No handler registered with window. */
5051 }
5052 handlerPtr = (XDndHandler *)Blt_GetHashValue(hPtr);
5053 mesg = eventPtr->xclient.message_type;
5054 if (mesg == dataPtr->XdndEnterAtom) {
5055 XDndGetFormats(handlerPtr, eventPtr);
5056 handlerPtr->dndPtr = NULL;
5057 } else if (mesg == dataPtr->XdndPositionAtom) {
5058 XDndPointerEvent(handlerPtr, eventPtr);
5059 } else if (mesg == dataPtr->XdndLeaveAtom) {
5060 XDndFreeFormats(handlerPtr); /* Free up any collected formats. */
5061 if (handlerPtr->dndPtr != NULL) {
5062 InvokeCallback(handlerPtr->dndPtr, handlerPtr->dndPtr->leaveCmd,
5063 -1, -1, NULL, 0, 0);
5064 /* Send leave event to drop target. */
5065 }
5066 } else if (mesg == dataPtr->XdndDropAtom) {
5067 XDndDropEvent(handlerPtr, eventPtr);
5068 } else {
5069 fprintf(stderr, "Unknown client message type = 0x%x\n", mesg);
5070 return 0; /* Unknown message type. */
5071 }
5072 return 1;
5073 }
5074
5075 static XDndHandler *
5076 XDndCreateHandler(dndPtr)
5077 Dnd *dndPtr;
5078 {
5079 Tk_Window tkwin;
5080 Window window;
5081 Blt_HashEntry *hPtr;
5082 int isNew;
5083 XDndHandler *handlerPtr;
5084
5085 /*
5086 * Find the containing toplevel of this window. See if an XDND
5087 * handler is already registered for it.
5088 */
5089 tkwin = Blt_GetToplevel(dndPtr->tkwin);
5090 window = Blt_GetRealWindowId(tkwin); /* Use the wrapper window as
5091 * the real toplevel window. */
5092 hPtr = Blt_CreateHashEntry(&(dataPtr->XDndHandlerTable), (char *)window,
5093 &isNew);
5094 if (!isNew) {
5095 handlerPtr = (XDndHandler *)Blt_GetHashEntry(hPtr);
5096 handlerPtr->refCount++;
5097 } else {
5098 handlerPtr = Blt_Malloc(sizeof(XDndHandler));
5099 handlerPtr->tkwin = tkwin;
5100 handlerPtr->dndPtr = NULL;
5101 handlerPtr->refCount = 1;
5102 handlerPtr->dataPtr = dataPtr;
5103 /* FIXME */
5104 SetProperty(window, dataPtr->XdndAwareAtom, "3");
5105 Blt_SetHashValue(hPtr, handlerPtr);
5106 }
5107 return handlerPtr;
5108 }
5109
5110 static void
5111 XDndDeleteHandler(dndPtr)
5112 Dnd *dndPtr;
5113 {
5114 Tk_Window tkwin;
5115 Window window;
5116 Blt_HashEntry *hPtr;
5117
5118 tkwin = Blt_GetToplevel(dndPtr->tkwin);
5119 window = Blt_GetRealWindowId(tkwin); /* Use the wrapper window as the real
5120 * toplevel window. */
5121 hPtr = Blt_FindHashEntry(&(dataPtr->XDndHandlerTable), (char *)window);
5122 if (hPtr != NULL) {
5123 XDndHandler *handlerPtr;
5124
5125 handlerPtr = (XDndHandler *)Blt_GetHashEntry(hPtr);
5126 handlerPtr->refCount--;
5127 if (handlerPtr->refCount == 0) {
5128 XDndFreeFormats(handlerPtr);
5129 XDeleteProperty(dndPtr->display, window,
5130 dndPtr->dataPtr->XdndAwareAtom);
5131 Blt_DeleteHashEntry(&(dataPtr->XDndHandlerTable), hPtr);
5132 Blt_Free(handlerPtr);
5133 }
5134 }
5135 }
5136
5137 #endif /* HAVE_XDND */
5138
5139 #endif /* NO_DRAGDROP */