tuikit/widgets/textbox.py
author Radek Brich <radek.brich@devl.cz>
Wed, 03 Sep 2014 08:57:24 +0200
changeset 108 11dac45bfba4
parent 104 742e504ec053
child 113 6796adfdc7eb
permissions -rw-r--r--
Fix cursor.
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
96
68c562e0eb1f Add TextBox, text editor demo. Update demobase.
Radek Brich <radek.brich@devl.cz>
parents: 77
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
104
742e504ec053 Update TextBox: Replace "spot" with "cursor".
Radek Brich <radek.brich@devl.cz>
parents: 97
diff changeset
     9
    Cursor is used for text 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
    10
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
    11
    """
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
    12
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
    13
    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
    14
        Widget.__init__(self)
96
68c562e0eb1f Add TextBox, text editor demo. Update demobase.
Radek Brich <radek.brich@devl.cz>
parents: 77
diff changeset
    15
68c562e0eb1f Add TextBox, text editor demo. Update demobase.
Radek Brich <radek.brich@devl.cz>
parents: 77
diff changeset
    16
        # Text content, splitted as lines
68c562e0eb1f Add TextBox, text editor demo. Update demobase.
Radek Brich <radek.brich@devl.cz>
parents: 77
diff changeset
    17
        self._lines = []
68c562e0eb1f Add TextBox, text editor demo. Update demobase.
Radek Brich <radek.brich@devl.cz>
parents: 77
diff changeset
    18
        self.text = text
68c562e0eb1f Add TextBox, text editor demo. Update demobase.
Radek Brich <radek.brich@devl.cz>
parents: 77
diff changeset
    19
34
e3beacd5e536 Update event propagation, keypress event, focusing.
Radek Brich <radek.brich@devl.cz>
parents: 32
diff changeset
    20
        self.allow_focus = True
e3beacd5e536 Update event propagation, keypress event, focusing.
Radek Brich <radek.brich@devl.cz>
parents: 32
diff changeset
    21
104
742e504ec053 Update TextBox: Replace "spot" with "cursor".
Radek Brich <radek.brich@devl.cz>
parents: 97
diff changeset
    22
        # This variable rememberes horizontal position of cursor
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
    23
        # 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
    24
        self.cursor_column = 0
108
11dac45bfba4 Fix cursor.
Radek Brich <radek.brich@devl.cz>
parents: 104
diff changeset
    25
        self._cursor.update(0, 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
    26
        # 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
    27
        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
    28
        self.sel_column = 0
0
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    29
96
68c562e0eb1f Add TextBox, text editor demo. Update demobase.
Radek Brich <radek.brich@devl.cz>
parents: 77
diff changeset
    30
        #self.add_events('scroll', Event)
0
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    31
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
    32
    @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
    33
    def text(self):
96
68c562e0eb1f Add TextBox, text editor demo. Update demobase.
Radek Brich <radek.brich@devl.cz>
parents: 77
diff changeset
    34
        return '\n'.join(self._lines)
0
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    35
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
    36
    @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
    37
    def text(self, value):
96
68c562e0eb1f Add TextBox, text editor demo. Update demobase.
Radek Brich <radek.brich@devl.cz>
parents: 77
diff changeset
    38
        self._lines = value.split('\n')
68c562e0eb1f Add TextBox, text editor demo. Update demobase.
Radek Brich <radek.brich@devl.cz>
parents: 77
diff changeset
    39
        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
    40
        self.sizereq.update(w=maxlen, h=len(self._lines))
0
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    41
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
    42
    @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
    43
    def cur_line(self):
104
742e504ec053 Update TextBox: Replace "spot" with "cursor".
Radek Brich <radek.brich@devl.cz>
parents: 97
diff changeset
    44
        return self._lines[self._cursor.y]
3
33ec838dc021 Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents: 0
diff changeset
    45
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
    46
    @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
    47
    def cur_line(self, value):
104
742e504ec053 Update TextBox: Replace "spot" with "cursor".
Radek Brich <radek.brich@devl.cz>
parents: 97
diff changeset
    48
        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
    49
68c562e0eb1f Add TextBox, text editor demo. Update demobase.
Radek Brich <radek.brich@devl.cz>
parents: 77
diff changeset
    50
    def set_theme(self, theme):
68c562e0eb1f Add TextBox, text editor demo. Update demobase.
Radek Brich <radek.brich@devl.cz>
parents: 77
diff changeset
    51
        Widget.set_theme(self, theme)
68c562e0eb1f Add TextBox, text editor demo. Update demobase.
Radek Brich <radek.brich@devl.cz>
parents: 77
diff changeset
    52
        self.color = theme.normal
0
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    53
96
68c562e0eb1f Add TextBox, text editor demo. Update demobase.
Radek Brich <radek.brich@devl.cz>
parents: 77
diff changeset
    54
    def draw(self, buffer):
68c562e0eb1f Add TextBox, text editor demo. Update demobase.
Radek Brich <radek.brich@devl.cz>
parents: 77
diff changeset
    55
        exposed = self.exposed(buffer)
68c562e0eb1f Add TextBox, text editor demo. Update demobase.
Radek Brich <radek.brich@devl.cz>
parents: 77
diff changeset
    56
        with buffer.attr(self.color):
68c562e0eb1f Add TextBox, text editor demo. Update demobase.
Radek Brich <radek.brich@devl.cz>
parents: 77
diff changeset
    57
            buffer.fill()
68c562e0eb1f Add TextBox, text editor demo. Update demobase.
Radek Brich <radek.brich@devl.cz>
parents: 77
diff changeset
    58
            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
    59
            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
    60
                line = self._lines[j]
68c562e0eb1f Add TextBox, text editor demo. Update demobase.
Radek Brich <radek.brich@devl.cz>
parents: 77
diff changeset
    61
                buffer.puts(line, 0, j)
0
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    62
97
0c2e0c09ba5c Add TextField widget, keypress event, cursor.
Radek Brich <radek.brich@devl.cz>
parents: 96
diff changeset
    63
    def keypress(self, keyname, char, mod=0):
0c2e0c09ba5c Add TextField widget, keypress event, cursor.
Radek Brich <radek.brich@devl.cz>
parents: 96
diff changeset
    64
        if keyname:
0c2e0c09ba5c Add TextField widget, keypress event, cursor.
Radek Brich <radek.brich@devl.cz>
parents: 96
diff changeset
    65
            if keyname == 'left':        self.move_left()
0c2e0c09ba5c Add TextField widget, keypress event, cursor.
Radek Brich <radek.brich@devl.cz>
parents: 96
diff changeset
    66
            if keyname == 'right':       self.move_right()
0c2e0c09ba5c Add TextField widget, keypress event, cursor.
Radek Brich <radek.brich@devl.cz>
parents: 96
diff changeset
    67
            if keyname == 'home':        self.move_home()
0c2e0c09ba5c Add TextField widget, keypress event, cursor.
Radek Brich <radek.brich@devl.cz>
parents: 96
diff changeset
    68
            if keyname == 'end':         self.move_end()
0c2e0c09ba5c Add TextField widget, keypress event, cursor.
Radek Brich <radek.brich@devl.cz>
parents: 96
diff changeset
    69
            if keyname == 'up':          self.move_up()
0c2e0c09ba5c Add TextField widget, keypress event, cursor.
Radek Brich <radek.brich@devl.cz>
parents: 96
diff changeset
    70
            if keyname == 'down':        self.move_down()
0c2e0c09ba5c Add TextField widget, keypress event, cursor.
Radek Brich <radek.brich@devl.cz>
parents: 96
diff changeset
    71
            if keyname == 'pageup':      self.move_pageup()
0c2e0c09ba5c Add TextField widget, keypress event, cursor.
Radek Brich <radek.brich@devl.cz>
parents: 96
diff changeset
    72
            if keyname == 'pagedown':    self.move_pagedown()
0c2e0c09ba5c Add TextField widget, keypress event, cursor.
Radek Brich <radek.brich@devl.cz>
parents: 96
diff changeset
    73
            if keyname == 'backspace':   self.backspace()
0c2e0c09ba5c Add TextField widget, keypress event, cursor.
Radek Brich <radek.brich@devl.cz>
parents: 96
diff changeset
    74
            if keyname == 'delete':      self.del_char()
0c2e0c09ba5c Add TextField widget, keypress event, cursor.
Radek Brich <radek.brich@devl.cz>
parents: 96
diff changeset
    75
            if keyname == 'enter':       self.add_newline(move=True)
104
742e504ec053 Update TextBox: Replace "spot" with "cursor".
Radek Brich <radek.brich@devl.cz>
parents: 97
diff changeset
    76
            if 'ctrl' in mod:
97
0c2e0c09ba5c Add TextField widget, keypress event, cursor.
Radek Brich <radek.brich@devl.cz>
parents: 96
diff changeset
    77
                if keyname == 'home':    self.move_top()
0c2e0c09ba5c Add TextField widget, keypress event, cursor.
Radek Brich <radek.brich@devl.cz>
parents: 96
diff changeset
    78
                if keyname == 'end':     self.move_bottom()
0
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    79
97
0c2e0c09ba5c Add TextField widget, keypress event, cursor.
Radek Brich <radek.brich@devl.cz>
parents: 96
diff changeset
    80
        if char:
0c2e0c09ba5c Add TextField widget, keypress event, cursor.
Radek Brich <radek.brich@devl.cz>
parents: 96
diff changeset
    81
            self.add_char(char)
0
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    82
            self.move_right()
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    83
97
0c2e0c09ba5c Add TextField widget, keypress event, cursor.
Radek Brich <radek.brich@devl.cz>
parents: 96
diff changeset
    84
        #self.redraw()
0
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    85
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
    86
    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
    87
        y = ev.wy
96
68c562e0eb1f Add TextBox, text editor demo. Update demobase.
Radek Brich <radek.brich@devl.cz>
parents: 77
diff changeset
    88
        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
    89
        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
    90
        self.redraw()
0
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    91
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
    92
    def on_mousewheel(self, ev):
0
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    93
        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
    94
            # 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
    95
            self.emit('scrollreq', -5)
0
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    96
        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
    97
            # 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
    98
            self.emit('scrollreq', +5)
0
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    99
        self.redraw()
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   100
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   101
    def move_left(self):
104
742e504ec053 Update TextBox: Replace "spot" with "cursor".
Radek Brich <radek.brich@devl.cz>
parents: 97
diff changeset
   102
        if self._cursor.x > 0:
742e504ec053 Update TextBox: Replace "spot" with "cursor".
Radek Brich <radek.brich@devl.cz>
parents: 97
diff changeset
   103
            self._cursor.x -= 1
0
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   104
        else:
104
742e504ec053 Update TextBox: Replace "spot" with "cursor".
Radek Brich <radek.brich@devl.cz>
parents: 97
diff changeset
   105
            if self._cursor.y > 0:
742e504ec053 Update TextBox: Replace "spot" with "cursor".
Radek Brich <radek.brich@devl.cz>
parents: 97
diff changeset
   106
                self._cursor.y -= 1
742e504ec053 Update TextBox: Replace "spot" with "cursor".
Radek Brich <radek.brich@devl.cz>
parents: 97
diff changeset
   107
                self._cursor.x = len(self.cur_line)
742e504ec053 Update TextBox: Replace "spot" with "cursor".
Radek Brich <radek.brich@devl.cz>
parents: 97
diff changeset
   108
        self.cursor_column = self._cursor.x
0
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   109
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   110
    def move_right(self):
104
742e504ec053 Update TextBox: Replace "spot" with "cursor".
Radek Brich <radek.brich@devl.cz>
parents: 97
diff changeset
   111
        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
   112
            self._cursor.x += 1
0
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   113
        else:
104
742e504ec053 Update TextBox: Replace "spot" with "cursor".
Radek Brich <radek.brich@devl.cz>
parents: 97
diff changeset
   114
            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
   115
                self._cursor.y += 1
742e504ec053 Update TextBox: Replace "spot" with "cursor".
Radek Brich <radek.brich@devl.cz>
parents: 97
diff changeset
   116
                self._cursor.x = 0
742e504ec053 Update TextBox: Replace "spot" with "cursor".
Radek Brich <radek.brich@devl.cz>
parents: 97
diff changeset
   117
        self.cursor_column = self._cursor.x
0
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   118
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   119
    def move_home(self):
104
742e504ec053 Update TextBox: Replace "spot" with "cursor".
Radek Brich <radek.brich@devl.cz>
parents: 97
diff changeset
   120
        self._cursor.x = 0
742e504ec053 Update TextBox: Replace "spot" with "cursor".
Radek Brich <radek.brich@devl.cz>
parents: 97
diff changeset
   121
        self.cursor_column = self._cursor.x
0
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   122
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   123
    def move_end(self):
104
742e504ec053 Update TextBox: Replace "spot" with "cursor".
Radek Brich <radek.brich@devl.cz>
parents: 97
diff changeset
   124
        self._cursor.x = len(self.cur_line)
742e504ec053 Update TextBox: Replace "spot" with "cursor".
Radek Brich <radek.brich@devl.cz>
parents: 97
diff changeset
   125
        self.cursor_column = self._cursor.x
0
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   126
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   127
    def move_up(self):
104
742e504ec053 Update TextBox: Replace "spot" with "cursor".
Radek Brich <radek.brich@devl.cz>
parents: 97
diff changeset
   128
        if self._cursor.y > 0:
742e504ec053 Update TextBox: Replace "spot" with "cursor".
Radek Brich <radek.brich@devl.cz>
parents: 97
diff changeset
   129
            self._cursor.y -= 1
742e504ec053 Update TextBox: Replace "spot" with "cursor".
Radek Brich <radek.brich@devl.cz>
parents: 97
diff changeset
   130
        self._update_cursor_x()
0
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   131
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   132
    def move_down(self):
104
742e504ec053 Update TextBox: Replace "spot" with "cursor".
Radek Brich <radek.brich@devl.cz>
parents: 97
diff changeset
   133
        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
   134
            self._cursor.y += 1
742e504ec053 Update TextBox: Replace "spot" with "cursor".
Radek Brich <radek.brich@devl.cz>
parents: 97
diff changeset
   135
        self._update_cursor_x()
0
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   136
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   137
    def move_pageup(self):
104
742e504ec053 Update TextBox: Replace "spot" with "cursor".
Radek Brich <radek.brich@devl.cz>
parents: 97
diff changeset
   138
        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
   139
            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
   140
            self._cursor.y -= self.view_height - 1
0
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   141
        else:
104
742e504ec053 Update TextBox: Replace "spot" with "cursor".
Radek Brich <radek.brich@devl.cz>
parents: 97
diff changeset
   142
            self._cursor.y = 0
742e504ec053 Update TextBox: Replace "spot" with "cursor".
Radek Brich <radek.brich@devl.cz>
parents: 97
diff changeset
   143
        self._update_cursor_x()
0
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   144
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   145
    def move_pagedown(self):
104
742e504ec053 Update TextBox: Replace "spot" with "cursor".
Radek Brich <radek.brich@devl.cz>
parents: 97
diff changeset
   146
        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
   147
            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
   148
            self._cursor.y += self.view_height - 1
0
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   149
        else:
104
742e504ec053 Update TextBox: Replace "spot" with "cursor".
Radek Brich <radek.brich@devl.cz>
parents: 97
diff changeset
   150
            self._cursor.y = len(self._lines) - 1
742e504ec053 Update TextBox: Replace "spot" with "cursor".
Radek Brich <radek.brich@devl.cz>
parents: 97
diff changeset
   151
        self._update_cursor_x()
0
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   152
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
   153
    def move_top(self):
104
742e504ec053 Update TextBox: Replace "spot" with "cursor".
Radek Brich <radek.brich@devl.cz>
parents: 97
diff changeset
   154
        self._cursor.y = 0
742e504ec053 Update TextBox: Replace "spot" with "cursor".
Radek Brich <radek.brich@devl.cz>
parents: 97
diff changeset
   155
        self._update_cursor_x()
3
33ec838dc021 Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents: 0
diff changeset
   156
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
   157
    def move_bottom(self):
104
742e504ec053 Update TextBox: Replace "spot" with "cursor".
Radek Brich <radek.brich@devl.cz>
parents: 97
diff changeset
   158
        self._cursor.y = len(self._lines) - 1
742e504ec053 Update TextBox: Replace "spot" with "cursor".
Radek Brich <radek.brich@devl.cz>
parents: 97
diff changeset
   159
        self._update_cursor_x()
3
33ec838dc021 Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents: 0
diff changeset
   160
0
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   161
    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
   162
        ln = self.cur_line
104
742e504ec053 Update TextBox: Replace "spot" with "cursor".
Radek Brich <radek.brich@devl.cz>
parents: 97
diff changeset
   163
        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
   164
        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
   165
        self.cursor_column = sx
0
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   166
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
   167
    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
   168
        ln = self.cur_line
104
742e504ec053 Update TextBox: Replace "spot" with "cursor".
Radek Brich <radek.brich@devl.cz>
parents: 97
diff changeset
   169
        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
   170
        self.cur_line = ln[sx:]
104
742e504ec053 Update TextBox: Replace "spot" with "cursor".
Radek Brich <radek.brich@devl.cz>
parents: 97
diff changeset
   171
        self._lines.insert(self._cursor.y, ln[:sx])
96
68c562e0eb1f Add TextBox, text editor demo. Update demobase.
Radek Brich <radek.brich@devl.cz>
parents: 77
diff changeset
   172
        self._default_size.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
   173
        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
   174
            self.move_right()
0
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   175
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
   176
    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
   177
        ln = self.cur_line
104
742e504ec053 Update TextBox: Replace "spot" with "cursor".
Radek Brich <radek.brich@devl.cz>
parents: 97
diff changeset
   178
        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
   179
        self.cur_line = ln[sx:]
104
742e504ec053 Update TextBox: Replace "spot" with "cursor".
Radek Brich <radek.brich@devl.cz>
parents: 97
diff changeset
   180
        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
   181
        self.cursor_column = 0
104
742e504ec053 Update TextBox: Replace "spot" with "cursor".
Radek Brich <radek.brich@devl.cz>
parents: 97
diff changeset
   182
        self._cursor.x = 0
742e504ec053 Update TextBox: Replace "spot" with "cursor".
Radek Brich <radek.brich@devl.cz>
parents: 97
diff changeset
   183
        self._cursor.y += 1
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
   184
        w = max(self._default_size.w, len(ln[:sx] + text))
96
68c562e0eb1f Add TextBox, text editor demo. Update demobase.
Radek Brich <radek.brich@devl.cz>
parents: 77
diff changeset
   185
        self._default_size.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
   186
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
   187
    def backspace(self):
104
742e504ec053 Update TextBox: Replace "spot" with "cursor".
Radek Brich <radek.brich@devl.cz>
parents: 97
diff changeset
   188
        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
   189
            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
   190
            self.del_char()
0
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   191
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   192
    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
   193
        ln = self.cur_line
104
742e504ec053 Update TextBox: Replace "spot" with "cursor".
Radek Brich <radek.brich@devl.cz>
parents: 97
diff changeset
   194
        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
   195
        if sx == len(self.cur_line):
104
742e504ec053 Update TextBox: Replace "spot" with "cursor".
Radek Brich <radek.brich@devl.cz>
parents: 97
diff changeset
   196
            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
   197
                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
   198
                del self._lines[self._cursor.y+1]
96
68c562e0eb1f Add TextBox, text editor demo. Update demobase.
Radek Brich <radek.brich@devl.cz>
parents: 77
diff changeset
   199
                self._default_size.update(h=len(self._lines))
0
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   200
        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
   201
            self.cur_line = ln[:sx] + ln[sx+1:]
18
e6c3a5ee91aa Eliminate relative imports.
Radek Brich <radek.brich@devl.cz>
parents: 9
diff changeset
   202
104
742e504ec053 Update TextBox: Replace "spot" with "cursor".
Radek Brich <radek.brich@devl.cz>
parents: 97
diff changeset
   203
    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
   204
        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
   205
            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
   206
        else:
104
742e504ec053 Update TextBox: Replace "spot" with "cursor".
Radek Brich <radek.brich@devl.cz>
parents: 97
diff changeset
   207
            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
   208