# Bryan Schumaker (10/11/2010)

import ocarina
gtk      = ocarina.gtk
gobject  = ocarina.gobject
update   = None
seek     = None
time_at  = None

def set_fns():
	global update
	global get_time
	global seek
	global time_at
	update   = ocarina.libsaria.audio.get_progress
	seek     = ocarina.libsaria.audio.seek
	time_at  = ocarina.libsaria.audio.get_time_at
ocarina.libsaria.event.invite("POSTSTART", set_fns)


class PBar(gtk.HScale):
	def __init__(self):
		adj = gtk.Adjustment(0.0, 0.0, 100.0, 0.1, 1.0, 1.0)
		gtk.HScale.__init__(self, adj)

		self.set_size_request(150, 20)
		self.set_draw_value(False)
		self.set_range(0, 101)
		self.set_update_policy(gtk.UPDATE_DISCONTINUOUS)
		self.connect("change-value", self.change_value)
		self.connect("button-press-event", self.button_down)
		self.connect("motion-notify-event", self.mouse_motion)
		gobject.timeout_add(500, self.update)
		self.show()

	def update(self):
		self.set_value(update() * 100)
		return True

	def change_value(self, scale, type, value):
		seek(value / 100.0)

	def button_down(self, widget, event):
		alloc = self.get_allocation()
		prcnt = event.x / alloc.width
		seek(prcnt)
		self.update()

	def mouse_motion(self, widget, event):
		alloc = self.get_allocation()
		prcnt = event.x / float(alloc.width)
		self.set_tooltip_text(time_at(prcnt))
