#!/usr/bin/env python import gobject import gtk import gtk.gdk class SessionWidget (gtk.Widget): # FIXME: # Should be able override do_size_allocate() without this # See bug #308099 __gsignals__ = { 'size-allocate' : 'override' } def __init__ (self, session_width, session_height): gtk.Widget.__init__ (self) self.set_flags (self.flags() | gtk.NO_WINDOW) self.set_property ("can-focus", True) self.session_width = session_width self.session_height = session_height def do_size_request (self, requisition): focus_width = self.style_get_property ("focus-line-width") focus_pad = self.style_get_property ("focus-padding") requisition.width = self.session_width + 2 * focus_width + 2 * focus_pad requisition.height = self.session_height + 2 * focus_width + 2 * focus_pad def __calculate_position (self, allocation): focus_width = self.style_get_property ("focus-line-width") focus_pad = self.style_get_property ("focus-padding") x = allocation.x + focus_width + focus_pad y = allocation.y + focus_width + focus_pad alloc_width = allocation.width - 2 * focus_width - 2 * focus_pad alloc_height = allocation.height - 2 * focus_width - 2 * focus_pad x += max (0, (alloc_width - self.session_width) / 2) y += max (0, (alloc_height - self.session_height) / 2) return (x, y) def do_size_allocate (self, allocation): self.allocation = allocation if self.flags() & gtk.REALIZED: (x, y) = self.__calculate_position (allocation) self.session_window.move_resize (x, y, self.session_width, self.session_height) def do_realize (self): gtk.Widget.do_realize (self) (x, y) = self.__calculate_position (self.allocation) self.session_window = gtk.gdk.Window (parent = self.get_parent_window (), window_type = gtk.gdk.WINDOW_CHILD, x = x, y = y, width = self.session_width, height = self.session_height, wclass = gtk.gdk.INPUT_OUTPUT, visual = self.get_visual (), colormap = self.get_colormap (), event_mask = gtk.gdk.BUTTON_PRESS_MASK) self.session_window.set_user_data (self) def do_unrealize (self): # FIXME: # Causes a warning with pygtk 2.6.2 # See bug #308384 self.session_window.set_user_data (None) self.session_window.destroy () gtk.Widget.do_unrealize (self) def do_map (self): gtk.Widget.do_map (self) self.session_window.show () def do_unmap (self): self.session_window.hide () gtk.Widget.do_unmap (self) def do_expose_event (self, event): if self.flags() & gtk.HAS_FOCUS: focus_width = self.style_get_property ("focus-line-width") focus_pad = self.style_get_property ("focus-padding") (x, y) = self.__calculate_position (self.allocation) self.style.paint_focus (self.window, self.state, event.area, widget, "sabayon-session", x - focus_width - focus_pad, y - focus_width - focus_pad, self.session_width + 2 * focus_width + 2 * focus_pad, self.session_height + 2 * focus_width + 2 * focus_pad) return False def do_key_press_event (self, event): print "key press" return False def do_key_release_event (self, event): print "key release" return False def do_button_press_event (self, event): print "button press" if not self.flags() & gtk.HAS_FOCUS: self.grab_focus () return False gobject.type_register (SessionWidget) if __name__ == "__main__": window = gtk.Window () window.connect ("delete-event", gtk.main_quit) box = gtk.VBox (False, 5) window.add (box) box.show () button = gtk.Button (gtk.STOCK_ADD) box.pack_start (button) button.show () widget = SessionWidget (768, 576) box.pack_start (widget, True, False, 0) widget.show () button = gtk.Button (gtk.STOCK_REMOVE) box.pack_start (button) button.show () window.show () gtk.main ()