import pygtk pygtk.require('2.0') import gtk import sugar.env class AddressEntry(gtk.HBox): def __init__(self, callback): gtk.HBox.__init__(self) self.callback = callback self.folded = True label = gtk.Label("Open") self.pack_start(label, False) label.show() self.button = gtk.Button() self.button.set_relief(gtk.RELIEF_NONE) self.button.connect("clicked", self.__button_clicked_cb) self.pack_start(self.button, False) self.button.show() self.entry = gtk.Entry() self.entry.connect("activate", self.__activate_cb) self.pack_start(self.entry, False) self.entry.show() self._update_folded_state() def _update_folded_state(self): if self.folded: image = gtk.Image() image.set_from_icon_name('expand', gtk.ICON_SIZE_SMALL_TOOLBAR) self.button.set_image(image) image.show() self.entry.hide() else: image = gtk.Image() image.set_from_icon_name('unexpand', gtk.ICON_SIZE_SMALL_TOOLBAR) self.button.set_image(image) image.show() self.entry.show() self.entry.grab_focus() def get_folded(self): return self.folded def set_folded(self, folded): self.folded = folded self._update_folded_state() def __button_clicked_cb(self, button): self.set_folded(not self.get_folded()) def __activate_cb(self, entry): self.callback(entry.get_text()) self.set_folded(True) class AddressItem(gtk.ToolItem): def __init__(self, callback): gtk.ToolItem.__init__(self) address_entry = AddressEntry(callback) self.add(address_entry) address_entry.show()