2007-05-24 19:37:48 +02:00
|
|
|
# Copyright (C) 2007, Eduardo Silva (edsiper@gmail.com)
|
|
|
|
#
|
2007-06-14 22:04:25 +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.
|
2007-05-24 19:37:48 +02:00
|
|
|
#
|
2007-06-14 22:04:25 +02:00
|
|
|
# This library is distributed in the hope that it will be useful,
|
2007-05-24 19:37:48 +02:00
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2007-06-14 22:04:25 +02:00
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
# Lesser General Public License for more details.
|
2007-05-24 19:37:48 +02:00
|
|
|
#
|
2007-06-14 22:04:25 +02:00
|
|
|
# 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-05-24 19:37:48 +02:00
|
|
|
|
|
|
|
import gtk
|
|
|
|
import gobject
|
2007-06-06 23:21:33 +02:00
|
|
|
import time
|
2007-06-19 22:02:25 +02:00
|
|
|
import hippo
|
2007-05-24 19:37:48 +02:00
|
|
|
|
2007-06-25 14:31:43 +02:00
|
|
|
from sugar.graphics import animator
|
2007-06-26 14:55:16 +02:00
|
|
|
from sugar import _sugarext
|
2007-06-25 14:31:43 +02:00
|
|
|
|
2007-06-01 06:08:24 +02:00
|
|
|
ALIGNMENT_AUTOMATIC = 0
|
|
|
|
ALIGNMENT_BOTTOM_LEFT = 1
|
|
|
|
ALIGNMENT_BOTTOM_RIGHT = 2
|
|
|
|
ALIGNMENT_LEFT_BOTTOM = 3
|
|
|
|
ALIGNMENT_LEFT_TOP = 4
|
|
|
|
ALIGNMENT_RIGHT_BOTTOM = 5
|
|
|
|
ALIGNMENT_RIGHT_TOP = 6
|
|
|
|
ALIGNMENT_TOP_LEFT = 7
|
|
|
|
ALIGNMENT_TOP_RIGHT = 8
|
2007-05-24 19:37:48 +02:00
|
|
|
|
2007-06-26 14:55:16 +02:00
|
|
|
class Palette(gobject.GObject):
|
2007-05-24 19:37:48 +02:00
|
|
|
__gtype_name__ = 'SugarPalette'
|
|
|
|
|
|
|
|
__gproperties__ = {
|
2007-06-25 11:39:51 +02:00
|
|
|
'invoker' : (object, None, None,
|
|
|
|
gobject.PARAM_READWRITE),
|
|
|
|
'alignment' : (gobject.TYPE_INT, None, None, 0, 8,
|
|
|
|
ALIGNMENT_AUTOMATIC,
|
|
|
|
gobject.PARAM_READWRITE)
|
2007-05-24 19:37:48 +02:00
|
|
|
}
|
|
|
|
|
2007-06-26 14:55:16 +02:00
|
|
|
def __init__(self, label, accel_path=None):
|
|
|
|
gobject.GObject.__init__(self)
|
2007-05-24 19:37:48 +02:00
|
|
|
|
2007-06-01 06:08:24 +02:00
|
|
|
self._alignment = ALIGNMENT_AUTOMATIC
|
|
|
|
|
2007-06-26 14:28:31 +02:00
|
|
|
self._popup_anim = animator.Animator(0.3, 10)
|
2007-06-25 14:31:43 +02:00
|
|
|
self._popup_anim.add(_PopupAnimation(self))
|
|
|
|
|
|
|
|
self._popdown_anim = animator.Animator(0.6, 10)
|
|
|
|
self._popdown_anim.add(_PopdownAnimation(self))
|
|
|
|
|
2007-06-26 14:55:16 +02:00
|
|
|
self._menu = _sugarext.Menu()
|
2007-05-24 19:37:48 +02:00
|
|
|
|
2007-06-26 14:55:16 +02:00
|
|
|
primary = _PrimaryMenuItem(label, accel_path)
|
|
|
|
self._menu.append(primary)
|
|
|
|
primary.show()
|
2007-06-02 06:33:41 +02:00
|
|
|
|
2007-06-26 14:55:16 +02:00
|
|
|
self._separator = gtk.SeparatorMenuItem()
|
|
|
|
self._menu.append(self._separator)
|
2007-06-06 06:51:01 +02:00
|
|
|
|
2007-06-26 14:55:16 +02:00
|
|
|
self._content = _ContentMenuItem()
|
|
|
|
self._menu.append(self._content)
|
2007-06-25 11:39:51 +02:00
|
|
|
|
2007-06-26 14:55:16 +02:00
|
|
|
self._button_bar = _ButtonBarMenuItem()
|
|
|
|
self._menu.append(self._button_bar)
|
2007-06-06 04:43:42 +02:00
|
|
|
|
2007-06-26 14:55:16 +02:00
|
|
|
self._menu.connect('enter-notify-event',
|
|
|
|
self._enter_notify_event_cb)
|
|
|
|
self._menu.connect('leave-notify-event',
|
|
|
|
self._leave_notify_event_cb)
|
|
|
|
self._menu.connect('button-press-event',
|
|
|
|
self._button_press_event_cb)
|
2007-05-24 19:37:48 +02:00
|
|
|
|
2007-06-26 14:55:16 +02:00
|
|
|
def append_menu_item(self, item):
|
|
|
|
self._separator.show()
|
|
|
|
self._menu.insert(item, len(self._menu.get_children()) - 2)
|
|
|
|
|
2007-06-26 18:19:26 +02:00
|
|
|
def insert_menu_item(self, item, index=-1):
|
|
|
|
self._separator.show()
|
|
|
|
if index < 0:
|
|
|
|
self._menu.insert(item, len(self._menu.get_children()) - 2)
|
|
|
|
else:
|
|
|
|
self._menu.insert(item, index + 2)
|
|
|
|
|
|
|
|
def remove_menu_item(self, index):
|
|
|
|
if index > len(self._menu.get_children()) - 4:
|
|
|
|
raise ValueError('index %i out of range' % index)
|
|
|
|
self._menu.remove(self._menu.get_children()[index + 2])
|
|
|
|
if len(self._menu.get_children()) == 0:
|
|
|
|
self._separator.hide()
|
|
|
|
|
|
|
|
def menu_item_count(self):
|
|
|
|
return len(self._menu.get_children()) - 4
|
|
|
|
|
2007-06-26 14:55:16 +02:00
|
|
|
def set_content(self, widget):
|
|
|
|
self._content.set_widget(widget)
|
|
|
|
self._content.show()
|
2007-05-24 19:37:48 +02:00
|
|
|
|
2007-06-26 14:55:16 +02:00
|
|
|
def append_button(self, button):
|
|
|
|
self._button_bar.append_button(button)
|
|
|
|
self._button_bar.show()
|
2007-06-06 06:51:01 +02:00
|
|
|
|
2007-06-01 06:08:24 +02:00
|
|
|
def do_set_property(self, pspec, value):
|
2007-06-19 22:02:25 +02:00
|
|
|
if pspec.name == 'invoker':
|
|
|
|
self._invoker = value
|
2007-06-25 14:31:43 +02:00
|
|
|
self._invoker.add_listener(self)
|
2007-05-24 19:37:48 +02:00
|
|
|
elif pspec.name == 'alignment':
|
|
|
|
self._alignment = value
|
|
|
|
else:
|
|
|
|
raise AssertionError
|
|
|
|
|
2007-06-26 14:55:16 +02:00
|
|
|
def _get_position(self):
|
2007-06-01 06:08:24 +02:00
|
|
|
if self._alignment == ALIGNMENT_AUTOMATIC:
|
2007-06-26 14:55:16 +02:00
|
|
|
x, y = self._try_position(ALIGNMENT_BOTTOM_LEFT)
|
|
|
|
if x == -1:
|
|
|
|
x, y = self._try_position(ALIGNMENT_BOTTOM_RIGHT)
|
|
|
|
if x == -1:
|
|
|
|
x, y = self._try_position(ALIGNMENT_LEFT_BOTTOM)
|
|
|
|
if x == -1:
|
|
|
|
x, y = self._try_position(ALIGNMENT_LEFT_TOP)
|
|
|
|
if x == -1:
|
|
|
|
x, y = self._try_position(ALIGNMENT_RIGHT_BOTTOM)
|
|
|
|
if x == -1:
|
|
|
|
x, y = self._try_position(ALIGNMENT_RIGHT_TOP)
|
|
|
|
if x == -1:
|
|
|
|
x, y = self._try_position(ALIGNMENT_LEFT_BOTTOM)
|
|
|
|
if x == -1:
|
|
|
|
x, y = self._try_position(ALIGNMENT_LEFT_BOTTOM)
|
2007-06-01 06:08:24 +02:00
|
|
|
else:
|
2007-06-26 14:55:16 +02:00
|
|
|
x, y = self._position_for_alignment(self._alignment)
|
|
|
|
|
|
|
|
return (x, y)
|
2007-05-24 19:37:48 +02:00
|
|
|
|
2007-06-01 06:08:24 +02:00
|
|
|
def _try_position(self, alignment):
|
2007-06-26 14:55:16 +02:00
|
|
|
x, y = self._position_for_alignment(alignment)
|
|
|
|
allocation = self._menu.get_allocation()
|
|
|
|
|
|
|
|
if (x + allocation.width > gtk.gdk.screen_width()) or \
|
|
|
|
(y + allocation.height > gtk.gdk.screen_height()):
|
|
|
|
return (-1, -1)
|
2007-06-01 06:08:24 +02:00
|
|
|
else:
|
2007-06-26 14:55:16 +02:00
|
|
|
return (x, y)
|
2007-06-01 06:08:24 +02:00
|
|
|
|
2007-06-26 14:55:16 +02:00
|
|
|
def _position_for_alignment(self, alignment):
|
2007-06-19 22:02:25 +02:00
|
|
|
# Invoker: x, y, width and height
|
|
|
|
inv_rect = self._invoker.get_rect()
|
2007-06-26 14:55:16 +02:00
|
|
|
palette_rect = self._menu.get_allocation()
|
2007-05-24 19:37:48 +02:00
|
|
|
|
2007-06-01 06:08:24 +02:00
|
|
|
if alignment == ALIGNMENT_BOTTOM_LEFT:
|
2007-06-19 22:02:25 +02:00
|
|
|
move_x = inv_rect.x
|
|
|
|
move_y = inv_rect.y + inv_rect.height
|
2007-06-01 06:08:24 +02:00
|
|
|
elif alignment == ALIGNMENT_BOTTOM_RIGHT:
|
2007-06-27 11:41:41 +02:00
|
|
|
move_x = (inv_rect.x + inv_rect.width) - palette_rect.width
|
2007-06-19 22:02:25 +02:00
|
|
|
move_y = inv_rect.y + inv_rect.height
|
2007-06-01 06:08:24 +02:00
|
|
|
elif alignment == ALIGNMENT_LEFT_BOTTOM:
|
2007-06-27 11:41:41 +02:00
|
|
|
move_x = inv_rect.x - palette_rect.width
|
2007-06-19 22:02:25 +02:00
|
|
|
move_y = inv_rect.y
|
2007-06-01 06:08:24 +02:00
|
|
|
elif alignment == ALIGNMENT_LEFT_TOP:
|
2007-06-27 11:41:41 +02:00
|
|
|
move_x = inv_rect.x - palette_rect.width
|
2007-06-26 14:55:16 +02:00
|
|
|
move_y = (inv_rect.y + inv_rect.height) - palette_rect.height
|
2007-06-01 06:08:24 +02:00
|
|
|
elif alignment == ALIGNMENT_RIGHT_BOTTOM:
|
2007-06-19 22:02:25 +02:00
|
|
|
move_x = inv_rect.x + inv_rect.width
|
|
|
|
move_y = inv_rect.y
|
2007-06-01 06:08:24 +02:00
|
|
|
elif alignment == ALIGNMENT_RIGHT_TOP:
|
2007-06-19 22:02:25 +02:00
|
|
|
move_x = inv_rect.x + inv_rect.width
|
2007-06-26 14:55:16 +02:00
|
|
|
move_y = (inv_rect.y + inv_rect.height) - palette_rect.height
|
2007-06-01 06:08:24 +02:00
|
|
|
elif alignment == ALIGNMENT_TOP_LEFT:
|
2007-06-19 22:02:25 +02:00
|
|
|
move_x = inv_rect.x
|
2007-06-26 14:55:16 +02:00
|
|
|
move_y = inv_rect.y - palette_rect.height
|
2007-06-01 06:08:24 +02:00
|
|
|
elif alignment == ALIGNMENT_TOP_RIGHT:
|
2007-06-27 11:41:41 +02:00
|
|
|
move_x = (inv_rect.x + inv_rect.width) - palette_rect.width
|
2007-06-26 14:55:16 +02:00
|
|
|
move_y = inv_rect.y - palette_rect.height
|
2007-05-24 19:37:48 +02:00
|
|
|
|
2007-06-01 06:08:24 +02:00
|
|
|
return move_x, move_y
|
2007-05-24 19:37:48 +02:00
|
|
|
|
2007-06-26 14:55:16 +02:00
|
|
|
def _show(self):
|
|
|
|
x, y = self._get_position()
|
|
|
|
self._menu.popup(x, y)
|
2007-05-24 19:37:48 +02:00
|
|
|
|
2007-06-26 14:55:16 +02:00
|
|
|
def _hide(self):
|
|
|
|
self._menu.popdown()
|
2007-05-24 19:37:48 +02:00
|
|
|
|
2007-05-30 19:29:29 +02:00
|
|
|
def popup(self):
|
2007-06-25 14:31:43 +02:00
|
|
|
self._popdown_anim.stop()
|
|
|
|
self._popup_anim.start()
|
2007-06-19 22:02:25 +02:00
|
|
|
|
|
|
|
def popdown(self):
|
2007-06-25 14:31:43 +02:00
|
|
|
self._popup_anim.stop()
|
|
|
|
self._popdown_anim.start()
|
2007-05-29 18:27:54 +02:00
|
|
|
|
2007-06-25 14:31:43 +02:00
|
|
|
def invoker_mouse_enter(self):
|
|
|
|
self.popup()
|
2007-05-30 19:29:29 +02:00
|
|
|
|
2007-06-25 14:31:43 +02:00
|
|
|
def invoker_mouse_leave(self):
|
|
|
|
self.popdown()
|
2007-05-30 19:29:29 +02:00
|
|
|
|
2007-06-25 14:31:43 +02:00
|
|
|
def _enter_notify_event_cb(self, widget, event):
|
|
|
|
if event.detail == gtk.gdk.NOTIFY_NONLINEAR:
|
|
|
|
self._popdown_anim.stop()
|
2007-05-30 19:29:29 +02:00
|
|
|
|
2007-06-25 14:31:43 +02:00
|
|
|
def _leave_notify_event_cb(self, widget, event):
|
|
|
|
if event.detail == gtk.gdk.NOTIFY_NONLINEAR:
|
|
|
|
self.popdown()
|
2007-05-30 19:29:29 +02:00
|
|
|
|
2007-06-25 14:31:43 +02:00
|
|
|
def _button_press_event_cb(self, widget, event):
|
|
|
|
pass
|
|
|
|
|
2007-06-26 14:55:16 +02:00
|
|
|
class _PrimaryMenuItem(gtk.MenuItem):
|
|
|
|
def __init__(self, label, accel_path):
|
|
|
|
gtk.MenuItem.__init__(self)
|
|
|
|
|
|
|
|
label = gtk.AccelLabel(label)
|
|
|
|
label.set_accel_widget(self)
|
|
|
|
|
|
|
|
if accel_path:
|
|
|
|
self.set_accel_path(accel_path)
|
|
|
|
label.set_alignment(0.0, 0.5)
|
|
|
|
|
|
|
|
self.add(label)
|
|
|
|
label.show()
|
|
|
|
|
|
|
|
class _ContentMenuItem(gtk.MenuItem):
|
|
|
|
def __init__(self):
|
|
|
|
gtk.MenuItem.__init__(self)
|
|
|
|
|
|
|
|
def set_widget(self, widget):
|
|
|
|
if self.child:
|
|
|
|
self.remove(self.child)
|
|
|
|
self.add(widget)
|
|
|
|
|
|
|
|
class _ButtonBarMenuItem(gtk.MenuItem):
|
|
|
|
def __init__(self):
|
|
|
|
gtk.MenuItem.__init__(self)
|
|
|
|
|
|
|
|
self._hbar = gtk.HButtonBox()
|
|
|
|
self.add(self._hbar)
|
|
|
|
self._hbar.show()
|
|
|
|
|
|
|
|
def append_button(self, button):
|
|
|
|
self._hbar.pack_start(button)
|
|
|
|
|
2007-06-25 14:31:43 +02:00
|
|
|
class _PopupAnimation(animator.Animation):
|
|
|
|
def __init__(self, palette):
|
|
|
|
animator.Animation.__init__(self, 0.0, 1.0)
|
|
|
|
self._palette = palette
|
|
|
|
|
|
|
|
def next_frame(self, current):
|
|
|
|
if current == 1.0:
|
2007-06-26 14:55:16 +02:00
|
|
|
self._palette._show()
|
2007-05-30 19:29:29 +02:00
|
|
|
|
2007-06-25 14:31:43 +02:00
|
|
|
class _PopdownAnimation(animator.Animation):
|
|
|
|
def __init__(self, palette):
|
|
|
|
animator.Animation.__init__(self, 0.0, 1.0)
|
|
|
|
self._palette = palette
|
2007-06-19 22:02:25 +02:00
|
|
|
|
2007-06-25 14:31:43 +02:00
|
|
|
def next_frame(self, current):
|
|
|
|
if current == 1.0:
|
2007-06-26 14:55:16 +02:00
|
|
|
self._palette._hide()
|
2007-05-29 18:27:54 +02:00
|
|
|
|
2007-06-25 14:31:43 +02:00
|
|
|
class Invoker(object):
|
|
|
|
def __init__(self):
|
|
|
|
self._listeners = []
|
2007-05-29 18:27:54 +02:00
|
|
|
|
2007-06-25 14:31:43 +02:00
|
|
|
def add_listener(self, listener):
|
|
|
|
self._listeners.append(listener)
|
2007-06-06 04:43:42 +02:00
|
|
|
|
2007-06-25 14:31:43 +02:00
|
|
|
def notify_mouse_enter(self):
|
|
|
|
for listener in self._listeners:
|
|
|
|
listener.invoker_mouse_enter()
|
2007-06-19 22:02:25 +02:00
|
|
|
|
2007-06-25 14:31:43 +02:00
|
|
|
def notify_mouse_leave(self):
|
|
|
|
for listener in self._listeners:
|
|
|
|
listener.invoker_mouse_leave()
|
|
|
|
|
|
|
|
class WidgetInvoker(Invoker):
|
|
|
|
def __init__(self, widget):
|
|
|
|
Invoker.__init__(self)
|
|
|
|
self._widget = widget
|
|
|
|
|
|
|
|
widget.connect('enter-notify-event', self._enter_notify_event_cb)
|
|
|
|
widget.connect('leave-notify-event', self._leave_notify_event_cb)
|
2007-06-19 22:02:25 +02:00
|
|
|
|
|
|
|
def get_rect(self):
|
2007-06-25 14:31:43 +02:00
|
|
|
win_x, win_y = self._widget.window.get_origin()
|
|
|
|
rectangle = self._widget.get_allocation()
|
2007-06-19 22:02:25 +02:00
|
|
|
|
|
|
|
x = win_x + rectangle.x
|
|
|
|
y = win_y + rectangle.y
|
|
|
|
width = rectangle.width
|
|
|
|
height = rectangle.height
|
|
|
|
|
|
|
|
return gtk.gdk.Rectangle(x, y, width, height)
|
|
|
|
|
2007-06-25 14:31:43 +02:00
|
|
|
def _enter_notify_event_cb(self, widget, event):
|
|
|
|
self.notify_mouse_enter()
|
2007-06-19 22:02:25 +02:00
|
|
|
|
2007-06-25 14:31:43 +02:00
|
|
|
def _leave_notify_event_cb(self, widget, event):
|
|
|
|
self.notify_mouse_leave()
|
2007-06-19 22:02:25 +02:00
|
|
|
|
2007-06-25 14:31:43 +02:00
|
|
|
class CanvasInvoker(Invoker):
|
|
|
|
def __init__(self, item):
|
|
|
|
Invoker.__init__(self)
|
|
|
|
self._item = item
|
2007-06-19 22:02:25 +02:00
|
|
|
|
2007-06-25 14:31:43 +02:00
|
|
|
item.connect('motion-notify-event',
|
|
|
|
self._motion_notify_event_cb)
|
2007-06-19 22:02:25 +02:00
|
|
|
|
|
|
|
def get_rect(self):
|
2007-06-25 14:31:43 +02:00
|
|
|
context = self._item.get_context()
|
2007-06-26 14:28:31 +02:00
|
|
|
if context:
|
|
|
|
x, y = context.translate_to_screen(self._item)
|
|
|
|
|
2007-06-25 14:31:43 +02:00
|
|
|
width, height = self._item.get_allocation()
|
2007-06-19 22:02:25 +02:00
|
|
|
|
|
|
|
return gtk.gdk.Rectangle(x, y, width, height)
|
|
|
|
|
2007-06-25 14:31:43 +02:00
|
|
|
def _motion_notify_event_cb(self, button, event):
|
|
|
|
if event.detail == hippo.MOTION_DETAIL_ENTER:
|
|
|
|
self.notify_mouse_enter()
|
|
|
|
elif event.detail == hippo.MOTION_DETAIL_LEAVE:
|
|
|
|
self.notify_mouse_leave()
|
|
|
|
|
|
|
|
return False
|