From 6bb645b44a0db59672fc2dde9ea591f69e097d19 Mon Sep 17 00:00:00 2001 From: Eugenio Parodi Date: Mon, 21 Mar 2022 00:03:26 +0000 Subject: [PATCH] Added tab renderer in the TTkTextEdit --- TermTk/TTkCore/string.py | 15 +++++++++++++++ TermTk/TTkWidgets/texedit.py | 4 +++- demo/showcase/textedit.py | 22 +++++++++++++++++++--- 3 files changed, 37 insertions(+), 4 deletions(-) diff --git a/TermTk/TTkCore/string.py b/TermTk/TTkCore/string.py index 68c543b9..0135cd27 100644 --- a/TermTk/TTkCore/string.py +++ b/TermTk/TTkCore/string.py @@ -130,6 +130,21 @@ class TTkString(): def __gt__(self, other): return self._text > other._text def __ge__(self, other): return self._text >= other._text + def tab2spaces(self, tabSpaces): + ret = TTkString() + slices = self._text.split("\t") + ret._text += slices[0] + pos = len(slices[0]) + ret._colors += self._colors[0:pos] + for s in slices[1:]: + c = self._colors[pos] + lentxt = len(ret._text) + spaces = tabSpaces - (lentxt+tabSpaces)%tabSpaces + ret._text += " "*spaces + s + ret._colors += [c]*spaces + self._colors[pos+1:pos+1+len(s)] + pos+=len(s)+1 + return ret + def toAscii(self): ''' Return the ascii representation of the string ''' return self._text diff --git a/TermTk/TTkWidgets/texedit.py b/TermTk/TTkWidgets/texedit.py index 03257c90..2e85ab29 100644 --- a/TermTk/TTkWidgets/texedit.py +++ b/TermTk/TTkWidgets/texedit.py @@ -35,6 +35,7 @@ class _TTkTextEditView(TTkAbstractScrollView): __slots__ = ( '_lines', '_hsize', '_cursorPos', '_cursorParams', '_selectionFrom', '_selectionTo', + '_tabSpaces', '_replace', '_readOnly' ) @@ -44,6 +45,7 @@ class _TTkTextEditView(TTkAbstractScrollView): self._readOnly = True self._hsize = 0 self._lines = [''] + self._tabSpaces = 4 self._replace = False self._cursorPos = (0,0) self._selectionFrom = (0,0) @@ -51,7 +53,6 @@ class _TTkTextEditView(TTkAbstractScrollView): self._cursorParams = None self.setFocusPolicy(TTkK.ClickFocus + TTkK.TabFocus) - def isReadOnly(self) -> bool : return self._readOnly @@ -314,6 +315,7 @@ class _TTkTextEditView(TTkAbstractScrollView): h = self.height() for y, t in enumerate(self._lines[oy:oy+h]): + t = t.tab2spaces(self._tabSpaces) if self._selectionFrom[1] <= y+oy <= self._selectionTo[1]: pf = 0 if y+oy > self._selectionFrom[1] else self._selectionFrom[0] pt = len(t) if y+oy < self._selectionTo[1] else self._selectionTo[0] diff --git a/demo/showcase/textedit.py b/demo/showcase/textedit.py index 0831a503..e4214eec 100755 --- a/demo/showcase/textedit.py +++ b/demo/showcase/textedit.py @@ -46,12 +46,28 @@ def getSentence(a,b,i): def demoTextEdit(root=None): te = ttk.TTkTextEdit(parent=root) te.setReadOnly(False) - te.setText("Text Edit DEMO\n") + + te.setText(ttk.TTkString("Text Edit DEMO\n",ttk.TTkColor.UNDERLINE+ttk.TTkColor.BOLD+ttk.TTkColor.ITALIC)) + # Load ANSI input - te.append("ANSI Input Test\n") + te.append(ttk.TTkString("ANSI Input Test\n",ttk.TTkColor.UNDERLINE+ttk.TTkColor.BOLD)) with open(os.path.join(os.path.dirname(os.path.abspath(__file__)),'textedit.colors.txt')) as f: te.append(f.read()) - te.append("Random TTkString Input Test\n") + + # Test Tabs + te.append(ttk.TTkString("Tabs Test\n",ttk.TTkColor.UNDERLINE+ttk.TTkColor.BOLD)) + te.append("Word\tAnother Word\tYet more words") + te.append("What a wonderful word\tOut of this word\tBattle of the words\n") + te.append("tab\ttab\ttab\ttab\ttab\ttab\ttab\ttab\ttab\ttab\ttab\ttab") + te.append("-tab\ttab\ttab\ttab\ttab\ttab\ttab\ttab\ttab\ttab\ttab\ttab") + te.append("--tab\ttab\ttab\ttab\ttab\ttab\ttab\ttab\ttab\ttab\ttab\ttab") + te.append("---tab\ttab\ttab\ttab\ttab\ttab\ttab\ttab\ttab\ttab\ttab\ttab") + te.append("----tab\ttab\ttab\ttab\ttab\ttab\ttab\ttab\ttab\ttab\ttab\ttab") + te.append("-----tab\ttab\ttab\ttab\ttab\ttab\ttab\ttab\ttab\ttab\ttab\ttab") + te.append("------tab\ttab\ttab\ttab\ttab\ttab\ttab\ttab\ttab\ttab\ttab\ttab") + te.append("-------tab\ttab\ttab\ttab\ttab\ttab\ttab\ttab\ttab\ttab\ttab\ttab\n") + + te.append(ttk.TTkString("Random TTkString Input Test\n",ttk.TTkColor.UNDERLINE+ttk.TTkColor.BOLD)) te.append(ttk.TTkString('\n').join([ getSentence(5,25,i) for i in range(50)])) return te