#!/usr/bin/python2
import os, sys
from Xlib import X
from plwm import wmanager, keys, color, event
import panes, focus
from resize import resize_pane

# Monkeypatch to jump to upper-left corner of window instead of center.
# Not worth subclassing or cloning imo.
def warp_patch(self, x = 0, y = 0):
	return self._warppointer(x, y)
wmanager.Client._warppointer = wmanager.Client.warppointer
wmanager.Client.warppointer = warp_patch

# TODO: WM restart/exec? Ratpoison's tmpwm is neat too.
# TODO: Xinerama, EWMH, struts/reserved regions/fixed windows/nonfocusable windows?
# For struts, check out plwm.wmanager.Screen.alloc_border.

class keybindings(keys.KeyHandler):
	# TODO: Do this right. (config file)
	# We could do this like tritium does (config file has keybinding class)
	# or do some metaclass dickery to make it nicer for the user.
	# Or we could rewrite KeyHandler if we're feeling adventurous.
	# TODO MAYBE: Add prefix support (a la screen & ratpoison) to KeyHandler.
	def _spawn(self, command):
		self.wm.current_screen.system(command)
	def _spawn_term(self):
		self._spawn('urxvt')
		
	def M_S_Return(self, event):
		self._spawn_term()
	def M_p(self, event):
		self._spawn('dmenu_run')

	def M_h(self, event):
		self.wm.move_focus('left')
	def M_j(self, event):
		self.wm.move_focus('down')
	def M_k(self, event):
		self.wm.move_focus('up')
	def M_l(self, event):
		self.wm.move_focus('right')

	def M_S_h(self, event):
		self.wm.current_screen.current_pane.move_window('left')
	def M_S_j(self, event):
		self.wm.current_screen.current_pane.move_window('down')
	def M_S_k(self, event):
		self.wm.current_screen.current_pane.move_window('up')
	def M_S_l(self, event):
		self.wm.current_screen.current_pane.move_window('right')

	def M_S_comma(self, event):
		self.wm.current_screen.current_pane.vertical_split()
		self._spawn_term()
	def M_S_period(self, event):
		self.wm.current_screen.current_pane.horizontal_split()
		self._spawn_term()

	def M_minus(self, event):
		resize_pane(self.wm.current_screen.current_pane, 'hshrink')
	def M_equal(self, event):
		resize_pane(self.wm.current_screen.current_pane, 'hgrow')
	def M_underscore(self, event):
		resize_pane(self.wm.current_screen.current_pane, 'vshrink')
	def M_plus(self, event):
		resize_pane(self.wm.current_screen.current_pane, 'vgrow')

	def M_1(self, event):
		self.wm.current_screen.current_pane.switch_window(0)
	def M_2(self, event):
		self.wm.current_screen.current_pane.switch_window(1)
	def M_3(self, event):
		self.wm.current_screen.current_pane.switch_window(2)
	def M_4(self, event):
		self.wm.current_screen.current_pane.switch_window(3)
	def M_5(self, event):
		self.wm.current_screen.current_pane.switch_window(4)

	def M_q(self, event):
		# FIXME: Preserve layout (or some of it) on restart.
		self.wm.display.sync()
		os.execv(sys.argv[0], sys.argv)
		sys.exit(1) # Shouldn't get this far.

class wmclient(wmanager.Client, panes.panesClient):
	pass

class wmscreen(wmanager.Screen, color.Color, panes.panesScreen):
	pass

class wm(wmanager.WindowManager, focus.MoveFocus):
	client_class = wmclient
	screen_class = wmscreen
	def __init__(self, *args, **kwargs):
		# If this is placed in __wm_init__ instead and there are windows open
		# before the wm starts (ie. we restart), plwm tries to place the window
		# before __wm_init__ and stuff breaks because misc_dispatch isn't there.
		self.misc_dispatch = event.Dispatcher(None)
		return wmanager.WindowManager.__init__(self, *args, **kwargs)

	def __wm_init__(self):
		keybindings(self)
		self.outline_pane = panes.OutlinePane(self)

wmanager.main(wm)

