Class: TkXML

Inherits:
Object
  • Object
show all
Defined in:
lib/tkxml.rb,
lib/tkxml/rexml.rb,
lib/tkxml/nokogiri.rb

Overview

A combination of Ruby, Tcl/Tk and Nokogiri or REXML to allow for fast and easy creation of Tk interfaces using standard XML.

xml = File.open("ui.xml")
tkxml_instance = TkXML.new(xml)
tkxml_instance.build

Defined Under Namespace

Classes: Nokogiri_Listener, REXML_Listener

Constant Summary collapse

VERSION =

Current version of TkXML.

'0.3.1'

Instance Method Summary collapse

Constructor Details

#initialize(source, type = :nokogiri) ⇒ TkXML

Returns a new instance of TkXML.



16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/tkxml.rb', line 16

def initialize(source, type=:nokogiri)
  @widget = {}
  @widget_stack = []
  @parent = nil

  case type
  when :nokogiri
    require 'tkxml/nokogiri'
  else
    require 'tkxml/rexml'
  end

  parse(source)
end

Instance Method Details

#buildObject



32
33
34
# File 'lib/tkxml.rb', line 32

def build
  Tk.mainloop
end

#end_method(name) ⇒ Object



129
130
131
# File 'lib/tkxml.rb', line 129

def end_method(name)
  puts "End Method: #{name}"
end

#end_widget(name) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/tkxml.rb', line 71

def end_widget(name)
  @parent = @widget_stack[-2]
  current = @widget_stack.last

  case name.downcase
  when "menu"
    @parent.menu(current)
  end

  # pop current widget off of stack
  @widget_stack.pop

  puts "End Widget: #{name}"
end

#parse(source) ⇒ Object



6
7
8
9
# File 'lib/tkxml/rexml.rb', line 6

def parse(source)
  listener = REXML_Listener.new(self)
  REXML::Document.parse_stream(source, listener)
end

#startObject



36
37
38
39
# File 'lib/tkxml.rb', line 36

def start
  ## get parent, the widget on the bottom of the stack
  @parent = @widget_stack.last
end

#start_method(name, attrs) ⇒ Object



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
# File 'lib/tkxml.rb', line 87

def start_method(name, attrs)
  ## apply method
  puts "Applying Method: #{name} to #{@parent}"

  ##
  attrs = assoc_to_hash(attrs)

  ## assign the method's parameters
  p_arr  = []  # array for parameters to be passed
  p_init = {}  # for the ordered arguments _1 _2 etc.
  p_hash = {}  # for all other named parameters

  ## weed out the ordered parameters from the hash parameters
  attrs.each do |n, v|
    puts "   #{n} => #{v}"
    if n[0..0] == "_"
      p_init[n] = v
    else
      p_hash[n] = v
    end
  end

  ## sort the ordered parameters based on the hash key
  ## note: this converts p_init into an associative array
  ## then place each one in the parameter array
  if not p_init.empty?
    p_init.sort
    p_init.each do |a|
      p_arr.push a[1]
    end
  end

  ## now add the hash to the array if there is one
  if not p_hash.empty?
    p_arr.push p_hash
  end

  ## call the method
  @parent.send(name, *p_arr)
end

#start_widget(name, attrs) ⇒ Object



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
# File 'lib/tkxml.rb', line 42

def start_widget(name, attrs)
  ## create widget
  puts "Creating Widget: #{name} of #{@parent}"

  ##
  attrs = assoc_to_hash(attrs)

  widget_class = "Tk" + name.capitalize
  widget_name = attrs['name']

  if @parent == nil
    @widget[widget_name] = Tk.const_get(widget_class).new
  else
    @widget[widget_name] = Tk.const_get(widget_class).new(@parent)
  end

  ## assign the widget properties
  attrs.each do |n, v|
    if not n == 'name'
      puts "   #{n} => #{v}"
      @widget[widget_name].send(n, v)
    end
  end

  ## push widget on to the stack
  @widget_stack.push(@widget[widget_name])
end