tuikit/widgets/textfield.py
changeset 103 49f212aa0228
parent 97 0c2e0c09ba5c
child 104 742e504ec053
--- a/tuikit/widgets/textfield.py	Wed Aug 20 15:01:11 2014 +0200
+++ b/tuikit/widgets/textfield.py	Wed Aug 27 23:09:53 2014 +0200
@@ -19,11 +19,15 @@
         self.maxlen = None   # unlimited
 
         self.tw = 0       # real width of text field (minus space for arrows)
-        self.curspos = len(value)      # position of cursor in value
+        self.curspos = 0  # position of cursor in value
         self.ofs = 0      # position of value beginning on screen
 
+        self.move_end()
+
     def resize(self, w, h):
         self.tw = self.width - 2
+        if self.curspos >= self.tw:
+            self.ofs = self.curspos - self.tw
 
     def set_theme(self, theme):
         self.color = theme.normal
@@ -50,25 +54,22 @@
 
     def keypress(self, keyname, char, mod=0):
         Widget.keypress(self, keyname, char, mod)
-        accepted = True
-        if keyname == 'left':
-            self.move_left()
-        elif keyname == 'right':
-            self.move_right()
-        elif keyname == 'backspace':
-            if self.curspos > 0:
-                self.move_left()
-                self.del_char()
-        elif keyname == 'delete':
-            self.del_char()
-        else:
-            accepted = False
-
+        map_keyname_to_func = {
+            'left':       self.move_left,
+            'right':      self.move_right,
+            'home':       self.move_home,
+            'end':        self.move_end,
+            'backspace':  self.move_left_and_del,
+            'delete':     self.del_char,
+        }
+        accepted = False
+        if keyname in map_keyname_to_func:
+            map_keyname_to_func[keyname]()
+            accepted = True
         if char:
             self.add_char(char)
             self.move_right()
             accepted = True
-
         #if accepted:
             #self.redraw()
         return accepted
@@ -85,8 +86,8 @@
 
     def move_right(self):
         if self.curspos < len(self.value):
-            if self.curspos - self.ofs < self.tw - 2 \
-            or (self.curspos - self.ofs == self.tw - 2 and self.curspos == len(self.value)-1):
+            if self.curspos - self.ofs < self.tw - 1 \
+            or (self.curspos - self.ofs == self.tw - 1 and self.curspos == len(self.value)-1):
                 # move cursor
                 self.curspos += 1
             else:
@@ -94,9 +95,22 @@
                 self.curspos += 1
                 self.ofs += 1
 
+    def move_home(self):
+        self.curspos = 0
+        self.ofs = 0
+
+    def move_end(self):
+        self.curspos = len(self.value)
+        if self.curspos >= self.tw:
+            self.ofs = self.curspos - self.tw
+
     def add_char(self, c):
         self.value = self.value[:self.curspos] + c + self.value[self.curspos:]
 
     def del_char(self):
         self.value = self.value[:self.curspos] + self.value[self.curspos+1:]
 
+    def move_left_and_del(self):
+        if self.curspos > 0:
+            self.move_left()
+            self.del_char()