#!/usr/bin/python

import sys
import glib
from gi.repository import Gdk, Gtk, Vte

class TestTerminal(Vte.Terminal):
    __gtype__name = "TestTerminal"
    
    def __init__(self):
        super(TestTerminal, self).__init__()
        self.set_size(80, 36)
        self.connect("child-exited", self.child_exited_cb)
        glib.idle_add(self.start_shell)
    
    def start_shell(self):
        # No get_user_shell() method in python
        sh = "/bin/sh"
        # No spawn_sync() either
        self.fork_command_full(0, None, [sh], None, 0, None, None)
        return False
    
    def get_geometry_hints(self):
        # Can't get style properties ("inner-border") due to bindings bug
        padding_w = 2
        padding_h = 2
        cell_w = self.get_char_width()
        cell_h = self.get_char_height()
        g = Gdk.Geometry()
        g.min_width = padding_w + cell_w
        g.min_height = padding_h + cell_h
        g.base_width = padding_w
        g.base_height = padding_h
        g.width_inc = cell_w
        g.height_inc = cell_h
        h = Gdk.WindowHints
        return [g, h.MIN_SIZE | h.BASE_SIZE | h.RESIZE_INC, 80, 36]
    
    def child_exited_cb(self, *args):
        Gtk.main_quit()
        

class TerminalWindow(Gtk.Window):
    __gtype_name = "TerminalWindow"
    
    def __init__(self, with_tab = False):
        super(TerminalWindow, self).__init__()
        self.connect("destroy", self.destroy_cb)
        self.set_title("VTE test")
        self.vte = TestTerminal()
        if with_tab:
            self.notebook = Gtk.Notebook()
            self.notebook.append_page(self.vte, Gtk.Label("VTE test"))
            self.add(self.notebook)
        else:
            self.add(self.vte)
        hints = self.vte.get_geometry_hints()
        self.set_geometry_hints(self.vte, hints[0], hints[1])
        self.set_default_geometry(hints[2], hints[3])
        self.show_all()
    
    def destroy_cb(self, *args):
        Gtk.main_quit()


w = TerminalWindow("-t" in sys.argv)
Gtk.main()
