Split CandidatePanel from candidatewindow.py.
authorHuang Peng <shawn.p.huang@gmail.com>
Wed, 28 May 2008 03:00:25 +0000 (11:00 +0800)
committerHuang Peng <shawn.p.huang@gmail.com>
Wed, 28 May 2008 03:00:25 +0000 (11:00 +0800)
panel/candidatepanel.py [new file with mode: 0644]
panel/candidatewindow.py

diff --git a/panel/candidatepanel.py b/panel/candidatepanel.py
new file mode 100644 (file)
index 0000000..40f3a7d
--- /dev/null
@@ -0,0 +1,190 @@
+import gtk
+import gtk.gdk as gdk
+import gobject
+import pango
+
+class HSeparator (gtk.HBox):
+       def __init__ (self):
+               gtk.HBox.__init__ (self)
+               self.pack_start (gtk.HSeparator (), True, True, 4)
+
+class VSeparator (gtk.VBox):
+       def __init__ (self):
+               gtk.VBox.__init__ (self)
+               self.pack_start (gtk.VSeparator (), True, True, 4)
+
+class CandidatePanel (gtk.VBox):
+       __gproperties__ = {
+               'orientation' : (gtk.Orientation,               # type
+               'orientation of candidates',                    # nick name
+               'the orientation of candidates list',   # description
+               0,
+               gobject.PARAM_READWRITE)                                # flags
+               }
+
+       def __init__ (self):
+               gtk.VBox.__init__ (self)
+               self._tooltips = gtk.Tooltips ()
+               
+               self._orientation = gtk.ORIENTATION_HORIZONTAL
+               self._orientation = gtk.ORIENTATION_VERTICAL
+               self._show_preedit_string = True
+               self._preedit_string = "preedit string"
+               self._preedit_attrs = pango.AttrList ()
+               self._aux_string = "aux string"
+               self._aux_attrs = pango.AttrList ()
+               self._lookup_table = None
+               
+               self._recreate_ui ()
+               
+
+       def _recreate_ui (self):
+               for w in self:
+                       self.remove (w)
+                       w.destroy ()
+               # create preedit label
+               self._preedit_label = gtk.Label (self._preedit_string)
+               self._preedit_label.set_attributes (self._preedit_attrs)
+               self._preedit_label.set_property ("xalign", 0.0)
+               self._preedit_label.set_property ("xpad", 8)
+               self._preedit_label.set_no_show_all (True)
+               if self._show_preedit_string:
+                       self._preedit_label.show ()
+
+               # create aux label
+               self._aux_label = gtk.Label (self._aux_string)
+               self._aux_label.set_attributes (self._aux_attrs)
+               self._aux_label.set_property ("xalign", 0.0)
+               self._aux_label.set_property ("xpad", 8)
+               self._tooltips.set_tip (self._aux_label, "Aux string")
+
+               # create candidates area
+               self._candidates_area = gtk.HBox ()
+               self._candidates_area.pack_start (gtk.Label ("Candidates Area"))
+
+               # create state label
+               self._state_label = gtk.Label ()
+
+               # create buttons
+               self._prev_button = gtk.Button ()
+               self._prev_button.set_relief (gtk.RELIEF_NONE)
+               self._tooltips.set_tip (self._prev_button, "Previous candidate")
+               
+               self._next_button = gtk.Button ()
+               self._next_button.set_relief (gtk.RELIEF_NONE)
+               self._tooltips.set_tip (self._next_button, "Next candidate")
+               
+               self._pack_all_widgets ()
+
+       def _pack_all_widgets (self):
+               if self._orientation == gtk.ORIENTATION_VERTICAL:               
+                       # package all widgets in vertical mode
+                       image = gtk.Image ()
+                       image.set_from_stock (gtk.STOCK_GO_UP, gtk.ICON_SIZE_MENU)
+                       self._prev_button.set_image (image)
+
+                       image = gtk.Image ()
+                       image.set_from_stock (gtk.STOCK_GO_DOWN, gtk.ICON_SIZE_MENU)
+                       self._next_button.set_image (image)
+                       vbox = gtk.VBox ()
+                       vbox.pack_start (self._preedit_label, False, False, 0)
+                       vbox.pack_start (self._aux_label, False, False, 0)
+                       self.pack_start (vbox, False, False, 4)
+                       self.pack_start (HSeparator (), False, False)
+                       self.pack_start (self._candidates_area, False, False, 4)
+                       self.pack_start (HSeparator (), False, False)
+                       hbox= gtk.HBox ()
+                       hbox.pack_start (self._state_label, True, True)
+                       hbox.pack_start (VSeparator (), False, False)
+                       hbox.pack_start (self._prev_button, False, False, 2)
+                       hbox.pack_start (self._next_button, False, False, 2)
+                       self.pack_start (hbox, False, False)
+               else:
+                       # package all widgets in HORIZONTAL mode
+                       image = gtk.Image ()
+                       image.set_from_stock (gtk.STOCK_GO_BACK, gtk.ICON_SIZE_MENU)
+                       self._prev_button.set_image (image)
+
+                       image = gtk.Image ()
+                       image.set_from_stock (gtk.STOCK_GO_FORWARD, gtk.ICON_SIZE_MENU)
+                       self._next_button.set_image (image)
+
+                       vbox = gtk.VBox ()
+                       vbox.pack_start (self._preedit_label, False, False, 0)
+                       vbox.pack_start (self._aux_label, False, False, 0)
+                       self.pack_start (vbox, False, False, 4)
+                       self.pack_start (HSeparator (), False, False)
+                       hbox= gtk.HBox ()
+                       hbox.pack_start (self._candidates_area, True, True, 4)
+                       hbox.pack_start (VSeparator (), False, False)
+                       hbox.pack_start (self._prev_button, False, False, 2)
+                       hbox.pack_start (self._next_button, False, False, 2)
+                       self.pack_start (hbox, False, False)
+                       
+               self.hide_all ()
+               self.show_all ()
+
+
+       def show_preedit_string (self):
+               self._show_preedit_string = True
+               self._preedit_label.show ()
+
+       def hide_preedit_string (self):
+               self._hide_preedit_string = False
+               self._preedit_label.hide ()
+
+       def set_aux_string (self, text, attrs):
+               self._aux_string = text
+               self._aux_label.set_text (text)
+               if attrs == None:
+                       attrs = pango.AttrList ()
+               self._aux_attrs = attrs
+               self._aux_label.set_attributes (attrs)
+       
+       def set_preedit_string (self, text, attrs):
+               self._preedit_string = text
+               self._preedit_label.set_text (text)
+               if attrs == None:
+                       attrs = pango.AttrList ()
+               self._preedit_attrs = attrs
+               self._preedit_label.set_attributes (attrs)
+
+       def set_lookup_table (self, lookup_table):
+               self._lookup_table = lookup_table
+               candidates = self._lookup_table.get_canidates_in_current_page ()
+
+       def set_orientation (self, orientation):
+               if self._orientation == orientation:
+                       return
+               self._orientation = orientation
+               self._recreate_ui ()
+       
+       def get_orientation (self):
+               return self._orientation
+
+       def do_set_property (self, property, value):
+               if property == 'orientation':
+                       self.set_orientation (value)
+               else:
+                       return gtk.DrawingArea.do_set_property (property, value)
+       
+       def do_get_property (self, property):
+               if property == 'orientation':
+                       return self._orientation
+               else:
+                       return gtk.DrawingArea.do_get_property (property)
+
+       def do_expose_event (self, event):
+               self.style.paint_box (self.window,
+                                       gtk.STATE_NORMAL,
+                                       gtk.SHADOW_IN,
+                                       event.area,
+                                       self,
+                                       "menu",
+                                       self.allocation.x, self.allocation.y, 
+                                       self.allocation.width, self.allocation.height) 
+
+               gtk.VBox.do_expose_event (self, event)
+
+gobject.type_register (CandidatePanel, "IBusCandidate")
+
index cdf6e1b16f1a9cbd58921188965b8363d21617c7..8e9fab659e5a0be5f0a1beb92171e092908eb38d 100644 (file)
@@ -2,181 +2,7 @@ import gtk
 import gtk.gdk as gdk
 import gobject
 import ibus
-
-SPACING = 4
-
-class HSeparator (gtk.HBox):
-       def __init__ (self):
-               gtk.HBox.__init__ (self)
-               self.pack_start (gtk.HSeparator (), True, True, 4)
-
-class VSeparator (gtk.VBox):
-       def __init__ (self):
-               gtk.VBox.__init__ (self)
-               self.pack_start (gtk.VSeparator (), True, True, 4)
-
-class CandidatePanel (gtk.VBox):
-       __gproperties__ = {
-               'orientation' : (gtk.Orientation,               # type
-               'orientation of candidates',                    # nick name
-               'the orientation of candidates list',   # description
-               0,
-               gobject.PARAM_READWRITE)                                # flags
-               }
-
-       def __init__ (self):
-               gtk.VBox.__init__ (self)
-               self._tooltips = gtk.Tooltips ()
-               
-               self._orientation = gtk.ORIENTATION_HORIZONTAL
-               self._orientation = gtk.ORIENTATION_VERTICAL
-               self._show_preedit_string = False
-               self._preedit_string = "preedit string"
-               self._aux_string = "aux string"
-               self._lookup_table = None
-               
-               self._recreate_ui ()
-               
-
-       def _recreate_ui (self):
-               for w in self:
-                       self.remove (w)
-                       w.destroy ()
-               # create preedit label
-               self._preedit_label = gtk.Label (self._preedit_string)
-               self._preedit_label.set_property ("xalign", 0.0)
-               self._preedit_label.set_property ("xpad", 8)
-               self._preedit_label.set_no_show_all (True)
-
-               # create aux label
-               self._aux_label = gtk.Label (self._aux_string)
-               self._aux_label.set_property ("xalign", 0.0)
-               self._aux_label.set_property ("xpad", 8)
-               self._tooltips.set_tip (self._aux_label, "Aux string")
-
-               # create candidates area
-               self._candidates_area = gtk.HBox ()
-               self._candidates_area.pack_start (gtk.Label ("Candidates Area"))
-
-               # create state label
-               self._state_label = gtk.Label ()
-
-               # create buttons
-               self._prev_button = gtk.Button ()
-               self._prev_button.set_relief (gtk.RELIEF_NONE)
-               self._tooltips.set_tip (self._prev_button, "Previous candidate")
-               
-               self._next_button = gtk.Button ()
-               self._next_button.set_relief (gtk.RELIEF_NONE)
-               self._tooltips.set_tip (self._next_button, "Next candidate")
-               
-               self.pack_all_widgets ()
-
-       def pack_all_widgets (self):
-               if self._orientation == gtk.ORIENTATION_VERTICAL:               
-                       # package all widgets in vertical mode
-                       image = gtk.Image ()
-                       image.set_from_stock (gtk.STOCK_GO_UP, gtk.ICON_SIZE_MENU)
-                       self._prev_button.set_image (image)
-
-                       image = gtk.Image ()
-                       image.set_from_stock (gtk.STOCK_GO_DOWN, gtk.ICON_SIZE_MENU)
-                       self._next_button.set_image (image)
-
-                       self.pack_start (self._preedit_label, False, False, 4)
-                       self.pack_start (self._aux_label, False, False, 4)
-                       self.pack_start (HSeparator (), False, False)
-                       self.pack_start (self._candidates_area, False, False, 4)
-                       self.pack_start (HSeparator (), False, False)
-                       hbox= gtk.HBox ()
-                       hbox.pack_start (self._state_label, True, True)
-                       hbox.pack_start (VSeparator (), False, False)
-                       hbox.pack_start (self._prev_button, False, False, 2)
-                       hbox.pack_start (self._next_button, False, False, 2)
-                       self.pack_start (hbox, False, False)
-               else:
-                       # package all widgets in HORIZONTAL mode
-                       image = gtk.Image ()
-                       image.set_from_stock (gtk.STOCK_GO_BACK, gtk.ICON_SIZE_MENU)
-                       self._prev_button.set_image (image)
-
-                       image = gtk.Image ()
-                       image.set_from_stock (gtk.STOCK_GO_FORWARD, gtk.ICON_SIZE_MENU)
-                       self._next_button.set_image (image)
-
-                       self.pack_start (self._preedit_label, False, False, 4)
-                       self.pack_start (self._aux_label, False, False, 4)
-                       self.pack_start (HSeparator (), False, False)
-                       hbox= gtk.HBox ()
-                       hbox.pack_start (self._candidates_area, True, True, 4)
-                       hbox.pack_start (VSeparator (), False, False)
-                       hbox.pack_start (self._prev_button, False, False, 2)
-                       hbox.pack_start (self._next_button, False, False, 2)
-                       self.pack_start (hbox, False, False)
-                       
-               self.hide_all ()
-               self.show_all ()
-
-
-       def show_preedit_string (self):
-               self._show_preedit_string = True
-               self._preedit_label.show ()
-
-       def hide_preedit_string (self):
-               self._hide_preedit_string = False
-               self._preedit_label.hide ()
-
-       def set_aux_string (self, text):
-               self._aux_string = text
-               self._aux_label.set_text (text)
-       
-       def set_preedit_string (self, text):
-               self._preedit_string = text
-               self._preedit_label.set_text (text)
-
-       def set_lookup_table (self, lookup_table):
-               self._lookup_table = lookup_table
-               candidates = self._lookup_table.get_canidates_in_current_page ()
-               candidate_layouts = []
-               for text, attrs in candidates:
-                       candidate_layouts.append (self.create_pango_layout (text))
-
-               self._candidate_layouts = candidate_layouts
-
-       def set_orientation (self, orientation):
-               if self._orientation == orientation:
-                       return
-               self._orientation = orientation
-               self._recreate_ui ()
-       
-       def get_orientation (self):
-               return self._orientation
-
-       def do_set_property (self, property, value):
-               if property == 'orientation':
-                       self.set_orientation (value)
-               else:
-                       return gtk.DrawingArea.do_set_property (property, value)
-       
-       def do_get_property (self, property):
-               if property == 'orientation':
-                       return self._orientation
-               else:
-                       return gtk.DrawingArea.do_get_property (property)
-
-       def do_expose_event (self, event):
-               self.style.paint_box (self.window,
-                                       gtk.STATE_NORMAL,
-                                       gtk.SHADOW_IN,
-                                       event.area,
-                                       self,
-                                       "menu",
-                                       self.allocation.x, self.allocation.y, 
-                                       self.allocation.width, self.allocation.height) 
-
-               gtk.VBox.do_expose_event (self, event)
-
-gobject.type_register (CandidatePanel, "IBusCandidate")
+from candidatepanel import CandidatePanel
 
 class CandidateWindow (gtk.Window):
        def __init__ (self):
@@ -186,31 +12,48 @@ class CandidateWindow (gtk.Window):
                        gdk.BUTTON_RELEASE_MASK | \
                        gdk.BUTTON1_MOTION_MASK)
 
-               self.set_property ("border-width", 1)
                self._candidate_panel = CandidatePanel ()
                self._begin_move = False
                self._candidate_panel.connect ("size-request", self._size_request_cb)
                self.add (self._candidate_panel)
                self.move (100, 100)
 
+       def set_preedit_string (self, text, attrs):
+               self._candidate_panel.set_preedit_string (text, attrs)
+
+       def show_preedit_string (self, text, attrs):
+               self._candidate_panel.show_preedit_string ()
+
+       def hide_preedit_string (self, text, attrs):
+               self._candidate_panel.hide_preedit_string ()
+
+       def set_aux_string (self, text, attrs):
+               self._candidate_panel.set_aux_string (text, attrs)
+
+       def set_lookup_table (self, lookup_table):
+               self._candidate_pabel.set_lookup_table (lookup_table)
+
        def _size_request_cb (self, widget, size):
                self.resize (max (size.width, 200), 1)
 
+       def _change_orientation (self):
+               if self._candidate_panel.get_orientation () == gtk.ORIENTATION_HORIZONTAL:
+                       self._candidate_panel.set_orientation (gtk.ORIENTATION_VERTICAL)
+               else:
+                       self._candidate_panel.set_orientation (gtk.ORIENTATION_HORIZONTAL)
+
        def do_button_press_event (self, event):
                if event.button == 1:
                        self._begin_move = True
                        self._press_pos = event.x_root, event.y_root
                        self.window.set_cursor (gdk.Cursor (gdk.FLEUR))
                        return True
-               if event.button == 3:
-                       if self._candidate_panel.get_orientation () == gtk.ORIENTATION_HORIZONTAL:
-                               self._candidate_panel.set_orientation (gtk.ORIENTATION_VERTICAL)
-                       else:
-                               self._candidate_panel.set_orientation (gtk.ORIENTATION_HORIZONTAL)
 
+               if event.button == 3:
+                       self._change_orientation ()
                        return True
                return False
-       
+
        def do_button_release_event (self, event):
                if event.button == 1:
                        del self._press_pos
@@ -218,7 +61,7 @@ class CandidateWindow (gtk.Window):
                        self.window.set_cursor (gdk.Cursor (gdk.LEFT_PTR))
                        return True
                return False
-       
+
        def do_motion_notify_event (self, event):
                if self._begin_move != True:
                        return False