Source code for tangram.widget.main_window

# Copyright 2017 The Tangram Developers. See the AUTHORS file at the
# top-level directory of this distribution and at
# https://github.com/renatoGarcia/tangram/blob/master/AUTHORS.
#
# This file is part of Tangram.
#
# Tangram 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 3 of the License, or
# (at your option) any later version.
#
# Tangram 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 Tangram in the COPYING and COPYING.LESSER files.
# If not, see <http://www.gnu.org/licenses/>.

from gi.repository import GLib, Gtk

from .._base import Base
from .._gui_thread import on_gtk_thread
from .._utility import Signal
from .keys import Key, InputState


class _MainWindow(Gtk.Window):
    def __init__(self, widget, title):
        Gtk.Window.__init__(self, title=title)
        self.add(widget)


[docs]class MainWindow(Base): """Main window holding all tangram widgets. All widgets inside MainWindow can be accessed as a class attribute, by its name argument set at creation. Signals: :key_press(key, input_state): Emitted when a key is pressed on mainWindow. :key_release(key, input_state): Emitted when a key is released on mainWindow. """
[docs] @on_gtk_thread def __init__(self, widget, *, title: str = "Tangram"): super().__init__() self._gtk_obj = _MainWindow(widget._gtk_obj, title) self._widget = widget self.key_press = Signal() self.key_release = Signal() self.destroy = Signal() self._gtk_obj.connect('key-press-event', self.__on_key_press) self._gtk_obj.connect('key-release-event', self.__on_key_release) self._gtk_obj.connect('focus-out-event', self.__on_focus_out_event) self._gtk_obj.connect('destroy', lambda widget: self.destroy.emit())
def __getattr__(self, name): try: return Base._get_item(name) except KeyError: pass raise AttributeError(name) def __on_key_press(self, widget, event): key = Key(event.keyval) Base._pressed_keys.add(key) input_state = InputState(Base._pressed_keys, event.state) self.key_press.emit(key, input_state) return GLib.SOURCE_REMOVE def __on_key_release(self, widget, event): key = Key(event.keyval) Base._pressed_keys.discard(key) input_state = InputState(Base._pressed_keys, event.state) self.key_release.emit(key, input_state) return GLib.SOURCE_REMOVE def __on_focus_out_event(cls, widget, event): Base._pressed_keys.clear() return GLib.SOURCE_REMOVE @on_gtk_thread def show(self): self._gtk_obj.show_all() @on_gtk_thread def close(self): self._gtk_obj.close()