// Decompiled with JetBrains decompiler
// Type: ListViewEmbeddedControls.ListViewEx
// Assembly: FileCash_v3.0, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
// MVID: 1DECC99D-34C5-4E02-A85D-DC744B5C555A
// Assembly location: C:\Users\Administrateur\Downloads\Virusshare-00001-msil\Constructor.MSIL.ArchSMS.a-33cc8dceb24ed9df287e1f92d02773477af42b8d2b6de53a45bb9b5a3ead070b.exe

using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace ListViewEmbeddedControls
{
  public class ListViewEx : ListView
  {
    private const int LVM_FIRST = 4096;
    private const int LVM_GETCOLUMNORDERARRAY = 4155;
    private const int WM_PAINT = 15;
    private ArrayList _embeddedControls = new ArrayList();

    [DllImport("user32.dll")]
    private static extern IntPtr SendMessage(IntPtr hWnd, int msg, IntPtr wPar, IntPtr lPar);

    protected int[] GetColumnOrder()
    {
      IntPtr num = Marshal.AllocHGlobal(Marshal.SizeOf(typeof (int)) * this.Columns.Count);
      if (ListViewEx.SendMessage(this.Handle, 4155, new IntPtr(this.Columns.Count), num).ToInt32() == 0)
      {
        Marshal.FreeHGlobal(num);
        return (int[]) null;
      }
      int[] destination = new int[this.Columns.Count];
      Marshal.Copy(num, destination, 0, this.Columns.Count);
      Marshal.FreeHGlobal(num);
      return destination;
    }

    protected Rectangle GetSubItemBounds(ListViewItem Item, int SubItem)
    {
      Rectangle subItemBounds = Rectangle.Empty;
      if (Item == null)
        throw new ArgumentNullException(nameof (Item));
      int[] columnOrder = this.GetColumnOrder();
      if (columnOrder == null)
        return subItemBounds;
      if (SubItem >= columnOrder.Length)
        throw new IndexOutOfRangeException("SubItem " + (object) SubItem + " out of range");
      Rectangle bounds = Item.GetBounds(ItemBoundsPortion.Entire);
      int left = bounds.Left;
      int index;
      for (index = 0; index < columnOrder.Length; ++index)
      {
        ColumnHeader column = this.Columns[columnOrder[index]];
        if (column.Index != SubItem)
          left += column.Width;
        else
          break;
      }
      subItemBounds = new Rectangle(left, bounds.Top, this.Columns[columnOrder[index]].Width, bounds.Height);
      return subItemBounds;
    }

    public void AddEmbeddedControl(Control c, int col, int row) => this.AddEmbeddedControl(c, col, row, DockStyle.Fill);

    public void AddEmbeddedControl(Control c, int col, int row, DockStyle dock)
    {
      ListViewEx.EmbeddedControl embeddedControl;
      embeddedControl.Control = c;
      embeddedControl.Column = col;
      embeddedControl.Row = row;
      embeddedControl.Dock = dock;
      embeddedControl.Item = this.Items[row];
      this._embeddedControls.Add((object) embeddedControl);
      c.Click += new EventHandler(this._embeddedControl_Click);
      this.Controls.Add(c);
    }

    public void RemoveEmbeddedControl(Control c)
    {
      if (c == null)
        throw new ArgumentNullException();
      for (int index = 0; index < this._embeddedControls.Count; ++index)
      {
        if (((ListViewEx.EmbeddedControl) this._embeddedControls[index]).Control == c)
        {
          c.Click -= new EventHandler(this._embeddedControl_Click);
          this.Controls.Remove(c);
          this._embeddedControls.RemoveAt(index);
          return;
        }
      }
      throw new Exception("Control not found!");
    }

    public Control GetEmbeddedControl(int col, int row)
    {
      foreach (ListViewEx.EmbeddedControl embeddedControl in this._embeddedControls)
      {
        if (embeddedControl.Row == row && embeddedControl.Column == col)
          return embeddedControl.Control;
      }
      return (Control) null;
    }

    [DefaultValue(View.LargeIcon)]
    public new View View
    {
      get => base.View;
      set
      {
        foreach (ListViewEx.EmbeddedControl embeddedControl in this._embeddedControls)
          embeddedControl.Control.Visible = value == View.Details;
        base.View = value;
      }
    }

    protected override void WndProc(ref Message m)
    {
      if (m.Msg == 15 && this.View == View.Details)
      {
        foreach (ListViewEx.EmbeddedControl embeddedControl in this._embeddedControls)
        {
          Rectangle subItemBounds = this.GetSubItemBounds(embeddedControl.Item, embeddedControl.Column);
          if (this.HeaderStyle != ColumnHeaderStyle.None && subItemBounds.Top < this.Font.Height)
          {
            embeddedControl.Control.Visible = false;
          }
          else
          {
            embeddedControl.Control.Visible = true;
            switch (embeddedControl.Dock)
            {
              case DockStyle.None:
                subItemBounds.Size = embeddedControl.Control.Size;
                break;
              case DockStyle.Top:
                subItemBounds.Height = embeddedControl.Control.Height;
                break;
              case DockStyle.Bottom:
                subItemBounds.Offset(0, subItemBounds.Height - embeddedControl.Control.Height);
                subItemBounds.Height = embeddedControl.Control.Height;
                break;
              case DockStyle.Left:
                subItemBounds.Width = embeddedControl.Control.Width;
                break;
              case DockStyle.Right:
                subItemBounds.Offset(subItemBounds.Width - embeddedControl.Control.Width, 0);
                subItemBounds.Width = embeddedControl.Control.Width;
                break;
            }
            embeddedControl.Control.Bounds = subItemBounds;
          }
        }
      }
      base.WndProc(ref m);
    }

    private void _embeddedControl_Click(object sender, EventArgs e)
    {
      foreach (ListViewEx.EmbeddedControl embeddedControl in this._embeddedControls)
      {
        if (embeddedControl.Control == (Control) sender)
        {
          this.SelectedItems.Clear();
          embeddedControl.Item.Selected = true;
        }
      }
    }

    private struct EmbeddedControl
    {
      public Control Control;
      public int Column;
      public int Row;
      public DockStyle Dock;
      public ListViewItem Item;
    }
  }
}
