Example #1
0
        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);
            }
        }
Example #2
0
        /// <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);
                }
            }
        }
Example #3
0
        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);
        }
Example #4
0
        public bool Save(bool onlyIfModified)
        {
            string script = SourceCode;

            if (string.IsNullOrEmpty(script))
            {
                return(false);
            }

            // prompt the user if saving an undefined file
            string path = FullPath;

            if (string.IsNullOrEmpty(path))
            {
                return(false);
            }

            string dir = System.IO.Path.GetDirectoryName(path);

            if (!System.IO.Directory.Exists(dir))
            {
                return(false);
            }

            if (onlyIfModified && !Modified)
            {
                return(false);
            }

            bool rc = false;

            try
            {
                System.IO.File.WriteAllText(path, script);
                rc = true;
            }
            catch (Exception)
            {
            }

            if (rc)
            {
                bool old_modified = Modified;
                if (old_modified == true)
                {
                    Modified = false;
                    DocumentEvents.FireDocumentModifiedStateChange(Id);
                }
                DocumentEvents.FireSaveDocument(Id);
            }
            return(rc);
        }
Example #5
0
 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;
         }
     }
 }
Example #6
0
        /// <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);
        }