Import from 1.9a8 tarball
[mozilla-extra.git] / extensions / manticore / toolkit / toolkit.menus.cs
blob3088f24d37ffee829e65eaa44c05c1250c6772c9
1 /* -*- Mode: C#; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
3 * ***** BEGIN LICENSE BLOCK *****
4 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
6 * The contents of this file are subject to the Mozilla Public License Version
7 * 1.1 (the "License"); you may not use this file except in compliance with
8 * the License. You may obtain a copy of the License at
9 * http://www.mozilla.org/MPL/
11 * Software distributed under the License is distributed on an "AS IS" basis,
12 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13 * for the specific language governing rights and limitations under the
14 * License.
16 * The Original Code is Manticore.
18 * The Initial Developer of the Original Code is
19 * Silverstone Interactive.
20 * Portions created by the Initial Developer are Copyright (C) 2001
21 * the Initial Developer. All Rights Reserved.
23 * Contributor(s):
24 * Ben Goodger <ben@netscape.com>
26 * Alternatively, the contents of this file may be used under the terms of
27 * either the GNU General Public License Version 2 or later (the "GPL"), or
28 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
29 * in which case the provisions of the GPL or the LGPL are applicable instead
30 * of those above. If you wish to allow use of your version of this file only
31 * under the terms of either the GPL or the LGPL, and not to allow others to
32 * use your version of this file under the terms of the MPL, indicate your
33 * decision by deleting the provisions above and replace them with the notice
34 * and other provisions required by the GPL or the LGPL. If you do not delete
35 * the provisions above, a recipient may use your version of this file under
36 * the terms of any one of the MPL, the GPL or the LGPL.
38 * ***** END LICENSE BLOCK ***** */
40 namespace Silverstone.Manticore.Toolkit
42 using System;
43 using System.ComponentModel;
44 using System.Drawing;
45 using System.Windows.Forms;
47 using System.Collections;
49 using System.IO;
50 using System.Xml;
52 using Silverstone.Manticore.Core;
53 using Silverstone.Manticore.Toolkit;
55 public class MenuBuilder
57 protected String mMenuFile = "";
58 protected Form mForm = null;
59 protected MainMenu mMainMenu = null;
61 public Hashtable mItems;
62 public Hashtable mBuilders;
64 public event EventHandler OnCommand;
66 public MenuBuilder(String aFile, Form aForm)
68 mMenuFile = aFile;
69 mForm = aForm;
70 mItems = new Hashtable();
71 mBuilders = new Hashtable();
73 mMainMenu = new MainMenu();
74 mForm.Menu = mMainMenu;
77 public void Build()
79 XmlTextReader reader;
80 reader = new XmlTextReader(mMenuFile);
82 reader.WhitespaceHandling = WhitespaceHandling.None;
83 reader.MoveToContent();
85 Recurse(reader, mMainMenu);
88 protected MenuItem mCurrentMenuItem;
89 protected void Recurse(XmlTextReader reader, Menu root)
91 String inner = reader.ReadInnerXml();
93 NameTable nt = new NameTable();
94 XmlNamespaceManager nsmgr = new XmlNamespaceManager(nt);
95 XmlParserContext ctxt = new XmlParserContext(null, nsmgr, null, XmlSpace.None);
96 XmlTextReader reader2 = new XmlTextReader(inner, XmlNodeType.Element, ctxt);
98 while (reader2.Read())
100 if (reader2.NodeType == XmlNodeType.Element)
102 switch (reader2.LocalName)
104 case "menu":
105 // Menuitem. Find the name, accesskey, command and id strings
106 String[] values = new String[3] {"", "", ""};
107 String[] names = new String[3] {"label", "accesskey", "command"};
108 for (int i = 0; i < names.Length; ++i)
110 if (reader2.MoveToAttribute(names[i]) &&
111 reader2.ReadAttributeValue())
112 values[i] = reader2.Value; // XXX need to handle entities
113 reader2.MoveToElement();
116 // Handle Accesskey
117 values[0] = ManticoreMenuItem.GenerateAccessKeyString(values[0], values[1]);
119 // Create menu item and attach an event handler
120 // BLUESKY - should we support data stored in the XML file as an attribute?
121 mCurrentMenuItem = new ManticoreMenuItem(values[0],
122 new EventHandler(OnCommandInternal),
123 values[2], "");
124 if (values[2] != "")
125 mItems.Add(values[2], mCurrentMenuItem);
126 root.MenuItems.Add(mCurrentMenuItem);
127 Recurse(reader2, mCurrentMenuItem);
128 break;
129 case "menuseparator":
130 mCurrentMenuItem = new MenuItem("-");
131 root.MenuItems.Add(mCurrentMenuItem);
132 break;
133 case "menubuilder":
134 String id = "";
135 if (reader2.MoveToAttribute("id") &&
136 reader2.ReadAttributeValue())
137 id = reader2.Value;
138 reader2.MoveToElement();
139 String datastore = "";
140 if (reader2.MoveToAttribute("datastore") &&
141 reader2.ReadAttributeValue())
142 datastore = reader2.Value;
144 BaseMenuBuilder builder = new BaseMenuBuilder(mMainMenu, root as MenuItem, mCurrentMenuItem, null);
145 builder.Root = id;
146 builder.DataStore = DataStoreRegistry.GetDataStore(datastore);
147 builder.DataStore.AddObserver(builder);
148 builder.OnCommand += new EventHandler(OnCommandInternal);
149 mBuilders.Add(builder.GetHashCode(), builder);
150 break;
156 protected void OnCommandInternal(Object sender, EventArgs e)
158 if (OnCommand != null)
159 OnCommand(sender, e);
163 // XXX should be "ManticoreMenuItem"
164 public class ManticoreMenuItem : MenuItem
166 private String mCommand;
167 /// <summary>
168 /// The Command String associated with this item. Used with |IController|
169 /// </summary>
170 public String Command
174 return mCommand;
178 /// <summary>
179 /// Any data associated with this item. Useful to hold DataStore ID references.
180 /// </summary>
181 private Object mData;
182 public Object Data
184 get
186 return mData;
190 public static String GenerateAccessKeyString(String aLabel, String aAccessKey)
192 int idx = aLabel.ToLower().IndexOf(aAccessKey.ToLower());
193 if (idx != -1)
194 return aLabel.Insert(idx, "&");
195 else
196 return aLabel + " (&" + aAccessKey.ToUpper() + ")";
199 public ManticoreMenuItem(String aLabel, EventHandler aHandler, String aCommand, Object aData) : base(aLabel, aHandler)
201 mCommand = aCommand;
202 mData = aData;
206 public class ManticoreTreeNode : TreeNode
208 private Object mData;
209 public Object Data
211 get
213 return mData;
217 public ManticoreTreeNode(String aLabel, Object aData) : base(aLabel)
219 mData = aData;
223 public class ManticoreTreeView : TreeView
225 /// <summary>
226 /// Holds imageURL->imageIndex mapping of images
227 /// </summary>
228 protected Hashtable mImages = null;
230 /// <summary>
231 /// Number of images currently hashed (current image index)
232 /// </summary>
233 protected int mImageCount = 0;
235 /// <summary>
236 /// Transparent colour used in icons. We could be pedantic and allow this
237 /// to come from the datastore, but this'll do for now.
238 /// </summary>
239 protected Color mIconTransparentColor;
241 public virtual bool ShouldBuild(CommandTarget aTarget)
243 return true;
246 public int GetIconIndex(String aIconURL)
248 if (aIconURL == "")
249 return -1;
251 if (mImages == null)
252 mImages = new Hashtable();
254 int key = aIconURL.GetHashCode();
255 if (!mImages.ContainsKey(key))
257 if (ImageList == null)
258 ImageList = new ImageList();
259 Console.WriteLine(mIconTransparentColor);
260 try
262 ImageList.Images.Add(Image.FromFile(aIconURL), mIconTransparentColor);
263 mImages.Add(key, mImageCount);
265 catch (FileNotFoundException)
267 // If the file can't be found, don't add it to the list.
268 return -1;
270 return mImageCount++;
272 else
273 return (int) mImages[key];
276 /// <summary>
277 /// Client overrides to provide special Icons for the particular treeview.
278 /// </summary>
279 /// <param name="aCommandTarget"></param>
280 /// <returns></returns>
281 public virtual int GetIconIndex(CommandTarget aCommandTarget)
283 return -1;