PolyUIFileDialog.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /*
  2. Copyright (C) 2012 by Ivan Safrin
  3. Permission is hereby granted, free of charge, to any person obtaining a copy
  4. of this software and associated documentation files (the "Software"), to deal
  5. in the Software without restriction, including without limitation the rights
  6. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. copies of the Software, and to permit persons to whom the Software is
  8. furnished to do so, subject to the following conditions:
  9. The above copyright notice and this permission notice shall be included in
  10. all copies or substantial portions of the Software.
  11. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  12. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  13. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  14. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  15. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  16. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  17. THE SOFTWARE.
  18. */
  19. #pragma once
  20. #include "PolyGlobals.h"
  21. #include "PolyUIWindow.h"
  22. #include "PolyUIButton.h"
  23. #include "PolyUIScrollContainer.h"
  24. #include "PolyUITextInput.h"
  25. #include "OSBasics.h"
  26. #include "PolyInputEvent.h"
  27. namespace Polycode {
  28. class CreateFolderWindow : public UIWindow {
  29. public:
  30. CreateFolderWindow();
  31. ~CreateFolderWindow();
  32. UIButton *okButton;
  33. UIButton *cancelButton;
  34. UITextInput *nameInput;
  35. };
  36. class UIFileDialogEntry : public UIElement {
  37. public:
  38. UIFileDialogEntry(OSFileEntry entry, bool canSelect, int width=340, bool isPlace=false);
  39. ~UIFileDialogEntry();
  40. void Select();
  41. void Deselect();
  42. bool canSelect;
  43. ScreenShape *bg;
  44. ScreenLabel *label;
  45. OSFileEntry fileEntry;
  46. ScreenImage *icon;
  47. };
  48. /**
  49. * A dialog that allows the user to choose a file or directory from a
  50. * file system.
  51. */
  52. class UIFileDialog : public UIWindow {
  53. public:
  54. /**
  55. * Create a new file dialog.
  56. *
  57. * @param baseDir The top-level directory, only entries in this directory and below can be selected.
  58. * @param foldersOnly If true, directories will be selected. If false, files will be selected.
  59. * @param extensions A list of accepted file extensions.
  60. * @param allowMultiple If true, multiple entries can be selected at once.
  61. */
  62. UIFileDialog(String baseDir, bool foldersOnly, std::vector<String> extensions, bool allowMultiple);
  63. virtual ~UIFileDialog();
  64. void onClose();
  65. void handleEvent(Event *event);
  66. /**
  67. * Clears all entries in the file dialog.
  68. */
  69. void clearEntries();
  70. /**
  71. * Set a new top-level directory to display.
  72. */
  73. void showFolder(String folderPath);
  74. /**
  75. * Returns whether a file with a specific file extension
  76. * can be selected.
  77. *
  78. * @param extension The file extension to be tested.
  79. */
  80. bool canOpen(String extension);
  81. void addToSidebar(String path, String name);
  82. void Update();
  83. /**
  84. * Get the selected entry.
  85. *
  86. * @return Full path of the selected entry
  87. * (base path + relative path to top level directory)
  88. */
  89. String getSelection();
  90. protected:
  91. String selection;
  92. String currentFolderPath;
  93. UIFileDialogEntry *currentEntry;
  94. bool foldersOnly;
  95. bool allowMultiple;
  96. bool doChangeFolder;
  97. String newPath;
  98. UIButton *okButton;
  99. UIButton *cancelButton;
  100. UIButton *newFolderButton;
  101. CreateFolderWindow *createFolderWindow;
  102. UIScrollContainer *scrollContainer;
  103. std::vector<String> extensions;
  104. std::vector<UIFileDialogEntry*> entries;
  105. std::vector<UIFileDialogEntry*> sideBarEntries;
  106. UIElement *entryHolder;
  107. };
  108. }