File: GraphForm.cs

package info (click to toggle)
graphviz 2.26.3-5%2Bsqueeze2
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 63,032 kB
  • ctags: 25,930
  • sloc: ansic: 212,134; sh: 20,316; cpp: 7,239; makefile: 4,211; yacc: 3,335; xml: 2,450; tcl: 1,900; cs: 1,890; objc: 1,149; perl: 829; lex: 363; awk: 171; python: 41; ruby: 35; php: 26
file content (192 lines) | stat: -rwxr-xr-x 5,833 bytes parent folder | download | duplicates (3)
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
/* $Id: GraphForm.cs,v 1.10 2009/06/03 01:10:59 ellson Exp $ $Revision: 1.10 $ */
/* 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.ComponentModel;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Printing;
using System.IO;
using System.Windows.Forms;
using System.Collections.Generic;
using System.Text;

namespace Graphviz
{
	public partial class GraphForm : Form, FormController.IMenus
	{
		public event EventHandler Changed;
		
		public Graph Graph
		{
			get { return _graph; }
		}
		
		public GraphForm(string fileName)
		{
			InitializeComponent();

			/* window title is the filename portion */
			Text = Path.GetFileName(fileName);
		
			/* associated icon is eventually sourced by the desktop from this executable's embedded Win32 resources */
			Icon = Icon.ExtractAssociatedIcon(fileName);

			_graph = new Graph(fileName);
		
			/* whenever graph changes, rerender and display the graph */
			_graph.Changed += delegate(object sender, EventArgs e)
		{
				RenderGraph();
			};
			_graph.Arguments["layout"] = "dot";
			
			_watcher = new PathWatcher(fileName);
			_watcher.SynchronizingObject = this;
			_watcher.Changed += delegate(object sender, CancelEventArgs eventArgs)
			{
				try
				{
					/* replace old graph with new and rerender */
					Graph newGraph = new Graph(((PathWatcher)sender).Watched);
						((IDisposable)_graph).Dispose();
						_graph = newGraph;
						_graph.Arguments["layout"] = "dot";
						RenderGraph();
						
						if (Changed != null)
							Changed(this, EventArgs.Empty);
					}
				catch (Win32Exception exception)
				{
					/* if another process is holding on to the graph, try opening again later */
					if (exception.NativeErrorCode == 32)
						eventArgs.Cancel = false;
				}
			};
			_watcher.Start();
		
			/* set up export dialog with initial file and filters from the graph devices */
			StringBuilder exportFilter = new StringBuilder();
			int filterIndex = 0;
			for (int deviceIndex = 0; deviceIndex < _devices.Count; ++deviceIndex)
			{
				if (deviceIndex > 0)
					exportFilter.Append("|");
					
				string device = _devices[deviceIndex];
				exportFilter.Append(String.Format("{0} (*.{1})|*.{1}", device.ToUpper(), device));
				
				if (filterIndex == 0 && device == "emfplus")
					filterIndex = deviceIndex;
			}
			exportFileDialog.FileName = Path.GetFileNameWithoutExtension(fileName);
			exportFileDialog.Filter = exportFilter.ToString();
			exportFileDialog.FilterIndex = filterIndex + 1;
			
			printDocument.DocumentName = Path.GetFileName(fileName);
		}

		protected override void OnFormClosed(FormClosedEventArgs e)
		{
			/* when form closes, clean up graph too */
			((IDisposable)_graph).Dispose();
			base.OnFormClosed(e);
		}

		private void RenderGraph()
		{
			using (Stream stream = _graph.Render("emfplus:gdiplus"))
				graphControl.Image = new Metafile(stream);
		}
		
		ToolStripMenuItem FormController.IMenus.ExitMenuItem
		{
			get { return exitToolStripMenuItem; }
		}

		ToolStripMenuItem FormController.IMenus.OpenMenuItem
		{
			get { return openToolStripMenuItem; }
		}

		ToolStripMenuItem FormController.IMenus.WindowMenuItem
		{
			get { return windowToolStripMenuItem; }
		}
		
		private void ShowAttributes_Click(object sender, EventArgs eventArgs)
		{
			AttributeInspectorForm.Instance.Show();
		}

		private void ZoomToFit_Click(object sender, EventArgs eventArgs)
		{
			graphControl.ZoomToFit();
		}
		
		private void ActualSize_Click(object sender, EventArgs eventArgs)
		{
			graphControl.ActualSize();
		}
		
		private void ZoomIn_Click(object sender, EventArgs eventArgs)
		{
			graphControl.ZoomIn();
		}

		private void ZoomOut_Click(object sender, EventArgs eventArgs)
		{
			graphControl.ZoomOut();
		}

		private void exportToolStripMenuItem_Click(object sender, EventArgs e)
		{
			/* pose the export dialog and then render the graph with the selected file and format */
			if (exportFileDialog.ShowDialog(this) == DialogResult.OK)
			{
				int filterIndex = exportFileDialog.FilterIndex - 1;
				if (filterIndex >= 0 && filterIndex < _devices.Count)
					_graph.Render(_devices[filterIndex], exportFileDialog.FileName);
			}
		}
		
		private void pageSetupToolStripMenuItem_Click(object sender, EventArgs e)
		{
			pageSetupDialog.ShowDialog(this);
		}

		private void printToolStripMenuItem_Click(object sender, EventArgs e)
		{
			if (printDialog.ShowDialog(this) == DialogResult.OK)
				printDocument.Print();
		}

		private void printPreviewToolStripMenuItem_Click(object sender, EventArgs e)
		{
			printPreviewDialog.ShowDialog(this);
		}

		private void printDocument_BeginPrint(object sender, PrintEventArgs e)
		{
			new TileableImagePrinter((PrintDocument)sender, graphControl.Image);
		}
		
		private static readonly IList<string> _devices = Graph.GetPlugins(Graph.API.Device, false);
		private Graph _graph;
		private readonly PathWatcher _watcher;
	}
}