[Sugar-devel] [PATCH v4 sugar 1/3] Properly wrap labels in the Network Control Panel (GTK#318276 workaround)

Sascha Silbe silbe at activitycentral.com
Tue Feb 14 08:51:36 EST 2012


When line wrapping is enabled (label.set_line_wrap(True)), GTK restricts
labels to an arbitrary size, rather than utilising the entire allocated space.
This has been known upstream since 2005 (GTK#318276 [1]), but remains unfixed
to date.

Work around this bug by using a size-allocate signal handler that sets the
requested size of the label to the actual allocation.

[1] https://bugzilla.gnome.org/show_bug.cgi?id=318276

Signed-off-by: Sascha Silbe <silbe at activitycentral.com>
---
This is v4 of the series, but the first time it includes this patch.

 extensions/cpsection/network/view.py |   42 +++++++++++++++++++++++++---------
 1 files changed, 31 insertions(+), 11 deletions(-)

diff --git a/extensions/cpsection/network/view.py b/extensions/cpsection/network/view.py
index 381dcb6..6298f5c 100644
--- a/extensions/cpsection/network/view.py
+++ b/extensions/cpsection/network/view.py
@@ -31,6 +31,28 @@ TITLE = _('Network')
 _APPLY_TIMEOUT = 3000


+class WrappedLabel(gtk.Label):
+    """Variant of gtk.Label that automatically line-wraps the text as needed.
+
+    In theory, stock gtk.Label can already do this, but in practice a
+    long-standing bug in GTK prevents it from working as expected.
+    This class includes a workaround for that bug.
+
+    Cave-at: WrappedLabel.set_alignment() will stop working.
+    """
+
+    def __init__(self, string=None):
+        gtk.Label.__init__(self, str=string)
+        self.set_line_wrap(True)
+        self.connect('size-allocate', self.__size_allocate_cb)
+
+    def __size_allocate_cb(self, widget, rect):
+        """Propagate parent size allocation to gtk.Label"""
+        # This is a workaround for GTK bug #318276
+        # https://bugzilla.gnome.org/show_bug.cgi?id=318276
+        widget.set_size_request(rect.width, -1)
+
+
 class Network(SectionView):
     def __init__(self, model, alerts):
         SectionView.__init__(self)
@@ -66,10 +88,9 @@ class Network(SectionView):
         box_wireless.set_border_width(style.DEFAULT_SPACING * 2)
         box_wireless.set_spacing(style.DEFAULT_SPACING)

-        radio_info = gtk.Label(_('Turn off the wireless radio to save battery'
-                                 ' life'))
+        radio_info = WrappedLabel(_('Turn off the wireless radio to save'
+                                    ' battery life'))
         radio_info.set_alignment(0, 0)
-        radio_info.set_line_wrap(True)
         radio_info.show()
         box_wireless.pack_start(radio_info, expand=False)

@@ -95,10 +116,9 @@ class Network(SectionView):
             self._radio_alert.props.msg = self.restart_msg
             self._radio_alert.show()

-        history_info = gtk.Label(_('Discard network history if you have'
-                                   ' trouble connecting to the network'))
+        history_info = WrappedLabel(_('Discard network history if you have'
+                                      ' trouble connecting to the network'))
         history_info.set_alignment(0, 0)
-        history_info.set_line_wrap(True)
         history_info.show()
         box_wireless.pack_start(history_info, expand=False)

@@ -127,12 +147,12 @@ class Network(SectionView):
         box_mesh.set_border_width(style.DEFAULT_SPACING * 2)
         box_mesh.set_spacing(style.DEFAULT_SPACING)

-        server_info = gtk.Label(_("The server is the equivalent of what"
-                                  " room you are in; people on the same server"
-                                  " will be able to see each other, even when"
-                                  " they aren't on the same network."))
+        server_info = WrappedLabel(_("The server is the equivalent of what"
+                                     " room you are in; people on the same"
+                                     " server will be able to see each other,"
+                                     " even when they aren't on the same "
+                                     " network."))
         server_info.set_alignment(0, 0)
-        server_info.set_line_wrap(True)
         box_mesh.pack_start(server_info, expand=False)
         server_info.show()

--
1.7.8.3



More information about the Sugar-devel mailing list