2007-04-20 14:45:39 +02:00
|
|
|
# Copyright (C) 2007, 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.
|
|
|
|
|
|
|
|
import gtk
|
2007-06-19 22:02:25 +02:00
|
|
|
import gobject
|
2007-06-01 06:08:24 +02:00
|
|
|
import time
|
2007-04-20 14:45:39 +02:00
|
|
|
|
2007-04-30 13:42:45 +02:00
|
|
|
from sugar.graphics.icon import Icon
|
2007-05-28 07:05:31 +02:00
|
|
|
from sugar.graphics.palette import *
|
2007-04-30 13:42:45 +02:00
|
|
|
|
2007-04-26 11:31:41 +02:00
|
|
|
class ToolButton(gtk.ToolButton):
|
2007-06-19 22:02:25 +02:00
|
|
|
_POPUP_PALETTE_DELAY = 100
|
2007-06-01 06:08:24 +02:00
|
|
|
|
2007-06-01 21:26:18 +02:00
|
|
|
def __init__(self, icon_name=None):
|
2007-04-26 11:31:41 +02:00
|
|
|
gtk.ToolButton.__init__(self)
|
2007-06-06 04:43:42 +02:00
|
|
|
self._palette = None
|
2007-06-01 21:26:18 +02:00
|
|
|
self.set_icon(icon_name)
|
2007-06-19 22:02:25 +02:00
|
|
|
self.child.connect('enter-notify-event',self._enter_notify_event_cb)
|
|
|
|
self.child.connect('leave-notify-event',self._leave_notify_event_cb)
|
|
|
|
self._enter_tag = None
|
|
|
|
self._leave_tag = None
|
2007-06-01 06:08:24 +02:00
|
|
|
|
2007-06-01 21:26:18 +02:00
|
|
|
def set_icon(self, icon_name):
|
|
|
|
icon = Icon(icon_name)
|
2007-04-30 13:42:45 +02:00
|
|
|
self.set_icon_widget(icon)
|
2007-05-28 07:05:31 +02:00
|
|
|
icon.show()
|
|
|
|
|
|
|
|
def set_palette(self, palette):
|
2007-05-30 19:29:29 +02:00
|
|
|
self._palette = palette
|
2007-06-19 22:02:25 +02:00
|
|
|
self._palette.props.invoker = WidgetInvoker(self)
|
2007-06-01 06:08:24 +02:00
|
|
|
|
2007-05-28 07:05:31 +02:00
|
|
|
def set_tooltip(self, text):
|
2007-06-06 04:43:42 +02:00
|
|
|
if self._palette:
|
|
|
|
self._palette.destroy()
|
2007-05-30 19:29:29 +02:00
|
|
|
|
2007-06-25 11:41:33 +02:00
|
|
|
self._palette = Palette()
|
2007-06-06 04:43:42 +02:00
|
|
|
self._palette.set_primary_state(text)
|
2007-06-19 22:02:25 +02:00
|
|
|
self._palette.props.invoker = WidgetInvoker(self)
|
2007-06-06 04:43:42 +02:00
|
|
|
|
2007-06-19 22:02:25 +02:00
|
|
|
def _enter_notify_event_cb(self, widget, event):
|
2007-06-21 02:33:09 +02:00
|
|
|
if not self._palette:
|
|
|
|
return
|
|
|
|
|
2007-06-19 22:02:25 +02:00
|
|
|
gtk.gdk.pointer_ungrab()
|
|
|
|
|
|
|
|
if self._leave_tag:
|
|
|
|
gobject.source_remove(self._leave_tag)
|
|
|
|
self._leave_tag = None
|
|
|
|
|
|
|
|
self._enter_tag = gobject.timeout_add(self._POPUP_PALETTE_DELAY, \
|
|
|
|
self._show_palette)
|
|
|
|
|
|
|
|
def _leave_notify_event_cb(self, widget, event):
|
2007-06-21 02:33:09 +02:00
|
|
|
if not self._palette:
|
|
|
|
return
|
|
|
|
|
2007-06-19 22:02:25 +02:00
|
|
|
if self._enter_tag:
|
|
|
|
gobject.source_remove(self._enter_tag)
|
|
|
|
self._enter_tag = None
|
|
|
|
|
|
|
|
self._leave_tag = gobject.timeout_add(self._POPUP_PALETTE_DELAY,\
|
|
|
|
self._hide_palette)
|
|
|
|
|
|
|
|
def _show_palette(self):
|
2007-06-06 04:43:42 +02:00
|
|
|
self._palette.popup()
|
2007-06-19 22:02:25 +02:00
|
|
|
return False
|
|
|
|
|
|
|
|
def _hide_palette(self):
|
|
|
|
# Just hide the palette if the mouse pointer is
|
|
|
|
# out of the toolbutton and the palette
|
|
|
|
if self._is_mouse_out(self._palette):
|
|
|
|
self._palette.popdown()
|
|
|
|
else:
|
|
|
|
gtk.gdk.pointer_ungrab()
|
|
|
|
|
|
|
|
return False
|
|
|
|
|
|
|
|
def _pointer_grab(self):
|
|
|
|
gtk.gdk.pointer_grab(self.window, owner_events=True,\
|
|
|
|
event_mask=gtk.gdk.PROPERTY_CHANGE_MASK )
|
|
|
|
|
|
|
|
def _is_mouse_out(self, widget):
|
|
|
|
mouse_x, mouse_y = widget.get_pointer()
|
|
|
|
event_rect = gdk.Rectangle(mouse_x, mouse_y, 1, 1)
|
|
|
|
|
|
|
|
if (widget.allocation.intersect(event_rect).width==0):
|
|
|
|
return True
|
|
|
|
else:
|
|
|
|
return False
|