summaryrefslogtreecommitdiff
path: root/lib/gui/edit_json/combo_prompt.py
blob: bebb1947f767c14a43016d7aaea9f42244b306b8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import wx


class PromptingComboBox(wx.ComboBox):
    def __init__(self, parent, choices=[], style=0, **kwargs):
        if choices is None:
            choices = []
        wx.ComboBox.__init__(self, parent, wx.ID_ANY, style=style, choices=choices, **kwargs)
        self.choices = choices
        self.Bind(wx.EVT_KEY_DOWN, self.on_button_down)
        self.Bind(wx.EVT_TEXT, self.on_text_edit)
        self.Bind(wx.EVT_TEXT_ENTER, self.on_text_edit)
        self.ignore_text_event = False
        self.delete_key = False
        self.found_choice = False
        self.parent = parent

    def update_choices(self, choices):
        self.choices = choices

    def on_button_down(self, event):
        if event.GetKeyCode() == 8:
            self.delete_key = True
        event.Skip()

    def on_text_edit(self, event):
        current_text = event.GetString()
        if len(current_text) == 0 or current_text == " ":
            self.delete_key = False
            self.parent.on_combobox_change(event)
            return
        if self.ignore_text_event:
            self.ignore_text_event = False
            return
        if self.delete_key:
            self.delete_key = False
            if self.found_choice:
                current_text = current_text[:-1]

        self.found_choice = False
        for i, choice in enumerate(self.choices):
            if choice.startswith(current_text.strip()):
                self.ignore_text_event = True
                self.SetValue(choice)
                self.SetInsertionPoint(len(current_text))
                self.SetTextSelection(len(current_text), len(choice))
                self.found_choice = True
                self.parent.on_combobox_change(event)
                break