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.

592 lines
17KB

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