OSDN Git Service

PostClass.CreatedAtの型をDateTimeUtcに変更
[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(int index, Color backColor)
134         {
135             ChangeSubItemBackColor(index, 0, backColor);
136         }
137
138         public void ChangeItemForeColor(int index, Color foreColor)
139         {
140             ChangeSubItemForeColor(index, 0, foreColor);
141         }
142
143         public void ChangeItemFont(int index, Font fnt)
144         {
145             ChangeSubItemFont(index, 0, fnt);
146         }
147
148         public void ChangeItemFontAndColor(int index, Color foreColor, Font fnt)
149         {
150             ChangeSubItemStyles(index, 0, BackColor, foreColor, fnt);
151         }
152
153         public void ChangeItemStyles(int index, Color backColor, Color foreColor, Font fnt)
154         {
155             ChangeSubItemStyles(index, 0, backColor, foreColor, fnt);
156         }
157
158         public void ChangeSubItemBackColor(int itemIndex, int subitemIndex, Color backColor)
159         {
160             var item = this.Items[itemIndex];
161             item.SubItems[subitemIndex].BackColor = backColor;
162             SetUpdateBounds(item, subitemIndex);
163             this.Update();
164             this.changeBounds = Rectangle.Empty;
165         }
166
167         public void ChangeSubItemForeColor(int itemIndex, int subitemIndex, Color foreColor)
168         {
169             var item = this.Items[itemIndex];
170             item.SubItems[subitemIndex].ForeColor = foreColor;
171             SetUpdateBounds(item, subitemIndex);
172             this.Update();
173             this.changeBounds = Rectangle.Empty;
174         }
175
176         public void ChangeSubItemFont(int itemIndex, int subitemIndex, Font fnt)
177         {
178             var item = this.Items[itemIndex];
179             item.SubItems[subitemIndex].Font = fnt;
180             SetUpdateBounds(item, subitemIndex);
181             this.Update();
182             this.changeBounds = Rectangle.Empty;
183         }
184
185         public void ChangeSubItemFontAndColor(int itemIndex, int subitemIndex, Color foreColor, Font fnt)
186         {
187             var item = this.Items[itemIndex];
188             var subItem = item.SubItems[subitemIndex];
189             subItem.ForeColor = foreColor;
190             subItem.Font = fnt;
191             SetUpdateBounds(item, subitemIndex);
192             this.Update();
193             this.changeBounds = Rectangle.Empty;
194         }
195
196         public void ChangeSubItemStyles(int itemIndex, int subitemIndex, Color backColor, Color foreColor, Font fnt)
197         {
198             var item = this.Items[itemIndex];
199             var subItem = item.SubItems[subitemIndex];
200             subItem.BackColor = backColor;
201             subItem.ForeColor = foreColor;
202             subItem.Font = fnt;
203             SetUpdateBounds(item, subitemIndex);
204             this.Update();
205             this.changeBounds = Rectangle.Empty;
206         }
207
208         private void SetUpdateBounds(ListViewItem item, int subItemIndex)
209         {
210             try
211             {
212                 if (subItemIndex > this.Columns.Count)
213                 {
214                     throw new ArgumentOutOfRangeException(nameof(subItemIndex));
215                 }
216                 if (item.UseItemStyleForSubItems)
217                 {
218                     this.changeBounds = item.Bounds;
219                 }
220                 else
221                 {
222                     this.changeBounds = this.GetSubItemBounds(item, subItemIndex);
223                 }
224             }
225             catch (ArgumentException)
226             {
227                 //タイミングによりBoundsプロパティが取れない?
228                 this.changeBounds = Rectangle.Empty;
229             }
230         }
231
232         private Rectangle GetSubItemBounds(ListViewItem item, int subitemIndex)
233         {
234             if (subitemIndex == 0 && this.Columns.Count > 0)
235             {
236                 Rectangle col0 = item.Bounds;
237                 return new Rectangle(col0.Left, col0.Top, item.SubItems[1].Bounds.X + 1, col0.Height);
238             }
239             else
240             {
241                 return item.SubItems[subitemIndex].Bounds;
242             }
243         }
244
245         [DebuggerStepThrough]
246         protected override void WndProc(ref Message m)
247         {
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;
259
260             int hPos = -1;
261             int vPos = -1;
262
263             switch (m.Msg)
264             {
265                 case WM_ERASEBKGND:
266                     if (this.changeBounds != Rectangle.Empty)
267                         m.Msg = 0;
268                     break;
269                 case WM_PAINT:
270                     if (this.changeBounds != Rectangle.Empty)
271                     {
272                         NativeMethods.ValidateRect(this.Handle, IntPtr.Zero);
273                         this.Invalidate(this.changeBounds);
274                         this.changeBounds = Rectangle.Empty;
275                     }
276                     break;
277                 case WM_HSCROLL:
278                     HScrolled?.Invoke(this, EventArgs.Empty);
279                     break;
280                 case WM_VSCROLL:
281                     VScrolled?.Invoke(this, EventArgs.Empty);
282                     break;
283                 case WM_MOUSEWHEEL:
284                 case WM_MOUSEHWHEEL:
285                 case WM_KEYDOWN:
286                     vPos = NativeMethods.GetScrollPosition(this, NativeMethods.ScrollBarDirection.SB_VERT);
287                     hPos = NativeMethods.GetScrollPosition(this, NativeMethods.ScrollBarDirection.SB_HORZ);
288                     break;
289                 case WM_CONTEXTMENU:
290                     if (m.WParam != this.Handle)
291                     {
292                         //カラムヘッダメニューを表示
293                         this.ColumnHeaderContextMenuStrip?.Show(new Point(m.LParam.ToInt32()));
294                         return;
295                     }
296                     break;
297                 case LVM_SETITEMCOUNT:
298                     m.LParam = new IntPtr(LVSICF_NOSCROLL | LVSICF_NOINVALIDATEALL);
299                     break;
300             }
301
302             try
303             {
304                 base.WndProc(ref m);
305             }
306             catch (ArgumentOutOfRangeException)
307             {
308                 //Substringでlengthが0以下。アイコンサイズが影響?
309             }
310             catch (AccessViolationException)
311             {
312                 //WndProcのさらに先で発生する。
313             }
314             if (this.IsDisposed) return;
315
316             if (vPos != -1)
317                 if (vPos != NativeMethods.GetScrollPosition(this, NativeMethods.ScrollBarDirection.SB_VERT))
318                     VScrolled?.Invoke(this, EventArgs.Empty);
319             if (hPos != -1)
320                 if (hPos != NativeMethods.GetScrollPosition(this, NativeMethods.ScrollBarDirection.SB_HORZ))
321                     HScrolled?.Invoke(this, EventArgs.Empty);
322         }
323    }
324 }