Emulating text widget's "see" in canvas

3 views
Skip to first unread message

Jeffrey Hobbs

unread,
Nov 12, 1996, 3:00:00 AM11/12/96
to

In article <56abuh$v...@skylark.ucr.edu>,
Brian Harvey <br...@cs.ucr.edu> wrote:
>text widget so that the index given to "see" is visible.
>How might I emulate this functionality in the canvas widget?

I just answered this last week. I guess it belongs in the FAQ...

## "see" method alternative for canvas
## Aligns the named item as best it can in the middle of the screen
## Behavior depends on whether -scrollregion is set
##
## c - a canvas widget
## item - a canvas tagOrId
proc canvas_see {c item} {
set box [$c bbox $item]
if [string match {} $box] return
if [string match {} [$c cget -scrollreg]] {
## People really should set -scrollregion you know...
foreach {x y x1 y1} $box {
set x [expr round(2.5*($x1+$x)/[winfo width $c])]
set y [expr round(2.5*($y1+$y)/[winfo height $c])]
}
$c xview moveto 0
$c yview moveto 0
$c xview scroll $x units
$c yview scroll $y units
} else {
## If -scrollregion is set properly, use this
foreach {x y x1 y1} $box {top btm} [$c yview] \
{left right} [$c xview] {p q xmax ymax} [$c cget -scrollreg] {
set xpos [expr (($x1+$x)/2.0)/$xmax - ($right-$left)/2.0]
set ypos [expr (($y1+$y)/2.0)/$ymax - ($btm-$top)/2.0]
}
$c xview moveto $xpos
$c yview moveto $ypos
}
}
## EOC

--
Jeffrey Hobbs office: 541.683.7891
Nomad of the 'Net email: jho...@cs.uoregon.edu
URL: http://www.cs.uoregon.edu/~jhobbs/

Brian Harvey

unread,
Nov 12, 1996, 3:00:00 AM11/12/96
to

The text widget has the "see" command which automatically scrolls the

text widget so that the index given to "see" is visible.

How might I emulate this functionality in the canvas widget?

---
Brian Harvey * br...@cs.ucr.edu
University of California, Riverside * bria...@prost.ucr.edu
http://www.cs.ucr.edu/~brian/

Richard J. Duncan

unread,
Nov 14, 1996, 3:00:00 AM11/14/96
to

jho...@cs.uoregon.edu (Jeffrey Hobbs) writes:

>
> In article <56abuh$v...@skylark.ucr.edu>,
> Brian Harvey <br...@cs.ucr.edu> wrote:

> >text widget so that the index given to "see" is visible.
> >How might I emulate this functionality in the canvas widget?
>

> I just answered this last week. I guess it belongs in the FAQ...
>

[code snipped]

I updated Jeffrey Hobbs's code a bit so that it behaves more closely
to the listbox see command, ie: only making it visible, not
centering. For simplicity, it only works when the scrollregion is set
(everyone should really set the scrollregion :)

Here is the code:

------------------------------------------------------------------


## "see" method alternative for canvas

## makes the named item visible on the screen
## Behavior requires -scrollregion being set


##
## c - a canvas widget
## item - a canvas tagOrId
proc canvas_see {c item} {
set box [$c bbox $item]

if [string match {} $box] return

foreach {xs ys xf yf} $box {top btm} [$c yview] \
{left right} [$c xview] \
{x_min y_min x_max y_max} [$c cget -scrollreg] {

set width [expr $x_max - $x_min]
set right [expr $right * $width]
set left [expr $left * $width]

set height [expr $y_max - $y_min]
set top [expr $top * $height]
set btm [expr $btm * $height]

# see if the display is to far over to the right for the image
# ie: item number to small to be displayed
#
if { $xs < $left } {

$c xview moveto [expr (0.0 + $xs-$x_min)/$width]
} else {

# see if the display is to far over to the left for the image
# ie: item number to large to be displayed
#
if {$xf > $right} {
$c xview moveto [expr ($xf-$x_min-($right-$left))/$width]
}
}
# see if the display is to far down for the image
#
if { $ys < $top } {

$c yview moveto [expr (0.0 + $ys-$y_min)/$height]
} else {

# see if the display is to far up for the image
#
if {$yf > $btm} {
$c yview moveto [expr ($yf-$y_min-($btm-$top))/$height]
}
}


}
}

# end of file

+-------------------------------------------<dun...@isip.msstate.edu>-+
| Richard Jennings Duncan http://www.gtlug.org/~rduncan |
| U-grad research assistant Institute for Signal and Info. Proc. |
| Mississippi State University Computer Engineering |
| 'if you really want a challenge, |
| just deal with yourself' - Tori Amos |
+---------------------------------------------------------------------+


Reply all
Reply to author
Forward
0 new messages