#!/usr/bin/env python

import sys

import pygtk
pygtk.require("2.0")
import gtk

import xfce4.gui
import xfce4.panel

TRAYICON_SIZE = 24

class PySystrayPlugin(xfce4.panel.Plugin):
	def get_size(self):
		return xfce4.panel.PanelPlugin.get_size(self)

	def destroy_object(self, obj):
		self.systray.unregister()

	def update_view(self, obj=None, data=None, data_type=None):
		orientation = data_type == "orientation" and data or self.get_orientation()

		if data_type == "orientation":
			self.main_box.set_orientation(orientation)

		tray_icons = self.fixed_view.get_children()
		tray_icon_count = len(tray_icons)

		rows = max((self.panel_size - self.panel_size % TRAYICON_SIZE) / TRAYICON_SIZE, 1)
		cols = tray_icon_count % rows == 0 and tray_icon_count / rows or (tray_icon_count - tray_icon_count % rows) / rows + 1

		row_height = self.panel_size / float(rows)

		icon_row = 0
		icon_col = 0
		last_pos = 0
		for item in tray_icons:
			width = TRAYICON_SIZE
			pos_x = icon_col * TRAYICON_SIZE
			if icon_row == rows - 1:
				height = self.panel_size - last_pos
			else:
				height = int(row_height)
			pos_y = last_pos
			last_pos = pos_y + height

			if orientation == gtk.ORIENTATION_VERTICAL:
				pos_x, pos_y, width, height = (pos_y, pos_x, height, width)
			self.fixed_view.move(item, pos_x, pos_y)
			item.set_size_request(width, height)

			icon_row += 1
			if icon_row >= rows:
				icon_row = 0
				icon_col += 1
				last_pos = 0

		if len(tray_icons) == 0:
			self.fixed_view.hide()

		if orientation == gtk.ORIENTATION_HORIZONTAL:
			width = cols * TRAYICON_SIZE + 6
			self.set_size_request(width, 6)
			self.set_geometry_hints(None, width, 6, width, -1)
		else:
			height = cols * TRAYICON_SIZE + 6
			self.set_size_request(6, height)
			self.set_geometry_hints(None, 6, height, -1, height)

		handle_allocation = self.handle_start.allocation
		if orientation == gtk.ORIENTATION_HORIZONTAL:
			handle_allocation.width = 6
			handle_allocation.height = self.panel_size
		else:
			handle_allocation.width = self.panel_size
			handle_allocation.height = 6
		self.handle_start.size_allocate(handle_allocation)
		self.handle_start.queue_draw()

	def plugin_size_allocate(self, widget, allocation):
		if self.get_orientation() == gtk.ORIENTATION_HORIZONTAL:
			new_panel_size = allocation.height
		else:
			new_panel_size = allocation.width
		new_panel_size = max(new_panel_size, self.get_size())
		if new_panel_size != self.panel_size:
			self.panel_size = new_panel_size
			self.update_view()

	def plugin_size_changed(self, widget, size):
		if size > self.panel_size:
			self.panel_size = size
			self.update_view()

	def handle_expose(self, widget, event):
		allocation = widget.allocation

		x = allocation.x + widget.style.xthickness
		y = allocation.y + widget.style.ythickness
		w = allocation.width - 2 * widget.style.xthickness
		h = allocation.height - 2 * widget.style.ythickness

		widget.style.paint_box(widget.window, widget.state, gtk.SHADOW_OUT, event.area, widget, "xfce-panel", x, y, w, h)

		return True

	def icon_docked_cb(self, tray, item):
		self.fixed_view.show()
		self.fixed_view.put(item, 0, 0)
		self.update_view()

	def icon_undocked_cb(self, tray, item):
		self.fixed_view.remove(item)
		self.update_view()

	def selection_cleared_cb(self, tray):
		pass
 
	def message_new_cb(self, tray, item, id, timeout, text):
		pass

	def message_clear_cb(self, tray, item, id):
		pass

	def __init__(self):
		xfce4.panel.Plugin.__init__(self)

		if xfce4.gui.system_tray_check_running(self.get_screen()) == True:
			print "there already is a system tray!"
			sys.exit(1)

		self.panel_size = self.get_size()

		self.connect("destroy", self.destroy_object)
		self.connect("size-changed", self.plugin_size_changed)
		self.connect("size-allocate", self.plugin_size_allocate)
		self.connect("orientation-changed", self.update_view, "orientation")

		self.main_box = xfce4.panel.Itembar(self.get_orientation())
		self.add(self.main_box)
		self.main_box.show()

		self.add_action_widget(self.main_box)

		self.handle_start = gtk.Alignment(0, 0, 0, 0)
		self.handle_start.set_size_request(6, 6)
		self.handle_start.connect("expose-event", self.handle_expose)
		self.main_box.append(self.handle_start)
		self.handle_start.show()

		self.fixed_view = gtk.Fixed()
		self.main_box.append(self.fixed_view)

		self.systray = xfce4.gui.SystemTray()
		self.systray.connect("icon-docked", self.icon_docked_cb)
		self.systray.connect("icon-undocked", self.icon_undocked_cb)
		self.systray.connect("selection-cleared", self.selection_cleared_cb)
		self.systray.connect("message-new", self.message_new_cb)
		self.systray.connect("message-clear", self.message_clear_cb)

		self.update_view()

		self.systray.register(self.get_screen())

plugin = PySystrayPlugin()
if plugin == None:
	sys.exit(0)

plugin.show()

gtk.main()
sys.exit(0)
