// mainform.cs: main program  
// --------------------------------------------------------
// June 20,2004 
// By Xiaotao Shu.


using System;
using System.Drawing;
using System.Drawing.Printing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
using System.Runtime.Remoting.Channels.Http;

using TrackingController;

namespace tools
{
	/// <summary>
	/// mainform the main form for the Tools with mainmenu, it is a container that show other forms in it. 
	/// </summary>
	public class mainform : System.Windows.Forms.Form
	{
		//declaration menu items
		private System.Windows.Forms.MainMenu mainMenu1;
		private System.Windows.Forms.MenuItem menuItemFile;
		private System.Windows.Forms.MenuItem menuItemNew;
		private System.Windows.Forms.MenuItem menuItemOpen;
		private System.Windows.Forms.MenuItem menuItemClose;
		private System.Windows.Forms.MenuItem menuItemSave;
		private System.Windows.Forms.MenuItem menuItemSaveAs;
		private System.Windows.Forms.MenuItem menuItem12;
		private System.Windows.Forms.MenuItem menuItemPrint;
		private System.Windows.Forms.MenuItem menuItemPreview;
		private System.Windows.Forms.MenuItem menuItemExit;
		private System.Windows.Forms.MenuItem menuItemView;
		private System.Windows.Forms.MenuItem menuItemToolbar;
		private System.Windows.Forms.MenuItem menuItemStatusbar;
		private System.Windows.Forms.MenuItem menuItemWindow;
		private System.Windows.Forms.MenuItem menuItemCascade;
		private System.Windows.Forms.MenuItem menuItemTile;
		private System.Windows.Forms.MenuItem menuItemHelp;
		private System.Windows.Forms.MenuItem menuItemHelpTopics;
		private System.Windows.Forms.MenuItem menuItem37;
		private System.Windows.Forms.MenuItem menuItemAbout;
		
		//declaration imagelist that storts some images for toolbar
		private System.Windows.Forms.ImageList imageList1;
		

		//declaration toolbar
		private System.Windows.Forms.ToolBar toolBar1;
		private System.Windows.Forms.ToolBarButton newButton;
		private System.Windows.Forms.ToolBarButton openButton;
		private System.Windows.Forms.ToolBarButton previewButton;
		private System.Windows.Forms.ToolBarButton printButton;
		private System.Windows.Forms.ToolBarButton helpButton;
		private System.Windows.Forms.ToolBarButton separator1;
		private System.Windows.Forms.MenuItem menuItem1;
		private System.Drawing.Printing.PrintDocument printDoc;
		private System.Windows.Forms.MenuItem menuItemStart;
		private System.Windows.Forms.MenuItem menuItemStop;
		private System.Windows.Forms.ToolBarButton separator2;
		private System.Windows.Forms.ToolBarButton startButton;
		private System.Windows.Forms.ToolBarButton stopButton;
		private System.Windows.Forms.MenuItem menuItem2;
		private System.Windows.Forms.MenuItem menuItemConnect;
		private System.ComponentModel.IContainer components;
		
		private Controller m_controller;                                  //a controller storing data
		private TrackeeManagerServant m_TrackeeManagerServant;  //TrackeeManagerServant object 
		private ObjRef m_TrackeeManagerRef;                     //reference of the TrackeeManagerServant object for remoting object lifetime 
		private IChannel m_channel;                             //for remoting object
 		//some paramter for display view
		public bool isFillObject = true;                        //whether or not fill the object, when draw the map
		public bool displayLabel = true;                        //whether or not display label, when draw the map
		public bool displaySymbol = true;                       //whether or not display symbol, when draw the map

		private System.Windows.Forms.ToolBarButton connectButton;
		private System.Windows.Forms.MenuItem menuItem3;
		private System.Windows.Forms.ToolBarButton separator3;
		private System.Windows.Forms.MenuItem menuItemZoomIn;
		private System.Windows.Forms.MenuItem menuItemZoomOut;
		public System.Windows.Forms.StatusBar statusBar1;
		public System.Windows.Forms.StatusBarPanel statusBarPanel1;
		public System.Windows.Forms.StatusBarPanel statusBarPanel2;
		public System.Windows.Forms.StatusBarPanel statusBarPanel3;
		public System.Windows.Forms.StatusBarPanel statusBarPanel4;
		public System.Windows.Forms.StatusBarPanel statusBarPanel5;
		private System.Windows.Forms.ToolBarButton zoomHomeButton;
		private System.Windows.Forms.MenuItem menuItemZoomHome;
		private System.Windows.Forms.MenuItem menuItem4;
		private System.Windows.Forms.ToolBarButton separater4;
		private System.Windows.Forms.ToolBarButton separater5;
		private System.Windows.Forms.MenuItem menuItemMapManager;
		public System.Windows.Forms.StatusBarPanel statusBarPanel6;
		private System.Windows.Forms.MenuItem menuItem5;
		private System.Windows.Forms.MenuItem menuItemConfigTracking;
		private System.Windows.Forms.ToolBarButton zoomOutButton;
		private System.Windows.Forms.ToolBarButton zoomInButton;
		private System.Windows.Forms.MenuItem menuItem7;
		private System.Windows.Forms.MenuItem menuItemOpenImage;
		private System.Windows.Forms.MenuItem menuItemFill;
		private System.Windows.Forms.MenuItem menuItemLabel;
		private System.Windows.Forms.MenuItem menuItemSymbol;
		private System.Windows.Forms.ToolBarButton separater6; 
		

		//mainform constructor
		public mainform()
		{
			//intialize the main form 
			InitializeComponent();
			// printDoc
			printDoc.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(PrintPage);
	
			//create a view
			MapView aView =new MapView(this);
			//initalize the m_controller of the mainform 
			this.Controller = new Controller();
			//set the controller into view
			aView.Controller = this.Controller;
			//add view to controller
			this.Controller.Views.Add(aView);
			//display this view
			aView.Show();

			//initalize the m_TrackeeManagerServant for remoting
			m_TrackeeManagerServant = new TrackeeManagerServant(this.Controller);
			m_channel = null;

			//initialize the current working directory
			this.Controller.WorkDir = Environment.CurrentDirectory;
		}

		//declaring Controller property
		public Controller Controller
		{
			set	{	m_controller = value;	}
			get	{	return m_controller;		}
		}
		//declaring TrackeeManagerRef property
		public IChannel Channel 
		{
			set	{	m_channel = value;	}
			get	{	return m_channel;	}
		}
		//declaring TrackeeManagerServant property
		public TrackeeManagerServant TrackeeManagerServant 
		{
			set	{	m_TrackeeManagerServant = value;	}
			get	{	return m_TrackeeManagerServant;		}
		}


		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if (components != null) 
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.components = new System.ComponentModel.Container();
			System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(mainform));
			this.mainMenu1 = new System.Windows.Forms.MainMenu();
			this.menuItemFile = new System.Windows.Forms.MenuItem();
			this.menuItemNew = new System.Windows.Forms.MenuItem();
			this.menuItemOpenImage = new System.Windows.Forms.MenuItem();
			this.menuItemOpen = new System.Windows.Forms.MenuItem();
			this.menuItemClose = new System.Windows.Forms.MenuItem();
			this.menuItemSave = new System.Windows.Forms.MenuItem();
			this.menuItemSaveAs = new System.Windows.Forms.MenuItem();
			this.menuItem12 = new System.Windows.Forms.MenuItem();
			this.menuItemPrint = new System.Windows.Forms.MenuItem();
			this.menuItemPreview = new System.Windows.Forms.MenuItem();
			this.menuItemExit = new System.Windows.Forms.MenuItem();
			this.menuItem1 = new System.Windows.Forms.MenuItem();
			this.menuItemStart = new System.Windows.Forms.MenuItem();
			this.menuItemStop = new System.Windows.Forms.MenuItem();
			this.menuItem2 = new System.Windows.Forms.MenuItem();
			this.menuItemConnect = new System.Windows.Forms.MenuItem();
			this.menuItem5 = new System.Windows.Forms.MenuItem();
			this.menuItemConfigTracking = new System.Windows.Forms.MenuItem();
			this.menuItem4 = new System.Windows.Forms.MenuItem();
			this.menuItemMapManager = new System.Windows.Forms.MenuItem();
			this.menuItem7 = new System.Windows.Forms.MenuItem();
			this.menuItemFill = new System.Windows.Forms.MenuItem();
			this.menuItemLabel = new System.Windows.Forms.MenuItem();
			this.menuItemSymbol = new System.Windows.Forms.MenuItem();
			this.menuItemView = new System.Windows.Forms.MenuItem();
			this.menuItemToolbar = new System.Windows.Forms.MenuItem();
			this.menuItemStatusbar = new System.Windows.Forms.MenuItem();
			this.menuItem3 = new System.Windows.Forms.MenuItem();
			this.menuItemZoomHome = new System.Windows.Forms.MenuItem();
			this.menuItemZoomIn = new System.Windows.Forms.MenuItem();
			this.menuItemZoomOut = new System.Windows.Forms.MenuItem();
			this.menuItemWindow = new System.Windows.Forms.MenuItem();
			this.menuItemCascade = new System.Windows.Forms.MenuItem();
			this.menuItemTile = new System.Windows.Forms.MenuItem();
			this.menuItemHelp = new System.Windows.Forms.MenuItem();
			this.menuItemHelpTopics = new System.Windows.Forms.MenuItem();
			this.menuItem37 = new System.Windows.Forms.MenuItem();
			this.menuItemAbout = new System.Windows.Forms.MenuItem();
			this.imageList1 = new System.Windows.Forms.ImageList(this.components);
			this.toolBar1 = new System.Windows.Forms.ToolBar();
			this.newButton = new System.Windows.Forms.ToolBarButton();
			this.openButton = new System.Windows.Forms.ToolBarButton();
			this.previewButton = new System.Windows.Forms.ToolBarButton();
			this.printButton = new System.Windows.Forms.ToolBarButton();
			this.separator1 = new System.Windows.Forms.ToolBarButton();
			this.separater4 = new System.Windows.Forms.ToolBarButton();
			this.startButton = new System.Windows.Forms.ToolBarButton();
			this.stopButton = new System.Windows.Forms.ToolBarButton();
			this.connectButton = new System.Windows.Forms.ToolBarButton();
			this.separator2 = new System.Windows.Forms.ToolBarButton();
			this.separater5 = new System.Windows.Forms.ToolBarButton();
			this.zoomHomeButton = new System.Windows.Forms.ToolBarButton();
			this.zoomOutButton = new System.Windows.Forms.ToolBarButton();
			this.zoomInButton = new System.Windows.Forms.ToolBarButton();
			this.separator3 = new System.Windows.Forms.ToolBarButton();
			this.separater6 = new System.Windows.Forms.ToolBarButton();
			this.helpButton = new System.Windows.Forms.ToolBarButton();
			this.printDoc = new System.Drawing.Printing.PrintDocument();
			this.statusBar1 = new System.Windows.Forms.StatusBar();
			this.statusBarPanel1 = new System.Windows.Forms.StatusBarPanel();
			this.statusBarPanel2 = new System.Windows.Forms.StatusBarPanel();
			this.statusBarPanel3 = new System.Windows.Forms.StatusBarPanel();
			this.statusBarPanel4 = new System.Windows.Forms.StatusBarPanel();
			this.statusBarPanel5 = new System.Windows.Forms.StatusBarPanel();
			this.statusBarPanel6 = new System.Windows.Forms.StatusBarPanel();
			((System.ComponentModel.ISupportInitialize)(this.statusBarPanel1)).BeginInit();
			((System.ComponentModel.ISupportInitialize)(this.statusBarPanel2)).BeginInit();
			((System.ComponentModel.ISupportInitialize)(this.statusBarPanel3)).BeginInit();
			((System.ComponentModel.ISupportInitialize)(this.statusBarPanel4)).BeginInit();
			((System.ComponentModel.ISupportInitialize)(this.statusBarPanel5)).BeginInit();
			((System.ComponentModel.ISupportInitialize)(this.statusBarPanel6)).BeginInit();
			this.SuspendLayout();
			// 
			// mainMenu1
			// 
			this.mainMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
																					  this.menuItemFile,
																					  this.menuItem1,
																					  this.menuItem4,
																					  this.menuItemView,
																					  this.menuItemWindow,
																					  this.menuItemHelp});
			// 
			// menuItemFile
			// 
			this.menuItemFile.Index = 0;
			this.menuItemFile.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
																						 this.menuItemNew,
																						 this.menuItemOpenImage,
																						 this.menuItemOpen,
																						 this.menuItemClose,
																						 this.menuItemSave,
																						 this.menuItemSaveAs,
																						 this.menuItem12,
																						 this.menuItemPrint,
																						 this.menuItemPreview,
																						 this.menuItemExit});
			this.menuItemFile.Text = "File";
			// 
			// menuItemNew
			// 
			this.menuItemNew.Index = 0;
			this.menuItemNew.Text = "New View";
			this.menuItemNew.Click += new System.EventHandler(this.MenuItemHandler);
			// 
			// menuItemOpenImage
			// 
			this.menuItemOpenImage.Index = 1;
			this.menuItemOpenImage.Text = "Open Image From Map Source";
			this.menuItemOpenImage.Click += new System.EventHandler(this.MenuItemHandler);
			// 
			// menuItemOpen
			// 
			this.menuItemOpen.Index = 2;
			this.menuItemOpen.Text = "Open Image File";
			this.menuItemOpen.Click += new System.EventHandler(this.MenuItemHandler);
			// 
			// menuItemClose
			// 
			this.menuItemClose.Index = 3;
			this.menuItemClose.Text = "Close Actived View";
			this.menuItemClose.Click += new System.EventHandler(this.MenuItemHandler);
			// 
			// menuItemSave
			// 
			this.menuItemSave.Enabled = false;
			this.menuItemSave.Index = 4;
			this.menuItemSave.Text = "Save";
			this.menuItemSave.Click += new System.EventHandler(this.MenuItemHandler);
			// 
			// menuItemSaveAs
			// 
			this.menuItemSaveAs.Index = 5;
			this.menuItemSaveAs.Text = "Save As...";
			this.menuItemSaveAs.Click += new System.EventHandler(this.MenuItemHandler);
			// 
			// menuItem12
			// 
			this.menuItem12.Index = 6;
			this.menuItem12.Text = "-";
			// 
			// menuItemPrint
			// 
			this.menuItemPrint.Index = 7;
			this.menuItemPrint.Text = "Print...";
			this.menuItemPrint.Click += new System.EventHandler(this.MenuItemHandler);
			// 
			// menuItemPreview
			// 
			this.menuItemPreview.Index = 8;
			this.menuItemPreview.Text = "Print Preview";
			this.menuItemPreview.Click += new System.EventHandler(this.MenuItemHandler);
			// 
			// menuItemExit
			// 
			this.menuItemExit.Index = 9;
			this.menuItemExit.Text = "Exit";
			this.menuItemExit.Click += new System.EventHandler(this.MenuItemHandler);
			// 
			// menuItem1
			// 
			this.menuItem1.Index = 1;
			this.menuItem1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
																					  this.menuItemStart,
																					  this.menuItemStop,
																					  this.menuItem2,
																					  this.menuItemConnect,
																					  this.menuItem5,
																					  this.menuItemConfigTracking});
			this.menuItem1.Text = "System";
			// 
			// menuItemStart
			// 
			this.menuItemStart.Index = 0;
			this.menuItemStart.Text = "Start TrackingServer";
			this.menuItemStart.Click += new System.EventHandler(this.MenuItemHandler);
			// 
			// menuItemStop
			// 
			this.menuItemStop.Enabled = false;
			this.menuItemStop.Index = 1;
			this.menuItemStop.Text = "Stop TrackingServer";
			this.menuItemStop.Click += new System.EventHandler(this.MenuItemHandler);
			// 
			// menuItem2
			// 
			this.menuItem2.Index = 2;
			this.menuItem2.Text = "-";
			// 
			// menuItemConnect
			// 
			this.menuItemConnect.Index = 3;
			this.menuItemConnect.Text = "Set connect parameter";
			this.menuItemConnect.Click += new System.EventHandler(this.MenuItemHandler);
			// 
			// menuItem5
			// 
			this.menuItem5.Index = 4;
			this.menuItem5.Text = "-";
			// 
			// menuItemConfigTracking
			// 
			this.menuItemConfigTracking.Index = 5;
			this.menuItemConfigTracking.Text = "Config Tracking";
			this.menuItemConfigTracking.Click += new System.EventHandler(this.MenuItemHandler);
			// 
			// menuItem4
			// 
			this.menuItem4.Index = 2;
			this.menuItem4.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
																					  this.menuItemMapManager,
																					  this.menuItem7,
																					  this.menuItemFill,
																					  this.menuItemLabel,
																					  this.menuItemSymbol});
			this.menuItem4.Text = "Map";
			// 
			// menuItemMapManager
			// 
			this.menuItemMapManager.Index = 0;
			this.menuItemMapManager.Text = "Maintain map source";
			this.menuItemMapManager.Click += new System.EventHandler(this.MenuItemHandler);
			// 
			// menuItem7
			// 
			this.menuItem7.Index = 1;
			this.menuItem7.Text = "-";
			// 
			// menuItemFill
			// 
			this.menuItemFill.Checked = true;
			this.menuItemFill.Index = 2;
			this.menuItemFill.Text = "Fill Object";
			this.menuItemFill.Click += new System.EventHandler(this.menuItemFill_Click);
			// 
			// menuItemLabel
			// 
			this.menuItemLabel.Checked = true;
			this.menuItemLabel.Index = 3;
			this.menuItemLabel.Text = "Map Label";
			this.menuItemLabel.Click += new System.EventHandler(this.menuItemLabel_Click);
			// 
			// menuItemSymbol
			// 
			this.menuItemSymbol.Checked = true;
			this.menuItemSymbol.Index = 4;
			this.menuItemSymbol.Text = "Map Symbol";
			this.menuItemSymbol.Click += new System.EventHandler(this.menuItemSymbol_Click);
			// 
			// menuItemView
			// 
			this.menuItemView.Index = 3;
			this.menuItemView.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
																						 this.menuItemToolbar,
																						 this.menuItemStatusbar,
																						 this.menuItem3,
																						 this.menuItemZoomHome,
																						 this.menuItemZoomIn,
																						 this.menuItemZoomOut});
			this.menuItemView.Text = "View";
			// 
			// menuItemToolbar
			// 
			this.menuItemToolbar.Checked = true;
			this.menuItemToolbar.Index = 0;
			this.menuItemToolbar.Text = "Toolbar";
			this.menuItemToolbar.Click += new System.EventHandler(this.MenuItemHandler);
			// 
			// menuItemStatusbar
			// 
			this.menuItemStatusbar.Checked = true;
			this.menuItemStatusbar.Index = 1;
			this.menuItemStatusbar.Text = "Status Bar";
			this.menuItemStatusbar.Click += new System.EventHandler(this.MenuItemHandler);
			// 
			// menuItem3
			// 
			this.menuItem3.Index = 2;
			this.menuItem3.Text = "-";
			// 
			// menuItemZoomHome
			// 
			this.menuItemZoomHome.Index = 3;
			this.menuItemZoomHome.Text = "Zoom Home";
			this.menuItemZoomHome.Click += new System.EventHandler(this.MenuItemHandler);
			// 
			// menuItemZoomIn
			// 
			this.menuItemZoomIn.Index = 4;
			this.menuItemZoomIn.Text = "Zoom In";
			this.menuItemZoomIn.Click += new System.EventHandler(this.MenuItemHandler);
			// 
			// menuItemZoomOut
			// 
			this.menuItemZoomOut.Index = 5;
			this.menuItemZoomOut.Text = "Zoom Out";
			this.menuItemZoomOut.Click += new System.EventHandler(this.MenuItemHandler);
			// 
			// menuItemWindow
			// 
			this.menuItemWindow.Index = 4;
			this.menuItemWindow.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
																						   this.menuItemCascade,
																						   this.menuItemTile});
			this.menuItemWindow.Text = "Window";
			// 
			// menuItemCascade
			// 
			this.menuItemCascade.Index = 0;
			this.menuItemCascade.Text = "Cascade";
			this.menuItemCascade.Click += new System.EventHandler(this.MenuItemHandler);
			// 
			// menuItemTile
			// 
			this.menuItemTile.Index = 1;
			this.menuItemTile.Text = "Tile";
			this.menuItemTile.Click += new System.EventHandler(this.MenuItemHandler);
			// 
			// menuItemHelp
			// 
			this.menuItemHelp.Index = 5;
			this.menuItemHelp.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
																						 this.menuItemHelpTopics,
																						 this.menuItem37,
																						 this.menuItemAbout});
			this.menuItemHelp.Text = "Help";
			// 
			// menuItemHelpTopics
			// 
			this.menuItemHelpTopics.Index = 0;
			this.menuItemHelpTopics.Text = "Help Topics";
			this.menuItemHelpTopics.Click += new System.EventHandler(this.MenuItemHandler);
			// 
			// menuItem37
			// 
			this.menuItem37.Index = 1;
			this.menuItem37.Text = "-";
			// 
			// menuItemAbout
			// 
			this.menuItemAbout.Index = 2;
			this.menuItemAbout.Text = "About Tools";
			this.menuItemAbout.Click += new System.EventHandler(this.MenuItemHandler);
			// 
			// imageList1
			// 
			this.imageList1.ImageSize = new System.Drawing.Size(16, 16);
			this.imageList1.ImageStream = ((System.Windows.Forms.ImageListStreamer)(resources.GetObject("imageList1.ImageStream")));
			this.imageList1.TransparentColor = System.Drawing.Color.Transparent;
			// 
			// toolBar1
			// 
			this.toolBar1.Appearance = System.Windows.Forms.ToolBarAppearance.Flat;
			this.toolBar1.Buttons.AddRange(new System.Windows.Forms.ToolBarButton[] {
																						this.newButton,
																						this.openButton,
																						this.previewButton,
																						this.printButton,
																						this.separator1,
																						this.separater4,
																						this.startButton,
																						this.stopButton,
																						this.connectButton,
																						this.separator2,
																						this.separater5,
																						this.zoomHomeButton,
																						this.zoomOutButton,
																						this.zoomInButton,
																						this.separator3,
																						this.separater6,
																						this.helpButton});
			this.toolBar1.DropDownArrows = true;
			this.toolBar1.ImageList = this.imageList1;
			this.toolBar1.Location = new System.Drawing.Point(0, 0);
			this.toolBar1.Name = "toolBar1";
			this.toolBar1.ShowToolTips = true;
			this.toolBar1.Size = new System.Drawing.Size(616, 28);
			this.toolBar1.TabIndex = 2;
			this.toolBar1.ButtonClick += new System.Windows.Forms.ToolBarButtonClickEventHandler(this.toolBar1_ButtonClick);
			// 
			// newButton
			// 
			this.newButton.ImageIndex = 0;
			this.newButton.ToolTipText = "New a MapView";
			// 
			// openButton
			// 
			this.openButton.ImageIndex = 1;
			this.openButton.ToolTipText = "Open an Image ";
			// 
			// previewButton
			// 
			this.previewButton.ImageIndex = 3;
			this.previewButton.ToolTipText = "Print Preview";
			// 
			// printButton
			// 
			this.printButton.ImageIndex = 4;
			this.printButton.ToolTipText = "Print";
			// 
			// separator1
			// 
			this.separator1.Style = System.Windows.Forms.ToolBarButtonStyle.Separator;
			// 
			// separater4
			// 
			this.separater4.Style = System.Windows.Forms.ToolBarButtonStyle.Separator;
			// 
			// startButton
			// 
			this.startButton.ImageIndex = 6;
			this.startButton.ToolTipText = "Start the Trackee Server";
			// 
			// stopButton
			// 
			this.stopButton.ImageIndex = 7;
			this.stopButton.ToolTipText = "Stop the Trackee Server";
			this.stopButton.Visible = false;
			// 
			// connectButton
			// 
			this.connectButton.ImageIndex = 8;
			this.connectButton.ToolTipText = "set connection parameter";
			// 
			// separator2
			// 
			this.separator2.Style = System.Windows.Forms.ToolBarButtonStyle.Separator;
			// 
			// separater5
			// 
			this.separater5.Style = System.Windows.Forms.ToolBarButtonStyle.Separator;
			// 
			// zoomHomeButton
			// 
			this.zoomHomeButton.ImageIndex = 11;
			// 
			// zoomOutButton
			// 
			this.zoomOutButton.ImageIndex = 9;
			this.zoomOutButton.ToolTipText = "Zoom out";
			// 
			// zoomInButton
			// 
			this.zoomInButton.ImageIndex = 10;
			this.zoomInButton.ToolTipText = "Zoom in";
			// 
			// separator3
			// 
			this.separator3.Style = System.Windows.Forms.ToolBarButtonStyle.Separator;
			// 
			// separater6
			// 
			this.separater6.Style = System.Windows.Forms.ToolBarButtonStyle.Separator;
			// 
			// helpButton
			// 
			this.helpButton.ImageIndex = 5;
			this.helpButton.ToolTipText = "Help";
			// 
			// statusBar1
			// 
			this.statusBar1.Location = new System.Drawing.Point(0, 291);
			this.statusBar1.Name = "statusBar1";
			this.statusBar1.Panels.AddRange(new System.Windows.Forms.StatusBarPanel[] {
																						  this.statusBarPanel1,
																						  this.statusBarPanel2,
																						  this.statusBarPanel3,
																						  this.statusBarPanel4,
																						  this.statusBarPanel5,
																						  this.statusBarPanel6});
			this.statusBar1.ShowPanels = true;
			this.statusBar1.Size = new System.Drawing.Size(616, 22);
			this.statusBar1.TabIndex = 3;
			// 
			// statusBarPanel3
			// 
			this.statusBarPanel3.Width = 200;
			// 
			// statusBarPanel4
			// 
			this.statusBarPanel4.Width = 200;
			// 
			// statusBarPanel5
			// 
			this.statusBarPanel5.MinWidth = 100;
			this.statusBarPanel5.Width = 200;
			// 
			// statusBarPanel6
			// 
			this.statusBarPanel6.AutoSize = System.Windows.Forms.StatusBarPanelAutoSize.Spring;
			this.statusBarPanel6.MinWidth = 100;
			// 
			// mainform
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(616, 313);
			this.Controls.Add(this.statusBar1);
			this.Controls.Add(this.toolBar1);
			this.IsMdiContainer = true;
			this.Menu = this.mainMenu1;
			this.Name = "mainform";
			this.Text = "Tools";
			this.WindowState = System.Windows.Forms.FormWindowState.Maximized;
			((System.ComponentModel.ISupportInitialize)(this.statusBarPanel1)).EndInit();
			((System.ComponentModel.ISupportInitialize)(this.statusBarPanel2)).EndInit();
			((System.ComponentModel.ISupportInitialize)(this.statusBarPanel3)).EndInit();
			((System.ComponentModel.ISupportInitialize)(this.statusBarPanel4)).EndInit();
			((System.ComponentModel.ISupportInitialize)(this.statusBarPanel5)).EndInit();
			((System.ComponentModel.ISupportInitialize)(this.statusBarPanel6)).EndInit();
			this.ResumeLayout(false);

		}
		#endregion

		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		[STAThread]
		static void Main() 
		{
			Application.Run(new mainform());
		}

		
		//handle menu item events
		private void MenuItemHandler(object sender, System.EventArgs e)
		{
			if (sender == menuItemNew)	
			{
				New();
			}
			else if (sender == menuItemOpen)
			{
				Open();
			}
			else if (sender == menuItemOpenImage)
			{
				OpenImage();
			}
			else if (sender == menuItemPreview)
			{
				PrintPreview();
			}
			else if (sender == menuItemPrint)
			{
				Print();
			}
			else if (sender == menuItemAbout)
			{
				AboutHelp();
			}
			else if (sender == menuItemExit)
			{
				Exit();
			}
			else if (sender == menuItemClose)
			{
				CloseView();
			}
			else if (sender == menuItemStart)
			{
				StartServer();
			}
			else if (sender == menuItemStop)
			{
				StopServer();
			}
			else if (sender == menuItemConnect)
			{
				connectForm aConnectForm = new connectForm(this.Controller);
				aConnectForm.Show();
			}
			else if (sender == menuItemTile)
			{
				Tile();
			}
			else if (sender == menuItemCascade)
			{
				Cascade();
			}
			else if (sender == menuItemSaveAs)
			{
				SaveAs();
			}
			else if (sender == menuItemHelpTopics)
			{
				ShowHelpTopics();			
			}
			else if (sender == menuItemToolbar)
			{
				toolBar1.Visible  = menuItemToolbar.Checked  = !toolBar1.Visible ;
			}
			else if (sender == menuItemStatusbar)
			{
				statusBar1.Visible = menuItemStatusbar.Checked = !statusBar1.Visible;			
			}
			else if (sender == menuItemZoomHome)
			{
				ZoomHome();	
			}
			else if (sender == menuItemZoomIn)
			{
				ZoomIn();	
			}
			else if (sender == menuItemZoomOut)
			{
				ZoomOut();	
			}
			else if (sender == menuItemOpenImage)
			{
				OpenImage();
			}
			else if (sender == menuItemMapManager)
			{
				MapSourceManager();
			}
			else if (sender == menuItemConfigTracking)
			{
				trackerConfigForm aConfigTracking = new trackerConfigForm(this.Controller);
				aConfigTracking.ShowDialog();
			}
		}


		//handle toolbar button events
		private void toolBar1_ButtonClick(object sender, System.Windows.Forms.ToolBarButtonClickEventArgs e)
		{
			if (e.Button == newButton)	
			{
				New();
			}
			else if (e.Button==openButton)
			{
				OpenImage();
			}
			else if (e.Button==startButton)
			{
				StartServer();
			}
			else if (e.Button==stopButton)
			{
				StopServer();
			}
			else if (e.Button==connectButton)
			{
				connectForm aConnectForm = new connectForm(this.Controller);
				aConnectForm.Show();
			}
			else if (e.Button == previewButton)
			{
				PrintPreview();
			}
			else if (e.Button == printButton)
			{
				Print();
			}
			else if (e.Button == zoomHomeButton)
			{
				ZoomHome();
			}
			else if (e.Button == zoomInButton)
			{
				ZoomIn();
			}
			else if (e.Button == zoomOutButton)
			{
				ZoomOut();
			}
			else if (e.Button == helpButton)
			{
				ShowHelpTopics();
			}
		}


		/*********************************************************************
		 *                        Methods                                    *
		 *********************************************************************/
		//New(): open a new view with defualt image
		public void New()
		{
			//create a view
			MapView aView = new MapView(this);
			//add view to controller
			this.Controller.Views.Add(aView);
			//set the controller into the view
			aView.Controller = this.Controller;
			//display this view
			aView.Show();
		}
		
		//open an imagefile and put it to the actived mapview
		public void Open()
		{
			//get the actived MapView
			MapView activeView = (MapView)this.ActiveMdiChild;
			if (activeView == null)
			{
				MessageBox.Show(" No active map view!");
				return;
			}

			//open file dialog
			OpenFileDialog openFileDialog1 = new OpenFileDialog();
			openFileDialog1.Filter = "IMAGE files (*.map;*.bmp;*.jpg;*.gif;*.jpeg;*.jpe;)|*.map;*.bmp;*.jpg;*.jpeg;*.gif|All files (*.*)|*.*" ;
			openFileDialog1.FilterIndex = 1 ;
			openFileDialog1.RestoreDirectory = true ;
			if(openFileDialog1.ShowDialog() != DialogResult.OK)
			{
				return;
			}
			if (openFileDialog1.FileName == "")
			{
				MessageBox.Show("No Map loaded!");
				return;
			}
			//load image from the file 
			activeView.ImageFile = openFileDialog1.FileName;
			double top = 0.0;
			double left = 0.0;
			double right = 0.0;
			double bottom = 0.0;
			activeView.LoadImage(activeView.ImageFile,top,left,right,bottom);
			activeView.Invalidate();
		}


		public void SaveAs()
		{
			//get the actived MapView
			MapView activeView = (MapView)this.ActiveMdiChild;
			if (activeView == null)
			{
				MessageBox.Show(" No active map view!");
				return;
			}

			//save file dialog
			SaveFileDialog saveFileDialog1 = new SaveFileDialog();
			saveFileDialog1.Filter = "IMAGE files (*.bmp)|*.bmp|All files (*.*)|*.*" ;
			saveFileDialog1.FilterIndex = 1 ;
			saveFileDialog1.RestoreDirectory = true ;
			if(saveFileDialog1.ShowDialog() != DialogResult.OK)
			{
				return;
			}
			//save image to a bmp 
			try
			{
				string extentName = "";
				int index = saveFileDialog1.FileName.IndexOf(".");
				if (index>0)
					extentName = saveFileDialog1.FileName.Substring(index+1);
				activeView.SaveImage(saveFileDialog1.FileName,extentName);
			}
			catch (Exception e)
			{
				MessageBox.Show(e.ToString());
			}
		}

		//start the server
		public bool StartServer()
		{
			bool result = true;
			try
			{
				// create and publish the TrackeeManager remoting object
				// its lifetime is controled by the server side.
				if (this.TrackeeManagerServant == null)
				{
					//create a TrackeeManagerServant
					this.TrackeeManagerServant = new TrackeeManagerServant(this.Controller);
				}
	   
				if (this.Channel == null)
				{
					if (this.Controller.ChannelType == "TCP")
					{
						//create a Tcp channel 
						this.Channel = new TcpServerChannel(this.Controller.Port);
					}
					else
					{
						//create a Http channel 
						this.Channel = new HttpServerChannel(this.Controller.Port);
					}

					//register the Tcp/Http channel
					ChannelServices.RegisterChannel(this.Channel);
				}
			


				//TrackeeManager is publish as remoting ob
				RemotingServices.Marshal(this.TrackeeManagerServant,"TrackeeManager");

				//publish the existing trackers
				/*for (int i=0; i< Controller.Trackees.Count;i++)
				{
					string trackeeIdentity = ((TrackeeServant)Controller.Trackees[i]).ID;
    				//generate the reference of the remoting object.
					ObjRef ref1 = RemotingServices.Marshal((TrackeeServant)Controller.Trackees[i],trackeeIdentity);
				}*/

				//enable or disable menuitems and toolbar's buttons
				itemEnabled(true);
			}
				// Handle exception
			catch(Exception e ) 
			{
				result = false;
				MessageBox.Show(e.ToString());
			}	
			return result;
		}

		//stop the server
		public bool StopServer()
		{
			///StopServer method: unregister the TrackeeManager remoting object,
			///but the TrackeeManagerServant object is still exists.
			///The server could reregister the channel and republish the object again.
			
			bool result = true;
			try
			{
				this.m_TrackeeManagerRef = null; //release the Marshal-by-reference objects (MBRs)
				//unregisters the object from publication 
				RemotingServices.Disconnect(this.TrackeeManagerServant);

				for (int i=0; i< this.Controller.Trackees.Count;i++)
				{
					//unregisters the trackee objects from publication 
					RemotingServices.Disconnect((TrackeeServant)this.Controller.Trackees[i]);
				}
				
				//unregisters the channel
				ChannelServices.UnregisterChannel(this.Channel);
				this.Channel = null;    
				
				//enable, disenable menuitems
				itemEnabled(false);
			}
			catch(Exception e)
			{
				MessageBox.Show(e.ToString());
				result = false;
			}
			return result;
		}

		//About Help
		private void AboutHelp()
		{
			AboutForm about = new AboutForm();
			about.ShowDialog();
		}

		//Close the View
		private  void CloseView()
		{
			//get the actived map view
			MapView activeView = (MapView)this.ActiveMdiChild;
			if (activeView == null)
			{
				MessageBox.Show(" No active map view!");
				return;
			}

			//close the view
			activeView.Close();		
		}
		
		//Help Topics
		private void ShowHelpTopics()
		{	
			//Help.ShowHelp(this,"TrcakerTools.HLP");
		}
	
		//Print
		private void Print()
		{
			try 
			{
				printDoc.Print();
			} 
			catch(Exception e)
			{
				MessageBox.Show(e.ToString());
			}
		}

		//PrintPreview
		private void PrintPreview()
		{
			try
			{		
				PrintPreviewDialog prevDlg = new PrintPreviewDialog();
				prevDlg.Document = printDoc;
				prevDlg.Size = new System.Drawing.Size(600, 329);
				prevDlg.ShowDialog();
			}
			catch(Exception ex)
			{
				MessageBox.Show(ex.ToString());
			}
		
		}
		//Exit application
		private void Exit()
		{
			Form[] childForm = this.MdiChildren ;
			//close all child forms of the app
			for(int i=0; i < childForm.Length ; i++)
				childForm[i].Close();
			Application.Exit();
		}
		
		//Tile
		private void Tile()
		{
			this.LayoutMdi(MdiLayout.TileVertical);
		}
		//Cascade
		private void Cascade()
		{
			this.LayoutMdi(MdiLayout.Cascade);
		}

		//Zoom Home
		private  void ZoomHome()
		{
			//get the actived map view
			MapView activeView = (MapView)this.ActiveMdiChild;
			if (activeView == null)
			{
				return;
			}
			activeView.ZoomHome();		
		}

		//Zoom In
		private  void ZoomIn()
		{
			//get the actived map view
			MapView activeView = (MapView)this.ActiveMdiChild;
			if (activeView == null)
			{
				return;
			}
			activeView.ZoomIn();		
		}

		//Zoom Out
		private  void ZoomOut()
		{
			//get the actived map view
			MapView activeView = (MapView)this.ActiveMdiChild;
			if (activeView == null)
			{
				return;
			}
			activeView.ZoomOut();		
		}
		
		
		//addtional methods
		private void itemEnabled(bool isStart)
		{
			//enable or disable menuitems and toolbar's buttons
			this.menuItemStop.Enabled = isStart;
			this.menuItemStart.Enabled = (isStart==false);
			this.stopButton.Visible = isStart;
			this.startButton.Visible = (isStart==false);
		}


		//load a map from the map source.
		private void OpenImage()
		{
			//get the actived MapView
			MapView activeView = (MapView)this.ActiveMdiChild;
			if (activeView == null)
			{
				MessageBox.Show(" No active map view!");
				return;
			}

			MapListForm aMapList =  new MapListForm ();
			aMapList.WorkDir = this.Controller.WorkDir;
			if(aMapList.ShowDialog() != DialogResult.OK)
			{
				return;
			}
			//load image from the file 
			activeView.LoadImage(aMapList.FileName,aMapList.MapTop,aMapList.MapLeft,aMapList.MapRight,aMapList.MapBottom);
			activeView.Text = aMapList.Title; 
			activeView.Invalidate();
		}

		//set the map source
		private void MapSourceManager()
		{
			MapSourceForm aMapSource = new MapSourceForm();
			aMapSource.WorkDir = this.Controller.WorkDir;
			aMapSource.ShowDialog();
		}

		private void menuItemFill_Click(object sender, System.EventArgs e)
		{
			bool bFilled = !menuItemFill.Checked;
			menuItemFill.Checked = bFilled;
			isFillObject = menuItemFill.Checked;
			
			//get the actived MapView
			MapView activeView = (MapView)this.ActiveMdiChild;
			if (activeView != null)
			{
				//re-generate the bitmap from vector map
				activeView.setBackground();
				activeView.Refresh();
			}
		}

		private void menuItemLabel_Click(object sender, System.EventArgs e)
		{
			bool bDispalyLabel = !menuItemLabel.Checked;
			menuItemLabel.Checked = bDispalyLabel;
			displayLabel = menuItemLabel.Checked;
			
			//get the actived MapView
			MapView activeView = (MapView)this.ActiveMdiChild;
			if (activeView != null)
			{
				//re-generate the bitmap from vector map
				activeView.setBackground();
				activeView.Refresh();
			}
		}

		private void menuItemSymbol_Click(object sender, System.EventArgs e)
		{
			bool bDispalySymbol = !menuItemSymbol.Checked;
			menuItemSymbol.Checked = bDispalySymbol;
			displaySymbol = menuItemSymbol.Checked;
			
			//get the actived MapView
			MapView activeView = (MapView)this.ActiveMdiChild;
			if (activeView != null)
			{
				//re-generate the bitmap from vector map
				activeView.setBackground();
				activeView.Refresh();
			}
		}

		//PrintPage event handler
		private void PrintPage(object sender,PrintPageEventArgs ev)
		{
			try
			{
				//get the actived MapView
				MapView activeView = (MapView)this.ActiveMdiChild;
				if (activeView != null)
				{
					float scale = (float)activeView.ClientSize.Height/(float)activeView.ClientSize.Width;
					//draw the image to printer
					ev.Graphics.DrawImage(activeView.Image,0,0,ev.PageBounds.Width,ev.PageBounds.Width*scale);
				}
				ev.HasMorePages = false;
			}
			catch (Exception ex)
			{
				MessageBox.Show(ex.ToString());
			}
		}
	}
}
