Created
December 1, 2009 18:38
-
-
Save bolthar/246516 to your computer and use it in GitHub Desktop.
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 'rubygems' | |
require 'libglade2' | |
class GtkDefinition | |
attr_reader :definition | |
def initialize | |
@definition = '<?xml version="1.0"?> | |
<interface> | |
<requires lib="gtk+" version="2.16"/> | |
<!-- interface-naming-policy project-wide --> | |
<object class="GtkWindow" id="window"> | |
<property name="visible">True</property> | |
<child> | |
<object class="GtkEntry" id="entry"> | |
<property name="visible">True</property> | |
<property name="can_focus">True</property> | |
<property name="invisible_char">●</property> | |
</object> | |
</child> | |
</object> | |
<object class="GtkEntryCompletion" id="entrycompletion"> | |
<property name="text_column">1</property> | |
<signal name="match_selected" handler="on_entrycompletion_match_selected"/> | |
</object> | |
</interface>' | |
end | |
end | |
class Item | |
attr_reader :value | |
def initialize(value) | |
@value = "value" + value.to_s | |
end | |
end | |
class TestCompletion | |
def get_items | |
return (0..10).inject([]) { |items, number| items << Item.new(number) } | |
end | |
def initialize | |
builder = Gtk::Builder.new | |
builder << GtkDefinition.new.definition | |
builder.connect_signals do |signal| | |
method(signal) | |
end | |
entry = builder.get_object('entry') | |
completion = builder.get_object('entrycompletion') | |
completion.set_match_func do |comp, key, iter| | |
true | |
end | |
completion.text_column = 1 | |
entry.completion = completion | |
list_store = Gtk::ListStore.new(Object, String) | |
get_items.each do |item| | |
iterator = list_store.append | |
iterator[0] = item | |
iterator[1] = item.value.to_s | |
end | |
completion.model = list_store | |
end | |
def on_entrycompletion_match_selected(widget, model, iter) | |
p iter[0] | |
end | |
end | |
TestCompletion.new | |
Gtk.main |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment