2007-06-24 14:43:48 +02:00
|
|
|
# Copyright (C) 2006-2007 Red Hat, Inc.
|
2007-04-30 14:04:50 +02:00
|
|
|
#
|
|
|
|
# This library is free software; you can redistribute it and/or
|
|
|
|
# modify it under the terms of the GNU Lesser General Public
|
|
|
|
# License as published by the Free Software Foundation; either
|
|
|
|
# version 2 of the License, or (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This library is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
# Lesser General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU Lesser General Public
|
|
|
|
# License along with this library; if not, write to the
|
|
|
|
# Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
|
|
# Boston, MA 02111-1307, USA.
|
|
|
|
|
2007-08-25 13:15:28 +02:00
|
|
|
import re
|
2007-08-29 11:34:56 +02:00
|
|
|
import math
|
2007-08-25 13:15:28 +02:00
|
|
|
import logging
|
|
|
|
|
2007-08-02 13:57:41 +02:00
|
|
|
import gobject
|
2007-04-30 14:04:50 +02:00
|
|
|
import gtk
|
2007-08-25 13:15:28 +02:00
|
|
|
import hippo
|
|
|
|
import cairo
|
2007-08-02 13:57:41 +02:00
|
|
|
|
2007-08-25 13:15:28 +02:00
|
|
|
from sugar.graphics.xocolor import XoColor
|
2007-09-08 12:10:35 +02:00
|
|
|
from sugar.util import LRU
|
2007-04-30 14:04:50 +02:00
|
|
|
|
2007-08-26 00:26:11 +02:00
|
|
|
_BADGE_SIZE = 0.45
|
|
|
|
|
2007-08-25 18:26:59 +02:00
|
|
|
class _SVGLoader(object):
|
2007-08-26 12:08:50 +02:00
|
|
|
def __init__(self):
|
2007-09-08 12:10:35 +02:00
|
|
|
self._cache = LRU(50)
|
|
|
|
|
|
|
|
def load(self, file_name, entities, cache):
|
|
|
|
if file_name in self._cache:
|
|
|
|
icon = self._cache[file_name]
|
|
|
|
else:
|
|
|
|
icon_file = open(file_name, 'r')
|
|
|
|
icon = icon_file.read()
|
|
|
|
icon_file.close()
|
2007-08-26 12:08:50 +02:00
|
|
|
|
2007-09-08 12:10:35 +02:00
|
|
|
if cache:
|
|
|
|
self._cache[file_name] = icon
|
2007-08-25 18:26:59 +02:00
|
|
|
|
|
|
|
for entity, value in entities.items():
|
2007-09-10 02:06:24 +02:00
|
|
|
if isinstance(value, basestring):
|
|
|
|
xml = '<!ENTITY %s "%s">' % (entity, value)
|
|
|
|
icon = re.sub('<!ENTITY %s .*>' % entity, xml, icon)
|
|
|
|
else:
|
|
|
|
logging.error(
|
|
|
|
'Icon %s, entity %s is invalid.', file_name, entity)
|
2007-09-08 12:10:35 +02:00
|
|
|
|
2008-01-09 15:35:23 +01:00
|
|
|
import rsvg # XXX this is very slow! why?
|
2007-09-08 12:10:35 +02:00
|
|
|
return rsvg.Handle(data=icon)
|
2007-08-25 18:26:59 +02:00
|
|
|
|
2007-08-26 01:57:48 +02:00
|
|
|
class _IconInfo(object):
|
2007-08-25 18:26:59 +02:00
|
|
|
def __init__(self):
|
2007-08-26 01:57:48 +02:00
|
|
|
self.file_name = None
|
|
|
|
self.attach_x = 0
|
|
|
|
self.attach_y = 0
|
2007-08-25 18:26:59 +02:00
|
|
|
|
2007-08-26 02:59:05 +02:00
|
|
|
class _BadgeInfo(object):
|
|
|
|
def __init__(self):
|
|
|
|
self.attach_x = 0
|
|
|
|
self.attach_y = 0
|
|
|
|
self.size = 0
|
|
|
|
self.icon_padding = 0
|
|
|
|
|
2007-08-26 01:57:48 +02:00
|
|
|
class _IconBuffer(object):
|
2007-09-08 12:10:35 +02:00
|
|
|
_surface_cache = LRU(50)
|
|
|
|
_loader = _SVGLoader()
|
2007-08-25 18:26:59 +02:00
|
|
|
|
2007-09-08 12:10:35 +02:00
|
|
|
def __init__(self):
|
2007-08-25 18:26:59 +02:00
|
|
|
self.icon_name = None
|
2008-04-19 00:37:19 +02:00
|
|
|
self.icon_size = None
|
2007-08-25 18:26:59 +02:00
|
|
|
self.file_name = None
|
|
|
|
self.fill_color = None
|
|
|
|
self.stroke_color = None
|
2007-08-26 00:26:11 +02:00
|
|
|
self.badge_name = None
|
2007-08-25 18:26:59 +02:00
|
|
|
self.width = None
|
|
|
|
self.height = None
|
2007-09-08 12:10:35 +02:00
|
|
|
self.cache = False
|
2007-11-03 16:10:56 +01:00
|
|
|
self.scale = 1.0
|
2007-08-25 18:26:59 +02:00
|
|
|
|
2007-12-14 19:59:46 +01:00
|
|
|
def _get_cache_key(self, sensitive):
|
2007-08-26 12:36:34 +02:00
|
|
|
return (self.icon_name, self.file_name, self.fill_color,
|
2007-12-14 19:59:46 +01:00
|
|
|
self.stroke_color, self.badge_name, self.width, self.height,
|
|
|
|
sensitive)
|
2007-08-26 12:36:34 +02:00
|
|
|
|
2007-08-25 18:26:59 +02:00
|
|
|
def _load_svg(self, file_name):
|
|
|
|
entities = {}
|
|
|
|
if self.fill_color:
|
|
|
|
entities['fill_color'] = self.fill_color
|
|
|
|
if self.stroke_color:
|
|
|
|
entities['stroke_color'] = self.stroke_color
|
|
|
|
|
2007-09-08 12:10:35 +02:00
|
|
|
return self._loader.load(file_name, entities, self.cache)
|
2007-08-25 18:26:59 +02:00
|
|
|
|
2007-08-26 01:57:48 +02:00
|
|
|
def _get_attach_points(self, info, size_request):
|
|
|
|
attach_points = info.get_attach_points()
|
2007-08-25 18:26:59 +02:00
|
|
|
|
2007-08-26 01:57:48 +02:00
|
|
|
if attach_points:
|
|
|
|
attach_x = float(attach_points[0][0]) / size_request
|
|
|
|
attach_y = float(attach_points[0][1]) / size_request
|
|
|
|
else:
|
|
|
|
attach_x = attach_y = 0
|
2007-08-26 00:26:11 +02:00
|
|
|
|
2007-08-26 01:57:48 +02:00
|
|
|
return attach_x, attach_y
|
2007-08-26 00:26:11 +02:00
|
|
|
|
2007-08-26 01:57:48 +02:00
|
|
|
def _get_icon_info(self):
|
|
|
|
icon_info = _IconInfo()
|
2007-08-26 00:26:11 +02:00
|
|
|
|
2007-08-26 01:57:48 +02:00
|
|
|
if self.file_name:
|
|
|
|
icon_info.file_name = self.file_name
|
|
|
|
elif self.icon_name:
|
|
|
|
theme = gtk.icon_theme_get_default()
|
2007-08-26 00:26:11 +02:00
|
|
|
|
2007-08-26 01:57:48 +02:00
|
|
|
size = 50
|
|
|
|
if self.width != None:
|
|
|
|
size = self.width
|
2007-08-26 00:26:11 +02:00
|
|
|
|
2007-08-26 01:57:48 +02:00
|
|
|
info = theme.lookup_icon(self.icon_name, size, 0)
|
|
|
|
if info:
|
|
|
|
attach_x, attach_y = self._get_attach_points(info, size)
|
2007-08-26 00:26:11 +02:00
|
|
|
|
2007-08-26 01:57:48 +02:00
|
|
|
icon_info.file_name = info.get_filename()
|
|
|
|
icon_info.attach_x = attach_x
|
|
|
|
icon_info.attach_y = attach_y
|
2007-10-23 17:39:31 +02:00
|
|
|
|
|
|
|
del info
|
2007-08-26 19:00:08 +02:00
|
|
|
else:
|
2007-09-08 12:10:35 +02:00
|
|
|
logging.warning('No icon with the name %s '
|
|
|
|
'was found in the theme.' % self.icon_name)
|
2007-08-26 00:26:11 +02:00
|
|
|
|
2007-08-26 01:57:48 +02:00
|
|
|
return icon_info
|
2007-08-26 00:26:11 +02:00
|
|
|
|
2007-12-14 19:59:46 +01:00
|
|
|
def _draw_badge(self, context, size, sensitive, widget):
|
2007-08-26 02:24:45 +02:00
|
|
|
theme = gtk.icon_theme_get_default()
|
|
|
|
badge_info = theme.lookup_icon(self.badge_name, size, 0)
|
|
|
|
if badge_info:
|
|
|
|
badge_file_name = badge_info.get_filename()
|
|
|
|
if badge_file_name.endswith('.svg'):
|
2007-09-08 12:10:35 +02:00
|
|
|
handle = self._loader.load(badge_file_name, {}, self.cache)
|
2008-04-03 17:31:55 +02:00
|
|
|
|
|
|
|
dimensions = handle.get_dimension_data()
|
|
|
|
icon_width = int(dimensions[0])
|
|
|
|
icon_height = int(dimensions[1])
|
|
|
|
|
2007-12-14 19:59:46 +01:00
|
|
|
pixbuf = handle.get_pixbuf()
|
2007-08-26 02:24:45 +02:00
|
|
|
else:
|
|
|
|
pixbuf = gtk.gdk.pixbuf_new_from_file(badge_file_name)
|
2007-12-14 19:59:46 +01:00
|
|
|
|
2008-04-03 17:31:55 +02:00
|
|
|
icon_width = pixbuf.get_width()
|
|
|
|
icon_height = pixbuf.get_height()
|
|
|
|
|
|
|
|
context.scale(float(size) / icon_width,
|
|
|
|
float(size) / icon_height)
|
|
|
|
|
2007-12-14 19:59:46 +01:00
|
|
|
if not sensitive:
|
|
|
|
pixbuf = self._get_insensitive_pixbuf(pixbuf, widget)
|
|
|
|
surface = hippo.cairo_surface_from_gdk_pixbuf(pixbuf)
|
|
|
|
context.set_source_surface(surface, 0, 0)
|
|
|
|
context.paint()
|
2007-08-26 02:24:45 +02:00
|
|
|
|
2007-09-02 23:51:01 +02:00
|
|
|
def _get_size(self, icon_width, icon_height, padding):
|
2007-08-26 02:24:45 +02:00
|
|
|
if self.width is not None and self.height is not None:
|
2007-09-02 23:51:01 +02:00
|
|
|
width = self.width + padding
|
|
|
|
height = self.height + padding
|
2007-08-26 02:24:45 +02:00
|
|
|
else:
|
2007-09-02 23:51:01 +02:00
|
|
|
width = icon_width + padding
|
|
|
|
height = icon_height + padding
|
2007-08-26 02:24:45 +02:00
|
|
|
|
|
|
|
return width, height
|
|
|
|
|
2007-08-26 02:59:05 +02:00
|
|
|
def _get_badge_info(self, icon_info, icon_width, icon_height):
|
|
|
|
info = _BadgeInfo()
|
|
|
|
if self.badge_name is None:
|
|
|
|
return info
|
|
|
|
|
|
|
|
info.size = int(_BADGE_SIZE * icon_width)
|
2007-08-26 15:01:16 +02:00
|
|
|
info.attach_x = int(icon_info.attach_x * icon_width - info.size / 2)
|
|
|
|
info.attach_y = int(icon_info.attach_y * icon_height - info.size / 2)
|
2007-08-26 02:59:05 +02:00
|
|
|
|
|
|
|
if info.attach_x < 0 or info.attach_y < 0:
|
|
|
|
info.icon_padding = max(-info.attach_x, -info.attach_y)
|
|
|
|
elif info.attach_x + info.size > icon_width or \
|
|
|
|
info.attach_y + info.size > icon_height:
|
2007-08-26 15:01:16 +02:00
|
|
|
x_padding = info.attach_x + info.size - icon_width
|
|
|
|
y_padding = info.attach_y + info.size - icon_height
|
2007-08-26 02:59:05 +02:00
|
|
|
info.icon_padding = max(x_padding, y_padding)
|
|
|
|
|
|
|
|
return info
|
|
|
|
|
2007-09-10 02:54:47 +02:00
|
|
|
def _get_xo_color(self):
|
2007-09-10 17:55:35 +02:00
|
|
|
if self.stroke_color and self.fill_color:
|
|
|
|
return XoColor('%s,%s' % (self.stroke_color, self.fill_color))
|
|
|
|
else:
|
|
|
|
return None
|
2007-09-10 02:54:47 +02:00
|
|
|
|
|
|
|
def _set_xo_color(self, xo_color):
|
2007-09-10 02:18:29 +02:00
|
|
|
if xo_color:
|
|
|
|
self.stroke_color = xo_color.get_stroke_color()
|
|
|
|
self.fill_color = xo_color.get_fill_color()
|
|
|
|
else:
|
|
|
|
self.stroke_color = None
|
|
|
|
self.fill_color = None
|
|
|
|
|
2007-12-14 19:59:46 +01:00
|
|
|
def _get_insensitive_pixbuf (self, pixbuf, widget):
|
|
|
|
if not (widget and widget.style):
|
|
|
|
return pixbuf
|
|
|
|
|
|
|
|
icon_source = gtk.IconSource()
|
|
|
|
# Special size meaning "don't touch"
|
|
|
|
icon_source.set_size(-1)
|
|
|
|
icon_source.set_pixbuf(pixbuf)
|
|
|
|
icon_source.set_state(gtk.STATE_INSENSITIVE)
|
|
|
|
icon_source.set_direction_wildcarded(False)
|
|
|
|
icon_source.set_size_wildcarded(False)
|
|
|
|
|
|
|
|
# Please note that the pixbuf returned by this function is leaked
|
|
|
|
# with current stable versions of pygtk. The relevant bug is
|
|
|
|
# http://bugzilla.gnome.org/show_bug.cgi?id=502871
|
|
|
|
# -- 2007-12-14 Benjamin Berg
|
|
|
|
pixbuf = widget.style.render_icon(icon_source, widget.get_direction(),
|
|
|
|
gtk.STATE_INSENSITIVE, -1, widget,
|
|
|
|
"sugar-icon")
|
|
|
|
|
|
|
|
return pixbuf
|
|
|
|
|
|
|
|
def get_surface(self, sensitive=True, widget=None):
|
|
|
|
cache_key = self._get_cache_key(sensitive)
|
2007-09-08 12:10:35 +02:00
|
|
|
if cache_key in self._surface_cache:
|
2007-08-26 12:36:34 +02:00
|
|
|
return self._surface_cache[cache_key]
|
|
|
|
|
2007-08-26 01:57:48 +02:00
|
|
|
icon_info = self._get_icon_info()
|
|
|
|
if icon_info.file_name is None:
|
2007-08-25 18:26:59 +02:00
|
|
|
return None
|
|
|
|
|
2007-08-26 01:57:48 +02:00
|
|
|
is_svg = icon_info.file_name.endswith('.svg')
|
2007-08-25 18:26:59 +02:00
|
|
|
|
2007-08-26 01:57:48 +02:00
|
|
|
if is_svg:
|
|
|
|
handle = self._load_svg(icon_info.file_name)
|
2007-08-25 18:26:59 +02:00
|
|
|
dimensions = handle.get_dimension_data()
|
|
|
|
icon_width = int(dimensions[0])
|
|
|
|
icon_height = int(dimensions[1])
|
2007-08-26 01:57:48 +02:00
|
|
|
else:
|
2007-08-27 14:31:38 +02:00
|
|
|
pixbuf = gtk.gdk.pixbuf_new_from_file(icon_info.file_name)
|
2007-08-27 00:43:56 +02:00
|
|
|
icon_width = pixbuf.get_width()
|
|
|
|
icon_height = pixbuf.get_height()
|
2007-08-25 18:26:59 +02:00
|
|
|
|
2007-08-26 02:59:05 +02:00
|
|
|
badge_info = self._get_badge_info(icon_info, icon_width, icon_height)
|
2007-08-26 01:57:48 +02:00
|
|
|
|
2007-09-02 23:51:01 +02:00
|
|
|
padding = badge_info.icon_padding
|
|
|
|
width, height = self._get_size(icon_width, icon_height, padding)
|
2007-08-26 02:24:45 +02:00
|
|
|
surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, width, height)
|
2007-08-26 01:57:48 +02:00
|
|
|
|
|
|
|
context = cairo.Context(surface)
|
2007-08-26 02:59:05 +02:00
|
|
|
context.scale(float(width) / (icon_width + padding * 2),
|
|
|
|
float(height) / (icon_height + padding * 2))
|
2007-08-26 02:24:45 +02:00
|
|
|
context.save()
|
2007-08-26 01:57:48 +02:00
|
|
|
|
2007-08-26 02:59:05 +02:00
|
|
|
context.translate(padding, padding)
|
2007-08-26 01:57:48 +02:00
|
|
|
if is_svg:
|
2007-12-14 19:59:46 +01:00
|
|
|
if sensitive:
|
|
|
|
handle.render_cairo(context)
|
|
|
|
else:
|
|
|
|
pixbuf = handle.get_pixbuf()
|
|
|
|
pixbuf = self._get_insensitive_pixbuf(pixbuf, widget)
|
|
|
|
|
|
|
|
pixbuf_surface = hippo.cairo_surface_from_gdk_pixbuf(pixbuf)
|
|
|
|
context.set_source_surface(pixbuf_surface, 0, 0)
|
|
|
|
context.paint()
|
2007-08-25 18:26:59 +02:00
|
|
|
else:
|
2007-12-14 19:59:46 +01:00
|
|
|
if not sensitive:
|
|
|
|
pixbuf = self._get_insensitive_pixbuf(pixbuf, widget)
|
2007-08-26 01:57:48 +02:00
|
|
|
pixbuf_surface = hippo.cairo_surface_from_gdk_pixbuf(pixbuf)
|
|
|
|
context.set_source_surface(pixbuf_surface, 0, 0)
|
|
|
|
context.paint()
|
2007-08-25 18:26:59 +02:00
|
|
|
|
2007-08-26 00:26:11 +02:00
|
|
|
if self.badge_name:
|
2007-08-26 02:24:45 +02:00
|
|
|
context.restore()
|
2007-08-26 02:59:05 +02:00
|
|
|
context.translate(badge_info.attach_x, badge_info.attach_y)
|
2007-12-14 19:59:46 +01:00
|
|
|
self._draw_badge(context, badge_info.size, sensitive, widget)
|
2007-08-26 00:26:11 +02:00
|
|
|
|
2007-08-26 12:36:34 +02:00
|
|
|
self._surface_cache[cache_key] = surface
|
2007-08-26 00:26:11 +02:00
|
|
|
|
|
|
|
return surface
|
2007-08-25 18:26:59 +02:00
|
|
|
|
2007-09-10 02:54:47 +02:00
|
|
|
xo_color = property(_get_xo_color, _set_xo_color)
|
|
|
|
|
2007-04-30 14:04:50 +02:00
|
|
|
class Icon(gtk.Image):
|
2007-08-02 13:57:41 +02:00
|
|
|
__gtype_name__ = 'SugarIcon'
|
|
|
|
|
|
|
|
__gproperties__ = {
|
|
|
|
'xo-color' : (object, None, None,
|
|
|
|
gobject.PARAM_WRITABLE),
|
|
|
|
'fill-color' : (object, None, None,
|
|
|
|
gobject.PARAM_READWRITE),
|
|
|
|
'stroke-color' : (object, None, None,
|
2007-08-26 00:26:11 +02:00
|
|
|
gobject.PARAM_READWRITE),
|
|
|
|
'badge-name' : (str, None, None, None,
|
2007-08-02 13:57:41 +02:00
|
|
|
gobject.PARAM_READWRITE)
|
|
|
|
}
|
|
|
|
|
2007-08-25 18:26:59 +02:00
|
|
|
def __init__(self, **kwargs):
|
|
|
|
self._buffer = _IconBuffer()
|
2007-08-17 19:53:41 +02:00
|
|
|
|
2007-08-02 13:57:41 +02:00
|
|
|
gobject.GObject.__init__(self, **kwargs)
|
|
|
|
|
2007-08-25 18:26:59 +02:00
|
|
|
def _sync_image_properties(self):
|
|
|
|
if self._buffer.icon_name != self.props.icon_name:
|
|
|
|
self._buffer.icon_name = self.props.icon_name
|
2007-04-30 14:04:50 +02:00
|
|
|
|
2007-08-25 18:26:59 +02:00
|
|
|
if self._buffer.file_name != self.props.file:
|
|
|
|
self._buffer.file_name = self.props.file
|
2007-08-02 13:57:41 +02:00
|
|
|
|
2008-03-22 17:11:25 +01:00
|
|
|
if self.props.pixel_size == -1:
|
|
|
|
width, height = gtk.icon_size_lookup(self.props.icon_size)
|
|
|
|
else:
|
|
|
|
width = height = self.props.pixel_size
|
2007-11-18 11:58:26 +01:00
|
|
|
if self._buffer.width != width or self._buffer.height != height:
|
2007-08-25 18:26:59 +02:00
|
|
|
self._buffer.width = width
|
|
|
|
self._buffer.height = height
|
2007-08-02 13:57:41 +02:00
|
|
|
|
2007-08-25 18:26:59 +02:00
|
|
|
def _icon_size_changed_cb(self, image, pspec):
|
|
|
|
self._buffer.icon_size = self.props.icon_size
|
2007-08-02 13:57:41 +02:00
|
|
|
|
2007-08-25 18:26:59 +02:00
|
|
|
def _icon_name_changed_cb(self, image, pspec):
|
|
|
|
self._buffer.icon_name = self.props.icon_name
|
2007-04-30 14:04:50 +02:00
|
|
|
|
2007-08-25 18:26:59 +02:00
|
|
|
def _file_changed_cb(self, image, pspec):
|
|
|
|
self._buffer.file_name = self.props.file
|
2007-08-24 20:15:30 +02:00
|
|
|
|
2007-08-29 11:34:56 +02:00
|
|
|
def do_size_request(self, requisition):
|
2007-08-25 18:26:59 +02:00
|
|
|
self._sync_image_properties()
|
|
|
|
surface = self._buffer.get_surface()
|
2007-08-29 11:34:56 +02:00
|
|
|
if surface:
|
|
|
|
requisition[0] = surface.get_width()
|
|
|
|
requisition[1] = surface.get_height()
|
|
|
|
elif self._buffer.width and self._buffer.height:
|
|
|
|
requisition[0] = self._buffer.width
|
|
|
|
requisition[1] = self._buffer.width
|
|
|
|
else:
|
|
|
|
requisition[0] = requisition[1] = 0
|
2007-08-02 13:57:41 +02:00
|
|
|
|
2007-08-29 11:34:56 +02:00
|
|
|
def do_expose_event(self, event):
|
|
|
|
self._sync_image_properties()
|
2007-12-14 19:59:46 +01:00
|
|
|
sensitive = (self.state != gtk.STATE_INSENSITIVE)
|
|
|
|
surface = self._buffer.get_surface(sensitive, self)
|
2007-08-29 11:34:56 +02:00
|
|
|
if surface is None:
|
|
|
|
return
|
|
|
|
|
|
|
|
xpad, ypad = self.get_padding()
|
|
|
|
xalign, yalign = self.get_alignment()
|
|
|
|
requisition = self.get_child_requisition()
|
2007-11-18 11:58:26 +01:00
|
|
|
if self.get_direction() != gtk.TEXT_DIR_LTR:
|
2007-08-29 11:34:56 +02:00
|
|
|
xalign = 1.0 - xalign
|
|
|
|
|
2008-04-19 00:37:19 +02:00
|
|
|
allocation = self.get_allocation()
|
|
|
|
x = math.floor(allocation.x + xpad +
|
|
|
|
(allocation.width - requisition[0]) * xalign)
|
|
|
|
y = math.floor(allocation.y + ypad +
|
|
|
|
(allocation.height - requisition[1]) * yalign)
|
2007-08-29 11:34:56 +02:00
|
|
|
|
|
|
|
cr = self.window.cairo_create()
|
|
|
|
cr.set_source_surface(surface, x, y)
|
|
|
|
cr.paint()
|
2007-08-02 13:57:41 +02:00
|
|
|
|
|
|
|
def do_set_property(self, pspec, value):
|
|
|
|
if pspec.name == 'xo-color':
|
2007-09-10 02:54:47 +02:00
|
|
|
if self._buffer.xo_color != value:
|
|
|
|
self._buffer.xo_color = value
|
|
|
|
self.queue_draw()
|
2007-08-02 13:57:41 +02:00
|
|
|
elif pspec.name == 'fill-color':
|
2007-09-10 02:54:47 +02:00
|
|
|
if self._buffer.fill_color != value:
|
|
|
|
self._buffer.fill_color = value
|
|
|
|
self.queue_draw()
|
2007-08-02 13:57:41 +02:00
|
|
|
elif pspec.name == 'stroke-color':
|
2007-09-10 02:54:47 +02:00
|
|
|
if self._buffer.stroke_color != value:
|
|
|
|
self._buffer.stroke_color = value
|
|
|
|
self.queue_draw()
|
2007-08-26 00:26:11 +02:00
|
|
|
elif pspec.name == 'badge-name':
|
2007-09-10 02:54:47 +02:00
|
|
|
if self._buffer.badge_name != value:
|
|
|
|
self._buffer.badge_name = value
|
|
|
|
self.queue_resize()
|
2007-08-17 19:53:41 +02:00
|
|
|
else:
|
|
|
|
gtk.Image.do_set_property(self, pspec, value)
|
2007-08-02 13:57:41 +02:00
|
|
|
|
|
|
|
def do_get_property(self, pspec):
|
|
|
|
if pspec.name == 'fill-color':
|
2007-08-25 18:26:59 +02:00
|
|
|
return self._buffer.fill_color
|
2007-08-02 13:57:41 +02:00
|
|
|
elif pspec.name == 'stroke-color':
|
2007-08-25 18:26:59 +02:00
|
|
|
return self._buffer.stroke_color
|
2007-08-26 00:26:11 +02:00
|
|
|
elif pspec.name == 'badge-name':
|
|
|
|
return self._buffer.badge_name
|
2007-08-17 19:53:41 +02:00
|
|
|
else:
|
|
|
|
return gtk.Image.do_get_property(self, pspec)
|
2007-08-25 13:15:28 +02:00
|
|
|
|
|
|
|
class CanvasIcon(hippo.CanvasBox, hippo.CanvasItem):
|
|
|
|
__gtype_name__ = 'CanvasIcon'
|
|
|
|
|
|
|
|
__gproperties__ = {
|
2007-08-26 14:16:09 +02:00
|
|
|
'file-name' : (str, None, None, None,
|
|
|
|
gobject.PARAM_READWRITE),
|
2007-08-25 13:15:28 +02:00
|
|
|
'icon-name' : (str, None, None, None,
|
|
|
|
gobject.PARAM_READWRITE),
|
|
|
|
'xo-color' : (object, None, None,
|
|
|
|
gobject.PARAM_WRITABLE),
|
|
|
|
'fill-color' : (object, None, None,
|
|
|
|
gobject.PARAM_READWRITE),
|
|
|
|
'stroke-color' : (object, None, None,
|
|
|
|
gobject.PARAM_READWRITE),
|
|
|
|
'size' : (int, None, None, 0, 1024, 0,
|
|
|
|
gobject.PARAM_READWRITE),
|
2007-08-26 14:16:09 +02:00
|
|
|
'scale' : (float, None, None, -1024.0, 1024.0, 1.0,
|
2007-08-25 13:15:28 +02:00
|
|
|
gobject.PARAM_READWRITE),
|
2007-09-08 12:10:35 +02:00
|
|
|
'cache' : (bool, None, None, False,
|
2007-08-25 13:15:28 +02:00
|
|
|
gobject.PARAM_READWRITE),
|
|
|
|
'badge-name' : (str, None, None, None,
|
|
|
|
gobject.PARAM_READWRITE)
|
|
|
|
}
|
|
|
|
|
|
|
|
def __init__(self, **kwargs):
|
2007-08-26 14:16:09 +02:00
|
|
|
self._buffer = _IconBuffer()
|
2007-08-25 13:15:28 +02:00
|
|
|
|
|
|
|
hippo.CanvasBox.__init__(self, **kwargs)
|
2007-08-26 14:16:09 +02:00
|
|
|
|
|
|
|
self._palette = None
|
2008-01-09 21:21:06 +01:00
|
|
|
self.connect('destroy', self.__destroy_cb)
|
|
|
|
|
|
|
|
def __destroy_cb(self, icon):
|
|
|
|
if self._palette is not None:
|
|
|
|
self._palette.destroy()
|
2007-08-25 13:15:28 +02:00
|
|
|
|
|
|
|
def do_set_property(self, pspec, value):
|
2007-08-26 14:16:09 +02:00
|
|
|
if pspec.name == 'file-name':
|
2007-09-10 02:54:47 +02:00
|
|
|
if self._buffer.file_name != value:
|
|
|
|
self._buffer.file_name = value
|
|
|
|
self.emit_paint_needed(0, 0, -1, -1)
|
2007-08-26 14:16:09 +02:00
|
|
|
elif pspec.name == 'icon-name':
|
2007-09-10 02:54:47 +02:00
|
|
|
if self._buffer.icon_name != value:
|
|
|
|
self._buffer.icon_name = value
|
|
|
|
self.emit_paint_needed(0, 0, -1, -1)
|
2007-08-25 13:15:28 +02:00
|
|
|
elif pspec.name == 'xo-color':
|
2007-09-10 02:54:47 +02:00
|
|
|
if self._buffer.xo_color != value:
|
|
|
|
self._buffer.xo_color = value
|
|
|
|
self.emit_paint_needed(0, 0, -1, -1)
|
2007-08-25 13:15:28 +02:00
|
|
|
elif pspec.name == 'fill-color':
|
2007-09-10 02:54:47 +02:00
|
|
|
if self._buffer.fill_color != value:
|
|
|
|
self._buffer.fill_color = value
|
|
|
|
self.emit_paint_needed(0, 0, -1, -1)
|
2007-08-25 13:15:28 +02:00
|
|
|
elif pspec.name == 'stroke-color':
|
2007-09-10 02:54:47 +02:00
|
|
|
if self._buffer.stroke_color != value:
|
|
|
|
self._buffer.stroke_color = value
|
|
|
|
self.emit_paint_needed(0, 0, -1, -1)
|
2007-08-25 13:15:28 +02:00
|
|
|
elif pspec.name == 'size':
|
2007-09-10 02:54:47 +02:00
|
|
|
if self._buffer.width != value:
|
|
|
|
self._buffer.width = value
|
|
|
|
self._buffer.height = value
|
|
|
|
self.emit_request_changed()
|
2007-08-25 13:15:28 +02:00
|
|
|
elif pspec.name == 'scale':
|
2008-04-19 00:37:19 +02:00
|
|
|
logging.warning(
|
|
|
|
'CanvasIcon: the scale parameter is currently unsupported')
|
2007-09-10 02:54:47 +02:00
|
|
|
if self._buffer.scale != value:
|
|
|
|
self._buffer.scale = value
|
|
|
|
self.emit_request_changed()
|
2007-09-08 12:10:35 +02:00
|
|
|
elif pspec.name == 'cache':
|
|
|
|
self._buffer.cache = value
|
2007-08-25 13:15:28 +02:00
|
|
|
elif pspec.name == 'badge-name':
|
2007-09-10 02:54:47 +02:00
|
|
|
if self._buffer.badge_name != value:
|
|
|
|
self._buffer.badge_name = value
|
|
|
|
self.emit_paint_needed(0, 0, -1, -1)
|
2007-08-25 13:15:28 +02:00
|
|
|
|
|
|
|
def do_get_property(self, pspec):
|
|
|
|
if pspec.name == 'size':
|
2007-08-26 14:16:09 +02:00
|
|
|
return self._buffer.width
|
|
|
|
elif pspec.name == 'file-name':
|
|
|
|
return self._buffer.file_name
|
2007-08-25 13:15:28 +02:00
|
|
|
elif pspec.name == 'icon-name':
|
2007-08-26 14:16:09 +02:00
|
|
|
return self._buffer.icon_name
|
2007-08-25 13:15:28 +02:00
|
|
|
elif pspec.name == 'fill-color':
|
2007-08-26 14:16:09 +02:00
|
|
|
return self._buffer.fill_color
|
2007-08-25 13:15:28 +02:00
|
|
|
elif pspec.name == 'stroke-color':
|
2007-08-26 14:16:09 +02:00
|
|
|
return self._buffer.stroke_color
|
2007-09-08 12:10:35 +02:00
|
|
|
elif pspec.name == 'cache':
|
|
|
|
return self._buffer.cache
|
2007-08-25 13:15:28 +02:00
|
|
|
elif pspec.name == 'badge-name':
|
2007-08-26 14:16:09 +02:00
|
|
|
return self._buffer.badge_name
|
2007-08-25 13:15:28 +02:00
|
|
|
elif pspec.name == 'scale':
|
2007-08-26 14:16:09 +02:00
|
|
|
return self._buffer.scale
|
2007-08-25 13:15:28 +02:00
|
|
|
|
2007-08-26 14:16:09 +02:00
|
|
|
def do_paint_below_children(self, cr, damaged_box):
|
|
|
|
surface = self._buffer.get_surface()
|
|
|
|
if surface:
|
|
|
|
width, height = self.get_allocation()
|
2007-08-25 13:15:28 +02:00
|
|
|
|
2007-08-26 14:16:09 +02:00
|
|
|
x = (width - surface.get_width()) / 2
|
|
|
|
y = (height - surface.get_height()) / 2
|
2007-08-25 13:15:28 +02:00
|
|
|
|
2007-08-26 14:16:09 +02:00
|
|
|
cr.set_source_surface(surface, x, y)
|
|
|
|
cr.paint()
|
2007-08-25 13:15:28 +02:00
|
|
|
|
|
|
|
def do_get_content_width_request(self):
|
2007-08-26 14:16:09 +02:00
|
|
|
surface = self._buffer.get_surface()
|
|
|
|
if surface:
|
|
|
|
size = surface.get_width()
|
|
|
|
elif self._buffer.width:
|
|
|
|
size = self._buffer.width
|
|
|
|
else:
|
|
|
|
size = 0
|
|
|
|
|
|
|
|
return size, size
|
2007-08-25 13:15:28 +02:00
|
|
|
|
|
|
|
def do_get_content_height_request(self, for_width):
|
2007-08-26 14:16:09 +02:00
|
|
|
surface = self._buffer.get_surface()
|
|
|
|
if surface:
|
|
|
|
size = surface.get_height()
|
|
|
|
elif self._buffer.height:
|
|
|
|
size = self._buffer.height
|
|
|
|
else:
|
|
|
|
size = 0
|
|
|
|
|
|
|
|
return size, size
|
2007-08-25 13:15:28 +02:00
|
|
|
|
|
|
|
def do_button_press_event(self, event):
|
|
|
|
self.emit_activated()
|
|
|
|
return True
|
|
|
|
|
|
|
|
def get_palette(self):
|
|
|
|
return self._palette
|
|
|
|
|
|
|
|
def set_palette(self, palette):
|
2008-04-20 04:11:24 +02:00
|
|
|
from sugar.graphics.palette import CanvasInvoker
|
|
|
|
|
2007-09-17 23:42:17 +02:00
|
|
|
if self._palette is not None:
|
|
|
|
self._palette.props.invoker = None
|
2007-08-25 13:15:28 +02:00
|
|
|
self._palette = palette
|
|
|
|
if not self._palette.props.invoker:
|
|
|
|
self._palette.props.invoker = CanvasInvoker(self)
|
|
|
|
|
|
|
|
def set_tooltip(self, text):
|
2008-04-20 04:11:24 +02:00
|
|
|
from sugar.graphics.palette import Palette
|
|
|
|
|
2007-08-25 13:15:28 +02:00
|
|
|
self.set_palette(Palette(text))
|
|
|
|
|
|
|
|
palette = property(get_palette, set_palette)
|
|
|
|
|
|
|
|
def get_icon_state(base_name, perc):
|
2008-04-19 00:37:19 +02:00
|
|
|
step = 5
|
|
|
|
strength = round(perc / step) * step
|
|
|
|
icon_theme = gtk.icon_theme_get_default()
|
2007-08-25 13:15:28 +02:00
|
|
|
|
2008-04-19 00:37:19 +02:00
|
|
|
while strength <= 100:
|
|
|
|
icon_name = '%s-%03d' % (base_name, strength)
|
|
|
|
if icon_theme.has_icon(icon_name):
|
|
|
|
return icon_name
|
2007-08-25 13:15:28 +02:00
|
|
|
|
2008-04-19 00:37:19 +02:00
|
|
|
strength = strength + step
|