3
# This demonstration script creates a simple canvas that can be
4
# scrolled in two dimensions.
6
# simple scrollable canvas widget demo (called by 'widget')
10
if defined?($cscroll_demo) && $cscroll_demo
15
# demo toplevel widget
16
$cscroll_demo = TkToplevel.new {|w|
17
title("Scrollable Canvas Demonstration")
23
TkLabel.new($cscroll_demo, 'font'=>$font, 'wraplength'=>'4i',
24
'justify'=>'left', 'text'=>"This window displays a canvas widget that can be scrolled either using the scrollbars or by dragging with button 2 in the canvas. If you click button 1 on one of the rectangles, its indices will be printed on stdout."){
29
$cscroll_buttons = TkFrame.new($cscroll_demo) {|frame|
33
tmppath = $cscroll_demo
37
}.pack('side'=>'left', 'expand'=>'yes')
41
command proc{showCode 'cscroll'}
42
}.pack('side'=>'left', 'expand'=>'yes')
44
$cscroll_buttons.pack('side'=>'bottom', 'fill'=>'x', 'pady'=>'2m')
47
unless $tk_version =~ /^4\.[01]/
48
$cscroll_grid = TkFrame.new($cscroll_demo) {
49
pack('expand'=>'yes', 'fill'=>'both', 'padx'=>1, 'pady'=>1)
51
TkGrid.rowconfigure($cscroll_grid, 0, 'weight'=>1, 'minsize'=>0)
52
TkGrid.columnconfigure($cscroll_grid, 0, 'weight'=>1, 'minsize'=>0)
56
$cscroll_canvas = TkCanvas.new($cscroll_demo,
57
'relief'=>'sunken', 'borderwidth'=>2,
58
'scrollregion'=>['-11c', '-11c', '50c', '20c']
60
if $tk_version =~ /^4\.[01]/
61
pack('expand'=>'yes', 'fill'=>'both')
63
grid('in'=>$cscroll_grid, 'padx'=>1, 'pady'=>1, 'row'=>0, 'column'=>0,
64
'rowspan'=>1, 'columnspan'=>1, 'sticky'=>'news')
67
TkScrollbar.new($cscroll_demo, 'command'=>proc{|*args| c.yview(*args)}) {|vs|
68
c.yscrollcommand(proc{|first,last| vs.set first,last})
69
if $tk_version =~ /^4\.[01]/
70
pack('side'=>'right', 'fill'=>'y')
72
grid('in'=>$cscroll_grid, 'padx'=>1, 'pady'=>1, 'row'=>0, 'column'=>1,
73
'rowspan'=>1, 'columnspan'=>1, 'sticky'=>'news')
77
TkScrollbar.new($cscroll_demo, 'orient'=>'horiz',
78
'command'=>proc{|*args| c.xview(*args)}) {|hs|
79
c.xscrollcommand(proc{|first,last| hs.set first,last})
80
if $tk_version =~ /^4\.[01]/
81
pack('side'=>'bottom', 'fill'=>'x')
83
grid('in'=>$cscroll_grid, 'padx'=>1, 'pady'=>1, 'row'=>1, 'column'=>0,
84
'rowspan'=>1, 'columnspan'=>1, 'sticky'=>'news')
89
bg = $cscroll_canvas.configinfo('bg')[4]
94
TkcRectangle.new($cscroll_canvas, "#{x}c", "#{y}c", "#{x+2}c", "#{y+2}c",
95
'outline'=>'black', 'fill'=>bg, 'tags'=>'rect')
96
TkcText.new($cscroll_canvas, "#{x+1}c", "#{y+1}c",
97
'text'=>"#{i},#{j}", 'anchor'=>'center', 'tags'=>'text')
102
$cscroll_canvas.itembind('all', 'Any-Enter', proc{scrollEnter $cscroll_canvas})
103
$cscroll_canvas.itembind('all', 'Any-Leave', proc{scrollLeave $cscroll_canvas})
104
$cscroll_canvas.itembind('all', '1', proc{scrollButton $cscroll_canvas})
105
$cscroll_canvas.itembind('all', 'Any-Enter', proc{scrollEnter $cscroll_canvas})
106
$cscroll_canvas.bind('2', proc{|x,y| $cscroll_canvas.scan_mark(x,y)}, '%x %y')
107
$cscroll_canvas.bind('B2-Motion',
108
proc{|x,y| $cscroll_canvas.scan_dragto(x,y)}, '%x %y')
111
id = c.find_withtag('current')[0].id
112
id -= 1 if c.gettags('current').include?('text')
113
$oldFill = c.itemconfiginfo(id, 'fill')[4]
114
if TkWinfo.depth(c) > 1
115
c.itemconfigure(id, 'fill'=>'SeaGreen1')
117
c.itemconfigure(id, 'fill'=>'black')
118
c.itemconfigure(id+1, 'fill'=>'white')
123
id = c.find_withtag('current')[0].id
124
id -= 1 if c.gettags('current').include?('text')
125
c.itemconfigure(id, 'fill'=>$oldFill)
126
c.itemconfigure(id+1, 'fill'=>'black')
130
id = c.find_withtag('current')[0].id
131
id += 1 unless c.gettags('current').include?('text')
132
print "You buttoned at #{c.itemconfiginfo(id,'text')[4]}\n"