1 // Copyright 2001-2019 Crytek GmbH / Crytek Group. All rights reserved.
4 using System
.Collections
.Generic
;
7 using System
.Reflection
;
8 using System
.ComponentModel
;
9 using System
.Windows
.Forms
;
13 class BucketListView
: ListView
15 public BucketListView(BucketSet bucketSet
)
17 InitializeComponent();
20 Dock
= DockStyle
.Fill
;
24 VirtualListSize
= bucketSet
.m_buckets
.Count
;
25 RetrieveVirtualItem
+= new RetrieveVirtualItemEventHandler(bucketsListView_RetrieveVirtualItem
);
26 CacheVirtualItems
+= new CacheVirtualItemsEventHandler(bucketsListView_CacheVirtualItems
);
27 StatoscopeForm
.SetDoubleBuffered(this);
29 int numColHeaders
= CountHeaders(bucketSet
);
30 int colHeaderWidth
= Math
.Max(50, (Width
) / numColHeaders
);
32 Columns
.Add(LogControl
.CreateColumnHeader("Bucket", colHeaderWidth
));
33 CreateHeaders(bucketSet
, colHeaderWidth
);
35 m_bucketSet
= bucketSet
;
36 m_lviCache
= new List
<ListViewItem
>(bucketSet
.m_buckets
.Count
);
40 public void UpdateLviCache()
44 foreach (Bucket bucket
in m_bucketSet
.m_buckets
)
46 ListViewItem lvi
= new ListViewItem(bucket
.RangeDescription
);
48 foreach (PropertyInfo pi
in m_columnProperties
)
49 lvi
.SubItems
.Add(GetPropertyValAsString(pi
, bucket
));
55 protected override void OnCreateControl()
57 base.OnCreateControl();
59 ContextMenuStrip
= m_contextMenu
;
60 m_copyMenuItem
.Click
+= new EventHandler(m_copyMenuItem_Click
);
63 void bucketsListView_CacheVirtualItems(object sender
, CacheVirtualItemsEventArgs e
)
65 BucketListView blv
= (BucketListView
)sender
;
69 void bucketsListView_RetrieveVirtualItem(object sender
, RetrieveVirtualItemEventArgs e
)
71 BucketListView blv
= (BucketListView
)sender
;
72 e
.Item
= blv
.m_lviCache
[e
.ItemIndex
];
75 private string GetPropertyValAsString(PropertyInfo pi
, Bucket b
)
77 object val
= pi
.GetValue(b
, null);
79 string s
= string.Empty
;
81 s
= string.Format("{0:n0}", val
);
82 else if (val
is float)
83 s
= string.Format("{0:n2}%", val
);
84 else if (val
is TimeSpan
)
85 s
= string.Format("{0:n2}s", ((TimeSpan
)val
).TotalMilliseconds
/ 1000.0f
);
90 void m_copyMenuItem_Click(object sender
, EventArgs e
)
92 StringBuilder sb
= new StringBuilder();
94 sb
.Append("Bucket\t");
95 foreach (PropertyInfo pi
in m_columnProperties
)
96 sb
.AppendFormat("{0}\t", pi
.Name
.ToString());
99 foreach (Bucket bucket
in m_bucketSet
.m_buckets
)
101 sb
.AppendFormat("{0}\t", bucket
.RangeDescription
);
102 foreach (PropertyInfo pi
in m_columnProperties
)
103 sb
.AppendFormat("{0}\t", GetPropertyValAsString(pi
, bucket
));
108 Clipboard
.SetText(sb
.ToString());
111 private Attribute
GetFirstAttribute(PropertyInfo pi
, Type attrType
, bool inherits
)
113 foreach (var attr
in pi
.GetCustomAttributes(attrType
, inherits
))
114 return (Attribute
)attr
;
118 private IEnumerable
<PropertyInfo
> EnumColumns(BucketSet bset
)
120 if (bset
.m_buckets
.Count
> 0)
122 Type bucketType
= bset
.m_buckets
[0].GetType();
124 // Assuming that all buckets have the same type..
126 foreach (PropertyInfo prop
in bucketType
.GetProperties())
128 // Ensure it's browseable
129 var ba
= GetFirstAttribute(prop
, typeof(BrowsableAttribute
), true);
130 bool isBrowseable
= (ba
!= null) ? ((BrowsableAttribute
)ba
).Browsable
: true;
138 private int CountHeaders(BucketSet bucketSet
)
142 foreach (var pi
in EnumColumns(bucketSet
))
148 private void CreateHeaders(BucketSet bucketSet
, int colHeaderWidth
)
150 m_columnProperties
= new List
<PropertyInfo
>();
151 foreach (var pi
in EnumColumns(bucketSet
))
153 var descAttr
= GetFirstAttribute(pi
, typeof(DescriptionAttribute
), true);
154 string desc
= descAttr
!= null ? ((DescriptionAttribute
)descAttr
).Description
: pi
.Name
;
155 Columns
.Add(LogControl
.CreateColumnHeader(desc
, colHeaderWidth
));
156 m_columnProperties
.Add(pi
);
160 private BucketSet m_bucketSet
;
161 private List
<PropertyInfo
> m_columnProperties
;
162 private IContainer components
;
163 private ContextMenuStrip m_contextMenu
;
164 private ToolStripMenuItem m_copyMenuItem
;
165 private List
<ListViewItem
> m_lviCache
;
167 private void InitializeComponent()
169 this.components
= new System
.ComponentModel
.Container();
170 this.m_contextMenu
= new System
.Windows
.Forms
.ContextMenuStrip(this.components
);
171 this.m_copyMenuItem
= new System
.Windows
.Forms
.ToolStripMenuItem();
172 this.m_contextMenu
.SuspendLayout();
173 this.SuspendLayout();
177 this.m_contextMenu
.Items
.AddRange(new System
.Windows
.Forms
.ToolStripItem
[] {
178 this.m_copyMenuItem
});
179 this.m_contextMenu
.Name
= "m_contextMenu";
180 this.m_contextMenu
.Size
= new System
.Drawing
.Size(103, 26);
184 this.m_copyMenuItem
.Name
= "m_copyMenuItem";
185 this.m_copyMenuItem
.Size
= new System
.Drawing
.Size(102, 22);
186 this.m_copyMenuItem
.Text
= "&Copy";
187 this.m_contextMenu
.ResumeLayout(false);
188 this.ResumeLayout(false);