TextCtrlAutoComplete.py
author etisserant
Tue, 23 Dec 2008 19:31:28 +0100
changeset 279 47d29c4b55a3
parent 268 66843376a982
child 295 bc6fc07c3153
permissions -rw-r--r--
Fixed minimize/maximize invertion
268
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
     1
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
     2
'''
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
     3
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
     4
wxPython Custom Widget Collection 20060207
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
     5
Written By: Edward Flick (eddy -=at=- cdf-imaging -=dot=- com)
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
     6
            Michele Petrazzo (michele -=dot=- petrazzo -=at=- unipex -=dot=- it)
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
     7
            Will Sadkin (wsadkin-=at=- nameconnector -=dot=- com)
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
     8
Copyright 2006 (c) CDF Inc. ( http://www.cdf-imaging.com )
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
     9
Contributed to the wxPython project under the wxPython project's license.
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
    10
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
    11
'''
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
    12
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
    13
import locale, wx, sys, cStringIO
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
    14
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
    15
import  wx.lib.mixins.listctrl  as  listmix
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
    16
import cPickle
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
    17
from wx import ImageFromStream, BitmapFromImage
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
    18
#----------------------------------------------------------------------
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
    19
def getSmallUpArrowData():
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
    20
    return \
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
    21
'\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR\x00\x00\x00\x10\x00\x00\x00\x10\x08\x06\
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
    22
\x00\x00\x00\x1f\xf3\xffa\x00\x00\x00\x04sBIT\x08\x08\x08\x08|\x08d\x88\x00\
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
    23
\x00\x00<IDAT8\x8dcddbf\xa0\x040Q\xa4{h\x18\xf0\xff\xdf\xdf\xffd\x1b\x00\xd3\
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
    24
\x8c\xcf\x10\x9c\x06\xa0k\xc2e\x08m\xc2\x00\x97m\xd8\xc41\x0c \x14h\xe8\xf2\
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
    25
\x8c\xa3)q\x10\x18\x00\x00R\xd8#\xec\xb2\xcd\xc1Y\x00\x00\x00\x00IEND\xaeB`\
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
    26
\x82'
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
    27
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
    28
def getSmallUpArrowBitmap():
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
    29
    return BitmapFromImage(getSmallUpArrowImage())
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
    30
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
    31
def getSmallUpArrowImage():
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
    32
    stream = cStringIO.StringIO(getSmallUpArrowData())
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
    33
    return ImageFromStream(stream)
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
    34
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
    35
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
    36
def getSmallDnArrowData():
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
    37
    return \
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
    38
"\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR\x00\x00\x00\x10\x00\x00\x00\x10\x08\x06\
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
    39
\x00\x00\x00\x1f\xf3\xffa\x00\x00\x00\x04sBIT\x08\x08\x08\x08|\x08d\x88\x00\
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
    40
\x00\x00HIDAT8\x8dcddbf\xa0\x040Q\xa4{\xd4\x00\x06\x06\x06\x06\x06\x16t\x81\
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
    41
\xff\xff\xfe\xfe'\xa4\x89\x91\x89\x99\x11\xa7\x0b\x90%\ti\xc6j\x00>C\xb0\x89\
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
    42
\xd3.\x10\xd1m\xc3\xe5*\xbc.\x80i\xc2\x17.\x8c\xa3y\x81\x01\x00\xa1\x0e\x04e\
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
    43
?\x84B\xef\x00\x00\x00\x00IEND\xaeB`\x82"
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
    44
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
    45
def getSmallDnArrowBitmap():
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
    46
    return BitmapFromImage(getSmallDnArrowImage())
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
    47
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
    48
def getSmallDnArrowImage():
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
    49
    stream = cStringIO.StringIO(getSmallDnArrowData())
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
    50
    return ImageFromStream(stream)
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
    51
#----------------------------------------------------------------------
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
    52
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
    53
class myListCtrl(wx.ListCtrl, listmix.ListCtrlAutoWidthMixin):
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
    54
    def __init__(self, parent, ID=-1, pos=wx.DefaultPosition,
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
    55
                 size=wx.DefaultSize, style=0):
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
    56
        wx.ListCtrl.__init__(self, parent, ID, pos, size, style)
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
    57
        listmix.ListCtrlAutoWidthMixin.__init__(self)
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
    58
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
    59
class TextCtrlAutoComplete (wx.TextCtrl, listmix.ColumnSorterMixin ):
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
    60
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
    61
    def __init__ ( self, parent, colNames=None, choices = None,
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
    62
                  multiChoices=None, showHead=True, dropDownClick=True,
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
    63
                  colFetch=-1, colSearch=0, hideOnNoMatch=True,
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
    64
                  selectCallback=None, entryCallback=None, matchFunction=None,
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
    65
                  element_path=None, **therest) :
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
    66
        '''
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
    67
        Constructor works just like wx.TextCtrl except you can pass in a
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
    68
        list of choices.  You can also change the choice list at any time
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
    69
        by calling setChoices.
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
    70
        '''
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
    71
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
    72
        if therest.has_key('style'):
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
    73
            therest['style']=wx.TE_PROCESS_ENTER | therest['style']
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
    74
        else:
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
    75
            therest['style']=wx.TE_PROCESS_ENTER
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
    76
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
    77
        wx.TextCtrl.__init__(self, parent, **therest )
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
    78
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
    79
        #Some variables
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
    80
        self._dropDownClick = dropDownClick
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
    81
        self._colNames = colNames
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
    82
        self._multiChoices = multiChoices
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
    83
        self._showHead = showHead
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
    84
        self._choices = choices
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
    85
        self._lastinsertionpoint = 0
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
    86
        self._hideOnNoMatch = hideOnNoMatch
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
    87
        self._selectCallback = selectCallback
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
    88
        self._entryCallback = entryCallback
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
    89
        self._matchFunction = matchFunction
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
    90
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
    91
        self._screenheight = wx.SystemSettings.GetMetric( wx.SYS_SCREEN_Y )
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
    92
        self.element_path = element_path
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
    93
        #sort variable needed by listmix
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
    94
        self.itemDataMap = dict()
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
    95
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
    96
        #widgets
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
    97
        self.dropdown = wx.PopupWindow( self )
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
    98
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
    99
        #Control the style
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   100
        flags = wx.LC_REPORT | wx.LC_SINGLE_SEL | wx.LC_SORT_ASCENDING
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   101
        if not (showHead and multiChoices) :
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   102
            flags = flags | wx.LC_NO_HEADER
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   103
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   104
        #Create the list and bind the events
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   105
        self.dropdownlistbox = myListCtrl( self.dropdown, style=flags,
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   106
                                 pos=wx.Point( 0, 0) )
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   107
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   108
        #initialize the parent
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   109
        if multiChoices: ln = len(multiChoices)
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   110
        else: ln = 1
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   111
        #else: ln = len(choices)
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   112
        listmix.ColumnSorterMixin.__init__(self, ln)
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   113
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   114
        #load the data
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   115
        if multiChoices: self.SetMultipleChoices (multiChoices, colSearch=colSearch, colFetch=colFetch)
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   116
        else: self.SetChoices ( choices )
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   117
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   118
        #gp = self
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   119
        #while ( gp != None ) :
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   120
        #    gp.Bind ( wx.EVT_MOVE , self.onControlChanged, gp )
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   121
        #    gp.Bind ( wx.EVT_SIZE , self.onControlChanged, gp )
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   122
        #    gp = gp.GetParent()
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   123
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   124
        self.Bind( wx.EVT_KILL_FOCUS, self.onControlChanged, self )
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   125
        self.Bind( wx.EVT_TEXT , self.onEnteredText, self )
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   126
        self.Bind( wx.EVT_KEY_DOWN , self.onKeyDown, self )
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   127
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   128
        #If need drop down on left click
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   129
        if dropDownClick:
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   130
            self.Bind ( wx.EVT_LEFT_DOWN , self.onClickToggleDown, self )
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   131
            self.Bind ( wx.EVT_LEFT_UP , self.onClickToggleUp, self )
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   132
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   133
        self.dropdown.Bind( wx.EVT_LISTBOX , self.onListItemSelected, self.dropdownlistbox )
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   134
        self.dropdownlistbox.Bind(wx.EVT_LEFT_DOWN, self.onListClick)
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   135
        self.dropdownlistbox.Bind(wx.EVT_LEFT_DCLICK, self.onListDClick)
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   136
        self.dropdownlistbox.Bind(wx.EVT_LIST_COL_CLICK, self.onListColClick)
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   137
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   138
        self.il = wx.ImageList(16, 16)
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   139
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   140
        self.sm_dn = self.il.Add(getSmallDnArrowBitmap())
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   141
        self.sm_up = self.il.Add(getSmallUpArrowBitmap())
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   142
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   143
        self.dropdownlistbox.SetImageList(self.il, wx.IMAGE_LIST_SMALL)
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   144
        self._ascending = True
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   145
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   146
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   147
    #-- methods called from mixin class
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   148
    def GetSortImages(self):
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   149
        return (self.sm_dn, self.sm_up)
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   150
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   151
    def GetListCtrl(self):
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   152
        return self.dropdownlistbox
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   153
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   154
    # -- event methods
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   155
    def onListClick(self, evt):
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   156
        toSel, flag = self.dropdownlistbox.HitTest( evt.GetPosition() )
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   157
        #no values on poition, return
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   158
        if toSel == -1: return
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   159
        self.dropdownlistbox.Select(toSel)
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   160
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   161
    def onListDClick(self, evt):
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   162
        self._setValueFromSelected()
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   163
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   164
    def onListColClick(self, evt):
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   165
        col = evt.GetColumn()
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   166
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   167
        #reverse the sort
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   168
        if col == self._colSearch:
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   169
            self._ascending = not self._ascending
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   170
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   171
        self.SortListItems( evt.GetColumn(), ascending=self._ascending )
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   172
        self._colSearch = evt.GetColumn()
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   173
        evt.Skip()
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   174
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   175
    def onEnteredText(self, event):
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   176
        text = event.GetString()
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   177
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   178
        if self._entryCallback:
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   179
            self._entryCallback()
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   180
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   181
        if not text:
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   182
            # control is empty; hide dropdown if shown:
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   183
            if self.dropdown.IsShown():
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   184
                self._showDropDown(False)
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   185
            event.Skip()
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   186
            return
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   187
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   188
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   189
        found = False
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   190
        if self._multiChoices:
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   191
            #load the sorted data into the listbox
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   192
            dd = self.dropdownlistbox
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   193
            choices = [dd.GetItem(x, self._colSearch).GetText()
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   194
                for x in xrange(dd.GetItemCount())]
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   195
        else:
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   196
            choices = self._choices
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   197
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   198
        for numCh, choice in enumerate(choices):
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   199
            if self._matchFunction and self._matchFunction(text, choice):
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   200
                found = True
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   201
            elif choice.lower().startswith(text.lower()) :
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   202
                found = True
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   203
            if found:
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   204
                self._showDropDown(True)
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   205
                item = self.dropdownlistbox.GetItem(numCh)
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   206
                toSel = item.GetId()
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   207
                self.dropdownlistbox.Select(toSel)
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   208
                break
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   209
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   210
        if not found:
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   211
            self.dropdownlistbox.Select(self.dropdownlistbox.GetFirstSelected(), False)
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   212
            if self._hideOnNoMatch:
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   213
                self._showDropDown(False)
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   214
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   215
        self._listItemVisible()
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   216
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   217
        event.Skip ()
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   218
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   219
    def onKeyDown ( self, event ) :
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   220
        """ Do some work when the user press on the keys:
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   221
            up and down: move the cursor
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   222
            left and right: move the search
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   223
        """
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   224
        skip = True
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   225
        sel = self.dropdownlistbox.GetFirstSelected()
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   226
        visible = self.dropdown.IsShown()
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   227
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   228
        KC = event.GetKeyCode()
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   229
        if KC == wx.WXK_DOWN :
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   230
            if sel < (self.dropdownlistbox.GetItemCount () - 1) :
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   231
                self.dropdownlistbox.Select ( sel+1 )
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   232
                self._listItemVisible()
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   233
            self._showDropDown ()
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   234
            skip = False
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   235
        elif KC == wx.WXK_UP :
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   236
            if sel > 0 :
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   237
                self.dropdownlistbox.Select ( sel - 1 )
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   238
                self._listItemVisible()
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   239
            self._showDropDown ()
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   240
            skip = False
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   241
        elif KC == wx.WXK_LEFT :
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   242
            if not self._multiChoices: return
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   243
            if self._colSearch > 0:
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   244
                self._colSearch -=1
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   245
            self._showDropDown ()
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   246
        elif KC == wx.WXK_RIGHT:
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   247
            if not self._multiChoices: return
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   248
            if self._colSearch < self.dropdownlistbox.GetColumnCount() -1:
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   249
                self._colSearch += 1
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   250
            self._showDropDown()
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   251
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   252
        if visible :
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   253
            if event.GetKeyCode() == wx.WXK_RETURN :
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   254
                self._setValueFromSelected()
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   255
                skip = False
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   256
            if event.GetKeyCode() == wx.WXK_ESCAPE :
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   257
                self._showDropDown( False )
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   258
                skip = False
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   259
        if skip :
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   260
            event.Skip()
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   261
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   262
    def onListItemSelected (self, event):
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   263
        self._setValueFromSelected()
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   264
        event.Skip()
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   265
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   266
    def onClickToggleDown(self, event):
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   267
        self._lastinsertionpoint = self.GetInsertionPoint()
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   268
        event.Skip ()
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   269
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   270
    def onClickToggleUp ( self, event ) :
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   271
        if ( self.GetInsertionPoint() == self._lastinsertionpoint ) :
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   272
            self._showDropDown ( not self.dropdown.IsShown() )
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   273
        event.Skip ()
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   274
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   275
    def onControlChanged(self, event):
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   276
        res = self.GetValue()
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   277
        config = wx.ConfigBase.Get()
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   278
        listentries = cPickle.loads(str(config.Read(self.element_path, cPickle.dumps([]))))
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   279
        if len(res) and res not in listentries:
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   280
            config.Write(self.element_path, cPickle.dumps((listentries + [res])[-10:]))
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   281
            config.Flush()
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   282
            self.SetChoices((listentries + [res])[-10:])
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   283
        self._showDropDown( False )
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   284
        event.Skip()
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   285
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   286
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   287
    # -- Interfaces methods
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   288
    def SetMultipleChoices(self, choices, colSearch=0, colFetch=-1):
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   289
        ''' Set multi-column choice
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   290
        '''
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   291
        self._multiChoices = choices
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   292
        self._choices = None
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   293
        if not isinstance(self._multiChoices, list):
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   294
            self._multiChoices = [ x for x in self._multiChoices]
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   295
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   296
        flags = wx.LC_REPORT | wx.LC_SINGLE_SEL | wx.LC_SORT_ASCENDING
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   297
        if not self._showHead:
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   298
            flags |= wx.LC_NO_HEADER
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   299
        self.dropdownlistbox.SetWindowStyleFlag(flags)
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   300
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   301
        #prevent errors on "old" systems
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   302
        if sys.version.startswith("2.3"):
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   303
            self._multiChoices.sort(lambda x, y: cmp(x[0].lower(), y[0].lower()))
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   304
        else:
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   305
            self._multiChoices.sort(key=lambda x: locale.strxfrm(x[0]).lower() )
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   306
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   307
        self._updateDataList(self._multiChoices)
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   308
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   309
        lChoices = len(choices)
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   310
        if lChoices < 2:
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   311
            raise ValueError, "You have to pass me a multi-dimension list"
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   312
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   313
        for numCol, rowValues in enumerate(choices[0]):
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   314
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   315
            if self._colNames: colName = self._colNames[numCol]
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   316
            else: colName = "Select %i" % numCol
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   317
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   318
            self.dropdownlistbox.InsertColumn(numCol, colName)
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   319
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   320
        for numRow, valRow in enumerate(choices):
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   321
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   322
            for numCol, colVal in enumerate(valRow):
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   323
                if numCol == 0:
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   324
                    index = self.dropdownlistbox.InsertImageStringItem(sys.maxint, colVal, -1)
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   325
                self.dropdownlistbox.SetStringItem(index, numCol, colVal)
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   326
                self.dropdownlistbox.SetItemData(index, numRow)
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   327
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   328
        self._setListSize()
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   329
        self._colSearch = colSearch
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   330
        self._colFetch = colFetch
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   331
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   332
    def SetChoices(self, choices):
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   333
        '''
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   334
        Sets the choices available in the popup wx.ListBox.
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   335
        The items will be sorted case insensitively.
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   336
        '''
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   337
        self._choices = choices
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   338
        self._multiChoices = None
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   339
        flags = wx.LC_REPORT | wx.LC_SINGLE_SEL | wx.LC_SORT_ASCENDING | wx.LC_NO_HEADER
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   340
        self.dropdownlistbox.SetWindowStyleFlag(flags)
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   341
                
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   342
        #if not isinstance(choices, list):
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   343
        #    self._choices = [ x for x in choices if len(x)]
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   344
        self._choices = [ x for x in choices if len(x)]
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   345
        #prevent errors on "old" systems
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   346
        if sys.version.startswith("2.3"):
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   347
            self._choices.sort(lambda x, y: cmp(x.lower(), y.lower()))
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   348
        else:
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   349
            self._choices.sort(key=lambda x: locale.strxfrm(x).lower())
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   350
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   351
        self._updateDataList(self._choices)
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   352
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   353
        self.dropdownlistbox.InsertColumn(0, "")
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   354
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   355
        for num, colVal in enumerate(self._choices):
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   356
            index = self.dropdownlistbox.InsertImageStringItem(sys.maxint, colVal, -1)
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   357
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   358
            self.dropdownlistbox.SetStringItem(index, 0, colVal)
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   359
            self.dropdownlistbox.SetItemData(index, num)
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   360
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   361
        self._setListSize()
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   362
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   363
        # there is only one choice for both search and fetch if setting a single column:
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   364
        self._colSearch = 0
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   365
        self._colFetch = -1
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   366
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   367
    def GetChoices(self):
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   368
        if self._choices:
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   369
            return self._choices
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   370
        else:
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   371
            return self._multiChoices
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   372
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   373
    def SetSelectCallback(self, cb=None):
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   374
        self._selectCallback = cb
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   375
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   376
    def SetEntryCallback(self, cb=None):
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   377
        self._entryCallback = cb
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   378
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   379
    def SetMatchFunction(self, mf=None):
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   380
        self._matchFunction = mf
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   381
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   382
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   383
    #-- Internal methods
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   384
    def _setValueFromSelected( self ) :
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   385
         '''
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   386
         Sets the wx.TextCtrl value from the selected wx.ListCtrl item.
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   387
         Will do nothing if no item is selected in the wx.ListCtrl.
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   388
         '''
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   389
         sel = self.dropdownlistbox.GetFirstSelected()
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   390
         if sel > -1:
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   391
            if self._colFetch != -1: col = self._colFetch
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   392
            else: col = self._colSearch
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   393
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   394
            itemtext = self.dropdownlistbox.GetItem(sel, col).GetText()
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   395
            if self._selectCallback:
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   396
                dd = self.dropdownlistbox
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   397
                values = [dd.GetItem(sel, x).GetText()
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   398
                    for x in xrange(dd.GetColumnCount())]
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   399
                self._selectCallback( values )
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   400
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   401
            self.SetValue (itemtext)
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   402
            self.SetInsertionPointEnd ()
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   403
            self.SetSelection ( -1, -1 )
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   404
            self._showDropDown ( False )
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   405
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   406
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   407
    def _showDropDown ( self, show = True ) :
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   408
        '''
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   409
        Either display the drop down list (show = True) or hide it (show = False).
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   410
        '''
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   411
        if show :
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   412
            size = self.dropdown.GetSize()
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   413
            width, height = self . GetSizeTuple()
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   414
            x, y = self . ClientToScreenXY ( 0, height )
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   415
            if size.GetWidth() != width :
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   416
                size.SetWidth(width)
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   417
                self.dropdown.SetSize(size)
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   418
                self.dropdownlistbox.SetSize(self.dropdown.GetClientSize())
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   419
            if (y + size.GetHeight()) < self._screenheight :
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   420
                self.dropdown . SetPosition ( wx.Point(x, y) )
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   421
            else:
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   422
                self.dropdown . SetPosition ( wx.Point(x, y - height - size.GetHeight()) )
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   423
        self.dropdown.Show ( show )
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   424
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   425
    def _listItemVisible( self ) :
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   426
        '''
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   427
        Moves the selected item to the top of the list ensuring it is always visible.
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   428
        '''
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   429
        toSel =  self.dropdownlistbox.GetFirstSelected ()
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   430
        if toSel == -1: return
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   431
        self.dropdownlistbox.EnsureVisible( toSel )
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   432
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   433
    def _updateDataList(self, choices):
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   434
        #delete, if need, all the previous data
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   435
        if self.dropdownlistbox.GetColumnCount() != 0:
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   436
            self.dropdownlistbox.DeleteAllColumns()
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   437
            self.dropdownlistbox.DeleteAllItems()
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   438
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   439
        #and update the dict
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   440
        if choices:
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   441
            for numVal, data in enumerate(choices):
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   442
                self.itemDataMap[numVal] = data
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   443
        else:
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   444
            numVal = 0
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   445
        self.SetColumnCount(numVal)
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   446
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   447
    def _setListSize(self):
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   448
        if self._multiChoices:
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   449
            choices = self._multiChoices
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   450
        else:
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   451
            choices = self._choices
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   452
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   453
        longest = 0
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   454
        for choice in choices :
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   455
            longest = max(len(choice), longest)
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   456
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   457
        longest += 3
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   458
        itemcount = min( len( choices ) , 7 ) + 2
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   459
        charheight = self.dropdownlistbox.GetCharHeight()
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   460
        charwidth = self.dropdownlistbox.GetCharWidth()
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   461
        self.popupsize = wx.Size( charwidth*longest, charheight*itemcount )
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   462
        self.dropdownlistbox.SetSize ( self.popupsize )
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   463
        self.dropdown.SetClientSize( self.popupsize )
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   464
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   465
    
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   466
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   467
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   468
class test:
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   469
    def __init__(self):
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   470
        args = dict()
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   471
        if 1:
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   472
            args["colNames"] = ("col1", "col2")
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   473
            args["multiChoices"] = [ ("Zoey","WOW"), ("Alpha", "wxPython"),
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   474
                                    ("Ceda","Is"), ("Beta", "fantastic"),
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   475
                                    ("zoebob", "!!")]
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   476
            args["colFetch"] = 1
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   477
        else:
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   478
            args["choices"] = ["123", "cs", "cds", "Bob","Marley","Alpha"]
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   479
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   480
        args["selectCallback"] = self.selectCallback
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   481
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   482
        self.dynamic_choices = [
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   483
                        'aardvark', 'abandon', 'acorn', 'acute', 'adore',
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   484
                        'aegis', 'ascertain', 'asteroid',
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   485
                        'beautiful', 'bold', 'classic',
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   486
                        'daring', 'dazzling', 'debonair', 'definitive',
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   487
                        'effective', 'elegant',
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   488
                        'http://python.org', 'http://www.google.com',
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   489
                        'fabulous', 'fantastic', 'friendly', 'forgiving', 'feature',
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   490
                        'sage', 'scarlet', 'scenic', 'seaside', 'showpiece', 'spiffy',
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   491
                        'www.wxPython.org', 'www.osafoundation.org'
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   492
                        ]
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   493
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   494
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   495
        app = wx.PySimpleApp()
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   496
        frm = wx.Frame(None,-1,"Test",style=wx.TAB_TRAVERSAL|wx.DEFAULT_FRAME_STYLE)
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   497
        panel = wx.Panel(frm)
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   498
        sizer = wx.BoxSizer(wx.VERTICAL)
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   499
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   500
        self._ctrl = TextCtrlAutoComplete(panel, **args)
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   501
        but = wx.Button(panel,label="Set other multi-choice")
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   502
        but.Bind(wx.EVT_BUTTON, self.onBtMultiChoice)
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   503
        but2 = wx.Button(panel,label="Set other one-colum choice")
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   504
        but2.Bind(wx.EVT_BUTTON, self.onBtChangeChoice)
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   505
        but3 = wx.Button(panel,label="Set the starting choices")
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   506
        but3.Bind(wx.EVT_BUTTON, self.onBtStartChoices)
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   507
        but4 = wx.Button(panel,label="Enable dynamic choices")
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   508
        but4.Bind(wx.EVT_BUTTON, self.onBtDynamicChoices)
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   509
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   510
        sizer.Add(but, 0, wx.ADJUST_MINSIZE, 0)
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   511
        sizer.Add(but2, 0, wx.ADJUST_MINSIZE, 0)
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   512
        sizer.Add(but3, 0, wx.ADJUST_MINSIZE, 0)
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   513
        sizer.Add(but4, 0, wx.ADJUST_MINSIZE, 0)
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   514
        sizer.Add(self._ctrl, 0, wx.EXPAND|wx.ADJUST_MINSIZE, 0)
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   515
        panel.SetAutoLayout(True)
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   516
        panel.SetSizer(sizer)
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   517
        sizer.Fit(panel)
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   518
        sizer.SetSizeHints(panel)
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   519
        panel.Layout()
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   520
        app.SetTopWindow(frm)
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   521
        frm.Show()
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   522
        but.SetFocus()
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   523
        app.MainLoop()
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   524
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   525
    def onBtChangeChoice(self, event):
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   526
        #change the choices
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   527
        self._ctrl.SetChoices(["123", "cs", "cds", "Bob","Marley","Alpha"])
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   528
        self._ctrl.SetEntryCallback(None)
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   529
        self._ctrl.SetMatchFunction(None)
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   530
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   531
    def onBtMultiChoice(self, event):
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   532
        #change the choices
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   533
        self._ctrl.SetMultipleChoices( [ ("Test","Hello"), ("Other word","World"),
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   534
                                        ("Yes!","it work?") ], colFetch = 1 )
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   535
        self._ctrl.SetEntryCallback(None)
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   536
        self._ctrl.SetMatchFunction(None)
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   537
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   538
    def onBtStartChoices(self, event):
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   539
        #change the choices
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   540
        self._ctrl.SetMultipleChoices( [ ("Zoey","WOW"), ("Alpha", "wxPython"),
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   541
                                    ("Ceda","Is"), ("Beta", "fantastic"),
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   542
                                    ("zoebob", "!!")], colFetch = 1 )
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   543
        self._ctrl.SetEntryCallback(None)
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   544
        self._ctrl.SetMatchFunction(None)
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   545
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   546
    def onBtDynamicChoices(self, event):
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   547
        '''
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   548
        Demonstrate dynamic adjustment of the auto-complete list, based on what's
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   549
        been typed so far:
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   550
        '''
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   551
        self._ctrl.SetChoices(self.dynamic_choices)
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   552
        self._ctrl.SetEntryCallback(self.setDynamicChoices)
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   553
        self._ctrl.SetMatchFunction(self.match)
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   554
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   555
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   556
    def match(self, text, choice):
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   557
        '''
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   558
        Demonstrate "smart" matching feature, by ignoring http:// and www. when doing
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   559
        matches.
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   560
        '''
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   561
        t = text.lower()
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   562
        c = choice.lower()
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   563
        if c.startswith(t): return True
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   564
        if c.startswith(r'http://'): c = c[7:]
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   565
        if c.startswith(t): return True
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   566
        if c.startswith('www.'): c = c[4:]
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   567
        return c.startswith(t)
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   568
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   569
    def setDynamicChoices(self):
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   570
        ctrl = self._ctrl
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   571
        text = ctrl.GetValue().lower()
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   572
        current_choices = ctrl.GetChoices()
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   573
        choices = [choice for choice in self.dynamic_choices if self.match(text, choice)]
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   574
        if choices != current_choices:
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   575
            ctrl.SetChoices(choices)
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   576
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   577
    def selectCallback(self, values):
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   578
        """ Simply function that receive the row values when the
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   579
            user select an item
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   580
        """
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   581
        print "Select Callback called...:",  values
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   582
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   583
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   584
if __name__ == "__main__":
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   585
    test()
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   586