[POS-commit] Kiwi/Kiwi Basic.py,1.47,1.48

Christian Robottom Reis kiko at async.com.br
Wed Oct 8 18:52:43 BRST 2003


Update of /cvs/Kiwi/Kiwi
In directory anthem:/tmp/cvs-serv27021

Modified Files:
	Basic.py 
Log Message:
Changes to Combo and AutoCombo: instead of using _kiwi_data on the items
(which is volatile and dies when we do set_popdown_strings, etc), store
the associated data in a dictionary. Change the AutoCombo callback to
return the data item associated, and update API to manipulate it.

Also changed the internal Combo API to use get_string_list instead of
self._string_list, which allows subclasses to override as necessary.

Added comments, and removed some duplicated code.

Added a clear() call to Label, which was really missing.


Index: Basic.py
===================================================================
RCS file: /cvs/Kiwi/Kiwi/Basic.py,v
retrieving revision 1.47
retrieving revision 1.48
diff -u -d -r1.47 -r1.48
--- Basic.py	30 May 2003 23:45:59 -0000	1.47
+++ Basic.py	8 Oct 2003 18:52:41 -0000	1.48
@@ -50,6 +50,7 @@
         self._list_parent = None
         self._popdown_strings = None
         self._popdown_id = None
+        self.datadict = {}
 
     def prefill(self, itemdata, sort=gtk.FALSE, clear_entry=gtk.FALSE):
         """
@@ -61,9 +62,7 @@
           [ label0, label1, label2 ]
       
       If you require a data item to be specified for each item, use a
-      2-item tuple for each element. The data item will be attached to
-      the listitem's `data' (obtainable through item.get_data("_kiwi_data"). 
-      The format is as follows::
+      2-item tuple for each element. The format is as follows::
 
           [ ( label0, data0 ), (label1, data1), ... ]
 
@@ -117,8 +116,7 @@
         """
 Adds a single item to the Combo. Takes:
     - label: a string with the text to be added
-    - data: the data to be set to the "_kiwi_data" field; defaults to
-      the same string as the label.
+    - data: the data to be associated with that item
 """
         # Avoid stuffing something bad in the list, as it seems you can
         # create a ListItem with label=None and it works -- sorta. You
@@ -126,10 +124,10 @@
         if type(label) is not StringType:
             raise TypeError, "label must be string, found %s" % label
         i = gtk.GtkListItem(label=label)
-        if data is not None:
-            i.set_data("_kiwi_data", data)
+        if data is None:
+            self.datadict[label] = label
         else:
-            i.set_data("_kiwi_data", label)
+            self.datadict[label] = data
         i.show()
         self.list.add(i)
         return self.find_position_from_item(i)
@@ -142,6 +140,7 @@
         """Removes all items from list and erases entry"""
         self.list.clear_items(0, len(self.list.children()) )
         self.entry.set_text("")
+        self.datadict = {}
        
     def find_position_from_item(self, item):
         """Returns the position of a certain item"""
@@ -218,9 +217,6 @@
 
         self.get_button().connect("button_press_event", self._on_button_pressed)
 
-        # _string_list: contains the complete list of item's string
-        # (usually the initial list passed to prefill())
-        self._string_list = ['']
         # _last_text stores the entry's content in the event it needs to
         # be set back (with the Escape key).
         self._last_text = self.entry.get_text()
@@ -280,9 +276,13 @@
         # list and you get an empty string in it.
         self.list.set_selection_mode(gtk.SELECTION_BROWSE)
         # Be nice and send the text in since we have it already
-        text = self.entry.get_text()
-        # XXX: check if text is in _string_list and return None if not?
-        self._callback(text)
+        item = self.entry.get_text()
+        if self.datadict.has_key(item):
+            item = self.datadict[item]
+        else:
+            item = None
+        # XXX: check if text is valid and return None if not?
+        self._callback(item)
 
     def _attach_keypress(self):
         """This method should be run when the AutoCombo is actually inside a
@@ -312,7 +312,7 @@
 
     def select_item(self, item):
         """Selects an item in the AutoCombo. Raises KeyError if item not valid"""
-        if item not in self._string_list:
+        if item not in self.get_string_list():
             raise KeyError, "Item %s not in list" % item
         self._block_entry = TRUE
         self.entry.set_text(item)
@@ -425,7 +425,7 @@
                 # minimum_chars, so we do it here.
                 if not entry_text:
                     self._block_list = TRUE
-                    gtk.GtkCombo.set_popdown_strings(self, self._string_list)
+                    gtk.GtkCombo.set_popdown_strings(self, self.get_string_list())
                     self._block_list = FALSE
                     # Since a key was pressed in the entry, I consider
                     # it sane to reset last_text
@@ -511,7 +511,7 @@
         # about it meant. We show the full list here.
         if not self.entry.get_text() and self._last_text:
             self._block_list = TRUE
-            gtk.GtkCombo.set_popdown_strings(self, self._string_list)
+            gtk.GtkCombo.set_popdown_strings(self, self.get_string_list())
             self._block_list = FALSE
         if popwin.focus_widget and self.list.children():
             # If we have a focus widget, it's a weird leftover from a
@@ -553,21 +553,22 @@
 return the complete list.
     - text: string used to filter the list.
 """
+        strings = self.get_string_list()
         if len(text) < self.min_chars:
-            return self._string_list
+            return strings
 
         text = string.lower(text)
 
         result = []
         try:
-            for item in self._string_list:
+            for item in strings:
                 if item.lower().find(text) != -1:
                     result.append(item)
         # We do Python1.5's sucky strings here
         except AttributeError:
             find = string.find
             lower = string.lower
-            for item in map(lower, self._string_list):
+            for item in map(lower, strings):
                 if find(item, text) != -1:
                     result.append(item)
 
@@ -580,32 +581,18 @@
 """
         self.min_chars = int(qty)
 
+    # XXX: breaks data association! Data sent in will *NOT* be respected here!
     def set_popdown_strings(self, strings):
         self.string_list = strings
         gtk.GtkCombo.set_popdown_strings(self, strings)
 
-    # XXX: prefill is BROKEN! Data sent in will *NOT* be respected here!
     def prefill(self, itemdata, sort=gtk.FALSE, clear_entry=gtk.FALSE):
         self._block_entry = TRUE
         Combo.prefill(self, itemdata, sort, clear_entry)
         self._block_entry = FALSE
-   
-    # XXX: Copied from Combo for perf?
-    def append_item(self, label, data=None):
-        # Avoid stuffing something bad in the list, as it seems you can
-        # create a ListItem with label=None and it works -- sorta. You
-        # get random hangs and infinite loops in gtk-land if not.
-        if type(label) is not StringType:
-            raise TypeError, "label must be string, found %s" % label
-        self._string_list.append(label)
-        i = gtk.GtkListItem(label)
-        i.show()
-        self.list.add(i)
-        return i
 
-    def clear_list(self):
-        self._string_list = ['']
-        Combo.clear_list(self)
+    def get_string_list(self):
+        return self.datadict.keys() or [""]
 
 class Label(gtk.GtkLabel):
     """An enhanced label class, that allows easily changing the font
@@ -705,6 +692,10 @@
         s.fg[gtk.STATE_NORMAL] = map.alloc(color)
         self.set_style(s)
 
+    def clear(self):
+        """Clears the Label."""
+        self.set_text("")
+    
 # Entry hacking starts here
 
 class MaskSection:



More information about the POS-commit mailing list