summary refs log tree commit diff stats
path: root/ranger/gui
diff options
context:
space:
mode:
authorhut <hut@lavabit.com>2009-12-12 04:57:42 +0100
committerhut <hut@lavabit.com>2009-12-12 04:57:42 +0100
commit49ae0dd10749a312f20d006a927408201e710d53 (patch)
tree6fce5f932ee505b29867e70c206a11d765817ada /ranger/gui
parent61de0a4defd3fe275f6f51fe81f362b0adbdf382 (diff)
downloadranger-49ae0dd10749a312f20d006a927408201e710d53.tar.gz
implemented FileListContainer
Diffstat (limited to 'ranger/gui')
-rw-r--r--ranger/gui/defaultui.py37
-rw-r--r--ranger/gui/displayable.py33
-rw-r--r--ranger/gui/ui.py23
-rw-r--r--ranger/gui/widgets/__init__.py4
-rw-r--r--ranger/gui/widgets/console.py6
-rw-r--r--ranger/gui/widgets/filelist.py8
-rw-r--r--ranger/gui/widgets/filelistcontainer.py42
-rw-r--r--ranger/gui/widgets/titlebar.py4
8 files changed, 105 insertions, 52 deletions
diff --git a/ranger/gui/defaultui.py b/ranger/gui/defaultui.py
index 8d9f7ded..b9758e8d 100644
--- a/ranger/gui/defaultui.py
+++ b/ranger/gui/defaultui.py
@@ -1,25 +1,19 @@
 
-RATIO = ( 0.15, 0.15, 0.4, 0.3 )
+RATIO = ( 3, 3, 12, 9 )
 
 from ranger.gui.ui import UI as SuperClass
 class DefaultUI(SuperClass):
 	def setup(self):
-		from ranger.gui.widgets.filelist import FileList
+		"""Build up the UI by initializing widgets."""
+		from ranger.gui.widgets.filelistcontainer import FileListContainer
 		from ranger.gui.widgets.titlebar import TitleBar
 		from ranger.gui.widgets.console import Console
 		self.titlebar = TitleBar(self.win)
 		self.add_obj(self.titlebar)
 
-		self.displays = [
-				FileList(self.win, -2),
-				FileList(self.win, -1),
-				FileList(self.win, 0),
-				FileList(self.win, 1) ]
-		self.main_display = self.displays[2]
-		self.displays[2].display_infostring = True
-		self.displays[2].main_display = True
-		for disp in self.displays:
-			self.add_obj(disp)
+		self.filelist_container = FileListContainer(self.win, RATIO)
+		self.add_obj(self.filelist_container)
+		self.main_filelist = self.filelist_container.main_filelist
 
 		self.console = Console(self.win)
 		self.add_obj(self.console)
@@ -27,20 +21,9 @@ class DefaultUI(SuperClass):
 	def update_size(self):
 		"""resize all widgets"""
 		SuperClass.update_size(self)
-		y, x = self.win.getmaxyx()
-
-		leftborder = 0
-
-		i = 0
-		for ratio in RATIO:
-			wid = int(ratio * x)
-			try:
-				self.displays[i].resize(1, leftborder, y-2, wid - 1)
-			except KeyError:
-				pass
-			leftborder += wid
-			i += 1
+		y, x = self.env.termsize
 
+		self.filelist_container.resize(1, 0, y-2, x)
 		self.titlebar.resize(0, 0, 1, x)
 		self.console.resize(y-1, 0, 1, x)
 
@@ -53,5 +36,5 @@ class DefaultUI(SuperClass):
 		self.console.visible = False
 
 	def scroll(self, relative):
-		self.main_display.scroll(relative)
-
+		if self.main_filelist:
+			self.main_filelist.scroll(relative)
diff --git a/ranger/gui/displayable.py b/ranger/gui/displayable.py
index d9f56076..a19633b5 100644
--- a/ranger/gui/displayable.py
+++ b/ranger/gui/displayable.py
@@ -7,7 +7,10 @@ class Displayable(EnvironmentAware, FileManagerAware, SettingsAware):
 	colorscheme = None
 
 	def __init__(self, win):
-		self.resize(0, 0, 0, 0)
+		self.x = 0
+		self.y = 0
+		self.wid = 0
+		self.hei = 0
 		self.colorscheme = self.env.settings.colorscheme
 
 		if win is not None:
@@ -17,6 +20,21 @@ class Displayable(EnvironmentAware, FileManagerAware, SettingsAware):
 		"""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)
@@ -130,18 +148,23 @@ class DisplayableContainer(Displayable):
 			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:
-			focused_obj.press(key)
+		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:
diff --git a/ranger/gui/ui.py b/ranger/gui/ui.py
index 8abd39df..c1764546 100644
--- a/ranger/gui/ui.py
+++ b/ranger/gui/ui.py
@@ -48,21 +48,20 @@ class UI(DisplayableContainer):
 		except:
 			return
 
-		from ranger import log
-		log(str(event.bstate))
+		if DisplayableContainer.click(self, event):
+			return
 
-		if event.pressed(1) or event.pressed(3):
-			for displayable in self.container:
-				if displayable.contains_point(event.y, event.x):
-					displayable.click(event)
-					break
+#		if event.pressed(1) or event.pressed(3):
+#			for displayable in self.container:
+#				if displayable.contains_point(event.y, event.x):
+#					displayable.click(event)
+#					break
 
 #		if event.pressed(4) or event.pressed(2) or event.bstate & 134217728:
-		if event.pressed(4) or event.pressed(2):
-			if event.pressed(4):
-				self.fm.scroll(relative = -3)
-			else:
-				self.fm.scroll(relative = 3)
+		if event.pressed(4):
+			self.fm.scroll(relative = -3)
+		elif event.pressed(2):
+			self.fm.scroll(relative = 3)
 
 	def handle_key(self, key):
 		"""Handles key input"""
diff --git a/ranger/gui/widgets/__init__.py b/ranger/gui/widgets/__init__.py
index e69de29b..5761215a 100644
--- a/ranger/gui/widgets/__init__.py
+++ b/ranger/gui/widgets/__init__.py
@@ -0,0 +1,4 @@
+from ranger.gui.displayable import Displayable
+
+class Widget(Displayable):
+	"""A simple abstract class"""
diff --git a/ranger/gui/widgets/console.py b/ranger/gui/widgets/console.py
index b1c9e15a..17197a2e 100644
--- a/ranger/gui/widgets/console.py
+++ b/ranger/gui/widgets/console.py
@@ -1,15 +1,15 @@
 """The Console widget implements a vim-like console for entering
 commands, searching and executing files."""
-from ..displayable import Displayable
+from . import Widget
 import curses
 
 CONSOLE_MODES = tuple(':@/?>!')
 CONSOLE_PROMPTS = { '@': 'open with: ' }
 
-class Console(Displayable):
+class Console(Widget):
 	def __init__(self, win):
 		from ranger.container import CommandList
-		Displayable.__init__(self, win)
+		Widget.__init__(self, win)
 		self.mode = None
 		self.visible = False
 		self.commandlist = CommandList()
diff --git a/ranger/gui/widgets/filelist.py b/ranger/gui/widgets/filelist.py
index 2bca6e24..48c6c284 100644
--- a/ranger/gui/widgets/filelist.py
+++ b/ranger/gui/widgets/filelist.py
@@ -1,13 +1,13 @@
 """The FileList widget displays the contents of a directory or file."""
-from ..displayable import Displayable
+from . import Widget
 
-class FileList(Displayable):
+class FileList(Widget):
 	main_display = False
 	display_infostring = False
 	scroll_begin = 0
 
 	def __init__(self, win, level):
-		Displayable.__init__(self, win)
+		Widget.__init__(self, win)
 		self.level = level
 
 	def click(self, event):
@@ -37,6 +37,8 @@ class FileList(Displayable):
 			if self.level > 0:
 				self.fm.move_right()
 
+		return True
+
 	def draw(self):
 		"""Call either draw_file() or draw_directory()"""
 		from ranger.fsobject.file import File
diff --git a/ranger/gui/widgets/filelistcontainer.py b/ranger/gui/widgets/filelistcontainer.py
new file mode 100644
index 00000000..ed5a36c4
--- /dev/null
+++ b/ranger/gui/widgets/filelistcontainer.py
@@ -0,0 +1,42 @@
+"""The FileListContainer manages a set of FileLists."""
+from . import Widget
+from .filelist import FileList
+from ..displayable import DisplayableContainer
+
+class FileListContainer(Widget, DisplayableContainer):
+	ratios = None
+	preview = True
+
+	def __init__(self, win, ratios, preview = True):
+		DisplayableContainer.__init__(self, win)
+		from functools import reduce
+		self.ratios = ratios
+		# normalize ratios:
+		ratio_sum = float(reduce(lambda x,y: x + y, ratios))
+		self.ratios = tuple(map(lambda x: x / ratio_sum, ratios))
+		
+		offset = 1 - len(ratios)
+		if preview: offset += 1
+
+		for level in range(len(ratios)):
+			self.add_obj(FileList(win, level + offset))
+
+		try:
+			self.main_filelist = self.container[preview and -2 or -1]
+		except IndexError:
+			self.main_filelist = None
+		else:
+			self.main_filelist.display_infostring = True
+			self.main_filelist.main_display = True
+	
+	def resize(self, y, x, hei, wid):
+		"""Resize all the filelists according to the given ratio"""
+		DisplayableContainer.resize(self, y, x, hei, wid)
+		left = self.y
+		for ratio, i in zip(self.ratios, range(len(self.ratios))):
+			wid = int(ratio * self.wid)
+			try:
+				self.container[i].resize(self.y, left, hei, max(1, wid-1))
+			except KeyError:
+				pass
+			left += wid
diff --git a/ranger/gui/widgets/titlebar.py b/ranger/gui/widgets/titlebar.py
index 4ec48766..0e1c0aff 100644
--- a/ranger/gui/widgets/titlebar.py
+++ b/ranger/gui/widgets/titlebar.py
@@ -1,9 +1,9 @@
 """The TitleBar widget displays the current path and some other useful
 information."""
 
-from ..displayable import Displayable
+from . import Widget
 
-class TitleBar(Displayable):
+class TitleBar(Widget):
 	def draw(self):
 		import curses, socket, os
 		self.win.move(self.y, self.x)