// clsTreeView.cs
// Copyright (C) 2008 Karel Jordaan karel_jordaan@yahoo.com.au
// 
// 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, or
// (at your option) any later version.
// 
// 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, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

using System;
using System.Data ;
using System.Windows.Forms;

namespace GUIControlLogic
{
	///	<summary>
	///	clsTreeView hasclasses for the treeview for CV.
	///	</summary>
	public class clsTreeView:clsGlobalErr
	{
		private	TreeView  m_trv;
		private	ImageList  m_img = new ImageList();
		private	DataTable  m_tbl = new DataTable();
		//private	ZANZILib.clsMain m_lib =new	CVLib.clsMain();
		private GenrealLibLogic.clsMain m_lib = new GenrealLibLogic.clsMain();
		private ToolTip m_tt;
		private clsMain	frmm;
		
		public clsTreeView(TreeView	trv, ToolTip tt)
		{
			try
			{
				m_tt = tt;
				m_trv =	 trv;
				m_trv.ImageList	= m_img;
			}
			catch (Exception  ex)
			{
				clsExeption exx = new clsExeption(ex);
				base.ErrByte = 1;
			}
		}
/// <summary>
/// Retrieves the structure of the tree from CVLib.
/// </summary>
		internal  void GetTree()
		{
			try
			{
				m_tbl =	 m_lib.GetDataTable (m_tbl,CVLib.clsMain.TypeTable.TreeViewStructure );  
				BiuldImageList();
				BiuldTree();
			}
			catch (Exception  ex)
			{
				clsExeption exx = new clsExeption(ex);
				base.ErrByte = 1;
			}
		}

/// <summary>
/// Builds the imagelist from data retrieved by GetTree
/// </summary>
		private	void BiuldImageList()
		{
			try
			{
				foreach	(DataRow r in m_tbl.Rows)
				{
					System.IO.Stream strm =	 (System.IO.Stream)r["ico"];
					System.Drawing.Icon	ico	=new System.Drawing.Icon(strm);
					m_img.Images.Add(ico);
				}
			}
			catch (Exception  ex)
			{
				clsExeption exx = new clsExeption(ex);
				base.ErrByte = 1;
			}			
		}
/// <summary>
/// Builds the tree from data retrived from GetTree
/// </summary>
		private	void BiuldTree()
		{
			//biulds the tree
			TreeNode ndparent =	new	TreeNode ();
			TreeNode ndprev	= new TreeNode ();
			int	pos = 0;

			//	objTag
			clsMain	frmm;
			frmm = (clsMain) m_trv.Parent;
			TreeTag	t;

			try
			{
				int	i =	0;
				TreeNode[] ndd	= new TreeNode[10];
 

				foreach	(DataRow r in m_tbl.Rows)
				{
					
					TreeNode nd	= (TreeNode)CreateNode (r);
					nd.Text	= frmm.resources.GetString(r["val"].ToString());
					nd.ImageIndex =	i;
					i++;
					t =	(TreeTag)nd.Tag;

					//FirstTime
					if (i==1)
					{
						m_trv.Nodes.Add(nd);
						ndparent  =	nd;
						ndd[2] = nd;
						ndprev = nd;
					}


					//if not child
					TreeTag	t2 = (TreeTag)ndparent.Tag;
					//if (t.Postion	!= t2.Postion)
					//{
					if (t.Postion.Length ==	t2.Postion.Length +2)
					{
						//m_trv.Nodes.Add(nd);
						//ndparent	= ndd[t.Postion.Length -2];
						ndparent  =	ndd[t.Postion.Length -2];
						ndparent.Nodes.Add(nd);
						ndprev = nd;
					}
					else
					{
						if (t.Postion.Length > t2.Postion.Length)
						{
							ndd[t.Postion.Length  -2] =	ndprev;
							ndparent  =	ndprev;
							ndparent.Nodes.Add(nd);
						}
						else
						{
							if(i!=1)
							{
								ndparent  =	ndd[t.Postion.Length  -2];
								ndparent.Nodes.Add(nd);
							{
							}
							}
						}
					}
				}
			}

			catch (Exception  ex)
			{
				clsExeption exx = new clsExeption(ex);
				base.ErrByte = 1;
			}

		}
/// <summary>
/// Changes the text of the TreeView based on a culture change.
/// </summary>
			public void	ResetCulture()
			{
				try
				{	
					m_trv.Nodes.Clear();
					BiuldTree ();
				}
				catch (Exception  ex)
				{
					clsExeption exx = new clsExeption(ex);
					base.ErrByte = 1;
				}
			}
/// <summary>
/// Creates a node for treeview
/// </summary>
/// <param name="r">Datarow to create node from</param>
/// <returns>Returns a treenode</returns>
		private	object	CreateNode(DataRow r)
		{

			
			TreeNode nd	= new TreeNode ();
			TreeTag	t =	new	TreeTag();

			t.text =r["val"].ToString();
			t.Postion =	r["pos"].ToString(); //This	is maybe not nessary - can use local var
			//this.toolTip1.SetToolTip(this.trvNav, resources.GetString("trvNav.ToolTip"));
			//m_tt.SetToolTip ((System.Windows.Forms.Control)nd,frmm.resources.GetString("trvNav.ToolTip"));
			nd.Tag = t;//Needed	for	culture	reset
					
			return nd;

		}
/// <summary>
/// Internal structure for treebuild and for later reference when nodeclick
/// </summary>
		private	struct TreeTag
		{
			public string Postion;
			public string text;
		}

	}
}
