Don't require that types be specified on the command line if the displayer
[mono.git] / mcs / tools / type-reflector / displayers / swf / SwfTypeDisplayer.cs
1 //
2 // SwfTypeDisplayer.cs: 
3 //   Display types using System.Windows.Forms
4 //
5 // Author: Jonathan Pryor (jonpryor@vt.edu)
6 //
7 // (C) 2003 Jonathan Pryor
8 //
9
10 // #define TRACE
11
12 using System;
13 using System.Collections;
14 using System.IO;
15 using System.Diagnostics;
16 using System.Reflection;
17 using System.Text;
18 using System.Text.RegularExpressions;
19
20 // for GUI support
21 using System.Drawing;
22 using System.ComponentModel;
23 using System.Windows.Forms;
24
25 namespace Mono.TypeReflector.Displayers.Swf
26 {
27         class ReflectorTreeNode : TreeNode
28         {
29                 private Node node = null;
30
31                 public ReflectorTreeNode (string text)
32                 {
33                         base.Text = text;
34                 }
35
36                 public ReflectorTreeNode (Node node)
37                 {
38                         this.node = node;
39                         base.Text = node.Description;
40                 }
41                 
42                 public Node Node {
43                         get {return node;}
44                 }
45         }
46
47         public class SwfTypeDisplayer : TypeDisplayer
48         {
49                 private static int windows = 0;
50
51                 private const string dummyText = "dummy: you shouldn't see this!";
52
53                 private SwfWindow window = new SwfWindow ();
54
55                 public override int MaxDepth {
56                         set {/* ignore */}
57                 }
58
59                 public override bool RequireTypes {
60                         get {return false;}
61                 }
62
63                 public SwfTypeDisplayer ()
64                 {
65                         ++windows;
66
67                         window.FileOpenClick += new EventHandler (OnFileOpen);
68                         window.FileQuitClick += new EventHandler (OnFileQuit);
69
70                         window.EditCopyClick += new EventHandler (OnEditCopy);
71
72                         window.ViewFormatterDefaultClick += new EventHandler (OnViewFormatterDefault);
73                         window.ViewFormatterVBClick += new EventHandler (OnViewFormatterVB);
74                         window.ViewFormatterCSharpClick += new EventHandler (OnViewFormatterCSharp);
75                         window.ViewFinderExplicitClick += new EventHandler (OnViewFinderExplicit);
76                         window.ViewFinderReflectionClick += new EventHandler (OnViewFinderReflection);
77
78                         window.HelpAboutClick += new EventHandler (OnHelpAbout);
79
80                         window.TreeView.BeforeExpand += new TreeViewCancelEventHandler (OnNodeExpand);
81                 }
82
83                 public override void Run ()
84                 {
85                         ShowTypes ();
86
87                         Application.Run (window);
88                 }
89
90                 public override void ShowError (string message)
91                 {
92                         MessageBox.Show (message, "Type Reflector", MessageBoxButtons.OK, 
93                                         MessageBoxIcon.Error);
94                 }
95
96                 private void ShowTypes ()
97                 {
98                         foreach (Assembly a in Assemblies) {
99                                 ReflectorTreeNode tn = new ReflectorTreeNode (string.Format ("Assembly: {0}", a.FullName));
100
101                                 foreach (string ns in Namespaces (a)) {
102                                         ReflectorTreeNode nn = new ReflectorTreeNode (string.Format ("Namespace: {0}", ns));
103                                         tn.Nodes.Add (nn);
104
105                                         foreach (Type type in Types (a, ns))
106                                                 AddType (type, nn);
107                                 }
108
109                                 window.TreeView.Nodes.Add (tn);
110                         }
111
112                         window.Show ();
113                 }
114                 
115                 private void AddType (Type type, ReflectorTreeNode parent)
116                 {
117                         ReflectorTreeNode tn = CreateTreeNode (type);
118                         tn.Nodes.Add (new ReflectorTreeNode (dummyText));
119                         parent.Nodes.Add (tn);
120                 }
121
122                 private ReflectorTreeNode CreateTreeNode (Type type)
123                 {
124                         Node root = new Node (Formatter, Finder);
125                         root.NodeInfo = new NodeInfo (null, type);
126                         return new ReflectorTreeNode (root);
127                 }
128
129                 // System.Windows.Forms Functions...
130                 private void OnFileQuit (object o, EventArgs args) 
131                 {
132                         Console.WriteLine ("Asked to quit app; windows=" + windows);
133                         window.Hide ();
134                         window.Dispose ();
135                         if (--windows == 0) {
136                                 Console.WriteLine ("App.Exit");
137                                 Application.Exit ();
138                         }
139                 }
140
141                 private void OnFileOpen (object o, EventArgs args)
142                 {
143                         OpenFileDialog ofd = new OpenFileDialog ();
144                         ofd.CheckFileExists = true;
145                         ofd.Multiselect = true;
146                         ofd.Title = "Open Assembly";
147                         ofd.ValidateNames = true;
148                         ofd.Filter = 
149                                 "Assemblies (*.dll;*.exe)|*.dll;*.exe" +
150                                 "|Dynamic Link Libraries (*.dll)|*.dll" +
151                                 "|Executables (*.exe)|*.exe" +
152                                 "|All Files (*.*)|*.*";
153
154                         if (ofd.ShowDialog() == DialogResult.OK) {
155                                 OpenAssemblies (ofd.FileNames);
156                         }
157                 }
158
159                 private void OpenAssemblies (string[] assemblies)
160                 {
161                         SwfTypeDisplayer d = null;
162                         if (base.Assemblies.Count == 0)
163                                 d = this;
164                         else {
165                                 d = new SwfTypeDisplayer ();
166                                 d.Finder = Finder;
167                                 d.Formatter = Formatter;
168                                 d.Options = Options;
169                         }
170
171                         TypeLoader tl = TypeReflectorApp.CreateLoader (Options);
172                         tl.Assemblies = assemblies;
173
174                         try {
175                                 TypeReflectorApp.FindTypes (d, tl, new string[]{"."});
176                                 d.ShowTypes ();
177                         }
178                         catch (Exception e) {
179                                 ShowError (string.Format ("Unable to load Assembly '{0}': {1}",
180                                                         assemblies, e.ToString()));
181                         }
182                 }
183
184                 public void OnEditCopy (object o, EventArgs args)
185                 {
186                         /* ignore */
187                 }
188
189                 public void OnViewFormatterDefault (object o, EventArgs args)
190                 {
191                 }
192
193                 public void OnViewFormatterVB (object o, EventArgs args)
194                 {
195                 }
196
197                 public void OnViewFormatterCSharp (object o, EventArgs args)
198                 {
199                 }
200
201                 public void OnViewFinderReflection (object o, EventArgs args)
202                 {
203                 }
204
205                 public void OnViewFinderExplicit (object o, EventArgs args)
206                 {
207                 }
208
209                 public void OnHelpAbout (object o, EventArgs args)
210                 {
211                         /* ignore */
212                         MessageBox.Show (
213                                         "Type Reflector, version x.y.  Copyright (C) 2002-2003 Jonathan Pryor",
214                                         "About Type Reflector",
215                                         MessageBoxButtons.OK,
216                                         MessageBoxIcon.Information);
217                         TypeReflectorApp.PrintVersion ();
218                 }
219
220                 private void OnNodeExpand (object sender, TreeViewCancelEventArgs e)
221                 {
222                         /*
223                         Console.WriteLine ("(node-expanded (Action {0}) (Cancel {1}) (Node {2}))",
224                                         e.Action, e.Cancel, e.Node);
225                          */
226
227                         if ((e.Node.Nodes.Count > 0) && (e.Node.Nodes[0].Text == dummyText)) {
228                                 ReflectorTreeNode tn = (ReflectorTreeNode) e.Node;
229                                 tn.Nodes.Clear ();
230
231                                 foreach (Node child in tn.Node.GetChildren()) {
232                                         ReflectorTreeNode cn = new ReflectorTreeNode (child);
233                                         cn.Nodes.Add (new ReflectorTreeNode (dummyText));
234                                         tn.Nodes.Add (cn);
235                                 }
236
237                                 if (tn.Nodes.Count == 0)
238                                         e.Cancel = true;
239                         }
240                 }
241         }
242 }
243