# Bryan Schumaker (8/13/2010)

import gtk
import ocarina
import libsaria
import body
import shortcuts
from libsaria.path import files

TARGET_TYPE_URI_LIST = 80
DND_MASK = gtk.DEST_DEFAULT_MOTION | gtk.DEST_DEFAULT_HIGHLIGHT | gtk.DEST_DEFAULT_DROP
DND_LIST = [("text/uri-list", 0, TARGET_TYPE_URI_LIST)]

width  = libsaria.prefs.init("ocarina.window.width", 800)
height = libsaria.prefs.init("ocarina.window.height", 600)

window = gtk.Window(gtk.WINDOW_TOPLEVEL)
window.resize(width, height)
window.connect("delete-event", ocarina.quit)
window.connect("key-press-event", shortcuts.key_pressed)
window.add(body.body)
window.show()

def grab_focus():
	window.set_focus(None)
shortcuts.register_shortcut("Escape", grab_focus, True)

def set_title(new_title = None):
	if new_title == None:
		new_title = ocarina.__vers__
	window.set_title(new_title)
set_title()

def set_icon(icon = "images/ocarina.png"):
	window.set_icon_from_file(icon)
set_icon()

def resized(widget, geom):
	if libsaria.prefs.get("ocarina.window.width") != geom.width:
		libsaria.prefs.set("ocarina.window.width", geom.width)
	if libsaria.prefs.get("ocarina.window.height") != geom.height:
		libsaria.prefs.set("ocarina.window.height", geom.height)
window.connect("size-allocate", resized)

def dnd_receive(widget, context, x, y, selection, type, time):
	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)
window.connect("drag-data-received", dnd_receive)
window.drag_dest_set(DND_MASK, DND_LIST, gtk.gdk.ACTION_COPY)
