FileDialog.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. //
  2. // FileDialog.cs: File system dialogs for open and save
  3. //
  4. using System;
  5. using System.Collections.Generic;
  6. using NStack;
  7. namespace Terminal.Gui {
  8. public class FileDialog : Dialog {
  9. Button prompt, cancel;
  10. Label nameFieldLabel, message, dirLabel;
  11. TextField dir;
  12. public FileDialog (ustring title, ustring prompt, ustring nameFieldLabel, ustring message) : base (title, Driver.Cols - 20, Driver.Rows - 6, null)
  13. {
  14. dirLabel = new Label (2, 1, "Directory");
  15. Add (dirLabel);
  16. this.cancel = new Button ("Cancel");
  17. AddButton (cancel);
  18. this.prompt = new Button (prompt);
  19. AddButton (this.prompt);
  20. this.nameFieldLabel = new Label (Rect.Empty, nameFieldLabel);
  21. this.message = new Label (Rect.Empty, message);
  22. }
  23. /// <summary>
  24. /// Gets or sets the prompt label for the button displayed to the user
  25. /// </summary>
  26. /// <value>The prompt.</value>
  27. public ustring Prompt {
  28. get => prompt.Text;
  29. set {
  30. prompt.Text = value;
  31. }
  32. }
  33. /// <summary>
  34. /// Gets or sets the name field label.
  35. /// </summary>
  36. /// <value>The name field label.</value>
  37. public ustring NameFieldLabel {
  38. get => nameFieldLabel.Text;
  39. set {
  40. nameFieldLabel.Text = value;
  41. }
  42. }
  43. /// <summary>
  44. /// Gets or sets the message displayed to the user, defaults to nothing
  45. /// </summary>
  46. /// <value>The message.</value>
  47. public ustring Message { get; set; }
  48. /// <summary>
  49. /// Gets or sets a value indicating whether this <see cref="T:Terminal.Gui.FileDialog"/> can create directories.
  50. /// </summary>
  51. /// <value><c>true</c> if can create directories; otherwise, <c>false</c>.</value>
  52. public bool CanCreateDirectories { get; set; }
  53. /// <summary>
  54. /// Gets or sets a value indicating whether this <see cref="T:Terminal.Gui.FileDialog"/> is extension hidden.
  55. /// </summary>
  56. /// <value><c>true</c> if is extension hidden; otherwise, <c>false</c>.</value>
  57. public bool IsExtensionHidden { get; set; }
  58. /// <summary>
  59. /// Gets or sets the directory path for this panel
  60. /// </summary>
  61. /// <value>The directory path.</value>
  62. public ustring DirectoryPath { get; set; }
  63. /// <summary>
  64. /// The array of filename extensions allowed, or null if all file extensions are allowed.
  65. /// </summary>
  66. /// <value>The allowed file types.</value>
  67. public ustring [] AllowedFileTypes { get; set; }
  68. /// <summary>
  69. /// Gets or sets a value indicating whether this <see cref="T:Terminal.Gui.FileDialog"/> allows the file to be saved with a different extension
  70. /// </summary>
  71. /// <value><c>true</c> if allows other file types; otherwise, <c>false</c>.</value>
  72. public bool AllowsOtherFileTypes { get; set; }
  73. /// <summary>
  74. /// The File path that is currently shown on the panel
  75. /// </summary>
  76. /// <value>The absolute file path for the file path entered.</value>
  77. public ustring FilePath { get; set; }
  78. }
  79. public class SaveDialog : FileDialog {
  80. public SaveDialog (ustring title, ustring message) : base (title, "Save", "Save as:", message)
  81. {
  82. }
  83. }
  84. public class OpenDialog : FileDialog {
  85. public OpenDialog (ustring title, ustring message) : base (title, "Open", "Open", message)
  86. {
  87. }
  88. /// <summary>
  89. /// Gets or sets a value indicating whether this <see cref="T:Terminal.Gui.OpenDialog"/> can choose files.
  90. /// </summary>
  91. /// <value><c>true</c> if can choose files; otherwise, <c>false</c>.</value>
  92. public bool CanChooseFiles { get; set; }
  93. /// <summary>
  94. /// Gets or sets a value indicating whether this <see cref="T:Terminal.Gui.OpenDialog"/> can choose directories.
  95. /// </summary>
  96. /// <value><c>true</c> if can choose directories; otherwise, <c>false</c>.</value>
  97. public bool CanChooseDirectories { get; set; }
  98. /// <summary>
  99. /// Gets or sets a value indicating whether this <see cref="T:Terminal.Gui.OpenDialog"/> allows multiple selection.
  100. /// </summary>
  101. /// <value><c>true</c> if allows multiple selection; otherwise, <c>false</c>.</value>
  102. public bool AllowsMultipleSelection { get; set; }
  103. /// <summary>
  104. /// Gets the file paths selected
  105. /// </summary>
  106. /// <value>The file paths.</value>
  107. public IReadOnlyList<ustring> FilePaths { get; }
  108. }
  109. }