| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 14 | |
|---|
| 15 | |
|---|
| 16 | |
|---|
| 17 | |
|---|
| 18 | |
|---|
| 19 | |
|---|
| 20 | |
|---|
| 21 | |
|---|
| 22 | |
|---|
| 23 | |
|---|
| 24 | |
|---|
| 25 | |
|---|
| 26 | import gtk |
|---|
| 27 | import gobject |
|---|
| 28 | |
|---|
| 29 | class CellRendererImage(gtk.GenericCellRenderer): |
|---|
| 30 | |
|---|
| 31 | __gproperties__ = { |
|---|
| 32 | 'image': (gobject.TYPE_OBJECT, 'Image', |
|---|
| 33 | 'Image', gobject.PARAM_READWRITE), |
|---|
| 34 | } |
|---|
| 35 | |
|---|
| 36 | def __init__(self, col_index, tv_index): |
|---|
| 37 | self.__gobject_init__() |
|---|
| 38 | self.image = None |
|---|
| 39 | self.col_index = col_index |
|---|
| 40 | self.tv_index = tv_index |
|---|
| 41 | self.iters = {} |
|---|
| 42 | |
|---|
| 43 | def do_set_property(self, pspec, value): |
|---|
| 44 | setattr(self, pspec.name, value) |
|---|
| 45 | |
|---|
| 46 | def do_get_property(self, pspec): |
|---|
| 47 | return getattr(self, pspec.name) |
|---|
| 48 | |
|---|
| 49 | def func(self, model, path, iter, (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 | animation = self.image.get_animation() |
|---|
| 93 | iter = animation.get_iter() |
|---|
| 94 | self.iters[self.image] = iter |
|---|
| 95 | gobject.timeout_add(iter.get_delay_time(), |
|---|
| 96 | self.animation_timeout, widget, self.image) |
|---|
| 97 | |
|---|
| 98 | pix = self.iters[self.image].get_pixbuf() |
|---|
| 99 | elif self.image.get_storage_type() == gtk.IMAGE_PIXBUF: |
|---|
| 100 | pix = self.image.get_pixbuf() |
|---|
| 101 | else: |
|---|
| 102 | return |
|---|
| 103 | if draw_rect.x < 1: |
|---|
| 104 | return |
|---|
| 105 | window.draw_pixbuf(widget.style.black_gc, pix, |
|---|
| 106 | draw_rect.x - pix_rect.x, |
|---|
| 107 | draw_rect.y - pix_rect.y, |
|---|
| 108 | draw_rect.x, draw_rect.y + 2, |
|---|
| 109 | draw_rect.width, draw_rect.height, |
|---|
| 110 | gtk.gdk.RGB_DITHER_NONE, 0, 0) |
|---|
| 111 | |
|---|
| 112 | def on_get_size(self, widget, cell_area): |
|---|
| 113 | if not self.image: |
|---|
| 114 | return 0, 0, 0, 0 |
|---|
| 115 | if self.image.get_storage_type() == gtk.IMAGE_ANIMATION: |
|---|
| 116 | animation = self.image.get_animation() |
|---|
| 117 | pix = animation.get_iter().get_pixbuf() |
|---|
| 118 | elif self.image.get_storage_type() == gtk.IMAGE_PIXBUF: |
|---|
| 119 | pix = self.image.get_pixbuf() |
|---|
| 120 | else: |
|---|
| 121 | return 0, 0, 0, 0 |
|---|
| 122 | pixbuf_width = pix.get_width() |
|---|
| 123 | pixbuf_height = pix.get_height() |
|---|
| 124 | calc_width = self.get_property('xpad') * 2 + pixbuf_width |
|---|
| 125 | calc_height = self.get_property('ypad') * 2 + pixbuf_height |
|---|
| 126 | x_offset = 0 |
|---|
| 127 | y_offset = 0 |
|---|
| 128 | if cell_area and pixbuf_width > 0 and pixbuf_height > 0: |
|---|
| 129 | x_offset = self.get_property('xalign') * \ |
|---|
| 130 | (cell_area.width - calc_width - \ |
|---|
| 131 | self.get_property('xpad')) |
|---|
| 132 | y_offset = self.get_property('yalign') * \ |
|---|
| 133 | (cell_area.height - calc_height - \ |
|---|
| 134 | self.get_property('ypad')) |
|---|
| 135 | return x_offset, y_offset, calc_width, calc_height |
|---|
| 136 | |
|---|
| 137 | if gtk.pygtk_version < (2, 8, 0): |
|---|
| 138 | gobject.type_register(CellRendererImage) |
|---|