Search code examples
gtk3pygobject

How to get preview from childs in notebook like in sublime or terminology


For example I have a window with a notebook widget and in this notebook I have two tabs which contain one vte widgets each The idea it to get a preview of each vte widgets (You know like in Terminology) here is the simple code I have so far:

 import gi
 import os
 gi.require_version('Gtk', '3.0')
 gi.require_version('Vte', '2.91')
 from gi.repository import Gtk
 from gi.repository import Vte
 from gi.repository import GLib
 from gi.repository import Gdk
 from gi.repository import GdkPixbuf
 win = Gtk.Window()
 win.connect('delete-event', Gtk.main_quit)
 notebook = Gtk.Notebook()
 win.add(notebook)
 directory = os.environ['HOME']
 env=[]
 term1 = Vte.Terminal()
 term1.spawn_sync( Vte.PtyFlags.DEFAULT, directory, ["/bin/bash"],
                   env, GLib.SpawnFlags.CHILD_INHERITS_STDIN | GLib.SpawnFlags.SEARCH_PATH,
                   None, None, )
 term1.show()
 term2 = Vte.Terminal()
 term2.spawn_sync( Vte.PtyFlags.DEFAULT, directory, ["/bin/bash"],
                   env, GLib.SpawnFlags.CHILD_INHERITS_STDIN | GLib.SpawnFlags.SEARCH_PATH,
                   None, None, )
 term2.show()
 notebook.append_page(term1)
 notebook.append_page(term2)
 notebook.show()
 notebook.next_page()

 def manage_key_press_events_cb(widget, event):
   keyval = event.keyval
   if ((event.state & (Gdk.ModifierType.CONTROL_MASK|Gdk.ModifierType.SHIFT_MASK)) == (Gdk.ModifierType.CONTROL_MASK|Gdk.ModifierType.SHIFT_MASK)):
     if keyval == Gdk.KEY_O:
       for i in range(notebook.get_n_pages()):
         child = notebook.get_nth_page(i)
         child.get_window().show()
         width = child.get_allocation().width
         height = child.get_allocation().height
         x = child.translate_coordinates(notebook,0,0)[0]
         y = child.translate_coordinates(notebook,0,0)[1]
         p = Gdk.pixbuf_get_from_window(child.get_window(), 0, 0, width, height)
         p.savev(str(i) + ".png", "png", (), ())
       return True
     else:
       return False
   else:
     return False

 win.show_all()

 win.connect('key-press-event', manage_key_press_events_cb)
 Gtk.main()

In one tab I launch htop and in another I launch glances (the applications doesn't matter, they just help me to differenciate the previews)

The problem I cannot solve is that each preview represents the currently focused tab. What ever I do I am not abble to have a preview of the tabs that are not the current one.

FYI I use python in order to give you a simple working example but initialy I have this problem with ruby from the ruby-gnome2 project. So I guess this is not related to python or ruby.


Solution

  • The GtkNotebook implementation doesn't allow to show/hide/draw its childs directly. In order to have a pixbuf of a child it must be the currently selected.

    So the idea is to cycle throught all the childs of the notebook and use the "switch-page" event in order to generate the pixbuf: see here :

    http://www.gtkforums.com/viewtopic.php?f=3&t=179132

    and here

    https://github.com/cedlemo/germinal
    https://github.com/cedlemo/germinal/blob/master/bin/terminal_chooser.rb https://github.com/cedlemo/germinal/blob/master/bin/notebook.rb