| 1 |
## This code was translated to python from the original C version in |
| 2 |
## Rhythmbox. The original authors are: |
| 3 |
|
| 4 |
## Copyright (C) 2002 Jorn Baayen <[email protected]> |
| 5 |
## Copyright (C) 2003 Colin Walters <[email protected]> |
| 6 |
|
| 7 |
## Further modifications by: |
| 8 |
|
| 9 |
## Copyright (C) 2008 Rui Matos <[email protected]> |
| 10 |
|
| 11 |
## Copyright (C) 2009, 2012 Red Hat, Inc. |
| 12 |
## Author: Tim Waugh <[email protected]> |
| 13 |
|
| 14 |
## This program is free software; you can redistribute it and/or modify |
| 15 |
## it under the terms of the GNU General Public License as published by |
| 16 |
## the Free Software Foundation; either version 2 of the License, or |
| 17 |
## (at your option) any later version. |
| 18 |
|
| 19 |
## This program is distributed in the hope that it will be useful, |
| 20 |
## but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 21 |
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 22 |
## GNU General Public License for more details. |
| 23 |
|
| 24 |
## You should have received a copy of the GNU General Public License |
| 25 |
## along with this program; if not, write to the Free Software |
| 26 |
## Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 27 |
|
| 28 |
from gi.repository import GObject |
| 29 |
from gi.repository import GLib |
| 30 |
from gi.repository import Gdk |
| 31 |
from gi.repository import Gtk |
| 32 |
import HIG |
| 33 |
import config |
| 34 |
import gettext |
| 35 |
gettext.install(domain=config.PACKAGE, localedir=config.localedir) |
| 36 |
|
| 37 |
|
| 38 |
class ToolbarSearchEntry (Gtk.HBox): |
| 39 |
__gproperties__ = { |
| 40 |
'search_timeout' : (GObject.TYPE_UINT, |
| 41 |
'search timeout', |
| 42 |
'search signal rate limiter (in ms)', |
| 43 |
0, |
| 44 |
5000, |
| 45 |
300, |
| 46 |
GObject.ParamFlags.READWRITE) |
| 47 |
} |
| 48 |
|
| 49 |
__gsignals__ = { |
| 50 |
'search' : (GObject.SignalFlags.RUN_LAST, |
| 51 |
None, |
| 52 |
(str,)), |
| 53 |
'activate' : (GObject.SignalFlags.RUN_LAST, |
| 54 |
None, |
| 55 |
()) |
| 56 |
} |
| 57 |
|
| 58 |
def __init__ (self): |
| 59 |
self.entry = None |
| 60 |
self.timeout = 0 |
| 61 |
self.is_a11y_theme = False |
| 62 |
self.search_timeout = 300 |
| 63 |
self.menu = None |
| 64 |
|
| 65 |
Gtk.HBox.__init__ (self) |
| 66 |
self.set_spacing (HIG.PAD_NORMAL) |
| 67 |
self.set_border_width (HIG.PAD_NORMAL) |
| 68 |
|
| 69 |
settings = Gtk.Settings.get_for_screen (self.get_screen ()) |
| 70 |
theme = settings.get_property ('gtk-theme-name') |
| 71 |
self.is_a11y_theme = theme == 'HighContrast' or theme == 'LowContrast' |
| 72 |
|
| 73 |
label = Gtk.Label () |
| 74 |
label.set_text_with_mnemonic (_("_Filter:")) |
| 75 |
label.set_justify (Gtk.Justification.RIGHT) |
| 76 |
self.pack_start (label, False, True, 0) |
| 77 |
|
| 78 |
self.entry = Gtk.Entry() |
| 79 |
if 'PRIMARY' in Gtk.EntryIconPosition.__dict__: |
| 80 |
# We have primary/secondary icon support. |
| 81 |
self.entry.set_icon_from_stock (Gtk.EntryIconPosition.PRIMARY, |
| 82 |
Gtk.STOCK_FIND) |
| 83 |
self.entry.set_icon_from_stock (Gtk.EntryIconPosition.SECONDARY, |
| 84 |
Gtk.STOCK_CLEAR) |
| 85 |
|
| 86 |
self.entry.set_icon_sensitive (Gtk.EntryIconPosition.SECONDARY, False) |
| 87 |
self.entry.set_icon_activatable (Gtk.EntryIconPosition.SECONDARY, False) |
| 88 |
self.entry.connect ('icon-press', self.on_icon_press) |
| 89 |
|
| 90 |
label.set_mnemonic_widget (self.entry) |
| 91 |
|
| 92 |
self.pack_start (self.entry, True, True, 0) |
| 93 |
|
| 94 |
self.entry.connect ('changed', self.on_changed) |
| 95 |
self.entry.connect ('focus_out_event', self.on_focus_out_event) |
| 96 |
self.entry.connect ('activate', self.on_activate) |
| 97 |
|
| 98 |
def do_get_property (self, property): |
| 99 |
if property.name == 'search_timeout': |
| 100 |
return self.search_timeout |
| 101 |
else: |
| 102 |
raise AttributeError('unknown property %s' % property.name) |
| 103 |
|
| 104 |
def do_set_property (self, property, value): |
| 105 |
if property.name == 'search_timeout': |
| 106 |
self.search_timeout = value |
| 107 |
else: |
| 108 |
raise AttributeError('unknown property %s' % property.name) |
| 109 |
|
| 110 |
def clear (self): |
| 111 |
if self.timeout != 0: |
| 112 |
GLib.source_remove (self.timeout) |
| 113 |
self.timeout = 0 |
| 114 |
|
| 115 |
self.entry.set_text ("") |
| 116 |
|
| 117 |
def get_text (self): |
| 118 |
return self.entry.get_text () |
| 119 |
|
| 120 |
def set_text (self, text): |
| 121 |
self.entry.set_text (text) |
| 122 |
|
| 123 |
def check_style (self): |
| 124 |
if self.is_a11y_theme: |
| 125 |
return |
| 126 |
|
| 127 |
bg_colour = Gdk.color_parse ('#f7f7be') # yellow-ish |
| 128 |
fg_colour = Gdk.color_parse ('#000000') # black |
| 129 |
|
| 130 |
text = self.entry.get_text () |
| 131 |
if len (text) > 0: |
| 132 |
self.entry.modify_text (Gtk.StateType.NORMAL, fg_colour) |
| 133 |
self.entry.modify_base (Gtk.StateType.NORMAL, bg_colour) |
| 134 |
else: |
| 135 |
self.entry.modify_text (Gtk.StateType.NORMAL, None) |
| 136 |
self.entry.modify_base (Gtk.StateType.NORMAL, None) |
| 137 |
|
| 138 |
self.queue_draw () |
| 139 |
|
| 140 |
def on_changed (self, UNUSED): |
| 141 |
self.check_style () |
| 142 |
|
| 143 |
if self.timeout != 0: |
| 144 |
GLib.source_remove (self.timeout) |
| 145 |
self.timeout = 0 |
| 146 |
|
| 147 |
# emit it now if we have no more text |
| 148 |
has_text = self.entry.get_text_length () > 0 |
| 149 |
if has_text: |
| 150 |
self.timeout = GLib.timeout_add (self.search_timeout, |
| 151 |
self.on_search_timeout) |
| 152 |
else: |
| 153 |
self.on_search_timeout () |
| 154 |
|
| 155 |
if 'PRIMARY' in Gtk.EntryIconPosition.__dict__: |
| 156 |
self.entry.set_icon_sensitive (Gtk.EntryIconPosition.SECONDARY, has_text) |
| 157 |
self.entry.set_icon_activatable (Gtk.EntryIconPosition.SECONDARY, has_text) |
| 158 |
|
| 159 |
def on_search_timeout (self): |
| 160 |
self.emit ('search', self.entry.get_text ()) |
| 161 |
self.timeout = 0 |
| 162 |
|
| 163 |
return False |
| 164 |
|
| 165 |
def on_focus_out_event (self, UNUSED_widget, UNUSED_event): |
| 166 |
if self.timeout == 0: |
| 167 |
return False |
| 168 |
|
| 169 |
GLib.source_remove (self.timeout) |
| 170 |
self.timeout = 0 |
| 171 |
|
| 172 |
self.emit ('search', self.entry.get_text ()) |
| 173 |
|
| 174 |
return False |
| 175 |
|
| 176 |
def searching (self): |
| 177 |
return self.entry.get_text () != '' |
| 178 |
|
| 179 |
def on_activate (self, UNUSED_entry): |
| 180 |
self.emit ('search', self.entry.get_text ()) |
| 181 |
|
| 182 |
def grab_focus (self): |
| 183 |
self.entry.grab_focus () |
| 184 |
|
| 185 |
def set_drop_down_menu (self, menu): |
| 186 |
if 'PRIMARY' not in Gtk.EntryIconPosition.__dict__: |
| 187 |
return |
| 188 |
|
| 189 |
if menu: |
| 190 |
self.entry.set_icon_sensitive (Gtk.EntryIconPosition.PRIMARY, True) |
| 191 |
self.entry.set_icon_activatable (Gtk.EntryIconPosition.PRIMARY, True) |
| 192 |
self.menu = menu |
| 193 |
else: |
| 194 |
self.entry.set_icon_sensitive (Gtk.EntryIconPosition.PRIMARY, False) |
| 195 |
self.entry.set_icon_activatable (Gtk.EntryIconPosition.PRIMARY, False) |
| 196 |
self.menu = None |
| 197 |
|
| 198 |
def on_icon_press (self, UNUSED, icon_position, event): |
| 199 |
if icon_position == Gtk.EntryIconPosition.SECONDARY: |
| 200 |
self.set_text ("") |
| 201 |
return |
| 202 |
|
| 203 |
if icon_position == Gtk.EntryIconPosition.PRIMARY: |
| 204 |
if not self.menu: |
| 205 |
return |
| 206 |
|
| 207 |
self.menu.popup (None, None, None, None, event.button, event.time) |