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.

859 lines
25KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE 6 technical preview.
  4. Copyright (c) 2017 - ROLI Ltd.
  5. You may use this code under the terms of the GPL v3
  6. (see www.gnu.org/licenses).
  7. For this technical preview, this file is not subject to commercial licensing.
  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. #include "../Application/jucer_Headers.h"
  14. #include "jucer_Application.h"
  15. #include "jucer_MainWindow.h"
  16. #include "../Wizards/jucer_NewProjectWizardClasses.h"
  17. #include "../Utility/UI/jucer_JucerTreeViewBase.h"
  18. #include "../ProjectSaving/jucer_ProjectSaver.h"
  19. //==============================================================================
  20. MainWindow::MainWindow()
  21. : DocumentWindow (ProjucerApplication::getApp().getApplicationName(),
  22. ProjucerApplication::getApp().lookAndFeel.getCurrentColourScheme()
  23. .getUIColour (LookAndFeel_V4::ColourScheme::UIColour::windowBackground),
  24. DocumentWindow::allButtons,
  25. false)
  26. {
  27. setUsingNativeTitleBar (true);
  28. #if ! JUCE_MAC
  29. setMenuBar (ProjucerApplication::getApp().getMenuModel());
  30. #endif
  31. createProjectContentCompIfNeeded();
  32. setResizable (true, false);
  33. centreWithSize (800, 600);
  34. auto& commandManager = ProjucerApplication::getCommandManager();
  35. auto registerAllAppCommands = [&]
  36. {
  37. commandManager.registerAllCommandsForTarget (this);
  38. commandManager.registerAllCommandsForTarget (getProjectContentComponent());
  39. };
  40. auto updateAppKeyMappings = [&]
  41. {
  42. commandManager.getKeyMappings()->resetToDefaultMappings();
  43. if (auto keys = getGlobalProperties().getXmlValue ("keyMappings"))
  44. commandManager.getKeyMappings()->restoreFromXml (*keys);
  45. addKeyListener (commandManager.getKeyMappings());
  46. };
  47. registerAllAppCommands();
  48. updateAppKeyMappings();
  49. setWantsKeyboardFocus (false);
  50. getLookAndFeel().setColour (ColourSelector::backgroundColourId, Colours::transparentBlack);
  51. projectNameValue.addListener (this);
  52. setResizeLimits (600, 500, 32000, 32000);
  53. }
  54. MainWindow::~MainWindow()
  55. {
  56. #if ! JUCE_MAC
  57. setMenuBar (nullptr);
  58. #endif
  59. removeKeyListener (ProjucerApplication::getCommandManager().getKeyMappings());
  60. getGlobalProperties().setValue ("lastMainWindowPos", getWindowStateAsString());
  61. clearContentComponent();
  62. currentProject.reset();
  63. }
  64. void MainWindow::createProjectContentCompIfNeeded()
  65. {
  66. if (getProjectContentComponent() == nullptr)
  67. {
  68. clearContentComponent();
  69. setContentOwned (new ProjectContentComponent(), false);
  70. }
  71. }
  72. void MainWindow::setTitleBarIcon()
  73. {
  74. if (auto* peer = getPeer())
  75. {
  76. if (currentProject != nullptr)
  77. {
  78. peer->setRepresentedFile (currentProject->getFile());
  79. peer->setIcon (ImageCache::getFromMemory (BinaryData::juce_icon_png, BinaryData::juce_icon_pngSize));
  80. }
  81. else
  82. {
  83. peer->setRepresentedFile ({});
  84. }
  85. }
  86. }
  87. void MainWindow::makeVisible()
  88. {
  89. setVisible (true);
  90. addToDesktop();
  91. restoreWindowPosition();
  92. setTitleBarIcon();
  93. getContentComponent()->grabKeyboardFocus();
  94. }
  95. ProjectContentComponent* MainWindow::getProjectContentComponent() const
  96. {
  97. return dynamic_cast<ProjectContentComponent*> (getContentComponent());
  98. }
  99. void MainWindow::closeButtonPressed()
  100. {
  101. ProjucerApplication::getApp().mainWindowList.closeWindow (this);
  102. }
  103. bool MainWindow::closeCurrentProject (bool askUserToSave)
  104. {
  105. if (currentProject == nullptr)
  106. return true;
  107. currentProject->getStoredProperties().setValue (getProjectWindowPosName(), getWindowStateAsString());
  108. if (auto* pcc = getProjectContentComponent())
  109. {
  110. pcc->saveTreeViewState();
  111. pcc->saveOpenDocumentList();
  112. pcc->hideEditor();
  113. }
  114. if (ProjucerApplication::getApp().openDocumentManager
  115. .closeAllDocumentsUsingProject (*currentProject, askUserToSave))
  116. {
  117. if (! askUserToSave || (currentProject->saveIfNeededAndUserAgrees() == FileBasedDocument::savedOk))
  118. {
  119. setProject (nullptr);
  120. return true;
  121. }
  122. }
  123. return false;
  124. }
  125. void MainWindow::moveProject (File newProjectFileToOpen)
  126. {
  127. auto openInIDE = currentProject->shouldOpenInIDEAfterSaving();
  128. closeCurrentProject (false);
  129. openFile (newProjectFileToOpen);
  130. if (currentProject != nullptr)
  131. {
  132. ProjucerApplication::getApp().getCommandManager().invokeDirectly (openInIDE ? CommandIDs::saveAndOpenInIDE
  133. : CommandIDs::saveProject,
  134. false);
  135. }
  136. }
  137. void MainWindow::setProject (std::unique_ptr<Project> newProject)
  138. {
  139. if (newProject == nullptr)
  140. {
  141. getProjectContentComponent()->setProject (nullptr);
  142. projectNameValue.referTo (Value());
  143. currentProject.reset();
  144. }
  145. else
  146. {
  147. currentProject = std::move (newProject);
  148. createProjectContentCompIfNeeded();
  149. getProjectContentComponent()->setProject (currentProject.get());
  150. projectNameValue.referTo (currentProject->getProjectValue (Ids::name));
  151. if (auto* peer = getPeer())
  152. peer->setRepresentedFile (currentProject->getFile());
  153. }
  154. ProjucerApplication::getCommandManager().commandStatusChanged();
  155. }
  156. void MainWindow::restoreWindowPosition()
  157. {
  158. String windowState;
  159. if (currentProject != nullptr)
  160. windowState = currentProject->getStoredProperties().getValue (getProjectWindowPosName());
  161. if (windowState.isEmpty())
  162. windowState = getGlobalProperties().getValue ("lastMainWindowPos");
  163. restoreWindowStateFromString (windowState);
  164. }
  165. bool MainWindow::canOpenFile (const File& file) const
  166. {
  167. return (! file.isDirectory())
  168. && (file.hasFileExtension (Project::projectFileExtension)
  169. || ProjucerApplication::getApp().openDocumentManager.canOpenFile (file));
  170. }
  171. bool MainWindow::openFile (const File& file)
  172. {
  173. createProjectContentCompIfNeeded();
  174. if (file.hasFileExtension (Project::projectFileExtension))
  175. {
  176. auto newDoc = std::make_unique<Project> (file);
  177. auto result = newDoc->loadFrom (file, true);
  178. if (result.wasOk() && closeCurrentProject (true))
  179. {
  180. setProject (std::move (newDoc));
  181. currentProject->setChangedFlag (false);
  182. getProjectContentComponent()->reloadLastOpenDocuments();
  183. currentProject->updateDeprecatedProjectSettingsInteractively();
  184. return true;
  185. }
  186. }
  187. else if (file.exists())
  188. {
  189. return getProjectContentComponent()->showEditorForFile (file, true);
  190. }
  191. return false;
  192. }
  193. bool MainWindow::tryToOpenPIP (const File& pipFile)
  194. {
  195. PIPGenerator generator (pipFile);
  196. if (! generator.hasValidPIP())
  197. return false;
  198. auto generatorResult = generator.createJucerFile();
  199. if (generatorResult != Result::ok())
  200. {
  201. AlertWindow::showMessageBoxAsync (AlertWindow::WarningIcon,
  202. "PIP Error.",
  203. generatorResult.getErrorMessage());
  204. return false;
  205. }
  206. if (! generator.createMainCpp())
  207. {
  208. AlertWindow::showMessageBoxAsync (AlertWindow::WarningIcon,
  209. "PIP Error.",
  210. "Failed to create Main.cpp.");
  211. return false;
  212. }
  213. if (! ProjucerApplication::getApp().mainWindowList.openFile (generator.getJucerFile()))
  214. return false;
  215. openPIP (generator);
  216. return true;
  217. }
  218. static bool isDivider (const String& line)
  219. {
  220. auto afterIndent = line.trim();
  221. if (afterIndent.startsWith ("//") && afterIndent.length() > 20)
  222. {
  223. afterIndent = afterIndent.substring (2);
  224. if (afterIndent.containsOnly ("=")
  225. || afterIndent.containsOnly ("/")
  226. || afterIndent.containsOnly ("-"))
  227. {
  228. return true;
  229. }
  230. }
  231. return false;
  232. }
  233. static bool isEndOfCommentBlock (const String& line)
  234. {
  235. if (line.contains ("*/"))
  236. return true;
  237. return false;
  238. }
  239. static int getIndexOfCommentBlockStart (const StringArray& lines, int blockEndIndex)
  240. {
  241. for (int i = blockEndIndex; i >= 0; --i)
  242. {
  243. if (lines[i].contains ("/*"))
  244. return i;
  245. }
  246. return 0;
  247. }
  248. static int findBestLineToScrollTo (StringArray lines, StringRef className)
  249. {
  250. for (auto line : lines)
  251. {
  252. if (line.contains ("struct " + className) || line.contains ("class " + className))
  253. {
  254. auto index = lines.indexOf (line);
  255. if (isDivider (lines[index - 1]))
  256. return index - 1;
  257. if (isEndOfCommentBlock (lines[index - 1]))
  258. {
  259. auto blockStartIndex = getIndexOfCommentBlockStart (lines, index - 1);
  260. if (blockStartIndex > 0 && isDivider (lines [blockStartIndex - 1]))
  261. return blockStartIndex - 1;
  262. return blockStartIndex;
  263. }
  264. return lines.indexOf (line);
  265. }
  266. }
  267. return 0;
  268. }
  269. void MainWindow::openPIP (PIPGenerator& generator)
  270. {
  271. if (auto* window = ProjucerApplication::getApp().mainWindowList.getMainWindowForFile (generator.getJucerFile()))
  272. {
  273. if (auto* project = window->getProject())
  274. {
  275. project->setTemporaryDirectory (generator.getOutputDirectory());
  276. ProjectSaver liveBuildSaver (*project, project->getFile());
  277. liveBuildSaver.saveContentNeededForLiveBuild();
  278. if (auto* pcc = window->getProjectContentComponent())
  279. {
  280. pcc->invokeDirectly (CommandIDs::toggleBuildEnabled, true);
  281. pcc->invokeDirectly (CommandIDs::buildNow, true);
  282. pcc->invokeDirectly (CommandIDs::toggleContinuousBuild, true);
  283. auto fileToDisplay = generator.getPIPFile();
  284. if (fileToDisplay != File())
  285. {
  286. pcc->showEditorForFile (fileToDisplay, true);
  287. if (auto* sourceCodeEditor = dynamic_cast <SourceCodeEditor*> (pcc->getEditorComponent()))
  288. {
  289. sourceCodeEditor->editor->scrollToLine (findBestLineToScrollTo (StringArray::fromLines (fileToDisplay.loadFileAsString()),
  290. generator.getMainClassName()));
  291. }
  292. }
  293. }
  294. }
  295. }
  296. }
  297. bool MainWindow::isInterestedInFileDrag (const StringArray& filenames)
  298. {
  299. for (auto& filename : filenames)
  300. if (canOpenFile (File (filename)))
  301. return true;
  302. return false;
  303. }
  304. void MainWindow::filesDropped (const StringArray& filenames, int /*mouseX*/, int /*mouseY*/)
  305. {
  306. for (auto& filename : filenames)
  307. {
  308. const File f (filename);
  309. if (tryToOpenPIP (f))
  310. continue;
  311. if (! isPIPFile (f) && (canOpenFile (f) && openFile (f)))
  312. break;
  313. }
  314. }
  315. bool MainWindow::shouldDropFilesWhenDraggedExternally (const DragAndDropTarget::SourceDetails& sourceDetails,
  316. StringArray& files, bool& canMoveFiles)
  317. {
  318. if (auto* tv = dynamic_cast<TreeView*> (sourceDetails.sourceComponent.get()))
  319. {
  320. Array<JucerTreeViewBase*> selected;
  321. for (int i = tv->getNumSelectedItems(); --i >= 0;)
  322. if (auto* b = dynamic_cast<JucerTreeViewBase*> (tv->getSelectedItem(i)))
  323. selected.add (b);
  324. if (! selected.isEmpty())
  325. {
  326. for (int i = selected.size(); --i >= 0;)
  327. {
  328. if (auto* jtvb = selected.getUnchecked(i))
  329. {
  330. auto f = jtvb->getDraggableFile();
  331. if (f.existsAsFile())
  332. files.add (f.getFullPathName());
  333. }
  334. }
  335. canMoveFiles = false;
  336. return ! files.isEmpty();
  337. }
  338. }
  339. return false;
  340. }
  341. void MainWindow::activeWindowStatusChanged()
  342. {
  343. DocumentWindow::activeWindowStatusChanged();
  344. if (auto* pcc = getProjectContentComponent())
  345. pcc->updateMissingFileStatuses();
  346. ProjucerApplication::getApp().openDocumentManager.reloadModifiedFiles();
  347. if (auto* p = getProject())
  348. {
  349. if (p->hasProjectBeenModified())
  350. {
  351. Component::SafePointer<Component> safePointer (this);
  352. MessageManager::callAsync ([=] ()
  353. {
  354. if (safePointer == nullptr)
  355. return; // bail out if the window has been deleted
  356. auto result = AlertWindow::showOkCancelBox (AlertWindow::QuestionIcon,
  357. TRANS ("The .jucer file has been modified since the last save."),
  358. TRANS ("Do you want to keep the current project or re-load from disk?"),
  359. TRANS ("Keep"),
  360. TRANS ("Re-load from disk"));
  361. if (safePointer == nullptr)
  362. return;
  363. if (result == 0)
  364. {
  365. if (auto* project = getProject())
  366. {
  367. auto oldTemporaryDirectory = project->getTemporaryDirectory();
  368. auto projectFile = project->getFile();
  369. setProject (nullptr);
  370. openFile (projectFile);
  371. if (oldTemporaryDirectory != File())
  372. if (auto* newProject = getProject())
  373. newProject->setTemporaryDirectory (oldTemporaryDirectory);
  374. }
  375. }
  376. else
  377. {
  378. ProjucerApplication::getApp().getCommandManager().invokeDirectly (CommandIDs::saveProject, true);
  379. }
  380. });
  381. }
  382. }
  383. }
  384. void MainWindow::showStartPage()
  385. {
  386. jassert (currentProject == nullptr);
  387. setContentOwned (createNewProjectWizardComponent(), true);
  388. centreWithSize (900, 630);
  389. setVisible (true);
  390. addToDesktop();
  391. getContentComponent()->grabKeyboardFocus();
  392. }
  393. //==============================================================================
  394. ApplicationCommandTarget* MainWindow::getNextCommandTarget()
  395. {
  396. return nullptr;
  397. }
  398. void MainWindow::getAllCommands (Array <CommandID>& commands)
  399. {
  400. const CommandID ids[] =
  401. {
  402. CommandIDs::closeWindow,
  403. CommandIDs::goToPreviousWindow,
  404. CommandIDs::goToNextWindow
  405. };
  406. commands.addArray (ids, numElementsInArray (ids));
  407. }
  408. void MainWindow::getCommandInfo (const CommandID commandID, ApplicationCommandInfo& result)
  409. {
  410. switch (commandID)
  411. {
  412. case CommandIDs::closeWindow:
  413. result.setInfo ("Close Window", "Closes the current window", CommandCategories::general, 0);
  414. result.defaultKeypresses.add (KeyPress ('w', ModifierKeys::commandModifier, 0));
  415. break;
  416. case CommandIDs::goToPreviousWindow:
  417. result.setInfo ("Previous Window", "Activates the previous window", CommandCategories::general, 0);
  418. result.setActive (ProjucerApplication::getApp().mainWindowList.windows.size() > 1);
  419. result.defaultKeypresses.add (KeyPress (KeyPress::tabKey, ModifierKeys::shiftModifier | ModifierKeys::ctrlModifier, 0));
  420. break;
  421. case CommandIDs::goToNextWindow:
  422. result.setInfo ("Next Window", "Activates the next window", CommandCategories::general, 0);
  423. result.setActive (ProjucerApplication::getApp().mainWindowList.windows.size() > 1);
  424. result.defaultKeypresses.add (KeyPress (KeyPress::tabKey, ModifierKeys::ctrlModifier, 0));
  425. break;
  426. default:
  427. break;
  428. }
  429. }
  430. bool MainWindow::perform (const InvocationInfo& info)
  431. {
  432. switch (info.commandID)
  433. {
  434. case CommandIDs::closeWindow:
  435. closeButtonPressed();
  436. break;
  437. case CommandIDs::goToPreviousWindow:
  438. ProjucerApplication::getApp().mainWindowList.goToSiblingWindow (this, -1);
  439. break;
  440. case CommandIDs::goToNextWindow:
  441. ProjucerApplication::getApp().mainWindowList.goToSiblingWindow (this, 1);
  442. break;
  443. default:
  444. return false;
  445. }
  446. return true;
  447. }
  448. void MainWindow::valueChanged (Value&)
  449. {
  450. if (currentProject != nullptr)
  451. setName (currentProject->getProjectNameString() + " - Projucer");
  452. else
  453. setName ("Projucer");
  454. }
  455. //==============================================================================
  456. MainWindowList::MainWindowList()
  457. {
  458. }
  459. void MainWindowList::forceCloseAllWindows()
  460. {
  461. windows.clear();
  462. }
  463. bool MainWindowList::askAllWindowsToClose()
  464. {
  465. saveCurrentlyOpenProjectList();
  466. while (windows.size() > 0)
  467. {
  468. if (! windows[0]->closeCurrentProject (true))
  469. return false;
  470. windows.remove (0);
  471. }
  472. return true;
  473. }
  474. void MainWindowList::createWindowIfNoneAreOpen()
  475. {
  476. if (windows.isEmpty())
  477. createNewMainWindow()->showStartPage();
  478. }
  479. void MainWindowList::closeWindow (MainWindow* w)
  480. {
  481. jassert (windows.contains (w));
  482. #if ! JUCE_MAC
  483. if (windows.size() == 1 && ! isInReopenLastProjects)
  484. {
  485. JUCEApplicationBase::getInstance()->systemRequestedQuit();
  486. }
  487. else
  488. #endif
  489. {
  490. if (w->closeCurrentProject (true))
  491. {
  492. windows.removeObject (w);
  493. saveCurrentlyOpenProjectList();
  494. }
  495. }
  496. }
  497. void MainWindowList::goToSiblingWindow (MainWindow* w, int delta)
  498. {
  499. auto index = windows.indexOf (w);
  500. if (index >= 0)
  501. if (auto* next = windows[(index + delta + windows.size()) % windows.size()])
  502. next->toFront (true);
  503. }
  504. void MainWindowList::openDocument (OpenDocumentManager::Document* doc, bool grabFocus)
  505. {
  506. auto& desktop = Desktop::getInstance();
  507. for (int i = desktop.getNumComponents(); --i >= 0;)
  508. {
  509. if (auto* mw = dynamic_cast<MainWindow*> (desktop.getComponent(i)))
  510. {
  511. if (auto* pcc = mw->getProjectContentComponent())
  512. {
  513. if (pcc->hasFileInRecentList (doc->getFile()))
  514. {
  515. mw->toFront (true);
  516. mw->getProjectContentComponent()->showDocument (doc, grabFocus);
  517. return;
  518. }
  519. }
  520. }
  521. }
  522. getFrontmostWindow()->getProjectContentComponent()->showDocument (doc, grabFocus);
  523. }
  524. bool MainWindowList::openFile (const File& file, bool openInBackground)
  525. {
  526. for (auto* w : windows)
  527. {
  528. if (w->getProject() != nullptr && w->getProject()->getFile() == file)
  529. {
  530. w->toFront (true);
  531. return true;
  532. }
  533. }
  534. if (file.hasFileExtension (Project::projectFileExtension))
  535. {
  536. WeakReference<Component> previousFrontWindow (getFrontmostWindow());
  537. auto* w = getOrCreateEmptyWindow();
  538. jassert (w != nullptr);
  539. if (w->openFile (file))
  540. {
  541. w->makeVisible();
  542. checkWindowBounds (*w);
  543. if (openInBackground && previousFrontWindow != nullptr)
  544. previousFrontWindow->toFront (true);
  545. return true;
  546. }
  547. closeWindow (w);
  548. return false;
  549. }
  550. if (getFrontmostWindow()->tryToOpenPIP (file))
  551. return true;
  552. if (! isPIPFile (file) && file.exists())
  553. return getFrontmostWindow()->openFile (file);
  554. return false;
  555. }
  556. MainWindow* MainWindowList::createNewMainWindow()
  557. {
  558. windows.add (new MainWindow());
  559. return windows.getLast();
  560. }
  561. MainWindow* MainWindowList::getFrontmostWindow (bool createIfNotFound)
  562. {
  563. if (windows.isEmpty())
  564. {
  565. if (createIfNotFound)
  566. {
  567. auto* w = createNewMainWindow();
  568. jassert (w != nullptr);
  569. w->makeVisible();
  570. checkWindowBounds (*w);
  571. return w;
  572. }
  573. return nullptr;
  574. }
  575. for (int i = Desktop::getInstance().getNumComponents(); --i >= 0;)
  576. {
  577. auto* mw = dynamic_cast<MainWindow*> (Desktop::getInstance().getComponent (i));
  578. if (windows.contains (mw))
  579. return mw;
  580. }
  581. return windows.getLast();
  582. }
  583. MainWindow* MainWindowList::getOrCreateEmptyWindow()
  584. {
  585. if (windows.size() == 0)
  586. return createNewMainWindow();
  587. for (int i = Desktop::getInstance().getNumComponents(); --i >= 0;)
  588. {
  589. auto* mw = dynamic_cast<MainWindow*> (Desktop::getInstance().getComponent (i));
  590. if (windows.contains (mw) && mw->getProject() == nullptr)
  591. return mw;
  592. }
  593. return createNewMainWindow();
  594. }
  595. MainWindow* MainWindowList::getMainWindowForFile (const File& file)
  596. {
  597. if (windows.size() > 0)
  598. {
  599. for (auto* window : windows)
  600. {
  601. if (auto* project = window->getProject())
  602. {
  603. if (project->getFile() == file)
  604. return window;
  605. }
  606. }
  607. }
  608. return nullptr;
  609. }
  610. void MainWindowList::checkWindowBounds (MainWindow& windowToCheck)
  611. {
  612. auto avoidSuperimposedWindows = [&]
  613. {
  614. for (auto* otherWindow : windows)
  615. {
  616. if (otherWindow == nullptr || otherWindow == &windowToCheck)
  617. continue;
  618. auto boundsToCheck = windowToCheck.getScreenBounds();
  619. auto otherBounds = otherWindow->getScreenBounds();
  620. if (std::abs (boundsToCheck.getX() - otherBounds.getX()) < 3
  621. && std::abs (boundsToCheck.getY() - otherBounds.getY()) < 3
  622. && std::abs (boundsToCheck.getRight() - otherBounds.getRight()) < 3
  623. && std::abs (boundsToCheck.getBottom() - otherBounds.getBottom()) < 3)
  624. {
  625. int dx = 40, dy = 30;
  626. if (otherBounds.getCentreX() >= boundsToCheck.getCentreX()) dx = -dx;
  627. if (otherBounds.getCentreY() >= boundsToCheck.getCentreY()) dy = -dy;
  628. windowToCheck.setBounds (boundsToCheck.translated (dx, dy));
  629. }
  630. }
  631. };
  632. auto ensureWindowIsFullyOnscreen = [&]
  633. {
  634. auto windowBounds = windowToCheck.getScreenBounds();
  635. auto screenLimits = Desktop::getInstance().getDisplays().findDisplayForRect (windowBounds).userArea;
  636. if (auto* peer = windowToCheck.getPeer())
  637. peer->getFrameSize().subtractFrom (screenLimits);
  638. auto constrainedX = jlimit (screenLimits.getX(), jmax (screenLimits.getX(), screenLimits.getRight() - windowBounds.getWidth()), windowBounds.getX());
  639. auto constrainedY = jlimit (screenLimits.getY(), jmax (screenLimits.getY(), screenLimits.getBottom() - windowBounds.getHeight()), windowBounds.getY());
  640. Point<int> constrainedTopLeft (constrainedX, constrainedY);
  641. if (windowBounds.getPosition() != constrainedTopLeft)
  642. windowToCheck.setTopLeftPosition (constrainedTopLeft);
  643. };
  644. avoidSuperimposedWindows();
  645. ensureWindowIsFullyOnscreen();
  646. }
  647. void MainWindowList::saveCurrentlyOpenProjectList()
  648. {
  649. Array<File> projects;
  650. auto& desktop = Desktop::getInstance();
  651. for (int i = 0; i < desktop.getNumComponents(); ++i)
  652. {
  653. if (auto* mw = dynamic_cast<MainWindow*> (desktop.getComponent(i)))
  654. if (auto* p = mw->getProject())
  655. if (! p->isTemporaryProject())
  656. projects.add (p->getFile());
  657. }
  658. getAppSettings().setLastProjects (projects);
  659. }
  660. void MainWindowList::reopenLastProjects()
  661. {
  662. const ScopedValueSetter<bool> setter (isInReopenLastProjects, true);
  663. for (auto& p : getAppSettings().getLastProjects())
  664. if (p.existsAsFile())
  665. openFile (p, true);
  666. }
  667. void MainWindowList::sendLookAndFeelChange()
  668. {
  669. for (auto* w : windows)
  670. w->sendLookAndFeelChange();
  671. }
  672. Project* MainWindowList::getFrontmostProject()
  673. {
  674. auto& desktop = Desktop::getInstance();
  675. for (int i = desktop.getNumComponents(); --i >= 0;)
  676. if (auto* mw = dynamic_cast<MainWindow*> (desktop.getComponent(i)))
  677. if (auto* p = mw->getProject())
  678. return p;
  679. return nullptr;
  680. }