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
5.1KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2022 - 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 7 End-User License
  8. Agreement and JUCE Privacy Policy.
  9. End User License Agreement: www.juce.com/juce-7-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. RecentlyOpenedFilesList::RecentlyOpenedFilesList()
  21. : maxNumberOfItems (10)
  22. {
  23. }
  24. //==============================================================================
  25. void RecentlyOpenedFilesList::setMaxNumberOfItems (const int newMaxNumber)
  26. {
  27. maxNumberOfItems = jmax (1, newMaxNumber);
  28. files.removeRange (maxNumberOfItems, getNumFiles());
  29. }
  30. int RecentlyOpenedFilesList::getNumFiles() const
  31. {
  32. return files.size();
  33. }
  34. File RecentlyOpenedFilesList::getFile (const int index) const
  35. {
  36. return File (files [index]);
  37. }
  38. void RecentlyOpenedFilesList::clear()
  39. {
  40. files.clear();
  41. }
  42. void RecentlyOpenedFilesList::addFile (const File& file)
  43. {
  44. removeFile (file);
  45. files.insert (0, file.getFullPathName());
  46. setMaxNumberOfItems (maxNumberOfItems);
  47. }
  48. void RecentlyOpenedFilesList::removeFile (const File& file)
  49. {
  50. files.removeString (file.getFullPathName());
  51. }
  52. void RecentlyOpenedFilesList::removeNonExistentFiles()
  53. {
  54. for (int i = getNumFiles(); --i >= 0;)
  55. if (! getFile (i).exists())
  56. files.remove (i);
  57. }
  58. //==============================================================================
  59. int RecentlyOpenedFilesList::createPopupMenuItems (PopupMenu& menuToAddTo,
  60. const int baseItemId,
  61. const bool showFullPaths,
  62. const bool dontAddNonExistentFiles,
  63. const File** filesToAvoid)
  64. {
  65. int num = 0;
  66. for (int i = 0; i < getNumFiles(); ++i)
  67. {
  68. const File f (getFile (i));
  69. if ((! dontAddNonExistentFiles) || f.exists())
  70. {
  71. bool needsAvoiding = false;
  72. if (filesToAvoid != nullptr)
  73. {
  74. for (const File** avoid = filesToAvoid; *avoid != nullptr; ++avoid)
  75. {
  76. if (f == **avoid)
  77. {
  78. needsAvoiding = true;
  79. break;
  80. }
  81. }
  82. }
  83. if (! needsAvoiding)
  84. {
  85. menuToAddTo.addItem (baseItemId + i,
  86. showFullPaths ? f.getFullPathName()
  87. : f.getFileName());
  88. ++num;
  89. }
  90. }
  91. }
  92. return num;
  93. }
  94. //==============================================================================
  95. String RecentlyOpenedFilesList::toString() const
  96. {
  97. return files.joinIntoString ("\n");
  98. }
  99. void RecentlyOpenedFilesList::restoreFromString (const String& stringifiedVersion)
  100. {
  101. clear();
  102. files.addLines (stringifiedVersion);
  103. setMaxNumberOfItems (maxNumberOfItems);
  104. }
  105. //==============================================================================
  106. void RecentlyOpenedFilesList::registerRecentFileNatively ([[maybe_unused]] const File& file)
  107. {
  108. #if JUCE_MAC
  109. JUCE_AUTORELEASEPOOL
  110. {
  111. [[NSDocumentController sharedDocumentController] noteNewRecentDocumentURL: createNSURLFromFile (file)];
  112. }
  113. #endif
  114. }
  115. void RecentlyOpenedFilesList::forgetRecentFileNatively ([[maybe_unused]] const File& file)
  116. {
  117. #if JUCE_MAC
  118. JUCE_AUTORELEASEPOOL
  119. {
  120. // for some reason, OSX doesn't provide a method to just remove a single file
  121. // from the recent list, so we clear them all and add them back excluding
  122. // the specified file
  123. auto sharedDocController = [NSDocumentController sharedDocumentController];
  124. auto recentDocumentURLs = [sharedDocController recentDocumentURLs];
  125. [sharedDocController clearRecentDocuments: nil];
  126. auto* nsFile = createNSURLFromFile (file);
  127. auto reverseEnumerator = [recentDocumentURLs reverseObjectEnumerator];
  128. for (NSURL* url : reverseEnumerator)
  129. if (! [url isEqual:nsFile])
  130. [sharedDocController noteNewRecentDocumentURL:url];
  131. }
  132. #endif
  133. }
  134. void RecentlyOpenedFilesList::clearRecentFilesNatively()
  135. {
  136. #if JUCE_MAC
  137. JUCE_AUTORELEASEPOOL
  138. {
  139. [[NSDocumentController sharedDocumentController] clearRecentDocuments: nil];
  140. }
  141. #endif
  142. }
  143. } // namespace juce