/usr/share/pyshared/kiwi/ui/entrycompletion.py is in python-kiwi 1.9.22-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 | #
# Kiwi: a Framework and Enhanced Widgets for Python
#
# Copyright (C) 2006 Async Open Source
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library 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
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
# USA
#
# Author(s): Ronaldo Maia <romaia@async.com.br>
#
import gobject
import gtk
from gtk import gdk
from gtk import keysyms
from kiwi.utils import gsignal, type_register
COMPLETION_TIMEOUT = 300
PAGE_INCREMENT = 14
class KiwiEntryCompletion(gtk.EntryCompletion):
def __init__(self):
gtk.EntryCompletion.__init__(self)
self._inline_completion = False
self._popup_completion = True
self._entry = None
self._completion_timeout = 0
self._match_function = None
self._match_function_data = None
self._key = None
self.changed_id = 0
self._filter_model = None
self._treeview = None
self._popup_window = None
self._selected_index = -1
gsignal('match-selected', 'override')
def do_match_selected(self, model, iter):
self._entry.set_text(model[iter][0])
return True
def _visible_function(self, model, iter, data=None):
if not self._entry:
return False
if not self._key:
return False
if self._match_function:
return self._match_function(self, self._key, iter)
value = model[iter][0]
if not value:
return False
entry_text = self._entry.get_text()
return value.lower().startswith(entry_text.lower())
def _connect_completion_signals(self):
if self._popup_completion:
self.changed_id = self._entry.connect('changed',
self._on_completion_changed)
self._entry.connect('key-press-event',
self._on_completion_key_press)
self._entry.connect('button-press-event', self._on_button_press_event)
def _on_button_press_event(self, window, event):
# If we're clicking outside of the window, close the popup
if not self._popup_window:
return
if (event.window != self._popup_window.window or
(tuple(self._popup_window.allocation.intersect(
gdk.Rectangle(x=int(event.x), y=int(event.y),
width=1, height=1)))) == (0, 0, 0, 0)):
self.popdown()
def _on_completion_timeout(self):
if self._completion_timeout:
gobject.source_remove(self._completion_timeout)
self._completion_timeout = 0
minimum_key_length = self.get_property('minimum-key-length')
if (self._filter_model and
len(self._entry.get_text()) >= minimum_key_length and
self._entry.is_focus()):
self.complete()
matches = self._filter_model.iter_n_children(None)
if matches:
self.popup()
return False
def _on_completion_changed(self, entry):
if (self.get_property('minimum_key_length') > 0 and
not self._entry.get_text()):
self.popdown()
return
self._selected_index = -1
timeout = gobject.timeout_add(COMPLETION_TIMEOUT,
self._on_completion_timeout)
self._completion_timeout = timeout
return True
def _select_item(self, index):
# Make the selection
matches = self._filter_model.iter_n_children(None)
if 0 <= index < matches:
self._treeview.set_cursor((index,))
else:
selection = self._treeview.get_selection()
selection.unselect_all()
self._selected_index = index
def _on_completion_key_press(self, entry, event):
window = self._popup_window
if window and not window.flags() & gtk.VISIBLE:
return False
if not self._treeview:
return False
matches = self._filter_model.iter_n_children(None)
keyval = event.keyval
index = self._selected_index
if keyval == keysyms.Up or keyval == keysyms.KP_Up:
index -= 1
if index < -1:
index = matches -1
self._select_item(index)
return True
elif keyval == keysyms.Down or keyval == keysyms.KP_Down:
index += 1
if index > matches-1:
index = -1
self._select_item(index)
return True
elif keyval == keysyms.Page_Up:
if index < 0:
index = matches-1
elif index > 0 and index - PAGE_INCREMENT < 0:
index = 0
else:
index -= PAGE_INCREMENT
if index < 0:
index = -1
self._select_item(index)
return True
elif keyval == keysyms.Page_Down:
if index < 0:
index = 0
elif index < matches-1 and index + PAGE_INCREMENT > matches - 1:
index = matches -1
else:
index += PAGE_INCREMENT
if index > matches:
index = -1
self._select_item(index)
return True
elif keyval == keysyms.Escape:
self.popdown()
return True
elif (keyval == keysyms.Return or
keyval == keysyms.KP_Enter):
self.popdown()
selection = self._treeview.get_selection()
model, titer = selection.get_selected()
if not titer:
return False
self._entry.handler_block(self.changed_id)
self.emit('match-selected', model, titer)
self._entry.handler_unblock(self.changed_id)
selection.unselect_all()
return True
return False
def _popup_grab_window(self):
activate_time = 0L
if gdk.pointer_grab(self._entry.window, True,
(gdk.BUTTON_PRESS_MASK |
gdk.BUTTON_RELEASE_MASK |
gdk.POINTER_MOTION_MASK),
None, None, activate_time) == 0:
if gdk.keyboard_grab(self._entry.window, True, activate_time) == 0:
return True
else:
self._entry.window.get_display().pointer_ungrab(activate_time);
return False
return False
def _popup_ungrab_window(self):
activate_time = 0L
self._entry.window.get_display().pointer_ungrab(activate_time);
self._entry.window.get_display().keyboard_ungrab(activate_time);
# Public API
def complete(self):
if not self._filter_model:
return
self._key = self._entry.get_text()
self._filter_model.refilter()
self._treeview.set_model(self._filter_model)
if self._treeview.flags() & gtk.REALIZED:
self._treeview.scroll_to_point(0,0)
def set_entry(self, entry):
self._entry = entry
self._connect_completion_signals()
def get_entry(self):
return self._entry
def set_popup_window(self, window):
self._popup_window = window
def set_treeview(self, treeview):
self._treeview = treeview
def popup(self):
if not self._popup_window:
return
self._popup_window.popup(text=None, filter=True)
self._popup_grab_window()
def popdown(self):
if not self._popup_window:
return
self._popup_window.popdown()
self._popup_ungrab_window()
def set_model(self, model):
if not model:
if self._popup_window:
self._popup_window.set_model(None)
self.popdown()
self._model = None
self._filter_model = None
return
self._model = model
self._filter_model = model.filter_new()
self._filter_model.set_visible_func(self._visible_function)
if self._popup_window:
self._popup_window.set_model(self._filter_model)
def get_model(self):
return self._model
def set_match_func(self, function, data=None):
self._match_function = function
self._match_function_data = data
type_register(KiwiEntryCompletion)
|