Changeset 1474 for trunk/peppy/lib/textctrl_autocomplete.py
- Timestamp:
- 07/08/08 17:21:01 (5 months ago)
- Files:
-
- 1 modified
-
trunk/peppy/lib/textctrl_autocomplete.py (modified) (14 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/peppy/lib/textctrl_autocomplete.py
r1473 r1474 5 5 Will Sadkin (wsadkin-=at=- nameconnector -=dot=- com) 6 6 Copyright 2006 (c) CDF Inc. ( http://www.cdf-imaging.com ) 7 8 Refactored by Rob McMullen as part of peppy (http://peppy.flipturn.org) 9 7 10 Contributed to the wxPython project under the wxPython project\'s license. 8 11 """ … … 12 15 class FakePopupWindow(wx.MiniFrame): 13 16 def __init__(self, parent, style=None): 14 wx.MiniFrame.__init__(self, parent, style = wx.NO_BORDER 15 | wx.FRAME_NO_TASKBAR | wx.STAY_ON_TOP) 16 self.Panel = wx.Panel(self) 17 self.list = None 18 19 def setList(self, list): 20 #self.GetSizer().Add(list, 1, wx.EXPAND) 21 #self.SetAutoLayout(True) 22 #list.Bind(wx.EVT_SET_FOCUS, self.OnFocus) 23 #list.Bind(wx.EVT_KEY_UP, self.OnKeyUp) 24 self.list = list 17 super(FakePopupWindow, self).__init__(parent, style = wx.NO_BORDER |wx.FRAME_FLOAT_ON_PARENT 18 | wx.FRAME_NO_TASKBAR) 19 #self.Bind(wx.EVT_KEY_DOWN , self.OnKeyDown) 20 self.Bind(wx.EVT_CHAR, self.OnChar) 21 self.Bind(wx.EVT_SET_FOCUS, self.OnFocus) 22 23 def OnChar(self, evt): 24 #print("OnChar: keycode=%s" % evt.GetKeyCode()) 25 self.GetParent().GetEventHandler().ProcessEvent(evt) 25 26 26 27 def Position(self, position, size): … … 32 33 self.Move((position[0], position[1])) 33 34 34 def SetBackgroundColour(self, colour):35 self.Panel.SetBackgroundColour(colour)36 37 def Window(self):38 return self.Panel39 40 35 def ActivateParent(self): 41 36 """Activate the parent window … … 55 50 print("On Focus: set focus to %s" % str(self.GetParent())) 56 51 self.ActivateParent() 57 self.list.SetFocus()58 52 evt.Skip() 59 53 60 Window = property(Window) 61 62 if wx.Platform == '__WXMAC__': 63 wx.PopupWindow = FakePopupWindow 64 65 66 class FakePopup(wx.Frame): 67 def __init__(self, parent, pos=wx.DefaultPosition): 68 style = wx.FRAME_NO_TASKBAR | wx.FRAME_FLOAT_ON_PARENT 69 if wx.Platform == '__WXMAC__': 70 style = style | wx.BORDER_NONE | wx.POPUP_WINDOW 71 else: 72 style = style | wx.SIMPLE_BORDER 73 wx.Frame.__init__(self, parent, pos=pos, style=style) 54 55 class HighlightListBox(wx.HtmlListBox): 56 """Virtual List Box used to highlight partial matches in the ListCtrl""" 57 def __init__(self, *args, **kwargs): 58 wx.HtmlListBox.__init__(self, *args, **kwargs) 74 59 self.Bind(wx.EVT_SET_FOCUS, self.OnFocus) 75 76 sizer = wx.BoxSizer(wx.HORIZONTAL) 77 self.SetSizer(sizer) 78 79 self.list = None 80 81 def OnKillFocus(self, evt): 82 win = evt.GetEventObject() 83 print("list losing focus %s" % win) 84 evt.Skip() 85 86 def setKillFocus(self, callback): 87 self.GetParent().Bind(wx.EVT_KILL_FOCUS, callback, self.GetParent()) 88 self.list.Bind(wx.EVT_KILL_FOCUS, self.OnKillFocus) 89 pass 90 91 def setList(self, list): 92 self.GetSizer().Add(list, 1, wx.EXPAND) 93 self.SetAutoLayout(True) 94 list.Bind(wx.EVT_SET_FOCUS, self.OnFocus) 95 list.Bind(wx.EVT_KEY_UP, self.OnKeyUp) 96 self.list = list 97 98 def Show(self, state=True): 99 print("Showing %s" % state) 100 import traceback 101 traceback.print_stack() 102 if state: 103 wx.Frame.Show(self, state) 104 else: 105 wx.Frame.Show(self, state) 106 107 def ActivateParent(self): 108 """Activate the parent window 109 @postcondition: parent window is raised 110 111 """ 112 parent = self.GetParent() 113 parent.Raise() 114 parent.SetFocus() 115 60 116 61 def OnFocus(self, evt): 117 62 """Raise and reset the focus to the parent window whenever … … 120 65 121 66 """ 122 print("On Focus: set focus to %s" % str(self.GetParent()))123 self.ActivateParent()124 self.GetParent().SetFocus()67 focus = self.GetParent().GetParent() 68 #print("On Focus: set focus to %s" % str(focus)) 69 focus.SetFocus() 125 70 evt.Skip() 126 71 127 def OnKeyUp(self, evt):128 """Process key upevents in the control129 @param evt: event that called this handler130 131 """132 if evt.GetKeyCode() == wx.WXK_RETURN:133 self.__PostEvent()134 else:135 evt.Skip()136 137 138 class HighlightListBox(wx.HtmlListBox):139 """Virtual List Box used to highlight partial matches in the ListCtrl"""140 72 def setChoices(self, choices): 141 73 self.choices = choices … … 257 189 """ 258 190 if therest.has_key('style'): 259 therest['style']=wx.TE_PROCESS_ENTER | therest['style']260 else: 261 therest['style']=wx.TE_PROCESS_ENTER 191 therest['style']=wx.TE_PROCESS_ENTER | wx.TE_PROCESS_TAB |therest['style'] 192 else: 193 therest['style']=wx.TE_PROCESS_ENTER | wx.TE_PROCESS_TAB 262 194 wx.TextCtrl.__init__(self, parent, **therest ) 263 195 #Some variables … … 280 212 self.dropdownlistbox = HighlightListBox(self.dropdown) 281 213 if mac: 282 self.dropdown.setList(self.dropdownlistbox) 283 #self.dropdown.setKillFocus(self.onControlChanged) 284 #self.Bind( wx.EVT_KILL_FOCUS, self.onControlChanged, self ) 285 else: 286 self.Bind( wx.EVT_KILL_FOCUS, self.onControlChanged, self ) 214 self.Bind(wx.EVT_SET_FOCUS, self.OnSetFocus) 215 216 self.Bind(wx.EVT_KILL_FOCUS, self.onControlChanged) 287 217 288 218 self.SetChoices(choices) 289 219 290 self.Bind( wx.EVT_TEXT , self.onEnteredText, self)291 self.Bind( wx.EVT_KEY_DOWN , self.onKeyDown, self)220 self.Bind(wx.EVT_TEXT, self.OnText) 221 self.Bind(wx.EVT_KEY_DOWN, self.onKeyDown) 292 222 #If need drop down on left click 293 223 if dropDownClick: … … 307 237 self._setValueFromSelected() 308 238 309 def onEnteredText(self, event): 310 text = event.GetString() 239 def OnText(self, evt): 240 text = evt.GetString() 241 #print("OnText: %s" % text) 242 self.processText(text) 243 evt.Skip() 244 245 def processText(self, text=None): 246 if text is None: 247 text = self.GetValue() 311 248 if self._entryCallback: 312 249 self._entryCallback() 313 250 self.dropdownlistbox.clearBold() 251 #print("processText: %s" % text) 314 252 if not text: 315 253 # control is empty; hide dropdown if shown: 316 254 if self.dropdown.IsShown(): 317 255 self._showDropDown(False) 318 event.Skip()319 256 return 320 257 best = None … … 351 288 self._showDropDown(False) 352 289 self._listItemVisible() 353 event.Skip ()354 290 355 291 def onKeyDown ( self, event ) : … … 363 299 visible = self.dropdown.IsShown() 364 300 KC = event.GetKeyCode() 301 #print("keycode = %d" % KC) 365 302 if KC == wx.WXK_DOWN : 366 303 if sel < (dd.GetItemCount () - 1) : … … 393 330 dd.ScrollPages(-1) 394 331 dd.SetSelection(first) 332 elif KC == wx.WXK_RIGHT: 333 # A right arrow at the end of the string is equivalent to a tab 334 if self.GetInsertionPoint() == self.GetLastPosition(): 335 KC = wx.WXK_TAB 395 336 if KC == wx.WXK_TAB: 396 337 #print self._choices … … 441 382 wx.CallAfter(self.SetFocus) 442 383 event.Skip () 384 385 def OnSetFocus(self, evt): 386 #print("OnSetFocus: insertion point = %d" % self.GetLastPosition()) 387 wx.CallAfter(self.SetInsertionPointEnd) 443 388 444 389 def onControlChanged(self, event): 445 #print(event)446 390 changed = event.GetEventObject() 447 391 other = event.GetWindow() 448 392 #print("changed=%s other=%s" % (changed, other)) 393 if self._mac: 394 wx.CallAfter(self.SetFocus) 395 wx.CallAfter(self.SetInsertionPointEnd) 396 event.Skip() 397 return 449 398 if self.dropdown.IsShown(): 450 399 self._showDropDown( False ) … … 521 470 self.dropdown . SetPosition ( wx.Point(x, y - height - size.GetHeight()) ) 522 471 self.dropdown.Show ( show ) 472 wx.CallAfter(self.SetFocus) 523 473 524 474 def _listItemVisible( self ) : … … 543 493 self.dropdown.SetClientSize( self.popupsize ) 544 494 545 class test:546 def __init__(self):547 args = dict()548 if 1:549 args["colNames"] = ("col1", "col2")550 args["multiChoices"] = [ ("Zoey","WOW"), ("Alpha", "wxPython"),551 ("Ceda","Is"), ("Beta", "fantastic"),552 ("zoebob", "!!")]553 args["colFetch"] = 1554 else:555 args["choices"] = ["123", "cs", "cds", "Bob","Marley","Alpha"]556 args["selectCallback"] = self.selectCallback557 self.dynamic_choices = [558 'aardvark', 'abandon', 'acorn', 'acute', 'adore',559 'aegis', 'ascertain', 'asteroid',560 'beautiful', 'bold', 'classic',561 'daring', 'dazzling', 'debonair', 'definitive',562 'effective', 'elegant',563 'http://python.org', 'http://www.google.com',564 'fabulous', 'fantastic', 'friendly', 'forgiving', 'feature',565 'sage', 'scarlet', 'scenic', 'seaside', 'showpiece', 'spiffy',566 'www.wxPython.org', 'www.osafoundation.org'567 ]568 app = wx.PySimpleApp()569 frm = wx.Frame(None,-1,"Test",style=wx.TAB_TRAVERSAL|wx.DEFAULT_FRAME_STYLE)570 panel = wx.Panel(frm)571 sizer = wx.BoxSizer(wx.VERTICAL)572 self._ctrl = TextCtrlAutoComplete(panel, **args)573 but = wx.Button(panel,label="Set other multi-choice")574 but.Bind(wx.EVT_BUTTON, self.onBtMultiChoice)575 but2 = wx.Button(panel,label="Set other one-colum choice")576 but2.Bind(wx.EVT_BUTTON, self.onBtChangeChoice)577 but3 = wx.Button(panel,label="Set the starting choices")578 but3.Bind(wx.EVT_BUTTON, self.onBtStartChoices)579 but4 = wx.Button(panel,label="Enable dynamic choices")580 but4.Bind(wx.EVT_BUTTON, self.onBtDynamicChoices)581 sizer.Add(but, 0, wx.ADJUST_MINSIZE, 0)582 sizer.Add(but2, 0, wx.ADJUST_MINSIZE, 0)583 sizer.Add(but3, 0, wx.ADJUST_MINSIZE, 0)584 sizer.Add(but4, 0, wx.ADJUST_MINSIZE, 0)585 sizer.Add(self._ctrl, 0, wx.EXPAND|wx.ADJUST_MINSIZE, 0)586 panel.SetAutoLayout(True)587 panel.SetSizer(sizer)588 sizer.Fit(panel)589 sizer.SetSizeHints(panel)590 panel.Layout()591 app.SetTopWindow(frm)592 frm.Show()593 but.SetFocus()594 app.MainLoop()595 596 def onBtChangeChoice(self, event):597 #change the choices598 self._ctrl.SetChoices(["123", "cs", "cds", "Bob","Marley","Alpha"])599 self._ctrl.SetEntryCallback(None)600 self._ctrl.SetMatchFunction(None)601 602 def onBtMultiChoice(self, event):603 #change the choices604 self._ctrl.SetMultipleChoices( [ ("Test","Hello"), ("Other word","World"),605 ("Yes!","it work?") ], colFetch = 1 )606 self._ctrl.SetEntryCallback(None)607 self._ctrl.SetMatchFunction(None)608 609 def onBtStartChoices(self, event):610 #change the choices611 self._ctrl.SetMultipleChoices( [ ("Zoey","WOW"), ("Alpha", "wxPython"),612 ("Ceda","Is"), ("Beta", "fantastic"),613 ("zoebob", "!!")], colFetch = 1 )614 self._ctrl.SetEntryCallback(None)615 self._ctrl.SetMatchFunction(None)616 617 def onBtDynamicChoices(self, event):618 """619 Demonstrate dynamic adjustment of the auto-complete list, based on what's620 been typed so far:621 """622 self._ctrl.SetChoices(self.dynamic_choices)623 self._ctrl.SetEntryCallback(self.setDynamicChoices)624 self._ctrl.SetMatchFunction(self.match)625 626 def match(self, text, choice):627 """628 Demonstrate "smart" matching feature, by ignoring http:// and www. when doing629 matches.630 """631 t = text.lower()632 c = choice.lower()633 if c.startswith(t): return True634 if c.startswith(r'http://'): c = c[7:]635 if c.startswith(t): return True636 if c.startswith('www.'): c = c[4:]637 if c.startswith(t):638 return 0, len(t)639 return -1, 0640 641 def setDynamicChoices(self):642 ctrl = self._ctrl643 text = ctrl.GetValue().lower()644 current_choices = ctrl.GetChoices()645 choices = [choice for choice in self.dynamic_choices if self.match(text, choice)]646 if choices != current_choices:647 ctrl.SetChoices(choices)648 649 def selectCallback(self, values):650 """ Simply function that receive the row values when the651 user select an item652 """653 print "Select Callback called...:", values654 495 655 496 if __name__ == "__main__":
