#!/usr/bin/python3
# -*- coding:utf-8 -*-

#  Copyright © 2011-2013  B. Clausius <barcc@gmx.de>
#
#  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
#  (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 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 <http://www.gnu.org/licenses/>.

from __future__ import print_function, division

import sys, os
sys.path.insert(0, '..')

import gi.repository
from gi.repository import GObject
from gi.repository import Gio
from gi.repository import Gtk
from gi.repository import GtkSource

Gedit = sys.modules['__builtin__']
sys.modules['gi.repository'].Gedit = Gedit


class _Activatable (object):
    pass
    
    
class _Document (GtkSource.Buffer):
    '''Emulate Gedit.Document'''
    __gsignals__ = { 'saved': (GObject.SignalFlags.RUN_FIRST, None, (object,)), }
    def __init__(self, window):
        GtkSource.Buffer.__init__(self)
        self.window = window
    def get_location(self):
        return Gio.File.new_for_path(self.window.filename)
        
        
class _Window (Gtk.Window):
    '''Emulate Gedit.Window and Gedit.Tab'''
    def __init__(self, filename):
        Gtk.Window.__init__(self)
        self.filename = filename
        self.set_title(filename)
        
        self.doc = _Document(self)
        
        vbox = Gtk.Box.new(Gtk.Orientation.VERTICAL, 0)
        scrolledwindow = Gtk.ScrolledWindow()
        self.view = GtkSource.View()
        self.view.set_buffer(self.doc)
        self.view.get_buffer().set_text(open(filename).read())
        self.view.set_show_line_marks(True)
        self.view.set_show_line_numbers(True)
        self.viewhelper = ViewHelper()
        self.viewhelper.view = self.view
        
        self.add(vbox)
        vbox.pack_start(scrolledwindow, True, True, 0)
        scrolledwindow.add(self.view)
        
        self.panel = bzrpanel.PanelHelper(self)
        self.panel.update_changed(self.view, filename)
        vbox.pack_start(self.panel.box, True, True, 0)
        self.viewhelper.do_activate()
        
        width, height = self.get_size()
        self.resize(width*4, height*2)
        self.connect_object("destroy", _Window.on_destroy, self)
        self.show_all()
        
    get_view = lambda self: self.view
    get_views = lambda self: [self.view]
    get_document = lambda self: self.doc
    get_documents = lambda self: [self.doc]
    
    def get_tab_from_location(self, location):
        return self if location.get_path() == self.filename else None
        
    def create_tab_from_location(self, *unused_args):
        print('Cannot create tab, only one document supported')
        return self
        
    def set_active_tab(self, tab):
        print('Cannot change tab, only one document supported')
        
    @staticmethod
    def get_from_document(doc):
        return doc.window
    
    def on_destroy(self):
        try:
            self.panel.deactivate()
            self.viewhelper.do_deactivate()
        finally:
            Gtk.main_quit()
            
Gedit.WindowActivatable = _Activatable
Gedit.ViewActivatable = _Activatable
Gedit.Window = _Window
Gedit.Tab = _Window
Gedit.View = GtkSource.View
from bzr_panel import bzrpanel, ViewHelper


def main(*args):
    filename = args[1 if len(args) > 1 else 0] if args else __file__
    if filename:
        filename = os.path.abspath(filename)
        
    bzrpanel.AppData.activate()
    window = _Window(filename)
    Gtk.main()
    bzrpanel.appdata.deactivate()
    
if __name__ == '__main__':
    main(*sys.argv)
    
    
