FileSelector.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2011 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. std::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 std::string& text);
  59. /// Set button texts
  60. void SetButtonTexts(const std::string& okText, const std::string& cancelText);
  61. /// Set current path
  62. void SetPath(const std::string& path);
  63. /// Set current filename
  64. void SetFileName(const std::string& fileName);
  65. /// Set filters
  66. void SetFilters(const std::vector<std::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
  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 current path
  92. const std::string& GetPath() const { return path_; }
  93. /// Return current filename
  94. const std::string& GetFileName() const;
  95. /// Return current filter
  96. const std::string& GetFilter() const;
  97. /// Return current filter index
  98. unsigned GetFilterIndex() const;
  99. /// Return directory mode flag
  100. bool GetDirectoryMode() const { return directoryMode_; }
  101. private:
  102. /// Set the text of an edit field and ignore the resulting event
  103. void SetLineEditText(LineEdit* edit, const std::string& text);
  104. /// Refresh the directory listing
  105. void RefreshFiles();
  106. /// Enter a directory or confirm a file. Return true if a directory entered
  107. bool EnterFile();
  108. /// Handle filter changed
  109. void HandleFilterChanged(StringHash eventType, VariantMap& eventData);
  110. /// Handle path edited
  111. void HandlePathChanged(StringHash eventType, VariantMap& eventData);
  112. /// Handle file selected from the list
  113. void HandleFileSelected(StringHash eventType, VariantMap& eventData);
  114. /// Handle file doubleclicked from the list (enter directory / OK the file selection)
  115. void HandleFileDoubleClicked(StringHash eventType, VariantMap& eventData);
  116. /// Handle file list key pressed
  117. void HandleFileListKey(StringHash eventType, VariantMap& eventData);
  118. /// Handle OK button pressed
  119. void HandleOKPressed(StringHash eventType, VariantMap& eventData);
  120. /// Handle cancel button pressed
  121. void HandleCancelPressed(StringHash eventType, VariantMap& eventData);
  122. /// UI subsystem
  123. SharedPtr<UI> ui_;
  124. /// UI style file
  125. SharedPtr<XMLFile> style_;
  126. /// Fileselector window
  127. SharedPtr<Window> window_;
  128. /// Title layout
  129. SharedPtr<UIElement> titleLayout;
  130. /// Window title text
  131. SharedPtr<Text> titleText_;
  132. /// File list
  133. SharedPtr<ListView> fileList_;
  134. /// Path editor
  135. SharedPtr<LineEdit> pathEdit_;
  136. /// Filename editor
  137. SharedPtr<LineEdit> fileNameEdit_;
  138. /// Filter dropdown
  139. SharedPtr<DropDownList> filterList_;
  140. /// OK button
  141. SharedPtr<Button> okButton_;
  142. /// OK button text
  143. SharedPtr<Text> okButtonText_;
  144. /// Cancel button
  145. SharedPtr<Button> cancelButton_;
  146. /// Cancel button text
  147. SharedPtr<Text> cancelButtonText_;
  148. /// Close button
  149. SharedPtr<Button> closeButton_;
  150. /// Filename and filter layout
  151. SharedPtr<UIElement> fileNameLayout_;
  152. /// Button layout
  153. SharedPtr<UIElement> buttonLayout_;
  154. /// Current path
  155. std::string path_;
  156. /// Filters
  157. std::vector<std::string> filters_;
  158. /// File entries
  159. std::vector<FileSelectorEntry> fileEntries_;
  160. /// Filter used to get the file list
  161. std::string lastUsedFilter_;
  162. /// Directory mode flag
  163. bool directoryMode_;
  164. /// Ignore events flag, used when changing line edits manually
  165. bool ignoreEvents_;
  166. };