1#!/usr/bin/env ruby
2#
3#  ttk_wrapper.rb  --  use Ttk widgets as default on old Ruby/Tk scripts
4#
5#                       by Hidetoshi NAGAI (nagai@ai.kyutech.ac.jp)
6#
7version = '0.1.3'
8#
9##########################################################################
10#  parse commandline arguments
11##########################################################################
12require 'optparse'
13opt = OptionParser.new("Usage: #{$0} [options] rubytk_script" << "\n    " <<
14                         "Ruby/Tk script wrapper. Use Ttk widgets as default.")
15opt.version = version
16
17OPTS = {}
18OPTS[:themedir] = []
19OPTS[:rb_theme] = []
20OPTS[:theme] = 'default'
21
22opt.on('-l', '--list', 'list available theme names'){|v| OPTS[:list] = true}
23opt.on('-t', '--theme theme', 'theme name'){|v| OPTS[:theme] = v}
24opt.on('-d', '--themedir themes_dir', 'directory of theme definitions'){|v|
25  OPTS[:themedir] << v
26}
27opt.on('-r', '--rubytheme rb_theme', 'theme definition file (ruby script)'){|v|
28  OPTS[:rb_theme] << v
29}
30opt.on('-v', '--verbose', 'print verbose messages'){|v| OPTS[:verbose] = true}
31
32opt.parse!(ARGV)
33
34
35##########################################################################
36#  load Ttk (Tile) extension
37##########################################################################
38require 'tk'
39
40begin
41  require 'tkextlib/tile'
42  Tk.default_widget_set = :Ttk
43rescue LoadError
44  if OPTS[:verbose]
45    print "warning: fail to load 'Ttk' extension. use standard widgets.\n"
46  end
47end
48
49if OPTS[:verbose]
50  print "current default widget set is '#{Tk.default_widget_set}'\n"
51end
52
53
54##########################################################################
55# define Tcl/Tk procedures for compatibility.
56# those are required when want to use themes included
57# in "sample/tkextlib/tile/demo.rb".
58##########################################################################
59Tk::Tile.__define_LoadImages_proc_for_compatibility__!
60Tk::Tile::Style.__define_wrapper_proc_for_compatibility__!
61
62
63##########################################################################
64#  use themes defined on the demo of Ttk (Tile) extension
65##########################################################################
66demodir = File.dirname(__FILE__)
67demo_themesdir = File.expand_path(File.join(demodir, 'tkextlib', 'tile', 'themes'))
68
69Tk::AUTO_PATH.lappend(*OPTS[:themedir]) unless OPTS[:themedir].empty?
70Tk::AUTO_PATH.lappend('.', demodir, demo_themesdir)
71
72OPTS[:themedir] << demo_themesdir
73print "theme-dirs: #{OPTS[:themedir].inspect}\n" if OPTS[:verbose]
74
75OPTS[:themedir].each{|themesdir|
76  if File.directory?(themesdir)
77    Dir.foreach(themesdir){|name|
78      next if name == '.' || name == '..'
79      path = File.join(themesdir, name)
80      Tk::AUTO_PATH.lappend(path) if File.directory?(path)
81    }
82  end
83}
84
85# This forces an update of the available packages list. It's required
86# for package names to find the themes in demos/themes/*.tcl
87Tk.ip_eval("#{TkPackage.unknown_proc}  Tcl #{TkPackage.provide('Tcl')}")
88
89# load themes written in Ruby.
90themes_by_ruby = [File.join(demo_themesdir, 'kroc.rb')]
91themes_by_ruby.concat OPTS[:rb_theme]
92print "ruby-themes: #{themes_by_ruby.inspect}\n" if OPTS[:verbose]
93
94themes_by_ruby.each{|f|
95  begin
96    load(f, true)
97  rescue LoadError
98    print "fail to load \"#{f}\"\n" if OPTS[:verbose]
99  end
100}
101
102
103##########################################################################
104# ignore unsupported options of Ttk widgets
105##########################################################################
106TkConfigMethod.__set_IGNORE_UNKNOWN_CONFIGURE_OPTION__! true
107TkItemConfigMethod.__set_IGNORE_UNKNOWN_CONFIGURE_OPTION__! true
108
109
110##########################################################################
111#  set theme of widget style
112##########################################################################
113if OPTS[:list] || OPTS[:verbose]
114  print "supported theme names: #{Tk::Tile.themes.inspect}\n"
115  exit if OPTS[:list] && ARGV.empty?
116end
117print "use theme: \"#{OPTS[:theme]}\"\n" if OPTS[:theme] && OPTS[:verbose]
118#setTheme(OPTS[:theme]) if OPTS[:theme]
119Tk::Tile.set_theme(OPTS[:theme]) if OPTS[:theme]
120
121
122##########################################################################
123#  replace $0 and $RPAGRAM_NAME
124##########################################################################
125#  When the expand_path of the target script is long, ruby sometimes
126#  fails to set the path to $0 (the path string is trimmed).
127#  The following replaces $0 and $PROGNAME to avoid such trouble.
128progname_obj = $0.dup
129$program_name = progname_obj
130
131alias $REAL_PROGRAM_NAME $0
132alias $PROGRAM_NAME $program_name
133alias $0 $program_name
134
135trace_var(:$program_name){|val|
136  unless progname_obj.object_id == val.object_id
137    progname_obj.replace(val.to_s)
138    $program_name = progname_obj
139  end
140}
141
142
143##########################################################################
144#  load script
145##########################################################################
146if (path = ARGV.shift) && (script = File.expand_path(path))
147  print "load script \"#{script}\"\n" if OPTS[:verbose]
148  $0 = script
149  load(script)
150else
151  print "Error: no script is given.\n"
152  print opt.help
153  exit(1)
154end
155