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;
42 private EventHandlerList _handlers = new EventHandlerList();
44 public ContextMenuStrip ColumnHeaderContextMenuStrip { get; set; }
46 public event EventHandler VScrolled;
47 public event EventHandler HScrolled;
49 public DetailsListView()
53 HideSelection = false;
54 DoubleBuffered = true;
57 //[System.ComponentModel.DefaultValue(0),
58 // System.ComponentModel.RefreshProperties(System.ComponentModel.RefreshProperties.Repaint)]
59 //public new int VirtualListSize
61 // get { return base.VirtualListSize; }
64 // if (value == base.VirtualListSize) return;
65 // if (base.VirtualListSize > 0 && value > 0)
68 // if (!this.IsDisposed)
70 // if (base.VirtualListSize < value)
72 // if (this.TopItem == null)
78 // topIndex = this.TopItem.Index;
80 // topIndex = Math.Min(topIndex, Math.Abs(value - 1));
81 // this.TopItem = this.Items[topIndex];
85 // if (this.TopItem == null)
93 // this.TopItem = this.Items[0];
97 // base.VirtualListSize = value;
102 /// 複数選択時の起点になるアイテム (selection mark) の位置を取得・設定する
105 /// Items[idx].Selected の設定では mark が設定されるが、SelectedIndices.Add(idx) では設定されないため、
108 public int SelectionMark
110 get { return Win32Api.ListView_GetSelectionMark(this.Handle); }
111 set { Win32Api.ListView_SetSelectionMark(this.Handle, value); }
114 public void ChangeItemBackColor(int index, Color backColor)
116 ChangeSubItemBackColor(index, 0, backColor);
119 public void ChangeItemForeColor(int index, Color foreColor)
121 ChangeSubItemForeColor(index, 0, foreColor);
124 public void ChangeItemFont(int index, Font fnt)
126 ChangeSubItemFont(index, 0, fnt);
129 public void ChangeItemFontAndColor(int index, Color foreColor, Font fnt)
131 ChangeSubItemStyles(index, 0, BackColor, foreColor, fnt);
134 public void ChangeItemStyles(int index, Color backColor, Color foreColor, Font fnt)
136 ChangeSubItemStyles(index, 0, backColor, foreColor, fnt);
139 public void ChangeSubItemBackColor(int itemIndex, int subitemIndex, Color backColor)
141 var item = this.Items[itemIndex];
142 item.SubItems[subitemIndex].BackColor = backColor;
143 SetUpdateBounds(item, subitemIndex);
145 this.changeBounds = Rectangle.Empty;
148 public void ChangeSubItemForeColor(int itemIndex, int subitemIndex, Color foreColor)
150 var item = this.Items[itemIndex];
151 item.SubItems[subitemIndex].ForeColor = foreColor;
152 SetUpdateBounds(item, subitemIndex);
154 this.changeBounds = Rectangle.Empty;
157 public void ChangeSubItemFont(int itemIndex, int subitemIndex, Font fnt)
159 var item = this.Items[itemIndex];
160 item.SubItems[subitemIndex].Font = fnt;
161 SetUpdateBounds(item, subitemIndex);
163 this.changeBounds = Rectangle.Empty;
166 public void ChangeSubItemFontAndColor(int itemIndex, int subitemIndex, Color foreColor, Font fnt)
168 var item = this.Items[itemIndex];
169 var subItem = item.SubItems[subitemIndex];
170 subItem.ForeColor = foreColor;
172 SetUpdateBounds(item, subitemIndex);
174 this.changeBounds = Rectangle.Empty;
177 public void ChangeSubItemStyles(int itemIndex, int subitemIndex, Color backColor, Color foreColor, Font fnt)
179 var item = this.Items[itemIndex];
180 var subItem = item.SubItems[subitemIndex];
181 subItem.BackColor = backColor;
182 subItem.ForeColor = foreColor;
184 SetUpdateBounds(item, subitemIndex);
186 this.changeBounds = Rectangle.Empty;
189 private void SetUpdateBounds(ListViewItem item, int subItemIndex)
193 if (subItemIndex > this.Columns.Count)
195 throw new ArgumentOutOfRangeException("subItemIndex");
197 if (item.UseItemStyleForSubItems)
199 this.changeBounds = item.Bounds;
203 this.changeBounds = this.GetSubItemBounds(item, subItemIndex);
206 catch (ArgumentException)
208 //タイミングによりBoundsプロパティが取れない?
209 this.changeBounds = Rectangle.Empty;
213 private Rectangle GetSubItemBounds(ListViewItem item, int subitemIndex)
215 if (subitemIndex == 0 && this.Columns.Count > 0)
217 Rectangle col0 = item.Bounds;
218 return new Rectangle(col0.Left, col0.Top, item.SubItems[1].Bounds.X + 1, col0.Height);
222 return item.SubItems[subitemIndex].Bounds;
226 [StructLayout(LayoutKind.Sequential)]
227 private struct SCROLLINFO
235 public int nTrackPos;
238 private enum ScrollBarDirection
246 private enum ScrollInfoMask
251 SIF_DISABLENOSCROLL = 0x8,
253 SIF_ALL = (SIF_RANGE | SIF_PAGE | SIF_POS | SIF_TRACKPOS),
256 [DllImport("user32.dll")]
257 private static extern int GetScrollInfo(IntPtr hWnd, ScrollBarDirection fnBar, ref SCROLLINFO lpsi);
259 private SCROLLINFO si = new SCROLLINFO {
260 cbSize = Marshal.SizeOf(new SCROLLINFO()),
261 fMask = (int)ScrollInfoMask.SIF_POS
264 [DebuggerStepThrough()]
265 protected override void WndProc(ref Message m)
267 const int WM_ERASEBKGND = 0x14;
268 const int WM_PAINT = 0xF;
269 const int WM_MOUSEWHEEL = 0x20A;
270 const int WM_MOUSEHWHEEL = 0x20E;
271 const int WM_HSCROLL = 0x114;
272 const int WM_VSCROLL = 0x115;
273 const int WM_KEYDOWN = 0x100;
274 const int WM_CONTEXTMENU = 0x7B;
275 const int LVM_SETITEMCOUNT = 0x102F;
276 const long LVSICF_NOSCROLL = 0x2;
277 const long LVSICF_NOINVALIDATEALL = 0x1;
285 if (this.changeBounds != Rectangle.Empty)
289 if (this.changeBounds != Rectangle.Empty)
291 Win32Api.ValidateRect(this.Handle, IntPtr.Zero);
292 this.Invalidate(this.changeBounds);
293 this.changeBounds = Rectangle.Empty;
297 if (HScrolled != null)
298 HScrolled(this, EventArgs.Empty);
301 if (VScrolled != null)
302 VScrolled(this, EventArgs.Empty);
307 if (GetScrollInfo(this.Handle, ScrollBarDirection.SB_VERT, ref si) != 0)
309 if (GetScrollInfo(this.Handle, ScrollBarDirection.SB_HORZ, ref si) != 0)
313 if (m.WParam != this.Handle)
316 if (this.ColumnHeaderContextMenuStrip != null)
317 this.ColumnHeaderContextMenuStrip.Show(new Point(m.LParam.ToInt32()));
321 case LVM_SETITEMCOUNT:
322 m.LParam = new IntPtr(LVSICF_NOSCROLL | LVSICF_NOINVALIDATEALL);
330 catch (ArgumentOutOfRangeException)
332 //Substringでlengthが0以下。アイコンサイズが影響?
334 catch (AccessViolationException)
338 if (this.IsDisposed) return;
341 if (GetScrollInfo(this.Handle, ScrollBarDirection.SB_VERT, ref si) != 0 && vPos != si.nPos)
342 if (VScrolled != null)
343 VScrolled(this, EventArgs.Empty);
345 if (GetScrollInfo(this.Handle, ScrollBarDirection.SB_HORZ, ref si) != 0 && hPos != si.nPos)
346 if (HScrolled != null)
347 HScrolled(this, EventArgs.Empty);