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.
|
|
|
|
|
2008-10-28 14:19:01 +01:00
|
|
|
"""
|
|
|
|
A small fixed size picture, typically used to decorate components.
|
|
|
|
|
|
|
|
STABLE.
|
|
|
|
"""
|
|
|
|
|
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
|
2015-04-01 14:56:44 +02:00
|
|
|
import os
|
|
|
|
from ConfigParser import ConfigParser
|
2007-08-25 13:15:28 +02:00
|
|
|
|
2015-10-31 04:44:46 +01:00
|
|
|
import gi
|
|
|
|
gi.require_version('Rsvg', '2.0')
|
2011-11-15 19:29:07 +01:00
|
|
|
from gi.repository import GObject
|
|
|
|
from gi.repository import Gtk
|
2011-12-15 02:23:18 +01:00
|
|
|
from gi.repository import Gdk
|
|
|
|
from gi.repository import GdkPixbuf
|
2012-03-20 13:32:41 +01:00
|
|
|
from gi.repository import Rsvg
|
2007-08-25 13:15:28 +02:00
|
|
|
import cairo
|
2007-08-02 13:57:41 +02:00
|
|
|
|
2014-01-13 20:57:22 +01:00
|
|
|
from sugar3.graphics import style
|
2011-10-29 10:44:18 +02:00
|
|
|
from sugar3.graphics.xocolor import XoColor
|
|
|
|
from sugar3.util import LRU
|
2007-04-30 14:04:50 +02:00
|
|
|
|
2007-08-26 00:26:11 +02:00
|
|
|
_BADGE_SIZE = 0.45
|
|
|
|
|
2009-08-25 21:12:40 +02:00
|
|
|
|
2007-08-25 18:26:59 +02:00
|
|
|
class _SVGLoader(object):
|
2009-08-25 21:12:40 +02:00
|
|
|
|
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
|
|
|
|
2013-09-08 21:25:38 +02:00
|
|
|
return Rsvg.Handle.new_from_data(icon.encode('utf-8'))
|
2007-08-25 18:26:59 +02:00
|
|
|
|
2009-08-25 21:12:40 +02:00
|
|
|
|
2007-08-26 01:57:48 +02:00
|
|
|
class _IconInfo(object):
|
2009-08-25 21:12:40 +02:00
|
|
|
|
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
|
|
|
|
2009-08-25 21:12:40 +02:00
|
|
|
|
2007-08-26 02:59:05 +02:00
|
|
|
class _BadgeInfo(object):
|
2009-08-25 21:12:40 +02:00
|
|
|
|
2007-08-26 02:59:05 +02:00
|
|
|
def __init__(self):
|
|
|
|
self.attach_x = 0
|
|
|
|
self.attach_y = 0
|
|
|
|
self.size = 0
|
|
|
|
self.icon_padding = 0
|
|
|
|
|
2009-08-25 21:12:40 +02:00
|
|
|
|
2007-08-26 01:57:48 +02:00
|
|
|
class _IconBuffer(object):
|
2009-08-25 21:12:40 +02:00
|
|
|
|
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
|
2009-02-10 17:58:02 +01:00
|
|
|
self.background_color = None
|
2007-08-25 18:26:59 +02:00
|
|
|
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
|
2014-03-24 04:21:40 +01:00
|
|
|
self.pixbuf = None
|
2007-08-25 18:26:59 +02:00
|
|
|
|
2007-12-14 19:59:46 +01:00
|
|
|
def _get_cache_key(self, sensitive):
|
2009-02-10 17:58:02 +01:00
|
|
|
if self.background_color is None:
|
|
|
|
color = None
|
|
|
|
else:
|
|
|
|
color = (self.background_color.red, self.background_color.green,
|
|
|
|
self.background_color.blue)
|
2014-03-24 04:21:40 +01:00
|
|
|
|
|
|
|
return (self.icon_name, self.file_name, self.pixbuf, self.fill_color,
|
2007-12-14 19:59:46 +01:00
|
|
|
self.stroke_color, self.badge_name, self.width, self.height,
|
2009-02-10 17:58:02 +01:00
|
|
|
color, 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):
|
2012-09-06 18:05:56 +02:00
|
|
|
has_attach_points_, attach_points = info.get_attach_points()
|
2015-04-01 14:56:44 +02:00
|
|
|
attach_x = attach_y = 0
|
2007-08-26 01:57:48 +02:00
|
|
|
if attach_points:
|
2015-04-01 14:56:44 +02:00
|
|
|
# this works only for Gtk < 3.14
|
|
|
|
# https://developer.gnome.org/gtk3/stable/GtkIconTheme.html
|
|
|
|
# #gtk-icon-info-get-attach-points
|
2011-11-15 19:29:07 +01:00
|
|
|
attach_x = float(attach_points[0].x) / size_request
|
|
|
|
attach_y = float(attach_points[0].y) / size_request
|
2015-04-01 14:56:44 +02:00
|
|
|
elif info.get_filename():
|
|
|
|
# try read from the .icon file
|
|
|
|
icon_filename = info.get_filename().replace('.svg', '.icon')
|
|
|
|
if os.path.exists(icon_filename):
|
|
|
|
try:
|
|
|
|
with open(icon_filename) as config_file:
|
|
|
|
cp = ConfigParser()
|
|
|
|
cp.readfp(config_file)
|
|
|
|
attach_points_str = cp.get('Icon Data', 'AttachPoints')
|
|
|
|
attach_points = attach_points_str.split(',')
|
|
|
|
attach_x = float(attach_points[0].strip()) / 1000
|
|
|
|
attach_y = float(attach_points[1].strip()) / 1000
|
|
|
|
except Exception as e:
|
|
|
|
logging.exception('Exception reading icon info: %s', e)
|
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
|
|
|
|
2013-07-04 17:50:15 +02:00
|
|
|
def _get_icon_info(self, file_name, icon_name):
|
2007-08-26 01:57:48 +02:00
|
|
|
icon_info = _IconInfo()
|
2007-08-26 00:26:11 +02:00
|
|
|
|
2013-07-04 17:50:15 +02:00
|
|
|
if file_name:
|
|
|
|
icon_info.file_name = file_name
|
|
|
|
elif icon_name:
|
2011-11-15 19:29:07 +01:00
|
|
|
theme = Gtk.IconTheme.get_default()
|
2007-08-26 00:26:11 +02:00
|
|
|
|
2007-08-26 01:57:48 +02:00
|
|
|
size = 50
|
2013-05-18 04:27:04 +02:00
|
|
|
if self.width is not None:
|
2007-08-26 01:57:48 +02:00
|
|
|
size = self.width
|
2007-08-26 00:26:11 +02:00
|
|
|
|
2013-07-04 17:50:15 +02:00
|
|
|
info = theme.lookup_icon(icon_name, int(size), 0)
|
2007-08-26 01:57:48 +02:00
|
|
|
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:
|
2009-08-24 12:54:02 +02:00
|
|
|
logging.warning('No icon with the name %s was found in the '
|
2013-07-04 17:50:15 +02:00
|
|
|
'theme.', 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):
|
2011-11-15 19:29:07 +01:00
|
|
|
theme = Gtk.IconTheme.get_default()
|
2010-10-04 16:56:26 +02:00
|
|
|
badge_info = theme.lookup_icon(self.badge_name, int(size), 0)
|
2007-08-26 02:24:45 +02:00
|
|
|
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
|
|
|
|
2012-08-27 13:42:53 +02:00
|
|
|
icon_width = handle.props.width
|
|
|
|
icon_height = handle.props.height
|
2008-04-03 17:31:55 +02:00
|
|
|
|
2007-12-14 19:59:46 +01:00
|
|
|
pixbuf = handle.get_pixbuf()
|
2007-08-26 02:24:45 +02:00
|
|
|
else:
|
2011-11-15 19:29:07 +01:00
|
|
|
pixbuf = GdkPixbuf.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)
|
2011-11-15 20:35:45 +01:00
|
|
|
Gdk.cairo_set_source_pixbuf(context, pixbuf, 0, 0)
|
2007-12-14 19:59:46 +01:00
|
|
|
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 \
|
2013-05-18 04:27:04 +02:00
|
|
|
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
|
|
|
|
|
2009-08-25 21:12:40 +02:00
|
|
|
def _get_insensitive_pixbuf(self, pixbuf, widget):
|
2011-11-15 21:32:03 +01:00
|
|
|
if not (widget and widget.get_style()):
|
2007-12-14 19:59:46 +01:00
|
|
|
return pixbuf
|
|
|
|
|
2011-11-15 19:29:07 +01:00
|
|
|
icon_source = Gtk.IconSource()
|
2007-12-14 19:59:46 +01:00
|
|
|
# Special size meaning "don't touch"
|
|
|
|
icon_source.set_size(-1)
|
|
|
|
icon_source.set_pixbuf(pixbuf)
|
2011-11-15 19:29:07 +01:00
|
|
|
icon_source.set_state(Gtk.StateType.INSENSITIVE)
|
2007-12-14 19:59:46 +01:00
|
|
|
icon_source.set_direction_wildcarded(False)
|
|
|
|
icon_source.set_size_wildcarded(False)
|
|
|
|
|
2014-01-13 20:57:22 +01:00
|
|
|
widget_style = widget.get_style()
|
|
|
|
pixbuf = widget_style.render_icon(
|
|
|
|
icon_source, widget.get_direction(),
|
|
|
|
Gtk.StateType.INSENSITIVE, -1, widget,
|
|
|
|
'sugar-icon')
|
2007-12-14 19:59:46 +01:00
|
|
|
|
|
|
|
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]
|
|
|
|
|
2014-03-24 04:21:40 +01:00
|
|
|
if self.pixbuf:
|
|
|
|
# We alredy have the pixbuf for this icon.
|
|
|
|
pixbuf = self.pixbuf
|
|
|
|
icon_width = pixbuf.get_width()
|
|
|
|
icon_height = pixbuf.get_height()
|
|
|
|
icon_info = self._get_icon_info(self.file_name, self.icon_name)
|
|
|
|
is_svg = False
|
|
|
|
else:
|
|
|
|
# We run two attempts at finding the icon. First, we try the icon
|
|
|
|
# requested by the user. If that fails, we fall back on
|
|
|
|
# document-generic. If that doesn't work out, bail.
|
|
|
|
icon_width = None
|
|
|
|
for (file_name, icon_name) in ((self.file_name, self.icon_name),
|
2014-03-29 20:25:34 +01:00
|
|
|
(None, 'document-generic')):
|
2014-03-24 04:21:40 +01:00
|
|
|
icon_info = self._get_icon_info(file_name, icon_name)
|
|
|
|
if icon_info.file_name is None:
|
|
|
|
return None
|
|
|
|
|
|
|
|
is_svg = icon_info.file_name.endswith('.svg')
|
|
|
|
|
|
|
|
if is_svg:
|
|
|
|
try:
|
|
|
|
handle = self._load_svg(icon_info.file_name)
|
|
|
|
icon_width = handle.props.width
|
|
|
|
icon_height = handle.props.height
|
|
|
|
break
|
|
|
|
except IOError:
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
try:
|
|
|
|
path = icon_info.file_name
|
|
|
|
pixbuf = GdkPixbuf.Pixbuf.new_from_file(path)
|
|
|
|
icon_width = pixbuf.get_width()
|
|
|
|
icon_height = pixbuf.get_height()
|
|
|
|
break
|
|
|
|
except GObject.GError:
|
|
|
|
pass
|
2013-07-04 17:50:15 +02:00
|
|
|
|
|
|
|
if icon_width is None:
|
|
|
|
# Neither attempt found an icon for us to use
|
2007-08-25 18:26:59 +02:00
|
|
|
return None
|
|
|
|
|
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)
|
2009-02-10 17:58:02 +01:00
|
|
|
if self.background_color is None:
|
2010-10-04 16:56:26 +02:00
|
|
|
surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, int(width),
|
|
|
|
int(height))
|
2009-02-10 17:58:02 +01:00
|
|
|
context = cairo.Context(surface)
|
|
|
|
else:
|
2010-10-04 16:56:26 +02:00
|
|
|
surface = cairo.ImageSurface(cairo.FORMAT_RGB24, int(width),
|
|
|
|
int(height))
|
2009-02-10 17:58:02 +01:00
|
|
|
context = cairo.Context(surface)
|
|
|
|
context.set_source_color(self.background_color)
|
|
|
|
context.paint()
|
2007-08-26 01:57:48 +02:00
|
|
|
|
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)
|
|
|
|
|
2011-11-15 20:35:45 +01:00
|
|
|
Gdk.cairo_set_source_pixbuf(context, pixbuf, 0, 0)
|
2007-12-14 19:59:46 +01:00
|
|
|
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)
|
2011-11-15 20:35:45 +01:00
|
|
|
Gdk.cairo_set_source_pixbuf(context, pixbuf, 0, 0)
|
2007-08-26 01:57:48 +02:00
|
|
|
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)
|
|
|
|
|
2009-08-25 21:12:40 +02:00
|
|
|
|
2011-11-15 19:29:07 +01:00
|
|
|
class Icon(Gtk.Image):
|
2009-08-25 21:12:40 +02:00
|
|
|
|
2007-08-02 13:57:41 +02:00
|
|
|
__gtype_name__ = 'SugarIcon'
|
|
|
|
|
2014-03-29 20:25:34 +01:00
|
|
|
# FIXME: deprecate icon_size
|
2014-01-13 20:57:22 +01:00
|
|
|
_MENU_SIZES = (Gtk.IconSize.MENU, Gtk.IconSize.DND,
|
|
|
|
Gtk.IconSize.SMALL_TOOLBAR, Gtk.IconSize.BUTTON)
|
|
|
|
|
2007-08-25 18:26:59 +02:00
|
|
|
def __init__(self, **kwargs):
|
|
|
|
self._buffer = _IconBuffer()
|
2009-08-17 19:21:17 +02:00
|
|
|
# HACK: need to keep a reference to the path so it doesn't get garbage
|
2011-10-29 10:44:18 +02:00
|
|
|
# collected while it's still used if it's a sugar3.util.TempFilePath.
|
2009-08-17 19:21:17 +02:00
|
|
|
# See #1175
|
2009-08-14 09:17:39 +02:00
|
|
|
self._file = None
|
2011-03-24 00:37:03 +01:00
|
|
|
self._alpha = 1.0
|
|
|
|
self._scale = 1.0
|
2007-08-17 19:53:41 +02:00
|
|
|
|
2014-03-29 20:25:34 +01:00
|
|
|
# FIXME: deprecate icon_size
|
2014-01-13 20:57:22 +01:00
|
|
|
if 'icon_size' in kwargs:
|
|
|
|
logging.warning("icon_size is deprecated. Use pixel_size instead.")
|
|
|
|
|
2011-11-15 19:29:07 +01:00
|
|
|
GObject.GObject.__init__(self, **kwargs)
|
2007-08-02 13:57:41 +02:00
|
|
|
|
2009-08-14 09:17:39 +02:00
|
|
|
def get_file(self):
|
|
|
|
return self._file
|
|
|
|
|
|
|
|
def set_file(self, file_name):
|
|
|
|
self._file = file_name
|
|
|
|
self._buffer.file_name = file_name
|
|
|
|
|
2011-11-15 19:29:07 +01:00
|
|
|
file = GObject.property(type=object, setter=set_file, getter=get_file)
|
2009-08-14 09:17:39 +02:00
|
|
|
|
2014-03-24 04:21:40 +01:00
|
|
|
def get_pixbuf(self):
|
|
|
|
return self._buffer.pixbuf
|
|
|
|
|
|
|
|
def set_pixbuf(self, pixbuf):
|
|
|
|
self._buffer.pixbuf = pixbuf
|
|
|
|
|
2014-03-24 10:43:20 +01:00
|
|
|
pixbuf = GObject.property(type=object, setter=set_pixbuf,
|
|
|
|
getter=get_pixbuf)
|
2014-03-24 04:21:40 +01:00
|
|
|
|
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
|
|
|
|
2014-03-29 20:25:34 +01:00
|
|
|
# FIXME: deprecate icon_size
|
2014-01-13 20:57:22 +01:00
|
|
|
pixel_size = None
|
2009-08-25 19:55:48 +02:00
|
|
|
if self.props.pixel_size == -1:
|
2014-01-13 20:57:22 +01:00
|
|
|
if self.props.icon_size in self._MENU_SIZES:
|
|
|
|
pixel_size = style.SMALL_ICON_SIZE
|
|
|
|
else:
|
|
|
|
pixel_size = style.STANDARD_ICON_SIZE
|
2008-03-22 17:11:25 +01:00
|
|
|
else:
|
2014-01-13 20:57:22 +01:00
|
|
|
pixel_size = self.props.pixel_size
|
|
|
|
|
|
|
|
width = height = 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
|
|
|
|
2011-10-30 12:20:41 +01:00
|
|
|
def do_get_preferred_height(self):
|
|
|
|
self._sync_image_properties()
|
|
|
|
surface = self._buffer.get_surface()
|
|
|
|
if surface:
|
|
|
|
height = surface.get_height()
|
|
|
|
elif self._buffer.height:
|
|
|
|
height = self._buffer.height
|
|
|
|
else:
|
|
|
|
height = 0
|
|
|
|
return (height, height)
|
2008-09-20 02:32:55 +02:00
|
|
|
|
2011-10-30 12:20:41 +01:00
|
|
|
def do_get_preferred_width(self):
|
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:
|
2011-10-30 12:20:41 +01:00
|
|
|
width = surface.get_width()
|
|
|
|
elif self._buffer.width:
|
|
|
|
width = self._buffer.width
|
2007-08-29 11:34:56 +02:00
|
|
|
else:
|
2011-10-30 12:20:41 +01:00
|
|
|
width = 0
|
|
|
|
return (width, width)
|
2007-08-02 13:57:41 +02:00
|
|
|
|
2011-10-30 23:06:13 +01:00
|
|
|
def do_draw(self, cr):
|
2007-08-29 11:34:56 +02:00
|
|
|
self._sync_image_properties()
|
2011-11-15 19:29:07 +01:00
|
|
|
sensitive = (self.is_sensitive())
|
2007-12-14 19:59:46 +01:00
|
|
|
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()
|
2011-11-15 19:29:07 +01:00
|
|
|
if self.get_direction() != Gtk.TextDirection.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()
|
2011-10-30 12:20:41 +01:00
|
|
|
x = math.floor(xpad +
|
2014-03-29 20:25:34 +01:00
|
|
|
(allocation.width - requisition.width) * xalign)
|
2011-10-30 12:20:41 +01:00
|
|
|
y = math.floor(ypad +
|
2014-03-29 20:25:34 +01:00
|
|
|
(allocation.height - requisition.height) * yalign)
|
2007-08-29 11:34:56 +02:00
|
|
|
|
2011-03-24 00:37:03 +01:00
|
|
|
if self._scale != 1.0:
|
|
|
|
cr.scale(self._scale, self._scale)
|
|
|
|
|
|
|
|
margin = self._buffer.width * (1 - self._scale) / 2
|
|
|
|
x, y = x + margin, y + margin
|
|
|
|
|
|
|
|
x = x / self._scale
|
|
|
|
y = y / self._scale
|
|
|
|
|
2007-08-29 11:34:56 +02:00
|
|
|
cr.set_source_surface(surface, x, y)
|
2011-03-24 00:37:03 +01:00
|
|
|
|
|
|
|
if self._alpha == 1.0:
|
|
|
|
cr.paint()
|
|
|
|
else:
|
|
|
|
cr.paint_with_alpha(self._alpha)
|
2007-08-02 13:57:41 +02:00
|
|
|
|
2008-08-11 01:10:02 +02:00
|
|
|
def set_xo_color(self, value):
|
|
|
|
if self._buffer.xo_color != value:
|
|
|
|
self._buffer.xo_color = value
|
|
|
|
self.queue_draw()
|
|
|
|
|
2011-11-15 19:29:07 +01:00
|
|
|
xo_color = GObject.property(
|
2008-08-11 01:10:02 +02:00
|
|
|
type=object, getter=None, setter=set_xo_color)
|
|
|
|
|
|
|
|
def set_fill_color(self, value):
|
|
|
|
if self._buffer.fill_color != value:
|
|
|
|
self._buffer.fill_color = value
|
|
|
|
self.queue_draw()
|
|
|
|
|
|
|
|
def get_fill_color(self):
|
|
|
|
return self._buffer.fill_color
|
|
|
|
|
2011-11-15 19:29:07 +01:00
|
|
|
fill_color = GObject.property(
|
2008-08-11 01:10:02 +02:00
|
|
|
type=object, getter=get_fill_color, setter=set_fill_color)
|
|
|
|
|
|
|
|
def set_stroke_color(self, value):
|
|
|
|
if self._buffer.stroke_color != value:
|
|
|
|
self._buffer.stroke_color = value
|
|
|
|
self.queue_draw()
|
|
|
|
|
|
|
|
def get_stroke_color(self):
|
|
|
|
return self._buffer.stroke_color
|
|
|
|
|
2011-11-15 19:29:07 +01:00
|
|
|
stroke_color = GObject.property(
|
2008-08-11 01:10:02 +02:00
|
|
|
type=object, getter=get_stroke_color, setter=set_stroke_color)
|
|
|
|
|
|
|
|
def set_badge_name(self, value):
|
|
|
|
if self._buffer.badge_name != value:
|
|
|
|
self._buffer.badge_name = value
|
|
|
|
self.queue_resize()
|
|
|
|
|
|
|
|
def get_badge_name(self):
|
|
|
|
return self._buffer.badge_name
|
|
|
|
|
2011-11-15 19:29:07 +01:00
|
|
|
badge_name = GObject.property(
|
2008-08-11 01:10:02 +02:00
|
|
|
type=str, getter=get_badge_name, setter=set_badge_name)
|
2007-08-25 13:15:28 +02:00
|
|
|
|
2014-04-25 19:45:41 +02:00
|
|
|
def get_badge_size(self):
|
|
|
|
return int(_BADGE_SIZE * self.props.pixel_size)
|
|
|
|
|
2011-03-24 00:37:03 +01:00
|
|
|
def set_alpha(self, value):
|
|
|
|
if self._alpha != value:
|
|
|
|
self._alpha = value
|
|
|
|
self.queue_draw()
|
|
|
|
|
2011-11-15 19:29:07 +01:00
|
|
|
alpha = GObject.property(
|
2011-03-24 00:37:03 +01:00
|
|
|
type=float, setter=set_alpha)
|
|
|
|
|
|
|
|
def set_scale(self, value):
|
|
|
|
if self._scale != value:
|
|
|
|
self._scale = value
|
|
|
|
self.queue_draw()
|
|
|
|
|
2011-11-15 19:29:07 +01:00
|
|
|
scale = GObject.property(
|
2011-03-24 00:37:03 +01:00
|
|
|
type=float, setter=set_scale)
|
|
|
|
|
2009-08-25 21:12:40 +02:00
|
|
|
|
2011-12-15 00:47:11 +01:00
|
|
|
class EventIcon(Gtk.EventBox):
|
|
|
|
"""
|
|
|
|
An Icon class that provides access to mouse events and that can act as a
|
|
|
|
cursor-positioned palette invoker.
|
|
|
|
"""
|
|
|
|
|
2015-07-24 16:19:57 +02:00
|
|
|
__gsignals__ = {
|
|
|
|
'activate': (GObject.SignalFlags.RUN_FIRST, None, []),
|
|
|
|
}
|
|
|
|
|
2012-06-22 16:04:47 +02:00
|
|
|
__gtype_name__ = 'SugarEventIcon'
|
2011-12-15 00:47:11 +01:00
|
|
|
|
|
|
|
def __init__(self, **kwargs):
|
2012-06-22 16:04:47 +02:00
|
|
|
self._buffer = _IconBuffer()
|
|
|
|
self._alpha = 1.0
|
2011-12-15 00:47:11 +01:00
|
|
|
|
2012-06-22 16:04:47 +02:00
|
|
|
Gtk.EventBox.__init__(self)
|
2012-06-21 10:04:43 +02:00
|
|
|
self.set_visible_window(False)
|
2012-06-21 10:18:00 +02:00
|
|
|
self.set_above_child(True)
|
2012-10-25 11:11:30 +02:00
|
|
|
self.add_events(Gdk.EventMask.BUTTON_PRESS_MASK |
|
|
|
|
Gdk.EventMask.TOUCH_MASK |
|
|
|
|
Gdk.EventMask.BUTTON_RELEASE_MASK)
|
2016-06-15 00:01:11 +02:00
|
|
|
# Connect after the default so that the palette can silence events
|
|
|
|
# for example, after a touch palette invocation
|
|
|
|
self.connect_after('button-release-event',
|
|
|
|
self.__button_release_event_cb)
|
2011-12-15 00:47:11 +01:00
|
|
|
for key, value in kwargs.iteritems():
|
2012-06-22 16:04:47 +02:00
|
|
|
self.set_property(key, value)
|
2011-12-15 00:47:11 +01:00
|
|
|
|
2013-03-20 16:34:13 +01:00
|
|
|
from sugar3.graphics.palette import CursorInvoker
|
2011-12-15 00:47:11 +01:00
|
|
|
self._palette_invoker = CursorInvoker()
|
2012-06-22 16:04:47 +02:00
|
|
|
self._palette_invoker.attach(self)
|
2011-12-15 00:47:11 +01:00
|
|
|
self.connect('destroy', self.__destroy_cb)
|
|
|
|
|
2012-06-22 16:04:47 +02:00
|
|
|
def do_draw(self, cr):
|
|
|
|
surface = self._buffer.get_surface()
|
|
|
|
if surface:
|
|
|
|
allocation = self.get_allocation()
|
|
|
|
|
|
|
|
x = (allocation.width - surface.get_width()) / 2
|
|
|
|
y = (allocation.height - surface.get_height()) / 2
|
|
|
|
|
|
|
|
cr.set_source_surface(surface, x, y)
|
|
|
|
if self._alpha == 1.0:
|
|
|
|
cr.paint()
|
|
|
|
else:
|
|
|
|
cr.paint_with_alpha(self._alpha)
|
|
|
|
|
|
|
|
def do_get_preferred_height(self):
|
|
|
|
surface = self._buffer.get_surface()
|
|
|
|
if surface:
|
|
|
|
height = surface.get_height()
|
|
|
|
elif self._buffer.height:
|
|
|
|
height = self._buffer.height
|
|
|
|
else:
|
|
|
|
height = 0
|
|
|
|
return (height, height)
|
|
|
|
|
|
|
|
def do_get_preferred_width(self):
|
|
|
|
surface = self._buffer.get_surface()
|
|
|
|
if surface:
|
|
|
|
width = surface.get_width()
|
|
|
|
elif self._buffer.width:
|
|
|
|
width = self._buffer.width
|
|
|
|
else:
|
|
|
|
width = 0
|
|
|
|
return (width, width)
|
|
|
|
|
2011-12-15 00:47:11 +01:00
|
|
|
def __destroy_cb(self, icon):
|
|
|
|
if self._palette_invoker is not None:
|
|
|
|
self._palette_invoker.detach()
|
|
|
|
|
2012-06-22 16:04:47 +02:00
|
|
|
def set_file_name(self, value):
|
|
|
|
if self._buffer.file_name != value:
|
|
|
|
self._buffer.file_name = value
|
|
|
|
self.queue_draw()
|
|
|
|
|
|
|
|
def get_file_name(self):
|
|
|
|
return self._buffer.file_name
|
|
|
|
|
|
|
|
file_name = GObject.property(
|
|
|
|
type=object, getter=get_file_name, setter=set_file_name)
|
|
|
|
|
|
|
|
def set_icon_name(self, value):
|
|
|
|
if self._buffer.icon_name != value:
|
|
|
|
self._buffer.icon_name = value
|
|
|
|
self.queue_draw()
|
|
|
|
|
|
|
|
def get_icon_name(self):
|
|
|
|
return self._buffer.icon_name
|
|
|
|
|
|
|
|
icon_name = GObject.property(
|
|
|
|
type=object, getter=get_icon_name, setter=set_icon_name)
|
|
|
|
|
|
|
|
def set_xo_color(self, value):
|
|
|
|
if self._buffer.xo_color != value:
|
|
|
|
self._buffer.xo_color = value
|
|
|
|
self.queue_draw()
|
|
|
|
|
|
|
|
xo_color = GObject.property(
|
|
|
|
type=object, getter=None, setter=set_xo_color)
|
|
|
|
|
|
|
|
def set_fill_color(self, value):
|
|
|
|
if self._buffer.fill_color != value:
|
|
|
|
self._buffer.fill_color = value
|
|
|
|
self.queue_draw()
|
|
|
|
|
|
|
|
def get_fill_color(self):
|
|
|
|
return self._buffer.fill_color
|
|
|
|
|
|
|
|
fill_color = GObject.property(
|
|
|
|
type=object, getter=get_fill_color, setter=set_fill_color)
|
|
|
|
|
|
|
|
def set_stroke_color(self, value):
|
|
|
|
if self._buffer.stroke_color != value:
|
|
|
|
self._buffer.stroke_color = value
|
|
|
|
self.queue_draw()
|
2011-12-15 00:47:11 +01:00
|
|
|
|
2012-06-22 16:04:47 +02:00
|
|
|
def get_stroke_color(self):
|
|
|
|
return self._buffer.stroke_color
|
|
|
|
|
|
|
|
stroke_color = GObject.property(
|
|
|
|
type=object, getter=get_stroke_color, setter=set_stroke_color)
|
|
|
|
|
|
|
|
def set_background_color(self, value):
|
|
|
|
if self._buffer.background_color != value:
|
|
|
|
self._buffer.background_color = value
|
|
|
|
self.queue_draw()
|
|
|
|
|
|
|
|
def get_background_color(self):
|
|
|
|
return self._buffer.background_color
|
|
|
|
|
|
|
|
background_color = GObject.property(
|
|
|
|
type=object, getter=get_background_color, setter=set_background_color)
|
|
|
|
|
|
|
|
def set_size(self, value):
|
|
|
|
if self._buffer.width != value:
|
|
|
|
self._buffer.width = value
|
|
|
|
self._buffer.height = value
|
|
|
|
self.queue_resize()
|
2011-12-15 00:47:11 +01:00
|
|
|
|
2012-06-22 16:04:47 +02:00
|
|
|
def get_size(self):
|
|
|
|
return self._buffer.width
|
|
|
|
|
|
|
|
pixel_size = GObject.property(
|
|
|
|
type=object, getter=get_size, setter=set_size)
|
|
|
|
|
|
|
|
def set_scale(self, value):
|
|
|
|
if self._buffer.scale != value:
|
|
|
|
self._buffer.scale = value
|
|
|
|
self.queue_resize()
|
|
|
|
|
|
|
|
def get_scale(self):
|
|
|
|
return self._buffer.scale
|
|
|
|
|
|
|
|
scale = GObject.property(
|
|
|
|
type=float, getter=get_scale, setter=set_scale)
|
|
|
|
|
|
|
|
def set_alpha(self, alpha):
|
|
|
|
if self._alpha != alpha:
|
|
|
|
self._alpha = alpha
|
|
|
|
self.queue_draw()
|
|
|
|
|
|
|
|
alpha = GObject.property(
|
|
|
|
type=float, setter=set_alpha)
|
|
|
|
|
|
|
|
def set_cache(self, value):
|
|
|
|
self._buffer.cache = value
|
|
|
|
|
|
|
|
def get_cache(self):
|
|
|
|
return self._buffer.cache
|
|
|
|
|
|
|
|
cache = GObject.property(
|
|
|
|
type=bool, default=False, getter=get_cache, setter=set_cache)
|
|
|
|
|
|
|
|
def set_badge_name(self, value):
|
|
|
|
if self._buffer.badge_name != value:
|
|
|
|
self._buffer.badge_name = value
|
|
|
|
self.queue_draw()
|
|
|
|
|
|
|
|
def get_badge_name(self):
|
|
|
|
return self._buffer.badge_name
|
|
|
|
|
|
|
|
badge_name = GObject.property(
|
|
|
|
type=object, getter=get_badge_name, setter=set_badge_name)
|
2011-12-15 00:47:11 +01:00
|
|
|
|
2012-06-21 10:31:48 +02:00
|
|
|
def create_palette(self):
|
|
|
|
return None
|
|
|
|
|
2011-12-15 00:47:11 +01:00
|
|
|
def get_palette(self):
|
|
|
|
return self._palette_invoker.palette
|
|
|
|
|
|
|
|
def set_palette(self, palette):
|
|
|
|
self._palette_invoker.palette = palette
|
|
|
|
|
|
|
|
palette = GObject.property(
|
|
|
|
type=object, setter=set_palette, getter=get_palette)
|
|
|
|
|
|
|
|
def get_palette_invoker(self):
|
|
|
|
return self._palette_invoker
|
|
|
|
|
|
|
|
def set_palette_invoker(self, palette_invoker):
|
|
|
|
self._palette_invoker.detach()
|
|
|
|
self._palette_invoker = palette_invoker
|
|
|
|
|
|
|
|
palette_invoker = GObject.property(
|
|
|
|
type=object, setter=set_palette_invoker, getter=get_palette_invoker)
|
|
|
|
|
|
|
|
def set_tooltip(self, text):
|
2013-03-20 16:34:13 +01:00
|
|
|
from sugar3.graphics.palette import Palette
|
|
|
|
|
2011-12-15 00:47:11 +01:00
|
|
|
self.set_palette(Palette(text))
|
|
|
|
|
2015-07-24 16:19:57 +02:00
|
|
|
def __button_release_event_cb(self, icon, event):
|
2015-08-06 21:59:13 +02:00
|
|
|
if event.button == 1:
|
|
|
|
alloc = self.get_allocation()
|
|
|
|
if 0 < event.x < alloc.width and 0 < event.y < alloc.height:
|
|
|
|
self.emit('activate')
|
2015-07-24 16:19:57 +02:00
|
|
|
|
2011-12-15 00:47:11 +01:00
|
|
|
|
2012-10-10 21:20:05 +02:00
|
|
|
class CanvasIcon(EventIcon):
|
|
|
|
"""
|
|
|
|
An EventIcon with active and prelight states, and a styleable
|
|
|
|
background.
|
|
|
|
|
|
|
|
If the icon pops up a palette, the prelight state is set until the
|
|
|
|
palette pops down.
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
__gtype_name__ = 'SugarCanvasIcon'
|
|
|
|
|
|
|
|
def __init__(self, **kwargs):
|
|
|
|
EventIcon.__init__(self, **kwargs)
|
2015-07-26 01:22:16 +02:00
|
|
|
self._button_down = False
|
2012-10-10 21:20:05 +02:00
|
|
|
|
|
|
|
self.connect('enter-notify-event', self.__enter_notify_event_cb)
|
|
|
|
self.connect('leave-notify-event', self.__leave_notify_event_cb)
|
|
|
|
self.connect('button-press-event', self.__button_press_event_cb)
|
|
|
|
self.connect('button-release-event', self.__button_release_event_cb)
|
|
|
|
|
|
|
|
def connect_to_palette_pop_events(self, palette):
|
|
|
|
palette.connect('popup', self.__palette_popup_cb)
|
|
|
|
palette.connect('popdown', self.__palette_popdown_cb)
|
|
|
|
|
|
|
|
def do_draw(self, cr):
|
|
|
|
"""Render a background that fits the allocated space."""
|
|
|
|
allocation = self.get_allocation()
|
|
|
|
context = self.get_style_context()
|
|
|
|
Gtk.render_background(context, cr, 0, 0,
|
|
|
|
allocation.width,
|
|
|
|
allocation.height)
|
|
|
|
|
|
|
|
EventIcon.do_draw(self, cr)
|
|
|
|
|
|
|
|
def __enter_notify_event_cb(self, icon, event):
|
2012-10-15 16:28:44 +02:00
|
|
|
self.set_state_flags(self.get_state_flags() | Gtk.StateFlags.PRELIGHT,
|
|
|
|
clear=True)
|
2015-07-26 01:22:16 +02:00
|
|
|
if self._button_down:
|
|
|
|
self.set_state_flags(Gtk.StateFlags.ACTIVE, clear=False)
|
2012-10-10 21:20:05 +02:00
|
|
|
|
|
|
|
def __leave_notify_event_cb(self, icon, event):
|
2012-10-15 15:53:18 +02:00
|
|
|
if self.palette and self.palette.is_up():
|
2012-10-10 21:20:05 +02:00
|
|
|
return
|
|
|
|
|
2015-07-26 01:22:16 +02:00
|
|
|
self.unset_state_flags(Gtk.StateFlags.PRELIGHT | Gtk.StateFlags.ACTIVE)
|
2012-10-10 21:20:05 +02:00
|
|
|
|
|
|
|
def __button_press_event_cb(self, icon, event):
|
2012-11-29 19:10:18 +01:00
|
|
|
if self.palette and not self.palette.is_up():
|
2015-07-26 01:22:16 +02:00
|
|
|
self._button_down = True
|
2013-05-18 04:27:04 +02:00
|
|
|
self.set_state_flags(
|
|
|
|
self.get_state_flags() | Gtk.StateFlags.ACTIVE,
|
|
|
|
clear=True)
|
2012-10-10 21:20:05 +02:00
|
|
|
|
|
|
|
def __button_release_event_cb(self, icon, event):
|
2012-10-15 16:28:44 +02:00
|
|
|
self.unset_state_flags(Gtk.StateFlags.ACTIVE)
|
2015-07-26 01:22:16 +02:00
|
|
|
self._button_down = False
|
2012-10-10 21:20:05 +02:00
|
|
|
|
|
|
|
def __palette_popup_cb(self, palette):
|
2012-10-15 16:28:44 +02:00
|
|
|
self.set_state_flags(Gtk.StateFlags.PRELIGHT, clear=True)
|
2012-10-10 21:20:05 +02:00
|
|
|
|
|
|
|
def __palette_popdown_cb(self, palette):
|
2012-10-15 16:28:44 +02:00
|
|
|
self.unset_state_flags(Gtk.StateFlags.PRELIGHT)
|
2015-12-31 12:53:47 +01:00
|
|
|
if hasattr(CanvasIcon, 'set_css_name'):
|
|
|
|
CanvasIcon.set_css_name('canvasicon')
|
2012-10-10 21:20:05 +02:00
|
|
|
|
|
|
|
|
2011-11-15 19:29:07 +01:00
|
|
|
class CellRendererIcon(Gtk.CellRenderer):
|
2009-08-25 21:12:40 +02:00
|
|
|
|
2009-06-06 14:31:53 +02:00
|
|
|
__gtype_name__ = 'SugarCellRendererIcon'
|
|
|
|
|
2009-06-06 16:24:49 +02:00
|
|
|
__gsignals__ = {
|
2011-11-15 19:29:07 +01:00
|
|
|
'clicked': (GObject.SignalFlags.RUN_FIRST, None, [object]),
|
2009-06-06 16:24:49 +02:00
|
|
|
}
|
|
|
|
|
2014-11-28 12:45:22 +01:00
|
|
|
def __init__(self, treeview=None):
|
|
|
|
# treeview is not used anymore, is here just to not break the API
|
2014-11-17 10:19:15 +01:00
|
|
|
if treeview is not None:
|
|
|
|
logging.warning('CellRendererIcon: treeview parameter in '
|
|
|
|
'constructor is deprecated')
|
2009-06-06 14:31:53 +02:00
|
|
|
self._buffer = _IconBuffer()
|
2009-06-24 20:25:26 +02:00
|
|
|
self._buffer.cache = True
|
2009-08-29 23:50:18 +02:00
|
|
|
self._xo_color = None
|
2009-06-13 20:57:21 +02:00
|
|
|
self._fill_color = None
|
|
|
|
self._stroke_color = None
|
|
|
|
self._prelit_fill_color = None
|
|
|
|
self._prelit_stroke_color = None
|
2012-10-18 15:42:32 +02:00
|
|
|
self._active_state = False
|
2014-05-07 14:07:36 +02:00
|
|
|
self._cached_offsets = None
|
2009-06-07 13:41:40 +02:00
|
|
|
|
2012-10-17 14:31:42 +02:00
|
|
|
Gtk.CellRenderer.__init__(self)
|
2009-06-07 13:41:40 +02:00
|
|
|
|
2014-05-07 14:20:14 +02:00
|
|
|
self._is_scrolling = False
|
|
|
|
|
|
|
|
def connect_to_scroller(self, scrolled):
|
|
|
|
scrolled.connect('scroll-start', self._scroll_start_cb)
|
|
|
|
scrolled.connect('scroll-end', self._scroll_end_cb)
|
|
|
|
|
|
|
|
def _scroll_start_cb(self, event):
|
|
|
|
self._is_scrolling = True
|
|
|
|
|
|
|
|
def _scroll_end_cb(self, event):
|
|
|
|
self._is_scrolling = False
|
|
|
|
|
|
|
|
def is_scrolling(self):
|
|
|
|
return self._is_scrolling
|
|
|
|
|
2009-06-07 13:41:40 +02:00
|
|
|
def create_palette(self):
|
|
|
|
return None
|
|
|
|
|
2009-06-06 14:31:53 +02:00
|
|
|
def set_file_name(self, value):
|
|
|
|
if self._buffer.file_name != value:
|
|
|
|
self._buffer.file_name = value
|
|
|
|
|
2011-11-15 19:29:07 +01:00
|
|
|
file_name = GObject.property(type=str, setter=set_file_name)
|
2009-06-06 14:31:53 +02:00
|
|
|
|
|
|
|
def set_icon_name(self, value):
|
|
|
|
if self._buffer.icon_name != value:
|
|
|
|
self._buffer.icon_name = value
|
|
|
|
|
2011-11-15 19:29:07 +01:00
|
|
|
icon_name = GObject.property(type=str, setter=set_icon_name)
|
2009-06-06 14:31:53 +02:00
|
|
|
|
2009-08-29 23:50:18 +02:00
|
|
|
def get_xo_color(self):
|
|
|
|
return self._xo_color
|
|
|
|
|
2009-06-06 14:31:53 +02:00
|
|
|
def set_xo_color(self, value):
|
2009-08-29 23:50:18 +02:00
|
|
|
self._xo_color = value
|
2009-06-06 14:31:53 +02:00
|
|
|
|
2011-11-15 19:29:07 +01:00
|
|
|
xo_color = GObject.property(type=object,
|
2013-05-18 04:27:04 +02:00
|
|
|
getter=get_xo_color, setter=set_xo_color)
|
2009-06-06 14:31:53 +02:00
|
|
|
|
|
|
|
def set_fill_color(self, value):
|
2009-06-13 20:57:21 +02:00
|
|
|
if self._fill_color != value:
|
|
|
|
self._fill_color = value
|
2009-06-06 14:31:53 +02:00
|
|
|
|
2011-11-15 19:29:07 +01:00
|
|
|
fill_color = GObject.property(type=object, setter=set_fill_color)
|
2009-06-06 14:31:53 +02:00
|
|
|
|
|
|
|
def set_stroke_color(self, value):
|
2009-06-13 20:57:21 +02:00
|
|
|
if self._stroke_color != value:
|
|
|
|
self._stroke_color = value
|
2009-06-06 14:31:53 +02:00
|
|
|
|
2011-11-15 19:29:07 +01:00
|
|
|
stroke_color = GObject.property(type=object, setter=set_stroke_color)
|
2009-06-06 14:31:53 +02:00
|
|
|
|
2009-06-13 20:57:21 +02:00
|
|
|
def set_prelit_fill_color(self, value):
|
|
|
|
if self._prelit_fill_color != value:
|
|
|
|
self._prelit_fill_color = value
|
|
|
|
|
2011-11-15 19:29:07 +01:00
|
|
|
prelit_fill_color = GObject.property(type=object,
|
2009-06-13 20:57:21 +02:00
|
|
|
setter=set_prelit_fill_color)
|
|
|
|
|
|
|
|
def set_prelit_stroke_color(self, value):
|
|
|
|
if self._prelit_stroke_color != value:
|
|
|
|
self._prelit_stroke_color = value
|
|
|
|
|
2011-11-15 19:29:07 +01:00
|
|
|
prelit_stroke_color = GObject.property(type=object,
|
2009-06-13 20:57:21 +02:00
|
|
|
setter=set_prelit_stroke_color)
|
|
|
|
|
2009-06-06 14:31:53 +02:00
|
|
|
def set_background_color(self, value):
|
|
|
|
if self._buffer.background_color != value:
|
|
|
|
self._buffer.background_color = value
|
|
|
|
|
2011-11-15 19:29:07 +01:00
|
|
|
background_color = GObject.property(type=object,
|
2009-08-24 14:51:00 +02:00
|
|
|
setter=set_background_color)
|
2009-06-06 14:31:53 +02:00
|
|
|
|
|
|
|
def set_size(self, value):
|
|
|
|
if self._buffer.width != value:
|
|
|
|
self._buffer.width = value
|
|
|
|
self._buffer.height = value
|
|
|
|
|
2014-05-07 14:07:36 +02:00
|
|
|
self._cached_offsets = None
|
|
|
|
|
2011-11-15 19:29:07 +01:00
|
|
|
size = GObject.property(type=object, setter=set_size)
|
2009-06-06 14:31:53 +02:00
|
|
|
|
2012-08-23 20:16:32 +02:00
|
|
|
def do_get_size(self, widget, cell_area, x_offset=None, y_offset=None,
|
|
|
|
width=None, height=None):
|
2009-06-24 20:25:26 +02:00
|
|
|
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:
|
|
|
|
|
2011-11-15 19:29:07 +01:00
|
|
|
if widget.get_direction() == Gtk.TextDirection.RTL:
|
2009-06-24 20:25:26 +02:00
|
|
|
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)
|
2014-05-07 14:07:36 +02:00
|
|
|
self._cached_offsets = xoffset, yoffset
|
2009-06-24 20:25:26 +02:00
|
|
|
|
|
|
|
return xoffset, yoffset, width, height
|
|
|
|
|
2014-05-07 14:07:36 +02:00
|
|
|
def _get_offsets(self, widget, cell_area):
|
|
|
|
if self._cached_offsets is not None:
|
|
|
|
return self._cached_offsets
|
|
|
|
|
|
|
|
xoffset, yoffset, width_, height_ = self.do_get_size(widget, cell_area)
|
|
|
|
return xoffset, yoffset
|
|
|
|
|
2012-08-23 20:16:32 +02:00
|
|
|
def do_activate(self, event, widget, path, background_area, cell_area,
|
2009-06-27 18:04:00 +02:00
|
|
|
flags):
|
|
|
|
pass
|
2009-06-24 20:25:26 +02:00
|
|
|
|
2012-08-23 20:16:32 +02:00
|
|
|
def do_start_editing(self, event, widget, path, background_area, cell_area,
|
2009-06-27 18:04:00 +02:00
|
|
|
flags):
|
2009-06-24 20:25:26 +02:00
|
|
|
pass
|
|
|
|
|
2012-08-23 20:16:32 +02:00
|
|
|
def do_render(self, cr, widget, background_area, cell_area, flags):
|
2014-05-07 14:20:14 +02:00
|
|
|
if not self._is_scrolling:
|
|
|
|
|
|
|
|
context = widget.get_style_context()
|
|
|
|
context.save()
|
|
|
|
context.add_class("sugar-icon-cell")
|
|
|
|
|
|
|
|
def is_pointer_inside():
|
|
|
|
# widget is the treeview
|
|
|
|
x, y = widget.get_pointer()
|
|
|
|
x, y = widget.convert_widget_to_bin_window_coords(x, y)
|
|
|
|
return ((cell_area.x <= x <= cell_area.x + cell_area.width)
|
|
|
|
and
|
|
|
|
(cell_area.y <= y <= cell_area.y + cell_area.height))
|
|
|
|
|
|
|
|
pointer_inside = is_pointer_inside()
|
|
|
|
|
|
|
|
# The context will have prelight state if the mouse pointer is
|
|
|
|
# in the entire row, but we want that state if the pointer is
|
|
|
|
# in this cell only:
|
|
|
|
if flags & Gtk.CellRendererState.PRELIT:
|
|
|
|
if pointer_inside:
|
|
|
|
if self._active_state:
|
|
|
|
context.set_state(Gtk.StateFlags.ACTIVE)
|
|
|
|
else:
|
|
|
|
context.set_state(Gtk.StateFlags.NORMAL)
|
2012-10-17 14:31:42 +02:00
|
|
|
|
2014-05-07 14:20:14 +02:00
|
|
|
Gtk.render_background(
|
|
|
|
context, cr, background_area.x, background_area.y,
|
|
|
|
background_area.width, background_area.height)
|
2012-10-17 14:31:42 +02:00
|
|
|
|
2014-05-07 14:20:14 +02:00
|
|
|
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
|
2009-08-29 23:50:18 +02:00
|
|
|
|
2014-05-07 14:20:14 +02:00
|
|
|
has_prelit_colors = None not in [prelit_fill_color,
|
|
|
|
prelit_stroke_color]
|
2009-08-29 23:50:18 +02:00
|
|
|
|
2014-05-07 14:20:14 +02:00
|
|
|
if flags & Gtk.CellRendererState.PRELIT and has_prelit_colors and \
|
|
|
|
pointer_inside:
|
2009-06-13 20:57:21 +02:00
|
|
|
|
2014-05-07 14:20:14 +02:00
|
|
|
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
|
2009-06-13 20:57:21 +02:00
|
|
|
else:
|
2014-05-07 14:20:14 +02:00
|
|
|
if self._xo_color is not None:
|
|
|
|
self._buffer.fill_color = self._xo_color.get_fill_color()
|
|
|
|
self._buffer.stroke_color = self._xo_color.get_stroke_color()
|
|
|
|
else:
|
|
|
|
self._buffer.fill_color = self._fill_color
|
|
|
|
self._buffer.stroke_color = self._stroke_color
|
2009-06-13 20:57:21 +02:00
|
|
|
|
2009-06-06 14:31:53 +02:00
|
|
|
surface = self._buffer.get_surface()
|
|
|
|
if surface is None:
|
2009-06-24 20:25:26 +02:00
|
|
|
return
|
2009-06-06 14:31:53 +02:00
|
|
|
|
2014-05-07 14:07:36 +02:00
|
|
|
xoffset, yoffset = self._get_offsets(widget, cell_area)
|
2009-06-06 14:31:53 +02:00
|
|
|
|
2009-06-24 20:25:26 +02:00
|
|
|
x = cell_area.x + xoffset
|
|
|
|
y = cell_area.y + yoffset
|
|
|
|
|
|
|
|
cr.set_source_surface(surface, math.floor(x), math.floor(y))
|
2012-08-23 20:16:32 +02:00
|
|
|
cr.rectangle(cell_area.x, cell_area.y, cell_area.width,
|
|
|
|
cell_area.height)
|
2012-09-11 16:00:42 +02:00
|
|
|
cr.clip()
|
2009-06-24 20:25:26 +02:00
|
|
|
cr.paint()
|
2009-06-06 16:24:49 +02:00
|
|
|
|
2009-08-25 21:12:40 +02:00
|
|
|
|
2008-06-23 13:58:46 +02:00
|
|
|
def get_icon_state(base_name, perc, step=5):
|
2008-04-19 00:37:19 +02:00
|
|
|
strength = round(perc / step) * step
|
2011-11-15 19:29:07 +01:00
|
|
|
icon_theme = Gtk.IconTheme.get_default()
|
2007-08-25 13:15:28 +02:00
|
|
|
|
2008-07-14 13:31:05 +02:00
|
|
|
while strength <= 100 and strength >= 0:
|
2008-04-19 00:37:19 +02:00
|
|
|
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
|
2009-02-16 16:06:38 +01:00
|
|
|
|
2009-08-25 21:12:40 +02:00
|
|
|
|
2009-02-16 16:06:38 +01:00
|
|
|
def get_icon_file_name(icon_name):
|
2011-11-15 19:29:07 +01:00
|
|
|
icon_theme = Gtk.IconTheme.get_default()
|
|
|
|
info = icon_theme.lookup_icon(icon_name, Gtk.IconSize.LARGE_TOOLBAR, 0)
|
2009-02-16 16:06:38 +01:00
|
|
|
if not info:
|
|
|
|
return None
|
|
|
|
filename = info.get_filename()
|
|
|
|
del info
|
|
|
|
return filename
|
2010-01-25 18:40:58 +01:00
|
|
|
|
|
|
|
|
|
|
|
def get_surface(**kwargs):
|
2010-01-31 17:41:34 +01:00
|
|
|
"""Get cached cairo surface.
|
|
|
|
|
|
|
|
Keyword arguments:
|
|
|
|
icon_name -- name of icon to load, default None
|
|
|
|
file_name -- path to image file, default None
|
|
|
|
fill_color -- for svg images, change default fill color
|
|
|
|
default None
|
|
|
|
stroke_color -- for svg images, change default stroke color
|
|
|
|
default None
|
|
|
|
background_color -- draw background or surface will be transparent
|
|
|
|
default None
|
|
|
|
badge_name -- name of icon which will be drawn on top of
|
|
|
|
original image, default None
|
|
|
|
width -- change image width, default None
|
|
|
|
height -- change image height, default None
|
|
|
|
cache -- if image is svg, keep svg file content for later
|
|
|
|
scale -- scale image, default 1.0
|
|
|
|
|
|
|
|
Return: cairo surface or None if image was not found
|
|
|
|
|
|
|
|
"""
|
2010-01-25 18:40:58 +01:00
|
|
|
icon = _IconBuffer()
|
|
|
|
for key, value in kwargs.items():
|
|
|
|
icon.__setattr__(key, value)
|
|
|
|
return icon.get_surface()
|