1# -*- coding: utf-8 -*-
2# image3.rb
3#
4# This demonstration script creates a simple collection of widgets
5# that allow you to select and view images in a Tk label.
6#
7# widget demo 'load image' (called by 'widget')
8#
9
10# toplevel widget
11if defined?($image3_demo) && $image3_demo
12  $image3_demo.destroy
13  $image3_demo = nil
14end
15
16# demo toplevel widget
17$image3_demo = TkToplevel.new {|w|
18  title('Image Demonstration #3')
19  iconname("Image3")
20  positionWindow(w)
21}
22
23base_frame = TkFrame.new($image3_demo).pack(:fill=>:both, :expand=>true)
24
25#
26def loadDir3(w)
27  w.delete(0,'end')
28  Dir.glob([$dirName,'*'].join(File::Separator)).sort.each{|f|
29    w.insert('end',File.basename(f))
30  }
31end
32
33# selectAndLoadDir3 --
34# This procedure pops up a dialog to ask for a directory to load into
35# the listobx and (if the user presses OK) reloads the directory
36# listbox from the directory named in the demo's entry.
37#
38# Arguments:
39# w -                   Name of the toplevel window of the demo.
40def selectAndLoadDir3(w, lbox)
41  dir = Tk.chooseDirectory(:initialdir=>$dirName.value,
42                           :parent=>w, :mustexist=>true)
43  if dir.length > 0
44    $dirName.value = dir
45    loadDir3(lbox)
46  end
47end
48
49def loadImage3(w,x,y)
50  $image3a.file([$dirName, w.get("@#{x},#{y}")].join(File::Separator))
51end
52
53
54# label
55msg = TkLabel.new(base_frame) {
56  font $font
57  wraplength '4i'
58  justify 'left'
59  text "このデモではTkの photo image を使用して画像を 見ることができます。最初にエントリ内ににディレクトリ名を入れて下さい。次に下のリストボックスにこのディレクトリをロードするため、リターンを押してください。その後、画像を選択するためにリストボックスの中のファイル名をダブルクリックして下さい。"
60}
61msg.pack('side'=>'top')
62
63# frame
64TkFrame.new(base_frame) {|frame|
65  TkButton.new(frame) {
66    #text '了解'
67    text '閉じる'
68    command proc{
69      tmppath = $image3_demo
70      $image3_demo = nil
71      tmppath.destroy
72    }
73  }.pack('side'=>'left', 'expand'=>'yes')
74
75  TkButton.new(frame) {
76    text 'コード参照'
77    command proc{showCode 'image3'}
78  }.pack('side'=>'left', 'expand'=>'yes')
79
80}.pack('side'=>'bottom', 'fill'=>'x', 'pady'=>'2m')
81
82# variable
83$dirName = TkVariable.new([$demo_dir,'..','images'].join(File::Separator))
84
85# image
86begin
87  $image3a.delete
88rescue
89end
90$image3a = TkPhotoImage.new
91
92#
93image3_f = TkFrame.new(base_frame).pack(:fill=>:both, :expand=>true)
94
95image3_df = TkLabelFrame.new(base_frame, :text=>'ディレクトリ:')
96
97image3_ff = TkLabelFrame.new(base_frame, :text=>'ファイル:',
98                             :padx=>'2m', :pady=>'2m')
99image3_lbx = TkListbox.new(image3_ff, :width=>20, :height=>10) {
100  pack(:side=>:left, :fill=>:y, :expand=>true)
101  yscrollbar(TkScrollbar.new(image3_ff).pack(:side=>:left, :fill=>:y,
102                                             :expand=>true))
103  insert(0, *(%w(earth.gif earthris.gif teapot.ppm)))
104  bind('Double-1', proc{|x,y| loadImage3(self, x, y)}, '%x %y')
105}
106
107image3_ent = TkEntry.new(image3_df, :width=>30, :textvariable=>$dirName){
108  pack(:side=>:left, :fill=>:both, :padx=>'2m', :pady=>'2m', :expand=>true)
109  bind('Return', proc{loadDir3(image3_lbx)})
110}
111
112TkButton.new(image3_df, :pady=>0, :padx=>'2m', :text=>"ディレクトリ選択",
113             :command=>proc{selectAndLoadDir3(image3_ent, image3_lbx)}) {
114  pack(:side=>:left, :fill=>:y, :padx=>[0, '2m'], :pady=>'2m')
115}
116
117image3_if = TkLabelFrame.new(base_frame, :text=>'イメージ:') {|f|
118  # TkLabel.new(f, :image=>$image3a).pack(:padx=>'2m', :pady=>'2m')
119  Tk::Label.new(f, :image=>$image3a).pack(:padx=>'2m', :pady=>'2m')
120}
121
122Tk.grid(image3_df,  '-',
123        :sticky=>:ew, :padx=>'1m', :pady=>'1m', :in=>image3_f)
124Tk.grid(image3_ff, image3_if,
125        :sticky=>:nw, :padx=>'1m', :pady=>'1m', :in=>image3_f)
126TkGrid.columnconfigure(image3_f, 1, :weight=>1)
127
128