using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace EhLib
{
  [DesignerCategory("Code")]
  //[Designer("EhLib.Design.DataVertGridDesigner" + EhLibUtils.EhLibDesignDesignerVersionInfo)]
  [ToolboxItem(true)]
  [Description("Vertical grid that shows only one list item at a time")]
  //[ToolboxBitmap(typeof(DataVertGridEh), "ToolboxBitmaps.EhLib_DataVertGrid.bmp")]
  //[ToolboxBitmap(typeof(DataGridEh))]
  //[ComplexBindingProperties("DataSource", "DataMember")]
  public class DataPropertyGridEh : DataVertGridEh
  {
    #region private consts
    #endregion

    #region privates
    private DataPropertyGridPrintService printService;
    #endregion

    public DataPropertyGridEh()
    {
    }

    #region designtime properties
    [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
    //[Editor("EhLib.Design.ObjectSelectorEditorByType" + EhLibUtils.EhLibDesignDesignerVersionInfo, "System.Drawing.Design.UITypeEditor, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")]
    [Editor("EhLib.Design.PrintServiceComponentEditor" + EhLibUtils.EhLibDesignDesignerVersionInfo, "System.Drawing.Design.UITypeEditor, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")]
    [DefaultValue(null)]
    public DataPropertyGridPrintService PrintService
    {
      get
      {
        return printService;
      }
      set
      {
        if (printService != value)
        {
          if (printService != null)
            printService.Dispose();

          printService = value;
        }
      }
    }
    #endregion

    #region protected properties
    [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
    new protected DataVertGridColumnOptions ColumnOptions
    {
      get { return base.ColumnOptions; }
    }

    protected override DataVertGridManager DefaultManager
    {
      get
      {
        return DataPropertyGridManager.DefaultManager;
      }
    }

    [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
    [Browsable(false)]
    new public DataPropertyGridManager Manager
    {
      get
      {
        return (DataPropertyGridManager)base.Manager;
      }
      set
      {
        base.Manager = Manager;
      }
    }
    #endregion

    #region methods
    public override void InteractiveFocusCell(int col, int row, InteractiveActionSource actionSource)
    {
      if ((col == Col) && (row == Row)) return;
      if (!PrepareChangeCellPos()) return;
      if (!DataLink.Active) return;

      FocusCell(Col, row, false);
    }

    protected internal override void CurrencyManagerPositionChanged(object sender, EventArgs e)
    {
      UpdateDataColumns();
      UpdateRowHeights();
    }

    protected internal override void UpdateListItems()
    {
      listItems.Clear();
      if (CurrencyManager != null && CurrencyManager.Count > 0)
        listItems.Add(CurrencyManager.Current);
    }

    internal protected override void UpdateBaseColWidths()
    {
      int width = HorzAxis.GridClientLen;

      int titleColWidth = TitleColumn.Width;
                                                                                
      if (titleColWidth > width - 4)
        titleColWidth = width - 4;
      if (titleColWidth < 0)
        titleColWidth = width / 2;
      DataColumnWidth = width - titleColWidth;
      if (TitleColumnBaseColIndex >= 0)
        ColWidths[TitleColumnBaseColIndex] = titleColWidth;
      ColWidths[TitleColumnBaseColIndex+1] = DataColumnWidth;
    }

    protected internal override bool CanShowScrollBar(Orientation kind)
    {
      if (kind == Orientation.Horizontal)
        return false;
      else
        return base.CanShowScrollBar(kind);
    }

    protected override void OnResize(EventArgs e)
    {
      base.OnResize(e);
      UpdateDataColumns();
      UpdateRowHeights();
    }
    #endregion
  }
}
