Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/pynxc/waxy/textbox.py
blob: 7595b5afc59498c040f94e4ca1bdc7d373878250 (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
# textbox.py

import waxyobject
import wx
import core
import styles

class TextBox(wx.TextCtrl, waxyobject.WaxyObject):

    __events__ = {
        'Char': wx.EVT_CHAR,    # do all controls have this?
        'MaxLength': wx.EVT_TEXT_MAXLEN,  # alias for TextMaxLen
        'Text': wx.EVT_TEXT,
        'TextEnter': wx.EVT_TEXT_ENTER,
        'TextMaxLen': wx.EVT_TEXT_MAXLEN,
        'TextURL': wx.EVT_TEXT_URL,
    }

    def __init__(self, parent, text="", size=None, **kwargs):
        style = 0
        style |= self._params(kwargs)
        style |= styles.window(kwargs)

        wx.TextCtrl.__init__(self, parent, wx.NewId(), text,
         size=size or (125,-1), style=style)

        self.BindEvents()
        styles.properties(self, kwargs)

    def write(self, s):
        # Added so we can use a TextBox as a file-like object and redirect
        # stdout to it.
        self.AppendText(s)
        try:
            core.Yield()
        except:
            pass

    def GetCurrentLineNumber(self):
        """ Return the current line number (i.e. the number of the line the
            cursor is on). """
        pos = self.GetInsertionPoint()
        x, y = self.PositionToXY(pos)
        return y

    def GetLines(self):
        """ Return the current text as a list of lines.  (Changing the list
            does not affect the contents of the TextBox.) """
        text = self.GetValue()
        lines = text.split("\n")
        return lines

    def SetModified(self, modified):
        if modified:
            # set to modified by appending a dummy space and removing it again
            self.AppendText(' ')
            lastpos = self.GetLastPosition()
            self.Remove(lastpos-1, lastpos)
        else:
            self.DiscardEdits()

    def GetModified(self):
        """ Returns true if the contents of the control were modified.  (Alias
            for IsModified(). """
        return self.IsModified()

    def InsertText(self, pos, text):
        """ Insert text at the given position. """
        old_insertion_point = self.GetInsertionPoint()
        self.SetInsertionPoint(pos)
        self.WriteText(text)
        # put cursor at original insertion point
        if old_insertion_point <= pos:
            self.SetInsertionPoint(old_insertion_point)
        else:
            self.SetInsertionPoint(old_insertion_point + len(text))

    # ideas:
    # should Remove support negative indexes? (like slices)
    # should it support slicing?  e.g. del atextbox[10:20]

    #
    # style parameters

    #_textbox_justify = {
    #    "left": wx.TE_LEFT,
    #    "center": wx.TE_CENTRE,
    #    "centre": wx.TE_CENTRE,
    #    "middle": wx.TE_CENTRE,
    #    "right": wx.TE_RIGHT,
    #}

    __styles__ = {
        'justify': ({
            'left': wx.TE_LEFT,
            'center': wx.TE_CENTRE,
            'centre': wx.TE_CENTRE,
            'middle': wx.TE_CENTRE,
            'right': wx.TE_RIGHT,
        }, styles.DICTSTART),
        'multiline': (wx.TE_MULTILINE, styles.NORMAL),
        'password': (wx.TE_PASSWORD, styles.NORMAL),
        'readonly': (wx.TE_READONLY, styles.NORMAL),
        'wrap': (wx.TE_DONTWRAP, styles.NORMAL | styles.REVERSE),
        'process_enter': (wx.TE_PROCESS_ENTER, styles.NORMAL),
        'process_tab': (wx.TE_PROCESS_TAB, styles.NORMAL),
        'rich': (wx.TE_RICH, styles.NORMAL),
        'rich2': (wx.TE_RICH2, styles.NORMAL),
        'auto_url': (wx.TE_AUTO_URL, styles.NORMAL),
        'hscroll': (wx.HSCROLL, styles.NORMAL),
    }
        
    def _params(self, kwargs):
        flags = 0 | wx.TE_NOHIDESEL # maybe add the option of changing this one
        #flags |= styles.stylebool('multiline', wx.TE_MULTILINE, kwargs)
        #flags |= styles.stylebool('password', wx.TE_PASSWORD, kwargs)
        #flags |= styles.stylebool('readonly', wx.TE_READONLY, kwargs)
        #flags |= styles.stylebool('wrap', wx.TE_DONTWRAP, kwargs, reverse=1)
        #flags |= styles.stylebool('process_enter', wx.TE_PROCESS_ENTER, kwargs)
        #flags |= styles.stylebool('process_tab', wx.TE_PROCESS_TAB, kwargs)
        #flags |= styles.stylebool('rich', wx.TE_RICH, kwargs)
        #flags |= styles.stylebool('rich2', wx.TE_RICH2, kwargs)
        #flags |= styles.stylebool('auto_url', wx.TE_AUTO_URL, kwargs)
        #flags |= styles.stylebool('hscroll', wx.HSCROLL, kwargs)
        #flags |= styles.styledictstart('justify', self._textbox_justify, kwargs, 0)
        flags |= styles.dostyle(self.__styles__, kwargs)
        return flags