2007-05-24 19:37:48 +02:00
|
|
|
#!/usr/bin/env python
|
|
|
|
|
|
|
|
# Copyright (C) 2007, Eduardo Silva (edsiper@gmail.com)
|
|
|
|
#
|
|
|
|
# This program is free software; you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation; either version 2 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program 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 General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with this program; if not, write to the Free Software
|
|
|
|
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
|
|
|
|
import gtk
|
2007-05-29 18:27:54 +02:00
|
|
|
from gtk import gdk, keysyms
|
2007-05-24 19:37:48 +02:00
|
|
|
import gobject
|
|
|
|
import pango
|
|
|
|
|
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
|
|
|
|
|
|
|
class Palette(gtk.Window):
|
|
|
|
__gtype_name__ = 'SugarPalette'
|
|
|
|
|
|
|
|
__gproperties__ = {
|
|
|
|
'parent': (object, None, None, gobject.PARAM_READWRITE),
|
|
|
|
|
2007-06-01 06:08:24 +02:00
|
|
|
'alignment': (gobject.TYPE_INT, None, None, 0, 8, ALIGNMENT_AUTOMATIC,
|
2007-06-02 06:33:41 +02:00
|
|
|
gobject.PARAM_READWRITE),
|
|
|
|
|
|
|
|
'is-tooltip': (bool, None, None, False, gobject.PARAM_READWRITE | gobject.PARAM_CONSTRUCT_ONLY)
|
2007-05-24 19:37:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
_PADDING = 1
|
|
|
|
_WIN_BORDER = 5
|
|
|
|
|
2007-06-02 06:33:41 +02:00
|
|
|
def __init__(self, **kwargs):
|
|
|
|
gobject.GObject.__init__(self, type=gtk.WINDOW_POPUP, **kwargs)
|
2007-05-28 07:05:31 +02:00
|
|
|
gtk.Window.__init__(self)
|
2007-05-24 19:37:48 +02:00
|
|
|
|
2007-06-01 06:08:24 +02:00
|
|
|
self._alignment = ALIGNMENT_AUTOMATIC
|
|
|
|
|
2007-05-24 19:37:48 +02:00
|
|
|
self._palette_label = gtk.Label()
|
2007-05-29 18:27:54 +02:00
|
|
|
self._palette_label.show()
|
2007-05-24 19:37:48 +02:00
|
|
|
|
|
|
|
vbox = gtk.VBox(False, 0)
|
2007-06-02 06:33:41 +02:00
|
|
|
vbox.pack_start(self._palette_label, True, True, self._PADDING)
|
|
|
|
|
|
|
|
# If it's a tooltip palette..
|
|
|
|
if not self._is_tooltip:
|
|
|
|
self._separator = gtk.HSeparator()
|
|
|
|
self._separator.hide()
|
|
|
|
|
|
|
|
self._menu_bar = gtk.MenuBar()
|
|
|
|
self._menu_bar.set_pack_direction(gtk.PACK_DIRECTION_TTB)
|
|
|
|
self._menu_bar.show()
|
|
|
|
|
|
|
|
self._content = gtk.HBox()
|
|
|
|
self._content.show()
|
|
|
|
|
|
|
|
self._button_bar = gtk.HButtonBox()
|
|
|
|
self._button_bar.show()
|
|
|
|
|
|
|
|
# Set main container
|
|
|
|
vbox.pack_start(self._separator, True, True, self._PADDING)
|
|
|
|
vbox.pack_start(self._menu_bar, True, True, self._PADDING)
|
|
|
|
vbox.pack_start(self._content, True, True, self._PADDING)
|
|
|
|
vbox.pack_start(self._button_bar, True, True, self._PADDING)
|
2007-06-06 04:43:42 +02:00
|
|
|
|
2007-05-24 19:37:48 +02:00
|
|
|
vbox.show()
|
2007-06-02 06:33:41 +02:00
|
|
|
self.add(vbox)
|
2007-05-24 19:37:48 +02:00
|
|
|
|
2007-05-29 18:27:54 +02:00
|
|
|
# Widget events
|
2007-05-30 19:29:29 +02:00
|
|
|
self.connect('motion-notify-event', self._mouse_over_widget_cb)
|
|
|
|
self.connect('leave-notify-event', self._mouse_out_widget_cb)
|
|
|
|
self.connect('button-press-event', self._close_palette_cb)
|
|
|
|
self.connect('key-press-event', self._on_key_press_event_cb)
|
2007-05-24 19:37:48 +02:00
|
|
|
|
|
|
|
self.set_border_width(self._WIN_BORDER)
|
2007-05-28 07:05:31 +02:00
|
|
|
|
2007-06-01 06:08:24 +02:00
|
|
|
def do_set_property(self, pspec, value):
|
2007-05-24 19:37:48 +02:00
|
|
|
if pspec.name == 'parent':
|
|
|
|
self._parent_widget = value
|
|
|
|
elif pspec.name == 'alignment':
|
|
|
|
self._alignment = value
|
2007-06-02 06:33:41 +02:00
|
|
|
elif pspec.name == 'is-tooltip':
|
|
|
|
self._is_tooltip = value
|
2007-05-24 19:37:48 +02:00
|
|
|
else:
|
|
|
|
raise AssertionError
|
|
|
|
|
2007-05-28 07:05:31 +02:00
|
|
|
def set_position(self):
|
2007-06-01 06:08:24 +02:00
|
|
|
# Automatic Alignment
|
|
|
|
if self._alignment == ALIGNMENT_AUTOMATIC:
|
|
|
|
# Trying Different types of ALIGNMENTS,
|
|
|
|
# and return the choosen one
|
|
|
|
if self._try_position(ALIGNMENT_BOTTOM_LEFT):
|
|
|
|
return ALIGNMENT_BOTTOM_LEFT
|
|
|
|
elif self._try_position(ALIGNMENT_BOTTOM_RIGHT):
|
|
|
|
return ALIGNMENT_BOTTOM_RIGHT
|
|
|
|
elif self._try_position(ALIGNMENT_LEFT_BOTTOM):
|
|
|
|
return ALIGNMENT_LEFT_BOTTOM
|
|
|
|
elif self._try_position(ALIGNMENT_LEFT_TOP):
|
|
|
|
return ALIGNMENT_LEFT_TOP
|
|
|
|
elif self._try_position(ALIGNMENT_RIGHT_BOTTOM):
|
|
|
|
return ALIGNMENT_RIGHT_BOTTOM
|
|
|
|
elif self._try_position(ALIGNMENT_RIGHT_TOP):
|
|
|
|
return ALIGNMENT_RIGHT_TOP
|
|
|
|
elif self._try_position(ALIGNMENT_TOP_LEFT):
|
|
|
|
return ALIGNMENT_TOP_LEFT
|
|
|
|
elif self._try_position(ALIGNMENT_TOP_RIGHT):
|
|
|
|
return ALIGNMENT_TOP_RIGHT
|
|
|
|
else:
|
|
|
|
# Manual Alignment
|
|
|
|
move_x, move_y = self._calc_position(self._alignment)
|
|
|
|
self.move(move_x, move_y)
|
2007-05-24 19:37:48 +02:00
|
|
|
|
2007-06-01 06:08:24 +02:00
|
|
|
def _try_position(self, alignment):
|
|
|
|
scr_width = gtk.gdk.screen_width()
|
|
|
|
scr_height = gtk.gdk.screen_height()
|
|
|
|
|
|
|
|
plt_width, plt_height = self.size_request()
|
|
|
|
|
|
|
|
move_x, move_y = self._calc_position(alignment)
|
|
|
|
self.move(move_x, move_y)
|
|
|
|
plt_x, plt_y = self.window.get_origin()
|
|
|
|
|
|
|
|
if (plt_x<0 or plt_x+plt_width>scr_width) or (plt_y<0 or plt_y+plt_height>scr_height):
|
|
|
|
return False
|
|
|
|
else:
|
|
|
|
self.move(move_x, move_y)
|
|
|
|
return True
|
|
|
|
|
|
|
|
def _calc_position(self, alignment):
|
|
|
|
win_x, win_y = self._parent_widget.window.get_origin()
|
2007-05-24 19:37:48 +02:00
|
|
|
parent_rectangle = self._parent_widget.get_allocation()
|
2007-06-01 06:08:24 +02:00
|
|
|
palette_rectangle = self.get_allocation()
|
2007-05-24 19:37:48 +02:00
|
|
|
|
2007-06-01 06:08:24 +02:00
|
|
|
palette_width, palette_height = self.size_request()
|
2007-05-24 19:37:48 +02:00
|
|
|
|
2007-06-01 06:08:24 +02:00
|
|
|
if alignment == ALIGNMENT_BOTTOM_LEFT:
|
|
|
|
move_x = win_x + parent_rectangle.x
|
|
|
|
move_y = win_y + parent_rectangle.y + parent_rectangle.height
|
2007-05-24 19:37:48 +02:00
|
|
|
|
2007-06-01 06:08:24 +02:00
|
|
|
elif alignment == ALIGNMENT_BOTTOM_RIGHT:
|
|
|
|
move_x = (win_x + parent_rectangle.x + parent_rectangle.width) - palette_width
|
|
|
|
move_y = win_y + parent_rectangle.y + parent_rectangle.height
|
2007-05-29 18:27:54 +02:00
|
|
|
|
2007-06-01 06:08:24 +02:00
|
|
|
elif alignment == ALIGNMENT_LEFT_BOTTOM:
|
|
|
|
move_x = (win_x + parent_rectangle.x) - palette_width
|
|
|
|
move_y = win_y + parent_rectangle.y
|
2007-05-24 19:37:48 +02:00
|
|
|
|
2007-06-01 06:08:24 +02:00
|
|
|
elif alignment == ALIGNMENT_LEFT_TOP:
|
|
|
|
move_x = (win_x + parent_rectangle.x) - palette_width
|
|
|
|
move_y = (win_y + parent_rectangle.y + parent_rectangle.height) - palette_rectangle.height
|
2007-05-24 19:37:48 +02:00
|
|
|
|
2007-06-01 06:08:24 +02:00
|
|
|
elif alignment == ALIGNMENT_RIGHT_BOTTOM:
|
|
|
|
move_x = win_x + parent_rectangle.x + parent_rectangle.width
|
|
|
|
move_y = win_y + parent_rectangle.y
|
2007-05-24 19:37:48 +02:00
|
|
|
|
2007-06-01 06:08:24 +02:00
|
|
|
elif alignment == ALIGNMENT_RIGHT_TOP:
|
|
|
|
move_x = win_x + parent_rectangle.x + parent_rectangle.width
|
|
|
|
move_y = (win_y + parent_rectangle.y + parent_rectangle.height) - palette_rectangle.height
|
2007-05-24 19:37:48 +02:00
|
|
|
|
2007-06-01 06:08:24 +02:00
|
|
|
elif alignment == ALIGNMENT_TOP_LEFT:
|
|
|
|
move_x = (win_x + parent_rectangle.x)
|
|
|
|
move_y = (win_y + parent_rectangle.y) - (palette_rectangle.height)
|
2007-05-24 19:37:48 +02:00
|
|
|
|
2007-06-01 06:08:24 +02:00
|
|
|
elif alignment == ALIGNMENT_TOP_RIGHT:
|
|
|
|
move_x = (win_x + parent_rectangle.x + parent_rectangle.width) - palette_width
|
|
|
|
move_y = (win_y + parent_rectangle.y) - (palette_rectangle.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
|
|
|
|
|
|
|
def set_primary_state(self, label, accel_path=None):
|
|
|
|
if accel_path != None:
|
|
|
|
item = gtk.MenuItem(label)
|
|
|
|
item.set_accel_path(accel_path)
|
|
|
|
self.append_menu_item(item)
|
|
|
|
self._separator.hide()
|
|
|
|
else:
|
|
|
|
self._palette_label.set_text(label)
|
2007-06-02 06:33:41 +02:00
|
|
|
if not self._is_tooltip:
|
|
|
|
self._separator.show()
|
2007-05-24 19:37:48 +02:00
|
|
|
|
|
|
|
def append_menu_item(self, item):
|
|
|
|
self._menu_bar.append(item)
|
|
|
|
item.show()
|
|
|
|
|
|
|
|
def set_content(self, widget):
|
|
|
|
self._content.pack_start(widget, True, True, self._PADDING)
|
|
|
|
widget.show()
|
|
|
|
|
|
|
|
def append_button(self, button):
|
2007-05-30 19:29:29 +02:00
|
|
|
button.connect('released', self._close_palette_cb)
|
2007-05-24 19:37:48 +02:00
|
|
|
self._button_bar.pack_start(button, True, True, self._PADDING)
|
|
|
|
button.show()
|
|
|
|
|
2007-06-01 06:08:24 +02:00
|
|
|
# Display the palette and set the position on the screen
|
2007-05-30 19:29:29 +02:00
|
|
|
def popup(self):
|
2007-06-01 06:08:24 +02:00
|
|
|
# We need to know if the mouse pointer continue inside
|
|
|
|
# the parent widget (opener)
|
|
|
|
pointer_x, pointer_y = self._parent_widget.get_pointer()
|
|
|
|
parent_alloc = self._parent_widget.get_allocation()
|
|
|
|
pointer_rect = gdk.Rectangle(pointer_x + parent_alloc.x, pointer_y + parent_alloc.y, 1, 1)
|
|
|
|
|
|
|
|
if (self._parent_widget.allocation.intersect(pointer_rect).width == 0):
|
|
|
|
return
|
|
|
|
|
2007-05-28 07:05:31 +02:00
|
|
|
self.show()
|
|
|
|
self.set_position()
|
2007-05-29 18:27:54 +02:00
|
|
|
self._pointer_grab()
|
|
|
|
|
2007-05-30 19:29:29 +02:00
|
|
|
# PRIVATE METHODS
|
|
|
|
|
|
|
|
def _is_mouse_out(self, window, event):
|
|
|
|
# If we're clicking outside of the Palette
|
|
|
|
# return True
|
2007-06-01 06:08:24 +02:00
|
|
|
event_rect = gdk.Rectangle(int(event.x), int(event.y), 1, 1)
|
2007-05-30 19:29:29 +02:00
|
|
|
|
|
|
|
if (event.window != self.window or self.allocation.intersect(event_rect).width==0):
|
|
|
|
return True
|
|
|
|
else:
|
|
|
|
return False
|
|
|
|
|
2007-05-29 18:27:54 +02:00
|
|
|
def _pointer_grab(self):
|
|
|
|
gtk.gdk.pointer_grab(self.window, owner_events=False,
|
|
|
|
event_mask=gtk.gdk.BUTTON_PRESS_MASK |
|
|
|
|
gtk.gdk.BUTTON_RELEASE_MASK |
|
|
|
|
gtk.gdk.ENTER_NOTIFY_MASK |
|
|
|
|
gtk.gdk.LEAVE_NOTIFY_MASK |
|
|
|
|
gtk.gdk.POINTER_MOTION_MASK)
|
|
|
|
|
|
|
|
gdk.keyboard_grab(self.window, False)
|
|
|
|
|
2007-05-30 19:29:29 +02:00
|
|
|
# SIGNAL HANDLERS
|
|
|
|
|
|
|
|
# Release the GDK pointer and hide the palette
|
|
|
|
def _close_palette_cb(self, widget=None, event=None):
|
|
|
|
gtk.gdk.pointer_ungrab()
|
|
|
|
self.hide()
|
|
|
|
|
|
|
|
# Mouse is out of the widget
|
|
|
|
def _mouse_out_widget_cb(self, widget, event):
|
2007-05-29 18:27:54 +02:00
|
|
|
if (widget == self) and self._is_mouse_out(widget, event):
|
|
|
|
self._pointer_grab()
|
|
|
|
|
2007-05-30 19:29:29 +02:00
|
|
|
# Mouse inside the widget
|
|
|
|
def _mouse_over_widget_cb(self, widget, event):
|
2007-05-29 18:27:54 +02:00
|
|
|
gtk.gdk.pointer_ungrab()
|
|
|
|
|
2007-05-30 19:29:29 +02:00
|
|
|
# Some key is pressed
|
|
|
|
def _on_key_press_event_cb(self, window, event):
|
2007-05-29 18:27:54 +02:00
|
|
|
# Escape or Alt+Up: Close
|
|
|
|
# Enter, Return or Space: Select
|
|
|
|
keyval = event.keyval
|
|
|
|
state = event.state & gtk.accelerator_get_default_mod_mask()
|
2007-06-06 04:43:42 +02:00
|
|
|
|
2007-05-29 18:27:54 +02:00
|
|
|
if (keyval == keysyms.Escape or
|
|
|
|
((keyval == keysyms.Up or keyval == keysyms.KP_Up) and
|
|
|
|
state == gdk.MOD1_MASK)):
|
2007-05-30 19:29:29 +02:00
|
|
|
self._close_palette_cb()
|
2007-05-29 18:27:54 +02:00
|
|
|
elif keyval == keysyms.Tab:
|
2007-06-01 06:08:24 +02:00
|
|
|
self._close_palette_cb()
|