Debian Codegraph @

generic/bltDragdrop.c

raw symbols ◀ ▶
Source reconstructed at patch state 1 of 2 — not the installed (fully patched) source.
2 Debian patches touch this file
1 /*
2 * bltDnd.c --
3 *
4 * This module implements a drag-and-drop mechanism for the Tk
5 * Toolkit. Allows widgets to be registered as drag&drop sources
6 * and targets for handling "drag-and-drop" operations between
7 * Tcl/Tk applications.
8 *
9 * Copyright 1993-1998 Lucent Technologies, Inc.
10 *
11 * Permission to use, copy, modify, and distribute this software and
12 * its documentation for any purpose and without fee is hereby
13 * granted, provided that the above copyright notice appear in all
14 * copies and that both that the copyright notice and warranty
15 * disclaimer appear in supporting documentation, and that the names
16 * of Lucent Technologies any of their entities not be used in
17 * advertising or publicity pertaining to distribution of the software
18 * without specific, written prior permission.
19 *
20 * Lucent Technologies disclaims all warranties with regard to this
21 * software, including all implied warranties of merchantability and
22 * fitness. In no event shall Lucent Technologies be liable for any
23 * special, indirect or consequential damages or any damages
24 * whatsoever resulting from loss of use, data or profits, whether in
25 * an action of contract, negligence or other tortuous action, arising
26 * out of or in connection with the use or performance of this
27 * software.
28 *
29 * The "drag&drop" command was created by Michael J. McLennan.
30 */
31 #include "bltInt.h"
32
33 #ifndef NO_DRAGDROP
34 #include "bltHash.h"
35 #include "bltChain.h"
36
37 #include <X11/Xatom.h>
38
39 #ifdef WIN32
40 #define MAX_PROP_SIZE 255 /* Maximum size of property. */
41 typedef HWND WINDOW;
42 #else
43 #define MAX_PROP_SIZE 1000 /* Maximum size of property. */
44 typedef Window WINDOW;
45 static Atom dndAtom;
46 #endif
47
48 /*
49 * Each "drag&drop" target widget is tagged with a "BltDrag&DropTarget"
50 * property in XA_STRING format. This property identifies the window
51 * as a "drag&drop" target. It's formated as a Tcl list and contains
52 * the following information:
53 *
54 * "INTERP_NAME TARGET_NAME DATA_TYPE DATA_TYPE ..."
55 *
56 * INTERP_NAME Name of the target application's interpreter.
57 * TARGET_NAME Path name of widget registered as the drop target.
58 * DATA_TYPE One or more "types" handled by the target.
59 *
60 * When the user invokes the "drag" operation, the window hierarchy
61 * is progressively examined. Window information is cached during
62 * the operation, to minimize X server traffic. Windows carrying a
63 * "BltDrag&DropTarget" property are identified. When the token is
64 * dropped over a valid site, the drop information is sent to the
65 * application
66 * via the usual "send" command. If communication fails, the drag&drop
67 * facility automatically posts a rejection symbol on the token window.
68 */
69
70 #define INTERP_NAME 0
71 #define TARGET_NAME 1
72 #define DATA_TYPE 2
73
74 /* Error Proc used to report drag&drop background errors */
75 #define DEF_ERROR_PROC "bgerror"
76 /*
77 * CONFIG PARAMETERS
78 */
79 #define DEF_DND_BUTTON_BACKGROUND RGB_YELLOW
80 #define DEF_DND_BUTTON_BG_MONO STD_NORMAL_BG_MONO
81 #define DEF_DND_BUTTON_NUMBER "3"
82 #define DEF_DND_PACKAGE_COMMAND (char *)NULL
83 #define DEF_DND_SELF_TARGET "no"
84 #define DEF_DND_SEND "all"
85 #define DEF_DND_SITE_COMMAND (char *)NULL
86 #define DEF_TOKEN_ACTIVE_BACKGROUND STD_ACTIVE_BACKGROUND
87 #define DEF_TOKEN_ACTIVE_BG_MONO STD_ACTIVE_BG_MONO
88 #define DEF_TOKEN_ACTIVE_BORDERWIDTH "3"
89 #define DEF_TOKEN_ACTIVE_RELIEF "sunken"
90 #define DEF_TOKEN_ANCHOR "se"
91 #define DEF_TOKEN_BACKGROUND STD_NORMAL_BACKGROUND
92 #define DEF_TOKEN_BG_MONO STD_NORMAL_BG_MONO
93 #define DEF_TOKEN_BORDERWIDTH "3"
94 #define DEF_TOKEN_CURSOR "arrow"
95 #define DEF_TOKEN_OUTLINE_COLOR RGB_BLACK
96 #define DEF_TOKEN_OUTLINE_MONO RGB_BLACK
97 #define DEF_TOKEN_REJECT_BACKGROUND STD_NORMAL_BACKGROUND
98 #define DEF_TOKEN_REJECT_BG_MONO RGB_WHITE
99 #define DEF_TOKEN_REJECT_FOREGROUND RGB_RED
100 #define DEF_TOKEN_REJECT_FG_MONO RGB_BLACK
101 #define DEF_TOKEN_REJECT_STIPPLE_COLOR (char *)NULL
102 #define DEF_TOKEN_REJECT_STIPPLE_MONO RGB_GREY50
103 #define DEF_TOKEN_RELIEF "raised"
104
105 #if HAVE_NAMESPACES
106 static char dragDropCmd[] = "blt::drag&drop";
107 #else
108 static char dragDropCmd[] = "drag&drop";
109 #endif
110
111 static char className[] = "DragDropToken"; /* CLASS NAME of token window */
112 static char propName[] = "BltDrag&DropTarget"; /* Property name */
113
114 static Blt_HashTable sourceTable;
115 static Blt_HashTable targetTable;
116 static char *errorCmd;
117 static int nActive;
118 static int locX, locY;
119 static int initialized = FALSE;
120
121 /*
122 * Percent substitutions
123 */
124 typedef struct {
125 char letter; /* character like 'x' in "%x" */
126 char *value; /* value to be substituted in place of "%x" */
127 } SubstDescriptors;
128
129
130 /*
131 * AnyWindow --
132 *
133 * This structure represents a window hierarchy examined during
134 * a single "drag" operation. It's used to cache information
135 * to reduce the round-trip calls to the server needed to query
136 * window geometry information and grab the target property.
137 */
138 typedef struct AnyWindowStruct AnyWindow;
139
140 struct AnyWindowStruct {
141 WINDOW nativeWindow; /* Native window: HWINDOW (Win32) or
142 * Window (X11). */
143
144 int initialized; /* If non-zero, the rest of this structure's
145 * information had been previously built. */
146
147 int x1, y1, x2, y2; /* Extents of the window (upper-left and
148 * lower-right corners). */
149
150 AnyWindow *parentPtr; /* Parent node. NULL if root. Used to
151 * compute offset for X11 windows. */
152
153 Blt_Chain *chainPtr; /* List of this window's children. If NULL,
154 * there are no children. */
155
156 char **targetInfo; /* An array of target window drag&drop
157 * information: target interpreter,
158 * pathname, and optionally possible
159 * type matches. NULL if the window is
160 * not a drag&drop target or is not a
161 * valid match for the drop source. */
162
163 };
164
165 /*
166 * Drag&Drop Registration Data
167 */
168 typedef struct {
169
170 /*
171 * This is a goof in the Tk API. It assumes that only an official
172 * Tk "toplevel" widget will ever become a toplevel window (i.e. a
173 * window whose parent is the root window). Because under Win32,
174 * Tk tries to use the widget record associated with the TopLevel
175 * as a Tk frame widget, to read its menu name. What this means
176 * is that any widget that's going to be a toplevel, must also look
177 * like a frame. Therefore we've copied the frame widget structure
178 * fields into the token.
179 */
180
181 Tk_Window tkwin; /* Window that embodies the frame. NULL
182 * means that the window has been destroyed
183 * but the data structures haven't yet been
184 * cleaned up. */
185 Display *display; /* Display containing widget. Used, among
186 * other things, so that resources can be
187 * freed even after tkwin has gone away. */
188 Tcl_Interp *interp; /* Interpreter associated with widget. Used
189 * to delete widget command. */
190 Tcl_Command widgetCmd; /* Token for frame's widget command. */
191 char *className; /* Class name for widget (from configuration
192 * option). Malloc-ed. */
193 int mask; /* Either FRAME or TOPLEVEL; used to select
194 * which configuration options are valid for
195 * widget. */
196 char *screenName; /* Screen on which widget is created. Non-null
197 * only for top-levels. Malloc-ed, may be
198 * NULL. */
199 char *visualName; /* Textual description of visual for window,
200 * from -visual option. Malloc-ed, may be
201 * NULL. */
202 char *colormapName; /* Textual description of colormap for window,
203 * from -colormap option. Malloc-ed, may be
204 * NULL. */
205 char *menuName; /* Textual description of menu to use for
206 * menubar. Malloc-ed, may be NULL. */
207 Colormap colormap; /* If not None, identifies a colormap
208 * allocated for this window, which must be
209 * freed when the window is deleted. */
210 Tk_3DBorder border; /* Structure used to draw 3-D border and
211 * background. NULL means no background
212 * or border. */
213 int borderWidth; /* Width of 3-D border (if any). */
214 int relief; /* 3-d effect: TK_RELIEF_RAISED etc. */
215 int highlightWidth; /* Width in pixels of highlight to draw
216 * around widget when it has the focus.
217 * 0 means don't draw a highlight. */
218 XColor *highlightBgColorPtr;
219 /* Color for drawing traversal highlight
220 * area when highlight is off. */
221 XColor *highlightColorPtr; /* Color for drawing traversal highlight. */
222 int width; /* Width to request for window. <= 0 means
223 * don't request any size. */
224 int height; /* Height to request for window. <= 0 means
225 * don't request any size. */
226 Tk_Cursor cursor; /* Current cursor for window, or None. */
227 char *takeFocus; /* Value of -takefocus option; not used in
228 * the C code, but used by keyboard traversal
229 * scripts. Malloc'ed, but may be NULL. */
230 int isContainer; /* 1 means this window is a container, 0 means
231 * that it isn't. */
232 char *useThis; /* If the window is embedded, this points to
233 * the name of the window in which it is
234 * embedded (malloc'ed). For non-embedded
235 * windows this is NULL. */
236 int flags; /* Various flags; see below for
237 * definitions. */
238
239 /* Token specific fields */
240
241 int lastX, lastY; /* last position of token window */
242 int active; /* non-zero => over target window */
243 Tcl_TimerToken timer; /* token for routine to hide tokenwin */
244 GC rejectFgGC; /* GC used to draw rejection fg: (\) */
245 GC rejectBgGC; /* GC used to draw rejection bg: (\) */
246
247 /* User-configurable fields */
248
249 Tk_Anchor anchor; /* Position of token win relative to mouse */
250 Tk_3DBorder outline; /* Outline border around token window */
251 Tk_3DBorder normalBorder; /* Border/background for token window */
252 Tk_3DBorder activeBorder; /* Border/background for token window */
253 int activeRelief;
254 int activeBorderWidth; /* Border width in pixels */
255 XColor *rejectFg; /* Color used to draw rejection fg: (\) */
256 XColor *rejectBg; /* Color used to draw rejection bg: (\) */
257 Pixmap rejectStipple; /* Stipple used to draw rejection: (\) */
258 } Token;
259
260 typedef struct {
261 Tcl_Interp *interp; /* Interpreter associated with the Tk source
262 * widget. */
263
264 Tk_Window tkwin; /* Tk window registered as the drag&drop
265 * source. */
266
267 Display *display; /* Drag&drop source window display */
268
269 Blt_HashTable handlerTable; /* Table of data handlers (converters)
270 * registered for this source. */
271
272 int button; /* Button used to invoke drag operation. */
273
274 Token token; /* Token used to provide special cursor. */
275
276 int pkgCmdInProgress; /* Indicates if a pkgCmd is currently active. */
277 char *pkgCmd; /* Tcl command executed at start of "drag"
278 * operation to gather information about
279 * the source data. */
280
281 char *pkgCmdResult; /* Result returned by the most recent
282 * pkgCmd. */
283
284 char *siteCmd; /* Tcl command executed to update token
285 * window. */
286
287 AnyWindow *rootPtr; /* Cached window information: Gathered
288 * and used during the "drag" operation
289 * to see if the mouse pointer is over a
290 * valid target. */
291
292 int selfTarget; /* Indicated if the source should drop onto
293 * itself. */
294
295 Tk_Cursor cursor; /* cursor restored after dragging */
296
297 char **sendTypes; /* list of data handler names or "all" */
298
299 Blt_HashEntry *hashPtr;
300
301 AnyWindow *windowPtr; /* Last target examined. If NULL, mouse
302 * pointer is not currently over a valid
303 * target. */
304 } Source;
305
306 typedef struct {
307 Tcl_Interp *interp;
308 Tk_Window tkwin; /* drag&drop target window */
309 Display *display; /* drag&drop target window display */
310 Blt_HashTable handlerTable; /* Table of data handlers (converters)
311 * registered for this target. */
312 Blt_HashEntry *hashPtr;
313 } Target;
314
315
316 extern Tk_CustomOption bltListOption;
317
318 static Tk_ConfigSpec configSpecs[] =
319 {
320 {TK_CONFIG_INT, "-button", "buttonBinding", "ButtonBinding",
321 DEF_DND_BUTTON_NUMBER, Tk_Offset(Source, button), 0},
322 {TK_CONFIG_STRING, "-packagecmd", "packageCommand", "Command",
323 DEF_DND_PACKAGE_COMMAND, Tk_Offset(Source, pkgCmd), TK_CONFIG_NULL_OK},
324 {TK_CONFIG_COLOR, "-rejectbg", "rejectBackground", "Background",
325 DEF_TOKEN_REJECT_BACKGROUND, Tk_Offset(Source, token.rejectBg),
326 TK_CONFIG_COLOR_ONLY},
327 {TK_CONFIG_COLOR, "-rejectbg", "rejectBackground", "Background",
328 DEF_TOKEN_REJECT_BG_MONO, Tk_Offset(Source, token.rejectBg),
329 TK_CONFIG_MONO_ONLY},
330 {TK_CONFIG_COLOR, "-rejectfg", "rejectForeground", "Foreground",
331 DEF_TOKEN_REJECT_FOREGROUND, Tk_Offset(Source, token.rejectFg),
332 TK_CONFIG_COLOR_ONLY},
333 {TK_CONFIG_COLOR, "-rejectfg", "rejectForeground", "Foreground",
334 DEF_TOKEN_REJECT_BACKGROUND, Tk_Offset(Source, token.rejectFg),
335 TK_CONFIG_MONO_ONLY},
336 {TK_CONFIG_BITMAP, "-rejectstipple", "rejectStipple", "Stipple",
337 DEF_TOKEN_REJECT_STIPPLE_COLOR, Tk_Offset(Source, token.rejectStipple),
338 TK_CONFIG_COLOR_ONLY | TK_CONFIG_NULL_OK},
339 {TK_CONFIG_BITMAP, "-rejectstipple", "rejectStipple", "Stipple",
340 DEF_TOKEN_REJECT_STIPPLE_MONO, Tk_Offset(Source, token.rejectStipple),
341 TK_CONFIG_MONO_ONLY},
342 {TK_CONFIG_BOOLEAN, "-selftarget", "selfTarget", "SelfTarget",
343 DEF_DND_SELF_TARGET, Tk_Offset(Source, selfTarget), 0},
344 {TK_CONFIG_CUSTOM, "-send", "send", "Send", DEF_DND_SEND,
345 Tk_Offset(Source, sendTypes), TK_CONFIG_NULL_OK, &bltListOption},
346 {TK_CONFIG_STRING, "-sitecmd", "siteCommand", "Command",
347 DEF_DND_SITE_COMMAND, Tk_Offset(Source, siteCmd), TK_CONFIG_NULL_OK},
348 {TK_CONFIG_ANCHOR, "-tokenanchor", "tokenAnchor", "Anchor",
349 DEF_TOKEN_ANCHOR, Tk_Offset(Source, token.anchor), 0},
350 {TK_CONFIG_BORDER, "-tokenactivebackground", "tokenActiveBackground",
351 "ActiveBackground", DEF_TOKEN_ACTIVE_BACKGROUND,
352 Tk_Offset(Source, token.activeBorder), TK_CONFIG_COLOR_ONLY},
353 {TK_CONFIG_BORDER, "-tokenactivebackground", "tokenActiveBackground",
354 "ActiveBackground", DEF_TOKEN_ACTIVE_BG_MONO,
355 Tk_Offset(Source, token.activeBorder), TK_CONFIG_MONO_ONLY},
356 {TK_CONFIG_BORDER, "-tokenbg", "tokenBackground", "Background",
357 DEF_TOKEN_BACKGROUND, Tk_Offset(Source, token.normalBorder),
358 TK_CONFIG_COLOR_ONLY},
359 {TK_CONFIG_BORDER, "-tokenbg", "tokenBackground", "Background",
360 DEF_TOKEN_BG_MONO, Tk_Offset(Source, token.normalBorder),
361 TK_CONFIG_MONO_ONLY},
362 {TK_CONFIG_BORDER, "-tokenoutline", "tokenOutline", "Outline",
363 DEF_TOKEN_OUTLINE_COLOR, Tk_Offset(Source, token.outline),
364 TK_CONFIG_COLOR_ONLY},
365 {TK_CONFIG_BORDER, "-tokenoutline", "tokenOutline", "Outline",
366 DEF_TOKEN_OUTLINE_MONO, Tk_Offset(Source, token.outline),
367 TK_CONFIG_MONO_ONLY},
368 {TK_CONFIG_PIXELS, "-tokenborderwidth", "tokenBorderWidth", "BorderWidth",
369 DEF_TOKEN_BORDERWIDTH, Tk_Offset(Source, token.borderWidth), 0},
370 {TK_CONFIG_CURSOR, "-tokencursor", "tokenCursor", "Cursor",
371 DEF_TOKEN_CURSOR, Tk_Offset(Source, token.cursor),
372 TK_CONFIG_NULL_OK},
373 {TK_CONFIG_END, (char *)NULL, (char *)NULL, (char *)NULL, (char *)NULL,
374 0, 0},
375 };
376
377 static Tk_ConfigSpec tokenConfigSpecs[] =
378 {
379 {TK_CONFIG_BORDER, "-activebackground", "activeBackground",
380 "ActiveBackground", DEF_TOKEN_ACTIVE_BACKGROUND,
381 Tk_Offset(Token, activeBorder), TK_CONFIG_COLOR_ONLY},
382 {TK_CONFIG_BORDER, "-activebackground", "activeBackground",
383 "ActiveBackground", DEF_TOKEN_ACTIVE_BG_MONO,
384 Tk_Offset(Token, activeBorder), TK_CONFIG_MONO_ONLY},
385 {TK_CONFIG_RELIEF, "-activerelief", "activeRelief", "activeRelief",
386 DEF_TOKEN_ACTIVE_RELIEF, Tk_Offset(Token, activeRelief), 0},
387 {TK_CONFIG_ANCHOR, "-anchor", "anchor", "Anchor",
388 DEF_TOKEN_ANCHOR, Tk_Offset(Token, anchor), 0},
389 {TK_CONFIG_PIXELS, "-activeborderwidth", "activeBorderWidth",
390 "ActiveBorderWidth", DEF_TOKEN_ACTIVE_BORDERWIDTH,
391 Tk_Offset(Token, activeBorderWidth), 0},
392 {TK_CONFIG_BORDER, "-background", "background", "Background",
393 DEF_TOKEN_BACKGROUND, Tk_Offset(Token, normalBorder),
394 TK_CONFIG_COLOR_ONLY},
395 {TK_CONFIG_BORDER, "-background", "background", "Background",
396 DEF_TOKEN_BG_MONO, Tk_Offset(Token, normalBorder),
397 TK_CONFIG_MONO_ONLY},
398 {TK_CONFIG_PIXELS, "-borderwidth", "borderWidth", "BorderWidth",
399 DEF_TOKEN_BORDERWIDTH, Tk_Offset(Token, borderWidth), 0},
400 {TK_CONFIG_CURSOR, "-cursor", "cursor", "Cursor",
401 DEF_TOKEN_CURSOR, Tk_Offset(Token, cursor), TK_CONFIG_NULL_OK},
402 {TK_CONFIG_BORDER, "-outline", "outline", "Outline",
403 DEF_TOKEN_OUTLINE_COLOR, Tk_Offset(Token, outline),
404 TK_CONFIG_COLOR_ONLY},
405 {TK_CONFIG_BORDER, "-outline", "outline", "Outline",
406 DEF_TOKEN_OUTLINE_MONO, Tk_Offset(Token, outline),
407 TK_CONFIG_MONO_ONLY},
408 {TK_CONFIG_COLOR, "-rejectbg", "rejectBackground", "Background",
409 DEF_TOKEN_REJECT_BACKGROUND, Tk_Offset(Token, rejectBg),
410 TK_CONFIG_COLOR_ONLY},
411 {TK_CONFIG_COLOR, "-rejectbg", "rejectBackground", "Background",
412 DEF_TOKEN_REJECT_BG_MONO, Tk_Offset(Token, rejectBg),
413 TK_CONFIG_MONO_ONLY},
414 {TK_CONFIG_COLOR, "-rejectfg", "rejectForeground", "Foreground",
415 DEF_TOKEN_REJECT_FOREGROUND, Tk_Offset(Token, rejectFg),
416 TK_CONFIG_COLOR_ONLY},
417 {TK_CONFIG_COLOR, "-rejectfg", "rejectForeground", "Foreground",
418 DEF_TOKEN_REJECT_BACKGROUND, Tk_Offset(Token, rejectFg),
419 TK_CONFIG_MONO_ONLY},
420 {TK_CONFIG_BITMAP, "-rejectstipple", "rejectStipple", "Stipple",
421 DEF_TOKEN_REJECT_STIPPLE_COLOR, Tk_Offset(Token, rejectStipple),
422 TK_CONFIG_COLOR_ONLY | TK_CONFIG_NULL_OK},
423 {TK_CONFIG_BITMAP, "-rejectstipple", "rejectStipple", "Stipple",
424 DEF_TOKEN_REJECT_STIPPLE_MONO, Tk_Offset(Token, rejectStipple),
425 TK_CONFIG_MONO_ONLY},
426 {TK_CONFIG_RELIEF, "-relief", "relief", "Relief",
427 DEF_TOKEN_RELIEF, Tk_Offset(Token, relief), 0},
428 {TK_CONFIG_END, (char *)NULL, (char *)NULL, (char *)NULL, (char *)NULL,
429 0, 0},
430 };
431
432
433 /*
434 * Forward Declarations
435 */
436 static int DragDropCmd _ANSI_ARGS_((ClientData clientData, Tcl_Interp *interp,
437 int argc, char **argv));
438 static void TokenEventProc _ANSI_ARGS_((ClientData clientData,
439 XEvent *eventPtr));
440 static void TargetEventProc _ANSI_ARGS_((ClientData clientData,
441 XEvent *eventPtr));
442 static void MoveToken _ANSI_ARGS_((Source * srcPtr, Token *tokenPtr));
443 static void UpdateToken _ANSI_ARGS_((ClientData clientData));
444 static void HideToken _ANSI_ARGS_((Token *tokenPtr));
445 static void RejectToken _ANSI_ARGS_((Token *tokenPtr));
446
447 static int GetSource _ANSI_ARGS_((Tcl_Interp *interp, char *name,
448 Source **srcPtrPtr));
449 static Source *CreateSource _ANSI_ARGS_((Tcl_Interp *interp, char *pathname,
450 int *newEntry));
451 static void DestroySource _ANSI_ARGS_((Source * srcPtr));
452 static void SourceEventProc _ANSI_ARGS_((ClientData clientData,
453 XEvent *eventPtr));
454 static int ConfigureSource _ANSI_ARGS_((Tcl_Interp *interp, Source * srcPtr,
455 int argc, char **argv, int flags));
456 static int ConfigureToken _ANSI_ARGS_((Tcl_Interp *interp, Source * srcPtr,
457 int argc, char **argv));
458
459 static Target *CreateTarget _ANSI_ARGS_((Tcl_Interp *interp, Tk_Window tkwin));
460 static Target *FindTarget _ANSI_ARGS_((Tk_Window tkwin));
461 static void DestroyTarget _ANSI_ARGS_((DestroyData dataPtr));
462 static int OverTarget _ANSI_ARGS_((Source * srcPtr, int x, int y));
463 static void AddTargetProperty _ANSI_ARGS_((Tcl_Interp *interp,
464 Target * targetPtr));
465
466 static void DndSend _ANSI_ARGS_((Source * srcPtr));
467
468 static void InitRoot _ANSI_ARGS_((Source * srcPtr));
469 static void RemoveWindow _ANSI_ARGS_((AnyWindow *wr));
470 static void QueryWindow _ANSI_ARGS_((Display *display, AnyWindow * windowPtr));
471
472 static char *ExpandPercents _ANSI_ARGS_((char *str, SubstDescriptors * subs,
473 int nsubs, Tcl_DString *resultPtr));
474
475
476
477 #ifdef WIN32
478
479 #if defined( _MSC_VER) || defined(__BORLANDC__)
480 #include <tchar.h>
481 #endif /* _MSC_VER || __BORLANDC__ */
482
483 typedef struct {
484 char *prefix;
485 int prefixSize;
486 char *propReturn;
487 } PropertyInfo;
488
489
490 static BOOL CALLBACK
491 GetEnumWindowsProc(HWND hWnd, LPARAM clientData)
492 {
493 Blt_Chain *chainPtr = (Blt_Chain *) clientData;
494
495 Blt_ChainAppend(chainPtr, (ClientData)hWnd);
496 return TRUE;
497 }
498
499 static WINDOW
500 GetNativeWindow(Tk_Window tkwin)
501 {
502 return (WINDOW) Tk_GetHWND(Tk_WindowId(tkwin));
503 }
504
505 /*
506 * ------------------------------------------------------------------------
507 *
508 * GetWindowZOrder --
509 *
510 * Returns a list of the child windows according to their stacking
511 * order. The window handles are ordered from top to bottom.
512 *
513 * ------------------------------------------------------------------------
514 */
515 static Blt_Chain *
516 GetWindowZOrder(
517 Display *display,
518 HWND parent)
519 {
520 Blt_Chain *chainPtr;
521 HWND hWnd;
522
523 chainPtr = Blt_ChainCreate();
524 for (hWnd = GetTopWindow(parent); hWnd != NULL;
525 hWnd = GetNextWindow(hWnd, GW_HWNDNEXT)) {
526 Blt_ChainAppend(chainPtr, (ClientData)hWnd);
527 }
528 return chainPtr;
529 }
530
531 /*
532 * ------------------------------------------------------------------------
533 *
534 * GetEnumPropsExProc --
535 *
536 * ------------------------------------------------------------------------
537 */
538 static BOOL CALLBACK
539 GetEnumPropsExProc(
540 HWND hwnd,
541 LPCTSTR atom,
542 HANDLE hData,
543 DWORD clientData)
544 {
545 PropertyInfo *infoPtr = (PropertyInfo *) clientData;
546
547 if (strncmp(infoPtr->prefix, atom, infoPtr->prefixSize) == 0) {
548 assert(infoPtr->propReturn == NULL);
549 infoPtr->propReturn = (char *)atom;
550 return FALSE;
551 }
552 return TRUE;
553 }
554
555 /*
556 * ------------------------------------------------------------------------
557 *
558 * GetPropData --
559 *
560 * This is a bad Windows hack to pass property information between
561 * applications. (Ab)Normally the property data (one-word value) is
562 * stored in the data handle. But the data content is available only
563 * within the application. The pointer value is meaningless outside
564 * of the current application address space. Not really useful at all.
565 *
566 * So the trick here is to encode the property name with all the
567 * necessary information and to loop through all the properties
568 * of a window, looking for one that starts with our property name
569 * prefix. The downside is that the property name is limited to
570 * 255 bytes. But that should be enough. It's also slower since
571 * we examine each property until we find ours.
572 *
573 * We'll plug in the OLE stuff later.
574 *
575 * ------------------------------------------------------------------------
576 */
577
578 static char *
579 GetPropData(HWND hWnd, char *atom)
580 {
581 PropertyInfo propInfo;
582 if (hWnd == NULL) {
583 return NULL;
584 }
585 propInfo.prefix = atom;
586 propInfo.prefixSize = strlen(atom);
587 propInfo.propReturn = NULL;
588 EnumPropsEx(hWnd, (PROPENUMPROCEX)GetEnumPropsExProc, (DWORD)&propInfo);
589 return propInfo.propReturn;
590 }
591
592
593 static char *
594 GetProperty(Display *display, HWND hWnd)
595 {
596 ATOM atom;
597
598 atom = (ATOM)GetProp(hWnd, propName);
599 if (atom != (ATOM)0) {
600 char buffer[MAX_PROP_SIZE + 1];
601 UINT nBytes;
602
603 nBytes = GlobalGetAtomName(atom, buffer, MAX_PROP_SIZE);
604 if (nBytes > 0) {
605 buffer[nBytes] = '\0';
606 return Blt_Strdup(buffer);
607 }
608 }
609 return NULL;
610 }
611
612 static void
613 SetProperty(Tk_Window tkwin, char *data)
614 {
615 HWND hWnd;
616 ATOM atom;
617
618 hWnd = Tk_GetHWND(Tk_WindowId(tkwin));
619 if (hWnd == NULL) {
620 return;
621 }
622 atom = (ATOM)GetProp(hWnd, propName);
623 if (atom != 0) {
624 GlobalDeleteAtom(atom);
625 }
626 atom = GlobalAddAtom(data);
627 if (atom != (ATOM)0) {
628 SetProp(hWnd, propName, (HANDLE)atom);
629 }
630 }
631
632 static void
633 RemoveProperty(Tk_Window tkwin)
634 {
635 HWND hWnd;
636 ATOM atom;
637
638 hWnd = Tk_GetHWND(Tk_WindowId(tkwin));
639 if (hWnd == NULL) {
640 return;
641 }
642 atom = (ATOM)GetProp(hWnd, propName);
643 if (atom != 0) {
644 GlobalDeleteAtom(atom);
645 }
646 RemoveProp(hWnd, propName);
647 }
648
649 /*
650 *----------------------------------------------------------------------
651 *
652 * GetWindowRegion --
653 *
654 *----------------------------------------------------------------------
655 */
656 /*ARGSUSED*/
657 static int
658 GetWindowRegion(
659 Display *display, /* Not used. */
660 HWND hWnd,
661 int *x1Ptr,
662 int *y1Ptr,
663 int *x2Ptr,
664 int *y2Ptr)
665 {
666 RECT rect;
667
668 if (GetWindowRect(hWnd, &rect)) {
669 *x1Ptr = rect.left;
670 *y1Ptr = rect.top;
671 *x2Ptr = rect.right;
672 *y2Ptr = rect.bottom;
673 return IsWindowVisible(hWnd);
674 }
675 return FALSE;
676 }
677
678 #else
679
680 static WINDOW
681 GetNativeWindow(tkwin)
682 Tk_Window tkwin;
683 {
684 return Tk_WindowId(tkwin);
685 }
686
687 /*
688 * ------------------------------------------------------------------------
689 *
690 * GetWindowZOrder --
691 *
692 * Returns a chain of the child windows according to their stacking
693 * order. The window ids are ordered from top to bottom.
694 *
695 * ------------------------------------------------------------------------
696 */
697 static Blt_Chain *
698 GetWindowZOrder(display, window)
699 Display *display;
700 Window window;
701 {
702 Blt_Chain *chainPtr;
703 Window *childArr;
704 unsigned int nChildren;
705 Window dummy;
706
707 chainPtr = NULL;
708 if ((XQueryTree(display, window, &dummy, &dummy, &childArr, &nChildren)) &&
709 (nChildren > 0)) {
710 register int i;
711
712 chainPtr = Blt_ChainCreate();
713 for (i = 0; i < nChildren; i++) {
714 /*
715 * XQuery returns windows in bottom to top order.
716 * We only care about the top window.
717 */
718 Blt_ChainPrepend(chainPtr, (ClientData)childArr[i]);
719 }
720 if (childArr != NULL) {
721 XFree((char *)childArr); /* done with list of kids */
722 }
723 }
724 return chainPtr;
725 }
726
727 static char *
728 GetProperty(display, window)
729 Display *display;
730 Window window;
731 {
732 char *data;
733 int result, actualFormat;
734 Atom actualType;
735 unsigned long nItems, bytesAfter;
736
737 if (window == None) {
738 return NULL;
739 }
740 data = NULL;
741 result = XGetWindowProperty(display, window, dndAtom, 0, MAX_PROP_SIZE,
742 False, XA_STRING, &actualType, &actualFormat, &nItems, &bytesAfter,
743 (unsigned char **)&data);
744 if ((result != Success) || (actualFormat != 8) ||
745 (actualType != XA_STRING)) {
746 if (data != NULL) {
747 XFree((char *)data);
748 data = NULL;
749 }
750 }
751 return data;
752 }
753
754 static void
755 SetProperty(tkwin, data)
756 Tk_Window tkwin;
757 char *data;
758 {
759 XChangeProperty(Tk_Display(tkwin), Tk_WindowId(tkwin), dndAtom, XA_STRING,
760 8, PropModeReplace, (unsigned char *)data, strlen(data) + 1);
761 }
762
763 static int
764 GetWindowRegion(display, window, x1Ptr, y1Ptr, x2Ptr, y2Ptr)
765 Display *display;
766 Window window;
767 int *x1Ptr, *y1Ptr, *x2Ptr, *y2Ptr;
768 {
769 XWindowAttributes winAttrs;
770
771 if (XGetWindowAttributes(display, window, &winAttrs)) {
772 *x1Ptr = winAttrs.x;
773 *y1Ptr = winAttrs.y;
774 *x2Ptr = winAttrs.x + winAttrs.width - 1;
775 *y2Ptr = winAttrs.y + winAttrs.height - 1;
776 }
777 return (winAttrs.map_state == IsViewable);
778 }
779
780 #endif /* WIN32 */
781
782 /*
783 * ------------------------------------------------------------------------
784 *
785 * ChangeToken --
786 *
787 * ------------------------------------------------------------------------
788 */
789 static void
790 ChangeToken(tokenPtr, active)
791 Token *tokenPtr;
792 int active;
793 {
794 int relief;
795 Tk_3DBorder border;
796 int borderWidth;
797
798 Blt_Fill3DRectangle(tokenPtr->tkwin, Tk_WindowId(tokenPtr->tkwin),
799 tokenPtr->outline, 0, 0, Tk_Width(tokenPtr->tkwin),
800 Tk_Height(tokenPtr->tkwin), 0, TK_RELIEF_FLAT);
801 if (active) {
802 relief = tokenPtr->activeRelief;
803 border = tokenPtr->activeBorder;
804 borderWidth = tokenPtr->activeBorderWidth;
805 } else {
806 relief = tokenPtr->relief;
807 border = tokenPtr->normalBorder;
808 borderWidth = tokenPtr->borderWidth;
809 }
810 Blt_Fill3DRectangle(tokenPtr->tkwin, Tk_WindowId(tokenPtr->tkwin), border,
811 2, 2, Tk_Width(tokenPtr->tkwin) - 4, Tk_Height(tokenPtr->tkwin) - 4,
812 borderWidth, relief);
813 }
814
815 /*
816 * ------------------------------------------------------------------------
817 *
818 * TokenEventProc --
819 *
820 * Invoked by the Tk dispatcher to handle widget events.
821 * Manages redraws for the drag&drop token window.
822 *
823 * ------------------------------------------------------------------------
824 */
825 static void
826 TokenEventProc(clientData, eventPtr)
827 ClientData clientData; /* data associated with widget */
828 XEvent *eventPtr; /* information about event */
829 {
830 Token *tokenPtr = clientData;
831
832 if ((eventPtr->type == Expose) && (eventPtr->xexpose.count == 0)) {
833 if (tokenPtr->tkwin != NULL) {
834 ChangeToken(tokenPtr, tokenPtr->active);
835 }
836 } else if (eventPtr->type == DestroyNotify) {
837 tokenPtr->tkwin = NULL;
838 }
839 }
840
841 /*
842 * ------------------------------------------------------------------------
843 *
844 * HideToken --
845 *
846 * Unmaps the drag&drop token. Invoked directly at the end of a
847 * successful communication, or after a delay if the communication
848 * fails (allowing the user to see a graphical picture of failure).
849 *
850 * ------------------------------------------------------------------------
851 */
852 static void
853 HideToken(tokenPtr)
854 Token *tokenPtr;
855 {
856 if (tokenPtr->tkwin != NULL) {
857 Tk_UnmapWindow(tokenPtr->tkwin);
858 }
859 tokenPtr->timer = NULL;
860 }
861
862 /*
863 * ------------------------------------------------------------------------
864 *
865 * RaiseToken --
866 *
867 * ------------------------------------------------------------------------
868 */
869 static void
870 RaiseToken(tokenPtr)
871 Token *tokenPtr;
872 {
873 Blt_MapToplevel(tokenPtr->tkwin);
874 Blt_RaiseToplevel(tokenPtr->tkwin);
875 }
876
877 /*
878 * ------------------------------------------------------------------------
879 *
880 * MoveToken --
881 *
882 * Invoked during "drag" operations to move a token window to its
883 * current "drag" coordinate.
884 *
885 * ------------------------------------------------------------------------
886 */
887 static void
888 MoveToken(srcPtr, tokenPtr)
889 Source *srcPtr; /* drag&drop source window data */
890 Token *tokenPtr;
891 {
892 int x, y;
893 int maxX, maxY;
894 int vx, vy, vw, vh;
895 Screen *screenPtr;
896
897 /* Adjust current location for virtual root windows. */
898 Tk_GetVRootGeometry(srcPtr->tkwin, &vx, &vy, &vw, &vh);
899 x = tokenPtr->lastX + vx - 3;
900 y = tokenPtr->lastY + vy - 3;
901
902 screenPtr = Tk_Screen(srcPtr->tkwin);
903 maxX = WidthOfScreen(screenPtr) - Tk_Width(tokenPtr->tkwin);
904 maxY = HeightOfScreen(screenPtr) - Tk_Height(tokenPtr->tkwin);
905 Blt_TranslateAnchor(x, y, Tk_Width(tokenPtr->tkwin),
906 Tk_Height(tokenPtr->tkwin), tokenPtr->anchor, &x, &y);
907 if (x > maxX) {
908 x = maxX;
909 } else if (x < 0) {
910 x = 0;
911 }
912 if (y > maxY) {
913 y = maxY;
914 } else if (y < 0) {
915 y = 0;
916 }
917 if ((x != Tk_X(tokenPtr->tkwin)) || (y != Tk_Y(tokenPtr->tkwin))) {
918 Tk_MoveToplevelWindow(tokenPtr->tkwin, x, y);
919 }
920 RaiseToken(tokenPtr);
921 }
922
923 static Tk_Cursor
924 GetWidgetCursor(interp, tkwin)
925 Tk_Window tkwin;
926 Tcl_Interp *interp;
927 {
928 CONST char *cursorName;
929 Tk_Cursor cursor;
930
931 cursor = None;
932 if (Tcl_VarEval(interp, Tk_PathName(tkwin), " cget -cursor",
933 (char *)NULL) != TCL_OK) {
934 return None;
935 }
936 cursorName = Tcl_GetStringResult(interp);
937 if ((cursorName != NULL) && (cursorName[0] != '\0')) {
938 cursor = Tk_GetCursor(interp, tkwin, Tk_GetUid((char *)cursorName));
939 }
940 Tcl_ResetResult(interp);
941 return cursor;
942 }
943
944 /*
945 * ------------------------------------------------------------------------
946 *
947 * UpdateToken --
948 *
949 * Invoked when the event loop is idle to determine whether or not
950 * the current drag&drop token position is over another drag&drop
951 * target.
952 *
953 * ------------------------------------------------------------------------
954 */
955 static void
956 UpdateToken(clientData)
957 ClientData clientData; /* widget data */
958 {
959 Source *srcPtr = clientData;
960 Token *tokenPtr = &(srcPtr->token);
961
962 ChangeToken(tokenPtr, tokenPtr->active);
963 /*
964 * If the source has a site command, then invoke it to
965 * modify the appearance of the token window. Pass any
966 * errors onto the drag&drop error handler.
967 */
968 if (srcPtr->siteCmd) {
969 char buffer[200];
970 Tcl_DString dString;
971 int result;
972 SubstDescriptors subs[2];
973
974 sprintf(buffer, "%d", tokenPtr->active);
975 subs[0].letter = 's';
976 subs[0].value = buffer;
977 subs[1].letter = 't';
978 subs[1].value = Tk_PathName(tokenPtr->tkwin);
979
980 Tcl_DStringInit(&dString);
981 result = Tcl_Eval(srcPtr->interp,
982 ExpandPercents(srcPtr->siteCmd, subs, 2, &dString));
983 Tcl_DStringFree(&dString);
984 if ((result != TCL_OK) && (errorCmd != NULL) && (*errorCmd)) {
985 (void)Tcl_VarEval(srcPtr->interp, errorCmd, " {",
986 Tcl_GetStringResult(srcPtr->interp), "}", (char *)NULL);
987 }
988 }
989 }
990
991 /*
992 * ------------------------------------------------------------------------
993 *
994 * RejectToken --
995 *
996 * Draws a rejection mark on the current drag&drop token, and arranges
997 * for the token to be unmapped after a small delay.
998 *
999 * ------------------------------------------------------------------------
1000 */
1001 static void
1002 RejectToken(tokenPtr)
1003 Token *tokenPtr;
1004 {
1005 int divisor = 6; /* controls size of rejection symbol */
1006 int w, h, lineWidth, x, y, margin;
1007
1008 margin = 2 * tokenPtr->borderWidth;
1009 w = Tk_Width(tokenPtr->tkwin) - 2 * margin;
1010 h = Tk_Height(tokenPtr->tkwin) - 2 * margin;
1011 lineWidth = (w < h) ? w / divisor : h / divisor;
1012 lineWidth = (lineWidth < 1) ? 1 : lineWidth;
1013
1014 w = h = lineWidth * (divisor - 1);
1015 x = (Tk_Width(tokenPtr->tkwin) - w) / 2;
1016 y = (Tk_Height(tokenPtr->tkwin) - h) / 2;
1017
1018 /*
1019 * Draw the rejection symbol background (\) on the token window...
1020 */
1021 XSetLineAttributes(Tk_Display(tokenPtr->tkwin), tokenPtr->rejectBgGC,
1022 lineWidth + 4, LineSolid, CapButt, JoinBevel);
1023
1024 XDrawArc(Tk_Display(tokenPtr->tkwin), Tk_WindowId(tokenPtr->tkwin),
1025 tokenPtr->rejectBgGC, x, y, w, h, 0, 23040);
1026
1027 XDrawLine(Tk_Display(tokenPtr->tkwin), Tk_WindowId(tokenPtr->tkwin),
1028 tokenPtr->rejectBgGC, x + lineWidth, y + lineWidth, x + w - lineWidth,
1029 y + h - lineWidth);
1030
1031 /*
1032 * Draw the rejection symbol foreground (\) on the token window...
1033 */
1034 XSetLineAttributes(Tk_Display(tokenPtr->tkwin), tokenPtr->rejectFgGC,
1035 lineWidth, LineSolid, CapButt, JoinBevel);
1036
1037 XDrawArc(Tk_Display(tokenPtr->tkwin), Tk_WindowId(tokenPtr->tkwin),
1038 tokenPtr->rejectFgGC, x, y, w, h, 0, 23040);
1039
1040 XDrawLine(Tk_Display(tokenPtr->tkwin), Tk_WindowId(tokenPtr->tkwin),
1041 tokenPtr->rejectFgGC, x + lineWidth, y + lineWidth, x + w - lineWidth,
1042 y + h - lineWidth);
1043
1044 /*
1045 * Arrange for token window to disappear eventually.
1046 */
1047 tokenPtr->timer = Tcl_CreateTimerHandler(1000, (Tcl_TimerProc *) HideToken,
1048 tokenPtr);
1049 }
1050
1051 /*
1052 * ------------------------------------------------------------------------
1053 *
1054 * ConfigureToken --
1055 *
1056 * ------------------------------------------------------------------------
1057 */
1058 static int
1059 ConfigureToken(interp, srcPtr, argc, argv)
1060 Tcl_Interp *interp;
1061 Source *srcPtr;
1062 int argc;
1063 char **argv;
1064 {
1065 Token *tokenPtr;
1066
1067 tokenPtr = &(srcPtr->token);
1068 if (Blt_ConfigureWidget(interp, srcPtr->tkwin, tokenConfigSpecs, argc, argv,
1069 (char *)tokenPtr, TK_CONFIG_ARGV_ONLY) != TCL_OK) {
1070 return TCL_ERROR;
1071 }
1072 return ConfigureSource(interp, srcPtr, 0, (char **)NULL,
1073 TK_CONFIG_ARGV_ONLY);
1074 }
1075
1076 /*
1077 * ------------------------------------------------------------------------
1078 *
1079 * CreateToken --
1080 *
1081 * ------------------------------------------------------------------------
1082 */
1083 static int
1084 CreateToken(interp, srcPtr)
1085 Tcl_Interp *interp;
1086 Source *srcPtr;
1087 {
1088 XSetWindowAttributes attrs;
1089 Tk_Window tkwin;
1090 char string[200];
1091 static int nextTokenId = 0;
1092 unsigned int mask;
1093 Token *tokenPtr = &(srcPtr->token);
1094
1095 sprintf(string, "dd-token%d", ++nextTokenId);
1096
1097 /* Create toplevel on parent's screen. */
1098 tkwin = Tk_CreateWindow(interp, srcPtr->tkwin, string, "");
1099 if (tkwin == NULL) {
1100 return TCL_ERROR;
1101 }
1102 Tk_SetClass(tkwin, className);
1103 Tk_CreateEventHandler(tkwin, ExposureMask | StructureNotifyMask,
1104 TokenEventProc, tokenPtr);
1105
1106 attrs.override_redirect = True;
1107 attrs.backing_store = WhenMapped;
1108 attrs.save_under = True;
1109 mask = CWOverrideRedirect | CWSaveUnder | CWBackingStore;
1110 Tk_ChangeWindowAttributes(tkwin, mask, &attrs);
1111
1112 Tk_SetInternalBorder(tkwin, tokenPtr->borderWidth + 2);
1113 tokenPtr->tkwin = tkwin;
1114 #ifdef WIN32
1115 {
1116 Tk_FakeWin *winPtr = (Tk_FakeWin *) tkwin;
1117 winPtr->dummy18 = tokenPtr;
1118 }
1119 #endif /* WIN32 */
1120 Tk_MakeWindowExist(tkwin);
1121 return TCL_OK;
1122 }
1123
1124 /*
1125 * ------------------------------------------------------------------------
1126 *
1127 * CreateSource --
1128 *
1129 * Looks for a Source record in the hash table for drag&drop source
1130 * widgets. Creates a new record if the widget name is not already
1131 * registered. Returns a pointer to the desired record.
1132 *
1133 * ------------------------------------------------------------------------
1134 */
1135 static Source *
1136 CreateSource(interp, pathName, newPtr)
1137 Tcl_Interp *interp;
1138 char *pathName; /* widget pathname for desired record */
1139 int *newPtr; /* returns non-zero => new record created */
1140 {
1141 Blt_HashEntry *hPtr;
1142 Tk_Window tkwin;
1143 Source *srcPtr;
1144
1145 tkwin = Tk_NameToWindow(interp, pathName, Tk_MainWindow(interp));
1146 if (tkwin == NULL) {
1147 return NULL;
1148 }
1149 hPtr = Blt_CreateHashEntry(&sourceTable, (char *)tkwin, newPtr);
1150 if (!(*newPtr)) {
1151 return (Source *) Blt_GetHashValue(hPtr);
1152 }
1153 srcPtr = Blt_Calloc(1, sizeof(Source));
1154 assert(srcPtr);
1155 srcPtr->tkwin = tkwin;
1156 srcPtr->display = Tk_Display(tkwin);
1157 srcPtr->interp = interp;
1158 srcPtr->token.anchor = TK_ANCHOR_SE;
1159 srcPtr->token.relief = TK_RELIEF_RAISED;
1160 srcPtr->token.activeRelief = TK_RELIEF_SUNKEN;
1161 srcPtr->token.borderWidth = srcPtr->token.activeBorderWidth = 3;
1162 srcPtr->hashPtr = hPtr;
1163 Blt_InitHashTable(&(srcPtr->handlerTable), BLT_STRING_KEYS);
1164 if (ConfigureSource(interp, srcPtr, 0, (char **)NULL, 0) != TCL_OK) {
1165 DestroySource(srcPtr);
1166 return NULL;
1167 }
1168 Blt_SetHashValue(hPtr, srcPtr);
1169 /*
1170 * Arrange for the window to unregister itself when it
1171 * is destroyed.
1172 */
1173 Tk_CreateEventHandler(tkwin, StructureNotifyMask, SourceEventProc, srcPtr);
1174 return srcPtr;
1175 }
1176
1177 /*
1178 * ------------------------------------------------------------------------
1179 *
1180 * DestroySource --
1181 *
1182 * Looks for a Source record in the hash table for drag&drop source
1183 * widgets. Destroys the record if found.
1184 *
1185 * ------------------------------------------------------------------------
1186 */
1187 static void
1188 DestroySource(srcPtr)
1189 Source *srcPtr;
1190 {
1191 Blt_HashEntry *hPtr;
1192 Blt_HashSearch cursor;
1193 char *cmd;
1194
1195 Tcl_CancelIdleCall(UpdateToken, srcPtr);
1196 if (srcPtr->token.timer) {
1197 Tcl_DeleteTimerHandler(srcPtr->token.timer);
1198 }
1199 Tk_FreeOptions(configSpecs, (char *)srcPtr, srcPtr->display, 0);
1200
1201 if (srcPtr->token.rejectFgGC != NULL) {
1202 Tk_FreeGC(srcPtr->display, srcPtr->token.rejectFgGC);
1203 }
1204 if (srcPtr->token.rejectBgGC != NULL) {
1205 Tk_FreeGC(srcPtr->display, srcPtr->token.rejectBgGC);
1206 }
1207 if (srcPtr->pkgCmdResult) {
1208 Blt_Free(srcPtr->pkgCmdResult);
1209 }
1210 if (srcPtr->rootPtr != NULL) {
1211 RemoveWindow(srcPtr->rootPtr);
1212 }
1213 if (srcPtr->cursor != None) {
1214 Tk_FreeCursor(srcPtr->display, srcPtr->cursor);
1215 }
1216 if (srcPtr->token.cursor != None) {
1217 Tk_FreeCursor(srcPtr->display, srcPtr->token.cursor);
1218 }
1219 Blt_Free(srcPtr->sendTypes);
1220
1221 for (hPtr = Blt_FirstHashEntry(&(srcPtr->handlerTable), &cursor);
1222 hPtr != NULL; hPtr = Blt_NextHashEntry(&cursor)) {
1223 cmd = (char *)Blt_GetHashValue(hPtr);
1224 if (cmd != NULL) {
1225 Blt_Free(cmd);
1226 }
1227 }
1228 Blt_DeleteHashTable(&(srcPtr->handlerTable));
1229 if (srcPtr->hashPtr != NULL) {
1230 Blt_DeleteHashEntry(&sourceTable, srcPtr->hashPtr);
1231 }
1232 Blt_Free(srcPtr);
1233 }
1234
1235 /*
1236 * ------------------------------------------------------------------------
1237 *
1238 * GetSource --
1239 *
1240 * Looks for a Source record in the hash table for drag&drop source
1241 * widgets. Returns a pointer to the desired record.
1242 *
1243 * ------------------------------------------------------------------------
1244 */
1245 static int
1246 GetSource(interp, pathName, srcPtrPtr)
1247 Tcl_Interp *interp;
1248 char *pathName; /* widget pathname for desired record */
1249 Source **srcPtrPtr;
1250 {
1251 Blt_HashEntry *hPtr;
1252 Tk_Window tkwin;
1253
1254 tkwin = Tk_NameToWindow(interp, pathName, Tk_MainWindow(interp));
1255 if (tkwin == NULL) {
1256 return TCL_ERROR;
1257 }
1258 hPtr = Blt_FindHashEntry(&sourceTable, (char *)tkwin);
1259 if (hPtr == NULL) {
1260 Tcl_AppendResult(interp, "window \"", pathName,
1261 "\" has not been initialized as a drag&drop source", (char *)NULL);
1262 return TCL_ERROR;
1263 }
1264 *srcPtrPtr = (Source *)Blt_GetHashValue(hPtr);
1265 return TCL_OK;
1266 }
1267
1268 /*
1269 * ------------------------------------------------------------------------
1270 *
1271 * ConfigureSource --
1272 *
1273 * Called to process an (argc,argv) list to configure (or
1274 * reconfigure) a drag&drop source widget.
1275 *
1276 * ------------------------------------------------------------------------
1277 */
1278 static int
1279 ConfigureSource(interp, srcPtr, argc, argv, flags)
1280 Tcl_Interp *interp; /* current interpreter */
1281 register Source *srcPtr; /* drag&drop source widget record */
1282 int argc; /* number of arguments */
1283 char **argv; /* argument strings */
1284 int flags; /* flags controlling interpretation */
1285 {
1286 unsigned long gcMask;
1287 XGCValues gcValues;
1288 GC newGC;
1289 Tcl_DString dString;
1290 Tcl_CmdInfo cmdInfo;
1291 int result;
1292
1293 /*
1294 * Handle the bulk of the options...
1295 */
1296 if (Blt_ConfigureWidget(interp, srcPtr->tkwin, configSpecs, argc, argv,
1297 (char *)srcPtr, flags) != TCL_OK) {
1298 return TCL_ERROR;
1299 }
1300 /*
1301 * Check the button binding for valid range (0 or 1-5)
1302 */
1303 if ((srcPtr->button < 0) || (srcPtr->button > 5)) {
1304 Tcl_AppendResult(interp,
1305 "button number must be 1-5, or 0 for no bindings",
1306 (char *)NULL);
1307 return TCL_ERROR;
1308 }
1309 /*
1310 * Set up the rejection foreground GC for the token window...
1311 */
1312 gcValues.foreground = srcPtr->token.rejectFg->pixel;
1313 gcValues.subwindow_mode = IncludeInferiors;
1314 gcValues.graphics_exposures = False;
1315 gcMask = GCForeground | GCSubwindowMode | GCGraphicsExposures;
1316
1317 if (srcPtr->token.rejectStipple != None) {
1318 gcValues.stipple = srcPtr->token.rejectStipple;
1319 gcValues.fill_style = FillStippled;
1320 gcMask |= GCForeground | GCStipple | GCFillStyle;
1321 }
1322 newGC = Tk_GetGC(srcPtr->tkwin, gcMask, &gcValues);
1323
1324 if (srcPtr->token.rejectFgGC != NULL) {
1325 Tk_FreeGC(srcPtr->display, srcPtr->token.rejectFgGC);
1326 }
1327 srcPtr->token.rejectFgGC = newGC;
1328
1329 /*
1330 * Set up the rejection background GC for the token window...
1331 */
1332 gcValues.foreground = srcPtr->token.rejectBg->pixel;
1333 gcValues.subwindow_mode = IncludeInferiors;
1334 gcValues.graphics_exposures = False;
1335 gcMask = GCForeground | GCSubwindowMode | GCGraphicsExposures;
1336
1337 newGC = Tk_GetGC(srcPtr->tkwin, gcMask, &gcValues);
1338
1339 if (srcPtr->token.rejectBgGC != NULL) {
1340 Tk_FreeGC(srcPtr->display, srcPtr->token.rejectBgGC);
1341 }
1342 srcPtr->token.rejectBgGC = newGC;
1343
1344 /*
1345 * Reset the border width in case it has changed...
1346 */
1347 if (srcPtr->token.tkwin) {
1348 Tk_SetInternalBorder(srcPtr->token.tkwin,
1349 srcPtr->token.borderWidth + 2);
1350 }
1351 if (!Tcl_GetCommandInfo(interp, "blt::Drag&DropInit", &cmdInfo)) {
1352 static char cmd[] = "source [file join $blt_library dragdrop.tcl]";
1353
1354 if (Tcl_GlobalEval(interp, cmd) != TCL_OK) {
1355 Tcl_AddErrorInfo(interp,
1356 "\n (while loading bindings for blt::drag&drop)");
1357 return TCL_ERROR;
1358 }
1359 }
1360 Tcl_DStringInit(&dString);
1361 Blt_DStringAppendElements(&dString, "blt::Drag&DropInit",
1362 Tk_PathName(srcPtr->tkwin), Blt_Itoa(srcPtr->button), (char *)NULL);
1363 result = Tcl_Eval(interp, Tcl_DStringValue(&dString));
1364 Tcl_DStringFree(&dString);
1365 return result;
1366 }
1367
1368 /*
1369 * ------------------------------------------------------------------------
1370 *
1371 * SourceEventProc --
1372 *
1373 * Invoked by Tk_HandleEvent whenever a DestroyNotify event is received
1374 * on a registered drag&drop source widget.
1375 *
1376 * ------------------------------------------------------------------------
1377 */
1378 static void
1379 SourceEventProc(clientData, eventPtr)
1380 ClientData clientData; /* drag&drop registration list */
1381 XEvent *eventPtr; /* event description */
1382 {
1383 Source *srcPtr = (Source *) clientData;
1384
1385 if (eventPtr->type == DestroyNotify) {
1386 DestroySource(srcPtr);
1387 }
1388 }
1389
1390 /*
1391 * ------------------------------------------------------------------------
1392 *
1393 * FindTarget --
1394 *
1395 * Looks for a Target record in the hash table for drag&drop
1396 * target widgets. Creates a new record if the widget name is
1397 * not already registered. Returns a pointer to the desired
1398 * record.
1399 *
1400 * ------------------------------------------------------------------------
1401 */
1402 static Target *
1403 FindTarget(tkwin)
1404 Tk_Window tkwin; /* Widget pathname for desired record */
1405 {
1406 Blt_HashEntry *hPtr;
1407
1408 hPtr = Blt_FindHashEntry(&targetTable, (char *)tkwin);
1409 if (hPtr == NULL) {
1410 return NULL;
1411 }
1412 return (Target *) Blt_GetHashValue(hPtr);
1413 }
1414
1415
1416 /*
1417 * ------------------------------------------------------------------------
1418 *
1419 * CreateTarget --
1420 *
1421 * Looks for a Target record in the hash table for drag&drop
1422 * target widgets. Creates a new record if the widget name is
1423 * not already registered. Returns a pointer to the desired
1424 * record.
1425 *
1426 * ------------------------------------------------------------------------
1427 */
1428 static Target *
1429 CreateTarget(interp, tkwin)
1430 Tcl_Interp *interp;
1431 Tk_Window tkwin; /* Widget pathname for desired record */
1432 {
1433 Target *targetPtr;
1434 int isNew;
1435
1436 targetPtr = Blt_Calloc(1, sizeof(Target));
1437 assert(targetPtr);
1438 targetPtr->display = Tk_Display(tkwin);
1439 targetPtr->tkwin = tkwin;
1440 Blt_InitHashTable(&(targetPtr->handlerTable), BLT_STRING_KEYS);
1441 targetPtr->hashPtr = Blt_CreateHashEntry(&targetTable, (char *)tkwin,
1442 &isNew);
1443 Blt_SetHashValue(targetPtr->hashPtr, targetPtr);
1444
1445 /*
1446 * Arrange for the target to removed if the host window is destroyed.
1447 */
1448 Tk_CreateEventHandler(tkwin, StructureNotifyMask, TargetEventProc,
1449 targetPtr);
1450 /*
1451 * If this is a new target, attach a property to identify
1452 * window as "drag&drop" target, and arrange for the window
1453 * to un-register itself when it is destroyed.
1454 */
1455 Tk_MakeWindowExist(targetPtr->tkwin);
1456 AddTargetProperty(interp, targetPtr);
1457 return targetPtr;
1458 }
1459
1460 /*
1461 * ------------------------------------------------------------------------
1462 *
1463 * DestroyTarget --
1464 *
1465 * ------------------------------------------------------------------------
1466 */
1467 static void
1468 DestroyTarget(data)
1469 DestroyData data;
1470 {
1471 Target *targetPtr = (Target *)data;
1472 Blt_HashEntry *hPtr;
1473 Blt_HashSearch cursor;
1474 char *cmd;
1475
1476 for (hPtr = Blt_FirstHashEntry(&(targetPtr->handlerTable), &cursor);
1477 hPtr != NULL; hPtr = Blt_NextHashEntry(&cursor)) {
1478 cmd = (char *)Blt_GetHashValue(hPtr);
1479 if (cmd != NULL) {
1480 Blt_Free(cmd);
1481 }
1482 }
1483 Blt_DeleteHashTable(&(targetPtr->handlerTable));
1484 if (targetPtr->hashPtr != NULL) {
1485 Blt_DeleteHashEntry(&targetTable, targetPtr->hashPtr);
1486 }
1487 Tk_DeleteEventHandler(targetPtr->tkwin, StructureNotifyMask,
1488 TargetEventProc, targetPtr);
1489 Blt_Free(targetPtr);
1490 }
1491
1492 /*
1493 * ------------------------------------------------------------------------
1494 *
1495 * TargetEventProc --
1496 *
1497 * Invoked by Tk_HandleEvent whenever a DestroyNotify event is received
1498 * on a registered drag&drop target widget.
1499 *
1500 * ------------------------------------------------------------------------
1501 */
1502 static void
1503 TargetEventProc(clientData, eventPtr)
1504 ClientData clientData; /* drag&drop registration list */
1505 XEvent *eventPtr; /* event description */
1506 {
1507 Target *targetPtr = (Target *) clientData;
1508
1509 if (eventPtr->type == DestroyNotify) {
1510 #ifdef WIN32
1511 /*
1512 * Under Win32 the properties must be removed before the window
1513 * can be destroyed.
1514 */
1515 RemoveProperty(targetPtr->tkwin);
1516 #endif
1517 DestroyTarget((DestroyData)targetPtr);
1518 }
1519 }
1520
1521 /*
1522 * ------------------------------------------------------------------------
1523 *
1524 * DndSend --
1525 *
1526 * Invoked after a drop operation to send data to the drop
1527 * application.
1528 *
1529 * ------------------------------------------------------------------------
1530 */
1531 static void
1532 DndSend(srcPtr)
1533 register Source *srcPtr; /* drag&drop source record */
1534 {
1535 int status;
1536 SubstDescriptors subs[3];
1537 Tcl_DString dString;
1538 Blt_HashEntry *hPtr;
1539 char *dataType;
1540 char **targetInfo;
1541 char *cmd;
1542
1543 /* See if current position is over drop point. */
1544 if (!OverTarget(srcPtr, srcPtr->token.lastX, srcPtr->token.lastY)) {
1545 return;
1546 }
1547 targetInfo = srcPtr->windowPtr->targetInfo;
1548 Tcl_DStringInit(&dString);
1549 Blt_DStringAppendElements(&dString, "send", targetInfo[INTERP_NAME],
1550 dragDropCmd, "location", (char *)NULL);
1551 Tcl_DStringAppendElement(&dString, Blt_Itoa(srcPtr->token.lastX));
1552 Tcl_DStringAppendElement(&dString, Blt_Itoa(srcPtr->token.lastY));
1553 status = Tcl_Eval(srcPtr->interp, Tcl_DStringValue(&dString));
1554
1555 Tcl_DStringFree(&dString);
1556 if (status != TCL_OK) {
1557 goto reject;
1558 }
1559 if (targetInfo[DATA_TYPE] == NULL) {
1560 Blt_HashSearch cursor;
1561
1562 hPtr = Blt_FirstHashEntry(&(srcPtr->handlerTable), &cursor);
1563 dataType = Blt_GetHashKey(&(srcPtr->handlerTable), hPtr);
1564 } else {
1565 hPtr = Blt_FindHashEntry(&(srcPtr->handlerTable),
1566 targetInfo[DATA_TYPE]);
1567 dataType = targetInfo[DATA_TYPE];
1568 }
1569 /* Start building the command line here, before we invoke any Tcl
1570 * commands. The is because the Tcl command may let in another
1571 * drag event and change the target property data. */
1572 Tcl_DStringInit(&dString);
1573 Blt_DStringAppendElements(&dString, "send", targetInfo[INTERP_NAME],
1574 dragDropCmd, "target", targetInfo[TARGET_NAME], "handle",
1575 dataType, (char *)NULL);
1576 cmd = NULL;
1577 if (hPtr != NULL) {
1578 cmd = (char *)Blt_GetHashValue(hPtr);
1579 }
1580 if (cmd != NULL) {
1581 Tcl_DString cmdString;
1582
1583 subs[0].letter = 'i';
1584 subs[0].value = targetInfo[INTERP_NAME];
1585 subs[1].letter = 'w';
1586 subs[1].value = targetInfo[TARGET_NAME];
1587 subs[2].letter = 'v';
1588 subs[2].value = srcPtr->pkgCmdResult;
1589
1590 Tcl_DStringInit(&cmdString);
1591 status = Tcl_Eval(srcPtr->interp,
1592 ExpandPercents(cmd, subs, 3, &cmdString));
1593 Tcl_DStringFree(&cmdString);
1594 if (status != TCL_OK) {
1595 goto reject;
1596 }
1597 Tcl_DStringAppendElement(&dString, Tcl_GetStringResult(srcPtr->interp));
1598 } else {
1599 Tcl_DStringAppendElement(&dString, srcPtr->pkgCmdResult);
1600 }
1601
1602 /*
1603 * Part 2: Now tell target application to handle the data.
1604 */
1605 status = Tcl_Eval(srcPtr->interp, Tcl_DStringValue(&dString));
1606 Tcl_DStringFree(&dString);
1607 if (status != TCL_OK) {
1608 goto reject;
1609 }
1610 HideToken(&(srcPtr->token));
1611 return;
1612 reject:
1613 /*
1614 * Give failure information to user. If an error occurred and an
1615 * error proc is defined, then use it to handle the error.
1616 */
1617 RejectToken(&(srcPtr->token));
1618 if (errorCmd != NULL) {
1619 Tcl_VarEval(srcPtr->interp, errorCmd, " {",
1620 Tcl_GetStringResult(srcPtr->interp), "}", (char *)NULL);
1621 }
1622 }
1623
1624 /*
1625 * ------------------------------------------------------------------------
1626 *
1627 * InitRoot --
1628 *
1629 * Invoked at the start of a "drag" operation to capture the
1630 * positions of all windows on the current root. Queries the
1631 * entire window hierarchy and determines the placement of each
1632 * window. Queries the "BltDrag&DropTarget" property info where
1633 * appropriate. This information is used during the drag
1634 * operation to determine when the drag&drop token is over a
1635 * valid drag&drop target.
1636 *
1637 * Results:
1638 * Returns the record for the root window, which contains records
1639 * for all other windows as children.
1640 *
1641 * ------------------------------------------------------------------------
1642 */
1643 static void
1644 InitRoot(srcPtr)
1645 Source *srcPtr;
1646 {
1647 srcPtr->rootPtr = Blt_Calloc(1, sizeof(AnyWindow));
1648 assert(srcPtr->rootPtr);
1649 #ifdef WIN32
1650 srcPtr->rootPtr->nativeWindow = GetDesktopWindow();
1651 #else
1652 srcPtr->rootPtr->nativeWindow = DefaultRootWindow(srcPtr->display);
1653 #endif
1654 srcPtr->windowPtr = NULL;
1655 QueryWindow(srcPtr->display, srcPtr->rootPtr);
1656 }
1657
1658 /*
1659 * ------------------------------------------------------------------------
1660 *
1661 * FindTopWindow --
1662 *
1663 * Searches for the topmost window at a given pair of X-Y coordinates.
1664 *
1665 * Results:
1666 * Returns a pointer to the node representing the window containing
1667 * the point. If one can't be found, NULL is returned.
1668 *
1669 * ------------------------------------------------------------------------
1670 */
1671 static AnyWindow *
1672 FindTopWindow(srcPtr, x, y)
1673 Source *srcPtr;
1674 int x, y;
1675 {
1676 AnyWindow *rootPtr;
1677 register Blt_ChainLink *linkPtr;
1678 register AnyWindow *windowPtr;
1679 WINDOW nativeTokenWindow;
1680
1681 rootPtr = srcPtr->rootPtr;
1682 if (!rootPtr->initialized) {
1683 QueryWindow(srcPtr->display, rootPtr);
1684 }
1685 if ((x < rootPtr->x1) || (x > rootPtr->x2) ||
1686 (y < rootPtr->y1) || (y > rootPtr->y2)) {
1687 return NULL; /* Point is not over window */
1688 }
1689 windowPtr = rootPtr;
1690
1691 nativeTokenWindow = (WINDOW)Blt_GetRealWindowId(srcPtr->token.tkwin);
1692 /*
1693 * The window list is ordered top to bottom, so stop when we find
1694 * the first child that contains the X-Y coordinate. It will be
1695 * the topmost window in that hierarchy. If none exists, then we
1696 * already have the topmost window.
1697 */
1698 descend:
1699 for (linkPtr = Blt_ChainFirstLink(rootPtr->chainPtr); linkPtr != NULL;
1700 linkPtr = Blt_ChainNextLink(linkPtr)) {
1701 rootPtr = Blt_ChainGetValue(linkPtr);
1702 if (!rootPtr->initialized) {
1703 QueryWindow(srcPtr->display, rootPtr);
1704 }
1705 if (rootPtr->nativeWindow == nativeTokenWindow) {
1706 continue; /* Don't examine the token window. */
1707 }
1708 if ((x >= rootPtr->x1) && (x <= rootPtr->x2) &&
1709 (y >= rootPtr->y1) && (y <= rootPtr->y2)) {
1710 /*
1711 * Remember the last window containing the pointer and
1712 * descend into its window hierarchy. We'll look for a
1713 * child that also contains the pointer.
1714 */
1715 windowPtr = rootPtr;
1716 goto descend;
1717 }
1718 }
1719 return windowPtr;
1720 }
1721
1722 /*
1723 * ------------------------------------------------------------------------
1724 *
1725 * OverTarget --
1726 *
1727 * Checks to see if a compatible drag&drop target exists at the
1728 * given position. A target is "compatible" if it is a drag&drop
1729 * window, and if it has a handler that is compatible with the
1730 * current source window.
1731 *
1732 * Results:
1733 * Returns a pointer to a structure describing the target, or NULL
1734 * if no compatible target is found.
1735 *
1736 * ------------------------------------------------------------------------
1737 */
1738 static int
1739 OverTarget(srcPtr, x, y)
1740 Source *srcPtr; /* drag&drop source window */
1741 int x, y; /* current drag&drop location
1742 * (in virtual coords) */
1743 {
1744 int virtX, virtY;
1745 int dummy;
1746 AnyWindow *newPtr, *oldPtr;
1747 char **elemArr;
1748 int nElems;
1749 char *data;
1750 int result;
1751
1752 /*
1753 * If no window info has been been gathered yet for this target,
1754 * then abort this call. This probably means that the token is
1755 * moved before it has been properly built.
1756 */
1757 if (srcPtr->rootPtr == NULL) {
1758 return FALSE;
1759 }
1760 if (srcPtr->sendTypes == NULL) {
1761 return FALSE; /* Send is turned off. */
1762 }
1763
1764 /* Adjust current location for virtual root windows. */
1765 Tk_GetVRootGeometry(srcPtr->tkwin, &virtX, &virtY, &dummy, &dummy);
1766 x += virtX;
1767 y += virtY;
1768
1769 oldPtr = srcPtr->windowPtr;
1770 srcPtr->windowPtr = NULL;
1771
1772 newPtr = FindTopWindow(srcPtr, x, y);
1773 if (newPtr == NULL) {
1774 return FALSE; /* Not over a window. */
1775 }
1776 if ((!srcPtr->selfTarget) &&
1777 (GetNativeWindow(srcPtr->tkwin) == newPtr->nativeWindow)) {
1778 return FALSE; /* If the self-target flag isn't set,
1779 * don't allow the source window to
1780 * drop onto itself. */
1781 }
1782 if (newPtr == oldPtr) {
1783 srcPtr->windowPtr = oldPtr;
1784 /* No need to collect the target information if we're still
1785 * over the same window. */
1786 return (newPtr->targetInfo != NULL);
1787 }
1788
1789 /* See if this window has a "BltDrag&DropTarget" property. */
1790 data = GetProperty(srcPtr->display, newPtr->nativeWindow);
1791 if (data == NULL) {
1792 return FALSE; /* No such property on window. */
1793 }
1794 result = Tcl_SplitList(srcPtr->interp, data, &nElems, &elemArr);
1795 XFree((char *)data);
1796 if (result != TCL_OK) {
1797 return FALSE; /* Malformed property list. */
1798 }
1799 srcPtr->windowPtr = newPtr;
1800 /* Interpreter name, target name, type1, type2, ... */
1801 if (nElems > 2) {
1802 register char **s;
1803 int count;
1804 register int i;
1805
1806 /*
1807 * The candidate target has a list of possible types.
1808 * Compare this with what types the source is willing to
1809 * transmit and compress the list down to just the matching
1810 * types. It's up to the target to request the specific type
1811 * it wants.
1812 */
1813 count = 2;
1814 for (i = 2; i < nElems; i++) {
1815 for (s = srcPtr->sendTypes; *s != NULL; s++) {
1816 if (((**s == 'a') && (strcmp(*s, "all") == 0)) ||
1817 ((**s == elemArr[i][0]) && (strcmp(*s, elemArr[i]) == 0))) {
1818 elemArr[count++] = elemArr[i];
1819 }
1820 }
1821 }
1822 if (count == 2) {
1823 Blt_Free(elemArr);
1824 fprintf(stderr, "source/target mismatch: No matching types\n");
1825 return FALSE; /* No matching data type. */
1826 }
1827 elemArr[count] = NULL;
1828 }
1829 newPtr->targetInfo = elemArr;
1830 return TRUE;
1831 }
1832
1833 /*
1834 * ------------------------------------------------------------------------
1835 *
1836 * RemoveWindow --
1837 *
1838 * ------------------------------------------------------------------------
1839 */
1840 static void
1841 RemoveWindow(windowPtr)
1842 AnyWindow *windowPtr; /* window rep to be freed */
1843 {
1844 AnyWindow *childPtr;
1845 Blt_ChainLink *linkPtr;
1846
1847 /* Throw away leftover slots. */
1848 for (linkPtr = Blt_ChainFirstLink(windowPtr->chainPtr); linkPtr != NULL;
1849 linkPtr = Blt_ChainNextLink(linkPtr)) {
1850 childPtr = Blt_ChainGetValue(linkPtr);
1851 RemoveWindow(childPtr);
1852 }
1853 Blt_ChainDestroy(windowPtr->chainPtr);
1854 if (windowPtr->targetInfo != NULL) {
1855 Blt_Free(windowPtr->targetInfo);
1856 }
1857 Blt_Free(windowPtr);
1858 }
1859
1860 /*
1861 * ------------------------------------------------------------------------
1862 *
1863 * QueryWindow --
1864 *
1865 * Invoked during "drag" operations. Digs into the root window
1866 * hierarchy and caches the resulting information.
1867 * If a point coordinate lies within an uninitialized AnyWindow,
1868 * this routine is called to query window coordinates and
1869 * drag&drop info. If this particular window has any children,
1870 * more uninitialized AnyWindow structures are allocated.
1871 * Further queries will cause these structures to be initialized
1872 * in turn.
1873 *
1874 * ------------------------------------------------------------------------
1875 */
1876 static void
1877 QueryWindow(display, windowPtr)
1878 Display *display;
1879 AnyWindow *windowPtr; /* window rep to be initialized */
1880 {
1881 int visible;
1882
1883 if (windowPtr->initialized) {
1884 return;
1885 }
1886 /*
1887 * Query for the window coordinates.
1888 */
1889 visible = GetWindowRegion(display, windowPtr->nativeWindow,
1890 &(windowPtr->x1), &(windowPtr->y1), &(windowPtr->x2), &(windowPtr->y2));
1891 if (visible) {
1892 Blt_ChainLink *linkPtr;
1893 Blt_Chain *chainPtr;
1894 AnyWindow *childPtr;
1895
1896 #ifndef WIN32
1897 /* Add offset from parent's origin to coordinates */
1898 if (windowPtr->parentPtr != NULL) {
1899 windowPtr->x1 += windowPtr->parentPtr->x1;
1900 windowPtr->y1 += windowPtr->parentPtr->y1;
1901 windowPtr->x2 += windowPtr->parentPtr->x1;
1902 windowPtr->y2 += windowPtr->parentPtr->y1;
1903 }
1904 #endif
1905 /*
1906 * Collect a list of child windows, sorted in z-order. The
1907 * topmost window will be first in the list.
1908 */
1909 chainPtr = GetWindowZOrder(display, windowPtr->nativeWindow);
1910
1911 /* Add and initialize extra slots if needed. */
1912 for (linkPtr = Blt_ChainFirstLink(chainPtr); linkPtr != NULL;
1913 linkPtr = Blt_ChainNextLink(linkPtr)) {
1914 childPtr = Blt_Calloc(1, sizeof(AnyWindow));
1915 assert(childPtr);
1916 childPtr->initialized = FALSE;
1917 childPtr->nativeWindow = (WINDOW)Blt_ChainGetValue(linkPtr);
1918 childPtr->parentPtr = windowPtr;
1919 Blt_ChainSetValue(linkPtr, childPtr);
1920 }
1921 windowPtr->chainPtr = chainPtr;
1922 } else {
1923 /* If it's not viewable don't bother doing anything else. */
1924 windowPtr->x1 = windowPtr->y1 = windowPtr->x2 = windowPtr->y2 = -1;
1925 windowPtr->chainPtr = NULL;
1926 }
1927 windowPtr->initialized = TRUE;
1928 }
1929
1930 /*
1931 * ------------------------------------------------------------------------
1932 *
1933 * AddTargetProperty --
1934 *
1935 * Attaches a drag&drop property to the given target window.
1936 * This property allows us to recognize the window later as a
1937 * valid target. It also stores important information including
1938 * the interpreter managing the target and the pathname of the
1939 * target window. Usually this routine is called when the target
1940 * is first registered or first exposed (so that the X-window
1941 * really exists).
1942 *
1943 * ------------------------------------------------------------------------
1944 */
1945 static void
1946 AddTargetProperty(interp, targetPtr)
1947 Tcl_Interp *interp;
1948 Target *targetPtr; /* drag&drop target window data */
1949 {
1950 Tcl_DString dString;
1951 Blt_HashEntry *hPtr;
1952 Blt_HashSearch cursor;
1953
1954 if (targetPtr->tkwin == NULL) {
1955 return;
1956 }
1957 Tcl_DStringInit(&dString);
1958 /*
1959 * Each target window's dnd property contains
1960 *
1961 * 1. name of the application (ie. the interpreter's name).
1962 * 2. Tk path name of the target window.
1963 * 3. List of all the data types that can be handled. If none
1964 * are listed, then all can be handled.
1965 */
1966 Tcl_DStringAppendElement(&dString, Tk_Name(Tk_MainWindow(interp)));
1967 Tcl_DStringAppendElement(&dString, Tk_PathName(targetPtr->tkwin));
1968 for (hPtr = Blt_FirstHashEntry(&(targetPtr->handlerTable), &cursor);
1969 hPtr != NULL; hPtr = Blt_NextHashEntry(&cursor)) {
1970 Tcl_DStringAppendElement(&dString,
1971 Blt_GetHashKey(&(targetPtr->handlerTable), hPtr));
1972 }
1973 SetProperty(targetPtr->tkwin, Tcl_DStringValue(&dString));
1974 Tcl_DStringFree(&dString);
1975 }
1976
1977 /*
1978 * ------------------------------------------------------------------------
1979 *
1980 * ExpandPercents --
1981 *
1982 * Expands all percent substitutions found in the input "str"
1983 * that match specifications in the "subs" list. Any percent
1984 * field that is not found in the "subs" list is left alone.
1985 * Returns a string that remains valid until the next call to
1986 * this routine.
1987 *
1988 * ------------------------------------------------------------------------
1989 */
1990 static char *
1991 ExpandPercents(string, subsArr, nSubs, resultPtr)
1992 char *string; /* Incoming command string */
1993 SubstDescriptors *subsArr; /* Array of known substitutions */
1994 int nSubs; /* Number of elements in subs array */
1995 Tcl_DString *resultPtr;
1996 {
1997 register char *chunk, *p;
1998 char letter;
1999 char percentSign;
2000 int i;
2001
2002 /*
2003 * Scan through the copy of the input string, look for
2004 * the next '%' character, and try to make the substitution.
2005 * Continue doing this to the end of the string.
2006 */
2007 chunk = p = string;
2008 while ((p = strchr(p, '%')) != NULL) {
2009
2010 /* Copy up to the percent sign. Repair the string afterwards */
2011 percentSign = *p;
2012 *p = '\0';
2013 Tcl_DStringAppend(resultPtr, chunk, -1);
2014 *p = percentSign;
2015
2016 /* Search for a matching substition rule */
2017 letter = *(p + 1);
2018 for (i = 0; i < nSubs; i++) {
2019 if (subsArr[i].letter == letter) {
2020 break;
2021 }
2022 }
2023 if (i < nSubs) {
2024 /* Make the substitution */
2025 Tcl_DStringAppend(resultPtr, subsArr[i].value, -1);
2026 } else {
2027 /* Copy in the %letter verbatim */
2028 char verbatim[3];
2029
2030 verbatim[0] = '%';
2031 verbatim[1] = letter;
2032 verbatim[2] = '\0';
2033 Tcl_DStringAppend(resultPtr, verbatim, -1);
2034 }
2035 p += 2; /* Skip % + letter */
2036 if (letter == '\0') {
2037 p += 1; /* Premature % substitution (end of string) */
2038 }
2039 chunk = p;
2040 }
2041 /* Pick up last chunk if a substition wasn't the last thing in the string */
2042 if (*chunk != '\0') {
2043 Tcl_DStringAppend(resultPtr, chunk, -1);
2044 }
2045 return Tcl_DStringValue(resultPtr);
2046 }
2047
2048
2049 static int
2050 DragOp(interp, argc, argv)
2051 Tcl_Interp *interp;
2052 int argc;
2053 char **argv;
2054 {
2055 int x, y;
2056 Token *tokenPtr;
2057 int status;
2058 Source *srcPtr;
2059 SubstDescriptors subst[2];
2060 int active;
2061 CONST char *result;
2062
2063 /*
2064 * HANDLE: drag&drop drag <path> <x> <y>
2065 */
2066 if (argc != 5) {
2067 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
2068 " drag pathname x y\"", (char *)NULL);
2069 return TCL_ERROR;
2070 }
2071 if ((GetSource(interp, argv[2], &srcPtr) != TCL_OK) ||
2072 (Tcl_GetInt(interp, argv[3], &x) != TCL_OK) ||
2073 (Tcl_GetInt(interp, argv[4], &y) != TCL_OK)) {
2074 return TCL_ERROR;
2075 }
2076 tokenPtr = &(srcPtr->token);
2077
2078 tokenPtr->lastX = locX = x; /* Save drag&drop location */
2079 tokenPtr->lastY = locY = y;
2080
2081 /* If HideToken() is pending, then do it now! */
2082 if (tokenPtr->timer != 0) {
2083 Tcl_DeleteTimerHandler(tokenPtr->timer);
2084 HideToken(tokenPtr);
2085 }
2086
2087 /*
2088 * If pkgCmd is in progress, then ignore subsequent calls
2089 * until it completes. Only perform drag if pkgCmd
2090 * completed successfully and token window is mapped.
2091 */
2092 if ((!Tk_IsMapped(tokenPtr->tkwin)) && (!srcPtr->pkgCmdInProgress)) {
2093 Tcl_DString dString;
2094
2095 /*
2096 * No list of send handlers? Then source is disabled.
2097 * Abort drag quietly.
2098 */
2099 if (srcPtr->sendTypes == NULL) {
2100 return TCL_OK;
2101 }
2102 /*
2103 * No token command? Then cannot build token.
2104 * Signal error.
2105 */
2106 if (srcPtr->pkgCmd == NULL) {
2107 Tcl_AppendResult(interp, "missing -packagecmd: ", argv[2],
2108 (char *)NULL);
2109 return TCL_ERROR;
2110 }
2111 /*
2112 * Execute token command to initialize token window.
2113 */
2114 srcPtr->pkgCmdInProgress = TRUE;
2115 subst[0].letter = 'W';
2116 subst[0].value = Tk_PathName(srcPtr->tkwin);
2117 subst[1].letter = 't';
2118 subst[1].value = Tk_PathName(tokenPtr->tkwin);
2119
2120 Tcl_DStringInit(&dString);
2121 status = Tcl_Eval(srcPtr->interp,
2122 ExpandPercents(srcPtr->pkgCmd, subst, 2, &dString));
2123 Tcl_DStringFree(&dString);
2124
2125 srcPtr->pkgCmdInProgress = FALSE;
2126
2127 result = Tcl_GetStringResult(interp);
2128 /*
2129 * Null string from the package command?
2130 * Then quietly abort the drag&drop operation.
2131 */
2132 if (result[0] == '\0') {
2133 return TCL_OK;
2134 }
2135
2136 /* Save result of token command for send command. */
2137 if (srcPtr->pkgCmdResult != NULL) {
2138 Blt_Free(srcPtr->pkgCmdResult);
2139 }
2140 srcPtr->pkgCmdResult = Blt_Strdup(result);
2141 if (status != TCL_OK) {
2142 /*
2143 * Token building failed. If an error handler is defined,
2144 * then signal the error. Otherwise, abort quietly.
2145 */
2146 if ((errorCmd != NULL) && (errorCmd[0] != '\0')) {
2147 return Tcl_VarEval(interp, errorCmd, " {", result, "}",
2148 (char *)NULL);
2149 }
2150 return TCL_OK;
2151 }
2152 /* Install token cursor. */
2153 if (tokenPtr->cursor != None) {
2154 Tk_Cursor cursor;
2155
2156 /* Save the old cursor */
2157 cursor = GetWidgetCursor(srcPtr->interp, srcPtr->tkwin);
2158 if (srcPtr->cursor != None) {
2159 Tk_FreeCursor(srcPtr->display, srcPtr->cursor);
2160 }
2161 srcPtr->cursor = cursor;
2162 /* Temporarily install the token cursor */
2163 Tk_DefineCursor(srcPtr->tkwin, tokenPtr->cursor);
2164 }
2165 /*
2166 * Get ready to drag token window...
2167 * 1) Cache info for all windows on root
2168 * 2) Map token window to begin drag operation
2169 */
2170 if (srcPtr->rootPtr != NULL) {
2171 RemoveWindow(srcPtr->rootPtr);
2172 }
2173 InitRoot(srcPtr);
2174
2175 nActive++; /* One more drag&drop window active */
2176
2177 if (Tk_WindowId(tokenPtr->tkwin) == None) {
2178 Tk_MakeWindowExist(tokenPtr->tkwin);
2179 }
2180 if (!Tk_IsMapped(tokenPtr->tkwin)) {
2181 Tk_MapWindow(tokenPtr->tkwin);
2182 }
2183 RaiseToken(tokenPtr);
2184 }
2185
2186 /* Arrange to update status of token window. */
2187 Tcl_CancelIdleCall(UpdateToken, srcPtr);
2188
2189 active = OverTarget(srcPtr, x, y);
2190 if (tokenPtr->active != active) {
2191 tokenPtr->active = active;
2192 Tcl_DoWhenIdle(UpdateToken, srcPtr);
2193 }
2194 MoveToken(srcPtr, tokenPtr); /* Move token window to current drag point. */
2195 return TCL_OK;
2196 }
2197
2198 /*
2199 * HANDLE: drag&drop drop <path> <x> <y>
2200 */
2201 static int
2202 DropOp(interp, argc, argv)
2203 Tcl_Interp *interp;
2204 int argc;
2205 char **argv;
2206 {
2207 Source *srcPtr;
2208 Token *tokenPtr;
2209 int x, y;
2210
2211 if (argc < 5) {
2212 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
2213 " drop pathname x y\"", (char *)NULL);
2214 return TCL_ERROR;
2215 }
2216 if ((GetSource(interp, argv[2], &srcPtr) != TCL_OK) ||
2217 (Tcl_GetInt(interp, argv[3], &x) != TCL_OK) ||
2218 (Tcl_GetInt(interp, argv[4], &y) != TCL_OK)) {
2219 return TCL_ERROR;
2220 }
2221 tokenPtr = &(srcPtr->token);
2222 tokenPtr->lastX = locX = x; /* Save drag&drop location */
2223 tokenPtr->lastY = locY = y;
2224
2225 /* Put the cursor back to its usual state. */
2226 if (srcPtr->cursor == None) {
2227 Tk_UndefineCursor(srcPtr->tkwin);
2228 } else {
2229 Tk_DefineCursor(srcPtr->tkwin, srcPtr->cursor);
2230 }
2231 Tcl_CancelIdleCall(UpdateToken, srcPtr);
2232
2233 /*
2234 * Make sure that token window was not dropped before it
2235 * was either mapped or packed with info.
2236 */
2237 if ((Tk_IsMapped(tokenPtr->tkwin)) && (!srcPtr->pkgCmdInProgress)) {
2238 int active;
2239
2240 active = OverTarget(srcPtr, tokenPtr->lastX, tokenPtr->lastY);
2241 if (tokenPtr->active != active) {
2242 tokenPtr->active = active;
2243 UpdateToken(srcPtr);
2244 }
2245 if (srcPtr->sendTypes != NULL) {
2246 if (tokenPtr->active) {
2247 DndSend(srcPtr);
2248 } else {
2249 HideToken(tokenPtr);
2250 }
2251 }
2252 nActive--; /* One less active token window. */
2253 }
2254 return TCL_OK;
2255 }
2256
2257 /*
2258 * HANDLE: drag&drop errors ?<proc>?
2259 */
2260 static int
2261 ErrorsOp(interp, argc, argv)
2262 Tcl_Interp *interp;
2263 int argc;
2264 char **argv;
2265 {
2266 if (argc == 3) {
2267 if (errorCmd) {
2268 Blt_Free(errorCmd);
2269 }
2270 errorCmd = Blt_Strdup(argv[2]);
2271 } else if (argc != 2) {
2272 Tcl_AppendResult(interp, "wrong # args: should be \"",
2273 argv[0], " errors ?proc?\"",
2274 (char *)NULL);
2275 return TCL_ERROR;
2276 }
2277 Tcl_SetResult(interp, errorCmd, TCL_VOLATILE);
2278 return TCL_OK;
2279 }
2280
2281 /*
2282 * HANDLE: drag&drop active
2283 */
2284 static int
2285 ActiveOp(interp, argc, argv)
2286 Tcl_Interp *interp;
2287 int argc;
2288 char **argv;
2289 {
2290 if (argc != 2) {
2291 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
2292 " active\"", (char *)NULL);
2293 return TCL_ERROR;
2294 }
2295 Blt_SetBooleanResult(interp, (nActive > 0));
2296 return TCL_OK;
2297 }
2298
2299 /*
2300 * HANDLE: drag&drop location ?<x> <y>?
2301 */
2302 static int
2303 LocationOp(interp, argc, argv)
2304 Tcl_Interp *interp;
2305 int argc;
2306 char **argv;
2307 {
2308 if ((argc != 2) && (argc != 4)) {
2309 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
2310 " location ?x y?\"", (char *)NULL);
2311 return TCL_ERROR;
2312 }
2313 if (argc == 4) {
2314 int x, y;
2315
2316 if ((Tcl_GetInt(interp, argv[2], &x) != TCL_OK) ||
2317 (Tcl_GetInt(interp, argv[3], &y) != TCL_OK)) {
2318 return TCL_ERROR;
2319 }
2320 locX = x;
2321 locY = y;
2322 }
2323 Tcl_AppendElement(interp, Blt_Itoa(locX));
2324 Tcl_AppendElement(interp, Blt_Itoa(locY));
2325 return TCL_OK;
2326 }
2327
2328 /*
2329 * HANDLE: drag&drop token <pathName>
2330 */
2331 static int
2332 TokenOp(interp, argc, argv)
2333 Tcl_Interp *interp;
2334 int argc;
2335 char **argv;
2336 {
2337 Source *srcPtr;
2338
2339 if (GetSource(interp, argv[2], &srcPtr) != TCL_OK) {
2340 return TCL_ERROR;
2341 }
2342 if ((argc > 3) &&
2343 (ConfigureToken(interp, srcPtr, argc - 3, argv + 3) != TCL_OK)) {
2344 return TCL_ERROR;
2345 }
2346 Tcl_SetResult(interp, Tk_PathName(srcPtr->token.tkwin), TCL_VOLATILE);
2347 return TCL_OK;
2348 }
2349
2350 static int
2351 HandlerOpOp(srcPtr, interp, argc, argv)
2352 Source *srcPtr;
2353 Tcl_Interp *interp;
2354 int argc;
2355 char **argv;
2356 {
2357 Blt_HashEntry *hPtr;
2358 Blt_HashSearch cursor;
2359 int isNew;
2360 char *cmd;
2361
2362 /*
2363 * HANDLE: drag&drop source <pathName> handler \
2364 * ?<data>? ?<scmd>...?
2365 */
2366 if (argc == 4) {
2367 /* Show source handler data types */
2368 for (hPtr = Blt_FirstHashEntry(&(srcPtr->handlerTable), &cursor);
2369 hPtr != NULL; hPtr = Blt_NextHashEntry(&cursor)) {
2370 Tcl_AppendElement(interp,
2371 Blt_GetHashKey(&(srcPtr->handlerTable), hPtr));
2372 }
2373 return TCL_OK;
2374 }
2375 hPtr = Blt_CreateHashEntry(&(srcPtr->handlerTable), argv[4], &isNew);
2376
2377 /*
2378 * HANDLE: drag&drop source <pathName> handler <data>
2379 *
2380 * Create the new <data> type if it doesn't already
2381 * exist, and return the code associated with it.
2382 */
2383 if (argc == 5) {
2384 cmd = (char *)Blt_GetHashValue(hPtr);
2385 if (cmd == NULL) {
2386 cmd = "";
2387 }
2388 Tcl_SetResult(interp, cmd, TCL_VOLATILE);
2389 return TCL_OK;
2390 }
2391 /*
2392 * HANDLE: drag&drop source <pathName> handler \
2393 * <data> <cmd> ?<arg>...?
2394 *
2395 * Create the new <data> type and set its command
2396 */
2397 cmd = Tcl_Concat(argc - 5, argv + 5);
2398 Blt_SetHashValue(hPtr, cmd);
2399 return TCL_OK;
2400 }
2401
2402 /*
2403 * HANDLE: drag&drop source
2404 * drag&drop source <pathName> ?options...?
2405 * drag&drop source <pathName> handler ?<data>? ?<scmd> <arg>...?
2406 */
2407 static int
2408 SourceOp(interp, argc, argv)
2409 Tcl_Interp *interp;
2410 int argc;
2411 char **argv;
2412 {
2413 Source *srcPtr;
2414 int isNew;
2415 Token *tokenPtr;
2416
2417 if (argc == 2) {
2418 Blt_HashSearch cursor;
2419 Blt_HashEntry *hPtr;
2420 Tk_Window tkwin;
2421
2422 for (hPtr = Blt_FirstHashEntry(&sourceTable, &cursor);
2423 hPtr != NULL; hPtr = Blt_NextHashEntry(&cursor)) {
2424 tkwin = (Tk_Window)Blt_GetHashKey(&sourceTable, hPtr);
2425 Tcl_AppendElement(interp, Tk_PathName(tkwin));
2426 }
2427 return TCL_OK;
2428 }
2429 /*
2430 * Find or create source info...
2431 */
2432 srcPtr = CreateSource(interp, argv[2], &isNew);
2433 if (srcPtr == NULL) {
2434 return TCL_ERROR;
2435 }
2436 tokenPtr = &(srcPtr->token);
2437 if (argc > 3) {
2438 char c;
2439 int length;
2440 int status;
2441
2442 /*
2443 * HANDLE: drag&drop source <pathName> ?options...?
2444 */
2445 c = argv[3][0];
2446 length = strlen(argv[3]);
2447
2448 if (c == '-') {
2449 if (argc == 3) {
2450 status = Tk_ConfigureInfo(interp, tokenPtr->tkwin, configSpecs,
2451 (char *)srcPtr, (char *)NULL, 0);
2452 } else if (argc == 4) {
2453 status = Tk_ConfigureInfo(interp, tokenPtr->tkwin, configSpecs,
2454 (char *)srcPtr, argv[3], 0);
2455 } else {
2456 status = ConfigureSource(interp, srcPtr, argc - 3, argv + 3,
2457 TK_CONFIG_ARGV_ONLY);
2458 }
2459 if (status != TCL_OK) {
2460 return TCL_ERROR;
2461 }
2462 } else if ((c == 'h') && strncmp(argv[3], "handler", length) == 0) {
2463 return HandlerOpOp(srcPtr, interp, argc, argv);
2464 } else {
2465 Tcl_AppendResult(interp, "bad operation \"", argv[3],
2466 "\": must be \"handler\" or a configuration option",
2467 (char *)NULL);
2468 return TCL_ERROR;
2469 }
2470 }
2471 if (isNew) {
2472 /*
2473 * Create the window for the drag&drop token...
2474 */
2475 if (CreateToken(interp, srcPtr) != TCL_OK) {
2476 DestroySource(srcPtr);
2477 return TCL_ERROR;
2478 }
2479 }
2480 return TCL_OK;
2481 }
2482
2483 /*
2484 * HANDLE: drag&drop target ?<pathName>? ?handling info...?
2485 */
2486 static int
2487 TargetOp(interp, argc, argv)
2488 Tcl_Interp *interp;
2489 int argc;
2490 char **argv;
2491 {
2492 SubstDescriptors subst[2];
2493 Tk_Window tkwin;
2494 Blt_HashEntry *hPtr;
2495 Target *targetPtr;
2496 int isNew;
2497
2498 if (argc == 2) {
2499 Blt_HashSearch cursor;
2500
2501 for (hPtr = Blt_FirstHashEntry(&targetTable, &cursor);
2502 hPtr != NULL; hPtr = Blt_NextHashEntry(&cursor)) {
2503 tkwin = (Tk_Window)Blt_GetHashKey(&targetTable, hPtr);
2504 Tcl_AppendElement(interp, Tk_PathName(tkwin));
2505 }
2506 return TCL_OK;
2507 }
2508 tkwin = Tk_NameToWindow(interp, argv[2], Tk_MainWindow(interp));
2509 if (tkwin == NULL) {
2510 return TCL_ERROR;
2511 }
2512 targetPtr = FindTarget(tkwin);
2513 if (targetPtr == NULL) {
2514 targetPtr = CreateTarget(interp, tkwin);
2515 }
2516 if (targetPtr == NULL) {
2517 return TCL_ERROR;
2518 }
2519
2520 if ((argc >= 4) && (strcmp(argv[3], "handler") == 0)) {
2521 /*
2522 * HANDLE: drag&drop target <pathName> handler
2523 * drag&drop target <pathName> handler ?<data> <cmd> <arg>...?
2524 */
2525 if (argc == 4) {
2526 Blt_HashSearch cursor;
2527
2528 for (hPtr = Blt_FirstHashEntry(&(targetPtr->handlerTable), &cursor);
2529 hPtr != NULL; hPtr = Blt_NextHashEntry(&cursor)) {
2530 Tcl_AppendElement(interp,
2531 Blt_GetHashKey(&(targetPtr->handlerTable), hPtr));
2532 }
2533 return TCL_OK;
2534 } else if (argc >= 6) {
2535 char *cmd;
2536
2537 /*
2538 * Process handler definition
2539 */
2540 hPtr = Blt_CreateHashEntry(&(targetPtr->handlerTable), argv[4],
2541 &isNew);
2542 cmd = Tcl_Concat(argc - 5, argv + 5);
2543 if (hPtr != NULL) {
2544 char *oldCmd;
2545
2546 oldCmd = (char *)Blt_GetHashValue(hPtr);
2547 if (oldCmd != NULL) {
2548 Blt_Free(oldCmd);
2549 }
2550 }
2551 Blt_SetHashValue(hPtr, cmd);
2552 /*
2553 * Update the target property on the window.
2554 */
2555 AddTargetProperty(interp, targetPtr);
2556 return TCL_OK;
2557 }
2558 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
2559 " ", argv[1], " ", argv[2], " ", argv[3],
2560 " data command ?arg arg...?", (char *)NULL);
2561 return TCL_ERROR;
2562 } else if ((argc >= 4) && (strcmp(argv[3], "handle") == 0)) {
2563 /*
2564 * HANDLE: drag&drop target <pathName> handle <data> ?<value>?
2565 */
2566 Tcl_DString dString;
2567 int result;
2568 char *cmd;
2569
2570 if (argc < 5 || argc > 6) {
2571 Tcl_AppendResult(interp, "wrong # args: should be \"",
2572 argv[0], " ", argv[1], " ", argv[2], " handle data ?value?",
2573 (char *)NULL);
2574 return TCL_ERROR;
2575 }
2576 hPtr = Blt_FindHashEntry(&(targetPtr->handlerTable), argv[4]);
2577 if (hPtr == NULL) {
2578 Tcl_AppendResult(interp, "target can't handle datatype: ",
2579 argv[4], (char *)NULL);
2580 return TCL_ERROR; /* no handler found */
2581 }
2582 cmd = (char *)Blt_GetHashValue(hPtr);
2583 if (cmd != NULL) {
2584 subst[0].letter = 'W';
2585 subst[0].value = Tk_PathName(targetPtr->tkwin);
2586 subst[1].letter = 'v';
2587 if (argc > 5) {
2588 subst[1].value = argv[5];
2589 } else {
2590 subst[1].value = "";
2591 }
2592 Tcl_DStringInit(&dString);
2593 result = Tcl_Eval(interp, ExpandPercents(cmd, subst, 2, &dString));
2594 Tcl_DStringFree(&dString);
2595 return result;
2596 }
2597 return TCL_OK;
2598 }
2599 Tcl_AppendResult(interp, "usage: ", argv[0], " target ", argv[2],
2600 " handler ?data command arg arg...?\n or: ",
2601 argv[0], " target ", argv[2], " handle <data>",
2602 (char *)NULL);
2603 return TCL_ERROR;
2604 }
2605
2606 /*
2607 * ------------------------------------------------------------------------
2608 *
2609 * DragDropCmd --
2610 *
2611 * Invoked by TCL whenever the user issues a drag&drop command.
2612 * Handles the following syntax:
2613 *
2614 * drag&drop source
2615 * drag&drop source <pathName> ?options...?
2616 * drag&drop source <pathName> handler ?<dataType>? ?<cmd> <arg>...?
2617 *
2618 * drag&drop target
2619 * drag&drop target <pathName> handler ?<dataType> <cmd> <arg>...?
2620 * drag&drop target <pathName> handle <dataType> ?<value>?
2621 *
2622 * drag&drop token <pathName>
2623 * drag&drop drag <pathName> <x> <y>
2624 * drag&drop drop <pathName> <x> <y>
2625 *
2626 * drag&drop errors ?<proc>?
2627 * drag&drop active
2628 * drag&drop location ?<x> <y>?
2629 *
2630 * ------------------------------------------------------------------------
2631 */
2632 /*ARGSUSED*/
2633 static int
2634 DragDropCmd(clientData, interp, argc, argv)
2635 ClientData clientData; /* Not used. */
2636 Tcl_Interp *interp; /* Current interpreter */
2637 int argc; /* # of arguments */
2638 char **argv; /* Argument strings */
2639 {
2640 int length;
2641 char c;
2642
2643 if (argc < 2) {
2644 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
2645 " oper ?args?\"", (char *)NULL);
2646 return TCL_ERROR;
2647 }
2648 c = argv[1][0];
2649 length = strlen(argv[1]);
2650
2651 if ((c == 's') && strncmp(argv[1], "source", length) == 0) {
2652 return SourceOp(interp, argc, argv);
2653 } else if ((c == 't') && (length >= 2) &&
2654 (strncmp(argv[1], "target", length) == 0)) {
2655 return TargetOp(interp, argc, argv);
2656 } else if ((c == 't') && (length >= 2) &&
2657 (strncmp(argv[1], "token", length) == 0)) {
2658 return TokenOp(interp, argc, argv);
2659 } else if ((c == 'd') && strncmp(argv[1], "drag", length) == 0) {
2660 return DragOp(interp, argc, argv);
2661 } else if ((c == 'd') && strncmp(argv[1], "drop", length) == 0) {
2662 return DropOp(interp, argc, argv);
2663 } else if ((c == 'e') && strncmp(argv[1], "errors", length) == 0) {
2664 return ErrorsOp(interp, argc, argv);
2665 } else if ((c == 'a') && strncmp(argv[1], "active", length) == 0) {
2666 return ActiveOp(interp, argc, argv);
2667 } else if ((c == 'l') && strncmp(argv[1], "location", length) == 0) {
2668 return LocationOp(interp, argc, argv);
2669 }
2670 /*
2671 * Report improper command arguments
2672 */
2673 Tcl_AppendResult(interp, "bad operation \"", argv[1],
2674 "\": must be active, drag, drop, errors, location, ",
2675 "source, target or token",
2676 (char *)NULL);
2677 return TCL_ERROR;
2678 }
2679
2680 /*
2681 * ------------------------------------------------------------------------
2682 *
2683 * Blt_DragDropInit --
2684 *
2685 * Adds the drag&drop command to the given interpreter. Should
2686 * be invoked to properly install the command whenever a new
2687 * interpreter is created.
2688 *
2689 * ------------------------------------------------------------------------
2690 */
2691 int
2692 Blt_DragDropInit(interp)
2693 Tcl_Interp *interp; /* interpreter to be updated */
2694 {
2695 static Blt_CmdSpec cmdSpec =
2696 {
2697 "drag&drop", DragDropCmd,
2698 };
2699
2700 if (Blt_InitCmd(interp, "blt", &cmdSpec) == NULL) {
2701 return TCL_ERROR;
2702 }
2703 if (!initialized) {
2704 Blt_InitHashTable(&sourceTable, BLT_ONE_WORD_KEYS);
2705 Blt_InitHashTable(&targetTable, BLT_ONE_WORD_KEYS);
2706 errorCmd = Blt_Strdup(DEF_ERROR_PROC);
2707 nActive = 0;
2708 locX = locY = 0;
2709 initialized = TRUE;
2710 #ifndef WIN32
2711 dndAtom = XInternAtom(Tk_Display(Tk_MainWindow(interp)), propName,
2712 False);
2713 #endif
2714 }
2715 return TCL_OK;
2716 }
2717 #endif /* NO_DRAGDROP */