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

« back to all changes in this revision

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