Exemple #1
0
class CellRendererIcon(gtk.GenericCellRenderer):

    __gtype_name__ = 'SugarCellRendererIcon'

    __gsignals__ = {
        'clicked': (gobject.SIGNAL_RUN_FIRST, gobject.TYPE_NONE, [object]),
    }

    def __init__(self, tree_view):
        from sugar.graphics.palette import CellRendererInvoker

        self._buffer = _IconBuffer()
        self._buffer.cache = True
        self._xo_color = None
        self._fill_color = None
        self._stroke_color = None
        self._prelit_fill_color = None
        self._prelit_stroke_color = None
        self._palette_invoker = CellRendererInvoker()

        gobject.GObject.__init__(self)

        self._palette_invoker.attach_cell_renderer(tree_view, self)

        self.connect('destroy', self.__destroy_cb)

    def __destroy_cb(self, icon):
        self._palette_invoker.detach()

    def create_palette(self):
        return None

    def get_palette_invoker(self):
        return self._palette_invoker

    palette_invoker = gobject.property(type=object, getter=get_palette_invoker)

    def set_file_name(self, value):
        if self._buffer.file_name != value:
            self._buffer.file_name = value

    file_name = gobject.property(type=str, setter=set_file_name)

    def set_icon_name(self, value):
        if self._buffer.icon_name != value:
            self._buffer.icon_name = value

    icon_name = gobject.property(type=str, setter=set_icon_name)

    def get_xo_color(self):
        return self._xo_color

    def set_xo_color(self, value):
        self._xo_color = value

    xo_color = gobject.property(type=object,
            getter=get_xo_color, setter=set_xo_color)

    def set_fill_color(self, value):
        if self._fill_color != value:
            self._fill_color = value

    fill_color = gobject.property(type=object, setter=set_fill_color)

    def set_stroke_color(self, value):
        if self._stroke_color != value:
            self._stroke_color = value

    stroke_color = gobject.property(type=object, setter=set_stroke_color)

    def set_prelit_fill_color(self, value):
        if self._prelit_fill_color != value:
            self._prelit_fill_color = value

    prelit_fill_color = gobject.property(type=object,
                                         setter=set_prelit_fill_color)

    def set_prelit_stroke_color(self, value):
        if self._prelit_stroke_color != value:
            self._prelit_stroke_color = value

    prelit_stroke_color = gobject.property(type=object,
                                           setter=set_prelit_stroke_color)

    def set_background_color(self, value):
        if self._buffer.background_color != value:
            self._buffer.background_color = value

    background_color = gobject.property(type=object,
                                        setter=set_background_color)

    def set_size(self, value):
        if self._buffer.width != value:
            self._buffer.width = value
            self._buffer.height = value

    size = gobject.property(type=object, setter=set_size)

    def on_get_size(self, widget, cell_area):
        width = self._buffer.width + self.props.xpad * 2
        height = self._buffer.height + self.props.ypad * 2
        xoffset = 0
        yoffset = 0

        if width > 0 and height > 0 and cell_area is not None:

            if widget.get_direction() == gtk.TEXT_DIR_RTL:
                xoffset = 1.0 - self.props.xalign
            else:
                xoffset = self.props.xalign

            xoffset = max(xoffset * (cell_area.width - width), 0)
            yoffset = max(self.props.yalign * (cell_area.height - height), 0)

        return xoffset, yoffset, width, height

    def on_activate(self, event, widget, path, background_area, cell_area,
                    flags):
        pass

    def on_start_editing(self, event, widget, path, background_area, cell_area,
                         flags):
        pass

    def _is_prelit(self, tree_view):
        x, y = tree_view.get_pointer()
        x, y = tree_view.convert_widget_to_bin_window_coords(x, y)
        pos = tree_view.get_path_at_pos(x, y)
        if pos is None:
            return False

        path_, column, x, y = pos

        for cell_renderer in column.get_cell_renderers():
            if cell_renderer == self:
                cell_x, cell_width = column.cell_get_position(cell_renderer)
                if x > cell_x and x < (cell_x + cell_width):
                    return True
                return False

        return False

    def on_render(self, window, widget, background_area, cell_area,
            expose_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()
            prelit_fill_color = None
            prelit_stroke_color = None
        else:
            stroke_color = self._stroke_color
            fill_color = self._fill_color
            prelit_fill_color = self._prelit_fill_color
            prelit_stroke_color = self._prelit_stroke_color

        has_prelit_colors = None not in [prelit_fill_color,
                                         prelit_stroke_color]

        if flags & gtk.CELL_RENDERER_PRELIT and has_prelit_colors and \
                self._is_prelit(widget):

            self._buffer.fill_color = prelit_fill_color
            self._buffer.stroke_color = prelit_stroke_color
        else:
            self._buffer.fill_color = fill_color
            self._buffer.stroke_color = stroke_color

        surface = self._buffer.get_surface()
        if surface is None:
            return

        xoffset, yoffset, width_, height_ = self.on_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.paint()
Exemple #2
0
class CellRendererIcon(gtk.GenericCellRenderer):

    __gtype_name__ = 'SugarCellRendererIcon'

    __gsignals__ = {
        'clicked': (gobject.SIGNAL_RUN_FIRST, gobject.TYPE_NONE, [object]),
    }

    def __init__(self, tree_view):
        from sugar.graphics.palette import CellRendererInvoker

        self._buffer = _IconBuffer()
        self._buffer.cache = True
        self._xo_color = None
        self._fill_color = None
        self._stroke_color = None
        self._prelit_fill_color = None
        self._prelit_stroke_color = None
        self._palette_invoker = CellRendererInvoker()

        gobject.GObject.__init__(self)

        self._palette_invoker.attach_cell_renderer(tree_view, self)

        self.connect('destroy', self.__destroy_cb)

    def __destroy_cb(self, icon):
        self._palette_invoker.detach()

    def create_palette(self):
        return None

    def get_palette_invoker(self):
        return self._palette_invoker

    palette_invoker = gobject.property(type=object, getter=get_palette_invoker)

    def set_file_name(self, value):
        if self._buffer.file_name != value:
            self._buffer.file_name = value

    file_name = gobject.property(type=str, setter=set_file_name)

    def set_icon_name(self, value):
        if self._buffer.icon_name != value:
            self._buffer.icon_name = value

    icon_name = gobject.property(type=str, setter=set_icon_name)

    def get_xo_color(self):
        return self._xo_color

    def set_xo_color(self, value):
        self._xo_color = value

    xo_color = gobject.property(type=object,
            getter=get_xo_color, setter=set_xo_color)

    def set_fill_color(self, value):
        if self._fill_color != value:
            self._fill_color = value

    fill_color = gobject.property(type=object, setter=set_fill_color)

    def set_stroke_color(self, value):
        if self._stroke_color != value:
            self._stroke_color = value

    stroke_color = gobject.property(type=object, setter=set_stroke_color)

    def set_prelit_fill_color(self, value):
        if self._prelit_fill_color != value:
            self._prelit_fill_color = value

    prelit_fill_color = gobject.property(type=object,
                                         setter=set_prelit_fill_color)

    def set_prelit_stroke_color(self, value):
        if self._prelit_stroke_color != value:
            self._prelit_stroke_color = value

    prelit_stroke_color = gobject.property(type=object,
                                           setter=set_prelit_stroke_color)

    def set_background_color(self, value):
        if self._buffer.background_color != value:
            self._buffer.background_color = value

    background_color = gobject.property(type=object,
                                        setter=set_background_color)

    def set_size(self, value):
        if self._buffer.width != value:
            self._buffer.width = value
            self._buffer.height = value

    size = gobject.property(type=object, setter=set_size)

    def on_get_size(self, widget, cell_area):
        width = self._buffer.width + self.props.xpad * 2
        height = self._buffer.height + self.props.ypad * 2
        xoffset = 0
        yoffset = 0

        if width > 0 and height > 0 and cell_area is not None:

            if widget.get_direction() == gtk.TEXT_DIR_RTL:
                xoffset = 1.0 - self.props.xalign
            else:
                xoffset = self.props.xalign

            xoffset = max(xoffset * (cell_area.width - width), 0)
            yoffset = max(self.props.yalign * (cell_area.height - height), 0)

        return xoffset, yoffset, width, height

    def on_activate(self, event, widget, path, background_area, cell_area,
                    flags):
        pass

    def on_start_editing(self, event, widget, path, background_area, cell_area,
                         flags):
        pass

    def _is_prelit(self, tree_view):
        x, y = tree_view.get_pointer()
        x, y = tree_view.convert_widget_to_bin_window_coords(x, y)
        pos = tree_view.get_path_at_pos(x, y)
        if pos is None:
            return False

        path_, column, x, y = pos

        for cell_renderer in column.get_cell_renderers():
            if cell_renderer == self:
                cell_x, cell_width = column.cell_get_position(cell_renderer)
                if x > cell_x and x < (cell_x + cell_width):
                    return True
                return False

        return False

    def on_render(self, window, widget, background_area, cell_area,
            expose_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()
            prelit_fill_color = None
            prelit_stroke_color = None
        else:
            stroke_color = self._stroke_color
            fill_color = self._fill_color
            prelit_fill_color = self._prelit_fill_color
            prelit_stroke_color = self._prelit_stroke_color

        has_prelit_colors = None not in [prelit_fill_color,
                                         prelit_stroke_color]

        if flags & gtk.CELL_RENDERER_PRELIT and has_prelit_colors and \
                self._is_prelit(widget):

            self._buffer.fill_color = prelit_fill_color
            self._buffer.stroke_color = prelit_stroke_color
        else:
            self._buffer.fill_color = fill_color
            self._buffer.stroke_color = stroke_color

        surface = self._buffer.get_surface()
        if surface is None:
            return

        xoffset, yoffset, width_, height_ = self.on_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.paint()