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.

589 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. {
  216. String projectName (currentProject->getDocumentTitle());
  217. if (currentProject->getFile().getFileNameWithoutExtension() != projectName)
  218. projectName = currentProject->getFile().getFileName();
  219. name << " - " << projectName;
  220. }
  221. if (documentName.isNotEmpty())
  222. name << " - " << documentName;
  223. setName (name);
  224. }
  225. void MainWindow::showNewProjectWizard()
  226. {
  227. jassert (currentProject == nullptr);
  228. setContentOwned (createNewProjectWizardComponent(), true);
  229. centreWithSize (900, 630);
  230. setVisible (true);
  231. addToDesktop();
  232. getContentComponent()->grabKeyboardFocus();
  233. }
  234. //==============================================================================
  235. ApplicationCommandTarget* MainWindow::getNextCommandTarget()
  236. {
  237. return nullptr;
  238. }
  239. void MainWindow::getAllCommands (Array <CommandID>& commands)
  240. {
  241. const CommandID ids[] = { CommandIDs::closeWindow };
  242. commands.addArray (ids, numElementsInArray (ids));
  243. }
  244. void MainWindow::getCommandInfo (const CommandID commandID, ApplicationCommandInfo& result)
  245. {
  246. switch (commandID)
  247. {
  248. case CommandIDs::closeWindow:
  249. result.setInfo ("Close Window", "Closes the current window", CommandCategories::general, 0);
  250. result.defaultKeypresses.add (KeyPress ('w', ModifierKeys::commandModifier, 0));
  251. break;
  252. default:
  253. break;
  254. }
  255. }
  256. bool MainWindow::perform (const InvocationInfo& info)
  257. {
  258. switch (info.commandID)
  259. {
  260. case CommandIDs::closeWindow:
  261. closeButtonPressed();
  262. break;
  263. default:
  264. return false;
  265. }
  266. return true;
  267. }
  268. //==============================================================================
  269. MainWindowList::MainWindowList()
  270. {
  271. }
  272. void MainWindowList::forceCloseAllWindows()
  273. {
  274. windows.clear();
  275. }
  276. bool MainWindowList::askAllWindowsToClose()
  277. {
  278. saveCurrentlyOpenProjectList();
  279. while (windows.size() > 0)
  280. {
  281. if (! windows[0]->closeCurrentProject())
  282. return false;
  283. windows.remove (0);
  284. }
  285. return true;
  286. }
  287. void MainWindowList::createWindowIfNoneAreOpen()
  288. {
  289. if (windows.size() == 0)
  290. createNewMainWindow()->showNewProjectWizard();
  291. }
  292. void MainWindowList::closeWindow (MainWindow* w)
  293. {
  294. jassert (windows.contains (w));
  295. #if ! JUCE_MAC
  296. if (windows.size() == 1)
  297. {
  298. JUCEApplicationBase::getInstance()->systemRequestedQuit();
  299. }
  300. else
  301. #endif
  302. {
  303. if (w->closeCurrentProject())
  304. {
  305. windows.removeObject (w);
  306. saveCurrentlyOpenProjectList();
  307. }
  308. }
  309. }
  310. void MainWindowList::openDocument (OpenDocumentManager::Document* doc, bool grabFocus)
  311. {
  312. getOrCreateFrontmostWindow()->getProjectContentComponent()->showDocument (doc, grabFocus);
  313. }
  314. bool MainWindowList::openFile (const File& file)
  315. {
  316. for (int i = windows.size(); --i >= 0;)
  317. {
  318. MainWindow* const w = windows.getUnchecked(i);
  319. if (w->getProject() != nullptr && w->getProject()->getFile() == file)
  320. {
  321. w->toFront (true);
  322. return true;
  323. }
  324. }
  325. if (file.hasFileExtension (Project::projectFileExtension))
  326. {
  327. MainWindow* const w = getOrCreateEmptyWindow();
  328. bool ok = w->openFile (file);
  329. w->makeVisible();
  330. avoidSuperimposedWindows (w);
  331. return ok;
  332. }
  333. if (file.exists())
  334. return getOrCreateFrontmostWindow()->openFile (file);
  335. return false;
  336. }
  337. MainWindow* MainWindowList::createNewMainWindow()
  338. {
  339. MainWindow* const w = new MainWindow();
  340. windows.add (w);
  341. w->restoreWindowPosition();
  342. avoidSuperimposedWindows (w);
  343. return w;
  344. }
  345. MainWindow* MainWindowList::getOrCreateFrontmostWindow()
  346. {
  347. if (windows.size() == 0)
  348. {
  349. MainWindow* w = createNewMainWindow();
  350. avoidSuperimposedWindows (w);
  351. w->makeVisible();
  352. return w;
  353. }
  354. for (int i = Desktop::getInstance().getNumComponents(); --i >= 0;)
  355. {
  356. MainWindow* mw = dynamic_cast<MainWindow*> (Desktop::getInstance().getComponent (i));
  357. if (windows.contains (mw))
  358. return mw;
  359. }
  360. return windows.getLast();
  361. }
  362. MainWindow* MainWindowList::getOrCreateEmptyWindow()
  363. {
  364. if (windows.size() == 0)
  365. return createNewMainWindow();
  366. for (int i = Desktop::getInstance().getNumComponents(); --i >= 0;)
  367. {
  368. MainWindow* mw = dynamic_cast<MainWindow*> (Desktop::getInstance().getComponent (i));
  369. if (windows.contains (mw) && mw->getProject() == nullptr)
  370. return mw;
  371. }
  372. return createNewMainWindow();
  373. }
  374. void MainWindowList::updateAllWindowTitles()
  375. {
  376. for (int i = 0; i < windows.size(); ++i)
  377. if (ProjectContentComponent* pc = windows.getUnchecked(i)->getProjectContentComponent())
  378. pc->updateMainWindowTitle();
  379. }
  380. void MainWindowList::avoidSuperimposedWindows (MainWindow* const mw)
  381. {
  382. for (int i = windows.size(); --i >= 0;)
  383. {
  384. MainWindow* const other = windows.getUnchecked(i);
  385. const Rectangle<int> b1 (mw->getBounds());
  386. const Rectangle<int> b2 (other->getBounds());
  387. if (mw != other
  388. && std::abs (b1.getX() - b2.getX()) < 3
  389. && std::abs (b1.getY() - b2.getY()) < 3
  390. && std::abs (b1.getRight() - b2.getRight()) < 3
  391. && std::abs (b1.getBottom() - b2.getBottom()) < 3)
  392. {
  393. int dx = 40, dy = 30;
  394. if (b1.getCentreX() >= mw->getScreenBounds().getCentreX()) dx = -dx;
  395. if (b1.getCentreY() >= mw->getScreenBounds().getCentreY()) dy = -dy;
  396. mw->setBounds (b1.translated (dx, dy));
  397. }
  398. }
  399. }
  400. void MainWindowList::saveCurrentlyOpenProjectList()
  401. {
  402. Array<File> projects;
  403. Desktop& desktop = Desktop::getInstance();
  404. for (int i = 0; i < desktop.getNumComponents(); ++i)
  405. {
  406. if (MainWindow* const mw = dynamic_cast<MainWindow*> (desktop.getComponent(i)))
  407. if (Project* p = mw->getProject())
  408. projects.add (p->getFile());
  409. }
  410. getAppSettings().setLastProjects (projects);
  411. }
  412. void MainWindowList::reopenLastProjects()
  413. {
  414. Array<File> projects (getAppSettings().getLastProjects());
  415. for (int i = 0; i < projects.size(); ++ i)
  416. openFile (projects.getReference(i));
  417. }
  418. void MainWindowList::sendLookAndFeelChange()
  419. {
  420. for (int i = windows.size(); --i >= 0;)
  421. windows.getUnchecked(i)->sendLookAndFeelChange();
  422. }
  423. Project* MainWindowList::getFrontmostProject()
  424. {
  425. Desktop& desktop = Desktop::getInstance();
  426. for (int i = desktop.getNumComponents(); --i >= 0;)
  427. if (MainWindow* const mw = dynamic_cast<MainWindow*> (desktop.getComponent(i)))
  428. if (Project* p = mw->getProject())
  429. return p;
  430. return nullptr;
  431. }
  432. File findDefaultModulesFolder (bool mustContainJuceCoreModule)
  433. {
  434. const MainWindowList& windows = IntrojucerApp::getApp().mainWindowList;
  435. for (int i = windows.windows.size(); --i >= 0;)
  436. {
  437. if (Project* p = windows.windows.getUnchecked (i)->getProject())
  438. {
  439. const File f (EnabledModuleList::findDefaultModulesFolder (*p));
  440. if (isJuceModulesFolder (f) || (f.isDirectory() && ! mustContainJuceCoreModule))
  441. return f;
  442. }
  443. }
  444. if (mustContainJuceCoreModule)
  445. return findDefaultModulesFolder (false);
  446. File f (File::getSpecialLocation (File::currentApplicationFile));
  447. for (;;)
  448. {
  449. File parent (f.getParentDirectory());
  450. if (parent == f || ! parent.isDirectory())
  451. break;
  452. if (isJuceFolder (parent))
  453. return parent.getChildFile ("modules");
  454. f = parent;
  455. }
  456. return File::nonexistent;
  457. }