2007-07-06 14:36:59 +02:00
|
|
|
# Copyright (C) 2007, Eduardo Silva <edsiper@gmail.com>
|
2007-05-24 19:37:48 +02:00
|
|
|
#
|
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
|
|
|
|
2007-07-01 11:05:14 +02:00
|
|
|
import logging
|
|
|
|
|
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-07-06 14:36:59 +02:00
|
|
|
from sugar.graphics import palettegroup
|
2007-06-25 14:31:43 +02:00
|
|
|
from sugar.graphics import animator
|
2007-07-18 20:15:54 +02:00
|
|
|
from sugar.graphics import style
|
2007-07-27 20:15:31 +02:00
|
|
|
from sugar import _sugaruiext
|
2007-06-25 14:31:43 +02:00
|
|
|
|
2007-06-29 14:45:04 +02:00
|
|
|
_BOTTOM_LEFT = 0
|
|
|
|
_BOTTOM_RIGHT = 1
|
|
|
|
_LEFT_BOTTOM = 2
|
|
|
|
_LEFT_TOP = 3
|
|
|
|
_RIGHT_BOTTOM = 4
|
|
|
|
_RIGHT_TOP = 5
|
|
|
|
_TOP_LEFT = 6
|
|
|
|
_TOP_RIGHT = 7
|
2007-05-24 19:37:48 +02:00
|
|
|
|
2007-08-10 18:29:28 +02:00
|
|
|
|
|
|
|
# Helper function to find the gap position and size of widget a
|
|
|
|
def _calculate_gap(a, b):
|
|
|
|
# Test for each side if the palette and invoker are
|
|
|
|
# adjacent to each other.
|
|
|
|
gap = True
|
|
|
|
|
|
|
|
if a.y + a.height == b.y:
|
|
|
|
gap_side = gtk.POS_BOTTOM
|
|
|
|
elif a.x + a.width == b.x:
|
|
|
|
gap_side = gtk.POS_RIGHT
|
|
|
|
elif a.x == b.x + b.width:
|
|
|
|
gap_side = gtk.POS_LEFT
|
|
|
|
elif a.y == b.y + b.height:
|
|
|
|
gap_side = gtk.POS_TOP
|
|
|
|
else:
|
|
|
|
gap = False
|
|
|
|
|
|
|
|
if gap:
|
|
|
|
if gap_side == gtk.POS_BOTTOM or gap_side == gtk.POS_TOP:
|
2007-08-10 23:39:12 +02:00
|
|
|
gap_start = min(a.width, max(0, b.x - a.x))
|
2007-08-10 18:29:28 +02:00
|
|
|
gap_size = max(0, min(a.width,
|
|
|
|
(b.x + b.width) - a.x) - gap_start)
|
|
|
|
elif gap_side == gtk.POS_RIGHT or gap_side == gtk.POS_LEFT:
|
|
|
|
gap_start = min(a.height, max(0, b.y - a.y))
|
|
|
|
gap_size = max(0, min(a.height,
|
|
|
|
(b.y + b.height) - a.y) - gap_start)
|
|
|
|
|
|
|
|
if gap and gap_size > 0:
|
|
|
|
return (gap_side, gap_start, gap_size)
|
|
|
|
else:
|
|
|
|
return False
|
|
|
|
|
2007-08-08 03:07:00 +02:00
|
|
|
class Palette(gtk.Window):
|
2007-07-18 16:59:47 +02:00
|
|
|
DEFAULT = 0
|
|
|
|
AT_CURSOR = 1
|
|
|
|
AROUND = 2
|
|
|
|
BOTTOM = 3
|
|
|
|
LEFT = 4
|
|
|
|
RIGHT = 5
|
|
|
|
TOP = 6
|
2007-06-29 14:45:04 +02:00
|
|
|
|
2007-07-24 15:53:35 +02:00
|
|
|
_PRIMARY = 0
|
|
|
|
_SECONDARY = 1
|
|
|
|
|
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),
|
2007-07-19 15:38:50 +02:00
|
|
|
'position' : (gobject.TYPE_INT, None, None, 0, 6,
|
2007-08-10 18:29:28 +02:00
|
|
|
0, gobject.PARAM_READWRITE),
|
|
|
|
'draw-gap' : (bool, None, None, False,
|
|
|
|
gobject.PARAM_READWRITE)
|
2007-05-24 19:37:48 +02:00
|
|
|
}
|
|
|
|
|
2007-07-06 14:36:59 +02:00
|
|
|
__gsignals__ = {
|
|
|
|
'popup' : (gobject.SIGNAL_RUN_FIRST,
|
|
|
|
gobject.TYPE_NONE, ([])),
|
|
|
|
'popdown' : (gobject.SIGNAL_RUN_FIRST,
|
|
|
|
gobject.TYPE_NONE, ([]))
|
|
|
|
}
|
|
|
|
|
2007-06-26 14:55:16 +02:00
|
|
|
def __init__(self, label, accel_path=None):
|
2007-08-08 03:07:00 +02:00
|
|
|
gtk.Window.__init__(self)
|
|
|
|
|
|
|
|
self.set_decorated(False)
|
|
|
|
self.set_resizable(False)
|
|
|
|
self.connect('realize', self._realize_cb)
|
2007-05-24 19:37:48 +02:00
|
|
|
|
2007-07-24 16:15:13 +02:00
|
|
|
self._full_request = [0, 0]
|
2007-07-25 16:01:07 +02:00
|
|
|
self._cursor_x = 0
|
|
|
|
self._cursor_y = 0
|
2007-08-08 03:07:00 +02:00
|
|
|
self._state = self._PRIMARY
|
2007-07-06 16:51:18 +02:00
|
|
|
self._invoker = None
|
2007-07-06 14:36:59 +02:00
|
|
|
self._group_id = None
|
|
|
|
self._up = False
|
2007-07-18 16:59:47 +02:00
|
|
|
self._position = self.DEFAULT
|
2007-08-10 18:29:28 +02:00
|
|
|
self._draw_gap = False
|
2007-07-05 14:16:36 +02:00
|
|
|
self._palette_popup_sid = None
|
2007-06-01 06:08:24 +02:00
|
|
|
|
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))
|
|
|
|
|
2007-07-18 16:59:47 +02:00
|
|
|
self._secondary_anim = animator.Animator(1.0, 10)
|
2007-07-02 12:05:42 +02:00
|
|
|
self._secondary_anim.add(_SecondaryAnimation(self))
|
|
|
|
|
2007-06-25 14:31:43 +02:00
|
|
|
self._popdown_anim = animator.Animator(0.6, 10)
|
|
|
|
self._popdown_anim.add(_PopdownAnimation(self))
|
|
|
|
|
2007-08-08 03:07:00 +02:00
|
|
|
vbox = gtk.VBox()
|
|
|
|
vbox.set_border_width(style.DEFAULT_PADDING)
|
|
|
|
|
|
|
|
self._label = gtk.Label()
|
|
|
|
vbox.pack_start(self._label, False)
|
|
|
|
|
2007-08-08 12:56:19 +02:00
|
|
|
self._secondary_box = gtk.VBox()
|
|
|
|
vbox.pack_start(self._secondary_box)
|
|
|
|
|
2007-08-08 03:07:00 +02:00
|
|
|
self._separator = gtk.HSeparator()
|
2007-08-08 12:56:19 +02:00
|
|
|
self._secondary_box.pack_start(self._separator)
|
2007-08-08 03:07:00 +02:00
|
|
|
|
2007-08-11 12:16:49 +02:00
|
|
|
self._menu_box = gtk.VBox()
|
|
|
|
self._secondary_box.pack_start(self._menu_box)
|
|
|
|
self._menu_box.show()
|
2007-08-08 03:07:00 +02:00
|
|
|
|
|
|
|
self._content = gtk.VBox()
|
2007-08-08 12:56:19 +02:00
|
|
|
self._secondary_box.pack_start(self._content)
|
|
|
|
self._content.show()
|
2007-06-02 06:33:41 +02:00
|
|
|
|
2007-08-08 13:03:09 +02:00
|
|
|
self.action_bar = PaletteActionBar()
|
|
|
|
self._secondary_box.pack_start(self.action_bar)
|
|
|
|
self.action_bar.show()
|
2007-06-06 06:51:01 +02:00
|
|
|
|
2007-08-08 03:07:00 +02:00
|
|
|
self.add(vbox)
|
|
|
|
vbox.show()
|
2007-06-25 11:39:51 +02:00
|
|
|
|
2007-08-08 12:56:19 +02:00
|
|
|
self.menu = _Menu(self)
|
|
|
|
self.menu.show()
|
2007-08-08 11:53:41 +02:00
|
|
|
|
2007-08-08 03:07:00 +02:00
|
|
|
self.connect('enter-notify-event',
|
|
|
|
self._enter_notify_event_cb)
|
|
|
|
self.connect('leave-notify-event',
|
|
|
|
self._leave_notify_event_cb)
|
2007-06-06 04:43:42 +02:00
|
|
|
|
2007-08-08 03:07:00 +02:00
|
|
|
self.set_primary_text(label, accel_path)
|
2007-05-24 19:37:48 +02:00
|
|
|
|
2007-07-06 14:36:59 +02:00
|
|
|
def is_up(self):
|
|
|
|
return self._up
|
|
|
|
|
2007-08-10 18:29:28 +02:00
|
|
|
def get_rect(self):
|
|
|
|
win_x, win_y = self.window.get_origin()
|
|
|
|
rectangle = self.get_allocation()
|
|
|
|
|
|
|
|
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-07-01 12:31:23 +02:00
|
|
|
def set_primary_text(self, label, accel_path=None):
|
2007-08-08 03:07:00 +02:00
|
|
|
self._label.set_text(label)
|
|
|
|
self._label.show()
|
2007-07-01 11:05:14 +02:00
|
|
|
|
2007-08-08 12:56:19 +02:00
|
|
|
def set_content(self, widget):
|
|
|
|
if len(self._content.get_children()) > 0:
|
|
|
|
self.remove(self._content.get_children()[0])
|
2007-06-26 18:19:26 +02:00
|
|
|
|
2007-08-08 12:56:19 +02:00
|
|
|
if widget is not None:
|
|
|
|
self._content.add(widget)
|
2007-06-26 18:19:26 +02:00
|
|
|
|
2007-08-08 11:53:41 +02:00
|
|
|
self._update_accept_focus()
|
2007-08-08 12:56:19 +02:00
|
|
|
self._update_separator()
|
2007-05-24 19:37:48 +02:00
|
|
|
|
2007-07-06 14:36:59 +02:00
|
|
|
def set_group_id(self, group_id):
|
|
|
|
if self._group_id:
|
|
|
|
group = palettegroup.get_group(self._group_id)
|
|
|
|
group.remove(self)
|
|
|
|
if group_id:
|
|
|
|
group = palettegroup.get_group(group_id)
|
|
|
|
group.add(self)
|
|
|
|
|
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-07-09 14:26:41 +02:00
|
|
|
self._invoker.connect('mouse-enter', self._invoker_mouse_enter_cb)
|
|
|
|
self._invoker.connect('mouse-leave', self._invoker_mouse_leave_cb)
|
2007-06-29 14:45:04 +02:00
|
|
|
elif pspec.name == 'position':
|
|
|
|
self._position = value
|
2007-08-10 18:29:28 +02:00
|
|
|
elif pspec.name == 'draw-gap':
|
|
|
|
self._draw_gap = value
|
|
|
|
self.queue_draw()
|
2007-05-24 19:37:48 +02:00
|
|
|
else:
|
|
|
|
raise AssertionError
|
|
|
|
|
2007-07-06 16:51:18 +02:00
|
|
|
def do_get_property(self, pspec):
|
|
|
|
if pspec.name == 'invoker':
|
|
|
|
return self._invoker
|
|
|
|
elif pspec.name == 'position':
|
|
|
|
return self._position
|
2007-08-10 18:29:28 +02:00
|
|
|
elif pspec.name == 'draw-gap':
|
|
|
|
return self._draw_gap
|
2007-07-06 16:51:18 +02:00
|
|
|
else:
|
|
|
|
raise AssertionError
|
|
|
|
|
2007-08-11 12:16:49 +02:00
|
|
|
def do_size_allocate(self, allocation):
|
|
|
|
gtk.Window.do_size_allocate(self, allocation)
|
|
|
|
self.queue_draw()
|
|
|
|
|
2007-08-10 18:29:28 +02:00
|
|
|
def do_expose_event(self, event):
|
|
|
|
# We want to draw a border with a beautiful gap
|
|
|
|
if self._draw_gap:
|
|
|
|
invoker = self._invoker.get_rect()
|
|
|
|
palette = self.get_rect()
|
|
|
|
|
|
|
|
gap = _calculate_gap(palette, invoker)
|
|
|
|
else:
|
|
|
|
gap = False
|
|
|
|
|
|
|
|
if gap:
|
|
|
|
self.style.paint_box_gap(event.window, gtk.STATE_PRELIGHT,
|
|
|
|
gtk.SHADOW_IN, event.area, self, "palette",
|
|
|
|
0, 0,
|
|
|
|
self.allocation.width,
|
|
|
|
self.allocation.height,
|
|
|
|
gap[0], gap[1], gap[2])
|
|
|
|
else:
|
|
|
|
self.style.paint_box(event.window, gtk.STATE_PRELIGHT,
|
|
|
|
gtk.SHADOW_IN, event.area, self, "palette",
|
|
|
|
0, 0,
|
|
|
|
self.allocation.width,
|
|
|
|
self.allocation.height)
|
|
|
|
|
|
|
|
# Fall trough to the container expose handler.
|
|
|
|
# (Leaving out the window expose handler which redraws everything)
|
|
|
|
gtk.Bin.do_expose_event(self, event)
|
|
|
|
|
2007-08-08 12:56:19 +02:00
|
|
|
def _update_separator(self):
|
|
|
|
visible = len(self.menu.get_children()) > 0 or \
|
|
|
|
len(self._content.get_children()) > 0
|
|
|
|
self._separator.props.visible = visible
|
|
|
|
|
2007-08-08 11:53:41 +02:00
|
|
|
def _update_accept_focus(self):
|
|
|
|
accept_focus = len(self._content.get_children())
|
|
|
|
if self.window:
|
|
|
|
self.window.set_accept_focus(accept_focus)
|
|
|
|
|
2007-08-08 03:07:00 +02:00
|
|
|
def _realize_cb(self, widget):
|
|
|
|
self.window.set_type_hint(gtk.gdk.WINDOW_TYPE_HINT_DIALOG)
|
2007-08-08 11:53:41 +02:00
|
|
|
self._update_accept_focus()
|
2007-08-08 03:07:00 +02:00
|
|
|
|
2007-06-29 14:45:04 +02:00
|
|
|
def _in_screen(self, x, y):
|
2007-07-24 16:15:13 +02:00
|
|
|
[width, height] = self._full_request
|
2007-07-06 16:51:18 +02:00
|
|
|
screen_area = self._invoker.get_screen_area()
|
2007-07-01 12:31:23 +02:00
|
|
|
|
2007-07-06 16:51:18 +02:00
|
|
|
return x >= screen_area.x and \
|
|
|
|
y >= screen_area.y and \
|
|
|
|
x + width <= screen_area.width and \
|
|
|
|
y + height <= screen_area.height
|
2007-06-01 06:08:24 +02:00
|
|
|
|
2007-07-18 20:15:54 +02:00
|
|
|
def _get_position(self, palette_halign, palette_valign,
|
|
|
|
invoker_halign, invoker_valign, inv_rect=None):
|
|
|
|
if inv_rect == None:
|
|
|
|
inv_rect = self._invoker.get_rect()
|
|
|
|
|
2007-08-08 03:07:00 +02:00
|
|
|
palette_width, palette_height = self.size_request()
|
2007-05-24 19:37:48 +02:00
|
|
|
|
2007-07-18 20:15:54 +02:00
|
|
|
x = inv_rect.x + inv_rect.width * invoker_halign + \
|
|
|
|
palette_width * palette_halign
|
2007-05-24 19:37:48 +02:00
|
|
|
|
2007-07-18 20:15:54 +02:00
|
|
|
y = inv_rect.y + inv_rect.height * invoker_valign + \
|
|
|
|
palette_height * palette_valign
|
|
|
|
|
|
|
|
return int(x), int(y)
|
|
|
|
|
|
|
|
def _get_left_position(self, inv_rect=None):
|
|
|
|
x, y = self._get_position(-1.0, 0.0, 0.0, 0.0, inv_rect)
|
|
|
|
if not self._in_screen(x, y):
|
|
|
|
x, y = self._get_position(-1.0, -1.0, 0.0, 1.0, inv_rect)
|
|
|
|
return x, y
|
|
|
|
|
|
|
|
def _get_right_position(self, inv_rect=None):
|
|
|
|
x, y = self._get_position(0.0, 0.0, 1.0, 0.0, inv_rect)
|
|
|
|
if not self._in_screen(x, y):
|
|
|
|
x, y = self._get_position(0.0, -1.0, 1.0, 1.0, inv_rect)
|
|
|
|
return x, y
|
|
|
|
|
|
|
|
def _get_top_position(self, inv_rect=None):
|
|
|
|
x, y = self._get_position(0.0, -1.0, 0.0, 0.0, inv_rect)
|
|
|
|
if not self._in_screen(x, y):
|
|
|
|
x, y = self._get_position(-1.0, -1.0, 1.0, 0.0, inv_rect)
|
|
|
|
return x, y
|
|
|
|
|
|
|
|
def _get_bottom_position(self, inv_rect=None):
|
|
|
|
x, y = self._get_position(0.0, 0.0, 0.0, 1.0, inv_rect)
|
|
|
|
if not self._in_screen(x, y):
|
|
|
|
x, y = self._get_position(-1.0, 0.0, 1.0, 1.0, inv_rect)
|
|
|
|
return x, y
|
|
|
|
|
|
|
|
def _get_around_position(self, inv_rect=None):
|
|
|
|
x, y = self._get_bottom_position(inv_rect)
|
|
|
|
if not self._in_screen(x, y):
|
|
|
|
x, y = self._get_right_position(inv_rect)
|
|
|
|
if not self._in_screen(x, y):
|
|
|
|
x, y = self._get_top_position(inv_rect)
|
|
|
|
if not self._in_screen(x, y):
|
|
|
|
x, y = self._get_left_position(inv_rect)
|
|
|
|
|
|
|
|
return x, y
|
|
|
|
|
|
|
|
def _get_at_cursor_position(self, inv_rect=None):
|
|
|
|
x, y = self._get_position(0.0, 0.0, 1.0, 1.0, inv_rect)
|
|
|
|
if not self._in_screen(x, y):
|
|
|
|
x, y = self._get_position(0.0, -1.0, 1.0, 0.0, inv_rect)
|
|
|
|
if not self._in_screen(x, y):
|
|
|
|
x, y = self._get_position(-1.0, -1.0, 0.0, 0.0, inv_rect)
|
|
|
|
if not self._in_screen(x, y):
|
|
|
|
x, y = self._get_position(-1.0, 0.0, 0.0, 1.0, inv_rect)
|
|
|
|
|
|
|
|
return x, y
|
2007-07-06 16:51:18 +02:00
|
|
|
|
2007-07-24 16:15:13 +02:00
|
|
|
def _update_full_request(self):
|
|
|
|
state = self._state
|
|
|
|
|
2007-08-08 03:07:00 +02:00
|
|
|
self.set_size_request(-1, -1)
|
2007-07-24 16:15:13 +02:00
|
|
|
|
|
|
|
self._set_state(self._SECONDARY)
|
2007-08-08 03:07:00 +02:00
|
|
|
self._full_request = self.size_request()
|
2007-07-24 16:15:13 +02:00
|
|
|
|
2007-08-08 03:07:00 +02:00
|
|
|
self.set_size_request(self._full_request[0], -1)
|
2007-07-24 16:15:13 +02:00
|
|
|
|
|
|
|
self._set_state(state)
|
|
|
|
|
2007-07-25 16:01:07 +02:00
|
|
|
def _update_cursor_position(self):
|
|
|
|
display = gtk.gdk.display_get_default()
|
|
|
|
screen, x, y, mask = display.get_pointer()
|
|
|
|
self._cursor_x = x
|
|
|
|
self._cursor_y = y
|
|
|
|
|
2007-07-24 16:21:21 +02:00
|
|
|
def _update_position(self):
|
2007-06-29 14:45:04 +02:00
|
|
|
x = y = 0
|
|
|
|
|
2007-07-18 16:59:47 +02:00
|
|
|
if self._position == self.DEFAULT:
|
|
|
|
position = self._invoker.get_default_position()
|
|
|
|
else:
|
|
|
|
position = self._position
|
|
|
|
|
|
|
|
if position == self.AT_CURSOR:
|
2007-07-18 20:15:54 +02:00
|
|
|
dist = style.PALETTE_CURSOR_DISTANCE
|
2007-07-25 16:01:07 +02:00
|
|
|
rect = gtk.gdk.Rectangle(self._cursor_x - dist,
|
|
|
|
self._cursor_y - dist,
|
|
|
|
dist * 2, dist * 2)
|
2007-07-18 20:15:54 +02:00
|
|
|
|
|
|
|
x, y = self._get_at_cursor_position(rect)
|
2007-07-18 16:59:47 +02:00
|
|
|
elif position == self.AROUND:
|
2007-07-18 20:15:54 +02:00
|
|
|
x, y = self._get_around_position()
|
2007-07-18 16:59:47 +02:00
|
|
|
elif position == self.BOTTOM:
|
2007-07-18 20:15:54 +02:00
|
|
|
x, y = self._get_bottom_position()
|
2007-07-18 16:59:47 +02:00
|
|
|
elif position == self.LEFT:
|
2007-07-18 20:15:54 +02:00
|
|
|
x, y = self._get_left_position()
|
2007-07-18 16:59:47 +02:00
|
|
|
elif position == self.RIGHT:
|
2007-07-18 20:15:54 +02:00
|
|
|
x, y = self._get_right_position()
|
2007-07-18 16:59:47 +02:00
|
|
|
elif position == self.TOP:
|
2007-07-18 20:15:54 +02:00
|
|
|
x, y = self._get_top_position()
|
2007-06-29 14:45:04 +02:00
|
|
|
|
2007-08-08 03:07:00 +02:00
|
|
|
self.move(x, y)
|
2007-07-24 16:21:21 +02:00
|
|
|
|
|
|
|
def _show(self):
|
|
|
|
if self._up:
|
|
|
|
return
|
|
|
|
|
2007-07-25 16:01:07 +02:00
|
|
|
self._update_cursor_position()
|
2007-07-24 16:21:21 +02:00
|
|
|
self._update_full_request()
|
2007-07-09 14:26:41 +02:00
|
|
|
|
2007-08-08 11:53:41 +02:00
|
|
|
self._palette_popup_sid = _palette_observer.connect(
|
|
|
|
'popup', self._palette_observer_popup_cb)
|
2007-07-24 16:21:21 +02:00
|
|
|
|
|
|
|
self._update_position()
|
2007-08-08 12:56:19 +02:00
|
|
|
self.menu.set_active(True)
|
2007-08-08 03:07:00 +02:00
|
|
|
self.show()
|
2007-07-06 14:36:59 +02:00
|
|
|
|
|
|
|
self._up = True
|
2007-07-05 14:16:36 +02:00
|
|
|
_palette_observer.emit('popup', self)
|
2007-07-06 14:36:59 +02:00
|
|
|
self.emit('popup')
|
2007-05-24 19:37:48 +02:00
|
|
|
|
2007-06-26 14:55:16 +02:00
|
|
|
def _hide(self):
|
2007-07-05 14:16:36 +02:00
|
|
|
if not self._palette_popup_sid is None:
|
|
|
|
_palette_observer.disconnect(self._palette_popup_sid)
|
|
|
|
self._palette_popup_sid = None
|
2007-08-08 12:56:19 +02:00
|
|
|
self.menu.set_active(False)
|
2007-08-08 03:07:00 +02:00
|
|
|
self.hide()
|
2007-05-24 19:37:48 +02:00
|
|
|
|
2007-07-06 14:36:59 +02:00
|
|
|
self._up = False
|
|
|
|
self.emit('popdown')
|
|
|
|
|
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-07-02 12:05:42 +02:00
|
|
|
self._secondary_anim.start()
|
2007-06-19 22:02:25 +02:00
|
|
|
|
2007-07-08 16:29:43 +02:00
|
|
|
def popdown(self, inmediate=False):
|
2007-07-02 12:05:42 +02:00
|
|
|
self._secondary_anim.stop()
|
2007-06-25 14:31:43 +02:00
|
|
|
self._popup_anim.stop()
|
2007-07-08 16:29:43 +02:00
|
|
|
|
|
|
|
if not inmediate:
|
|
|
|
self._popdown_anim.start()
|
|
|
|
else:
|
|
|
|
self._hide()
|
2007-05-29 18:27:54 +02:00
|
|
|
|
2007-07-24 15:53:35 +02:00
|
|
|
def _set_state(self, state):
|
|
|
|
if self._state == state:
|
|
|
|
return
|
|
|
|
|
|
|
|
if state == self._PRIMARY:
|
2007-08-11 12:16:49 +02:00
|
|
|
self.menu.unembed()
|
2007-08-08 12:56:19 +02:00
|
|
|
self._secondary_box.hide()
|
2007-07-24 15:53:35 +02:00
|
|
|
elif state == self._SECONDARY:
|
2007-08-11 12:16:49 +02:00
|
|
|
self.menu.embed(self._menu_box)
|
2007-08-08 12:56:19 +02:00
|
|
|
self._secondary_box.show()
|
2007-07-24 15:53:35 +02:00
|
|
|
|
|
|
|
self._state = state
|
|
|
|
|
2007-07-09 14:26:41 +02:00
|
|
|
def _invoker_mouse_enter_cb(self, invoker):
|
2007-06-25 14:31:43 +02:00
|
|
|
self.popup()
|
2007-05-30 19:29:29 +02:00
|
|
|
|
2007-07-09 14:26:41 +02:00
|
|
|
def _invoker_mouse_leave_cb(self, invoker):
|
2007-06-25 14:31:43 +02:00
|
|
|
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):
|
2007-08-08 14:56:43 +02:00
|
|
|
if event.detail != gtk.gdk.NOTIFY_INFERIOR:
|
2007-06-25 14:31:43 +02:00
|
|
|
self._popdown_anim.stop()
|
2007-07-18 16:59:47 +02:00
|
|
|
self._secondary_anim.start()
|
2007-05-30 19:29:29 +02:00
|
|
|
|
2007-06-25 14:31:43 +02:00
|
|
|
def _leave_notify_event_cb(self, widget, event):
|
2007-08-08 14:56:43 +02:00
|
|
|
if event.detail != gtk.gdk.NOTIFY_INFERIOR:
|
2007-06-25 14:31:43 +02:00
|
|
|
self.popdown()
|
2007-05-30 19:29:29 +02:00
|
|
|
|
2007-07-05 14:16:36 +02:00
|
|
|
def _palette_observer_popup_cb(self, observer, palette):
|
|
|
|
if self != palette:
|
|
|
|
self._hide()
|
|
|
|
|
2007-08-08 13:03:09 +02:00
|
|
|
class PaletteActionBar(gtk.HButtonBox):
|
|
|
|
def add_action(label, icon_name=None):
|
|
|
|
button = Button(label)
|
|
|
|
|
|
|
|
if icon_name:
|
|
|
|
icon = Icon(icon_name)
|
|
|
|
button.set_image(icon)
|
|
|
|
icon.show()
|
|
|
|
|
|
|
|
self.pack_start(button)
|
|
|
|
button.show()
|
|
|
|
|
2007-08-08 12:56:19 +02:00
|
|
|
class _Menu(_sugaruiext.Menu):
|
|
|
|
__gtype_name__ = 'SugarPaletteMenu'
|
|
|
|
|
|
|
|
def __init__(self, palette):
|
|
|
|
_sugaruiext.Menu.__init__(self)
|
|
|
|
self._palette = palette
|
|
|
|
|
|
|
|
def do_insert(self, item, position):
|
|
|
|
_sugaruiext.Menu.do_insert(self, item, position)
|
|
|
|
self._palette._update_separator()
|
|
|
|
|
2007-08-10 18:29:28 +02:00
|
|
|
def do_expose_event(self, event):
|
|
|
|
# Ignore the Menu expose, just do the MenuShell expose to prevent any
|
|
|
|
# border from being drawn here. A border is drawn by the palette object
|
|
|
|
# around everything.
|
|
|
|
gtk.MenuShell.do_expose_event(self, event)
|
|
|
|
|
2007-08-10 23:21:36 +02:00
|
|
|
def do_grab_notify(self, was_grabbed):
|
|
|
|
# Ignore grab_notify as the menu would close otherwise
|
|
|
|
pass
|
|
|
|
|
2007-08-08 14:41:30 +02:00
|
|
|
def do_deactivate(self):
|
|
|
|
self._palette._hide()
|
|
|
|
|
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-07-24 15:53:35 +02:00
|
|
|
self._palette._set_state(Palette._PRIMARY)
|
2007-06-26 14:55:16 +02:00
|
|
|
self._palette._show()
|
2007-05-30 19:29:29 +02:00
|
|
|
|
2007-07-02 12:05:42 +02:00
|
|
|
class _SecondaryAnimation(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-07-24 15:53:35 +02:00
|
|
|
self._palette._set_state(Palette._SECONDARY)
|
2007-07-24 16:21:21 +02:00
|
|
|
self._palette._update_position()
|
2007-07-02 12:39:16 +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-07-09 14:26:41 +02:00
|
|
|
class Invoker(gobject.GObject):
|
|
|
|
__gtype_name__ = 'SugarPaletteInvoker'
|
2007-06-06 04:43:42 +02:00
|
|
|
|
2007-07-09 14:26:41 +02:00
|
|
|
__gsignals__ = {
|
|
|
|
'mouse-enter': (gobject.SIGNAL_RUN_FIRST, gobject.TYPE_NONE, ([])),
|
|
|
|
'mouse-leave': (gobject.SIGNAL_RUN_FIRST, gobject.TYPE_NONE, ([])),
|
|
|
|
'focus-out': (gobject.SIGNAL_RUN_FIRST, gobject.TYPE_NONE, ([]))
|
|
|
|
}
|
2007-06-19 22:02:25 +02:00
|
|
|
|
2007-07-09 14:26:41 +02:00
|
|
|
def __init__(self):
|
|
|
|
gobject.GObject.__init__(self)
|
2007-06-25 14:31:43 +02:00
|
|
|
|
2007-07-18 16:59:47 +02:00
|
|
|
def get_default_position(self):
|
|
|
|
return Palette.AROUND
|
|
|
|
|
2007-07-06 16:51:18 +02:00
|
|
|
def get_screen_area(self):
|
|
|
|
width = gtk.gdk.screen_width()
|
|
|
|
height = gtk.gdk.screen_height()
|
|
|
|
return gtk.gdk.Rectangle(0, 0, width, height)
|
|
|
|
|
2007-06-25 14:31:43 +02:00
|
|
|
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-08-10 18:29:28 +02:00
|
|
|
def draw_invoker_rect(self, event, palette):
|
|
|
|
style = self._widget.style
|
|
|
|
if palette.is_up():
|
|
|
|
gap = _calculate_gap(self.get_rect(), palette.get_rect())
|
|
|
|
|
|
|
|
if gap:
|
|
|
|
style.paint_box_gap(event.window, gtk.STATE_PRELIGHT,
|
|
|
|
gtk.SHADOW_IN, event.area, self._widget,
|
|
|
|
"palette-invoker",
|
|
|
|
self._widget.allocation.x,
|
|
|
|
self._widget.allocation.y,
|
|
|
|
self._widget.allocation.width,
|
|
|
|
self._widget.allocation.height,
|
|
|
|
gap[0], gap[1], gap[2])
|
|
|
|
else:
|
|
|
|
style.paint_box(event.window, gtk.STATE_PRELIGHT,
|
|
|
|
gtk.SHADOW_IN, event.area, self._widget,
|
|
|
|
"palette-invoker",
|
|
|
|
self._widget.allocation.x,
|
|
|
|
self._widget.allocation.y,
|
|
|
|
self._widget.allocation.width,
|
|
|
|
self._widget.allocation.height)
|
|
|
|
else:
|
|
|
|
style.paint_box(event.window, gtk.STATE_PRELIGHT,
|
|
|
|
gtk.SHADOW_NONE, event.area, self._widget,
|
|
|
|
"palette-invoker",
|
|
|
|
self._widget.allocation.x,
|
|
|
|
self._widget.allocation.y,
|
|
|
|
self._widget.allocation.width,
|
|
|
|
self._widget.allocation.height)
|
|
|
|
|
2007-06-25 14:31:43 +02:00
|
|
|
def _enter_notify_event_cb(self, widget, event):
|
2007-07-09 14:26:41 +02:00
|
|
|
self.emit('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):
|
2007-07-09 14:26:41 +02:00
|
|
|
self.emit('mouse-leave')
|
|
|
|
|
|
|
|
def get_toplevel(self):
|
|
|
|
return self._widget.get_toplevel()
|
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
|
|
|
|
2007-07-18 16:59:47 +02:00
|
|
|
def get_default_position(self):
|
|
|
|
return Palette.AT_CURSOR
|
|
|
|
|
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:
|
2007-07-09 14:26:41 +02:00
|
|
|
self.emit('mouse-enter')
|
2007-06-25 14:31:43 +02:00
|
|
|
elif event.detail == hippo.MOTION_DETAIL_LEAVE:
|
2007-07-09 14:26:41 +02:00
|
|
|
self.emit('mouse-leave')
|
2007-06-25 14:31:43 +02:00
|
|
|
|
|
|
|
return False
|
2007-07-05 14:16:36 +02:00
|
|
|
|
2007-07-09 14:26:41 +02:00
|
|
|
def get_toplevel(self):
|
|
|
|
return hippo.get_canvas_for_item(self._item).get_toplevel()
|
|
|
|
|
2007-07-05 14:16:36 +02:00
|
|
|
class _PaletteObserver(gobject.GObject):
|
|
|
|
__gtype_name__ = 'SugarPaletteObserver'
|
|
|
|
|
|
|
|
__gsignals__ = {
|
|
|
|
'popup': (gobject.SIGNAL_RUN_FIRST, gobject.TYPE_NONE, ([object]))
|
|
|
|
}
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
gobject.GObject.__init__(self)
|
|
|
|
|
|
|
|
_palette_observer = _PaletteObserver()
|