--- /dev/null
+/* $Id$ $Revision$ */\r
+/* vim:set shiftwidth=4 ts=8: */\r
+\r
+/**********************************************************\r
+* This software is part of the graphviz package *\r
+* http://www.graphviz.org/ *\r
+* *\r
+* Copyright (c) 1994-2008 AT&T Corp. *\r
+* and is licensed under the *\r
+* Common Public License, Version 1.0 *\r
+* by AT&T Corp. *\r
+* *\r
+* Information and Software Systems Research *\r
+* AT&T Research, Florham Park NJ *\r
+**********************************************************/\r
+\r
+using System;\r
+using System.Collections.Generic;\r
+using System.IO;\r
+using System.Windows.Forms;\r
+\r
+namespace Graphviz\r
+{\r
+ public class FormController\r
+ {\r
+ /* document forms need to declare these menus */\r
+ public interface IMenus\r
+ {\r
+ ToolStripMenuItem ExitMenuItem\r
+ {\r
+ get;\r
+ }\r
+\r
+ ToolStripMenuItem OpenMenuItem\r
+ {\r
+ get;\r
+ }\r
+\r
+ ToolStripMenuItem ShowAttributesMenuItem\r
+ {\r
+ get;\r
+ }\r
+\r
+ ToolStripMenuItem WindowMenuItem\r
+ {\r
+ get;\r
+ }\r
+ }\r
+ \r
+ public static FormController Instance\r
+ {\r
+ get { return _instance; }\r
+ }\r
+ \r
+ public Form MainForm\r
+ {\r
+ /* return the topmost document form */\r
+ get { return _mainForm; }\r
+ }\r
+\r
+ public event EventHandler MainFormChanged;\r
+\r
+ public string[] FilesToOpen()\r
+ {\r
+ /* if user said OK, return the files he selected */\r
+ return _openFileDialog.ShowDialog() == DialogResult.OK ? _openFileDialog.FileNames : null;\r
+ }\r
+\r
+ public Form OpenFiles(ICollection<string> fileNames)\r
+ {\r
+ Form foundForm = null;\r
+ foreach (string fileName in fileNames) {\r
+ string canonicalPath = Path.GetFullPath(fileName).ToLower();\r
+ if (_documentForms.ContainsKey(canonicalPath))\r
+ /* document already open */\r
+ foundForm = _documentForms[canonicalPath];\r
+ else {\r
+ /* document needs to be created */\r
+ _documentForms[canonicalPath] = foundForm = CreateForm(fileName);\r
+\r
+ IMenus documentFormMenus = foundForm as IMenus;\r
+ if (documentFormMenus != null) {\r
+ /* exit menu quits the app */\r
+ documentFormMenus.ExitMenuItem.Click += delegate(object sender, EventArgs eventArgs)\r
+ {\r
+ Application.Exit();\r
+ };\r
+\r
+ /* open menu asks user which files to open and then opens them */\r
+ documentFormMenus.OpenMenuItem.Click += delegate(object sender, EventArgs eventArgs)\r
+ {\r
+ string[] filesToOpen = FilesToOpen();\r
+ if (filesToOpen != null)\r
+ OpenFiles(filesToOpen);\r
+ };\r
+\r
+ /* attribute menu shows the attribute inspector */\r
+ documentFormMenus.ShowAttributesMenuItem.Click += delegate(object sender, EventArgs eventArgs)\r
+ {\r
+ AttributeInspectorForm.Instance.Show();\r
+ };\r
+\r
+ /* window menu shows a list of all forms, select one to activate it */\r
+ documentFormMenus.WindowMenuItem.DropDownOpening += delegate(object sender, EventArgs eventArgs)\r
+ {\r
+ ToolStripMenuItem windowMenuItem = sender as ToolStripMenuItem;\r
+ if (windowMenuItem != null)\r
+ {\r
+ windowMenuItem.DropDownItems.Clear();\r
+ int i = 0;\r
+ foreach (Form form in Application.OpenForms)\r
+ {\r
+ Form innerForm = form;\r
+ ToolStripMenuItem formMenuItem = new ToolStripMenuItem(string.Format("{0} {1}", ++i, form.Text));\r
+ formMenuItem.Checked = Form.ActiveForm == innerForm;\r
+ formMenuItem.Click += delegate(object innerSender, EventArgs innerEventArgs)\r
+ {\r
+ innerForm.Activate();\r
+ };\r
+ windowMenuItem.DropDownItems.Add(formMenuItem);\r
+ }\r
+ }\r
+ };\r
+ }\r
+\r
+ /* when form activated, change the main form */\r
+ foundForm.Activated += delegate(object sender, EventArgs eventArgs)\r
+ {\r
+ _mainForm = (Form)sender;\r
+ if (MainFormChanged != null)\r
+ MainFormChanged(_mainForm, EventArgs.Empty);\r
+ };\r
+\r
+ /* when form closed, remove it from our list */\r
+ foundForm.FormClosed += delegate(object sender, FormClosedEventArgs e)\r
+ {\r
+ _documentForms.Remove(canonicalPath);\r
+ };\r
+\r
+ /* window title is the filename portion */\r
+ foundForm.Text = Path.GetFileName(fileName);\r
+ \r
+ foundForm.Show();\r
+ }\r
+ }\r
+ return foundForm;\r
+ }\r
+ \r
+ private FormController()\r
+ {\r
+ _openFileDialog = new OpenFileDialog();\r
+ _openFileDialog.Filter = "Graphviz graphs (*.dot)|*.dot|All files (*.*)|*.*";\r
+ _openFileDialog.Multiselect = true;\r
+ \r
+ _documentForms = new Dictionary<string, Form>();\r
+ _mainForm = null;\r
+ }\r
+\r
+ private Form CreateForm(string fileName)\r
+ {\r
+ return new GraphForm(fileName);\r
+ }\r
+\r
+ private static FormController _instance = new FormController();\r
+ private readonly OpenFileDialog _openFileDialog;\r
+ private readonly Dictionary<string, Form> _documentForms;\r
+ private Form _mainForm;\r
+ }\r
+}\r