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.

349 lines
12KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library - "Jules' Utility Class Extensions"
  4. Copyright 2004-11 by Raw Material Software Ltd.
  5. ------------------------------------------------------------------------------
  6. JUCE can be redistributed and/or modified under the terms of the GNU General
  7. Public License (Version 2), as published by the Free Software Foundation.
  8. A copy of the license is included in the JUCE distribution, or can be found
  9. online at www.gnu.org/licenses.
  10. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  11. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  12. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  13. ------------------------------------------------------------------------------
  14. To release a closed-source product which uses JUCE, commercial licenses are
  15. available: visit www.rawmaterialsoftware.com/juce for more information.
  16. ==============================================================================
  17. */
  18. #include "jucer_ProjectContentComponent.h"
  19. #include "../Application/jucer_MainWindow.h"
  20. #include "../Code Editor/jucer_SourceCodeEditor.h"
  21. #include "jucer_ProjectInformationComponent.h"
  22. #include "jucer_TreeViewTypes.h"
  23. #include "../Project Saving/jucer_ProjectExporter.h"
  24. //==============================================================================
  25. ProjectContentComponent::ProjectContentComponent()
  26. : project (nullptr),
  27. currentDocument (nullptr)
  28. {
  29. setOpaque (true);
  30. setWantsKeyboardFocus (true);
  31. treeSizeConstrainer.setMinimumWidth (100);
  32. treeSizeConstrainer.setMaximumWidth (500);
  33. }
  34. ProjectContentComponent::~ProjectContentComponent()
  35. {
  36. setProject (nullptr);
  37. contentView = nullptr;
  38. jassert (getNumChildComponents() == 0);
  39. }
  40. void ProjectContentComponent::paint (Graphics& g)
  41. {
  42. g.fillAll (Colour::greyLevel (0.8f));
  43. }
  44. void ProjectContentComponent::setProject (Project* newProject)
  45. {
  46. if (project != newProject)
  47. {
  48. if (project != nullptr)
  49. project->removeChangeListener (this);
  50. contentView = nullptr;
  51. resizerBar = nullptr;
  52. if (projectTree != nullptr)
  53. {
  54. StoredSettings::getInstance()->getProps().setValue ("projectTreeviewWidth", projectTree->getWidth());
  55. projectTree->deleteRootItem();
  56. projectTree = nullptr;
  57. }
  58. project = newProject;
  59. if (project != nullptr)
  60. {
  61. addAndMakeVisible (projectTree = new TreeView());
  62. projectTree->setComponentID ("tree");
  63. projectTree->setRootItemVisible (true);
  64. projectTree->setMultiSelectEnabled (true);
  65. projectTree->setDefaultOpenness (true);
  66. projectTree->setColour (TreeView::backgroundColourId, Colour::greyLevel (0.93f));
  67. projectTree->setIndentSize (14);
  68. projectTree->setRootItem (new GroupTreeViewItem (project->getMainGroup()));
  69. projectTree->getRootItem()->setOpen (true);
  70. String lastTreeWidth (StoredSettings::getInstance()->getProps().getValue ("projectTreeviewWidth"));
  71. if (lastTreeWidth.getIntValue() < 150)
  72. lastTreeWidth = "250";
  73. projectTree->setBounds ("0, 0, left + " + lastTreeWidth + ", parent.height");
  74. addAndMakeVisible (resizerBar = new ResizableEdgeComponent (projectTree, &treeSizeConstrainer,
  75. ResizableEdgeComponent::rightEdge));
  76. resizerBar->setComponentID ("resizer");
  77. resizerBar->setBounds ("tree.right, 0, tree.right + 4, parent.height");
  78. project->addChangeListener (this);
  79. if (currentDocument == nullptr)
  80. invokeDirectly (CommandIDs::showProjectSettings, true);
  81. updateMissingFileStatuses();
  82. }
  83. }
  84. }
  85. void ProjectContentComponent::changeListenerCallback (ChangeBroadcaster*)
  86. {
  87. updateMissingFileStatuses();
  88. }
  89. void ProjectContentComponent::updateMissingFileStatuses()
  90. {
  91. if (projectTree != nullptr)
  92. {
  93. ProjectTreeViewBase* p = dynamic_cast <ProjectTreeViewBase*> (projectTree->getRootItem());
  94. if (p != nullptr)
  95. p->checkFileStatus();
  96. }
  97. }
  98. bool ProjectContentComponent::showEditorForFile (const File& f)
  99. {
  100. return showDocument (OpenDocumentManager::getInstance()
  101. ->getDocumentForFile (project, f));
  102. }
  103. bool ProjectContentComponent::showDocument (OpenDocumentManager::Document* doc)
  104. {
  105. if (doc == nullptr)
  106. return false;
  107. OpenDocumentManager::getInstance()->moveDocumentToTopOfStack (doc);
  108. if (doc->hasFileBeenModifiedExternally())
  109. doc->reloadFromFile();
  110. return setEditorComponent (doc->createEditor(), doc);
  111. }
  112. void ProjectContentComponent::hideDocument (OpenDocumentManager::Document* doc)
  113. {
  114. if (doc == currentDocument)
  115. {
  116. currentDocument = nullptr;
  117. contentView = nullptr;
  118. updateMainWindowTitle();
  119. commandManager->commandStatusChanged();
  120. }
  121. }
  122. bool ProjectContentComponent::setEditorComponent (Component* editor, OpenDocumentManager::Document* doc)
  123. {
  124. if (editor != nullptr)
  125. {
  126. contentView = editor;
  127. currentDocument = doc;
  128. addAndMakeVisible (editor);
  129. editor->setBounds ("resizer.right, 0, parent.right, parent.height");
  130. updateMainWindowTitle();
  131. commandManager->commandStatusChanged();
  132. return true;
  133. }
  134. updateMainWindowTitle();
  135. return false;
  136. }
  137. void ProjectContentComponent::updateMainWindowTitle()
  138. {
  139. MainWindow* mw = Component::findParentComponentOfClass ((MainWindow*) 0);
  140. if (mw != nullptr)
  141. mw->updateTitle (currentDocument != nullptr ? currentDocument->getName() : String::empty);
  142. }
  143. ApplicationCommandTarget* ProjectContentComponent::getNextCommandTarget()
  144. {
  145. return findFirstTargetParentComponent();
  146. }
  147. void ProjectContentComponent::getAllCommands (Array <CommandID>& commands)
  148. {
  149. const CommandID ids[] = { CommandIDs::saveProject,
  150. CommandIDs::saveProjectAs,
  151. CommandIDs::closeProject,
  152. CommandIDs::openInIDE,
  153. CommandIDs::saveAndOpenInIDE,
  154. CommandIDs::showProjectSettings,
  155. StandardApplicationCommandIDs::del};
  156. commands.addArray (ids, numElementsInArray (ids));
  157. }
  158. void ProjectContentComponent::getCommandInfo (const CommandID commandID, ApplicationCommandInfo& result)
  159. {
  160. switch (commandID)
  161. {
  162. case CommandIDs::saveProject:
  163. result.setInfo ("Save Project",
  164. "Saves the current project",
  165. CommandCategories::general, 0);
  166. result.setActive (project != nullptr);
  167. result.defaultKeypresses.add (KeyPress ('s', ModifierKeys::commandModifier, 0));
  168. break;
  169. case CommandIDs::saveProjectAs:
  170. result.setInfo ("Save Project As...",
  171. "Saves the current project to a different filename",
  172. CommandCategories::general, 0);
  173. result.setActive (project != nullptr);
  174. result.defaultKeypresses.add (KeyPress ('s', ModifierKeys::commandModifier | ModifierKeys::shiftModifier, 0));
  175. break;
  176. case CommandIDs::closeProject:
  177. result.setInfo ("Close Project",
  178. "Closes the current project",
  179. CommandCategories::general, 0);
  180. result.setActive (project != nullptr);
  181. result.defaultKeypresses.add (KeyPress ('w', ModifierKeys::commandModifier | ModifierKeys::shiftModifier, 0));
  182. break;
  183. case CommandIDs::openInIDE:
  184. #if JUCE_MAC
  185. result.setInfo ("Open in XCode...",
  186. #elif JUCE_WINDOWS
  187. result.setInfo ("Open in Visual Studio...",
  188. #else
  189. result.setInfo ("Open as a Makefile...",
  190. #endif
  191. "Launches the project in an external IDE",
  192. CommandCategories::general, 0);
  193. result.setActive (project != nullptr);
  194. break;
  195. case CommandIDs::saveAndOpenInIDE:
  196. #if JUCE_MAC
  197. result.setInfo ("Save Project and Open in XCode...",
  198. #elif JUCE_WINDOWS
  199. result.setInfo ("Save Project and Open in Visual Studio...",
  200. #else
  201. result.setInfo ("Save Project and Open as a Makefile...",
  202. #endif
  203. "Saves the project and launches it in an external IDE",
  204. CommandCategories::general, 0);
  205. result.setActive (project != nullptr);
  206. result.defaultKeypresses.add (KeyPress ('l', ModifierKeys::commandModifier, 0));
  207. break;
  208. case CommandIDs::showProjectSettings:
  209. result.setInfo ("Show Project Build Settings",
  210. "Shows the build options for the project",
  211. CommandCategories::general, 0);
  212. result.setActive (project != nullptr);
  213. result.defaultKeypresses.add (KeyPress ('i', ModifierKeys::commandModifier | ModifierKeys::shiftModifier, 0));
  214. break;
  215. case StandardApplicationCommandIDs::del:
  216. result.setInfo ("Delete", String::empty, CommandCategories::general, 0);
  217. result.defaultKeypresses.add (KeyPress (KeyPress::deleteKey, 0, 0));
  218. result.defaultKeypresses.add (KeyPress (KeyPress::backspaceKey, 0, 0));
  219. result.setActive (projectTree != nullptr);
  220. break;
  221. default:
  222. break;
  223. }
  224. }
  225. bool ProjectContentComponent::isCommandActive (const CommandID commandID)
  226. {
  227. return project != nullptr;
  228. }
  229. bool ProjectContentComponent::perform (const InvocationInfo& info)
  230. {
  231. switch (info.commandID)
  232. {
  233. case CommandIDs::saveProject:
  234. if (project != nullptr)
  235. project->save (true, true);
  236. break;
  237. case CommandIDs::saveProjectAs:
  238. if (project != nullptr)
  239. project->saveAsInteractive (true);
  240. break;
  241. case CommandIDs::closeProject:
  242. {
  243. MainWindow* mw = Component::findParentComponentOfClass ((MainWindow*) 0);
  244. if (mw != nullptr)
  245. mw->closeCurrentProject();
  246. }
  247. break;
  248. case CommandIDs::openInIDE:
  249. if (project != nullptr)
  250. {
  251. ScopedPointer <ProjectExporter> exporter (ProjectExporter::createPlatformDefaultExporter (*project));
  252. if (exporter != nullptr)
  253. exporter->launchProject();
  254. }
  255. break;
  256. case CommandIDs::saveAndOpenInIDE:
  257. if (project != nullptr && project->save (true, true) == FileBasedDocument::savedOk)
  258. {
  259. ScopedPointer <ProjectExporter> exporter (ProjectExporter::createPlatformDefaultExporter (*project));
  260. if (exporter != nullptr)
  261. exporter->launchProject();
  262. }
  263. break;
  264. case CommandIDs::showProjectSettings:
  265. if (projectTree != nullptr)
  266. projectTree->getRootItem()->setSelected (true, true);
  267. break;
  268. case StandardApplicationCommandIDs::del:
  269. if (projectTree != nullptr)
  270. {
  271. ProjectTreeViewBase* p = dynamic_cast <ProjectTreeViewBase*> (projectTree->getRootItem());
  272. if (p != nullptr)
  273. p->deleteAllSelectedItems();
  274. }
  275. break;
  276. default:
  277. return false;
  278. }
  279. return true;
  280. }