File: ruby-glade-create-template

package info (click to toggle)
ruby-gnome2 0.12.0-2sarge1
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 6,820 kB
  • ctags: 7,421
  • sloc: ansic: 61,387; ruby: 17,307; makefile: 85; xml: 35; sql: 13
file content (246 lines) | stat: -rwxr-xr-x 6,960 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
#!/usr/bin/env ruby
#
# ruby-glade-create-template
#
# Create a ruby-glade template .rb file from a .glade file.
#
# Usage: ruby ruby-glade-create-template yourgladefile.glade > file.rb
#
# $Id: ruby-glade-create-template,v 1.11 2005/02/12 04:10:51 mutoh Exp $
#
# Copyright (c) 2002-2004 Masao Mutoh <mutoh@highway.ne.jp>
#

require 'libglade2'
require 'rexml/document'

LG_VERSION = %Q[$Revision: 1.11 $].scan(/\d/).join(".")

unless ARGV.size == 1
  puts "ruby-glade-create-template #{LG_VERSION}"
  puts "\nUsage: ruby ruby-glade-create-template.rb yourgladefile.glade > file.rb\n\n"
  exit 1
end

path = ARGV[0]

#
# Support GNOME ?
#
type_gnome = false
doc = REXML::Document.new(IO.read(path))
doc.elements.each("//requires") do |e|
  if e.attributes["lib"] == "gnome"
    type_gnome = true
    begin
      require 'gnome2'
    rescue LoadError
      puts "The .glade requires Ruby/GNOME2, but it is not available on your system."
      exit 1
    end
  end
end

#
# Analyse .glade file.
#
filename = File.basename(path, ".*")

# Create class name from the filename.
classname = filename.split(/[_-]/).collect{|item| item.capitalize}.join("") + "Glade"

if type_gnome
  Gnome::Program.new("ruby-glade-create-template", LG_VERSION)
else
  Gtk.init
end

# Get signals.
GladeXML.set_custom_widget_handler(false)
glade = GladeXML.new(path)
signals = []
glade.signal_autoconnect_full{|source, target, signal_name, handler, data, after|
  signals << [source.class.signal(signal_name), glade.canonical_handler(handler)]
}

# Get Menu for GnomeApp and Tooltips.
gnomeapp = nil
uiinfos = []
tooltips = []
doc.elements.each("//widget") do |e|
  if e.attributes["class"] == "GnomeApp"
    gnomeapp = e.attributes["id"]
  elsif /Separator/ =~ e.attributes["class"]
    #Do nothing
    #    elsif /ImageMenuItem/ =~ e.attributes["class"]
  elsif e.elements["property[@name='stock_item']"]
    e.elements.each("property[@name='stock_item']"){|v|
      if v.attributes["name"] == "stock_item"
        if /GNOMEUIINFO_MENU_(.*)_ITEM/ =~ v.text
          if $1 == "EXIT"
            uiinfos << [%Q!Gnome::UIInfo::menu_quit_item(callback_dummy, nil)!, e.attributes['id']]
          elsif $1 == "NEW"
            label = ""
            e.elements.each("property[@name='label']") do |v|
              label = "'#{v.text}'"
            end
            uiinfos << [%Q!Gnome::UIInfo::menu_new_item(#{label}, nil, callback_dummy, nil)!, e.attributes['id']]
          else
            tooltip = "nil"
            e.elements.each("property[@name='tooltip']") do |v|
              tooltip = "'#{v.text}'"
            end
            uiinfos << [%Q!Gnome::UIInfo::menu_#{$1.downcase}_item(callback_dummy, #{tooltip})!, e.attributes['id']]
          end
        end
      end
    }
  elsif /MenuItem/ =~ e.attributes["class"]
    label = "nil"
    tooltip = nil
    e.elements.each("property[@name='tooltip']") do |v|
      tooltip = "'#{v.text}'"
    end
    if tooltip
      uiinfos << [%Q!Gnome::UIInfo.item_none(#{label}, #{tooltip}, callback_dummy)!, e.attributes['id'], true]
    end
  elsif /Tool|Action/ =~ e.attributes["class"]
    tooltip = nil
    e.elements.each("property[@name='tooltip']") do |v|
      tooltip = "'#{v.text}'"
    end
    if tooltip
      tooltips << %Q!@glade['#{e.attributes['id']}'].set_tooltip(@tooltip, _(#{tooltip}))!
    end
  end
end

additional_methods = ""
# Create an additional method for creating additional menu hints of GnomeApp.
additional_method_body = ""
if gnomeapp and uiinfos.size > 0
  additional_method_body << "\n  # Creates menu hints.\n"
  additional_method_body << "  def create_uiinfo_menus(name)\n"
  additional_method_body << "    app = @glade['#{gnomeapp}']\n"
  additional_method_body << "    tips = @glade.get_tooltips(app.toplevel)\n"
  additional_method_body << "    callback_dummy = Proc.new{} #Dummy \n"
  additional_method_body << "    uiinfos = [\n"
  uiinfos.each do |info|
    additional_method_body << "      #{info[0]},\n"
  end
  additional_method_body << "    ]\n"
  uiinfos.each_with_index do |info, i|
    additional_method_body << "    uiinfos[#{i}][9] = @glade['#{info[1]}']\n"
    additional_method_body << "    tips.set_tip(uiinfos[#{i}][9], nil, nil)\n" if info[2]
  end
  additional_method_body << "    app.install_menu_hints(uiinfos)\n"
  additional_method_body << "  end"
  additional_methods << "\n    create_uiinfo_menus(domain)"
end

# Create an additional method for creating tooltips.
if tooltips.size > 0
  additional_method_body << "\n  # Creates tooltips.\n"
  additional_method_body << "  def create_tooltips\n"
  additional_method_body << "    @tooltip = Gtk::Tooltips.new\n"
  tooltips.each do |v|
    additional_method_body << "    #{v}\n"
  end
  additional_method_body << "  end"
  additional_methods << "\n    create_tooltips"
end

# Creates handler methods
handler_methods = ""
if signals.size > 0
  signals.each do |signal, handler|
    args = "widget"
    (0...signal.param_types.size).each do |i|
      args << ", arg#{i}"
    end
    handler_methods << "\n  def #{handler}(#{args})\n"
    handler_methods << "    puts \"#{handler}() is not implemented yet.\"\n"
    handler_methods << "  end"
  end
end

#
# Print template.
#
puts <<CLASS_DEF
#!/usr/bin/env ruby
#
# This file is gererated by ruby-glade-create-template #{LG_VERSION}.
#
require 'libglade2'

# Support L10n (Requires RAA:Ruby-GetText-Package)
begin
  require 'gettext'
rescue LoadError
  unless defined? GetText
    module GetText
      module_function
      def _(msgid); msgid; end
      def N_(msgid); msgid; end
      def n_(msgid, msgid_plural, n)
	msgid
      end
      def s_(msgid, div = '|')
	if index = msgid.rindex(div)
	  msgid = msgid[(index + 1)..-1]
	else
	  msgid
	end
      end
      def bindtextdomain(domainname, path = nil, locale = nil, charset = nil)
      end
    end
  end
end

class #{classname}
  include GetText
  #{additional_method_body}
  def initialize(path_or_data, root = nil, domain = nil, localedir = nil, flag = GladeXML::FILE)
    GetText.bindtextdomain(domain, localedir, nil, "UTF-8")
    @glade = GladeXML.new(path_or_data, root, domain, localedir, flag) {|handler| method(handler)}
    #{additional_methods}
  end
  #{handler_methods}
end
CLASS_DEF

puts <<MAIN1

# Main program
if __FILE__ == $0
  # Set values as your own application. 
  PROG_PATH = "#{path}"
  PROG_NAME = "YOUR_APPLICATION_NAME"
MAIN1

# Bottom mehotds
if type_gnome
puts <<FOOTER_GNOME
  PROG_VERSION = "YOUR_APPLICATION_VERSION"
  Gnome::Program.new(PROG_NAME, PROG_VERSION)
  #If you use Ruby/GTK2 widgets only, call Gtk.init not Gnome::Program.new here.
  #Gtk.init
FOOTER_GNOME
else
  puts "  Gtk.init"
end
puts <<FOOTER
  #{classname}.new(PROG_PATH, nil, PROG_NAME)
  Gtk.main
end
FOOTER

if glade.custom_creation_methods.size > 0
  puts "#You may need to implement some custom creation methods which return new widget."
  glade.custom_creation_methods.each do |v|
    puts "#" + v
  end
end