tuikit/button.py
author Radek Brich <radek.brich@devl.cz>
Fri, 14 Dec 2012 10:20:14 +0100
changeset 32 088b92ffb119
parent 30 05500124d7fb
child 34 e3beacd5e536
permissions -rw-r--r--
Clean up, refactoring. Rename EventSource to Emitter, begin merging emit() method with handle().
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
18
e6c3a5ee91aa Eliminate relative imports.
Radek Brich <radek.brich@devl.cz>
parents: 16
diff changeset
     3
from tuikit.widget import Widget
0
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     4
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     5
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     6
class Button(Widget):
32
088b92ffb119 Clean up, refactoring. Rename EventSource to Emitter, begin merging emit() method with handle().
Radek Brich <radek.brich@devl.cz>
parents: 30
diff changeset
     7
13
19ebde2fd594 Add more generated documentation.
Radek Brich <radek.brich@devl.cz>
parents: 9
diff changeset
     8
    '''Clickable button.'''
32
088b92ffb119 Clean up, refactoring. Rename EventSource to Emitter, begin merging emit() method with handle().
Radek Brich <radek.brich@devl.cz>
parents: 30
diff changeset
     9
0
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    10
    def __init__(self, label=''):
13
19ebde2fd594 Add more generated documentation.
Radek Brich <radek.brich@devl.cz>
parents: 9
diff changeset
    11
        '''Create button with given label, size according to label.'''
16
8791a7da6835 Update VerticalLayout/HorizontalLayout. Add layout demo. Add Size, Borders to common. Update Coords, Rect.
Radek Brich <radek.brich@devl.cz>
parents: 13
diff changeset
    12
        w = len(label) + 4
8791a7da6835 Update VerticalLayout/HorizontalLayout. Add layout demo. Add Size, Borders to common. Update Coords, Rect.
Radek Brich <radek.brich@devl.cz>
parents: 13
diff changeset
    13
        h = 1
8791a7da6835 Update VerticalLayout/HorizontalLayout. Add layout demo. Add Size, Borders to common. Update Coords, Rect.
Radek Brich <radek.brich@devl.cz>
parents: 13
diff changeset
    14
        Widget.__init__(self, w, h)
0
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    15
13
19ebde2fd594 Add more generated documentation.
Radek Brich <radek.brich@devl.cz>
parents: 9
diff changeset
    16
        #: Button label.
0
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    17
        self.label = label
30
05500124d7fb Add setup script. Add Checkbox widget + demo. Updates.
Radek Brich <radek.brich@devl.cz>
parents: 18
diff changeset
    18
        #: Text or graphics to be added before label
05500124d7fb Add setup script. Add Checkbox widget + demo. Updates.
Radek Brich <radek.brich@devl.cz>
parents: 18
diff changeset
    19
        self.prefix = '['
05500124d7fb Add setup script. Add Checkbox widget + demo. Updates.
Radek Brich <radek.brich@devl.cz>
parents: 18
diff changeset
    20
        #: Text or graphics to be added after label
05500124d7fb Add setup script. Add Checkbox widget + demo. Updates.
Radek Brich <radek.brich@devl.cz>
parents: 18
diff changeset
    21
        self.suffix = ']'
05500124d7fb Add setup script. Add Checkbox widget + demo. Updates.
Radek Brich <radek.brich@devl.cz>
parents: 18
diff changeset
    22
        #: How should label be aligned if button has excess space - center | left | right
05500124d7fb Add setup script. Add Checkbox widget + demo. Updates.
Radek Brich <radek.brich@devl.cz>
parents: 18
diff changeset
    23
        self.align = 'center'
32
088b92ffb119 Clean up, refactoring. Rename EventSource to Emitter, begin merging emit() method with handle().
Radek Brich <radek.brich@devl.cz>
parents: 30
diff changeset
    24
7
d4a291b31cbb New color management - named colors.
Radek Brich <radek.brich@devl.cz>
parents: 2
diff changeset
    25
        self.bg = 'button'
d4a291b31cbb New color management - named colors.
Radek Brich <radek.brich@devl.cz>
parents: 2
diff changeset
    26
        self.bghi = 'button-active'
2
684cdc352562 Menu, Window and other improvements.
Radek Brich <radek.brich@devl.cz>
parents: 0
diff changeset
    27
        self.highlight = False
32
088b92ffb119 Clean up, refactoring. Rename EventSource to Emitter, begin merging emit() method with handle().
Radek Brich <radek.brich@devl.cz>
parents: 30
diff changeset
    28
16
8791a7da6835 Update VerticalLayout/HorizontalLayout. Add layout demo. Add Size, Borders to common. Update Coords, Rect.
Radek Brich <radek.brich@devl.cz>
parents: 13
diff changeset
    29
        # size
8791a7da6835 Update VerticalLayout/HorizontalLayout. Add layout demo. Add Size, Borders to common. Update Coords, Rect.
Radek Brich <radek.brich@devl.cz>
parents: 13
diff changeset
    30
        self.sizereq.w = w
8791a7da6835 Update VerticalLayout/HorizontalLayout. Add layout demo. Add Size, Borders to common. Update Coords, Rect.
Radek Brich <radek.brich@devl.cz>
parents: 13
diff changeset
    31
        self.sizereq.h = h
2
684cdc352562 Menu, Window and other improvements.
Radek Brich <radek.brich@devl.cz>
parents: 0
diff changeset
    32
0
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    33
        self.connect('draw', self.on_draw)
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    34
        self.connect('mousedown', self.on_mousedown)
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    35
        self.connect('mouseup', self.on_mouseup)
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
    36
        self.connect('keypress', self.on_keypress)
0
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    37
32
088b92ffb119 Clean up, refactoring. Rename EventSource to Emitter, begin merging emit() method with handle().
Radek Brich <radek.brich@devl.cz>
parents: 30
diff changeset
    38
        self.add_events('click')
0
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    39
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    40
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    41
    def on_draw(self, screen, x, y):
30
05500124d7fb Add setup script. Add Checkbox widget + demo. Updates.
Radek Brich <radek.brich@devl.cz>
parents: 18
diff changeset
    42
        pad = self.width - len(self.label) - len(self.prefix) - len(self.suffix)
05500124d7fb Add setup script. Add Checkbox widget + demo. Updates.
Radek Brich <radek.brich@devl.cz>
parents: 18
diff changeset
    43
        lpad, rpad = self._divide_padding(pad)
2
684cdc352562 Menu, Window and other improvements.
Radek Brich <radek.brich@devl.cz>
parents: 0
diff changeset
    44
        screen.pushcolor(self.getcolor())
30
05500124d7fb Add setup script. Add Checkbox widget + demo. Updates.
Radek Brich <radek.brich@devl.cz>
parents: 18
diff changeset
    45
        # prefix
05500124d7fb Add setup script. Add Checkbox widget + demo. Updates.
Radek Brich <radek.brich@devl.cz>
parents: 18
diff changeset
    46
        screen.puts(x, y, self.prefix)
05500124d7fb Add setup script. Add Checkbox widget + demo. Updates.
Radek Brich <radek.brich@devl.cz>
parents: 18
diff changeset
    47
        pos = len(self.prefix)
05500124d7fb Add setup script. Add Checkbox widget + demo. Updates.
Radek Brich <radek.brich@devl.cz>
parents: 18
diff changeset
    48
        # left pad
05500124d7fb Add setup script. Add Checkbox widget + demo. Updates.
Radek Brich <radek.brich@devl.cz>
parents: 18
diff changeset
    49
        screen.puts(x + pos, y, ' ' * lpad)
05500124d7fb Add setup script. Add Checkbox widget + demo. Updates.
Radek Brich <radek.brich@devl.cz>
parents: 18
diff changeset
    50
        pos += lpad
05500124d7fb Add setup script. Add Checkbox widget + demo. Updates.
Radek Brich <radek.brich@devl.cz>
parents: 18
diff changeset
    51
        # label
05500124d7fb Add setup script. Add Checkbox widget + demo. Updates.
Radek Brich <radek.brich@devl.cz>
parents: 18
diff changeset
    52
        screen.puts(x + pos, y, self.label)
05500124d7fb Add setup script. Add Checkbox widget + demo. Updates.
Radek Brich <radek.brich@devl.cz>
parents: 18
diff changeset
    53
        pos += len(self.label)
05500124d7fb Add setup script. Add Checkbox widget + demo. Updates.
Radek Brich <radek.brich@devl.cz>
parents: 18
diff changeset
    54
        # right pad
05500124d7fb Add setup script. Add Checkbox widget + demo. Updates.
Radek Brich <radek.brich@devl.cz>
parents: 18
diff changeset
    55
        screen.puts(x + pos, y, ' ' * rpad)
05500124d7fb Add setup script. Add Checkbox widget + demo. Updates.
Radek Brich <radek.brich@devl.cz>
parents: 18
diff changeset
    56
        pos += rpad
05500124d7fb Add setup script. Add Checkbox widget + demo. Updates.
Radek Brich <radek.brich@devl.cz>
parents: 18
diff changeset
    57
        # suffix
05500124d7fb Add setup script. Add Checkbox widget + demo. Updates.
Radek Brich <radek.brich@devl.cz>
parents: 18
diff changeset
    58
        screen.puts(x + pos, y, self.suffix)
0
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    59
        screen.popcolor()
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    60
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    61
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    62
    def on_mousedown(self, ev):
2
684cdc352562 Menu, Window and other improvements.
Radek Brich <radek.brich@devl.cz>
parents: 0
diff changeset
    63
        self.highlight = True
0
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    64
        self.redraw()
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    65
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    66
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    67
    def on_mouseup(self, ev):
2
684cdc352562 Menu, Window and other improvements.
Radek Brich <radek.brich@devl.cz>
parents: 0
diff changeset
    68
        self.highlight = False
0
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    69
        self.redraw()
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    70
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    71
        if self.enclose(ev.px, ev.py):
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    72
            self.handle('click')
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    73
2
684cdc352562 Menu, Window and other improvements.
Radek Brich <radek.brich@devl.cz>
parents: 0
diff changeset
    74
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
    75
    def on_keypress(self, keyname, char):
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
    76
        if keyname == 'enter':
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
    77
            self.handle('click')
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
    78
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
    79
2
684cdc352562 Menu, Window and other improvements.
Radek Brich <radek.brich@devl.cz>
parents: 0
diff changeset
    80
    def getcolor(self):
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
    81
        if self.highlight or self.hasfocus():
2
684cdc352562 Menu, Window and other improvements.
Radek Brich <radek.brich@devl.cz>
parents: 0
diff changeset
    82
            return self.bghi
684cdc352562 Menu, Window and other improvements.
Radek Brich <radek.brich@devl.cz>
parents: 0
diff changeset
    83
        return self.bg
684cdc352562 Menu, Window and other improvements.
Radek Brich <radek.brich@devl.cz>
parents: 0
diff changeset
    84
30
05500124d7fb Add setup script. Add Checkbox widget + demo. Updates.
Radek Brich <radek.brich@devl.cz>
parents: 18
diff changeset
    85
    def _divide_padding(self, pad):
05500124d7fb Add setup script. Add Checkbox widget + demo. Updates.
Radek Brich <radek.brich@devl.cz>
parents: 18
diff changeset
    86
        # default is 'left'
05500124d7fb Add setup script. Add Checkbox widget + demo. Updates.
Radek Brich <radek.brich@devl.cz>
parents: 18
diff changeset
    87
        lpad, rpad = 0, pad
05500124d7fb Add setup script. Add Checkbox widget + demo. Updates.
Radek Brich <radek.brich@devl.cz>
parents: 18
diff changeset
    88
        if self.align == 'center':
05500124d7fb Add setup script. Add Checkbox widget + demo. Updates.
Radek Brich <radek.brich@devl.cz>
parents: 18
diff changeset
    89
            lpad = pad // 2
05500124d7fb Add setup script. Add Checkbox widget + demo. Updates.
Radek Brich <radek.brich@devl.cz>
parents: 18
diff changeset
    90
            rpad = pad - lpad
05500124d7fb Add setup script. Add Checkbox widget + demo. Updates.
Radek Brich <radek.brich@devl.cz>
parents: 18
diff changeset
    91
        elif self.align == 'right':
05500124d7fb Add setup script. Add Checkbox widget + demo. Updates.
Radek Brich <radek.brich@devl.cz>
parents: 18
diff changeset
    92
            lpad, rpad = pad, 0
05500124d7fb Add setup script. Add Checkbox widget + demo. Updates.
Radek Brich <radek.brich@devl.cz>
parents: 18
diff changeset
    93
        return lpad, rpad
05500124d7fb Add setup script. Add Checkbox widget + demo. Updates.
Radek Brich <radek.brich@devl.cz>
parents: 18
diff changeset
    94