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.
30 using System.Collections.Generic;
33 using System.Windows.Forms;
35 using System.ComponentModel;
36 using System.Runtime.InteropServices;
37 using System.Diagnostics;
39 namespace OpenTween.OpenTweenCustomControl
41 public sealed class DetailsListView : ListView
43 private Rectangle changeBounds;
45 public ContextMenuStrip? ColumnHeaderContextMenuStrip { get; set; }
47 public event EventHandler? VScrolled;
48 public event EventHandler? HScrolled;
50 public DetailsListView()
54 HideSelection = false;
55 DoubleBuffered = true;
59 /// 複数選択時の起点になるアイテム (selection mark) の位置を取得・設定する
62 /// Items[idx].Selected の設定では mark が設定されるが、SelectedIndices.Add(idx) では設定されないため、
65 public int SelectionMark
67 get => NativeMethods.ListView_GetSelectionMark(this.Handle);
68 set => NativeMethods.ListView_SetSelectionMark(this.Handle, value);
71 public void SelectItems(int[] indices)
73 foreach (var index in indices)
75 if (index < 0 || index >= this.VirtualListSize)
76 throw new ArgumentOutOfRangeException(nameof(indices));
78 NativeMethods.SelectItem(this, index);
81 this.OnSelectedIndexChanged(EventArgs.Empty);
84 public void SelectAllItems()
86 NativeMethods.SelectAllItems(this);
88 this.OnSelectedIndexChanged(EventArgs.Empty);
91 public void ChangeItemBackColor(ListViewItem item, Color backColor)
93 if (item.BackColor == backColor)
96 item.BackColor = backColor;
97 this.RefreshItemBounds(item);
100 public void ChangeItemForeColor(ListViewItem item, Color foreColor)
102 if (item.ForeColor == foreColor)
105 item.ForeColor = foreColor;
106 this.RefreshItemBounds(item);
109 public void ChangeItemFontAndColor(ListViewItem item, Color foreColor, Font fnt)
111 if (item.ForeColor == foreColor && item.Font == fnt)
114 item.ForeColor = foreColor;
116 this.RefreshItemBounds(item);
119 private void RefreshItemBounds(ListViewItem item)
123 var itemBounds = item.Bounds;
124 var drawBounds = Rectangle.Intersect(this.ClientRectangle, itemBounds);
125 if (drawBounds == Rectangle.Empty)
128 this.changeBounds = drawBounds;
130 this.changeBounds = Rectangle.Empty;
132 catch (ArgumentException)
134 //タイミングによりBoundsプロパティが取れない?
135 this.changeBounds = Rectangle.Empty;
139 [StructLayout(LayoutKind.Sequential)]
142 public IntPtr hwndFrom;
143 public IntPtr idFrom;
147 [DebuggerStepThrough]
148 protected override void WndProc(ref Message m)
150 const int WM_ERASEBKGND = 0x14;
151 const int WM_PAINT = 0xF;
152 const int WM_MOUSEWHEEL = 0x20A;
153 const int WM_MOUSEHWHEEL = 0x20E;
154 const int WM_HSCROLL = 0x114;
155 const int WM_VSCROLL = 0x115;
156 const int WM_KEYDOWN = 0x100;
157 const int WM_USER = 0x400;
158 const int WM_REFLECT = WM_USER + 0x1C00;
159 const int WM_NOTIFY = 0x004E;
160 const int WM_CONTEXTMENU = 0x7B;
161 const int LVM_SETITEMCOUNT = 0x102F;
162 const int LVN_ODSTATECHANGED = ((0 - 100) - 15);
163 const long LVSICF_NOSCROLL = 0x2;
164 const long LVSICF_NOINVALIDATEALL = 0x1;
172 if (this.changeBounds != Rectangle.Empty)
176 if (this.changeBounds != Rectangle.Empty)
178 NativeMethods.ValidateRect(this.Handle, IntPtr.Zero);
179 this.Invalidate(this.changeBounds);
180 this.changeBounds = Rectangle.Empty;
184 HScrolled?.Invoke(this, EventArgs.Empty);
187 VScrolled?.Invoke(this, EventArgs.Empty);
192 vPos = NativeMethods.GetScrollPosition(this, NativeMethods.ScrollBarDirection.SB_VERT);
193 hPos = NativeMethods.GetScrollPosition(this, NativeMethods.ScrollBarDirection.SB_HORZ);
196 if (m.WParam != this.Handle)
199 this.ColumnHeaderContextMenuStrip?.Show(new Point(m.LParam.ToInt32()));
203 case LVM_SETITEMCOUNT:
204 m.LParam = new IntPtr(LVSICF_NOSCROLL | LVSICF_NOINVALIDATEALL);
206 case WM_REFLECT + WM_NOTIFY:
207 var nmhdr = Marshal.PtrToStructure<NMHDR>(m.LParam);
209 // Ctrl+クリックで選択状態を変更した場合にイベントが発生しない問題への対処
210 if (nmhdr.code == LVN_ODSTATECHANGED)
211 this.OnSelectedIndexChanged(EventArgs.Empty);
219 catch (ArgumentOutOfRangeException)
221 //Substringでlengthが0以下。アイコンサイズが影響?
223 catch (AccessViolationException)
227 if (this.IsDisposed) return;
230 if (vPos != NativeMethods.GetScrollPosition(this, NativeMethods.ScrollBarDirection.SB_VERT))
231 VScrolled?.Invoke(this, EventArgs.Empty);
233 if (hPos != NativeMethods.GetScrollPosition(this, NativeMethods.ScrollBarDirection.SB_HORZ))
234 HScrolled?.Invoke(this, EventArgs.Empty);