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(ListViewItem item, Color backColor)
135 if (item.BackColor == backColor)
138 item.BackColor = backColor;
139 this.RefreshItemBounds(item);
142 public void ChangeItemForeColor(ListViewItem item, Color foreColor)
144 if (item.ForeColor == foreColor)
147 item.ForeColor = foreColor;
148 this.RefreshItemBounds(item);
151 public void ChangeItemFontAndColor(ListViewItem item, Color foreColor, Font fnt)
153 if (item.ForeColor == foreColor && item.Font == fnt)
156 item.ForeColor = foreColor;
158 this.RefreshItemBounds(item);
161 private void RefreshItemBounds(ListViewItem item)
165 var itemBounds = item.Bounds;
166 var drawBounds = Rectangle.Intersect(this.ClientRectangle, itemBounds);
167 if (drawBounds == Rectangle.Empty)
170 this.changeBounds = drawBounds;
172 this.changeBounds = Rectangle.Empty;
174 catch (ArgumentException)
176 //タイミングによりBoundsプロパティが取れない?
177 this.changeBounds = Rectangle.Empty;
181 [StructLayout(LayoutKind.Sequential)]
184 public IntPtr hwndFrom;
185 public IntPtr idFrom;
189 [DebuggerStepThrough]
190 protected override void WndProc(ref Message m)
192 const int WM_ERASEBKGND = 0x14;
193 const int WM_PAINT = 0xF;
194 const int WM_MOUSEWHEEL = 0x20A;
195 const int WM_MOUSEHWHEEL = 0x20E;
196 const int WM_HSCROLL = 0x114;
197 const int WM_VSCROLL = 0x115;
198 const int WM_KEYDOWN = 0x100;
199 const int WM_USER = 0x400;
200 const int WM_REFLECT = WM_USER + 0x1C00;
201 const int WM_NOTIFY = 0x004E;
202 const int WM_CONTEXTMENU = 0x7B;
203 const int LVM_SETITEMCOUNT = 0x102F;
204 const int LVN_ODSTATECHANGED = ((0 - 100) - 15);
205 const long LVSICF_NOSCROLL = 0x2;
206 const long LVSICF_NOINVALIDATEALL = 0x1;
214 if (this.changeBounds != Rectangle.Empty)
218 if (this.changeBounds != Rectangle.Empty)
220 NativeMethods.ValidateRect(this.Handle, IntPtr.Zero);
221 this.Invalidate(this.changeBounds);
222 this.changeBounds = Rectangle.Empty;
226 HScrolled?.Invoke(this, EventArgs.Empty);
229 VScrolled?.Invoke(this, EventArgs.Empty);
234 vPos = NativeMethods.GetScrollPosition(this, NativeMethods.ScrollBarDirection.SB_VERT);
235 hPos = NativeMethods.GetScrollPosition(this, NativeMethods.ScrollBarDirection.SB_HORZ);
238 if (m.WParam != this.Handle)
241 this.ColumnHeaderContextMenuStrip?.Show(new Point(m.LParam.ToInt32()));
245 case LVM_SETITEMCOUNT:
246 m.LParam = new IntPtr(LVSICF_NOSCROLL | LVSICF_NOINVALIDATEALL);
248 case WM_REFLECT + WM_NOTIFY:
249 var nmhdr = Marshal.PtrToStructure<NMHDR>(m.LParam);
251 // Ctrl+クリックで選択状態を変更した場合にイベントが発生しない問題への対処
252 if (nmhdr.code == LVN_ODSTATECHANGED)
253 this.OnSelectedIndexChanged(EventArgs.Empty);
261 catch (ArgumentOutOfRangeException)
263 //Substringでlengthが0以下。アイコンサイズが影響?
265 catch (AccessViolationException)
269 if (this.IsDisposed) return;
272 if (vPos != NativeMethods.GetScrollPosition(this, NativeMethods.ScrollBarDirection.SB_VERT))
273 VScrolled?.Invoke(this, EventArgs.Empty);
275 if (hPos != NativeMethods.GetScrollPosition(this, NativeMethods.ScrollBarDirection.SB_HORZ))
276 HScrolled?.Invoke(this, EventArgs.Empty);