#!/usr/bin/python3
#
# Copyright (C) 2017 Christian Stadelmann
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 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 Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk
from gi.repository import Gdk


class DecorationDemo():

    window = None

    def __init__(self):
        window = Gtk.Window(Gtk.WindowType.TOPLEVEL)
        window.set_title('Window decoration demo')
        window.set_default_size(300, 120)

        label = Gtk.Label("Some dummy text\n\nfoo\nbar")
        window.add(label)

        self.window = window

        # if the window was undecorated before showing, the issue would be gone.
        window.set_decorated(True)

        window.show()
        window.get_window().move_resize(400, 400, 300, 120)

        window.connect('delete-event', self.__on_delete_event)
        window.add_events(Gdk.EventMask.BUTTON_PRESS_MASK)
        window.connect('button-press-event', self.__on_button_press_event)

    def __toggle_decorated(self):
        will_decorate = not self.window.get_decorated()
        print("decorated: %s" % will_decorate)
        self.window.set_decorated(will_decorate)

    def __on_button_press_event(self, _widget, button_event):
        if button_event.button == Gdk.BUTTON_SECONDARY:
            self.__toggle_decorated()
            return True
        return False

    def __on_delete_event(self, _widget, _event):
        Gtk.main_quit()


DecorationDemo()
Gtk.main()
