Welcome to mirror list, hosted at ThFree Co, Russian Federation.

ContextMenuTreeView.cs « MonoDevelop.Components « MonoDevelop.Ide « core « src « main - github.com/mono/monodevelop.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: a0e32d669ee73b5bc7d8a6361f5953a270e60bd0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
// 
// ContextMenuTreeView.cs
//  
// Author:
//       Michael Hutchinson <mhutch@xamarin.com>
// 
// Copyright (c) 2011 Xamarin Inc. (http://xamarin.com)
// 
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
// 
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
using System;
using Mono.TextEditor;

namespace MonoDevelop.Components
{
	/// <summary>
	/// TreeView with context menu support.
	/// </summary>
	public class ContextMenuTreeView : Gtk.TreeView
	{
		public ContextMenuTreeView ()
		{
		}

		public ContextMenuTreeView (Gtk.TreeModel model) : base (model)
		{
		}

		public Action<Gdk.EventButton> DoPopupMenu { get; set; }

		Gtk.TreePath buttonPressPath;
		bool selectOnRelease;

		protected override void OnDragBegin (Gdk.DragContext context)
		{
			//If user starts dragging don't do any selection
			//useful in case user press Esc to abort dragging and
			//didn't release mouse button yet
			selectOnRelease = false;
			base.OnDragBegin (context);
		}

		protected override bool OnButtonPressEvent (Gdk.EventButton evnt)
		{
			selectOnRelease = false;
			if (!evnt.TriggersContextMenu ()) {
				//Because we are blocking selection changes with SelectFunction base.OnButtonPressEvent
				//can be called so expanders work. Another good effect is when expander is clicked
				//SelectFunction is not called so selectOnRelease remains false.
				//Which means no selection operation is performed in OnButtonReleaseEvent.
				//When Shift is pressed we don't do our magic becasue:
				//a) it works as expected((item is still selected when dragging starts
				//(it's by nature of Shift selecting))
				//b) we would have to simulate Shift selecting in OnButtonReleaseEvent
				//which would mean we have to implement all selecting logic...
				//Also notice that our magic is requiered only when item is selected.
				if (GetPathAtPos ((int)evnt.X, (int)evnt.Y, out buttonPressPath) &&
				    ((evnt.State & Gdk.ModifierType.ShiftMask) == 0) &&
				    Selection.PathIsSelected (buttonPressPath)) {
					this.Selection.SelectFunction = (s, m, p, b) => {
						selectOnRelease = true;
						//Always returning false means we are blocking base.OnButtonPressEvent
						//from doing any changes to selectiong we will do changes in OnButtonReleaseEvent
						return false;
					};
				}
				return base.OnButtonPressEvent (evnt);
			}
			//pass click to base it it can update the selection
			//unless the node is already selected, in which case we don't want to change the selection
			bool res = false;
			if (!IsClickedNodeSelected ((int)evnt.X, (int)evnt.Y)) {
				res = base.OnButtonPressEvent (evnt);
			}
			
			if (DoPopupMenu != null) {
				DoPopupMenu (evnt);
				return true;
			}
			
			return res;
		}

		protected override bool OnButtonReleaseEvent (Gdk.EventButton evnt)
		{
			this.Selection.SelectFunction = (s, m, p, b) => {
				return true;
			};
			Gtk.TreePath buttonReleasePath;
			//If OnButtonPressEvent attempted on making deselection and dragging was not started
			//check if we are on same item as when we clicked(could be different if dragging is disabled)
			if (selectOnRelease &&
			    GetPathAtPos ((int)evnt.X, (int)evnt.Y, out buttonReleasePath) &&
			    buttonPressPath.Compare (buttonReleasePath) == 0) {

				//Simulate what would happen in OnButtonPressEvent if we were not blocking selection
				//notice that item is currently 100% selected since this check was performed in OnButtonPressEvent
				if (Selection.Mode == Gtk.SelectionMode.Multiple &&
				    (evnt.State & Gdk.ModifierType.ControlMask) > 0) {
					Selection.UnselectPath (buttonReleasePath);
				} else {
					//UnselectAll in case multiple were selected we want only our item to be selected now
					//if it was clicked but not dragged
					Selection.UnselectAll ();
					Selection.SelectPath (buttonReleasePath);
				}
				buttonPressPath = null;
			}
			selectOnRelease = false;

			bool res = base.OnButtonReleaseEvent (evnt);
			
			if (DoPopupMenu != null && evnt.IsContextMenuButton ()) {
				return true;
			}
			
			return res;
		}

		protected override bool OnPopupMenu ()
		{
			if (DoPopupMenu != null) {
				DoPopupMenu (null);
				return true;
			}
			return base.OnPopupMenu ();
		}

		bool IsClickedNodeSelected (int x, int y)
		{
			Gtk.TreePath path;
			if (GetPathAtPos (x, y, out path))
				return Selection.PathIsSelected (path);

			return false;
		}

		bool MultipleNodesSelected ()
		{
			return Selection.GetSelectedRows ().Length > 1;
		}
	}
}