// A tab is selected
        private void tabs_Selecting(object sender, TabControlCancelEventArgs e)
        {
            //mxd. Update script navigator
            ScriptDocumentTab tab = e.TabPage as ScriptDocumentTab;

            if (tab != null)
            {
                // Show all errors...
                ShowErrors(tab.UpdateNavigator(), true);
            }

            UpdateToolbar(true);
        }
        // This initializes the control
        public void Initialize()
        {
            // Make list of script configs
            scriptconfigs = new List <ScriptConfiguration>(General.ScriptConfigs.Values);
            scriptconfigs.Add(new ScriptConfiguration());
            scriptconfigs.Sort();

            // Fill the list of new document types
            foreach (ScriptConfiguration cfg in scriptconfigs)
            {
                // Button for new script menu
                ToolStripMenuItem item = new ToolStripMenuItem(cfg.Description);
                //item.Image = buttonnew.Image;
                item.Tag    = cfg;
                item.Click += buttonnew_Click;
                buttonnew.DropDownItems.Add(item);

                // Button for script type menu
                item = new ToolStripMenuItem(cfg.Description);
                //item.Image = buttonnew.Image;
                item.Tag    = cfg;
                item.Click += buttonscriptconfig_Click;
                buttonscriptconfig.DropDownItems.Add(item);
            }

            // Setup supported extensions
            string filterall      = "";
            string filterseperate = "";

            foreach (ScriptConfiguration cfg in scriptconfigs)
            {
                if (cfg.Extensions.Length > 0)
                {
                    string exts = "*." + string.Join(";*.", cfg.Extensions);
                    if (filterseperate.Length > 0)
                    {
                        filterseperate += "|";
                    }
                    filterseperate += cfg.Description + "|" + exts;
                    if (filterall.Length > 0)
                    {
                        filterall += ";";
                    }
                    filterall += exts;
                }
            }
            openfile.Filter = "Script files|" + filterall + "|" + filterseperate + "|All files|*.*";

            // Load the script lumps
            ScriptDocumentTab activetab = null;             //mxd

            foreach (MapLumpInfo maplumpinfo in General.Map.Config.MapLumps.Values)
            {
                // Is this a script lump?
                if (maplumpinfo.ScriptBuild)                //mxd
                {
                    ScriptConfiguration config = General.GetScriptConfiguration(ScriptType.ACS);
                    if (config == null)
                    {
                        General.ErrorLogger.Add(ErrorType.Warning, "Unable to find script configuration for \"" + ScriptType.ACS + "\" script type. Using plain text configuration.");
                        config = new ScriptConfiguration();
                    }

                    // Load this!
                    ScriptLumpDocumentTab t = new ScriptLumpDocumentTab(this, maplumpinfo.Name, config);

                    //mxd. Apply stored settings?
                    if (General.Map.Options.ScriptLumpSettings.ContainsKey(maplumpinfo.Name))
                    {
                        t.SetViewSettings(General.Map.Options.ScriptLumpSettings[maplumpinfo.Name]);
                        if (General.Map.Options.ScriptLumpSettings[maplumpinfo.Name].IsActiveTab)
                        {
                            activetab = t;
                        }
                    }
                    else
                    {
                        t.SetDefaultViewSettings();
                    }

                    t.OnTextChanged      += tabpage_OnLumpTextChanged;                //mxd
                    t.Scintilla.UpdateUI += scintilla_OnUpdateUI;                     //mxd
                    tabs.TabPages.Add(t);
                }
                else if (maplumpinfo.Script != null)
                {
                    // Load this!
                    ScriptLumpDocumentTab t = new ScriptLumpDocumentTab(this, maplumpinfo.Name, maplumpinfo.Script);

                    //mxd. Apply stored settings?
                    if (General.Map.Options.ScriptLumpSettings.ContainsKey(maplumpinfo.Name))
                    {
                        t.SetViewSettings(General.Map.Options.ScriptLumpSettings[maplumpinfo.Name]);
                        if (General.Map.Options.ScriptLumpSettings[maplumpinfo.Name].IsActiveTab)
                        {
                            activetab = t;
                        }
                    }
                    else
                    {
                        t.SetDefaultViewSettings();
                    }

                    t.OnTextChanged      += tabpage_OnLumpTextChanged;                //mxd
                    t.Scintilla.UpdateUI += scintilla_OnUpdateUI;                     //mxd
                    tabs.TabPages.Add(t);
                }
            }

            // Load the files that were previously opened for this map
            foreach (ScriptDocumentSettings settings in General.Map.Options.ScriptFileSettings.Values)
            {
                // Does this file exist?
                if (File.Exists(settings.Filename))
                {
                    // Load this!
                    ScriptFileDocumentTab t = OpenFile(settings.Filename);
                    t.SetViewSettings(settings);                     //mxd
                    if (settings.IsActiveTab)
                    {
                        activetab = t;
                    }
                }
            }

            //mxd. Reselect previously selected tab
            if (activetab != null)
            {
                tabs.SelectedTab = activetab;
            }
            //mxd. Select "Scripts" tab, because that's what user will want 99% of time
            else if (tabs.TabPages.Count > 0)
            {
                int scriptsindex = GetTabPageIndex("SCRIPTS");
                tabs.SelectedIndex = (scriptsindex == -1 ? 0 : scriptsindex);
                activetab          = tabs.TabPages[tabs.SelectedIndex] as ScriptDocumentTab;
            }

            //mxd. Apply quick search settings
            searchmatchcase.Checked = matchcase;
            searchwholeword.Checked = matchwholeword;
            searchbox_TextChanged(this, EventArgs.Empty);

            //mxd. If the map or script navigator has any compile errors, show them
            if (activetab != null)
            {
                List <CompilerError> errors = (General.Map.Errors.Count > 0 ? General.Map.Errors : activetab.UpdateNavigator());
                if (errors.Count > 0)
                {
                    ShowErrors(errors, false);
                }
                else
                {
                    ClearErrors();
                }
            }
            else
            {
                ClearErrors();
            }

            // Done
            UpdateToolbar(true);
        }