File: dragger.tcl

package info (click to toggle)
tkgate 1.8.7-1
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 6,380 kB
  • ctags: 7,974
  • sloc: ansic: 38,037; tcl: 6,804; sh: 615; lex: 445; yacc: 413; makefile: 68
file content (125 lines) | stat: -rw-r--r-- 2,827 bytes parent folder | download | duplicates (3)
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
#   Copyright (C) 1987-2007 by Jeffery P. Hansen
#
#   This program is free software; you can redistribute it and/or modify
#   it under the terms of the GNU General Public License as published by
#   the Free Software Foundation; either version 2 of the License, or
#   (at your option) any later version.
#
#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU General Public License for more details.
#
#   You should have received a copy of the GNU General Public License
#   along with this program; if not, write to the Free Software
#   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
# Last edit by hansen on Tue Feb 24 14:06:55 2004
#

#############################################################################
#
# Drag-and-drop functions
#
namespace eval Dragger {
  variable drag_w
  variable W
  variable H
  variable command
  variable absolute
  variable grabpoint

  proc setGeometry {X Y} {
    variable drag_w
    variable W
    variable H
    variable grabpoint

    if { [llength $grabpoint] < 2 } {
      wm geometry $drag_w +[expr ${X} - $W/2]+[expr ${Y} - 3*$H/4]
    } else {
      set dx [lindex $grabpoint 0]
      set dy [lindex $grabpoint 1]
      wm geometry $drag_w +[expr ${X} - $dx]+[expr ${Y} - $dy]
    }
  }


  proc windowConfigure {} {
    variable drag_w
    variable W
    variable H

    scan [winfo pointerxy .] "%d %d" X Y

    set W [winfo width $drag_w]
    set H [winfo height $drag_w]

    updatePosition $X $Y
  }

  proc updatePosition {X Y} {
    setGeometry $X $Y
  }

  proc drop {w x y} {
    variable command
    variable absolute

    destroy $w
    if { $command != "" } {
      update
      set w [winfo containing $x $y]

      if { $w == "" } {
	eval "$command \"\" $x $y"
      } else {
	if { ! $absolute } {
	  set x [expr $x - [winfo rootx $w]]
	  set y [expr $y - [winfo rooty $w]]
	}
	eval "$command $w $x $y"
      }
    }
  }

  proc make {args} {
    variable drag_w
    variable W
    variable H
    variable command
    variable absolute
    variable grabpoint

    set command ""
    set absolute 0
    set grabpoint 0
    parseargs $args {-command -absolute -grabpoint}

    set W 10
    set H 10

    set drag_w .dragger
    set w $drag_w
    if {[catch { toplevel $w -cursor arrow } xx]} {
      return ""
    }

    scan [winfo pointerxy .] "%d %d" X Y

    setGeometry $X $Y
    wm overrideredirect $w 1

    bind $w <ButtonRelease-1> "Dragger::drop $w %X %Y"
    bind $w <B1-Motion> { Dragger::updatePosition %X %Y}
    bind $w <Configure> { Dragger::windowConfigure }

    update
    if {[catch { grab -global $w } xx]} {
      destroy $w
      return ""
    }

    return $w
  }
}