home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / share / pyshared / AppInstall / widgets / SearchEntry.py < prev   
Encoding:
Python Source  |  2009-03-31  |  3.9 KB  |  102 lines

  1. # coding: utf-8
  2. #
  3. # SearchEntry - An enhanced search entry with alternating background colouring 
  4. #               and timeout support
  5. #
  6. # Copyright (C) 2007 Sebastian Heinlein
  7. #               2007 Canonical Ltd.
  8. #
  9. # Authors:
  10. #  Sebastian Heinlein <glatzor@ubuntu.com>
  11. #
  12. # This program is free software; you can redistribute it and/or modify it under
  13. # the terms of the GNU General Public License as published by the Free Software
  14. # Foundation; either version 2 of the License, or (at your option) any later
  15. # version.
  16. #
  17. # This program is distributed in the hope that it will be useful, but WITHOUT
  18. # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  19. # FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
  20. # details.
  21. #
  22. # You should have received a copy of the GNU General Public License along with
  23. # this program; if not, write to the Free Software Foundation, Inc., 59 Temple
  24. # Place, Suite 330, Boston, MA 02111-1307 USA
  25.  
  26. import sexy
  27. import gtk
  28. import gobject
  29.  
  30. class SearchEntry(sexy.IconEntry, gobject.GObject):
  31.     __gsignals__ = {'terms-changed':(gobject.SIGNAL_RUN_FIRST,
  32.                                      gobject.TYPE_NONE,
  33.                                      (gobject.TYPE_STRING,))}
  34.  
  35.     def __init__(self, icon_theme):
  36.         """
  37.         Creates an enhanced IconEntry that supports a time out when typing
  38.         and uses a different background colour when the search is active
  39.         """
  40.         sexy.IconEntry.__init__(self)
  41.         self.__gobject_init__()
  42.         self._handler_changed = self.connect_after("changed",
  43.                                                    self._on_changed)
  44.         self.connect("icon-pressed", self._on_icon_pressed)
  45.         # Does not work - known bug in libsexy
  46.         # image = gtk.image_new_from_icon_name(gtk.STOCK_CLEAR,
  47.         #                                      gtk.ICON_SIZE_MENU)
  48.         image = gtk.Image()
  49.         pixbuf = icon_theme.load_icon(gtk.STOCK_CLEAR,
  50.                                       gtk.ICON_SIZE_MENU,
  51.                                       0)
  52.         image.set_from_pixbuf(pixbuf)
  53.         self.set_icon(sexy.ICON_ENTRY_SECONDARY, image)
  54.         self.set_icon_highlight(sexy.ICON_ENTRY_PRIMARY, True)
  55.  
  56.         # Do not draw a yellow bg if an a11y theme is used
  57.         settings = gtk.settings_get_default()
  58.         theme = settings.get_property("gtk-theme-name")
  59.         self._a11y = theme.startswith("HighContrast") or\
  60.                      theme.startswith("LowContrast")
  61.  
  62.         self._timeout_id = 0
  63.  
  64.     def _on_icon_pressed(self, widget, icon, mouse_button):
  65.         """
  66.         Emit the terms-changed signal without any time out when the clear
  67.         button was clicked
  68.         """
  69.         if icon == sexy.ICON_ENTRY_SECONDARY:
  70.             self.handler_block(self._handler_changed)
  71.             self.set_text("")
  72.             self._check_style()
  73.             self.handler_unblock(self._handler_changed)
  74.             self.emit("terms-changed", self.get_text())
  75.  
  76.     def _on_changed(self, widget):
  77.         """
  78.         Call the actual search method after a small timeout to allow the user
  79.         to enter a longer search term
  80.         """
  81.         self._check_style()
  82.         if self._timeout_id > 0:
  83.             gobject.source_remove(self._timeout_id)
  84.         #FIXME: Could be of use for a11y
  85.         #timeout = self.config.get_int("/apps/gnome-app-install/search-timeout")
  86.         timeout = 1000
  87.         self._timeout_id = gobject.timeout_add(timeout,
  88.                                                lambda: self.emit("terms-changed", self.get_text()))
  89.  
  90.     def _check_style(self):
  91.         """
  92.         Use a different background colour if a search is active
  93.         """
  94.         # Based on the Rhythmbox code
  95.         yellowish = gtk.gdk.Color(63479, 63479, 48830)
  96.         if self._a11y == True:
  97.             return
  98.         if self.get_text() == "":
  99.             self.modify_base(gtk.STATE_NORMAL, None)
  100.         else:
  101.             self.modify_base(gtk.STATE_NORMAL, yellowish)
  102.