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.

178 lines
4.9KB

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