#region Disclaimer / License
// Copyright (C) 2010, Jackie Ng
// http://trac.osgeo.org/mapguide/wiki/maestro, jumpinjackie@gmail.com
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
//
#endregion
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using ICSharpCode.Core.WinForms;
using ICSharpCode.Core;
using ICSharpCode.Core.Services;
using Maestro.Base.UI;
using Maestro.Base.Editor;
namespace Maestro.Base
{
public sealed partial class Workbench : Form
{
static Workbench instance;
///
/// Gets the instance.
///
/// The instance.
public static Workbench Instance
{
get
{
return instance;
}
}
///
/// Occurs when [workbench initialized].
///
public static event EventHandler WorkbenchInitialized = delegate { };
private static bool _init = false;
///
/// Initializes the workbench.
///
public static void InitializeWorkbench()
{
if (!_init)
{
instance = new Workbench();
_init = true;
WorkbenchInitialized(instance, EventArgs.Empty);
}
}
MenuStrip menu;
ToolStripContainer toolStripContainer;
ToolStrip toolbar;
StatusStrip status;
ToolStripStatusLabel statusLabel;
ZonedContainer contentPanel;
ContextMenuStrip ctxToolbar;
private Workbench()
{
InitializeComponent();
_toolstrips = new Dictionary();
_toolstripRegions = new Dictionary();
this.Icon = Properties.Resources.MapGuide_Maestro;
contentPanel = new ZonedContainer();
contentPanel.Dock = DockStyle.Fill;
contentPanel.ViewActivated += new ViewContentActivateEventHandler(OnViewActivated);
menu = new MenuStrip();
MenuService.AddItemsToMenu(menu.Items, this, "/Maestro/Shell/MainMenu");
toolStripContainer = new ToolStripContainer();
toolStripContainer.ContentPanel.Controls.Add(contentPanel);
toolStripContainer.Dock = DockStyle.Fill;
this.Controls.Add(toolStripContainer);
ctxToolbar = new ContextMenuStrip();
menu.ContextMenuStrip = ctxToolbar;
toolStripContainer.TopToolStripPanel.ContextMenuStrip = ctxToolbar;
toolStripContainer.BottomToolStripPanel.ContextMenuStrip = ctxToolbar;
toolStripContainer.LeftToolStripPanel.ContextMenuStrip = ctxToolbar;
toolStripContainer.RightToolStripPanel.ContextMenuStrip = ctxToolbar;
toolbar = ToolbarService.CreateToolStrip(this, "/Maestro/Shell/Toolbars/Main");
toolbar.Stretch = true;
AddToolbar("Base", toolbar, ToolbarRegion.Top, true);
status = new StatusStrip();
statusLabel = new ToolStripStatusLabel();
status.Items.Add(statusLabel);
this.Controls.Add(menu);
this.Controls.Add(status);
// Use the Idle event to update the status of menu and toolbar items.
Application.Idle += OnApplicationIdle;
}
void OnViewActivated(object sender, IViewContent content)
{
//If a site explorer was activated, update our active site explorer property
var exp = content as ISiteExplorer;
if (exp != null)
{
this.ActiveSiteExplorer = exp;
}
var editor = content as IEditorViewContent;
if (editor != null)
{
this.ActiveEditor = editor;
}
}
private Dictionary _toolstrips;
private Dictionary _toolstripRegions;
///
/// Adds the toolbar.
///
/// The name.
/// The toolbar.
/// The region.
/// if set to true [can toggle visibility].
public void AddToolbar(string name, ToolStrip toolbar, ToolbarRegion region, bool canToggleVisibility)
{
_toolstrips.Add(name, toolbar);
_toolstripRegions.Add(name, region);
if (canToggleVisibility)
{
ToolStripMenuItem item = new ToolStripMenuItem();
item.Text = name;
item.Tag = name;
item.Checked = true;
item.CheckOnClick = true;
item.Click += delegate
{
SetToolbarVisibility(name, item.Checked);
};
ctxToolbar.Items.Add(item);
}
switch (region)
{
case ToolbarRegion.Top:
toolStripContainer.TopToolStripPanel.Controls.Add(toolbar);
break;
case ToolbarRegion.Bottom:
toolStripContainer.BottomToolStripPanel.Controls.Add(toolbar);
break;
case ToolbarRegion.Left:
toolStripContainer.LeftToolStripPanel.Controls.Add(toolbar);
break;
case ToolbarRegion.Right:
toolStripContainer.RightToolStripPanel.Controls.Add(toolbar);
break;
}
}
///
/// Sets the toolbar visibility.
///
/// Name of the toolbar.
/// if set to true [visible].
public void SetToolbarVisibility(string toolbarName, bool visible)
{
ToolStrip strip = GetToolbar(toolbarName);
if (strip != null)
{
ToolbarRegion region = _toolstripRegions[toolbarName];
if (visible)
{
switch (region)
{
case ToolbarRegion.Bottom:
toolStripContainer.BottomToolStripPanel.Controls.Add(strip);
break;
case ToolbarRegion.Left:
toolStripContainer.LeftToolStripPanel.Controls.Add(strip);
break;
case ToolbarRegion.Right:
toolStripContainer.RightToolStripPanel.Controls.Add(strip);
break;
case ToolbarRegion.Top:
toolStripContainer.TopToolStripPanel.Controls.Add(strip);
break;
}
}
else
{
switch (region)
{
case ToolbarRegion.Bottom:
toolStripContainer.BottomToolStripPanel.Controls.Remove(strip);
break;
case ToolbarRegion.Left:
toolStripContainer.LeftToolStripPanel.Controls.Remove(strip);
break;
case ToolbarRegion.Right:
toolStripContainer.RightToolStripPanel.Controls.Remove(strip);
break;
case ToolbarRegion.Top:
toolStripContainer.TopToolStripPanel.Controls.Remove(strip);
break;
}
}
}
}
///
/// Gets the toolbar.
///
/// The name.
///
public ToolStrip GetToolbar(string name)
{
if (_toolstrips.ContainsKey(name))
return _toolstrips[name];
return null;
}
///
/// Gets the toolbar names.
///
/// The toolbar names.
public ICollection ToolbarNames
{
get { return _toolstrips.Keys; }
}
///
/// Sets the status label.
///
/// The text.
public void SetStatusLabel(string text)
{
statusLabel.Text = text;
}
///
/// Sets the title.
///
/// The title.
public void SetTitle(string title)
{
this.Text = title;
}
///
/// Gets the active view in the document region
///
public IViewContent ActiveDocumentView
{
get { return contentPanel.ActiveDocumentView; }
}
private ISiteExplorer _siteExp;
///
/// Gets the active site explorer
///
public ISiteExplorer ActiveSiteExplorer
{
get { return _siteExp; }
internal set
{
var current = _siteExp;
_siteExp = value;
if (value == null && current != null)
{
current.Close();
CheckContainerStatus();
//TODO: When we do support multiple repos we'll need to find and activate the next open
//ISiteExplorer in the ZonedContainer and designate that as the ActiveSiteExplorer
}
}
}
public IEditorViewContent ActiveEditor
{
get;
private set;
}
///
/// Shows the content.
///
/// The vc.
internal void ShowContent(IViewContent vc)
{
switch (vc.DefaultRegion)
{
case ViewRegion.Bottom:
case ViewRegion.Left:
case ViewRegion.Right:
case ViewRegion.Document:
contentPanel.AddContent(vc);
var vcb = vc as ViewContentBase;
if (vcb != null)
vcb.IsAttached = true;
break;
case ViewRegion.Floating:
throw new NotImplementedException();
case ViewRegion.Dialog:
throw new NotImplementedException();
}
}
internal void CheckContainerStatus()
{
contentPanel.CheckContainerStatus();
}
void OnApplicationIdle(object sender, EventArgs e)
{
// Use the Idle event to update the status of menu and toolbar.
// Depending on your application and the number of menu items with complex conditions,
// you might want to update the status less frequently.
UpdateMenuItemStatus();
}
/// Update Enabled/Visible state of items in the main menu based on conditions
void UpdateMenuItemStatus()
{
foreach (ToolStripItem item in menu.Items)
{
if (item is IStatusUpdate)
(item as IStatusUpdate).UpdateStatus();
}
foreach (ToolStrip ts in _toolstrips.Values)
{
foreach (ToolStripItem item in ts.Items)
{
if (item is IStatusUpdate)
(item as IStatusUpdate).UpdateStatus();
}
}
}
}
///
/// Defines the valid regions a toolbar can reside on a workbench
///
public enum ToolbarRegion
{
///
/// On the top
///
Top,
///
/// On the left
///
Left,
///
/// On the right
///
Right,
///
/// On the bottom
///
Bottom
}
}