/* 

   Copyright (C) Arnaud Houdelette		2012-2014
   Copyright (C) Emmanuel Langlois		2012-2014

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License.
   
   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
   
   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

using System;
using System.Collections.Generic;
using Gtk;
using System.IO;

namespace DMX2
{
	[System.ComponentModel.ToolboxItem(true)]
	public partial class DriverBoitierV1UI : Gtk.Bin
	{
		DriverBoitierV1 drv;
		public DriverBoitierV1UI (DriverBoitierV1 _drv)
		{
			drv = _drv;
			this.Build ();
			ConstruitCBUnivers();
		}

	ListStore lsCbUnivers = new ListStore(typeof(UniversDMX));		
	void ConstruitCBUnivers ()
		{
			cbUnivers.Model = lsCbUnivers;
			var cellCbUnivers = new CellRendererText ();
			cbUnivers.PackStart (cellCbUnivers, false);
			cbUnivers.SetCellDataFunc (cellCbUnivers, new CellLayoutDataFunc (RenderUniversName));
			int indx = 0;
			int i=0;
			foreach (UniversDMX u in Conduite.Courante.Patches) {
				lsCbUnivers.AppendValues (u);
				if (u==drv.patch) indx=i;
				i++;
			}

			TreeIter iter;
			lsCbUnivers.GetIterFirst(out iter);
			cbUnivers.SetActiveIter(iter);
			cbUnivers.Active=indx;

		}

		void RenderUniversName (CellLayout cell_layout, CellRenderer cell, TreeModel tree_model, TreeIter iter)
		{
			UniversDMX univers =  tree_model.GetValue (iter, 0) as UniversDMX;
			if(univers != null)
			(cell as Gtk.CellRendererText).Text = univers.Nom;
		}

		protected void OnCbUniversChanged (object sender, EventArgs e)
		{
			TreeIter iter;
			if(cbUnivers.GetActiveIter(out iter))
			{
				drv.patch = lsCbUnivers.GetValue(iter,0) as UniversDMX;

			}
		}		

		protected void OnBtnValiderClicked (object sender, EventArgs e)
		{
			TreeIter iter;
			if (cbUnivers.GetActiveIter (out iter)) {
				drv.patch = lsCbUnivers.GetValue (iter, 0) as UniversDMX;
			}
		}

	}
}