summary refs log tree commit diff stats
path: root/ranger/gui/displayable.py
blob: a19633b5b937742ef2bd73aed87a7544ff32c81f (plain) (blame)
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
from ranger.shared import FileManagerAware, EnvironmentAware, SettingsAware

class Displayable(EnvironmentAware, FileManagerAware, SettingsAware):
	focused = False
	visible = True
	win = None
	colorscheme = None

	def __init__(self, win):
		self.x = 0
		self.y = 0
		self.wid = 0
		self.hei = 0
		self.colorscheme = self.env.settings.colorscheme

		if win is not None:
			self.win = win

	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
		except ValueError:
			return False
		except TypeError:
			try:
				y, x = item.y, item.x
			except AttributeError:
				return False
		
		return self.contains_point(y, x)

	def color(self, keylist = None, *keys):
		"""Change the colors from now on."""
		keys = combine(keylist, keys)
		self.win.attrset(self.colorscheme.get_attr(*keys))

	def color_at(self, y, x, wid, keylist = None, *keys):
		"""Change the colors at the specified position"""
		keys = combine(keylist, keys)
		self.win.chgat(y, x, wid, self.colorscheme.get_attr(*keys))
	
	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 if the point 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 draw(self):
		"""Draw displayable. Called on every main iteration.
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"""
		try:
			maxy, maxx = self.env.termsize
		except TypeError:
			pass
		else:
			wid = wid or maxx - x
			hei = hei or maxy - y

			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!")

		self.x = x
		self.y = y
		self.wid = wid
		self.hei = hei


class DisplayableContainer(Displayable):
	container = None
	def __init__(self, win):
		Displayable.__init__(self, win)
		self.container = []

	def draw(self):
		"""Recursively called on objects in container"""
		for displayable in self.container:
			if displayable.visible:
				displayable.draw()

	def finalize(self):
		"""Recursively called on objects in container"""
		for displayable in self.container:
			if displayable.visible:
				displayable.finalize()
	
	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

	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(key):
			return True

		for displayable in self.container:
			if event in displayable:
				if displayable.click(event):
					return True

		return False

	def add_obj(self, obj):
		self.container.append(obj)

	def destroy(self):
		"""Recursively called on objects in container"""
		for displayable in self.container:
			displayable.destroy()

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 ()