cell_renderer_image.py 4.53 KB
Newer Older
1 2
##	cell_renderer_image.py
##
3
## Contributors for this file:
Yann Leboulanger's avatar
Yann Leboulanger committed
4
## - Yann Leboulanger <asterix@lagaule.org>
5 6
## - Nikos Kouremenos <kourem@gmail.com>
##
Yann Leboulanger's avatar
Yann Leboulanger committed
7
## Copyright (C) 2003-2004 Yann Leboulanger <asterix@lagaule.org>
8
##                         Vincent Hanquez <tab@snarc.org>
Yann Leboulanger's avatar
Yann Leboulanger committed
9
## Copyright (C) 2005 Yann Leboulanger <asterix@lagaule.org>
10
##                    Vincent Hanquez <tab@snarc.org>
11
##                    Nikos Kouremenos <kourem@gmail.com>
12 13 14
##                    Dimitur Kirov <dkirov@gmail.com>
##                    Travis Shirk <travis@pobox.com>
##                    Norman Rasmussen <norman@rasmussen.co.za>
15
##
16 17 18
## This file is part of Gajim.
##
## Gajim is free software; you can redistribute it and/or modify
19
## it under the terms of the GNU General Public License as published
20
## by the Free Software Foundation; version 3 only.
21
##
22
## Gajim is distributed in the hope that it will be useful,
23 24 25 26
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
## GNU General Public License for more details.
##
27 28 29
## You should have received a copy of the GNU General Public License
## along with Gajim.  If not, see <http://www.gnu.org/licenses/>.
##
30 31 32 33 34 35 36 37

import gtk
import gobject

class CellRendererImage(gtk.GenericCellRenderer):

	__gproperties__ = {
		'image': (gobject.TYPE_OBJECT, 'Image', 
Vincent Hanquez's avatar
Vincent Hanquez committed
38
			'Image', gobject.PARAM_READWRITE),
39 40
	}

41
	def __init__(self, col_index, tv_index):
42 43
		self.__gobject_init__()
		self.image = None
44 45
		self.col_index = col_index
		self.tv_index = tv_index
46
		self.iters = {}
47 48 49 50 51 52 53 54

	def do_set_property(self, pspec, value):
		setattr(self, pspec.name, value)

	def do_get_property(self, pspec):
		return getattr(self, pspec.name)

	def func(self, model, path, iter, (image, tree)):
55
		if model.get_value(iter, self.tv_index) != image:
Vincent Hanquez's avatar
Vincent Hanquez committed
56 57
			return
		self.redraw = 1
58 59 60
		col = tree.get_column(self.col_index)
		cell_area = tree.get_cell_area(path, col)
		
Vincent Hanquez's avatar
Vincent Hanquez committed
61 62
		tree.queue_draw_area(cell_area.x, cell_area.y,
					cell_area.width, cell_area.height)
63 64

	def animation_timeout(self, tree, image):
Vincent Hanquez's avatar
Vincent Hanquez committed
65 66 67
		if image.get_storage_type() != gtk.IMAGE_ANIMATION:
			return
		self.redraw = 0
68 69
		iter = self.iters[image]
		iter.advance()
Vincent Hanquez's avatar
Vincent Hanquez committed
70
		model = tree.get_model()
71 72
		if model:
			model.foreach(self.func, (image, tree))
Vincent Hanquez's avatar
Vincent Hanquez committed
73
		if self.redraw:
74
			gobject.timeout_add(iter.get_delay_time(),
75
					self.animation_timeout, tree, image)
76 77
		elif image in self.iters:
			del self.iters[image]
78
				
Vincent Hanquez's avatar
Vincent Hanquez committed
79 80
	def on_render(self, window, widget, background_area, cell_area,
					expose_area, flags):
81 82 83 84
		if not self.image:
			return
		pix_rect = gtk.gdk.Rectangle()
		pix_rect.x, pix_rect.y, pix_rect.width, pix_rect.height = \
85
			self.on_get_size(widget, cell_area)
86 87 88

		pix_rect.x += cell_area.x
		pix_rect.y += cell_area.y
Vincent Hanquez's avatar
Vincent Hanquez committed
89
		pix_rect.width -= 2 * self.get_property('xpad')
90 91 92 93 94 95
		pix_rect.height -= 2 * self.get_property('ypad')

		draw_rect = cell_area.intersect(pix_rect)
		draw_rect = expose_area.intersect(draw_rect)

		if self.image.get_storage_type() == gtk.IMAGE_ANIMATION:
96
			if self.image not in self.iters:
97 98
				if not isinstance(widget, gtk.TreeView):
					return
99
				animation = self.image.get_animation()
100 101 102 103
				iter =  animation.get_iter()
				self.iters[self.image] = iter
				gobject.timeout_add(iter.get_delay_time(),
					self.animation_timeout, widget, self.image)
104

105
			pix = self.iters[self.image].get_pixbuf()
106 107 108 109
		elif self.image.get_storage_type() == gtk.IMAGE_PIXBUF:
			pix = self.image.get_pixbuf()
		else:
			return
nicfit's avatar
nicfit committed
110 111
		if draw_rect.x < 1:
			return
Vincent Hanquez's avatar
Vincent Hanquez committed
112 113 114 115 116 117
		window.draw_pixbuf(widget.style.black_gc, pix,
					draw_rect.x - pix_rect.x,
					draw_rect.y - pix_rect.y,
					draw_rect.x, draw_rect.y + 2,
					draw_rect.width, draw_rect.height,
					gtk.gdk.RGB_DITHER_NONE, 0, 0)
118 119 120 121 122 123 124 125 126 127 128

	def on_get_size(self, widget, cell_area):
		if not self.image:
			return 0, 0, 0, 0
		if self.image.get_storage_type() == gtk.IMAGE_ANIMATION:
			animation = self.image.get_animation()
			pix = animation.get_iter().get_pixbuf()
		elif self.image.get_storage_type() == gtk.IMAGE_PIXBUF:
			pix = self.image.get_pixbuf()
		else:
			return 0, 0, 0, 0
Vincent Hanquez's avatar
Vincent Hanquez committed
129
		pixbuf_width = pix.get_width()
130
		pixbuf_height = pix.get_height()
Vincent Hanquez's avatar
Vincent Hanquez committed
131
		calc_width = self.get_property('xpad') * 2 + pixbuf_width
132 133 134 135
		calc_height = self.get_property('ypad') * 2 + pixbuf_height
		x_offset = 0
		y_offset = 0
		if cell_area and pixbuf_width > 0 and pixbuf_height > 0:
136 137
			x_offset = self.get_property('xalign') * \
					(cell_area.width - calc_width - \
Vincent Hanquez's avatar
Vincent Hanquez committed
138
					self.get_property('xpad'))
139 140
			y_offset = self.get_property('yalign') * \
					(cell_area.height - calc_height - \
Vincent Hanquez's avatar
Vincent Hanquez committed
141
					self.get_property('ypad'))
142
		return x_offset, y_offset, calc_width, calc_height