Version 3 of Adding Points to gnocl::canvas Line Items

Updated 2009-05-19 12:44:38 by WJG

WJG (19/05/09) Gnocl support for the Gnome Canvas widget enables all sorts of interesting manipulations to be applied canvas objects. However, as Gnocl widget commmands and options are generally fashioned after the Gtk/Gnome library properties and functions, the way to do things might not be too apparent at first glance. Take adding or even deleting points from a polyline item. Here's a simple code example to show how its done.

#---------------
# Author:   William J Giddings
# Date:     07/05/09
#---------------
#!/bin/sh
# the next line restarts using tclsh \
exec tclsh "$0" "$@"
#---------------

package require Gnocl
package require GnoclCanvas

# assuming this is a line..
# can canvaswidget
# id  item id or tag
proc addPoint {canv id pos newPoints} {
    set coords [$canv itemCget $id -coords]
    puts "$id = $coords"

    # beware! items added, must be a multiple of 2!

    foreach item $newPoints {
        set coords [linsert $coords $pos $item]
        incr pos
    }
    $canv itemConfigure $id -coords $coords
    $canv update
}

# assuming this is a line..
# can canvaswidget
# id  item it
proc deletePoint {canv id pos newPoints} {
    set coords [$canv itemCget $id -coords]
    puts "$id = $coords"

    # errochecking on number of items added, must be a multiple of 2!

    foreach item $newPoints {
        set coords [linsert $coords $pos $item]
        incr pos
    }
    $canv itemConfigure $id -coords $coords
    $canv update
}



set canv [gnocl::canvas -background white -antialiased 1]
set coords {10 100 30 60 50 20 110 20 150 60 190 100}

$canv create line -coords $coords -fill blue -tags line

set but [gnocl::button \
    -text "Add Extra Points" \
    -onClicked {
        addPoint $canv line 4 {103 103 125 152 151 17 5 5}
    }]

set box [gnocl::box -orientation vertical]

$box add $canv -fill {1 1} -expand 1
$box add $but -fill {0 0} -expand 0

$canv itemConfigure dots -fill darkgreen
gnocl::window -title "Canvas" -child $box -onDestroy exit -widthRequest 320 -heightRequest 240

enter categories here