#!ruby # -*- mode: ruby; coding: utf-8 -*- # Last updated: <2018/02/28 05:50:27 +0900> # # gtk2 table layout sample require 'gtk2' require 'uri' class MainWindow < Gtk::Window def initialize super("Table layout sample") set_default_size(640, 260) # set MainWindow spacing self.border_width = 10 # set close button signal_connect("destroy") { Gtk.main_quit } tb = Gtk::Table.new(3, 7, false) tbls = [ {:name => "jsonfile", :label => "Source Tiled JSON", :type => 0 }, {:name => "pngfile", :label => "Source PNG Image", :type => 0 }, {:name => "outfile", :label => "Output JSON", :type => 0 }, {:name => "layername", :label => "Collision layer name", :type => 1 }, {:name => "layername_s", :label => "Layer name (optional)", :type => 1 }, {:name => "nullcode", :label => "Null code", :type => 1 }, ] @entrys = {} tbls.each_with_index do |dt, i| lb = Gtk::Label.new(dt[:label]) tb.attach(lb, 0, 1, i, i + 1, Gtk::SHRINK, Gtk::SHRINK, 6, 6) tf = Gtk::Entry.new case dt[:type] when 0 tb.attach(tf, 1, 2, i, i + 1, Gtk::FILL | Gtk::EXPAND, Gtk::SHRINK, 0, 6) # file drop setting Gtk::Drag.dest_set( tf, Gtk::Drag::DEST_DEFAULT_MOTION | Gtk::Drag::DEST_DEFAULT_HIGHLIGHT | Gtk::Drag::DEST_DEFAULT_DROP, [["text/uri-list", Gtk::Drag::TARGET_OTHER_APP, 12345]], Gdk::DragContext::ACTION_COPY | Gdk::DragContext::ACTION_MOVE ) tf.signal_connect("drag-data-received") do |w, ct, x, y, data, info, tm| data.uris.each { |uri| tf.text = URI.decode(uri).gsub(/^file\:\/\/\//, "") } end btn = Gtk::Button.new("...") btn.signal_connect("clicked") { # file select dialog dlg = Gtk::FileChooserDialog.new( "File select", self, # Gtk::FileChooser::ACTION_OPEN, Gtk::FileChooser::ACTION_SAVE, nil, [Gtk::Stock::CANCEL, Gtk::Dialog::RESPONSE_CANCEL], [Gtk::Stock::OK, Gtk::Dialog::RESPONSE_ACCEPT] ) dlg.run do |res| tf.text = dlg.filename if res == Gtk::Dialog::RESPONSE_ACCEPT end dlg.destroy } tb.attach(btn, 2, 3, i, i + 1, Gtk::SHRINK, Gtk::SHRINK, 2, 2) when 1 tb.attach(tf, 1, 2, i, i + 1, Gtk::FILL | Gtk::EXPAND, Gtk::SHRINK, 0, 6) end @entrys[dt[:name]] = tf end btn = Gtk::Button.new("Convert") tb.attach_defaults(btn, 0, 3, 6, 7) btn.signal_connect("clicked") { jsonfile = @entrys["jsonfile"].text pngfile = @entrys["pngfile"].text outfile = @entrys["outfile"].text layername = @entrys["layername"].text layername_s = @entrys["layername_s"].text nullcode = @entrys["nullcode"].text.to_i s = [ "json file : #{jsonfile}", "png image : #{pngfile}", "output json : #{outfile}", "layer name : #{layername}", "layer name sub : #{layername_s}", "null code : #{nullcode}", ].join("\n") # display message dialog dlg = Gtk::MessageDialog.new( self, Gtk::Dialog::DESTROY_WITH_PARENT, Gtk::MessageDialog::INFO, Gtk::MessageDialog::BUTTONS_CLOSE, s ) dlg.run dlg.destroy } add(tb) @entrys["layername"].text = "layer0" @entrys["nullcode"].text = "0" show_all end end if $0 == __FILE__ w = MainWindow.new Gtk.main end