home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2010 November / maximum-cd-2010-11.iso / DiscContents / calibre-0.7.13.msi / file_407 (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2010-08-06  |  5.0 KB  |  188 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. import curses
  5. import curses.ascii as curses
  6.  
  7. def rectangle(win, uly, ulx, lry, lrx):
  8.     win.vline(uly + 1, ulx, curses.ACS_VLINE, lry - uly - 1)
  9.     win.hline(uly, ulx + 1, curses.ACS_HLINE, lrx - ulx - 1)
  10.     win.hline(lry, ulx + 1, curses.ACS_HLINE, lrx - ulx - 1)
  11.     win.vline(uly + 1, lrx, curses.ACS_VLINE, lry - uly - 1)
  12.     win.addch(uly, ulx, curses.ACS_ULCORNER)
  13.     win.addch(uly, lrx, curses.ACS_URCORNER)
  14.     win.addch(lry, lrx, curses.ACS_LRCORNER)
  15.     win.addch(lry, ulx, curses.ACS_LLCORNER)
  16.  
  17.  
  18. class Textbox:
  19.     
  20.     def __init__(self, win, insert_mode = False):
  21.         self.win = win
  22.         self.insert_mode = insert_mode
  23.         (self.maxy, self.maxx) = win.getmaxyx()
  24.         self.maxy = self.maxy - 1
  25.         self.maxx = self.maxx - 1
  26.         self.stripspaces = 1
  27.         self.lastcmd = None
  28.         win.keypad(1)
  29.  
  30.     
  31.     def _end_of_line(self, y):
  32.         last = self.maxx
  33.         while True:
  34.             if curses.ascii.ascii(self.win.inch(y, last)) != curses.ascii.SP:
  35.                 last = min(self.maxx, last + 1)
  36.                 break
  37.             elif last == 0:
  38.                 break
  39.             
  40.             last = last - 1
  41.         return last
  42.  
  43.     
  44.     def _insert_printable_char(self, ch):
  45.         (y, x) = self.win.getyx()
  46.         if y < self.maxy or x < self.maxx:
  47.             if self.insert_mode:
  48.                 oldch = self.win.inch()
  49.             
  50.             
  51.             try:
  52.                 self.win.addch(ch)
  53.             except curses.error:
  54.                 pass
  55.  
  56.             if self.insert_mode:
  57.                 (backy, backx) = self.win.getyx()
  58.                 if curses.ascii.isprint(oldch):
  59.                     self._insert_printable_char(oldch)
  60.                     self.win.move(backy, backx)
  61.                 
  62.             
  63.         
  64.  
  65.     
  66.     def do_command(self, ch):
  67.         (y, x) = self.win.getyx()
  68.         self.lastcmd = ch
  69.         if curses.ascii.isprint(ch):
  70.             if y < self.maxy or x < self.maxx:
  71.                 self._insert_printable_char(ch)
  72.             
  73.         elif ch == curses.ascii.SOH:
  74.             self.win.move(y, 0)
  75.         elif ch in (curses.ascii.STX, curses.KEY_LEFT, curses.ascii.BS, curses.KEY_BACKSPACE):
  76.             if x > 0:
  77.                 self.win.move(y, x - 1)
  78.             elif y == 0:
  79.                 pass
  80.             elif self.stripspaces:
  81.                 self.win.move(y - 1, self._end_of_line(y - 1))
  82.             else:
  83.                 self.win.move(y - 1, self.maxx)
  84.             if ch in (curses.ascii.BS, curses.KEY_BACKSPACE):
  85.                 self.win.delch()
  86.             
  87.         elif ch == curses.ascii.EOT:
  88.             self.win.delch()
  89.         elif ch == curses.ascii.ENQ:
  90.             if self.stripspaces:
  91.                 self.win.move(y, self._end_of_line(y))
  92.             else:
  93.                 self.win.move(y, self.maxx)
  94.         elif ch in (curses.ascii.ACK, curses.KEY_RIGHT):
  95.             if x < self.maxx:
  96.                 self.win.move(y, x + 1)
  97.             elif y == self.maxy:
  98.                 pass
  99.             else:
  100.                 self.win.move(y + 1, 0)
  101.         elif ch == curses.ascii.BEL:
  102.             return 0
  103.         if ch == curses.ascii.NL:
  104.             if self.maxy == 0:
  105.                 return 0
  106.             if y < self.maxy:
  107.                 self.win.move(y + 1, 0)
  108.             
  109.         elif ch == curses.ascii.VT:
  110.             if x == 0 and self._end_of_line(y) == 0:
  111.                 self.win.deleteln()
  112.             else:
  113.                 self.win.move(y, x)
  114.                 self.win.clrtoeol()
  115.         elif ch == curses.ascii.FF:
  116.             self.win.refresh()
  117.         elif ch in (curses.ascii.SO, curses.KEY_DOWN):
  118.             if y < self.maxy:
  119.                 self.win.move(y + 1, x)
  120.                 if x > self._end_of_line(y + 1):
  121.                     self.win.move(y + 1, self._end_of_line(y + 1))
  122.                 
  123.             
  124.         elif ch == curses.ascii.SI:
  125.             self.win.insertln()
  126.         elif ch in (curses.ascii.DLE, curses.KEY_UP):
  127.             if y > 0:
  128.                 self.win.move(y - 1, x)
  129.                 if x > self._end_of_line(y - 1):
  130.                     self.win.move(y - 1, self._end_of_line(y - 1))
  131.                 
  132.             
  133.         
  134.         return 1
  135.  
  136.     
  137.     def gather(self):
  138.         result = ''
  139.         for y in range(self.maxy + 1):
  140.             self.win.move(y, 0)
  141.             stop = self._end_of_line(y)
  142.             if stop == 0 and self.stripspaces:
  143.                 continue
  144.             
  145.             for x in range(self.maxx + 1):
  146.                 if self.stripspaces and x > stop:
  147.                     break
  148.                 
  149.                 result = result + chr(curses.ascii.ascii(self.win.inch(y, x)))
  150.             
  151.             if self.maxy > 0:
  152.                 result = result + '\n'
  153.                 continue
  154.         
  155.         return result
  156.  
  157.     
  158.     def edit(self, validate = None):
  159.         while None:
  160.             ch = self.win.getch()
  161.             if validate:
  162.                 ch = validate(ch)
  163.             
  164.             if not ch:
  165.                 continue
  166.             
  167.             if not self.do_command(ch):
  168.                 break
  169.             
  170.             continue
  171.             return self.gather()
  172.  
  173.  
  174. if __name__ == '__main__':
  175.     
  176.     def test_editbox(stdscr):
  177.         (ncols, nlines) = (9, 4)
  178.         (uly, ulx) = (15, 20)
  179.         stdscr.addstr(uly - 2, ulx, 'Use Ctrl-G to end editing.')
  180.         win = curses.newwin(nlines, ncols, uly, ulx)
  181.         rectangle(stdscr, uly - 1, ulx - 1, uly + nlines, ulx + ncols)
  182.         stdscr.refresh()
  183.         return Textbox(win).edit()
  184.  
  185.     str = curses.wrapper(test_editbox)
  186.     print 'Contents of text box:', repr(str)
  187.  
  188.