示例#1
0
        internal static void ResetTab(Form1 form)
        {
            XtraTabControl    pagesTabControl   = form.pagesTabControl;
            CustomRichTextBox pageTextBox       = ProgramUtil.GetPageTextBox(pagesTabControl.SelectedTabPage);
            CustomLineNumbers customLineNumbers = ProgramUtil.GetCustomLineNumbers(pagesTabControl.SelectedTabPage);

            if (ColumnRulerManager.IsPanelOpen(form))
            {
                ColumnRulerManager.TogglePanel(form);
            }

            String newTabTitle = LanguageUtil.GetCurrentLanguageString("tabPage1", form.Name);

            SetTabColor(pagesTabControl.SelectedTabPage, Color.Black); //pagesTabControl.SelectedTabPage.Appearance.Header.ForeColor = Color.Black;
            pagesTabControl.SelectedTabPage.Appearance.Header.Reset();
            pageTextBox.Text = String.Empty;
            pagesTabControl.SelectedTabPage.ImageIndex = 0;
            pagesTabControl.SelectedTabPage.Text       = newTabTitle;
            pagesTabControl.SelectedTabPage.Tooltip    = newTabTitle;
            form.Text = String.Format("DtPad - {0}", newTabTitle);
            ProgramUtil.SetFilenameTabPage(pagesTabControl.SelectedTabPage, String.Empty);
            pageTextBox.CustomZoom           = 100;
            pageTextBox.CustomModified       = false;
            pageTextBox.CustomOriginal       = String.Empty.GetHashCode().ToString();
            pageTextBox.CustomEncoding       = String.Empty;
            pageTextBox.CustomEncodingForced = false;
            customLineNumbers.Width          = customLineNumbers.MinimumSize.Width;
            ZoomSelect(form, 100);
            pageTextBox.ClearUndo();

            TextManager.RefreshUndoRedoExternal(form);

            if (String.IsNullOrEmpty(ProgramUtil.GetFilenameTabPage(pagesTabControl.SelectedTabPage)))
            {
                ToggleTabFileTools(form, false);
            }
            OtherManager.FocusOnEditor(form);
        }
示例#2
0
        internal static int OpenFile(Form1 form, int tabIdentity, String[] specificFileNames, bool showMessages, bool saveNewRecentFile, out List <String> errors)
        {
            CustomXtraTabControl pagesTabControl      = form.pagesTabControl;
            ToolStripStatusLabel toolStripStatusLabel = form.toolStripStatusLabel;
            ToolStripProgressBar toolStripProgressBar = form.toolStripProgressBar;
            OpenFileDialog       openFileDialog       = form.openFileDialog;

            bool isWindowsHostsFile = false;
            int  localTabIdentity   = tabIdentity;

            errors = new List <String>();

            openFileDialog.InitialDirectory = FileUtil.GetInitialFolder(form);
            openFileDialog.Multiselect      = true;
            SetFileDialogFilter(openFileDialog);

            TrayManager.RestoreFormIfIsInTray(form);

            try
            {
                String[] fileNames;

                if (specificFileNames == null || specificFileNames.Length <= 0)
                {
                    if (openFileDialog.ShowDialog() != DialogResult.OK)
                    {
                        return(tabIdentity);
                    }

                    fileNames = openFileDialog.FileNames;
                }
                else
                {
                    fileNames = specificFileNames;
                }

                //Verify if file is a DtPad session
                if (fileNames.Length == 1 && fileNames[0].EndsWith(".dps"))
                {
                    SessionManager.OpenSession(form, fileNames[0]);
                    return(form.TabIdentity);
                }

                Application.DoEvents();
                toolStripProgressBar.Value = 0;

                foreach (String fileName in fileNames)
                {
                    //Verify if file is Windows hosts file
                    if (fileName.Contains(@"drivers\etc\hosts"))
                    {
                        if (!SystemUtil.IsUserAdministrator())
                        {
                            WindowManager.ShowAlertBox(form, LanguageUtil.GetCurrentLanguageString("UserNotAdmin", className));
                        }

                        isWindowsHostsFile = true;
                    }

                    if (!showMessages)
                    {
                        if (!File.Exists(fileName))
                        {
                            errors.Add(String.Format(LanguageUtil.GetCurrentLanguageString("NoMessageFileNotExists", className), fileName));
                            continue;
                        }
                        if (FileUtil.IsFileInUse(fileName))
                        {
                            errors.Add(String.Format(LanguageUtil.GetCurrentLanguageString("NoMessageFileInUse", className), fileName));
                            continue;
                        }
                        if (FileUtil.IsFileTooLargeForDtPad(fileName))
                        {
                            errors.Add(String.Format(LanguageUtil.GetCurrentLanguageString("NoMessageFileTooHeavy", className), fileName));
                            continue;
                        }
                    }
                    else if (!File.Exists(fileName))
                    {
                        WindowManager.ShowAlertBox(form, String.Format(LanguageUtil.GetCurrentLanguageString("FileNotExisting", className), fileName));
                        continue;
                    }
                    else if (FileUtil.IsFileInUse(fileName))
                    {
                        WindowManager.ShowAlertBox(form, String.Format(LanguageUtil.GetCurrentLanguageString("FileInUse", className), fileName));
                        continue;
                    }
                    if (FileUtil.IsFileTooLargeForDtPad(fileName))
                    {
                        WindowManager.ShowAlertBox(form, String.Format(LanguageUtil.GetCurrentLanguageString("FileTooHeavy", className), fileName));
                        continue;
                    }

                    //Cycle and check if the file is already open, in which case I select its tab and continue with the next one
                    XtraTabPage tabPage;
                    if (FileUtil.IsFileAlreadyOpen(form, fileName, out tabPage))
                    {
                        pagesTabControl.SelectedTabPage = tabPage;
                        toolStripProgressBar.PerformStep();
                        toolStripProgressBar.PerformStep();
                        toolStripProgressBar.PerformStep();
                        toolStripProgressBar.PerformStep();
                        toolStripProgressBar.Visible = false;
                        continue;
                    }

                    //Verify if file is an archive
                    try
                    {
                        ZipFile file = null;
                        bool    isZipFile;

                        try
                        {
                            file      = new ZipFile(fileName);
                            isZipFile = file.TestArchive(false, TestStrategy.FindFirstError, form.Zip_Errors);
                        }
                        finally
                        {
                            if (file != null)
                            {
                                file.Close();
                            }
                        }

                        if (isZipFile)
                        {
                            WindowManager.ShowZipExtract(form, fileName);
                            continue;
                        }
                    }
                    catch (ZipException)
                    {
                    }

                    toolStripProgressBar.Visible = true;
                    toolStripProgressBar.PerformStep();

                    String   fileContents;
                    Encoding fileEncoding;
                    bool     anonymousFile = false;

                    //Verify if file is a PDF
                    if (fileName.EndsWith(".pdf"))
                    {
                        bool success;
                        fileContents = PdfUtil.ExtractText(fileName, out success);

                        if (!success)
                        {
                            WindowManager.ShowAlertBox(form, LanguageUtil.GetCurrentLanguageString("InvalidPdf", className));
                            return(tabIdentity);
                        }

                        fileEncoding  = EncodingUtil.GetDefaultEncoding();
                        anonymousFile = true;
                    }
                    else
                    {
                        fileContents = FileUtil.ReadToEndWithEncoding(fileName, out fileEncoding);
                    }

                    bool favouriteFile = FavouriteManager.IsFileFavourite(fileName);
                    if (!favouriteFile && saveNewRecentFile)
                    {
                        ConfigUtil.UpdateParameter("LastUserFolder", Path.GetDirectoryName(fileName));
                        FileListManager.SetNewRecentFile(form, fileName);
                    }
                    toolStripProgressBar.PerformStep();

                    CustomRichTextBox pageTextBox = ProgramUtil.GetPageTextBox(pagesTabControl.SelectedTabPage);
                    if (!String.IsNullOrEmpty(pageTextBox.Text) || !String.IsNullOrEmpty(ProgramUtil.GetFilenameTabPage(pagesTabControl.SelectedTabPage)))
                    {
                        localTabIdentity = TabManager.AddNewPage(form, localTabIdentity);
                    }
                    toolStripProgressBar.PerformStep();

                    //Row number check
                    WindowManager.CheckLineNumbersForTextLenght(form, fileContents);

                    FileInfo fileInfo = new FileInfo(fileName);

                    pageTextBox = ProgramUtil.GetPageTextBox(pagesTabControl.SelectedTabPage);

                    //Verify if file is a tweet file
                    if (fileName.EndsWith(".tweet") && !ColumnRulerManager.IsPanelOpen(form))
                    {
                        ColumnRulerManager.TogglePanel(form);
                    }

                    pageTextBox.Text           = fileContents.Replace(Environment.NewLine, ConstantUtil.newLine);
                    pageTextBox.CustomOriginal = ProgramUtil.GetPageTextBox(pagesTabControl.SelectedTabPage).Text.GetHashCode().ToString();
                    pageTextBox.CustomEncoding = fileEncoding.CodePage.ToString();

                    if (!anonymousFile)
                    {
                        String fileNameWithoutPath = Path.GetFileName(fileName);

                        pageTextBox.CustomModified = false;
                        ProgramUtil.SetFilenameTabPage(pagesTabControl.SelectedTabPage, fileName);
                        pagesTabControl.SelectedTabPage.ImageIndex   = fileInfo.IsReadOnly ? 2 : 0;
                        pagesTabControl.SelectedTabPage.Text         = fileNameWithoutPath;
                        pagesTabControl.SelectedTabPage.Tooltip      = fileName;
                        pagesTabControl.SelectedTabPage.TooltipTitle = fileNameWithoutPath;
                        form.Text = String.Format("DtPad - {0}", fileNameWithoutPath);
                        TabManager.ToggleTabFileTools(form, true);
                    }
                    else
                    {
                        pageTextBox.CustomModified = true;
                    }

                    toolStripStatusLabel.Text = String.Format("{0} \"{1}\" {2}", LanguageUtil.GetCurrentLanguageString("File", className), Path.GetFileName(fileName), LanguageUtil.GetCurrentLanguageString("Opened", className));
                    toolStripProgressBar.PerformStep();

                    tabIdentity = localTabIdentity;

                    if (!String.IsNullOrEmpty(fileInfo.Extension) && ConfigUtil.GetStringParameter("AutoFormatFiles").Contains(fileInfo.Extension))
                    {
                        FormatManager.FormatXml(form);
                    }

                    if (ConfigUtil.GetBoolParameter("AutoOpenHostsConfigurator") && isWindowsHostsFile)
                    {
                        pagesTabControl.SelectedTabPage.Appearance.Header.ForeColor = ConfigUtil.GetColorParameter("ColorHostsConfigurator");
                        CustomFilesManager.OpenHostsSectionPanel(form);
                        isWindowsHostsFile = false;
                    }
                }
            }
            catch (Exception exception)
            {
                TabManager.ToggleTabFileTools(form, false);

                toolStripProgressBar.Visible = false;
                toolStripProgressBar.Value   = 0;

                if (showMessages)
                {
                    WindowManager.ShowErrorBox(form, exception.Message, exception);
                }
            }
            finally
            {
                toolStripProgressBar.Visible = false;
                toolStripProgressBar.Value   = 0;
            }

            return(tabIdentity);
        }