1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
|
/* $Id: FormController.cs,v 1.7 2009/06/03 01:10:59 ellson Exp $ $Revision: 1.7 $ */
/* vim:set shiftwidth=4 ts=8: */
/**********************************************************
* This software is part of the graphviz package *
* http://www.graphviz.org/ *
* *
* Copyright (c) 1994-2008 AT&T Corp. *
* and is licensed under the *
* Common Public License, Version 1.0 *
* by AT&T Corp. *
* *
* Information and Software Systems Research *
* AT&T Research, Florham Park NJ *
**********************************************************/
using System;
using System.Collections.Generic;
using System.IO;
using System.Windows.Forms;
namespace Graphviz
{
public class FormController
{
/* document forms need to declare these menus */
public interface IMenus
{
ToolStripMenuItem ExitMenuItem
{
get;
}
ToolStripMenuItem OpenMenuItem
{
get;
}
ToolStripMenuItem WindowMenuItem
{
get;
}
}
public static FormController Instance
{
get { return _instance; }
}
public Form MainForm
{
/* return the topmost document form */
get { return _mainForm; }
}
public event EventHandler MainFormChanged;
public string[] FilesToOpen()
{
/* if user said OK, return the files he selected */
return _openFileDialog.ShowDialog() == DialogResult.OK ? _openFileDialog.FileNames : null;
}
public Form OpenFiles(ICollection<string> fileNames)
{
Form foundForm = null;
foreach (string fileName in fileNames) {
string canonicalPath = Path.GetFullPath(fileName).ToLower();
if (_documentForms.ContainsKey(canonicalPath))
/* document already open */
foundForm = _documentForms[canonicalPath];
else {
/* document needs to be created */
_documentForms[canonicalPath] = foundForm = CreateForm(fileName);
IMenus documentFormMenus = foundForm as IMenus;
if (documentFormMenus != null) {
/* exit menu quits the app */
documentFormMenus.ExitMenuItem.Click += delegate(object sender, EventArgs eventArgs)
{
Application.Exit();
};
/* open menu asks user which files to open and then opens them */
documentFormMenus.OpenMenuItem.Click += delegate(object sender, EventArgs eventArgs)
{
string[] filesToOpen = FilesToOpen();
if (filesToOpen != null)
OpenFiles(filesToOpen);
};
/* window menu shows a list of all document forms, select one to activate it */
documentFormMenus.WindowMenuItem.DropDownOpening += delegate(object sender, EventArgs eventArgs)
{
ToolStripMenuItem windowMenuItem = sender as ToolStripMenuItem;
if (windowMenuItem != null)
{
windowMenuItem.DropDownItems.Clear();
int i = 0;
foreach (Form form in _documentForms.Values)
{
Form innerForm = form;
ToolStripMenuItem formMenuItem = new ToolStripMenuItem(string.Format("{0} {1}", ++i, form.Text));
formMenuItem.Checked = Form.ActiveForm == innerForm;
formMenuItem.Click += delegate(object innerSender, EventArgs innerEventArgs)
{
innerForm.Activate();
};
windowMenuItem.DropDownItems.Add(formMenuItem);
}
}
};
}
/* when form activated, change the main form */
foundForm.Activated += delegate(object sender, EventArgs eventArgs)
{
_mainForm = (Form)sender;
if (MainFormChanged != null)
MainFormChanged(_mainForm, EventArgs.Empty);
};
/* when form closed, remove it from our list; exit when all closed */
foundForm.FormClosed += delegate(object sender, FormClosedEventArgs e)
{
_documentForms.Remove(canonicalPath);
if (_documentForms.Count == 0)
Application.Exit();
};
foundForm.Show();
}
}
return foundForm;
}
private FormController()
{
_openFileDialog = new OpenFileDialog();
_openFileDialog.Filter = "Graphviz graphs (*.dot;*.gv)|*.dot;*.gv|All files (*.*)|*.*";
_openFileDialog.Multiselect = true;
_documentForms = new SortedDictionary<string, Form>();
_mainForm = null;
}
private Form CreateForm(string fileName)
{
return new GraphForm(fileName);
}
private static FormController _instance = new FormController();
private readonly OpenFileDialog _openFileDialog;
private readonly SortedDictionary<string, Form> _documentForms;
private Form _mainForm;
}
}
|