| 53 | |
| 54 | |
| 55 | class Gtk3Example(Gtk.Application): |
| 56 | |
| 57 | def __init__(self): |
| 58 | super(Gtk3Example, self).__init__(application_id='cefpython.gtk3') |
| 59 | self.browser = None |
| 60 | self.window = None |
| 61 | self.win32_handle = None |
| 62 | |
| 63 | def run(self, argv): |
| 64 | GObject.threads_init() |
| 65 | GObject.timeout_add(10, self.on_timer) |
| 66 | self.connect("startup", self.on_startup) |
| 67 | self.connect("activate", self.on_activate) |
| 68 | self.connect("shutdown", self.on_shutdown) |
| 69 | return super(Gtk3Example, self).run(argv) |
| 70 | |
| 71 | def get_handle(self): |
| 72 | if WINDOWS: |
| 73 | Gdk.threads_enter() |
| 74 | ctypes.pythonapi.PyCapsule_GetPointer.restype = ctypes.c_void_p |
| 75 | ctypes.pythonapi.PyCapsule_GetPointer.argtypes = \ |
| 76 | [ctypes.py_object] |
| 77 | gpointer = ctypes.pythonapi.PyCapsule_GetPointer( |
| 78 | self.window.get_property("window").__gpointer__, None) |
| 79 | libgdk = ctypes.CDLL("libgdk-3-0.dll") |
| 80 | self.win32_handle = libgdk.gdk_win32_window_get_handle(gpointer) |
| 81 | Gdk.threads_leave() |
| 82 | return self.win32_handle |
| 83 | elif LINUX: |
| 84 | return self.window.get_property("window").get_xid() |
| 85 | elif MAC: |
| 86 | # TODO: Must call libgdk.gdk_quartz_window_get_nsview(gpointer) |
| 87 | # similarly as on Windows. |
| 88 | print("[gtk3.py] WARNING: get_handle not implemented on Mac") |
| 89 | return 0 |
| 90 | |
| 91 | def on_timer(self): |
| 92 | cef.MessageLoopWork() |
| 93 | return True |
| 94 | |
| 95 | def on_startup(self, *_): |
| 96 | self.window = Gtk.ApplicationWindow.new(self) |
| 97 | self.window.set_title("GTK 3 example (PyGObject)") |
| 98 | self.window.set_default_size(800, 600) |
| 99 | self.window.connect("configure-event", self.on_configure) |
| 100 | self.window.connect("size-allocate", self.on_size_allocate) |
| 101 | self.window.connect("focus-in-event", self.on_focus_in) |
| 102 | self.window.connect("delete-event", self.on_window_close) |
| 103 | self.add_window(self.window) |
| 104 | self.setup_icon() |
| 105 | |
| 106 | def on_activate(self, *_): |
| 107 | self.window.realize() |
| 108 | self.embed_browser() |
| 109 | self.window.show_all() |
| 110 | # Must set size of the window again after it was shown, |
| 111 | # otherwise browser occupies only part of the window area. |
| 112 | self.window.resize(*self.window.get_default_size()) |