Update editfield - place cursor to end initially.
authorRadek Brich <radek.brich@devl.cz>
Wed, 13 Apr 2011 13:07:26 +0200
changeset 8 fcaabd817774
parent 7 d4a291b31cbb
child 9 7175ed629a76
Update editfield - place cursor to end initially.
tuikit/editfield.py
--- a/tuikit/editfield.py	Wed Apr 13 01:33:27 2011 +0200
+++ b/tuikit/editfield.py	Wed Apr 13 13:07:26 2011 +0200
@@ -16,9 +16,8 @@
         self.value = value
         self.maxlen = None   # unlimited
 
-        self.tw = 0
-        self.cur = 0   # position of cursor on screen
-        self.pos = 0      # position of cursor in value
+        self.tw = 0       # real width of text field (minus space for arrows)
+        self.pos = len(value)      # position of cursor in value
         self.ofs = 0      # position of value beginning on screen
 
         self.connect('resize', self.on_resize)
@@ -27,7 +26,7 @@
 
 
     def on_resize(self):
-        self.tw = self.width - 2  # real width of text field (minus space for arrows)
+        self.tw = self.width - 2
 
 
     def on_draw(self, screen, x, y):
@@ -47,7 +46,7 @@
             c = '>'
         screen.putch(x + self.width-1, y, c)
 
-        self.cursor = (1 + self.cur, 0)
+        self.cursor = (1 + self.pos - self.ofs, 0)
 
 
     def on_keypress(self, keyname, char):
@@ -74,24 +73,22 @@
 
 
     def move_left(self):
-        if self.cur > 1 or (self.cur == 1 and self.pos == 1):
+        if self.pos - self.ofs > 1 or (self.ofs == 0 and self.pos == 1):
             # move cursor
             self.pos -= 1
-            self.cur -= 1
         else:
             # move content in field
-            if self.pos > self.cur:
+            if self.ofs > 0:
                 self.pos -= 1
                 self.ofs -= 1
 
 
     def move_right(self):
         if self.pos < len(self.value):
-            if self.cur < self.tw - 2 \
-            or (self.cur == self.tw - 2 and self.pos == len(self.value)-1):
+            if self.pos - self.ofs < self.tw - 2 \
+            or (self.pos - self.ofs == self.tw - 2 and self.pos == len(self.value)-1):
                 # move cursor
                 self.pos += 1
-                self.cur += 1
             else:
                 # move content in field
                 self.pos += 1