using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.IO;
using System.Runtime.InteropServices;
using ESRI.ArcGIS.esriSystem;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Controls;
using ESRI.ArcGIS.SystemUI;
namespace Final
{
public class ControlsSynchronizer
{
#region class members
private IMapControl3 m_mapControl = null;
private IPageLayoutControl2 m_pageLayoutControl = null;
private ITool m_mapActiveTool = null;
private ITool m_pageLayoutActiveTool = null;
private bool m_IsMapCtrlactive = true;
private ArrayList m_frameworkControls = null;
#endregion
#region constructor
public ControlsSynchronizer()
{
m_frameworkControls = new ArrayList();
}
public ControlsSynchronizer(IMapControl3 mapControl, IPageLayoutControl2 pageLayeroutControl)
: this()
{
m_mapControl = mapControl;
m_pageLayoutControl = pageLayeroutControl;
}
#endregion
#region properties
public IMapControl3 MapControl
{
get { return m_mapControl; }
set { m_mapControl = value; }
}
public IPageLayoutControl2 PageLayoutControl
{
get { return m_pageLayoutControl; }
set { m_pageLayoutControl = value; }
}
public string ActiveViewType
{
get
{
if (m_IsMapCtrlactive)
return "MapControl";
else
return "PageLayoutControl";
}
}
public object ActiveControl
{
get
{
if (m_mapControl == null || m_pageLayoutControl == null)
throw new Exception("ControlsSynchronizer::ActiveControl:\r\nEither MapControl or PageLayoutControl are not initialized!");
if (m_IsMapCtrlactive)
return m_mapControl.Object;
else
return m_pageLayoutControl.Object;
}
}
#endregion
#region Methods
public void ActivateMap()
{
try
{
if (m_pageLayoutControl == null || m_mapControl == null)
throw new Exception("ControlsSynchronizer::ActivateMap:\r\nEither MapControl or PageLayoutControl are not initialized!");
if (m_pageLayoutControl.CurrentTool != null)
m_pageLayoutActiveTool = m_pageLayoutControl.CurrentTool;
m_pageLayoutControl.ActiveView.Deactivate();
m_mapControl.ActiveView.Activate(m_mapControl.hWnd);
if (m_mapActiveTool != null) m_mapControl.CurrentTool = m_mapActiveTool;
m_IsMapCtrlactive = true;
this.SetBuddies(m_mapControl.Object);
}
catch (Exception ex)
{
throw new Exception(string.Format("ControlsSynchronizer::ActiveMap:\r\n{0}", ex.Message));
}
}
public void ActivatePageLayout()
{
try
{
if (m_pageLayoutControl == null || m_mapControl == null)
throw new Exception("ControlsSynchronizer::ActivatePageLayout:\r\nEither Map Control or PageLayoutControl are not initialized!");
if (m_mapControl.CurrentTool != null) m_mapActiveTool = m_mapControl.CurrentTool;
m_mapControl.ActiveView.Deactivate();
m_pageLayoutControl.ActiveView.Activate (m_pageLayoutControl.hWnd);
if (m_pageLayoutActiveTool != null) m_pageLayoutControl.CurrentTool = m_pageLayoutActiveTool;
m_IsMapCtrlactive = false;
this.SetBuddies(m_pageLayoutControl.Object);
}
catch (Exception ex)
{
throw new Exception(string.Format("ControlsSynchronizer::ActivatePageLayout:\r\n{0}", ex.Message));
}
}
public void ReplaceMap(IMap newMap)
{
if (newMap == null)
throw new Exception("ControlsSynchronizer::ReplaceMap:\r\nNew map for replacement is not initialized!");
if (m_pageLayoutControl == null || m_mapControl == null)
throw new Exception("ControlsSynchronizer::ReplaceMap:\r\nEither MapControl or PageLayoutControl are not initialized!");
IMaps maps = new Maps();
maps.Add(newMap);
bool bIsMapActive = m_IsMapCtrlactive;
this.ActivatePageLayout();
m_pageLayoutControl.PageLayout.ReplaceMaps(maps);
m_mapControl.Map = newMap;
m_pageLayoutActiveTool = null;
m_mapActiveTool = null;
if (bIsMapActive)
{
this.ActivateMap();
m_mapControl.ActiveView.Refresh();
}
else
{
this.ActivatePageLayout();
m_pageLayoutControl.ActiveView.Refresh();
}
}
public void BindControls(IMapControl3 mapControl, IPageLayoutControl2 pageLayoutControl, bool activateMapFirst)
{
if (mapControl == null || pageLayoutControl == null)
throw new Exception("ControlsSynchronizer::BindControls:\r\nEither MapControl or PageLayoutControl are not initialized!");
m_mapControl = MapControl;
m_pageLayoutControl = pageLayoutControl;
this.BindControls(activateMapFirst);
}
public void BindControls(bool activateMapFirst)
{
if (m_pageLayoutControl == null || m_mapControl == null)
throw new Exception("ControlsSynchronizer::BindControls:\r\nEither MapControl or PageLayoutControl are not initialized!");
IMap newMap = new MapClass();
newMap.Name = "Map";
IMaps maps = new Maps();
maps.Add(newMap);
m_pageLayoutControl.PageLayout.ReplaceMaps(maps);
m_mapControl.Map = newMap;
m_pageLayoutActiveTool = null;
m_mapActiveTool = null;
if (activateMapFirst)
this.ActivateMap();
else
this.ActivatePageLayout();
}
public void AddFrameworkControl(object control)
{
if (control == null)
throw new Exception("ControlsSynchronizer::AddFrameworkControl:\r\nAdded control is not initialized!");
m_frameworkControls.Add(control);
}
public void RemoveFrameworkControl(object control)
{
if(control == null )
throw new Exception("ControlsSynchronizer::RemoveFrameworkControl:\r\nControl to be removed is not initialized!");
m_frameworkControls.Remove(control);
}
public void RemoveFrameworkControlAt(int index)
{
if (m_frameworkControls.Count < index)
throw new Exception("ControlsSynchronizer::RemoveFrameworkControlAt:\r\nIndex is out of range!");
m_frameworkControls.RemoveAt(index);
}
private void SetBuddies(object buddy)
{
try
{
if (buddy == null)
throw new Exception("ControlsSynchronizer::SetBuddies:\r\nTarget Buddy Control is not initialized!");
foreach (object obj in m_frameworkControls)
{
if (obj is IToolbarControl)
{
((IToolbarControl)obj).SetBuddyControl(buddy);
}
else if (obj is ITOCControl)
{
((ITOCControl)obj).SetBuddyControl(buddy);
}
}
}
catch (Exception ex)
{
throw new Exception(string.Format("ControlsSynchronizer::SetBuddies:\r\n{0}", ex.Message));
}
}
#endregion
}
}