FileSelector.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. //
  2. // Copyright (c) 2008-2022 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. /// @nobind
  55. static void RegisterObject(Context* context);
  56. /// Set fileselector UI style.
  57. /// @property
  58. void SetDefaultStyle(XMLFile* style);
  59. /// Set title text.
  60. /// @property
  61. void SetTitle(const String& text);
  62. /// Set button texts.
  63. void SetButtonTexts(const String& okText, const String& cancelText);
  64. /// Set current path.
  65. /// @property
  66. void SetPath(const String& path);
  67. /// Set current filename.
  68. /// @property
  69. void SetFileName(const String& fileName);
  70. /// Set filters.
  71. void SetFilters(const Vector<String>& filters, unsigned defaultIndex);
  72. /// Set directory selection mode. Default false.
  73. /// @property
  74. void SetDirectoryMode(bool enable);
  75. /// Update elements to layout properly. Call this after manually adjusting the sub-elements.
  76. void UpdateElements();
  77. /// Return the UI style file.
  78. /// @property
  79. XMLFile* GetDefaultStyle() const;
  80. /// Return fileselector window.
  81. /// @property
  82. Window* GetWindow() const { return window_; }
  83. /// Return window title text element.
  84. /// @property
  85. Text* GetTitleText() const { return titleText_; }
  86. /// Return file list.
  87. /// @property
  88. ListView* GetFileList() const { return fileList_; }
  89. /// Return path editor.
  90. /// @property
  91. LineEdit* GetPathEdit() const { return pathEdit_; }
  92. /// Return filename editor.
  93. /// @property
  94. LineEdit* GetFileNameEdit() const { return fileNameEdit_; }
  95. /// Return filter dropdown.
  96. /// @property
  97. DropDownList* GetFilterList() const { return filterList_; }
  98. /// Return OK button.
  99. /// @property{get_okButton}
  100. Button* GetOKButton() const { return okButton_; }
  101. /// Return cancel button.
  102. /// @property
  103. Button* GetCancelButton() const { return cancelButton_; }
  104. /// Return close button.
  105. Button* GetCloseButton() const { return closeButton_; }
  106. /// Return window title.
  107. /// @property
  108. const String& GetTitle() const;
  109. /// Return current path.
  110. /// @property
  111. const String& GetPath() const { return path_; }
  112. /// Return current filename.
  113. /// @property
  114. const String& GetFileName() const;
  115. /// Return current filter.
  116. /// @property
  117. const String& GetFilter() const;
  118. /// Return current filter index.
  119. /// @property
  120. unsigned GetFilterIndex() const;
  121. /// Return directory mode flag.
  122. /// @property
  123. bool GetDirectoryMode() const { return directoryMode_; }
  124. private:
  125. /// Set the text of an edit field and ignore the resulting event.
  126. void SetLineEditText(LineEdit* edit, const String& text);
  127. /// Refresh the directory listing.
  128. void RefreshFiles();
  129. /// Enter a directory or confirm a file. Return true if a directory entered.
  130. bool EnterFile();
  131. /// Handle filter changed.
  132. void HandleFilterChanged(StringHash eventType, VariantMap& eventData);
  133. /// Handle path edited.
  134. void HandlePathChanged(StringHash eventType, VariantMap& eventData);
  135. /// Handle file selected from the list.
  136. void HandleFileSelected(StringHash eventType, VariantMap& eventData);
  137. /// Handle file doubleclicked from the list (enter directory / OK the file selection).
  138. void HandleFileDoubleClicked(StringHash eventType, VariantMap& eventData);
  139. /// Handle file list key pressed.
  140. void HandleFileListKey(StringHash eventType, VariantMap& eventData);
  141. /// Handle OK button pressed.
  142. void HandleOKPressed(StringHash eventType, VariantMap& eventData);
  143. /// Handle cancel button pressed.
  144. void HandleCancelPressed(StringHash eventType, VariantMap& eventData);
  145. /// Fileselector window.
  146. SharedPtr<Window> window_;
  147. /// Title layout.
  148. UIElement* titleLayout;
  149. /// Window title text.
  150. Text* titleText_;
  151. /// File list.
  152. ListView* fileList_;
  153. /// Path editor.
  154. LineEdit* pathEdit_;
  155. /// Filename editor.
  156. LineEdit* fileNameEdit_;
  157. /// Filter dropdown.
  158. DropDownList* filterList_;
  159. /// OK button.
  160. Button* okButton_;
  161. /// OK button text.
  162. Text* okButtonText_;
  163. /// Cancel button.
  164. Button* cancelButton_;
  165. /// Cancel button text.
  166. Text* cancelButtonText_;
  167. /// Close button.
  168. Button* closeButton_;
  169. /// Filename and filter layout.
  170. UIElement* fileNameLayout_;
  171. /// Separator layout.
  172. UIElement* separatorLayout_;
  173. /// Button layout.
  174. UIElement* buttonLayout_;
  175. /// Current directory.
  176. String path_;
  177. /// Filters.
  178. Vector<String> filters_;
  179. /// File entries.
  180. Vector<FileSelectorEntry> fileEntries_;
  181. /// Filter used to get the file list.
  182. String lastUsedFilter_;
  183. /// Directory mode flag.
  184. bool directoryMode_;
  185. /// Ignore events flag, used when changing line edits manually.
  186. bool ignoreEvents_;
  187. };
  188. }