Back to index

lightning-sunbird  0.9+nobinonly
toolkit.menus.cs
Go to the documentation of this file.
00001 /* -*- Mode: C#; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
00002  *
00003  * ***** BEGIN LICENSE BLOCK *****
00004  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00005  *
00006  * The contents of this file are subject to the Mozilla Public License Version
00007  * 1.1 (the "License"); you may not use this file except in compliance with
00008  * the License. You may obtain a copy of the License at
00009  * http://www.mozilla.org/MPL/
00010  *
00011  * Software distributed under the License is distributed on an "AS IS" basis,
00012  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00013  * for the specific language governing rights and limitations under the
00014  * License.
00015  *
00016  * The Original Code is Manticore.
00017  *
00018  * The Initial Developer of the Original Code is
00019  * Silverstone Interactive.
00020  * Portions created by the Initial Developer are Copyright (C) 2001
00021  * the Initial Developer. All Rights Reserved.
00022  *
00023  * Contributor(s):
00024  *   Ben Goodger <ben@netscape.com>
00025  *
00026  * Alternatively, the contents of this file may be used under the terms of
00027  * either the GNU General Public License Version 2 or later (the "GPL"), or
00028  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00029  * in which case the provisions of the GPL or the LGPL are applicable instead
00030  * of those above. If you wish to allow use of your version of this file only
00031  * under the terms of either the GPL or the LGPL, and not to allow others to
00032  * use your version of this file under the terms of the MPL, indicate your
00033  * decision by deleting the provisions above and replace them with the notice
00034  * and other provisions required by the GPL or the LGPL. If you do not delete
00035  * the provisions above, a recipient may use your version of this file under
00036  * the terms of any one of the MPL, the GPL or the LGPL.
00037  *
00038  * ***** END LICENSE BLOCK ***** */
00039 
00040 namespace Silverstone.Manticore.Toolkit
00041 {
00042   using System;
00043   using System.ComponentModel;
00044   using System.Drawing;
00045   using System.Windows.Forms;
00046 
00047   using System.Collections;
00048 
00049   using System.IO;
00050   using System.Xml;
00051 
00052   using Silverstone.Manticore.Core;
00053   using Silverstone.Manticore.Toolkit;
00054 
00055   public class MenuBuilder
00056   {
00057     protected String mMenuFile = "";
00058     protected Form mForm = null;
00059     protected MainMenu mMainMenu = null;
00060 
00061     public Hashtable mItems;
00062     public Hashtable mBuilders;
00063 
00064     public event EventHandler OnCommand;
00065 
00066     public MenuBuilder(String aFile, Form aForm)
00067     {
00068       mMenuFile = aFile;
00069       mForm = aForm;
00070       mItems = new Hashtable();
00071       mBuilders = new Hashtable();
00072 
00073       mMainMenu = new MainMenu();
00074       mForm.Menu = mMainMenu;
00075     }
00076 
00077     public void Build()
00078     {
00079       XmlTextReader reader;
00080       reader = new XmlTextReader(mMenuFile);
00081       
00082       reader.WhitespaceHandling = WhitespaceHandling.None;
00083       reader.MoveToContent();
00084 
00085       Recurse(reader, mMainMenu);
00086     }
00087 
00088     protected MenuItem mCurrentMenuItem;
00089     protected void Recurse(XmlTextReader reader, Menu root)
00090     {
00091       String inner = reader.ReadInnerXml();
00092     
00093       NameTable nt = new NameTable();
00094       XmlNamespaceManager nsmgr = new XmlNamespaceManager(nt);
00095       XmlParserContext ctxt = new XmlParserContext(null, nsmgr, null, XmlSpace.None);
00096       XmlTextReader reader2 = new XmlTextReader(inner, XmlNodeType.Element, ctxt);
00097 
00098       while (reader2.Read()) 
00099       {
00100         if (reader2.NodeType == XmlNodeType.Element) 
00101         {
00102           switch (reader2.LocalName) 
00103           {
00104             case "menu":
00105               // Menuitem. Find the name, accesskey, command and id strings
00106               String[] values = new String[3] {"", "", ""};
00107               String[] names = new String[3] {"label", "accesskey", "command"};
00108               for (int i = 0; i < names.Length; ++i) 
00109               {
00110                 if (reader2.MoveToAttribute(names[i]) &&
00111                   reader2.ReadAttributeValue())
00112                   values[i] = reader2.Value; // XXX need to handle entities
00113                 reader2.MoveToElement();
00114               }
00115 
00116               // Handle Accesskey
00117               values[0] = ManticoreMenuItem.GenerateAccessKeyString(values[0], values[1]);
00118 
00119               // Create menu item and attach an event handler
00120               // BLUESKY - should we support data stored in the XML file as an attribute?
00121               mCurrentMenuItem = new ManticoreMenuItem(values[0], 
00122                                                        new EventHandler(OnCommandInternal),
00123                                                        values[2], "");
00124               if (values[2] != "")
00125                 mItems.Add(values[2], mCurrentMenuItem);
00126               root.MenuItems.Add(mCurrentMenuItem);
00127               Recurse(reader2, mCurrentMenuItem);
00128               break;
00129             case "menuseparator":
00130               mCurrentMenuItem = new MenuItem("-");
00131               root.MenuItems.Add(mCurrentMenuItem);
00132               break;
00133             case "menubuilder":
00134               String id = "";
00135               if (reader2.MoveToAttribute("id") &&
00136                   reader2.ReadAttributeValue())
00137                 id = reader2.Value;
00138               reader2.MoveToElement();
00139               String datastore = "";
00140               if (reader2.MoveToAttribute("datastore") && 
00141                   reader2.ReadAttributeValue())
00142                 datastore = reader2.Value;
00143 
00144               BaseMenuBuilder builder = new BaseMenuBuilder(mMainMenu, root as MenuItem, mCurrentMenuItem, null);
00145               builder.Root = id;
00146               builder.DataStore = DataStoreRegistry.GetDataStore(datastore);
00147               builder.DataStore.AddObserver(builder);
00148               builder.OnCommand += new EventHandler(OnCommandInternal);
00149               mBuilders.Add(builder.GetHashCode(), builder);
00150               break;
00151           }
00152         }
00153       }
00154     }
00155 
00156     protected void OnCommandInternal(Object sender, EventArgs e)
00157     {
00158       if (OnCommand != null) 
00159         OnCommand(sender, e);
00160     }
00161   }
00162 
00163   // XXX should be "ManticoreMenuItem"
00164   public class ManticoreMenuItem : MenuItem
00165   {
00166     private String mCommand;
00170     public String Command
00171     {
00172       get
00173       {
00174         return mCommand;
00175       }
00176     }
00177 
00181     private Object mData;
00182     public Object Data 
00183     {
00184       get 
00185       {
00186         return mData;
00187       }
00188     }
00189 
00190     public static String GenerateAccessKeyString(String aLabel, String aAccessKey)
00191     {
00192       int idx = aLabel.ToLower().IndexOf(aAccessKey.ToLower());
00193       if (idx != -1)
00194         return aLabel.Insert(idx, "&");
00195       else 
00196         return aLabel + " (&" + aAccessKey.ToUpper() + ")";
00197     }
00198 
00199     public ManticoreMenuItem(String aLabel, EventHandler aHandler, String aCommand, Object aData) : base(aLabel, aHandler)
00200     {
00201       mCommand = aCommand;
00202       mData = aData;
00203     }
00204   }
00205 
00206   public class ManticoreTreeNode : TreeNode
00207   {
00208     private Object mData;
00209     public Object Data
00210     {
00211       get 
00212       {
00213         return mData;
00214       }
00215     }
00216 
00217     public ManticoreTreeNode(String aLabel, Object aData) : base(aLabel)
00218     {
00219       mData = aData;
00220     }
00221   }
00222 
00223   public class ManticoreTreeView : TreeView
00224   {
00228     protected Hashtable mImages = null;
00229 
00233     protected int mImageCount = 0;
00234 
00239     protected Color mIconTransparentColor;
00240 
00241     public virtual bool ShouldBuild(CommandTarget aTarget)
00242     {
00243       return true;
00244     }
00245 
00246     public int GetIconIndex(String aIconURL)
00247     {
00248       if (aIconURL == "")
00249         return -1;
00250 
00251       if (mImages == null)
00252         mImages = new Hashtable();
00253 
00254       int key = aIconURL.GetHashCode();
00255       if (!mImages.ContainsKey(key)) 
00256       {
00257         if (ImageList == null) 
00258           ImageList = new ImageList();
00259         Console.WriteLine(mIconTransparentColor);
00260         try 
00261         {
00262           ImageList.Images.Add(Image.FromFile(aIconURL), mIconTransparentColor);
00263           mImages.Add(key, mImageCount);
00264         }
00265         catch (FileNotFoundException)
00266         {
00267           // If the file can't be found, don't add it to the list.
00268           return -1;
00269         }
00270         return mImageCount++;
00271       }
00272       else
00273         return (int) mImages[key];
00274     }
00275 
00281     public virtual int GetIconIndex(CommandTarget aCommandTarget)
00282     {
00283       return -1;
00284     }
00285   }
00286 
00287 }
00288 
00289