1# filebox.rb 2# 3# This demonstration script prompts the user to select a file.# 4# widget demo prompts the user to select a file (called by 'widget') 5# 6 7# toplevel widget 8if defined?($filebox_demo) && $filebox_demo 9 $filebox_demo.destroy 10 $filebox_demo = nil 11end 12 13# demo toplevel widget 14$filebox_demo = TkToplevel.new {|w| 15 title("File Selection Dialogs") 16 iconname("filebox") 17 positionWindow(w) 18} 19 20base_frame = TkFrame.new($filebox_demo).pack(:fill=>:both, :expand=>true) 21 22# label 23TkLabel.new(base_frame,'font'=>$font,'wraplength'=>'4i','justify'=>'left', 24 'text'=>"Enter a file name in the entry box or click on the \"Browse\" buttons to select a file name using the file selection dialog.").pack('side'=>'top') 25 26# frame 27TkFrame.new(base_frame) {|frame| 28 TkButton.new(frame) { 29 text 'Dismiss' 30 command proc{ 31 tmppath = $filebox_demo 32 $filebox_demo = nil 33 tmppath.destroy 34 } 35 }.pack('side'=>'left', 'expand'=>'yes') 36 37 TkButton.new(frame) { 38 text 'Show Code' 39 command proc{showCode 'filebox'} 40 }.pack('side'=>'left', 'expand'=>'yes') 41}.pack('side'=>'bottom', 'fill'=>'x', 'pady'=>'2m') 42 43# frame 44['open', 'save'].each{|type| 45 TkFrame.new(base_frame) {|f| 46 TkLabel.new(f, 'text'=>"Select a file to #{type}: ", 'anchor'=>'e')\ 47 .pack('side'=>'left') 48 49 TkEntry.new(f, 'width'=>20) {|e| 50 pack('side'=>'left', 'expand'=>'yes', 'fill'=>'x') 51 52 TkButton.new(f, 'text'=>'Browse ...', 53 'command'=>proc{fileDialog base_frame,e,type})\ 54 .pack('side'=>'left') 55 } 56 57 pack('fill'=>'x', 'padx'=>'1c', 'pady'=>3) 58 } 59} 60 61$tk_strictMotif = TkVarAccess.new('tk_strictMotif') 62if ($tk_platform['platform'] == 'unix') 63 TkCheckButton.new(base_frame, 64 'text'=>'Use Motif Style Dialog', 65 'variable'=>$tk_strictMotif, 66 'onvalue'=>1, 'offvalue'=>0 ).pack('anchor'=>'c') 67end 68 69def fileDialog(w,ent,operation) 70 # Type names Extension(s) Mac File Type(s) 71 # 72 #-------------------------------------------------------- 73 types = [ 74 ['Text files', ['.txt','.doc'] ], 75 ['Text files', [], 'TEXT' ], 76 ['Ruby Scripts', ['.rb'], 'TEXT' ], 77 ['Tcl Scripts', ['.tcl'], 'TEXT' ], 78 ['C Source Files', ['.c','.h'] ], 79 ['All Source Files', ['.rb','.tcl','.c','.h'] ], 80 ['Image Files', ['.gif'] ], 81 ['Image Files', ['.jpeg','.jpg'] ], 82 ['Image Files', [], ['GIFF','JPEG']], 83 ['All files', '*' ] 84 ] 85 86 if operation == 'open' 87 file = Tk.getOpenFile('filetypes'=>types, 'parent'=>w) 88 else 89 file = Tk.getSaveFile('filetypes'=>types, 'parent'=>w, 90 'initialfile'=>'Untitled', 91 'defaultextension'=>'.txt') 92 end 93 if file != "" 94 ent.delete 0, 'end' 95 ent.insert 0, file 96 # ent.xview 'end' 97 Tk.update_idletasks # need this for Tk::Tile::Entry 98 # (to find right position of 'xview'). 99 ent.xview(ent.index('end')) 100 end 101end 102 103