File: TypeView.cs

package info (click to toggle)
mono-tools 4.2-3
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 13,868 kB
  • sloc: cs: 100,909; sh: 3,845; makefile: 2,040; xml: 126
file content (135 lines) | stat: -rw-r--r-- 3,569 bytes parent folder | download | duplicates (8)
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
// TypeView.cs
//Authors: ${Author}
//
// Copyright (c) 2008 [copyright holders]
//
// 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 System.IO;
using System.Collections.Generic;
using Gtk;
using Gdk;

namespace Mono.CSharp.Gui
{
	[System.ComponentModel.ToolboxItem(true)]
	public class TypeView : TextView
	{
		MainWindow container;
		
		public TypeView(MainWindow container) : base ()
		{
			WrapMode = Gtk.WrapMode.None;
			CreateTags ();

			Pango.FontDescription font_description = new Pango.FontDescription();
			font_description.Family = "Monospace";
			ModifyFont(font_description);
			this.container = container;
		}

		double  press_x = -1, press_y = -1;
		
		void CreateTags ()
		{
			TextTag keyword = new TextTag ("keyword") {
				Weight = Pango.Weight.Bold
			};

			TextTag deftag = new TextTag ("default"){
			};

			Buffer.TagTable.Add (keyword);
			Buffer.TagTable.Add (deftag);
		}

		string current_tag;

		Dictionary<Type,TextTag> typetags = new Dictionary<Type,TextTag> ();
		static int typec;
		
		string GetTypeTag (Type t)
		{
			if (typetags.ContainsKey (t)){
				return typetags [t].Name;
			}
			
			TextTag typetag = new TextTag ("t" + (typec++)){
				Foreground = "blue",
				Weight = Pango.Weight.Bold
			};
			
			typetag.TextEvent += delegate (object o, TextEventArgs e){
				EventButton eb = e.Event as EventButton;
				
				if (eb != null && eb.Button == 1){
					if (eb.Type == EventType.ButtonPress){
						press_x = eb.X;
						press_y = eb.Y;
					} else if (eb.Type == EventType.ButtonRelease){
						if (press_x != -1){
							int dx = System.Math.Abs ((int)(eb.X - press_x));
							int dy = System.Math.Abs ((int)(eb.Y - press_y));

							if (dx < 8 && dy < 8){
								container.Describe (t);
							}
						}
						press_x = -1;
					}
				}
			};

			Buffer.TagTable.Add (typetag);

			return typetag.Name;
		}
		
		public void ShowType (Type t)
		{
			Buffer.Clear ();
			
			current_tag = "default";

			GuiStream guistream = new GuiStream (null, (x,y) => {
				TextIter end = Buffer.EndIter;
				Buffer.InsertWithTagsByName (ref end, y, current_tag);
			});

			StreamWriter writer = new StreamWriter (guistream);
			Outline outliner = new Outline (t, writer, true, false, false);

			outliner.OutlineNotificationEvent += delegate (int kind, object value) {
				switch (kind){
				case 0:
					current_tag = "default";
					break;
				case 1:
					current_tag = GetTypeTag ((Type) value);
					break;
				}
			};
			outliner.OutlineType ();
			writer.Flush ();
		}
	}
}