/// <summary> /// Adds/updates a tab page with the provided name and the provided component. /// </summary> /// <param name="name">Name of the tab page to add or update</param> /// <param name="component">Component to add or update</param> /// <param name="swap">Whether to swap to the new component</param> public void AddTabComponent(string name, object component, bool swap = true) { string html = (string)component; string fontSize = Properties.Settings.Default.FontSize.ToString(); string falseColor = Properties.Settings.Default.Colorblind ? "royalblue" : "green"; string commentDisplay = Properties.Settings.Default.SimulationComments ? "inline" : "none"; string semicolonDisplay = Properties.Settings.Default.OutputSemicolons ? "inline" : "none"; html = html.Replace("@TRUECOLOR@", "crimson").Replace("@FALSECOLOR@", falseColor).Replace("@FONTSIZE@", fontSize); html = html.Replace("@COMMENTDISPLAY@", commentDisplay).Replace("@SEMICOLONDISPLAY@", semicolonDisplay); TabPage existingTabPage = null; foreach (TabPage tabPage in TabControl.TabPages) { if (tabPage.Text == name) { existingTabPage = tabPage; break; } } if (existingTabPage == null) { TabPage newTabPage = new TabPage(name); newTabPage.Text = name; newTabPage.ToolTipText = name; // Init browser WebBrowser browser = new WebBrowser(); browser.IsWebBrowserContextMenuEnabled = false; browser.AllowWebBrowserDrop = false; browser.WebBrowserShortcutsEnabled = false; browser.ObjectForScripting = Controller; browser.ScriptErrorsSuppressed = true; // Create browser with empty body //browser.DocumentText = CreateDocumentText(html); string metaTag = "<meta name=\"viewport\" content=\"initial-scale=1.0, user-scalable=no\"/>"; string scriptTag = "<script>function KeyPress(e) { var evtobj = window.event? event : e; if (evtobj.ctrlKey &&" + "(evtobj.keyCode == 187 || evtobj.keyCode == 189 || evtobj.keyCode == 107 || evtobj.keyCode == 109" + "|| evtobj.keyCode == 61 || evtobj.keyCode == 173)) { e.preventDefault(); } }</script>"; string styleTag = "<style type=\"text/css\"> p { margin: 0; font-family: consolas; } span { margin: 0; font-family: consolas; }</style>"; browser.DocumentText = $"<head>{metaTag}{styleTag}{scriptTag}</head><body>{html}</body>"; browser.PreviewKeyDown += (sender, eventArgs) => { if (eventArgs.Control) { if (eventArgs.KeyCode == Keys.E) { CloseTabs(); } else if (eventArgs.KeyCode == Keys.Add || eventArgs.KeyCode == Keys.Oemplus) { Properties.Settings.Default.FontSize += 2; Controller.RefreshOutput(); } else if (eventArgs.KeyCode == Keys.Subtract || eventArgs.KeyCode == Keys.OemMinus) { if (Properties.Settings.Default.FontSize > 9) { Properties.Settings.Default.FontSize -= 2; Controller.RefreshOutput(); } } } }; browser.DocumentCompleted += (sender, eventArgs) => { browser.Document.Body.DoubleClick += (sender2, eventArgs2) => { Controller.RetrieveFocus(); }; }; Panel browserPanel = new Panel(); browserPanel.BorderStyle = BorderStyle.Fixed3D; browserPanel.Controls.Add(browser); browser.Dock = DockStyle.Fill; newTabPage.Controls.Add(browserPanel); browserPanel.Dock = DockStyle.Fill; TabControl.TabPages.Add(newTabPage); if (swap) { TabControl.SelectedTab = newTabPage; UpdateTickControls(!name.Contains(".")); } } else { WebBrowser browser = ((WebBrowser)existingTabPage.Controls[0].Controls[0]); browser.Document.Body.InnerHtml = html; if (swap) { TabControl.SelectedTab = existingTabPage; } } pnlMain.Focus(); }