public void NewFile(string templateName)
        {
            m_fileNum++;

            string newFileTitle = string.Format("<untitled> {0}", m_fileNum);

            ChameleonEditor editor = new ChameleonEditor();


            editor.ConfigurationManager.Configure(m_cppConfig);

            editor.SetCPPEditorStyles();
            editor.Dock = DockStyle.Fill;

            FATabStripItem tabItem = new FATabStripItem("empty tab title", editor);

            m_editorsToTabs[editor]  = tabItem;
            m_tabsToEditors[tabItem] = editor;
            editor.ParentTab         = tabItem;

            editor.Filename = newFileTitle;

            editor.Zoom = m_currentZoom;

            m_tabStrip.AddTab(tabItem, true);

            if (templateName != "")
            {
                editor.Snippets.InsertSnippet(templateName);
            }
        }
        public ChameleonEditor GetEditorByFilename(string filename)
        {
            ChameleonEditor ed = null;

            foreach (ChameleonEditor editor in m_editorsToTabs.Keys)
            {
                // match  the whole path
                if (editor.Filename == filename)
                {
                    ed = editor;
                }
            }

            if (ed == null)
            {
                foreach (ChameleonEditor editor in m_editorsToTabs.Keys)
                {
                    // match just the filename portion
                    if (editor.FileInfo.Filename.FullName == filename)
                    {
                        ed = editor;
                    }
                }
            }

            return(ed);
        }
Beispiel #3
0
        private void OnEditorModifiedChanged(object sender, EventArgs e)
        {
            ChameleonEditor editor = sender as ChameleonEditor;

            if (m_parentTab != null)
            {
                FATabStripItem tab = m_parentTab;

                string tabText = tab.Title;

                if (editor.Modified)
                {
                    if (!tabText.EndsWith(" *"))
                    {
                        tabText += " *";
                    }
                }
                else
                {
                    if (tabText.EndsWith(" *"))
                    {
                        tabText = tabText.Substring(0, tabText.Length - 2);
                    }
                }

                tab.Title = tabText;
            }

            IsCompiled = false;
        }
        private void SelectEditor(ChameleonEditor editor)
        {
            FATabStripItem tab = m_editorsToTabs[editor];

            m_tabStrip.SelectedItem = tab;
            editor.Focus();
            m_currentEditor = editor;
        }
        private void OnSelectedTabChanged(TabStripItemChangedEventArgs e)
        {
            m_currentEditor = m_tabsToEditors[e.Item];

            if (EditorStatusChanged != null)
            {
                EditorStatusChanged(this, null);
            }
        }
        void OnTabStripMouseHover(object sender, EventArgs e)
        {
            Point          translated = m_tabStrip.PointToClient(Cursor.Position);
            FATabStripItem tab        = m_tabStrip.GetTabItemByPoint(translated);

            if (tab != null)
            {
                ChameleonEditor editor = m_tabsToEditors[tab];
                m_tooltip.SetToolTip(m_tabStrip, editor.Filename);
                m_tooltip.Show(editor.Filename, m_tabStrip);
            }
        }
        void OnTabClosing(TabStripItemClosingEventArgs e)
        {
            if (m_closingTab)
            {
                m_closingTab = false;
                return;
            }

            // we want to handle the closing ourselves
            m_closingTab = true;
            e.Cancel     = true;

            ChameleonEditor editor = m_tabsToEditors[e.Item];

            CloseFile(editor);

            m_closingTab = false;
        }
        public bool CloseFile(ChameleonEditor editor)
        {
            if (editor == null)
            {
                editor = CurrentEditor;
            }

            ModifiedFileResult fileHandled = HandleModifiedFile(editor, ModifiedFileAction.Close);

            if (fileHandled == ModifiedFileResult.Cancel)
            {
                return(false);
            }

            FATabStripItem tab = m_editorsToTabs[editor];

            if (m_tabStrip.Items.Count > 1 || ChameleonForm.AppClosing)
            {
                m_tabStrip.RemoveTab(tab);

                m_editorsToTabs.Remove(editor);
                m_tabsToEditors.Remove(tab);
                tab.Controls.Remove(editor);
                editor.Dispose();
            }
            // closing out the last buffer, reset it to act as a new one
            else
            {
                m_fileNum = 1;

                editor.ResetEditor();

                string newFileTitle = string.Format("<untitled> {0}", m_fileNum);
                editor.Filename = newFileTitle;
            }

            return(true);
        }
        bool OpenSourceFile(FileInformation fileInfo)
        {
            string fileContents   = "";
            string locationPrefix = (fileInfo.Location == FileLocation.Local) ? "(L)" : "(R)";

            ChameleonEditor alreadyOpenedFile = GetEditorByFilename(fileInfo.Filename.GetFullPath());

            if (alreadyOpenedFile != null)
            {
                ModifiedFileResult mfr = HandleModifiedFile(alreadyOpenedFile, ModifiedFileAction.Reload);

                // should only have these two choices returned for the Reload case
                if (mfr == ModifiedFileResult.Cancel)
                {
                    return(false);
                }
                else if (mfr == ModifiedFileResult.NoSave)
                {
                    SelectEditor(alreadyOpenedFile);
                    return(true);
                }
            }

            if (!GetFileContents(fileInfo, ref fileContents))
            {
                return(false);
            }

            ChameleonEditor destinationEditor = null;

            if (alreadyOpenedFile != null)
            {
                destinationEditor = alreadyOpenedFile;
            }
            else
            {
                // current buffer is empty and untouched, so load the file into it
                if (!m_currentEditor.Modified &&
                    !m_currentEditor.HasBeenSaved() &&
                    m_currentEditor.Text.Length == 0)
                {
                    destinationEditor = m_currentEditor;
                }
                // need to create a new buffer for the file;
                else
                {
                    NewFile();
                    FATabStripItem newTab = m_tabStrip.Items[m_tabStrip.Items.Count - 1];
                    destinationEditor = m_tabsToEditors[newTab];
                }
            }

            destinationEditor.LoadFileText(fileInfo, fileContents);
            SelectEditor(destinationEditor);
            destinationEditor.Modified = false;

            if (EditorStatusChanged != null)
            {
                EditorStatusChanged(this, null);
            }

            return(true);
        }
        public void RunCodeRules(ChameleonEditor editor)
        {
            m_ruleManager.ClearErrors();
            editor.ClearErrors();

            string filename         = editor.Filename;
            string originalFilename = filename;
            string tempFile         = "";

            bool isRemote = (editor.FileLocation == FileLocation.Remote);

            if (isRemote)
            {
                tempFile = Path.GetTempFileName();
                File.WriteAllText(tempFile, editor.Text);

                filename = tempFile;
            }

            List <string> files = new List <string>();

            files.Add(originalFilename);


            cmw.DeleteFilesTags(files);

            cmw.AddParserRequestSingleFile(filename);

            while (cmw.Parsing)
            {
                // TODO Get rid of this hack
                Thread.Sleep(50);
            }

            if (isRemote)
            {
                cmw.RenameTaggedFile(tempFile, originalFilename);
                File.Delete(tempFile);
            }


            List <Tag> functions = cmw.GetFunctions(originalFilename, false);


            Range wholeFile = editor.GetRange();

            // run all global rules
            m_ruleManager.ExamineSource(editor, wholeFile, true);


            ANTLRParser parser = Singleton <ANTLRParser> .Instance;


            foreach (Tag fn in functions)
            {
                int fnStart = 0, fnOpen = 0, fnClose = 0;

                if (fn.kind == "prototype")
                {
                    continue;
                }

                if (editor.Context.GetFunctionStartEnd(fn.lineNumber - 1, ref fnStart, ref fnOpen, ref fnClose))
                {
                    Range r = new Range(fnStart, fnClose, editor);

                    // do this once out here, but each rule is responsible for checking
                    // if it worked right
                    parser.SetSource(r.Text, editor.Filename);
                    parser.Parse();

                    // run local rules for each function
                    m_ruleManager.ExamineSource(editor, r, false);
                }
            }
        }
        public bool SaveFile(ChameleonEditor editor, FileLocation suggestedLocation,
                             bool saveAs, bool askLocalRemote)
        {
            bool         doSaveAs;
            FileLocation location = FileLocation.Unknown;

            if (suggestedLocation == FileLocation.Unknown)
            {
                doSaveAs = true;
            }
            else
            {
                doSaveAs = saveAs || !m_currentEditor.HasBeenSaved();
            }

            string filename = "";

            if (doSaveAs && suggestedLocation == FileLocation.Unknown)
            {
                string message = "Where should this file be saved?";

                List <string> buttons = new List <string>();
                buttons.Add("Locally (on the computer you're using)");
                buttons.Add("Remotely (on the server)");
                buttons.Add("Cancel");

                int button = cTaskDialog.ShowCommandBox("Save File Location", message, "",
                                                        string.Join("|", buttons), false);
                location = (FileLocation)button;
            }
            else
            {
                location = suggestedLocation;
            }

            if (location == FileLocation.Unknown)
            {
                return(false);
            }

            if (App.UserSettings.PermittedFeatures.HasFlag(ChameleonFeatures.AutoReformat))
            {
                m_currentEditor.ReformatBuffer();
            }

            string fileContents = m_currentEditor.Text;

LocationSelected:
            if (location == FileLocation.Local)
            {
                if (doSaveAs)
                {
                    SaveFileDialog sfd = new SaveFileDialog();
                    sfd.Title  = "Save File As";
                    sfd.Filter = m_fileFilter;

                    if (sfd.ShowDialog() != DialogResult.OK)
                    {
                        return(false);
                    }

                    // SFD automatically appends the appropriate extension if necessary
                    filename = sfd.FileName;
                }
                else
                {
                    filename = m_currentEditor.Filename;
                }

                if (filename == "")
                {
                    return(false);
                }

                File.WriteAllText(filename, fileContents);
            }
            else if (location == FileLocation.Remote)
            {
                if (!ChameleonNetworking.Instance.IsConnected)
                {
                    string message = "This is a remote file, but Chameleon is not currently connected to a remote server.  You can: ";

                    List <string> buttons = new List <string>();
                    buttons.Add("Save the file locally (on the computer you're using)");
                    buttons.Add("Cancel saving");

                    int button = cTaskDialog.ShowCommandBox("Save File Location", message, "",
                                                            string.Join("|", buttons), false);
                    location = (FileLocation)button;

                    if (location == FileLocation.Local)
                    {
                        doSaveAs = true;
                        goto LocationSelected;
                    }

                    return(false);
                }

                if (doSaveAs)
                {
                    RemoteFileDialog rfd = RemoteFileDialog.Instance;
                    rfd.Prepare(false, m_fileFilter);                    //, "");

                    DialogResult dr = rfd.ShowDialog();

                    if (dr == DialogResult.OK)
                    {
                        filename = rfd.SelectedFile.GetFullPath();
                    }
                    else
                    {
                        return(false);
                    }

                    if (filename == "")
                    {
                        return(false);
                    }
                }
                else
                {
                    filename = m_currentEditor.Filename;
                }

                FilePath fp = new FilePath(filename, PathFormat.Unix);

                ChameleonNetworking.Instance.SendFileContents(fp, fileContents);
            }
            // shouldn't be Unknown by this point

            editor.SetFileSaved(filename, location);

            if (EditorStatusChanged != null)
            {
                EditorStatusChanged(this, null);
            }

            if (App.UserSettings.PermittedFeatures.HasFlag(ChameleonFeatures.CodeRules))
            {
                RunCodeRules(editor);
            }


            return(true);
        }
        // returns true if things were handled successfully, and the task should continue
        private ModifiedFileResult HandleModifiedFile(ChameleonEditor editor, ModifiedFileAction fileAction)
        {
            if (!editor.Modified)
            {
                return(ModifiedFileResult.NoSave);
            }

            string messageEnd       = "";
            string saveButtonText   = "";
            string noSaveButtonText = "";
            string cancelButtonText = "";

            List <string> buttons = new List <string>();
            Dictionary <int, ModifiedFileResult> buttonActions = new Dictionary <int, ModifiedFileResult>();

            switch (fileAction)
            {
            case ModifiedFileAction.Close:
            {
                messageEnd       = "Do you want to save the file before it is closed?";
                saveButtonText   = "Save this file, then close it";
                noSaveButtonText = "Don't save this file, but still close it";
                cancelButtonText = "Cancel closing, and leave this file open";

                buttons.Add(saveButtonText);
                buttons.Add(noSaveButtonText);

                buttonActions[0] = ModifiedFileResult.Save;
                buttonActions[1] = ModifiedFileResult.NoSave;

                break;
            }

            case ModifiedFileAction.Reload:
            {
                if (editor.FileLocation == FileLocation.Unknown)
                {
                    return(ModifiedFileResult.Cancel);
                }

                if (!editor.Modified)
                {
                    return(ModifiedFileResult.NoSave);
                }

                messageEnd       = "Do you want to revert to what was last saved?";
                noSaveButtonText = "Lose changes and reload this file";
                cancelButtonText = "Cancel reloading, and return to editing";

                buttons.Add(noSaveButtonText);

                buttonActions[0] = ModifiedFileResult.NoSave;
                break;
            }

            case ModifiedFileAction.Compile:
            {
                messageEnd       = "Do you want to save this file and compile it?";
                saveButtonText   = "Save and compile this file";
                cancelButtonText = "Cancel compiling, and return to editing";

                buttons.Add(saveButtonText);
                buttonActions[0] = ModifiedFileResult.Save;
                break;
            }
            }
            string message = string.Format("The file '{0}' has unsaved changes. {1}", editor.Filename, messageEnd);

            buttons.Add(cancelButtonText);
            buttonActions[buttonActions.Count] = ModifiedFileResult.Cancel;

            string commandButtonString = string.Join("|", buttons);

            int selectedButtonID = cTaskDialog.ShowCommandBox("Unsaved Changes", message, "",
                                                              commandButtonString, false);
            ModifiedFileResult selectedAction = buttonActions[selectedButtonID];

            ModifiedFileResult returnResult = ModifiedFileResult.Cancel;

            switch (selectedAction)
            {
            case ModifiedFileResult.Save:
            {
                bool saveResult = SaveFile(editor, editor.FileLocation, false, true);

                returnResult = saveResult ? ModifiedFileResult.Save : ModifiedFileResult.Cancel;
                break;
            }

            case ModifiedFileResult.NoSave:
            {
                returnResult = ModifiedFileResult.NoSave;
                break;
            }

            case ModifiedFileResult.Cancel:
            {
                // returnResult is already cancel, no actions needed
                break;
            }
            }

            return(returnResult);
        }