布局容器(The Layout container)与固定容器(the Fixed container)类似,不过它可以在一个无限的滚动区域定位构件(其实也不能大于2^32像素)。在X系统中,窗口的宽度和高度只能限于在32767像素以你。布局容器构件使用一些特殊的技巧(doing some exotic stuff using window and bit gravities)越过这种限制。所以,即使在滚动区域你有很多子构件,也可以平滑地滚动。
用以下函数创建布局容器:
GtkWidget *gtk_layout_new( GtkAdjustment *hadjustment,
GtkAdjustment *vadjustment );
可以看到,你可以有选择地指定布局容器滚动时要使用的调整对象。
你可以用下面的两个函数在布局容器构件你添加和移动构件。
void gtk_layout_put( GtkLayout *layout,
GtkWidget *widget,
gint x,
gint y );
void gtk_layout_move( GtkLayout *layout,
GtkWidget *widget,
gint x,
gint y );
布局容器构件的尺寸可以用接下来的这个函数指定:
void gtk_layout_set_size( GtkLayout *layout,
guint width,
guint height );
最后4个函数用于操纵垂直和水平的调整对象。
GtkAdjustment* gtk_layout_get_hadjustment( GtkLayout *layout );
GtkAdjustment* gtk_layout_get_vadjustment( GtkLayout *layout );
void gtk_layout_set_hadjustment( GtkLayout *layout,
GtkAdjustment *adjustment );
void gtk_layout_set_vadjustment( GtkLayout *layout,
GtkAdjustment *adjustment);
#!/usr/bin/env python # example layout.py import pygtk pygtk.require('2.0') import gtk import random class LayoutExample: def WindowDeleteEvent(self, widget, event): # return false so that window will be destroyed return False def WindowDestroy(self, widget, *data): # exit main loop gtk.main_quit() def ButtonClicked(self, button): # move the button self.layout.move(button, random.randint(0,500), random.randint(0,500)) def __init__(self): # create the top level window window = gtk.Window(gtk.WINDOW_TOPLEVEL) window.set_title("Layout Example") window.set_default_size(300, 300) window.connect("delete-event", self.WindowDeleteEvent) window.connect("destroy", self.WindowDestroy) # create the table and pack into the window table = gtk.Table(2, 2, False) window.add(table) # create the layout widget and pack into the table self.layout = gtk.Layout(None, None) self.layout.set_size(600, 600) table.attach(self.layout, 0, 1, 0, 1, gtk.FILL|gtk.EXPAND, gtk.FILL|gtk.EXPAND, 0, 0) # create the scrollbars and pack into the table vScrollbar = gtk.VScrollbar(None) table.attach(vScrollbar, 1, 2, 0, 1, gtk.FILL|gtk.SHRINK, gtk.FILL|gtk.SHRINK, 0, 0) hScrollbar = gtk.HScrollbar(None) table.attach(hScrollbar, 0, 1, 1, 2, gtk.FILL|gtk.SHRINK, gtk.FILL|gtk.SHRINK, 0, 0) # tell the scrollbars to use the layout widget’s adjustments vAdjust = self.layout.get_vadjustment() vScrollbar.set_adjustment(vAdjust) hAdjust = self.layout.get_hadjustment() hScrollbar.set_adjustment(hAdjust) # create 3 buttons and put them into the layout widget button = gtk.Button("Press Me") button.connect("clicked", self.ButtonClicked) self.layout.put(button, 0, 0) button = gtk.Button("Press Me") button.connect("clicked", self.ButtonClicked) self.layout.put(button, 100, 0) button = gtk.Button("Press Me") button.connect("clicked", self.ButtonClicked) self.layout.put(button, 200, 0) # show all the widgets window.show_all() def main(): # enter the main loop gtk.main() return 0 if __name__ == "__main__": LayoutExample() main()