[Sugar-devel] [PATCH sugar-toolkit-gtk3] Finish the port of the CellRendererIcon

Manuel Quiñones manuq at laptop.org
Thu Aug 23 14:16:32 EDT 2012


In pygtk custom cellrenderers were done inheriting
gtk.GenericCellRenderer, and overriding the on_* methods.  Now we
inherit Gtk.CellRenderer and the methods to override changed to do_* .

The destroy signal was moved to Gtk.Widget [1] so the Gtk.CellRenderer
doesn't have it anymore.  Now we do the cleanup in the python
destructor method.  A testcase tests/graphics/customdestroy.py shows
how is done.

tests/graphics/cellrenderericon.py tests the usage.

Signed-off-by: Manuel Quiñones <manuq at laptop.org>

[1] http://developer.gnome.org/gtk3/stable/ch24s02.html#id1459754
---
 src/sugar3/graphics/icon.py        | 25 ++++++++++------------
 tests/graphics/cellrenderericon.py | 37 ++++++++++++++++++++++++++++++++
 tests/graphics/customdestroy.py    | 44 ++++++++++++++++++++++++++++++++++++++
 3 files changed, 92 insertions(+), 14 deletions(-)
 create mode 100644 tests/graphics/cellrenderericon.py
 create mode 100644 tests/graphics/customdestroy.py

diff --git a/src/sugar3/graphics/icon.py b/src/sugar3/graphics/icon.py
index 1bea136..ecf657c 100644
--- a/src/sugar3/graphics/icon.py
+++ b/src/sugar3/graphics/icon.py
@@ -497,7 +497,6 @@ class EventIcon(Gtk.EventBox):
 
         from sugar3.graphics.palette import CursorInvoker
         self._palette_invoker = CursorInvoker()
-
         self._palette_invoker.attach(self)
         self.connect('destroy', self.__destroy_cb)
 
@@ -705,9 +704,7 @@ class CellRendererIcon(Gtk.CellRenderer):
 
         self._palette_invoker.attach_cell_renderer(tree_view, self)
 
-        self.connect('destroy', self.__destroy_cb)
-
-    def __destroy_cb(self, icon):
+    def __del__(self):
         self._palette_invoker.detach()
 
     def create_palette(self):
@@ -779,7 +776,8 @@ class CellRendererIcon(Gtk.CellRenderer):
 
     size = GObject.property(type=object, setter=set_size)
 
-    def on_get_size(self, widget, cell_area):
+    def do_get_size(self, widget, cell_area, x_offset=None, y_offset=None,
+                    width=None, height=None):
         width = self._buffer.width + self.props.xpad * 2
         height = self._buffer.height + self.props.ypad * 2
         xoffset = 0
@@ -797,11 +795,11 @@ class CellRendererIcon(Gtk.CellRenderer):
 
         return xoffset, yoffset, width, height
 
-    def on_activate(self, event, widget, path, background_area, cell_area,
+    def do_activate(self, event, widget, path, background_area, cell_area,
                     flags):
         pass
 
-    def on_start_editing(self, event, widget, path, background_area, cell_area,
+    def do_start_editing(self, event, widget, path, background_area, cell_area,
                          flags):
         pass
 
@@ -814,7 +812,7 @@ class CellRendererIcon(Gtk.CellRenderer):
 
         path_, column, x, y = pos
 
-        for cell_renderer in column.get_cell_renderers():
+        for cell_renderer in column.get_cells():
             if cell_renderer == self:
                 cell_x, cell_width = column.cell_get_position(cell_renderer)
                 if x > cell_x and x < (cell_x + cell_width):
@@ -823,8 +821,7 @@ class CellRendererIcon(Gtk.CellRenderer):
 
         return False
 
-    def on_render(self, window, widget, background_area, cell_area,
-            expose_area, flags):
+    def do_render(self, cr, widget, background_area, cell_area, flags):
         if self._xo_color is not None:
             stroke_color = self._xo_color.get_stroke_color()
             fill_color = self._xo_color.get_fill_color()
@@ -839,7 +836,7 @@ class CellRendererIcon(Gtk.CellRenderer):
         has_prelit_colors = None not in [prelit_fill_color,
                                          prelit_stroke_color]
 
-        if flags & Gtk.CELL_RENDERER_PRELIT and has_prelit_colors and \
+        if flags & Gtk.CellRendererState.PRELIT and has_prelit_colors and \
                 self._is_prelit(widget):
 
             self._buffer.fill_color = prelit_fill_color
@@ -852,14 +849,14 @@ class CellRendererIcon(Gtk.CellRenderer):
         if surface is None:
             return
 
-        xoffset, yoffset, width_, height_ = self.on_get_size(widget, cell_area)
+        xoffset, yoffset, width_, height_ = self.do_get_size(widget, cell_area)
 
         x = cell_area.x + xoffset
         y = cell_area.y + yoffset
 
-        cr = window.cairo_create()
         cr.set_source_surface(surface, math.floor(x), math.floor(y))
-        cr.rectangle(expose_area)
+        cr.rectangle(cell_area.x, cell_area.y, cell_area.width,
+                     cell_area.height)
         cr.paint()
 
 
diff --git a/tests/graphics/cellrenderericon.py b/tests/graphics/cellrenderericon.py
new file mode 100644
index 0000000..1f87552
--- /dev/null
+++ b/tests/graphics/cellrenderericon.py
@@ -0,0 +1,37 @@
+from gi.repository import Gtk
+
+from sugar3.graphics import style
+from sugar3.graphics.icon import CellRendererIcon
+
+import common
+
+
+test = common.Test()
+test.show()
+
+model = Gtk.ListStore(str)
+for icon in ['one', 'two', 'three']:
+    model.append([icon])
+
+treeview = Gtk.TreeView()
+treeview.set_model(model)
+test.pack_start(treeview, True, True, 0)
+treeview.show()
+
+col = Gtk.TreeViewColumn()
+treeview.append_column(col)
+
+cell_icon = CellRendererIcon(treeview)
+cell_icon.props.width = style.GRID_CELL_SIZE
+cell_icon.props.height = style.GRID_CELL_SIZE
+cell_icon.props.size = style.SMALL_ICON_SIZE
+cell_icon.props.icon_name = 'emblem-favorite'
+col.pack_start(cell_icon, expand=False)
+
+cell_text = Gtk.CellRendererText()
+col.pack_start(cell_text, expand=True)
+col.add_attribute(cell_text, 'text', 0)
+
+
+if __name__ == '__main__':
+    common.main(test)
diff --git a/tests/graphics/customdestroy.py b/tests/graphics/customdestroy.py
new file mode 100644
index 0000000..72156a8
--- /dev/null
+++ b/tests/graphics/customdestroy.py
@@ -0,0 +1,44 @@
+from gi.repository import Gtk
+
+"""
+Since GTK+3 Gtk.CellRenderer doesn't have a destroy signal anymore.
+We can do the cleanup in the python destructor method instead.
+
+"""
+
+
+class MyCellRenderer(Gtk.CellRenderer):
+    def __init__(self):
+        Gtk.CellRenderer.__init__(self)
+
+    def __del__(self):
+        print "cellrenderer destroy"
+
+    def do_render(self, cairo_t, widget, background_area, cell_area, flags):
+        pass
+
+
+def window_destroy_cb(*kwargs):
+    print "window destroy"
+    Gtk.main_quit()
+
+window = Gtk.Window(Gtk.WindowType.TOPLEVEL)
+window.connect("destroy", window_destroy_cb)
+window.show()
+
+
+def treeview_destroy_cb(*kwargs):
+    print "treeview destroy"
+
+treeview = Gtk.TreeView()
+treeview.connect("destroy", treeview_destroy_cb)
+window.add(treeview)
+treeview.show()
+
+col = Gtk.TreeViewColumn()
+treeview.append_column(col)
+
+cel = MyCellRenderer()
+col.pack_start(cel, expand=True)
+
+Gtk.main()
-- 
1.7.11.4



More information about the Sugar-devel mailing list