2006-10-15 01:08:44 +02:00
|
|
|
# Copyright (C) 2006, Red Hat, Inc.
|
|
|
|
#
|
|
|
|
# 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-03-12 17:47:09 +01:00
|
|
|
|
2007-02-28 19:53:06 +01:00
|
|
|
import logging
|
2006-10-01 19:08:26 +02:00
|
|
|
import re
|
|
|
|
|
|
|
|
import gobject
|
|
|
|
import gtk
|
|
|
|
import hippo
|
|
|
|
import rsvg
|
|
|
|
import cairo
|
2007-01-14 19:59:11 +01:00
|
|
|
import time
|
2006-10-01 19:08:26 +02:00
|
|
|
|
2007-02-22 22:51:24 +01:00
|
|
|
from sugar.graphics.popup import Popup
|
|
|
|
from sugar.graphics import color
|
2007-02-23 17:08:37 +01:00
|
|
|
from sugar.graphics.xocolor import XoColor
|
2007-02-22 22:51:24 +01:00
|
|
|
from sugar.graphics import font
|
|
|
|
from sugar.graphics import units
|
2007-03-12 17:47:09 +01:00
|
|
|
from sugar.graphics import animator
|
|
|
|
|
|
|
|
class _PopupAnimation(animator.Animation):
|
|
|
|
def __init__(self, icon):
|
|
|
|
animator.Animation.__init__(self, 0.0, 1.0)
|
|
|
|
self._icon = icon
|
|
|
|
|
|
|
|
def next_frame(self, current):
|
|
|
|
if current == 1.0:
|
|
|
|
self._icon.show_popup()
|
2006-10-01 19:08:26 +02:00
|
|
|
|
2007-03-16 14:29:39 +01:00
|
|
|
class _PopdownAnimation(animator.Animation):
|
|
|
|
def __init__(self, icon):
|
|
|
|
animator.Animation.__init__(self, 0.0, 1.0)
|
|
|
|
self._icon = icon
|
|
|
|
|
|
|
|
def next_frame(self, current):
|
|
|
|
if current == 1.0:
|
|
|
|
self._icon.hide_popup()
|
|
|
|
|
2007-01-14 19:59:11 +01:00
|
|
|
class _IconCacheIcon:
|
2007-02-23 17:08:37 +01:00
|
|
|
def __init__(self, name, fill_color, stroke_color, now):
|
2007-01-14 19:59:11 +01:00
|
|
|
self.data_size = None
|
2007-02-23 17:08:37 +01:00
|
|
|
self.handle = self._read_icon_data(name, fill_color, stroke_color)
|
2007-01-14 19:59:11 +01:00
|
|
|
self.last_used = now
|
|
|
|
self.usage_count = 1
|
2006-10-01 19:08:26 +02:00
|
|
|
|
2007-02-23 17:08:37 +01:00
|
|
|
def _read_icon_data(self, filename, fill_color, stroke_color):
|
2006-12-04 20:12:24 +01:00
|
|
|
icon_file = open(filename, 'r')
|
2007-01-14 19:59:11 +01:00
|
|
|
data = icon_file.read()
|
|
|
|
icon_file.close()
|
2006-10-01 19:08:26 +02:00
|
|
|
|
2007-02-23 17:08:37 +01:00
|
|
|
if fill_color:
|
|
|
|
entity = '<!ENTITY fill_color "%s">' % fill_color
|
2006-12-04 20:12:24 +01:00
|
|
|
data = re.sub('<!ENTITY fill_color .*>', entity, data)
|
2006-10-01 19:08:26 +02:00
|
|
|
|
2007-02-23 17:08:37 +01:00
|
|
|
if stroke_color:
|
|
|
|
entity = '<!ENTITY stroke_color "%s">' % stroke_color
|
2006-12-04 20:12:24 +01:00
|
|
|
data = re.sub('<!ENTITY stroke_color .*>', entity, data)
|
2006-10-01 19:08:26 +02:00
|
|
|
|
2007-01-14 19:59:11 +01:00
|
|
|
self.data_size = len(data)
|
|
|
|
return rsvg.Handle(data=data)
|
2006-10-01 19:08:26 +02:00
|
|
|
|
2007-01-14 19:59:11 +01:00
|
|
|
class _IconCache:
|
|
|
|
_CACHE_MAX = 50000 # in bytes
|
2007-01-12 21:35:53 +01:00
|
|
|
|
2007-01-14 19:59:11 +01:00
|
|
|
def __init__(self):
|
|
|
|
self._icons = {}
|
|
|
|
self._theme = gtk.icon_theme_get_default()
|
|
|
|
self._cache_size = 0
|
2006-10-01 19:08:26 +02:00
|
|
|
|
2007-01-27 01:36:31 +01:00
|
|
|
def _get_real_name_from_theme(self, name):
|
|
|
|
info = self._theme.lookup_icon(name, 50, 0)
|
2007-01-11 23:57:06 +01:00
|
|
|
if not info:
|
2007-01-14 19:59:11 +01:00
|
|
|
raise ValueError("Icon '" + name + "' not found.")
|
|
|
|
fname = info.get_filename()
|
|
|
|
del info
|
|
|
|
return fname
|
|
|
|
|
|
|
|
def _cache_cleanup(self, key, now):
|
|
|
|
while self._cache_size > self._CACHE_MAX:
|
|
|
|
evict_key = None
|
|
|
|
oldest_key = None
|
|
|
|
oldest_time = now
|
|
|
|
for icon_key, icon in self._icons.items():
|
|
|
|
# Don't evict the icon we are about to use if it's in the cache
|
|
|
|
if icon_key == key:
|
|
|
|
continue
|
|
|
|
|
|
|
|
# evict large icons first
|
|
|
|
if icon.data_size > self._CACHE_MAX:
|
|
|
|
evict_key = icon_key
|
|
|
|
break
|
|
|
|
# evict older icons next; those used over 2 minutes ago
|
|
|
|
if icon.last_used < now - 120:
|
|
|
|
evict_key = icon_key
|
|
|
|
break
|
|
|
|
# otherwise, evict the oldest
|
|
|
|
if oldest_time > icon.last_used:
|
|
|
|
oldest_time = icon.last_used
|
|
|
|
oldest_key = icon_key
|
|
|
|
|
|
|
|
# If there's nothing specific to evict, try evicting
|
|
|
|
# the oldest thing
|
|
|
|
if not evict_key:
|
|
|
|
if not oldest_key:
|
|
|
|
break
|
|
|
|
evict_key = oldest_key
|
|
|
|
|
|
|
|
self._cache_size -= self._icons[evict_key].data_size
|
|
|
|
del self._icons[evict_key]
|
|
|
|
|
2007-02-23 17:08:37 +01:00
|
|
|
def get_handle(self, name, fill_color, stroke_color):
|
2007-01-27 01:36:31 +01:00
|
|
|
if name == None:
|
|
|
|
return None
|
|
|
|
|
2007-01-14 19:59:11 +01:00
|
|
|
if name[0:6] == "theme:":
|
2007-01-27 01:36:31 +01:00
|
|
|
name = self._get_real_name_from_theme(name[6:])
|
2007-01-11 23:57:06 +01:00
|
|
|
|
2007-02-23 17:08:37 +01:00
|
|
|
if fill_color or stroke_color:
|
|
|
|
key = (name, fill_color, stroke_color)
|
2006-12-04 20:12:24 +01:00
|
|
|
else:
|
2007-01-14 19:59:11 +01:00
|
|
|
key = name
|
|
|
|
|
|
|
|
# If we're over the cache limit, evict something from the cache
|
|
|
|
now = time.time()
|
|
|
|
self._cache_cleanup(key, now)
|
2006-10-01 19:08:26 +02:00
|
|
|
|
2006-12-04 20:12:24 +01:00
|
|
|
if self._icons.has_key(key):
|
|
|
|
icon = self._icons[key]
|
2007-01-14 19:59:11 +01:00
|
|
|
icon.usage_count += 1
|
|
|
|
icon.last_used = now
|
2006-12-04 20:12:24 +01:00
|
|
|
else:
|
2007-02-23 17:08:37 +01:00
|
|
|
icon = _IconCacheIcon(name, fill_color, stroke_color, now)
|
2006-12-04 20:12:24 +01:00
|
|
|
self._icons[key] = icon
|
2007-01-14 19:59:11 +01:00
|
|
|
self._cache_size += icon.data_size
|
|
|
|
return icon.handle
|
|
|
|
|
2006-10-01 19:08:26 +02:00
|
|
|
|
|
|
|
class CanvasIcon(hippo.CanvasBox, hippo.CanvasItem):
|
2006-12-04 20:12:24 +01:00
|
|
|
__gtype_name__ = 'CanvasIcon'
|
|
|
|
|
|
|
|
__gproperties__ = {
|
2007-02-23 17:08:37 +01:00
|
|
|
'icon-name' : (str, None, None, None,
|
|
|
|
gobject.PARAM_READWRITE),
|
|
|
|
'xo-color' : (object, None, None,
|
2007-02-28 19:53:06 +01:00
|
|
|
gobject.PARAM_WRITABLE),
|
2007-02-23 17:08:37 +01:00
|
|
|
'fill-color' : (object, None, None,
|
|
|
|
gobject.PARAM_READWRITE),
|
|
|
|
'stroke-color' : (object, None, None,
|
|
|
|
gobject.PARAM_READWRITE),
|
|
|
|
'scale' : (float, None, None,
|
2007-02-24 17:15:47 +01:00
|
|
|
0.0, 1024.0, units.STANDARD_ICON_SCALE,
|
2007-02-23 17:08:37 +01:00
|
|
|
gobject.PARAM_READWRITE),
|
|
|
|
'cache' : (bool, None, None, False,
|
|
|
|
gobject.PARAM_READWRITE),
|
|
|
|
'tooltip' : (str, None, None, None,
|
2007-02-28 19:53:06 +01:00
|
|
|
gobject.PARAM_READWRITE),
|
|
|
|
'active' : (bool, None, None, True,
|
2007-02-23 17:08:37 +01:00
|
|
|
gobject.PARAM_READWRITE)
|
2006-12-04 20:12:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
_cache = _IconCache()
|
|
|
|
|
|
|
|
def __init__(self, **kwargs):
|
2007-01-15 00:20:43 +01:00
|
|
|
self._buffers = {}
|
|
|
|
self._cur_buffer = None
|
2007-02-24 17:15:47 +01:00
|
|
|
self._scale = units.STANDARD_ICON_SCALE
|
2007-02-23 17:08:37 +01:00
|
|
|
self._fill_color = None
|
|
|
|
self._stroke_color = None
|
2006-12-04 20:12:24 +01:00
|
|
|
self._icon_name = None
|
2007-01-15 00:20:43 +01:00
|
|
|
self._cache = False
|
2007-01-27 01:36:31 +01:00
|
|
|
self._handle = None
|
2007-02-21 14:13:52 +01:00
|
|
|
self._popup = None
|
2007-03-12 17:47:09 +01:00
|
|
|
self._hover_icon = False
|
2007-02-21 14:13:52 +01:00
|
|
|
self._hover_popup = False
|
2007-02-22 22:51:24 +01:00
|
|
|
self._tooltip = False
|
2007-02-28 19:53:06 +01:00
|
|
|
self._active = True
|
2007-03-12 17:47:09 +01:00
|
|
|
self._popup_anim = None
|
|
|
|
self._enter_or_leave_sid = 0
|
2007-02-22 22:51:24 +01:00
|
|
|
|
2006-12-04 20:12:24 +01:00
|
|
|
hippo.CanvasBox.__init__(self, **kwargs)
|
|
|
|
|
2007-02-23 20:39:52 +01:00
|
|
|
self.connect_after('motion-notify-event', self._motion_notify_event_cb)
|
2006-12-04 20:12:24 +01:00
|
|
|
|
2007-01-15 00:20:43 +01:00
|
|
|
def _clear_buffers(self):
|
2007-01-15 15:53:41 +01:00
|
|
|
cur_buf_key = self._get_current_buffer_key()
|
2007-01-15 00:20:43 +01:00
|
|
|
for key in self._buffers.keys():
|
2007-01-15 15:53:41 +01:00
|
|
|
if key != cur_buf_key:
|
|
|
|
del self._buffers[key]
|
2007-01-15 00:20:43 +01:00
|
|
|
self._buffers = {}
|
2007-01-08 23:12:20 +01:00
|
|
|
|
2006-12-04 20:12:24 +01:00
|
|
|
def do_set_property(self, pspec, value):
|
|
|
|
if pspec.name == 'icon-name':
|
2007-01-15 00:20:43 +01:00
|
|
|
if self._icon_name != value and not self._cache:
|
|
|
|
self._clear_buffers()
|
2006-12-04 20:12:24 +01:00
|
|
|
self._icon_name = value
|
2007-01-27 01:36:31 +01:00
|
|
|
self._handle = None
|
2006-12-04 20:12:24 +01:00
|
|
|
self.emit_paint_needed(0, 0, -1, -1)
|
2007-02-23 17:08:37 +01:00
|
|
|
elif pspec.name == 'xo-color':
|
|
|
|
self.props.fill_color = color.HTMLColor(value.get_fill_color())
|
|
|
|
self.props.stroke_color = color.HTMLColor(value.get_stroke_color())
|
|
|
|
elif pspec.name == 'fill-color':
|
|
|
|
if self._fill_color != value:
|
|
|
|
if not self._cache:
|
|
|
|
self._clear_buffers()
|
|
|
|
self._fill_color = value
|
|
|
|
self._handle = None
|
|
|
|
self.emit_paint_needed(0, 0, -1, -1)
|
|
|
|
elif pspec.name == 'stroke-color':
|
|
|
|
if self._stroke_color != value:
|
|
|
|
if not self._cache:
|
|
|
|
self._clear_buffers()
|
|
|
|
self._stroke_color = value
|
|
|
|
self._handle = None
|
|
|
|
self.emit_paint_needed(0, 0, -1, -1)
|
2007-01-27 01:36:31 +01:00
|
|
|
elif pspec.name == 'scale':
|
|
|
|
if self._scale != value and not self._cache:
|
2007-01-15 00:20:43 +01:00
|
|
|
self._clear_buffers()
|
2007-01-27 01:36:31 +01:00
|
|
|
self._scale = value
|
2006-12-04 20:12:24 +01:00
|
|
|
self.emit_request_changed()
|
2007-01-15 00:20:43 +01:00
|
|
|
elif pspec.name == 'cache':
|
|
|
|
self._cache = value
|
2007-02-22 22:51:24 +01:00
|
|
|
elif pspec.name == 'tooltip':
|
|
|
|
self._tooltip = value
|
2007-02-28 19:53:06 +01:00
|
|
|
elif pspec.name == 'active':
|
|
|
|
if self._active != value:
|
|
|
|
if not self._cache:
|
|
|
|
self._clear_buffers()
|
|
|
|
self._active = value
|
|
|
|
self._handle = None
|
|
|
|
self.emit_paint_needed(0, 0, -1, -1)
|
|
|
|
|
|
|
|
def _choose_colors(self):
|
|
|
|
fill_color = None
|
|
|
|
stroke_color = None
|
|
|
|
if self._active:
|
|
|
|
if self._fill_color:
|
|
|
|
fill_color = self._fill_color.get_html()
|
|
|
|
if self._stroke_color:
|
|
|
|
stroke_color = self._stroke_color.get_html()
|
|
|
|
else:
|
|
|
|
stroke_color = color.ICON_STROKE_INACTIVE.get_html()
|
|
|
|
if self._fill_color:
|
|
|
|
fill_color = self._fill_color.get_html()
|
|
|
|
return [fill_color, stroke_color]
|
2006-12-04 20:12:24 +01:00
|
|
|
|
2007-01-27 01:36:31 +01:00
|
|
|
def _get_handle(self):
|
|
|
|
if not self._handle:
|
|
|
|
cache = CanvasIcon._cache
|
2007-02-23 17:08:37 +01:00
|
|
|
|
2007-02-28 19:53:06 +01:00
|
|
|
[fill_color, stroke_color] = self._choose_colors()
|
2007-02-23 17:08:37 +01:00
|
|
|
|
|
|
|
self._handle = cache.get_handle(self._icon_name, fill_color,
|
|
|
|
stroke_color)
|
2007-01-27 01:36:31 +01:00
|
|
|
return self._handle
|
|
|
|
|
2007-01-15 15:53:41 +01:00
|
|
|
def _get_current_buffer_key(self):
|
2007-02-28 19:53:06 +01:00
|
|
|
[fill_color, stroke_color] = self._choose_colors()
|
|
|
|
return (self._icon_name, fill_color, stroke_color, self._scale)
|
2007-01-15 15:53:41 +01:00
|
|
|
|
2006-12-04 20:12:24 +01:00
|
|
|
def do_get_property(self, pspec):
|
2007-01-27 01:36:31 +01:00
|
|
|
if pspec.name == 'scale':
|
|
|
|
return self._scale
|
2006-12-04 20:12:24 +01:00
|
|
|
elif pspec.name == 'icon-name':
|
|
|
|
return self._icon_name
|
2007-02-23 17:08:37 +01:00
|
|
|
elif pspec.name == 'fill-color':
|
|
|
|
return self._fill_color
|
|
|
|
elif pspec.name == 'stroke-color':
|
|
|
|
return self._stroke_color
|
2007-01-15 00:20:43 +01:00
|
|
|
elif pspec.name == 'cache':
|
|
|
|
return self._cache
|
2007-02-22 22:51:24 +01:00
|
|
|
elif pspec.name == 'tooltip':
|
|
|
|
return self._tooltip
|
2007-02-28 19:53:06 +01:00
|
|
|
elif pspec.name == 'active':
|
|
|
|
return self._active
|
2007-01-15 00:20:43 +01:00
|
|
|
|
2007-01-27 01:36:31 +01:00
|
|
|
def _get_icon_size(self):
|
|
|
|
handle = self._get_handle()
|
|
|
|
if handle:
|
|
|
|
dimensions = handle.get_dimension_data()
|
|
|
|
|
|
|
|
width = int(dimensions[0] * self._scale) + 1
|
|
|
|
height = int(dimensions[1] * self._scale) + 1
|
|
|
|
|
|
|
|
return [width, height]
|
|
|
|
|
|
|
|
return [0, 0]
|
|
|
|
|
2007-01-15 00:20:43 +01:00
|
|
|
def _get_buffer(self, cr, handle):
|
2007-01-15 15:53:41 +01:00
|
|
|
key = self._get_current_buffer_key()
|
2007-01-15 00:20:43 +01:00
|
|
|
buf = None
|
2007-01-27 01:36:31 +01:00
|
|
|
|
2007-01-15 00:20:43 +01:00
|
|
|
if self._buffers.has_key(key):
|
|
|
|
buf = self._buffers[key]
|
|
|
|
else:
|
2006-12-04 20:12:24 +01:00
|
|
|
target = cr.get_target()
|
|
|
|
|
2007-01-27 01:36:31 +01:00
|
|
|
[w, h] = self._get_icon_size()
|
|
|
|
buf = target.create_similar(cairo.CONTENT_COLOR_ALPHA, w, h)
|
2006-12-04 20:12:24 +01:00
|
|
|
|
2007-01-15 00:20:43 +01:00
|
|
|
ctx = cairo.Context(buf)
|
2007-01-27 01:36:31 +01:00
|
|
|
ctx.scale(self._scale, self._scale)
|
2006-12-04 20:12:24 +01:00
|
|
|
handle.render_cairo(ctx)
|
|
|
|
del ctx
|
2007-01-15 00:20:43 +01:00
|
|
|
self._buffers[key] = buf
|
2006-12-04 20:12:24 +01:00
|
|
|
|
2007-01-15 00:20:43 +01:00
|
|
|
return buf
|
2006-12-04 20:12:24 +01:00
|
|
|
|
|
|
|
def do_paint_below_children(self, cr, damaged_box):
|
2007-01-27 01:36:31 +01:00
|
|
|
handle = self._get_handle()
|
|
|
|
if handle == None:
|
|
|
|
return
|
2006-12-04 20:12:24 +01:00
|
|
|
|
2007-01-15 00:20:43 +01:00
|
|
|
buf = self._get_buffer(cr, handle)
|
2006-12-04 20:12:24 +01:00
|
|
|
|
|
|
|
[width, height] = self.get_allocation()
|
2007-01-27 01:36:31 +01:00
|
|
|
[icon_width, icon_height] = self._get_icon_size()
|
|
|
|
x = (width - icon_width) / 2
|
|
|
|
y = (height - icon_height) / 2
|
2006-12-04 20:12:24 +01:00
|
|
|
|
|
|
|
cr.set_source_surface(buf, x, y)
|
|
|
|
cr.paint()
|
|
|
|
|
2007-01-27 01:36:31 +01:00
|
|
|
def do_get_content_width_request(self):
|
|
|
|
[width, height] = self._get_icon_size()
|
2007-03-14 03:19:24 +01:00
|
|
|
return (width, width)
|
2007-01-26 17:28:45 +01:00
|
|
|
|
2007-01-27 01:36:31 +01:00
|
|
|
def do_get_content_height_request(self, for_width):
|
|
|
|
[width, height] = self._get_icon_size()
|
2007-03-14 03:19:24 +01:00
|
|
|
return (height, height)
|
2007-01-26 17:28:45 +01:00
|
|
|
|
2007-03-09 13:12:52 +01:00
|
|
|
def do_button_press_event(self, event):
|
2007-03-09 14:57:54 +01:00
|
|
|
self.emit_activated()
|
2007-03-09 13:12:52 +01:00
|
|
|
return True
|
2007-02-21 14:13:52 +01:00
|
|
|
|
|
|
|
def get_popup(self):
|
2007-02-22 22:51:24 +01:00
|
|
|
if self._tooltip:
|
|
|
|
tooltip_popup = Popup()
|
2007-02-25 15:05:56 +01:00
|
|
|
text = hippo.CanvasText(text=self._tooltip)
|
|
|
|
text.props.background_color = color.MENU_BACKGROUND.get_int()
|
|
|
|
text.props.color = color.LABEL_TEXT.get_int()
|
|
|
|
text.props.font_desc = font.DEFAULT.get_pango_desc()
|
|
|
|
text.props.padding = units.points_to_pixels(5)
|
|
|
|
tooltip_popup.append(text)
|
2007-02-22 22:51:24 +01:00
|
|
|
|
|
|
|
return tooltip_popup
|
|
|
|
else:
|
|
|
|
return None
|
2007-02-21 14:13:52 +01:00
|
|
|
|
|
|
|
def get_popup_context(self):
|
|
|
|
return None
|
|
|
|
|
2007-03-12 17:47:09 +01:00
|
|
|
def show_popup(self):
|
2007-03-16 12:20:03 +01:00
|
|
|
if not self._popup:
|
|
|
|
self._popup = self.get_popup()
|
|
|
|
if not self._popup:
|
|
|
|
return
|
2007-02-21 14:13:52 +01:00
|
|
|
|
|
|
|
popup_context = self.get_popup_context()
|
|
|
|
|
|
|
|
[x, y] = [None, None]
|
|
|
|
if popup_context:
|
|
|
|
try:
|
2007-03-16 12:20:03 +01:00
|
|
|
[x, y] = popup_context.get_position(self, self._popup)
|
2007-02-21 14:13:52 +01:00
|
|
|
except NotImplementedError:
|
|
|
|
pass
|
|
|
|
|
|
|
|
if [x, y] == [None, None]:
|
|
|
|
context = self.get_context()
|
2007-02-21 21:29:08 +01:00
|
|
|
[x, y] = context.translate_to_screen(self)
|
2007-02-21 14:13:52 +01:00
|
|
|
|
|
|
|
# TODO: Any better place to do this?
|
2007-03-14 03:19:24 +01:00
|
|
|
[min_width, natural_width] = self.get_width_request()
|
2007-03-16 12:20:03 +01:00
|
|
|
[pop_min_width, pop_natural_width] = self._popup.get_width_request()
|
|
|
|
self._popup.props.box_width = max(pop_min_width, min_width)
|
2007-02-21 14:13:52 +01:00
|
|
|
|
|
|
|
[width, height] = self.get_allocation()
|
|
|
|
y += height
|
|
|
|
position = [x, y]
|
|
|
|
|
2007-03-16 12:20:03 +01:00
|
|
|
self._popup.popup(x, y)
|
|
|
|
self._popup.connect('motion-notify-event',
|
|
|
|
self.popup_motion_notify_event_cb)
|
|
|
|
self._popup.connect('action-completed',
|
|
|
|
self._popup_action_completed_cb)
|
2007-02-21 14:13:52 +01:00
|
|
|
|
|
|
|
if popup_context:
|
2007-03-16 12:20:03 +01:00
|
|
|
popup_context.popped_up(self._popup)
|
2007-02-21 14:13:52 +01:00
|
|
|
|
2007-03-12 17:47:09 +01:00
|
|
|
def hide_popup(self):
|
2007-02-21 14:13:52 +01:00
|
|
|
if self._popup:
|
|
|
|
self._popup.popdown()
|
|
|
|
|
|
|
|
popup_context = self.get_popup_context()
|
|
|
|
if popup_context:
|
|
|
|
popup_context.popped_down(self._popup)
|
|
|
|
|
|
|
|
self._popup = None
|
|
|
|
|
2007-03-12 17:47:09 +01:00
|
|
|
def _enter(self):
|
|
|
|
self._popup_anim = animator.Animator(0.2, 10)
|
|
|
|
self._popup_anim.add(_PopupAnimation(self))
|
|
|
|
self._popup_anim.start()
|
|
|
|
|
|
|
|
self.prelight(enter=True)
|
|
|
|
|
|
|
|
def _leave(self):
|
2007-03-28 19:34:11 +02:00
|
|
|
# FIXME: This is a hack for taking out the popdown delay for tooltips and
|
|
|
|
# increasing the rest of rollovers. We need a better way for specifiying
|
|
|
|
# different behaviors for the different kinds of popups.
|
|
|
|
if type(self._popup) == Popup:
|
|
|
|
self.hide_popup()
|
|
|
|
else:
|
|
|
|
self._popup_anim = animator.Animator(0.5, 10)
|
|
|
|
self._popup_anim.add(_PopdownAnimation(self))
|
|
|
|
self._popup_anim.start()
|
2007-03-16 14:29:39 +01:00
|
|
|
|
2007-03-12 17:47:09 +01:00
|
|
|
self.prelight(enter=False)
|
|
|
|
|
|
|
|
def _enter_or_leave_cb(self):
|
|
|
|
if self._popup_anim:
|
|
|
|
self._popup_anim.stop()
|
|
|
|
|
|
|
|
if self._hover_icon or self._hover_popup:
|
|
|
|
self._enter()
|
|
|
|
else:
|
|
|
|
self._leave()
|
|
|
|
|
|
|
|
self._enter_or_leave_sid = 0
|
|
|
|
|
|
|
|
return False
|
|
|
|
|
|
|
|
def _schedule_enter_or_leave(self):
|
|
|
|
if self._enter_or_leave_sid == 0:
|
|
|
|
sid = gobject.idle_add(self._enter_or_leave_cb)
|
|
|
|
self._enter_or_leave_sid = sid
|
2007-02-21 14:13:52 +01:00
|
|
|
|
|
|
|
def _motion_notify_event_cb(self, button, event):
|
|
|
|
if event.detail == hippo.MOTION_DETAIL_ENTER:
|
2007-03-12 17:47:09 +01:00
|
|
|
self._hover_icon = True
|
2007-02-21 14:13:52 +01:00
|
|
|
elif event.detail == hippo.MOTION_DETAIL_LEAVE:
|
2007-03-12 17:47:09 +01:00
|
|
|
self._hover_icon = False
|
|
|
|
|
|
|
|
self._schedule_enter_or_leave()
|
|
|
|
|
2007-02-23 20:39:52 +01:00
|
|
|
return False
|
2007-02-21 14:13:52 +01:00
|
|
|
|
2007-02-23 20:39:52 +01:00
|
|
|
def popup_motion_notify_event_cb(self, popup, event):
|
2007-02-21 14:13:52 +01:00
|
|
|
if event.detail == hippo.MOTION_DETAIL_ENTER:
|
|
|
|
self._hover_popup = True
|
|
|
|
elif event.detail == hippo.MOTION_DETAIL_LEAVE:
|
|
|
|
self._hover_popup = False
|
2007-03-12 17:47:09 +01:00
|
|
|
|
|
|
|
self._schedule_enter_or_leave()
|
|
|
|
|
2007-02-23 20:39:52 +01:00
|
|
|
return False
|
2007-02-21 14:13:52 +01:00
|
|
|
|
|
|
|
def _popup_action_completed_cb(self, popup):
|
2007-03-12 17:47:09 +01:00
|
|
|
self.hide_popup()
|
2007-02-23 20:39:52 +01:00
|
|
|
|
|
|
|
def prelight(self, enter):
|
|
|
|
"""
|
|
|
|
Override this method for adding prelighting behavior.
|
|
|
|
"""
|
|
|
|
pass
|
2007-02-28 15:42:41 +01:00
|
|
|
|
|
|
|
def get_icon_state(base_name, perc):
|
|
|
|
step = 5
|
|
|
|
strength = round(perc / step) * step
|
|
|
|
icon_theme = gtk.icon_theme_get_default()
|
|
|
|
|
|
|
|
while strength <= 100:
|
|
|
|
icon_name = '%s-%03d' % (base_name, strength)
|
|
|
|
if icon_theme.has_icon(icon_name):
|
|
|
|
return 'theme:' + icon_name
|
|
|
|
|
|
|
|
strength = strength + step
|