TextCtrlAutoComplete.py
author beremiz
Wed, 12 Aug 2009 15:21:00 +0200
branchcherry-pick
changeset 671 6de6d6958efd
parent 326 386566f263f3
child 426 3f285782ac9b
permissions -rw-r--r--
forgoten branch now closed
295
bc6fc07c3153 Rewrite TestCtrlAutoComplete to fix all bugs
lbessard
parents: 268
diff changeset
     1
#!/usr/bin/env python
bc6fc07c3153 Rewrite TestCtrlAutoComplete to fix all bugs
lbessard
parents: 268
diff changeset
     2
# -*- coding: utf-8 -*-
bc6fc07c3153 Rewrite TestCtrlAutoComplete to fix all bugs
lbessard
parents: 268
diff changeset
     3
bc6fc07c3153 Rewrite TestCtrlAutoComplete to fix all bugs
lbessard
parents: 268
diff changeset
     4
#This file is part of Beremiz, a Integrated Development Environment for
bc6fc07c3153 Rewrite TestCtrlAutoComplete to fix all bugs
lbessard
parents: 268
diff changeset
     5
#programming IEC 61131-3 automates supporting plcopen standard and CanFestival. 
bc6fc07c3153 Rewrite TestCtrlAutoComplete to fix all bugs
lbessard
parents: 268
diff changeset
     6
#
bc6fc07c3153 Rewrite TestCtrlAutoComplete to fix all bugs
lbessard
parents: 268
diff changeset
     7
#Copyright (C) 2007: Edouard TISSERANT and Laurent BESSARD
bc6fc07c3153 Rewrite TestCtrlAutoComplete to fix all bugs
lbessard
parents: 268
diff changeset
     8
#
bc6fc07c3153 Rewrite TestCtrlAutoComplete to fix all bugs
lbessard
parents: 268
diff changeset
     9
#See COPYING file for copyrights details.
bc6fc07c3153 Rewrite TestCtrlAutoComplete to fix all bugs
lbessard
parents: 268
diff changeset
    10
#
bc6fc07c3153 Rewrite TestCtrlAutoComplete to fix all bugs
lbessard
parents: 268
diff changeset
    11
#This library is free software; you can redistribute it and/or
bc6fc07c3153 Rewrite TestCtrlAutoComplete to fix all bugs
lbessard
parents: 268
diff changeset
    12
#modify it under the terms of the GNU General Public
bc6fc07c3153 Rewrite TestCtrlAutoComplete to fix all bugs
lbessard
parents: 268
diff changeset
    13
#License as published by the Free Software Foundation; either
bc6fc07c3153 Rewrite TestCtrlAutoComplete to fix all bugs
lbessard
parents: 268
diff changeset
    14
#version 2.1 of the License, or (at your option) any later version.
bc6fc07c3153 Rewrite TestCtrlAutoComplete to fix all bugs
lbessard
parents: 268
diff changeset
    15
#
bc6fc07c3153 Rewrite TestCtrlAutoComplete to fix all bugs
lbessard
parents: 268
diff changeset
    16
#This library is distributed in the hope that it will be useful,
bc6fc07c3153 Rewrite TestCtrlAutoComplete to fix all bugs
lbessard
parents: 268
diff changeset
    17
#but WITHOUT ANY WARRANTY; without even the implied warranty of
bc6fc07c3153 Rewrite TestCtrlAutoComplete to fix all bugs
lbessard
parents: 268
diff changeset
    18
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
bc6fc07c3153 Rewrite TestCtrlAutoComplete to fix all bugs
lbessard
parents: 268
diff changeset
    19
#General Public License for more details.
bc6fc07c3153 Rewrite TestCtrlAutoComplete to fix all bugs
lbessard
parents: 268
diff changeset
    20
#
bc6fc07c3153 Rewrite TestCtrlAutoComplete to fix all bugs
lbessard
parents: 268
diff changeset
    21
#You should have received a copy of the GNU General Public
bc6fc07c3153 Rewrite TestCtrlAutoComplete to fix all bugs
lbessard
parents: 268
diff changeset
    22
#License along with this library; if not, write to the Free Software
bc6fc07c3153 Rewrite TestCtrlAutoComplete to fix all bugs
lbessard
parents: 268
diff changeset
    23
#Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
bc6fc07c3153 Rewrite TestCtrlAutoComplete to fix all bugs
lbessard
parents: 268
diff changeset
    24
bc6fc07c3153 Rewrite TestCtrlAutoComplete to fix all bugs
lbessard
parents: 268
diff changeset
    25
import wx
268
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
    26
import cPickle
295
bc6fc07c3153 Rewrite TestCtrlAutoComplete to fix all bugs
lbessard
parents: 268
diff changeset
    27
bc6fc07c3153 Rewrite TestCtrlAutoComplete to fix all bugs
lbessard
parents: 268
diff changeset
    28
MAX_ITEM_COUNT = 10
bc6fc07c3153 Rewrite TestCtrlAutoComplete to fix all bugs
lbessard
parents: 268
diff changeset
    29
MAX_ITEM_SHOWN = 6
bc6fc07c3153 Rewrite TestCtrlAutoComplete to fix all bugs
lbessard
parents: 268
diff changeset
    30
ITEM_HEIGHT = 25
bc6fc07c3153 Rewrite TestCtrlAutoComplete to fix all bugs
lbessard
parents: 268
diff changeset
    31
bc6fc07c3153 Rewrite TestCtrlAutoComplete to fix all bugs
lbessard
parents: 268
diff changeset
    32
class TextCtrlAutoComplete(wx.TextCtrl):
bc6fc07c3153 Rewrite TestCtrlAutoComplete to fix all bugs
lbessard
parents: 268
diff changeset
    33
bc6fc07c3153 Rewrite TestCtrlAutoComplete to fix all bugs
lbessard
parents: 268
diff changeset
    34
    def __init__ (self, parent, choices=None, dropDownClick=True,
bc6fc07c3153 Rewrite TestCtrlAutoComplete to fix all bugs
lbessard
parents: 268
diff changeset
    35
                  element_path=None, **therest):
bc6fc07c3153 Rewrite TestCtrlAutoComplete to fix all bugs
lbessard
parents: 268
diff changeset
    36
        """
268
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
    37
        Constructor works just like wx.TextCtrl except you can pass in a
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
    38
        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
    39
        by calling setChoices.
295
bc6fc07c3153 Rewrite TestCtrlAutoComplete to fix all bugs
lbessard
parents: 268
diff changeset
    40
        """
bc6fc07c3153 Rewrite TestCtrlAutoComplete to fix all bugs
lbessard
parents: 268
diff changeset
    41
bc6fc07c3153 Rewrite TestCtrlAutoComplete to fix all bugs
lbessard
parents: 268
diff changeset
    42
        therest['style'] = wx.TE_PROCESS_ENTER | therest.get('style', 0)
bc6fc07c3153 Rewrite TestCtrlAutoComplete to fix all bugs
lbessard
parents: 268
diff changeset
    43
bc6fc07c3153 Rewrite TestCtrlAutoComplete to fix all bugs
lbessard
parents: 268
diff changeset
    44
        wx.TextCtrl.__init__(self, parent, **therest)
268
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
    45
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
    46
        #Some variables
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
    47
        self._dropDownClick = dropDownClick
326
386566f263f3 Bug opening Auto complete frame when not expected fixed
lbessard
parents: 295
diff changeset
    48
        self._lastinsertionpoint = None
295
bc6fc07c3153 Rewrite TestCtrlAutoComplete to fix all bugs
lbessard
parents: 268
diff changeset
    49
        
bc6fc07c3153 Rewrite TestCtrlAutoComplete to fix all bugs
lbessard
parents: 268
diff changeset
    50
        self._screenheight = wx.SystemSettings.GetMetric(wx.SYS_SCREEN_Y)
268
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
    51
        self.element_path = element_path
295
bc6fc07c3153 Rewrite TestCtrlAutoComplete to fix all bugs
lbessard
parents: 268
diff changeset
    52
        
268
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
    53
        #widgets
295
bc6fc07c3153 Rewrite TestCtrlAutoComplete to fix all bugs
lbessard
parents: 268
diff changeset
    54
        self.dropdown = wx.PopupWindow(self)
268
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
    55
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
    56
        #Control the style
295
bc6fc07c3153 Rewrite TestCtrlAutoComplete to fix all bugs
lbessard
parents: 268
diff changeset
    57
        flags = wx.LB_HSCROLL | wx.LB_SINGLE | wx.LB_SORT
bc6fc07c3153 Rewrite TestCtrlAutoComplete to fix all bugs
lbessard
parents: 268
diff changeset
    58
        
268
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
    59
        #Create the list and bind the events
295
bc6fc07c3153 Rewrite TestCtrlAutoComplete to fix all bugs
lbessard
parents: 268
diff changeset
    60
        self.dropdownlistbox = wx.ListBox(self.dropdown, style=flags,
bc6fc07c3153 Rewrite TestCtrlAutoComplete to fix all bugs
lbessard
parents: 268
diff changeset
    61
                                 pos=wx.Point(0, 0))
bc6fc07c3153 Rewrite TestCtrlAutoComplete to fix all bugs
lbessard
parents: 268
diff changeset
    62
        
bc6fc07c3153 Rewrite TestCtrlAutoComplete to fix all bugs
lbessard
parents: 268
diff changeset
    63
        self.SetChoices(choices)
268
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
    64
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
    65
        #gp = self
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
    66
        #while ( gp != None ) :
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
    67
        #    gp.Bind ( wx.EVT_MOVE , self.onControlChanged, gp )
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
    68
        #    gp.Bind ( wx.EVT_SIZE , self.onControlChanged, gp )
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
    69
        #    gp = gp.GetParent()
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
    70
295
bc6fc07c3153 Rewrite TestCtrlAutoComplete to fix all bugs
lbessard
parents: 268
diff changeset
    71
        self.Bind(wx.EVT_KILL_FOCUS, self.onControlChanged, self)
bc6fc07c3153 Rewrite TestCtrlAutoComplete to fix all bugs
lbessard
parents: 268
diff changeset
    72
        self.Bind(wx.EVT_TEXT_ENTER, self.onControlChanged, self)
bc6fc07c3153 Rewrite TestCtrlAutoComplete to fix all bugs
lbessard
parents: 268
diff changeset
    73
        self.Bind(wx.EVT_TEXT, self.onEnteredText, self)
bc6fc07c3153 Rewrite TestCtrlAutoComplete to fix all bugs
lbessard
parents: 268
diff changeset
    74
        self.Bind(wx.EVT_KEY_DOWN, self.onKeyDown, self)
268
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
    75
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
    76
        #If need drop down on left click
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
    77
        if dropDownClick:
295
bc6fc07c3153 Rewrite TestCtrlAutoComplete to fix all bugs
lbessard
parents: 268
diff changeset
    78
            self.Bind(wx.EVT_LEFT_DOWN , self.onClickToggleDown, self)
bc6fc07c3153 Rewrite TestCtrlAutoComplete to fix all bugs
lbessard
parents: 268
diff changeset
    79
            self.Bind(wx.EVT_LEFT_UP , self.onClickToggleUp, self)
bc6fc07c3153 Rewrite TestCtrlAutoComplete to fix all bugs
lbessard
parents: 268
diff changeset
    80
bc6fc07c3153 Rewrite TestCtrlAutoComplete to fix all bugs
lbessard
parents: 268
diff changeset
    81
        self.dropdownlistbox.Bind(wx.EVT_LISTBOX, self.onListItemSelected)
bc6fc07c3153 Rewrite TestCtrlAutoComplete to fix all bugs
lbessard
parents: 268
diff changeset
    82
        self.dropdownlistbox.Bind(wx.EVT_LISTBOX_DCLICK, self.onListItemSelected)
bc6fc07c3153 Rewrite TestCtrlAutoComplete to fix all bugs
lbessard
parents: 268
diff changeset
    83
bc6fc07c3153 Rewrite TestCtrlAutoComplete to fix all bugs
lbessard
parents: 268
diff changeset
    84
    def ChangeValue(self, value):
bc6fc07c3153 Rewrite TestCtrlAutoComplete to fix all bugs
lbessard
parents: 268
diff changeset
    85
        wx.TextCtrl.ChangeValue(self, value)
bc6fc07c3153 Rewrite TestCtrlAutoComplete to fix all bugs
lbessard
parents: 268
diff changeset
    86
        self._refreshListBoxChoices()
268
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
    87
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
    88
    def onEnteredText(self, event):
295
bc6fc07c3153 Rewrite TestCtrlAutoComplete to fix all bugs
lbessard
parents: 268
diff changeset
    89
        wx.CallAfter(self._refreshListBoxChoices)
bc6fc07c3153 Rewrite TestCtrlAutoComplete to fix all bugs
lbessard
parents: 268
diff changeset
    90
        event.Skip()
bc6fc07c3153 Rewrite TestCtrlAutoComplete to fix all bugs
lbessard
parents: 268
diff changeset
    91
bc6fc07c3153 Rewrite TestCtrlAutoComplete to fix all bugs
lbessard
parents: 268
diff changeset
    92
    def onKeyDown(self, event) :
268
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
    93
        """ Do some work when the user press on the keys:
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
    94
            up and down: move the cursor
295
bc6fc07c3153 Rewrite TestCtrlAutoComplete to fix all bugs
lbessard
parents: 268
diff changeset
    95
        """
268
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
    96
        visible = self.dropdown.IsShown()
295
bc6fc07c3153 Rewrite TestCtrlAutoComplete to fix all bugs
lbessard
parents: 268
diff changeset
    97
        keycode = event.GetKeyCode()
bc6fc07c3153 Rewrite TestCtrlAutoComplete to fix all bugs
lbessard
parents: 268
diff changeset
    98
        if keycode in [wx.WXK_DOWN, wx.WXK_UP]:
bc6fc07c3153 Rewrite TestCtrlAutoComplete to fix all bugs
lbessard
parents: 268
diff changeset
    99
            if not visible:
bc6fc07c3153 Rewrite TestCtrlAutoComplete to fix all bugs
lbessard
parents: 268
diff changeset
   100
                self._showDropDown()
bc6fc07c3153 Rewrite TestCtrlAutoComplete to fix all bugs
lbessard
parents: 268
diff changeset
   101
            elif keycode == wx.WXK_DOWN:
bc6fc07c3153 Rewrite TestCtrlAutoComplete to fix all bugs
lbessard
parents: 268
diff changeset
   102
                self._moveSelection(1)
bc6fc07c3153 Rewrite TestCtrlAutoComplete to fix all bugs
lbessard
parents: 268
diff changeset
   103
            else:
bc6fc07c3153 Rewrite TestCtrlAutoComplete to fix all bugs
lbessard
parents: 268
diff changeset
   104
                self._moveSelection(-1)
bc6fc07c3153 Rewrite TestCtrlAutoComplete to fix all bugs
lbessard
parents: 268
diff changeset
   105
        elif keycode in [wx.WXK_LEFT, wx.WXK_RIGHT, wx.WXK_RETURN] and visible:
bc6fc07c3153 Rewrite TestCtrlAutoComplete to fix all bugs
lbessard
parents: 268
diff changeset
   106
            if self.dropdownlistbox.GetSelection() != wx.NOT_FOUND:
268
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   107
                self._setValueFromSelected()
295
bc6fc07c3153 Rewrite TestCtrlAutoComplete to fix all bugs
lbessard
parents: 268
diff changeset
   108
            else:
bc6fc07c3153 Rewrite TestCtrlAutoComplete to fix all bugs
lbessard
parents: 268
diff changeset
   109
                self._showDropDown(False)
bc6fc07c3153 Rewrite TestCtrlAutoComplete to fix all bugs
lbessard
parents: 268
diff changeset
   110
                event.Skip()
bc6fc07c3153 Rewrite TestCtrlAutoComplete to fix all bugs
lbessard
parents: 268
diff changeset
   111
        elif event.GetKeyCode() == wx.WXK_ESCAPE:
bc6fc07c3153 Rewrite TestCtrlAutoComplete to fix all bugs
lbessard
parents: 268
diff changeset
   112
            self._showDropDown(False)
bc6fc07c3153 Rewrite TestCtrlAutoComplete to fix all bugs
lbessard
parents: 268
diff changeset
   113
        else:
268
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   114
            event.Skip()
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   115
295
bc6fc07c3153 Rewrite TestCtrlAutoComplete to fix all bugs
lbessard
parents: 268
diff changeset
   116
    def onListItemSelected(self, event):
268
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   117
        self._setValueFromSelected()
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   118
        event.Skip()
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   119
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   120
    def onClickToggleDown(self, event):
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   121
        self._lastinsertionpoint = self.GetInsertionPoint()
295
bc6fc07c3153 Rewrite TestCtrlAutoComplete to fix all bugs
lbessard
parents: 268
diff changeset
   122
        event.Skip()
bc6fc07c3153 Rewrite TestCtrlAutoComplete to fix all bugs
lbessard
parents: 268
diff changeset
   123
bc6fc07c3153 Rewrite TestCtrlAutoComplete to fix all bugs
lbessard
parents: 268
diff changeset
   124
    def onClickToggleUp(self, event):
bc6fc07c3153 Rewrite TestCtrlAutoComplete to fix all bugs
lbessard
parents: 268
diff changeset
   125
        if self.GetInsertionPoint() == self._lastinsertionpoint:
bc6fc07c3153 Rewrite TestCtrlAutoComplete to fix all bugs
lbessard
parents: 268
diff changeset
   126
            self._showDropDown(not self.dropdown.IsShown())
326
386566f263f3 Bug opening Auto complete frame when not expected fixed
lbessard
parents: 295
diff changeset
   127
        self._lastinsertionpoint = None
295
bc6fc07c3153 Rewrite TestCtrlAutoComplete to fix all bugs
lbessard
parents: 268
diff changeset
   128
        event.Skip()
268
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   129
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   130
    def onControlChanged(self, event):
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   131
        res = self.GetValue()
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   132
        config = wx.ConfigBase.Get()
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   133
        listentries = cPickle.loads(str(config.Read(self.element_path, cPickle.dumps([]))))
295
bc6fc07c3153 Rewrite TestCtrlAutoComplete to fix all bugs
lbessard
parents: 268
diff changeset
   134
        if res and res not in listentries:
bc6fc07c3153 Rewrite TestCtrlAutoComplete to fix all bugs
lbessard
parents: 268
diff changeset
   135
            listentries = (listentries + [res])[-MAX_ITEM_COUNT:]
bc6fc07c3153 Rewrite TestCtrlAutoComplete to fix all bugs
lbessard
parents: 268
diff changeset
   136
            config.Write(self.element_path, cPickle.dumps(listentries))
268
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   137
            config.Flush()
295
bc6fc07c3153 Rewrite TestCtrlAutoComplete to fix all bugs
lbessard
parents: 268
diff changeset
   138
            self.SetChoices(listentries)
bc6fc07c3153 Rewrite TestCtrlAutoComplete to fix all bugs
lbessard
parents: 268
diff changeset
   139
        self._showDropDown(False)
bc6fc07c3153 Rewrite TestCtrlAutoComplete to fix all bugs
lbessard
parents: 268
diff changeset
   140
        event.Skip()
bc6fc07c3153 Rewrite TestCtrlAutoComplete to fix all bugs
lbessard
parents: 268
diff changeset
   141
    
bc6fc07c3153 Rewrite TestCtrlAutoComplete to fix all bugs
lbessard
parents: 268
diff changeset
   142
    def SetChoices(self, choices):
bc6fc07c3153 Rewrite TestCtrlAutoComplete to fix all bugs
lbessard
parents: 268
diff changeset
   143
        self._choices = choices
bc6fc07c3153 Rewrite TestCtrlAutoComplete to fix all bugs
lbessard
parents: 268
diff changeset
   144
        self._refreshListBoxChoices()
bc6fc07c3153 Rewrite TestCtrlAutoComplete to fix all bugs
lbessard
parents: 268
diff changeset
   145
        
bc6fc07c3153 Rewrite TestCtrlAutoComplete to fix all bugs
lbessard
parents: 268
diff changeset
   146
    def GetChoices(self):
bc6fc07c3153 Rewrite TestCtrlAutoComplete to fix all bugs
lbessard
parents: 268
diff changeset
   147
        return self._choices
bc6fc07c3153 Rewrite TestCtrlAutoComplete to fix all bugs
lbessard
parents: 268
diff changeset
   148
    
bc6fc07c3153 Rewrite TestCtrlAutoComplete to fix all bugs
lbessard
parents: 268
diff changeset
   149
#-------------------------------------------------------------------------------
bc6fc07c3153 Rewrite TestCtrlAutoComplete to fix all bugs
lbessard
parents: 268
diff changeset
   150
#                           Internal methods
bc6fc07c3153 Rewrite TestCtrlAutoComplete to fix all bugs
lbessard
parents: 268
diff changeset
   151
#-------------------------------------------------------------------------------
bc6fc07c3153 Rewrite TestCtrlAutoComplete to fix all bugs
lbessard
parents: 268
diff changeset
   152
bc6fc07c3153 Rewrite TestCtrlAutoComplete to fix all bugs
lbessard
parents: 268
diff changeset
   153
    def _refreshListBoxChoices(self):
bc6fc07c3153 Rewrite TestCtrlAutoComplete to fix all bugs
lbessard
parents: 268
diff changeset
   154
        text = self.GetValue()
bc6fc07c3153 Rewrite TestCtrlAutoComplete to fix all bugs
lbessard
parents: 268
diff changeset
   155
bc6fc07c3153 Rewrite TestCtrlAutoComplete to fix all bugs
lbessard
parents: 268
diff changeset
   156
        self.dropdownlistbox.Clear()
bc6fc07c3153 Rewrite TestCtrlAutoComplete to fix all bugs
lbessard
parents: 268
diff changeset
   157
        for choice in self._choices:
bc6fc07c3153 Rewrite TestCtrlAutoComplete to fix all bugs
lbessard
parents: 268
diff changeset
   158
            if choice.startswith(text):
bc6fc07c3153 Rewrite TestCtrlAutoComplete to fix all bugs
lbessard
parents: 268
diff changeset
   159
                self.dropdownlistbox.Append(choice)
bc6fc07c3153 Rewrite TestCtrlAutoComplete to fix all bugs
lbessard
parents: 268
diff changeset
   160
        
bc6fc07c3153 Rewrite TestCtrlAutoComplete to fix all bugs
lbessard
parents: 268
diff changeset
   161
        itemcount = min(len(self.dropdownlistbox.GetStrings()), MAX_ITEM_SHOWN)
bc6fc07c3153 Rewrite TestCtrlAutoComplete to fix all bugs
lbessard
parents: 268
diff changeset
   162
        self.popupsize = wx.Size(self.GetSize()[0], ITEM_HEIGHT * itemcount + 4)
bc6fc07c3153 Rewrite TestCtrlAutoComplete to fix all bugs
lbessard
parents: 268
diff changeset
   163
        self.dropdownlistbox.SetSize(self.popupsize)
bc6fc07c3153 Rewrite TestCtrlAutoComplete to fix all bugs
lbessard
parents: 268
diff changeset
   164
        self.dropdown.SetClientSize(self.popupsize)
bc6fc07c3153 Rewrite TestCtrlAutoComplete to fix all bugs
lbessard
parents: 268
diff changeset
   165
    
bc6fc07c3153 Rewrite TestCtrlAutoComplete to fix all bugs
lbessard
parents: 268
diff changeset
   166
    def _moveSelection(self, direction):
bc6fc07c3153 Rewrite TestCtrlAutoComplete to fix all bugs
lbessard
parents: 268
diff changeset
   167
        selected = self.dropdownlistbox.GetSelection()
bc6fc07c3153 Rewrite TestCtrlAutoComplete to fix all bugs
lbessard
parents: 268
diff changeset
   168
        if selected == wx.NOT_FOUND:
bc6fc07c3153 Rewrite TestCtrlAutoComplete to fix all bugs
lbessard
parents: 268
diff changeset
   169
            if direction >= 0:
bc6fc07c3153 Rewrite TestCtrlAutoComplete to fix all bugs
lbessard
parents: 268
diff changeset
   170
                selected = 0
bc6fc07c3153 Rewrite TestCtrlAutoComplete to fix all bugs
lbessard
parents: 268
diff changeset
   171
            else:
bc6fc07c3153 Rewrite TestCtrlAutoComplete to fix all bugs
lbessard
parents: 268
diff changeset
   172
                selected = self.dropdownlistbox.GetCount() - 1
268
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   173
        else:
295
bc6fc07c3153 Rewrite TestCtrlAutoComplete to fix all bugs
lbessard
parents: 268
diff changeset
   174
            selected = (selected + direction) % (self.dropdownlistbox.GetCount() + 1)
bc6fc07c3153 Rewrite TestCtrlAutoComplete to fix all bugs
lbessard
parents: 268
diff changeset
   175
        if selected == self.dropdownlistbox.GetCount():
bc6fc07c3153 Rewrite TestCtrlAutoComplete to fix all bugs
lbessard
parents: 268
diff changeset
   176
            selected = wx.NOT_FOUND
bc6fc07c3153 Rewrite TestCtrlAutoComplete to fix all bugs
lbessard
parents: 268
diff changeset
   177
        self.dropdownlistbox.SetSelection(selected)
bc6fc07c3153 Rewrite TestCtrlAutoComplete to fix all bugs
lbessard
parents: 268
diff changeset
   178
    
bc6fc07c3153 Rewrite TestCtrlAutoComplete to fix all bugs
lbessard
parents: 268
diff changeset
   179
    def _setValueFromSelected(self):
bc6fc07c3153 Rewrite TestCtrlAutoComplete to fix all bugs
lbessard
parents: 268
diff changeset
   180
         """
268
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   181
         Sets the wx.TextCtrl value from the selected wx.ListCtrl item.
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   182
         Will do nothing if no item is selected in the wx.ListCtrl.
295
bc6fc07c3153 Rewrite TestCtrlAutoComplete to fix all bugs
lbessard
parents: 268
diff changeset
   183
         """
bc6fc07c3153 Rewrite TestCtrlAutoComplete to fix all bugs
lbessard
parents: 268
diff changeset
   184
         selected = self.dropdownlistbox.GetStringSelection()
bc6fc07c3153 Rewrite TestCtrlAutoComplete to fix all bugs
lbessard
parents: 268
diff changeset
   185
         if selected:
bc6fc07c3153 Rewrite TestCtrlAutoComplete to fix all bugs
lbessard
parents: 268
diff changeset
   186
            self.SetValue(selected)
bc6fc07c3153 Rewrite TestCtrlAutoComplete to fix all bugs
lbessard
parents: 268
diff changeset
   187
            self._showDropDown(False)
bc6fc07c3153 Rewrite TestCtrlAutoComplete to fix all bugs
lbessard
parents: 268
diff changeset
   188
bc6fc07c3153 Rewrite TestCtrlAutoComplete to fix all bugs
lbessard
parents: 268
diff changeset
   189
bc6fc07c3153 Rewrite TestCtrlAutoComplete to fix all bugs
lbessard
parents: 268
diff changeset
   190
    def _showDropDown(self, show=True) :
bc6fc07c3153 Rewrite TestCtrlAutoComplete to fix all bugs
lbessard
parents: 268
diff changeset
   191
        """
268
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   192
        Either display the drop down list (show = True) or hide it (show = False).
295
bc6fc07c3153 Rewrite TestCtrlAutoComplete to fix all bugs
lbessard
parents: 268
diff changeset
   193
        """
268
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   194
        if show :
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   195
            size = self.dropdown.GetSize()
295
bc6fc07c3153 Rewrite TestCtrlAutoComplete to fix all bugs
lbessard
parents: 268
diff changeset
   196
            width, height = self.GetSizeTuple()
bc6fc07c3153 Rewrite TestCtrlAutoComplete to fix all bugs
lbessard
parents: 268
diff changeset
   197
            x, y = self.ClientToScreenXY(0, height)
268
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   198
            if size.GetWidth() != width :
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   199
                size.SetWidth(width)
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   200
                self.dropdown.SetSize(size)
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   201
                self.dropdownlistbox.SetSize(self.dropdown.GetClientSize())
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   202
            if (y + size.GetHeight()) < self._screenheight :
295
bc6fc07c3153 Rewrite TestCtrlAutoComplete to fix all bugs
lbessard
parents: 268
diff changeset
   203
                self.dropdown.SetPosition(wx.Point(x, y))
bc6fc07c3153 Rewrite TestCtrlAutoComplete to fix all bugs
lbessard
parents: 268
diff changeset
   204
            else:
bc6fc07c3153 Rewrite TestCtrlAutoComplete to fix all bugs
lbessard
parents: 268
diff changeset
   205
                self.dropdown.SetPosition(wx.Point(x, y - height - size.GetHeight()))
bc6fc07c3153 Rewrite TestCtrlAutoComplete to fix all bugs
lbessard
parents: 268
diff changeset
   206
        self.dropdown.Show(show) 
bc6fc07c3153 Rewrite TestCtrlAutoComplete to fix all bugs
lbessard
parents: 268
diff changeset
   207