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

MonoRuntimePanel.cs « MonoDevelop.Ide.Gui.OptionPanels « MonoDevelop.Ide « core « src « main - github.com/mono/monodevelop.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: f4a1629b2ed4e30f6cbcc7cd0d7cc795d5977afc (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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
// 
// MonoRuntimePanelWidget.cs
//  
// Author:
//       Lluis Sanchez Gual <lluis@novell.com>
// 
// Copyright (c) 2009 Novell, Inc (http://www.novell.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 System.Linq;
using System.Collections.Generic;

using Gtk;

using MonoDevelop.Core;
using MonoDevelop.Ide.Gui.Dialogs;
using MonoDevelop.Core.Assemblies;
using MonoDevelop.Components;
using MonoDevelop.Components.AtkCocoaHelper;

namespace MonoDevelop.Ide.Gui.OptionPanels
{
	class MonoRuntimePanel : OptionsPanel
	{
		MonoRuntimePanelWidget widget;
		
		public override Control CreatePanelWidget ()
		{
			return widget = new MonoRuntimePanelWidget ();
		}
		
		public override void ApplyChanges ()
		{
			widget.Store();
		}
	}
	
	[System.ComponentModel.ToolboxItem(true)]
	internal partial class MonoRuntimePanelWidget : Gtk.Bin
	{
		readonly List<TargetRuntime> removedRuntimes = new List<TargetRuntime> ();
		readonly List<MonoRuntimeInfo> newInfos = new List<MonoRuntimeInfo> ();
		readonly ListStore store;
		TreeIter defaultIter;
		TreeIter runningIter;
		
		public MonoRuntimePanelWidget()
		{
			this.Build();
			
			textview1.SetMarkup (textview1.Buffer.Text);
			
			labelRunning.Markup = GettextCatalog.GetString (
				"{0} is currently running on <b>{1}</b>.",
				BrandingService.ApplicationName,
				Runtime.SystemAssemblyService.CurrentRuntime.DisplayName
			);
			store = new ListStore (typeof(string), typeof(object));
			tree.Model = store;
			tree.SearchColumn = -1; // disable the interactive search

			CellRendererText crt = new CellRendererText ();
			tree.AppendColumn (GettextCatalog.GetString ("Runtime"), crt, "markup", 0);
			TargetRuntime defRuntime = IdeApp.Preferences.DefaultTargetRuntime;
			
			foreach (TargetRuntime tr in Runtime.SystemAssemblyService.GetTargetRuntimes ()) {
				string name = tr.DisplayName;
				TreeIter it;
				if (tr == defRuntime) {
					name = string.Format ("<b>{0} {1}</b>", name, GettextCatalog.GetString ("(Default)"));
					defaultIter = it = store.AppendValues (name, tr);
				} else
					it = store.AppendValues (name, tr);
				if (tr.IsRunning)
					runningIter = it;
			}
			
			tree.Selection.Changed += HandleChanged;
			UpdateButtons ();

			SetupAccessibility ();
		}

		void SetupAccessibility ()
		{
			tree.SetCommonAccessibilityAttributes ("MonoRuntimePanel.tree", GettextCatalog.GetString ("Available Runtimes"),
			                                       GettextCatalog.GetString ("A list of available runtimes"));
			buttonAdd.SetCommonAccessibilityAttributes ("MonoRuntimePanel.add", "",
			                                            GettextCatalog.GetString ("Click to install a new runtime"));
			buttonRemove.SetCommonAccessibilityAttributes ("MonoRuntimePanel.remove", "",
			                                               GettextCatalog.GetString ("Click to remove the currently selected runtime"));
			buttonDefault.SetCommonAccessibilityAttributes ("MonoRuntimePanel.default", "",
			                                                GettextCatalog.GetString ("Click to set the currently selected runtime as default"));
		}

		void HandleChanged(object sender, EventArgs e)
		{
			UpdateButtons ();
		}
		
		public void Store ()
		{
			object ob = store.GetValue (defaultIter, 1);
			MonoRuntimeInfo newDefaultInfo = ob as MonoRuntimeInfo;
			if (ob is TargetRuntime)
				IdeApp.Preferences.DefaultTargetRuntime.Value = (TargetRuntime)ob;

			foreach (var rinfo in newInfos) {
				TargetRuntime tr = MonoTargetRuntime.RegisterRuntime (rinfo);
				if (rinfo == newDefaultInfo)
					IdeApp.Preferences.DefaultTargetRuntime.Value = tr;
			}

			foreach (var tr in removedRuntimes.OfType<MonoTargetRuntime> ())
				MonoTargetRuntime.UnregisterRuntime (tr);
			
		}

		// ProgramFilesX86 is broken on 32-bit WinXP, this is a workaround
		static string GetProgramFilesX86 ()
		{
			return Environment.GetFolderPath (IntPtr.Size == 8?
				Environment.SpecialFolder.ProgramFilesX86 : Environment.SpecialFolder.ProgramFiles);
		}
		
		protected virtual void OnButtonAddClicked (object sender, System.EventArgs e)
		{
			var dlg = new SelectFolderDialog (GettextCatalog.GetString ("Select the mono installation prefix")) {
				TransientFor = this.Toplevel as Gtk.Window,
			};
			
			//set a platform-dependent default folder for the dialog if possible
			if (Platform.IsWindows) {
				// ProgramFilesX86 is broken on 32-bit WinXP
				string programFilesX86 = GetProgramFilesX86 ();
				if (!string.IsNullOrEmpty (programFilesX86) && System.IO.Directory.Exists (programFilesX86))
					dlg.CurrentFolder = programFilesX86;
			} else {
				if (System.IO.Directory.Exists ("/usr"))
					dlg.CurrentFolder = "/usr";
			}
			
			if (!dlg.Run ())
				return;
			
			var rinfo = new MonoRuntimeInfo (dlg.SelectedFile);
			if (!rinfo.IsValidRuntime) {
				MessageService.ShowError (GettextCatalog.GetString ("Mono runtime not found"), GettextCatalog.GetString ("Please provide a valid directory prefix where mono is installed (for example, /usr)"));
				return;
			}
			newInfos.Add (rinfo);
			store.AppendValues (rinfo.DisplayName, rinfo);
		}
	
		protected virtual void OnButtonRemoveClicked (object sender, System.EventArgs e)
		{
			TreeIter it;
			if (tree.Selection.GetSelected (out it)) {
				object ob = store.GetValue (it, 1);
				if (ob is MonoRuntimeInfo)
					newInfos.Remove ((MonoRuntimeInfo)ob);
				else {
					TargetRuntime tr = (TargetRuntime) ob;
					if (tr.IsRunning)
						return;
					removedRuntimes.Add (tr);
				}
				if (store.GetPath (it).Equals (store.GetPath (defaultIter))) {
					defaultIter = runningIter;
					UpdateRow (defaultIter);
				}
				store.Remove (ref it);
			}
		}
	
		protected virtual void OnButtonDefaultClicked (object sender, System.EventArgs e)
		{
			TreeIter it;
			if (tree.Selection.GetSelected (out it)) {
				TreeIter oldDefault = defaultIter;
				defaultIter = it;
				UpdateRow (oldDefault);
				UpdateRow (defaultIter);
			}
		}
		
		void UpdateRow (TreeIter it)
		{
			object ob = store.GetValue (it, 1);
			string text;
			if (ob is MonoRuntimeInfo)
				text = ((MonoRuntimeInfo)ob).DisplayName;
			else
				text = ((TargetRuntime)ob).DisplayName;
			if (store.GetPath (it).Equals (store.GetPath (defaultIter)))
				text = string.Format ("<b>{0} {1}</b>", text, GettextCatalog.GetString ("(Default)"));
			store.SetValue (it, 0, text);
		}
		
		void UpdateButtons ()
		{
			TreeIter it;
			if (tree.Selection.GetSelected (out it)) {
				object ob = store.GetValue (it, 1);
				MonoTargetRuntime tr = ob as MonoTargetRuntime;
				buttonRemove.Sensitive = (tr != null && tr.UserDefined) || ob is MonoRuntimeInfo;
				buttonDefault.Sensitive = true;
			} else {
				buttonRemove.Sensitive = false;
				buttonDefault.Sensitive = false;
			}
		}
	}
}