dabo Commit
Revision 6102
Date: 2010-10-12 06:58:45 -0700 (Tue, 12 Oct 2010)
Author: Ed
Trac: http://trac.dabodev.com/changeset/6102

Changed:
U   trunk/dabo/ui/uiwx/dTextBoxMixin.py

Log:
Added the 'charsBeforeCursor()' and 'charsAfterCursor()' methods, as requested 
by Brendan Barnwell. By default, they return the character immediately 
before/after the insertion point, or if there is selected text, before/after 
the selection. You can optionally pass the number of characters to return if 
you want more than one. You can also specify that any selected text be included 
in the return value; by default it is not.

I'm open to renaming these methods, as I find them rather clunky, but can't 
think of anything better at the moment.



Diff:
Modified: trunk/dabo/ui/uiwx/dTextBoxMixin.py
===================================================================
--- trunk/dabo/ui/uiwx/dTextBoxMixin.py 2010-10-12 04:03:27 UTC (rev 6101)
+++ trunk/dabo/ui/uiwx/dTextBoxMixin.py 2010-10-12 13:58:45 UTC (rev 6102)
@@ -156,6 +156,49 @@
                self._inForceCase = False
 
 
+       def charsBeforeCursor(self, num=None, includeSelectedText=False):
+               """Returns the characters immediately before the current 
InsertionPoint,
+               or, if there is selected text, before the beginning of the 
current
+               selection. By default, it will return one character, but you 
can specify
+               a greater number to be returned. If there is selected text, and
+               includeSelectedText is True, this will return the string 
consisting of
+               the characters before plus the selected text.
+               """
+               if num is None:
+                       num = 1
+               return self._substringByRange(before=num, 
includeSelectedText=includeSelectedText)
+
+
+       def charsAfterCursor(self, num=None, includeSelectedText=False):
+               """Returns the characters immediately after the current 
InsertionPoint,
+               or, if there is selected text, before the end of the current 
selection.
+               By default, it will return one character, but you can specify a 
greater
+               number to be returned.
+               """
+               if num is None:
+                       num = 1
+               return self._substringByRange(after=num, 
includeSelectedText=includeSelectedText)
+
+
+       def _substringByRange(self, before=0, after=0, 
includeSelectedText=False):
+               """Handles the substring calculation for the 
chars[Before|After]Cursor()
+               methods.
+               """
+               start, end = self.GetSelection()
+               ret = ""
+               if before:
+                       if includeSelectedText:
+                               ret = self.GetRange(max(0, start - before), end)
+                       else:
+                               ret = self.GetRange(max(0, start - before), 
start)
+               else:
+                       if includeSelectedText:
+                               ret = self.GetRange(start, end + after)
+                       else:
+                               ret = self.GetRange(end, end + after)
+               return ret
+
+
        # property get/set functions
        def _getAlignment(self):
                if self._hasWindowStyleFlag(wx.TE_RIGHT):



_______________________________________________
Post Messages to: [email protected]
Subscription Maintenance: http://leafe.com/mailman/listinfo/dabo-dev
Searchable Archives: http://leafe.com/archives/search/dabo-dev
This message: 
http://leafe.com/archives/byMID/[email protected]

Reply via email to