Back to index

lightning-sunbird  0.9+nobinonly
toolkit.toolbars.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 
00041 namespace Silverstone.Manticore.Toolkit
00042 {
00043   using System;
00044   using System.ComponentModel;
00045   using System.Drawing;
00046   using System.Windows.Forms;
00047 
00048   using System.Collections;
00049 
00050   using System.IO;
00051   using System.Xml;
00052 
00053   using AxComCtl3;
00054   using ComCtl3;
00055 
00056   public abstract class ToolbarBuilder
00057   {
00058     protected String mToolbarFile;
00059     protected AxCoolBar mCoolBar;
00060     protected Form mForm;
00061 
00062     public Hashtable mItems;
00063 
00064     // XXX band-set hack 
00065     protected ToolBar mToolBar;
00066     public Rectangle Bounds
00067     {
00068       get 
00069       {
00070         return mToolBar.Bounds;
00071       }
00072     }
00073    
00074     public ToolbarBuilder(String aToolbarFile, Form aForm)
00075     {
00076       mToolbarFile = aToolbarFile;
00077       mForm = aForm;
00078       mItems = new Hashtable();
00079 
00080 
00081       // XXX band-set hack 
00082       mToolBar = new ToolBar();
00083       mForm.Controls.Add(mToolBar);
00084 
00085       /*
00086       // Initialize CoolBar
00087       mCoolBar = new AxCoolBar();
00088       AxHost host = mCoolBar as AxHost;
00089       host.BeginInit();
00090       host.Dock = DockStyle.Top;
00091       host.EndInit();
00092       mForm.Controls.Add(host);
00093       */
00094 
00095       // We can't build the CoolBar until after the window is visible
00096       mForm.VisibleChanged += new EventHandler(Build);
00097     }
00098 
00099     public void Build(Object sender, EventArgs e)
00100     {
00101       XmlTextReader reader;
00102       reader = new XmlTextReader(mToolbarFile);
00103       reader.WhitespaceHandling = WhitespaceHandling.None;
00104       reader.MoveToContent();
00105 
00106       bool shouldBuildNewRow = true;
00107 
00108       while (reader.Read()) 
00109       {
00110         if (reader.NodeType == XmlNodeType.Element) 
00111         {
00112           switch (reader.LocalName) 
00113           {
00114             case "toolstrip":
00115               // The next <toolbar/> we encounter should be created on a new row. 
00116               shouldBuildNewRow = true;
00117               break;
00118             case "toolbar":
00119               String[] tbvalues = new String[4] {"", "", "",  ""};
00120               String[] tbnames = new String[4] {"id", "label", "description", "visible"};
00121               for (int i = 0; i < tbnames.Length; ++i) 
00122               {
00123                 if (reader.MoveToAttribute(tbnames[i]) &&
00124                   reader.ReadAttributeValue())
00125                   tbvalues[i] = reader.Value; // XXX need to handle entities
00126                 reader.MoveToElement();
00127               }
00128 
00129               String key = tbvalues[0];
00130               String label = tbvalues[1];
00131               bool visible = tbvalues[3] == "true";
00132       
00133               // Create and add a new toolbar.
00134               mToolBar.Appearance = ToolBarAppearance.Flat;
00135               mToolBar.ButtonClick += new ToolBarButtonClickEventHandler(this.OnCommand);
00136               mForm.Controls.Add(mToolBar);
00137 
00138               //mCoolBar.Bands.Add(-1, key, label, new Object(), true, mToolBar, true);
00139 
00140               shouldBuildNewRow = false;
00141               break;
00142             case "toolbarseparator": 
00143             {
00144               if (mToolBar != null) 
00145               {
00146                 ToolBarButton button = new ToolBarButton();
00147                 button.Style = ToolBarButtonStyle.Separator;
00148                 mToolBar.Buttons.Add(button);
00149               }
00150               break;
00151             }
00152             case "toolbarbutton":
00153             {
00154               if (mToolBar != null) 
00155               {
00156                 String[] tbbvalues = new String[3] {"", "", ""};
00157                 String[] tbbnames = new String[3] {"label", "icon", "command"};
00158                 for (int i = 0; i < tbbnames.Length; ++i) 
00159                 {
00160                   if (reader.MoveToAttribute(tbbnames[i]) &&
00161                     reader.ReadAttributeValue())
00162                     tbbvalues[i] = reader.Value; // XXX need to handle entities
00163                   reader.MoveToElement();
00164                 }
00165          
00166                 ToolBarButton button = new CommandButtonItem(tbbvalues[1]);
00167                 button.Text = tbbvalues[0];
00168                 mToolBar.Buttons.Add(button);
00169               }
00170               break;
00171             }
00172           }
00173         }
00174       }
00175     }
00176 
00177     public abstract void OnCommand(Object sender, ToolBarButtonClickEventArgs e);
00178   }
00179 
00180   public class CommandButtonItem : ToolBarButton
00181   {
00182     private string mCommand;
00183     public string Command 
00184     {
00185       get 
00186       {
00187         return mCommand;
00188       }
00189     }
00190 
00191     public CommandButtonItem(String cmd) : base()
00192     {
00193       mCommand = cmd;
00194     }
00195   }
00196 }