FileSelector.h 6.2 KB

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