Advertisement
Guest User

Untitled

a guest
Oct 15th, 2014
248
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.27 KB | None | 0 0
  1. from gi.repository import Gtk, cairo
  2.  
  3. class OwnContainer(Gtk.Container):
  4.     def __init__(self):
  5.         Gtk.Container.__init__(self)
  6.         self.set_has_window(False)
  7.  
  8.         self.child_widget = None
  9.  
  10.     # own method to set child ...
  11.     def set_child(self, child):
  12.         if self.child_widget is not None:
  13.             return
  14.  
  15.         self.child_widget = child
  16.         self.child_widget.set_parent(self)
  17.  
  18.     # ... and default one
  19.     def do_add(self, child):
  20.         self.set_child(child)
  21.  
  22.     def do_remove(self, child):
  23.         child.unparent()
  24.         self.child_widget = None
  25.  
  26.     def do_get_request_mode(self):
  27.         return Gtk.SizeRequestMode.WIDTH_FOR_HEIGHT
  28.  
  29.     # all 'preferred' methods are actually stubs and return fixed values
  30.     # container should calculate that values according to its content
  31.     def do_get_preferred_width(self):
  32.         return (200, 200)
  33.  
  34.     def do_get_preferred_height(self):
  35.         return (260,260)
  36.  
  37.     def do_get_preferred_width_for_height(self, height):
  38.         return (200, 200)
  39.  
  40.     def do_get_preferred_height_for_width(self, width):
  41.         return (260, 260)
  42.  
  43.     # propagate action to childs
  44.     # Some bug occur here on window destroy, see http://stackoverflow.com/questions/21214732/custom-gtk-container-with-pygobject
  45.     def do_forall(self , include_internals , callback):
  46.         if self.child_widget is not None:
  47.             callback(self.child_widget)
  48.  
  49.     # that method decides where to put child
  50.     # implement it as you wish
  51.     def do_size_allocate(self, allocation):
  52.         self.set_allocation(allocation)
  53.  
  54.         if self.child_widget is not None:
  55.             if self.child_widget.get_visible():
  56.                 print('showed')
  57.                 button_alloc = cairo.RectangleInt()
  58.  
  59.                 button_alloc.x = allocation.x + 60
  60.                 button_alloc.y = allocation.y + 60
  61.  
  62.                 button_alloc.width = 100
  63.                 button_alloc.height = 140
  64.  
  65.                 self.child_widget.size_allocate(button_alloc)
  66.  
  67.  
  68.  
  69. def main():
  70.     win = Gtk.Window()
  71.     win.connect('destroy', Gtk.main_quit)
  72.  
  73.     container = OwnContainer()
  74.  
  75.     button = Gtk.Button('Hello')
  76.     container.set_child(button)
  77.  
  78.     win.add(container)
  79.  
  80.     win.show_all()
  81.  
  82.     Gtk.main()
  83.  
  84. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement