| 1 | n/a | """ |
|---|
| 2 | n/a | An auto-completion window for IDLE, used by the autocomplete extension |
|---|
| 3 | n/a | """ |
|---|
| 4 | n/a | from tkinter import * |
|---|
| 5 | n/a | from tkinter.ttk import Scrollbar |
|---|
| 6 | n/a | |
|---|
| 7 | n/a | from idlelib.autocomplete import COMPLETE_FILES, COMPLETE_ATTRIBUTES |
|---|
| 8 | n/a | from idlelib.multicall import MC_SHIFT |
|---|
| 9 | n/a | |
|---|
| 10 | n/a | HIDE_VIRTUAL_EVENT_NAME = "<<autocompletewindow-hide>>" |
|---|
| 11 | n/a | HIDE_SEQUENCES = ("<FocusOut>", "<ButtonPress>") |
|---|
| 12 | n/a | KEYPRESS_VIRTUAL_EVENT_NAME = "<<autocompletewindow-keypress>>" |
|---|
| 13 | n/a | # We need to bind event beyond <Key> so that the function will be called |
|---|
| 14 | n/a | # before the default specific IDLE function |
|---|
| 15 | n/a | KEYPRESS_SEQUENCES = ("<Key>", "<Key-BackSpace>", "<Key-Return>", "<Key-Tab>", |
|---|
| 16 | n/a | "<Key-Up>", "<Key-Down>", "<Key-Home>", "<Key-End>", |
|---|
| 17 | n/a | "<Key-Prior>", "<Key-Next>") |
|---|
| 18 | n/a | KEYRELEASE_VIRTUAL_EVENT_NAME = "<<autocompletewindow-keyrelease>>" |
|---|
| 19 | n/a | KEYRELEASE_SEQUENCE = "<KeyRelease>" |
|---|
| 20 | n/a | LISTUPDATE_SEQUENCE = "<B1-ButtonRelease>" |
|---|
| 21 | n/a | WINCONFIG_SEQUENCE = "<Configure>" |
|---|
| 22 | n/a | DOUBLECLICK_SEQUENCE = "<B1-Double-ButtonRelease>" |
|---|
| 23 | n/a | |
|---|
| 24 | n/a | class AutoCompleteWindow: |
|---|
| 25 | n/a | |
|---|
| 26 | n/a | def __init__(self, widget): |
|---|
| 27 | n/a | # The widget (Text) on which we place the AutoCompleteWindow |
|---|
| 28 | n/a | self.widget = widget |
|---|
| 29 | n/a | # The widgets we create |
|---|
| 30 | n/a | self.autocompletewindow = self.listbox = self.scrollbar = None |
|---|
| 31 | n/a | # The default foreground and background of a selection. Saved because |
|---|
| 32 | n/a | # they are changed to the regular colors of list items when the |
|---|
| 33 | n/a | # completion start is not a prefix of the selected completion |
|---|
| 34 | n/a | self.origselforeground = self.origselbackground = None |
|---|
| 35 | n/a | # The list of completions |
|---|
| 36 | n/a | self.completions = None |
|---|
| 37 | n/a | # A list with more completions, or None |
|---|
| 38 | n/a | self.morecompletions = None |
|---|
| 39 | n/a | # The completion mode. Either autocomplete.COMPLETE_ATTRIBUTES or |
|---|
| 40 | n/a | # autocomplete.COMPLETE_FILES |
|---|
| 41 | n/a | self.mode = None |
|---|
| 42 | n/a | # The current completion start, on the text box (a string) |
|---|
| 43 | n/a | self.start = None |
|---|
| 44 | n/a | # The index of the start of the completion |
|---|
| 45 | n/a | self.startindex = None |
|---|
| 46 | n/a | # The last typed start, used so that when the selection changes, |
|---|
| 47 | n/a | # the new start will be as close as possible to the last typed one. |
|---|
| 48 | n/a | self.lasttypedstart = None |
|---|
| 49 | n/a | # Do we have an indication that the user wants the completion window |
|---|
| 50 | n/a | # (for example, he clicked the list) |
|---|
| 51 | n/a | self.userwantswindow = None |
|---|
| 52 | n/a | # event ids |
|---|
| 53 | n/a | self.hideid = self.keypressid = self.listupdateid = self.winconfigid \ |
|---|
| 54 | n/a | = self.keyreleaseid = self.doubleclickid = None |
|---|
| 55 | n/a | # Flag set if last keypress was a tab |
|---|
| 56 | n/a | self.lastkey_was_tab = False |
|---|
| 57 | n/a | |
|---|
| 58 | n/a | def _change_start(self, newstart): |
|---|
| 59 | n/a | min_len = min(len(self.start), len(newstart)) |
|---|
| 60 | n/a | i = 0 |
|---|
| 61 | n/a | while i < min_len and self.start[i] == newstart[i]: |
|---|
| 62 | n/a | i += 1 |
|---|
| 63 | n/a | if i < len(self.start): |
|---|
| 64 | n/a | self.widget.delete("%s+%dc" % (self.startindex, i), |
|---|
| 65 | n/a | "%s+%dc" % (self.startindex, len(self.start))) |
|---|
| 66 | n/a | if i < len(newstart): |
|---|
| 67 | n/a | self.widget.insert("%s+%dc" % (self.startindex, i), |
|---|
| 68 | n/a | newstart[i:]) |
|---|
| 69 | n/a | self.start = newstart |
|---|
| 70 | n/a | |
|---|
| 71 | n/a | def _binary_search(self, s): |
|---|
| 72 | n/a | """Find the first index in self.completions where completions[i] is |
|---|
| 73 | n/a | greater or equal to s, or the last index if there is no such |
|---|
| 74 | n/a | one.""" |
|---|
| 75 | n/a | i = 0; j = len(self.completions) |
|---|
| 76 | n/a | while j > i: |
|---|
| 77 | n/a | m = (i + j) // 2 |
|---|
| 78 | n/a | if self.completions[m] >= s: |
|---|
| 79 | n/a | j = m |
|---|
| 80 | n/a | else: |
|---|
| 81 | n/a | i = m + 1 |
|---|
| 82 | n/a | return min(i, len(self.completions)-1) |
|---|
| 83 | n/a | |
|---|
| 84 | n/a | def _complete_string(self, s): |
|---|
| 85 | n/a | """Assuming that s is the prefix of a string in self.completions, |
|---|
| 86 | n/a | return the longest string which is a prefix of all the strings which |
|---|
| 87 | n/a | s is a prefix of them. If s is not a prefix of a string, return s.""" |
|---|
| 88 | n/a | first = self._binary_search(s) |
|---|
| 89 | n/a | if self.completions[first][:len(s)] != s: |
|---|
| 90 | n/a | # There is not even one completion which s is a prefix of. |
|---|
| 91 | n/a | return s |
|---|
| 92 | n/a | # Find the end of the range of completions where s is a prefix of. |
|---|
| 93 | n/a | i = first + 1 |
|---|
| 94 | n/a | j = len(self.completions) |
|---|
| 95 | n/a | while j > i: |
|---|
| 96 | n/a | m = (i + j) // 2 |
|---|
| 97 | n/a | if self.completions[m][:len(s)] != s: |
|---|
| 98 | n/a | j = m |
|---|
| 99 | n/a | else: |
|---|
| 100 | n/a | i = m + 1 |
|---|
| 101 | n/a | last = i-1 |
|---|
| 102 | n/a | |
|---|
| 103 | n/a | if first == last: # only one possible completion |
|---|
| 104 | n/a | return self.completions[first] |
|---|
| 105 | n/a | |
|---|
| 106 | n/a | # We should return the maximum prefix of first and last |
|---|
| 107 | n/a | first_comp = self.completions[first] |
|---|
| 108 | n/a | last_comp = self.completions[last] |
|---|
| 109 | n/a | min_len = min(len(first_comp), len(last_comp)) |
|---|
| 110 | n/a | i = len(s) |
|---|
| 111 | n/a | while i < min_len and first_comp[i] == last_comp[i]: |
|---|
| 112 | n/a | i += 1 |
|---|
| 113 | n/a | return first_comp[:i] |
|---|
| 114 | n/a | |
|---|
| 115 | n/a | def _selection_changed(self): |
|---|
| 116 | n/a | """Should be called when the selection of the Listbox has changed. |
|---|
| 117 | n/a | Updates the Listbox display and calls _change_start.""" |
|---|
| 118 | n/a | cursel = int(self.listbox.curselection()[0]) |
|---|
| 119 | n/a | |
|---|
| 120 | n/a | self.listbox.see(cursel) |
|---|
| 121 | n/a | |
|---|
| 122 | n/a | lts = self.lasttypedstart |
|---|
| 123 | n/a | selstart = self.completions[cursel] |
|---|
| 124 | n/a | if self._binary_search(lts) == cursel: |
|---|
| 125 | n/a | newstart = lts |
|---|
| 126 | n/a | else: |
|---|
| 127 | n/a | min_len = min(len(lts), len(selstart)) |
|---|
| 128 | n/a | i = 0 |
|---|
| 129 | n/a | while i < min_len and lts[i] == selstart[i]: |
|---|
| 130 | n/a | i += 1 |
|---|
| 131 | n/a | newstart = selstart[:i] |
|---|
| 132 | n/a | self._change_start(newstart) |
|---|
| 133 | n/a | |
|---|
| 134 | n/a | if self.completions[cursel][:len(self.start)] == self.start: |
|---|
| 135 | n/a | # start is a prefix of the selected completion |
|---|
| 136 | n/a | self.listbox.configure(selectbackground=self.origselbackground, |
|---|
| 137 | n/a | selectforeground=self.origselforeground) |
|---|
| 138 | n/a | else: |
|---|
| 139 | n/a | self.listbox.configure(selectbackground=self.listbox.cget("bg"), |
|---|
| 140 | n/a | selectforeground=self.listbox.cget("fg")) |
|---|
| 141 | n/a | # If there are more completions, show them, and call me again. |
|---|
| 142 | n/a | if self.morecompletions: |
|---|
| 143 | n/a | self.completions = self.morecompletions |
|---|
| 144 | n/a | self.morecompletions = None |
|---|
| 145 | n/a | self.listbox.delete(0, END) |
|---|
| 146 | n/a | for item in self.completions: |
|---|
| 147 | n/a | self.listbox.insert(END, item) |
|---|
| 148 | n/a | self.listbox.select_set(self._binary_search(self.start)) |
|---|
| 149 | n/a | self._selection_changed() |
|---|
| 150 | n/a | |
|---|
| 151 | n/a | def show_window(self, comp_lists, index, complete, mode, userWantsWin): |
|---|
| 152 | n/a | """Show the autocomplete list, bind events. |
|---|
| 153 | n/a | If complete is True, complete the text, and if there is exactly one |
|---|
| 154 | n/a | matching completion, don't open a list.""" |
|---|
| 155 | n/a | # Handle the start we already have |
|---|
| 156 | n/a | self.completions, self.morecompletions = comp_lists |
|---|
| 157 | n/a | self.mode = mode |
|---|
| 158 | n/a | self.startindex = self.widget.index(index) |
|---|
| 159 | n/a | self.start = self.widget.get(self.startindex, "insert") |
|---|
| 160 | n/a | if complete: |
|---|
| 161 | n/a | completed = self._complete_string(self.start) |
|---|
| 162 | n/a | start = self.start |
|---|
| 163 | n/a | self._change_start(completed) |
|---|
| 164 | n/a | i = self._binary_search(completed) |
|---|
| 165 | n/a | if self.completions[i] == completed and \ |
|---|
| 166 | n/a | (i == len(self.completions)-1 or |
|---|
| 167 | n/a | self.completions[i+1][:len(completed)] != completed): |
|---|
| 168 | n/a | # There is exactly one matching completion |
|---|
| 169 | n/a | return completed == start |
|---|
| 170 | n/a | self.userwantswindow = userWantsWin |
|---|
| 171 | n/a | self.lasttypedstart = self.start |
|---|
| 172 | n/a | |
|---|
| 173 | n/a | # Put widgets in place |
|---|
| 174 | n/a | self.autocompletewindow = acw = Toplevel(self.widget) |
|---|
| 175 | n/a | # Put it in a position so that it is not seen. |
|---|
| 176 | n/a | acw.wm_geometry("+10000+10000") |
|---|
| 177 | n/a | # Make it float |
|---|
| 178 | n/a | acw.wm_overrideredirect(1) |
|---|
| 179 | n/a | try: |
|---|
| 180 | n/a | # This command is only needed and available on Tk >= 8.4.0 for OSX |
|---|
| 181 | n/a | # Without it, call tips intrude on the typing process by grabbing |
|---|
| 182 | n/a | # the focus. |
|---|
| 183 | n/a | acw.tk.call("::tk::unsupported::MacWindowStyle", "style", acw._w, |
|---|
| 184 | n/a | "help", "noActivates") |
|---|
| 185 | n/a | except TclError: |
|---|
| 186 | n/a | pass |
|---|
| 187 | n/a | self.scrollbar = scrollbar = Scrollbar(acw, orient=VERTICAL) |
|---|
| 188 | n/a | self.listbox = listbox = Listbox(acw, yscrollcommand=scrollbar.set, |
|---|
| 189 | n/a | exportselection=False, bg="white") |
|---|
| 190 | n/a | for item in self.completions: |
|---|
| 191 | n/a | listbox.insert(END, item) |
|---|
| 192 | n/a | self.origselforeground = listbox.cget("selectforeground") |
|---|
| 193 | n/a | self.origselbackground = listbox.cget("selectbackground") |
|---|
| 194 | n/a | scrollbar.config(command=listbox.yview) |
|---|
| 195 | n/a | scrollbar.pack(side=RIGHT, fill=Y) |
|---|
| 196 | n/a | listbox.pack(side=LEFT, fill=BOTH, expand=True) |
|---|
| 197 | n/a | acw.lift() # work around bug in Tk 8.5.18+ (issue #24570) |
|---|
| 198 | n/a | |
|---|
| 199 | n/a | # Initialize the listbox selection |
|---|
| 200 | n/a | self.listbox.select_set(self._binary_search(self.start)) |
|---|
| 201 | n/a | self._selection_changed() |
|---|
| 202 | n/a | |
|---|
| 203 | n/a | # bind events |
|---|
| 204 | n/a | self.hideid = self.widget.bind(HIDE_VIRTUAL_EVENT_NAME, |
|---|
| 205 | n/a | self.hide_event) |
|---|
| 206 | n/a | for seq in HIDE_SEQUENCES: |
|---|
| 207 | n/a | self.widget.event_add(HIDE_VIRTUAL_EVENT_NAME, seq) |
|---|
| 208 | n/a | self.keypressid = self.widget.bind(KEYPRESS_VIRTUAL_EVENT_NAME, |
|---|
| 209 | n/a | self.keypress_event) |
|---|
| 210 | n/a | for seq in KEYPRESS_SEQUENCES: |
|---|
| 211 | n/a | self.widget.event_add(KEYPRESS_VIRTUAL_EVENT_NAME, seq) |
|---|
| 212 | n/a | self.keyreleaseid = self.widget.bind(KEYRELEASE_VIRTUAL_EVENT_NAME, |
|---|
| 213 | n/a | self.keyrelease_event) |
|---|
| 214 | n/a | self.widget.event_add(KEYRELEASE_VIRTUAL_EVENT_NAME,KEYRELEASE_SEQUENCE) |
|---|
| 215 | n/a | self.listupdateid = listbox.bind(LISTUPDATE_SEQUENCE, |
|---|
| 216 | n/a | self.listselect_event) |
|---|
| 217 | n/a | self.winconfigid = acw.bind(WINCONFIG_SEQUENCE, self.winconfig_event) |
|---|
| 218 | n/a | self.doubleclickid = listbox.bind(DOUBLECLICK_SEQUENCE, |
|---|
| 219 | n/a | self.doubleclick_event) |
|---|
| 220 | n/a | return None |
|---|
| 221 | n/a | |
|---|
| 222 | n/a | def winconfig_event(self, event): |
|---|
| 223 | n/a | if not self.is_active(): |
|---|
| 224 | n/a | return |
|---|
| 225 | n/a | # Position the completion list window |
|---|
| 226 | n/a | text = self.widget |
|---|
| 227 | n/a | text.see(self.startindex) |
|---|
| 228 | n/a | x, y, cx, cy = text.bbox(self.startindex) |
|---|
| 229 | n/a | acw = self.autocompletewindow |
|---|
| 230 | n/a | acw_width, acw_height = acw.winfo_width(), acw.winfo_height() |
|---|
| 231 | n/a | text_width, text_height = text.winfo_width(), text.winfo_height() |
|---|
| 232 | n/a | new_x = text.winfo_rootx() + min(x, max(0, text_width - acw_width)) |
|---|
| 233 | n/a | new_y = text.winfo_rooty() + y |
|---|
| 234 | n/a | if (text_height - (y + cy) >= acw_height # enough height below |
|---|
| 235 | n/a | or y < acw_height): # not enough height above |
|---|
| 236 | n/a | # place acw below current line |
|---|
| 237 | n/a | new_y += cy |
|---|
| 238 | n/a | else: |
|---|
| 239 | n/a | # place acw above current line |
|---|
| 240 | n/a | new_y -= acw_height |
|---|
| 241 | n/a | acw.wm_geometry("+%d+%d" % (new_x, new_y)) |
|---|
| 242 | n/a | |
|---|
| 243 | n/a | def hide_event(self, event): |
|---|
| 244 | n/a | if self.is_active(): |
|---|
| 245 | n/a | self.hide_window() |
|---|
| 246 | n/a | |
|---|
| 247 | n/a | def listselect_event(self, event): |
|---|
| 248 | n/a | if self.is_active(): |
|---|
| 249 | n/a | self.userwantswindow = True |
|---|
| 250 | n/a | cursel = int(self.listbox.curselection()[0]) |
|---|
| 251 | n/a | self._change_start(self.completions[cursel]) |
|---|
| 252 | n/a | |
|---|
| 253 | n/a | def doubleclick_event(self, event): |
|---|
| 254 | n/a | # Put the selected completion in the text, and close the list |
|---|
| 255 | n/a | cursel = int(self.listbox.curselection()[0]) |
|---|
| 256 | n/a | self._change_start(self.completions[cursel]) |
|---|
| 257 | n/a | self.hide_window() |
|---|
| 258 | n/a | |
|---|
| 259 | n/a | def keypress_event(self, event): |
|---|
| 260 | n/a | if not self.is_active(): |
|---|
| 261 | n/a | return None |
|---|
| 262 | n/a | keysym = event.keysym |
|---|
| 263 | n/a | if hasattr(event, "mc_state"): |
|---|
| 264 | n/a | state = event.mc_state |
|---|
| 265 | n/a | else: |
|---|
| 266 | n/a | state = 0 |
|---|
| 267 | n/a | if keysym != "Tab": |
|---|
| 268 | n/a | self.lastkey_was_tab = False |
|---|
| 269 | n/a | if (len(keysym) == 1 or keysym in ("underscore", "BackSpace") |
|---|
| 270 | n/a | or (self.mode == COMPLETE_FILES and keysym in |
|---|
| 271 | n/a | ("period", "minus"))) \ |
|---|
| 272 | n/a | and not (state & ~MC_SHIFT): |
|---|
| 273 | n/a | # Normal editing of text |
|---|
| 274 | n/a | if len(keysym) == 1: |
|---|
| 275 | n/a | self._change_start(self.start + keysym) |
|---|
| 276 | n/a | elif keysym == "underscore": |
|---|
| 277 | n/a | self._change_start(self.start + '_') |
|---|
| 278 | n/a | elif keysym == "period": |
|---|
| 279 | n/a | self._change_start(self.start + '.') |
|---|
| 280 | n/a | elif keysym == "minus": |
|---|
| 281 | n/a | self._change_start(self.start + '-') |
|---|
| 282 | n/a | else: |
|---|
| 283 | n/a | # keysym == "BackSpace" |
|---|
| 284 | n/a | if len(self.start) == 0: |
|---|
| 285 | n/a | self.hide_window() |
|---|
| 286 | n/a | return None |
|---|
| 287 | n/a | self._change_start(self.start[:-1]) |
|---|
| 288 | n/a | self.lasttypedstart = self.start |
|---|
| 289 | n/a | self.listbox.select_clear(0, int(self.listbox.curselection()[0])) |
|---|
| 290 | n/a | self.listbox.select_set(self._binary_search(self.start)) |
|---|
| 291 | n/a | self._selection_changed() |
|---|
| 292 | n/a | return "break" |
|---|
| 293 | n/a | |
|---|
| 294 | n/a | elif keysym == "Return": |
|---|
| 295 | n/a | self.hide_window() |
|---|
| 296 | n/a | return None |
|---|
| 297 | n/a | |
|---|
| 298 | n/a | elif (self.mode == COMPLETE_ATTRIBUTES and keysym in |
|---|
| 299 | n/a | ("period", "space", "parenleft", "parenright", "bracketleft", |
|---|
| 300 | n/a | "bracketright")) or \ |
|---|
| 301 | n/a | (self.mode == COMPLETE_FILES and keysym in |
|---|
| 302 | n/a | ("slash", "backslash", "quotedbl", "apostrophe")) \ |
|---|
| 303 | n/a | and not (state & ~MC_SHIFT): |
|---|
| 304 | n/a | # If start is a prefix of the selection, but is not '' when |
|---|
| 305 | n/a | # completing file names, put the whole |
|---|
| 306 | n/a | # selected completion. Anyway, close the list. |
|---|
| 307 | n/a | cursel = int(self.listbox.curselection()[0]) |
|---|
| 308 | n/a | if self.completions[cursel][:len(self.start)] == self.start \ |
|---|
| 309 | n/a | and (self.mode == COMPLETE_ATTRIBUTES or self.start): |
|---|
| 310 | n/a | self._change_start(self.completions[cursel]) |
|---|
| 311 | n/a | self.hide_window() |
|---|
| 312 | n/a | return None |
|---|
| 313 | n/a | |
|---|
| 314 | n/a | elif keysym in ("Home", "End", "Prior", "Next", "Up", "Down") and \ |
|---|
| 315 | n/a | not state: |
|---|
| 316 | n/a | # Move the selection in the listbox |
|---|
| 317 | n/a | self.userwantswindow = True |
|---|
| 318 | n/a | cursel = int(self.listbox.curselection()[0]) |
|---|
| 319 | n/a | if keysym == "Home": |
|---|
| 320 | n/a | newsel = 0 |
|---|
| 321 | n/a | elif keysym == "End": |
|---|
| 322 | n/a | newsel = len(self.completions)-1 |
|---|
| 323 | n/a | elif keysym in ("Prior", "Next"): |
|---|
| 324 | n/a | jump = self.listbox.nearest(self.listbox.winfo_height()) - \ |
|---|
| 325 | n/a | self.listbox.nearest(0) |
|---|
| 326 | n/a | if keysym == "Prior": |
|---|
| 327 | n/a | newsel = max(0, cursel-jump) |
|---|
| 328 | n/a | else: |
|---|
| 329 | n/a | assert keysym == "Next" |
|---|
| 330 | n/a | newsel = min(len(self.completions)-1, cursel+jump) |
|---|
| 331 | n/a | elif keysym == "Up": |
|---|
| 332 | n/a | newsel = max(0, cursel-1) |
|---|
| 333 | n/a | else: |
|---|
| 334 | n/a | assert keysym == "Down" |
|---|
| 335 | n/a | newsel = min(len(self.completions)-1, cursel+1) |
|---|
| 336 | n/a | self.listbox.select_clear(cursel) |
|---|
| 337 | n/a | self.listbox.select_set(newsel) |
|---|
| 338 | n/a | self._selection_changed() |
|---|
| 339 | n/a | self._change_start(self.completions[newsel]) |
|---|
| 340 | n/a | return "break" |
|---|
| 341 | n/a | |
|---|
| 342 | n/a | elif (keysym == "Tab" and not state): |
|---|
| 343 | n/a | if self.lastkey_was_tab: |
|---|
| 344 | n/a | # two tabs in a row; insert current selection and close acw |
|---|
| 345 | n/a | cursel = int(self.listbox.curselection()[0]) |
|---|
| 346 | n/a | self._change_start(self.completions[cursel]) |
|---|
| 347 | n/a | self.hide_window() |
|---|
| 348 | n/a | return "break" |
|---|
| 349 | n/a | else: |
|---|
| 350 | n/a | # first tab; let AutoComplete handle the completion |
|---|
| 351 | n/a | self.userwantswindow = True |
|---|
| 352 | n/a | self.lastkey_was_tab = True |
|---|
| 353 | n/a | return None |
|---|
| 354 | n/a | |
|---|
| 355 | n/a | elif any(s in keysym for s in ("Shift", "Control", "Alt", |
|---|
| 356 | n/a | "Meta", "Command", "Option")): |
|---|
| 357 | n/a | # A modifier key, so ignore |
|---|
| 358 | n/a | return None |
|---|
| 359 | n/a | |
|---|
| 360 | n/a | elif event.char and event.char >= ' ': |
|---|
| 361 | n/a | # Regular character with a non-length-1 keycode |
|---|
| 362 | n/a | self._change_start(self.start + event.char) |
|---|
| 363 | n/a | self.lasttypedstart = self.start |
|---|
| 364 | n/a | self.listbox.select_clear(0, int(self.listbox.curselection()[0])) |
|---|
| 365 | n/a | self.listbox.select_set(self._binary_search(self.start)) |
|---|
| 366 | n/a | self._selection_changed() |
|---|
| 367 | n/a | return "break" |
|---|
| 368 | n/a | |
|---|
| 369 | n/a | else: |
|---|
| 370 | n/a | # Unknown event, close the window and let it through. |
|---|
| 371 | n/a | self.hide_window() |
|---|
| 372 | n/a | return None |
|---|
| 373 | n/a | |
|---|
| 374 | n/a | def keyrelease_event(self, event): |
|---|
| 375 | n/a | if not self.is_active(): |
|---|
| 376 | n/a | return |
|---|
| 377 | n/a | if self.widget.index("insert") != \ |
|---|
| 378 | n/a | self.widget.index("%s+%dc" % (self.startindex, len(self.start))): |
|---|
| 379 | n/a | # If we didn't catch an event which moved the insert, close window |
|---|
| 380 | n/a | self.hide_window() |
|---|
| 381 | n/a | |
|---|
| 382 | n/a | def is_active(self): |
|---|
| 383 | n/a | return self.autocompletewindow is not None |
|---|
| 384 | n/a | |
|---|
| 385 | n/a | def complete(self): |
|---|
| 386 | n/a | self._change_start(self._complete_string(self.start)) |
|---|
| 387 | n/a | # The selection doesn't change. |
|---|
| 388 | n/a | |
|---|
| 389 | n/a | def hide_window(self): |
|---|
| 390 | n/a | if not self.is_active(): |
|---|
| 391 | n/a | return |
|---|
| 392 | n/a | |
|---|
| 393 | n/a | # unbind events |
|---|
| 394 | n/a | for seq in HIDE_SEQUENCES: |
|---|
| 395 | n/a | self.widget.event_delete(HIDE_VIRTUAL_EVENT_NAME, seq) |
|---|
| 396 | n/a | self.widget.unbind(HIDE_VIRTUAL_EVENT_NAME, self.hideid) |
|---|
| 397 | n/a | self.hideid = None |
|---|
| 398 | n/a | for seq in KEYPRESS_SEQUENCES: |
|---|
| 399 | n/a | self.widget.event_delete(KEYPRESS_VIRTUAL_EVENT_NAME, seq) |
|---|
| 400 | n/a | self.widget.unbind(KEYPRESS_VIRTUAL_EVENT_NAME, self.keypressid) |
|---|
| 401 | n/a | self.keypressid = None |
|---|
| 402 | n/a | self.widget.event_delete(KEYRELEASE_VIRTUAL_EVENT_NAME, |
|---|
| 403 | n/a | KEYRELEASE_SEQUENCE) |
|---|
| 404 | n/a | self.widget.unbind(KEYRELEASE_VIRTUAL_EVENT_NAME, self.keyreleaseid) |
|---|
| 405 | n/a | self.keyreleaseid = None |
|---|
| 406 | n/a | self.listbox.unbind(LISTUPDATE_SEQUENCE, self.listupdateid) |
|---|
| 407 | n/a | self.listupdateid = None |
|---|
| 408 | n/a | self.autocompletewindow.unbind(WINCONFIG_SEQUENCE, self.winconfigid) |
|---|
| 409 | n/a | self.winconfigid = None |
|---|
| 410 | n/a | |
|---|
| 411 | n/a | # destroy widgets |
|---|
| 412 | n/a | self.scrollbar.destroy() |
|---|
| 413 | n/a | self.scrollbar = None |
|---|
| 414 | n/a | self.listbox.destroy() |
|---|
| 415 | n/a | self.listbox = None |
|---|
| 416 | n/a | self.autocompletewindow.destroy() |
|---|
| 417 | n/a | self.autocompletewindow = None |
|---|