from gi.repository import Gtk
from gi.repository import Gdk

class MainWindow(Gtk.Window):
    
    def __init__(self):
        Gtk.Window.__init__(self, title="Teste")
        self.set_size_request(700, 500)
        self.set_resizable(False)
        self.connect("delete_event", lambda w,e: Gtk.main_quit())
        
        self.settings = Gtk.Settings.get_default()
        self.settings.set_property("gtk-application-prefer-dark-theme", True)
        
        self.header = Gtk.HeaderBar()
        self.header.set_show_close_button(True)
        self.set_titlebar (self.header)
        self.header.show()
        
        self.header.set_title ("Welcome to Pitivi")
        self.header.set_subtitle ("Select a project or create a new one")
        
        self._createUI()
        self._applyCSS()
        self._createIcons()
        
        self.show()
        
    def _createUI(self):
        
        # Top buttons
        
        # Left
        self.open = Gtk.Button.new_from_icon_name ("document-open-symbolic", Gtk.IconSize.SMALL_TOOLBAR)
        self.open.set_label("Open project")
        self.open.set_always_show_image (True)
        self.open.show()
        self.header.pack_start(self.open)
        
        
        # Right
        self.create = Gtk.Button("New project")
        self.create.show()
        self.create.get_style_context().add_class("suggested-action")
        self.header.pack_end(self.create)
        
        # Main view
        grid = Gtk.Grid(expand=True)
        grid.set_orientation(Gtk.Orientation.VERTICAL)
        grid.set_hexpand(True)
        grid.set_vexpand(True)
        self.add (grid)
        
        self.flowbox = Gtk.FlowBox(hexpand=True, vexpand=False, 
                                   max_children_per_line=4, homogeneous=True,
                                   column_spacing=5, row_spacing=5)
        
        viewport = Gtk.Viewport(expand=True)
        viewport.add(self.flowbox)
        viewport.show()
        
        self.scrolled_window = Gtk.ScrolledWindow()
        self.scrolled_window.add(viewport)
        self.scrolled_window.show()
        grid.add (self.scrolled_window)
        
        self.flowbox.show()
        
        # Bottom panel
        self.bottom_frame = Gtk.Frame(hexpand=True, vexpand=False)
        self.bottom_frame.set_size_request(0,50)
        grid.add(self.bottom_frame)
        
        grid2 = Gtk.Grid(expand=True, margin=5, column_spacing=5)
        
        # Help button
        help = Gtk.Button.new_from_icon_name ("system-help-symbolic", Gtk.IconSize.SMALL_TOOLBAR)
        help.set_vexpand(True)
        help.set_label("Help")
        help.set_always_show_image (True)
        help.show()
        
        grid2.add (help)
        grid2.add (Gtk.Box(hexpand=True))
        
        # Keyboard-settings button
        keyboard = Gtk.Button.new_from_icon_name ("preferences-desktop-keyboard-shortcuts-symbolic", Gtk.IconSize.SMALL_TOOLBAR)
        keyboard.set_vexpand(True)
        keyboard.set_label("Keyboard shortcuts")
        keyboard.set_always_show_image (True)
        keyboard.show()
        
        grid2.add (keyboard)
        grid2.add (Gtk.Button("Missing dependencies..."))
        grid2.show()
        
        self.bottom_frame.add (grid2)
        
        grid.show_all()

    def _applyCSS(self):
        self.css = """
        GtkFrame {
            border-bottom: 0px;
            border-right: 0px;
            border-left: 0px;
        }
        
        GtkFlowBox {
            border: solid 0px #000
        }
        
        GtkViewport {
            border: solid 0px #000
        }
        
        """
        self.provider = Gtk.CssProvider()
        Gtk.StyleContext.add_provider_for_screen (Gdk.Screen.get_default(),
                                                  self.provider,
	                                              Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION);
        self.provider.load_from_data (self.css)
        
    # Create some useless icons
    def _createIcons(self):
        
        for i in range(30):
            item = Gtk.FlowBoxChild()
            item.set_size_request(100, 100)
            
            grid = Gtk.Grid()
            grid.set_orientation(Gtk.Orientation.VERTICAL)
            
            image = Gtk.Image()
            image.set_from_icon_name("camera-video-symbolic", Gtk.IconSize.SMALL_TOOLBAR)
            image.set_hexpand(True)
            image.set_vexpand(True)
            
            grid.add (image)
            grid.add (Gtk.Label("Test %d"%(i+1)))

            item.add (grid)
            item.show_all()
            
            self.flowbox.add (item)
    

def main():
    main = MainWindow()
    Gtk.main()


if __name__ == "__main__":
    main()
