Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/pynxc/waxy/textentrydialog.py
blob: afe31cae066685ba7722771fdbd66325f076f7ed (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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# textentrydialog.py
# Simple dialog for entering a string.
# Note: Not based on wxPython's TextEntryDialog.

from dialog import Dialog
from textbox import TextBox
from label import Label
from keys import keys
import string

class TextEntryDialog(Dialog):

    def __init__(self, parent, title="Enter some text", prompt="Enter some text",
     default="", cancel_button=1):
        self.prompt = prompt
        self.default = default
        Dialog.__init__(self, parent, title, cancel_button=cancel_button)

    def Body(self):
        label = Label(self, self.prompt)
        self.AddComponent(label, expand='h', border=7)

        self.text = TextBox(self, size=(100,25), process_enter=1)
        self.text.SetValue(self.default)
        self.text.OnChar = self.OnTextBoxChar
        self.AddComponent(self.text, expand='h', border=5)

    def OnTextBoxChar(self, event=None):
        # pressing Enter in the TextBox is the same as clicking OK
        if event.GetKeyCode() == keys.enter:
            self.OnClickOKButton(event)
        else:
            event.Skip()

    def GetValue(self):
        return self.text.GetValue()




class IntegerInputDialog(TextEntryDialog):

    def __init__(self, parent, title='Enter some text', 
                 prompt='Enter some text', default='', cancel_button=1):

        TextEntryDialog.__init__(self, parent, title=title,
                 prompt=prompt, default=str(default), 
                 cancel_button=cancel_button)


    def GetValue(self):

        try:
            val=TextEntryDialog.GetValue(self)
        except ValueError:
            return None

        try:
            int_val=eval("int("+val+")")
        except (SyntaxError,NameError):
            int_val=None

        return int_val


    def OnCharHook(self, event):
        key = event.KeyCode()

        if key == wx.WXK_ESCAPE:
            self.OnClickCancelButton()

        if key < wx.WXK_SPACE or key == wx.WXK_DELETE or key > 255:
            event.Skip()
            return


        good_chars=string.digits+'+*-()'
        if chr(key) in good_chars:
            event.Skip()
            return

        if not wx.Validator_IsSilent():
            wx.Bell()

        # Returning without calling even.Skip eats the event before it
        # gets to the text control
        return



    def Validate(self):
        val = TextEntryDialog.GetValue(self)  # make sure the get a string

        good_chars=string.digits+'+*-()'
        for x in val:
            if x not in good_chars:
                return False

        try:
            int_val=eval("int("+val+")")
        except (SyntaxError,NameError):
            Error("Syntax Error")
            return False

        return True


def Input_Integer(title,
                 prompt, default=None,parent=None):


    a=IntegerInputDialog(parent,title,prompt,default)
    result=a.ShowModal()

    if result=='ok':
        r=a.GetValue()
    else:
        r=None

    a.Destroy()

    return r


class FloatInputDialog(TextEntryDialog):

    def __init__(self, parent, title='Enter some text', 
                 prompt='Enter some text', default=None, cancel_button=1):

        TextEntryDialog.__init__(self, parent, title=title,
                 prompt=prompt, default=str(default), 
                 cancel_button=cancel_button)


    def GetValue(self):

        try:
            val=TextEntryDialog.GetValue(self)
        except ValueError:
            return None

        try:
            float_val=eval("float("+val+")")
        except SyntaxError:
            float_val=None

        return float_val



    def Validate(self):
        val = TextEntryDialog.GetValue(self)  # make sure the get a string

        try:
            float_val=eval("float("+val+")")
        except (SyntaxError,NameError):
            Error("Syntax Error")
            return False

        return True



def Input_Float(title,
                 prompt, default=None,parent=None):

    a=FloatInputDialog(parent,title,prompt,default)
    result=a.ShowModal()

    if result=='ok':
        r=a.GetValue()
    else:
        r=None

    a.Destroy()

    return r