root/trunk/src/cell_renderer_image.py

Revision 10518, 4.3 kB (checked in by asterix, 7 weeks ago)

[thorstenp] rename some var to not hide builts-in functions (list -> list_, ...)

  • Property svn:eol-style set to LF
Line 
1# -*- coding:utf-8 -*-
2## src/cell_renderer_image.py
3##
4## Copyright (C) 2003-2008 Yann Leboulanger <asterix AT lagaule.org>
5## Copyright (C) 2005 Vincent Hanquez <tab AT snarc.org>
6## Copyright (C) 2005-2007 Nikos Kouremenos <kourem AT gmail.com>
7## Copyright (C) 2006 Travis Shirk <travis AT pobox.com>
8##
9## This file is part of Gajim.
10##
11## Gajim is free software; you can redistribute it and/or modify
12## it under the terms of the GNU General Public License as published
13## by the Free Software Foundation; version 3 only.
14##
15## Gajim is distributed in the hope that it will be useful,
16## but WITHOUT ANY WARRANTY; without even the implied warranty of
17## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18## GNU General Public License for more details.
19##
20## You should have received a copy of the GNU General Public License
21## along with Gajim. If not, see <http://www.gnu.org/licenses/>.
22##
23
24
25import gtk
26import gobject
27
28class CellRendererImage(gtk.GenericCellRenderer):
29
30        __gproperties__ = {
31                'image': (gobject.TYPE_OBJECT, 'Image', 
32                        'Image', gobject.PARAM_READWRITE),
33        }
34
35        def __init__(self, col_index, tv_index):
36                self.__gobject_init__()
37                self.image = None
38                self.col_index = col_index
39                self.tv_index = tv_index
40                self.iters = {}
41
42        def do_set_property(self, pspec, value):
43                setattr(self, pspec.name, value)
44
45        def do_get_property(self, pspec):
46                return getattr(self, pspec.name)
47
48        def func(self, model, path, iter_, image_tree):
49                image, tree = image_tree       
50                if model.get_value(iter_, self.tv_index) != image:
51                        return
52                self.redraw = 1
53                col = tree.get_column(self.col_index)
54                cell_area = tree.get_cell_area(path, col)
55               
56                tree.queue_draw_area(cell_area.x, cell_area.y,
57                                        cell_area.width, cell_area.height)
58
59        def animation_timeout(self, tree, image):
60                if image.get_storage_type() != gtk.IMAGE_ANIMATION:
61                        return
62                self.redraw = 0
63                iter = self.iters[image]
64                iter.advance()
65                model = tree.get_model()
66                if model:
67                        model.foreach(self.func, (image, tree))
68                if self.redraw:
69                        gobject.timeout_add(iter.get_delay_time(),
70                                        self.animation_timeout, tree, image)
71                elif image in self.iters:
72                        del self.iters[image]
73                               
74        def on_render(self, window, widget, background_area, cell_area,
75                                        expose_area, flags):
76                if not self.image:
77                        return
78                pix_rect = gtk.gdk.Rectangle()
79                pix_rect.x, pix_rect.y, pix_rect.width, pix_rect.height = \
80                        self.on_get_size(widget, cell_area)
81
82                pix_rect.x += cell_area.x
83                pix_rect.y += cell_area.y
84                pix_rect.width -= 2 * self.get_property('xpad')
85                pix_rect.height -= 2 * self.get_property('ypad')
86
87                draw_rect = cell_area.intersect(pix_rect)
88                draw_rect = expose_area.intersect(draw_rect)
89
90                if self.image.get_storage_type() == gtk.IMAGE_ANIMATION:
91                        if self.image not in self.iters:
92                                if not isinstance(widget, gtk.TreeView):
93                                        return
94                                animation = self.image.get_animation()
95                                iter =  animation.get_iter()
96                                self.iters[self.image] = iter
97                                gobject.timeout_add(iter.get_delay_time(),
98                                        self.animation_timeout, widget, self.image)
99
100                        pix = self.iters[self.image].get_pixbuf()
101                elif self.image.get_storage_type() == gtk.IMAGE_PIXBUF:
102                        pix = self.image.get_pixbuf()
103                else:
104                        return
105                if draw_rect.x < 1:
106                        return
107                window.draw_pixbuf(widget.style.black_gc, pix,
108                                        draw_rect.x - pix_rect.x,
109                                        draw_rect.y - pix_rect.y,
110                                        draw_rect.x, draw_rect.y,
111                                        draw_rect.width, draw_rect.height,
112                                        gtk.gdk.RGB_DITHER_NONE, 0, 0)
113
114        def on_get_size(self, widget, cell_area):
115                if not self.image:
116                        return 0, 0, 0, 0
117                if self.image.get_storage_type() == gtk.IMAGE_ANIMATION:
118                        animation = self.image.get_animation()
119                        pix = animation.get_iter().get_pixbuf()
120                elif self.image.get_storage_type() == gtk.IMAGE_PIXBUF:
121                        pix = self.image.get_pixbuf()
122                else:
123                        return 0, 0, 0, 0
124                pixbuf_width = pix.get_width()
125                pixbuf_height = pix.get_height()
126                calc_width = self.get_property('xpad') * 2 + pixbuf_width
127                calc_height = self.get_property('ypad') * 2 + pixbuf_height
128                x_offset = 0
129                y_offset = 0
130                if cell_area and pixbuf_width > 0 and pixbuf_height > 0:
131                        x_offset = self.get_property('xalign') * \
132                                        (cell_area.width - calc_width - \
133                                        self.get_property('xpad'))
134                        y_offset = self.get_property('yalign') * \
135                                        (cell_area.height - calc_height - \
136                                        self.get_property('ypad'))
137                return x_offset, y_offset, calc_width, calc_height
138
139# vim: se ts=3:
Note: See TracBrowser for help on using the browser.