FileSelector.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. //
  2. // Copyright (c) 2008-2020 the Urho3D project.
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #pragma once
  23. #include "../Core/Object.h"
  24. namespace Urho3D
  25. {
  26. class Button;
  27. class DropDownList;
  28. class Font;
  29. class LineEdit;
  30. class ListView;
  31. class ResourceCache;
  32. class Text;
  33. class UIElement;
  34. class Window;
  35. class XMLFile;
  36. /// %File selector's list entry (file or directory).
  37. struct FileSelectorEntry
  38. {
  39. /// Name.
  40. String name_;
  41. /// Directory flag.
  42. bool directory_;
  43. };
  44. /// %File selector dialog.
  45. class URHO3D_API FileSelector : public Object
  46. {
  47. URHO3D_OBJECT(FileSelector, Object);
  48. public:
  49. /// Construct.
  50. explicit FileSelector(Context* context);
  51. /// Destruct.
  52. ~FileSelector() override;
  53. /// Register object factory.
  54. static void RegisterObject(Context* context);
  55. /// Set fileselector UI style.
  56. /// @property
  57. void SetDefaultStyle(XMLFile* style);
  58. /// Set title text.
  59. /// @property
  60. void SetTitle(const String& text);
  61. /// Set button texts.
  62. void SetButtonTexts(const String& okText, const String& cancelText);
  63. /// Set current path.
  64. /// @property
  65. void SetPath(const String& path);
  66. /// Set current filename.
  67. /// @property
  68. void SetFileName(const String& fileName);
  69. /// Set filters.
  70. void SetFilters(const Vector<String>& filters, unsigned defaultIndex);
  71. /// Set directory selection mode. Default false.
  72. /// @property
  73. void SetDirectoryMode(bool enable);
  74. /// Update elements to layout properly. Call this after manually adjusting the sub-elements.
  75. void UpdateElements();
  76. /// Return the UI style file.
  77. /// @property
  78. XMLFile* GetDefaultStyle() const;
  79. /// Return fileselector window.
  80. /// @property
  81. Window* GetWindow() const { return window_; }
  82. /// Return window title text element.
  83. /// @property
  84. Text* GetTitleText() const { return titleText_; }
  85. /// Return file list.
  86. /// @property
  87. ListView* GetFileList() const { return fileList_; }
  88. /// Return path editor.
  89. /// @property
  90. LineEdit* GetPathEdit() const { return pathEdit_; }
  91. /// Return filename editor.
  92. /// @property
  93. LineEdit* GetFileNameEdit() const { return fileNameEdit_; }
  94. /// Return filter dropdown.
  95. /// @property
  96. DropDownList* GetFilterList() const { return filterList_; }
  97. /// Return OK button.
  98. /// @property{get_okButton}
  99. Button* GetOKButton() const { return okButton_; }
  100. /// Return cancel button.
  101. /// @property
  102. Button* GetCancelButton() const { return cancelButton_; }
  103. /// Return close button.
  104. Button* GetCloseButton() const { return closeButton_; }
  105. /// Return window title.
  106. /// @property
  107. const String& GetTitle() const;
  108. /// Return current path.
  109. /// @property
  110. const String& GetPath() const { return path_; }
  111. /// Return current filename.
  112. /// @property
  113. const String& GetFileName() const;
  114. /// Return current filter.
  115. /// @property
  116. const String& GetFilter() const;
  117. /// Return current filter index.
  118. /// @property
  119. unsigned GetFilterIndex() const;
  120. /// Return directory mode flag.
  121. /// @property
  122. bool GetDirectoryMode() const { return directoryMode_; }
  123. private:
  124. /// Set the text of an edit field and ignore the resulting event.
  125. void SetLineEditText(LineEdit* edit, const String& text);
  126. /// Refresh the directory listing.
  127. void RefreshFiles();
  128. /// Enter a directory or confirm a file. Return true if a directory entered.
  129. bool EnterFile();
  130. /// Handle filter changed.
  131. void HandleFilterChanged(StringHash eventType, VariantMap& eventData);
  132. /// Handle path edited.
  133. void HandlePathChanged(StringHash eventType, VariantMap& eventData);
  134. /// Handle file selected from the list.
  135. void HandleFileSelected(StringHash eventType, VariantMap& eventData);
  136. /// Handle file doubleclicked from the list (enter directory / OK the file selection).
  137. void HandleFileDoubleClicked(StringHash eventType, VariantMap& eventData);
  138. /// Handle file list key pressed.
  139. void HandleFileListKey(StringHash eventType, VariantMap& eventData);
  140. /// Handle OK button pressed.
  141. void HandleOKPressed(StringHash eventType, VariantMap& eventData);
  142. /// Handle cancel button pressed.
  143. void HandleCancelPressed(StringHash eventType, VariantMap& eventData);
  144. /// Fileselector window.
  145. SharedPtr<Window> window_;
  146. /// Title layout.
  147. UIElement* titleLayout;
  148. /// Window title text.
  149. Text* titleText_;
  150. /// File list.
  151. ListView* fileList_;
  152. /// Path editor.
  153. LineEdit* pathEdit_;
  154. /// Filename editor.
  155. LineEdit* fileNameEdit_;
  156. /// Filter dropdown.
  157. DropDownList* filterList_;
  158. /// OK button.
  159. Button* okButton_;
  160. /// OK button text.
  161. Text* okButtonText_;
  162. /// Cancel button.
  163. Button* cancelButton_;
  164. /// Cancel button text.
  165. Text* cancelButtonText_;
  166. /// Close button.
  167. Button* closeButton_;
  168. /// Filename and filter layout.
  169. UIElement* fileNameLayout_;
  170. /// Separator layout.
  171. UIElement* separatorLayout_;
  172. /// Button layout.
  173. UIElement* buttonLayout_;
  174. /// Current directory.
  175. String path_;
  176. /// Filters.
  177. Vector<String> filters_;
  178. /// File entries.
  179. Vector<FileSelectorEntry> fileEntries_;
  180. /// Filter used to get the file list.
  181. String lastUsedFilter_;
  182. /// Directory mode flag.
  183. bool directoryMode_;
  184. /// Ignore events flag, used when changing line edits manually.
  185. bool ignoreEvents_;
  186. };
  187. }