using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.IO;

using Icarus.Graphics.Animation;
using Icarus.Graphics.Animation.GDI;

namespace FramesetViewer
{
	/// <summary>
	/// Summary description for FrameView.
	/// </summary>
	public class FrameView : System.Windows.Forms.Form
	{
		private System.Windows.Forms.MainMenu mainMenu1;
		private System.Windows.Forms.MenuItem menuItem1;
		private System.Windows.Forms.MenuItem menuItem2;
		private System.Windows.Forms.MenuItem menuItem3;
		private System.Windows.Forms.MenuItem menuItem4;
		private System.Windows.Forms.MenuItem menuItem5;
		private System.Windows.Forms.MenuItem menuItem6;
		private System.Windows.Forms.OpenFileDialog dlg_OpenImage;
		private System.Windows.Forms.MenuItem menuItem7;
		private System.Windows.Forms.MenuItem item_ShowGuides;
		private System.Windows.Forms.Panel pnl_Frames;
		private System.Windows.Forms.Panel pnl_FrameView;
		private FramesetViewer.FramePanel m_FramePanel;
		private System.Windows.Forms.ListBox lst_FrameList;
		private System.Windows.Forms.GroupBox grp_CurrentFrame;
		private System.Windows.Forms.GroupBox grp_FrameList;
		private System.Windows.Forms.Panel pnl_Contents;
		private System.Windows.Forms.Panel panel1;
		private System.Windows.Forms.Panel pnl_FrameViewTools;
		private System.Windows.Forms.ToolBar tlb_FrameView;
		private System.Windows.Forms.Panel panel2;
		private System.Windows.Forms.ToolBar tlb_Frames;
		private System.Windows.Forms.Panel panel3;
		private System.Windows.Forms.ImageList iml_Frames;
		private System.Windows.Forms.ToolBarButton tbb_AddFrame;
		private System.Windows.Forms.MenuItem menuItem8;
		private System.Windows.Forms.MenuItem menuItem9;
		private System.Windows.Forms.MenuItem menuItem10;
		private System.Windows.Forms.ToolBarButton tbb_FrameUp;
		private System.Windows.Forms.ToolBarButton tbb_FrameDown;
		private System.Windows.Forms.ToolBarButton tbb_DeleteFrame;
		private System.Windows.Forms.ToolBarButton tbb_ApplyAnchor;
		private System.Windows.Forms.ToolBarButton tbb_PreviousFrame;
		private System.Windows.Forms.ToolBarButton tbb_NextFrame;
		private System.Windows.Forms.OpenFileDialog dlg_OpenFrameDialog;
		private System.Windows.Forms.SaveFileDialog dlg_SaveFrameset;
		private System.Windows.Forms.MenuItem item_SetBackground;
		private System.ComponentModel.IContainer components;

		public FrameView()
		{
			InitializeComponent();
			m_FramePanel.ShowGuides = item_ShowGuides.Checked;
		}

		/// <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(FrameView));
			this.mainMenu1 = new System.Windows.Forms.MainMenu();
			this.menuItem1 = new System.Windows.Forms.MenuItem();
			this.menuItem2 = new System.Windows.Forms.MenuItem();
			this.menuItem3 = new System.Windows.Forms.MenuItem();
			this.menuItem4 = new System.Windows.Forms.MenuItem();
			this.menuItem8 = new System.Windows.Forms.MenuItem();
			this.menuItem9 = new System.Windows.Forms.MenuItem();
			this.menuItem10 = new System.Windows.Forms.MenuItem();
			this.menuItem5 = new System.Windows.Forms.MenuItem();
			this.menuItem6 = new System.Windows.Forms.MenuItem();
			this.menuItem7 = new System.Windows.Forms.MenuItem();
			this.item_ShowGuides = new System.Windows.Forms.MenuItem();
			this.dlg_OpenImage = new System.Windows.Forms.OpenFileDialog();
			this.pnl_Frames = new System.Windows.Forms.Panel();
			this.panel3 = new System.Windows.Forms.Panel();
			this.grp_FrameList = new System.Windows.Forms.GroupBox();
			this.lst_FrameList = new System.Windows.Forms.ListBox();
			this.panel2 = new System.Windows.Forms.Panel();
			this.tlb_Frames = new System.Windows.Forms.ToolBar();
			this.tbb_AddFrame = new System.Windows.Forms.ToolBarButton();
			this.tbb_FrameUp = new System.Windows.Forms.ToolBarButton();
			this.tbb_FrameDown = new System.Windows.Forms.ToolBarButton();
			this.tbb_DeleteFrame = new System.Windows.Forms.ToolBarButton();
			this.tbb_ApplyAnchor = new System.Windows.Forms.ToolBarButton();
			this.iml_Frames = new System.Windows.Forms.ImageList(this.components);
			this.pnl_FrameView = new System.Windows.Forms.Panel();
			this.panel1 = new System.Windows.Forms.Panel();
			this.grp_CurrentFrame = new System.Windows.Forms.GroupBox();
			this.m_FramePanel = new FramesetViewer.FramePanel();
			this.pnl_FrameViewTools = new System.Windows.Forms.Panel();
			this.tlb_FrameView = new System.Windows.Forms.ToolBar();
			this.tbb_PreviousFrame = new System.Windows.Forms.ToolBarButton();
			this.tbb_NextFrame = new System.Windows.Forms.ToolBarButton();
			this.pnl_Contents = new System.Windows.Forms.Panel();
			this.dlg_OpenFrameDialog = new System.Windows.Forms.OpenFileDialog();
			this.dlg_SaveFrameset = new System.Windows.Forms.SaveFileDialog();
			this.item_SetBackground = new System.Windows.Forms.MenuItem();
			this.pnl_Frames.SuspendLayout();
			this.panel3.SuspendLayout();
			this.grp_FrameList.SuspendLayout();
			this.panel2.SuspendLayout();
			this.pnl_FrameView.SuspendLayout();
			this.panel1.SuspendLayout();
			this.grp_CurrentFrame.SuspendLayout();
			this.pnl_FrameViewTools.SuspendLayout();
			this.pnl_Contents.SuspendLayout();
			this.SuspendLayout();
			// 
			// mainMenu1
			// 
			this.mainMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
																					  this.menuItem1,
																					  this.menuItem7});
			// 
			// menuItem1
			// 
			this.menuItem1.Index = 0;
			this.menuItem1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
																					  this.menuItem2,
																					  this.menuItem8,
																					  this.menuItem5,
																					  this.menuItem6});
			this.menuItem1.Text = "File";
			// 
			// menuItem2
			// 
			this.menuItem2.Index = 0;
			this.menuItem2.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
																					  this.menuItem3,
																					  this.menuItem4});
			this.menuItem2.Text = "Open";
			// 
			// menuItem3
			// 
			this.menuItem3.Index = 0;
			this.menuItem3.Text = "Simple Frameset";
			this.menuItem3.Click += new System.EventHandler(this.menuItem3_Click);
			// 
			// menuItem4
			// 
			this.menuItem4.Index = 1;
			this.menuItem4.Text = "Pazera Frameset";
			this.menuItem4.Click += new System.EventHandler(this.menuItem4_Click);
			// 
			// menuItem8
			// 
			this.menuItem8.Index = 1;
			this.menuItem8.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
																					  this.menuItem9,
																					  this.menuItem10});
			this.menuItem8.Text = "Save";
			// 
			// menuItem9
			// 
			this.menuItem9.Enabled = false;
			this.menuItem9.Index = 0;
			this.menuItem9.Text = "As Simple Frameset";
			// 
			// menuItem10
			// 
			this.menuItem10.Index = 1;
			this.menuItem10.Text = "As Pazera Frameset";
			this.menuItem10.Click += new System.EventHandler(this.menuItem10_Click);
			// 
			// menuItem5
			// 
			this.menuItem5.Index = 2;
			this.menuItem5.Text = "-";
			// 
			// menuItem6
			// 
			this.menuItem6.Index = 3;
			this.menuItem6.Text = "Exit";
			this.menuItem6.Click += new System.EventHandler(this.menuItem6_Click);
			// 
			// menuItem7
			// 
			this.menuItem7.Index = 1;
			this.menuItem7.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
																					  this.item_SetBackground,
																					  this.item_ShowGuides});
			this.menuItem7.Text = "View";
			// 
			// item_ShowGuides
			// 
			this.item_ShowGuides.Checked = true;
			this.item_ShowGuides.Index = 1;
			this.item_ShowGuides.Text = "Show Guides";
			this.item_ShowGuides.Click += new System.EventHandler(this.item_ShowGuides_Click);
			// 
			// dlg_OpenImage
			// 
			this.dlg_OpenImage.Filter = "All Supported Types|*.gif;*.jpg;*.jpeg;*.png|GIF Files (*.gif)|*.gif|JPEG Files (" +
				"*.jpg; *.jpeg)|*.jpg;*.jpeg|PNG Files (*.png)|*.png";
			// 
			// pnl_Frames
			// 
			this.pnl_Frames.Controls.Add(this.panel3);
			this.pnl_Frames.Controls.Add(this.panel2);
			this.pnl_Frames.Dock = System.Windows.Forms.DockStyle.Left;
			this.pnl_Frames.Location = new System.Drawing.Point(0, 0);
			this.pnl_Frames.Name = "pnl_Frames";
			this.pnl_Frames.Size = new System.Drawing.Size(200, 421);
			this.pnl_Frames.TabIndex = 2;
			// 
			// panel3
			// 
			this.panel3.Controls.Add(this.grp_FrameList);
			this.panel3.Dock = System.Windows.Forms.DockStyle.Fill;
			this.panel3.Location = new System.Drawing.Point(0, 24);
			this.panel3.Name = "panel3";
			this.panel3.Size = new System.Drawing.Size(200, 397);
			this.panel3.TabIndex = 5;
			// 
			// grp_FrameList
			// 
			this.grp_FrameList.Controls.Add(this.lst_FrameList);
			this.grp_FrameList.Dock = System.Windows.Forms.DockStyle.Fill;
			this.grp_FrameList.Location = new System.Drawing.Point(0, 0);
			this.grp_FrameList.Name = "grp_FrameList";
			this.grp_FrameList.Size = new System.Drawing.Size(200, 397);
			this.grp_FrameList.TabIndex = 3;
			this.grp_FrameList.TabStop = false;
			this.grp_FrameList.Text = "Frames:";
			// 
			// lst_FrameList
			// 
			this.lst_FrameList.Dock = System.Windows.Forms.DockStyle.Fill;
			this.lst_FrameList.Location = new System.Drawing.Point(3, 16);
			this.lst_FrameList.Name = "lst_FrameList";
			this.lst_FrameList.Size = new System.Drawing.Size(194, 368);
			this.lst_FrameList.TabIndex = 2;
			this.lst_FrameList.SelectedIndexChanged += new System.EventHandler(this.lst_FrameList_SelectedIndexChanged);
			// 
			// panel2
			// 
			this.panel2.Controls.Add(this.tlb_Frames);
			this.panel2.Dock = System.Windows.Forms.DockStyle.Top;
			this.panel2.Location = new System.Drawing.Point(0, 0);
			this.panel2.Name = "panel2";
			this.panel2.Size = new System.Drawing.Size(200, 24);
			this.panel2.TabIndex = 4;
			// 
			// tlb_Frames
			// 
			this.tlb_Frames.Appearance = System.Windows.Forms.ToolBarAppearance.Flat;
			this.tlb_Frames.Buttons.AddRange(new System.Windows.Forms.ToolBarButton[] {
																						  this.tbb_AddFrame,
																						  this.tbb_FrameUp,
																						  this.tbb_FrameDown,
																						  this.tbb_DeleteFrame,
																						  this.tbb_ApplyAnchor});
			this.tlb_Frames.DropDownArrows = true;
			this.tlb_Frames.ImageList = this.iml_Frames;
			this.tlb_Frames.Location = new System.Drawing.Point(0, 0);
			this.tlb_Frames.Name = "tlb_Frames";
			this.tlb_Frames.ShowToolTips = true;
			this.tlb_Frames.Size = new System.Drawing.Size(200, 28);
			this.tlb_Frames.TabIndex = 0;
			this.tlb_Frames.ButtonClick += new System.Windows.Forms.ToolBarButtonClickEventHandler(this.tlb_Frames_ButtonClick);
			// 
			// tbb_AddFrame
			// 
			this.tbb_AddFrame.ImageIndex = 0;
			this.tbb_AddFrame.ToolTipText = "Add a Frame";
			// 
			// tbb_FrameUp
			// 
			this.tbb_FrameUp.Enabled = false;
			this.tbb_FrameUp.ImageIndex = 1;
			this.tbb_FrameUp.ToolTipText = "Move Selected Frame Up";
			// 
			// tbb_FrameDown
			// 
			this.tbb_FrameDown.Enabled = false;
			this.tbb_FrameDown.ImageIndex = 3;
			this.tbb_FrameDown.ToolTipText = "Move Selected Frame Down";
			// 
			// tbb_DeleteFrame
			// 
			this.tbb_DeleteFrame.Enabled = false;
			this.tbb_DeleteFrame.ImageIndex = 5;
			this.tbb_DeleteFrame.ToolTipText = "Delete Currently Selected Frame";
			// 
			// tbb_ApplyAnchor
			// 
			this.tbb_ApplyAnchor.ImageIndex = 6;
			this.tbb_ApplyAnchor.ToolTipText = "Apply Anchor to All Frames";
			// 
			// iml_Frames
			// 
			this.iml_Frames.ColorDepth = System.Windows.Forms.ColorDepth.Depth32Bit;
			this.iml_Frames.ImageSize = new System.Drawing.Size(16, 16);
			this.iml_Frames.ImageStream = ((System.Windows.Forms.ImageListStreamer)(resources.GetObject("iml_Frames.ImageStream")));
			this.iml_Frames.TransparentColor = System.Drawing.Color.Transparent;
			// 
			// pnl_FrameView
			// 
			this.pnl_FrameView.Controls.Add(this.panel1);
			this.pnl_FrameView.Controls.Add(this.pnl_FrameViewTools);
			this.pnl_FrameView.Dock = System.Windows.Forms.DockStyle.Fill;
			this.pnl_FrameView.Location = new System.Drawing.Point(200, 0);
			this.pnl_FrameView.Name = "pnl_FrameView";
			this.pnl_FrameView.Size = new System.Drawing.Size(428, 421);
			this.pnl_FrameView.TabIndex = 3;
			// 
			// panel1
			// 
			this.panel1.Controls.Add(this.grp_CurrentFrame);
			this.panel1.Dock = System.Windows.Forms.DockStyle.Fill;
			this.panel1.Location = new System.Drawing.Point(0, 24);
			this.panel1.Name = "panel1";
			this.panel1.Size = new System.Drawing.Size(428, 397);
			this.panel1.TabIndex = 6;
			// 
			// grp_CurrentFrame
			// 
			this.grp_CurrentFrame.Controls.Add(this.m_FramePanel);
			this.grp_CurrentFrame.Dock = System.Windows.Forms.DockStyle.Fill;
			this.grp_CurrentFrame.Location = new System.Drawing.Point(0, 0);
			this.grp_CurrentFrame.Name = "grp_CurrentFrame";
			this.grp_CurrentFrame.Size = new System.Drawing.Size(428, 397);
			this.grp_CurrentFrame.TabIndex = 4;
			this.grp_CurrentFrame.TabStop = false;
			this.grp_CurrentFrame.Text = "Frame: 0 / 0";
			// 
			// m_FramePanel
			// 
			this.m_FramePanel.BackColor = System.Drawing.SystemColors.Control;
			this.m_FramePanel.BackgroundFrameset = null;
			this.m_FramePanel.CurrentFrame = 0;
			this.m_FramePanel.Dock = System.Windows.Forms.DockStyle.Fill;
			this.m_FramePanel.Frames = null;
			this.m_FramePanel.Location = new System.Drawing.Point(3, 16);
			this.m_FramePanel.Name = "m_FramePanel";
			this.m_FramePanel.ShowGuides = false;
			this.m_FramePanel.Size = new System.Drawing.Size(422, 378);
			this.m_FramePanel.TabIndex = 2;
			// 
			// pnl_FrameViewTools
			// 
			this.pnl_FrameViewTools.Controls.Add(this.tlb_FrameView);
			this.pnl_FrameViewTools.Dock = System.Windows.Forms.DockStyle.Top;
			this.pnl_FrameViewTools.Location = new System.Drawing.Point(0, 0);
			this.pnl_FrameViewTools.Name = "pnl_FrameViewTools";
			this.pnl_FrameViewTools.Size = new System.Drawing.Size(428, 24);
			this.pnl_FrameViewTools.TabIndex = 5;
			// 
			// tlb_FrameView
			// 
			this.tlb_FrameView.Appearance = System.Windows.Forms.ToolBarAppearance.Flat;
			this.tlb_FrameView.Buttons.AddRange(new System.Windows.Forms.ToolBarButton[] {
																							 this.tbb_PreviousFrame,
																							 this.tbb_NextFrame});
			this.tlb_FrameView.DropDownArrows = true;
			this.tlb_FrameView.ImageList = this.iml_Frames;
			this.tlb_FrameView.Location = new System.Drawing.Point(0, 0);
			this.tlb_FrameView.Name = "tlb_FrameView";
			this.tlb_FrameView.ShowToolTips = true;
			this.tlb_FrameView.Size = new System.Drawing.Size(428, 28);
			this.tlb_FrameView.TabIndex = 0;
			this.tlb_FrameView.ButtonClick += new System.Windows.Forms.ToolBarButtonClickEventHandler(this.tlb_FrameView_ButtonClick);
			// 
			// tbb_PreviousFrame
			// 
			this.tbb_PreviousFrame.ImageIndex = 4;
			this.tbb_PreviousFrame.ToolTipText = "Previous Frame";
			// 
			// tbb_NextFrame
			// 
			this.tbb_NextFrame.ImageIndex = 2;
			this.tbb_NextFrame.ToolTipText = "Next Frame";
			// 
			// pnl_Contents
			// 
			this.pnl_Contents.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
			this.pnl_Contents.Controls.Add(this.pnl_FrameView);
			this.pnl_Contents.Controls.Add(this.pnl_Frames);
			this.pnl_Contents.Dock = System.Windows.Forms.DockStyle.Fill;
			this.pnl_Contents.Location = new System.Drawing.Point(0, 0);
			this.pnl_Contents.Name = "pnl_Contents";
			this.pnl_Contents.Size = new System.Drawing.Size(632, 425);
			this.pnl_Contents.TabIndex = 5;
			// 
			// dlg_OpenFrameDialog
			// 
			this.dlg_OpenFrameDialog.Filter = "All Supported Types|*.gif;*.jpg;*.jpeg;*.png|GIF Files (*.gif)|*.gif|JPEG Files (" +
				"*.jpg; *.jpeg)|*.jpg;*.jpeg|PNG Files (*.png)|*.png";
			this.dlg_OpenFrameDialog.Multiselect = true;
			this.dlg_OpenFrameDialog.Title = "Select Images to Add";
			// 
			// dlg_SaveFrameset
			// 
			this.dlg_SaveFrameset.DefaultExt = "png";
			this.dlg_SaveFrameset.Filter = "All Files (*.*)|*.*";
			this.dlg_SaveFrameset.Title = "Save Frameset";
			// 
			// item_SetBackground
			// 
			this.item_SetBackground.Index = 0;
			this.item_SetBackground.Text = "Set Background";
			this.item_SetBackground.Click += new System.EventHandler(this.item_SetBackground_Click);
			// 
			// FrameView
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(632, 425);
			this.Controls.Add(this.pnl_Contents);
			this.Menu = this.mainMenu1;
			this.Name = "FrameView";
			this.Text = "Frame View";
			this.pnl_Frames.ResumeLayout(false);
			this.panel3.ResumeLayout(false);
			this.grp_FrameList.ResumeLayout(false);
			this.panel2.ResumeLayout(false);
			this.pnl_FrameView.ResumeLayout(false);
			this.panel1.ResumeLayout(false);
			this.grp_CurrentFrame.ResumeLayout(false);
			this.pnl_FrameViewTools.ResumeLayout(false);
			this.pnl_Contents.ResumeLayout(false);
			this.ResumeLayout(false);

		}
		#endregion

		private void menuItem4_Click(object sender, System.EventArgs e)
		{
			this.LoadFrameset(GetLoadImageName(), new PazeraFramesetProvider());
		}

		private void menuItem3_Click(object sender, System.EventArgs e)
		{
			this.LoadFrameset(GetLoadImageName(), new SimpleFramesetProvider());
		}

		private void menuItem10_Click(object sender, System.EventArgs e)
		{
			this.SaveFrameset(GetSaveImageName(), new PazeraFramesetProvider());
		}

		private void item_SetBackground_Click(object sender, System.EventArgs e)
		{
			this.LoadBackground(GetLoadImageName(), new PazeraFramesetProvider());
		}

		private void GenerateFrameList() 
		{
			lst_FrameList.Items.Clear();

			for (int i = 0; i < m_FramePanel.Frames.getFrameCount(); i++) 
			{
				lst_FrameList.Items.Add("Frame " + (i + 1));
			}
		}

		private void UpdateCurrentFrame(int new_frame, bool updateListSelection) 
		{
			m_FramePanel.CurrentFrame = new_frame;
			grp_CurrentFrame.Text = "Frame: " + (m_FramePanel.CurrentFrame + 1).ToString() + " / " + m_FramePanel.Frames.getFrameCount();

			if (updateListSelection) 
			{
				lst_FrameList.SelectedIndex = new_frame;
			}
		}

		private void menuItem6_Click(object sender, System.EventArgs e)
		{
			Application.Exit();
		}

		private void item_ShowGuides_Click(object sender, System.EventArgs e)
		{
			bool new_setting = !item_ShowGuides.Checked;

			m_FramePanel.ShowGuides = new_setting;
			item_ShowGuides.Checked = new_setting;
		}

		private void tlb_FrameView_ButtonClick(object sender, System.Windows.Forms.ToolBarButtonClickEventArgs e)
		{
			if (e.Button == tbb_NextFrame) 
			{
				NextFrame();
			} 
			else if (e.Button == tbb_PreviousFrame)
			{
				PreviousFrame();
			}
		}

		private void tlb_Frames_ButtonClick(object sender, System.Windows.Forms.ToolBarButtonClickEventArgs e)
		{
			if (e.Button == tbb_AddFrame) 
			{
				AddFrames();
			} 
			else if (e.Button == tbb_ApplyAnchor) 
			{
				ApplyAnchorPoint();
			}
		}

		private void lst_FrameList_SelectedIndexChanged(object sender, System.EventArgs e)
		{
			UpdateCurrentFrame(lst_FrameList.SelectedIndex, false);
		}

		#region Toolbar and Menu Actions
		#region Loading
		private void LoadFrameset(string file, IFramesetProvider provider) 
		{
			if (file == null) return;

			Bitmap image = (Bitmap) Bitmap.FromFile(file);
			Frameset fs = new Frameset();

			provider.fillFrameset(fs, image);

			this.m_FramePanel.Frames = fs;

			GenerateFrameList();
			UpdateCurrentFrame(0, true);
		}

		private void LoadBackground(string file, IFramesetProvider provider)
		{
			if (file == null) return;

			Bitmap image = (Bitmap) Bitmap.FromFile(file);
			Frameset fs = new Frameset();

			provider.fillFrameset(fs, image);

			this.m_FramePanel.BackgroundFrameset = fs;
		}

		private string GetLoadImageName() 
		{
			DialogResult result = dlg_OpenImage.ShowDialog(this);
			if (result == DialogResult.OK) 
			{
				return dlg_OpenImage.FileName;
			} 
			else 
			{
				return null;
			}
		}
		#endregion

		#region Saving
		private void SaveFrameset(string file, IFramesetProvider provider) 
		{
			if (file == null) return;

			provider.exportFrameset(this.m_FramePanel.Frames, file);
		}

		private string GetSaveImageName() 
		{
			DialogResult result = dlg_SaveFrameset.ShowDialog(this);
			if (result == DialogResult.OK) 
			{
				return dlg_SaveFrameset.FileName;
			} 
			else 
			{
				return null;
			}
		}
		#endregion

		#region Frame View Control
		private void NextFrame() 
		{
			if (m_FramePanel.HasFrames) 
			{
				int cf = m_FramePanel.CurrentFrame;
				cf++;
				if (cf >= m_FramePanel.Frames.getFrameCount()) 
				{
					cf = 0;
				}

				UpdateCurrentFrame(cf, true);
			}
		}

		private void PreviousFrame() 
		{
			if (m_FramePanel.HasFrames) 
			{
				int cf = m_FramePanel.CurrentFrame;
				cf --;
				if (cf < 0) 
				{
					cf = m_FramePanel.Frames.getFrameCount() - 1;
				}

				UpdateCurrentFrame(cf, true);
			}
		}
		#endregion

		#region Frame Control
		private void AddFrames() 
		{
			DialogResult result = dlg_OpenFrameDialog.ShowDialog(this);
			if (result == DialogResult.OK) 
			{
				if (!m_FramePanel.HasFrames) 
				{
					m_FramePanel.Frames = new Frameset();
				}

				string[] files = dlg_OpenFrameDialog.FileNames;
				foreach (string name in files) 
				{
					string[] file_parts = name.Split('\\');
					Bitmap image = (Bitmap) Bitmap.FromFile(name);

					int count = m_FramePanel.Frames.addFrame(image, new Point(0, 0));
					lst_FrameList.Items.Add(file_parts[file_parts.Length - 1]);

					UpdateCurrentFrame(count - 1, true);
				}
			}
		}

		private void ApplyAnchorPoint() 
		{
			if (m_FramePanel.HasFrames) 
			{
				//Get the current anchor point:
				Point ca = m_FramePanel.Frames.getFrameAt(m_FramePanel.CurrentFrame).getAnchorPoint();

				//Apply it to all frames:
				for (int i = 0; i < m_FramePanel.Frames.getFrameCount(); i++) 
				{
					((AbstractFrame) m_FramePanel.Frames.getFrameAt(i)).setAnchorPoint(new Point(ca.X, ca.Y));
				}
			}
		}
		#endregion

		#endregion
	}
}
