Back to index

system-config-printer  1.3.9+20120706
ToolbarSearchEntry.py
Go to the documentation of this file.
00001 ## This code was translated to python from the original C version in
00002 ## Rhythmbox. The original authors are:
00003 
00004 ## Copyright (C) 2002 Jorn Baayen <jorn@nl.linux.org>
00005 ## Copyright (C) 2003 Colin Walters <walters@verbum.org>
00006 
00007 ## Further modifications by:
00008 
00009 ## Copyright (C) 2008 Rui Matos <tiagomatos@gmail.com>
00010 
00011 ## Copyright (C) 2009 Red Hat, Inc.
00012 ## Author: Tim Waugh <twaugh@redhat.com>
00013 
00014 ## This program is free software; you can redistribute it and/or modify
00015 ## it under the terms of the GNU General Public License as published by
00016 ## the Free Software Foundation; either version 2 of the License, or
00017 ## (at your option) any later version.
00018 
00019 ## This program is distributed in the hope that it will be useful,
00020 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
00021 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00022 ## GNU General Public License for more details.
00023 
00024 ## You should have received a copy of the GNU General Public License
00025 ## along with this program; if not, write to the Free Software
00026 ## Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
00027 
00028 import gobject
00029 import gtk
00030 import HIG
00031 from gettext import gettext as _
00032 
00033 class ToolbarSearchEntry (gtk.HBox):
00034     __gproperties__ = {
00035         'search_timeout' : (gobject.TYPE_UINT,
00036                             'search timeout',
00037                             'search signal rate limiter (in ms)',
00038                             0,
00039                             5000,
00040                             300,
00041                             gobject.PARAM_READWRITE)
00042         }
00043 
00044     __gsignals__ = {
00045         'search' : (gobject.SIGNAL_RUN_LAST,
00046                     gobject.TYPE_NONE,
00047                     [ gobject.TYPE_STRING ]),
00048         'activate' : (gobject.SIGNAL_RUN_LAST,
00049                       gobject.TYPE_NONE,
00050                       [])
00051         }
00052 
00053     def __init__ (self):
00054         self.entry = None
00055         self.timeout = 0
00056         self.is_a11y_theme = False
00057         self.search_timeout = 300
00058         self.menu = None
00059 
00060         gtk.HBox.__gobject_init__ (self)
00061         self.set_spacing (HIG.PAD_NORMAL)
00062         self.set_border_width (HIG.PAD_NORMAL)
00063 
00064         settings = gtk.settings_get_for_screen (self.get_screen ())
00065         theme = settings.get_property ('gtk-theme-name')
00066         self.is_a11y_theme = theme == 'HighContrast' or theme == 'LowContrast'
00067 
00068         label = gtk.Label ()
00069         label.set_text_with_mnemonic (_("_Filter:"))
00070         label.set_justify (gtk.JUSTIFY_RIGHT)
00071         self.pack_start (label, False, True, 0)
00072 
00073         self.entry = gtk.Entry()
00074         if gtk.__dict__.has_key ('ENTRY_ICON_PRIMARY'):
00075             # We have primary/secondary icon support.
00076             self.entry.set_icon_from_stock (gtk.ENTRY_ICON_PRIMARY,
00077                                             gtk.STOCK_FIND)
00078             self.entry.set_icon_from_stock (gtk.ENTRY_ICON_SECONDARY,
00079                                             gtk.STOCK_CLEAR)
00080 
00081             self.entry.set_icon_sensitive (gtk.ENTRY_ICON_SECONDARY, False)
00082             self.entry.set_icon_activatable (gtk.ENTRY_ICON_SECONDARY, False)
00083             self.entry.connect ('icon-press', self.on_icon_press)
00084 
00085         label.set_mnemonic_widget (self.entry)
00086 
00087         self.pack_start (self.entry, True, True, 0)
00088 
00089         self.entry.connect ('changed', self.on_changed)
00090         self.entry.connect ('focus_out_event', self.on_focus_out_event)
00091         self.entry.connect ('activate', self.on_activate)
00092 
00093     def do_get_property (self, property):
00094         if property.name == 'search_timeout':
00095             return self.search_timeout
00096         else:
00097             raise AttributeError, 'unknown property %s' % property.name
00098 
00099     def do_set_property (self, property, value):
00100         if property.name == 'search_timeout':
00101             self.search_timeout = value
00102         else:
00103             raise AttributeError, 'unknown property %s' % property.name
00104 
00105     def clear (self):
00106         if self.timeout != 0:
00107             gobject.source_remove (self.timeout)
00108             self.timeout = 0
00109 
00110         self.entry.set_text ("")
00111 
00112     def get_text (self):
00113         return self.entry.get_text ()
00114 
00115     def set_text (self, text):
00116         self.entry.set_text (text)
00117 
00118     def check_style (self):
00119         if self.is_a11y_theme:
00120             return
00121 
00122         bg_colour = gtk.gdk.color_parse ('#f7f7be') # yellow-ish
00123         fg_colour = gtk.gdk.color_parse ('#000000') # black
00124 
00125         text = self.entry.get_text ()
00126         if len (text) > 0:
00127             self.entry.modify_text (gtk.STATE_NORMAL, fg_colour)
00128             self.entry.modify_base (gtk.STATE_NORMAL, bg_colour)
00129         else:
00130             self.entry.modify_text (gtk.STATE_NORMAL, None)
00131             self.entry.modify_base (gtk.STATE_NORMAL, None)
00132 
00133         self.queue_draw ()
00134 
00135     def on_changed (self, UNUSED):
00136         self.check_style ()
00137 
00138         if self.timeout != 0:
00139             gobject.source_remove (self.timeout)
00140             self.timeout = 0
00141 
00142               # emit it now if we have no more text
00143         has_text = self.entry.get_text_length () > 0
00144         if has_text:
00145             self.timeout = gobject.timeout_add (self.search_timeout,
00146                                                 self.on_search_timeout)
00147         else:
00148             self.on_search_timeout ()
00149 
00150         if gtk.__dict__.has_key ("ENTRY_ICON_PRIMARY"):
00151             self.entry.set_icon_sensitive (gtk.ENTRY_ICON_SECONDARY, has_text)
00152             self.entry.set_icon_activatable (gtk.ENTRY_ICON_SECONDARY, has_text)
00153 
00154     def on_search_timeout (self):
00155         self.emit ('search', self.entry.get_text ())
00156         self.timeout = 0
00157 
00158         return False
00159 
00160     def on_focus_out_event (self, UNUSED_widget, UNUSED_event):
00161         if self.timeout == 0:
00162             return False
00163 
00164         gobject.source_remove (self.timeout)
00165         self.timeout = 0
00166 
00167         self.emit ('search', self.entry.get_text ())
00168 
00169         return False
00170 
00171     def searching (self):
00172         return self.entry.get_text () != ''
00173 
00174     def on_activate (self, UNUSED_entry):
00175         self.emit ('search', self.entry.get_text ())
00176 
00177     def grab_focus (self):
00178         self.entry.grab_focus ()
00179 
00180     def set_drop_down_menu (self, menu):
00181         if not gtk.__dict__.has_key ("ENTRY_ICON_PRIMARY"):
00182             return
00183 
00184         if menu:
00185             self.entry.set_icon_sensitive (gtk.ENTRY_ICON_PRIMARY, True)
00186             self.entry.set_icon_activatable (gtk.ENTRY_ICON_PRIMARY, True)
00187             self.menu = menu
00188         else:
00189             self.entry.set_icon_sensitive (gtk.ENTRY_ICON_PRIMARY, False)
00190             self.entry.set_icon_activatable (gtk.ENTRY_ICON_PRIMARY, False)
00191             self.menu = None
00192 
00193     def on_icon_press (self, UNUSED, icon_position, event):
00194         if icon_position == gtk.ENTRY_ICON_SECONDARY:
00195             self.set_text ("")
00196             return
00197 
00198         if icon_position == gtk.ENTRY_ICON_PRIMARY:
00199             if not self.menu:
00200                 return
00201 
00202             self.menu.popup (None, None, None, event.button, event.time)
00203 
00204 gobject.type_register (ToolbarSearchEntry)