OSDN Git Service

Merge branch 'spx268/IconSizeMenu'
[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         private EventHandlerList _handlers = new EventHandlerList();
43
44         public ContextMenuStrip ColumnHeaderContextMenuStrip { get; set; }
45
46         public event EventHandler VScrolled;
47         public event EventHandler HScrolled;
48
49         public DetailsListView()
50         {
51             View = View.Details;
52             FullRowSelect = true;
53             HideSelection = false;
54             DoubleBuffered = true;
55         }
56
57         //[System.ComponentModel.DefaultValue(0),
58         // System.ComponentModel.RefreshProperties(System.ComponentModel.RefreshProperties.Repaint)]
59         //public new int VirtualListSize
60         //{
61         //    get { return base.VirtualListSize; }
62         //    set
63         //    {
64         //        if (value == base.VirtualListSize) return;
65         //        if (base.VirtualListSize > 0 && value > 0)
66         //        {
67         //            int topIndex = 0;
68         //            if (!this.IsDisposed)
69         //            {
70         //                if (base.VirtualListSize < value)
71         //                {
72         //                    if (this.TopItem == null)
73         //                    {
74         //                        topIndex = 0;
75         //                    }
76         //                    else
77         //                    {
78         //                        topIndex = this.TopItem.Index;
79         //                    }
80         //                    topIndex = Math.Min(topIndex, Math.Abs(value - 1));
81         //                    this.TopItem = this.Items[topIndex];
82         //                }
83         //                else
84         //                {
85         //                    if (this.TopItem == null)
86         //                    {
87         //                        topIndex = 0;
88         //                    }
89         //                    else
90         //                    {
91         //
92         //                    }
93         //                    this.TopItem = this.Items[0];
94         //                }
95         //            }
96         //        }
97         //        base.VirtualListSize = value;
98         //    }
99         //}
100
101         public void ChangeItemBackColor(int index, Color backColor)
102         {
103             ChangeSubItemBackColor(index, 0, backColor);
104         }
105
106         public void ChangeItemForeColor(int index, Color foreColor)
107         {
108             ChangeSubItemForeColor(index, 0, foreColor);
109         }
110
111         public void ChangeItemFont(int index, Font fnt)
112         {
113             ChangeSubItemFont(index, 0, fnt);
114         }
115
116         public void ChangeItemFontAndColor(int index, Color foreColor, Font fnt)
117         {
118             ChangeSubItemStyles(index, 0, BackColor, foreColor, fnt);
119         }
120
121         public void ChangeItemStyles(int index, Color backColor, Color foreColor, Font fnt)
122         {
123             ChangeSubItemStyles(index, 0, backColor, foreColor, fnt);
124         }
125
126         public void ChangeSubItemBackColor(int itemIndex, int subitemIndex, Color backColor)
127         {
128             var item = this.Items[itemIndex];
129             item.SubItems[subitemIndex].BackColor = backColor;
130             SetUpdateBounds(item, subitemIndex);
131             this.Update();
132             this.changeBounds = Rectangle.Empty;
133         }
134
135         public void ChangeSubItemForeColor(int itemIndex, int subitemIndex, Color foreColor)
136         {
137             var item = this.Items[itemIndex];
138             item.SubItems[subitemIndex].ForeColor = foreColor;
139             SetUpdateBounds(item, subitemIndex);
140             this.Update();
141             this.changeBounds = Rectangle.Empty;
142         }
143
144         public void ChangeSubItemFont(int itemIndex, int subitemIndex, Font fnt)
145         {
146             var item = this.Items[itemIndex];
147             item.SubItems[subitemIndex].Font = fnt;
148             SetUpdateBounds(item, subitemIndex);
149             this.Update();
150             this.changeBounds = Rectangle.Empty;
151         }
152
153         public void ChangeSubItemFontAndColor(int itemIndex, int subitemIndex, Color foreColor, Font fnt)
154         {
155             var item = this.Items[itemIndex];
156             var subItem = item.SubItems[subitemIndex];
157             subItem.ForeColor = foreColor;
158             subItem.Font = fnt;
159             SetUpdateBounds(item, subitemIndex);
160             this.Update();
161             this.changeBounds = Rectangle.Empty;
162         }
163
164         public void ChangeSubItemStyles(int itemIndex, int subitemIndex, Color backColor, Color foreColor, Font fnt)
165         {
166             var item = this.Items[itemIndex];
167             var subItem = item.SubItems[subitemIndex];
168             subItem.BackColor = backColor;
169             subItem.ForeColor = foreColor;
170             subItem.Font = fnt;
171             SetUpdateBounds(item, subitemIndex);
172             this.Update();
173             this.changeBounds = Rectangle.Empty;
174         }
175
176         private void SetUpdateBounds(ListViewItem item, int subItemIndex)
177         {
178             try
179             {
180                 if (subItemIndex > this.Columns.Count)
181                 {
182                     throw new ArgumentOutOfRangeException("subItemIndex");
183                 }
184                 if (item.UseItemStyleForSubItems)
185                 {
186                     this.changeBounds = item.Bounds;
187                 }
188                 else
189                 {
190                     this.changeBounds = this.GetSubItemBounds(item, subItemIndex);
191                 }
192             }
193             catch (ArgumentException)
194             {
195                 //タイミングによりBoundsプロパティが取れない?
196                 this.changeBounds = Rectangle.Empty;
197             }
198         }
199
200         private Rectangle GetSubItemBounds(ListViewItem item, int subitemIndex)
201         {
202             if (subitemIndex == 0 && this.Columns.Count > 0)
203             {
204                 Rectangle col0 = item.Bounds;
205                 return new Rectangle(col0.Left, col0.Top, item.SubItems[1].Bounds.X + 1, col0.Height);
206             }
207             else
208             {
209                 return item.SubItems[subitemIndex].Bounds;
210             }
211         }
212
213         [StructLayout(LayoutKind.Sequential)]
214         private struct SCROLLINFO
215         {
216             public int cbSize;
217             public int fMask;
218             public int nMin;
219             public int nMax;
220             public int nPage;
221             public int nPos;
222             public int nTrackPos;
223         }
224
225         private enum ScrollBarDirection
226         {
227             SB_HORZ = 0,
228             SB_VERT = 1,
229             SB_CTL = 2,
230             SB_BOTH = 3,
231         }
232
233         private enum ScrollInfoMask
234         {
235             SIF_RANGE = 0x1,
236             SIF_PAGE = 0x2,
237             SIF_POS = 0x4,
238             SIF_DISABLENOSCROLL = 0x8,
239             SIF_TRACKPOS = 0x10,
240             SIF_ALL = (SIF_RANGE | SIF_PAGE | SIF_POS | SIF_TRACKPOS),
241         }
242
243         [DllImport("user32.dll")]
244         private static extern int GetScrollInfo(IntPtr hWnd, ScrollBarDirection fnBar, ref SCROLLINFO lpsi);
245
246         private SCROLLINFO si = new SCROLLINFO {
247             cbSize = Marshal.SizeOf(new SCROLLINFO()),
248             fMask = (int)ScrollInfoMask.SIF_POS
249         };
250
251         [DebuggerStepThrough()]
252         protected override void WndProc(ref Message m)
253         {
254             const int WM_ERASEBKGND = 0x14;
255             const int WM_PAINT = 0xF;
256             const int WM_MOUSEWHEEL = 0x20A;
257             const int WM_MOUSEHWHEEL = 0x20E;
258             const int WM_HSCROLL = 0x114;
259             const int WM_VSCROLL = 0x115;
260             const int WM_KEYDOWN = 0x100;
261             const int WM_CONTEXTMENU = 0x7B;
262             const int LVM_SETITEMCOUNT = 0x102F;
263             const long LVSICF_NOSCROLL = 0x2;
264             const long LVSICF_NOINVALIDATEALL = 0x1;
265
266             int hPos = -1;
267             int vPos = -1;
268
269             switch (m.Msg)
270             {
271                 case WM_ERASEBKGND:
272                     if (this.changeBounds != Rectangle.Empty)
273                         m.Msg = 0;
274                     break;
275                 case WM_PAINT:
276                     if (this.changeBounds != Rectangle.Empty)
277                     {
278                         Win32Api.ValidateRect(this.Handle, IntPtr.Zero);
279                         this.Invalidate(this.changeBounds);
280                         this.changeBounds = Rectangle.Empty;
281                     }
282                     break;
283                 case WM_HSCROLL:
284                     if (HScrolled != null)
285                         HScrolled(this, EventArgs.Empty);
286                     break;
287                 case WM_VSCROLL:
288                     if (VScrolled != null)
289                         VScrolled(this, EventArgs.Empty);
290                     break;
291                 case WM_MOUSEWHEEL:
292                 case WM_MOUSEHWHEEL:
293                 case WM_KEYDOWN:
294                     if (GetScrollInfo(this.Handle, ScrollBarDirection.SB_VERT, ref si) != 0)
295                         vPos = si.nPos;
296                     if (GetScrollInfo(this.Handle, ScrollBarDirection.SB_HORZ, ref si) != 0)
297                         hPos = si.nPos;
298                     break;
299                 case WM_CONTEXTMENU:
300                     if (m.WParam != this.Handle)
301                     {
302                         //カラムヘッダメニューを表示
303                         if (this.ColumnHeaderContextMenuStrip != null)
304                             this.ColumnHeaderContextMenuStrip.Show(new Point(m.LParam.ToInt32()));
305                         return;
306                     }
307                     break;
308                 case LVM_SETITEMCOUNT:
309                     m.LParam = new IntPtr(LVSICF_NOSCROLL | LVSICF_NOINVALIDATEALL);
310                     break;
311             }
312
313             try
314             {
315                 base.WndProc(ref m);
316             }
317             catch (ArgumentOutOfRangeException)
318             {
319                 //Substringでlengthが0以下。アイコンサイズが影響?
320             }
321             catch (AccessViolationException)
322             {
323                 //WndProcのさらに先で発生する。
324             }
325             if (this.IsDisposed) return;
326
327             if (vPos != -1)
328                 if (GetScrollInfo(this.Handle, ScrollBarDirection.SB_VERT, ref si) != 0 && vPos != si.nPos)
329                     if (VScrolled != null)
330                         VScrolled(this, EventArgs.Empty);
331             if (hPos != -1)
332                 if (GetScrollInfo(this.Handle, ScrollBarDirection.SB_HORZ, ref si) != 0 && hPos != si.nPos)
333                     if (HScrolled != null)
334                         HScrolled(this, EventArgs.Empty);
335         }
336    }
337 }