/usr/share/tcltk/tklib0.6/diagrams/element.tcl is in tklib 0.6-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 | ## -*- tcl -*-
## (C) 2010 Andreas Kupries <andreas_kupries@users.sourceforge.net>
## BSD Licensed
# # ## ### ##### ######## ############# ######################
#
# Database of the created/drawn elements, with their canvas items,
# corners (named points), and sub-elements.
#
##
# # ## ### ##### ######## ############# ######################
## Requisites
package require Tcl 8.5 ; # Want the nice things it
# brings (dicts, {*}, etc.)
package require snit ; # Object framework.
package require math::geometry 1.1.2 ; # Vector math (points, line
# (segments), poly-lines).
package require diagram::point
# # ## ### ##### ######## ############# ######################
## Implementation
snit::type ::diagram::element {
# # ## ### ##### ######## ############# ######################
typemethod validate {id} {
if {[$type is $id]} {return $id}
return -code error "Expected element id, got \"$id\""
}
typemethod is {id} {
return [expr {[llength $id] == 2 &&
[lindex $id 0] eq "element" &&
[string is integer -strict [lindex $id 1]] &&
([lindex $id 1] >= 1)}]
}
# # ## ### ##### ######## ############# ######################
method shape {shape} {
set myshape($shape) .
return
}
method isShape {shape} {
return [info exists myshape($shape)]
}
# # ## ### ##### ######## ############# ######################
## Public API :: Extending the database
method new {shape corners items subelements} {
# Generate key
set id [NewIdentifier]
# Save the element information.
set myelement($id) [dict create \
shape $shape \
corners $corners \
items $items \
elements $subelements]
lappend myhistory() $id
lappend myhistory($shape) $id
return $id
}
method drop {} {
set mycounter 0
array unset myelement *
array unset myhistory *
set myhistory() {}
return
}
method {history get} {} {
return [array get myhistory]
}
method {history set} {history} {
array unset myhistory *
array set myhistory $history
return
}
method {history find} {shape offset} {
# 1, 2,...: Offset from the beginning of history, forward.
# -1,-2,...: Offset from the end history, backward.
if {$offset < 0} {
set offset [expr {[llength $myhistory($shape)] + $offset}]
} else {
incr offset -1
}
#parray myhistory
#puts E|hf|$shape|$offset|
return [lindex $myhistory($shape) $offset]
}
# # ## ### ##### ######## ############# ######################
## Public API :: Query database.
method elements {} {
return $myhistory()
}
method corner {id corner} {
#puts MAP($corner)=|[MapCorner $id $corner]|
set corners [dict get $myelement($id) corners]
return [dict get $corners [$dir map $corners $corner]]
}
method corners {id} {
return [dict get $myelement($id) corners]
}
method names {id {pattern *}} {
return [dict keys [dict get $myelement($id) corners] $pattern]
}
method items {args} {
set items {}
foreach id $args {
lappend items {*}[dict get $myelement($id) items]
lappend items {*}[$self items {*}[dict get $myelement($id) elements]]
}
# Elements with sub-elements elements can cause canvas items
# to appear multiple times. Reduce this to only one
# appearance. Otherwise items may be processed multiple times
# later.
return [lsort -uniq $items]
}
method bbox {args} {
# We compute the bounding box from the corners we have for the
# specified elements. This makes the assumption that the
# convex hull of the element's corners is a good approximation
# of the areas they cover.
#
# (1) We cannot fall back to canvas items, as the items may
# cover a much smaller area than the system believes. This
# notably happens for text elements. In essence a user-
# declared WxH would be ignored by looking at the canvas.
#
# (2) We have to look at all corners because the simple NW/SE
# diagonal may underestimate the box. This happens for circles
# where these anchors are near the circle boundary and thus
# describe the in-scribed box, instead of the outer bounds.
# Note that corners may contain other information than
# points. This is why the corner values are type tagged,
# allowing us to ignore the non-point corners.
set polyline {}
foreach id $args {
foreach v [dict values [dict get $myelement($id) corners]] {
lassign $v cmd detail
if {$cmd ne "point"} continue
lappend polyline [geo::x $detail] [geo::y $detail]
}
}
return [geo::bbox $polyline]
}
# # ## ### ##### ######## ############# ######################
## Public API :: Move elements to a point.
method relocate {id destination corner canvas} {
#puts \trelocate($id).$corner\ @$destination
# Move the id'entified element such that the corner's point is
# at the destination.
# Retrieve element data.
array set el $myelement($id)
# Find current location of the specified corner.
set origin [diagram::point unbox [$self corner $id $corner]]
#puts \t$corner=$origin
# Determine the movement vector which brings the corner into
# coincidence with the destination.
set delta [geo::- $destination $origin]
#puts \tdelta=$delta
# And perform the movement.
$self Move $id $delta $canvas
return
}
method move {delta corners} {
set newcorners {}
foreach {key location} $corners {
#puts PLACE|$key|$location|$delta|
if {[llength $location] == 2} {
lassign $location cmd detail
if {$cmd eq "point"} {
#puts \tSHIFT
lappend newcorners $key \
[list $cmd [geo::+ $detail $delta]]
} else {
lappend newcorners $key $location
}
} else {
lappend newcorners $key $location
}
}
return $newcorners
}
method Move {id delta canvas} {
# Retrieve element data.
array set el $myelement($id)
# Move the primary items on the canvas.
foreach item $el(items) {
$canvas move $item {*}$delta
}
# Recursively move child elements
foreach sid $el(elements) {
$self Move $sid $delta $canvas
}
# And modify the corners appropriately
set newcorners [$self move $delta $el(corners)]
dict set myelement($id) corners $newcorners
return
}
# # ## ### ##### ######## ############# ######################
constructor {thedir} {
set dir $thedir
return
}
# # ## ### ##### ######## ############# ######################
proc NewIdentifier {} {
upvar 1 mycounter mycounter
return [list element [incr mycounter]]
}
# # ## ### ##### ######## ############# ######################
## Instance data, database tables as arrays, keyed by direction
## and alias names.
component dir ; # Database of named directions.
# Used to check for and resolve
# corner aliases.
variable mycounter 0 ; # Counter for the generation of
# element identifiers. See
# 'NewIdentifier' for the user.
variable myelement -array {} ; # Database of drawn elements. Maps
# from element identifiers to a
# dictionary holding the pertinent
# information (type, canvas items,
# sub elements, and corners (aka
# attributes).
variable myhistory -array {
{} {}
} ; # History database. Keyed by
# element type, they are mapped to
# lists of element identifiers
# naming the elements in order of
# creation. The empty key has the
# history without regard to type.
variable myshape -array {} ; # Database of element shapes.
##
# # ## ### ##### ######## ############# ######################
}
namespace eval ::diagram::element::geo {
namespace import ::math::geometry::*
}
# # ## ### ##### ######## ############# ######################
## Ready
package provide diagram::element 1
|