FileDialog.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780
  1. //
  2. // FileDialog.cs: File system dialogs for open and save
  3. //
  4. // TODO:
  5. // * Add directory selector
  6. // * Implement subclasses
  7. // * Figure out why message text does not show
  8. // * Remove the extra space when message does not show
  9. // * Use a line separator to show the file listing, so we can use same colors as the rest
  10. // * DirListView: Add mouse support
  11. using System;
  12. using System.Collections.Generic;
  13. using NStack;
  14. using System.IO;
  15. using System.Linq;
  16. namespace Terminal.Gui {
  17. internal class DirListView : View {
  18. int top, selected;
  19. DirectoryInfo dirInfo;
  20. List<(string, bool, bool)> infos;
  21. internal bool canChooseFiles = true;
  22. internal bool canChooseDirectories = false;
  23. internal bool allowsMultipleSelection = false;
  24. FileDialog host;
  25. public DirListView (FileDialog host)
  26. {
  27. infos = new List<(string, bool, bool)> ();
  28. CanFocus = true;
  29. this.host = host;
  30. }
  31. bool IsAllowed (FileSystemInfo fsi)
  32. {
  33. if (fsi.Attributes.HasFlag (FileAttributes.Directory))
  34. return true;
  35. if (allowedFileTypes == null)
  36. return true;
  37. foreach (var ft in allowedFileTypes)
  38. if (fsi.Name.EndsWith (ft))
  39. return true;
  40. return false;
  41. }
  42. internal bool Reload (ustring value = null)
  43. {
  44. bool valid = false;
  45. try {
  46. dirInfo = new DirectoryInfo (value == null ? directory.ToString () : value.ToString ());
  47. infos = (from x in dirInfo.GetFileSystemInfos ()
  48. where IsAllowed (x) && (!canChooseFiles ? x.Attributes.HasFlag (FileAttributes.Directory) : true)
  49. orderby (!x.Attributes.HasFlag (FileAttributes.Directory)) + x.Name
  50. select (x.Name, x.Attributes.HasFlag (FileAttributes.Directory), false)).ToList ();
  51. infos.Insert (0, ("..", true, false));
  52. top = 0;
  53. selected = 0;
  54. valid = true;
  55. } catch (Exception ex) {
  56. switch (ex) {
  57. case DirectoryNotFoundException _:
  58. case ArgumentException _:
  59. dirInfo = null;
  60. infos.Clear ();
  61. valid = true;
  62. break;
  63. default:
  64. valid = false;
  65. break;
  66. }
  67. } finally {
  68. if (valid) {
  69. SetNeedsDisplay ();
  70. }
  71. }
  72. return valid;
  73. }
  74. ustring directory;
  75. public ustring Directory {
  76. get => directory;
  77. set {
  78. if (directory == value) {
  79. return;
  80. }
  81. if (Reload (value)) {
  82. directory = value;
  83. }
  84. }
  85. }
  86. public override void PositionCursor ()
  87. {
  88. Move (0, selected - top);
  89. }
  90. int lastSelected;
  91. bool shiftOnWheel;
  92. public override bool MouseEvent (MouseEvent me)
  93. {
  94. if ((me.Flags & (MouseFlags.Button1Clicked | MouseFlags.Button1DoubleClicked |
  95. MouseFlags.WheeledUp | MouseFlags.WheeledDown)) == 0)
  96. return false;
  97. if (!HasFocus)
  98. SetFocus ();
  99. if (infos == null)
  100. return false;
  101. if (me.Y + top >= infos.Count)
  102. return true;
  103. int lastSelectedCopy = shiftOnWheel ? lastSelected : selected;
  104. switch (me.Flags) {
  105. case MouseFlags.Button1Clicked:
  106. SetSelected (me);
  107. OnSelectionChanged ();
  108. SetNeedsDisplay ();
  109. break;
  110. case MouseFlags.Button1DoubleClicked:
  111. UnMarkAll ();
  112. SetSelected (me);
  113. if (ExecuteSelection ()) {
  114. host.canceled = false;
  115. Application.RequestStop ();
  116. }
  117. return true;
  118. case MouseFlags.Button1Clicked | MouseFlags.ButtonShift:
  119. SetSelected (me);
  120. if (shiftOnWheel)
  121. lastSelected = lastSelectedCopy;
  122. shiftOnWheel = false;
  123. PerformMultipleSelection (lastSelected);
  124. return true;
  125. case MouseFlags.Button1Clicked | MouseFlags.ButtonCtrl:
  126. SetSelected (me);
  127. PerformMultipleSelection ();
  128. return true;
  129. case MouseFlags.WheeledUp:
  130. SetSelected (me);
  131. selected = lastSelected;
  132. MoveUp ();
  133. return true;
  134. case MouseFlags.WheeledDown:
  135. SetSelected (me);
  136. selected = lastSelected;
  137. MoveDown ();
  138. return true;
  139. case MouseFlags.WheeledUp | MouseFlags.ButtonShift:
  140. SetSelected (me);
  141. selected = lastSelected;
  142. lastSelected = lastSelectedCopy;
  143. shiftOnWheel = true;
  144. MoveUp ();
  145. return true;
  146. case MouseFlags.WheeledDown | MouseFlags.ButtonShift:
  147. SetSelected (me);
  148. selected = lastSelected;
  149. lastSelected = lastSelectedCopy;
  150. shiftOnWheel = true;
  151. MoveDown ();
  152. return true;
  153. }
  154. return true;
  155. }
  156. private void UnMarkAll ()
  157. {
  158. if (allowsMultipleSelection && infos.Count > 0) {
  159. for (int i = 0; i < infos.Count; i++) {
  160. if (infos [i].Item3) {
  161. infos [i] = (infos [i].Item1, infos [i].Item2, false);
  162. }
  163. }
  164. }
  165. }
  166. void SetSelected (MouseEvent me)
  167. {
  168. lastSelected = selected;
  169. selected = top + me.Y;
  170. }
  171. void DrawString (int line, string str)
  172. {
  173. var f = Frame;
  174. var width = f.Width;
  175. var ustr = ustring.Make (str);
  176. Move (allowsMultipleSelection ? 3 : 2, line);
  177. int byteLen = ustr.Length;
  178. int used = allowsMultipleSelection ? 2 : 1;
  179. for (int i = 0; i < byteLen;) {
  180. (var rune, var size) = Utf8.DecodeRune (ustr, i, i - byteLen);
  181. var count = Rune.ColumnWidth (rune);
  182. if (used + count >= width)
  183. break;
  184. Driver.AddRune (rune);
  185. used += count;
  186. i += size;
  187. }
  188. for (; used < width - 1; used++) {
  189. Driver.AddRune (' ');
  190. }
  191. }
  192. public override void Redraw (Rect bounds)
  193. {
  194. var current = ColorScheme.Focus;
  195. Driver.SetAttribute (current);
  196. Move (0, 0);
  197. var f = Frame;
  198. var item = top;
  199. bool focused = HasFocus;
  200. var width = bounds.Width;
  201. for (int row = 0; row < f.Height; row++, item++) {
  202. bool isSelected = item == selected;
  203. Move (0, row);
  204. var newcolor = focused ? (isSelected ? ColorScheme.HotNormal : ColorScheme.Focus) : ColorScheme.Focus;
  205. if (newcolor != current) {
  206. Driver.SetAttribute (newcolor);
  207. current = newcolor;
  208. }
  209. if (item >= infos.Count) {
  210. for (int c = 0; c < f.Width; c++)
  211. Driver.AddRune (' ');
  212. continue;
  213. }
  214. var fi = infos [item];
  215. Driver.AddRune (isSelected ? '>' : ' ');
  216. if (allowsMultipleSelection)
  217. Driver.AddRune (fi.Item3 ? '*' : ' ');
  218. if (fi.Item2)
  219. Driver.AddRune ('/');
  220. else
  221. Driver.AddRune (' ');
  222. DrawString (row, fi.Item1);
  223. }
  224. }
  225. public Action<(string, bool)> SelectedChanged { get; set; }
  226. public Action<ustring> DirectoryChanged { get; set; }
  227. public Action<ustring> FileChanged { get; set; }
  228. void OnSelectionChanged ()
  229. {
  230. if (allowsMultipleSelection) {
  231. if (FilePaths.Count > 0) {
  232. FileChanged?.Invoke (string.Join (", ", GetFilesName (FilePaths)));
  233. } else {
  234. FileChanged?.Invoke (infos [selected].Item2 && !canChooseDirectories ? "" : Path.GetFileName (infos [selected].Item1));
  235. }
  236. } else {
  237. var sel = infos [selected];
  238. SelectedChanged?.Invoke ((sel.Item1, sel.Item2));
  239. }
  240. }
  241. List<string> GetFilesName (IReadOnlyList<string> files)
  242. {
  243. List<string> filesName = new List<string> ();
  244. foreach (var file in files) {
  245. filesName.Add (Path.GetFileName (file));
  246. }
  247. return filesName;
  248. }
  249. public override bool ProcessKey (KeyEvent keyEvent)
  250. {
  251. switch (keyEvent.Key) {
  252. case Key.CursorUp:
  253. case Key.ControlP:
  254. MoveUp ();
  255. return true;
  256. case Key.CursorDown:
  257. case Key.ControlN:
  258. MoveDown ();
  259. return true;
  260. case Key.ControlV:
  261. case Key.PageDown:
  262. var n = (selected + Frame.Height);
  263. if (n > infos.Count)
  264. n = infos.Count - 1;
  265. if (n != selected) {
  266. selected = n;
  267. if (infos.Count >= Frame.Height)
  268. top = selected;
  269. else
  270. top = 0;
  271. OnSelectionChanged ();
  272. SetNeedsDisplay ();
  273. }
  274. return true;
  275. case Key.Enter:
  276. UnMarkAll ();
  277. if (ExecuteSelection ())
  278. return false;
  279. else
  280. return true;
  281. case Key.PageUp:
  282. n = (selected - Frame.Height);
  283. if (n < 0)
  284. n = 0;
  285. if (n != selected) {
  286. selected = n;
  287. top = selected;
  288. OnSelectionChanged ();
  289. SetNeedsDisplay ();
  290. }
  291. return true;
  292. case Key.Space:
  293. case Key.ControlT:
  294. PerformMultipleSelection ();
  295. return true;
  296. case Key.Home:
  297. MoveFirst ();
  298. return true;
  299. case Key.End:
  300. MoveLast ();
  301. return true;
  302. }
  303. return base.ProcessKey (keyEvent);
  304. }
  305. void MoveLast ()
  306. {
  307. selected = infos.Count - 1;
  308. top = infos.Count () - 1;
  309. OnSelectionChanged ();
  310. SetNeedsDisplay ();
  311. }
  312. void MoveFirst ()
  313. {
  314. selected = 0;
  315. top = 0;
  316. OnSelectionChanged ();
  317. SetNeedsDisplay ();
  318. }
  319. void MoveDown ()
  320. {
  321. if (selected + 1 < infos.Count) {
  322. selected++;
  323. if (selected >= top + Frame.Height)
  324. top++;
  325. OnSelectionChanged ();
  326. SetNeedsDisplay ();
  327. }
  328. }
  329. void MoveUp ()
  330. {
  331. if (selected > 0) {
  332. selected--;
  333. if (selected < top)
  334. top = selected;
  335. OnSelectionChanged ();
  336. SetNeedsDisplay ();
  337. }
  338. }
  339. internal bool ExecuteSelection ()
  340. {
  341. if (infos.Count == 0) {
  342. return false;
  343. }
  344. var isDir = infos [selected].Item2;
  345. if (isDir) {
  346. Directory = Path.GetFullPath (Path.Combine (Path.GetFullPath (Directory.ToString ()), infos [selected].Item1));
  347. DirectoryChanged?.Invoke (Directory);
  348. } else {
  349. FileChanged?.Invoke (infos [selected].Item1);
  350. if (canChooseFiles) {
  351. // Ensures that at least one file is selected.
  352. if (FilePaths.Count == 0)
  353. PerformMultipleSelection ();
  354. // Let the OK handler take it over
  355. return true;
  356. }
  357. // No files allowed, do not let the default handler take it.
  358. }
  359. return false;
  360. }
  361. void PerformMultipleSelection (int? firstSelected = null)
  362. {
  363. if (allowsMultipleSelection) {
  364. int first = Math.Min (firstSelected ?? selected, selected);
  365. int last = Math.Max (selected, firstSelected ?? selected);
  366. for (int i = first; i <= last; i++) {
  367. if ((canChooseFiles && infos [i].Item2 == false) ||
  368. (canChooseDirectories && infos [i].Item2 &&
  369. infos [i].Item1 != "..")) {
  370. infos [i] = (infos [i].Item1, infos [i].Item2, !infos [i].Item3);
  371. }
  372. }
  373. OnSelectionChanged ();
  374. SetNeedsDisplay ();
  375. }
  376. }
  377. string [] allowedFileTypes;
  378. public string [] AllowedFileTypes {
  379. get => allowedFileTypes;
  380. set {
  381. allowedFileTypes = value;
  382. Reload ();
  383. }
  384. }
  385. public string MakePath (string relativePath)
  386. {
  387. return Path.GetFullPath (Path.Combine (Directory.ToString (), relativePath));
  388. }
  389. public IReadOnlyList<string> FilePaths {
  390. get {
  391. if (allowsMultipleSelection) {
  392. var res = new List<string> ();
  393. foreach (var item in infos) {
  394. if (item.Item3)
  395. res.Add (MakePath (item.Item1));
  396. }
  397. if (res.Count == 0 && infos.Count > 0 && infos [selected].Item1 != "..") {
  398. res.Add (MakePath (infos [selected].Item1));
  399. }
  400. return res;
  401. } else {
  402. if (infos.Count == 0) {
  403. return null;
  404. }
  405. if (infos [selected].Item2) {
  406. if (canChooseDirectories) {
  407. var sel = infos [selected].Item1;
  408. return sel == ".." ? new List<string> () : new List<string> () { MakePath (infos [selected].Item1) };
  409. }
  410. return Array.Empty<string> ();
  411. } else {
  412. if (canChooseFiles) {
  413. return new List<string> () { MakePath (infos [selected].Item1) };
  414. }
  415. return Array.Empty<string> ();
  416. }
  417. }
  418. }
  419. }
  420. }
  421. /// <summary>
  422. /// Base class for the <see cref="OpenDialog"/> and the <see cref="SaveDialog"/>
  423. /// </summary>
  424. public class FileDialog : Dialog {
  425. Button prompt, cancel;
  426. Label nameFieldLabel, message, dirLabel;
  427. TextField dirEntry, nameEntry;
  428. internal DirListView dirListView;
  429. /// <summary>
  430. /// Initializes a new <see cref="FileDialog"/>.
  431. /// </summary>
  432. public FileDialog () : this (title: string.Empty, prompt: string.Empty, nameFieldLabel: string.Empty, message: string.Empty) { }
  433. /// <summary>
  434. /// Initializes a new instance of <see cref="FileDialog"/>
  435. /// </summary>
  436. /// <param name="title">The title.</param>
  437. /// <param name="prompt">The prompt.</param>
  438. /// <param name="nameFieldLabel">The name field label.</param>
  439. /// <param name="message">The message.</param>
  440. public FileDialog (ustring title, ustring prompt, ustring nameFieldLabel, ustring message) : base (title)//, Driver.Cols - 20, Driver.Rows - 5, null)
  441. {
  442. this.message = new Label (message) {
  443. X = 1,
  444. Y = 0,
  445. };
  446. Add (this.message);
  447. var msgLines = TextFormatter.MaxLines (message, Driver.Cols - 20);
  448. dirLabel = new Label ("Directory: ") {
  449. X = 1,
  450. Y = 1 + msgLines
  451. };
  452. dirEntry = new TextField ("") {
  453. X = Pos.Right (dirLabel),
  454. Y = 1 + msgLines,
  455. Width = Dim.Fill () - 1,
  456. TextChanged = (e) => {
  457. DirectoryPath = dirEntry.Text;
  458. }
  459. };
  460. Add (dirLabel, dirEntry);
  461. this.nameFieldLabel = new Label ("Open: ") {
  462. X = 6,
  463. Y = 3 + msgLines,
  464. };
  465. nameEntry = new TextField ("") {
  466. X = Pos.Left (dirEntry),
  467. Y = 3 + msgLines,
  468. Width = Dim.Fill () - 1
  469. };
  470. Add (this.nameFieldLabel, nameEntry);
  471. dirListView = new DirListView (this) {
  472. X = 1,
  473. Y = 3 + msgLines + 2,
  474. Width = Dim.Fill () - 1,
  475. Height = Dim.Fill () - 2,
  476. };
  477. DirectoryPath = Path.GetFullPath (Environment.CurrentDirectory);
  478. Add (dirListView);
  479. dirListView.DirectoryChanged = (dir) => { nameEntry.Text = ustring.Empty; dirEntry.Text = dir; };
  480. dirListView.FileChanged = (file) => nameEntry.Text = file == ".." ? "" : file;
  481. dirListView.SelectedChanged = (file) => nameEntry.Text = file.Item1 == ".." ? "" : file.Item1;
  482. this.cancel = new Button ("Cancel");
  483. this.cancel.Clicked += () => {
  484. canceled = true;
  485. Application.RequestStop ();
  486. };
  487. AddButton (cancel);
  488. this.prompt = new Button (prompt) {
  489. IsDefault = true,
  490. };
  491. this.prompt.Clicked += () => {
  492. canceled = false;
  493. Application.RequestStop ();
  494. };
  495. AddButton (this.prompt);
  496. Width = Dim.Percent (80);
  497. Height = Dim.Percent (80);
  498. // On success, we will set this to false.
  499. canceled = true;
  500. }
  501. internal bool canceled;
  502. ///<inheritdoc/>
  503. public override void WillPresent ()
  504. {
  505. base.WillPresent ();
  506. //SetFocus (nameEntry);
  507. }
  508. //protected override void Dispose (bool disposing)
  509. //{
  510. // message?.Dispose ();
  511. // base.Dispose (disposing);
  512. //}
  513. /// <summary>
  514. /// Gets or sets the prompt label for the <see cref="Button"/> displayed to the user
  515. /// </summary>
  516. /// <value>The prompt.</value>
  517. public ustring Prompt {
  518. get => prompt.Text;
  519. set {
  520. prompt.Text = value;
  521. }
  522. }
  523. /// <summary>
  524. /// Gets or sets the name field label.
  525. /// </summary>
  526. /// <value>The name field label.</value>
  527. public ustring NameFieldLabel {
  528. get => nameFieldLabel.Text;
  529. set {
  530. nameFieldLabel.Text = value;
  531. }
  532. }
  533. /// <summary>
  534. /// Gets or sets the message displayed to the user, defaults to nothing
  535. /// </summary>
  536. /// <value>The message.</value>
  537. public ustring Message {
  538. get => message.Text;
  539. set {
  540. message.Text = value;
  541. }
  542. }
  543. /// <summary>
  544. /// Gets or sets a value indicating whether this <see cref="FileDialog"/> can create directories.
  545. /// </summary>
  546. /// <value><c>true</c> if can create directories; otherwise, <c>false</c>.</value>
  547. public bool CanCreateDirectories { get; set; }
  548. /// <summary>
  549. /// Gets or sets a value indicating whether this <see cref="FileDialog"/> is extension hidden.
  550. /// </summary>
  551. /// <value><c>true</c> if is extension hidden; otherwise, <c>false</c>.</value>
  552. public bool IsExtensionHidden { get; set; }
  553. /// <summary>
  554. /// Gets or sets the directory path for this panel
  555. /// </summary>
  556. /// <value>The directory path.</value>
  557. public ustring DirectoryPath {
  558. get => dirEntry.Text;
  559. set {
  560. dirEntry.Text = value;
  561. dirListView.Directory = value;
  562. }
  563. }
  564. /// <summary>
  565. /// The array of filename extensions allowed, or null if all file extensions are allowed.
  566. /// </summary>
  567. /// <value>The allowed file types.</value>
  568. public string [] AllowedFileTypes {
  569. get => dirListView.AllowedFileTypes;
  570. set => dirListView.AllowedFileTypes = value;
  571. }
  572. /// <summary>
  573. /// Gets or sets a value indicating whether this <see cref="FileDialog"/> allows the file to be saved with a different extension
  574. /// </summary>
  575. /// <value><c>true</c> if allows other file types; otherwise, <c>false</c>.</value>
  576. public bool AllowsOtherFileTypes { get; set; }
  577. /// <summary>
  578. /// The File path that is currently shown on the panel
  579. /// </summary>
  580. /// <value>The absolute file path for the file path entered.</value>
  581. public ustring FilePath {
  582. get => dirListView.MakePath (nameEntry.Text.ToString ());
  583. set {
  584. nameEntry.Text = Path.GetFileName (value.ToString ());
  585. }
  586. }
  587. /// <summary>
  588. /// Check if the dialog was or not canceled.
  589. /// </summary>
  590. public bool Canceled { get => canceled; }
  591. }
  592. /// <summary>
  593. /// The <see cref="SaveDialog"/> provides an interactive dialog box for users to pick a file to
  594. /// save.
  595. /// </summary>
  596. /// <remarks>
  597. /// <para>
  598. /// To use, create an instance of <see cref="SaveDialog"/>, and pass it to
  599. /// <see cref="Application.Run()"/>. This will run the dialog modally,
  600. /// and when this returns, the <see cref="FileName"/>property will contain the selected file name or
  601. /// null if the user canceled.
  602. /// </para>
  603. /// </remarks>
  604. public class SaveDialog : FileDialog {
  605. /// <summary>
  606. /// Initializes a new <see cref="SaveDialog"/>.
  607. /// </summary>
  608. public SaveDialog () : this (title: string.Empty, message: string.Empty) { }
  609. /// <summary>
  610. /// Initializes a new <see cref="SaveDialog"/>.
  611. /// </summary>
  612. /// <param name="title">The title.</param>
  613. /// <param name="message">The message.</param>
  614. public SaveDialog (ustring title, ustring message) : base (title, prompt: "Save", nameFieldLabel: "Save as:", message: message) { }
  615. /// <summary>
  616. /// Gets the name of the file the user selected for saving, or null
  617. /// if the user canceled the <see cref="SaveDialog"/>.
  618. /// </summary>
  619. /// <value>The name of the file.</value>
  620. public ustring FileName {
  621. get {
  622. if (canceled)
  623. return null;
  624. return Path.GetFileName (FilePath.ToString ());
  625. }
  626. }
  627. }
  628. /// <summary>
  629. /// The <see cref="OpenDialog"/>provides an interactive dialog box for users to select files or directories.
  630. /// </summary>
  631. /// <remarks>
  632. /// <para>
  633. /// The open dialog can be used to select files for opening, it can be configured to allow
  634. /// multiple items to be selected (based on the AllowsMultipleSelection) variable and
  635. /// you can control whether this should allow files or directories to be selected.
  636. /// </para>
  637. /// <para>
  638. /// To use, create an instance of <see cref="OpenDialog"/>, and pass it to
  639. /// <see cref="Application.Run()"/>. This will run the dialog modally,
  640. /// and when this returns, the list of filds will be available on the <see cref="FilePaths"/> property.
  641. /// </para>
  642. /// <para>
  643. /// To select more than one file, users can use the spacebar, or control-t.
  644. /// </para>
  645. /// </remarks>
  646. public class OpenDialog : FileDialog {
  647. /// <summary>
  648. /// Initializes a new <see cref="OpenDialog"/>.
  649. /// </summary>
  650. public OpenDialog () : this (title: string.Empty, message: string.Empty) { }
  651. /// <summary>
  652. /// Initializes a new <see cref="OpenDialog"/>.
  653. /// </summary>
  654. /// <param name="title"></param>
  655. /// <param name="message"></param>
  656. public OpenDialog (ustring title, ustring message) : base (title, prompt: "Open", nameFieldLabel: "Open", message: message)
  657. {
  658. }
  659. /// <summary>
  660. /// Gets or sets a value indicating whether this <see cref="Terminal.Gui.OpenDialog"/> can choose files.
  661. /// </summary>
  662. /// <value><c>true</c> if can choose files; otherwise, <c>false</c>. Defaults to <c>true</c></value>
  663. public bool CanChooseFiles {
  664. get => dirListView.canChooseFiles;
  665. set {
  666. dirListView.canChooseFiles = value;
  667. dirListView.Reload ();
  668. }
  669. }
  670. /// <summary>
  671. /// Gets or sets a value indicating whether this <see cref="OpenDialog"/> can choose directories.
  672. /// </summary>
  673. /// <value><c>true</c> if can choose directories; otherwise, <c>false</c> defaults to <c>false</c>.</value>
  674. public bool CanChooseDirectories {
  675. get => dirListView.canChooseDirectories;
  676. set {
  677. dirListView.canChooseDirectories = value;
  678. dirListView.Reload ();
  679. }
  680. }
  681. /// <summary>
  682. /// Gets or sets a value indicating whether this <see cref="OpenDialog"/> allows multiple selection.
  683. /// </summary>
  684. /// <value><c>true</c> if allows multiple selection; otherwise, <c>false</c>, defaults to false.</value>
  685. public bool AllowsMultipleSelection {
  686. get => dirListView.allowsMultipleSelection;
  687. set {
  688. dirListView.allowsMultipleSelection = value;
  689. dirListView.Reload ();
  690. }
  691. }
  692. /// <summary>
  693. /// Returns the selected files, or an empty list if nothing has been selected
  694. /// </summary>
  695. /// <value>The file paths.</value>
  696. public IReadOnlyList<string> FilePaths {
  697. get => dirListView.FilePaths;
  698. }
  699. }
  700. }