Back to index

system-config-printer  1.3.9+20120706
gtk_label_autowrap.py
Go to the documentation of this file.
00001 #
00002 # Copyright (C) 2004, 2007 Red Hat, Inc.
00003 # Authors:
00004 # Thomas Woerner <twoerner@redhat.com>
00005 # Florian Festi <ffesti@redhat.com>
00006 #
00007 # This program is free software; you can redistribute it and/or modify
00008 # it under the terms of the GNU General Public License as published by
00009 # the Free Software Foundation; either version 2 of the License, or
00010 # (at your option) any later version.
00011 #
00012 # This program is distributed in the hope that it will be useful,
00013 # but WITHOUT ANY WARRANTY; without even the implied warranty of
00014 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015 # GNU General Public License for more details.
00016 #
00017 # You should have received a copy of the GNU General Public License
00018 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
00019 #
00020 
00021 import gtk, pango
00022 
00023 ### set autowrapping for all labels in this widget tree
00024 def set_autowrap(widget):
00025     if isinstance(widget, gtk.Container):
00026         children = widget.get_children()
00027         for i in xrange(len(children)):
00028             set_autowrap(children[i])
00029     elif isinstance(widget, gtk.Label) and widget.get_line_wrap():
00030         widget.connect_after("size-allocate", label_size_allocate)
00031         widget.set_property("xalign", 0)
00032         widget.set_property("yalign", 0)
00033 
00034 ### set wrap width to the pango.Layout of the labels ###
00035 def label_size_allocate(widget, allocation):
00036     layout = widget.get_layout()
00037 
00038     lw_old, lh_old = layout.get_pixel_size()
00039 
00040     # fixed width labels
00041     if lw_old == allocation.width:
00042         return
00043 
00044     layout.set_width(allocation.width * pango.SCALE)
00045     lw, lh = layout.get_pixel_size()
00046 
00047     if lh_old != lh:
00048         widget.set_size_request(-1, lh)
00049 
00050 ##############################################################################
00051 
00052 if __name__ == "__main__":
00053     window = gtk.Window(gtk.WINDOW_TOPLEVEL)
00054     window.connect("delete_event", gtk.main_quit)
00055 
00056     label = gtk.Label("When you invoke GCC, it normally does preprocessing, compilation, assembly and linking.")
00057     label.set_line_wrap(True)
00058     label.set_use_markup(True)
00059     label.set_property("xalign", 1)
00060     label.set_property("yalign", 1)
00061 
00062     hbox = gtk.HBox()
00063     hbox.pack_start(label)
00064 #    window.add(label)
00065     window.add(hbox)
00066     set_autowrap(window)
00067 
00068     window.set_resizable(True)
00069     window.show_all()
00070     gtk.main()