Run test_glade.rb
Run basic.rb
Last active
June 12, 2018 15:19
-
-
Save arjunmenon/e0a65e21b6789b03eea704660001a98f to your computer and use it in GitHub Desktop.
Glade DSL
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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