[caribou/keyboard_position: 5/19] The start of some placement code



commit 6f172cc65a1c625a1204584f69e06034fccbaba2
Author: Eitan Isaacson <eitan monotonous org>
Date:   Mon Nov 30 17:45:54 2009 -0800

    The start of some placement code

 src/caribou.py  |    6 +-
 src/keyboard.py |  110 +++++++++++++++++++++++++++++++++++--------------------
 2 files changed, 73 insertions(+), 43 deletions(-)
---
diff --git a/src/caribou.py b/src/caribou.py
index 6ee1944..e6a5bd5 100644
--- a/src/caribou.py
+++ b/src/caribou.py
@@ -41,7 +41,7 @@ class Test:
     def __set_text_location(self, acc):
         text = acc.queryText() 
         [x, y, width, height] = text.getCharacterExtents(text.caretOffset, pyatspi.DESKTOP_COORDS)
-        cp.cursor_location = gtk.gdk.Rectangle(x, y, width, height)
+        cp.set_cursor_location(gtk.gdk.Rectangle(x, y, width, height))
         cp.show_all()
        
     def __set_entry_location(self, acc):
@@ -50,9 +50,9 @@ class Test:
         if x == 0 and y == 0 and width == 0 and height == 0:
             component = acc.queryComponent()
             bb = component.getExtents(pyatspi.DESKTOP_COORDS)
-            cp.cursor_location = gtk.gdk.Rectangle(bb.x, bb.y, bb.width, bb.height)
+            cp.set_cursor_location(gtk.gdk.Rectangle(bb.x, bb.y, bb.width, bb.height))
         else:
-            cp.cursor_location = gtk.gdk.Rectangle(x, y, width, height)
+            cp.set_cursor_location(gtk.gdk.Rectangle(x, y, width, height))
         cp.show_all()
        
     def on_state_changed_focused(self, event):
diff --git a/src/keyboard.py b/src/keyboard.py
index d7d290f..741d07f 100644
--- a/src/keyboard.py
+++ b/src/keyboard.py
@@ -110,62 +110,92 @@ class CaribouWindow(gtk.Window):
         self._vbox = gtk.VBox()
         self.add(self._vbox)
 
-        self.connect("size-allocate", lambda w, a: self._update_position())
-
         self._vbox.pack_start(CaribouKeyboard(qwerty))
 
 class CaribouHoverWindow(CaribouWindow):
     __gtype_name__ = "CaribouHoverWindow"
-    def __init__(self):
+
+    def __init__(self, default_placement=None):
         super(CaribouHoverWindow, self).__init__()
+
+        self.connect("size-allocate", lambda w, a: self._update_position())
+
         self._cursor_location = gtk.gdk.Rectangle()
+        self._entry_location = gtk.gdk.Rectangle()
+        self._default_placement = default_placement or \
+            CaribouKeyboardPlacement()
+    
 
-    def _set_cursor_location(self, val):
-        self._cursor_location = val
+    def set_cursor_location(self, cursor_location):
+        self._cursor_location = cursor_location
         self._update_position()
 
-    def _get_cursor_location(self):
-        return self._cursor_location
-
-    cursor_location = gobject.property(type=object, 
-                                       setter=_set_cursor_location,
-                                       getter=_get_cursor_location)
+    def set_entry_location(self, entry_location):
+        self._entry_location = entry_location
+        self._update_position()
 
-    def _update_position(self):
-        #print "---->>> CHECK POSITION"
-        bx = self.cursor_location.x + self.allocation.width
-        by = self.cursor_location.y + self.allocation.height
+    def set_default_placement(self, default_placement):
+        self._default_placement = default_placement
+        self._update_position()
 
+    def _get_root_bbox(self):
         root_window = gdk.get_default_root_window()
-        sx, sy = root_window.get_size()
-
-        if bx > sx:
-            x = sx - self.allocation.width
-        else:
-            x = self.cursor_location.x
-
-        if by > sy:
-            y = self.cursor_location.y - self.allocation.height
-        else:
-            y = self.cursor_location.y + self.cursor_location.height
+        args = root_window.get_position() + root_window.get_size()
+        return gdk.Rectangle(*args)
+            
+    def _update_position(self, placement=None):
+        placement = placement or self._default_placement
+        root_bbox = self._get_root_bbox()
+        bbox = root_bbox
+
+        if placement.stickto == placement.CURSOR:
+            bbox = self._cursor_location
+        elif placement.stickto == placement.ENTRY:
+            bbox = self._entry_location
+
+        x = bbox.x
+        y = bbox.y
+
+        if placement.halign == placement.LEFT:
+            x += bbox.width
+        elif placement.halign == placement.CENTER:
+            x += bbox.width/2
+
+        if placement.valign == placement.BOTTOM:
+            y += bbox.height
+        elif placement.halign == placement.CENTER:
+            y += bbox.height/2
 
         self.move(x, y)
 
-class CaribouKeyboardPlacement:
-    LEFT = 0
-    RIGHT = 1
-    TOP = 0
-    BOTTOM = 1
-    CENTER = 2
-
-    SCREEN = 0
-    ENTRY = 1
-    CURSOR = 2
-
-    INSIDE = 0
-    OUTSIDE = 1
-
+class CaribouKeyboardPlacement(object):
+    LEFT = 'left'
+    RIGHT = 'right'
+    TOP = 'top'
+    BOTTOM = 'bottom'
+    CENTER = 'center'
+    
+    SCREEN = 'screen'
+    ENTRY = 'entry'
+    CURSOR = 'cursor'
+    
+    INSIDE = 'inside'
+    OUTSIDE = 'outside'
     
+    def __init__(self, halign=None, valign=None, stickto=None, 
+                 hgravitate=None, vgravitate=None):
+        self.halign = halign or self.LEFT
+        self.valign = valign or self.BOTTOM
+        self.stickto = stickto or self.CURSOR
+        self.hgravitate = hgravitate or self.OUTSIDE
+        self.vgravitate = vgravitate or self.OUTSIDE
+
+    def copy(self, halign=None, valign=None, stickto=None, gravitate=None):
+        return self.__class__(halign or self.halign, 
+                              valign or self.valign, 
+                              stickto or self.stickto, 
+                              hgravitate or self.hgravitate,
+                              vgravitate or self.vgravitate)
 
 if __name__ == "__main__":
     ckbd = CaribouHoverWindow()



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