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.

582 lines
16KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2015 - ROLI Ltd.
  5. Permission is granted to use this software under the terms of either:
  6. a) the GPL v2 (or any later version)
  7. b) the Affero GPL v3
  8. Details of these licenses can be found at: www.gnu.org/licenses
  9. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  11. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  12. ------------------------------------------------------------------------------
  13. To release a closed-source product which uses JUCE, commercial licenses are
  14. available: visit www.juce.com for more information.
  15. ==============================================================================
  16. */
  17. #include "../jucer_Headers.h"
  18. #include "jucer_Application.h"
  19. #include "jucer_MainWindow.h"
  20. #include "jucer_OpenDocumentManager.h"
  21. #include "../Code Editor/jucer_SourceCodeEditor.h"
  22. #include "../Wizards/jucer_NewProjectWizardClasses.h"
  23. #include "../Utility/jucer_JucerTreeViewBase.h"
  24. //==============================================================================
  25. MainWindow::MainWindow()
  26. : DocumentWindow (IntrojucerApp::getApp().getApplicationName(),
  27. Colour::greyLevel (0.6f),
  28. DocumentWindow::allButtons,
  29. false)
  30. {
  31. setUsingNativeTitleBar (true);
  32. #if ! JUCE_MAC
  33. setMenuBar (IntrojucerApp::getApp().menuModel);
  34. #endif
  35. createProjectContentCompIfNeeded();
  36. setResizable (true, false);
  37. centreWithSize (800, 600);
  38. ApplicationCommandManager& commandManager = IntrojucerApp::getCommandManager();
  39. // Register all the app commands..
  40. commandManager.registerAllCommandsForTarget (this);
  41. commandManager.registerAllCommandsForTarget (getProjectContentComponent());
  42. // update key mappings..
  43. {
  44. commandManager.getKeyMappings()->resetToDefaultMappings();
  45. ScopedPointer<XmlElement> keys (getGlobalProperties().getXmlValue ("keyMappings"));
  46. if (keys != nullptr)
  47. commandManager.getKeyMappings()->restoreFromXml (*keys);
  48. addKeyListener (commandManager.getKeyMappings());
  49. }
  50. // don't want the window to take focus when the title-bar is clicked..
  51. setWantsKeyboardFocus (false);
  52. getLookAndFeel().setColour (ColourSelector::backgroundColourId, Colours::transparentBlack);
  53. setResizeLimits (600, 500, 32000, 32000);
  54. }
  55. MainWindow::~MainWindow()
  56. {
  57. #if ! JUCE_MAC
  58. setMenuBar (nullptr);
  59. #endif
  60. removeKeyListener (IntrojucerApp::getCommandManager().getKeyMappings());
  61. // save the current size and position to our settings file..
  62. getGlobalProperties().setValue ("lastMainWindowPos", getWindowStateAsString());
  63. clearContentComponent();
  64. currentProject = nullptr;
  65. }
  66. void MainWindow::createProjectContentCompIfNeeded()
  67. {
  68. if (getProjectContentComponent() == nullptr)
  69. {
  70. clearContentComponent();
  71. setContentOwned (IntrojucerApp::getApp().createProjectContentComponent(), false);
  72. jassert (getProjectContentComponent() != nullptr);
  73. }
  74. }
  75. void MainWindow::makeVisible()
  76. {
  77. restoreWindowPosition();
  78. setVisible (true);
  79. addToDesktop(); // (must add before restoring size so that fullscreen will work)
  80. restoreWindowPosition();
  81. getContentComponent()->grabKeyboardFocus();
  82. }
  83. ProjectContentComponent* MainWindow::getProjectContentComponent() const
  84. {
  85. return dynamic_cast<ProjectContentComponent*> (getContentComponent());
  86. }
  87. void MainWindow::closeButtonPressed()
  88. {
  89. IntrojucerApp::getApp().mainWindowList.closeWindow (this);
  90. }
  91. bool MainWindow::closeProject (Project* project)
  92. {
  93. jassert (project == currentProject && project != nullptr);
  94. if (project == nullptr)
  95. return true;
  96. project->getStoredProperties().setValue (getProjectWindowPosName(), getWindowStateAsString());
  97. if (ProjectContentComponent* const pcc = getProjectContentComponent())
  98. {
  99. pcc->saveTreeViewState();
  100. pcc->saveOpenDocumentList();
  101. pcc->hideEditor();
  102. }
  103. if (! IntrojucerApp::getApp().openDocumentManager.closeAllDocumentsUsingProject (*project, true))
  104. return false;
  105. FileBasedDocument::SaveResult r = project->saveIfNeededAndUserAgrees();
  106. if (r == FileBasedDocument::savedOk)
  107. {
  108. setProject (nullptr);
  109. return true;
  110. }
  111. return false;
  112. }
  113. bool MainWindow::closeCurrentProject()
  114. {
  115. return currentProject == nullptr || closeProject (currentProject);
  116. }
  117. void MainWindow::setProject (Project* newProject)
  118. {
  119. createProjectContentCompIfNeeded();
  120. getProjectContentComponent()->setProject (newProject);
  121. currentProject = newProject;
  122. getProjectContentComponent()->updateMainWindowTitle();
  123. IntrojucerApp::getCommandManager().commandStatusChanged();
  124. }
  125. void MainWindow::restoreWindowPosition()
  126. {
  127. String windowState;
  128. if (currentProject != nullptr)
  129. windowState = currentProject->getStoredProperties().getValue (getProjectWindowPosName());
  130. if (windowState.isEmpty())
  131. windowState = getGlobalProperties().getValue ("lastMainWindowPos");
  132. restoreWindowStateFromString (windowState);
  133. }
  134. bool MainWindow::canOpenFile (const File& file) const
  135. {
  136. return (! file.isDirectory())
  137. && (file.hasFileExtension (Project::projectFileExtension)
  138. || IntrojucerApp::getApp().openDocumentManager.canOpenFile (file));
  139. }
  140. bool MainWindow::openFile (const File& file)
  141. {
  142. createProjectContentCompIfNeeded();
  143. if (file.hasFileExtension (Project::projectFileExtension))
  144. {
  145. ScopedPointer<Project> newDoc (new Project (file));
  146. Result result (newDoc->loadFrom (file, true));
  147. if (result.wasOk() && closeCurrentProject())
  148. {
  149. setProject (newDoc);
  150. newDoc.release()->setChangedFlag (false);
  151. jassert (getProjectContentComponent() != nullptr);
  152. getProjectContentComponent()->reloadLastOpenDocuments();
  153. return true;
  154. }
  155. }
  156. else if (file.exists())
  157. {
  158. return getProjectContentComponent()->showEditorForFile (file, true);
  159. }
  160. return false;
  161. }
  162. bool MainWindow::isInterestedInFileDrag (const StringArray& filenames)
  163. {
  164. for (int i = filenames.size(); --i >= 0;)
  165. if (canOpenFile (File (filenames[i])))
  166. return true;
  167. return false;
  168. }
  169. void MainWindow::filesDropped (const StringArray& filenames, int /*mouseX*/, int /*mouseY*/)
  170. {
  171. for (int i = filenames.size(); --i >= 0;)
  172. {
  173. const File f (filenames[i]);
  174. if (canOpenFile (f) && openFile (f))
  175. break;
  176. }
  177. }
  178. bool MainWindow::shouldDropFilesWhenDraggedExternally (const DragAndDropTarget::SourceDetails& sourceDetails,
  179. StringArray& files, bool& canMoveFiles)
  180. {
  181. if (TreeView* tv = dynamic_cast<TreeView*> (sourceDetails.sourceComponent.get()))
  182. {
  183. Array<JucerTreeViewBase*> selected;
  184. for (int i = tv->getNumSelectedItems(); --i >= 0;)
  185. if (JucerTreeViewBase* b = dynamic_cast<JucerTreeViewBase*> (tv->getSelectedItem(i)))
  186. selected.add (b);
  187. if (selected.size() > 0)
  188. {
  189. for (int i = selected.size(); --i >= 0;)
  190. {
  191. if (JucerTreeViewBase* jtvb = selected.getUnchecked(i))
  192. {
  193. const File f (jtvb->getDraggableFile());
  194. if (f.existsAsFile())
  195. files.add (f.getFullPathName());
  196. }
  197. }
  198. canMoveFiles = false;
  199. return files.size() > 0;
  200. }
  201. }
  202. return false;
  203. }
  204. void MainWindow::activeWindowStatusChanged()
  205. {
  206. DocumentWindow::activeWindowStatusChanged();
  207. if (ProjectContentComponent* const pcc = getProjectContentComponent())
  208. pcc->updateMissingFileStatuses();
  209. IntrojucerApp::getApp().openDocumentManager.reloadModifiedFiles();
  210. }
  211. void MainWindow::updateTitle (const String& documentName)
  212. {
  213. String name (IntrojucerApp::getApp().getApplicationName());
  214. if (currentProject != nullptr)
  215. name << " - " << currentProject->getDocumentTitle();
  216. if (documentName.isNotEmpty())
  217. name << " - " << documentName;
  218. setName (name);
  219. }
  220. void MainWindow::showNewProjectWizard()
  221. {
  222. jassert (currentProject == nullptr);
  223. setContentOwned (createNewProjectWizardComponent(), true);
  224. centreWithSize (900, 630);
  225. setVisible (true);
  226. addToDesktop();
  227. getContentComponent()->grabKeyboardFocus();
  228. }
  229. //==============================================================================
  230. ApplicationCommandTarget* MainWindow::getNextCommandTarget()
  231. {
  232. return nullptr;
  233. }
  234. void MainWindow::getAllCommands (Array <CommandID>& commands)
  235. {
  236. const CommandID ids[] = { CommandIDs::closeWindow };
  237. commands.addArray (ids, numElementsInArray (ids));
  238. }
  239. void MainWindow::getCommandInfo (const CommandID commandID, ApplicationCommandInfo& result)
  240. {
  241. switch (commandID)
  242. {
  243. case CommandIDs::closeWindow:
  244. result.setInfo ("Close Window", "Closes the current window", CommandCategories::general, 0);
  245. result.defaultKeypresses.add (KeyPress ('w', ModifierKeys::commandModifier, 0));
  246. break;
  247. default:
  248. break;
  249. }
  250. }
  251. bool MainWindow::perform (const InvocationInfo& info)
  252. {
  253. switch (info.commandID)
  254. {
  255. case CommandIDs::closeWindow:
  256. closeButtonPressed();
  257. break;
  258. default:
  259. return false;
  260. }
  261. return true;
  262. }
  263. //==============================================================================
  264. MainWindowList::MainWindowList()
  265. {
  266. }
  267. void MainWindowList::forceCloseAllWindows()
  268. {
  269. windows.clear();
  270. }
  271. bool MainWindowList::askAllWindowsToClose()
  272. {
  273. saveCurrentlyOpenProjectList();
  274. while (windows.size() > 0)
  275. {
  276. if (! windows[0]->closeCurrentProject())
  277. return false;
  278. windows.remove (0);
  279. }
  280. return true;
  281. }
  282. void MainWindowList::createWindowIfNoneAreOpen()
  283. {
  284. if (windows.size() == 0)
  285. createNewMainWindow()->showNewProjectWizard();
  286. }
  287. void MainWindowList::closeWindow (MainWindow* w)
  288. {
  289. jassert (windows.contains (w));
  290. #if ! JUCE_MAC
  291. if (windows.size() == 1)
  292. {
  293. JUCEApplicationBase::getInstance()->systemRequestedQuit();
  294. }
  295. else
  296. #endif
  297. {
  298. if (w->closeCurrentProject())
  299. {
  300. windows.removeObject (w);
  301. saveCurrentlyOpenProjectList();
  302. }
  303. }
  304. }
  305. void MainWindowList::openDocument (OpenDocumentManager::Document* doc, bool grabFocus)
  306. {
  307. getOrCreateFrontmostWindow()->getProjectContentComponent()->showDocument (doc, grabFocus);
  308. }
  309. bool MainWindowList::openFile (const File& file)
  310. {
  311. for (int i = windows.size(); --i >= 0;)
  312. {
  313. MainWindow* const w = windows.getUnchecked(i);
  314. if (w->getProject() != nullptr && w->getProject()->getFile() == file)
  315. {
  316. w->toFront (true);
  317. return true;
  318. }
  319. }
  320. if (file.hasFileExtension (Project::projectFileExtension))
  321. {
  322. MainWindow* const w = getOrCreateEmptyWindow();
  323. bool ok = w->openFile (file);
  324. w->makeVisible();
  325. avoidSuperimposedWindows (w);
  326. return ok;
  327. }
  328. if (file.exists())
  329. return getOrCreateFrontmostWindow()->openFile (file);
  330. return false;
  331. }
  332. MainWindow* MainWindowList::createNewMainWindow()
  333. {
  334. MainWindow* const w = new MainWindow();
  335. windows.add (w);
  336. w->restoreWindowPosition();
  337. avoidSuperimposedWindows (w);
  338. return w;
  339. }
  340. MainWindow* MainWindowList::getOrCreateFrontmostWindow()
  341. {
  342. if (windows.size() == 0)
  343. {
  344. MainWindow* w = createNewMainWindow();
  345. avoidSuperimposedWindows (w);
  346. w->makeVisible();
  347. return w;
  348. }
  349. for (int i = Desktop::getInstance().getNumComponents(); --i >= 0;)
  350. {
  351. MainWindow* mw = dynamic_cast<MainWindow*> (Desktop::getInstance().getComponent (i));
  352. if (windows.contains (mw))
  353. return mw;
  354. }
  355. return windows.getLast();
  356. }
  357. MainWindow* MainWindowList::getOrCreateEmptyWindow()
  358. {
  359. if (windows.size() == 0)
  360. return createNewMainWindow();
  361. for (int i = Desktop::getInstance().getNumComponents(); --i >= 0;)
  362. {
  363. MainWindow* mw = dynamic_cast<MainWindow*> (Desktop::getInstance().getComponent (i));
  364. if (windows.contains (mw) && mw->getProject() == nullptr)
  365. return mw;
  366. }
  367. return createNewMainWindow();
  368. }
  369. void MainWindowList::updateAllWindowTitles()
  370. {
  371. for (int i = 0; i < windows.size(); ++i)
  372. if (ProjectContentComponent* pc = windows.getUnchecked(i)->getProjectContentComponent())
  373. pc->updateMainWindowTitle();
  374. }
  375. void MainWindowList::avoidSuperimposedWindows (MainWindow* const mw)
  376. {
  377. for (int i = windows.size(); --i >= 0;)
  378. {
  379. MainWindow* const other = windows.getUnchecked(i);
  380. const Rectangle<int> b1 (mw->getBounds());
  381. const Rectangle<int> b2 (other->getBounds());
  382. if (mw != other
  383. && std::abs (b1.getX() - b2.getX()) < 3
  384. && std::abs (b1.getY() - b2.getY()) < 3
  385. && std::abs (b1.getRight() - b2.getRight()) < 3
  386. && std::abs (b1.getBottom() - b2.getBottom()) < 3)
  387. {
  388. int dx = 40, dy = 30;
  389. if (b1.getCentreX() >= mw->getScreenBounds().getCentreX()) dx = -dx;
  390. if (b1.getCentreY() >= mw->getScreenBounds().getCentreY()) dy = -dy;
  391. mw->setBounds (b1.translated (dx, dy));
  392. }
  393. }
  394. }
  395. void MainWindowList::saveCurrentlyOpenProjectList()
  396. {
  397. Array<File> projects;
  398. Desktop& desktop = Desktop::getInstance();
  399. for (int i = 0; i < desktop.getNumComponents(); ++i)
  400. {
  401. if (MainWindow* const mw = dynamic_cast<MainWindow*> (desktop.getComponent(i)))
  402. if (Project* p = mw->getProject())
  403. projects.add (p->getFile());
  404. }
  405. getAppSettings().setLastProjects (projects);
  406. }
  407. void MainWindowList::reopenLastProjects()
  408. {
  409. Array<File> projects (getAppSettings().getLastProjects());
  410. for (int i = 0; i < projects.size(); ++ i)
  411. openFile (projects.getReference(i));
  412. }
  413. void MainWindowList::sendLookAndFeelChange()
  414. {
  415. for (int i = windows.size(); --i >= 0;)
  416. windows.getUnchecked(i)->sendLookAndFeelChange();
  417. }
  418. Project* MainWindowList::getFrontmostProject()
  419. {
  420. Desktop& desktop = Desktop::getInstance();
  421. for (int i = desktop.getNumComponents(); --i >= 0;)
  422. if (MainWindow* const mw = dynamic_cast<MainWindow*> (desktop.getComponent(i)))
  423. if (Project* p = mw->getProject())
  424. return p;
  425. return nullptr;
  426. }
  427. File findDefaultModulesFolder (bool mustContainJuceCoreModule)
  428. {
  429. const MainWindowList& windows = IntrojucerApp::getApp().mainWindowList;
  430. for (int i = windows.windows.size(); --i >= 0;)
  431. {
  432. if (Project* p = windows.windows.getUnchecked (i)->getProject())
  433. {
  434. const File f (EnabledModuleList::findDefaultModulesFolder (*p));
  435. if (isJuceModulesFolder (f) || (f.isDirectory() && ! mustContainJuceCoreModule))
  436. return f;
  437. }
  438. }
  439. if (mustContainJuceCoreModule)
  440. return findDefaultModulesFolder (false);
  441. File f (File::getSpecialLocation (File::currentApplicationFile));
  442. for (;;)
  443. {
  444. File parent (f.getParentDirectory());
  445. if (parent == f || ! parent.isDirectory())
  446. break;
  447. if (isJuceFolder (parent))
  448. return parent.getChildFile ("modules");
  449. f = parent;
  450. }
  451. return File::nonexistent;
  452. }