[MonoDevelop] MenuService.CreateQuickInsertMenu patch

John BouAntoun jbafactor@optusnet.com.au
Wed, 21 Jan 2004 21:51:22 +1100


--=-I0L/+FrgNs872vTgTIpQ
Content-Type: text/plain
Content-Transfer-Encoding: 7bit

Hi guys,

I decided to try and implement the Missing CreateQuickInsertMenu
functionality from the MenuService.

It only took me about an hour (took me 20 minutes to figure that this
was not in the .Core.Services namespace).

Anyways, I implemented the missing functionality and then used this new
implementation in the ExternalToolPanel.cs file (as a means of testing
it).

I have included both the diff file patch and the original modified files
(in case the diff is against the wrong revision).

I made 3 changes to the ExternalToolPanel.cs file, 

1) add the "using ICSharpCode.SharpDevelop.Services;" namespace
2) uncomment the FIXME that declared the MenuService
3) uncomment the FIXME that called CreateQuickInsertMenu

Hope all goes well,

JBA



--=-I0L/+FrgNs872vTgTIpQ
Content-Disposition: attachment; filename=MenuService-ExternalToolPanel.diff
Content-Type: text/x-patch; name=MenuService-ExternalToolPanel.diff; charset=UTF-8
Content-Transfer-Encoding: 8bit

Index: src/Main/Base/Services/MenuService/MenuService.cs
===================================================================
--- src/Main/Base/Services/MenuService/MenuService.cs	(revision 609)
+++ src/Main/Base/Services/MenuService/MenuService.cs	(working copy)
@@ -1,125 +1,134 @@
-// <file>
-//     <copyright see="prj:///doc/copyright.txt"/>
-//     <license see="prj:///doc/license.txt"/>
-//     <owner name="Mike Krüger" email="mike@icsharpcode.net"/>
-//     <version value="$version"/>
-// </file>
-
-using System;
-using System.IO;
-using System.Collections;
-using System.Threading;
-using System.Resources;
-using System.Drawing;
-using System.Diagnostics;
-using System.Reflection;
-using System.Xml;
-using ICSharpCode.Core.AddIns;
-using ICSharpCode.Core.AddIns.Codons;
-using ICSharpCode.Core.Services;
-using ICSharpCode.SharpDevelop.Gui.Components;
-
-namespace ICSharpCode.SharpDevelop.Services
-{
-	public class MenuService : AbstractService
-	{/*
-		void ContextMenuPopupHandler(object sender, EventArgs e)
-		{
-			CommandBarContextMenu contextMenu = (CommandBarContextMenu)sender;
-			foreach (object o in contextMenu.Items) {
-				if (o is IStatusUpdate) {
-					((IStatusUpdate)o).UpdateStatus();
-				}
-			}
-		}
-		*/
-		public Gtk.Menu CreateContextMenu(object owner, string addInTreePath)
-		{
-			ArrayList buildItems = AddInTreeSingleton.AddInTree.GetTreeNode(addInTreePath).BuildChildItems(owner);
-			//CommandBarContextMenu contextMenu = new CommandBarContextMenu();
-			//contextMenu.Popup += new EventHandler(ContextMenuPopupHandler);
-			
-			Gtk.Menu contextMenu = new Gtk.Menu();
-			foreach (object item in buildItems) {
-				if (item is Gtk.MenuItem) {
-					//contextMenu.Items.Add((CommandBarItem)item);
-					contextMenu.Append((Gtk.MenuItem)item);
-				} else {
-					ISubmenuBuilder submenuBuilder = (ISubmenuBuilder)item;
-					foreach(Gtk.MenuItem mi in submenuBuilder.BuildSubmenu(null, owner)) {
-						contextMenu.Append(mi);
-					}
-					//contextMenu.Items.AddRange(submenuBuilder.BuildSubmenu(null, owner));
-				}
-			}
-			contextMenu.ShowAll();
-			return contextMenu;
-		}
-		
-		public void ShowContextMenu(object owner, string addInTreePath, Gtk.Widget parent)
-		{
-			//CreateContextMenu(owner, addInTreePath).Show(parent, new Point(x, y));
-			CreateContextMenu(owner, addInTreePath).Popup(null, null, null, IntPtr.Zero, 0, Gtk.Global.CurrentEventTime);
-		}
-		/*
-		class QuickInsertMenuHandler
-		{
-			TextBoxBase targetControl;
-			string      text;
-			
-			public QuickInsertMenuHandler(TextBoxBase targetControl, string text)
-			{
-				this.targetControl = targetControl;
-				this.text          = text;
-			}
-			
-			public EventHandler EventHandler {
-				get {
-					return new EventHandler(PopupMenuHandler);
-				}
-			}
-			void PopupMenuHandler(object sender, EventArgs e)
-			{
-				targetControl.SelectedText += text;
-			}
-		}
-		
-		class QuickInsertHandler
-		{
-			Control               popupControl;
-			CommandBarContextMenu quickInsertMenu;
-			
-			public QuickInsertHandler(Control popupControl, CommandBarContextMenu quickInsertMenu)
-			{
-				this.popupControl    = popupControl;
-				this.quickInsertMenu = quickInsertMenu;
-				
-				popupControl.Click += new EventHandler(showQuickInsertMenu);
-			}
-			
-			void showQuickInsertMenu(object sender, EventArgs e)
-			{
-				Point cords = new Point(popupControl.Width, 0);
-				quickInsertMenu.Show(popupControl, cords);
-			}
-		}
-		
-		public void CreateQuickInsertMenu(TextBoxBase targetControl, Control popupControl, string[,] quickInsertMenuItems)
-		{
-			StringParserService stringParserService = (StringParserService)ServiceManager.Services.GetService(typeof(StringParserService));
-			
-			CommandBarContextMenu contextMenu = new CommandBarContextMenu();
-			for (int i = 0; i < quickInsertMenuItems.GetLength(0); ++i) {
-				if (quickInsertMenuItems[i, 0] == "-") {
-					contextMenu.Items.Add(new SdMenuSeparator());
-				} else {
-					SdMenuCommand cmd = new SdMenuCommand(this,
-					                                      stringParserService.Parse(quickInsertMenuItems[i, 0]),
-					                                      new QuickInsertMenuHandler(targetControl, quickInsertMenuItems[i, 1]).EventHandler);
-					contextMenu.Items.Add(cmd);
-				}
-			}
-			new QuickInsertHandler(popupControl, contextMenu);
-		}*/
-	}
-}
+// <file>
+//     <copyright see="prj:///doc/copyright.txt"/>
+//     <license see="prj:///doc/license.txt"/>
+//     <owner name="Mike Krüger" email="mike@icsharpcode.net"/>
+//     <version value="$version"/>
+// </file>
+
+using System;
+using System.IO;
+using System.Collections;
+using System.Threading;
+using System.Resources;
+using System.Drawing;
+using System.Diagnostics;
+using System.Reflection;
+using System.Xml;
+using ICSharpCode.Core.AddIns;
+using ICSharpCode.Core.AddIns.Codons;
+using ICSharpCode.Core.Services;
+using ICSharpCode.SharpDevelop.Gui.Components;
+
+namespace ICSharpCode.SharpDevelop.Services
+{
+	public class MenuService : AbstractService
+	{/*
+		void ContextMenuPopupHandler(object sender, EventArgs e)
+		{
+			CommandBarContextMenu contextMenu = (CommandBarContextMenu)sender;
+			foreach (object o in contextMenu.Items) {
+				if (o is IStatusUpdate) {
+					((IStatusUpdate)o).UpdateStatus();
+				}
+			}
+		}
+		*/
+		public Gtk.Menu CreateContextMenu(object owner, string addInTreePath)
+		{
+			ArrayList buildItems = AddInTreeSingleton.AddInTree.GetTreeNode(addInTreePath).BuildChildItems(owner);
+			//CommandBarContextMenu contextMenu = new CommandBarContextMenu();
+			//contextMenu.Popup += new EventHandler(ContextMenuPopupHandler);
+			
+			Gtk.Menu contextMenu = new Gtk.Menu();
+			foreach (object item in buildItems) {
+				if (item is Gtk.MenuItem) {
+					//contextMenu.Items.Add((CommandBarItem)item);
+					contextMenu.Append((Gtk.MenuItem)item);
+				} else {
+					ISubmenuBuilder submenuBuilder = (ISubmenuBuilder)item;
+					foreach(Gtk.MenuItem mi in submenuBuilder.BuildSubmenu(null, owner)) {
+						contextMenu.Append(mi);
+					}
+					//contextMenu.Items.AddRange(submenuBuilder.BuildSubmenu(null, owner));
+				}
+			}
+			contextMenu.ShowAll();
+			return contextMenu;
+		}
+		
+		public void ShowContextMenu(object owner, string addInTreePath, Gtk.Widget parent)
+		{
+			//CreateContextMenu(owner, addInTreePath).Show(parent, new Point(x, y));
+			CreateContextMenu(owner, addInTreePath).Popup(null, null, null, IntPtr.Zero, 0, Gtk.Global.CurrentEventTime);
+		}
+		
+		class QuickInsertMenuHandler
+		{
+			Gtk.Editable targetControl;
+			string      text;
+			
+			public QuickInsertMenuHandler(Gtk.Editable targetControl, string text)
+			{
+				this.targetControl = targetControl;
+				this.text          = text;
+			}
+			
+			public EventHandler EventHandler {
+				get {
+					return new EventHandler(PopupMenuHandler);
+				}
+			}
+			void PopupMenuHandler(object sender, EventArgs e)
+			{
+				// insert at current cursor position, deleting any selections
+				int tempInt = targetControl.Position;
+				targetControl.DeleteSelection();
+				targetControl.InsertText(text, ref tempInt);
+			}
+		}
+		
+		class QuickInsertHandler
+		{
+			Gtk.Button               popupControl;
+			Gtk.Menu quickInsertMenu;
+			
+			//public QuickInsertHandler(Control popupControl, CommandBarContextMenu quickInsertMenu)
+			public QuickInsertHandler(Gtk.Button popupControl, Gtk.Menu quickInsertMenu)
+			{
+				this.popupControl    = popupControl;
+				this.quickInsertMenu = quickInsertMenu;
+				
+				popupControl.Clicked += new EventHandler(showQuickInsertMenu);
+			}
+			
+			void showQuickInsertMenu(object sender, EventArgs e)
+			{
+				//Point cords = new Point(popupControl.Width, 0);
+				//quickInsertMenu.Show(popupControl, cords);
+				quickInsertMenu.Popup(null, null, null, IntPtr.Zero, 0, Gtk.Global.CurrentEventTime);
+			}
+		}
+		
+		//public void CreateQuickInsertMenu(TextBoxBase targetControl, Control popupControl, string[,] quickInsertMenuItems)		
+		public void CreateQuickInsertMenu(Gtk.Editable targetControl, Gtk.Button popupControl, string[,] quickInsertMenuItems)
+		{
+			StringParserService stringParserService = (StringParserService)ServiceManager.Services.GetService(typeof(StringParserService));
+			
+			//CommandBarContextMenu contextMenu = new CommandBarContextMenu();
+			Gtk.Menu contextMenu = new Gtk.Menu();
+			for (int i = 0; i < quickInsertMenuItems.GetLength(0); ++i) {
+				if (quickInsertMenuItems[i, 0] == "-") {
+					contextMenu.Append(new SdMenuSeparator());
+				} else {
+					SdMenuCommand cmd = new SdMenuCommand(this,
+					                                      stringParserService.Parse(quickInsertMenuItems[i, 0]),
+					                                      new QuickInsertMenuHandler(targetControl, quickInsertMenuItems[i, 1]).EventHandler);
+					contextMenu.Append(cmd);
+				}
+			}
+			new QuickInsertHandler(popupControl, contextMenu);
+			
+			contextMenu.ShowAll();
+		}
+	}
+}
Index: src/Main/Base/Gui/Dialogs/OptionPanels/ExternalToolPanel.cs
===================================================================
--- src/Main/Base/Gui/Dialogs/OptionPanels/ExternalToolPanel.cs	(revision 609)
+++ src/Main/Base/Gui/Dialogs/OptionPanels/ExternalToolPanel.cs	(working copy)
@@ -15,6 +15,9 @@
 using ICSharpCode.Core.Services;
 using ICSharpCode.Core.AddIns.Codons;
 
+using ICSharpCode.SharpDevelop.Services;
+//using MonoDevelop.Gui;
+
 namespace ICSharpCode.SharpDevelop.Gui.Dialogs.OptionPanels
 {
 	public class ExternalToolPane : AbstractOptionPanel
@@ -84,14 +87,14 @@
 		// needed for treeview listbox
 		int toolListBoxItemCount = 0;
 		
-		// Services
+		// Services		
 		FileUtilityService FileUtilityService = (FileUtilityService)ServiceManager.Services.GetService(typeof(FileUtilityService));
 		StringParserService StringParserService = (StringParserService)ServiceManager.Services.GetService (typeof (StringParserService));
 		PropertyService PropertyService = (PropertyService)ServiceManager.Services.GetService(typeof(PropertyService));
 		MessageService MessageService = (MessageService)ServiceManager.Services.GetService(typeof(MessageService));
-// FIXME: when menu service is created
-//		MenuService MenuService = (MenuService)ServiceManager.Services.GetService(typeof(MenuService));
+		MenuService MenuService = (MenuService)ServiceManager.Services.GetService(typeof(MenuService));
 		
+		
 		public override void LoadPanelContents()
 		{
 			// set up the form controls instance
@@ -114,17 +117,14 @@
 				"text", 
 				0);
 			
+			MenuService.CreateQuickInsertMenu(argumentTextBox,
+			                                  argumentQuickInsertButton,
+			                                  argumentQuickInsertMenu);
 			
-// FIXME: when menu service is created
-//			MenuService.CreateQuickInsertMenu(argumentTextBox,
-//			                                  argumentQuickInsertButton,
-//			                                  argumentQuickInsertMenu);
-			
-// FIXME: when menu service is created
-//			MenuService.CreateQuickInsertMenu(workingDirTextBox,
-//			                                  workingDirQuickInsertButton,
-//			                                  workingDirInsertMenu);
-			
+			MenuService.CreateQuickInsertMenu(workingDirTextBox,
+			                                  workingDirQuickInsertButton,
+			                                  workingDirInsertMenu);
+
 			toolListBox.Selection.Changed += new EventHandler(selectEvent);
 			
 			removeButton.Clicked   += new EventHandler(removeEvent);

--=-I0L/+FrgNs872vTgTIpQ
Content-Disposition: attachment; filename=MenuService.cs
Content-Type: text/plain; name=MenuService.cs; charset=UTF-8
Content-Transfer-Encoding: 8bit

// <file>
//     <copyright see="prj:///doc/copyright.txt"/>
//     <license see="prj:///doc/license.txt"/>
//     <owner name="Mike Krüger" email="mike@icsharpcode.net"/>
//     <version value="$version"/>
// </file>

using System;
using System.IO;
using System.Collections;
using System.Threading;
using System.Resources;
using System.Drawing;
using System.Diagnostics;
using System.Reflection;
using System.Xml;
using ICSharpCode.Core.AddIns;
using ICSharpCode.Core.AddIns.Codons;
using ICSharpCode.Core.Services;
using ICSharpCode.SharpDevelop.Gui.Components;

namespace ICSharpCode.SharpDevelop.Services
{
	public class MenuService : AbstractService
	{/*
		void ContextMenuPopupHandler(object sender, EventArgs e)
		{
			CommandBarContextMenu contextMenu = (CommandBarContextMenu)sender;
			foreach (object o in contextMenu.Items) {
				if (o is IStatusUpdate) {
					((IStatusUpdate)o).UpdateStatus();
				}
			}
		}
		*/
		public Gtk.Menu CreateContextMenu(object owner, string addInTreePath)
		{
			ArrayList buildItems = AddInTreeSingleton.AddInTree.GetTreeNode(addInTreePath).BuildChildItems(owner);
			//CommandBarContextMenu contextMenu = new CommandBarContextMenu();
			//contextMenu.Popup += new EventHandler(ContextMenuPopupHandler);
			
			Gtk.Menu contextMenu = new Gtk.Menu();
			foreach (object item in buildItems) {
				if (item is Gtk.MenuItem) {
					//contextMenu.Items.Add((CommandBarItem)item);
					contextMenu.Append((Gtk.MenuItem)item);
				} else {
					ISubmenuBuilder submenuBuilder = (ISubmenuBuilder)item;
					foreach(Gtk.MenuItem mi in submenuBuilder.BuildSubmenu(null, owner)) {
						contextMenu.Append(mi);
					}
					//contextMenu.Items.AddRange(submenuBuilder.BuildSubmenu(null, owner));
				}
			}
			contextMenu.ShowAll();
			return contextMenu;
		}
		
		public void ShowContextMenu(object owner, string addInTreePath, Gtk.Widget parent)
		{
			//CreateContextMenu(owner, addInTreePath).Show(parent, new Point(x, y));
			CreateContextMenu(owner, addInTreePath).Popup(null, null, null, IntPtr.Zero, 0, Gtk.Global.CurrentEventTime);
		}
		
		class QuickInsertMenuHandler
		{
			Gtk.Editable targetControl;
			string      text;
			
			public QuickInsertMenuHandler(Gtk.Editable targetControl, string text)
			{
				this.targetControl = targetControl;
				this.text          = text;
			}
			
			public EventHandler EventHandler {
				get {
					return new EventHandler(PopupMenuHandler);
				}
			}
			void PopupMenuHandler(object sender, EventArgs e)
			{
				// insert at current cursor position, deleting any selections
				int tempInt = targetControl.Position;
				targetControl.DeleteSelection();
				targetControl.InsertText(text, ref tempInt);
			}
		}
		
		class QuickInsertHandler
		{
			Gtk.Button               popupControl;
			Gtk.Menu quickInsertMenu;
			
			//public QuickInsertHandler(Control popupControl, CommandBarContextMenu quickInsertMenu)
			public QuickInsertHandler(Gtk.Button popupControl, Gtk.Menu quickInsertMenu)
			{
				this.popupControl    = popupControl;
				this.quickInsertMenu = quickInsertMenu;
				
				popupControl.Clicked += new EventHandler(showQuickInsertMenu);
			}
			
			void showQuickInsertMenu(object sender, EventArgs e)
			{
				//Point cords = new Point(popupControl.Width, 0);
				//quickInsertMenu.Show(popupControl, cords);
				quickInsertMenu.Popup(null, null, null, IntPtr.Zero, 0, Gtk.Global.CurrentEventTime);
			}
		}
		
		//public void CreateQuickInsertMenu(TextBoxBase targetControl, Control popupControl, string[,] quickInsertMenuItems)		
		public void CreateQuickInsertMenu(Gtk.Editable targetControl, Gtk.Button popupControl, string[,] quickInsertMenuItems)
		{
			StringParserService stringParserService = (StringParserService)ServiceManager.Services.GetService(typeof(StringParserService));
			
			//CommandBarContextMenu contextMenu = new CommandBarContextMenu();
			Gtk.Menu contextMenu = new Gtk.Menu();
			for (int i = 0; i < quickInsertMenuItems.GetLength(0); ++i) {
				if (quickInsertMenuItems[i, 0] == "-") {
					contextMenu.Append(new SdMenuSeparator());
				} else {
					SdMenuCommand cmd = new SdMenuCommand(this,
					                                      stringParserService.Parse(quickInsertMenuItems[i, 0]),
					                                      new QuickInsertMenuHandler(targetControl, quickInsertMenuItems[i, 1]).EventHandler);
					contextMenu.Append(cmd);
				}
			}
			new QuickInsertHandler(popupControl, contextMenu);
			
			contextMenu.ShowAll();
		}
	}
}

--=-I0L/+FrgNs872vTgTIpQ
Content-Disposition: attachment; filename=ExternalToolPanel.cs
Content-Type: text/plain; name=ExternalToolPanel.cs; charset=UTF-8
Content-Transfer-Encoding: 8bit

// <file>
//     <copyright see="prj:///doc/copyright.txt"/>
//     <license see="prj:///doc/license.txt"/>
//     <owner name="Mike Krüger" email="mike@icsharpcode.net"/>
//     <version value="$version"/>
// </file>

using System;
using System.IO;
using System.Collections;
using Gtk;

using ICSharpCode.SharpDevelop.Internal.ExternalTool;
using ICSharpCode.Core.Properties;
using ICSharpCode.Core.Services;
using ICSharpCode.Core.AddIns.Codons;

using ICSharpCode.SharpDevelop.Services;
//using MonoDevelop.Gui;

namespace ICSharpCode.SharpDevelop.Gui.Dialogs.OptionPanels
{
	public class ExternalToolPane : AbstractOptionPanel
	{

		static string[,] argumentQuickInsertMenu = new string[,] {
			{"${res:Dialog.Options.ExternalTool.QuickInsertMenu.FullItemPath}",      "${ItemPath}"},
			{"${res:Dialog.Options.ExternalTool.QuickInsertMenu.FullItemDirectory}", "${ItemDir}"},
			{"${res:Dialog.Options.ExternalTool.QuickInsertMenu.ItemFileName}",      "${ItemFileName}"},
			{"${res:Dialog.Options.ExternalTool.QuickInsertMenu.ItemExtension}",     "${ItemExt}"},
			{"-", ""},
			{"${res:Dialog.Options.ExternalTool.QuickInsertMenu.CurrentLine}",   "${CurLine}"},
			{"${res:Dialog.Options.ExternalTool.QuickInsertMenu.CurrentColumn}", "${CurCol}"},
			{"${res:Dialog.Options.ExternalTool.QuickInsertMenu.CurrentText}",   "${CurText}"},
			{"-", ""},
			{"${res:Dialog.Options.ExternalTool.QuickInsertMenu.FullTargetPath}",  "${TargetPath}"},
			{"${res:Dialog.Options.ExternalTool.QuickInsertMenu.TargetDirectory}", "${TargetDir}"},
			{"${res:Dialog.Options.ExternalTool.QuickInsertMenu.TargetName}",      "${TargetName}"},
			{"${res:Dialog.Options.ExternalTool.QuickInsertMenu.TargetExtension}", "${TargetExt}"},
			{"-", ""},
			{"${res:Dialog.Options.ExternalTool.QuickInsertMenu.ProjectDirectory}", "${ProjectDir}"},
			{"${res:Dialog.Options.ExternalTool.QuickInsertMenu.ProjectFileName}",  "${ProjectFileName}"},
			{"-", ""},
			{"${res:Dialog.Options.ExternalTool.QuickInsertMenu.CombineDirectory}", "${CombineDir}"},
			{"${res:Dialog.Options.ExternalTool.QuickInsertMenu.CombineFileName}",  "${CombineFileName}"},
			{"-", ""},
			{"${res:Dialog.Options.ExternalTool.QuickInsertMenu.SharpDevelopStartupPath}",  "${StartupPath}"},
		};
		
		static string[,] workingDirInsertMenu = new string[,] {
			{"${res:Dialog.Options.ExternalTool.QuickInsertMenu.FullItemDirectory}", "${ItemDir}"},
			{"-", ""},
			{"${res:Dialog.Options.ExternalTool.QuickInsertMenu.TargetDirectory}", "${TargetDir}"},
			{"${res:Dialog.Options.ExternalTool.QuickInsertMenu.TargetName}",      "${TargetName}"},
			{"-", ""},
			{"${res:Dialog.Options.ExternalTool.QuickInsertMenu.ProjectDirectory}", "${ProjectDir}"},
			{"-", ""},
			{"${res:Dialog.Options.ExternalTool.QuickInsertMenu.CombineDirectory}", "${CombineDir}"},
			{"-", ""},
			{"${res:Dialog.Options.ExternalTool.QuickInsertMenu.SharpDevelopStartupPath}",  "${StartupPath}"},
		};
		
		// gtk controls
		ListStore toolListBoxStore;
		TreeView toolListBox;
		Entry titleTextBox; 
		Entry commandTextBox; 
		Entry argumentTextBox; 
		Entry workingDirTextBox; 
		CheckButton promptArgsCheckBox; 
		CheckButton useOutputPadCheckBox; 
		Label titleLabel; 
		Label argumentLabel; 
		Label commandLabel; 
		Label workingDirLabel; 
		Button browseButton; 
		Button argumentQuickInsertButton; 
		Button workingDirQuickInsertButton; 
		Button moveUpButton; 
		Button moveDownButton;
		Button addButton; 
		Button removeButton;
			
		// these are the control names which are enabled/disabled depending if tool is selected
		Widget[] dependendControls;
		
		// needed for treeview listbox
		int toolListBoxItemCount = 0;
		
		// Services		
		FileUtilityService FileUtilityService = (FileUtilityService)ServiceManager.Services.GetService(typeof(FileUtilityService));
		StringParserService StringParserService = (StringParserService)ServiceManager.Services.GetService (typeof (StringParserService));
		PropertyService PropertyService = (PropertyService)ServiceManager.Services.GetService(typeof(PropertyService));
		MessageService MessageService = (MessageService)ServiceManager.Services.GetService(typeof(MessageService));
		MenuService MenuService = (MenuService)ServiceManager.Services.GetService(typeof(MenuService));
		
		
		public override void LoadPanelContents()
		{
			// set up the form controls instance
			SetupPanelInstance();
			
			// add each tool to the treeStore
			foreach (object o in ToolLoader.Tool) {
					toolListBoxStore.AppendValues(((ExternalTool)o).MenuCommand, (ExternalTool) o);
					toolListBoxItemCount ++;
			}
			
			toolListBox.Reorderable = false;
			toolListBox.HeadersVisible = true;
			toolListBox.Selection.Mode = SelectionMode.Multiple;
			toolListBox.Model = toolListBoxStore;
			
			toolListBox.AppendColumn (
				StringParserService.Parse("${res:Dialog.Options.ExternalTool.ToolsLabel}"), 
				new CellRendererText (), 
				"text", 
				0);
			
			MenuService.CreateQuickInsertMenu(argumentTextBox,
			                                  argumentQuickInsertButton,
			                                  argumentQuickInsertMenu);
			
			MenuService.CreateQuickInsertMenu(workingDirTextBox,
			                                  workingDirQuickInsertButton,
			                                  workingDirInsertMenu);

			toolListBox.Selection.Changed += new EventHandler(selectEvent);
			
			removeButton.Clicked   += new EventHandler(removeEvent);
			addButton.Clicked      += new EventHandler(addEvent);
			moveUpButton.Clicked   += new EventHandler(moveUpEvent);
			moveDownButton.Clicked += new EventHandler(moveDownEvent);
			
			browseButton.Clicked   += new EventHandler(browseEvent);
			
			selectEvent(this, EventArgs.Empty);
		}
		
		public override bool StorePanelContents()
		{
			ArrayList newlist = new ArrayList();
			TreeIter first;
			
			if(toolListBox.Model.GetIterFirst(out first))
			{
				TreeIter current = first;
				
				do {
				// loop through items in the tree
				
					ExternalTool tool = toolListBox.Model.GetValue(current, 1) as ExternalTool;
					if (!FileUtilityService.IsValidFileName(tool.Command)) {
						MessageService.ShowError(String.Format("The command of tool \"{0}\" is invalid.", tool.MenuCommand));
						return false;
					}
					if ((tool.InitialDirectory != "") && (!FileUtilityService.IsValidFileName(tool.InitialDirectory))) {
						MessageService.ShowError(String.Format("The working directory of tool \"{0}\" is invalid.", tool.MenuCommand));
						return false;
					}
					newlist.Add(tool);				 
				} while(toolListBox.Model.IterNext(out current));
			}
			ToolLoader.Tool = newlist;
			ToolLoader.SaveTools();
			return true;
		}
		
		void SetupPanelInstance()
		{
			// instantiate controls			
			toolListBoxStore = new ListStore(typeof(string), typeof(ExternalTool));
			toolListBox = new TreeView();
			toolListBox.SetSizeRequest(200, 150);
			titleTextBox = new Entry(); 
			commandTextBox = new Entry(); 
			argumentTextBox = new Entry(); 
			workingDirTextBox = new Entry(); 
			promptArgsCheckBox = new CheckButton(StringParserService.Parse("${res:Dialog.Options.ExternalTool.PromptForArgsCheckBox}")); 
			useOutputPadCheckBox = new CheckButton(StringParserService.Parse("${res:Dialog.Options.ExternalTool.UseOutputWindow}")); 
			titleLabel = new Label(StringParserService.Parse("${res:Dialog.Options.ExternalTool.TitleLabel}")); 
			argumentLabel = new Label(StringParserService.Parse("${res:Dialog.Options.ExternalTool.ArgumentLabel}")); 
			commandLabel = new Label(StringParserService.Parse("${res:Dialog.Options.ExternalTool.CommandLabel}")); 
			workingDirLabel = new Label(StringParserService.Parse("${res:Dialog.Options.ExternalTool.WorkingDirLabel}"));			
			browseButton = new Button("..."); 
			argumentQuickInsertButton = new Button(">"); 
			workingDirQuickInsertButton = new Button(">"); 
			moveUpButton = new Button(StringParserService.Parse("${res:Dialog.Options.ExternalTool.MoveUpButton}")); 
			moveDownButton = new Button(StringParserService.Parse("${res:Dialog.Options.ExternalTool.MoveDownButton}"));
			removeButton = new Button(StringParserService.Parse("${res:Global.RemoveButtonText}"));
			addButton = new Button(StringParserService.Parse("${res:Global.AddButtonText}"));
			
			dependendControls = new Widget[] {
				titleTextBox, commandTextBox, argumentTextBox, 
				workingDirTextBox, promptArgsCheckBox, useOutputPadCheckBox, 
				titleLabel, argumentLabel, commandLabel, 
				workingDirLabel, browseButton, argumentQuickInsertButton, 
				workingDirQuickInsertButton, moveUpButton, moveDownButton
			};
			
			// pack all the controls
			VBox vBox1 = new VBox(false, 2);
			vBox1.PackStart(addButton, false, false, 2);
			vBox1.PackStart(removeButton, false, false, 2);
			vBox1.PackEnd(moveDownButton, false, false, 2);
			vBox1.PackEnd(moveUpButton, false, false, 2);			
			
			HBox hBox1 = new HBox(false, 2);
			hBox1.PackStart(toolListBox, false, true, 2);
			hBox1.PackStart(vBox1, false, false, 2);
			
			Table table1 = new Table(4, 3, false);
			table1.Attach(titleLabel, 0, 1, 0, 1);
			table1.Attach(titleTextBox, 1, 3, 0, 1);
			table1.Attach(commandLabel, 0, 1, 1, 2);
			table1.Attach(commandTextBox, 1, 2, 1, 2);
			table1.Attach(browseButton, 2, 3, 1, 2);
			table1.Attach(argumentLabel, 0, 1, 2, 3);
			table1.Attach(argumentTextBox, 1, 2, 2, 3);
			table1.Attach(argumentQuickInsertButton, 2, 3, 2, 3);
			table1.Attach(workingDirLabel, 0, 1, 3, 4);
			table1.Attach(workingDirTextBox, 1, 2, 3, 4);
			table1.Attach(workingDirQuickInsertButton, 2, 3, 3, 4);
			
			VBox mainBox = new VBox(false, 2);
			mainBox.PackStart(hBox1, false, false, 2);
			mainBox.PackStart(table1, false, false, 2);
			mainBox.PackStart(promptArgsCheckBox, false, false, 2);
			mainBox.PackStart(useOutputPadCheckBox, false, false, 2);
			this.Add(mainBox);
		}
		
		void browseEvent(object sender, EventArgs e)
		{
			Gtk.FileSelection fs = new Gtk.FileSelection ("File to Open");
			int response = fs.Run ();
			string name = fs.Filename;
			fs.Destroy ();
			if (response == (int)Gtk.ResponseType.Ok) {
				commandTextBox.Text = name;
			}
		}
		
		
		void moveUpEvent(object sender, EventArgs e)
		{
			if(toolListBox.Selection.CountSelectedRows() == 1)
			{
				TreeIter selectedItem;
				TreeModel ls;				
				((ListStore)toolListBox.Model).GetIter(out selectedItem, (TreePath) toolListBox.Selection.GetSelectedRows(out ls)[0]);
				// we know we have a selected item so get it's index
				// use that to get the path of the item before it, and swap the two
				int index = GetSelectedIndex(toolListBox);
				// only swap if at the top
				if(index > 0)
				{
					TreeIter prev; 
					if(toolListBox.Model.GetIterFromString(out prev, (index - 1).ToString()))
					{
						((ListStore)ls).Swap(selectedItem, prev);
					}
				}
			}
		}
		void moveDownEvent(object sender, EventArgs e)
		{
			if(toolListBox.Selection.CountSelectedRows() == 1)
			{
				TreeIter selectedItem;
				TreeModel ls;				
				((ListStore)toolListBox.Model).GetIter(out selectedItem, (TreePath) toolListBox.Selection.GetSelectedRows(out ls)[0]);
				
				// swap it with the next one
				TreeIter toSwap = selectedItem;
				if(ls.IterNext(out toSwap))
				{
					((ListStore)ls).Swap(selectedItem, toSwap);
				}
			}
		}
		
		void setToolValues(object sender, EventArgs e)
		{
							
			ExternalTool selectedItem = null;
			if(toolListBox.Selection.CountSelectedRows() == 1)
			{
				TreeIter selectedIter;
				TreeModel lv;				
				((ListStore)toolListBox.Model).GetIter(out selectedIter, (TreePath) toolListBox.Selection.GetSelectedRows(out lv)[0]);
				
				// get the value as an external tool object
				selectedItem = lv.GetValue(selectedIter, 1) as ExternalTool;
				
				
				lv.SetValue(selectedIter, 0, titleTextBox.Text);
				selectedItem.MenuCommand        = titleTextBox.Text;
				selectedItem.Command            = commandTextBox.Text;
				selectedItem.Arguments          = argumentTextBox.Text;
				selectedItem.InitialDirectory   = workingDirTextBox.Text;
				selectedItem.PromptForArguments = promptArgsCheckBox.Active;
				selectedItem.UseOutputPad       = useOutputPadCheckBox.Active;
			}
		}
		
		void selectEvent(object sender, EventArgs e)
		{
			SetEnabledStatus(toolListBox.Selection.CountSelectedRows() > 0, removeButton);
			
			titleTextBox.Changed      -= new EventHandler(setToolValues);
			commandTextBox.Changed    -= new EventHandler(setToolValues);
			argumentTextBox.Changed   -= new EventHandler(setToolValues);
			workingDirTextBox.Changed   -= new EventHandler(setToolValues);
			promptArgsCheckBox.Toggled   -= new EventHandler(setToolValues);
			useOutputPadCheckBox.Toggled -= new EventHandler(setToolValues);
			
			if (toolListBox.Selection.CountSelectedRows() == 1) {				
				TreeIter selectedIter;
				TreeModel ls;
				((ListStore)toolListBox.Model).GetIter(out selectedIter, (TreePath) toolListBox.Selection.GetSelectedRows(out ls)[0]);				
				
				// get the value as an external tool object				
				ExternalTool selectedItem = (ExternalTool) toolListBox.Model.GetValue(selectedIter, 1);
				
				SetEnabledStatus(true, dependendControls);
				titleTextBox.Text      = selectedItem.MenuCommand;
				commandTextBox.Text    = selectedItem.Command;
				argumentTextBox.Text   = selectedItem.Arguments;
				workingDirTextBox.Text = selectedItem.InitialDirectory;
				promptArgsCheckBox.Active   = selectedItem.PromptForArguments;
				useOutputPadCheckBox.Active = selectedItem.UseOutputPad;
			} else {
				SetEnabledStatus(false, dependendControls);
				
				titleTextBox.Text      = String.Empty;
				commandTextBox.Text    = String.Empty;
				argumentTextBox.Text   = String.Empty;
				workingDirTextBox.Text = String.Empty;
				promptArgsCheckBox.Active   = false;
				useOutputPadCheckBox.Active = false;
			}
			
			titleTextBox.Changed      += new EventHandler(setToolValues);
			commandTextBox.Changed    += new EventHandler(setToolValues);
			argumentTextBox.Changed   += new EventHandler(setToolValues);
			workingDirTextBox.Changed += new EventHandler(setToolValues);
			promptArgsCheckBox.Toggled   += new EventHandler(setToolValues);
			useOutputPadCheckBox.Toggled += new EventHandler(setToolValues);
		}
		
		void removeEvent(object sender, EventArgs e)
		{
			int selectedItemCount = toolListBox.Selection.CountSelectedRows();
			if(selectedItemCount > 0) {
				int maxIndex = 0;
				// first copy the selected item paths into a temp array
				TreeIter[] selectedIters = new TreeIter[selectedItemCount];
				TreeModel lv;
				GLib.List pathList = toolListBox.Selection.GetSelectedRows(out lv);								
				for(int i = 0; i < selectedItemCount; i++) {
					TreePath path = (TreePath) pathList[i];
					((ListStore)lv).GetIter(out selectedIters[i], path);
					maxIndex = path.Indices[0];
				}
				
				// now delete each item in that list
				foreach(TreeIter toDelete in selectedIters) {
					TreeIter itr = toDelete;
					toolListBoxItemCount --;
					((ListStore)lv).Remove(out itr);
				}
				
				if (toolListBoxItemCount == 0) {
					selectEvent(this, EventArgs.Empty);
				} else {
					SetSelectedIndex(toolListBox, Math.Min(maxIndex,toolListBoxItemCount - 1));
				}
			}
		}
		
		void addEvent(object sender, EventArgs e)
		{
			TreeIter itr = toolListBoxStore.AppendValues("New Tool", new ExternalTool());
			toolListBoxItemCount ++;
			toolListBox.Selection.UnselectAll();
			toolListBox.Selection.SelectIter(itr);
		}
		
		// added this event to get the last select row index from gtk TreeView
		int GetSelectedIndex(TreeView tv)
		{
			if(toolListBox.Selection.CountSelectedRows() == 1)
			{
				TreeIter selectedIter;
				TreeModel lv;				
				((ListStore)toolListBox.Model).GetIter(out selectedIter, (TreePath) toolListBox.Selection.GetSelectedRows(out lv)[0]);
			
				// return index of first level node (since only 1 level list model)
				return lv.GetPath(selectedIter).Indices[0];
			}
			else
			{
				return -1;
			}
		}
		
		// added this event to set a specific row as selected from index
		void SetSelectedIndex(TreeView tv, int index)
		{
			//convert index to a path
			TreePath path = new TreePath(index.ToString());
			tv.Selection.UnselectAll();
			tv.Selection.SelectPath(path);
		}
		
		// disables or enables (sets sensitivty) a specified array of widgets
		public void SetEnabledStatus(bool enabled, params Widget[] controls)
		{
			foreach (Widget control in controls) {				
				if (control == null) {
					MessageService.ShowError("Control not found!");
				} else {
					control.Sensitive = enabled;
				}
			}
		}
	}
}

--=-I0L/+FrgNs872vTgTIpQ--