File: libglade2.rb

package info (click to toggle)
ruby-gnome2 0.19.3-2
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 11,556 kB
  • ctags: 14,033
  • sloc: ansic: 83,294; ruby: 33,426; makefile: 3,721; cpp: 47; xml: 35
file content (98 lines) | stat: -rw-r--r-- 2,281 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
=begin
  libglade2.rb

  Copyright (c) 2002-2009 Ruby-GNOME2 Project Team
  This program is licenced under the same licence as Ruby-GNOME2.
=end

require 'gtk2'
begin
  require 'gnome2'
rescue LoadError
  puts "Ruby/GNOME2 is not supported." if $DEBUG
end
require 'libglade2.so'
GladeXML.set_custom_widget_handler(true)

class GladeXML
  attr_accessor :handler_proc
  attr_reader :xml

  def canonical_handler(handler)
    return handler.gsub(/[-\s]/, "_")
  end

  def connect(source, target, signal, handler, data, after = false)
    @handler_proc ||= Proc.new{}
    handler = canonical_handler(handler)
    if target
      signal_proc = target.method(handler)
    else
      signal_proc = @handler_proc.call(handler)
    end

    if after
      sig_conn_proc = source.method(:signal_connect_after)
    else
      sig_conn_proc = source.method(:signal_connect)
    end

    if signal_proc
      case signal_proc.arity
      when 0
        sig_conn_proc.call(signal) {signal_proc.call}
      else
        sig_conn_proc.call(signal, &signal_proc)
      end
    elsif $DEBUG
      puts "Undefined handler: #{handler}"
    end
  end

  def widget_names(nocache = false)
    build_names(nocache)[0]
  end

  def custom_creation_methods(nocache = false)
    build_names(nocache)[1]
  end

  private
  def escape_xml(str)
    str.gsub(/&(.*?);/n) {
      match = $1.dup
      case match
      when /\Aamp\z/ni           then '&'
      when /\Aquot\z/ni          then '"'
      when /\Agt\z/ni            then '>'
      when /\Alt\z/ni            then '<'
      else
	match
      end
    }
  end

  def build_names(nocache)
    if nocache || ! @widget_names
      @widget_names = []
      @custom_methods = []
    end
    if @widget_names.size == 0
      regexp_name = Regexp.new("<widget class=\".*\" id=\"(.*)\"")
      regexp_custom = Regexp.new("<property name=\"creation_function\">(.*)</property>")
      gladexml = filename ? IO.read(filename) : xml
      gladexml.each_line { |line|
	if md = regexp_name.match(line)
	  @widget_names << escape_xml(md[1])
	elsif md = regexp_custom.match(line)
	  @custom_methods << escape_xml(md[1])
	end
      }
    end
    [@widget_names, @custom_methods]    
  end

  LOG_DOMAIN = "libglade"
end

GLib::Log.set_log_domain(GladeXML::LOG_DOMAIN)