Finish the port of the CellRendererIcon
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@laptop.org> Acked-by: Simon Schampijer <simon@laptop.org> [1] http://developer.gnome.org/gtk3/stable/ch24s02.html#id1459754
This commit is contained in:
parent
40a3b2ece1
commit
a14574c666
@ -497,7 +497,6 @@ class EventIcon(Gtk.EventBox):
|
|||||||
|
|
||||||
from sugar3.graphics.palette import CursorInvoker
|
from sugar3.graphics.palette import CursorInvoker
|
||||||
self._palette_invoker = CursorInvoker()
|
self._palette_invoker = CursorInvoker()
|
||||||
|
|
||||||
self._palette_invoker.attach(self)
|
self._palette_invoker.attach(self)
|
||||||
self.connect('destroy', self.__destroy_cb)
|
self.connect('destroy', self.__destroy_cb)
|
||||||
|
|
||||||
@ -705,9 +704,7 @@ class CellRendererIcon(Gtk.CellRenderer):
|
|||||||
|
|
||||||
self._palette_invoker.attach_cell_renderer(tree_view, self)
|
self._palette_invoker.attach_cell_renderer(tree_view, self)
|
||||||
|
|
||||||
self.connect('destroy', self.__destroy_cb)
|
def __del__(self):
|
||||||
|
|
||||||
def __destroy_cb(self, icon):
|
|
||||||
self._palette_invoker.detach()
|
self._palette_invoker.detach()
|
||||||
|
|
||||||
def create_palette(self):
|
def create_palette(self):
|
||||||
@ -779,7 +776,8 @@ class CellRendererIcon(Gtk.CellRenderer):
|
|||||||
|
|
||||||
size = GObject.property(type=object, setter=set_size)
|
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
|
width = self._buffer.width + self.props.xpad * 2
|
||||||
height = self._buffer.height + self.props.ypad * 2
|
height = self._buffer.height + self.props.ypad * 2
|
||||||
xoffset = 0
|
xoffset = 0
|
||||||
@ -797,11 +795,11 @@ class CellRendererIcon(Gtk.CellRenderer):
|
|||||||
|
|
||||||
return xoffset, yoffset, width, height
|
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):
|
flags):
|
||||||
pass
|
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):
|
flags):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
@ -814,7 +812,7 @@ class CellRendererIcon(Gtk.CellRenderer):
|
|||||||
|
|
||||||
path_, column, x, y = pos
|
path_, column, x, y = pos
|
||||||
|
|
||||||
for cell_renderer in column.get_cell_renderers():
|
for cell_renderer in column.get_cells():
|
||||||
if cell_renderer == self:
|
if cell_renderer == self:
|
||||||
cell_x, cell_width = column.cell_get_position(cell_renderer)
|
cell_x, cell_width = column.cell_get_position(cell_renderer)
|
||||||
if x > cell_x and x < (cell_x + cell_width):
|
if x > cell_x and x < (cell_x + cell_width):
|
||||||
@ -823,8 +821,7 @@ class CellRendererIcon(Gtk.CellRenderer):
|
|||||||
|
|
||||||
return False
|
return False
|
||||||
|
|
||||||
def on_render(self, window, widget, background_area, cell_area,
|
def do_render(self, cr, widget, background_area, cell_area, flags):
|
||||||
expose_area, flags):
|
|
||||||
if self._xo_color is not None:
|
if self._xo_color is not None:
|
||||||
stroke_color = self._xo_color.get_stroke_color()
|
stroke_color = self._xo_color.get_stroke_color()
|
||||||
fill_color = self._xo_color.get_fill_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,
|
has_prelit_colors = None not in [prelit_fill_color,
|
||||||
prelit_stroke_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._is_prelit(widget):
|
||||||
|
|
||||||
self._buffer.fill_color = prelit_fill_color
|
self._buffer.fill_color = prelit_fill_color
|
||||||
@ -852,14 +849,14 @@ class CellRendererIcon(Gtk.CellRenderer):
|
|||||||
if surface is None:
|
if surface is None:
|
||||||
return
|
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
|
x = cell_area.x + xoffset
|
||||||
y = cell_area.y + yoffset
|
y = cell_area.y + yoffset
|
||||||
|
|
||||||
cr = window.cairo_create()
|
|
||||||
cr.set_source_surface(surface, math.floor(x), math.floor(y))
|
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()
|
cr.paint()
|
||||||
|
|
||||||
|
|
||||||
|
37
tests/graphics/cellrenderericon.py
Normal file
37
tests/graphics/cellrenderericon.py
Normal file
@ -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)
|
44
tests/graphics/customdestroy.py
Normal file
44
tests/graphics/customdestroy.py
Normal file
@ -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()
|
Loading…
Reference in New Issue
Block a user