tuikit/backend_curses.py
author Radek Brich <radek.brich@devl.cz>
Fri, 07 Oct 2011 11:07:59 +0200
changeset 18 e6c3a5ee91aa
parent 17 5be7cc43402e
child 20 472a753664f9
permissions -rw-r--r--
Eliminate relative imports.
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 curses
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     4
import curses.ascii
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     5
import locale
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     6
import logging
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     7
18
e6c3a5ee91aa Eliminate relative imports.
Radek Brich <radek.brich@devl.cz>
parents: 17
diff changeset
     8
from tuikit.common import Rect
0
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     9
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    10
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    11
class MouseEvent:
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    12
    def __init__(self, x=0, y=0):
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    13
        self.x = x   # global coordinates
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    14
        self.y = y
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    15
        self.wx = x  # local widget coordinates
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    16
        self.wy = y
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    17
        self.px = 0  # parent coordinates
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    18
        self.py = 0
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    19
        self.button = 0
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    20
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    21
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    22
    def childevent(self, child):
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    23
        ev = MouseEvent(self.x, self.y)
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    24
        # original local coordinates are new parent coordinates
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    25
        ev.px = self.wx
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    26
        ev.py = self.wy
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    27
        # update local coordinates
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    28
        ev.wx = self.wx - child.x
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    29
        ev.wy = self.wy - child.y
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    30
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    31
        return ev
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    32
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    33
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    34
class BackendCurses:
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    35
    xterm_codes = (
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    36
        (0x09,                      'tab'           ),
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    37
        (0x0a,                      'enter'         ),
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    38
        (0x7f,                      'backspace'     ),
4
d197ca00496f Add escape sequences for linux console.
Radek Brich <radek.brich@devl.cz>
parents: 3
diff changeset
    39
        (0x1b,                      'escape'        ),
0
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    40
        (0x1b,0x4f,0x50,            'f1'            ),
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    41
        (0x1b,0x4f,0x51,            'f2'            ),
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    42
        (0x1b,0x4f,0x52,            'f3'            ),
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    43
        (0x1b,0x4f,0x53,            'f4'            ),
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    44
        (0x1b,0x5b,0x31,0x35,0x7e,  'f5'            ),
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    45
        (0x1b,0x5b,0x31,0x37,0x7e,  'f6'            ),
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    46
        (0x1b,0x5b,0x31,0x38,0x7e,  'f7'            ),
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    47
        (0x1b,0x5b,0x31,0x39,0x7e,  'f8'            ),
4
d197ca00496f Add escape sequences for linux console.
Radek Brich <radek.brich@devl.cz>
parents: 3
diff changeset
    48
        (0x1b,0x5b,0x31,0x7e,       'home'          ),  # linux
0
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    49
        (0x1b,0x5b,0x32,0x30,0x7e,  'f9'            ),
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    50
        (0x1b,0x5b,0x32,0x31,0x7e,  'f10'           ),
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    51
        (0x1b,0x5b,0x32,0x33,0x7e,  'f11'           ),
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    52
        (0x1b,0x5b,0x32,0x34,0x7e,  'f12'           ),
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    53
        (0x1b,0x5b,0x32,0x7e,       'insert'        ),
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    54
        (0x1b,0x5b,0x33,0x7e,       'delete'        ),
4
d197ca00496f Add escape sequences for linux console.
Radek Brich <radek.brich@devl.cz>
parents: 3
diff changeset
    55
        (0x1b,0x5b,0x34,0x7e,       'end'           ),  # linux
0
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    56
        (0x1b,0x5b,0x35,0x7e,       'pageup'        ),
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    57
        (0x1b,0x5b,0x36,0x7e,       'pagedown'      ),
4
d197ca00496f Add escape sequences for linux console.
Radek Brich <radek.brich@devl.cz>
parents: 3
diff changeset
    58
        (0x1b,0x5b,0x41,            'up'            ),
d197ca00496f Add escape sequences for linux console.
Radek Brich <radek.brich@devl.cz>
parents: 3
diff changeset
    59
        (0x1b,0x5b,0x42,            'down'          ),
d197ca00496f Add escape sequences for linux console.
Radek Brich <radek.brich@devl.cz>
parents: 3
diff changeset
    60
        (0x1b,0x5b,0x43,            'right'         ),
d197ca00496f Add escape sequences for linux console.
Radek Brich <radek.brich@devl.cz>
parents: 3
diff changeset
    61
        (0x1b,0x5b,0x44,            'left'          ),
0
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    62
        (0x1b,0x5b,0x46,            'end'           ),
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    63
        (0x1b,0x5b,0x48,            'home'          ),
4
d197ca00496f Add escape sequences for linux console.
Radek Brich <radek.brich@devl.cz>
parents: 3
diff changeset
    64
        (0x1b,0x5b,0x4d,            'mouse'         ),
d197ca00496f Add escape sequences for linux console.
Radek Brich <radek.brich@devl.cz>
parents: 3
diff changeset
    65
        (0x1b,0x5b,0x5b,0x41,       'f1'            ),  # linux
d197ca00496f Add escape sequences for linux console.
Radek Brich <radek.brich@devl.cz>
parents: 3
diff changeset
    66
        (0x1b,0x5b,0x5b,0x42,       'f2'            ),  # linux
d197ca00496f Add escape sequences for linux console.
Radek Brich <radek.brich@devl.cz>
parents: 3
diff changeset
    67
        (0x1b,0x5b,0x5b,0x43,       'f3'            ),  # linux
d197ca00496f Add escape sequences for linux console.
Radek Brich <radek.brich@devl.cz>
parents: 3
diff changeset
    68
        (0x1b,0x5b,0x5b,0x44,       'f4'            ),  # linux
d197ca00496f Add escape sequences for linux console.
Radek Brich <radek.brich@devl.cz>
parents: 3
diff changeset
    69
        (0x1b,0x5b,0x5b,0x45,       'f5'            ),  # linux
0
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    70
    )
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    71
7
d4a291b31cbb New color management - named colors.
Radek Brich <radek.brich@devl.cz>
parents: 5
diff changeset
    72
    color_names = {
d4a291b31cbb New color management - named colors.
Radek Brich <radek.brich@devl.cz>
parents: 5
diff changeset
    73
        'black'   : curses.COLOR_BLACK,
d4a291b31cbb New color management - named colors.
Radek Brich <radek.brich@devl.cz>
parents: 5
diff changeset
    74
        'blue'    : curses.COLOR_BLUE,
d4a291b31cbb New color management - named colors.
Radek Brich <radek.brich@devl.cz>
parents: 5
diff changeset
    75
        'cyan'    : curses.COLOR_CYAN,
d4a291b31cbb New color management - named colors.
Radek Brich <radek.brich@devl.cz>
parents: 5
diff changeset
    76
        'green'   : curses.COLOR_GREEN,
d4a291b31cbb New color management - named colors.
Radek Brich <radek.brich@devl.cz>
parents: 5
diff changeset
    77
        'magenta' : curses.COLOR_MAGENTA,
d4a291b31cbb New color management - named colors.
Radek Brich <radek.brich@devl.cz>
parents: 5
diff changeset
    78
        'red'     : curses.COLOR_RED,
d4a291b31cbb New color management - named colors.
Radek Brich <radek.brich@devl.cz>
parents: 5
diff changeset
    79
        'white'   : curses.COLOR_WHITE,
d4a291b31cbb New color management - named colors.
Radek Brich <radek.brich@devl.cz>
parents: 5
diff changeset
    80
        'yellow'  : curses.COLOR_YELLOW,
d4a291b31cbb New color management - named colors.
Radek Brich <radek.brich@devl.cz>
parents: 5
diff changeset
    81
    }
d4a291b31cbb New color management - named colors.
Radek Brich <radek.brich@devl.cz>
parents: 5
diff changeset
    82
0
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    83
    def __init__(self, screen):
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    84
        self.screen = screen
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    85
        self.height, self.width = screen.getmaxyx()
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    86
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: 4
diff changeset
    87
        self.cursor = None
0
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    88
        self.clipstack = []
7
d4a291b31cbb New color management - named colors.
Radek Brich <radek.brich@devl.cz>
parents: 5
diff changeset
    89
d4a291b31cbb New color management - named colors.
Radek Brich <radek.brich@devl.cz>
parents: 5
diff changeset
    90
        self.colors = {}     # maps names to curses attributes
d4a291b31cbb New color management - named colors.
Radek Brich <radek.brich@devl.cz>
parents: 5
diff changeset
    91
        self.colorpairs = {} # maps tuple (fg,bg) to curses color_pair
d4a291b31cbb New color management - named colors.
Radek Brich <radek.brich@devl.cz>
parents: 5
diff changeset
    92
        self.colorstack = [] # pushcolor/popcolor puts or gets attributes from this
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: 7
diff changeset
    93
        self.colorprefix = [] # stack of color prefixes
7
d4a291b31cbb New color management - named colors.
Radek Brich <radek.brich@devl.cz>
parents: 5
diff changeset
    94
0
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    95
        self.inputqueue = []
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    96
        self.mbtnstack = []
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    97
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    98
        self.log = logging.getLogger('tuikit')
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    99
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   100
        # initialize curses
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   101
        curses.curs_set(False)
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   102
        curses.mousemask(curses.ALL_MOUSE_EVENTS | curses.REPORT_MOUSE_POSITION)
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   103
        curses.mouseinterval(0)  # do not wait to detect clicks, we use only press/release
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   104
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   105
        screen.immedok(0)
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   106
        screen.keypad(0)
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   107
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   108
        # http://en.wikipedia.org/wiki/List_of_Unicode_characters#Geometric_shapes
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   109
        self.UP_ARROW = '▲' #curses.ACS_UARROW
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   110
        self.DOWN_ARROW = '▼' #curses.ACS_DARROW
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   111
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   112
        # http://en.wikipedia.org/wiki/Box-drawing_characters
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   113
        self.LIGHT_SHADE = '░' #curses.ACS_BOARD
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   114
        self.MEDIUM_SHADE = '▒'
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   115
        self.DARK_SHADE = '▓'
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   116
        self.BLOCK = '█'
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   117
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   118
        self.COLUMN = '▁▂▃▄▅▆▇█'
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   119
        self.CORNER_ROUND = '╭╮╰╯'
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   120
        self.CORNER = '┌┐└┘'
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   121
        self.LINE = '─━│┃┄┅┆┇┈┉┊┋'
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   122
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   123
        self.HLINE = '─' # curses.ACS_HLINE
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   124
        self.VLINE = '│' # curses.ACS_VLINE
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   125
        self.ULCORNER = '┌' # curses.ACS_ULCORNER
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   126
        self.URCORNER = '┐' # curses.ACS_URCORNER
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   127
        self.LLCORNER = '└' # curses.ACS_LLCORNER
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   128
        self.LRCORNER = '┘' # curses.ACS_LRCORNER
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   129
        self.LTEE = '├'
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   130
        self.RTEE = '┤'
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   131
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   132
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   133
    ## clip operations ##
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   134
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   135
    def pushclip(self, x, y, w, h):
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   136
        newclip = Rect(x, y, w, h)
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   137
        if len(self.clipstack):
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   138
            oldclip = self.clipstack[-1]
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   139
            newclip = self.intersect(oldclip, newclip)
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   140
        self.clipstack.append(newclip)
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   141
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   142
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   143
    def popclip(self):
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   144
        self.clipstack.pop()
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   145
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   146
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   147
    def testclip(self, x, y):
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   148
        # no clip rectangle on stack => passed
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   149
        if not len(self.clipstack):
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   150
            return True
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   151
        # test against top clip rect from stack
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   152
        clip = self.clipstack[-1]
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   153
        if x < clip.x or y < clip.y \
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   154
        or x >= clip.x + clip.w or y >= clip.y + clip.h:
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   155
            return False
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   156
        # passed
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   157
        return True
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   158
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   159
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   160
    def intersect(self, r1, r2):
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   161
        x1 = max(r1.x, r2.x)
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   162
        y1 = max(r1.y, r2.y)
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   163
        x2 = min(r1.x + r1.w, r2.x + r2.w)
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   164
        y2 = min(r1.y + r1.h, r2.y + r2.h)
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   165
        if x1 >= x2 or y1 >= y2:
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   166
            return Rect()
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   167
        return Rect(x1, y1, x2-x1, y2-y1)
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   168
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   169
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   170
    def union(self, r1, r2):
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   171
        x = min(r1.x, r2.x)
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   172
        y = min(r1.y, r2.y)
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   173
        w = max(r1.x + r1.w, r2.x + r2.w) - x
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   174
        h = max(r1.y + r1.h, r2.y + r2.h) - y
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   175
        return Rect(x, y, w, h)
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   176
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   177
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: 7
diff changeset
   178
    ## colors, attributes ##
0
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   179
7
d4a291b31cbb New color management - named colors.
Radek Brich <radek.brich@devl.cz>
parents: 5
diff changeset
   180
    def _parsecolor(self, name):
d4a291b31cbb New color management - named colors.
Radek Brich <radek.brich@devl.cz>
parents: 5
diff changeset
   181
        name = name.lower().strip()
d4a291b31cbb New color management - named colors.
Radek Brich <radek.brich@devl.cz>
parents: 5
diff changeset
   182
        return self.color_names[name]
d4a291b31cbb New color management - named colors.
Radek Brich <radek.brich@devl.cz>
parents: 5
diff changeset
   183
d4a291b31cbb New color management - named colors.
Radek Brich <radek.brich@devl.cz>
parents: 5
diff changeset
   184
d4a291b31cbb New color management - named colors.
Radek Brich <radek.brich@devl.cz>
parents: 5
diff changeset
   185
    def _getcolorpair(self, fg, bg):
d4a291b31cbb New color management - named colors.
Radek Brich <radek.brich@devl.cz>
parents: 5
diff changeset
   186
        pair = (fg, bg)
d4a291b31cbb New color management - named colors.
Radek Brich <radek.brich@devl.cz>
parents: 5
diff changeset
   187
        if pair in self.colorpairs:
d4a291b31cbb New color management - named colors.
Radek Brich <radek.brich@devl.cz>
parents: 5
diff changeset
   188
            return self.colorpairs[pair]
d4a291b31cbb New color management - named colors.
Radek Brich <radek.brich@devl.cz>
parents: 5
diff changeset
   189
        num = len(self.colorpairs) + 1
d4a291b31cbb New color management - named colors.
Radek Brich <radek.brich@devl.cz>
parents: 5
diff changeset
   190
        curses.init_pair(num, fg, bg)
d4a291b31cbb New color management - named colors.
Radek Brich <radek.brich@devl.cz>
parents: 5
diff changeset
   191
        self.colorpairs[pair] = num
d4a291b31cbb New color management - named colors.
Radek Brich <radek.brich@devl.cz>
parents: 5
diff changeset
   192
        return num
d4a291b31cbb New color management - named colors.
Radek Brich <radek.brich@devl.cz>
parents: 5
diff changeset
   193
d4a291b31cbb New color management - named colors.
Radek Brich <radek.brich@devl.cz>
parents: 5
diff changeset
   194
d4a291b31cbb New color management - named colors.
Radek Brich <radek.brich@devl.cz>
parents: 5
diff changeset
   195
    def _parseattrs(self, attrs):
d4a291b31cbb New color management - named colors.
Radek Brich <radek.brich@devl.cz>
parents: 5
diff changeset
   196
        res = 0
d4a291b31cbb New color management - named colors.
Radek Brich <radek.brich@devl.cz>
parents: 5
diff changeset
   197
        for a in attrs:
d4a291b31cbb New color management - named colors.
Radek Brich <radek.brich@devl.cz>
parents: 5
diff changeset
   198
            a = a.lower().strip()
d4a291b31cbb New color management - named colors.
Radek Brich <radek.brich@devl.cz>
parents: 5
diff changeset
   199
            trans = {
d4a291b31cbb New color management - named colors.
Radek Brich <radek.brich@devl.cz>
parents: 5
diff changeset
   200
                'blink'     : curses.A_BLINK,
d4a291b31cbb New color management - named colors.
Radek Brich <radek.brich@devl.cz>
parents: 5
diff changeset
   201
                'bold'      : curses.A_BOLD,
d4a291b31cbb New color management - named colors.
Radek Brich <radek.brich@devl.cz>
parents: 5
diff changeset
   202
                'dim'       : curses.A_DIM,
d4a291b31cbb New color management - named colors.
Radek Brich <radek.brich@devl.cz>
parents: 5
diff changeset
   203
                'standout'  : curses.A_STANDOUT,
d4a291b31cbb New color management - named colors.
Radek Brich <radek.brich@devl.cz>
parents: 5
diff changeset
   204
                'underline' : curses.A_UNDERLINE,
d4a291b31cbb New color management - named colors.
Radek Brich <radek.brich@devl.cz>
parents: 5
diff changeset
   205
            }
d4a291b31cbb New color management - named colors.
Radek Brich <radek.brich@devl.cz>
parents: 5
diff changeset
   206
            res = res | trans[a]
d4a291b31cbb New color management - named colors.
Radek Brich <radek.brich@devl.cz>
parents: 5
diff changeset
   207
        return res
d4a291b31cbb New color management - named colors.
Radek Brich <radek.brich@devl.cz>
parents: 5
diff changeset
   208
d4a291b31cbb New color management - named colors.
Radek Brich <radek.brich@devl.cz>
parents: 5
diff changeset
   209
d4a291b31cbb New color management - named colors.
Radek Brich <radek.brich@devl.cz>
parents: 5
diff changeset
   210
    def setcolor(self, name, desc):
d4a291b31cbb New color management - named colors.
Radek Brich <radek.brich@devl.cz>
parents: 5
diff changeset
   211
        parts = desc.split(',')
d4a291b31cbb New color management - named colors.
Radek Brich <radek.brich@devl.cz>
parents: 5
diff changeset
   212
        fg, bg = parts[0].split(' on ')
d4a291b31cbb New color management - named colors.
Radek Brich <radek.brich@devl.cz>
parents: 5
diff changeset
   213
        attrs = parts[1:]
d4a291b31cbb New color management - named colors.
Radek Brich <radek.brich@devl.cz>
parents: 5
diff changeset
   214
        fg = self._parsecolor(fg)
d4a291b31cbb New color management - named colors.
Radek Brich <radek.brich@devl.cz>
parents: 5
diff changeset
   215
        bg = self._parsecolor(bg)
d4a291b31cbb New color management - named colors.
Radek Brich <radek.brich@devl.cz>
parents: 5
diff changeset
   216
        col = self._getcolorpair(fg, bg)
d4a291b31cbb New color management - named colors.
Radek Brich <radek.brich@devl.cz>
parents: 5
diff changeset
   217
        attr = self._parseattrs(attrs)
d4a291b31cbb New color management - named colors.
Radek Brich <radek.brich@devl.cz>
parents: 5
diff changeset
   218
        self.colors[name] = curses.color_pair(col) | attr
d4a291b31cbb New color management - named colors.
Radek Brich <radek.brich@devl.cz>
parents: 5
diff changeset
   219
d4a291b31cbb New color management - named colors.
Radek Brich <radek.brich@devl.cz>
parents: 5
diff changeset
   220
d4a291b31cbb New color management - named colors.
Radek Brich <radek.brich@devl.cz>
parents: 5
diff changeset
   221
    def pushcolor(self, name):
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: 7
diff changeset
   222
        # add prefix if available
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: 7
diff changeset
   223
        if len(self.colorprefix):
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: 7
diff changeset
   224
            prefixname = self.colorprefix[-1] + name
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: 7
diff changeset
   225
            if prefixname in self.colors:
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: 7
diff changeset
   226
                name = prefixname
7
d4a291b31cbb New color management - named colors.
Radek Brich <radek.brich@devl.cz>
parents: 5
diff changeset
   227
        attr = self.colors[name]
d4a291b31cbb New color management - named colors.
Radek Brich <radek.brich@devl.cz>
parents: 5
diff changeset
   228
        self.screen.attrset(attr)
d4a291b31cbb New color management - named colors.
Radek Brich <radek.brich@devl.cz>
parents: 5
diff changeset
   229
        self.colorstack.append(attr)
0
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   230
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   231
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   232
    def popcolor(self):
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   233
        self.colorstack.pop()
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   234
        if len(self.colorstack):
7
d4a291b31cbb New color management - named colors.
Radek Brich <radek.brich@devl.cz>
parents: 5
diff changeset
   235
            attr = self.colorstack[-1]
0
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   236
        else:
7
d4a291b31cbb New color management - named colors.
Radek Brich <radek.brich@devl.cz>
parents: 5
diff changeset
   237
            attr = 0
d4a291b31cbb New color management - named colors.
Radek Brich <radek.brich@devl.cz>
parents: 5
diff changeset
   238
        self.screen.attrset(attr)
0
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   239
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   240
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: 7
diff changeset
   241
    def pushcolorprefix(self, name):
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: 7
diff changeset
   242
        self.colorprefix.append(name)
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: 7
diff changeset
   243
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: 7
diff changeset
   244
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: 7
diff changeset
   245
    def popcolorprefix(self):
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: 7
diff changeset
   246
        self.colorprefix.pop()
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: 7
diff changeset
   247
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: 7
diff changeset
   248
0
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   249
    ## drawing ##
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   250
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   251
    def putch(self, x, y, c):
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   252
        if not self.testclip(x, y):
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   253
            return
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   254
        try:
10
ec1d47e6fe09 Use isinstance instead of type.
Radek Brich <radek.brich@devl.cz>
parents: 9
diff changeset
   255
            if isinstance(c, str) and len(c) == 1:
0
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   256
                self.screen.addstr(y, x, c)
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   257
            else:
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   258
                self.screen.addch(y, x, c)
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   259
        except curses.error:
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   260
            pass
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   261
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   262
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   263
    def puts(self, x, y, s):
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   264
        for c in s:
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   265
            self.putch(x, y, c)
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   266
            x += 1
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   267
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   268
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   269
    def hline(self, x, y, w, c=' '):
10
ec1d47e6fe09 Use isinstance instead of type.
Radek Brich <radek.brich@devl.cz>
parents: 9
diff changeset
   270
        if isinstance(c, str):
0
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   271
            s = c*w
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   272
        else:
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   273
            s = [c]*w
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   274
        self.puts(x, y, s)
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   275
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   276
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   277
    def vline(self, x, y, h, c=' '):
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   278
        for i in range(h):
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   279
            self.putch(x, y+i, c)
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   280
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   281
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   282
    def frame(self, x, y, w, h):
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   283
        self.putch(x, y, self.ULCORNER)
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   284
        self.putch(x+w-1, y, self.URCORNER)
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   285
        self.putch(x, y+h-1, self.LLCORNER)
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   286
        self.putch(x+w-1, y+h-1, self.LRCORNER)
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   287
        self.hline(x+1, y, w-2, self.HLINE)
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   288
        self.hline(x+1, y+h-1, w-2, self.HLINE)
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   289
        self.vline(x, y+1, h-2, self.VLINE)
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   290
        self.vline(x+w-1, y+1, h-2, self.VLINE)
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   291
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   292
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   293
    def fill(self, x, y, w, h, c=' '):
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   294
        for i in range(h):
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   295
            self.hline(x, y + i, w, c)
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   296
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   297
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   298
    def erase(self):
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   299
        self.screen.erase()
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   300
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   301
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   302
    def commit(self):
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   303
        if self.cursor:
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   304
            self.screen.move(*self.cursor)
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   305
            curses.curs_set(True)
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   306
        else:
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   307
            curses.curs_set(False)
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   308
        self.screen.refresh()
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   309
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   310
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   311
    ## cursor ##
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   312
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   313
    def showcursor(self, x, y):
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   314
        if not self.testclip(x, y):
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   315
            return
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   316
        self.cursor = (y, x)
7
d4a291b31cbb New color management - named colors.
Radek Brich <radek.brich@devl.cz>
parents: 5
diff changeset
   317
d4a291b31cbb New color management - named colors.
Radek Brich <radek.brich@devl.cz>
parents: 5
diff changeset
   318
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: 4
diff changeset
   319
    def hidecursor(self):
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: 4
diff changeset
   320
        curses.curs_set(False)
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: 4
diff changeset
   321
        self.cursor = None
0
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   322
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   323
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   324
    ## input ##
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   325
3
33ec838dc021 Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents: 2
diff changeset
   326
    def inputqueue_fill(self, timeout=None):
33ec838dc021 Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents: 2
diff changeset
   327
        if timeout is None:
33ec838dc021 Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents: 2
diff changeset
   328
            # wait indefinitely
33ec838dc021 Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents: 2
diff changeset
   329
            c = self.screen.getch()
33ec838dc021 Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents: 2
diff changeset
   330
            self.inputqueue.insert(0, c)
33ec838dc021 Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents: 2
diff changeset
   331
33ec838dc021 Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents: 2
diff changeset
   332
        elif timeout > 0:
33ec838dc021 Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents: 2
diff changeset
   333
            # wait
33ec838dc021 Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents: 2
diff changeset
   334
            curses.halfdelay(timeout)
33ec838dc021 Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents: 2
diff changeset
   335
            c = self.screen.getch()
33ec838dc021 Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents: 2
diff changeset
   336
            curses.cbreak()
33ec838dc021 Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents: 2
diff changeset
   337
            if c == -1:
33ec838dc021 Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents: 2
diff changeset
   338
                return
33ec838dc021 Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents: 2
diff changeset
   339
            self.inputqueue.insert(0, c)
33ec838dc021 Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents: 2
diff changeset
   340
33ec838dc021 Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents: 2
diff changeset
   341
        # timeout = 0 -> no wait
33ec838dc021 Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents: 2
diff changeset
   342
0
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   343
        self.screen.nodelay(1)
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   344
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   345
        while True:
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   346
            c = self.screen.getch()
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   347
            if c == -1:
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   348
                break
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   349
            self.inputqueue.insert(0, c)
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   350
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   351
        self.screen.nodelay(0)
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   352
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   353
3
33ec838dc021 Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents: 2
diff changeset
   354
    def inputqueue_top(self, num=0):
33ec838dc021 Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents: 2
diff changeset
   355
        return self.inputqueue[-1-num]
33ec838dc021 Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents: 2
diff changeset
   356
33ec838dc021 Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents: 2
diff changeset
   357
33ec838dc021 Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents: 2
diff changeset
   358
    def inputqueue_get(self):
33ec838dc021 Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents: 2
diff changeset
   359
        c = None
33ec838dc021 Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents: 2
diff changeset
   360
        try:
33ec838dc021 Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents: 2
diff changeset
   361
            c = self.inputqueue.pop()
33ec838dc021 Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents: 2
diff changeset
   362
        except IndexError:
33ec838dc021 Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents: 2
diff changeset
   363
            pass
33ec838dc021 Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents: 2
diff changeset
   364
        return c
33ec838dc021 Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents: 2
diff changeset
   365
33ec838dc021 Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents: 2
diff changeset
   366
33ec838dc021 Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents: 2
diff changeset
   367
    def inputqueue_get_wait(self):
0
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   368
        c = None
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   369
        while c is None:
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   370
            try:
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   371
                c = self.inputqueue.pop()
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   372
            except IndexError:
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   373
                curses.napms(25)
3
33ec838dc021 Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents: 2
diff changeset
   374
                self.inputqueue_fill(0)
0
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   375
        return c
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   376
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   377
3
33ec838dc021 Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents: 2
diff changeset
   378
    def inputqueue_unget(self, c):
33ec838dc021 Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents: 2
diff changeset
   379
        self.inputqueue.append(c)
33ec838dc021 Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents: 2
diff changeset
   380
33ec838dc021 Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents: 2
diff changeset
   381
0
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   382
    def process_input(self, timeout=None):
3
33ec838dc021 Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents: 2
diff changeset
   383
        # empty queue -> fill
33ec838dc021 Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents: 2
diff changeset
   384
        if len(self.inputqueue) == 0:
33ec838dc021 Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents: 2
diff changeset
   385
            self.inputqueue_fill(timeout)
0
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   386
3
33ec838dc021 Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents: 2
diff changeset
   387
        res = []
33ec838dc021 Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents: 2
diff changeset
   388
        while len(self.inputqueue):
33ec838dc021 Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents: 2
diff changeset
   389
            c = self.inputqueue_get()
33ec838dc021 Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents: 2
diff changeset
   390
33ec838dc021 Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents: 2
diff changeset
   391
            if c == curses.KEY_MOUSE:
33ec838dc021 Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents: 2
diff changeset
   392
                res += self.process_mouse()
17
5be7cc43402e Handle curses resize event.
Radek Brich <radek.brich@devl.cz>
parents: 10
diff changeset
   393
            
5be7cc43402e Handle curses resize event.
Radek Brich <radek.brich@devl.cz>
parents: 10
diff changeset
   394
            elif c == curses.KEY_RESIZE:
5be7cc43402e Handle curses resize event.
Radek Brich <radek.brich@devl.cz>
parents: 10
diff changeset
   395
                self.height, self.width = self.screen.getmaxyx()
5be7cc43402e Handle curses resize event.
Radek Brich <radek.brich@devl.cz>
parents: 10
diff changeset
   396
                res.append(('resize',))
0
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   397
3
33ec838dc021 Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents: 2
diff changeset
   398
            elif curses.ascii.isctrl(c):
33ec838dc021 Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents: 2
diff changeset
   399
                self.inputqueue_unget(c)
33ec838dc021 Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents: 2
diff changeset
   400
                res += self.process_control_chars()
33ec838dc021 Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents: 2
diff changeset
   401
33ec838dc021 Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents: 2
diff changeset
   402
            elif c >= 192 and c <= 255:
33ec838dc021 Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents: 2
diff changeset
   403
                self.inputqueue_unget(c)
33ec838dc021 Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents: 2
diff changeset
   404
                res += self.process_utf8_chars()
0
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   405
3
33ec838dc021 Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents: 2
diff changeset
   406
            elif curses.ascii.isprint(c):
33ec838dc021 Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents: 2
diff changeset
   407
                res += [('keypress', None, str(chr(c)))]
0
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   408
3
33ec838dc021 Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents: 2
diff changeset
   409
            else:
33ec838dc021 Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents: 2
diff changeset
   410
                #self.top.keypress(None, unicode(chr(c)))
33ec838dc021 Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents: 2
diff changeset
   411
                self.inputqueue_unget(c)
33ec838dc021 Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents: 2
diff changeset
   412
                res += self.process_control_chars()
0
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   413
3
33ec838dc021 Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents: 2
diff changeset
   414
        return res
0
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   415
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   416
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   417
    def process_mouse(self):
4
d197ca00496f Add escape sequences for linux console.
Radek Brich <radek.brich@devl.cz>
parents: 3
diff changeset
   418
        try:
d197ca00496f Add escape sequences for linux console.
Radek Brich <radek.brich@devl.cz>
parents: 3
diff changeset
   419
            id, x, y, z, bstate = curses.getmouse()
d197ca00496f Add escape sequences for linux console.
Radek Brich <radek.brich@devl.cz>
parents: 3
diff changeset
   420
        except curses.error:
d197ca00496f Add escape sequences for linux console.
Radek Brich <radek.brich@devl.cz>
parents: 3
diff changeset
   421
            return []
d197ca00496f Add escape sequences for linux console.
Radek Brich <radek.brich@devl.cz>
parents: 3
diff changeset
   422
0
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   423
        ev = MouseEvent(x, y)
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   424
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   425
        out = []
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   426
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   427
        if bstate & curses.REPORT_MOUSE_POSITION:
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   428
            out += [('mousemove', ev)]
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   429
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   430
        if bstate & curses.BUTTON1_PRESSED:
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   431
            ev.button = 1
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   432
            out += [('mousedown', ev)]
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   433
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   434
        if bstate & curses.BUTTON3_PRESSED:
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   435
            ev.button = 3
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   436
            out += [('mousedown', ev)]
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   437
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   438
        if bstate & curses.BUTTON1_RELEASED:
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   439
            ev.button = 1
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   440
            out += [('mouseup', ev)]
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   441
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   442
        if bstate & curses.BUTTON3_RELEASED:
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   443
            ev.button = 3
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   444
            out += [('mouseup', ev)]
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   445
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   446
        return out
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   447
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   448
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   449
    def process_utf8_chars(self):
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   450
        #FIXME read exact number of chars as defined by utf-8
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   451
        utf = ''
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   452
        while len(utf) <= 6:
3
33ec838dc021 Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents: 2
diff changeset
   453
            c = self.inputqueue_get_wait()
0
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   454
            utf += chr(c)
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   455
            try:
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   456
                uni = str(utf, 'utf-8')
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   457
                return [('keypress', None, uni)]
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   458
            except UnicodeDecodeError:
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   459
                continue
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   460
        raise Exception('Invalid UTF-8 sequence: %r' % utf)
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   461
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   462
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   463
    def process_control_chars(self):
3
33ec838dc021 Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents: 2
diff changeset
   464
        codes = self.xterm_codes
33ec838dc021 Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents: 2
diff changeset
   465
        matchingcodes = []
33ec838dc021 Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents: 2
diff changeset
   466
        match = None
33ec838dc021 Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents: 2
diff changeset
   467
        consumed = []
33ec838dc021 Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents: 2
diff changeset
   468
33ec838dc021 Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents: 2
diff changeset
   469
        # consume next char, filter out matching codes
33ec838dc021 Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents: 2
diff changeset
   470
        c = self.inputqueue_get_wait()
33ec838dc021 Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents: 2
diff changeset
   471
        consumed.append(c)
33ec838dc021 Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents: 2
diff changeset
   472
33ec838dc021 Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents: 2
diff changeset
   473
        while True:
33ec838dc021 Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents: 2
diff changeset
   474
            self.log.debug('c=%s len=%s', c, len(codes))
33ec838dc021 Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents: 2
diff changeset
   475
            for code in codes:
33ec838dc021 Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents: 2
diff changeset
   476
                if c == code[len(consumed)-1]:
33ec838dc021 Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents: 2
diff changeset
   477
                    if len(code) - 1 == len(consumed):
33ec838dc021 Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents: 2
diff changeset
   478
                        match = code
33ec838dc021 Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents: 2
diff changeset
   479
                    else:
33ec838dc021 Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents: 2
diff changeset
   480
                        matchingcodes += [code]
33ec838dc021 Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents: 2
diff changeset
   481
33ec838dc021 Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents: 2
diff changeset
   482
            self.log.debug('matching=%s', len(matchingcodes))
33ec838dc021 Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents: 2
diff changeset
   483
33ec838dc021 Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents: 2
diff changeset
   484
            # match found, or no matching code found -> stop
33ec838dc021 Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents: 2
diff changeset
   485
            if len(matchingcodes) == 0:
33ec838dc021 Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents: 2
diff changeset
   486
                break
0
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   487
3
33ec838dc021 Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents: 2
diff changeset
   488
            # match found and some sequencies still match -> continue
33ec838dc021 Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents: 2
diff changeset
   489
            if len(matchingcodes) > 0:
33ec838dc021 Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents: 2
diff changeset
   490
                if len(self.inputqueue) == 0:
33ec838dc021 Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents: 2
diff changeset
   491
                    self.inputqueue_fill(1)
33ec838dc021 Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents: 2
diff changeset
   492
33ec838dc021 Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents: 2
diff changeset
   493
            c = self.inputqueue_get()
33ec838dc021 Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents: 2
diff changeset
   494
            if c:
33ec838dc021 Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents: 2
diff changeset
   495
                consumed.append(c)
33ec838dc021 Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents: 2
diff changeset
   496
                codes = matchingcodes
33ec838dc021 Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents: 2
diff changeset
   497
                matchingcodes = []
33ec838dc021 Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents: 2
diff changeset
   498
            else:
33ec838dc021 Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents: 2
diff changeset
   499
                break
0
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   500
3
33ec838dc021 Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents: 2
diff changeset
   501
        keyname = None
33ec838dc021 Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents: 2
diff changeset
   502
        if match:
33ec838dc021 Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents: 2
diff changeset
   503
            # compare match to consumed, return unused chars
33ec838dc021 Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents: 2
diff changeset
   504
            l = len(match) - 1
33ec838dc021 Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents: 2
diff changeset
   505
            while len(consumed) > l:
33ec838dc021 Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents: 2
diff changeset
   506
                self.inputqueue_unget(consumed[-1])
33ec838dc021 Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents: 2
diff changeset
   507
                del consumed[-1]
33ec838dc021 Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents: 2
diff changeset
   508
            keyname = match[-1]
33ec838dc021 Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents: 2
diff changeset
   509
33ec838dc021 Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents: 2
diff changeset
   510
        if match is None:
0
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   511
            self.log.debug('Unknown control sequence: %s',
3
33ec838dc021 Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents: 2
diff changeset
   512
                ','.join(['0x%x'%x for x in consumed]))
33ec838dc021 Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents: 2
diff changeset
   513
            return [('keypress', 'Unknown', None)]
0
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   514
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   515
        if keyname == 'mouse':
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   516
           return self.process_xterm_mouse()
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   517
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   518
        return [('keypress', keyname, None)]
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   519
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   520
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   521
    def process_xterm_mouse(self):
3
33ec838dc021 Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents: 2
diff changeset
   522
        t = self.inputqueue_get_wait()
33ec838dc021 Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents: 2
diff changeset
   523
        x = self.inputqueue_get_wait() - 0x21
33ec838dc021 Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents: 2
diff changeset
   524
        y = self.inputqueue_get_wait() - 0x21
0
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   525
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   526
        ev = MouseEvent(x, y)
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   527
        out = []
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   528
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   529
        if t in (0x20, 0x21, 0x22): # button press
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   530
            btn = t - 0x1f
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   531
            ev.button = btn
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   532
            if not btn in self.mbtnstack:
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   533
                self.mbtnstack.append(btn)
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   534
                out += [('mousedown', ev)]
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   535
            else:
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   536
                out += [('mousemove', ev)]
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   537
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   538
        elif t == 0x23: # button release
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   539
            ev.button = self.mbtnstack.pop()
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   540
            out += [('mouseup', ev)]
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   541
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   542
        elif t in (0x60, 0x61): # wheel up, down
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   543
            ev.button = 4 + t - 0x60
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   544
            out += [('mousewheel', ev)]
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   545
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   546
        else:
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   547
            raise Exception('Unknown mouse event: %x' % t)
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   548
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   549
        return out