#
# misc_widgets.py
#
import gtk
import os.path

class MyButtonBox(gtk.HButtonBox):
	"""
	Default pyne layout of button boxes.
	"""
	def __init__(self):
		gtk.HButtonBox.__init__(self)
		self.set_layout(gtk.BUTTONBOX_END)
		self.set_border_width(5)
		self.set_spacing(5)

class ColorPickButton(gtk.Button):
	"""
	Button that is coloured. Clicking opens a dialog box
	to change its colour.
	"""
	def __init__(self, label, parent_win=None):
		self.col = '#000000'
		self.parent_win = parent_win
		gtk.Button.__init__(self, label)
		self.connect("clicked", self.changecol_box)
		self.update_col()
	
	def changecol_box(self, _w=None):
		colpick = gtk.ColorSelectionDialog(_("Howdy"))
		def _change(w):
			col = colpick.colorsel.get_current_color()
			self.col = "#%02x%02x%02x" % (col.red/256,col.green/256,col.blue/256)
			colpick.destroy()
			self.update_col()
		def _close(w):
			colpick.destroy()
		colpick.colorsel.set_current_color ( colpick.colorsel.get_colormap().alloc_color (self.col) )
		colpick.ok_button.connect("clicked", _change)
		colpick.help_button.hide()
		colpick.cancel_button.connect("clicked", _close)
		if self.parent_win:
			colpick.set_transient_for(self.parent_win)
		colpick.show()
	
	def update_col(self):
		# Obtain ye a GdkColor
		gdkcol = self.get_colormap().alloc_color(self.col)
		
		self.modify_bg(gtk.STATE_NORMAL, gdkcol)
		self.modify_bg(gtk.STATE_PRELIGHT, gdkcol)
	
	def get_color(self):
		return self.col

	def set_color(self, col):
		self.col = col
		self.update_col()
		
class FileSelectorBox(gtk.FileSelection):
	"""
	Callback flavoured file selector box.
	Needs users so we can get and set the default path.
	"""
	def __init__(self, user, title, filename, some_function, modal=False):
		self.user = user
		self.func = some_function
		gtk.FileSelection.__init__(self, title)
		# absolute path given. use it
		if os.path.dirname(filename) != "":
			self.set_filename(filename)
		else:
			self.set_filename( os.path.join(os.path.expanduser(user.default_dir), filename) )
		if modal:
			self.grab_add()
		self.ok_button.connect("clicked", self._yay)
		self.cancel_button.connect("clicked", self._boo)

	def _yay(self, _w=None):
		filename = self.get_filename()
		if filename != None:
			self.user.default_dir = os.path.dirname(filename)
		self.destroy()
		self.func(filename)
		
	def _boo(self, _w=None):
		self.destroy()
		self.func(None)

class CallbackDialog (gtk.Dialog):
	pass

def ReturnDialog (title, text, buttons, parent_win=None, modal=True):
		d = gtk.Dialog (title)
		if parent_win:
			d.set_transient_for (parent_win)
		if modal:
			d.grab_add ()
		
		d.vbox.set_spacing (5)
		mainbox = gtk.HBox (spacing=5)
		d.vbox.pack_start (mainbox)
		mainbox.show ()
		
		img = gtk.image_new_from_stock ("gtk-dialog-question", gtk.ICON_SIZE_DIALOG)
		mainbox.pack_start (img)
		img.show ()
		
		label = gtk.Label (text)
		mainbox.pack_start (label)
		label.show()

		# Add the buttons :-/
		for butt in buttons:
			label, val = butt
			d.add_button (label, val)
		ret = d.run ()
		d.destroy ()
		return ret

def InfoBox (title, text, button, parent_win=None, modal=True, blocking=False):
	if blocking:
		ReturnDialog (title, text, ((button,0),), parent_win, modal)
	else:
		# Non-blocking dialog box.
		d = gtk.Dialog (title)
		if parent_win:
			d.set_transient_for (parent_win)
		if modal:
			d.grab_add ()
		
		d.vbox.set_spacing (5)
		mainbox = gtk.HBox (spacing=5)
		d.vbox.pack_start (mainbox)
		mainbox.show ()
		
		img = gtk.image_new_from_stock ("gtk-dialog-info", gtk.ICON_SIZE_DIALOG)
		mainbox.pack_start (img)
		img.show ()
		
		label = gtk.Label (text)
		mainbox.pack_start (label)
		label.show()

		def _close (button, d=d):
			d.destroy()
		
		# Add the buttons :-/
		button = d.add_button (button, 0)
		button.connect ("clicked", _close)
		d.show ()



syntax highlighted by Code2HTML, v. 0.9.1