1# image2.rb
2#
3# This demonstration script creates a simple collection of widgets
4# that allow you to select and view images in a Tk label.
5#
6# widget demo 'load image' (called by 'widget')
7#
8
9# toplevel widget
10if defined?($image2_demo) && $image2_demo
11  $image2_demo.destroy
12  $image2_demo = nil
13end
14
15# demo toplevel widget
16$image2_demo = TkToplevel.new {|w|
17  title('Image Demonstration #2')
18  iconname("Image2")
19  positionWindow(w)
20}
21
22base_frame = TkFrame.new($image2_demo).pack(:fill=>:both, :expand=>true)
23
24# label
25msg = TkLabel.new(base_frame) {
26  font $font
27  wraplength '4i'
28  justify 'left'
29  text "This demonstration allows you to view images using a Tk \"photo\" image.  First type a directory name in the listbox, then press Enter to load the directory into the listbox.  Then double-click on a file name in the listbox to see that image."
30}
31msg.pack('side'=>'top')
32
33# frame
34TkFrame.new(base_frame) {|frame|
35  TkButton.new(frame) {
36    text 'Dismiss'
37    command proc{
38      tmppath = $image2_demo
39      $image2_demo = nil
40      tmppath.destroy
41    }
42  }.pack('side'=>'left', 'expand'=>'yes')
43
44  TkButton.new(frame) {
45    text 'Show Code'
46    command proc{showCode 'image2'}
47  }.pack('side'=>'left', 'expand'=>'yes')
48
49}.pack('side'=>'bottom', 'fill'=>'x', 'pady'=>'2m')
50
51# create variable
52$dirName = TkVariable.new([$demo_dir,'..','images'].join(File::Separator))
53
54# image
55$image2a = TkPhotoImage.new
56
57#
58TkLabel.new(base_frame, 'text'=>'Directory:')\
59.pack('side'=>'top', 'anchor'=>'w')
60
61image2_e = TkEntry.new(base_frame) {
62  width 30
63  textvariable $dirName
64}.pack('side'=>'top', 'anchor'=>'w')
65
66TkFrame.new(base_frame, 'height'=>'3m', 'width'=>20)\
67.pack('side'=>'top', 'anchor'=>'w')
68
69TkLabel.new(base_frame, 'text'=>'File:')\
70.pack('side'=>'top', 'anchor'=>'w')
71
72TkFrame.new(base_frame){|w|
73  s = TkScrollbar.new(w)
74  l = TkListbox.new(w) {
75    width 20
76    height 10
77    yscrollcommand proc{|first,last| s.set first,last}
78  }
79  s.command(proc{|*args| l.yview(*args)})
80  l.pack('side'=>'left', 'expand'=>'yes', 'fill'=>'y')
81  s.pack('side'=>'left', 'expand'=>'yes', 'fill'=>'y')
82  #l.insert(0,'earth.gif', 'earthris.gif', 'mickey.gif', 'teapot.ppm')
83  l.insert(0,'earth.gif', 'earthris.gif', 'teapot.ppm')
84  l.bind('Double-1', proc{|x,y| loadImage $image2a,l,x,y}, '%x %y')
85
86  image2_e.bind 'Return', proc{loadDir l}
87
88}.pack('side'=>'top', 'anchor'=>'w')
89
90# image
91[ TkFrame.new(base_frame, 'height'=>'3m', 'width'=>20),
92  TkLabel.new(base_frame, 'text'=>'Image:'),
93  TkLabel.new(base_frame, 'image'=>$image2a)
94].each{|w| w.pack('side'=>'top', 'anchor'=>'w')}
95
96#
97def loadDir(w)
98  w.delete(0,'end')
99  Dir.glob([$dirName,'*'].join(File::Separator)).sort.each{|f|
100    w.insert('end',File.basename(f))
101  }
102end
103
104def loadImage(img,w,x,y)
105  img.file([$dirName, w.get("@#{x},#{y}")].join(File::Separator))
106end
107
108