1# Terminator by Chris Jones <cmsj@tenshu.net>
2# GPL v2 only
3"""terminalshot.py - Terminator Plugin to take 'screenshots' of individual
4terminals"""
5
6import os
7from gi.repository import Gtk
8from gi.repository import GdkPixbuf
9import terminatorlib.plugin as plugin
10from terminatorlib.translation import _
11from terminatorlib.util import widget_pixbuf
12
13# Every plugin you want Terminator to load *must* be listed in 'AVAILABLE'
14AVAILABLE = ['TerminalShot']
15
16class TerminalShot(plugin.MenuItem):
17    """Add custom commands to the terminal menu"""
18    capabilities = ['terminal_menu']
19    dialog_action = Gtk.FileChooserAction.SAVE
20    dialog_buttons = (_("_Cancel"), Gtk.ResponseType.CANCEL,
21                      _("_Save"), Gtk.ResponseType.OK)
22
23    def __init__(self):
24        plugin.MenuItem.__init__(self)
25
26    def callback(self, menuitems, menu, terminal):
27        """Add our menu items to the menu"""
28        item = Gtk.MenuItem.new_with_mnemonic(_('Terminal _screenshot'))
29        item.connect("activate", self.terminalshot, terminal)
30        menuitems.append(item)
31
32    def terminalshot(self, _widget, terminal):
33        """Handle the taking, prompting and saving of a terminalshot"""
34        # Grab a pixbuf of the terminal
35        orig_pixbuf = widget_pixbuf(terminal)
36
37        savedialog = Gtk.FileChooserDialog(title=_("Save image"),
38                                           action=self.dialog_action,
39                                           buttons=self.dialog_buttons)
40        savedialog.set_transient_for(_widget.get_toplevel())
41        savedialog.set_do_overwrite_confirmation(True)
42        savedialog.set_local_only(True)
43
44        pixbuf = orig_pixbuf.scale_simple(orig_pixbuf.get_width() / 2,
45                                     orig_pixbuf.get_height() / 2,
46                                     GdkPixbuf.InterpType.BILINEAR)
47        image = Gtk.Image.new_from_pixbuf(pixbuf)
48        savedialog.set_preview_widget(image)
49
50        savedialog.show_all()
51        response = savedialog.run()
52        path = None
53        if response == Gtk.ResponseType.OK:
54            path = os.path.join(savedialog.get_current_folder(),
55                                savedialog.get_filename())
56            orig_pixbuf.savev(path, 'png', [], [])
57
58        savedialog.destroy()
59