Update TextBox: Replace "spot" with "cursor".
authorRadek Brich <radek.brich@devl.cz>
Mon, 01 Sep 2014 08:45:51 +0200
changeset 104 742e504ec053
parent 103 49f212aa0228
child 105 70c9cfc70cd6
Update TextBox: Replace "spot" with "cursor".
tuikit/core/container.py
tuikit/core/coords.py
tuikit/core/widget.py
tuikit/widgets/textbox.py
tuikit/widgets/textfield.py
--- a/tuikit/core/container.py	Wed Aug 27 23:09:53 2014 +0200
+++ b/tuikit/core/container.py	Mon Sep 01 08:45:51 2014 +0200
@@ -48,13 +48,19 @@
 
     @property
     def cursor(self):
+        """Return cursor coordinates or None if cursor is not set
+        or is set outside of widget boundaries.
+
+        If this container has child with focus, return its cursor position instead.
+
+        """
         if self.focus_child:
             cursor = self.focus_child.cursor
             if cursor is not None:
                 return cursor.moved(*self.focus_child.pos)
         else:
-            if self._cursor is not None:
-                return Point(self._cursor)
+            if self._cursor in Rect._make((0, 0), self._size):
+                return self._cursor
 
     ## input events ##
 
--- a/tuikit/core/coords.py	Wed Aug 27 23:09:53 2014 +0200
+++ b/tuikit/core/coords.py	Mon Sep 01 08:45:51 2014 +0200
@@ -124,6 +124,18 @@
         self.w = w
         self.h = h
 
+    @classmethod
+    def _make(cls, origin, size):
+        """Make new Rect instance with origin and size as specified.
+
+        `origin` should be Point or pair of coordinates,
+        `size` should be Size or pair of integers
+
+        """
+        x, y = origin
+        w, h = size
+        return Rect(x, y, w, h)
+
     def __repr__(self):
         return 'Rect(x={0.x},y={0.y},w={0.w},h={0.h})'.format(self)
 
--- a/tuikit/core/widget.py	Wed Aug 27 23:09:53 2014 +0200
+++ b/tuikit/core/widget.py	Mon Sep 01 08:45:51 2014 +0200
@@ -1,4 +1,4 @@
-from tuikit.core.coords import Point, Size
+from tuikit.core.coords import Point, Size, Rect
 from tuikit.core.theme import default_theme
 
 import logging
@@ -41,8 +41,9 @@
 
         #: Cursor is position where text input will occur.
         #: It is displayed on screen if widget is active.
-        #: None means no cursor (hidden).
-        self._cursor = None
+        #: The cursor coordinates are relative to widget.
+        #: Position outside of widget boundaries means no cursor (hidden).
+        self._cursor = Point(-1, -1)
 
     ## position and size ##
 
@@ -93,8 +94,10 @@
 
     @property
     def cursor(self):
-        if self._cursor is not None:
-            return Point(self._cursor)
+        """Return cursor coordinates or None if cursor is not set
+        or is set outside of widget boundaries."""
+        if self._cursor in Rect._make((0, 0), self._size):
+            return self._cursor
 
     ## input events ##
 
--- a/tuikit/widgets/textbox.py	Wed Aug 27 23:09:53 2014 +0200
+++ b/tuikit/widgets/textbox.py	Mon Sep 01 08:45:51 2014 +0200
@@ -6,7 +6,7 @@
 
     """Multiline text view/edit widget.
 
-    Spot is used for text cursor position.
+    Cursor is used for text cursor position.
 
     """
 
@@ -19,8 +19,7 @@
 
         self.allow_focus = True
 
-        # Cursor position is same as spot.
-        # This variable rememberes horizontal position
+        # This variable rememberes horizontal position of cursor
         # for the case when cursor moves to shorter line.
         self.cursor_column = 0
         # selection - line and column of selection start
@@ -41,11 +40,11 @@
 
     @property
     def cur_line(self):
-        return self._lines[self._spot.y]
+        return self._lines[self._cursor.y]
 
     @cur_line.setter
     def cur_line(self, value):
-        self._lines[self._spot.y] = value
+        self._lines[self._cursor.y] = value
 
     def set_theme(self, theme):
         Widget.set_theme(self, theme)
@@ -59,7 +58,6 @@
             for j in range(exposed.y, end_y):
                 line = self._lines[j]
                 buffer.puts(line, 0, j)
-            #self.cursor = (self._spot.x, self._spot.y)
 
     def keypress(self, keyname, char, mod=0):
         if keyname:
@@ -74,7 +72,7 @@
             if keyname == 'backspace':   self.backspace()
             if keyname == 'delete':      self.del_char()
             if keyname == 'enter':       self.add_newline(move=True)
-            if mod == MOD_CTRL:
+            if 'ctrl' in mod:
                 if keyname == 'home':    self.move_top()
                 if keyname == 'end':     self.move_bottom()
 
@@ -87,7 +85,7 @@
     def on_mousedown(self, ev):
         y = ev.wy
         x = min(ev.wx, len(self._lines[y]))
-        self._spot.update(x=x, y=y)
+        self._cursor.update(x=x, y=y)
         self.redraw()
 
     def on_mousewheel(self, ev):
@@ -100,110 +98,110 @@
         self.redraw()
 
     def move_left(self):
-        if self._spot.x > 0:
-            self._spot.x -= 1
+        if self._cursor.x > 0:
+            self._cursor.x -= 1
         else:
-            if self._spot.y > 0:
-                self._spot.y -= 1
-                self._spot.x = len(self.cur_line)
-        self.cursor_column = self._spot.x
+            if self._cursor.y > 0:
+                self._cursor.y -= 1
+                self._cursor.x = len(self.cur_line)
+        self.cursor_column = self._cursor.x
 
     def move_right(self):
-        if self._spot.x < len(self.cur_line):
-            self._spot.x += 1
+        if self._cursor.x < len(self.cur_line):
+            self._cursor.x += 1
         else:
-            if self._spot.y < len(self._lines) - 1:
-                self._spot.y += 1
-                self._spot.x = 0
-        self.cursor_column = self._spot.x
+            if self._cursor.y < len(self._lines) - 1:
+                self._cursor.y += 1
+                self._cursor.x = 0
+        self.cursor_column = self._cursor.x
 
     def move_home(self):
-        self._spot.x = 0
-        self.cursor_column = self._spot.x
+        self._cursor.x = 0
+        self.cursor_column = self._cursor.x
 
     def move_end(self):
-        self._spot.x = len(self.cur_line)
-        self.cursor_column = self._spot.x
+        self._cursor.x = len(self.cur_line)
+        self.cursor_column = self._cursor.x
 
     def move_up(self):
-        if self._spot.y > 0:
-            self._spot.y -= 1
-        self._update_spot_x()
+        if self._cursor.y > 0:
+            self._cursor.y -= 1
+        self._update_cursor_x()
 
     def move_down(self):
-        if self._spot.y < len(self._lines) - 1:
-            self._spot.y += 1
-        self._update_spot_x()
+        if self._cursor.y < len(self._lines) - 1:
+            self._cursor.y += 1
+        self._update_cursor_x()
 
     def move_pageup(self):
-        if self._spot.y >= self.view_height - 1:
+        if self._cursor.y >= self.view_height - 1:
             self.emit('scrollreq', - (self.view_height - 1))
-            self._spot.y -= self.view_height - 1
+            self._cursor.y -= self.view_height - 1
         else:
-            self._spot.y = 0
-        self._update_spot_x()
+            self._cursor.y = 0
+        self._update_cursor_x()
 
     def move_pagedown(self):
-        if len(self._lines) - self._spot.y > (self.view_height - 1):
+        if len(self._lines) - self._cursor.y > (self.view_height - 1):
             self.emit('scrollreq', (self.view_height - 1))
-            self._spot.y += self.view_height - 1
+            self._cursor.y += self.view_height - 1
         else:
-            self._spot.y = len(self._lines) - 1
-        self._update_spot_x()
+            self._cursor.y = len(self._lines) - 1
+        self._update_cursor_x()
 
     def move_top(self):
-        self._spot.y = 0
-        self._update_spot_x()
+        self._cursor.y = 0
+        self._update_cursor_x()
 
     def move_bottom(self):
-        self._spot.y = len(self._lines) - 1
-        self._update_spot_x()
+        self._cursor.y = len(self._lines) - 1
+        self._update_cursor_x()
 
     def add_char(self, c):
         ln = self.cur_line
-        sx = self._spot.x
+        sx = self._cursor.x
         self.cur_line = ln[:sx] + c + ln[sx:]
         self.cursor_column = sx
 
     def add_newline(self, move=False):
         ln = self.cur_line
-        sx = self._spot.x
+        sx = self._cursor.x
         self.cur_line = ln[sx:]
-        self._lines.insert(self._spot.y, ln[:sx])
+        self._lines.insert(self._cursor.y, ln[:sx])
         self._default_size.update(h=len(self._lines))
         if move:
             self.move_right()
 
     def add_line(self, text):
         ln = self.cur_line
-        sx = self._spot.x
+        sx = self._cursor.x
         self.cur_line = ln[sx:]
-        self._lines.insert(self._spot.y, ln[:sx] + text)
+        self._lines.insert(self._cursor.y, ln[:sx] + text)
         self.cursor_column = 0
-        self._spot.x = 0
-        self._spot.y += 1
+        self._cursor.x = 0
+        self._cursor.y += 1
         w = max(self._default_size.w, len(ln[:sx] + text))
         self._default_size.update(w=w, h=len(self._lines))
 
     def backspace(self):
-        if self._spot.y > 0 or self._spot.x > 0:
+        if self._cursor.y > 0 or self._cursor.x > 0:
             self.move_left()
             self.del_char()
 
     def del_char(self):
         ln = self.cur_line
-        sx = self._spot.x
+        sx = self._cursor.x
         if sx == len(self.cur_line):
-            if self._spot.y + 1 < len(self._lines):
-                self.cur_line += self._lines[self._spot.y+1]
-                del self._lines[self._spot.y+1]
+            if self._cursor.y + 1 < len(self._lines):
+                self.cur_line += self._lines[self._cursor.y+1]
+                del self._lines[self._cursor.y+1]
                 self._default_size.update(h=len(self._lines))
         else:
             self.cur_line = ln[:sx] + ln[sx+1:]
 
-    def _update_spot_x(self):
+    def _update_cursor_x(self):
         if self.cursor_column > len(self.cur_line):
-            self._spot.x = len(self.cur_line)
+            self._cursor.x = len(self.cur_line)
         else:
-            self._spot.x = self.cursor_column
+            self._cursor.x = self.cursor_column
 
--- a/tuikit/widgets/textfield.py	Wed Aug 27 23:09:53 2014 +0200
+++ b/tuikit/widgets/textfield.py	Mon Sep 01 08:45:51 2014 +0200
@@ -50,7 +50,7 @@
                 c = '>'
             buffer.putch(c, self.width-1, 0)
 
-            self._cursor = (1 + self.curspos - self.ofs, 0)
+            self._cursor.update(1 + self.curspos - self.ofs, 0)
 
     def keypress(self, keyname, char, mod=0):
         Widget.keypress(self, keyname, char, mod)