diff --git a/AutoUpdater.xml b/AutoUpdater.xml index 4d627bd..08b13d2 100644 --- a/AutoUpdater.xml +++ b/AutoUpdater.xml @@ -1,6 +1,6 @@ - 1.2.1.0 + 1.2.1.1 https://github.com/PocketMiner82/pseudocode-ide/releases/latest/download/pseudocode-ide.zip https://github.com/PocketMiner82/pseudocode-ide/releases false diff --git a/SolutionInfo.cs b/SolutionInfo.cs index 89d88ce..9fb576c 100644 --- a/SolutionInfo.cs +++ b/SolutionInfo.cs @@ -1,4 +1,4 @@ using System.Reflection; -[assembly: AssemblyVersion("1.2.1.0")] -[assembly: AssemblyFileVersion("1.2.1.0")] +[assembly: AssemblyVersion("1.2.1.1")] +[assembly: AssemblyFileVersion("1.2.1.1")] diff --git a/pseudocodeIde/SetClipboardHelper.cs b/pseudocodeIde/SetClipboardHelper.cs new file mode 100644 index 0000000..e0ce4f3 --- /dev/null +++ b/pseudocodeIde/SetClipboardHelper.cs @@ -0,0 +1,87 @@ +using System; +using System.Collections.Generic; +using System.Diagnostics; +using System.Linq; +using System.Text; +using System.Threading; +using System.Threading.Tasks; +using System.Windows.Forms; + +namespace pseudocode_ide +{ + // from https://stackoverflow.com/questions/899350/how-do-i-copy-the-contents-of-a-string-to-the-clipboard-in-c + + public class SetClipboardHelper : StaHelper + { + readonly string _format; + readonly object _data; + + public SetClipboardHelper(string format, object data) + { + _format = format; + _data = data; + } + + protected override void Work() + { + var obj = new System.Windows.Forms.DataObject( + _format, + _data + ); + + Clipboard.SetDataObject(obj, true); + } + } + + public abstract class StaHelper + { + readonly ManualResetEvent _complete = new ManualResetEvent(false); + + public void Go() + { + var thread = new Thread(new ThreadStart(DoWork)) + { + IsBackground = true, + }; + thread.SetApartmentState(ApartmentState.STA); + thread.Start(); + } + + // Thread entry method + private void DoWork() + { + try + { + _complete.Reset(); + Work(); + } + catch (Exception ex) + { + if (DontRetryWorkOnFailed) + throw; + else + { + try + { + Thread.Sleep(1000); + Work(); + } + catch + { + // ex from first exception + Debug.WriteLine(ex); + } + } + } + finally + { + _complete.Set(); + } + } + + public bool DontRetryWorkOnFailed { get; set; } + + // Implemented in base class to do actual work. + protected abstract void Work(); + } +} diff --git a/pseudocodeIde/interpreter/Interpreter.cs b/pseudocodeIde/interpreter/Interpreter.cs index 87e3a03..455ac46 100644 --- a/pseudocodeIde/interpreter/Interpreter.cs +++ b/pseudocodeIde/interpreter/Interpreter.cs @@ -16,6 +16,8 @@ public class Interpreter private Thread programThread = null; + public CSharpCode cSharpCode { get; private set; } = null; + private string code { get @@ -67,14 +69,15 @@ private bool tryRun() Logger.info($"Generierte Tokens:\n{tokensString}\n\n"); Parser parser = new Parser(tokens); - CSharpCode cSharpCode = parser.parseTokens(); + this.cSharpCode = parser.parseTokens(); + outputForm.showCopyButton(); if (this.cancelRequestedOrError()) { return false; } - cSharpCode.compile(); + this.cSharpCode.compile(); if (this.cancelRequestedOrError()) { @@ -84,7 +87,7 @@ private bool tryRun() Logger.info(LogMessage.RUNNING_PROGRAM); Logger.print(""); - this.programThread = new Thread(cSharpCode.execute); + this.programThread = new Thread(this.cSharpCode.execute); this.programThread.Start(); return true; } diff --git a/pseudocodeIde/interpreter/OutputForm.Designer.cs b/pseudocodeIde/interpreter/OutputForm.Designer.cs index 335f3bb..8fcaac0 100644 --- a/pseudocodeIde/interpreter/OutputForm.Designer.cs +++ b/pseudocodeIde/interpreter/OutputForm.Designer.cs @@ -31,21 +31,23 @@ private void InitializeComponent() this.menuStrip = new System.Windows.Forms.MenuStrip(); this.startMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.stopMenuItem = new System.Windows.Forms.ToolStripMenuItem(); + this.copyCSharpCodeMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.rtbOutput = new System.Windows.Forms.RichTextBox(); this.menuStrip.SuspendLayout(); this.SuspendLayout(); // // menuStrip // - this.menuStrip.GripMargin = new System.Windows.Forms.Padding(2, 2, 0, 2); this.menuStrip.ImageScalingSize = new System.Drawing.Size(24, 24); this.menuStrip.Items.AddRange(new System.Windows.Forms.ToolStripItem[] { this.startMenuItem, - this.stopMenuItem}); + this.stopMenuItem, + this.copyCSharpCodeMenuItem}); this.menuStrip.Location = new System.Drawing.Point(0, 0); this.menuStrip.Name = "menuStrip"; + this.menuStrip.Padding = new System.Windows.Forms.Padding(4, 1, 0, 1); this.menuStrip.ShowItemToolTips = true; - this.menuStrip.Size = new System.Drawing.Size(1176, 33); + this.menuStrip.Size = new System.Drawing.Size(784, 24); this.menuStrip.TabIndex = 0; this.menuStrip.Text = "menuStrip1"; // @@ -55,7 +57,7 @@ private void InitializeComponent() this.startMenuItem.ForeColor = System.Drawing.Color.Green; this.startMenuItem.Name = "startMenuItem"; this.startMenuItem.ShortcutKeys = System.Windows.Forms.Keys.F5; - this.startMenuItem.Size = new System.Drawing.Size(64, 29); + this.startMenuItem.Size = new System.Drawing.Size(43, 22); this.startMenuItem.Text = "Start"; this.startMenuItem.ToolTipText = "Start (F5)"; this.startMenuItem.Click += new System.EventHandler(this.startMenuItem_Click); @@ -67,34 +69,40 @@ private void InitializeComponent() this.stopMenuItem.ForeColor = System.Drawing.Color.Red; this.stopMenuItem.Name = "stopMenuItem"; this.stopMenuItem.ShortcutKeys = ((System.Windows.Forms.Keys)((System.Windows.Forms.Keys.Shift | System.Windows.Forms.Keys.F5))); - this.stopMenuItem.Size = new System.Drawing.Size(65, 29); + this.stopMenuItem.Size = new System.Drawing.Size(43, 22); this.stopMenuItem.Text = "Stop"; this.stopMenuItem.ToolTipText = "Stop (Shift+F5)"; this.stopMenuItem.Click += new System.EventHandler(this.stopMenuItem_Click); // + // copyCSharpCodeMenuItem + // + this.copyCSharpCodeMenuItem.Name = "copyCSharpCodeMenuItem"; + this.copyCSharpCodeMenuItem.Size = new System.Drawing.Size(114, 22); + this.copyCSharpCodeMenuItem.Text = "C# Code kopieren"; + this.copyCSharpCodeMenuItem.Visible = false; + this.copyCSharpCodeMenuItem.Click += new System.EventHandler(this.copyCSharpCodeMenuItem_Click); + // // rtbOutput // this.rtbOutput.Dock = System.Windows.Forms.DockStyle.Fill; this.rtbOutput.Font = new System.Drawing.Font("Courier New", 11.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0))); - this.rtbOutput.Location = new System.Drawing.Point(0, 33); - this.rtbOutput.Margin = new System.Windows.Forms.Padding(4, 5, 4, 5); + this.rtbOutput.Location = new System.Drawing.Point(0, 24); this.rtbOutput.Name = "rtbOutput"; this.rtbOutput.ReadOnly = true; - this.rtbOutput.Size = new System.Drawing.Size(1176, 676); + this.rtbOutput.Size = new System.Drawing.Size(784, 437); this.rtbOutput.TabIndex = 1; this.rtbOutput.Text = ""; this.rtbOutput.WordWrap = false; // // OutputForm // - this.AutoScaleDimensions = new System.Drawing.SizeF(9F, 20F); + this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; - this.ClientSize = new System.Drawing.Size(1176, 709); + this.ClientSize = new System.Drawing.Size(784, 461); this.Controls.Add(this.rtbOutput); this.Controls.Add(this.menuStrip); this.MainMenuStrip = this.menuStrip; - this.Margin = new System.Windows.Forms.Padding(4, 5, 4, 5); - this.MinimumSize = new System.Drawing.Size(514, 354); + this.MinimumSize = new System.Drawing.Size(348, 244); this.Name = "OutputForm"; this.ShowIcon = false; this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen; @@ -113,5 +121,6 @@ private void InitializeComponent() private System.Windows.Forms.ToolStripMenuItem startMenuItem; private System.Windows.Forms.ToolStripMenuItem stopMenuItem; private System.Windows.Forms.RichTextBox rtbOutput; + private System.Windows.Forms.ToolStripMenuItem copyCSharpCodeMenuItem; } } \ No newline at end of file diff --git a/pseudocodeIde/interpreter/OutputForm.cs b/pseudocodeIde/interpreter/OutputForm.cs index b0e9016..28c15ad 100644 --- a/pseudocodeIde/interpreter/OutputForm.cs +++ b/pseudocodeIde/interpreter/OutputForm.cs @@ -1,4 +1,5 @@ -using pseudocodeIde.interpreter; +using pseudocode_ide; +using pseudocodeIde.interpreter; using pseudocodeIde.interpreter.logging; using System; using System.Threading; @@ -93,6 +94,7 @@ public void ShowAndRun() private void startMenuItem_Click(object sender, EventArgs e) { + copyCSharpCodeMenuItem.Visible = false; this.stopMenuItem_Click(null, null); startMenuItem.Enabled = false; @@ -123,5 +125,19 @@ public void stopMenuItem_Click(object sender, EventArgs e) Logger.print(""); Logger.info(LogMessage.STOPPED_PROGRAM); } + + private void copyCSharpCodeMenuItem_Click(object sender, EventArgs e) + { + new SetClipboardHelper(DataFormats.UnicodeText, this.interpreter.cSharpCode.codeText).Go(); + MessageBox.Show("C# Code in Zwischenablage gespeichert.", "Zwischenablage"); + } + + public void showCopyButton() + { + Invoke(new Action(() => + { + copyCSharpCodeMenuItem.Visible = true; + })); + } } } diff --git a/pseudocodeIde/interpreter/parser/CSharpCode.cs b/pseudocodeIde/interpreter/parser/CSharpCode.cs index ece6b11..64e51eb 100644 --- a/pseudocodeIde/interpreter/parser/CSharpCode.cs +++ b/pseudocodeIde/interpreter/parser/CSharpCode.cs @@ -98,6 +98,8 @@ public CodeOutput(Action printMethod) : base(printMethod) { public string methods { get; set; } = ""; + public string codeText { get; private set; } = ""; + private Assembly compiledAssembly; @@ -122,20 +124,20 @@ public void compile() parameters.CompilerOptions += "/unsafe /optimize /langversion:9.0"; - string code = TEMPLATE_CLASS + this.codeText = TEMPLATE_CLASS .Replace("%FIELDS%", this.fields) .Replace("%CONSTRUCTOR%", this.constructor) .Replace("%METHODS%", this.methods); // pretty print code - SyntaxNode node = CSharpSyntaxTree.ParseText(code).GetRoot(); - code = node.NormalizeWhitespace().ToFullString(); + SyntaxNode node = CSharpSyntaxTree.ParseText(this.codeText).GetRoot(); + this.codeText = node.NormalizeWhitespace().ToFullString(); Logger.info(LogMessage.GENERATED_C_SHARP_CODE); string printCode = "1\t"; int line = 1; - foreach(char c in code) + foreach(char c in this.codeText) { if (c == '\n') { @@ -150,7 +152,7 @@ public void compile() Logger.info(LogMessage.COMPILING_C_SHARP_CODE); - CompilerResults result = provider.CompileAssemblyFromSource(parameters, code); + CompilerResults result = provider.CompileAssemblyFromSource(parameters, this.codeText); if (result.Errors.Count > 0) { diff --git a/pseudocodeIde/pseudocodeIde.csproj b/pseudocodeIde/pseudocodeIde.csproj index 3a93e3e..04b5839 100644 --- a/pseudocodeIde/pseudocodeIde.csproj +++ b/pseudocodeIde/pseudocodeIde.csproj @@ -154,6 +154,7 @@ OutputForm.cs + FindReplaceForm.cs