2007-04-26 11:31:41 +02:00
|
|
|
# Copyright (C) 2007, Red Hat, Inc.
|
2009-08-12 16:45:06 +02:00
|
|
|
# Copyright (C) 2009, Aleksey Lim, Sayamindu Dasgupta
|
2007-04-26 11:31:41 +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.
|
|
|
|
#
|
|
|
|
# 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.
|
|
|
|
|
2008-10-28 14:19:01 +01:00
|
|
|
"""
|
|
|
|
STABLE.
|
|
|
|
"""
|
|
|
|
|
2011-11-15 19:29:07 +01:00
|
|
|
from gi.repository import GObject
|
2013-05-06 18:18:10 +02:00
|
|
|
from gi.repository import GLib
|
2011-11-15 19:29:07 +01:00
|
|
|
from gi.repository import Gdk
|
2011-12-15 02:23:18 +01:00
|
|
|
from gi.repository import GdkX11
|
2011-11-15 19:29:07 +01:00
|
|
|
from gi.repository import Gtk
|
2007-11-26 13:14:53 +01:00
|
|
|
|
2011-10-29 10:44:18 +02:00
|
|
|
from sugar3.graphics.icon import Icon
|
|
|
|
from sugar3.graphics import palettegroup
|
2007-11-26 13:14:53 +01:00
|
|
|
|
2009-08-25 21:12:40 +02:00
|
|
|
|
2009-07-28 15:11:41 +02:00
|
|
|
_UNFULLSCREEN_BUTTON_VISIBILITY_TIMEOUT = 2
|
|
|
|
|
2009-08-25 21:12:40 +02:00
|
|
|
|
2011-11-15 19:29:07 +01:00
|
|
|
class UnfullscreenButton(Gtk.Window):
|
2007-02-27 13:41:51 +01:00
|
|
|
|
2007-02-27 20:08:33 +01:00
|
|
|
def __init__(self):
|
2011-12-15 02:23:18 +01:00
|
|
|
Gtk.Window.__init__(self)
|
2007-02-27 20:08:33 +01:00
|
|
|
|
2007-11-26 13:14:53 +01:00
|
|
|
self.set_decorated(False)
|
|
|
|
self.set_resizable(False)
|
2011-11-15 19:29:07 +01:00
|
|
|
self.set_type_hint(Gdk.WindowTypeHint.DIALOG)
|
2007-11-26 13:14:53 +01:00
|
|
|
|
|
|
|
self.set_border_width(0)
|
|
|
|
|
|
|
|
self.props.accept_focus = False
|
|
|
|
|
2013-05-18 04:27:04 +02:00
|
|
|
# Setup estimate of width, height
|
2011-12-15 02:23:18 +01:00
|
|
|
valid_, w, h = Gtk.icon_size_lookup(Gtk.IconSize.LARGE_TOOLBAR)
|
2007-11-26 13:14:53 +01:00
|
|
|
self._width = w
|
|
|
|
self._height = h
|
|
|
|
|
|
|
|
screen = self.get_screen()
|
|
|
|
screen.connect('size-changed', self._screen_size_changed_cb)
|
|
|
|
|
2011-11-15 19:29:07 +01:00
|
|
|
self._button = Gtk.Button()
|
|
|
|
self._button.set_relief(Gtk.ReliefStyle.NONE)
|
2007-11-26 13:14:53 +01:00
|
|
|
|
|
|
|
self._icon = Icon(icon_name='view-return',
|
2013-05-18 04:27:04 +02:00
|
|
|
icon_size=Gtk.IconSize.LARGE_TOOLBAR)
|
2007-11-26 13:14:53 +01:00
|
|
|
self._icon.show()
|
|
|
|
self._button.add(self._icon)
|
|
|
|
|
|
|
|
self._button.show()
|
|
|
|
self.add(self._button)
|
|
|
|
|
2012-08-09 18:09:31 +02:00
|
|
|
def connect_button_clicked(self, cb):
|
|
|
|
self._button.connect('clicked', cb)
|
2007-11-26 13:14:53 +01:00
|
|
|
|
|
|
|
def _reposition(self):
|
2011-11-15 19:29:07 +01:00
|
|
|
x = Gdk.Screen.width() - self._width
|
2007-11-26 13:14:53 +01:00
|
|
|
self.move(x, 0)
|
|
|
|
|
2011-12-15 02:23:18 +01:00
|
|
|
def do_get_preferred_width(self):
|
|
|
|
minimum, natural = Gtk.Window.do_get_preferred_width(self)
|
|
|
|
self._width = minimum
|
2007-11-26 13:14:53 +01:00
|
|
|
self._reposition()
|
2011-12-15 02:23:18 +01:00
|
|
|
return minimum, natural
|
2007-11-26 13:14:53 +01:00
|
|
|
|
|
|
|
def _screen_size_changed_cb(self, screen):
|
|
|
|
self._reposition()
|
|
|
|
|
2009-08-25 21:12:40 +02:00
|
|
|
|
2011-11-15 19:29:07 +01:00
|
|
|
class Window(Gtk.Window):
|
2009-08-25 21:12:40 +02:00
|
|
|
|
2007-11-26 13:14:53 +01:00
|
|
|
def __init__(self, **args):
|
|
|
|
self._enable_fullscreen_mode = True
|
|
|
|
|
2011-11-15 19:29:07 +01:00
|
|
|
GObject.GObject.__init__(self, **args)
|
2007-11-26 13:14:53 +01:00
|
|
|
|
2011-03-05 12:40:09 +01:00
|
|
|
self.set_decorated(False)
|
|
|
|
self.maximize()
|
2007-10-18 23:56:58 +02:00
|
|
|
self.connect('realize', self.__window_realize_cb)
|
2007-10-19 22:48:21 +02:00
|
|
|
self.connect('key-press-event', self.__key_press_cb)
|
2009-08-25 19:55:48 +02:00
|
|
|
|
2012-09-18 09:37:35 +02:00
|
|
|
# OSK support: canvas auto panning based on input focus
|
|
|
|
if GObject.signal_lookup('request-clear-area', Window) != 0 and \
|
|
|
|
GObject.signal_lookup('unset-clear-area', Window) != 0:
|
|
|
|
self.connect('size-allocate', self.__size_allocate_cb)
|
|
|
|
self.connect('request-clear-area', self.__request_clear_area_cb)
|
|
|
|
self.connect('unset-clear-area', self.__unset_clear_area_cb)
|
|
|
|
self._clear_area_dy = 0
|
|
|
|
|
2009-08-12 16:45:06 +02:00
|
|
|
self._toolbar_box = None
|
2007-10-11 20:04:04 +02:00
|
|
|
self._alerts = []
|
2009-07-08 13:16:22 +02:00
|
|
|
self._canvas = None
|
2007-10-18 23:56:58 +02:00
|
|
|
self.tray = None
|
2009-08-25 19:55:48 +02:00
|
|
|
|
2011-11-15 19:29:07 +01:00
|
|
|
self.__vbox = Gtk.VBox()
|
|
|
|
self.__hbox = Gtk.HBox()
|
|
|
|
self.__vbox.pack_start(self.__hbox, True, True, 0)
|
2009-09-10 11:46:37 +02:00
|
|
|
self.__hbox.show()
|
2007-11-04 17:16:16 +01:00
|
|
|
|
2012-10-23 09:24:56 +02:00
|
|
|
self.add_events(Gdk.EventMask.POINTER_MOTION_HINT_MASK |
|
|
|
|
Gdk.EventMask.POINTER_MOTION_MASK |
|
|
|
|
Gdk.EventMask.BUTTON_RELEASE_MASK |
|
|
|
|
Gdk.EventMask.TOUCH_MASK)
|
2011-09-11 22:41:09 +02:00
|
|
|
self.connect('motion-notify-event', self.__motion_notify_cb)
|
2012-10-23 09:24:56 +02:00
|
|
|
self.connect('button-release-event', self.__button_press_event_cb)
|
2009-07-28 15:11:41 +02:00
|
|
|
|
2009-09-10 11:46:37 +02:00
|
|
|
self.add(self.__vbox)
|
|
|
|
self.__vbox.show()
|
2007-02-27 13:41:51 +01:00
|
|
|
|
2007-11-26 13:14:53 +01:00
|
|
|
self._is_fullscreen = False
|
|
|
|
self._unfullscreen_button = UnfullscreenButton()
|
|
|
|
self._unfullscreen_button.set_transient_for(self)
|
2012-08-09 18:09:31 +02:00
|
|
|
self._unfullscreen_button.connect_button_clicked(
|
|
|
|
self.__unfullscreen_button_clicked)
|
2009-07-28 15:11:41 +02:00
|
|
|
self._unfullscreen_button_timeout_id = None
|
2007-11-26 13:14:53 +01:00
|
|
|
|
2009-09-29 20:33:13 +02:00
|
|
|
def reveal(self):
|
|
|
|
""" Make window active
|
|
|
|
|
|
|
|
In contrast with present(), brings window to the top
|
|
|
|
even after invoking on response on non-gtk events.
|
|
|
|
See #1423.
|
|
|
|
"""
|
2011-11-15 21:32:03 +01:00
|
|
|
window = self.get_window()
|
|
|
|
if window is None:
|
2009-09-29 20:33:13 +02:00
|
|
|
self.show()
|
|
|
|
return
|
2011-11-15 19:29:07 +01:00
|
|
|
timestamp = Gtk.get_current_event_time()
|
2009-09-29 20:33:13 +02:00
|
|
|
if not timestamp:
|
2012-01-19 15:14:53 +01:00
|
|
|
timestamp = GdkX11.x11_get_server_time(window)
|
2011-11-15 21:32:03 +01:00
|
|
|
window.focus(timestamp)
|
2009-09-29 20:33:13 +02:00
|
|
|
|
2013-11-20 05:38:14 +01:00
|
|
|
def is_fullscreen(self):
|
|
|
|
return self._is_fullscreen
|
|
|
|
|
2009-09-16 18:55:05 +02:00
|
|
|
def fullscreen(self):
|
2009-09-16 19:09:30 +02:00
|
|
|
palettegroup.popdown_all()
|
2009-09-16 18:55:05 +02:00
|
|
|
if self._toolbar_box is not None:
|
|
|
|
self._toolbar_box.hide()
|
|
|
|
if self.tray is not None:
|
|
|
|
self.tray.hide()
|
|
|
|
|
|
|
|
self._is_fullscreen = True
|
|
|
|
|
|
|
|
if self.props.enable_fullscreen_mode:
|
|
|
|
self._unfullscreen_button.show()
|
|
|
|
|
|
|
|
if self._unfullscreen_button_timeout_id is not None:
|
2011-11-15 19:29:07 +01:00
|
|
|
GObject.source_remove(self._unfullscreen_button_timeout_id)
|
2009-09-16 18:55:05 +02:00
|
|
|
self._unfullscreen_button_timeout_id = None
|
|
|
|
|
|
|
|
self._unfullscreen_button_timeout_id = \
|
2013-05-18 04:27:04 +02:00
|
|
|
GLib.timeout_add_seconds(
|
|
|
|
_UNFULLSCREEN_BUTTON_VISIBILITY_TIMEOUT,
|
2009-09-16 18:55:05 +02:00
|
|
|
self.__unfullscreen_button_timeout_cb)
|
|
|
|
|
|
|
|
def unfullscreen(self):
|
|
|
|
if self._toolbar_box is not None:
|
|
|
|
self._toolbar_box.show()
|
|
|
|
if self.tray is not None:
|
|
|
|
self.tray.show()
|
|
|
|
|
|
|
|
self._is_fullscreen = False
|
|
|
|
|
|
|
|
if self.props.enable_fullscreen_mode:
|
|
|
|
self._unfullscreen_button.hide()
|
|
|
|
|
|
|
|
if self._unfullscreen_button_timeout_id:
|
2011-11-15 19:29:07 +01:00
|
|
|
GObject.source_remove(self._unfullscreen_button_timeout_id)
|
2009-09-16 18:55:05 +02:00
|
|
|
self._unfullscreen_button_timeout_id = None
|
|
|
|
|
2007-04-26 11:31:41 +02:00
|
|
|
def set_canvas(self, canvas):
|
2009-07-08 13:16:22 +02:00
|
|
|
if self._canvas:
|
2011-09-11 22:41:09 +02:00
|
|
|
self.__hbox.remove(self._canvas)
|
2007-02-27 13:41:51 +01:00
|
|
|
|
2007-11-04 17:16:16 +01:00
|
|
|
if canvas:
|
2011-11-15 19:29:07 +01:00
|
|
|
self.__hbox.pack_start(canvas, True, True, 0)
|
2009-08-25 19:55:48 +02:00
|
|
|
|
2009-07-08 13:16:22 +02:00
|
|
|
self._canvas = canvas
|
2009-09-10 11:46:37 +02:00
|
|
|
self.__vbox.set_focus_child(self._canvas)
|
2009-07-08 13:16:22 +02:00
|
|
|
|
|
|
|
def get_canvas(self):
|
|
|
|
return self._canvas
|
|
|
|
|
|
|
|
canvas = property(get_canvas, set_canvas)
|
2007-04-27 10:51:19 +02:00
|
|
|
|
2009-07-31 05:49:05 +02:00
|
|
|
def get_toolbar_box(self):
|
2009-08-12 16:45:06 +02:00
|
|
|
return self._toolbar_box
|
2009-07-31 05:49:05 +02:00
|
|
|
|
|
|
|
def set_toolbar_box(self, toolbar_box):
|
2009-08-12 16:45:06 +02:00
|
|
|
if self._toolbar_box:
|
2009-09-10 11:46:37 +02:00
|
|
|
self.__vbox.remove(self._toolbar_box)
|
2009-07-31 05:49:05 +02:00
|
|
|
|
2010-07-05 03:34:14 +02:00
|
|
|
if toolbar_box:
|
2011-12-15 02:23:18 +01:00
|
|
|
self.__vbox.pack_start(toolbar_box, False, False, 0)
|
2010-07-05 03:34:14 +02:00
|
|
|
self.__vbox.reorder_child(toolbar_box, 0)
|
2009-07-31 05:49:05 +02:00
|
|
|
|
2009-08-12 16:45:06 +02:00
|
|
|
self._toolbar_box = toolbar_box
|
2009-07-31 05:49:05 +02:00
|
|
|
|
|
|
|
toolbar_box = property(get_toolbar_box, set_toolbar_box)
|
2007-10-11 20:04:04 +02:00
|
|
|
|
2007-10-18 23:56:58 +02:00
|
|
|
def set_tray(self, tray, position):
|
|
|
|
if self.tray:
|
2009-08-25 19:55:48 +02:00
|
|
|
box = self.tray.get_parent()
|
2007-10-18 23:56:58 +02:00
|
|
|
box.remove(self.tray)
|
2009-08-25 19:55:48 +02:00
|
|
|
|
2011-11-15 19:29:07 +01:00
|
|
|
if position == Gtk.PositionType.LEFT:
|
2011-12-15 02:23:18 +01:00
|
|
|
self.__hbox.pack_start(tray, False, False, 0)
|
2011-11-15 19:29:07 +01:00
|
|
|
elif position == Gtk.PositionType.RIGHT:
|
2011-12-15 02:23:18 +01:00
|
|
|
self.__hbox.pack_end(tray, False, False, 0)
|
2011-11-15 19:29:07 +01:00
|
|
|
elif position == Gtk.PositionType.BOTTOM:
|
2011-12-15 02:23:18 +01:00
|
|
|
self.__vbox.pack_end(tray, False, False, 0)
|
2009-08-25 19:55:48 +02:00
|
|
|
|
2007-10-18 23:56:58 +02:00
|
|
|
self.tray = tray
|
|
|
|
|
2007-10-11 20:04:04 +02:00
|
|
|
def add_alert(self, alert):
|
|
|
|
self._alerts.append(alert)
|
|
|
|
if len(self._alerts) == 1:
|
2011-12-15 02:23:18 +01:00
|
|
|
self.__vbox.pack_start(alert, False, False, 0)
|
2009-08-12 16:45:06 +02:00
|
|
|
if self._toolbar_box is not None:
|
2009-09-10 11:46:37 +02:00
|
|
|
self.__vbox.reorder_child(alert, 1)
|
2009-08-25 19:55:48 +02:00
|
|
|
else:
|
2009-09-10 11:46:37 +02:00
|
|
|
self.__vbox.reorder_child(alert, 0)
|
2009-08-25 19:55:48 +02:00
|
|
|
|
2007-10-11 20:04:04 +02:00
|
|
|
def remove_alert(self, alert):
|
|
|
|
if alert in self._alerts:
|
|
|
|
self._alerts.remove(alert)
|
2007-10-19 16:15:43 +02:00
|
|
|
# if the alert is the visible one on top of the queue
|
2009-08-25 19:55:48 +02:00
|
|
|
if alert.get_parent() is not None:
|
2009-09-10 11:46:37 +02:00
|
|
|
self.__vbox.remove(alert)
|
2007-10-19 16:15:43 +02:00
|
|
|
if len(self._alerts) >= 1:
|
2011-12-15 02:23:18 +01:00
|
|
|
self.__vbox.pack_start(self._alerts[0], False, False, 0)
|
2009-08-12 16:45:06 +02:00
|
|
|
if self._toolbar_box is not None:
|
2009-09-10 11:46:37 +02:00
|
|
|
self.__vbox.reorder_child(self._alerts[0], 1)
|
2007-10-19 16:15:43 +02:00
|
|
|
else:
|
2009-09-10 11:46:37 +02:00
|
|
|
self.__vbox.reorder_child(self._alert[0], 0)
|
2009-08-25 19:55:48 +02:00
|
|
|
|
2007-10-18 23:56:58 +02:00
|
|
|
def __window_realize_cb(self, window):
|
2011-11-15 19:29:07 +01:00
|
|
|
group = Gtk.Window()
|
2007-04-26 11:31:41 +02:00
|
|
|
group.realize()
|
2011-11-15 21:32:03 +01:00
|
|
|
window.get_window().set_group(group.get_window())
|
2007-06-15 18:03:17 +02:00
|
|
|
|
2007-10-19 22:48:21 +02:00
|
|
|
def __key_press_cb(self, widget, event):
|
2011-11-15 19:29:07 +01:00
|
|
|
key = Gdk.keyval_name(event.keyval)
|
|
|
|
if event.get_state() & Gdk.ModifierType.MOD1_MASK:
|
2009-02-20 16:38:41 +01:00
|
|
|
if self.tray is not None and key == 'space':
|
2007-10-19 22:48:21 +02:00
|
|
|
self.tray.props.visible = not self.tray.props.visible
|
|
|
|
return True
|
2007-11-26 13:14:53 +01:00
|
|
|
elif key == 'Escape' and self._is_fullscreen and \
|
2013-05-18 04:27:04 +02:00
|
|
|
self.props.enable_fullscreen_mode:
|
2007-11-26 13:14:53 +01:00
|
|
|
self.unfullscreen()
|
|
|
|
return True
|
2007-10-19 22:48:21 +02:00
|
|
|
return False
|
2007-11-26 13:14:53 +01:00
|
|
|
|
2012-08-09 18:09:31 +02:00
|
|
|
def __unfullscreen_button_clicked(self, button):
|
2007-11-26 13:14:53 +01:00
|
|
|
self.unfullscreen()
|
2008-04-20 22:55:05 +02:00
|
|
|
|
2012-10-23 09:24:56 +02:00
|
|
|
def __button_press_event_cb(self, widget, event):
|
|
|
|
self._show_unfullscreen_button()
|
|
|
|
return False
|
|
|
|
|
2009-07-28 15:11:41 +02:00
|
|
|
def __motion_notify_cb(self, widget, event):
|
2012-10-23 09:24:56 +02:00
|
|
|
self._show_unfullscreen_button()
|
|
|
|
return False
|
|
|
|
|
|
|
|
def _show_unfullscreen_button(self):
|
2009-07-28 15:11:41 +02:00
|
|
|
if self._is_fullscreen and self.props.enable_fullscreen_mode:
|
|
|
|
if not self._unfullscreen_button.props.visible:
|
|
|
|
self._unfullscreen_button.show()
|
2012-10-23 09:24:56 +02:00
|
|
|
|
|
|
|
# Reset the timer
|
|
|
|
if self._unfullscreen_button_timeout_id is not None:
|
|
|
|
GObject.source_remove(self._unfullscreen_button_timeout_id)
|
|
|
|
self._unfullscreen_button_timeout_id = None
|
|
|
|
|
|
|
|
self._unfullscreen_button_timeout_id = \
|
2013-05-18 04:27:04 +02:00
|
|
|
GLib.timeout_add_seconds(
|
|
|
|
_UNFULLSCREEN_BUTTON_VISIBILITY_TIMEOUT,
|
2012-10-23 09:24:56 +02:00
|
|
|
self.__unfullscreen_button_timeout_cb)
|
2009-07-28 15:11:41 +02:00
|
|
|
|
|
|
|
def __unfullscreen_button_timeout_cb(self):
|
|
|
|
self._unfullscreen_button.hide()
|
|
|
|
self._unfullscreen_button_timeout_id = None
|
|
|
|
return False
|
|
|
|
|
2012-09-18 09:37:35 +02:00
|
|
|
def __request_clear_area_cb(self, activity, osk_rect, cursor_rect):
|
|
|
|
self._clear_area_dy = cursor_rect.y + cursor_rect.height - osk_rect.y
|
2012-11-26 17:40:15 +01:00
|
|
|
|
|
|
|
if self._clear_area_dy < 0:
|
|
|
|
self._clear_area_dy = 0
|
|
|
|
return False
|
|
|
|
|
2012-09-18 09:37:35 +02:00
|
|
|
self.queue_resize()
|
2012-11-26 17:40:15 +01:00
|
|
|
return True
|
2012-09-18 09:37:35 +02:00
|
|
|
|
|
|
|
def __unset_clear_area_cb(self, activity, snap_back):
|
|
|
|
self._clear_area_dy = 0
|
|
|
|
self.queue_resize()
|
2012-11-26 17:40:15 +01:00
|
|
|
return True
|
2012-09-18 09:37:35 +02:00
|
|
|
|
|
|
|
def __size_allocate_cb(self, widget, allocation):
|
|
|
|
self.set_allocation(allocation)
|
|
|
|
allocation.y -= self._clear_area_dy
|
|
|
|
self.__vbox.size_allocate(allocation)
|
|
|
|
|
2008-04-20 22:55:05 +02:00
|
|
|
def set_enable_fullscreen_mode(self, enable_fullscreen_mode):
|
|
|
|
self._enable_fullscreen_mode = enable_fullscreen_mode
|
|
|
|
|
|
|
|
def get_enable_fullscreen_mode(self):
|
|
|
|
return self._enable_fullscreen_mode
|
|
|
|
|
2013-05-18 04:27:04 +02:00
|
|
|
enable_fullscreen_mode = GObject.property(
|
|
|
|
type=object,
|
|
|
|
setter=set_enable_fullscreen_mode,
|
|
|
|
getter=get_enable_fullscreen_mode)
|