| 0: | // MenuCreator.cs | |
| 1: | // Copyright (C) 2001 Mike Krueger | |
| 2: | // | |
| 3: | // This program is free software; you can redistribute it and/or modify | |
| 4: | // it under the terms of the GNU General Public License as published by | |
| 5: | // the Free Software Foundation; either version 2 of the License, or | |
| 6: | // (at your option) any later version. | |
| 7: | // | |
| 8: | // This program is distributed in the hope that it will be useful, | |
| 9: | // but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 10: | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 11: | // GNU General Public License for more details. | |
| 12: | // | |
| 13: | // You should have received a copy of the GNU General Public License | |
| 14: | // along with this program; if not, write to the Free Software | |
| 15: | // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
| 16: | ||
| 17: | using System; | |
| 18: | using System.Drawing; | |
| 19: | using System.Reflection; | |
| 20: | using System.Windows.Forms; | |
| 21: | using System.Xml; | |
| 22: | ||
| 23: | using SharpDevelop.Gui; | |
| 24: | using SharpDevelop.Gui.Components; | |
| 25: | using SharpDevelop.Tool.Data; | |
| 26: | using SharpDevelop.Tool.Text; | |
| 27: | using SharpDevelop.Actions; | |
| 28: | using SharpDevelop.Actions.Menu; | |
| 29: | using SharpDevelop.Internal.Messages; | |
| 30: | ||
| 31: | namespace SharpDevelop.Tool.Function { | |
| 32: | ||
| 33: | public class MenuCreator { | |
| 34: | ||
| 35: | public static MenuItem[] CreateMenu(MainWindow mainwindow, string eventnamespace, XmlElement xmlelement) | |
| 36: | { | |
| 37: | return CreateMenu(mainwindow, Assembly.GetCallingAssembly(), eventnamespace, xmlelement); | |
| 38: | } | |
| 39: | ||
| 40: | public static MenuItem[] CreateMenu(MainWindow mainwindow,Assembly assembly, string eventnamespace, XmlElement xmlelement) | |
| 41: | { | |
| 42: | XmlNodeList nodes = xmlelement.ChildNodes; | |
| 43: | ||
| 44: | IconMenuItem[] item = new IconMenuItem[nodes.Count]; | |
| 45: | ||
| 46: | for (int i = 0; i < nodes.Count; ++i) { | |
| 47: | XmlElement el = (XmlElement)nodes[i]; | |
| 48: | MenuAction action = null; | |
| 49: | EventHandler handler1 = null; | |
| 50: | EventHandler handler2 = null; | |
| 51: | ||
| 52: | XmlAttribute attribute = el.Attributes["ATTRIBUTE"]; | |
| 53: | ||
| 54: | bool link = attribute == null ? false : attribute.InnerText.ToUpper() == "WEBLINK" || attribute.InnerText.ToUpper() == "LINK"; | |
| 55: | bool ischecked = false; | |
| 56: | ||
| 57: | // set eventhandler | |
| 58: | if (el.Attributes["EVENT"] != null) { | |
| 59: | ISdPlugin ia = null; | |
| 60: | if (link) { | |
| 61: | if (attribute.InnerText.ToUpper() == "LINK") | |
| 62: | ia = new SharpDevelop.Actions.Menu.GotoLink(el.Attributes["EVENT"].InnerText); | |
| 63: | else | |
| 64: | ia = new SharpDevelop.Actions.Menu.GotoWebSite(el.Attributes["EVENT"].InnerText); | |
| 65: | } else { | |
| 66: | string objname = eventnamespace + el.Attributes["EVENT"].InnerText; | |
| 67: | ia = (ISdPlugin)assembly.CreateInstance(objname); | |
| 68: | } | |
| 69: | ||
| 70: | if (ia == null) { | |
| 71: | MessageBox.Show("Can't create MenuAction " + el.Attributes["EVENT"].InnerText, "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning); | |
| 72: | } else { | |
| 73: | if (ia.MessageHandler != null) | |
| 74: | Messenger.AddMessageHandler(ia.MessageHandler); | |
| 75: | ischecked = ia is ISdToggleablePlugin; | |
| 76: | action = new MenuAction(mainwindow, ia); | |
| 77: | handler1 = new EventHandler(action.Invoke); | |
| 78: | if (ischecked) | |
| 79: | handler2 = new EventHandler(action.Toggle); | |
| 80: | } | |
| 81: | } | |
| 82: | ||
| 83: | // create MenuItem | |
| 84: | string s1 = el.Attributes["NAME"] == null ? null : StringParser.Parse(el.Attributes["NAME"].InnerText, new string [,] {}); | |
| 85: | string s2 = el.Attributes["DESCRIPTION"] == null ? null : StringParser.Parse(el.Attributes["DESCRIPTION"].InnerText, new string [,] {}); | |
| 86: | ||
| 87: | item[i] = new IconMenuItem(mainwindow, s1, handler1, s2); | |
| 88: | ||
| 89: | if (ischecked && action.Plugin != null) { | |
| 90: | action.MenuItem = item[i]; | |
| 91: | SharpDevelopMain.InitComplete += new EventHandler(action.FirstInit); | |
| 92: | item[i].Click += handler2; | |
| 93: | } | |
| 94: | ||
| 95: | // read attributes | |
| 96: | if (attribute != null) { | |
| 97: | item[i].ItemTag = (SpecialItemTag)Enum.Parse(typeof(SpecialItemTag), attribute.InnerText); | |
| 98: | } | |
| 99: | ||
| 100: | // read open status | |
| 101: | XmlAttribute active = el.Attributes["ACTIVE"]; | |
| 102: | if (active != null) { | |
| 103: | item[i].IconEnabled = (IconEnabledStatus)Enum.Parse(typeof(IconEnabledStatus), active.InnerText); | |
| 104: | } | |
| 105: | ||
| 106: | // set menuitem icon | |
| 107: | XmlAttribute icon = el.Attributes["ICON"]; | |
| 108: | if (icon != null) { | |
| 109: | item[i].Icon = FileUtility.GetBitmap(icon.InnerText); | |
| 110: | } | |
| 111: | ||
| 112: | // set menuitem shortcut | |
| 113: | XmlAttribute shortcut = el.Attributes["SHORTCUT"]; | |
| 114: | if (shortcut != null) { | |
| 115: | try { | |
| 116: | item[i].Shortcut = (Shortcut)((Shortcut.F1.GetType()).InvokeMember(shortcut.InnerText, BindingFlags.GetField, null, Shortcut.F1, new object[0])); | |
| 117: | } catch (Exception) { | |
| 118: | item[i].Shortcut = Shortcut.None; | |
| 119: | item[i].ShortcutText = shortcut.InnerText; | |
| 120: | } | |
| 121: | } | |
| 122: | ||
| 123: | // add submenus | |
| 124: | MenuItem[] submenus = CreateMenu(mainwindow, assembly, eventnamespace, el); | |
| 125: | foreach (MenuItem menuitem in submenus) | |
| 126: | item[i].MenuItems.Add(menuitem); | |
| 127: | } | |
| 128: | return item; | |
| 129: | } | |
| 130: | } | |
| 131: | } |
This page was automatically generated by SharpDevelop.