tuikit/editfield.py
changeset 5 ae128c885d0f
parent 0 a35731b5e31a
child 8 fcaabd817774
--- a/tuikit/editfield.py	Fri Mar 18 20:14:44 2011 +0100
+++ b/tuikit/editfield.py	Sun Apr 10 22:54:38 2011 +0200
@@ -1,6 +1,5 @@
 # -*- coding: utf-8 -*-
 
-import curses
 import locale
 
 from .widget import Widget
@@ -17,15 +16,20 @@
         self.value = value
         self.maxlen = None   # unlimited
 
-        self.tw = self.width - 2  # real width of text field (minus space for arrows)
-        self.cursor = 0   # position of cursor on screen
+        self.tw = 0
+        self.cur = 0   # position of cursor on screen
         self.pos = 0      # position of cursor in value
         self.ofs = 0      # position of value beginning on screen
 
+        self.connect('resize', self.on_resize)
         self.connect('draw', self.on_draw)
         self.connect('keypress', self.on_keypress)
 
 
+    def on_resize(self):
+        self.tw = self.width - 2  # real width of text field (minus space for arrows)
+
+
     def on_draw(self, screen, x, y):
         # draw value
         val = self.value + ' ' * self.tw         # add spaces to fill rest of field
@@ -43,8 +47,7 @@
             c = '>'
         screen.putch(x + self.width-1, y, c)
 
-        # move cursor to the position
-        screen.showcursor(x + 1 + self.cursor, y)
+        self.cursor = (1 + self.cur, 0)
 
 
     def on_keypress(self, keyname, char):
@@ -71,24 +74,24 @@
 
 
     def move_left(self):
-        if self.cursor > 1 or (self.cursor == 1 and self.pos == 1):
+        if self.cur > 1 or (self.cur == 1 and self.pos == 1):
             # move cursor
             self.pos -= 1
-            self.cursor -= 1
+            self.cur -= 1
         else:
             # move content in field
-            if self.pos > self.cursor:
+            if self.pos > self.cur:
                 self.pos -= 1
                 self.ofs -= 1
 
 
     def move_right(self):
         if self.pos < len(self.value):
-            if self.cursor < self.tw - 2 \
-            or (self.cursor == self.tw - 2 and self.pos == len(self.value)-1):
+            if self.cur < self.tw - 2 \
+            or (self.cur == self.tw - 2 and self.pos == len(self.value)-1):
                 # move cursor
                 self.pos += 1
-                self.cursor += 1
+                self.cur += 1
             else:
                 # move content in field
                 self.pos += 1