[GRASS-SVN] r59497 - grass/trunk/gui/wxpython/gui_core

svn_grass at osgeo.org svn_grass at osgeo.org
Fri Mar 28 12:58:36 PDT 2014


Author: annakrat
Date: 2014-03-28 12:58:36 -0700 (Fri, 28 Mar 2014)
New Revision: 59497

Modified:
   grass/trunk/gui/wxpython/gui_core/forms.py
   grass/trunk/gui/wxpython/gui_core/widgets.py
Log:
wxGUI: fix StaticWrapText used in generated dialogs for wxPython 3

Modified: grass/trunk/gui/wxpython/gui_core/forms.py
===================================================================
--- grass/trunk/gui/wxpython/gui_core/forms.py	2014-03-28 14:38:06 UTC (rev 59496)
+++ grass/trunk/gui/wxpython/gui_core/forms.py	2014-03-28 19:58:36 UTC (rev 59497)
@@ -443,8 +443,8 @@
             module_desc = self.task.label + ' ' + self.task.description
         else:
             module_desc = self.task.description
-        self.description = StaticWrapText(parent = self.panel,
-                                          label = module_desc)
+        self.description = StaticWrapText(parent=self.panel,
+                                          label=module_desc, margin=10)
         topsizer.Add(item = self.description, proportion = 1, border = 5,
                      flag = wx.ALL | wx.ALIGN_CENTER_VERTICAL | wx.EXPAND)
         

Modified: grass/trunk/gui/wxpython/gui_core/widgets.py
===================================================================
--- grass/trunk/gui/wxpython/gui_core/widgets.py	2014-03-28 14:38:06 UTC (rev 59496)
+++ grass/trunk/gui/wxpython/gui_core/widgets.py	2014-03-28 19:58:36 UTC (rev 59497)
@@ -40,6 +40,8 @@
 import wx
 import wx.lib.mixins.listctrl as listmix
 import wx.lib.scrolledpanel as SP
+from wx.lib.stattext import GenStaticText
+from wx.lib.wordwrap import wordwrap
 import wx.combo
 try:
     import wx.lib.agw.flatnotebook   as FN
@@ -443,36 +445,45 @@
         dc.DrawRectangle(0, 0, 2 * size[0] / 5, size[1])
         dc.DrawRectangle(3 * size[0] / 5, 0, 2 * size[0] / 5, size[1])
 
-class StaticWrapText(wx.StaticText):
-    """!A Static Text field that wraps its text to fit its width,
-    enlarging its height if necessary.
-    """
-    def __init__(self, parent, id = wx.ID_ANY, label = '', *args, **kwds):
-        self.parent        = parent
-        self.originalLabel = label
-        
-        wx.StaticText.__init__(self, parent, id, label = '', *args, **kwds)
-        
-        self.SetLabel(label)
-        self.Bind(wx.EVT_SIZE, self.OnResize)
-    
+
+class StaticWrapText(GenStaticText):
+    """!A Static Text widget that wraps its text to fit parents width,
+    enlarging its height if necessary."""
+
+    def __init__(self, parent, id=wx.ID_ANY, label='', margin=0, *args, **kwds):
+        self._margin = margin
+        self._initialLabel = label
+        self.init = False
+        GenStaticText.__init__(self, parent, id, label, *args, **kwds)
+        self.Bind(wx.EVT_SIZE, self.OnSize)
+
+    def DoGetBestSize(self):
+        """!Overriden method which reports widget's best size."""
+        if not self.init:
+            self._updateLabel()
+            self.init = True
+        parent = self.GetParent()
+        newExtent = wx.ClientDC(parent).GetTextExtent(self.GetLabel())
+        # when starting, width is very small and height is big which creates very high windows
+        if newExtent[0] < newExtent[1]:
+            return (0, 0)
+        return newExtent
+
+    def OnSize(self, event):
+        self._updateLabel()
+        event.Skip()
+
+    def _updateLabel(self):
+        """!Calculates size of wrapped label"""
+        parent = self.GetParent()
+        newLabel = wordwrap(text=self._initialLabel, width=parent.GetSize()[0],
+                            dc=wx.ClientDC(parent), breakLongWords=True, margin=self._margin)
+        GenStaticText.SetLabel(self, newLabel)
+
     def SetLabel(self, label):
-        self.originalLabel = label
-        self.wrappedSize = None
-        self.OnResize(None)
+        self._initialLabel = label
+        self._updateLabel()
 
-    def OnResize(self, event):
-        if not getattr(self, "resizing", False):
-            self.resizing = True
-            newSize = wx.Size(self.parent.GetSize().width - 50,
-                              self.GetSize().height)
-            if self.wrappedSize != newSize:
-                wx.StaticText.SetLabel(self, self.originalLabel)
-                self.Wrap(newSize.width)
-                self.wrappedSize = newSize
-                
-                self.SetSize(self.wrappedSize)
-            del self.resizing
 
 class BaseValidator(wx.PyValidator):
     def __init__(self):



More information about the grass-commit mailing list