changeset 292:feca52afc109

Move editing code around
author Neil Muller <drnlmuller@gmail.com>
date Thu, 15 Sep 2011 23:55:48 +0200
parents 7f1e8fa3d123
children e6a3b00f997b
files mamba/widgets/entrybox.py mamba/widgets/text.py
diffstat 2 files changed, 25 insertions(+), 19 deletions(-) [+]
line wrap: on
line diff
--- a/mamba/widgets/entrybox.py	Thu Sep 15 23:25:02 2011 +0200
+++ b/mamba/widgets/entrybox.py	Thu Sep 15 23:55:48 2011 +0200
@@ -1,8 +1,7 @@
 import pygame
-from pygame.constants import SRCALPHA, K_ESCAPE, K_RETURN, K_UP, K_DOWN
+from pygame.constants import SRCALPHA, K_ESCAPE, K_RETURN, KEYDOWN
 
 from mamba.widgets.base import Container
-from mamba.constants import DELETE_KEYS
 from mamba.widgets.text import TextWidget, TextButton, EntryTextWidget
 
 
@@ -26,7 +25,8 @@
         self.add(message)
         self.entry_text = EntryTextWidget((self.rect.left + 5,
             self.rect.top + message.rect.height + 5), self.value,
-            update=self.edit, focus_color=self.entry_color)
+            focus_color=self.entry_color)
+        self.add_callback(KEYDOWN, self.edit)
         self.add(self.entry_text)
         ok_button = TextButton((self.rect.left + 50,
             self.entry_text.rect.bottom), 'Accept')
@@ -46,6 +46,7 @@
 
     def close(self, ev, widget, ok):
         if self.accept_callback and ok:
+            self.value = self.entry_text.value
             if self.accept_callback(self.value):
                 if self.parent:
                     self.parent.remove(self)
@@ -56,18 +57,11 @@
     def edit(self, ev, widget):
         if ev.key == K_ESCAPE:
             self.close(ev, widget, False)
+            return True
         elif ev.key == K_RETURN:
             self.close(ev, widget, True)
-        elif ev.key in DELETE_KEYS:
-            if self.value:
-                self.value = self.value[:-1]
-                self.entry_text.update(self.value)
-        elif ev.key in (K_UP, K_DOWN):
-            return False  # pass this up to parent
-        else:
-            self.value += ev.unicode
-            self.entry_text.update(self.value)
-        return True
+            return True
+        return False  # pass this up to parent
 
     def grab_focus(self):
         self.entry_text.grab_focus()
--- a/mamba/widgets/text.py	Thu Sep 15 23:25:02 2011 +0200
+++ b/mamba/widgets/text.py	Thu Sep 15 23:55:48 2011 +0200
@@ -1,7 +1,8 @@
 import pygame
-from pygame.constants import SRCALPHA, KEYDOWN
+from pygame.constants import (SRCALPHA, KEYDOWN, K_ESCAPE, K_RETURN, K_UP,
+        K_DOWN)
 
-from mamba.constants import COLOR, FONT_SIZE, FOCUS_COLOR
+from mamba.constants import COLOR, FONT_SIZE, FOCUS_COLOR, DELETE_KEYS
 from mamba.widgets.base import Widget, Button
 from mamba.data import filepath
 from mamba.constants import DEFAULT_FONT
@@ -70,6 +71,7 @@
     def __init__(self, rect, text, **kwargs):
         self.focus_color = kwargs.pop('focus_color', FOCUS_COLOR)
         self.prompt = kwargs.pop('prompt', 'Entry:')
+        self.value = text
         text = '%s %s' % (self.prompt, text)
         self.base_color = COLOR
         self.update_func = kwargs.pop('update', None)
@@ -78,11 +80,21 @@
             self.focus_color = pygame.Color(self.focus_color)
         self.focussable = True
         self.base_color = self.color
-        self.add_callback(KEYDOWN, self.update_func)
+        self.add_callback(KEYDOWN, self.update)
 
-    def update(self, new_value):
-        self.text = '%s %s' % (self.prompt, new_value)
-        self.prepare()
+    def update(self, ev, widget):
+        old_value = self.value
+        if ev.key in DELETE_KEYS:
+            if self.value:
+                self.value = self.value[:-1]
+        elif ev.key in (K_ESCAPE, K_RETURN, K_UP, K_DOWN):
+            return False  # ignore these
+        else:
+            self.value += ev.unicode
+        if old_value != self.value:
+            self.text = '%s %s' % (self.prompt, self.value)
+            self.prepare()
+        return True
 
     def prepare(self):
         self.color = self.focus_color if self.focussed else self.base_color