def _take_focus(self): log("Focus -> world window", type="focus") assert self.flags() & gtk.REALIZED # Weird hack: we are a GDK window, and the only way to properly get # input focus to a GDK window is to send it WM_TAKE_FOCUS. So this is # sending a WM_TAKE_FOCUS to our own window, which will go to the X # server and then come back to our own process, which will then issue # an XSetInputFocus on itself. Note not swallowing errors here, this # should always succeed. now = gtk.gdk.x11_get_server_time(self.window) send_wm_take_focus(self.window, now)
def do_map(self, *args): gtk.Window.do_map(self, *args) # We are being mapped, so we can focus ourselves. # Check for the property, just in case this is the second time we are # being mapped -- otherwise we might miss the special call to # reset_x_focus in do_focus_in_event: if not self.get_property("has-toplevel-focus"): # Take initial focus upon being mapped. Technically it is illegal # (ICCCM violating) to use CurrentTime in a WM_TAKE_FOCUS message, # but GTK doesn't happen to care, and this guarantees that we # *will* get the focus, and thus a real FocusIn event. current_time = const["CurrentTime"] send_wm_take_focus(self.window, current_time)
def test_send_wm_take_focus(self): self.evs = [] win = self.window() l.add_event_receiver(win, self) gtk.gdk.flush() l.send_wm_take_focus(win, 1234) gtk.main() assert len(self.evs) == 1 event = self.evs[0] assert event is not None assert event.window is win assert event.message_type == "WM_PROTOCOLS" assert event.format == 32 assert event.data == (l.get_xatom("WM_TAKE_FOCUS"), 1234, 0, 0, 0)
def test_send_wm_take_focus_large_time(self): self.evs = [] win = self.window() l.add_event_receiver(win, self) gtk.gdk.flush() l.send_wm_take_focus(win, 0xff000000) gtk.main() assert len(self.evs) == 1 event = self.evs[0] assert event is not None assert event.window is win assert event.message_type == "WM_PROTOCOLS" assert event.format == 32 assert event.data == (l.get_xatom("WM_TAKE_FOCUS"), 0xff000000, 0, 0, 0)