#!/usr/bin/env ruby require 'gtk2' def lookup_word(find) results = 0 # Create model model = Gtk::TreeStore.new(String, String) # Fill model with data File.read("/usr/share/trans/de-en").each do |line| de, en = line.split("::") if en != nil and en.downcase.include?(find.downcase) results += 1 # Add row to model root_iter = model.append(nil) root_iter[0] = en.strip root_iter[1] = de.strip end end window = Gtk::Window.new(sprintf("Suche nach »%s«", find)) if results == 0 # No results found box = Gtk::VBox.new(false, 10) window.add(box) label = Gtk::Label.new("Es konnten keine Übersetzungen gefunden werden."); box.add(label) button = Gtk::Button.new("Beenden") button.signal_connect("clicked") do window.destroy end box.add(button) else # Results found # Create view tv = Gtk::TreeView.new(model) # Column 1 renderer = Gtk::CellRendererText.new column = Gtk::TreeViewColumn.new( "Englisch", renderer, { :text => 0 } ) tv.append_column(column) # Column 2 renderer = Gtk::CellRendererText.new column = Gtk::TreeViewColumn.new( "Deutsch", renderer, { :text => 1 } ) tv.append_column(column) window.add(tv).set_default_size(300, 150).show_all end window.show_all; window.signal_connect("destroy"){ Gtk.main_quit } end Gtk.init if ARGV[0] then # Use command line argument lookup_word(ARGV[0]) else # Display entry window window = Gtk::Window.new("Wörterbuch") # VBox into Window vbox = Gtk::VBox.new(false, 10) window.add(vbox) # HBox into VBox hbox = Gtk::HBox.new(false, 10) vbox.add(hbox) # Label into HBox label = Gtk::Label.new("Englisch:") hbox.add(label) # Entry into HBox entry = Gtk::Entry.new hbox.add(entry) # Button into VBox button = Gtk::Button.new("Nachschlagen") button.signal_connect("clicked") do lookup_word(entry.text) window.destroy end vbox.add(button) # Show Window window.show_all end # Enter main loop Gtk.main