finishing fixing bugs from last commit
[public/library.git] / form.py
diff --git a/form.py b/form.py
index 899032c..2e9110f 100644 (file)
--- a/form.py
+++ b/form.py
@@ -1,4 +1,5 @@
 import curses
+import sys
 
 class formWindow:
     mx = my = 0
@@ -14,13 +15,16 @@ class formWindow:
     labels = ["label1"]
     entries = []
 
+    commands = [('pU', 'top'),('pD', 'bottom'),('Es', 'cancel')]
+
     def clear(self):
         self.w.erase()
         self.w.refresh()
 
-    def __init__(self,window,book={}):
+    def __init__(self,window,helpbar,book={}):
         self.w = window
         self.w.resize(len(self.labels)+6,50)
+        self.hb = helpbar
         self.updateEntries(book)
         self.updateGeometry()
 
@@ -45,9 +49,11 @@ class formWindow:
                 self.entries.append("")
 
     def refresh(self):
+        self.hb.commands = self.commands
+        self.hb.refresh()
         self.updateGeometry()
         self.w.box()
-        self.w.addstr(0,(self.mx-len(self.caption))/2,self.caption)
+        self.w.addstr(0,(self.mx-len(self.caption))//2,self.caption)
         r=self.top
         for l in self.labels:
             c = self.left-len(l)-2
@@ -81,7 +87,7 @@ class formWindow:
         self.hl = new
         self.row = self.hl + self.top
         if new == len(self.labels):
-            self.bt+=1
+            self.bt =1
             self.bt = min(self.bt,1)
             self.row+=1
         else:
@@ -100,7 +106,8 @@ class formWindow:
 
     def insert(self,ch):
         c = self.cursor
-        self.entries[self.hl]=self.entries[self.hl][:c] +ch+  self.entries[self.hl][c:]
+        #sys.stderr.write(str(type(ch)))
+        self.entries[self.hl]=self.entries[self.hl][:c] +ch.decode('utf-8')+  self.entries[self.hl][c:]
         self.drawRow(self.hl)
         self.mvCursor(+1)
         self.highlight()
@@ -160,7 +167,9 @@ class formWindow:
             if self.bt==-1:
                 self.mvCursor(-1)
             else:
+                self.unHighlight()
                 self.bt=0
+                self.highlight()
         elif ch==curses.KEY_HOME:
             if self.bt==-1:
                 self.mvCursor(-len(self.entries[self.hl]))
@@ -168,7 +177,9 @@ class formWindow:
             if self.bt==-1:
                 self.mvCursor(+1)
             else:
+                self.unHighlight()
                 self.bt=1
+                self.highlight()
         elif ch==curses.KEY_END:
             if self.bt==-1:
                 self.mvCursor(+len(self.entries[self.hl]))
@@ -195,17 +206,36 @@ class bookForm(formWindow):
 
     # redefineable functions lookup is called when 'enter' is pressed on ISBN
     # and returns the looked-up book. Default returns nothing
-    def lookup(self,isbn):
+    def lookup_isbn(self,isbn):
         return {'isbn':isbn}
+    
+    def lookup_lccn(self,lccn):
+        return {'lccn':lccn}
 
     def returnBook(self):
         return self.returnValues()
 
     def handleInput(self,ch):
         if ch==10 or ch==curses.KEY_ENTER:
-            if self.hl==0:
-                book = self.lookup(self.entries[0])
+            if self.hl==0:          # lookup by isbn
+                book = self.lookup_isbn(self.entries[0])
+                if book != {}:
+                    self.updateEntries(book)
+                self.refresh()
+                self.mvHighlight(+7)
+            if self.hl==1:          # lookup by lccn
+                book = self.lookup_lccn(self.entries[1])
                 if book != {}:
                     self.updateEntries(book)
                 self.refresh()
-        formWindow.handleInput(self,ch)
+                self.mvHighlight(+6)
+        else:
+            formWindow.handleInput(self,ch)
+
+class categoryForm(formWindow):
+    caption = "Add a Category"
+    blabel = "Add"
+    labels = ["Category"]
+
+    def returnValues(self):
+        return self.entries