# Bryan Schumaker (8/13/2010)

import ocarina
libsaria = ocarina.libsaria
gtk = ocarina.gtk
files = None

TARGET_TYPE_URI_LIST = 80

window    = None
add       = None
resize    = None
connect   = None
set_title = None
prefs     = None

def init(width, height):
	global window
	global add
	global resize
	global connect
	global set_title
	global prefs

	from components import entry

	window    = gtk.Window(gtk.WINDOW_TOPLEVEL)
	add       = window.add
	resize    = window.resize
	connect   = window.connect
	set_title = window.set_title
	prefs     = libsaria.prefs

	connect("key-press-event", entry.key_press)
	connect("delete-event", ocarina.exit)
	connect("size-allocate", resized)
	connect("drag-data-received", dnd_receive)

	mask = gtk.DEST_DEFAULT_MOTION | gtk.DEST_DEFAULT_HIGHLIGHT | gtk.DEST_DEFAULT_DROP
	dnd_list = [("text/uri-list", 0, TARGET_TYPE_URI_LIST)]
	window.drag_dest_set(mask, dnd_list, gtk.gdk.ACTION_COPY)

	resize(width, height)
	window.show()


def set_icon(path):
	global window
	if libsaria.path.exists(path):
		window.set_icon_from_file(path)


def resized(widget, geom):
	if prefs.get_pref("ocarina.window.width") != geom.width:
		libsaria.prefs.set_pref("ocarina.window.width", geom.width)
	if prefs.get_pref("ocarina.window.height") != geom.height:
		libsaria.prefs.set_pref("ocarina.window.height", geom.height)


def dnd_receive(widget, context, x, y, selection, type, time):
	global files
	if files == None:
		from libsaria.path import files
	if type == TARGET_TYPE_URI_LIST:
		uri = selection.data.strip('\r\n\x00')
		for file in uri.split():
			file = file[7:]
			files.universal_open(file)
