File: Main.cs

package info (click to toggle)
mono-tools 4.2-3.1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye
  • size: 13,868 kB
  • sloc: cs: 100,909; sh: 3,845; makefile: 2,040; javascript: 1,557; xml: 126
file content (149 lines) | stat: -rw-r--r-- 3,908 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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
// Main.cs created with MonoDevelop
// User: miguel at 10:22 PMĀ 9/27/2008
//
// To change standard headers go to Edit->Preferences->Coding->Standard Headers
//
using System;
using Gtk;
using System.Threading;
using System.Diagnostics;
using System.Reflection;
using Mono.Options;
using System.Collections.Generic;
using System.IO;

namespace Mono.CSharp.Gui
{
	class MainClass
	{
		public static bool Attached;
		public static bool HostHasGtkRunning;
		public static bool Debug;
		static Evaluator evaluator;
		
		public static void ShowHelp (OptionSet p)
		{
			Console.WriteLine ("Usage it: gsharp [--agent] [file1 [fileN]]");
			
			p.WriteOptionDescriptions (Console.Out);
		}

		static void ResetOutput ()
		{
			var stdout = new StreamWriter (Console.OpenStandardOutput ()) { AutoFlush = true };
			var stderr = new StreamWriter (Console.OpenStandardError ()) { AutoFlush = true };
			Console.SetOut (stdout);
			Console.SetError (stderr);
		}
		
		public static void Main (string[] args)
		{
			bool agent = false;

			OptionSet p = null;

			p = new OptionSet () {
				{ "agent", "Start up as an agent", f => agent = f != null },
				{ "help", "Shows the help", f => { ShowHelp (p); Environment.Exit (0); } },
				{ "debug", "Runs in debug mode, does not redirect IO to the window", f => Debug = true }
			};

			List<string> extra = null;
			try {
				extra = p.Parse (args);
			} catch (OptionException) {
				ShowHelp (p);
			}
			
			if (agent)
				StartAgent ();
			else
				Start ("C# InteractiveBase Shell", extra);
		}
		
		static void AssemblyLoaded (object sender, AssemblyLoadEventArgs e)
		{
			evaluator.ReferenceAssembly (e.LoadedAssembly);
		}

		internal static object RenderBitmaps (object o)
		{
			System.Drawing.Bitmap bitmap = o as System.Drawing.Bitmap;
			if (bitmap == null)
				return null;
			return new BitmapWidget (bitmap);
		}

		public static void StartAgent ()
		{
			Attached = true;
			
			// First, try to detect if Gtk.Application.Run is running,
			// to determine whether we need to run a mainloop oursvels or not.
			//
			// This test is not bullet proof, its just a simple guess.
			//
			// Thanks to Alan McGovern for this brilliant hack. 
			//
			ManualResetEvent handle = new ManualResetEvent(false);
			
			Gtk.Application.Invoke (delegate { handle.Set (); });
			HostHasGtkRunning = handle.WaitOne (3000, true);
			
			InteractiveGraphicsBase.Attached = true;
			Gtk.Application.Invoke (delegate {
				try {
					evaluator = new Evaluator (new CompilerContext (new CompilerSettings (), new ConsoleReportPrinter ()));
				} catch {
					return;
				}
				
				try {
					// Add all assemblies loaded later
					AppDomain.CurrentDomain.AssemblyLoad += AssemblyLoaded;
					
					// Add all currently loaded assemblies
					foreach (Assembly a in AppDomain.CurrentDomain.GetAssemblies ())
						evaluator.ReferenceAssembly (a);
					
					Start (String.Format ("Attached C# Interactive Shell at Process {0}", Process.GetCurrentProcess ().Id), null);
				} finally {
					AppDomain.CurrentDomain.AssemblyLoad -= AssemblyLoaded;
				}
				
				});
		}
		
		public static void Start (string title, List<string> files)
		{
			if (!HostHasGtkRunning)
				Application.Init ();

			InteractiveGraphicsBase.RegisterTransformHandler (RenderBitmaps);
			
			MainWindow m = new MainWindow ();
			InteractiveGraphicsBase.MainWindow = m;
			InteractiveGraphicsBase.PaneContainer = m.PaneContainer;
			
			m.Title = title;
			m.LoadStartupFiles ();
			if (files != null)
				m.LoadFiles (files, false);
			m.ShowAll ();

			if (!HostHasGtkRunning){
				try {
					GLib.ExceptionManager.UnhandledException += delegate (GLib.UnhandledExceptionArgs a) {
						ResetOutput ();
						Console.WriteLine ("Application terminating: " + a.ExceptionObject);
					};
					
					Application.Run ();
				} catch (Exception e) {
					ResetOutput ();
					throw;
				}
			}
		}
	}
}