[pygtk] date & multiline cell renderers for sqlkit

Osmo Salomaa otsaloma at cc.hut.fi
Fri Jan 9 03:16:07 WST 2009


As promised, here's code for a multiline text cell renderer that
deprecates my old post [1] (as long as you're not using an antiquated
version of PyGTK). Shift+Enter or Control+Enter can be used for a
linebreak, plain Enter will end editing.

        —  Osmo Salomaa

[1] http://www.daa.com.au/pipermail/pygtk/2005-April/009990.html


#!/usr/bin/env python

import gtk


class CellTextView(gtk.TextView, gtk.CellEditable):

    __gtype_name__ = "CellTextView"

    def do_editing_done(self, *args):
        self.remove_widget()

    def do_remove_widget(self, *args):
        pass

    def do_start_editing(self, *args):
        pass

    def get_text(self):
        text_buffer = self.get_buffer()
        bounds = text_buffer.get_bounds()
        return text_buffer.get_text(*bounds)

    def set_text(self, text):
        self.get_buffer().set_text(text)


class MultilineCellRenderer(gtk.CellRendererText):

    __gtype_name__ = "MultilineCellRenderer"

    def __init__(self):
        gtk.CellRendererText.__init__(self)
        self._in_editor_menu = False

    def _on_editor_focus_out_event(self, editor, *args):
        if self._in_editor_menu: return
        editor.remove_widget()
        self.emit("editing-canceled")

    def _on_editor_key_press_event(self, editor, event):
        if event.state & (gtk.gdk.SHIFT_MASK | gtk.gdk.CONTROL_MASK): return
        if event.keyval in (gtk.keysyms.Return, gtk.keysyms.KP_Enter):
            editor.remove_widget()
            self.emit("edited", editor.get_data("path"), editor.get_text())
        elif event.keyval == gtk.keysyms.Escape:
            editor.remove_widget()
            self.emit("editing-canceled")

    def _on_editor_populate_popup(self, editor, menu):
        self._in_editor_menu = True
        def on_menu_unmap(menu, self):
            self._in_editor_menu = False
        menu.connect("unmap", on_menu_unmap, self)

    def do_start_editing(self, event, widget, path, bg_area, cell_area, flags):
        editor = CellTextView()
        editor.modify_font(self.props.font_desc)
        editor.set_text(self.props.text)
        editor.set_size_request(cell_area.width, cell_area.height)
        editor.set_border_width(min(self.props.xpad, self.props.ypad))
        editor.set_data("path", path)
        editor.connect("focus-out-event", self._on_editor_focus_out_event)
        editor.connect("key-press-event", self._on_editor_key_press_event)
        editor.connect("populate-popup", self._on_editor_populate_popup)
        editor.show()
        return editor


if __name__ == "__main__":
    def on_edited(renderer, path, new_value, store, col):
        tree_iter = store.get_iter(path)
        store.set(tree_iter, col, new_value)
    store = gtk.ListStore(str, str)
    tree_view = gtk.TreeView()
    tree_view.set_model(store)
    tree_view.set_size_request(360, 140)
    tree_view.set_headers_visible(True)
    renderer = gtk.CellRendererText()
    renderer.props.editable = True
    renderer.connect("edited", on_edited, store, 0)
    column = gtk.TreeViewColumn("Single", renderer, text=0)
    column.set_expand(True)
    tree_view.append_column(column)
    renderer = MultilineCellRenderer()
    renderer.props.editable = True
    renderer.connect("edited", on_edited, store, 1)
    column = gtk.TreeViewColumn("Multi", renderer, text=1)
    column.set_expand(True)
    tree_view.append_column(column)
    store.append(("Phou flou\nghou fhou.", "Phou flou\nghou fhou."))
    store.append(("Bhuu fvou\nghaa neff.", "Bhuu fvou\nghaa neff."))
    window = gtk.Window()
    window.set_position(gtk.WIN_POS_CENTER)
    window.connect('delete-event', gtk.main_quit)
    window.add(tree_view)
    window.show_all()
    gtk.main()




More information about the pygtk mailing list