1#!/usr/bin/env ruby 2 3require 'tk' 4 5demo_dir = File.dirname($0) 6msgcat_dir = [demo_dir, 'msgs_tk'].join(File::Separator) 7top_win = nil 8#msgcat = TkMsgCatalog.new('::tk') 9msgcat = TkMsgCatalog.new('::tkmsgcat_demo') 10default_locale = msgcat.locale 11msgcat.load_tk(msgcat_dir) 12 13col_proc = TkComm.install_bind(proc{|w, color, frame, label| 14 TkComm.window(frame).background(color) 15 Tk.update 16 TkComm.window(label).text( 17 msgcat.mc("%1$s:: %2$s", 'Color', 18 color.capitalize)) 19 w.flash; w.flash 20 Tk.callback_break; 21 }, "%W") 22 23del_proc = TkComm.install_cmd(proc{top_win.destroy; top_win = nil}) 24 25err_proc = TkComm.install_cmd(proc{fail(RuntimeError, 26 msgcat.mc('Application Error'))}) 27 28show_sample = proc{|loc| 29 top_win = TkToplevel.new(:title=>loc) 30 31 msgcat.locale = loc 32 msgcat.load_tk(msgcat_dir) 33 34 TkLabel.new(top_win){ 35 text "preferences:: #{msgcat.preferences.join(' ')}" 36 pack(:pady=>10, :padx=>10) 37 } 38 39 lbl = TkLabel.new(top_win, :text=>msgcat.mc("%1$s:: %2$s", 40 'Color', '')).pack(:anchor=>'w') 41 42 bg = TkFrame.new(top_win).pack(:ipadx=>20, :ipady=>10, 43 :expand=>true, :fill=>:both) 44 45 TkFrame.new(bg){|f| 46 ['blue', 'green', 'red'].each{|col| 47 TkButton.new(f, :text=>msgcat.mc(col)){ 48 bind('ButtonRelease-1', col_proc, "#{col} #{bg.path} #{lbl.path}") 49 }.pack(:fill=>:x) 50=begin 51 TkButton.new(f, :text=>msgcat.mc(col), 52 :command=>proc{ 53 bg.background col 54 lbl.text msgcat.mc("%1$s:: %2$s", 'Color', col.capitalize) 55 }).pack(:fill=>:x) 56=end 57 } 58 }.pack(:anchor=>'center', :pady=>15) 59 60 TkFrame.new(top_win){|f| 61 TkButton.new(f, :text=>msgcat.mc('Delete'), 62 :command=>del_proc).pack(:side=>:right, :padx=>5) 63 TkButton.new(f, :text=>msgcat.mc('Error'), 64 :command=>err_proc).pack(:side=>:left, :padx=>5) 65=begin 66 TkButton.new(f, :text=>msgcat.mc('Delete'), 67 :command=>proc{ 68 top_win.destroy 69 top_win = nil 70 }).pack(:side=>:right, :padx=>5) 71 TkButton.new(f, :text=>msgcat.mc('Error'), 72 :command=>proc{ 73 fail RuntimeError, msgcat.mc('Application Error') 74 }).pack(:side=>:left, :padx=>5) 75=end 76 }.pack(:side=>:bottom, :fill=>:x) 77 78 top_win 79} 80 81 82# listbox for locale list 83TkLabel.new(:text=>"Please click a locale.").pack(:padx=>5, :pady=>3) 84 85TkFrame.new{|f| 86 TkButton.new(f, :text=>msgcat.mc('Exit'), 87 :command=>proc{exit}).pack(:side=>:right, :padx=>5) 88}.pack(:side=>:bottom, :fill=>:x) 89 90f = TkFrame.new.pack(:side=>:top, :fill=>:both, :expand=>true) 91lbox = TkListbox.new(f).pack(:side=>:left, :fill=>:both, :expand=>true) 92lbox.yscrollbar(TkScrollbar.new(f, :width=>12).pack(:side=>:right, :fill=>:y)) 93 94lbox.bind('ButtonRelease-1'){|ev| 95 idx = lbox.index("@#{ev.x},#{ev.y}") 96 if idx == 0 97 loc = default_locale 98 else 99 loc = lbox.get(idx) 100 end 101 if top_win != nil && top_win.exist? 102 top_win.destroy 103 end 104 top_win = show_sample.call(loc) 105} 106 107lbox.insert('end', 'default') 108 109Dir.entries(msgcat_dir).sort.each{|f| 110 if f =~ /^(.*).msg$/ 111 lbox.insert('end', $1) 112 end 113} 114 115top_win = show_sample.call(default_locale) 116 117# start eventloop 118Tk.mainloop 119