TextCtrlAutoComplete.py
author laurent
Thu, 22 Oct 2009 17:20:24 +0200
changeset 426 3f285782ac9b
parent 326 386566f263f3
child 428 ea09f33ce717
permissions -rw-r--r--
Bugs with TextCtrlAutoComplete on Windows fixed
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
426
3f285782ac9b Bugs with TextCtrlAutoComplete on Windows fixed
laurent
parents: 326
diff changeset
    30
if wx.Platform == '__WXMSW__':
3f285782ac9b Bugs with TextCtrlAutoComplete on Windows fixed
laurent
parents: 326
diff changeset
    31
    ITEM_INTERVAL_HEIGHT = 3
3f285782ac9b Bugs with TextCtrlAutoComplete on Windows fixed
laurent
parents: 326
diff changeset
    32
else:
3f285782ac9b Bugs with TextCtrlAutoComplete on Windows fixed
laurent
parents: 326
diff changeset
    33
    ITEM_INTERVAL_HEIGHT = 6
3f285782ac9b Bugs with TextCtrlAutoComplete on Windows fixed
laurent
parents: 326
diff changeset
    34
3f285782ac9b Bugs with TextCtrlAutoComplete on Windows fixed
laurent
parents: 326
diff changeset
    35
if wx.Platform == '__WXMSW__':
3f285782ac9b Bugs with TextCtrlAutoComplete on Windows fixed
laurent
parents: 326
diff changeset
    36
    popupclass = wx.PopupTransientWindow
3f285782ac9b Bugs with TextCtrlAutoComplete on Windows fixed
laurent
parents: 326
diff changeset
    37
else:
3f285782ac9b Bugs with TextCtrlAutoComplete on Windows fixed
laurent
parents: 326
diff changeset
    38
    popupclass = wx.PopupWindow
3f285782ac9b Bugs with TextCtrlAutoComplete on Windows fixed
laurent
parents: 326
diff changeset
    39
3f285782ac9b Bugs with TextCtrlAutoComplete on Windows fixed
laurent
parents: 326
diff changeset
    40
class PopupWithListbox(popupclass):
3f285782ac9b Bugs with TextCtrlAutoComplete on Windows fixed
laurent
parents: 326
diff changeset
    41
    
3f285782ac9b Bugs with TextCtrlAutoComplete on Windows fixed
laurent
parents: 326
diff changeset
    42
    def __init__(self, parent, choices=[]):
3f285782ac9b Bugs with TextCtrlAutoComplete on Windows fixed
laurent
parents: 326
diff changeset
    43
        popupclass.__init__(self, parent, wx.SIMPLE_BORDER)
3f285782ac9b Bugs with TextCtrlAutoComplete on Windows fixed
laurent
parents: 326
diff changeset
    44
        
3f285782ac9b Bugs with TextCtrlAutoComplete on Windows fixed
laurent
parents: 326
diff changeset
    45
        self.ListBox = wx.ListBox(self, -1, style=wx.LB_HSCROLL|wx.LB_SINGLE|wx.LB_SORT)
3f285782ac9b Bugs with TextCtrlAutoComplete on Windows fixed
laurent
parents: 326
diff changeset
    46
        if not wx.Platform == '__WXMSW__':
3f285782ac9b Bugs with TextCtrlAutoComplete on Windows fixed
laurent
parents: 326
diff changeset
    47
            self.ListBox.Bind(wx.EVT_LISTBOX, self.OnListBoxClick)
3f285782ac9b Bugs with TextCtrlAutoComplete on Windows fixed
laurent
parents: 326
diff changeset
    48
            self.ListBox.Bind(wx.EVT_LISTBOX_DCLICK, self.OnListBoxClick)
3f285782ac9b Bugs with TextCtrlAutoComplete on Windows fixed
laurent
parents: 326
diff changeset
    49
            
3f285782ac9b Bugs with TextCtrlAutoComplete on Windows fixed
laurent
parents: 326
diff changeset
    50
        self.SetChoices(choices)
3f285782ac9b Bugs with TextCtrlAutoComplete on Windows fixed
laurent
parents: 326
diff changeset
    51
        
3f285782ac9b Bugs with TextCtrlAutoComplete on Windows fixed
laurent
parents: 326
diff changeset
    52
        self.Bind(wx.EVT_KEY_DOWN, self.OnKeyDown)
3f285782ac9b Bugs with TextCtrlAutoComplete on Windows fixed
laurent
parents: 326
diff changeset
    53
    
3f285782ac9b Bugs with TextCtrlAutoComplete on Windows fixed
laurent
parents: 326
diff changeset
    54
    def SetChoices(self, choices):
3f285782ac9b Bugs with TextCtrlAutoComplete on Windows fixed
laurent
parents: 326
diff changeset
    55
        max_text_width = 0
3f285782ac9b Bugs with TextCtrlAutoComplete on Windows fixed
laurent
parents: 326
diff changeset
    56
        max_text_height = 0
3f285782ac9b Bugs with TextCtrlAutoComplete on Windows fixed
laurent
parents: 326
diff changeset
    57
        
3f285782ac9b Bugs with TextCtrlAutoComplete on Windows fixed
laurent
parents: 326
diff changeset
    58
        self.ListBox.Clear()
3f285782ac9b Bugs with TextCtrlAutoComplete on Windows fixed
laurent
parents: 326
diff changeset
    59
        for choice in choices:
3f285782ac9b Bugs with TextCtrlAutoComplete on Windows fixed
laurent
parents: 326
diff changeset
    60
            self.ListBox.Append(choice)
3f285782ac9b Bugs with TextCtrlAutoComplete on Windows fixed
laurent
parents: 326
diff changeset
    61
            w, h = self.ListBox.GetTextExtent(choice)
3f285782ac9b Bugs with TextCtrlAutoComplete on Windows fixed
laurent
parents: 326
diff changeset
    62
            max_text_width = max(max_text_width, w)
3f285782ac9b Bugs with TextCtrlAutoComplete on Windows fixed
laurent
parents: 326
diff changeset
    63
            max_text_height = max(max_text_height, h)
3f285782ac9b Bugs with TextCtrlAutoComplete on Windows fixed
laurent
parents: 326
diff changeset
    64
        
3f285782ac9b Bugs with TextCtrlAutoComplete on Windows fixed
laurent
parents: 326
diff changeset
    65
        itemcount = min(len(choices), MAX_ITEM_SHOWN)
3f285782ac9b Bugs with TextCtrlAutoComplete on Windows fixed
laurent
parents: 326
diff changeset
    66
        width = self.Parent.GetSize()[0]
3f285782ac9b Bugs with TextCtrlAutoComplete on Windows fixed
laurent
parents: 326
diff changeset
    67
        height = max_text_height * itemcount + ITEM_INTERVAL_HEIGHT * (itemcount + 1)
3f285782ac9b Bugs with TextCtrlAutoComplete on Windows fixed
laurent
parents: 326
diff changeset
    68
        if max_text_width + 10 > width:
3f285782ac9b Bugs with TextCtrlAutoComplete on Windows fixed
laurent
parents: 326
diff changeset
    69
            height += 15
3f285782ac9b Bugs with TextCtrlAutoComplete on Windows fixed
laurent
parents: 326
diff changeset
    70
        size = wx.Size(width, height)
3f285782ac9b Bugs with TextCtrlAutoComplete on Windows fixed
laurent
parents: 326
diff changeset
    71
        self.ListBox.SetSize(size)
3f285782ac9b Bugs with TextCtrlAutoComplete on Windows fixed
laurent
parents: 326
diff changeset
    72
        self.SetClientSize(size)
3f285782ac9b Bugs with TextCtrlAutoComplete on Windows fixed
laurent
parents: 326
diff changeset
    73
    
3f285782ac9b Bugs with TextCtrlAutoComplete on Windows fixed
laurent
parents: 326
diff changeset
    74
    def MoveSelection(self, direction):
3f285782ac9b Bugs with TextCtrlAutoComplete on Windows fixed
laurent
parents: 326
diff changeset
    75
        selected = self.ListBox.GetSelection()
3f285782ac9b Bugs with TextCtrlAutoComplete on Windows fixed
laurent
parents: 326
diff changeset
    76
        if selected == wx.NOT_FOUND:
3f285782ac9b Bugs with TextCtrlAutoComplete on Windows fixed
laurent
parents: 326
diff changeset
    77
            if direction >= 0:
3f285782ac9b Bugs with TextCtrlAutoComplete on Windows fixed
laurent
parents: 326
diff changeset
    78
                selected = 0
3f285782ac9b Bugs with TextCtrlAutoComplete on Windows fixed
laurent
parents: 326
diff changeset
    79
            else:
3f285782ac9b Bugs with TextCtrlAutoComplete on Windows fixed
laurent
parents: 326
diff changeset
    80
                selected = self.ListBox.GetCount() - 1
3f285782ac9b Bugs with TextCtrlAutoComplete on Windows fixed
laurent
parents: 326
diff changeset
    81
        else:
3f285782ac9b Bugs with TextCtrlAutoComplete on Windows fixed
laurent
parents: 326
diff changeset
    82
            selected = (selected + direction) % (self.ListBox.GetCount() + 1)
3f285782ac9b Bugs with TextCtrlAutoComplete on Windows fixed
laurent
parents: 326
diff changeset
    83
        if selected == self.ListBox.GetCount():
3f285782ac9b Bugs with TextCtrlAutoComplete on Windows fixed
laurent
parents: 326
diff changeset
    84
            selected = wx.NOT_FOUND
3f285782ac9b Bugs with TextCtrlAutoComplete on Windows fixed
laurent
parents: 326
diff changeset
    85
        self.ListBox.SetSelection(selected)
3f285782ac9b Bugs with TextCtrlAutoComplete on Windows fixed
laurent
parents: 326
diff changeset
    86
    
3f285782ac9b Bugs with TextCtrlAutoComplete on Windows fixed
laurent
parents: 326
diff changeset
    87
    def GetSelection(self):
3f285782ac9b Bugs with TextCtrlAutoComplete on Windows fixed
laurent
parents: 326
diff changeset
    88
        return self.ListBox.GetStringSelection()
3f285782ac9b Bugs with TextCtrlAutoComplete on Windows fixed
laurent
parents: 326
diff changeset
    89
    
3f285782ac9b Bugs with TextCtrlAutoComplete on Windows fixed
laurent
parents: 326
diff changeset
    90
    def ProcessLeftDown(self, event):
3f285782ac9b Bugs with TextCtrlAutoComplete on Windows fixed
laurent
parents: 326
diff changeset
    91
        selected = self.ListBox.HitTest(wx.Point(event.m_x, event.m_y))
3f285782ac9b Bugs with TextCtrlAutoComplete on Windows fixed
laurent
parents: 326
diff changeset
    92
        if selected != wx.NOT_FOUND:
3f285782ac9b Bugs with TextCtrlAutoComplete on Windows fixed
laurent
parents: 326
diff changeset
    93
            wx.CallAfter(self.Parent.SetValueFromSelected, self.ListBox.GetString(selected))
3f285782ac9b Bugs with TextCtrlAutoComplete on Windows fixed
laurent
parents: 326
diff changeset
    94
        return False
3f285782ac9b Bugs with TextCtrlAutoComplete on Windows fixed
laurent
parents: 326
diff changeset
    95
    
3f285782ac9b Bugs with TextCtrlAutoComplete on Windows fixed
laurent
parents: 326
diff changeset
    96
    def OnListBoxClick(self, event):
3f285782ac9b Bugs with TextCtrlAutoComplete on Windows fixed
laurent
parents: 326
diff changeset
    97
        selected = event.GetSelection()
3f285782ac9b Bugs with TextCtrlAutoComplete on Windows fixed
laurent
parents: 326
diff changeset
    98
        if selected != wx.NOT_FOUND:
3f285782ac9b Bugs with TextCtrlAutoComplete on Windows fixed
laurent
parents: 326
diff changeset
    99
            wx.CallAfter(self.Parent.SetValueFromSelected, self.ListBox.GetString(selected))
3f285782ac9b Bugs with TextCtrlAutoComplete on Windows fixed
laurent
parents: 326
diff changeset
   100
        event.Skip()
3f285782ac9b Bugs with TextCtrlAutoComplete on Windows fixed
laurent
parents: 326
diff changeset
   101
    
3f285782ac9b Bugs with TextCtrlAutoComplete on Windows fixed
laurent
parents: 326
diff changeset
   102
    def OnKeyDown(self, event):
3f285782ac9b Bugs with TextCtrlAutoComplete on Windows fixed
laurent
parents: 326
diff changeset
   103
        self.Parent.ProcessEvent(event)
3f285782ac9b Bugs with TextCtrlAutoComplete on Windows fixed
laurent
parents: 326
diff changeset
   104
3f285782ac9b Bugs with TextCtrlAutoComplete on Windows fixed
laurent
parents: 326
diff changeset
   105
    def OnDismiss(self):
3f285782ac9b Bugs with TextCtrlAutoComplete on Windows fixed
laurent
parents: 326
diff changeset
   106
        self.Parent.listbox = None
3f285782ac9b Bugs with TextCtrlAutoComplete on Windows fixed
laurent
parents: 326
diff changeset
   107
        wx.CallAfter(self.Parent.DismissListBox)
3f285782ac9b Bugs with TextCtrlAutoComplete on Windows fixed
laurent
parents: 326
diff changeset
   108
    
295
bc6fc07c3153 Rewrite TestCtrlAutoComplete to fix all bugs
lbessard
parents: 268
diff changeset
   109
class TextCtrlAutoComplete(wx.TextCtrl):
bc6fc07c3153 Rewrite TestCtrlAutoComplete to fix all bugs
lbessard
parents: 268
diff changeset
   110
426
3f285782ac9b Bugs with TextCtrlAutoComplete on Windows fixed
laurent
parents: 326
diff changeset
   111
    def __init__ (self, parent, appframe, choices=None, dropDownClick=True,
295
bc6fc07c3153 Rewrite TestCtrlAutoComplete to fix all bugs
lbessard
parents: 268
diff changeset
   112
                  element_path=None, **therest):
bc6fc07c3153 Rewrite TestCtrlAutoComplete to fix all bugs
lbessard
parents: 268
diff changeset
   113
        """
268
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   114
        Constructor works just like wx.TextCtrl except you can pass in a
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   115
        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
   116
        by calling setChoices.
295
bc6fc07c3153 Rewrite TestCtrlAutoComplete to fix all bugs
lbessard
parents: 268
diff changeset
   117
        """
bc6fc07c3153 Rewrite TestCtrlAutoComplete to fix all bugs
lbessard
parents: 268
diff changeset
   118
bc6fc07c3153 Rewrite TestCtrlAutoComplete to fix all bugs
lbessard
parents: 268
diff changeset
   119
        therest['style'] = wx.TE_PROCESS_ENTER | therest.get('style', 0)
bc6fc07c3153 Rewrite TestCtrlAutoComplete to fix all bugs
lbessard
parents: 268
diff changeset
   120
bc6fc07c3153 Rewrite TestCtrlAutoComplete to fix all bugs
lbessard
parents: 268
diff changeset
   121
        wx.TextCtrl.__init__(self, parent, **therest)
426
3f285782ac9b Bugs with TextCtrlAutoComplete on Windows fixed
laurent
parents: 326
diff changeset
   122
        self.AppFrame = appframe
3f285782ac9b Bugs with TextCtrlAutoComplete on Windows fixed
laurent
parents: 326
diff changeset
   123
        
268
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   124
        #Some variables
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   125
        self._dropDownClick = dropDownClick
326
386566f263f3 Bug opening Auto complete frame when not expected fixed
lbessard
parents: 295
diff changeset
   126
        self._lastinsertionpoint = None
295
bc6fc07c3153 Rewrite TestCtrlAutoComplete to fix all bugs
lbessard
parents: 268
diff changeset
   127
        
bc6fc07c3153 Rewrite TestCtrlAutoComplete to fix all bugs
lbessard
parents: 268
diff changeset
   128
        self._screenheight = wx.SystemSettings.GetMetric(wx.SYS_SCREEN_Y)
268
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   129
        self.element_path = element_path
295
bc6fc07c3153 Rewrite TestCtrlAutoComplete to fix all bugs
lbessard
parents: 268
diff changeset
   130
        
426
3f285782ac9b Bugs with TextCtrlAutoComplete on Windows fixed
laurent
parents: 326
diff changeset
   131
        self.listbox = None
295
bc6fc07c3153 Rewrite TestCtrlAutoComplete to fix all bugs
lbessard
parents: 268
diff changeset
   132
        
bc6fc07c3153 Rewrite TestCtrlAutoComplete to fix all bugs
lbessard
parents: 268
diff changeset
   133
        self.SetChoices(choices)
268
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   134
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   135
        #gp = self
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   136
        #while ( gp != None ) :
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   137
        #    gp.Bind ( wx.EVT_MOVE , self.onControlChanged, gp )
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   138
        #    gp.Bind ( wx.EVT_SIZE , self.onControlChanged, gp )
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   139
        #    gp = gp.GetParent()
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   140
426
3f285782ac9b Bugs with TextCtrlAutoComplete on Windows fixed
laurent
parents: 326
diff changeset
   141
        self.Bind(wx.EVT_KILL_FOCUS, self.OnControlChanged)
3f285782ac9b Bugs with TextCtrlAutoComplete on Windows fixed
laurent
parents: 326
diff changeset
   142
        self.Bind(wx.EVT_TEXT_ENTER, self.OnControlChanged)
3f285782ac9b Bugs with TextCtrlAutoComplete on Windows fixed
laurent
parents: 326
diff changeset
   143
        self.Bind(wx.EVT_TEXT, self.OnEnteredText)
3f285782ac9b Bugs with TextCtrlAutoComplete on Windows fixed
laurent
parents: 326
diff changeset
   144
        self.Bind(wx.EVT_KEY_DOWN, self.OnKeyDown)
268
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
        #If need drop down on left click
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   147
        if dropDownClick:
426
3f285782ac9b Bugs with TextCtrlAutoComplete on Windows fixed
laurent
parents: 326
diff changeset
   148
            self.Bind(wx.EVT_LEFT_DOWN, self.OnClickToggleDown)
3f285782ac9b Bugs with TextCtrlAutoComplete on Windows fixed
laurent
parents: 326
diff changeset
   149
            self.Bind(wx.EVT_LEFT_UP, self.OnClickToggleUp)
3f285782ac9b Bugs with TextCtrlAutoComplete on Windows fixed
laurent
parents: 326
diff changeset
   150
3f285782ac9b Bugs with TextCtrlAutoComplete on Windows fixed
laurent
parents: 326
diff changeset
   151
    def __del__(self):
3f285782ac9b Bugs with TextCtrlAutoComplete on Windows fixed
laurent
parents: 326
diff changeset
   152
        self.AppFrame = None
295
bc6fc07c3153 Rewrite TestCtrlAutoComplete to fix all bugs
lbessard
parents: 268
diff changeset
   153
bc6fc07c3153 Rewrite TestCtrlAutoComplete to fix all bugs
lbessard
parents: 268
diff changeset
   154
    def ChangeValue(self, value):
bc6fc07c3153 Rewrite TestCtrlAutoComplete to fix all bugs
lbessard
parents: 268
diff changeset
   155
        wx.TextCtrl.ChangeValue(self, value)
426
3f285782ac9b Bugs with TextCtrlAutoComplete on Windows fixed
laurent
parents: 326
diff changeset
   156
        self.RefreshListBoxChoices()
3f285782ac9b Bugs with TextCtrlAutoComplete on Windows fixed
laurent
parents: 326
diff changeset
   157
3f285782ac9b Bugs with TextCtrlAutoComplete on Windows fixed
laurent
parents: 326
diff changeset
   158
    def OnEnteredText(self, event):
3f285782ac9b Bugs with TextCtrlAutoComplete on Windows fixed
laurent
parents: 326
diff changeset
   159
        wx.CallAfter(self.RefreshListBoxChoices)
3f285782ac9b Bugs with TextCtrlAutoComplete on Windows fixed
laurent
parents: 326
diff changeset
   160
        event.Skip()
3f285782ac9b Bugs with TextCtrlAutoComplete on Windows fixed
laurent
parents: 326
diff changeset
   161
3f285782ac9b Bugs with TextCtrlAutoComplete on Windows fixed
laurent
parents: 326
diff changeset
   162
    def OnKeyDown(self, event):
268
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   163
        """ Do some work when the user press on the keys:
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   164
            up and down: move the cursor
295
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
        keycode = event.GetKeyCode()
bc6fc07c3153 Rewrite TestCtrlAutoComplete to fix all bugs
lbessard
parents: 268
diff changeset
   167
        if keycode in [wx.WXK_DOWN, wx.WXK_UP]:
426
3f285782ac9b Bugs with TextCtrlAutoComplete on Windows fixed
laurent
parents: 326
diff changeset
   168
            self.PopupListBox()
3f285782ac9b Bugs with TextCtrlAutoComplete on Windows fixed
laurent
parents: 326
diff changeset
   169
            if keycode == wx.WXK_DOWN:
3f285782ac9b Bugs with TextCtrlAutoComplete on Windows fixed
laurent
parents: 326
diff changeset
   170
                self.listbox.MoveSelection(1)
3f285782ac9b Bugs with TextCtrlAutoComplete on Windows fixed
laurent
parents: 326
diff changeset
   171
            else:
3f285782ac9b Bugs with TextCtrlAutoComplete on Windows fixed
laurent
parents: 326
diff changeset
   172
                self.listbox.MoveSelection(-1)
3f285782ac9b Bugs with TextCtrlAutoComplete on Windows fixed
laurent
parents: 326
diff changeset
   173
        elif keycode in [wx.WXK_LEFT, wx.WXK_RIGHT, wx.WXK_RETURN] and self.listbox is not None:
3f285782ac9b Bugs with TextCtrlAutoComplete on Windows fixed
laurent
parents: 326
diff changeset
   174
            self.SetValueFromSelected(self.listbox.GetSelection())
295
bc6fc07c3153 Rewrite TestCtrlAutoComplete to fix all bugs
lbessard
parents: 268
diff changeset
   175
        elif event.GetKeyCode() == wx.WXK_ESCAPE:
426
3f285782ac9b Bugs with TextCtrlAutoComplete on Windows fixed
laurent
parents: 326
diff changeset
   176
            self.DismissListBox()
295
bc6fc07c3153 Rewrite TestCtrlAutoComplete to fix all bugs
lbessard
parents: 268
diff changeset
   177
        else:
268
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   178
            event.Skip()
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   179
426
3f285782ac9b Bugs with TextCtrlAutoComplete on Windows fixed
laurent
parents: 326
diff changeset
   180
    def OnClickToggleDown(self, event):
268
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   181
        self._lastinsertionpoint = self.GetInsertionPoint()
295
bc6fc07c3153 Rewrite TestCtrlAutoComplete to fix all bugs
lbessard
parents: 268
diff changeset
   182
        event.Skip()
bc6fc07c3153 Rewrite TestCtrlAutoComplete to fix all bugs
lbessard
parents: 268
diff changeset
   183
426
3f285782ac9b Bugs with TextCtrlAutoComplete on Windows fixed
laurent
parents: 326
diff changeset
   184
    def OnClickToggleUp(self, event):
295
bc6fc07c3153 Rewrite TestCtrlAutoComplete to fix all bugs
lbessard
parents: 268
diff changeset
   185
        if self.GetInsertionPoint() == self._lastinsertionpoint:
426
3f285782ac9b Bugs with TextCtrlAutoComplete on Windows fixed
laurent
parents: 326
diff changeset
   186
            wx.CallAfter(self.PopupListBox)
326
386566f263f3 Bug opening Auto complete frame when not expected fixed
lbessard
parents: 295
diff changeset
   187
        self._lastinsertionpoint = None
295
bc6fc07c3153 Rewrite TestCtrlAutoComplete to fix all bugs
lbessard
parents: 268
diff changeset
   188
        event.Skip()
268
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   189
426
3f285782ac9b Bugs with TextCtrlAutoComplete on Windows fixed
laurent
parents: 326
diff changeset
   190
    def OnControlChanged(self, event):
268
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   191
        res = self.GetValue()
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   192
        config = wx.ConfigBase.Get()
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   193
        listentries = cPickle.loads(str(config.Read(self.element_path, cPickle.dumps([]))))
295
bc6fc07c3153 Rewrite TestCtrlAutoComplete to fix all bugs
lbessard
parents: 268
diff changeset
   194
        if res and res not in listentries:
bc6fc07c3153 Rewrite TestCtrlAutoComplete to fix all bugs
lbessard
parents: 268
diff changeset
   195
            listentries = (listentries + [res])[-MAX_ITEM_COUNT:]
bc6fc07c3153 Rewrite TestCtrlAutoComplete to fix all bugs
lbessard
parents: 268
diff changeset
   196
            config.Write(self.element_path, cPickle.dumps(listentries))
268
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   197
            config.Flush()
295
bc6fc07c3153 Rewrite TestCtrlAutoComplete to fix all bugs
lbessard
parents: 268
diff changeset
   198
            self.SetChoices(listentries)
426
3f285782ac9b Bugs with TextCtrlAutoComplete on Windows fixed
laurent
parents: 326
diff changeset
   199
        self.DismissListBox()
295
bc6fc07c3153 Rewrite TestCtrlAutoComplete to fix all bugs
lbessard
parents: 268
diff changeset
   200
        event.Skip()
bc6fc07c3153 Rewrite TestCtrlAutoComplete to fix all bugs
lbessard
parents: 268
diff changeset
   201
    
bc6fc07c3153 Rewrite TestCtrlAutoComplete to fix all bugs
lbessard
parents: 268
diff changeset
   202
    def SetChoices(self, choices):
bc6fc07c3153 Rewrite TestCtrlAutoComplete to fix all bugs
lbessard
parents: 268
diff changeset
   203
        self._choices = choices
426
3f285782ac9b Bugs with TextCtrlAutoComplete on Windows fixed
laurent
parents: 326
diff changeset
   204
        self.RefreshListBoxChoices()
295
bc6fc07c3153 Rewrite TestCtrlAutoComplete to fix all bugs
lbessard
parents: 268
diff changeset
   205
        
bc6fc07c3153 Rewrite TestCtrlAutoComplete to fix all bugs
lbessard
parents: 268
diff changeset
   206
    def GetChoices(self):
bc6fc07c3153 Rewrite TestCtrlAutoComplete to fix all bugs
lbessard
parents: 268
diff changeset
   207
        return self._choices
bc6fc07c3153 Rewrite TestCtrlAutoComplete to fix all bugs
lbessard
parents: 268
diff changeset
   208
    
426
3f285782ac9b Bugs with TextCtrlAutoComplete on Windows fixed
laurent
parents: 326
diff changeset
   209
    def SetValueFromSelected(self, selected):
295
bc6fc07c3153 Rewrite TestCtrlAutoComplete to fix all bugs
lbessard
parents: 268
diff changeset
   210
         """
268
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   211
         Sets the wx.TextCtrl value from the selected wx.ListCtrl item.
66843376a982 add autocomplete support for beremiz's textctrl
greg
parents:
diff changeset
   212
         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
   213
         """
426
3f285782ac9b Bugs with TextCtrlAutoComplete on Windows fixed
laurent
parents: 326
diff changeset
   214
         if selected != "":
295
bc6fc07c3153 Rewrite TestCtrlAutoComplete to fix all bugs
lbessard
parents: 268
diff changeset
   215
            self.SetValue(selected)
426
3f285782ac9b Bugs with TextCtrlAutoComplete on Windows fixed
laurent
parents: 326
diff changeset
   216
         self.DismissListBox()
3f285782ac9b Bugs with TextCtrlAutoComplete on Windows fixed
laurent
parents: 326
diff changeset
   217
    
3f285782ac9b Bugs with TextCtrlAutoComplete on Windows fixed
laurent
parents: 326
diff changeset
   218
    def RefreshListBoxChoices(self):
3f285782ac9b Bugs with TextCtrlAutoComplete on Windows fixed
laurent
parents: 326
diff changeset
   219
        if self.listbox is not None:
3f285782ac9b Bugs with TextCtrlAutoComplete on Windows fixed
laurent
parents: 326
diff changeset
   220
            text = self.GetValue()
3f285782ac9b Bugs with TextCtrlAutoComplete on Windows fixed
laurent
parents: 326
diff changeset
   221
            choices = [choice for choice in self._choices if choice.startswith(text)]
3f285782ac9b Bugs with TextCtrlAutoComplete on Windows fixed
laurent
parents: 326
diff changeset
   222
            self.listbox.SetChoices(choices)
3f285782ac9b Bugs with TextCtrlAutoComplete on Windows fixed
laurent
parents: 326
diff changeset
   223
3f285782ac9b Bugs with TextCtrlAutoComplete on Windows fixed
laurent
parents: 326
diff changeset
   224
    def PopupListBox(self):
3f285782ac9b Bugs with TextCtrlAutoComplete on Windows fixed
laurent
parents: 326
diff changeset
   225
        if self.listbox is None:
3f285782ac9b Bugs with TextCtrlAutoComplete on Windows fixed
laurent
parents: 326
diff changeset
   226
            self.listbox = PopupWithListbox(self)
3f285782ac9b Bugs with TextCtrlAutoComplete on Windows fixed
laurent
parents: 326
diff changeset
   227
            
3f285782ac9b Bugs with TextCtrlAutoComplete on Windows fixed
laurent
parents: 326
diff changeset
   228
            # Show the popup right below or above the button
3f285782ac9b Bugs with TextCtrlAutoComplete on Windows fixed
laurent
parents: 326
diff changeset
   229
            # depending on available screen space...
3f285782ac9b Bugs with TextCtrlAutoComplete on Windows fixed
laurent
parents: 326
diff changeset
   230
            pos = self.ClientToScreen((0, 0))
3f285782ac9b Bugs with TextCtrlAutoComplete on Windows fixed
laurent
parents: 326
diff changeset
   231
            sz = self.GetSize()
3f285782ac9b Bugs with TextCtrlAutoComplete on Windows fixed
laurent
parents: 326
diff changeset
   232
            self.listbox.Position(pos, (0, sz[1]))
3f285782ac9b Bugs with TextCtrlAutoComplete on Windows fixed
laurent
parents: 326
diff changeset
   233
            
3f285782ac9b Bugs with TextCtrlAutoComplete on Windows fixed
laurent
parents: 326
diff changeset
   234
            self.RefreshListBoxChoices()
3f285782ac9b Bugs with TextCtrlAutoComplete on Windows fixed
laurent
parents: 326
diff changeset
   235
            
3f285782ac9b Bugs with TextCtrlAutoComplete on Windows fixed
laurent
parents: 326
diff changeset
   236
            if wx.Platform == '__WXMSW__':
3f285782ac9b Bugs with TextCtrlAutoComplete on Windows fixed
laurent
parents: 326
diff changeset
   237
                self.listbox.Popup()
3f285782ac9b Bugs with TextCtrlAutoComplete on Windows fixed
laurent
parents: 326
diff changeset
   238
            else:
3f285782ac9b Bugs with TextCtrlAutoComplete on Windows fixed
laurent
parents: 326
diff changeset
   239
                self.listbox.Show()
3f285782ac9b Bugs with TextCtrlAutoComplete on Windows fixed
laurent
parents: 326
diff changeset
   240
            self.AppFrame.EnableScrolling(False)
3f285782ac9b Bugs with TextCtrlAutoComplete on Windows fixed
laurent
parents: 326
diff changeset
   241
3f285782ac9b Bugs with TextCtrlAutoComplete on Windows fixed
laurent
parents: 326
diff changeset
   242
    def DismissListBox(self):
3f285782ac9b Bugs with TextCtrlAutoComplete on Windows fixed
laurent
parents: 326
diff changeset
   243
        if self.listbox is not None:
3f285782ac9b Bugs with TextCtrlAutoComplete on Windows fixed
laurent
parents: 326
diff changeset
   244
            if wx.Platform == '__WXMSW__':
3f285782ac9b Bugs with TextCtrlAutoComplete on Windows fixed
laurent
parents: 326
diff changeset
   245
                self.listbox.Dismiss()
3f285782ac9b Bugs with TextCtrlAutoComplete on Windows fixed
laurent
parents: 326
diff changeset
   246
            else:
3f285782ac9b Bugs with TextCtrlAutoComplete on Windows fixed
laurent
parents: 326
diff changeset
   247
                self.listbox.Destroy()
3f285782ac9b Bugs with TextCtrlAutoComplete on Windows fixed
laurent
parents: 326
diff changeset
   248
            self.listbox = None
3f285782ac9b Bugs with TextCtrlAutoComplete on Windows fixed
laurent
parents: 326
diff changeset
   249
        self.AppFrame.EnableScrolling(True)
3f285782ac9b Bugs with TextCtrlAutoComplete on Windows fixed
laurent
parents: 326
diff changeset
   250