FileDialog.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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 dirEntry, nameEntry;
  12. public FileDialog (ustring title, ustring prompt, ustring nameFieldLabel, ustring message) : base (title, Driver.Cols - 20, Driver.Rows - 6, null)
  13. {
  14. this.message = new Label (Rect.Empty, message);
  15. var msgLines = Label.MeasureLines (message, Driver.Cols - 20);
  16. dirLabel = new Label ("Directory: ") {
  17. X = 2,
  18. Y = 1 + msgLines
  19. };
  20. dirEntry = new TextField ("") {
  21. X = 12,
  22. Y = 1 + msgLines
  23. };
  24. Add (dirLabel, dirEntry);
  25. this.nameFieldLabel = new Label (nameFieldLabel) {
  26. X = 2,
  27. Y = 3 + msgLines,
  28. };
  29. nameEntry = new TextField ("") {
  30. X = 2 + nameFieldLabel.RuneCount + 1,
  31. Y = 3 + msgLines,
  32. Width = Dim.Fill () - 1
  33. };
  34. Add (this.nameFieldLabel, nameEntry);
  35. this.cancel = new Button ("Cancel");
  36. AddButton (cancel);
  37. this.prompt = new Button (prompt);
  38. AddButton (this.prompt);
  39. }
  40. /// <summary>
  41. /// Gets or sets the prompt label for the button displayed to the user
  42. /// </summary>
  43. /// <value>The prompt.</value>
  44. public ustring Prompt {
  45. get => prompt.Text;
  46. set {
  47. prompt.Text = value;
  48. }
  49. }
  50. /// <summary>
  51. /// Gets or sets the name field label.
  52. /// </summary>
  53. /// <value>The name field label.</value>
  54. public ustring NameFieldLabel {
  55. get => nameFieldLabel.Text;
  56. set {
  57. nameFieldLabel.Text = value;
  58. }
  59. }
  60. /// <summary>
  61. /// Gets or sets the message displayed to the user, defaults to nothing
  62. /// </summary>
  63. /// <value>The message.</value>
  64. public ustring Message {
  65. get => message.Text;
  66. set {
  67. message.Text = value;
  68. }
  69. }
  70. /// <summary>
  71. /// Gets or sets a value indicating whether this <see cref="T:Terminal.Gui.FileDialog"/> can create directories.
  72. /// </summary>
  73. /// <value><c>true</c> if can create directories; otherwise, <c>false</c>.</value>
  74. public bool CanCreateDirectories { get; set; }
  75. /// <summary>
  76. /// Gets or sets a value indicating whether this <see cref="T:Terminal.Gui.FileDialog"/> is extension hidden.
  77. /// </summary>
  78. /// <value><c>true</c> if is extension hidden; otherwise, <c>false</c>.</value>
  79. public bool IsExtensionHidden { get; set; }
  80. /// <summary>
  81. /// Gets or sets the directory path for this panel
  82. /// </summary>
  83. /// <value>The directory path.</value>
  84. public ustring DirectoryPath {
  85. get => dirEntry.Text;
  86. set {
  87. dirEntry.Text = value;
  88. }
  89. }
  90. /// <summary>
  91. /// The array of filename extensions allowed, or null if all file extensions are allowed.
  92. /// </summary>
  93. /// <value>The allowed file types.</value>
  94. public ustring [] AllowedFileTypes { get; set; }
  95. /// <summary>
  96. /// Gets or sets a value indicating whether this <see cref="T:Terminal.Gui.FileDialog"/> allows the file to be saved with a different extension
  97. /// </summary>
  98. /// <value><c>true</c> if allows other file types; otherwise, <c>false</c>.</value>
  99. public bool AllowsOtherFileTypes { get; set; }
  100. /// <summary>
  101. /// The File path that is currently shown on the panel
  102. /// </summary>
  103. /// <value>The absolute file path for the file path entered.</value>
  104. public ustring FilePath {
  105. get => nameEntry.Text;
  106. set {
  107. nameEntry.Text = value;
  108. }
  109. }
  110. }
  111. public class SaveDialog : FileDialog {
  112. public SaveDialog (ustring title, ustring message) : base (title, prompt: "Save", nameFieldLabel: "Save as:", message: message)
  113. {
  114. }
  115. }
  116. public class OpenDialog : FileDialog {
  117. public OpenDialog (ustring title, ustring message) : base (title, prompt: "Open", nameFieldLabel: "Open", message: message)
  118. {
  119. }
  120. /// <summary>
  121. /// Gets or sets a value indicating whether this <see cref="T:Terminal.Gui.OpenDialog"/> can choose files.
  122. /// </summary>
  123. /// <value><c>true</c> if can choose files; otherwise, <c>false</c>.</value>
  124. public bool CanChooseFiles { get; set; }
  125. /// <summary>
  126. /// Gets or sets a value indicating whether this <see cref="T:Terminal.Gui.OpenDialog"/> can choose directories.
  127. /// </summary>
  128. /// <value><c>true</c> if can choose directories; otherwise, <c>false</c>.</value>
  129. public bool CanChooseDirectories { get; set; }
  130. /// <summary>
  131. /// Gets or sets a value indicating whether this <see cref="T:Terminal.Gui.OpenDialog"/> allows multiple selection.
  132. /// </summary>
  133. /// <value><c>true</c> if allows multiple selection; otherwise, <c>false</c>.</value>
  134. public bool AllowsMultipleSelection { get; set; }
  135. /// <summary>
  136. /// Gets the file paths selected
  137. /// </summary>
  138. /// <value>The file paths.</value>
  139. public IReadOnlyList<ustring> FilePaths { get; }
  140. }
  141. }