author | Radek Brich <radek.brich@devl.cz> |
Mon, 01 Sep 2014 08:45:51 +0200 | |
changeset 104 | 742e504ec053 |
parent 97 | 0c2e0c09ba5c |
child 105 | 70c9cfc70cd6 |
permissions | -rw-r--r-- |
104
742e504ec053
Update TextBox: Replace "spot" with "cursor".
Radek Brich <radek.brich@devl.cz>
parents:
97
diff
changeset
|
1 |
from tuikit.core.coords import Point, Size, Rect |
89 | 2 |
from tuikit.core.theme import default_theme |
87
ee5ea9671f28
Add core Widget, Container. Add widgets Label.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
3 |
|
94
e50dae408fe9
Add origin to Buffer. Use it to simplify hierarchical drawing.
Radek Brich <radek.brich@devl.cz>
parents:
93
diff
changeset
|
4 |
import logging |
e50dae408fe9
Add origin to Buffer. Use it to simplify hierarchical drawing.
Radek Brich <radek.brich@devl.cz>
parents:
93
diff
changeset
|
5 |
|
87
ee5ea9671f28
Add core Widget, Container. Add widgets Label.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
6 |
|
ee5ea9671f28
Add core Widget, Container. Add widgets Label.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
7 |
class Widget: |
ee5ea9671f28
Add core Widget, Container. Add widgets Label.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
8 |
|
ee5ea9671f28
Add core Widget, Container. Add widgets Label.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
9 |
"""Base class for all widgets.""" |
ee5ea9671f28
Add core Widget, Container. Add widgets Label.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
10 |
|
ee5ea9671f28
Add core Widget, Container. Add widgets Label.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
11 |
_num_instances = 0 |
ee5ea9671f28
Add core Widget, Container. Add widgets Label.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
12 |
|
ee5ea9671f28
Add core Widget, Container. Add widgets Label.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
13 |
def __init__(self): |
94
e50dae408fe9
Add origin to Buffer. Use it to simplify hierarchical drawing.
Radek Brich <radek.brich@devl.cz>
parents:
93
diff
changeset
|
14 |
self._num_instances += 1 |
e50dae408fe9
Add origin to Buffer. Use it to simplify hierarchical drawing.
Radek Brich <radek.brich@devl.cz>
parents:
93
diff
changeset
|
15 |
self._log = logging.getLogger(__name__) |
e50dae408fe9
Add origin to Buffer. Use it to simplify hierarchical drawing.
Radek Brich <radek.brich@devl.cz>
parents:
93
diff
changeset
|
16 |
|
87
ee5ea9671f28
Add core Widget, Container. Add widgets Label.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
17 |
#: Widget name is used for logging etc. Not visible anywhere. |
ee5ea9671f28
Add core Widget, Container. Add widgets Label.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
18 |
self.name = '{}{}'.format( |
ee5ea9671f28
Add core Widget, Container. Add widgets Label.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
19 |
self.__class__.__name__, |
ee5ea9671f28
Add core Widget, Container. Add widgets Label.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
20 |
self._num_instances) |
ee5ea9671f28
Add core Widget, Container. Add widgets Label.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
21 |
|
ee5ea9671f28
Add core Widget, Container. Add widgets Label.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
22 |
#: Parent Widget |
ee5ea9671f28
Add core Widget, Container. Add widgets Label.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
23 |
self.parent = None |
ee5ea9671f28
Add core Widget, Container. Add widgets Label.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
24 |
#: Window owning this Widget |
ee5ea9671f28
Add core Widget, Container. Add widgets Label.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
25 |
self.window = None |
89 | 26 |
#: Theme |
27 |
self.theme = default_theme |
|
87
ee5ea9671f28
Add core Widget, Container. Add widgets Label.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
28 |
|
ee5ea9671f28
Add core Widget, Container. Add widgets Label.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
29 |
#: Position inside parent widget. Modified by layout manager. |
ee5ea9671f28
Add core Widget, Container. Add widgets Label.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
30 |
self.pos = Point() |
ee5ea9671f28
Add core Widget, Container. Add widgets Label.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
31 |
#: Actual size. Modified by layout manager. |
93 | 32 |
self._size = Size(10, 10) |
90
781774a8d568
Add timer, adjust inheritance of Widget, Container, Window.
Radek Brich <radek.brich@devl.cz>
parents:
89
diff
changeset
|
33 |
|
87
ee5ea9671f28
Add core Widget, Container. Add widgets Label.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
34 |
#: Requested size. Layout manager will use this when placing the widget. |
ee5ea9671f28
Add core Widget, Container. Add widgets Label.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
35 |
self.sizereq = Size(1, 1) |
ee5ea9671f28
Add core Widget, Container. Add widgets Label.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
36 |
#: Minimal size of widget. Widget will never be sized smaller than this. |
ee5ea9671f28
Add core Widget, Container. Add widgets Label.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
37 |
self.sizemin = Size(1, 1) |
ee5ea9671f28
Add core Widget, Container. Add widgets Label.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
38 |
#: Maximum size of widget. Widget will never be sized bigger than this. |
ee5ea9671f28
Add core Widget, Container. Add widgets Label.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
39 |
#: None means no maximum size (infinite). |
ee5ea9671f28
Add core Widget, Container. Add widgets Label.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
40 |
self.sizemax = Size(None, None) |
ee5ea9671f28
Add core Widget, Container. Add widgets Label.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
41 |
|
97
0c2e0c09ba5c
Add TextField widget, keypress event, cursor.
Radek Brich <radek.brich@devl.cz>
parents:
94
diff
changeset
|
42 |
#: Cursor is position where text input will occur. |
0c2e0c09ba5c
Add TextField widget, keypress event, cursor.
Radek Brich <radek.brich@devl.cz>
parents:
94
diff
changeset
|
43 |
#: It is displayed on screen if widget is active. |
104
742e504ec053
Update TextBox: Replace "spot" with "cursor".
Radek Brich <radek.brich@devl.cz>
parents:
97
diff
changeset
|
44 |
#: The cursor coordinates are relative to widget. |
742e504ec053
Update TextBox: Replace "spot" with "cursor".
Radek Brich <radek.brich@devl.cz>
parents:
97
diff
changeset
|
45 |
#: Position outside of widget boundaries means no cursor (hidden). |
742e504ec053
Update TextBox: Replace "spot" with "cursor".
Radek Brich <radek.brich@devl.cz>
parents:
97
diff
changeset
|
46 |
self._cursor = Point(-1, -1) |
97
0c2e0c09ba5c
Add TextField widget, keypress event, cursor.
Radek Brich <radek.brich@devl.cz>
parents:
94
diff
changeset
|
47 |
|
90
781774a8d568
Add timer, adjust inheritance of Widget, Container, Window.
Radek Brich <radek.brich@devl.cz>
parents:
89
diff
changeset
|
48 |
## position and size ## |
87
ee5ea9671f28
Add core Widget, Container. Add widgets Label.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
49 |
|
ee5ea9671f28
Add core Widget, Container. Add widgets Label.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
50 |
@property |
ee5ea9671f28
Add core Widget, Container. Add widgets Label.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
51 |
def x(self): |
ee5ea9671f28
Add core Widget, Container. Add widgets Label.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
52 |
return self.pos.x |
ee5ea9671f28
Add core Widget, Container. Add widgets Label.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
53 |
|
ee5ea9671f28
Add core Widget, Container. Add widgets Label.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
54 |
@property |
ee5ea9671f28
Add core Widget, Container. Add widgets Label.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
55 |
def y(self): |
ee5ea9671f28
Add core Widget, Container. Add widgets Label.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
56 |
return self.pos.y |
ee5ea9671f28
Add core Widget, Container. Add widgets Label.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
57 |
|
ee5ea9671f28
Add core Widget, Container. Add widgets Label.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
58 |
@property |
ee5ea9671f28
Add core Widget, Container. Add widgets Label.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
59 |
def width(self): |
ee5ea9671f28
Add core Widget, Container. Add widgets Label.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
60 |
return self.size.w |
ee5ea9671f28
Add core Widget, Container. Add widgets Label.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
61 |
|
ee5ea9671f28
Add core Widget, Container. Add widgets Label.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
62 |
@property |
ee5ea9671f28
Add core Widget, Container. Add widgets Label.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
63 |
def height(self): |
ee5ea9671f28
Add core Widget, Container. Add widgets Label.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
64 |
return self.size.h |
ee5ea9671f28
Add core Widget, Container. Add widgets Label.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
65 |
|
93 | 66 |
@property |
67 |
def size(self): |
|
68 |
return self._size.readonly() |
|
69 |
||
70 |
def resize(self, w, h): |
|
71 |
self._size.update(w, h) |
|
72 |
||
94
e50dae408fe9
Add origin to Buffer. Use it to simplify hierarchical drawing.
Radek Brich <radek.brich@devl.cz>
parents:
93
diff
changeset
|
73 |
## drawing, looks ## |
87
ee5ea9671f28
Add core Widget, Container. Add widgets Label.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
74 |
|
94
e50dae408fe9
Add origin to Buffer. Use it to simplify hierarchical drawing.
Radek Brich <radek.brich@devl.cz>
parents:
93
diff
changeset
|
75 |
def draw(self, buffer): |
e50dae408fe9
Add origin to Buffer. Use it to simplify hierarchical drawing.
Radek Brich <radek.brich@devl.cz>
parents:
93
diff
changeset
|
76 |
"""Draw self into buffer.""" |
e50dae408fe9
Add origin to Buffer. Use it to simplify hierarchical drawing.
Radek Brich <radek.brich@devl.cz>
parents:
93
diff
changeset
|
77 |
self._log.debug('%s draw into %r at %s (exposed %s)', |
e50dae408fe9
Add origin to Buffer. Use it to simplify hierarchical drawing.
Radek Brich <radek.brich@devl.cz>
parents:
93
diff
changeset
|
78 |
self.name, buffer, buffer.origin, self.exposed(buffer)) |
89 | 79 |
|
80 |
def set_theme(self, theme): |
|
81 |
self.theme = theme |
|
90
781774a8d568
Add timer, adjust inheritance of Widget, Container, Window.
Radek Brich <radek.brich@devl.cz>
parents:
89
diff
changeset
|
82 |
|
94
e50dae408fe9
Add origin to Buffer. Use it to simplify hierarchical drawing.
Radek Brich <radek.brich@devl.cz>
parents:
93
diff
changeset
|
83 |
@staticmethod |
e50dae408fe9
Add origin to Buffer. Use it to simplify hierarchical drawing.
Radek Brich <radek.brich@devl.cz>
parents:
93
diff
changeset
|
84 |
def exposed(buffer): |
e50dae408fe9
Add origin to Buffer. Use it to simplify hierarchical drawing.
Radek Brich <radek.brich@devl.cz>
parents:
93
diff
changeset
|
85 |
"""Exposed part of widget. |
e50dae408fe9
Add origin to Buffer. Use it to simplify hierarchical drawing.
Radek Brich <radek.brich@devl.cz>
parents:
93
diff
changeset
|
86 |
|
e50dae408fe9
Add origin to Buffer. Use it to simplify hierarchical drawing.
Radek Brich <radek.brich@devl.cz>
parents:
93
diff
changeset
|
87 |
Only this area needs to be drawn. |
e50dae408fe9
Add origin to Buffer. Use it to simplify hierarchical drawing.
Radek Brich <radek.brich@devl.cz>
parents:
93
diff
changeset
|
88 |
|
e50dae408fe9
Add origin to Buffer. Use it to simplify hierarchical drawing.
Radek Brich <radek.brich@devl.cz>
parents:
93
diff
changeset
|
89 |
Returns exposed Rect in widget's local coordinates, |
e50dae408fe9
Add origin to Buffer. Use it to simplify hierarchical drawing.
Radek Brich <radek.brich@devl.cz>
parents:
93
diff
changeset
|
90 |
where 0,0 is left top corner of widget to be drawn. |
e50dae408fe9
Add origin to Buffer. Use it to simplify hierarchical drawing.
Radek Brich <radek.brich@devl.cz>
parents:
93
diff
changeset
|
91 |
|
e50dae408fe9
Add origin to Buffer. Use it to simplify hierarchical drawing.
Radek Brich <radek.brich@devl.cz>
parents:
93
diff
changeset
|
92 |
""" |
e50dae408fe9
Add origin to Buffer. Use it to simplify hierarchical drawing.
Radek Brich <radek.brich@devl.cz>
parents:
93
diff
changeset
|
93 |
return buffer.clip_rect.moved(-buffer.origin.x, -buffer.origin.y) |
e50dae408fe9
Add origin to Buffer. Use it to simplify hierarchical drawing.
Radek Brich <radek.brich@devl.cz>
parents:
93
diff
changeset
|
94 |
|
97
0c2e0c09ba5c
Add TextField widget, keypress event, cursor.
Radek Brich <radek.brich@devl.cz>
parents:
94
diff
changeset
|
95 |
@property |
0c2e0c09ba5c
Add TextField widget, keypress event, cursor.
Radek Brich <radek.brich@devl.cz>
parents:
94
diff
changeset
|
96 |
def cursor(self): |
104
742e504ec053
Update TextBox: Replace "spot" with "cursor".
Radek Brich <radek.brich@devl.cz>
parents:
97
diff
changeset
|
97 |
"""Return cursor coordinates or None if cursor is not set |
742e504ec053
Update TextBox: Replace "spot" with "cursor".
Radek Brich <radek.brich@devl.cz>
parents:
97
diff
changeset
|
98 |
or is set outside of widget boundaries.""" |
742e504ec053
Update TextBox: Replace "spot" with "cursor".
Radek Brich <radek.brich@devl.cz>
parents:
97
diff
changeset
|
99 |
if self._cursor in Rect._make((0, 0), self._size): |
742e504ec053
Update TextBox: Replace "spot" with "cursor".
Radek Brich <radek.brich@devl.cz>
parents:
97
diff
changeset
|
100 |
return self._cursor |
97
0c2e0c09ba5c
Add TextField widget, keypress event, cursor.
Radek Brich <radek.brich@devl.cz>
parents:
94
diff
changeset
|
101 |
|
0c2e0c09ba5c
Add TextField widget, keypress event, cursor.
Radek Brich <radek.brich@devl.cz>
parents:
94
diff
changeset
|
102 |
## input events ## |
0c2e0c09ba5c
Add TextField widget, keypress event, cursor.
Radek Brich <radek.brich@devl.cz>
parents:
94
diff
changeset
|
103 |
|
0c2e0c09ba5c
Add TextField widget, keypress event, cursor.
Radek Brich <radek.brich@devl.cz>
parents:
94
diff
changeset
|
104 |
def keypress(self, keyname, char, mod): |
0c2e0c09ba5c
Add TextField widget, keypress event, cursor.
Radek Brich <radek.brich@devl.cz>
parents:
94
diff
changeset
|
105 |
self._log.debug('%s keypress(%r, %r, %r)', |
0c2e0c09ba5c
Add TextField widget, keypress event, cursor.
Radek Brich <radek.brich@devl.cz>
parents:
94
diff
changeset
|
106 |
self.name, keyname, char, mod) |
0c2e0c09ba5c
Add TextField widget, keypress event, cursor.
Radek Brich <radek.brich@devl.cz>
parents:
94
diff
changeset
|
107 |
|
90
781774a8d568
Add timer, adjust inheritance of Widget, Container, Window.
Radek Brich <radek.brich@devl.cz>
parents:
89
diff
changeset
|
108 |
## timeouts ## |
781774a8d568
Add timer, adjust inheritance of Widget, Container, Window.
Radek Brich <radek.brich@devl.cz>
parents:
89
diff
changeset
|
109 |
|
781774a8d568
Add timer, adjust inheritance of Widget, Container, Window.
Radek Brich <radek.brich@devl.cz>
parents:
89
diff
changeset
|
110 |
def add_timeout(self, delay, callback, *args): |
781774a8d568
Add timer, adjust inheritance of Widget, Container, Window.
Radek Brich <radek.brich@devl.cz>
parents:
89
diff
changeset
|
111 |
"""Register `callback` to be called after `delay` seconds.""" |
781774a8d568
Add timer, adjust inheritance of Widget, Container, Window.
Radek Brich <radek.brich@devl.cz>
parents:
89
diff
changeset
|
112 |
self.parent.add_timeout(self, delay, callback, *args) |
781774a8d568
Add timer, adjust inheritance of Widget, Container, Window.
Radek Brich <radek.brich@devl.cz>
parents:
89
diff
changeset
|
113 |
|
781774a8d568
Add timer, adjust inheritance of Widget, Container, Window.
Radek Brich <radek.brich@devl.cz>
parents:
89
diff
changeset
|
114 |
def remove_timeout(self, callback, *args): |
781774a8d568
Add timer, adjust inheritance of Widget, Container, Window.
Radek Brich <radek.brich@devl.cz>
parents:
89
diff
changeset
|
115 |
"""Unregister callback previously registered with add_timeout. |
781774a8d568
Add timer, adjust inheritance of Widget, Container, Window.
Radek Brich <radek.brich@devl.cz>
parents:
89
diff
changeset
|
116 |
|
781774a8d568
Add timer, adjust inheritance of Widget, Container, Window.
Radek Brich <radek.brich@devl.cz>
parents:
89
diff
changeset
|
117 |
Removes all timeouts with the `callback` and `args`. |
781774a8d568
Add timer, adjust inheritance of Widget, Container, Window.
Radek Brich <radek.brich@devl.cz>
parents:
89
diff
changeset
|
118 |
|
781774a8d568
Add timer, adjust inheritance of Widget, Container, Window.
Radek Brich <radek.brich@devl.cz>
parents:
89
diff
changeset
|
119 |
""" |
781774a8d568
Add timer, adjust inheritance of Widget, Container, Window.
Radek Brich <radek.brich@devl.cz>
parents:
89
diff
changeset
|
120 |
self.parent.remove_timeout(self, callback, *args) |