OSDN Git Service

Merge branch 'cleanup'
[opentween/open-tween.git] / OpenTween / DetailsListView.cs
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.
9 // 
10 // This file is part of OpenTween.
11 // 
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)
15 // any later version.
16 // 
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
20 // for more details. 
21 // 
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.
26
27 using System;
28 using System.Collections.Generic;
29 using System.Linq;
30 using System.Text;
31 using System.Windows.Forms;
32 using System.Drawing;
33 using System.ComponentModel;
34 using System.Runtime.InteropServices;
35 using System.Diagnostics;
36
37 namespace OpenTween.OpenTweenCustomControl
38 {
39     public sealed class DetailsListView : ListView
40     {
41         private Rectangle changeBounds;
42
43         public ContextMenuStrip ColumnHeaderContextMenuStrip { get; set; }
44
45         public event EventHandler VScrolled;
46         public event EventHandler HScrolled;
47
48         public DetailsListView()
49         {
50             View = View.Details;
51             FullRowSelect = true;
52             HideSelection = false;
53             DoubleBuffered = true;
54         }
55
56         //[System.ComponentModel.DefaultValue(0),
57         // System.ComponentModel.RefreshProperties(System.ComponentModel.RefreshProperties.Repaint)]
58         //public new int VirtualListSize
59         //{
60         //    get { return base.VirtualListSize; }
61         //    set
62         //    {
63         //        if (value == base.VirtualListSize) return;
64         //        if (base.VirtualListSize > 0 && value > 0)
65         //        {
66         //            int topIndex = 0;
67         //            if (!this.IsDisposed)
68         //            {
69         //                if (base.VirtualListSize < value)
70         //                {
71         //                    if (this.TopItem == null)
72         //                    {
73         //                        topIndex = 0;
74         //                    }
75         //                    else
76         //                    {
77         //                        topIndex = this.TopItem.Index;
78         //                    }
79         //                    topIndex = Math.Min(topIndex, Math.Abs(value - 1));
80         //                    this.TopItem = this.Items[topIndex];
81         //                }
82         //                else
83         //                {
84         //                    if (this.TopItem == null)
85         //                    {
86         //                        topIndex = 0;
87         //                    }
88         //                    else
89         //                    {
90         //
91         //                    }
92         //                    this.TopItem = this.Items[0];
93         //                }
94         //            }
95         //        }
96         //        base.VirtualListSize = value;
97         //    }
98         //}
99
100         /// <summary>
101         /// 複数選択時の起点になるアイテム (selection mark) の位置を取得・設定する
102         /// </summary>
103         /// <remarks>
104         /// Items[idx].Selected の設定では mark が設定されるが、SelectedIndices.Add(idx) では設定されないため、
105         /// 主に後者と合わせて使用する
106         /// </remarks>
107         public int SelectionMark
108         {
109             get => NativeMethods.ListView_GetSelectionMark(this.Handle);
110             set => NativeMethods.ListView_SetSelectionMark(this.Handle, value);
111         }
112
113         public void SelectItems(int[] indices)
114         {
115             foreach (var index in indices)
116             {
117                 if (index < 0 || index >= this.VirtualListSize)
118                     throw new ArgumentOutOfRangeException(nameof(indices));
119
120                 NativeMethods.SelectItem(this, index);
121             }
122
123             this.OnSelectedIndexChanged(EventArgs.Empty);
124         }
125
126         public void SelectAllItems()
127         {
128             NativeMethods.SelectAllItems(this);
129
130             this.OnSelectedIndexChanged(EventArgs.Empty);
131         }
132
133         public void ChangeItemBackColor(ListViewItem item, Color backColor)
134         {
135             if (item.BackColor == backColor)
136                 return;
137
138             item.BackColor = backColor;
139             this.RefreshItemBounds(item);
140         }
141
142         public void ChangeItemForeColor(ListViewItem item, Color foreColor)
143         {
144             if (item.ForeColor == foreColor)
145                 return;
146
147             item.ForeColor = foreColor;
148             this.RefreshItemBounds(item);
149         }
150
151         public void ChangeItemFontAndColor(ListViewItem item, Color foreColor, Font fnt)
152         {
153             if (item.ForeColor == foreColor && item.Font == fnt)
154                 return;
155
156             item.ForeColor = foreColor;
157             item.Font = fnt;
158             this.RefreshItemBounds(item);
159         }
160
161         private void RefreshItemBounds(ListViewItem item)
162         {
163             try
164             {
165                 var itemBounds = item.Bounds;
166                 var drawBounds = Rectangle.Intersect(this.ClientRectangle, itemBounds);
167                 if (drawBounds == Rectangle.Empty)
168                     return;
169
170                 this.changeBounds = drawBounds;
171                 this.Update();
172                 this.changeBounds = Rectangle.Empty;
173             }
174             catch (ArgumentException)
175             {
176                 //タイミングによりBoundsプロパティが取れない?
177                 this.changeBounds = Rectangle.Empty;
178             }
179         }
180
181         [StructLayout(LayoutKind.Sequential)]
182         private struct NMHDR
183         {
184             public IntPtr hwndFrom;
185             public IntPtr idFrom;
186             public int code;
187         }
188
189         [DebuggerStepThrough]
190         protected override void WndProc(ref Message m)
191         {
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;
207
208             var hPos = -1;
209             var vPos = -1;
210
211             switch (m.Msg)
212             {
213                 case WM_ERASEBKGND:
214                     if (this.changeBounds != Rectangle.Empty)
215                         m.Msg = 0;
216                     break;
217                 case WM_PAINT:
218                     if (this.changeBounds != Rectangle.Empty)
219                     {
220                         NativeMethods.ValidateRect(this.Handle, IntPtr.Zero);
221                         this.Invalidate(this.changeBounds);
222                         this.changeBounds = Rectangle.Empty;
223                     }
224                     break;
225                 case WM_HSCROLL:
226                     HScrolled?.Invoke(this, EventArgs.Empty);
227                     break;
228                 case WM_VSCROLL:
229                     VScrolled?.Invoke(this, EventArgs.Empty);
230                     break;
231                 case WM_MOUSEWHEEL:
232                 case WM_MOUSEHWHEEL:
233                 case WM_KEYDOWN:
234                     vPos = NativeMethods.GetScrollPosition(this, NativeMethods.ScrollBarDirection.SB_VERT);
235                     hPos = NativeMethods.GetScrollPosition(this, NativeMethods.ScrollBarDirection.SB_HORZ);
236                     break;
237                 case WM_CONTEXTMENU:
238                     if (m.WParam != this.Handle)
239                     {
240                         //カラムヘッダメニューを表示
241                         this.ColumnHeaderContextMenuStrip?.Show(new Point(m.LParam.ToInt32()));
242                         return;
243                     }
244                     break;
245                 case LVM_SETITEMCOUNT:
246                     m.LParam = new IntPtr(LVSICF_NOSCROLL | LVSICF_NOINVALIDATEALL);
247                     break;
248                 case WM_REFLECT + WM_NOTIFY:
249                     var nmhdr = Marshal.PtrToStructure<NMHDR>(m.LParam);
250
251                     // Ctrl+クリックで選択状態を変更した場合にイベントが発生しない問題への対処
252                     if (nmhdr.code == LVN_ODSTATECHANGED)
253                         this.OnSelectedIndexChanged(EventArgs.Empty);
254                     break;
255             }
256
257             try
258             {
259                 base.WndProc(ref m);
260             }
261             catch (ArgumentOutOfRangeException)
262             {
263                 //Substringでlengthが0以下。アイコンサイズが影響?
264             }
265             catch (AccessViolationException)
266             {
267                 //WndProcのさらに先で発生する。
268             }
269             if (this.IsDisposed) return;
270
271             if (vPos != -1)
272                 if (vPos != NativeMethods.GetScrollPosition(this, NativeMethods.ScrollBarDirection.SB_VERT))
273                     VScrolled?.Invoke(this, EventArgs.Empty);
274             if (hPos != -1)
275                 if (hPos != NativeMethods.GetScrollPosition(this, NativeMethods.ScrollBarDirection.SB_HORZ))
276                     HScrolled?.Invoke(this, EventArgs.Empty);
277         }
278    }
279 }