Back to index

python3.2  3.2.2
Public Member Functions | Public Attributes | Private Member Functions
idlelib.AutoCompleteWindow.AutoCompleteWindow Class Reference

List of all members.

Public Member Functions

def __init__
def show_window
def winconfig_event
def hide_event
def listselect_event
def doubleclick_event
def keypress_event
def keyrelease_event
def is_active
def complete
def hide_window

Public Attributes

 widget
 autocompletewindow
 listbox
 scrollbar
 origselforeground
 origselbackground
 completions
 morecompletions
 mode
 start
 startindex
 lasttypedstart
 userwantswindow
 hideid
 keypressid
 listupdateid
 keyreleaseid
 doubleclickid
 lastkey_was_tab
 winconfigid

Private Member Functions

def _change_start
def _binary_search
def _complete_string
def _selection_changed

Detailed Description

Definition at line 22 of file AutoCompleteWindow.py.


Constructor & Destructor Documentation

Definition at line 24 of file AutoCompleteWindow.py.

00024 
00025     def __init__(self, widget):
00026         # The widget (Text) on which we place the AutoCompleteWindow
00027         self.widget = widget
00028         # The widgets we create
00029         self.autocompletewindow = self.listbox = self.scrollbar = None
00030         # The default foreground and background of a selection. Saved because
00031         # they are changed to the regular colors of list items when the
00032         # completion start is not a prefix of the selected completion
00033         self.origselforeground = self.origselbackground = None
00034         # The list of completions
00035         self.completions = None
00036         # A list with more completions, or None
00037         self.morecompletions = None
00038         # The completion mode. Either AutoComplete.COMPLETE_ATTRIBUTES or
00039         # AutoComplete.COMPLETE_FILES
00040         self.mode = None
00041         # The current completion start, on the text box (a string)
00042         self.start = None
00043         # The index of the start of the completion
00044         self.startindex = None
00045         # The last typed start, used so that when the selection changes,
00046         # the new start will be as close as possible to the last typed one.
00047         self.lasttypedstart = None
00048         # Do we have an indication that the user wants the completion window
00049         # (for example, he clicked the list)
00050         self.userwantswindow = None
00051         # event ids
00052         self.hideid = self.keypressid = self.listupdateid = self.winconfigid \
00053         = self.keyreleaseid = self.doubleclickid                         = None
00054         # Flag set if last keypress was a tab
00055         self.lastkey_was_tab = False

Here is the caller graph for this function:


Member Function Documentation

Find the first index in self.completions where completions[i] is
greater or equal to s, or the last index if there is no such
one.

Definition at line 69 of file AutoCompleteWindow.py.

00069 
00070     def _binary_search(self, s):
00071         """Find the first index in self.completions where completions[i] is
00072         greater or equal to s, or the last index if there is no such
00073         one."""
00074         i = 0; j = len(self.completions)
00075         while j > i:
00076             m = (i + j) // 2
00077             if self.completions[m] >= s:
00078                 j = m
00079             else:
00080                 i = m + 1
00081         return min(i, len(self.completions)-1)

Here is the caller graph for this function:

def idlelib.AutoCompleteWindow.AutoCompleteWindow._change_start (   self,
  newstart 
) [private]

Definition at line 56 of file AutoCompleteWindow.py.

00056 
00057     def _change_start(self, newstart):
00058         min_len = min(len(self.start), len(newstart))
00059         i = 0
00060         while i < min_len and self.start[i] == newstart[i]:
00061             i += 1
00062         if i < len(self.start):
00063             self.widget.delete("%s+%dc" % (self.startindex, i),
00064                                "%s+%dc" % (self.startindex, len(self.start)))
00065         if i < len(newstart):
00066             self.widget.insert("%s+%dc" % (self.startindex, i),
00067                                newstart[i:])
00068         self.start = newstart

Here is the caller graph for this function:

Assuming that s is the prefix of a string in self.completions,
return the longest string which is a prefix of all the strings which
s is a prefix of them. If s is not a prefix of a string, return s.

Definition at line 82 of file AutoCompleteWindow.py.

00082 
00083     def _complete_string(self, s):
00084         """Assuming that s is the prefix of a string in self.completions,
00085         return the longest string which is a prefix of all the strings which
00086         s is a prefix of them. If s is not a prefix of a string, return s."""
00087         first = self._binary_search(s)
00088         if self.completions[first][:len(s)] != s:
00089             # There is not even one completion which s is a prefix of.
00090             return s
00091         # Find the end of the range of completions where s is a prefix of.
00092         i = first + 1
00093         j = len(self.completions)
00094         while j > i:
00095             m = (i + j) // 2
00096             if self.completions[m][:len(s)] != s:
00097                 j = m
00098             else:
00099                 i = m + 1
00100         last = i-1
00101 
00102         if first == last: # only one possible completion
00103             return self.completions[first]
00104 
00105         # We should return the maximum prefix of first and last
00106         first_comp = self.completions[first]
00107         last_comp = self.completions[last]
00108         min_len = min(len(first_comp), len(last_comp))
00109         i = len(s)
00110         while i < min_len and first_comp[i] == last_comp[i]:
00111             i += 1
00112         return first_comp[:i]

Here is the call graph for this function:

Here is the caller graph for this function:

Should be called when the selection of the Listbox has changed.
Updates the Listbox display and calls _change_start.

Definition at line 113 of file AutoCompleteWindow.py.

00113 
00114     def _selection_changed(self):
00115         """Should be called when the selection of the Listbox has changed.
00116         Updates the Listbox display and calls _change_start."""
00117         cursel = int(self.listbox.curselection()[0])
00118 
00119         self.listbox.see(cursel)
00120 
00121         lts = self.lasttypedstart
00122         selstart = self.completions[cursel]
00123         if self._binary_search(lts) == cursel:
00124             newstart = lts
00125         else:
00126             min_len = min(len(lts), len(selstart))
00127             i = 0
00128             while i < min_len and lts[i] == selstart[i]:
00129                 i += 1
00130             newstart = selstart[:i]
00131         self._change_start(newstart)
00132 
00133         if self.completions[cursel][:len(self.start)] == self.start:
00134             # start is a prefix of the selected completion
00135             self.listbox.configure(selectbackground=self.origselbackground,
00136                                    selectforeground=self.origselforeground)
00137         else:
00138             self.listbox.configure(selectbackground=self.listbox.cget("bg"),
00139                                    selectforeground=self.listbox.cget("fg"))
00140             # If there are more completions, show them, and call me again.
00141             if self.morecompletions:
00142                 self.completions = self.morecompletions
00143                 self.morecompletions = None
00144                 self.listbox.delete(0, END)
00145                 for item in self.completions:
00146                     self.listbox.insert(END, item)
00147                 self.listbox.select_set(self._binary_search(self.start))
00148                 self._selection_changed()

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 373 of file AutoCompleteWindow.py.

00373 
00374     def complete(self):
00375         self._change_start(self._complete_string(self.start))
00376         # The selection doesn't change.

Here is the call graph for this function:

Definition at line 250 of file AutoCompleteWindow.py.

00250 
00251     def doubleclick_event(self, event):
00252         # Put the selected completion in the text, and close the list
00253         cursel = int(self.listbox.curselection()[0])
00254         self._change_start(self.completions[cursel])
00255         self.hide_window()

Here is the call graph for this function:

Definition at line 238 of file AutoCompleteWindow.py.

00238 
00239     def hide_event(self, event):
00240         if not self.is_active():
00241             return
00242         self.hide_window()

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 377 of file AutoCompleteWindow.py.

00377 
00378     def hide_window(self):
00379         if not self.is_active():
00380             return
00381 
00382         # unbind events
00383         for seq in HIDE_SEQUENCES:
00384             self.widget.event_delete(HIDE_VIRTUAL_EVENT_NAME, seq)
00385         self.widget.unbind(HIDE_VIRTUAL_EVENT_NAME, self.hideid)
00386         self.hideid = None
00387         for seq in KEYPRESS_SEQUENCES:
00388             self.widget.event_delete(KEYPRESS_VIRTUAL_EVENT_NAME, seq)
00389         self.widget.unbind(KEYPRESS_VIRTUAL_EVENT_NAME, self.keypressid)
00390         self.keypressid = None
00391         self.widget.event_delete(KEYRELEASE_VIRTUAL_EVENT_NAME,
00392                                  KEYRELEASE_SEQUENCE)
00393         self.widget.unbind(KEYRELEASE_VIRTUAL_EVENT_NAME, self.keyreleaseid)
00394         self.keyreleaseid = None
00395         self.listbox.unbind(LISTUPDATE_SEQUENCE, self.listupdateid)
00396         self.listupdateid = None
00397         self.autocompletewindow.unbind(WINCONFIG_SEQUENCE, self.winconfigid)
00398         self.winconfigid = None
00399 
00400         # destroy widgets
00401         self.scrollbar.destroy()
00402         self.scrollbar = None
00403         self.listbox.destroy()
00404         self.listbox = None
00405         self.autocompletewindow.destroy()
00406         self.autocompletewindow = None

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 370 of file AutoCompleteWindow.py.

00370 
00371     def is_active(self):
00372         return self.autocompletewindow is not None

Here is the caller graph for this function:

Definition at line 256 of file AutoCompleteWindow.py.

00256 
00257     def keypress_event(self, event):
00258         if not self.is_active():
00259             return
00260         keysym = event.keysym
00261         if hasattr(event, "mc_state"):
00262             state = event.mc_state
00263         else:
00264             state = 0
00265         if keysym != "Tab":
00266             self.lastkey_was_tab = False
00267         if (len(keysym) == 1 or keysym in ("underscore", "BackSpace")
00268             or (self.mode == COMPLETE_FILES and keysym in
00269                 ("period", "minus"))) \
00270            and not (state & ~MC_SHIFT):
00271             # Normal editing of text
00272             if len(keysym) == 1:
00273                 self._change_start(self.start + keysym)
00274             elif keysym == "underscore":
00275                 self._change_start(self.start + '_')
00276             elif keysym == "period":
00277                 self._change_start(self.start + '.')
00278             elif keysym == "minus":
00279                 self._change_start(self.start + '-')
00280             else:
00281                 # keysym == "BackSpace"
00282                 if len(self.start) == 0:
00283                     self.hide_window()
00284                     return
00285                 self._change_start(self.start[:-1])
00286             self.lasttypedstart = self.start
00287             self.listbox.select_clear(0, int(self.listbox.curselection()[0]))
00288             self.listbox.select_set(self._binary_search(self.start))
00289             self._selection_changed()
00290             return "break"
00291 
00292         elif keysym == "Return":
00293             self.hide_window()
00294             return
00295 
00296         elif (self.mode == COMPLETE_ATTRIBUTES and keysym in
00297               ("period", "space", "parenleft", "parenright", "bracketleft",
00298                "bracketright")) or \
00299              (self.mode == COMPLETE_FILES and keysym in
00300               ("slash", "backslash", "quotedbl", "apostrophe")) \
00301              and not (state & ~MC_SHIFT):
00302             # If start is a prefix of the selection, but is not '' when
00303             # completing file names, put the whole
00304             # selected completion. Anyway, close the list.
00305             cursel = int(self.listbox.curselection()[0])
00306             if self.completions[cursel][:len(self.start)] == self.start \
00307                and (self.mode == COMPLETE_ATTRIBUTES or self.start):
00308                 self._change_start(self.completions[cursel])
00309             self.hide_window()
00310             return
00311 
00312         elif keysym in ("Home", "End", "Prior", "Next", "Up", "Down") and \
00313              not state:
00314             # Move the selection in the listbox
00315             self.userwantswindow = True
00316             cursel = int(self.listbox.curselection()[0])
00317             if keysym == "Home":
00318                 newsel = 0
00319             elif keysym == "End":
00320                 newsel = len(self.completions)-1
00321             elif keysym in ("Prior", "Next"):
00322                 jump = self.listbox.nearest(self.listbox.winfo_height()) - \
00323                        self.listbox.nearest(0)
00324                 if keysym == "Prior":
00325                     newsel = max(0, cursel-jump)
00326                 else:
00327                     assert keysym == "Next"
00328                     newsel = min(len(self.completions)-1, cursel+jump)
00329             elif keysym == "Up":
00330                 newsel = max(0, cursel-1)
00331             else:
00332                 assert keysym == "Down"
00333                 newsel = min(len(self.completions)-1, cursel+1)
00334             self.listbox.select_clear(cursel)
00335             self.listbox.select_set(newsel)
00336             self._selection_changed()
00337             self._change_start(self.completions[newsel])
00338             return "break"
00339 
00340         elif (keysym == "Tab" and not state):
00341             if self.lastkey_was_tab:
00342                 # two tabs in a row; insert current selection and close acw
00343                 cursel = int(self.listbox.curselection()[0])
00344                 self._change_start(self.completions[cursel])
00345                 self.hide_window()
00346                 return "break"
00347             else:
00348                 # first tab; let AutoComplete handle the completion
00349                 self.userwantswindow = True
00350                 self.lastkey_was_tab = True
00351                 return
00352 
00353         elif any(s in keysym for s in ("Shift", "Control", "Alt",
00354                                        "Meta", "Command", "Option")):
00355             # A modifier key, so ignore
00356             return
00357 
00358         else:
00359             # Unknown event, close the window and let it through.
00360             self.hide_window()
00361             return

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 362 of file AutoCompleteWindow.py.

00362 
00363     def keyrelease_event(self, event):
00364         if not self.is_active():
00365             return
00366         if self.widget.index("insert") != \
00367            self.widget.index("%s+%dc" % (self.startindex, len(self.start))):
00368             # If we didn't catch an event which moved the insert, close window
00369             self.hide_window()

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 243 of file AutoCompleteWindow.py.

00243 
00244     def listselect_event(self, event):
00245         if not self.is_active():
00246             return
00247         self.userwantswindow = True
00248         cursel = int(self.listbox.curselection()[0])
00249         self._change_start(self.completions[cursel])

Here is the call graph for this function:

Here is the caller graph for this function:

def idlelib.AutoCompleteWindow.AutoCompleteWindow.show_window (   self,
  comp_lists,
  index,
  complete,
  mode,
  userWantsWin 
)
Show the autocomplete list, bind events.
If complete is True, complete the text, and if there is exactly one
matching completion, don't open a list.

Definition at line 149 of file AutoCompleteWindow.py.

00149 
00150     def show_window(self, comp_lists, index, complete, mode, userWantsWin):
00151         """Show the autocomplete list, bind events.
00152         If complete is True, complete the text, and if there is exactly one
00153         matching completion, don't open a list."""
00154         # Handle the start we already have
00155         self.completions, self.morecompletions = comp_lists
00156         self.mode = mode
00157         self.startindex = self.widget.index(index)
00158         self.start = self.widget.get(self.startindex, "insert")
00159         if complete:
00160             completed = self._complete_string(self.start)
00161             self._change_start(completed)
00162             i = self._binary_search(completed)
00163             if self.completions[i] == completed and \
00164                (i == len(self.completions)-1 or
00165                 self.completions[i+1][:len(completed)] != completed):
00166                 # There is exactly one matching completion
00167                 return
00168         self.userwantswindow = userWantsWin
00169         self.lasttypedstart = self.start
00170 
00171         # Put widgets in place
00172         self.autocompletewindow = acw = Toplevel(self.widget)
00173         # Put it in a position so that it is not seen.
00174         acw.wm_geometry("+10000+10000")
00175         # Make it float
00176         acw.wm_overrideredirect(1)
00177         try:
00178             # This command is only needed and available on Tk >= 8.4.0 for OSX
00179             # Without it, call tips intrude on the typing process by grabbing
00180             # the focus.
00181             acw.tk.call("::tk::unsupported::MacWindowStyle", "style", acw._w,
00182                         "help", "noActivates")
00183         except TclError:
00184             pass
00185         self.scrollbar = scrollbar = Scrollbar(acw, orient=VERTICAL)
00186         self.listbox = listbox = Listbox(acw, yscrollcommand=scrollbar.set,
00187                                          exportselection=False, bg="white")
00188         for item in self.completions:
00189             listbox.insert(END, item)
00190         self.origselforeground = listbox.cget("selectforeground")
00191         self.origselbackground = listbox.cget("selectbackground")
00192         scrollbar.config(command=listbox.yview)
00193         scrollbar.pack(side=RIGHT, fill=Y)
00194         listbox.pack(side=LEFT, fill=BOTH, expand=True)
00195 
00196         # Initialize the listbox selection
00197         self.listbox.select_set(self._binary_search(self.start))
00198         self._selection_changed()
00199 
00200         # bind events
00201         self.hideid = self.widget.bind(HIDE_VIRTUAL_EVENT_NAME,
00202                                        self.hide_event)
00203         for seq in HIDE_SEQUENCES:
00204             self.widget.event_add(HIDE_VIRTUAL_EVENT_NAME, seq)
00205         self.keypressid = self.widget.bind(KEYPRESS_VIRTUAL_EVENT_NAME,
00206                                            self.keypress_event)
00207         for seq in KEYPRESS_SEQUENCES:
00208             self.widget.event_add(KEYPRESS_VIRTUAL_EVENT_NAME, seq)
00209         self.keyreleaseid = self.widget.bind(KEYRELEASE_VIRTUAL_EVENT_NAME,
00210                                              self.keyrelease_event)
00211         self.widget.event_add(KEYRELEASE_VIRTUAL_EVENT_NAME,KEYRELEASE_SEQUENCE)
00212         self.listupdateid = listbox.bind(LISTUPDATE_SEQUENCE,
00213                                          self.listselect_event)
00214         self.winconfigid = acw.bind(WINCONFIG_SEQUENCE, self.winconfig_event)
00215         self.doubleclickid = listbox.bind(DOUBLECLICK_SEQUENCE,
00216                                           self.doubleclick_event)

Here is the call graph for this function:

Definition at line 217 of file AutoCompleteWindow.py.

00217 
00218     def winconfig_event(self, event):
00219         if not self.is_active():
00220             return
00221         # Position the completion list window
00222         text = self.widget
00223         text.see(self.startindex)
00224         x, y, cx, cy = text.bbox(self.startindex)
00225         acw = self.autocompletewindow
00226         acw_width, acw_height = acw.winfo_width(), acw.winfo_height()
00227         text_width, text_height = text.winfo_width(), text.winfo_height()
00228         new_x = text.winfo_rootx() + min(x, max(0, text_width - acw_width))
00229         new_y = text.winfo_rooty() + y
00230         if (text_height - (y + cy) >= acw_height # enough height below
00231             or y < acw_height): # not enough height above
00232             # place acw below current line
00233             new_y += cy
00234         else:
00235             # place acw above current line
00236             new_y -= acw_height
00237         acw.wm_geometry("+%d+%d" % (new_x, new_y))

Here is the call graph for this function:


Member Data Documentation

Definition at line 28 of file AutoCompleteWindow.py.

Definition at line 34 of file AutoCompleteWindow.py.

Definition at line 52 of file AutoCompleteWindow.py.

Definition at line 51 of file AutoCompleteWindow.py.

Definition at line 51 of file AutoCompleteWindow.py.

Definition at line 52 of file AutoCompleteWindow.py.

Definition at line 54 of file AutoCompleteWindow.py.

Definition at line 46 of file AutoCompleteWindow.py.

Definition at line 28 of file AutoCompleteWindow.py.

Definition at line 51 of file AutoCompleteWindow.py.

Definition at line 39 of file AutoCompleteWindow.py.

Definition at line 36 of file AutoCompleteWindow.py.

Definition at line 32 of file AutoCompleteWindow.py.

Definition at line 32 of file AutoCompleteWindow.py.

Definition at line 28 of file AutoCompleteWindow.py.

Definition at line 41 of file AutoCompleteWindow.py.

Definition at line 43 of file AutoCompleteWindow.py.

Definition at line 49 of file AutoCompleteWindow.py.

Definition at line 26 of file AutoCompleteWindow.py.

Definition at line 213 of file AutoCompleteWindow.py.


The documentation for this class was generated from the following file: