Example #1
0
    def __init__(self, file=None, **props):
        super().__init__(**dict(
            default_width=640, default_height=480, icon_name='eddy', **props))
        self.__file = file
        self.__saved = True
        self.__close_after_save = False

        grid = Gtk.Grid(orientation=Gtk.Orientation.VERTICAL)
        self.add(grid)

        self.__menu_stack = MenuStack(self)
        grid.attach(self.__menu_stack, 0, self.__ROW_MENU, 1, 1)

        scroller = Gtk.ScrolledWindow(shadow_type=Gtk.ShadowType.IN,
                                      expand=True)

        self.__source_view = GtkSource.View(expand=True, monospace=True)
        scroller.add(self.__source_view)
        scroller.show_all()
        self.buffer.connect('mark-set', self.__on_mark_set)
        self.__on_cursor_position_changed(self.buffer.get_start_iter())

        if file is None:
            grid.attach(scroller, 0, self.__ROW_TEXT_VIEW, 1, 1)
            self.buffer.connect('changed', self.__on_buffer_changed)
        else:
            hgrid = Gtk.Grid(orientation=Gtk.Orientation.HORIZONTAL)
            label = Gtk.Label(_('Loading…'))
            hgrid.add(label)
            cancel_button = Gtk.Button.new_from_stock(Gtk.STOCK_CANCEL)
            hgrid.add(cancel_button)
            grid.attach(hgrid, 0, self.__ROW_TEXT_VIEW, 1, 1)
            hgrid.show_all()

            cancellable = Gio.Cancellable()
            source_file = GtkSource.File(location=file)
            loader = GtkSource.FileLoader(buffer=self.buffer, file=source_file)
            # TODO: Show progress
            loader.load_async(
                GLib.PRIORITY_DEFAULT, cancellable, None, None,
                lambda loader, result: self.__finish_loading(
                    loader, result, grid, hgrid, scroller))
            cancel_button.connect('clicked', lambda b: cancellable.cancel())

        grid.show_all()