The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

181 lines
6.6KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2020 - Raw Material Software Limited
  5. JUCE is an open source library subject to commercial or open-source
  6. licensing.
  7. By using JUCE, you agree to the terms of both the JUCE 6 End-User License
  8. Agreement and JUCE Privacy Policy (both effective as of the 16th June 2020).
  9. End User License Agreement: www.juce.com/juce-6-licence
  10. Privacy Policy: www.juce.com/juce-privacy-policy
  11. Or: You may also use this code under the terms of the GPL v3 (see
  12. www.gnu.org/licenses).
  13. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  14. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  15. DISCLAIMED.
  16. ==============================================================================
  17. */
  18. namespace juce
  19. {
  20. //==============================================================================
  21. /**
  22. Manages a set of files for use as a list of recently-opened documents.
  23. This is a handy class for holding your list of recently-opened documents, with
  24. helpful methods for things like purging any non-existent files, automatically
  25. adding them to a menu, and making persistence easy.
  26. @see File, FileBasedDocument
  27. @tags{GUI}
  28. */
  29. class JUCE_API RecentlyOpenedFilesList
  30. {
  31. public:
  32. //==============================================================================
  33. /** Creates an empty list.
  34. */
  35. RecentlyOpenedFilesList();
  36. /** Destructor. */
  37. ~RecentlyOpenedFilesList();
  38. //==============================================================================
  39. /** Sets a limit for the number of files that will be stored in the list.
  40. When addFile() is called, then if there is no more space in the list, the
  41. least-recently added file will be dropped.
  42. @see getMaxNumberOfItems
  43. */
  44. void setMaxNumberOfItems (int newMaxNumber);
  45. /** Returns the number of items that this list will store.
  46. @see setMaxNumberOfItems
  47. */
  48. int getMaxNumberOfItems() const noexcept { return maxNumberOfItems; }
  49. /** Returns the number of files in the list.
  50. The most recently added file is always at index 0.
  51. */
  52. int getNumFiles() const;
  53. /** Returns one of the files in the list.
  54. The most recently added file is always at index 0.
  55. */
  56. File getFile (int index) const;
  57. /** Returns an array of all the absolute pathnames in the list.
  58. */
  59. const StringArray& getAllFilenames() const noexcept { return files; }
  60. /** Clears all the files from the list. */
  61. void clear();
  62. /** Adds a file to the list.
  63. The file will be added at index 0. If this file is already in the list, it will
  64. be moved up to index 0, but a file can only appear once in the list.
  65. If the list already contains the maximum number of items that is permitted, the
  66. least-recently added file will be dropped from the end.
  67. */
  68. void addFile (const File& file);
  69. /** Removes a file from the list. */
  70. void removeFile (const File& file);
  71. /** Checks each of the files in the list, removing any that don't exist.
  72. You might want to call this after reloading a list of files, or before putting them
  73. on a menu.
  74. */
  75. void removeNonExistentFiles();
  76. /** Tells the OS to add a file to the OS-managed list of recent documents for this app.
  77. Not all OSes maintain a list of recent files for an application, so this
  78. function will have no effect on some OSes. Currently it's just implemented for OSX.
  79. */
  80. static void registerRecentFileNatively (const File& file);
  81. /** Tells the OS to remove a file from the OS-managed list of recent documents for this app.
  82. Not all OSes maintain a list of recent files for an application, so this
  83. function will have no effect on some OSes. Currently it's just implemented for OSX.
  84. */
  85. static void forgetRecentFileNatively (const File& file);
  86. /** Tells the OS to clear the OS-managed list of recent documents for this app.
  87. Not all OSes maintain a list of recent files for an application, so this
  88. function will have no effect on some OSes. Currently it's just implemented for OSX.
  89. */
  90. static void clearRecentFilesNatively();
  91. //==============================================================================
  92. /** Adds entries to a menu, representing each of the files in the list.
  93. This is handy for creating an "open recent file..." menu in your app. The
  94. menu items are numbered consecutively starting with the baseItemId value,
  95. and can either be added as complete pathnames, or just the last part of the
  96. filename.
  97. If dontAddNonExistentFiles is true, then each file will be checked and only those
  98. that exist will be added.
  99. If filesToAvoid is not a nullptr, then it is considered to be a zero-terminated array
  100. of pointers to file objects. Any files that appear in this list will not be added to
  101. the menu - the reason for this is that you might have a number of files already open,
  102. so might not want these to be shown in the menu.
  103. It returns the number of items that were added.
  104. */
  105. int createPopupMenuItems (PopupMenu& menuToAddItemsTo,
  106. int baseItemId,
  107. bool showFullPaths,
  108. bool dontAddNonExistentFiles,
  109. const File** filesToAvoid = nullptr);
  110. //==============================================================================
  111. /** Returns a string that encapsulates all the files in the list.
  112. The string that is returned can later be passed into restoreFromString() in
  113. order to recreate the list. This is handy for persisting your list, e.g. in
  114. a PropertiesFile object.
  115. @see restoreFromString
  116. */
  117. String toString() const;
  118. /** Restores the list from a previously stringified version of the list.
  119. Pass in a stringified version created with toString() in order to persist/restore
  120. your list.
  121. @see toString
  122. */
  123. void restoreFromString (const String& stringifiedVersion);
  124. private:
  125. //==============================================================================
  126. StringArray files;
  127. int maxNumberOfItems;
  128. JUCE_LEAK_DETECTOR (RecentlyOpenedFilesList)
  129. };
  130. } // namespace juce