summaryrefslogtreecommitdiffstats
path: root/library/tkdnd_windows.tcl
blob: b1a8268136f50bc33bf71b6cba68bd7e57eb697a (plain)
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
#
# tkdnd_windows.tcl --
#
#    This file implements some utility procedures that are used by the TkDND
#    package.
#
# This software is copyrighted by:
# George Petasis, National Centre for Scientific Research "Demokritos",
# Aghia Paraskevi, Athens, Greece.
# e-mail: petasis@iit.demokritos.gr
#
# The following terms apply to all files associated
# with the software unless explicitly disclaimed in individual files.
#
# The authors hereby grant permission to use, copy, modify, distribute,
# and license this software and its documentation for any purpose, provided
# that existing copyright notices are retained in all copies and that this
# notice is included verbatim in any distributions. No written agreement,
# license, or royalty fee is required for any of the authorized uses.
# Modifications to this software may be copyrighted by their authors
# and need not follow the licensing terms described here, provided that
# the new terms are clearly indicated on the first page of each file where
# they apply.
#
# IN NO EVENT SHALL THE AUTHORS OR DISTRIBUTORS BE LIABLE TO ANY PARTY
# FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
# ARISING OUT OF THE USE OF THIS SOFTWARE, ITS DOCUMENTATION, OR ANY
# DERIVATIVES THEREOF, EVEN IF THE AUTHORS HAVE BEEN ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
#
# THE AUTHORS AND DISTRIBUTORS SPECIFICALLY DISCLAIM ANY WARRANTIES,
# INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE, AND NON-INFRINGEMENT.  THIS SOFTWARE
# IS PROVIDED ON AN "AS IS" BASIS, AND THE AUTHORS AND DISTRIBUTORS HAVE
# NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR
# MODIFICATIONS.
#

namespace eval olednd {
  variable _types {}
  variable _typelist {}
  variable _codelist {}
  variable _actionlist {}
  variable _pressedkeys {}
  variable _action {}
  variable _common_drag_source_types {}
  variable _common_drop_target_types {}
  variable _unhandled_types {}

  variable _last_mouse_root_x 0
  variable _last_mouse_root_y 0

  proc initialise { } {
  };# initialise
};# namespace olednd

# ----------------------------------------------------------------------------
#  Command olednd::_HandleDragEnter
# ----------------------------------------------------------------------------
proc olednd::_HandleDragEnter { drop_target typelist actionlist pressedkeys
                                rootX rootY codelist } {
  variable _typelist;                 set _typelist    $typelist
  variable _codelist;                 set _codelist    $codelist
  variable _actionlist;               set _actionlist  $actionlist
  variable _pressedkeys;              set _pressedkeys $pressedkeys
  variable _action;                   set _action      {}
  variable _common_drag_source_types; set _common_drag_source_types {}
  variable _common_drop_target_types; set _common_drop_target_types {}

  variable _last_mouse_root_x;        set _last_mouse_root_x $rootX
  variable _last_mouse_root_y;        set _last_mouse_root_y $rootY

  # puts "olednd::_HandleDragEnter: drop_target=$drop_target,\
  #       typelist=$typelist, actionlist=$actionlist,\
  #       pressedkeys=$pressedkeys, rootX=$rootX, rootY=$rootY"
  focus $drop_target

  ## Does the new drop target support any of our new types?
  variable _types; set _types [bind $drop_target <<DropTargetTypes>>]
  if {[llength $_types]} {
    ## Examine the drop target types, to find at least one match with the drag
    ## source types...
    set supported_types [_supported_types $_typelist]
    foreach type $_types {
      foreach matched [lsearch -glob -all -inline $supported_types $type] {
        ## Drop target supports this type.
        lappend common_drag_source_types $matched
        lappend common_drop_target_types $type
      }
    }
  }

  set _action refuse_drop
  if {[info exists common_drag_source_types]} {
    set _action copy
    set _common_drag_source_types $common_drag_source_types
    set _common_drop_target_types $common_drop_target_types
    ## Drop target supports at least one type. Send a <<DropEnter>>.
    set cmd [bind $drop_target <<DropEnter>>]
    if {[string length $cmd]} {
      set cmd [string map [list %W $drop_target %X $rootX %Y $rootY \
        %CST \{$_common_drag_source_types\} \
        %CTT \{$_common_drop_target_types\} \
        %CPT \{[lindex [_platform_independent_type [lindex $_common_drag_source_types 0]] 0]\} \
        %ST  \{$_typelist\}    %TT \{$_types\} \
        %A   $_action          %a  \{$_actionlist\} \
        %b   \{$_pressedkeys\} %m  \{$_pressedkeys\} \
        %D   \{\}              %e  <<DropEnter>> \
        %L   \{$_typelist\}    %%  % \
        %t   \{$_typelist\}    %T  \{[lindex $_common_drag_source_types 0]\} \
        %c   \{$_codelist\}    %C  \{[lindex $_codelist 0]\} \
        ] $cmd]
      set _action [uplevel \#0 $cmd]
    }
  }
  if {$::tkdnd::_auto_update} {update}
  # Return values: copy, move, link, ask, private, refuse_drop, default
  return $_action
};# olednd::_HandleDragEnter

# ----------------------------------------------------------------------------
#  Command olednd::_HandleDragOver
# ----------------------------------------------------------------------------
proc olednd::_HandleDragOver { drop_target pressedkeys rootX rootY } {
  variable _types
  variable _typelist
  variable _codelist
  variable _actionlist
  variable _pressedkeys
  variable _action
  variable _common_drag_source_types
  variable _common_drop_target_types

  variable _last_mouse_root_x;        set _last_mouse_root_x $rootX
  variable _last_mouse_root_y;        set _last_mouse_root_y $rootY

  # puts "olednd::_HandleDragOver: drop_target=$drop_target,\
  #             pressedkeys=$pressedkeys, rootX=$rootX, rootY=$rootY"

  if {![llength $_common_drag_source_types]} {return refuse_drop}
  set _pressedkeys $pressedkeys
  set cmd [bind $drop_target <<DropPosition>>]
  if {[string length $cmd]} {
    set cmd [string map [list %W $drop_target %X $rootX %Y $rootY \
      %CST \{$_common_drag_source_types\} \
      %CTT \{$_common_drop_target_types\} \
      %CPT \{[lindex [_platform_independent_type [lindex $_common_drag_source_types 0]] 0]\} \
      %ST  \{$_typelist\}    %TT \{$_types\} \
      %A   $_action          %a  \{$_actionlist\} \
      %b   \{$_pressedkeys\} %m  \{$_pressedkeys\} \
      %D   \{\}              %e  <<DropPosition>> \
      %L   \{$_typelist\}    %%  % \
      %t   \{$_typelist\}    %T  \{[lindex $_common_drag_source_types 0]\} \
      %c   \{$_codelist\}    %C  \{[lindex $_codelist 0]\} \
      ] $cmd]
    set _action [uplevel \#0 $cmd]
  }
  if {$::tkdnd::_auto_update} {update}
  # Return values: copy, move, link, ask, private, refuse_drop, default
  return $_action
};# olednd::_HandleDragOver

# ----------------------------------------------------------------------------
#  Command olednd::_HandleDragLeave
# ----------------------------------------------------------------------------
proc olednd::_HandleDragLeave { drop_target } {
  variable _types
  variable _typelist
  variable _codelist
  variable _actionlist
  variable _pressedkeys
  variable _action
  variable _common_drag_source_types
  variable _common_drop_target_types
  variable _last_mouse_root_x
  variable _last_mouse_root_y

  if {![llength $_common_drag_source_types]} {return}
  foreach var {_types _typelist _actionlist _pressedkeys _action
               _common_drag_source_types _common_drop_target_types} {
    set $var {}
  }

  set cmd [bind $drop_target <<DropLeave>>]
  if {[string length $cmd]} {
    set cmd [string map [list %W $drop_target \
      %X $_last_mouse_root_x %Y $_last_mouse_root_y \
      %CST \{$_common_drag_source_types\} \
      %CTT \{$_common_drop_target_types\} \
      %CPT \{[lindex [_platform_independent_type [lindex $_common_drag_source_types 0]] 0]\} \
      %ST  \{$_typelist\}    %TT \{$_types\} \
      %A   \{$_action\}      %a  \{$_actionlist\} \
      %b   \{$_pressedkeys\} %m  \{$_pressedkeys\} \
      %D   \{\}              %e  <<DropLeave>> \
      %L   \{$_typelist\}    %%  % \
      %t   \{$_typelist\}    %T  \{[lindex $_common_drag_source_types 0]\} \
      %u   \{$_codelist\}    %C  \{[lindex $_codelist 0]\} \
      ] $cmd]
    set _action [uplevel \#0 $cmd]
  }
  if {$::tkdnd::_auto_update} {update}
};# olednd::_HandleDragLeave

# ----------------------------------------------------------------------------
#  Command olednd::_HandleXdndDrop
# ----------------------------------------------------------------------------
proc olednd::_HandleDrop { drop_target pressedkeys rootX rootY _type data } {
  variable _types
  variable _typelist
  variable _codelist
  variable _actionlist
  variable _pressedkeys
  variable _action
  variable _common_drag_source_types
  variable _common_drop_target_types
  set data [_normalise_data $_type $data]
  # puts "olednd::_HandleDrop: drop_target=$drop_target,\
  #             pressedkeys=$pressedkeys, rootX=$rootX, rootY=$rootY,\
  #             data=\"$data\""

  if {![llength $_common_drag_source_types]} {return refuse_drop}
  set _pressedkeys $pressedkeys
  ## Try to select the most specific <<Drop>> event.
  foreach type [concat $_common_drag_source_types $_common_drop_target_types] {
    set type [lindex [_platform_independent_type $type] 0]
    set cmd [bind $drop_target <<Drop:$type>>]
    if {[string length $cmd]} {
      set cmd [string map [list %W $drop_target %X $rootX %Y $rootY \
        %CST \{$_common_drag_source_types\} \
        %CTT \{$_common_drop_target_types\} \
        %CPT \{[lindex [_platform_independent_type [lindex $_common_drag_source_types 0]] 0]\} \
        %ST  \{$_typelist\}    %TT \{$_types\} \
        %A   $_action          %a \{$_actionlist\} \
        %b   \{$_pressedkeys\} %m \{$_pressedkeys\} \
        %D   [list $data]      %e <<Drop:$type>> \
        %L   \{$_typelist\}    %% % \
        %t   \{$_typelist\}    %T \{[lindex $_common_drag_source_types 0]\} \
        %c   \{$_codelist\}    %C  \{[lindex $_codelist 0]\} \
        ] $cmd]
      return [uplevel \#0 $cmd]
    }
  }
  set cmd [bind $drop_target <<Drop>>]
  if {[string length $cmd]} {
    set cmd [string map [list %W $drop_target %X $rootX %Y $rootY \
      %CST \{$_common_drag_source_types\} \
      %CTT \{$_common_drop_target_types\} \
      %CPT \{[lindex [_platform_independent_type [lindex $_common_drag_source_types 0]] 0]\} \
      %ST  \{$_typelist\}    %TT \{$_types\} \
      %A   $_action          %a \{$_actionlist\} \
      %b   \{$_pressedkeys\} %m \{$_pressedkeys\} \
      %D   [list $data]      %e <<Drop>> \
      %L   \{$_typelist\}    %% % \
      %t   \{$_typelist\}    %T \{[lindex $_common_drag_source_types 0]\} \
      %c   \{$_codelist\}    %C  \{[lindex $_codelist 0]\} \
      ] $cmd]
    set _action [uplevel \#0 $cmd]
  }
  if {$::tkdnd::_auto_update} {update}
  # Return values: copy, move, link, ask, private, refuse_drop
  return $_action
};# olednd::_HandleXdndDrop

# ----------------------------------------------------------------------------
#  Command olednd::_GetDropTypes
# ----------------------------------------------------------------------------
proc olednd::_GetDropTypes { drop_target } {
  variable _common_drag_source_types
  return $_common_drag_source_types
};# olednd::_GetDropTypes

# ----------------------------------------------------------------------------
#  Command olednd::_GetDroppedData
# ----------------------------------------------------------------------------
proc olednd::_GetDroppedData {  } {
  variable _drop_target
  return [selection get -displayof $_drop_target \
                        -selection XdndSelection -type STRING]
};# olednd::_GetDroppedData

# ----------------------------------------------------------------------------
#  Command olednd::_GetDragSource
# ----------------------------------------------------------------------------
proc olednd::_GetDragSource {  } {
  variable _drag_source
  return $_drag_source
};# olednd::_GetDragSource

# ----------------------------------------------------------------------------
#  Command olednd::_GetDropTarget
# ----------------------------------------------------------------------------
proc olednd::_GetDropTarget {  } {
  variable _drop_target
  return [winfo id $_drop_target]
};# olednd::_GetDropTarget

# ----------------------------------------------------------------------------
#  Command olednd::_supported_types
# ----------------------------------------------------------------------------
proc olednd::_supported_types { types } {
  set new_types {}
  foreach type $types {
    if {[_supported_type $type]} {lappend new_types $type}
  }
  return $new_types
}; # olednd::_supported_types

# ----------------------------------------------------------------------------
#  Command olednd::_platform_specific_types
# ----------------------------------------------------------------------------
proc olednd::_platform_specific_types { types } {
  set new_types {}
  foreach type $types {
    set new_types [concat $new_types [_platform_specific_type $type]]
  }
  return $new_types
}; # olednd::_platform_specific_types

# ----------------------------------------------------------------------------
#  Command olednd::_platform_independent_types
# ----------------------------------------------------------------------------
proc olednd::_platform_independent_types { types } {
  set new_types {}
  foreach type $types {
    set new_types [concat $new_types [_platform_independent_type $type]]
  }
  return $new_types
}; # olednd::_platform_independent_types

# ----------------------------------------------------------------------------
#  Command olednd::_normalise_data
# ----------------------------------------------------------------------------
proc olednd::_normalise_data { type data } {
  switch [lindex [_platform_independent_type $type] 0] {
    DND_Text   {return $data}
    DND_Files  {return $data}
    DND_HTML   {return [encoding convertfrom utf-8 $data]}
    default    {return $data}
  }
}; # olednd::_normalise_data

# ----------------------------------------------------------------------------
#  Command olednd::_platform_specific_type
# ----------------------------------------------------------------------------
proc olednd::_platform_specific_type { type } {
  switch $type {
    DND_Text   {return [list CF_UNICODETEXT CF_TEXT]}
    DND_Files  {return [list CF_HDROP]}
    DND_HTML   {return [list "HTML Format"]}
    DND_RTF    {return [list "Rich Text Format"]}
    default    {
      # variable _unhandled_types
      # if {[lsearch -exact $_unhandled_types $type] == -1} {
      #   lappend _unhandled_types $type
      # }
      return [list $type]}
  }
}; # olednd::_platform_specific_type

# ----------------------------------------------------------------------------
#  Command olednd::_platform_independent_type
# ----------------------------------------------------------------------------
proc olednd::_platform_independent_type { type } {
  switch $type {
    CF_UNICODETEXT - CF_TEXT                 {return [list DND_Text]}
    CF_HDROP                                 {return [list DND_Files]}
    CF_HTML - "HTML Format"                  {return [list DND_HTML]}
    CF_RTF - CF_RTFTEXT - "Rich Text Format" {return [list DND_RTF]}
    default                                  {return [list $type]}
  }
}; # olednd::_platform_independent_type

# ----------------------------------------------------------------------------
#  Command olednd::_supported_type
# ----------------------------------------------------------------------------
proc olednd::_supported_type { type } {
  # return 1;
  switch $type {
    CF_UNICODETEXT - CF_TEXT -
    FileGroupDescriptor - FileGroupDescriptorW -
    CF_HTML - CF_RTF - CF_RTFTEXT -
    "HTML Format" - "Rich Text Format" -
    CF_HDROP {return 1}
  }
  # Is the type in our known, but unhandled types?
  variable _unhandled_types
  if {[lsearch -exact $_unhandled_types $type] != -1} {return 1}
  return 0
}; # olednd::_supported_type