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
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.
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
43 using System
.ComponentModel
;
45 using System
.Windows
.Forms
;
47 using System
.Collections
;
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
)
70 mItems
= new Hashtable();
71 mBuilders
= new Hashtable();
73 mMainMenu
= new MainMenu();
74 mForm
.Menu
= mMainMenu
;
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
)
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();
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
),
125 mItems
.Add(values
[2], mCurrentMenuItem
);
126 root
.MenuItems
.Add(mCurrentMenuItem
);
127 Recurse(reader2
, mCurrentMenuItem
);
129 case "menuseparator":
130 mCurrentMenuItem
= new MenuItem("-");
131 root
.MenuItems
.Add(mCurrentMenuItem
);
135 if (reader2
.MoveToAttribute("id") &&
136 reader2
.ReadAttributeValue())
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);
146 builder
.DataStore
= DataStoreRegistry
.GetDataStore(datastore
);
147 builder
.DataStore
.AddObserver(builder
);
148 builder
.OnCommand
+= new EventHandler(OnCommandInternal
);
149 mBuilders
.Add(builder
.GetHashCode(), builder
);
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
;
168 /// The Command String associated with this item. Used with |IController|
170 public String Command
179 /// Any data associated with this item. Useful to hold DataStore ID references.
181 private Object mData
;
190 public static String
GenerateAccessKeyString(String aLabel
, String aAccessKey
)
192 int idx
= aLabel
.ToLower().IndexOf(aAccessKey
.ToLower());
194 return aLabel
.Insert(idx
, "&");
196 return aLabel
+ " (&" + aAccessKey
.ToUpper() + ")";
199 public ManticoreMenuItem(String aLabel
, EventHandler aHandler
, String aCommand
, Object aData
) : base(aLabel
, aHandler
)
206 public class ManticoreTreeNode
: TreeNode
208 private Object mData
;
217 public ManticoreTreeNode(String aLabel
, Object aData
) : base(aLabel
)
223 public class ManticoreTreeView
: TreeView
226 /// Holds imageURL->imageIndex mapping of images
228 protected Hashtable mImages
= null;
231 /// Number of images currently hashed (current image index)
233 protected int mImageCount
= 0;
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.
239 protected Color mIconTransparentColor
;
241 public virtual bool ShouldBuild(CommandTarget aTarget
)
246 public int GetIconIndex(String aIconURL
)
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
);
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.
270 return mImageCount
++;
273 return (int) mImages
[key
];
277 /// Client overrides to provide special Icons for the particular treeview.
279 /// <param name="aCommandTarget"></param>
280 /// <returns></returns>
281 public virtual int GetIconIndex(CommandTarget aCommandTarget
)