13

I started to build a GUI with Glade, python and Gtk3 libraries. I want to try to view an external window linking it inside a container in my GUI. Is it possible? Which is the best container object to do this?

I started to search but easy methods in Gtk2 (like here) can't be used any more in Gtk3.

I found a very interesting post here which allow me to set the right ID of the target window but I'm still confused on how to show it inside my GUI.

I unsucessfully tried to change a bit these tutorials with cairo.

My piece of code so far ( I want to display the window with ID = 0x360000b in the map_area container and I have to use the "self." handles structure). Temporarily the map_area container is a Drawingarea.

# if condition occurs    
    Gdk.Window.process_all_updates()
    win_id = 0x360000b # from xwininfo command
    root_win = GdkX11.X11Display.get_default()
    win = GdkX11.X11Window.foreign_new_for_display(root_win, win_id)
    width = win.get_width()
    height = win.get_height()   
    self.map_area = cairo.ImageSurface(cairo.FORMAT_ARGB32, width, height)               
    pixbuf = Gdk.pixbuf_get_from_window(win, 0, 0, width, height)
    cr = cairo.Context(self.map_area)   
    Gdk.cairo_set_source_pixbuf(cr, pixbuf, 0, 0)
    cr.paint()

Can anyone help me? Thank you in advance!

Community
  • 1
  • 1
marcoresk
  • 1,837
  • 2
  • 17
  • 29
  • Have a look at https://developer.gnome.org/gtk3/stable/GtkSocket.html - this solution requires some cooperation from the other process though. – Phillip Sep 14 '16 at 08:57
  • Could it work even for embedding **external** process? I'm interested in a window not generated as a widget in my script. It could be like I want to embed Ubuntu terminal window or the current Mozilla Firefox browser window in a proper widget (I don't know which it could be) inside my gtk main window – marcoresk Sep 15 '16 at 09:06
  • 1
    Sure, that's what the documentation means by "other" process. *But* the external process needs to cooperate with yours; namely, it must actively reparent its top-level window to the window created by the GtkSocket. E.g., xterm has the `-into` option for this. (If you are willing to add Xlib as a dependency to your script, you can do this for the other application. I'll post an answer with an example.) – Phillip Sep 15 '16 at 09:23
  • (Turns out, reparenting is not *that* easy. There is `XReparentWindow`, and you also have access to that function from GdkX11 through the `reparent` function of a window, but at least xterm exits if one tries to reparent an already visible window. So there will be no example from me, sorry.) – Phillip Sep 15 '16 at 09:51
  • I attempted this some time ago and ended up going in a bit of a different direction as I ran into issues like you. All I ended up needing was a screenshot of the application, so I wrote something to grab a screenshot of the window specifically based off the title of the window for Mac and Linux. It also uses X11 and Quartz to do all of the heavy lifting, so it is actually pretty fast. The repository is located here: https://github.com/Kush131/PyFastCap/. It is quite ugly and hacky, but maybe it can help you! – Kush Sep 15 '16 at 16:24
  • @Phillip Thank you very much for try and confirming that is not a question made because I am sloth :) I hope in the bounty reward to "allure" others answers! – marcoresk Sep 15 '16 at 16:26
  • @Kush131 I'll have a look, thank you. – marcoresk Sep 15 '16 at 16:28
  • @marcoresk no problem at all. If you have any questions about it feel free to ask. – Kush Sep 15 '16 at 16:29

1 Answers1

1

It sounds like you want to be a window manager. Have a look at the answers to questions about how to write a window manager, such as this one:

Creating a window manager for Linux

or this one:

Building a Window Manager

Good luck!

Community
  • 1
  • 1
Bill Gribble
  • 1,797
  • 12
  • 15
  • Thank you for the answer. In the meantime, I stopped this part of my project and I moved from GTK and Glade to Qt-creator after some considerations, but I will take a look for sure! – marcoresk Nov 03 '16 at 15:46