using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using BizDraw.Core;
using BizDraw.Controls;
using BizDraw.IO;
using BizDraw.Printing;
using System.Reflection;
namespace BizDraw.Controls
{
[System.Runtime.InteropServices.ComVisible(true)]
public partial class DocumentSpace : UserControl
{
public DocumentSpace()
{
InitializeComponent();
printEngine = new DocumentPrintEngine();
RegisterCommands();
this.tTools.ToolChanged += new EventHandler(tTools_ToolChanged);
}
private Document currentDocument;
private DocumentPrintEngine printEngine ;
private DocumentArea editor;
private Dictionary<string, Command > Commands;
public string documentFileName ;
void tTools_ToolChanged(object sender, EventArgs e)
{
this.editor.ActiveTool = this.tTools.SelectedTool;
}
void RegisterCommands()
{
if (DesignMode)
return;
Commands = new Dictionary<string, Command >();
Type thisType = this.GetType();
//File commands
Commands.Add("NewDocument", new Command("NewDocument", this,GetMethod("NewDocument"),true, this.mNewDocument, this.tNew));
Commands.Add("OpenDocument", new Command("OpenDocument", this, GetMethod("OpenDocument"), true, this.mOpen, this.tOpen));
Commands.Add("CloseDocument", new Command("CloseDocument", this, GetMethod("CloseDocument"), false,this.mClose , this.tClose ) );
Commands.Add("SaveDocument", new Command("SaveDocument", this, GetMethod("SaveDocument"), false, this.mSave, this.tSave));
Commands.Add("SaveDocumentAs", new Command("SaveDocumentAs", this, GetMethod("SaveDocumentAs"), false, this.mSaveAs, this.tSaveAs));
Commands.Add("Print", new Command("Print", this, GetMethod("Print"), false, this.mPrint, this.tPrint));
Commands.Add("Properties", new Command("Properties", this, GetMethod("ShowDocumentProperties"), false, this.mProperties , this.tProperties ));
//Edit Commands
Commands.Add("Undo", new Command("Undo", this, GetMethod("UndoAction"), false, this.mUndo, this.tUndo));
Commands.Add("Redo", new Command("Redo", this, GetMethod("RedoAction"), false, this.mRedo, this.tRedo));
Commands.Add("Copy", new Command("Copy", this, GetMethod("CopySelection"), false, this.mCopy, this.tCopy));
Commands.Add("Cut", new Command("Cut", this, GetMethod("CutSelection"), false, this.mCut, this.tCut));
Commands.Add("Paste", new Command("Paste", this, GetMethod("PasteSelection"), false, this.mPaste, this.tPaste));
Commands.Add("Delete", new Command("Delete", this, GetMethod("DeleteSelection"), false, this.mDelete, this.tDelete));
Commands.Add("BringToFront", new Command("BringToFront", this, GetMethod("BringSelectionToFront"), false, this.mBringToFront, this.tBringToFront));
Commands.Add("SendToBack", new Command("SendToBack", this, GetMethod("SendSelectionToBack"), false, this.mSendToBack, this.tSendToBack));
}
private MethodInfo GetMethod(string name)
{
return this.GetType().GetMethod(name, BindingFlags.NonPublic | BindingFlags.Instance);
}
#region Properties
#endregion
#region File Commands
/// <summary>
/// Creates a new document
/// </summary>
private void NewDocument()
{
if (!CloseDocument())
return;
currentDocument = new Document();
FrmDocumentPropertiesEditor frm = new FrmDocumentPropertiesEditor();
frm.StartPosition = FormStartPosition.CenterScreen;
frm.Document = currentDocument;
if (frm.ShowDialog() != DialogResult.OK)
{
return;
}
DoOpenDocument();
}
void currentDocument_SelectedObjectChanged(object sender, EventArgs e)
{
if (this.currentDocument.Items.SelectionCount == 0)
{
Commands["Delete"].Disable();
Commands["Copy"].Disable();
Commands["Cut"].Disable();
Commands["BringToFront"].Disable();
Commands["SendToBack"].Disable();
}
else
{
Commands["Delete"].Enable ();
Commands["Copy"].Enable();
Commands["Cut"].Enable();
Commands["BringToFront"].Enable();
Commands["SendToBack"].Enable();
}
}
void DoOpenDocument()
{
currentDocument.SelectedObjectChanged += new EventHandler(currentDocument_SelectedObjectChanged);
editor = new DocumentArea();
editor.Document = this.currentDocument;
editor.Size = new Size(100, 100);
editor.Location = new Point(0, 0);
editor.Dock = DockStyle.Fill;
this.toolStripContainer1.ContentPanel.Controls.Add(editor);
//Enabling items
this.tTools.Enabled = true;
Commands["SaveDocument"].Enable();
Commands["SaveDocumentAs"].Enable();
Commands["CloseDocument"].Enable();
Commands["Print"].Enable();
Commands["Properties"].Enable();
this.tZoom.Enabled = true;
this.tZoomIn.Enabled = true;
this.tZoomOut.Enabled = true;
}
/// <summary>
/// Opens an existent document
/// </summary>
private void OpenDocument()
{
if (!CloseDocument())
return;
OpenFileDialog ofd = new OpenFileDialog();
ofd.Filter = "Fichiers BizDraw (*.bzw)|*.bzw";
ofd.InitialDirectory = Application.StartupPath + "\\Documents";
if (ofd.ShowDialog() == DialogResult.OK)
{
this.currentDocument = DocumentManager.Load(ofd.FileName);
documentFileName = ofd.FileName;
DoOpenDocument();
}
}
/// <summary>
/// Close an open document
/// </summary>
private bool CloseDocument()
{
bool result = false;
if (this.currentDocument != null)
{
if (this.currentDocument.IsDirty)
{
DialogResult dr = MessageBox.Show("Voulez vous enregistrer les modifications effectu�es avant d'ouvrir un nouveau document", "Nouveau", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question);
if (dr == DialogResult.Yes )
{
if (!SaveDocument())
return false;
else
result = true;
}
else if (dr == DialogResult.No)
{
DoCloseDocument();
result = true;
}
else
return false;
}
else
{
//close the document
DoCloseDocument();
result = true;
}
}
else
{
DoCloseDocument();
result = true;
}
return result;
}
private void DoCloseDocument()
{
if (this.editor == null)
return;
this.editor.Dispose();
this.toolStripContainer1.ContentPanel.Controls.Remove(editor);
this.editor = null;
//disabling items
this.tTools.Enabled = false ;
Commands["SaveDocument"].Disable();
Commands["SaveDocumentAs"].Disable();
Commands["CloseDocument"].Disable();
Commands["Print"].Disable();
Commands["Properties"].Disable();
this.tZoom.Enabled = false;
this.tZoomIn.Enabled = false;
this.tZoomOut.Enabled = false;
}
/// <summary>
/// Saves the document
/// </summary>
/// <returns></returns>
private bool SaveDocument()
{
if (documentFileName == null)
{
return SaveDocumentAs();
}
else
{
DocumentManager.Save(currentDocument, this.documentFileName);
return true;
}
}
/// <summary>
/// Saves the document to a new location
/// </summary>
/// <returns></returns>
private bool SaveDocumentAs()
{
SaveFileDialog sfd = new SaveFileDialog();
sfd.Filter = "Fichiers BizDraw (*.bzw)|*.bzw";
sfd.InitialDirectory = Application.StartupPath + "\\Documents";
if (sfd.ShowDialog() == DialogResult.OK)
{
DocumentManager.Save(currentDocument, sfd.FileName);
this.documentFileName = sfd.FileName;
sfd.Dispose();
return true;
}
else
return false;
}
/// <summary>
/// Prints the current document
/// </summary>
/// <returns></returns>
private void Print()
{
this.currentDocument.Items.UnselectAll();
printEngine.Document = this.currentDocument;
printEngine.ShowDialog();
}
/// <summary>
/// Show the document settings page
/// </summary>
private void ShowDocumentProperties()
{
FrmDocumentPropertiesEditor frm = new FrmDocumentPropertiesEditor();
frm.Document = this.currentDocument;
frm.ShowDialog();
}
#endregion
#region Edit Commands
/// <summary>
/// Undo the last action
/// </summary>
private void UndoAction()
{
}
/// <summary>
/// Redo the last undo action
/// </summary>
private void RedoAction()
{
}
/// <summary>
/// Copies the selected object(s) to the clipboard
/// </summary>
private void CopySelection()
{
this.currentDocument.CopySelection();
this.Commands["Paste"].Enable();
}
/// <summary>
/// Cuts the selected object(s)
/// </summary>
private void CutSelection()
{
this.currentDocument.CutSelection();
this.Commands["Paste"].Enable();
}
/// <summary>
/// Pastes the selected object(s)
/// </summary>
private void PasteSelection()
{
this.currentDocument.Paste();
this.Commands["Paste"].Disable();
this.editor.Refresh();
}
/// <summary>
/// Deletes the selected object(s)
/// </summary>
private void DeleteSelection()
{
this.currentDocument.DeleteSelection();
this.editor.Refresh();
}
/// <summary>
/// Brings to front the selected object(s)
/// </summary>
private void BringSelectionToFront()
{
this.currentDocument.MoveSelectionToFront();
this.Refresh();
}
/// <summary>
/// Sends to back the selected object(s)
/// </summary>
private void SendSelectionToBack()
{
this.currentDocument.MoveSelectionToBack();
this.Refresh();
}
#endregion
private void apProposToolStripMenuItem_Click(object sender, EventArgs e)
{
try
{
FrmAbout frm = new FrmAbout();
frm.ShowDialog();
}
catch (System.Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void FrmMain_Load(object sender, EventArgs e)
{
try
{
if (!string.IsNullOrEmpty(this.documentFileName))
{
this.currentDocument = DocumentManager.Load(this.documentFileName);
DoOpenDocument();
}
}
catch (System.Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void toolStripContainer1_TopToolStripPanel_Click(object sender, EventArgs e)
{
}
private void tTools_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
{
}
private void tZoomIn_Click(object sender, EventArgs e)
{
try
{
editor.Zoom *= 2;
this.tZoom.Text =(editor.Zoom).ToString("0%");
}
catch (System.Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void tZoomOut_Click(object sender, EventArgs e)
{
try
{
editor.Zoom /= 2;
this.tZoom.Text = (editor.Zoom).ToString("0%");
}
catch (System.Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void tZoom_KeyDown(object sender, KeyEventArgs e)
{
try
{
if (e.KeyData == Keys.Enter)
{
editor.Zoom = float.Parse((Convert.ToDouble(this.tZoom.Text.Replace("%", "")) /100).ToString());
}
}
catch (System.Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void tZoom_SelectedIndexChanged(object sender, EventArgs e)
{
try
{
editor.Zoom = float.Parse((Convert.ToDouble(this.tZoom.Text.Replace("%", "")) / 100).ToString());
}
catch (System.Exception ex)
{
MessageBox.Show(ex.Message);
}
}
#region Tools Commands
#endregion
[System.Runtime.InteropServices.ComRegisterFunction()]
public static void Register(Type t)
{
BizDraw.Interop.ComRegistration.RegisterControl(t);
}
[System.Runtime.InteropServices.ComUnregisterFunction()]
public static void UnRegister(Type t)
{
BizDraw.Interop.ComRegistration.UnRegisterControl(t);
}
}
}