1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
|
from ranger.shared import FileManagerAware, EnvironmentAware, SettingsAware
from ranger import log
import _curses
class Displayable(EnvironmentAware, FileManagerAware, SettingsAware):
"""
Displayables are objects which are displayed on the screen.
This is just the abstract class, defining basic operations
such as resizing, printing, changing colors.
Subclasses of displayable should implement this interface:
draw() -- draw the object here. Is only called if visible.
poke() -- is called just before draw(), even if not visible.
finalize() -- called after all objects finished drawing.
click(event) -- called with a MouseEvent. This is called on all
visible objects under the mouse, until one returns True.
press(key) -- called after a key press on focused objects.
destroy() -- called before destroying the displayable object
This abstract class defines the following (helper) methods:
color(*keys) -- sets the color associated with the keys from
the current colorscheme.
color_at(y, x, wid, *keys) -- sets the color at the given position
color_reset() -- resets the color to the default
addstr(*args) -- failsafe version of self.win.addstr(*args)
__contains__(item) -- is the item (y, x) inside the widget?
These attributes are set:
Modifiable:
focused -- Focused objects receive press() calls.
visible -- Visible objects receive draw() and finalize() calls
Read-Only: (i.e. reccomended not to change manually)
win -- the own curses window object
parent -- the parent (DisplayableContainer) object or None
x, y, wid, hei -- absolute coordinates and boundaries
settings, fm, env -- inherited shared variables
"""
def __init__(self, win, env=None, fm=None, settings=None):
from ranger.gui.ui import UI
if env is not None:
self.env = env
if fm is not None:
self.fm = fm
if settings is not None:
self.settings = settings
self.focused = False
self.visible = True
self.x = 0
self.y = 0
self.wid = 0
self.hei = 0
self.parent = None
if win is not None:
if isinstance(self, UI):
self.win = win
else:
self.win = win.derwin(1, 1, 0, 0)
def __nonzero__(self):
"""Always True"""
return True
def __contains__(self, item):
"""Is item inside the boundaries?
item can be an iterable like [y, x] or an object with x and y methods.
"""
try:
y, x = item.y, item.x
except AttributeError:
try:
y, x = item
except (ValueError, TypeError):
return False
return self.contains_point(y, x)
def addstr(self, *args):
try:
self.win.addstr(*args)
except _curses.error:
pass
def color(self, keylist = None, *keys):
"""Change the colors from now on."""
keys = combine(keylist, keys)
attr = self.settings.colorscheme.get_attr(*keys)
try:
self.win.attrset(attr)
except _curses.error:
pass
def color_at(self, y, x, wid, keylist = None, *keys):
"""Change the colors at the specified position"""
keys = combine(keylist, keys)
attr = self.settings.colorscheme.get_attr(*keys)
try:
self.win.chgat(y, x, wid, attr)
except _curses.error:
pass
def color_reset(self):
"""Change the colors to the default colors"""
Displayable.color(self, 'reset')
def draw(self):
"""Draw the object. Called on every main iteration.
Containers should call draw() on their contained objects here.
Override this!
"""
def destroy(self):
"""Called when the object is destroyed.
Override this!
"""
def contains_point(self, y, x):
"""
Test whether the point (with absolute coordinates) lies
within the boundaries of this object.
"""
return (x >= self.x and x < self.x + self.wid) and \
(y >= self.y and y < self.y + self.hei)
def click(self, event):
"""Called when a mouse key is pressed and self.focused is True.
Override this!
"""
pass
def press(self, key):
"""Called when a key is pressed and self.focused is True.
Override this!
"""
pass
def poke(self):
"""Called before drawing, even if invisible"""
def draw(self):
"""Draw displayable. Called on every main iteration if the object
is visible. Override this!
"""
pass
def finalize(self):
"""Called after every displayable is done drawing.
Override this!
"""
pass
def resize(self, y, x, hei=None, wid=None):
"""Resize the widget"""
do_move = False
try:
maxy, maxx = self.env.termsize
except TypeError:
pass
else:
if hei is None:
hei = maxy - y
if wid is None:
wid = maxx - x
if x < 0 or y < 0:
raise OutOfBoundsException("Starting point below zero!")
#if wid < 1 or hei < 1:
# raise OutOfBoundsException("WID and HEI must be >=1!")
if x + wid > maxx and y + hei > maxy:
raise OutOfBoundsException("X and Y out of bounds!")
if x + wid > maxx:
raise OutOfBoundsException("X out of bounds!")
if y + hei > maxy:
raise OutOfBoundsException("Y out of bounds!")
if hei != self.hei or wid != self.wid:
try:
self.win.resize(hei, wid)
except:
# Not enough space for resizing...
try:
self.win.mvderwin(0, 0)
do_move = True
self.win.resize(hei, wid)
except:
pass
#raise OutOfBoundsException("Resizing Failed!")
self.hei, self.wid = self.win.getmaxyx()
if do_move or y != self.y or x != self.x:
log("moving " + self.__class__.__name__)
try:
self.win.mvderwin(y, x)
except:
pass
self.y, self.x = self.win.getparyx()
if self.parent:
self.y += self.parent.y
self.x += self.parent.x
class DisplayableContainer(Displayable):
"""
DisplayableContainers are Displayables which contain other Displayables.
This is also an abstract class. The methods draw, poke, finalize,
click, press and destroy are overridden here and will recursively
call the function on all contained objects.
New methods:
add_child(object) -- add the object to the container.
remove_child(object) -- remove the object from the container.
New attributes:
container -- a list with all contained objects (rw)
"""
def __init__(self, win, env=None, fm=None, settings=None):
if env is not None:
self.env = env
if fm is not None:
self.fm = fm
if settings is not None:
self.settings = settings
self.container = []
Displayable.__init__(self, win)
# ----------------------------------------------- overrides
def poke(self):
"""Recursively called on objects in container"""
for displayable in self.container:
displayable.poke()
def draw(self):
"""Recursively called on visible objects in container"""
for displayable in self.container:
if displayable.visible:
displayable.draw()
def finalize(self):
"""Recursively called on visible objects in container"""
for displayable in self.container:
if displayable.visible:
displayable.finalize()
def press(self, key):
"""Recursively called on objects in container"""
focused_obj = self._get_focused_obj()
if focused_obj:
focused_obj.press(key)
return True
return False
def click(self, event):
"""Recursively called on objects in container"""
focused_obj = self._get_focused_obj()
if focused_obj and focused_obj.click(event):
return True
for displayable in self.container:
if event in displayable:
if displayable.click(event):
return True
return False
def destroy(self):
"""Recursively called on objects in container"""
for displayable in self.container:
displayable.destroy()
# ----------------------------------------------- new methods
def add_child(self, obj):
"""Add the objects to the container."""
if obj.parent:
obj.parent.remove_child(obj)
self.container.append(obj)
obj.parent = self
def remove_child(self, obj):
"""Remove the object from the container."""
try:
container.remove(obj)
except ValueError:
pass
else:
obj.parent = None
def _get_focused_obj(self):
# Finds a focused displayable object in the container.
for displayable in self.container:
if displayable.focused:
return displayable
try:
obj = displayable._get_focused_obj()
except AttributeError:
pass
else:
if obj is not None:
return obj
return None
class OutOfBoundsException(Exception):
pass
def combine(seq, tup):
"""Add seq and tup. Ensures that the result is a tuple."""
try:
if isinstance(seq, str): raise TypeError
return tuple(tuple(seq) + tup)
except TypeError:
try:
return tuple((seq, ) + tup)
except:
return ()
|