1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
|
# -*- coding:utf-8 -*-
import pygtk
pygtk.require('2.0')
import gtk
import gobject
#------------------------------------------------------------------------------
class Palette(gtk.Fixed):
"""Palette toolbar with automatic lines/columns adaptation."""
def __init__(self, parent=None):
gtk.Fixed.__init__(self)
self.set_size_request(50, -1)
if parent == None:
parent = self
###self.set_redraw_on_allocate(False)
###parent.connect('size-allocate', self.display_update)
parent.connect('expose-event', self.display_update)
self.tools = []
self.show_all()
gobject.timeout_add(10, self.display_update)
return
def add_tool(self, tool):
"""Add a tool to the palette."""
self.tools.append(tool)
self.put(tool, 0, 0)
tool.show_all()
return
def remove_tool(self, tool):
"""Remove the specified tool."""
self.tools.remove(tool)
tool.destroy()
return
def display_update(self, widget=None, data=None):
"""Callback to execute when the size of the widget is changed."""
# Get tools max size
w = max([1] + map(lambda t: t.get_allocation().width, self.tools))
h = max([1] + map(lambda t: t.get_allocation().height, self.tools))
print w, h, self.get_allocation().width, self.get_allocation().height, len(self.tools)
# Compute grid size
col = int(self.get_allocation().width / w)
if col < 1:
col = 1
row = int(len(self.tools) / col) + 1
if row*col < len(self.tools):
row += 1
# Attach tools to the grid
for i in range(len(self.tools)):
r = int(i / col)
c = i - r*col
self.move(self.tools[i], c*w, r*h)
return False
#------------------------------------------------------------------------------
if __name__ == "__main__":
win = gtk.Window()
win.connect('destroy', gtk.main_quit)
win.set_default_size(150, 250)
sw = gtk.ScrolledWindow()
sw.set_policy(gtk.POLICY_NEVER, gtk.POLICY_AUTOMATIC)
sw.set_shadow_type(gtk.SHADOW_NONE)
win.add(sw)
p = Palette(win)
for i in range(25):
img = gtk.Image()
img.set_from_stock(gtk.STOCK_HOME, gtk.ICON_SIZE_MENU)
b = gtk.Button()
b.add(img)
p.add_tool(b)
sw.add_with_viewport(p)
win.show_all()
gtk.main() |
Partager