#!/bin/sh
# ----------------------------------------------------------------------
#  DEMO: scrolledcanvas in [incr Widgets]
# ----------------------------------------------------------------------
#\
exec itkwish -f "$0" ${1+"$@"}
package require -exact Iwidgets 2.2

# itkwish interprets the rest...
# ----------------------------------------------------------------------
option add *textBackground seashell
. configure -background white

iwidgets::scrolledcanvas .canv -labeltext "Scrolledcanvas" \
    -vscrollmode dynamic -hscrollmode dynamic -autoresize yes

pack .canv -expand yes -fill both -padx 4 -pady 4
.canv xview moveto 0
.canv yview moveto 0


button .zoomin -text "Zoom In" -command {
    .canv scale all 0 0 2 2
}
pack .zoomin -side left -expand yes -padx 4 -pady 4

button .zoomout -text "Zoom Out" -command {
    .canv scale all 0 0 0.5 0.5
    .canv xview moveto 0
    .canv yview moveto 0
}
pack .zoomout -side left -expand yes -padx 4 -pady 4


bind [.canv component scrCanvas] <ButtonPress-1> {add_rectangle %W %x %y}
bind [.canv component scrCanvas] <B1-Motion> {add_rectangle %W %x %y}

proc add_rectangle {win x y} {
    set bbox [$win bbox all]
    if {$bbox != ""} {
        set w [expr [lindex $bbox 2]-[lindex $bbox 0]]
        set x0 [lindex [$win cget -scrollregion] 0]
        set x0 [expr $x0+[lindex [$win xview] 0]*$w]
        set h [expr [lindex $bbox 3]-[lindex $bbox 1]]
        set y0 [lindex [$win cget -scrollregion] 1]
        set y0 [expr $y0+[lindex [$win yview] 0]*$h]
    } else {
        set x0 0
        set y0 0
    }

    $win create rectangle \
        [expr $x0+$x-4] [expr $y0+$y-4] \
        [expr $x0+$x+4] [expr $y0+$y+4] \
        -outline "" -fill red
}
