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.

170 lines
6.2KB

  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. class SourceFileItem : public ProjectTreeItemBase
  20. {
  21. public:
  22. SourceFileItem (const Project::Item& projectItem)
  23. : ProjectTreeItemBase (projectItem)
  24. {
  25. }
  26. bool acceptsFileDrop (const StringArray&) const override { return false; }
  27. bool acceptsDragItems (const OwnedArray <Project::Item>&) override { return false; }
  28. String getDisplayName() const override
  29. {
  30. return getFile().getFileName();
  31. }
  32. static File findCorrespondingHeaderOrCpp (const File& f)
  33. {
  34. if (f.hasFileExtension (sourceFileExtensions)) return f.withFileExtension (".h");
  35. if (f.hasFileExtension (headerFileExtensions)) return f.withFileExtension (".cpp");
  36. return {};
  37. }
  38. void setName (const String& newName) override
  39. {
  40. if (newName != File::createLegalFileName (newName))
  41. {
  42. AlertWindow::showMessageBox (AlertWindow::WarningIcon, "File Rename",
  43. "That filename contained some illegal characters!");
  44. triggerAsyncRename (item);
  45. return;
  46. }
  47. File oldFile (getFile());
  48. File newFile (oldFile.getSiblingFile (newName));
  49. File correspondingFile (findCorrespondingHeaderOrCpp (oldFile));
  50. if (correspondingFile.exists() && newFile.hasFileExtension (oldFile.getFileExtension()))
  51. {
  52. Project::Item correspondingItem (item.project.getMainGroup().findItemForFile (correspondingFile));
  53. if (correspondingItem.isValid())
  54. {
  55. if (AlertWindow::showOkCancelBox (AlertWindow::NoIcon, "File Rename",
  56. "Do you also want to rename the corresponding file \"" + correspondingFile.getFileName()
  57. + "\" to match?"))
  58. {
  59. if (! item.renameFile (newFile))
  60. {
  61. AlertWindow::showMessageBox (AlertWindow::WarningIcon, "File Rename",
  62. "Failed to rename \"" + oldFile.getFullPathName() + "\"!\n\nCheck your file permissions!");
  63. return;
  64. }
  65. if (! correspondingItem.renameFile (newFile.withFileExtension (correspondingFile.getFileExtension())))
  66. {
  67. AlertWindow::showMessageBox (AlertWindow::WarningIcon, "File Rename",
  68. "Failed to rename \"" + correspondingFile.getFullPathName() + "\"!\n\nCheck your file permissions!");
  69. }
  70. }
  71. }
  72. }
  73. if (! item.renameFile (newFile))
  74. {
  75. AlertWindow::showMessageBox (AlertWindow::WarningIcon, "File Rename",
  76. "Failed to rename the file!\n\nCheck your file permissions!");
  77. }
  78. }
  79. ProjectTreeItemBase* createSubItem (const Project::Item&) override
  80. {
  81. jassertfalse;
  82. return nullptr;
  83. }
  84. void showDocument() override
  85. {
  86. const File f (getFile());
  87. if (f.exists())
  88. if (ProjectContentComponent* pcc = getProjectContentComponent())
  89. pcc->showEditorForFile (f, false);
  90. }
  91. void showPopupMenu() override
  92. {
  93. PopupMenu m;
  94. m.addItem (1, "Open in external editor");
  95. m.addItem (2,
  96. #if JUCE_MAC
  97. "Reveal in Finder");
  98. #else
  99. "Reveal in Explorer");
  100. #endif
  101. m.addItem (4, "Rename File...");
  102. m.addSeparator();
  103. if (auto* group = dynamic_cast<GroupItem*> (getParentItem()))
  104. {
  105. if (group->isRoot())
  106. {
  107. m.addItem (5, "Binary Resource", true, item.shouldBeAddedToBinaryResources());
  108. m.addItem (6, "Xcode Resource", true, item.shouldBeAddedToXcodeResources());
  109. m.addItem (7, "Compile", true, item.shouldBeCompiled());
  110. m.addSeparator();
  111. }
  112. }
  113. m.addItem (3, "Delete");
  114. launchPopupMenu (m);
  115. }
  116. void showPlusMenu() override
  117. {
  118. if (auto* group = dynamic_cast<GroupItem*> (getParentItem()))
  119. group->showPlusMenu();
  120. }
  121. void handlePopupMenuResult (int resultCode) override
  122. {
  123. switch (resultCode)
  124. {
  125. case 1: getFile().startAsProcess(); break;
  126. case 2: revealInFinder(); break;
  127. case 3: deleteAllSelectedItems(); break;
  128. case 4: triggerAsyncRename (item); break;
  129. case 5: item.getShouldAddToBinaryResourcesValue().setValue (! item.shouldBeAddedToBinaryResources()); break;
  130. case 6: item.getShouldAddToXcodeResourcesValue().setValue (! item.shouldBeAddedToXcodeResources()); break;
  131. case 7: item.getShouldCompileValue().setValue (! item.shouldBeCompiled()); break;
  132. default:
  133. if (GroupItem* parentGroup = dynamic_cast<GroupItem*> (getParentProjectItem()))
  134. parentGroup->processCreateFileMenuItem (resultCode);
  135. break;
  136. }
  137. }
  138. };