[caribou/introspection: 5/10] Another bunch of work.



commit bc037e05f2d5d3178e0c8375f847cb4aa1cd0b1c
Author: Eitan Isaacson <eitan monotonous org>
Date:   Thu Dec 30 23:18:07 2010 -0800

    Another bunch of work.

 bin/caribou.in         |    2 +-
 caribou/ui/keyboard.py |   52 ++++++++++++++++++++++---------
 caribou/ui/main.py     |   38 ++++++++++++-----------
 caribou/ui/opacity.py  |    2 +-
 caribou/ui/window.py   |   78 +++++++++++++++++++++++++++---------------------
 5 files changed, 103 insertions(+), 69 deletions(-)
---
diff --git a/bin/caribou.in b/bin/caribou.in
index 8cfaeb1..1c2235c 100644
--- a/bin/caribou.in
+++ b/bin/caribou.in
@@ -67,6 +67,6 @@ if __name__ == "__main__":
     main.debug = options.debug
 
     caribou = main.Caribou()
-    caribou.window.hide_all()
+    caribou.window.hide()
  
     Gtk.main()
diff --git a/caribou/ui/keyboard.py b/caribou/ui/keyboard.py
index 0f5c2e7..ccb898f 100644
--- a/caribou/ui/keyboard.py
+++ b/caribou/ui/keyboard.py
@@ -79,10 +79,12 @@ class BaseKey(object):
             self.set_relief(Gtk.ReliefStyle.NONE)
             self.set_sensitive(False)
         elif self.key_type == const.PREFERENCES_KEY_TYPE:
-            image = Gtk.Image()
-            image.set_from_stock(Gtk.STOCK_PREFERENCES,
-                                 Gtk.IconSize.BUTTON)
-            self.set_image(image)
+            #image = Gtk.Image()
+            #image.set_from_stock(Gtk.STOCK_PREFERENCES,
+            #                     Gtk.IconSize.BUTTON)
+            #self.set_image(image)
+            self.connect('map', self._on_image_key_mapped)
+            #self.connect('style-updated', self._style_updated)
         else:
             if label:
                 label_markup = Gtk.Label()
@@ -90,6 +92,27 @@ class BaseKey(object):
                 self.add(label_markup)
             else:
                 self.set_label(self.label)
+    
+    def _on_image_key_mapped(self, key):
+        print
+        key_width = key.get_allocated_height()
+        icon_size = Gtk.IconSize.MENU
+        image = Gtk.Image()
+        for size in [Gtk.IconSize.MENU,
+                     Gtk.IconSize.SMALL_TOOLBAR,
+                     Gtk.IconSize.LARGE_TOOLBAR,
+                     Gtk.IconSize.BUTTON,
+                     Gtk.IconSize.DND,
+                     Gtk.IconSize.DIALOG]:
+            pixbuf = image.render_icon_pixbuf(Gtk.STOCK_PREFERENCES, size)
+            pixel_size = pixbuf.get_width()
+            print size, pixel_size, key_width
+            if pixel_size > key_width:
+                break
+            icon_size = size
+        image.set_from_stock(Gtk.STOCK_PREFERENCES, icon_size)
+        self.set_image(image)
+
 
     def set_font(self, font):
         raise NotImplemented
@@ -108,8 +131,9 @@ class BaseKey(object):
 
     def _set_value(self, value):
         if self.key_type == const.NORMAL_KEY_TYPE:
-            if type(value) == str or type(value) == unicode:
-                value = value.encode('utf-8')
+            if type(value) == str:
+                value = value.decode('utf-8')
+            if type(value) == unicode:
                 if len(value) == 1:
                     self._value = Gdk.unicode_to_keyval(ord(value))
                 else:
@@ -133,22 +157,21 @@ class Key(Gtk.Button, BaseKey):
         BaseKey.__init__(self, label, value, key_type, width, fill)
 
     def set_font(self, font):
-        label = self.get_child()
-        if not isinstance(label, Gtk.Label):
-            return
-        label.modify_font(Pango.Font.description_from_string(font))
-        label.queue_resize()
+        child = self.get_child()
+        if isinstance(child, Gtk.Label):
+            child.modify_font(Pango.font_description_from_string(font))
+            child.queue_resize()
 
     def reset_font(self):
         label = self.get_child()
         if not isinstance(label, Gtk.Label):
             return
         label.modify_font(None)
-        label.queue_resize()
 
     def set_color(self, normal_color, mouse_over_color):
-        self.modify_bg(Gtk.StateType.NORMAL, Gdk.Color(normal_color))
-        self.modify_bg(Gtk.StateType.PRELIGHT, Gdk.Color(normal_color))
+        self.modify_bg(Gtk.StateType.NORMAL, Gdk.color_parse(normal_color)[1])
+        self.modify_bg(Gtk.StateType.PRELIGHT,
+                       Gdk.color_parse(mouse_over_color)[1])
 
     def reset_color(self):
         self.modify_bg(Gtk.StateType.NORMAL, None)
@@ -340,7 +363,6 @@ class CaribouKeyboard(Gtk.Notebook):
         self._update_key_style()
 
     def _key_font_changed(self, setting, val):
-        self.reset_row_height()
         self._update_key_style()
 
     def _update_key_style(self):
diff --git a/caribou/ui/main.py b/caribou/ui/main.py
index adae398..49058a2 100644
--- a/caribou/ui/main.py
+++ b/caribou/ui/main.py
@@ -1,10 +1,10 @@
-import Gtk.gdk as gdk
 import pyatspi
 from gi.repository import GConf
 from gi.repository import Gtk
+from gi.repository import Gdk
 import signal
 
-from window import CaribouWindowEntry
+from window import CaribouWindowEntry, Rectangle
 from keyboard import CaribouKeyboard
 from caribou.ui.i18n import _
 import caribou.common.const as const
@@ -23,8 +23,8 @@ class Caribou:
         self.window = window_factory(kb_factory())
         self.client = GConf.Client.get_default()
         self._register_event_listeners()
-        self.client.notify_add(const.CARIBOU_GCONF + "/layout", 
-                                self._on_layout_changed)
+        #self.client.notify_add(const.CARIBOU_GCONF + "/layout", 
+        #                        self._on_layout_changed)
         signal.signal(signal.SIGINT, self.signal_handler)
 
     def _register_event_listeners(self):
@@ -34,7 +34,7 @@ class Caribou:
         pyatspi.Registry.registerEventListener(
             self.on_text_caret_moved, "object:text-caret-moved")
         pyatspi.Registry.registerKeystrokeListener(
-            self.on_key_down, mask=None, kind=(pyatspi.KEY_PRESSED_EVENT,))
+            self.on_key_down, mask=0, kind=(pyatspi.KEY_PRESSED_EVENT,))
 
     def _deregister_event_listeners(self):
         pyatspi.Registry.deregisterEventListener(
@@ -43,7 +43,7 @@ class Caribou:
         pyatspi.Registry.deregisterEventListener(
             self.on_text_caret_moved, "object:text-caret-moved")
         pyatspi.Registry.deregisterKeystrokeListener(
-            self.on_key_down, mask=None, kind=pyatspi.KEY_PRESSED_EVENT)
+            self.on_key_down, mask=0, kind=pyatspi.KEY_PRESSED_EVENT)
 
     def _on_layout_changed(self, client, connection_id, entry, args):
         self._deregister_event_listeners()
@@ -73,7 +73,7 @@ class Caribou:
     def __set_text_location(self, acc):
         text = acc.queryText() 
         [x, y, width, height] = text.getCharacterExtents(text.caretOffset, pyatspi.DESKTOP_COORDS)
-        self.window.set_cursor_location(gdk.Rectangle(x, y, width, height))
+        self.window.set_cursor_location(Rectangle(x, y, width, height))
         
         component = acc.queryComponent()
         entry_bb = component.getExtents(pyatspi.DESKTOP_COORDS)
@@ -82,14 +82,14 @@ class Caribou:
        
     def __set_entry_location(self, acc):
         text = acc.queryText()
-        cursor_bb = gdk.Rectangle(
+        cursor_bb = Rectangle(
             *text.getCharacterExtents(text.caretOffset,
                                       pyatspi.DESKTOP_COORDS))
 
         component = acc.queryComponent()
         entry_bb = component.getExtents(pyatspi.DESKTOP_COORDS)
 
-        if cursor_bb == gdk.Rectangle(0, 0, 0, 0):
+        if cursor_bb == Rectangle(0, 0, 0, 0):
             cursor_bb = entry_bb
 
         self.window.set_cursor_location(cursor_bb)
@@ -99,11 +99,13 @@ class Caribou:
        
     def on_focus(self, event):
         acc = event.source
-        if pyatspi.STATE_EDITABLE in acc.getState().getStates() or event.source_role == pyatspi.ROLE_TERMINAL:
-            if event.source_role in (pyatspi.ROLE_TEXT,
-                                     pyatspi.ROLE_PARAGRAPH,
-                                     pyatspi.ROLE_PASSWORD_TEXT,
-                                     pyatspi.ROLE_TERMINAL):
+        source_role = acc.getRole()
+        if pyatspi.STATE_EDITABLE in acc.getState().getStates() or \
+                source_role == pyatspi.ROLE_TERMINAL:
+            if source_role in (pyatspi.ROLE_TEXT,
+                               pyatspi.ROLE_PARAGRAPH,
+                               pyatspi.ROLE_PASSWORD_TEXT,
+                               pyatspi.ROLE_TERMINAL):
                 if event.type.startswith("focus") or event.detail1 == 1:
                     self.__set_text_location(acc)
                     self.__current_acc = event.source
@@ -111,13 +113,13 @@ class Caribou:
                     if debug == True:
                         print "enter text widget in", event.host_application.name
                 elif event.detail1 == 0 and acc == self.__current_acc:
-                    self.window.hide_all()
+                    self.window.hide()
                     self.__current_acc = None 
                     self.__set_location = None
                     if debug == True:
                         print "leave text widget in", event.host_application.name
 
-            elif event.source_role == pyatspi.ROLE_ENTRY:
+            elif source_role == pyatspi.ROLE_ENTRY:
                 if event.type.startswith("focus") or event.detail1 == 1:
                     self.__set_entry_location(acc)
                     self.__current_acc = event.source
@@ -125,7 +127,7 @@ class Caribou:
                     if debug == True:
                         print "enter entry widget in", event.host_application.name
                 elif event.detail1 == 0:
-                    self.window.hide_all()
+                    self.window.hide()
                     self.__current_acc = None 
                     self.__set_location = None
                     if debug == True:
@@ -138,7 +140,7 @@ class Caribou:
         # This could be a way to get the entry widget leave events.
         #else:
         #    if event.detail1 == 1:
-        #        self.window.hide_all()
+        #        self.window.hide()
         #        print "--> LEAVE EDITABLE TEXT <--"
 
     def on_key_down(self, event):
diff --git a/caribou/ui/opacity.py b/caribou/ui/opacity.py
index 575eb8d..7214aa4 100644
--- a/caribou/ui/opacity.py
+++ b/caribou/ui/opacity.py
@@ -18,7 +18,7 @@
 # along with this program; if not, write to the Free Software Foundation,
 # Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 
-import gtk, glib
+import glib
 from math import sqrt
 
 class ProximityWindowBase(object):
diff --git a/caribou/ui/window.py b/caribou/ui/window.py
index 9b30a82..78b2c61 100644
--- a/caribou/ui/window.py
+++ b/caribou/ui/window.py
@@ -28,16 +28,17 @@ from gi.repository import Gdk
 import opacity
 import os
 import sys
+import gobject
 
 CARIBOU_GCONF_LAYOUT_KEY = '/apps/caribou/osk/layout'
 CARIBOU_LAYOUT_DIR = 'keyboards'
 
 class CaribouWindow(Gtk.Window):
     __gtype_name__ = "CaribouWindow"
-
     def __init__(self, text_entry_mech, default_placement=None,
                  min_alpha=1.0, max_alpha=1.0, max_distance=100):
-        super(CaribouWindow, self).__init__(Gtk.WINDOW_POPUP)
+        gobject.GObject.__init__(self, type=Gtk.WindowType.POPUP)
+
         self.set_name("CaribouWindow")
 
         self._vbox = Gtk.VBox()
@@ -48,8 +49,8 @@ class CaribouWindow(Gtk.Window):
         self.connect("size-allocate", lambda w, a: self._update_position())
         self._gconf_client = GConf.Client.get_default()
 
-        self._cursor_location = gdk.Rectangle()
-        self._entry_location = gdk.Rectangle()
+        self._cursor_location = Rectangle()
+        self._entry_location = Rectangle()
         self._default_placement = default_placement or \
             CaribouWindowPlacement()
 
@@ -77,10 +78,10 @@ class CaribouWindow(Gtk.Window):
         self._update_position()
 
     def _get_root_bbox(self):
-        root_window = gdk.get_default_root_window()
-        args = root_window.get_position() + root_window.get_size()
+        root_window = Gdk.get_default_root_window()
+        args = root_window.get_geometry()
 
-        root_bbox = gdk.Rectangle(*args)
+        root_bbox = Rectangle(*args)
 
         current_screen = Gdk.Screen.get_default().get_number()
         for panel in self._gconf_client.all_dirs('/apps/panel/toplevels'):
@@ -114,9 +115,9 @@ class CaribouWindow(Gtk.Window):
     def _update_position(self):
         x, y = self._calculate_position()
         root_bbox = self._get_root_bbox()
-        proposed_position = \
-            gdk.Rectangle(x, y, self.allocation.width, self.allocation.height)
-
+        proposed_position = Rectangle(x, y, self.get_allocated_width(),
+                                      self.get_allocated_height())
+        
         x += self._default_placement.x.adjust_to_bounds(root_bbox, proposed_position)
         y += self._default_placement.y.adjust_to_bounds(root_bbox, proposed_position)
         self.move(x, y)
@@ -129,17 +130,21 @@ class CaribouWindow(Gtk.Window):
         elif axis_placement.stickto == CaribouWindowPlacement.ENTRY:
             bbox = self._entry_location
 
-        offset = axis_placement.get_offset(bbox)
+        offset = axis_placement.get_offset(bbox.x, bbox.y)
 
         if axis_placement.align == CaribouWindowPlacement.END:
-            offset += axis_placement.get_length(bbox)
+            offset += axis_placement.get_length(bbox.width, bbox.height)
             if axis_placement.gravitate == CaribouWindowPlacement.INSIDE:
-                offset -= axis_placement.get_length(self.allocation)
+                offset -= axis_placement.get_length(
+                    self.get_allocated_width(),
+                    self.get_allocated_height())
         elif axis_placement.align == CaribouWindowPlacement.START:
             if axis_placement.gravitate == CaribouWindowPlacement.OUTSIDE:
-                offset -= axis_placement.get_length(self.allocation)
+                offset -= axis_placement.get_length(
+                    self.get_allocated_width(),
+                    self.get_allocated_height())
         elif axis_placement.align == CaribouWindowPlacement.CENTER:
-            offset += axis_placement.get_length(bbox)/2
+            offset += axis_placement.get_length(bbox.width, bbox.height)/2
 
         return offset
 
@@ -166,15 +171,15 @@ class CaribouWindow(Gtk.Window):
         Gtk.Window.show_all(self)
         self.keyboard.show_all()
 
-    def hide_all(self):
-        self.keyboard.hide_all()
-        Gtk.Window.hide_all(self)
+    def hide(self):
+        self.keyboard.hide()
+        Gtk.Window.hide(self)
 
     def _on_window_show(self, window):
         child = self.get_child()
         border = self.get_border_width()
-        w, h = child.size_request()
-        self.resize(w + border, h + border)
+        req = child.size_request()
+        self.resize(req.width + border, req.height + border)
 
 class CaribouWindowDocked(CaribouWindow, 
                           animation.AnimatedWindowBase,
@@ -208,10 +213,6 @@ class CaribouWindowDocked(CaribouWindow,
         x, y = self.get_position()
         #return self.animated_move(x + self.allocation.width, y)
 
-    def hide_all(self):
-        animation = self._roll_out()
-        animation.connect('completed', lambda x: CaribouWindow.hide_all(self)) 
-
     def hide(self):
         animation = self._roll_out()
         animation.connect('completed', lambda x: CaribouWindow.hide(self)) 
@@ -233,9 +234,8 @@ class CaribouWindowEntry(CaribouWindow):
 
     def _calculate_axis(self, axis_placement, root_bbox):
         offset = CaribouWindow._calculate_axis(self, axis_placement, root_bbox)
-
         if axis_placement.axis == 'y':
-            if offset + self.allocation.height > root_bbox.height + root_bbox.y:
+            if offset + self.get_allocated_height() > root_bbox.height + root_bbox.y:
                 new_axis_placement = axis_placement.copy(align=CaribouWindowPlacement.START)
                 offset = CaribouWindow._calculate_axis(self, new_axis_placement, root_bbox)
 
@@ -266,17 +266,20 @@ class CaribouWindowPlacement(object):
                                   stickto or self.stickto,
                                   gravitate or self.gravitate)
 
-        def get_offset(self, bbox):
-            return bbox.x if self.axis == 'x' else bbox.y
+        def get_offset(self, x, y):
+            return x if self.axis == 'x' else y
 
-        def get_length(self, bbox):
-            return bbox.width if self.axis == 'x' else bbox.height
+        def get_length(self, width, height):
+            return width if self.axis == 'x' else height
 
         def adjust_to_bounds(self, root_bbox, child_bbox):
-            child_vector_start = self.get_offset(child_bbox)
-            child_vector_end = self.get_length(child_bbox) + child_vector_start
-            root_vector_start = self.get_offset(root_bbox)
-            root_vector_end = self.get_length(root_bbox) + root_vector_start
+            child_vector_start = self.get_offset(child_bbox.x, child_bbox.y)
+            child_vector_end = \
+                self.get_length(child_bbox.width, child_bbox.height) + \
+                child_vector_start
+            root_vector_start = self.get_offset(root_bbox.x, root_bbox.y)
+            root_vector_end = self.get_length(
+                root_bbox.width, root_bbox.height) + root_vector_start
 
             if root_vector_end < child_vector_end:
                 return root_vector_end - child_vector_end
@@ -300,6 +303,13 @@ class CaribouWindowPlacement(object):
                                      ygravitate or self.OUTSIDE)
 
 
+class Rectangle(object):
+    def __init__(self, x=0, y=0, width=0, height=0):
+        self.x = x
+        self.y = y
+        self.width = width
+        self.height = height
+
 if __name__ == "__main__":
     import keyboard
     import signal



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