1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533
|
Content-type: text/html
<HTML><HEAD><TITLE>Manpage of TkDND</TITLE>
</HEAD><BODY>
<H1>TkDND</H1>
Section: Tk Built-In Commands (n)<BR>Updated: 8.4<BR><A HREF="#index">Index</A>
<A HREF="/man/man2html">Return to Main Contents</A><HR>
<BR>
<PRE>
<BR>
<HR></PRE>
<A NAME="lbAB"> </A>
<H2>NAME</H2>
TkDND - Tk Drag and Drop Interface
<A NAME="lbAC"> </A>
<H2>SYNOPSIS</H2>
<B>tkdnd::drop_target register</B><I> window ?type-list?</I>
<BR>
<B>tkdnd::drop_target unregister</B><I> window</I>
<P>
<B>tkdnd::drag_source register</B><I> window ?type-list? ?mouse-buttons?</I>
<BR>
<B>tkdnd::drag_source unregister</B><I> window</I>
<P>
<B>tkdnd::platform_specific_types</B><I> type-list</I>
<BR>
<B>tkdnd::platform_independent_types</B><I> type-list</I>
<P>
<B>tkdnd::GetDropFileTempDirectory</B>
<BR>
<B>tkdnd::SetDropFileTempDirectory </B><I>directory</I>
<P>
<A NAME="lbAD"> </A>
<H2>INTRODUCTION</H2>
The <B>TkDND</B> family commands provide a Tcl interface to native,
platform specific drag and drop mechanism.
Under Unix the drag & drop protocol in use is the XDND protocol version 5
(also used by the Qt toolkit, and the KDE and GNOME desktops).
Under Windows, the OLE2 drag & drop interfaces are used.
Under Macintosh, the Cocoa drag and drop interfaces are used.
<P>
The TkDND family can be built from source code using the standard configure; make; make install procedure. Cmake is also supported. Some binary package are available from the SourceForge download site, from ActiveState's ActiveTcl distribution, and elsewhere.
<P>
With the <B>TkDND</B> family commands the user is able to register existing Tk
widgets as drag sources or drop targets, that are able to send or receive data
during drag and drop operations, encoded in specific types.
These types can be platform independent types like DND_Text and DND_Files or
platform specific types, like CF_UNICODETEXT or text/plain.
<P>
The legal forms for the <B>TkDND</B> commands are:
<DL COMPACT>
<DT><B>tkdnd::drop_target register</B><I> window ?type-list?</I><DD>
This command will register <I>window</I> as a drop target. A drop target is a
widget than can accept a drop action. <I>window</I> must exist when this command
is executed and this command can be executed multiple times on a widget.
<P>
When <I>window</I> is registered as a drop target, and optional <I>type-list</I>
can be provided. This type list can contain one or more types that <I>window</I>
will accept during a drop action, and it can contain platform independent or
platform specific types. Platform independent are <B>DND_Text</B>
for dropping text portions and <B>DND_Files</B> for dropping a list of files
(which can contain one or multiple files) on <I>window</I>.
If <I>type-list</I> is not specified, it defaults to the empty list.
<DT><B>tkdnd::drop_target unregister</B><I> window</I><DD>
This command will stop <I>window</I> from being a drop target.
Thus, <I>window</I> will stop receiving events related to drop operations.
It is an error to use this command for a <I>window</I> that has not been
registered as a drop target with <B>tkdnd::drop_target register</B>.
<DT><B>tkdnd::drag_source register</B><I> window ?type-list? ?mouse-buttons?</I><DD>
This command will register <I>window</I> as a drag source. A drag source is a
widget than can start a drag action. <I>window</I> must exist when this command
is executed and this command can be executed multiple times on a widget.
<P>
When <I>window</I> is registered as a drag source, and optional <I>type-list</I>
can be provided. This type list can contain one or more types that <I>window</I>
will provide during a drag action, and it can contain platform independent or
platform specific types. Platform independent are <B>DND_Text</B>
for dropping text portions and <B>DND_Files</B> for dropping a list of files
(which can contain one or multiple files) on <I>window</I>. However, this type
list is indicative/informative. <I>window</I> can initiate a drag action with
even a different type list. If <I>type-list</I> is not specified, it defaults to
the empty list.
<P>
Finally, <I>mouse-buttons</I> is one or more mouse buttons that will be used for
starting the drag action. It can have any of the values "1" (left mouse
button), "2" (middle mouse button - wheel) and "3" (right mouse button). If
<I>mouse-buttons</I> is not specified, it defaults to "1" (left mouse button).
Multiple mouse buttons can be specified as a list of values (i.e. {1 3}).
<DT><B>tkdnd::drag_source unregister</B><I> window</I><DD>
This command will stop <I>window</I> from being a drag source.
Thus, <I>window</I> will stop receiving events related to drag operations.
It is an error to use this command for a <I>window</I> that has not been
registered as a drag source with <B>tkdnd::drag_source register</B>.
<DT><B>tkdnd::platform_specific_types </B><I>type-list</I><DD>
This command will accept a list of types that can contain platform independent
or platform specific types. A new list will be returned, where each platform
independent type in <I>type-list</I> will be substituted by one or more platform
specific types. Thus, the returned list may have more elements than
<I>type-list</I>.
<DT><B>tkdnd::platform_independent_types</B><I> type-list</I><DD>
This command will accept a list of types that can contain platform independent
or platform specific types. A new list will be returned, where each platform
specific type in <I>type-list</I> will be substituted by one or more platform
independent types. Thus, the returned list may have more elements than
<I>type-list</I>.
<DT><B>tkdnd::GetDropFileTempDirectory</B><DD>
This command will return the temporary directory used by TkDND for storing
temporary files. When the package is loaded, this temporary directory will be
initialised to a proper directory according to the operating system. This
default initial value can be changed to be the value of the following
environmental variables: <B>TKDND_TEMP_DIR</B>, <B>TEMP</B>, <B>TMP</B>.
<DT><B>tkdnd::SetDropFileTempDirectory </B><I>directory</I><DD>
This command will change the temporary directory used by TkDND for storing
temporary files to <I>directory</I>.
</DL>
<A NAME="lbAE"> </A>
<H2>SUPPORTED TYPES</H2>
In order to declare the format that the data that will transferred during
a drag and drop operation, all drag and drop protocols use the notion of types.
Unfortunately, each protocol defines its own, usually platform specific, types. TkDND, trying to maintain portability among different platforms, offers some
predefined types for some basic kinds of data, like text, and filenames.
Currently, the following predefined cross-platform values are available:
<DL COMPACT>
<DT><B>DND_Text</B>:<DD>
This type can be used for transferring textual data. Internally, it is
translated to the following platform specific formats:
<BR>
Windows: <B>CF_UNICODETEXT</B>, <B>CF_TEXT</B>.
<BR>
Unix: <B>text/plain;charset=UTF-8</B>, <B>text/plain</B>.
<BR>
Mac: <B>NSStringPboardType</B>.
<DT><B>DND_Files</B>:<DD>
This type can be used for transferring a list of file names.
Internally, it is translated to the following platform specific formats:
<BR>
Windows: <B>CF_HDROP</B>.
<BR>
Unix: <B>text/uri-list</B>.
<BR>
Mac: <B>NSFilenamesPboardType</B>.
<DT>Additionally to the platform independent types, TkDND supports the following platform specific types:<DD>
<DT><B>Windows</B>:<DD>
<B>CF_UNICODETEXT</B>: Text transfer encoded in Unicode.
<BR>
<B>CF_TEXT</B>: Text transfer with application dependent encoding. If an encoding
locale is specified through CF_LOCALE it is used, else the system encoding is
used for the conversion.
<BR>
<B>FileGroupDescriptor - FileContents</B>: These two types are used for
transferring a set of files that do not appear physically on disk, like files
from compressed folders or Outlook e-mail messages. File names are transferred as
in the CF_TEXT type, while file contents are transferred in binary. TkDND
retrieves both the file names and the file contents, and saves then in a
temporary directory. When the transfer is complete, the file names of the saved
files in the temporary folder are returned. Note that TkDND support this type
pair only as drop targets and not as drag sources.
<BR>
<B>FileGroupDescriptorW - FileContents</B>: These two types are used for
transferring a set of files that do not appear physically on disk, like files
from compressed folders or Outlook e-mail messages. File names are transferred as
in the CF_UNICODETEXT type, while file contents are transferred in binary. TkDND
retrieves both the file names and the file contents, and saves then in a
temporary directory. When the transfer is complete, the file names of the saved
files in the temporary folder are returned. Note that TkDND support this type
pair only as drop targets and not as drag sources.
<BR>
<B>CF_HDROP</B>: Files transfer encoded in UTF-8.
<DT><B>Unix</B>:<DD>
</DL>
<P>
Finally, format types used for drop types can have <B>wildcards</B>, following
the same rules as "string match". For example, registering a drop target with
the type "*", will accept any drop, no matter what the drop format is.
<A NAME="lbAF"> </A>
<H2>EVENTS</H2>
Windows registered as either drop targets or drag sources, will receive certain
events, during drag and drop operations. As a result, the windows are expected
to have bindings for some of these events. Some events are mandatory (in the
sense that a drag or drop operation can be stopped if the bindings do not
exist), while others are not. However, it is a good practice to define bindings
for all events, so as the application will behave as expected during drag and
drop operations.
<P>
The TkDND package defines a set of virtual events, that correspond to various
phases of a drag and drop operation. All windows that are either a drop target
or a drag source are expected to have bindings for (all) these events, created
with the <B>bind</B> Tk command. While these event bindings are regular Tk
events, they have a small difference from plain Tk events: most of them are
expected to return a value. Since Tk bindings cannot return a value, TkDND does
not actually generate these events (i.e. through "event generate"). Instead,
TkDND locates the script that has been bound to the virtual event, and directly
executes it, as a script. Apart from this small difference in how the events are
triggered, the events required by TkDND can be viewed as regular events. In the
following two sections all virtual events defined by the TkDND package are
presented.
<A NAME="lbAG"> </A>
<H2>DROP TARGET EVENTS</H2>
A window registered as a drop target, is expected to have bindings for the
following virtual events:
<DL COMPACT>
<DT><B><<DropEnter>></B>:<DD>
This event is triggered when the mouse enters the window
during a drop action. The purpose of this event is to change the visual state of
the window, so as to notify the user whether the drop will be accepted or not.
The binding script is expected to return a single value that will define the
drop action. This returned action can be one of <B>copy</B>, <B>move</B>,
<B>link</B>, <B>ask</B>, <B>private</B> and <B>refuse_drop</B>. This event is not
mandatory, but if it is defined, it has to return an action. In case an action
is not returned, the drop is refused for this window.
<DT><B><<DropPosition>></B>:<DD>
This events is triggered when the mouse moves inside the
window during a drop action. The purpose of this event is to let window decide
if it will accept the drop and the action of the drop, if a drop is going to
happen at the specific mouse coordinates. Thus, the script binding for such an
event can get the mouse coordinates and the pressed modifier buttons (such as
ctrl, shift or alt), and is expected to return the drop action, which again must
be one of <B>copy</B>, <B>move</B>,
<B>link</B>, <B>ask</B>, <B>private</B> and <B>refuse_drop</B>. This event is not
mandatory, but if it is defined, it has to return an action. In case an action
is not returned, the drop is refused for this window.
<DT><B><<DropLeave>></B>:<DD>
This event is triggered when the mouse leaves outside the
area covered by window, without a drop happening. The binding of such an event
is expected to restore the visual state of the window to normal (i.e. the visual
state the window was in before the <<DropEnter>> event was triggered). The
binding for such an event is not expected to return a value.
<DT><B><<Drop>></B>:<DD>
This event is triggered by a drop action, and it is expected to
handle the dropped data and reset the visual state of the window. The binding
script is expected to return a value, which will be the action that has been
performed to the data and must be one of <B>copy</B>, <B>move</B>,
<B>link</B>, <B>ask</B>, <B>private</B> and <B>refuse_drop</B>. This event is not
mandatory, but if it is defined, it has to return an action. In case an action
is not returned, the drop is refused for this window.
<DT><B><<Drop:</B><I>type</I><B>>></B>:<DD>
This event is a specialisation of the generic
<<Drop>> event, augmented with a type. If such a binding exists and the drop
type matches <I>type</I>, this event binding will be executed, instead of the
generic <<Drop>> event binding. These events allow for easy specialisation of
drop bindings, according to the type of the dropped data. <I>type</I> can be
either a platform independent or a platform specific type. The binding script of
such an event is expected to return a value, which will be the action that has
been performed to the data and must be one of <B>copy</B>, <B>move</B>,
<B>link</B>, <B>ask</B>, <B>private</B> and <B>refuse_drop</B>. This event is not
mandatory, but if it is defined, it has to return an action. In case an action
is not returned, the drop is refused for this window.
</DL>
<A NAME="lbAH"> </A>
<H2>DRAG SOURCE EVENTS</H2>
A window registered as a drag source, is expected to have bindings for the
following virtual events:
<DL COMPACT>
<DT><B><<DragInitCmd>></B>:<DD>
This event is triggered when a drag action is about to start. This is a
mandatory event (whose absence will cancel the drag action), and is responsible
for providing three things: the list of actions and format types supported by
the drag source, and of course the data to be dropped. Thus, the binding script
for such an event must return a list of three elements: the drop actions
supported by the drag source (which can be any of <B>copy</B>, <B>move</B>,
<B>link</B>, <B>ask</B>, and <B>private</B>), the format type list that the data
can be dropped as (which can be any platform independent or platform specific
type), and finally the data. A simple example of such a binding, is:
<DL COMPACT><DT><DD>
<PRE>
bind .drag_source <<DragInitCmd>> \
{list copy DND_Text {Hellow world!}}
</PRE>
</DL>
<DT><B><<DragEndCmd>></B>:<DD>
This event is triggered when the drag action has finished (either when the drop
was successful or not). Its main purpose is to process the dropped data
according to the drop action returned by the drop target. Binding for such an
event is not mandatory, and the binding is not expected to return a value.
</DL>
<A NAME="lbAI"> </A>
<H2>BINDING SCRIPTS AND SUBSTITUTIONS</H2>
All bindings scripts defined for any of the virtual events above will be
executed in the same interpreter that was used for creating the window the
event is bound to, and the binding script will executed at the global
level (i.e. only global variables will be accessible).
<P>
If the binding script contains any % characters, then substitutions will be
made, like in normal Tk binding scripts. Valid % specifiers are:
<DL COMPACT>
<DT><B>%%</B><DD>
Replaced with a single percent.
<DT><B>%A</B><DD>
The current action of the drag/drop operation.
<DT><B>%a</B><DD>
The action list supported by the drag source.
<DT><B>%b</B><DD>
The mouse buttons that are pressed during a drag/drop operation. Note that
typically a single mouse button is reported as pressed, even if more than one
mouse buttons are actually pressed. Under Unix though, all pressed mouse
buttons will be reported (if the XKeyboard extension is available - else only
"1" will always be reported).
<DT><B>%c</B><DD>
The codes of the list of types supported by the drag source. All codes are in
octal format and have the same order as the list of types obtained through the
<B>%t</B> substitution.
<DT><B>%C</B><DD>
The code (in octal format) of the current type of the drag and drop operation.
<DT><B>%CTT</B><DD>
The list of types from the drop target type list that are common to the drag
source type list.
<DT><B>%CST</B><DD>
The list of types from the drag source type list that are common to the drop
target type list.
<DT><B>%D</B><DD>
The data that has been dropped. Under some platforms the data will be
available before the drop has occurred. The format of the data is the current
type of the drop operation.
<DT><B>%e</B><DD>
The name of the current virtual event. One of <<DropEnter>>, <<DropPosition>>,
<<DropLeave>>, <<Drop:<I>type</I>>>, <<Drop>>, <<DragInitCmd>>, <<DragEndCmd>>.
<DT><B>%L</B><DD>
The list of types supported by the drag source.
<DT><B>%m</B><DD>
The list of modifier keyboard keys that are pressed.
Modifier keys are some special keys, like Shift, Control or Alt.
Valid modifiers are "shift", "ctrl" and "alt" (under all operating systems),
and "mod1" to "mod5" under Unix. Please note that the usefulness of modifiers
may differ across operating systems. For example, under Windows the drop target
must examine the modifiers, and decide upon the drop action that must be
performed (by selecting an action from the list of actions supported by the
drag source). However, under Unix, the drag source is expected to decide on the
drop action, not the drop target. The drop target is expected to either accept
the action selected by the drag source (and return it back), or select "copy",
"default", or "ask". So, under Unix examining the pressed modifier keys
fulfills only informative purposes.
<DT><B>%ST</B><DD>
The list of types supported by the drag source.
<DT><B>%t</B><DD>
The list of types supported by the drag source.
<DT><B>%T</B><DD>
The current type of the drag and drop operation.
<DT><B>%TT</B><DD>
The list of types supported by the drop target.
<DT><B>%W</B><DD>
The window that the event is delivered to.
<DT><B>%X</B><DD>
The mouse pointer x coordinate, relative to the root window.
<DT><B>%Y</B><DD>
The mouse pointer y coordinate, relative to the root window.
<DT><B>%x</B><DD>
The mouse pointer x coordinate, relative to the drop target window.
<DT><B>%y</B><DD>
The mouse pointer y coordinate, relative to the drop target window.
<P>
</DL>
<A NAME="lbAJ"> </A>
<H2>SPECIFYING DROP TARGETS</H2>
Creating drop targets is easy: we have to only register a window as a drop
target with the list of format types it can accept, and add a few bindings. For
example, a window that accepts textual drops can be as follows:
<DL COMPACT><DT><DD>
<PRE>
label .drop_target -text {Text Drop Target!} -bg white
tkdnd::drop_target register .drop_target DND_Text
bind .drop_target <<DropEnter>> {%W configure -bg yellow; list copy}
bind .drop_target <<DropPosition>> {list copy}
bind .drop_target <<DropLeave>> {%W configure -bg white}
bind .drop_target <<Drop>> {%W configure -text %D; %W configure -bg white}
</PRE>
</DL>
From the above bindings, none is obligatory. However, we usually want to receive
dropped data (thus the <<Drop>> event must be handled) and we want to give
visual feedback to the users through the <<DropEnter>> and <<DropLeave>> events.
Finally, <<DropPosition>> is only necessary if we want to only accept drops on
specific areas of the window, or we want to change the drop action according to
the pressed modifiers.
<P>
Now, if we want to also add the ability to receive file drops, we could add:
<DL COMPACT><DT><DD>
<PRE>
tkdnd::drop_target register .drop_target DND_Files
bind .drop_target <<Drop:DND_Files>> \
{puts %D; %W configure -bg white}
</PRE>
</DL>
Note that we have added a "specialised" drop binding, for the event
<<Drop:DND_Files>>: this means that when a text portion is dropped over the
window, the <<Drop>> event binding will be executed. But when a list of files is
dropped onto the window, the <<Drop:DND_Files>> event binding will be executed.
If we proceed and define a binding for the <<Drop:DND_Text>> event, the binding
of the "general" <<Drop>> event will never be executed.
<A NAME="lbAK"> </A>
<H2>SPECIFYING DRAG SOURCES</H2>
In order to specify a drag source, we need to register a window as a drag
source:
<DL COMPACT><DT><DD>
<PRE>
tkdnd::drag_source register .text_drag_source
</PRE>
</DL>
The above command defines a drag source with an empty type list (and which will
be declared in the <<DragInitCmd>> event binding) and arranges mouse bindings
such as a drag will be started with the left mouse button.
Then, it is absolutely necessary to define a binding for the <<DragInitCmd>>:
this event binding must return the list of actions, the list of format types and
the actual data to be dropped:
<DL COMPACT><DT><DD>
<PRE>
bind .text_drag_source <<DragInitCmd>> \
{list {copy move} DND_Text {Hello from Tk!}}
</PRE>
</DL>
Please note that all specified format types must be compatible to each other, as
they all characterise the same data.
<A NAME="lbAL"> </A>
<H2>SPECIFYING DRAG SOURCES WITH MULTIPLE DATA TYPES</H2>
In the case the drag source wants to send a set of (incompatible) data types,
the result of the <<DragInitCmd>> event binding must be slightly different, as
it must return two items (instead of three described in the previous paragraph).
The first element is again a list of allowable actions. However, the second item
is a list of "format type" and "data" pairs:
bind .text_drag_source <<DragInitCmd>> \
<BR> {list {copy move} {DND_Text {Hello from Tk!} DND_Files {/tmp}}}
And that was all!
<A NAME="lbAM"> </A>
<H2>BUGS</H2>
<B>OS X:</B>
TkDND under OS X is built on top of the Cocoa framework. Thus, it is compatible
with Tk version >= 8.6
<BR>
<A NAME="lbAN"> </A>
<H2>KEYWORDS</H2>
dnd, drag and drop
<P>
<HR>
<A NAME="index"> </A><H2>Index</H2>
<DL>
<DT><A HREF="#lbAB">NAME</A><DD>
<DT><A HREF="#lbAC">SYNOPSIS</A><DD>
<DT><A HREF="#lbAD">INTRODUCTION</A><DD>
<DT><A HREF="#lbAE">SUPPORTED TYPES</A><DD>
<DT><A HREF="#lbAF">EVENTS</A><DD>
<DT><A HREF="#lbAG">DROP TARGET EVENTS</A><DD>
<DT><A HREF="#lbAH">DRAG SOURCE EVENTS</A><DD>
<DT><A HREF="#lbAI">BINDING SCRIPTS AND SUBSTITUTIONS</A><DD>
<DT><A HREF="#lbAJ">SPECIFYING DROP TARGETS</A><DD>
<DT><A HREF="#lbAK">SPECIFYING DRAG SOURCES</A><DD>
<DT><A HREF="#lbAL">SPECIFYING DRAG SOURCES WITH MULTIPLE DATA TYPES</A><DD>
<DT><A HREF="#lbAM">BUGS</A><DD>
<DT><A HREF="#lbAN">KEYWORDS</A><DD>
</DL>
<HR>
This document was created by
<A HREF="/man/man2html">man2html</A>,
using the manual pages.<BR>
Time: 10:56:09 GMT, July 15, 2019
</BODY>
</HTML>
|