コード例 #1
0
        /// <summary>
        /// Initializes a new instance of <see cref="FileDialog"/>
        /// </summary>
        /// <param name="title">The title.</param>
        /// <param name="prompt">The prompt.</param>
        /// <param name="nameFieldLabel">The name field label.</param>
        /// <param name="message">The message.</param>
        public FileDialog(ustring title, ustring prompt, ustring nameFieldLabel, ustring message) : base(title, Driver.Cols - 20, Driver.Rows - 5, null)
        {
            this.message = new Label(Rect.Empty, "MESSAGE" + message);
            var msgLines = Label.MeasureLines(message, Driver.Cols - 20);

            dirLabel = new Label("Directory: ")
            {
                X = 1,
                Y = 1 + msgLines
            };

            dirEntry = new TextField("")
            {
                X     = Pos.Right(dirLabel),
                Y     = 1 + msgLines,
                Width = Dim.Fill() - 1
            };
            Add(dirLabel, dirEntry);

            this.nameFieldLabel = new Label("Open: ")
            {
                X = 6,
                Y = 3 + msgLines,
            };
            nameEntry = new TextField("")
            {
                X     = Pos.Left(dirEntry),
                Y     = 3 + msgLines,
                Width = Dim.Fill() - 1
            };
            Add(this.nameFieldLabel, nameEntry);

            dirListView = new DirListView(this)
            {
                X      = 1,
                Y      = 3 + msgLines + 2,
                Width  = Dim.Fill() - 1,
                Height = Dim.Fill() - 2,
            };
            DirectoryPath = Path.GetFullPath(Environment.CurrentDirectory);
            Add(dirListView);
            dirListView.DirectoryChanged = (dir) => dirEntry.Text = dir;
            dirListView.FileChanged      = (file) => nameEntry.Text = file;

            this.cancel          = new Button("Cancel");
            this.cancel.Clicked += () => {
                canceled = true;
                Application.RequestStop();
            };
            AddButton(cancel);

            this.prompt = new Button(prompt)
            {
                IsDefault = true,
            };
            this.prompt.Clicked += () => {
                dirListView.ExecuteSelection();
                canceled = false;
                Application.RequestStop();
            };
            AddButton(this.prompt);

            Width  = Dim.Percent(80);
            Height = Dim.Percent(80);

            // On success, we will set this to false.
            canceled = true;
        }
コード例 #2
0
        /// <summary>
        /// Initializes a new instance of <see cref="FileDialog"/>
        /// </summary>
        /// <param name="title">The title.</param>
        /// <param name="prompt">The prompt.</param>
        /// <param name="nameDirLabel">The name of the directory field label.</param>
        /// <param name="nameFieldLabel">The name of the file field label..</param>
        /// <param name="message">The message.</param>
        /// <param name="allowedTypes">The allowed types.</param>
        public FileDialog(ustring title, ustring prompt, ustring nameDirLabel, ustring nameFieldLabel, ustring message,
                          List <string> allowedTypes = null) : base(title)//, Driver.Cols - 20, Driver.Rows - 5, null)
        {
            this.message = new Label(message)
            {
                X = 1,
                Y = 0,
            };
            Add(this.message);
            var msgLines = TextFormatter.MaxLines(message, Driver.Cols - 20);

            this.nameDirLabel = new Label(nameDirLabel.IsEmpty ? $"{Strings.fdDirectory}: " : $"{nameDirLabel}: ")
            {
                X        = 1,
                Y        = 1 + msgLines,
                AutoSize = true
            };

            dirEntry = new TextField("")
            {
                X     = Pos.Right(this.nameDirLabel),
                Y     = 1 + msgLines,
                Width = Dim.Fill() - 1,
            };
            dirEntry.TextChanged += (e) => {
                DirectoryPath  = dirEntry.Text;
                nameEntry.Text = ustring.Empty;
            };
            Add(this.nameDirLabel, dirEntry);

            this.nameFieldLabel = new Label(nameFieldLabel.IsEmpty ? $"{Strings.fdFile}: " : $"{nameFieldLabel}: ")
            {
                X        = 1,
                Y        = 3 + msgLines,
                AutoSize = true
            };
            nameEntry = new TextField("")
            {
                X     = Pos.Left(dirEntry),
                Y     = 3 + msgLines,
                Width = Dim.Percent(70, true)
            };
            Add(this.nameFieldLabel, nameEntry);

            cmbAllowedTypes = new ComboBox()
            {
                X        = Pos.Right(nameEntry) + 2,
                Y        = Pos.Top(nameEntry),
                Width    = Dim.Fill(1),
                Height   = allowedTypes != null ? allowedTypes.Count + 1 : 1,
                Text     = allowedTypes?.Count > 0 ? allowedTypes [0] : string.Empty,
                ReadOnly = true
            };
            cmbAllowedTypes.SetSource(allowedTypes ?? new List <string> ());
            cmbAllowedTypes.OpenSelectedItem += (e) => AllowedFileTypes = cmbAllowedTypes.Text.ToString().Split(';');
            Add(cmbAllowedTypes);

            dirListView = new DirListView(this)
            {
                X      = 1,
                Y      = 3 + msgLines + 2,
                Width  = Dim.Fill() - 1,
                Height = Dim.Fill() - 2,
            };
            DirectoryPath = Path.GetFullPath(Environment.CurrentDirectory);
            Add(dirListView);

            AllowedFileTypes             = cmbAllowedTypes.Text.ToString().Split(';');
            dirListView.DirectoryChanged = (dir) => { nameEntry.Text = ustring.Empty; dirEntry.Text = dir; };
            dirListView.FileChanged      = (file) => nameEntry.Text = file == ".." ? "" : file;
            dirListView.SelectedChanged  = (file) => nameEntry.Text = file.Item1 == ".." ? "" : file.Item1;
            this.cancel          = new Button("Cancel");
            this.cancel.Clicked += () => {
                Cancel();
            };
            AddButton(cancel);

            this.prompt = new Button(prompt.IsEmpty ? "Ok" : prompt)
            {
                IsDefault = true,
                Enabled   = nameEntry.Text.IsEmpty ? false : true
            };
            this.prompt.Clicked += () => {
                if (this is OpenDialog)
                {
                    if (!dirListView.GetValidFilesName(nameEntry.Text.ToString(), out string res))
                    {
                        nameEntry.Text = res;
                        dirListView.SetNeedsDisplay();
                        return;
                    }
                    if (!dirListView.canChooseDirectories && !dirListView.ExecuteSelection(false))
                    {
                        return;
                    }
                }
                else if (this is SaveDialog)
                {
                    var name = nameEntry.Text.ToString();
                    if (FilePath.IsEmpty || name.Split(',').Length > 1)
                    {
                        return;
                    }
                    var ext = name.EndsWith(cmbAllowedTypes.Text.ToString())
                                                ? "" : cmbAllowedTypes.Text.ToString();
                    FilePath = Path.Combine(FilePath.ToString(), $"{name}{ext}");
                }
                canceled = false;
                Application.RequestStop();
            };
            AddButton(this.prompt);

            nameEntry.TextChanged += (e) => {
                if (nameEntry.Text.IsEmpty)
                {
                    this.prompt.Enabled = false;
                }
                else
                {
                    this.prompt.Enabled = true;
                }
            };

            Width  = Dim.Percent(80);
            Height = Dim.Percent(80);

            // On success, we will set this to false.
            canceled = true;

            KeyPress += (e) => {
                if (e.KeyEvent.Key == Key.Esc)
                {
                    Cancel();
                    e.Handled = true;
                }
            };
            void Cancel()
            {
                canceled = true;
                Application.RequestStop();
            }
        }