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.

190 lines
5.3KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2017 - ROLI Ltd.
  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 5 End-User License
  8. Agreement and JUCE 5 Privacy Policy (both updated and effective as of the
  9. 27th April 2017).
  10. End User License Agreement: www.juce.com/juce-5-licence
  11. Privacy Policy: www.juce.com/juce-5-privacy-policy
  12. Or: You may also use this code under the terms of the GPL v3 (see
  13. www.gnu.org/licenses).
  14. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  15. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  16. DISCLAIMED.
  17. ==============================================================================
  18. */
  19. namespace juce
  20. {
  21. RecentlyOpenedFilesList::RecentlyOpenedFilesList()
  22. : maxNumberOfItems (10)
  23. {
  24. }
  25. RecentlyOpenedFilesList::~RecentlyOpenedFilesList()
  26. {
  27. }
  28. //==============================================================================
  29. void RecentlyOpenedFilesList::setMaxNumberOfItems (const int newMaxNumber)
  30. {
  31. maxNumberOfItems = jmax (1, newMaxNumber);
  32. files.removeRange (maxNumberOfItems, getNumFiles());
  33. }
  34. int RecentlyOpenedFilesList::getNumFiles() const
  35. {
  36. return files.size();
  37. }
  38. File RecentlyOpenedFilesList::getFile (const int index) const
  39. {
  40. return File (files [index]);
  41. }
  42. void RecentlyOpenedFilesList::clear()
  43. {
  44. files.clear();
  45. }
  46. void RecentlyOpenedFilesList::addFile (const File& file)
  47. {
  48. removeFile (file);
  49. files.insert (0, file.getFullPathName());
  50. setMaxNumberOfItems (maxNumberOfItems);
  51. }
  52. void RecentlyOpenedFilesList::removeFile (const File& file)
  53. {
  54. files.removeString (file.getFullPathName());
  55. }
  56. void RecentlyOpenedFilesList::removeNonExistentFiles()
  57. {
  58. for (int i = getNumFiles(); --i >= 0;)
  59. if (! getFile(i).exists())
  60. files.remove (i);
  61. }
  62. //==============================================================================
  63. int RecentlyOpenedFilesList::createPopupMenuItems (PopupMenu& menuToAddTo,
  64. const int baseItemId,
  65. const bool showFullPaths,
  66. const bool dontAddNonExistentFiles,
  67. const File** filesToAvoid)
  68. {
  69. int num = 0;
  70. for (int i = 0; i < getNumFiles(); ++i)
  71. {
  72. const File f (getFile(i));
  73. if ((! dontAddNonExistentFiles) || f.exists())
  74. {
  75. bool needsAvoiding = false;
  76. if (filesToAvoid != nullptr)
  77. {
  78. for (const File** avoid = filesToAvoid; *avoid != nullptr; ++avoid)
  79. {
  80. if (f == **avoid)
  81. {
  82. needsAvoiding = true;
  83. break;
  84. }
  85. }
  86. }
  87. if (! needsAvoiding)
  88. {
  89. menuToAddTo.addItem (baseItemId + i,
  90. showFullPaths ? f.getFullPathName()
  91. : f.getFileName());
  92. ++num;
  93. }
  94. }
  95. }
  96. return num;
  97. }
  98. //==============================================================================
  99. String RecentlyOpenedFilesList::toString() const
  100. {
  101. return files.joinIntoString ("\n");
  102. }
  103. void RecentlyOpenedFilesList::restoreFromString (const String& stringifiedVersion)
  104. {
  105. clear();
  106. files.addLines (stringifiedVersion);
  107. setMaxNumberOfItems (maxNumberOfItems);
  108. }
  109. //==============================================================================
  110. void RecentlyOpenedFilesList::registerRecentFileNatively (const File& file)
  111. {
  112. #if JUCE_MAC
  113. JUCE_AUTORELEASEPOOL
  114. {
  115. [[NSDocumentController sharedDocumentController] noteNewRecentDocumentURL: createNSURLFromFile (file)];
  116. }
  117. #else
  118. ignoreUnused (file);
  119. #endif
  120. }
  121. void RecentlyOpenedFilesList::forgetRecentFileNatively (const File& file)
  122. {
  123. #if JUCE_MAC
  124. JUCE_AUTORELEASEPOOL
  125. {
  126. // for some reason, OSX doesn't provide a method to just remove a single file
  127. // from the recent list, so we clear them all and add them back excluding
  128. // the specified file
  129. auto sharedDocController = [NSDocumentController sharedDocumentController];
  130. auto recentDocumentURLs = [sharedDocController recentDocumentURLs];
  131. [sharedDocController clearRecentDocuments: nil];
  132. auto* nsFile = createNSURLFromFile (file);
  133. auto reverseEnumerator = [recentDocumentURLs reverseObjectEnumerator];
  134. for (NSURL* url : reverseEnumerator)
  135. if (! [url isEqual:nsFile])
  136. [sharedDocController noteNewRecentDocumentURL:url];
  137. }
  138. #else
  139. ignoreUnused (file);
  140. #endif
  141. }
  142. void RecentlyOpenedFilesList::clearRecentFilesNatively()
  143. {
  144. #if JUCE_MAC
  145. JUCE_AUTORELEASEPOOL
  146. {
  147. [[NSDocumentController sharedDocumentController] clearRecentDocuments: nil];
  148. }
  149. #endif
  150. }
  151. } // namespace juce