/usr/share/amsn/utils/drawboard/drawboard.tcl is in amsn-data 0.98.9-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 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 | ##Drawboard widget by Karel Demeyer
#
# NAME
#
# drawboard - Create and manipulate drawboard widgets
#
# SYNOPSIS
#
# drawboard pathName ?options?
#
# STANDARD OPTIONS
#
# ...
#
# WIDGET-SPECIFIC OPTIONS
#
# -grid, grid, Grid
# -drawmode, drawmode, Drawmode
# -color, color, Color
# -pencil, pencil, Pencil
# -width, width, Width
# -height, height, Height
#
# WIDGET COMMAND
#
# pathName cget option
# pathName configure ?option? ?value option value ...?
#TODO:
# -gridimg option
# provide 1 default pencil and 1 default gridimg
# Improved Saving
# Loading
# fixed dimensions (-width / -height doesn't work yet properly)
# remove amsn plus button
# review cutting .. it has bugs
package require snit
package provide drawboard 0.2
snit::widgetadaptor drawboard {
option -grid -default 1 -configuremethod SetConfig
option -drawmode -default free
option -color -default black -configuremethod SetConfig
option -pencil -default pencil1 -configuremethod SetConfig
option -width -default 100
option -height -default 100
#option -gridimg
variable buttondown
variable oldx
variable oldy
variable endx
variable endy
variable gridsourceimg
variable strokes_list
variable curStroke
variable curDrawingAttributes
variable drawAttrs_list
constructor { args } {
installhull using canvas -bg white -relief flat -highlightthickness 0 -width $options(-width) -height $options(-height)
status_log "creating drawboard widget $self"
$self configurelist $args
#if {$options(-gridimg) == ""} {
# set gridsourceimg [$self LoadImage drawboardgrid grid.png]
#} else {
# set gridsourceimg $options(-gridimg)
#}
set gridsourceimg [::skin::loadPixmap grid]
#give the grid a color
::picture::Colorize $gridsourceimg blue
#create the image where we will copy the grid on (tiled)
image create photo grid$self
#put the grid-image (initially empty) on the canvas
$hull create image 0 0 -anchor nw -image grid$self -tag grid
#create the initial drawboard img
set drawboard [image create photo] ;# -width $width -height $height]
#put the drawboard-img on the canvas
$hull create image 0 0 -anchor nw -image $drawboard -tag drawboard
#make it possible to disable coloring of pencils, for "stamps" (like using smileys as pencil)
$self SetConfig coloring 1
$self UpdatePencil
set endx 0
set endy 0
set strokes_list [list]
set curDrawingAttributes [list 7 $options(-color)]
set drawAttrs_list [list]
#bindings
bind $self <ButtonPress-1> "$self ButtonDown"
bind $self <B1-Motion> "$self MouseMove"
bind $self <ButtonRelease-1> "$self ButtonRelease"
bind $self <Configure> "$self Configure"
$self configurelist $args
}
method LoadImage {imagename filename} {
return [image create photo $imagename -file $filename.gif]
}
###############################################
# When mousebutton pressed #
###############################################
method ButtonDown {} {
set drawboard [$hull itemcget drawboard -image]
#change the buttondown-flag
set buttondown 1
#set initial coordinates of the mouse on the drawboard
set oldx [expr {[winfo pointerx $self] - [winfo rootx $self]}]
set oldy [expr {[winfo pointery $self] - [winfo rooty $self]}]
set curStroke [list $oldx $oldy]
$self DrawDot $oldx $oldy
$self SetEnds $oldx $oldy
}
###############################################
# When mousebutton is released #
###############################################
method ButtonRelease {} {
if { $options(-drawmode) == "line"} {
set drawboard [$hull itemcget drawboard -image]
set newx [expr {[winfo pointerx $self] - [winfo rootx $self]}]
set newy [expr {[winfo pointery $self] - [winfo rooty $self]}]
$self DrawLine $oldx $oldy $newx $newy
$self SetEnds $newx $newy
lappend curStroke $newx $newy
}
lappend strokes_list $curStroke
lappend drawAttrs_list $curDrawingAttributes
#change the buttondown-flag
set buttondown 0
unset oldx oldy
}
###############################################
# When the mouse is moved #
###############################################
method MouseMove {} {
if { $options(-drawmode) == "free" } {
#If we're dragging, draw
if {$buttondown} {
#Get the names of the items
set drawboard [$hull itemcget drawboard -image]
#find the coordinates of the mouse on the selfas (drawboard)
set posx [expr {[winfo pointerx $self] - [winfo rootx $self]}]
set posy [expr {[winfo pointery $self] - [winfo rooty $self]}]
#if the coords made a jump, draw a line between 'm, otherwise just draw a dot
if {[expr abs($oldx - $posx)] > 2 || [expr abs($oldy - $posy)] > 2} {
$self DrawLine $oldx $oldy $posx $posy
} else {
$self DrawDot $posx $posy
}
#remember where we were
set oldx $posx
set oldy $posy
lappend curStroke $oldx $oldy
$self SetEnds $posx $posy
}
}
}
###############################################
# Draws a dot with the pencil on given coords #
###############################################
method DrawDot { x y } {
set drawboard [$hull itemcget drawboard -image]
set x [expr {$x - [image width pencil_$self]/2}]
set y [expr {$y - [image height pencil_$self]/2}]
#only draw if on the drawboard
if {$x > 0 && $y > 0 && $x < [$drawboard cget width] && $y < [$drawboard cget height]} {
$drawboard copy pencil_$self -to $x $y
}
}
###############################################
# Draws a line between 2 given coord-pairs #
# with the pencil #
###############################################
method DrawLine { x1 y1 x2 y2 } {
set drawboard [$hull itemcget drawboard -image]
set xsize [$drawboard cget width]
set ysize [$drawboard cget heigth]
#if we end off the board, end on the edge
if {$x2 < 0} { set x2 0}
if {$y2 < 0} { set y2 0}
if { $x2 > $xsize } { set x2 $xsize }
if { $y2 > $ysize } { set y2 $ysize }
#calculate the x and y distance
set xdist [expr {($x1 - $x2)*1.0}]
set ydist [expr {($y1 - $y2)*1.0}]
#also get there absolute values
set absxdist [expr {abs($xdist)}]
set absydist [expr {abs($ydist)}]
#calculate the x and y diffs
if {$absxdist > $absydist} {
set absdist $absxdist
} else {
set absdist $absydist
}
if {$absdist == 0} {
set xdiff 0
set ydiff 0
} else {
set xdiff [expr {$xdist / $absdist}]
set ydiff [expr {$ydist / $absdist}]
}
set steps $absdist
#draw the line
for {set idx 0} { $idx < $steps } {incr idx } {
set x1 [expr {$x1 - $xdiff}]
set y1 [expr {$y1 - $ydiff}]
$self DrawDot [expr int($x1)] [expr int($y1)]
}
}
method Configure {} {
#get the new dimensions
set width [winfo width $self]
set height [winfo height $self]
if { $height < $endy } {
set endy $height
}
if { $width < $endx } {
set endx $width
}
set drawboard [$hull itemcget drawboard -image]
$drawboard configure -width $width -height $height
$self UpdateGrid
}
method UpdateGrid { } {
if { $options(-grid) == 1 } {
set width [winfo width $self]
set height [winfo height $self]
grid$self copy $gridsourceimg -to 0 0 $width $height -shrink
} else {
catch {grid$self blank}
}
}
method SetEnds {x y} {
set farx [expr $x + [image width pencil_$self]]
set fary [expr $y + [image height pencil_$self]]
if { $farx > $endx } {
set endx $farx
}
if { $fary > $endy } {
set endy $fary
}
}
method ClearDrawboard {} {
[$hull itemcget drawboard -image] blank
set endx 0
set endy 0
set strokes_list [list]
set curStroke_x [list]
set curStroke_y [list]
set curDrawingAttributes [list 7 $options(-color)]
set drawAttrs_list [list]
}
#TODO:
method LoadDrawing { filename } {
#$self ClearDrawboard
#draw loaded image
set drawboard [$hull itemcget drawboard -image]
set loadedimg [image create photo -file $filename]
$drawboard copy $loadedimg
$self SetEnds [image width $loadedimg] [image height $loadedimg]
$self Configure
}
method SaveDrawing {filename {gifFortified 0}} {
set drawboard [$hull itemcget drawboard -image]
#to make sure the ends are not to far and all is right
set boardh [image height $drawboard]
set boardw [image width $drawboard]
if {$boardh < $endy} {
set endy $boardh
}
if {$boardw < $endx} {
set endx $boardw
}
#only save to the most far used coordinates
image create photo temp
temp copy $drawboard -from 0 0 $endx $endy
image create photo $drawboard
$drawboard copy temp
#put the drawboard on a white background
image create photo copytosend ;# -width [image width $drawboard] -height [image height $drawboard]
#fix the "cannot save empty file" bug
if { $endx == 0 || $endy == 0 } {
copytosend put {#ffffff} -to 1 1 [image width $drawboard] [image height $drawboard]
} else {
copytosend put {#ffffff} -to 0 0 [image width $drawboard] [image height $drawboard]
}
copytosend copy $drawboard
set endx 0
set endy 0
::picture::Save copytosend $filename cxgif
# Fortify the GIF with the strokes_list
if {$gifFortified} {
if {![catch {package require tclISF 0.3}]} {
if {[catch {[tclISF save $filename $strokes_list $drawAttrs_list]} err]} {
status_log "\[SaveDrawing\] saving to file $filename. Got Error: $err" red
status_log "$strokes_list" red
status_log "$drawAttrs_list" red
}
} else {
if {![catch {package require -exact tclISF 0.2}]} {
if {[catch {[tclISF_save $filename $strokes_list $drawAttrs_list]} err]} {
status_log "\[SaveDrawing\] saving to file $filename. Got Error: $err" red
status_log "$strokes_list" red
status_log "$drawAttrs_list" red
}
}
}
}
image delete copytosend
image delete temp
}
method ToggleGrid {} {
if { $options(-grid) == 1} {
$self SetConfig -grid 0
} else {
$self SetConfig -grid 1
}
}
method SetConfig {option value} {
set options($option) $value
#actions after change of options
#the space was added so the option isn't passed to the switch command
switch " $option" {
" -grid" {
$self UpdateGrid
}
" -pencil" {
$self UpdatePencil
}
" -color" {
$self UpdatePencil
}
}
}
method UpdatePencil { } {
set pencilname $options(-pencil)
#if the pencil img already exists, delete it first before we (re)make it
catch { image delete pencil_$self }
set pencilimg [::skin::loadPixmap $pencilname]
#get the dimensions for the pencil-image
set width [image width $pencilimg]
set height [image height $pencilimg]
#create the pencil image
image create photo pencil_$self -width $width -height $height
#copy the source-img on the new (still empty) pencil image
pencil_$self copy $pencilimg
#color the pencil
::picture::Colorize pencil_$self $options(-color)
#fill the drawing attributes
if {[string first "pencil_" $pencilname] == 0} {
set width [string range $pencilname 7 end]
}
set curDrawingAttributes [list $width $options(-color)]
}
}
|