Skip to content

Instantly share code, notes, and snippets.

@arjunmenon
Last active June 12, 2018 15:19
Show Gist options
  • Save arjunmenon/e0a65e21b6789b03eea704660001a98f to your computer and use it in GitHub Desktop.
Save arjunmenon/e0a65e21b6789b03eea704660001a98f to your computer and use it in GitHub Desktop.
Glade DSL

Run test_glade.rb
Run basic.rb

require 'gtk3'
class MyApp
def initialize (glade_path)
@builder = Gtk::Builder.new
@builder.add_from_file(glade_path)
@builder.connect_signals { |handler| method (handler) }
@main_window = @builder.get_object('window1')
end
def run
@main_window.show
Gtk.main
end
# signal handler for main window destory event
def quit
Gtk.main_quit
end
end
MyApp.new('glade.glade').run
require './xmldsl'
xml = XML.generate do
interface do
requires lib: "gtk+", version:"3.12"
object class: "GtkWindow", id: "window1" do
property name:"can_focus" do
content ["False"]
end
child do
placeholder
end
end
end
end
open('glade.glade', 'w') {|f| f.puts xml}
puts xml
# DSL to generate XML from Ruby
# Fork of https://gist.github.com/kimjoar/2773597
class XML
def self.generate &block
XML.new.xml_generate &block
end
def xml_generate &block
instance_eval(&block).join
end
def xml
@xml or @xml = ["<?xml version=\"1.0\" encoding=\"UTF-8\"?> \n"]
end
def method_missing name, args = {}
xml << "#{indent}<#{name} #{attributes(args)}"
if block_given?
xml << ">\n"
indent do
yield
end
xml << "#{indent}</#{name}>\n"
else
xml << "/>\n"
end
end
def indent
@indent or @indent = 0
if block_given?
@indent += 3
yield
@indent -= 3
else
" "*@indent
end
end
def attributes args
args.map { |key, value| "#{key}='#{value}'"}.join(" ")
end
def content txt
xml << "#{indent}#{[txt].join}\n"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment