diff options
author | Miguel de Icaza <miguel@gnome.org> | 2008-11-02 07:40:31 +0300 |
---|---|---|
committer | Miguel de Icaza <miguel@gnome.org> | 2008-11-02 07:40:31 +0300 |
commit | 1208deb14db2dfbba9c7af7926de1786b0c69ff2 (patch) | |
tree | 0e7b3f5e5922392beccf650658a1998eb046d703 /gsharp | |
parent | 1e03186f4cc8049508225fe256270eab1b6e6704 (diff) |
2008-11-02 Miguel de Icaza <miguel@novell.com>
* InteractiveGraphicsBase.cs (RegisterRenderHandler,
UnregisterRenderHandler): new methods to allow scripts to register
their own rendering handlers.
(Attached): useful property to determine if we are running on an
attached process or not.
(MainWindow): pointer to the current main window.
* MainWindow.cs: Add support for loading startup files.
* Main.cs: Move bitmap rendering here, to split it out from the
Shell.
* Shell.c: Now can use any provided hook to render graphics on the
screen.
svn path=/trunk/mono-tools/; revision=117634
Diffstat (limited to 'gsharp')
-rw-r--r-- | gsharp/ChangeLog | 19 | ||||
-rw-r--r-- | gsharp/InteractiveGraphicsBase.cs | 38 | ||||
-rw-r--r-- | gsharp/Main.cs | 19 | ||||
-rw-r--r-- | gsharp/MainWindow.cs | 68 | ||||
-rw-r--r-- | gsharp/Shell.cs | 96 |
5 files changed, 218 insertions, 22 deletions
diff --git a/gsharp/ChangeLog b/gsharp/ChangeLog index 3ec400ea..d527f614 100644 --- a/gsharp/ChangeLog +++ b/gsharp/ChangeLog @@ -1,3 +1,22 @@ +2008-11-02 Miguel de Icaza <miguel@novell.com> + + * InteractiveGraphicsBase.cs (RegisterRenderHandler, + UnregisterRenderHandler): new methods to allow scripts to register + their own rendering handlers. + + (Attached): useful property to determine if we are running on an + attached process or not. + + (MainWindow): pointer to the current main window. + + * MainWindow.cs: Add support for loading startup files. + + * Main.cs: Move bitmap rendering here, to split it out from the + Shell. + + * Shell.c: Now can use any provided hook to render graphics on the + screen. + 2008-10-31 Sandy Armstrong <sanfordarmstrong@gmail.com> * gsharp.desktop.in: Add .desktop file for gsharp. diff --git a/gsharp/InteractiveGraphicsBase.cs b/gsharp/InteractiveGraphicsBase.cs index 542f04b0..c29cfbfd 100644 --- a/gsharp/InteractiveGraphicsBase.cs +++ b/gsharp/InteractiveGraphicsBase.cs @@ -26,6 +26,7 @@ using System; using System.Drawing; using System.Drawing.Drawing2D; +using System.Collections.Generic; namespace Mono.CSharp.Gui { @@ -33,6 +34,8 @@ namespace Mono.CSharp.Gui public class InteractiveGraphicsBase : Mono.CSharp.InteractiveBase { + static internal List<RenderHandler> type_handlers = new List<RenderHandler> (); + public delegate double DoubleFunc (double a); static int width = 400; static int height = 350; @@ -197,5 +200,40 @@ namespace Mono.CSharp.Gui ly = y; } } + + public delegate Gtk.Widget RenderHandler (object o); + + public static void RegisterRenderHandler (RenderHandler o) + { + if (o == null) + throw new ArgumentException ("parameter is null"); + + if (type_handlers.Contains (o)) + return; + + type_handlers.Insert (0, o); + } + + public static void UnregisterRenderHandler (RenderHandler o) + { + if (o == null) + return; + type_handlers.Remove (o); + } + + // Whether this is an attached program or not. + + static bool attached; + public static bool Attached { + get { return attached; } + internal set { attached = value; } + } + + static Gtk.Widget main_window; + // A handle to our main window. + public static Gtk.Widget MainWindow { + get { return main_window; } + internal set { main_window = value; } + } } } diff --git a/gsharp/Main.cs b/gsharp/Main.cs index 65d0ca44..8a06e770 100644 --- a/gsharp/Main.cs +++ b/gsharp/Main.cs @@ -32,7 +32,8 @@ namespace Mono.CSharp.Gui Gtk.Application.Invoke (delegate { handle.Set (); }); HostHasGtkRunning = handle.WaitOne (3000, true); - + + InteractiveGraphicsBase.Attached = true; Gtk.Application.Invoke (delegate { try { Evaluator.Init (new string [0]); @@ -63,13 +64,27 @@ namespace Mono.CSharp.Gui { Evaluator.ReferenceAssembly (e.LoadedAssembly); } - + + internal static Gtk.Widget RenderBitmaps (object o) + { + System.Drawing.Bitmap bitmap = o as System.Drawing.Bitmap; + if (bitmap == null) + return null; + + return new BitmapWidget (bitmap); + } + public static void Start (string title) { if (!HostHasGtkRunning) Application.Init (); + + InteractiveGraphicsBase.RegisterRenderHandler (RenderBitmaps); + MainWindow m = new MainWindow (); + InteractiveGraphicsBase.MainWindow = m; m.Title = title; + m.LoadStartupFiles (); m.ShowAll (); if (!HostHasGtkRunning) Application.Run (); diff --git a/gsharp/MainWindow.cs b/gsharp/MainWindow.cs index 4003f695..cb26b969 100644 --- a/gsharp/MainWindow.cs +++ b/gsharp/MainWindow.cs @@ -48,7 +48,73 @@ namespace Mono.CSharp.Gui p.Destroy (); } - + delegate string ReadLiner (bool primary); + + public void LoadStartupFiles () + { + string dir = System.IO.Path.Combine (Environment.GetFolderPath (Environment.SpecialFolder.ApplicationData), + "gsharp"); + if (!System.IO.Directory.Exists (dir)) + return; + + ArrayList sources = new ArrayList (); + ArrayList libraries = new ArrayList (); + + foreach (string file in System.IO.Directory.GetFiles (dir)){ + string l = file.ToLower (); + + if (l.EndsWith (".cs")) + sources.Add (file); + else if (l.EndsWith (".dll")) + libraries.Add (file); + } + + foreach (string file in libraries){ + Evaluator.LoadAssembly (file); + } + + foreach (string file in sources){ + try { + using (System.IO.StreamReader r = System.IO.File.OpenText (file)){ + ReadEvalPrintLoopWith (p => r.ReadLine ()); + } + } catch { + } + } + } + + string Evaluate (string input) + { + bool result_set; + object result; + + try { + input = Evaluator.Evaluate (input, out result, out result_set); + } catch (Exception e){ + Console.WriteLine (e); + return null; + } + + return input; + } + + void ReadEvalPrintLoopWith (ReadLiner readline) + { + string expr = null; + while (true){ + string input = readline (expr == null); + if (input == null) + return; + + if (input == "") + continue; + + expr = expr == null ? input : expr + "\n" + input; + + expr = Evaluate (expr); + } + } + public MainWindow() : base(Gtk.WindowType.Toplevel) { this.Build(); diff --git a/gsharp/Shell.cs b/gsharp/Shell.cs index 00a5945f..d6f2bd2c 100644 --- a/gsharp/Shell.cs +++ b/gsharp/Shell.cs @@ -40,6 +40,7 @@ using System.IO; using Gtk; using Mono.CSharp; using System.ComponentModel; +using System.Runtime.InteropServices; namespace Mono.CSharp.Gui { @@ -51,6 +52,7 @@ namespace Mono.CSharp.Gui ArrayList history = new ArrayList (); int history_cursor, new_top; + TextWriter gui_output; public Shell() : base() { @@ -70,6 +72,10 @@ namespace Mono.CSharp.Gui Evaluator.Run ("LoadAssembly (\"System.Drawing\");"); history.Add (""); + + gui_output = new StreamWriter (new GuiStream (this)); + //Console.SetError (gui_output); + //Console.SetOut (gui_output); } void CreateTags () @@ -115,7 +121,7 @@ namespace Mono.CSharp.Gui // Returns true if the line is complete, so that the line can be entered // into the history, false if this was partial // - bool Evaluate (string s) + public bool Evaluate (string s) { string res = null; object result; @@ -271,25 +277,34 @@ namespace Mono.CSharp.Gui prompt_start_iter, prompt_end_iter); } + public void WriteGui (string s) + { + ShowResult (s); + gui_output.Flush (); + } + public void ShowResult (object res) { TextIter end = Buffer.EndIter; Buffer.InsertWithTagsByName (ref end, "\n", "Stdout"); - System.Drawing.Bitmap bitmap = res as System.Drawing.Bitmap; - if (bitmap != null){ - TextChildAnchor anchor = Buffer.CreateChildAnchor (ref end); - BitmapWidget bw = new BitmapWidget (bitmap); - bw.Show (); - - AddChildAtAnchor (bw, anchor); - - return; + foreach (var render_handler in InteractiveGraphicsBase.type_handlers){ + Gtk.Widget w = render_handler (res); + if (w != null){ + TextChildAnchor anchor = Buffer.CreateChildAnchor (ref end); + w.Show (); + AddChildAtAnchor (w, anchor); + return; + } } StringWriter pretty = new StringWriter (); - PrettyPrint (pretty, res); + try { + PrettyPrint (pretty, res); + } catch (Exception e) { + Console.WriteLine (e); + } Buffer.InsertWithTagsByName (ref end, pretty.ToString (), "Stdout"); } @@ -445,9 +460,17 @@ namespace Mono.CSharp.Gui foreach (DictionaryEntry entry in dict){ count++; p (output, "{ "); - PrettyPrint (output, entry.Key); + try { + PrettyPrint (output, entry.Key); + } catch { + p (output, "<error>"); + } p (output, ", "); - PrettyPrint (output, entry.Value); + try { + PrettyPrint (output, entry.Value); + } catch { + p (output, "<error>"); + } if (count != top) p (output, " }, "); else @@ -457,19 +480,54 @@ namespace Mono.CSharp.Gui } else if (result is IEnumerable) { int i = 0; p (output, "{ "); - foreach (object item in (IEnumerable) result) { - if (i++ != 0) - p (output, ", "); - - PrettyPrint (output, item); + try { + foreach (object item in (IEnumerable) result) { + if (i++ != 0) + p (output, ", "); + + PrettyPrint (output, item); + } + } catch { + p (output, "<error>"); } p (output, " }"); } else if (result is char){ EscapeChar (output, (char) result); } else { - p (output, result.ToString ()); + try { + p (output, result.ToString ()); + } catch { + p (output, "<error>"); + } } } + } + + public class GuiStream : Stream { + Shell shell; + + public GuiStream (Shell s) + { + shell = s; + } + + public override bool CanRead { get { throw new Exception (); return false; } } + public override bool CanSeek { get { return false; } } + public override bool CanWrite { get { return true; } } + + + public override long Length { get { return 0; } } + public override long Position { get { return 0; } set {} } + public override void Flush () { Console.WriteLine ("Flush"); } + public override int Read ([In,Out] byte[] buffer, int offset, int count) { return -1; } + public override long Seek (long offset, SeekOrigin origin) { Console.WriteLine ("Fuck"); return 0; } + + public override void SetLength (long value) { } + + public override void Write (byte[] buffer, int offset, int count) { + Console.WriteLine ("WIRING {0}",Encoding.UTF8.GetString (buffer, offset, count)); + shell.ShowResult (Encoding.UTF8.GetString (buffer, offset, count)); + } } } |