#!/usr/bin/env python
# -*- coding: utf-8 -*-

import gtk, os

class caticonlist(gtk.ComboBox):
	"""Liste des thèmes disponibles"""
	def __init__(self, config):
		self.config = config
		self.store = caticonstore(config)
		
		gtk.ComboBox.__init__(self, self.store)
		
		pixbuf = gtk.CellRendererPixbuf()
		self.pack_start(pixbuf, False)
		self.add_attribute(pixbuf, 'pixbuf', 0)
		
		cell = gtk.CellRendererText()
		self.pack_start(cell, True)
		self.add_attribute(cell, 'text', 1)
		
	def get_cat(self):
		return self.store.get_value(self.get_active_iter(), 1)
		
	def update(self, cat):
		self.set_active_iter(self.store.update(cat))
		
class caticonstore(gtk.TreeStore):
	def __init__(self, config):
		self.config = config
		gtk.TreeStore.__init__(self, gtk.gdk.Pixbuf, str)
		
	def update(self, cat):
		self.clear()
		catiter = None
		
		themedir = self.config.get("FramaKioskTune", "FramaKioskPath") + "/Data/FramaKiosk/theme/" + self.config.get("Data", "actualtheme") + "/img/cat/"
		for fichier in sorted(os.listdir(themedir)):
			if os.path.splitext(fichier)[1] == ".png":
				iter = self.append(None, [gtk.gdk.pixbuf_new_from_file(themedir + fichier).scale_simple(22,22,gtk.gdk.INTERP_BILINEAR), fichier])
				
				if fichier == cat:
					catiter = iter
		
		if catiter is None:
			catiter = self.get_iter_first()
		
		return catiter
