FileSelector.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2012 Lasse Öörni
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. //
  23. #pragma once
  24. #include "Object.h"
  25. class Button;
  26. class DropDownList;
  27. class Font;
  28. class LineEdit;
  29. class ListView;
  30. class ResourceCache;
  31. class Text;
  32. class UI;
  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 FileSelector : public Object
  46. {
  47. OBJECT(FileSelector);
  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 SetStyle(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* GetStyle() const { return style_; }
  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. /// UI subsystem.
  125. SharedPtr<UI> ui_;
  126. /// UI style file.
  127. SharedPtr<XMLFile> style_;
  128. /// Fileselector window.
  129. SharedPtr<Window> window_;
  130. /// Title layout.
  131. SharedPtr<UIElement> titleLayout;
  132. /// Window title text.
  133. SharedPtr<Text> titleText_;
  134. /// File list.
  135. SharedPtr<ListView> fileList_;
  136. /// Path editor.
  137. SharedPtr<LineEdit> pathEdit_;
  138. /// Filename editor.
  139. SharedPtr<LineEdit> fileNameEdit_;
  140. /// Filter dropdown.
  141. SharedPtr<DropDownList> filterList_;
  142. /// OK button.
  143. SharedPtr<Button> okButton_;
  144. /// OK button text.
  145. SharedPtr<Text> okButtonText_;
  146. /// Cancel button.
  147. SharedPtr<Button> cancelButton_;
  148. /// Cancel button text.
  149. SharedPtr<Text> cancelButtonText_;
  150. /// Close button.
  151. SharedPtr<Button> closeButton_;
  152. /// Filename and filter layout.
  153. SharedPtr<UIElement> fileNameLayout_;
  154. /// Button layout.
  155. SharedPtr<UIElement> buttonLayout_;
  156. /// Current directory.
  157. String path_;
  158. /// Filters.
  159. Vector<String> filters_;
  160. /// File entries.
  161. Vector<FileSelectorEntry> fileEntries_;
  162. /// Filter used to get the file list.
  163. String lastUsedFilter_;
  164. /// Directory mode flag.
  165. bool directoryMode_;
  166. /// Ignore events flag, used when changing line edits manually.
  167. bool ignoreEvents_;
  168. };