commit-gnue
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

r5961 - in trunk/gnue-forms/src/uidrivers: _base _base/widgets curses cu


From: reinhard
Subject: r5961 - in trunk/gnue-forms/src/uidrivers: _base _base/widgets curses curses/widgets
Date: Sat, 10 Jul 2004 18:34:56 -0500 (CDT)

Author: reinhard
Date: 2004-07-10 18:34:54 -0500 (Sat, 10 Jul 2004)
New Revision: 5961

Modified:
   trunk/gnue-forms/src/uidrivers/_base/UIdriver.py
   trunk/gnue-forms/src/uidrivers/_base/widgets/_base.py
   trunk/gnue-forms/src/uidrivers/curses/UIdriver.py
   trunk/gnue-forms/src/uidrivers/curses/widgets/_base.py
   trunk/gnue-forms/src/uidrivers/curses/widgets/box.py
   trunk/gnue-forms/src/uidrivers/curses/widgets/button.py
   trunk/gnue-forms/src/uidrivers/curses/widgets/entry.py
   trunk/gnue-forms/src/uidrivers/curses/widgets/form.py
   trunk/gnue-forms/src/uidrivers/curses/widgets/label.py
   trunk/gnue-forms/src/uidrivers/curses/widgets/page.py
   trunk/gnue-forms/src/uidrivers/curses/widgets/scrollbar.py
Log:
More work on curses.


Modified: trunk/gnue-forms/src/uidrivers/_base/UIdriver.py
===================================================================
--- trunk/gnue-forms/src/uidrivers/_base/UIdriver.py    2004-07-10 13:10:25 UTC 
(rev 5960)
+++ trunk/gnue-forms/src/uidrivers/_base/UIdriver.py    2004-07-10 23:34:54 UTC 
(rev 5961)
@@ -146,6 +146,8 @@
         GDebug.printMesg(2,"%s.widgets.%s doesn't appear to be a valid ui 
widget" % (uiDriver,widgetName))
         GDebug.printMesg(5,' --> %s' % (mesg))
 
+    self._uiFocusWidget = None
+
     ############################################################
     #
     # Things you may need to  adjust in the individual UI drivers
@@ -278,10 +280,12 @@
   def switchFocus(self, event):
     object = event.object
     if object: # Some pages might not have any widgets that can be active
-      self._gfObjToUIWidget[object].indexedFocus(object._visibleIndex)
+      if self._uiFocusWidget:
+        self._uiFocusWidget.loseFocus ()
+      self._uiFocusWidget = self._gfObjToUIWidget[object]
+      self._uiFocusWidget.indexedFocus(object._visibleIndex)
+      self.dispatchEvent('beginEDITMODE', object, _form=object._form)
 
-    self.dispatchEvent('beginEDITMODE', object, _form=object._form)
-
   #
   # updateEntry
   #

Modified: trunk/gnue-forms/src/uidrivers/_base/widgets/_base.py
===================================================================
--- trunk/gnue-forms/src/uidrivers/_base/widgets/_base.py       2004-07-10 
13:10:25 UTC (rev 5960)
+++ trunk/gnue-forms/src/uidrivers/_base/widgets/_base.py       2004-07-10 
23:34:54 UTC (rev 5961)
@@ -113,6 +113,9 @@
   def indexedFocus(self, index):
     pass
 
+  def loseFocus(self):
+    pass
+
   def showModal(self):
     pass
 

Modified: trunk/gnue-forms/src/uidrivers/curses/UIdriver.py
===================================================================
--- trunk/gnue-forms/src/uidrivers/curses/UIdriver.py   2004-07-10 13:10:25 UTC 
(rev 5960)
+++ trunk/gnue-forms/src/uidrivers/curses/UIdriver.py   2004-07-10 23:34:54 UTC 
(rev 5961)
@@ -23,6 +23,7 @@
 
 import curses
 
+from gnue.common import events
 from gnue.forms.uidrivers._base.UIdriver import GFUserInterfaceBase
 
 # =============================================================================
@@ -41,7 +42,11 @@
     self.textWidth = 1
     self.textHeight = 1
     self.__screen = curses.initscr ()
-    self.__screen.keypad (True)
+    curses.start_color ()
+    curses.init_pair (1, curses.COLOR_WHITE, curses.COLOR_BLUE)
+    curses.init_pair (2, curses.COLOR_WHITE, curses.COLOR_CYAN)
+    curses.init_pair (3, curses.COLOR_WHITE, curses.COLOR_RED)
+    self.__exiting = False
 
   # ---------------------------------------------------------------------------
   # Activate the given form
@@ -54,17 +59,54 @@
   # Clean up everything
   # ---------------------------------------------------------------------------
 
-  def _exit (self):
+  def _exit (self, formName):
     curses.endwin ()
+    self.__exiting = True
 
   # ---------------------------------------------------------------------------
+  # Keys that simple generate an event
+  # ---------------------------------------------------------------------------
+
+  __keymap = {
+    27:                   'requestEXIT',        # <Esc>
+    9:                    'requestNEXTENTRY',   # <Tab>
+    10:                   'requestNEXTENTRY',   # <Enter>
+    curses.KEY_UP:        'requestPREVENTRY',
+    curses.KEY_DOWN:      'requestNEXTENTRY',
+    curses.KEY_LEFT:      'requestCURSORLEFT',
+    curses.KEY_RIGHT:     'requestCURSORRIGHT',
+    curses.KEY_HOME:      'requestCURSORHOME',
+    curses.KEY_END:       'requestCURSOREND',   # <Backspace>
+    8:                    'requestBACKSPACE',
+    curses.KEY_BACKSPACE: 'requestBACKSPACE',
+    curses.KEY_DC:        'requestDELETE',
+    curses.KEY_IC:        'requestMODETOGGLE',
+    curses.KEY_NPAGE:     'requestNEXTPAGE',
+    curses.KEY_PPAGE:     'requestPREVPAGE'
+  }
+
+  # ---------------------------------------------------------------------------
+  # Generate and execute event for user action
+  # ---------------------------------------------------------------------------
+
+  def __action (self, name, **params):
+    params ['_form'] = self._form
+    self.dispatchEvent (events.Event (name, **params))
+
+  # ---------------------------------------------------------------------------
   # Main loop
   # ---------------------------------------------------------------------------
 
   def mainLoop (self):
-    self.__currentForm.wait ()
-    self._exit ()
+    while not self.__exiting:
+      key = self.__currentForm.wait ()
 
+      if key >= 32 and key <= 255:
+        self.__action ('requestKEYPRESS', text = chr (key))
+
+      elif self.__keymap.has_key (key):
+        self.__action (self.__keymap [key])
+
   # ---------------------------------------------------------------------------
   # Display warning message
   # ---------------------------------------------------------------------------
@@ -88,9 +130,9 @@
     pass
 
   # ---------------------------------------------------------------------------
-  # Helper method to create a new pad
+  # Helper method for forms to get screen size
   # ---------------------------------------------------------------------------
 
-  def createPad (self):
+  def screenSize (self):
     (y, x) = self.__screen.getmaxyx ()
-    return curses.newpad (y, x)
+    return (x, y)

Modified: trunk/gnue-forms/src/uidrivers/curses/widgets/_base.py
===================================================================
--- trunk/gnue-forms/src/uidrivers/curses/widgets/_base.py      2004-07-10 
13:10:25 UTC (rev 5960)
+++ trunk/gnue-forms/src/uidrivers/curses/widgets/_base.py      2004-07-10 
23:34:54 UTC (rev 5961)
@@ -21,6 +21,8 @@
 #
 # $Id$
 
+import curses
+
 from gnue.forms.uidrivers._base.widgets._base import UIWidget
 
 # =============================================================================
@@ -30,28 +32,86 @@
 class UIHelper (UIWidget):
 
   # ---------------------------------------------------------------------------
-  # Initialize widget
+  # Initialize
   # ---------------------------------------------------------------------------
 
-  def createWidget (self, event, spacer):
-    self._interface = event.interface
+  def __init__ (self, event):
+
+    UIWidget.__init__ (self, event)
+
+    # should go in base uidriver
     self._GFobject = event.object
     self._parent = event.parent
-    self._spacer = spacer
-    self._init ()
+    self._focusIndex = None
+    self._x = self._GFobject ['Char:x']
+    self._y = self._GFobject ['Char:y']
 
+    # curses specific
+    self.__cursor = (0, 0)
+
   # ---------------------------------------------------------------------------
-  # Set text for widget
+  # Initialize widget
   # ---------------------------------------------------------------------------
 
-  def setValue (self, value, index = 0, enabled = True):
-    x = self._GFobject ['Char:x']
-    y = self._GFobject ['Char:y'] + self._spacer
-    self._parent.write (x, y, value)
+  def createWidget (self, event, spacer):
 
+    # should go in base uidriver
+    self._init (spacer)
+
   # ---------------------------------------------------------------------------
   # Set focus to widget
   # ---------------------------------------------------------------------------
 
   def indexedFocus (self, index):
-    pass
+
+    # should go in base uidriver
+    self._focusIndex = index
+    self._getFocus (index)
+
+    # curses specific
+    self.__updateCursorPosition ()
+
+  # ---------------------------------------------------------------------------
+  # Remove focus from widget
+  # ---------------------------------------------------------------------------
+
+  def loseFocus (self):
+
+    # should go in base uidriver
+    index = self._focusIndex
+    self._focusIndex = None
+    self._loseFocus (index)
+
+  # ---------------------------------------------------------------------------
+  # Set text for widget
+  # ---------------------------------------------------------------------------
+
+  def _setText (self, index, text, color, attr, selection = None):
+
+    if selection:
+      (s1, s2) = selection
+      self._parent.write (self._x, self._y + index, text[:s1], color, attr)
+      self._parent.write (self._x + s1, self._y + index, text[s1:s2], color,
+                          attr + curses.A_STANDOUT)
+      self._parent.write (self._x + s2, self._y + index, text[s2:], color, 
attr)
+    else:
+      self._parent.write (self._x, self._y + index, text, color, attr)
+
+  # ---------------------------------------------------------------------------
+  # Set cursor position for widget
+  # ---------------------------------------------------------------------------
+
+  def _setCursor (self, x, y):
+
+    self.__cursor = (x, y)
+    self.__updateCursorPosition ()
+
+  # ---------------------------------------------------------------------------
+  # Update cursor position
+  # ---------------------------------------------------------------------------
+
+  def __updateCursorPosition (self):
+
+    if self._focusIndex is not None:
+      (x, y) = self.__cursor
+      self._parent.move (self._x + x, self._y + self._focusIndex + y)

Modified: trunk/gnue-forms/src/uidrivers/curses/widgets/box.py
===================================================================
--- trunk/gnue-forms/src/uidrivers/curses/widgets/box.py        2004-07-10 
13:10:25 UTC (rev 5960)
+++ trunk/gnue-forms/src/uidrivers/curses/widgets/box.py        2004-07-10 
23:34:54 UTC (rev 5961)
@@ -28,7 +28,7 @@
 # =============================================================================
 
 class UIBox (UIHelper):
-  def _init (self):
+  def _init (self, index):
     pass
 
 # =============================================================================

Modified: trunk/gnue-forms/src/uidrivers/curses/widgets/button.py
===================================================================
--- trunk/gnue-forms/src/uidrivers/curses/widgets/button.py     2004-07-10 
13:10:25 UTC (rev 5960)
+++ trunk/gnue-forms/src/uidrivers/curses/widgets/button.py     2004-07-10 
23:34:54 UTC (rev 5961)
@@ -21,6 +21,8 @@
 #
 # $Id$
 
+import curses
+
 from _base import UIHelper
 
 # =============================================================================
@@ -28,9 +30,62 @@
 # =============================================================================
 
 class UIButton (UIHelper):
-  def _init (self):
-    pass
 
+  # ---------------------------------------------------------------------------
+  # Initialization
+  # ---------------------------------------------------------------------------
+
+  def __init__ (self, event):
+
+    UIHelper.__init__ (self, event)
+
+    # Determine button text
+    maxlen = event.object ['Char:width'] - 2
+    label = event.object.label [:maxlen]                # cut if too long
+    label = ' ' * ((maxlen - len (label)) / 2) + label  # expand if too short
+    label = label + ' ' * (maxlen - len (label))
+
+    self.__text = '[' + label + ']'
+
+  # ---------------------------------------------------------------------------
+  # Initialization per row
+  # ---------------------------------------------------------------------------
+
+  def _init (self, index):
+
+    self.__repaint (index)
+
+  # ---------------------------------------------------------------------------
+  # Focus has changed to this button
+  # ---------------------------------------------------------------------------
+
+  def _getFocus (self, index):
+
+    self.__repaint (index)
+
+  # ---------------------------------------------------------------------------
+  # Focus has changed away from this button
+  # ---------------------------------------------------------------------------
+
+  def _loseFocus (self, index):
+
+    self.__repaint (index)
+
+  # ---------------------------------------------------------------------------
+  # Update button representation on screen
+  # ---------------------------------------------------------------------------
+
+  def __repaint (self, index):
+
+    if index == self._focusIndex:
+      color = 3
+    else:
+      color = 2
+
+    attr = curses.A_BOLD
+
+    self._setText (index, self.__text, color, attr)
+
 # =============================================================================
 # Configuration data
 # =============================================================================

Modified: trunk/gnue-forms/src/uidrivers/curses/widgets/entry.py
===================================================================
--- trunk/gnue-forms/src/uidrivers/curses/widgets/entry.py      2004-07-10 
13:10:25 UTC (rev 5960)
+++ trunk/gnue-forms/src/uidrivers/curses/widgets/entry.py      2004-07-10 
23:34:54 UTC (rev 5961)
@@ -21,6 +21,8 @@
 #
 # $Id$
 
+import curses
+
 from _base import UIHelper
 
 # =============================================================================
@@ -28,15 +30,116 @@
 # =============================================================================
 
 class UIEntry (UIHelper):
-  def _init (self):
-    pass
 
+  # ---------------------------------------------------------------------------
+  # Initialization
+  # ---------------------------------------------------------------------------
+
+  def __init__ (self, event):
+
+    UIHelper.__init__ (self, event)
+
+    self.__style = event.object.style
+    if self.__style in ['default', 'label', 'dropdown', 'listbox']:
+      self.__length = event.object ['Char:width']
+    self.__value = {}
+    self.__selection = {}
+    self.__enabled = {}
+
+  # ---------------------------------------------------------------------------
+  # Initialization per row
+  # ---------------------------------------------------------------------------
+
+  def _init (self, index):
+
+    self.__value [index] = None
+    self.__selection [index] = None
+    self.__enabled [index] = True
+
+  # ---------------------------------------------------------------------------
+  # Focus has changed to this entry
+  # ---------------------------------------------------------------------------
+
+  def _getFocus (self, index):
+
+    self.__repaint (index)
+
+  # ---------------------------------------------------------------------------
+  # Focus has changed away from this entry
+  # ---------------------------------------------------------------------------
+
+  def _loseFocus (self, index):
+
+    self.__repaint (index)
+
+  # ---------------------------------------------------------------------------
+  # Set value for entry
+  # ---------------------------------------------------------------------------
+
+  def setValue (self, value, index = 0, enabled = True):
+
+    self.__value [index] = value
+    self.__enabled [index] = enabled
+
+    self.__repaint (index)
+
+  # ---------------------------------------------------------------------------
+  # Set cursor position
+  # ---------------------------------------------------------------------------
+
   def setCursorPosition (self, position, index = 0):
-    pass
 
+    self._setCursor (position, 0)
+
+  # ---------------------------------------------------------------------------
+  # Set start and end of selection area
+  # ---------------------------------------------------------------------------
+
   def setSelectedArea (self, selection1, selection2, index = 0):
-    pass
 
+    if selection1 == selection2:
+      self.__selection [index] = None
+    else:
+      self.__selection [index] = (selection1, selection2)
+
+    self.__repaint (index)
+
+  # ---------------------------------------------------------------------------
+  # Update entry representation on screen
+  # ---------------------------------------------------------------------------
+
+  def __repaint (self, index):
+
+    value = self.__value [index]
+
+    if self.__style in ['default', 'label', 'dropdown', 'listbox']:
+      text = value or ''
+      text += ' ' * (self.__length - len (text))
+
+    elif self.__style == 'password':
+      text = '*' * len (value or '')
+      text += ' ' * (self.__length - len (text))
+
+    elif self.__style == 'checkbox':
+      if self.__value [index]:
+        text = '[X]'
+      else:
+        text = '[ ]'
+
+    if self.__style == 'label':
+      color = 1
+    elif index == self._focusIndex:
+      color = 3
+    else:
+      color = 2
+
+    if self.__enabled [index]:
+      attr = curses.A_BOLD
+    else:
+      attr = curses.A_DIM
+
+    self._setText (index, text, color, attr, self.__selection [index])
+
 # =============================================================================
 # Configuration data
 # =============================================================================

Modified: trunk/gnue-forms/src/uidrivers/curses/widgets/form.py
===================================================================
--- trunk/gnue-forms/src/uidrivers/curses/widgets/form.py       2004-07-10 
13:10:25 UTC (rev 5960)
+++ trunk/gnue-forms/src/uidrivers/curses/widgets/form.py       2004-07-10 
23:34:54 UTC (rev 5961)
@@ -21,6 +21,8 @@
 #
 # $Id: $
 
+import curses
+
 from gnue.forms.uidrivers._base.widgets._base import UIWidget
 
 # =============================================================================
@@ -34,9 +36,13 @@
   # ---------------------------------------------------------------------------
 
   def createWidget (self, event, spacer):
+
     self.__pages = []
     self.__currentPage = None
 
+    (x, y) = event.interface.screenSize ()
+    self.__window = curses.newpad (y, x)
+
   # ---------------------------------------------------------------------------
   # Set status bar
   # ---------------------------------------------------------------------------
@@ -61,6 +67,15 @@
   def wait (self):
     return self.__currentPage.wait ()
 
+  # ---------------------------------------------------------------------------
+  # Get free area in the window
+  # ---------------------------------------------------------------------------
+
+  def getCanvas (self):
+
+    (y, x) = self.__window.getmaxyx ()
+    return (0, 2, x, y - 2)
+
 # =============================================================================
 # Configuration data
 # =============================================================================

Modified: trunk/gnue-forms/src/uidrivers/curses/widgets/label.py
===================================================================
--- trunk/gnue-forms/src/uidrivers/curses/widgets/label.py      2004-07-10 
13:10:25 UTC (rev 5960)
+++ trunk/gnue-forms/src/uidrivers/curses/widgets/label.py      2004-07-10 
23:34:54 UTC (rev 5961)
@@ -21,6 +21,8 @@
 #
 # $Id: $
 
+import curses
+
 from _base import UIHelper
 
 # =============================================================================
@@ -28,9 +30,11 @@
 # =============================================================================
 
 class UILabel (UIHelper):
-  def _init (self):
-    self.setValue (self._GFobject.text)
 
+  def _init (self, index):
+
+    self._setText (index, self._GFobject.text, 1, curses.A_BOLD)
+
 # =============================================================================
 # Configuration data
 # =============================================================================

Modified: trunk/gnue-forms/src/uidrivers/curses/widgets/page.py
===================================================================
--- trunk/gnue-forms/src/uidrivers/curses/widgets/page.py       2004-07-10 
13:10:25 UTC (rev 5960)
+++ trunk/gnue-forms/src/uidrivers/curses/widgets/page.py       2004-07-10 
23:34:54 UTC (rev 5961)
@@ -23,36 +23,53 @@
 
 import curses
 
-from _base import UIHelper
+from gnue.forms.uidrivers._base.widgets._base import UIWidget
 
 # =============================================================================
 # Page class
 # =============================================================================
 
-class UIPage (UIHelper):
+class UIPage (UIWidget):
 
   # ---------------------------------------------------------------------------
   # Initialize page
   # ---------------------------------------------------------------------------
 
-  def _init (self):
-    self.__window = self._interface.createPad ()
-    self._parent.addPage (self)
+  def createWidget (self, event, spacer):
 
+    event.parent.addPage (self)
+
+    (self.__x1, self.__y1, self.__x2, self.__y2) = event.parent.getCanvas ()
+
+    self.__window = curses.newpad (self.__y2 - self.__y1, self.__x2 - 
self.__x1)
+    self.__window.keypad (True)
+    self.__window.bkgd (' ', curses.color_pair (1))
+    self.__cursor = (0, 0)
+
   # ---------------------------------------------------------------------------
   # Write a text to a given position
   # ---------------------------------------------------------------------------
 
-  def write (self, x, y, text):
-    self.__window.addstr (y, x, text)
+  def write (self, x, y, text, color, attribute):
 
+    self.__window.addstr (y, x, o(text), curses.color_pair (color) + attribute)
+
   # ---------------------------------------------------------------------------
+  # Move the cursor to a given position
+  # ---------------------------------------------------------------------------
+
+  def move (self, x, y):
+
+    self.__cursor = (x, y)
+
+  # ---------------------------------------------------------------------------
   # Update screen and wait for user input
   # ---------------------------------------------------------------------------
 
   def wait (self):
-    (y, x) = self.__window.getmaxyx ()
-    self.__window.refresh (0, 0, 0, 0, y, x)
+
+    self.__window.move (self.__cursor [1], self.__cursor [0])
+    self.__window.refresh (0, 0, self.__y1, self.__x1, self.__y2, self.__x2)
     return self.__window.getch ()
 
 # =============================================================================

Modified: trunk/gnue-forms/src/uidrivers/curses/widgets/scrollbar.py
===================================================================
--- trunk/gnue-forms/src/uidrivers/curses/widgets/scrollbar.py  2004-07-10 
13:10:25 UTC (rev 5960)
+++ trunk/gnue-forms/src/uidrivers/curses/widgets/scrollbar.py  2004-07-10 
23:34:54 UTC (rev 5961)
@@ -28,7 +28,7 @@
 # =============================================================================
 
 class UIScrollBar (UIHelper):
-  def _init (self):
+  def _init (self, index):
     pass
 
 # =============================================================================





reply via email to

[Prev in Thread] Current Thread [Next in Thread]