~halega/+junk/sharpdevelop

« back to all changes in this revision

Viewing changes to samples/ComponentInspector/ComponentInspector.Core/Src/Controls/TreeListView.cs

  • Committer: sk
  • Date: 2011-09-10 05:17:57 UTC
  • Revision ID: halega@halega.com-20110910051757-qfouz1llya9m6boy
4.1.0.7915 Release Candidate 1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// <file>
 
2
//     <copyright see="prj:///doc/copyright.txt"/>
 
3
//     <license see="prj:///doc/license.txt"/>
 
4
//     <owner name="Oakland Software Incorporated" email="general@oaklandsoftware.com"/>
 
5
//     <version>$Revision$</version>
 
6
// </file>
 
7
 
 
8
using System;
 
9
using System.Collections;
 
10
using System.Reflection;
 
11
using System.Windows.Forms;
 
12
 
 
13
using NoGoop.Win32;
 
14
 
 
15
namespace NoGoop.Controls
 
16
{
 
17
        // You must add the parent of this control to the control
 
18
        // you want to be the parent.  The parent of this control
 
19
        // is a TreeListPanel which does some funny things in the
 
20
        // WndProc.
 
21
        public class TreeListView : TreeView
 
22
        {
 
23
                protected TreeListPanel         _panel;
 
24
                
 
25
                // See comments below
 
26
                protected bool                  _inMouseDown;
 
27
                protected bool                  _selected;
 
28
                
 
29
                protected bool                  _useCompareTo;
 
30
                
 
31
                // For now, just make this an untyped array list
 
32
                // Though its really supposed to contain only column headers
 
33
                public class ColumnHeaderCollection : ArrayList
 
34
                {
 
35
                }
 
36
                
 
37
                protected ColumnHeaderCollection _columns;
 
38
                
 
39
                internal Panel Panel {
 
40
                        get {
 
41
                                return _panel;
 
42
                        }
 
43
                }
 
44
                
 
45
                internal bool UseCompareTo {
 
46
                        get {
 
47
                                return _useCompareTo;
 
48
                        }
 
49
                        set {
 
50
                                _useCompareTo = value;
 
51
                        }
 
52
                }
 
53
                
 
54
                public TreeListView()
 
55
                {
 
56
                        _columns = new ColumnHeaderCollection();
 
57
                        _panel = new TreeListPanel();
 
58
                        _panel.TreeListView = this;
 
59
                        Scrollable = true;
 
60
                }
 
61
                
 
62
                // Called after this control is set up with its properties
 
63
                internal void SetupPanel()
 
64
                {
 
65
                        _panel.Setup();
 
66
                }
 
67
                
 
68
                internal ColumnHeaderCollection Columns {
 
69
                        get {
 
70
                                return _columns;
 
71
                        }
 
72
                }
 
73
                
 
74
                internal TreeNode FindNodeByFullPath(String fullPath)
 
75
                {
 
76
                        return FindNodeByFullPathInt(Nodes, fullPath);
 
77
                }
 
78
                
 
79
                internal TreeNode FindNodeByFullPathInt(TreeNodeCollection nodes, String fullPath)
 
80
                {
 
81
                        int pathSep = fullPath.IndexOf(PathSeparator);
 
82
                        String partPath;
 
83
                        if (pathSep == -1)
 
84
                                partPath = fullPath;
 
85
                        else
 
86
                                partPath = fullPath.Substring(0, pathSep);
 
87
                        
 
88
                        foreach (TreeNode node in nodes)
 
89
                        {
 
90
                                if (node.Text.Equals(partPath))
 
91
                                {
 
92
                                        // We are at the bottom
 
93
                                        if (pathSep == -1)
 
94
                                                return node;
 
95
                                        String restPath = fullPath.Substring
 
96
                                                (PathSeparator.Length + pathSep);
 
97
                                        return FindNodeByFullPathInt(node.Nodes,
 
98
                                                                                                restPath);
 
99
                                }
 
100
                        }
 
101
                        // Not found
 
102
                        return null;
 
103
                }
 
104
                
 
105
                // Used to allow normal selection processing if the node
 
106
                // is explicitly selected
 
107
                // This must be used instead of the SelectedNode property to
 
108
                // *set* the SelectedNode
 
109
                public void SetSelectedNode(TreeNode node)
 
110
                {
 
111
                        // Allow selection processing to happen
 
112
                        _selected = false;
 
113
                        SelectedNode = node;
 
114
                }
 
115
                
 
116
                // This is here just to invoke this private method using
 
117
                // reflection
 
118
                internal TreeListNode GetNodeFromHandle(int handle)
 
119
                {
 
120
                        MethodInfo m = typeof(TreeView).GetMethod("NodeFromHandle", 
 
121
                                                                                                         BindingFlags.Instance | 
 
122
                                                                                                         BindingFlags.NonPublic);
 
123
                        ParameterInfo[] p = m.GetParameters();
 
124
                        Object[] paramValues = new Object[1];
 
125
                        paramValues[0] = (IntPtr)handle;
 
126
                        return (TreeListNode)m.Invoke(this, paramValues);
 
127
                }
 
128
                
 
129
                internal virtual void HScrolled()
 
130
                {
 
131
                        // Meant to be overridden
 
132
                }
 
133
                
 
134
                internal virtual void VScrolled()
 
135
                {
 
136
                        // Meant to be overridden
 
137
                }
 
138
                
 
139
                internal void Add(TreeNodeCollection parent, TreeListNode node)
 
140
                {
 
141
                        if (!_useCompareTo)
 
142
                        {
 
143
                                parent.Add(node);
 
144
                                return;
 
145
                        }
 
146
                        /*
 
147
                        Console.WriteLine("AddSorted: " + node);
 
148
                        Console.WriteLine("Current nodes: ");
 
149
                        for (int j = 0; j < parent.Count; j++)
 
150
                                Console.WriteLine(" " + parent[j]);
 
151
                        */
 
152
                        int i;
 
153
                        for (i = parent.Count - 1; i >= 0; i--)
 
154
                        {
 
155
                                TreeListNode lookNode = (TreeListNode)parent[i];
 
156
                                //                Console.WriteLine("looking at: " + lookNode
 
157
                                //                                  + " (" + i + ")");
 
158
                                if (lookNode.CompareTo(node) <= 0)
 
159
                                        break;
 
160
                        }
 
161
                        parent.Insert(i + 1, node);
 
162
                }
 
163
                
 
164
                // Called by the OnBeforeSelect handler to determine
 
165
                // if its valid to respond to the event.  If this is response
 
166
                // to a mouse down event, because of the code below that selects
 
167
                // the mode in response to the mouse down event we have to do
 
168
                // some special things.  The problem is, we select the node below
 
169
                // but then if the tree shifts, later, in handling the same mouse
 
170
                // down event, the regular TreeView processing selects the node
 
171
                // at the original mouse position causing the wrong node to end
 
172
                // up being selected.  This way, we allow only one select per
 
173
                // mouse-down event.
 
174
                protected bool CanSelectNode()
 
175
                {
 
176
                        if (_inMouseDown)
 
177
                        {
 
178
                                if (!_selected)
 
179
                                        return true;
 
180
                                return false;
 
181
                        }
 
182
                        return true;
 
183
                }
 
184
                
 
185
                // Make sure we select the node if its clicked anywhere
 
186
                protected override void OnMouseDown(MouseEventArgs e)
 
187
                {
 
188
                        TreeNode node = GetNodeAt(0, e.Y); 
 
189
                        SelectedNode = node;
 
190
                        // Prevent further selections in this event sequence,
 
191
                        // see comment above.
 
192
                        _selected = true;
 
193
                        base.OnMouseDown(e);
 
194
                }
 
195
                
 
196
                protected override void WndProc(ref Message m)
 
197
                {
 
198
                        bool mouseDown = false;
 
199
                        switch (m.Msg) { 
 
200
                        case Windows.WM_HSCROLL:
 
201
                                HScrolled();
 
202
                                break;
 
203
                        case Windows.WM_VSCROLL:
 
204
                                VScrolled();
 
205
                                break;
 
206
                        case Windows.WM_LBUTTONDOWN:
 
207
                                mouseDown = true;
 
208
                                break;
 
209
                        case Windows.WM_RBUTTONDOWN:
 
210
                                mouseDown = true;
 
211
                                break;
 
212
                        }
 
213
                        if (mouseDown)
 
214
                        {
 
215
                                _inMouseDown = true;
 
216
                                _selected = false;
 
217
                        }
 
218
                        base.WndProc(ref m);
 
219
                        if (mouseDown)
 
220
                        {
 
221
                                _inMouseDown = false;
 
222
                        }
 
223
                }
 
224
        }
 
225
}