# Bryan Schumaker (2 / 12 / 2011)

import gtk
import list
import column
import libsaria

BUTTON_RIGHT = 3

class ListView(gtk.TreeView):
	def __init__(self):
		gtk.TreeView.__init__(self)
		self.list = list.List()

		for index, label in enumerate(column.columns):
			col = column.Column(index, label)
			if index != 0:
				self.append_column(col)

		self.set_rules_hint(True)
		#self.set_has_tooltip(True)
		self.set_fixed_height_mode(True)

		self.filter_model = self.list.filter_new()
		self.set_model(self.filter_model)

		self.selection = self.get_selection()
		self.selection.set_mode(gtk.SELECTION_MULTIPLE)

		self.connect("row-activated", self.double_click)
		self.connect("button-release-event", self.button_click)
		#self.connect("query-tooltip", self.query_tooltip)

		self.insert = self.list.insert
		self.show()

	def freeze(self):
		self.freeze_child_notify()
		self.set_model(None)
		self.hide()

	def thaw(self):
		self.show()
		self.set_model(self.filter_model)
		self.thaw_child_notify()

	def clear(self):
		self.freeze()
		self.list.clear()
		self.thaw()

	def insert_rows(self, rows):
		self.freeze()
		ins_next = 0
		insert   = self.insert
		for row in rows:
			insert(ins_next, row)
			ins_next += 1
		self.thaw()

	def right_click(self, event):
		pass

	def set_right_click(self, func):
		self.right_click = func

	def double_click(self, widget, path, column):
		model = self.get_model()
		iter  = model.get_iter(path)
		row   = model[iter]
		libsaria.sources.play_id(row[0])

	def button_click(self, widget, event):
		if event.button == BUTTON_RIGHT:
			self.right_click(event)

	def get_selected_ids(self):
		res = []
		def insert_func(tree, path, iter, func):
			func(tree[iter][0])
		self.selection.selected_foreach(insert_func, res.append)
		return res

	def set_filter_func(self, func):
		self.filter_model.set_visible_func(func)

	def refilter(self):
		self.freeze()
		self.filter_model.refilter()
		self.thaw()

	def goto(self, *args):
		id = libsaria.sources.get_attrs("id")
		if id == -1: # Bad id
			return
		vis = self.get_visible_rect()
		if vis[3] == 0: # The widget hasn't been realized yet
			return
		for index, row in enumerate(self.filter_model):
			if row[0] == id:
				col = self.get_column(0)
				row_vis = self.get_cell_area(index, col)
				if row_vis[3] > 0:
					n = vis[3] / row_vis[3]
					path = index - (n / 2)
					if path < 0:
						path = 0
					self.scroll_to_cell(path, None, True, 0, 0)
					self.set_cursor_on_cell(index, None, None, False)
				return

	#def query_tooltip(self, widget, x, y, keyboard, tip):
		#row = self.get_dest_row_at_pos(x,y)
		#if row == None:
			#return False
		#iter = self.filter_model.get_iter(row[0])
		#id   = self.filter_model[iter][0]

		##attrs = get_attrs(id, "art", "title", "artist", "album", "year", "lenstr", "count")
		#attrs = get_attrs(id, "title", "artist", "album", "year", "lenstr", "count")

		##art = image.Image()
		##art.set_from_file(attrs[0])
		##art.set_height(52)
		##tip.set_icon(art.get_pixbuf())

		#tip.set_markup("<b>%s</b>\nby %s\nfrom %s" %
			#(attrs[0].replace("&", "&amp;"),
			 #attrs[1].replace("&", "&amp;"),
			 #attrs[2].replace("&", "&amp;"))
		#)

		#extra = gtk.Label()
		#extra.set_markup("  Year: %s\n  Length: %s\n  Play count: %s" %
			#(attrs[3], attrs[4], attrs[5]) )
		#tip.set_custom(extra)
		#return True
