[GRASS-SVN] r65341 - grass/branches/releasebranch_6_4/gui/wxpython/gui_core

svn_grass at osgeo.org svn_grass at osgeo.org
Fri May 29 13:20:02 PDT 2015


Author: annakrat
Date: 2015-05-29 13:20:02 -0700 (Fri, 29 May 2015)
New Revision: 65341

Modified:
   grass/branches/releasebranch_6_4/gui/wxpython/gui_core/forms.py
   grass/branches/releasebranch_6_4/gui/wxpython/gui_core/widgets.py
Log:
wxGUI: backport r60690 from #2026 (v.surf.rst window too wide)

Modified: grass/branches/releasebranch_6_4/gui/wxpython/gui_core/forms.py
===================================================================
--- grass/branches/releasebranch_6_4/gui/wxpython/gui_core/forms.py	2015-05-29 09:35:00 UTC (rev 65340)
+++ grass/branches/releasebranch_6_4/gui/wxpython/gui_core/forms.py	2015-05-29 20:20:02 UTC (rev 65341)
@@ -403,8 +403,9 @@
             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)
         topsizer.Add(item = self.description, proportion = 1, border = 5,
                      flag = wx.ALL | wx.ALIGN_CENTER_VERTICAL | wx.EXPAND)
         

Modified: grass/branches/releasebranch_6_4/gui/wxpython/gui_core/widgets.py
===================================================================
--- grass/branches/releasebranch_6_4/gui/wxpython/gui_core/widgets.py	2015-05-29 09:35:00 UTC (rev 65340)
+++ grass/branches/releasebranch_6_4/gui/wxpython/gui_core/widgets.py	2015-05-29 20:20:02 UTC (rev 65341)
@@ -31,6 +31,9 @@
 
 import wx
 import wx.lib.scrolledpanel as SP
+from wx.lib.stattext import GenStaticText
+from wx.lib.wordwrap import wordwrap
+
 try:
     import wx.lib.agw.flatnotebook   as FN
 except ImportError:
@@ -222,36 +225,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).GetMultiLineTextExtent(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[:2]
+
+    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