//----------------------------------------------------------------------------
// ReportList.cs
//
// Custom list box for holding report objects in the C1ReportDesigner app.
//
// This is a part of the C1ReportDesigner package.
// Copyright (C) GrapeCity, Inc
// All rights reserved.
//
// This source code is intended as a supplement to the C1ReportDesigner 
// control and related documentation provided with the product.
//
// See these sources for detailed information regarding the
// C1ReportDesigner product.
//----------------------------------------------------------------------------
using System;
using System.Drawing;
using System.ComponentModel;
using System.Windows.Forms;

using C1.C1Report;

namespace ReportDesignerApp.Util
{
	/// <summary>
	/// ReportList, ReportItem:
	/// helper classes used to store reports in ListView control
	/// </summary>
	internal class ReportList : ListView
	{
		// indices into image list
		internal const int _imgReport = 0;
		internal const int _imgReportContainer = 1;

		private int _index = -1;

        #region Constructors

        public ReportList()
		{
			HideSelection = false;
			FullRowSelect = true;
			HeaderStyle = System.Windows.Forms.ColumnHeaderStyle.None;
			LabelEdit = true;
			MultiSelect = false;
			View = System.Windows.Forms.View.Details;
			Columns.Add(new ColumnHeader());
            Columns[0].Width = Screen.PrimaryScreen.Bounds.Width;
        }

        #endregion

        #region Protected
        protected override void OnSelectedIndexChanged(EventArgs e)
        {
            _index = -1;

            // find item that is selected
            for (int i = 0; i < base.Items.Count && _index < 0; i++)
            {
                if (base.Items[i].Selected)
                    _index = i;
            }

            // nothing selected, find item that has the focus
            if (_index < 0)
            {
                for (int i = 0; i < base.Items.Count && _index < 0; i++)
                {
                    if (base.Items[i].Focused)
                        _index = i;
                }
            }

            // fire event
            if (_index > -1 || base.Items.Count == 0)
            {
                base.OnSelectedIndexChanged(e);
            }
        }
        #endregion

        #region Public properties

        [Browsable(false)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
		public int SelectedIndex
		{
			get 
			{
				// sanity (in case list has been cleared)
				if (_index >= base.Items.Count)
					_index = -1;
				return _index; 
			}
			set
			{ 
                foreach (ListViewItem lvi in base.Items)
                {
                    lvi.Selected = (lvi.Index == value);
                    if (lvi.Selected)
                    {
                        lvi.EnsureVisible();
						if (base.Focused)
						{
							lvi.Focused = true;
						}
                    }
                }
				OnSelectedIndexChanged(EventArgs.Empty);
			}
		}

		[Browsable(false)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
		public ReportItem SelectedItem
		{
			get
            {
                // sanity (item could have been deleted)
                if (_index < 0) return null;
				if (_index >= this.Items.Count)
				{
					OnSelectedIndexChanged(EventArgs.Empty);
				}

                // return value
                return (_index < 0)? null : (ReportItem)base.Items[_index]; 
            }
        }

        #endregion

        #region Public

        public void UpdateIcons()
		{
			// scan all reports on the list
			foreach (ReportItem rpt in Items)
			{
				// scan fields looking for subreports
				int imgIndex = _imgReport;
				foreach (Field f in rpt._report.Fields)
				{
					if (f.Subreport != null)
					{
						imgIndex = _imgReportContainer;
						break;
					}
				}

				// update image
				if (imgIndex != rpt.ImageIndex)
					rpt.ImageIndex = imgIndex;
			}
        }

        #endregion
    }

	/// <summary>
	/// ReportItem
	/// </summary>
	internal class ReportItem : ListViewItem
	{
		internal C1Report _report;

        #region Constructors

        internal ReportItem(C1Report report)
		{
			_report = report;
			base.Text = _report.ReportName;
			base.ImageIndex = ReportList._imgReport;
        }

        #endregion

        #region Public

        public override string ToString()
		{
			return _report.ReportName;
        }

        #endregion
    }
}
