1 // OpenTween - Client of Twitter
2 // Copyright (c) 2007-2011 kiri_feather (@kiri_feather) <kiri.feather@gmail.com>
3 // (c) 2008-2011 Moz (@syo68k)
4 // (c) 2008-2011 takeshik (@takeshik) <http://www.takeshik.org/>
5 // (c) 2010-2011 anis774 (@anis774) <http://d.hatena.ne.jp/anis774/>
6 // (c) 2010-2011 fantasticswallow (@f_swallow) <http://twitter.com/f_swallow>
7 // (c) 2011 kim_upsilon (@kim_upsilon) <https://upsilo.net/~upsilon/>
8 // All rights reserved.
10 // This file is part of OpenTween.
12 // This program is free software; you can redistribute it and/or modify it
13 // under the terms of the GNU General Public License as published by the Free
14 // Software Foundation; either version 3 of the License, or (at your option)
17 // This program is distributed in the hope that it will be useful, but
18 // WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
19 // or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22 // You should have received a copy of the GNU General Public License along
23 // with this program. If not, see <http://www.gnu.org/licenses/>, or write to
24 // the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
25 // Boston, MA 02110-1301, USA.
28 using System.Collections.Generic;
31 using System.Windows.Forms;
33 using System.ComponentModel;
34 using System.Runtime.InteropServices;
35 using System.Diagnostics;
37 namespace OpenTween.OpenTweenCustomControl
39 public sealed class DetailsListView : ListView
41 private Rectangle changeBounds;
43 public ContextMenuStrip ColumnHeaderContextMenuStrip { get; set; }
45 public event EventHandler VScrolled;
46 public event EventHandler HScrolled;
48 public DetailsListView()
52 HideSelection = false;
53 DoubleBuffered = true;
56 //[System.ComponentModel.DefaultValue(0),
57 // System.ComponentModel.RefreshProperties(System.ComponentModel.RefreshProperties.Repaint)]
58 //public new int VirtualListSize
60 // get { return base.VirtualListSize; }
63 // if (value == base.VirtualListSize) return;
64 // if (base.VirtualListSize > 0 && value > 0)
67 // if (!this.IsDisposed)
69 // if (base.VirtualListSize < value)
71 // if (this.TopItem == null)
77 // topIndex = this.TopItem.Index;
79 // topIndex = Math.Min(topIndex, Math.Abs(value - 1));
80 // this.TopItem = this.Items[topIndex];
84 // if (this.TopItem == null)
92 // this.TopItem = this.Items[0];
96 // base.VirtualListSize = value;
101 /// 複数選択時の起点になるアイテム (selection mark) の位置を取得・設定する
104 /// Items[idx].Selected の設定では mark が設定されるが、SelectedIndices.Add(idx) では設定されないため、
107 public int SelectionMark
109 get => NativeMethods.ListView_GetSelectionMark(this.Handle);
110 set => NativeMethods.ListView_SetSelectionMark(this.Handle, value);
113 public void SelectItems(int[] indices)
115 foreach (var index in indices)
117 if (index < 0 || index >= this.VirtualListSize)
118 throw new ArgumentOutOfRangeException(nameof(indices));
120 NativeMethods.SelectItem(this, index);
123 this.OnSelectedIndexChanged(EventArgs.Empty);
126 public void SelectAllItems()
128 NativeMethods.SelectAllItems(this);
130 this.OnSelectedIndexChanged(EventArgs.Empty);
133 public void ChangeItemBackColor(int index, Color backColor)
135 ChangeSubItemBackColor(index, 0, backColor);
138 public void ChangeItemForeColor(int index, Color foreColor)
140 ChangeSubItemForeColor(index, 0, foreColor);
143 public void ChangeItemFont(int index, Font fnt)
145 ChangeSubItemFont(index, 0, fnt);
148 public void ChangeItemFontAndColor(int index, Color foreColor, Font fnt)
150 ChangeSubItemStyles(index, 0, BackColor, foreColor, fnt);
153 public void ChangeItemStyles(int index, Color backColor, Color foreColor, Font fnt)
155 ChangeSubItemStyles(index, 0, backColor, foreColor, fnt);
158 public void ChangeSubItemBackColor(int itemIndex, int subitemIndex, Color backColor)
160 var item = this.Items[itemIndex];
161 item.SubItems[subitemIndex].BackColor = backColor;
162 SetUpdateBounds(item, subitemIndex);
164 this.changeBounds = Rectangle.Empty;
167 public void ChangeSubItemForeColor(int itemIndex, int subitemIndex, Color foreColor)
169 var item = this.Items[itemIndex];
170 item.SubItems[subitemIndex].ForeColor = foreColor;
171 SetUpdateBounds(item, subitemIndex);
173 this.changeBounds = Rectangle.Empty;
176 public void ChangeSubItemFont(int itemIndex, int subitemIndex, Font fnt)
178 var item = this.Items[itemIndex];
179 item.SubItems[subitemIndex].Font = fnt;
180 SetUpdateBounds(item, subitemIndex);
182 this.changeBounds = Rectangle.Empty;
185 public void ChangeSubItemFontAndColor(int itemIndex, int subitemIndex, Color foreColor, Font fnt)
187 var item = this.Items[itemIndex];
188 var subItem = item.SubItems[subitemIndex];
189 subItem.ForeColor = foreColor;
191 SetUpdateBounds(item, subitemIndex);
193 this.changeBounds = Rectangle.Empty;
196 public void ChangeSubItemStyles(int itemIndex, int subitemIndex, Color backColor, Color foreColor, Font fnt)
198 var item = this.Items[itemIndex];
199 var subItem = item.SubItems[subitemIndex];
200 subItem.BackColor = backColor;
201 subItem.ForeColor = foreColor;
203 SetUpdateBounds(item, subitemIndex);
205 this.changeBounds = Rectangle.Empty;
208 private void SetUpdateBounds(ListViewItem item, int subItemIndex)
212 if (subItemIndex > this.Columns.Count)
214 throw new ArgumentOutOfRangeException(nameof(subItemIndex));
216 if (item.UseItemStyleForSubItems)
218 this.changeBounds = item.Bounds;
222 this.changeBounds = this.GetSubItemBounds(item, subItemIndex);
225 catch (ArgumentException)
227 //タイミングによりBoundsプロパティが取れない?
228 this.changeBounds = Rectangle.Empty;
232 private Rectangle GetSubItemBounds(ListViewItem item, int subitemIndex)
234 if (subitemIndex == 0 && this.Columns.Count > 0)
236 Rectangle col0 = item.Bounds;
237 return new Rectangle(col0.Left, col0.Top, item.SubItems[1].Bounds.X + 1, col0.Height);
241 return item.SubItems[subitemIndex].Bounds;
245 [DebuggerStepThrough]
246 protected override void WndProc(ref Message m)
248 const int WM_ERASEBKGND = 0x14;
249 const int WM_PAINT = 0xF;
250 const int WM_MOUSEWHEEL = 0x20A;
251 const int WM_MOUSEHWHEEL = 0x20E;
252 const int WM_HSCROLL = 0x114;
253 const int WM_VSCROLL = 0x115;
254 const int WM_KEYDOWN = 0x100;
255 const int WM_CONTEXTMENU = 0x7B;
256 const int LVM_SETITEMCOUNT = 0x102F;
257 const long LVSICF_NOSCROLL = 0x2;
258 const long LVSICF_NOINVALIDATEALL = 0x1;
266 if (this.changeBounds != Rectangle.Empty)
270 if (this.changeBounds != Rectangle.Empty)
272 NativeMethods.ValidateRect(this.Handle, IntPtr.Zero);
273 this.Invalidate(this.changeBounds);
274 this.changeBounds = Rectangle.Empty;
278 HScrolled?.Invoke(this, EventArgs.Empty);
281 VScrolled?.Invoke(this, EventArgs.Empty);
286 vPos = NativeMethods.GetScrollPosition(this, NativeMethods.ScrollBarDirection.SB_VERT);
287 hPos = NativeMethods.GetScrollPosition(this, NativeMethods.ScrollBarDirection.SB_HORZ);
290 if (m.WParam != this.Handle)
293 this.ColumnHeaderContextMenuStrip?.Show(new Point(m.LParam.ToInt32()));
297 case LVM_SETITEMCOUNT:
298 m.LParam = new IntPtr(LVSICF_NOSCROLL | LVSICF_NOINVALIDATEALL);
306 catch (ArgumentOutOfRangeException)
308 //Substringでlengthが0以下。アイコンサイズが影響?
310 catch (AccessViolationException)
314 if (this.IsDisposed) return;
317 if (vPos != NativeMethods.GetScrollPosition(this, NativeMethods.ScrollBarDirection.SB_VERT))
318 VScrolled?.Invoke(this, EventArgs.Empty);
320 if (hPos != NativeMethods.GetScrollPosition(this, NativeMethods.ScrollBarDirection.SB_HORZ))
321 HScrolled?.Invoke(this, EventArgs.Empty);