tuikit/editfield.py
author Radek Brich <radek.brich@devl.cz>
Sat, 29 Dec 2012 12:16:06 +0100
changeset 41 37b7dfc3eae6
parent 40 5faa38c10b67
child 45 43b2279b06e1
permissions -rw-r--r--
Update Emitter: All event handlers now have exactly one argument: object inherited from Event class, which carries any data.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     1
# -*- coding: utf-8 -*-
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     2
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     3
import locale
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     4
18
e6c3a5ee91aa Eliminate relative imports.
Radek Brich <radek.brich@devl.cz>
parents: 10
diff changeset
     5
from tuikit.widget import Widget
0
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     6
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     7
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     8
class EditField(Widget):
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     9
    def __init__(self, width=10, value=''):
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    10
        Widget.__init__(self, width, 1)
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    11
34
e3beacd5e536 Update event propagation, keypress event, focusing.
Radek Brich <radek.brich@devl.cz>
parents: 30
diff changeset
    12
        self.allow_focus = True
e3beacd5e536 Update event propagation, keypress event, focusing.
Radek Brich <radek.brich@devl.cz>
parents: 30
diff changeset
    13
0
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    14
        self.code = locale.getpreferredencoding()
10
ec1d47e6fe09 Use isinstance instead of type.
Radek Brich <radek.brich@devl.cz>
parents: 9
diff changeset
    15
        if not isinstance(value, str):
0
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    16
            value = str(value, self.code)
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    17
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    18
        self.value = value
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    19
        self.maxlen = None   # unlimited
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    20
8
fcaabd817774 Update editfield - place cursor to end initially.
Radek Brich <radek.brich@devl.cz>
parents: 5
diff changeset
    21
        self.tw = 0       # real width of text field (minus space for arrows)
fcaabd817774 Update editfield - place cursor to end initially.
Radek Brich <radek.brich@devl.cz>
parents: 5
diff changeset
    22
        self.pos = len(value)      # position of cursor in value
0
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    23
        self.ofs = 0      # position of value beginning on screen
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    24
41
37b7dfc3eae6 Update Emitter: All event handlers now have exactly one argument: object inherited from Event class, which carries any data.
Radek Brich <radek.brich@devl.cz>
parents: 40
diff changeset
    25
    def _handle_resize(self, ev):
37b7dfc3eae6 Update Emitter: All event handlers now have exactly one argument: object inherited from Event class, which carries any data.
Radek Brich <radek.brich@devl.cz>
parents: 40
diff changeset
    26
        super()._handle_resize(ev)
8
fcaabd817774 Update editfield - place cursor to end initially.
Radek Brich <radek.brich@devl.cz>
parents: 5
diff changeset
    27
        self.tw = self.width - 2
5
ae128c885d0f New GridLayout. Change cursor behavior (hide on unfocus event). Change resize event to propagate through containers. Change container clipping - allowlayout=false children are clipped without borders. More Widget doc.
Radek Brich <radek.brich@devl.cz>
parents: 0
diff changeset
    28
41
37b7dfc3eae6 Update Emitter: All event handlers now have exactly one argument: object inherited from Event class, which carries any data.
Radek Brich <radek.brich@devl.cz>
parents: 40
diff changeset
    29
    def _handle_draw(self, ev):
37b7dfc3eae6 Update Emitter: All event handlers now have exactly one argument: object inherited from Event class, which carries any data.
Radek Brich <radek.brich@devl.cz>
parents: 40
diff changeset
    30
        super()._handle_draw(ev)
37b7dfc3eae6 Update Emitter: All event handlers now have exactly one argument: object inherited from Event class, which carries any data.
Radek Brich <radek.brich@devl.cz>
parents: 40
diff changeset
    31
        ev.driver.pushcolor('normal')
0
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    32
        # draw value
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    33
        val = self.value + ' ' * self.tw         # add spaces to fill rest of field
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    34
        val = val[self.ofs : self.ofs + self.tw]  # cut value - begin from ofs, limit to tw chars
41
37b7dfc3eae6 Update Emitter: All event handlers now have exactly one argument: object inherited from Event class, which carries any data.
Radek Brich <radek.brich@devl.cz>
parents: 40
diff changeset
    35
        ev.driver.puts(ev.x + 1, ev.y, val.encode(self.code))
0
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    36
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    37
        # draw arrows if content overflows
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    38
        c = ' '
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    39
        if self.ofs > 0:
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    40
            c = '<'
41
37b7dfc3eae6 Update Emitter: All event handlers now have exactly one argument: object inherited from Event class, which carries any data.
Radek Brich <radek.brich@devl.cz>
parents: 40
diff changeset
    41
        ev.driver.putch(ev.x, ev.y, c)
0
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    42
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    43
        c = ' '
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    44
        if len(self.value[self.ofs:]) > self.tw:
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    45
            c = '>'
41
37b7dfc3eae6 Update Emitter: All event handlers now have exactly one argument: object inherited from Event class, which carries any data.
Radek Brich <radek.brich@devl.cz>
parents: 40
diff changeset
    46
        ev.driver.putch(ev.x + self.width-1, ev.y, c)
0
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    47
8
fcaabd817774 Update editfield - place cursor to end initially.
Radek Brich <radek.brich@devl.cz>
parents: 5
diff changeset
    48
        self.cursor = (1 + self.pos - self.ofs, 0)
41
37b7dfc3eae6 Update Emitter: All event handlers now have exactly one argument: object inherited from Event class, which carries any data.
Radek Brich <radek.brich@devl.cz>
parents: 40
diff changeset
    49
        ev.driver.popcolor()
0
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    50
41
37b7dfc3eae6 Update Emitter: All event handlers now have exactly one argument: object inherited from Event class, which carries any data.
Radek Brich <radek.brich@devl.cz>
parents: 40
diff changeset
    51
    def _handle_keypress(self, ev):
37b7dfc3eae6 Update Emitter: All event handlers now have exactly one argument: object inherited from Event class, which carries any data.
Radek Brich <radek.brich@devl.cz>
parents: 40
diff changeset
    52
        super()._handle_keypress(ev)
9
7175ed629a76 Added ComboBox, HorizontalLayout, TreeNode, TreeModel, TreeView. Widget is now descendant of EventSource. Improved color management (color prefixes).
Radek Brich <radek.brich@devl.cz>
parents: 8
diff changeset
    53
        handled = False
41
37b7dfc3eae6 Update Emitter: All event handlers now have exactly one argument: object inherited from Event class, which carries any data.
Radek Brich <radek.brich@devl.cz>
parents: 40
diff changeset
    54
        if ev.keyname:
9
7175ed629a76 Added ComboBox, HorizontalLayout, TreeNode, TreeModel, TreeView. Widget is now descendant of EventSource. Improved color management (color prefixes).
Radek Brich <radek.brich@devl.cz>
parents: 8
diff changeset
    55
            handled = True
41
37b7dfc3eae6 Update Emitter: All event handlers now have exactly one argument: object inherited from Event class, which carries any data.
Radek Brich <radek.brich@devl.cz>
parents: 40
diff changeset
    56
            if ev.keyname == 'left':
0
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    57
                self.move_left()
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    58
41
37b7dfc3eae6 Update Emitter: All event handlers now have exactly one argument: object inherited from Event class, which carries any data.
Radek Brich <radek.brich@devl.cz>
parents: 40
diff changeset
    59
            elif ev.keyname == 'right':
0
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    60
                self.move_right()
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    61
41
37b7dfc3eae6 Update Emitter: All event handlers now have exactly one argument: object inherited from Event class, which carries any data.
Radek Brich <radek.brich@devl.cz>
parents: 40
diff changeset
    62
            elif ev.keyname == 'backspace':
0
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    63
                if self.pos > 0:
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    64
                    self.move_left()
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    65
                    self.del_char()
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    66
41
37b7dfc3eae6 Update Emitter: All event handlers now have exactly one argument: object inherited from Event class, which carries any data.
Radek Brich <radek.brich@devl.cz>
parents: 40
diff changeset
    67
            elif ev.keyname == 'delete':
0
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    68
                self.del_char()
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    69
9
7175ed629a76 Added ComboBox, HorizontalLayout, TreeNode, TreeModel, TreeView. Widget is now descendant of EventSource. Improved color management (color prefixes).
Radek Brich <radek.brich@devl.cz>
parents: 8
diff changeset
    70
            else:
7175ed629a76 Added ComboBox, HorizontalLayout, TreeNode, TreeModel, TreeView. Widget is now descendant of EventSource. Improved color management (color prefixes).
Radek Brich <radek.brich@devl.cz>
parents: 8
diff changeset
    71
                handled = False
7175ed629a76 Added ComboBox, HorizontalLayout, TreeNode, TreeModel, TreeView. Widget is now descendant of EventSource. Improved color management (color prefixes).
Radek Brich <radek.brich@devl.cz>
parents: 8
diff changeset
    72
41
37b7dfc3eae6 Update Emitter: All event handlers now have exactly one argument: object inherited from Event class, which carries any data.
Radek Brich <radek.brich@devl.cz>
parents: 40
diff changeset
    73
        if ev.char:
37b7dfc3eae6 Update Emitter: All event handlers now have exactly one argument: object inherited from Event class, which carries any data.
Radek Brich <radek.brich@devl.cz>
parents: 40
diff changeset
    74
            self.add_char(ev.char)
0
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    75
            self.move_right()
9
7175ed629a76 Added ComboBox, HorizontalLayout, TreeNode, TreeModel, TreeView. Widget is now descendant of EventSource. Improved color management (color prefixes).
Radek Brich <radek.brich@devl.cz>
parents: 8
diff changeset
    76
            handled = True
0
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    77
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    78
        self.redraw()
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    79
9
7175ed629a76 Added ComboBox, HorizontalLayout, TreeNode, TreeModel, TreeView. Widget is now descendant of EventSource. Improved color management (color prefixes).
Radek Brich <radek.brich@devl.cz>
parents: 8
diff changeset
    80
        return handled
7175ed629a76 Added ComboBox, HorizontalLayout, TreeNode, TreeModel, TreeView. Widget is now descendant of EventSource. Improved color management (color prefixes).
Radek Brich <radek.brich@devl.cz>
parents: 8
diff changeset
    81
0
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    82
    def move_left(self):
8
fcaabd817774 Update editfield - place cursor to end initially.
Radek Brich <radek.brich@devl.cz>
parents: 5
diff changeset
    83
        if self.pos - self.ofs > 1 or (self.ofs == 0 and self.pos == 1):
0
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    84
            # move cursor
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    85
            self.pos -= 1
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    86
        else:
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    87
            # move content in field
8
fcaabd817774 Update editfield - place cursor to end initially.
Radek Brich <radek.brich@devl.cz>
parents: 5
diff changeset
    88
            if self.ofs > 0:
0
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    89
                self.pos -= 1
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    90
                self.ofs -= 1
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    91
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    92
    def move_right(self):
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    93
        if self.pos < len(self.value):
8
fcaabd817774 Update editfield - place cursor to end initially.
Radek Brich <radek.brich@devl.cz>
parents: 5
diff changeset
    94
            if self.pos - self.ofs < self.tw - 2 \
fcaabd817774 Update editfield - place cursor to end initially.
Radek Brich <radek.brich@devl.cz>
parents: 5
diff changeset
    95
            or (self.pos - self.ofs == self.tw - 2 and self.pos == len(self.value)-1):
0
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    96
                # move cursor
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    97
                self.pos += 1
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    98
            else:
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    99
                # move content in field
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   100
                self.pos += 1
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   101
                self.ofs += 1
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   102
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   103
    def add_char(self, c):
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   104
        self.value = self.value[:self.pos] + c + self.value[self.pos:]
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   105
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   106
    def del_char(self):
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   107
        self.value = self.value[:self.pos] + self.value[self.pos+1:]
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   108