3
# SearchEntry - An enhanced search entry with alternating background colouring
6
# Copyright (C) 2007 Sebastian Heinlein
7
# 2007-2009 Canonical Ltd.
10
# Sebastian Heinlein <glatzor@ubuntu.com>
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 3 of the License, or (at your option) any later
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
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
30
class SearchEntry(sexy.IconEntry):
32
__gsignals__ = {'terms-changed':(gobject.SIGNAL_RUN_FIRST,
34
(gobject.TYPE_STRING,))}
38
def __init__(self, icon_theme=None):
40
Creates an enhanced IconEntry that supports a time out when typing
41
and uses a different background colour when the search is active
43
sexy.IconEntry.__init__(self)
45
icon_theme = gtk.icon_theme_get_default()
46
self._handler_changed = self.connect_after("changed",
48
self.connect("icon-pressed", self._on_icon_pressed)
49
# Does not work - known bug in libsexy
50
# image = gtk.image_new_from_icon_name(gtk.STOCK_CLEAR,
53
image_find = gtk.Image()
54
pixbuf = icon_theme.load_icon(gtk.STOCK_FIND,
57
image_find.set_from_pixbuf(pixbuf)
58
self.set_icon(sexy.ICON_ENTRY_PRIMARY, image_find)
60
self.empty_image = gtk.Image()
61
self.clear_image = gtk.Image()
62
pixbuf = icon_theme.load_icon(gtk.STOCK_CLEAR,
65
self.clear_image.set_from_pixbuf(pixbuf)
66
self.set_icon(sexy.ICON_ENTRY_SECONDARY, self.clear_image)
67
self.set_icon_highlight(sexy.ICON_ENTRY_PRIMARY, True)
69
# Do not draw a yellow bg if an a11y theme is used
70
settings = gtk.settings_get_default()
71
theme = settings.get_property("gtk-theme-name")
72
self._a11y = (theme.startswith("HighContrast") or
73
theme.startswith("LowContrast"))
76
def _on_icon_pressed(self, widget, icon, mouse_button):
78
Emit the terms-changed signal without any time out when the clear
81
if icon == sexy.ICON_ENTRY_SECONDARY:
82
self.handler_block(self._handler_changed)
86
self.handler_unblock(self._handler_changed)
87
self.emit("terms-changed", self.get_text())
93
def clear_with_no_signal(self):
94
"""Clear and do not send a term-changed signal"""
95
self.handler_block(self._handler_changed)
97
self.handler_unblock(self._handler_changed)
99
def _on_changed(self, widget):
101
Call the actual search method after a small timeout to allow the user
102
to enter a longer search term
105
if self._timeout_id > 0:
106
gobject.source_remove(self._timeout_id)
107
self._timeout_id = gobject.timeout_add(self.SEARCH_TIMEOUT,
108
lambda: self.emit("terms-changed", self.get_text()))
110
def _check_style(self):
112
Use a different background colour if a search is active
115
if self.get_text() != "":
116
self.set_icon(sexy.ICON_ENTRY_SECONDARY, self.clear_image)
118
self.set_icon(sexy.ICON_ENTRY_SECONDARY, self.empty_image)
119
# Based on the Rhythmbox code
120
yellowish = gtk.gdk.Color(63479, 63479, 48830)
121
if self._a11y == True:
123
if self.get_text() == "":
124
self.modify_base(gtk.STATE_NORMAL, None)
126
self.modify_base(gtk.STATE_NORMAL, yellowish)
128
def on_entry_changed(self, terms):
131
if __name__ == "__main__":
133
icons = gtk.icon_theme_get_default()
134
entry = SearchEntry(icons)
135
entry.connect("terms-changed", on_entry_changed)
139
win.set_size_request(400,400)