tuikit/widgets/textbox.py
author Radek Brich <radek.brich@devl.cz>
Wed, 03 Sep 2014 21:56:20 +0200
changeset 113 6796adfdc7eb
parent 112 ce2e67e7bbb8
parent 108 11dac45bfba4
child 118 8c7970520632
permissions -rw-r--r--
Merge. Due to my schizophrenia, I've accidentally forked my own code. The other set of changes were found in another computer.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
96
68c562e0eb1f Add TextBox, text editor demo. Update demobase.
Radek Brich <radek.brich@devl.cz>
parents: 77
diff changeset
     1
from tuikit.core.widget import Widget
68c562e0eb1f Add TextBox, text editor demo. Update demobase.
Radek Brich <radek.brich@devl.cz>
parents: 77
diff changeset
     2
from tuikit.core.signal import Signal
18
e6c3a5ee91aa Eliminate relative imports.
Radek Brich <radek.brich@devl.cz>
parents: 9
diff changeset
     3
0
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     4
96
68c562e0eb1f Add TextBox, text editor demo. Update demobase.
Radek Brich <radek.brich@devl.cz>
parents: 77
diff changeset
     5
class TextBox(Widget):
74
23767a33a781 Add ScrollWindow. Rewrite EditBox to work with OffsetLayout. Add propery "exposed" to DrawEvent. Add Widget._view_size. Add config file (driver, log_level).
Radek Brich <radek.brich@devl.cz>
parents: 62
diff changeset
     6
112
ce2e67e7bbb8 Refactor cursor.
Radek Brich <radek.brich@devl.cz>
parents: 111
diff changeset
     7
    """Multiline text view/edit widget."""
74
23767a33a781 Add ScrollWindow. Rewrite EditBox to work with OffsetLayout. Add propery "exposed" to DrawEvent. Add Widget._view_size. Add config file (driver, log_level).
Radek Brich <radek.brich@devl.cz>
parents: 62
diff changeset
     8
62
2f61931520c9 Rework layouts: Layout is now normal Container which places its children upon resize event.
Radek Brich <radek.brich@devl.cz>
parents: 45
diff changeset
     9
    def __init__(self, text=''):
2f61931520c9 Rework layouts: Layout is now normal Container which places its children upon resize event.
Radek Brich <radek.brich@devl.cz>
parents: 45
diff changeset
    10
        Widget.__init__(self)
109
105b1affc3c2 Update keypress propagation. Allow focus change by tab key. Add log property to Widget for smart logging.
Radek Brich <radek.brich@devl.cz>
parents: 97
diff changeset
    11
        self.allow_focus = True
96
68c562e0eb1f Add TextBox, text editor demo. Update demobase.
Radek Brich <radek.brich@devl.cz>
parents: 77
diff changeset
    12
68c562e0eb1f Add TextBox, text editor demo. Update demobase.
Radek Brich <radek.brich@devl.cz>
parents: 77
diff changeset
    13
        # Text content, splitted as lines
68c562e0eb1f Add TextBox, text editor demo. Update demobase.
Radek Brich <radek.brich@devl.cz>
parents: 77
diff changeset
    14
        self._lines = []
68c562e0eb1f Add TextBox, text editor demo. Update demobase.
Radek Brich <radek.brich@devl.cz>
parents: 77
diff changeset
    15
        self.text = text
68c562e0eb1f Add TextBox, text editor demo. Update demobase.
Radek Brich <radek.brich@devl.cz>
parents: 77
diff changeset
    16
112
ce2e67e7bbb8 Refactor cursor.
Radek Brich <radek.brich@devl.cz>
parents: 111
diff changeset
    17
        self._cursor_visible = True
113
Radek Brich <radek.brich@devl.cz>
parents: 112 108
diff changeset
    18
        # This variable remembers horizontal cursor position
74
23767a33a781 Add ScrollWindow. Rewrite EditBox to work with OffsetLayout. Add propery "exposed" to DrawEvent. Add Widget._view_size. Add config file (driver, log_level).
Radek Brich <radek.brich@devl.cz>
parents: 62
diff changeset
    19
        # for the case when cursor moves to shorter line.
23767a33a781 Add ScrollWindow. Rewrite EditBox to work with OffsetLayout. Add propery "exposed" to DrawEvent. Add Widget._view_size. Add config file (driver, log_level).
Radek Brich <radek.brich@devl.cz>
parents: 62
diff changeset
    20
        self.cursor_column = 0
23767a33a781 Add ScrollWindow. Rewrite EditBox to work with OffsetLayout. Add propery "exposed" to DrawEvent. Add Widget._view_size. Add config file (driver, log_level).
Radek Brich <radek.brich@devl.cz>
parents: 62
diff changeset
    21
        # selection - line and column of selection start
23767a33a781 Add ScrollWindow. Rewrite EditBox to work with OffsetLayout. Add propery "exposed" to DrawEvent. Add Widget._view_size. Add config file (driver, log_level).
Radek Brich <radek.brich@devl.cz>
parents: 62
diff changeset
    22
        self.sel_line = 0
23767a33a781 Add ScrollWindow. Rewrite EditBox to work with OffsetLayout. Add propery "exposed" to DrawEvent. Add Widget._view_size. Add config file (driver, log_level).
Radek Brich <radek.brich@devl.cz>
parents: 62
diff changeset
    23
        self.sel_column = 0
0
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    24
96
68c562e0eb1f Add TextBox, text editor demo. Update demobase.
Radek Brich <radek.brich@devl.cz>
parents: 77
diff changeset
    25
        #self.add_events('scroll', Event)
0
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    26
74
23767a33a781 Add ScrollWindow. Rewrite EditBox to work with OffsetLayout. Add propery "exposed" to DrawEvent. Add Widget._view_size. Add config file (driver, log_level).
Radek Brich <radek.brich@devl.cz>
parents: 62
diff changeset
    27
    @property
23767a33a781 Add ScrollWindow. Rewrite EditBox to work with OffsetLayout. Add propery "exposed" to DrawEvent. Add Widget._view_size. Add config file (driver, log_level).
Radek Brich <radek.brich@devl.cz>
parents: 62
diff changeset
    28
    def text(self):
96
68c562e0eb1f Add TextBox, text editor demo. Update demobase.
Radek Brich <radek.brich@devl.cz>
parents: 77
diff changeset
    29
        return '\n'.join(self._lines)
0
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    30
74
23767a33a781 Add ScrollWindow. Rewrite EditBox to work with OffsetLayout. Add propery "exposed" to DrawEvent. Add Widget._view_size. Add config file (driver, log_level).
Radek Brich <radek.brich@devl.cz>
parents: 62
diff changeset
    31
    @text.setter
23767a33a781 Add ScrollWindow. Rewrite EditBox to work with OffsetLayout. Add propery "exposed" to DrawEvent. Add Widget._view_size. Add config file (driver, log_level).
Radek Brich <radek.brich@devl.cz>
parents: 62
diff changeset
    32
    def text(self, value):
96
68c562e0eb1f Add TextBox, text editor demo. Update demobase.
Radek Brich <radek.brich@devl.cz>
parents: 77
diff changeset
    33
        self._lines = value.split('\n')
68c562e0eb1f Add TextBox, text editor demo. Update demobase.
Radek Brich <radek.brich@devl.cz>
parents: 77
diff changeset
    34
        maxlen = max([len(line) for line in self._lines])
68c562e0eb1f Add TextBox, text editor demo. Update demobase.
Radek Brich <radek.brich@devl.cz>
parents: 77
diff changeset
    35
        self.sizereq.update(w=maxlen, h=len(self._lines))
0
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    36
74
23767a33a781 Add ScrollWindow. Rewrite EditBox to work with OffsetLayout. Add propery "exposed" to DrawEvent. Add Widget._view_size. Add config file (driver, log_level).
Radek Brich <radek.brich@devl.cz>
parents: 62
diff changeset
    37
    @property
23767a33a781 Add ScrollWindow. Rewrite EditBox to work with OffsetLayout. Add propery "exposed" to DrawEvent. Add Widget._view_size. Add config file (driver, log_level).
Radek Brich <radek.brich@devl.cz>
parents: 62
diff changeset
    38
    def cur_line(self):
104
742e504ec053 Update TextBox: Replace "spot" with "cursor".
Radek Brich <radek.brich@devl.cz>
parents: 97
diff changeset
    39
        return self._lines[self._cursor.y]
3
33ec838dc021 Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents: 0
diff changeset
    40
74
23767a33a781 Add ScrollWindow. Rewrite EditBox to work with OffsetLayout. Add propery "exposed" to DrawEvent. Add Widget._view_size. Add config file (driver, log_level).
Radek Brich <radek.brich@devl.cz>
parents: 62
diff changeset
    41
    @cur_line.setter
23767a33a781 Add ScrollWindow. Rewrite EditBox to work with OffsetLayout. Add propery "exposed" to DrawEvent. Add Widget._view_size. Add config file (driver, log_level).
Radek Brich <radek.brich@devl.cz>
parents: 62
diff changeset
    42
    def cur_line(self, value):
104
742e504ec053 Update TextBox: Replace "spot" with "cursor".
Radek Brich <radek.brich@devl.cz>
parents: 97
diff changeset
    43
        self._lines[self._cursor.y] = value
96
68c562e0eb1f Add TextBox, text editor demo. Update demobase.
Radek Brich <radek.brich@devl.cz>
parents: 77
diff changeset
    44
68c562e0eb1f Add TextBox, text editor demo. Update demobase.
Radek Brich <radek.brich@devl.cz>
parents: 77
diff changeset
    45
    def draw(self, buffer):
68c562e0eb1f Add TextBox, text editor demo. Update demobase.
Radek Brich <radek.brich@devl.cz>
parents: 77
diff changeset
    46
        exposed = self.exposed(buffer)
112
ce2e67e7bbb8 Refactor cursor.
Radek Brich <radek.brich@devl.cz>
parents: 111
diff changeset
    47
        with buffer.attr(self.theme.normal):
96
68c562e0eb1f Add TextBox, text editor demo. Update demobase.
Radek Brich <radek.brich@devl.cz>
parents: 77
diff changeset
    48
            buffer.fill()
68c562e0eb1f Add TextBox, text editor demo. Update demobase.
Radek Brich <radek.brich@devl.cz>
parents: 77
diff changeset
    49
            end_y = min(len(self._lines), exposed.y + exposed.h)
68c562e0eb1f Add TextBox, text editor demo. Update demobase.
Radek Brich <radek.brich@devl.cz>
parents: 77
diff changeset
    50
            for j in range(exposed.y, end_y):
68c562e0eb1f Add TextBox, text editor demo. Update demobase.
Radek Brich <radek.brich@devl.cz>
parents: 77
diff changeset
    51
                line = self._lines[j]
68c562e0eb1f Add TextBox, text editor demo. Update demobase.
Radek Brich <radek.brich@devl.cz>
parents: 77
diff changeset
    52
                buffer.puts(line, 0, j)
0
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    53
111
b055add74b18 Refactor events.
Radek Brich <radek.brich@devl.cz>
parents: 109
diff changeset
    54
    def keypress_event(self, ev):
b055add74b18 Refactor events.
Radek Brich <radek.brich@devl.cz>
parents: 109
diff changeset
    55
        if ev.keyname and not ev.mods:
b055add74b18 Refactor events.
Radek Brich <radek.brich@devl.cz>
parents: 109
diff changeset
    56
            consumed = True
b055add74b18 Refactor events.
Radek Brich <radek.brich@devl.cz>
parents: 109
diff changeset
    57
            if   ev.keyname == 'left':        self.move_left()
b055add74b18 Refactor events.
Radek Brich <radek.brich@devl.cz>
parents: 109
diff changeset
    58
            elif ev.keyname == 'right':       self.move_right()
b055add74b18 Refactor events.
Radek Brich <radek.brich@devl.cz>
parents: 109
diff changeset
    59
            elif ev.keyname == 'home':        self.move_home()
b055add74b18 Refactor events.
Radek Brich <radek.brich@devl.cz>
parents: 109
diff changeset
    60
            elif ev.keyname == 'end':         self.move_end()
b055add74b18 Refactor events.
Radek Brich <radek.brich@devl.cz>
parents: 109
diff changeset
    61
            elif ev.keyname == 'up':          self.move_up()
b055add74b18 Refactor events.
Radek Brich <radek.brich@devl.cz>
parents: 109
diff changeset
    62
            elif ev.keyname == 'down':        self.move_down()
b055add74b18 Refactor events.
Radek Brich <radek.brich@devl.cz>
parents: 109
diff changeset
    63
            elif ev.keyname == 'pageup':      self.move_pageup()
b055add74b18 Refactor events.
Radek Brich <radek.brich@devl.cz>
parents: 109
diff changeset
    64
            elif ev.keyname == 'pagedown':    self.move_pagedown()
b055add74b18 Refactor events.
Radek Brich <radek.brich@devl.cz>
parents: 109
diff changeset
    65
            elif ev.keyname == 'backspace':   self.backspace()
b055add74b18 Refactor events.
Radek Brich <radek.brich@devl.cz>
parents: 109
diff changeset
    66
            elif ev.keyname == 'delete':      self.del_char()
b055add74b18 Refactor events.
Radek Brich <radek.brich@devl.cz>
parents: 109
diff changeset
    67
            elif ev.keyname == 'enter':       self.add_newline(move=True)
b055add74b18 Refactor events.
Radek Brich <radek.brich@devl.cz>
parents: 109
diff changeset
    68
            else:
b055add74b18 Refactor events.
Radek Brich <radek.brich@devl.cz>
parents: 109
diff changeset
    69
                consumed = False
b055add74b18 Refactor events.
Radek Brich <radek.brich@devl.cz>
parents: 109
diff changeset
    70
            if consumed:
b055add74b18 Refactor events.
Radek Brich <radek.brich@devl.cz>
parents: 109
diff changeset
    71
                return True
b055add74b18 Refactor events.
Radek Brich <radek.brich@devl.cz>
parents: 109
diff changeset
    72
        if ev.mods:
b055add74b18 Refactor events.
Radek Brich <radek.brich@devl.cz>
parents: 109
diff changeset
    73
            consumed = True
b055add74b18 Refactor events.
Radek Brich <radek.brich@devl.cz>
parents: 109
diff changeset
    74
            mk = ev.mod_key()
b055add74b18 Refactor events.
Radek Brich <radek.brich@devl.cz>
parents: 109
diff changeset
    75
            if   mk == 'ctrl+home':    self.move_top()
b055add74b18 Refactor events.
Radek Brich <radek.brich@devl.cz>
parents: 109
diff changeset
    76
            elif mk == 'ctrl+end':     self.move_bottom()
b055add74b18 Refactor events.
Radek Brich <radek.brich@devl.cz>
parents: 109
diff changeset
    77
            else:
b055add74b18 Refactor events.
Radek Brich <radek.brich@devl.cz>
parents: 109
diff changeset
    78
                consumed = False
b055add74b18 Refactor events.
Radek Brich <radek.brich@devl.cz>
parents: 109
diff changeset
    79
            if consumed:
b055add74b18 Refactor events.
Radek Brich <radek.brich@devl.cz>
parents: 109
diff changeset
    80
                return True
0
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    81
111
b055add74b18 Refactor events.
Radek Brich <radek.brich@devl.cz>
parents: 109
diff changeset
    82
        if ev.char and not ev.keyname:
b055add74b18 Refactor events.
Radek Brich <radek.brich@devl.cz>
parents: 109
diff changeset
    83
            self.add_char(ev.char)
0
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    84
            self.move_right()
111
b055add74b18 Refactor events.
Radek Brich <radek.brich@devl.cz>
parents: 109
diff changeset
    85
            return True
0
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    86
74
23767a33a781 Add ScrollWindow. Rewrite EditBox to work with OffsetLayout. Add propery "exposed" to DrawEvent. Add Widget._view_size. Add config file (driver, log_level).
Radek Brich <radek.brich@devl.cz>
parents: 62
diff changeset
    87
    def on_mousedown(self, ev):
23767a33a781 Add ScrollWindow. Rewrite EditBox to work with OffsetLayout. Add propery "exposed" to DrawEvent. Add Widget._view_size. Add config file (driver, log_level).
Radek Brich <radek.brich@devl.cz>
parents: 62
diff changeset
    88
        y = ev.wy
96
68c562e0eb1f Add TextBox, text editor demo. Update demobase.
Radek Brich <radek.brich@devl.cz>
parents: 77
diff changeset
    89
        x = min(ev.wx, len(self._lines[y]))
104
742e504ec053 Update TextBox: Replace "spot" with "cursor".
Radek Brich <radek.brich@devl.cz>
parents: 97
diff changeset
    90
        self._cursor.update(x=x, y=y)
77
fc1989059e19 Propagate "quit" event, do not just terminate application. Resize: flag widgets to be resized, do resizes only once before draw. Draw: flag widgets to be redrawn, do not draw everything on any event.
Radek Brich <radek.brich@devl.cz>
parents: 76
diff changeset
    91
        self.redraw()
0
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    92
45
43b2279b06e1 Clean up Emitter class, simplify event handling. Fix Container.focusnext() method. Add events test (handler auto-registration, order).
Radek Brich <radek.brich@devl.cz>
parents: 43
diff changeset
    93
    def on_mousewheel(self, ev):
0
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    94
        if ev.button == 4:
74
23767a33a781 Add ScrollWindow. Rewrite EditBox to work with OffsetLayout. Add propery "exposed" to DrawEvent. Add Widget._view_size. Add config file (driver, log_level).
Radek Brich <radek.brich@devl.cz>
parents: 62
diff changeset
    95
            # wheel up
23767a33a781 Add ScrollWindow. Rewrite EditBox to work with OffsetLayout. Add propery "exposed" to DrawEvent. Add Widget._view_size. Add config file (driver, log_level).
Radek Brich <radek.brich@devl.cz>
parents: 62
diff changeset
    96
            self.emit('scrollreq', -5)
0
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    97
        if ev.button == 5:
74
23767a33a781 Add ScrollWindow. Rewrite EditBox to work with OffsetLayout. Add propery "exposed" to DrawEvent. Add Widget._view_size. Add config file (driver, log_level).
Radek Brich <radek.brich@devl.cz>
parents: 62
diff changeset
    98
            # wheel down
23767a33a781 Add ScrollWindow. Rewrite EditBox to work with OffsetLayout. Add propery "exposed" to DrawEvent. Add Widget._view_size. Add config file (driver, log_level).
Radek Brich <radek.brich@devl.cz>
parents: 62
diff changeset
    99
            self.emit('scrollreq', +5)
0
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   100
        self.redraw()
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   101
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   102
    def move_left(self):
104
742e504ec053 Update TextBox: Replace "spot" with "cursor".
Radek Brich <radek.brich@devl.cz>
parents: 97
diff changeset
   103
        if self._cursor.x > 0:
742e504ec053 Update TextBox: Replace "spot" with "cursor".
Radek Brich <radek.brich@devl.cz>
parents: 97
diff changeset
   104
            self._cursor.x -= 1
0
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   105
        else:
104
742e504ec053 Update TextBox: Replace "spot" with "cursor".
Radek Brich <radek.brich@devl.cz>
parents: 97
diff changeset
   106
            if self._cursor.y > 0:
742e504ec053 Update TextBox: Replace "spot" with "cursor".
Radek Brich <radek.brich@devl.cz>
parents: 97
diff changeset
   107
                self._cursor.y -= 1
742e504ec053 Update TextBox: Replace "spot" with "cursor".
Radek Brich <radek.brich@devl.cz>
parents: 97
diff changeset
   108
                self._cursor.x = len(self.cur_line)
742e504ec053 Update TextBox: Replace "spot" with "cursor".
Radek Brich <radek.brich@devl.cz>
parents: 97
diff changeset
   109
        self.cursor_column = self._cursor.x
0
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   110
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   111
    def move_right(self):
104
742e504ec053 Update TextBox: Replace "spot" with "cursor".
Radek Brich <radek.brich@devl.cz>
parents: 97
diff changeset
   112
        if self._cursor.x < len(self.cur_line):
742e504ec053 Update TextBox: Replace "spot" with "cursor".
Radek Brich <radek.brich@devl.cz>
parents: 97
diff changeset
   113
            self._cursor.x += 1
0
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   114
        else:
104
742e504ec053 Update TextBox: Replace "spot" with "cursor".
Radek Brich <radek.brich@devl.cz>
parents: 97
diff changeset
   115
            if self._cursor.y < len(self._lines) - 1:
742e504ec053 Update TextBox: Replace "spot" with "cursor".
Radek Brich <radek.brich@devl.cz>
parents: 97
diff changeset
   116
                self._cursor.y += 1
742e504ec053 Update TextBox: Replace "spot" with "cursor".
Radek Brich <radek.brich@devl.cz>
parents: 97
diff changeset
   117
                self._cursor.x = 0
742e504ec053 Update TextBox: Replace "spot" with "cursor".
Radek Brich <radek.brich@devl.cz>
parents: 97
diff changeset
   118
        self.cursor_column = self._cursor.x
0
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   119
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   120
    def move_home(self):
104
742e504ec053 Update TextBox: Replace "spot" with "cursor".
Radek Brich <radek.brich@devl.cz>
parents: 97
diff changeset
   121
        self._cursor.x = 0
742e504ec053 Update TextBox: Replace "spot" with "cursor".
Radek Brich <radek.brich@devl.cz>
parents: 97
diff changeset
   122
        self.cursor_column = self._cursor.x
0
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   123
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   124
    def move_end(self):
104
742e504ec053 Update TextBox: Replace "spot" with "cursor".
Radek Brich <radek.brich@devl.cz>
parents: 97
diff changeset
   125
        self._cursor.x = len(self.cur_line)
742e504ec053 Update TextBox: Replace "spot" with "cursor".
Radek Brich <radek.brich@devl.cz>
parents: 97
diff changeset
   126
        self.cursor_column = self._cursor.x
0
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   127
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   128
    def move_up(self):
104
742e504ec053 Update TextBox: Replace "spot" with "cursor".
Radek Brich <radek.brich@devl.cz>
parents: 97
diff changeset
   129
        if self._cursor.y > 0:
742e504ec053 Update TextBox: Replace "spot" with "cursor".
Radek Brich <radek.brich@devl.cz>
parents: 97
diff changeset
   130
            self._cursor.y -= 1
742e504ec053 Update TextBox: Replace "spot" with "cursor".
Radek Brich <radek.brich@devl.cz>
parents: 97
diff changeset
   131
        self._update_cursor_x()
0
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   132
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   133
    def move_down(self):
104
742e504ec053 Update TextBox: Replace "spot" with "cursor".
Radek Brich <radek.brich@devl.cz>
parents: 97
diff changeset
   134
        if self._cursor.y < len(self._lines) - 1:
742e504ec053 Update TextBox: Replace "spot" with "cursor".
Radek Brich <radek.brich@devl.cz>
parents: 97
diff changeset
   135
            self._cursor.y += 1
742e504ec053 Update TextBox: Replace "spot" with "cursor".
Radek Brich <radek.brich@devl.cz>
parents: 97
diff changeset
   136
        self._update_cursor_x()
0
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   137
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   138
    def move_pageup(self):
104
742e504ec053 Update TextBox: Replace "spot" with "cursor".
Radek Brich <radek.brich@devl.cz>
parents: 97
diff changeset
   139
        if self._cursor.y >= self.view_height - 1:
74
23767a33a781 Add ScrollWindow. Rewrite EditBox to work with OffsetLayout. Add propery "exposed" to DrawEvent. Add Widget._view_size. Add config file (driver, log_level).
Radek Brich <radek.brich@devl.cz>
parents: 62
diff changeset
   140
            self.emit('scrollreq', - (self.view_height - 1))
104
742e504ec053 Update TextBox: Replace "spot" with "cursor".
Radek Brich <radek.brich@devl.cz>
parents: 97
diff changeset
   141
            self._cursor.y -= self.view_height - 1
0
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   142
        else:
104
742e504ec053 Update TextBox: Replace "spot" with "cursor".
Radek Brich <radek.brich@devl.cz>
parents: 97
diff changeset
   143
            self._cursor.y = 0
742e504ec053 Update TextBox: Replace "spot" with "cursor".
Radek Brich <radek.brich@devl.cz>
parents: 97
diff changeset
   144
        self._update_cursor_x()
0
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   145
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   146
    def move_pagedown(self):
104
742e504ec053 Update TextBox: Replace "spot" with "cursor".
Radek Brich <radek.brich@devl.cz>
parents: 97
diff changeset
   147
        if len(self._lines) - self._cursor.y > (self.view_height - 1):
74
23767a33a781 Add ScrollWindow. Rewrite EditBox to work with OffsetLayout. Add propery "exposed" to DrawEvent. Add Widget._view_size. Add config file (driver, log_level).
Radek Brich <radek.brich@devl.cz>
parents: 62
diff changeset
   148
            self.emit('scrollreq', (self.view_height - 1))
104
742e504ec053 Update TextBox: Replace "spot" with "cursor".
Radek Brich <radek.brich@devl.cz>
parents: 97
diff changeset
   149
            self._cursor.y += self.view_height - 1
0
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   150
        else:
104
742e504ec053 Update TextBox: Replace "spot" with "cursor".
Radek Brich <radek.brich@devl.cz>
parents: 97
diff changeset
   151
            self._cursor.y = len(self._lines) - 1
742e504ec053 Update TextBox: Replace "spot" with "cursor".
Radek Brich <radek.brich@devl.cz>
parents: 97
diff changeset
   152
        self._update_cursor_x()
0
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   153
74
23767a33a781 Add ScrollWindow. Rewrite EditBox to work with OffsetLayout. Add propery "exposed" to DrawEvent. Add Widget._view_size. Add config file (driver, log_level).
Radek Brich <radek.brich@devl.cz>
parents: 62
diff changeset
   154
    def move_top(self):
104
742e504ec053 Update TextBox: Replace "spot" with "cursor".
Radek Brich <radek.brich@devl.cz>
parents: 97
diff changeset
   155
        self._cursor.y = 0
742e504ec053 Update TextBox: Replace "spot" with "cursor".
Radek Brich <radek.brich@devl.cz>
parents: 97
diff changeset
   156
        self._update_cursor_x()
3
33ec838dc021 Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents: 0
diff changeset
   157
74
23767a33a781 Add ScrollWindow. Rewrite EditBox to work with OffsetLayout. Add propery "exposed" to DrawEvent. Add Widget._view_size. Add config file (driver, log_level).
Radek Brich <radek.brich@devl.cz>
parents: 62
diff changeset
   158
    def move_bottom(self):
104
742e504ec053 Update TextBox: Replace "spot" with "cursor".
Radek Brich <radek.brich@devl.cz>
parents: 97
diff changeset
   159
        self._cursor.y = len(self._lines) - 1
742e504ec053 Update TextBox: Replace "spot" with "cursor".
Radek Brich <radek.brich@devl.cz>
parents: 97
diff changeset
   160
        self._update_cursor_x()
3
33ec838dc021 Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents: 0
diff changeset
   161
0
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   162
    def add_char(self, c):
74
23767a33a781 Add ScrollWindow. Rewrite EditBox to work with OffsetLayout. Add propery "exposed" to DrawEvent. Add Widget._view_size. Add config file (driver, log_level).
Radek Brich <radek.brich@devl.cz>
parents: 62
diff changeset
   163
        ln = self.cur_line
104
742e504ec053 Update TextBox: Replace "spot" with "cursor".
Radek Brich <radek.brich@devl.cz>
parents: 97
diff changeset
   164
        sx = self._cursor.x
74
23767a33a781 Add ScrollWindow. Rewrite EditBox to work with OffsetLayout. Add propery "exposed" to DrawEvent. Add Widget._view_size. Add config file (driver, log_level).
Radek Brich <radek.brich@devl.cz>
parents: 62
diff changeset
   165
        self.cur_line = ln[:sx] + c + ln[sx:]
23767a33a781 Add ScrollWindow. Rewrite EditBox to work with OffsetLayout. Add propery "exposed" to DrawEvent. Add Widget._view_size. Add config file (driver, log_level).
Radek Brich <radek.brich@devl.cz>
parents: 62
diff changeset
   166
        self.cursor_column = sx
0
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   167
74
23767a33a781 Add ScrollWindow. Rewrite EditBox to work with OffsetLayout. Add propery "exposed" to DrawEvent. Add Widget._view_size. Add config file (driver, log_level).
Radek Brich <radek.brich@devl.cz>
parents: 62
diff changeset
   168
    def add_newline(self, move=False):
23767a33a781 Add ScrollWindow. Rewrite EditBox to work with OffsetLayout. Add propery "exposed" to DrawEvent. Add Widget._view_size. Add config file (driver, log_level).
Radek Brich <radek.brich@devl.cz>
parents: 62
diff changeset
   169
        ln = self.cur_line
104
742e504ec053 Update TextBox: Replace "spot" with "cursor".
Radek Brich <radek.brich@devl.cz>
parents: 97
diff changeset
   170
        sx = self._cursor.x
74
23767a33a781 Add ScrollWindow. Rewrite EditBox to work with OffsetLayout. Add propery "exposed" to DrawEvent. Add Widget._view_size. Add config file (driver, log_level).
Radek Brich <radek.brich@devl.cz>
parents: 62
diff changeset
   171
        self.cur_line = ln[sx:]
104
742e504ec053 Update TextBox: Replace "spot" with "cursor".
Radek Brich <radek.brich@devl.cz>
parents: 97
diff changeset
   172
        self._lines.insert(self._cursor.y, ln[:sx])
112
ce2e67e7bbb8 Refactor cursor.
Radek Brich <radek.brich@devl.cz>
parents: 111
diff changeset
   173
        self.sizereq.update(h=len(self._lines))
74
23767a33a781 Add ScrollWindow. Rewrite EditBox to work with OffsetLayout. Add propery "exposed" to DrawEvent. Add Widget._view_size. Add config file (driver, log_level).
Radek Brich <radek.brich@devl.cz>
parents: 62
diff changeset
   174
        if move:
23767a33a781 Add ScrollWindow. Rewrite EditBox to work with OffsetLayout. Add propery "exposed" to DrawEvent. Add Widget._view_size. Add config file (driver, log_level).
Radek Brich <radek.brich@devl.cz>
parents: 62
diff changeset
   175
            self.move_right()
0
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   176
76
fa5301e58eca Update demo_input, demo_editor. Update ScrollView: show/hide scrollbars as needed on child size requests.
Radek Brich <radek.brich@devl.cz>
parents: 74
diff changeset
   177
    def add_line(self, text):
fa5301e58eca Update demo_input, demo_editor. Update ScrollView: show/hide scrollbars as needed on child size requests.
Radek Brich <radek.brich@devl.cz>
parents: 74
diff changeset
   178
        ln = self.cur_line
104
742e504ec053 Update TextBox: Replace "spot" with "cursor".
Radek Brich <radek.brich@devl.cz>
parents: 97
diff changeset
   179
        sx = self._cursor.x
76
fa5301e58eca Update demo_input, demo_editor. Update ScrollView: show/hide scrollbars as needed on child size requests.
Radek Brich <radek.brich@devl.cz>
parents: 74
diff changeset
   180
        self.cur_line = ln[sx:]
104
742e504ec053 Update TextBox: Replace "spot" with "cursor".
Radek Brich <radek.brich@devl.cz>
parents: 97
diff changeset
   181
        self._lines.insert(self._cursor.y, ln[:sx] + text)
76
fa5301e58eca Update demo_input, demo_editor. Update ScrollView: show/hide scrollbars as needed on child size requests.
Radek Brich <radek.brich@devl.cz>
parents: 74
diff changeset
   182
        self.cursor_column = 0
104
742e504ec053 Update TextBox: Replace "spot" with "cursor".
Radek Brich <radek.brich@devl.cz>
parents: 97
diff changeset
   183
        self._cursor.x = 0
742e504ec053 Update TextBox: Replace "spot" with "cursor".
Radek Brich <radek.brich@devl.cz>
parents: 97
diff changeset
   184
        self._cursor.y += 1
112
ce2e67e7bbb8 Refactor cursor.
Radek Brich <radek.brich@devl.cz>
parents: 111
diff changeset
   185
        w = max(self.sizereq.w, len(ln[:sx] + text))
ce2e67e7bbb8 Refactor cursor.
Radek Brich <radek.brich@devl.cz>
parents: 111
diff changeset
   186
        self.sizereq.update(w=w, h=len(self._lines))
76
fa5301e58eca Update demo_input, demo_editor. Update ScrollView: show/hide scrollbars as needed on child size requests.
Radek Brich <radek.brich@devl.cz>
parents: 74
diff changeset
   187
74
23767a33a781 Add ScrollWindow. Rewrite EditBox to work with OffsetLayout. Add propery "exposed" to DrawEvent. Add Widget._view_size. Add config file (driver, log_level).
Radek Brich <radek.brich@devl.cz>
parents: 62
diff changeset
   188
    def backspace(self):
104
742e504ec053 Update TextBox: Replace "spot" with "cursor".
Radek Brich <radek.brich@devl.cz>
parents: 97
diff changeset
   189
        if self._cursor.y > 0 or self._cursor.x > 0:
74
23767a33a781 Add ScrollWindow. Rewrite EditBox to work with OffsetLayout. Add propery "exposed" to DrawEvent. Add Widget._view_size. Add config file (driver, log_level).
Radek Brich <radek.brich@devl.cz>
parents: 62
diff changeset
   190
            self.move_left()
23767a33a781 Add ScrollWindow. Rewrite EditBox to work with OffsetLayout. Add propery "exposed" to DrawEvent. Add Widget._view_size. Add config file (driver, log_level).
Radek Brich <radek.brich@devl.cz>
parents: 62
diff changeset
   191
            self.del_char()
0
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   192
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   193
    def del_char(self):
74
23767a33a781 Add ScrollWindow. Rewrite EditBox to work with OffsetLayout. Add propery "exposed" to DrawEvent. Add Widget._view_size. Add config file (driver, log_level).
Radek Brich <radek.brich@devl.cz>
parents: 62
diff changeset
   194
        ln = self.cur_line
104
742e504ec053 Update TextBox: Replace "spot" with "cursor".
Radek Brich <radek.brich@devl.cz>
parents: 97
diff changeset
   195
        sx = self._cursor.x
74
23767a33a781 Add ScrollWindow. Rewrite EditBox to work with OffsetLayout. Add propery "exposed" to DrawEvent. Add Widget._view_size. Add config file (driver, log_level).
Radek Brich <radek.brich@devl.cz>
parents: 62
diff changeset
   196
        if sx == len(self.cur_line):
104
742e504ec053 Update TextBox: Replace "spot" with "cursor".
Radek Brich <radek.brich@devl.cz>
parents: 97
diff changeset
   197
            if self._cursor.y + 1 < len(self._lines):
742e504ec053 Update TextBox: Replace "spot" with "cursor".
Radek Brich <radek.brich@devl.cz>
parents: 97
diff changeset
   198
                self.cur_line += self._lines[self._cursor.y+1]
742e504ec053 Update TextBox: Replace "spot" with "cursor".
Radek Brich <radek.brich@devl.cz>
parents: 97
diff changeset
   199
                del self._lines[self._cursor.y+1]
112
ce2e67e7bbb8 Refactor cursor.
Radek Brich <radek.brich@devl.cz>
parents: 111
diff changeset
   200
                self.sizereq.update(h=len(self._lines))
0
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   201
        else:
74
23767a33a781 Add ScrollWindow. Rewrite EditBox to work with OffsetLayout. Add propery "exposed" to DrawEvent. Add Widget._view_size. Add config file (driver, log_level).
Radek Brich <radek.brich@devl.cz>
parents: 62
diff changeset
   202
            self.cur_line = ln[:sx] + ln[sx+1:]
18
e6c3a5ee91aa Eliminate relative imports.
Radek Brich <radek.brich@devl.cz>
parents: 9
diff changeset
   203
104
742e504ec053 Update TextBox: Replace "spot" with "cursor".
Radek Brich <radek.brich@devl.cz>
parents: 97
diff changeset
   204
    def _update_cursor_x(self):
74
23767a33a781 Add ScrollWindow. Rewrite EditBox to work with OffsetLayout. Add propery "exposed" to DrawEvent. Add Widget._view_size. Add config file (driver, log_level).
Radek Brich <radek.brich@devl.cz>
parents: 62
diff changeset
   205
        if self.cursor_column > len(self.cur_line):
104
742e504ec053 Update TextBox: Replace "spot" with "cursor".
Radek Brich <radek.brich@devl.cz>
parents: 97
diff changeset
   206
            self._cursor.x = len(self.cur_line)
74
23767a33a781 Add ScrollWindow. Rewrite EditBox to work with OffsetLayout. Add propery "exposed" to DrawEvent. Add Widget._view_size. Add config file (driver, log_level).
Radek Brich <radek.brich@devl.cz>
parents: 62
diff changeset
   207
        else:
104
742e504ec053 Update TextBox: Replace "spot" with "cursor".
Radek Brich <radek.brich@devl.cz>
parents: 97
diff changeset
   208
            self._cursor.x = self.cursor_column
74
23767a33a781 Add ScrollWindow. Rewrite EditBox to work with OffsetLayout. Add propery "exposed" to DrawEvent. Add Widget._view_size. Add config file (driver, log_level).
Radek Brich <radek.brich@devl.cz>
parents: 62
diff changeset
   209