|
I needed to add a search box to a plugin that I am working on. Since there are no python bindings for RBSearchEntry, I have implemented a class in python (attached) with the same functionality. Please refer to the documentation for RBSearchEntry for usage. This class does not make the entry box yellow when a search text is entered. Hotmail: Trusted email with Microsoft’s powerful SPAM protection. Sign up now. |
#
# Copyright (C) 2010 Rupesh Kumar
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2, or (at your option)
# any later version.
#
# The Rhythmbox authors hereby grant permission for non-GPL compatible
# GStreamer plugins to be used and distributed together with GStreamer
# and Rhythmbox. This permission is above and beyond the permissions granted
# by the GPL license by which Rhythmbox is covered. If you modify this code
# you may extend this exception to your version of the code, but you are not
# obligated to do so. If you do not wish to do so, delete this exception
# statement from your version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
import glib
import gobject
import gtk, gtk.glade
import pygtk
class RBSearchEntry(gtk.HBox) :
__gsignals__ = {
'search': (gobject.SIGNAL_RUN_LAST, gobject.TYPE_NONE, (gobject.TYPE_STRING,)),
'activate': (gobject.SIGNAL_RUN_LAST, gobject.TYPE_NONE, (gobject.TYPE_STRING,))
}
def __init__ (self) :
self.__gobject_init__()
self.entry = gtk.Entry()
self.entry.set_property("secondary-icon-name", gtk.STOCK_CLEAR)
self.entry.set_property("secondary-icon-tooltip-text", "Clear the search text")
self.entry.set_width_chars(30)
self.label = gtk.Label("Search:")
self.label.set_justify(gtk.JUSTIFY_RIGHT)
self.label.set_mnemonic_widget(self.entry)
self.pack_start(self.label,False,True)
self.pack_start(self.entry,True,True)
self.set_spacing(5)
self.clearing = False
self.timeout = 0
self.connect_signals()
def connect_signals(self):
self.entry.connect("icon-press", self.entry_clear_cb)
self.entry.connect("changed", self.entry_changed_cb)
self.entry.connect("focus-out-event", self.entry_focus_out_cb)
self.entry.connect("activate", self.entry_active_cb)
def set_entry_text(self, text):
self.entry.set_text(text)
def clear_entry(self):
if self.timeout != 0:
glib.source_remove(self.timeout)
self.timeout = 0
self.clearing = True
self.set_entry_text("")
self.clearing = False
def entry_changed_cb(self, entry):
if self.clearing:
return
if self.timeout != 0:
glib.source_remove(self.timeout)
self.timeout = 0
if self.entry.get_text() != "":
self.timeout = glib.timeout_add(300, self.entry_timeout_cb)
else:
self.entry_timeout_cb()
def entry_timeout_cb(self):
gtk.gdk.threads_enter()
self.emit('search', self.entry.get_text())
self.timeout = 0
gtk.gdk.threads_leave()
return False
def entry_focus_out_cb(self, entry, event):
if self.timeout == 0:
return False
glib.source_remove(self.timeout)
self.timeout = 0
self.emit('search', self.entry.get_text())
return False
def searching(self):
return self.entry.get_text() == ""
def entry_active_cb(self):
self.emit('activate', self.entry.get_text())
def grab_focus(self):
self.entry.grab_focus()
def entry_clear_cb(self, entry, icon_pos, event):
self.set_entry_text("")
gobject.type_register(RBSearchEntry)