~ubuntu-branches/ubuntu/hardy/ruby1.8/hardy-updates

« back to all changes in this revision

Viewing changes to ext/tk/sample/demos-en/cscroll.rb

  • Committer: Bazaar Package Importer
  • Author(s): akira yamada
  • Date: 2007-03-13 22:11:58 UTC
  • mfrom: (1.1.5 upstream)
  • Revision ID: james.westby@ubuntu.com-20070313221158-h3oql37brlaf2go2
Tags: 1.8.6-1
* new upstream version, 1.8.6.
* libruby1.8 conflicts with libopenssl-ruby1.8 (< 1.8.6) (closes: #410018)
* changed packaging style to cdbs from dbs.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# cscroll.rb
 
2
#
 
3
# This demonstration script creates a simple canvas that can be
 
4
# scrolled in two dimensions.
 
5
#
 
6
# simple scrollable canvas widget demo (called by 'widget')
 
7
#
 
8
 
 
9
# toplevel widget
 
10
if defined?($cscroll_demo) && $cscroll_demo
 
11
  $cscroll_demo.destroy 
 
12
  $cscroll_demo = nil
 
13
end
 
14
 
 
15
# demo toplevel widget
 
16
$cscroll_demo = TkToplevel.new {|w|
 
17
  title("Scrollable Canvas Demonstration")
 
18
  iconname("cscroll")
 
19
  positionWindow(w)
 
20
}
 
21
 
 
22
# label
 
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."){
 
25
  pack('side'=>'top')
 
26
}
 
27
 
 
28
# frame
 
29
$cscroll_buttons = TkFrame.new($cscroll_demo) {|frame|
 
30
  TkButton.new(frame) {
 
31
    text 'Dismiss'
 
32
    command proc{
 
33
      tmppath = $cscroll_demo
 
34
      $cscroll_demo = nil
 
35
      tmppath.destroy
 
36
    }
 
37
  }.pack('side'=>'left', 'expand'=>'yes')
 
38
 
 
39
  TkButton.new(frame) {
 
40
    text 'Show Code'
 
41
    command proc{showCode 'cscroll'}
 
42
  }.pack('side'=>'left', 'expand'=>'yes')
 
43
}
 
44
$cscroll_buttons.pack('side'=>'bottom', 'fill'=>'x', 'pady'=>'2m')
 
45
 
 
46
# frame 
 
47
unless $tk_version =~ /^4\.[01]/
 
48
  $cscroll_grid = TkFrame.new($cscroll_demo) {
 
49
    pack('expand'=>'yes', 'fill'=>'both', 'padx'=>1, 'pady'=>1)
 
50
  }
 
51
  TkGrid.rowconfigure($cscroll_grid, 0, 'weight'=>1, 'minsize'=>0)
 
52
  TkGrid.columnconfigure($cscroll_grid, 0, 'weight'=>1, 'minsize'=>0)
 
53
end
 
54
 
 
55
# canvas
 
56
$cscroll_canvas = TkCanvas.new($cscroll_demo, 
 
57
                               'relief'=>'sunken', 'borderwidth'=>2,
 
58
                               'scrollregion'=>['-11c', '-11c', '50c', '20c']
 
59
                               ) {|c|
 
60
  if $tk_version =~ /^4\.[01]/
 
61
    pack('expand'=>'yes', 'fill'=>'both') 
 
62
  else
 
63
    grid('in'=>$cscroll_grid, 'padx'=>1, 'pady'=>1, 'row'=>0, 'column'=>0, 
 
64
         'rowspan'=>1, 'columnspan'=>1, 'sticky'=>'news')
 
65
  end
 
66
 
 
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')
 
71
    else
 
72
      grid('in'=>$cscroll_grid, 'padx'=>1, 'pady'=>1, 'row'=>0, 'column'=>1, 
 
73
           'rowspan'=>1, 'columnspan'=>1, 'sticky'=>'news')
 
74
    end
 
75
  }
 
76
 
 
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') 
 
82
    else
 
83
      grid('in'=>$cscroll_grid, 'padx'=>1, 'pady'=>1, 'row'=>1, 'column'=>0, 
 
84
           'rowspan'=>1, 'columnspan'=>1, 'sticky'=>'news')
 
85
    end
 
86
  }
 
87
}
 
88
 
 
89
bg = $cscroll_canvas.configinfo('bg')[4]
 
90
(0..19).each{|i|
 
91
  x = -10+3*i
 
92
  y = -10
 
93
  (0..9).each{|j|
 
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')
 
98
    y += 3
 
99
  }
 
100
}
 
101
 
 
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')
 
109
 
 
110
def scrollEnter(c)
 
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')
 
116
  else
 
117
    c.itemconfigure(id, 'fill'=>'black')
 
118
    c.itemconfigure(id+1, 'fill'=>'white')
 
119
  end
 
120
end
 
121
 
 
122
def scrollLeave(c)
 
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')
 
127
end
 
128
 
 
129
def scrollButton(c)
 
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"
 
133
end
 
134