#!/usr/bin/env python3
import gi

gi.require_version("Gtk", "3.0")
from gi.repository import Gtk

"""
WindowShuffler
Author: Jacob Vlijm
Copyright © 2017-2018 Ubuntu Budgie Developers
Website=https://ubuntubudgie.org
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 3 of the License, or 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, see <https://www.gnu.org/licenses/>.
"""

css_data = """
.title {
  font-weight: bold;
}
.window {
  background-color: white;
}
.header {
  font-size: 30px;
}
.matrixmanagebutton {
  border-width: 0px;
  background-color: transparent;
}
.matrixmanagebutton:hover {
  background-color: transparent;
  border-width: 0px;
}
"""

usage_content = "Click a window and click the grid to place the " + \
                "window in the corresponding position. Shift-click to " + \
                "select multiple tiles, press [ a ] to arrange all " + \
                "windows to grid." + \
                "\n\nWindows cannot not be resized below their minimum " + \
                "sizes, nor can fixed-size windows be resized. They will " + \
                " however be positioned correctly in the grid."


class WindwoMatrix(Gtk.Window):

    def __init__(self):
        Gtk.Window.__init__(self, title="Usage & general shortcuts")
        self.connect("destroy", Gtk.main_quit)
        self.set_skip_taskbar_hint(True)
        self.set_default_size(500, 100)
        self.move(200, 100)
        self.set_decorated(False)
        self.maingrid = Gtk.Grid()
        self.add(self.maingrid)
        self.maingrid.set_border_width(30)
        # styling
        self.provider = Gtk.CssProvider.new()
        self.provider.load_from_data(css_data.encode())
        # icons
        self.bigx_1 = Gtk.Image.new_from_icon_name(
            "exit-splash-symbolic", Gtk.IconSize.MENU,
        )
        self.bigx_2 = Gtk.Image.new_from_icon_name(
            "fat-exit-splash-symbolic", Gtk.IconSize.MENU,
        )
        icon = Gtk.Image.new_from_icon_name(
            "windowshuffler-symbolic", Gtk.IconSize.DND,
        )
        # content
        self.xbox = Gtk.Box()
        self.maingrid.attach(self.xbox, 0, 0, 1, 1)
        line_extra1 = Gtk.Label("\n")
        self.maingrid.attach(line_extra1, 0, 1, 1, 1)
        self.box = Gtk.Box()
        self.box.pack_start(icon, False, False, 0)
        self.maingrid.attach(self.box, 0, 2, 2, 1)
        line_extra = Gtk.Label("\n")
        self.maingrid.attach(line_extra, 0, 3, 1, 1)
        sep = Gtk.Separator()
        self.maingrid.attach(sep, 0, 4, 2, 1)
        title = Gtk.Label("  WindowShuffler", xalign=0)
        self.set_widgetstyle(title, "header")
        self.box.pack_start(title, False, False, 0)
        usage_title = Gtk.Label("\nUsage & shortcuts", xalign=0)
        self.set_widgetstyle(usage_title, "title")
        self.maingrid.attach(usage_title, 0, 5, 2, 1)

        usage_text = Gtk.Label(usage_content, xalign=0)
        usage_text.set_line_wrap(True)
        self.maingrid.attach(usage_text, 0, 6, 2, 1)

        ntitle = 0
        for title in [
            "\nAdd a column to the grid", "\nRemove a column from the grid",
            "\nAdd a row to the grid", "\nremove a row from the grid",
            "\nArrange windows on current workspace to current grid",
            "\nTake a snapshot of the current layout",
            "\nRestore layout to snapshot",
            "\nDismiss the grid", "\nOpen (toggle) this window",
        ]:
            newtitle = Gtk.Label(title, xalign=0)
            self.maingrid.attach(newtitle, 0, 10 + ntitle, 2, 1)
            self.set_widgetstyle(newtitle, "title")
            ntitle = ntitle + 2
        ntext = 0
        for text in [
            "[ Right ] or [ + ]", "[ Left ] or [ - ]",
            "[ Down ] or [ Ctrl ] + [ + ]", "[ Up ] or [ Ctrl ] + [ - ]",
            "[ a ]", "[ s ]", "[ u ]", "[ Escape ] or [ . ] (period)", "[ i ]",
        ]:
            newtext = Gtk.Label(text, xalign=0)
            self.maingrid.attach(newtext, 0, 11 + ntext, 2, 1)
            ntext = ntext + 2
        self.set_widgetstyle(self, "window")
        self.exitbutton()
        self.maingrid.show_all()
        self.show_all()
        Gtk.main()

    def set_widgetstyle(self, widget, style):
        stylecontext = widget.get_style_context()
        stylecontext.add_class(style)
        Gtk.StyleContext.add_provider(
            stylecontext, self.provider,
            Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION,
        )

    def exitbutton(self):
        exitbutton = Gtk.Button()
        exitbutton.connect("enter-notify-event", self.change_icon)
        exitbutton.connect("leave-notify-event", self.change_icon)
        exitbutton.set_size_request(10, 10)
        exitbutton.set_image(self.bigx_1)
        exitbutton.connect("clicked", self.exit)
        exitbutton.set_relief(Gtk.ReliefStyle.NONE)
        self.set_widgetstyle(exitbutton, "matrixmanagebutton")
        self.xbox.pack_start(exitbutton, False, False, 0)

    def change_icon(self, button, event):
        enter = True if "GDK_ENTER_NOTIFY" in str(event.type) else False
        if enter:
            button.set_image(self.bigx_2)
        else:
            button.set_image(self.bigx_1)

    def exit(self, *args):
        Gtk.main_quit()


WindwoMatrix()
