public static void SetActiveFileToNext() { ScriptDocument doc = ActiveDocument; if (null == doc) { if (m_docs.Count > 0) { SetActiveDoc(m_docs[0].Id); } return; } for (int i = 0; i < m_docs.Count; i++) { if (m_docs[i].Id == m_active_doc_id) { Guid id = Guid.Empty; if (i < (m_docs.Count - 1)) { id = m_docs[i + 1].Id; } else { id = m_docs[0].Id; } if (id != Guid.Empty) { SetActiveDoc(id); } break; } } }
/// <summary>Create a new script file </summary> /// <param name="initialText"></param> /// <param name="setAsActive">make the new script the "active" document</param> /// <returns>id of the new script on success</returns> public static Guid NewDocument(string initialText, bool setAsActive) { Guid doc_to_close = Guid.Empty; // Look at the first file to see if it is a completely empty "untitled" script. // If so, close this file if (m_docs.Count == 1) { ScriptDocument old_doc = m_docs[0]; if (old_doc.IsUntitledAndEmpty) doc_to_close = old_doc.Id; } ScriptDocument doc = new ScriptDocument(initialText); // Insert the document at the beginning of the list instead of // appending it to the end. This will cause the file to "look" like // the topmost document in the script editor m_docs.Insert(0, doc); DocumentEvents.FireNewDocument(doc.Id); if (setAsActive) SetActiveDoc(doc.Id); if( doc_to_close != Guid.Empty ) CloseDocument(doc_to_close); return doc.Id; }
public static void SetActiveDoc(Guid id) { Guid old_active_id = m_active_doc_id; // Guid.Empty sets active file to nothing ScriptDocument oldActive = DocumentFromId(old_active_id); ScriptDocument newActive = null; for (int i = 0; i < m_docs.Count; i++) { if (m_docs[i].Id == id) { newActive = m_docs[i]; break; } } if (null == newActive) { return; } m_active_doc_id = id; if (old_active_id != m_active_doc_id) { DocumentEvents.FireSetActiveDocument(m_active_doc_id); } }
/// <summary> /// This function does NOT save the script if it has been modified. /// </summary> /// <param name="scriptId"></param> public static void CloseDocument(Guid scriptId) { int index = IndexFromId(scriptId); if (index < 0) { return; } ScriptDocument doc = m_docs[index]; if (null == doc) { return; } m_docs.RemoveAt(index); DocumentEvents.FireCloseDocument(scriptId); if (m_active_doc_id == doc.Id) { m_active_doc_id = Guid.Empty; if (m_docs.Count == 0) { NewDocument("", false); } if (m_docs.Count > 0) { SetActiveDoc(m_docs[0].Id); } } }
/// <summary> /// creates a new file which already exists on disk /// </summary> /// <param name="path">Path of file to load</param> public static ScriptDocument Load(string path) { if (string.IsNullOrEmpty(path) || !System.IO.File.Exists(path)) return null; string text = System.IO.File.ReadAllText(path); ScriptDocument doc = new ScriptDocument(text); doc.m_path = path; return doc; }
public static Guid OpenDocument(string path, bool setActive) { if (string.IsNullOrEmpty(path)) { return(Guid.Empty); } // See if the file is already opened. If it is then just make is active for (int i = 0; i < m_docs.Count; i++) { if (string.Compare(m_docs[i].FullPath, path, StringComparison.InvariantCultureIgnoreCase) == 0) { Guid rc = m_docs[i].Id; SetActiveDoc(rc); return(rc); } } ScriptDocument doc = ScriptDocument.Load(path); if (null == doc) { return(Guid.Empty); } // If the active file is an "unnamed" document that has not been modified, // close this file Guid scriptToClose = Guid.Empty; if (m_docs.Count == 1 && m_docs[0].IsUntitledAndEmpty) { scriptToClose = m_docs[0].Id; } // Insert the document at the beginning of the list instead of // appending it to the end. This will cause the file to "look" like // the topmost document in the script editor m_docs.Insert(0, doc); DocumentEvents.FireOpenDocument(doc.Id); if (scriptToClose != Guid.Empty) { CloseDocument(scriptToClose); } if (setActive) { SetActiveDoc(doc.Id); } return(doc.Id); }
/// <summary> /// creates a new file which already exists on disk /// </summary> /// <param name="path">Path of file to load</param> public static ScriptDocument Load(string path) { if (string.IsNullOrEmpty(path) || !System.IO.File.Exists(path)) { return(null); } string text = System.IO.File.ReadAllText(path); ScriptDocument doc = new ScriptDocument(text); doc.m_path = path; return(doc); }
public static void SetDocumentModified(Guid document_id, bool modified) { for (int i = 0; i < m_docs.Count; i++) { ScriptDocument doc = m_docs[i]; if (doc.Id == document_id) { if (doc.Modified != modified) { doc.Modified = modified; DocumentEvents.FireDocumentModifiedStateChange(document_id); } break; } } }
public EditorTabPage(ScriptDocument doc, Action<string> help_cb) { InitializeComponent(); m_help_callback = help_cb; m_document_id = doc.Id; //Set editor defaults this.ShowSpaces = true; this.TabIndent = 4; this.ShowTabs = true; this.ConvertTabsToSpaces = true; this.SetHighlighting("Python"); this.Dock = System.Windows.Forms.DockStyle.Fill; this.IsIconBarVisible = true; if (!string.IsNullOrEmpty(doc.FullPath)) LoadFile(doc.FullPath, false, true); else this.Text = doc.SourceCode; doc.SetDelegates(this.GetEditorText, this.SetEditorText, this.GetBreakPointLines); // test for custom colors ICSharpCode.TextEditor.Document.DefaultHighlightingStrategy strat = this.Document.HighlightingStrategy as ICSharpCode.TextEditor.Document.DefaultHighlightingStrategy; if (strat != null) strat.SetColorFor("SpaceMarkers", new ICSharpCode.TextEditor.Document.HighlightColor(System.Drawing.Color.BurlyWood, true, false)); this.Document.FoldingManager.FoldingStrategy = new PythonFoldingStrategy(); this.Document.FormattingStrategy = new PythonFormattingStrategy(); this.Document.FoldingManager.UpdateFoldings(null, null); // Wire up events for this TextEditor this.Document.LineCountChanged += new EventHandler<ICSharpCode.TextEditor.Document.LineCountChangeEventArgs>(Document_LineCountChanged); this.Document.DocumentChanged += new ICSharpCode.TextEditor.Document.DocumentEventHandler(OnDocumentChanged); this.ActiveTextAreaControl.TextArea.IconBarMargin.MouseDown += new ICSharpCode.TextEditor.MarginMouseEventHandler(IconBarMargin_MouseDown); //this.ActiveTextAreaControl.TextArea.DoProcessDialogKey += new ICSharpCode.TextEditor.DialogKeyProcessor(TextArea_DoProcessDialogKey); this.ActiveTextAreaControl.TextArea.KeyEventHandler += TextAreaKeyEventHandler; this.Disposed += CloseCodeCompletionWindow; this.ActiveTextAreaControl.TextArea.IconBarMargin.Painted += new ICSharpCode.TextEditor.MarginPaintEventHandler(IconBarMargin_Painted); }
/// <summary>Create a new script file </summary> /// <param name="initialText"></param> /// <param name="setAsActive">make the new script the "active" document</param> /// <returns>id of the new script on success</returns> public static Guid NewDocument(string initialText, bool setAsActive) { Guid doc_to_close = Guid.Empty; // Look at the first file to see if it is a completely empty "untitled" script. // If so, close this file if (m_docs.Count == 1) { ScriptDocument old_doc = m_docs[0]; if (old_doc.IsUntitledAndEmpty) { doc_to_close = old_doc.Id; } } ScriptDocument doc = new ScriptDocument(initialText); // Insert the document at the beginning of the list instead of // appending it to the end. This will cause the file to "look" like // the topmost document in the script editor m_docs.Insert(0, doc); DocumentEvents.FireNewDocument(doc.Id); if (setAsActive) { SetActiveDoc(doc.Id); } if (doc_to_close != Guid.Empty) { CloseDocument(doc_to_close); } return(doc.Id); }
public static void ScriptCloseEventHelper(Guid scriptId) { ScriptDocument doc = DocumentFromId(scriptId); if (null == doc) { return; } if (doc.Modified) { string question = "Save changes to " + doc.DisplayName + "?"; DialogResult rc = MessageBox.Show(question, "Save Changes?", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question); if (rc == DialogResult.Cancel) { return; } if (rc == DialogResult.Yes) { SaveDocument(scriptId, false); } } CloseDocument(scriptId); }
public static void SaveDocument(Guid scriptId, bool showSaveDialog) { ScriptDocument doc = DocumentFromId(scriptId); if (null == doc) { return; } string path = doc.FullPath; if (string.IsNullOrEmpty(path)) { showSaveDialog = true; } if (showSaveDialog) { System.Windows.Forms.SaveFileDialog fd = new System.Windows.Forms.SaveFileDialog(); fd.Title = "Save Script"; fd.Filter = "python scripts (*.py)|*.py|" + "All files (*.*)|*.*"; if (!string.IsNullOrEmpty(m_save_directory)) { fd.InitialDirectory = m_save_directory; } // 01 June 2010 S. Baer - RR64956 // Use the path to the existing script if it already exists if (!string.IsNullOrEmpty(path)) { string dir = System.IO.Path.GetDirectoryName(path); if (System.IO.Directory.Exists(dir)) { fd.InitialDirectory = dir; } string file = System.IO.Path.GetFileName(path); if (!string.IsNullOrEmpty(file)) { fd.FileName = file; } } if (fd.ShowDialog() != System.Windows.Forms.DialogResult.OK) { return; } path = fd.FileName; } if (string.IsNullOrEmpty(path)) { return; } doc.FullPath = path; doc.Save(false); // 01 June 2010 S. Baer RR67329 // Update the save directory so the SaveAs dialog will start in the last saved location string saveAsDir = System.IO.Path.GetDirectoryName(path); if (!string.IsNullOrEmpty(saveAsDir) && System.IO.Directory.Exists(saveAsDir)) { m_save_directory = saveAsDir; } }
/// <summary> /// Return true to handle the keypress, return false to let the text area handle the keypress /// </summary> bool TextAreaKeyEventHandler(char key) { if (m_code_complete != null) { // If completion window is open and wants to handle the key, don't let the text area // handle it if (m_code_complete.ProcessKeyEvent(key)) { return(true); } } ICSharpCode.TextEditor.Gui.CompletionWindow.ICompletionDataProvider completionDataProvider = CodeComplete.GetCompletionProvider(this, key); if (completionDataProvider == null && key == '(') { ICSharpCode.TextEditor.Document.LineSegment line = this.Document.GetLineSegment(ActiveTextAreaControl.Caret.Line); if (null == line) { return(false); } List <ICSharpCode.TextEditor.Document.TextWord> words = line.Words; if (words == null || words.Count < 1) { return(false); } ICSharpCode.TextEditor.Document.TextWord lastWord = words[words.Count - 1]; if (lastWord.Type != ICSharpCode.TextEditor.Document.TextWordType.Word) { return(false); } // Todo: Remove hard coded colors if (lastWord.Color == System.Drawing.Color.Green || lastWord.Color == System.Drawing.Color.Red) { return(false); } // make sure first real word on this line is not "def" for (int i = 0; i < words.Count; i++) { if (words[i].Type != ICSharpCode.TextEditor.Document.TextWordType.Word) { continue; } if (words[i].Word.Equals("def", StringComparison.Ordinal)) { return(false); } break; } char c = lastWord.Word[0]; if (char.IsLetter(c)) { // Build up a python script to execute int line_number = this.ActiveTextAreaControl.TextArea.Caret.Line; StringBuilder script = new StringBuilder(); for (int i = 0; i < line_number; i++) { line = this.ActiveTextAreaControl.TextArea.Document.GetLineSegment(i); if (line != null && line.Words.Count > 2) { string firstword = line.Words[0].Word; if (firstword.Equals("import", StringComparison.Ordinal) || firstword.Equals("from")) { script.AppendLine(this.ActiveTextAreaControl.TextArea.Document.GetText(line)); } } } ICSharpCode.TextEditor.Document.LineSegment lastLine = this.ActiveTextAreaControl.TextArea.Document.GetLineSegment(line_number); if (null == lastLine) { return(false); } //walk backward through the line until we hit something that is NOT a word or period string evaluation = ""; for (int i = lastLine.Words.Count - 1; i >= 0; i--) { ICSharpCode.TextEditor.Document.TextWord word = lastLine.Words[i]; if (word.Type != ICSharpCode.TextEditor.Document.TextWordType.Word) { break; } c = word.Word[0]; if (c != '.' && !char.IsLetter(c)) { break; } evaluation = evaluation.Insert(0, word.Word); } if (evaluation != "if" && evaluation != "while") { RhinoDLR_Python.Intellisense isense = m_intellisense; if (isense == null) { isense = CodeComplete.Intellisense; } string rc = isense.EvaluateHelp(script.ToString(), evaluation); if (!string.IsNullOrEmpty(rc) && null != m_help_callback) { m_help_callback(rc); } } } } else if (completionDataProvider != null) { ScriptEditor.Model.ScriptDocument doc = ScriptEditor.Model.Documents.DocumentFromId(DocumentId); string path = "none"; if (doc != null && !string.IsNullOrEmpty(doc.FullPath)) { path = doc.FullPath; } m_code_complete = ICSharpCode.TextEditor.Gui.CompletionWindow.CodeCompletionWindow.ShowCompletionWindow( m_mainform, // The parent window for the completion window this, // The text editor to show the window for path, // Filename - will be passed back to the provider completionDataProvider, // Provider to get the list of possible completions key // Key pressed - will be passed to the provider ); if (m_code_complete != null) { // ShowCompletionWindow can return null when the provider returns an empty list m_code_complete.Closed += new EventHandler(CloseCodeCompletionWindow); } } return(false); }