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.

543 lines
15KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2013 - Raw Material Software 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 "../Project/jucer_NewProjectWizard.h"
  23. #include "../Utility/jucer_JucerTreeViewBase.h"
  24. ScopedPointer<ApplicationCommandManager> commandManager;
  25. //==============================================================================
  26. MainWindow::MainWindow()
  27. : DocumentWindow (IntrojucerApp::getApp().getApplicationName(),
  28. Colour::greyLevel (0.6f),
  29. DocumentWindow::allButtons,
  30. false)
  31. {
  32. setUsingNativeTitleBar (true);
  33. createProjectContentCompIfNeeded();
  34. #if ! JUCE_MAC
  35. setMenuBar (IntrojucerApp::getApp().menuModel);
  36. #endif
  37. setResizable (true, false);
  38. centreWithSize (800, 600);
  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. //getPeer()->setCurrentRenderingEngine (0);
  53. getLookAndFeel().setColour (ColourSelector::backgroundColourId, Colours::transparentBlack);
  54. setResizeLimits (600, 500, 32000, 32000);
  55. }
  56. MainWindow::~MainWindow()
  57. {
  58. #if ! JUCE_MAC
  59. setMenuBar (nullptr);
  60. #endif
  61. removeKeyListener (commandManager->getKeyMappings());
  62. // save the current size and position to our settings file..
  63. getGlobalProperties().setValue ("lastMainWindowPos", getWindowStateAsString());
  64. clearContentComponent();
  65. currentProject = nullptr;
  66. }
  67. void MainWindow::createProjectContentCompIfNeeded()
  68. {
  69. if (getProjectContentComponent() == nullptr)
  70. {
  71. clearContentComponent();
  72. setContentOwned (IntrojucerApp::getApp().createProjectContentComponent(), false);
  73. jassert (getProjectContentComponent() != nullptr);
  74. }
  75. }
  76. void MainWindow::makeVisible()
  77. {
  78. restoreWindowPosition();
  79. setVisible (true);
  80. addToDesktop(); // (must add before restoring size so that fullscreen will work)
  81. restoreWindowPosition();
  82. getContentComponent()->grabKeyboardFocus();
  83. }
  84. ProjectContentComponent* MainWindow::getProjectContentComponent() const
  85. {
  86. return dynamic_cast <ProjectContentComponent*> (getContentComponent());
  87. }
  88. void MainWindow::closeButtonPressed()
  89. {
  90. IntrojucerApp::getApp().mainWindowList.closeWindow (this);
  91. }
  92. bool MainWindow::closeProject (Project* project)
  93. {
  94. jassert (project == currentProject && project != nullptr);
  95. if (project == nullptr)
  96. return true;
  97. project->getStoredProperties().setValue (getProjectWindowPosName(), getWindowStateAsString());
  98. if (ProjectContentComponent* const pcc = getProjectContentComponent())
  99. {
  100. pcc->saveTreeViewState();
  101. pcc->saveOpenDocumentList();
  102. pcc->hideEditor();
  103. }
  104. if (! IntrojucerApp::getApp().openDocumentManager.closeAllDocumentsUsingProject (*project, true))
  105. return false;
  106. FileBasedDocument::SaveResult r = project->saveIfNeededAndUserAgrees();
  107. if (r == FileBasedDocument::savedOk)
  108. {
  109. setProject (nullptr);
  110. return true;
  111. }
  112. return false;
  113. }
  114. bool MainWindow::closeCurrentProject()
  115. {
  116. return currentProject == nullptr || closeProject (currentProject);
  117. }
  118. void MainWindow::setProject (Project* newProject)
  119. {
  120. createProjectContentCompIfNeeded();
  121. getProjectContentComponent()->setProject (newProject);
  122. currentProject = newProject;
  123. getProjectContentComponent()->updateMainWindowTitle();
  124. commandManager->commandStatusChanged();
  125. }
  126. void MainWindow::restoreWindowPosition()
  127. {
  128. String windowState;
  129. if (currentProject != nullptr)
  130. windowState = currentProject->getStoredProperties().getValue (getProjectWindowPosName());
  131. if (windowState.isEmpty())
  132. windowState = getGlobalProperties().getValue ("lastMainWindowPos");
  133. restoreWindowStateFromString (windowState);
  134. }
  135. bool MainWindow::canOpenFile (const File& file) const
  136. {
  137. return (! file.isDirectory())
  138. && (file.hasFileExtension (Project::projectFileExtension)
  139. || IntrojucerApp::getApp().openDocumentManager.canOpenFile (file));
  140. }
  141. bool MainWindow::openFile (const File& file)
  142. {
  143. createProjectContentCompIfNeeded();
  144. if (file.hasFileExtension (Project::projectFileExtension))
  145. {
  146. ScopedPointer <Project> newDoc (new Project (file));
  147. if (newDoc->loadFrom (file, true)
  148. && closeCurrentProject())
  149. {
  150. setProject (newDoc.release());
  151. return true;
  152. }
  153. }
  154. else if (file.exists())
  155. {
  156. return getProjectContentComponent()->showEditorForFile (file, true);
  157. }
  158. return false;
  159. }
  160. bool MainWindow::isInterestedInFileDrag (const StringArray& filenames)
  161. {
  162. for (int i = filenames.size(); --i >= 0;)
  163. if (canOpenFile (filenames[i]))
  164. return true;
  165. return false;
  166. }
  167. void MainWindow::filesDropped (const StringArray& filenames, int /*mouseX*/, int /*mouseY*/)
  168. {
  169. for (int i = filenames.size(); --i >= 0;)
  170. {
  171. const File f (filenames[i]);
  172. if (canOpenFile (f) && openFile (f))
  173. break;
  174. }
  175. }
  176. bool MainWindow::shouldDropFilesWhenDraggedExternally (const DragAndDropTarget::SourceDetails& sourceDetails,
  177. StringArray& files, bool& canMoveFiles)
  178. {
  179. if (TreeView* tv = dynamic_cast <TreeView*> (sourceDetails.sourceComponent.get()))
  180. {
  181. Array<JucerTreeViewBase*> selected;
  182. for (int i = tv->getNumSelectedItems(); --i >= 0;)
  183. if (JucerTreeViewBase* b = dynamic_cast <JucerTreeViewBase*> (tv->getSelectedItem(i)))
  184. selected.add (b);
  185. if (selected.size() > 0)
  186. {
  187. for (int i = selected.size(); --i >= 0;)
  188. {
  189. if (JucerTreeViewBase* jtvb = selected.getUnchecked(i))
  190. {
  191. const File f (jtvb->getDraggableFile());
  192. if (f.existsAsFile())
  193. files.add (f.getFullPathName());
  194. }
  195. }
  196. canMoveFiles = false;
  197. return files.size() > 0;
  198. }
  199. }
  200. return false;
  201. }
  202. void MainWindow::activeWindowStatusChanged()
  203. {
  204. DocumentWindow::activeWindowStatusChanged();
  205. if (ProjectContentComponent* const pcc = getProjectContentComponent())
  206. pcc->updateMissingFileStatuses();
  207. IntrojucerApp::getApp().openDocumentManager.reloadModifiedFiles();
  208. }
  209. void MainWindow::updateTitle (const String& documentName)
  210. {
  211. String name (IntrojucerApp::getApp().getApplicationName());
  212. if (currentProject != nullptr)
  213. name << " - " << currentProject->getDocumentTitle();
  214. if (documentName.isNotEmpty())
  215. name << " - " << documentName;
  216. setName (name);
  217. }
  218. void MainWindow::showNewProjectWizard()
  219. {
  220. jassert (currentProject == nullptr);
  221. setContentOwned (createNewProjectWizardComponent(), true);
  222. makeVisible();
  223. }
  224. //==============================================================================
  225. ApplicationCommandTarget* MainWindow::getNextCommandTarget()
  226. {
  227. return nullptr;
  228. }
  229. void MainWindow::getAllCommands (Array <CommandID>& commands)
  230. {
  231. const CommandID ids[] = { CommandIDs::closeWindow };
  232. commands.addArray (ids, numElementsInArray (ids));
  233. }
  234. void MainWindow::getCommandInfo (const CommandID commandID, ApplicationCommandInfo& result)
  235. {
  236. switch (commandID)
  237. {
  238. case CommandIDs::closeWindow:
  239. result.setInfo ("Close Window", "Closes the current window", CommandCategories::general, 0);
  240. result.defaultKeypresses.add (KeyPress ('w', ModifierKeys::commandModifier, 0));
  241. break;
  242. default:
  243. break;
  244. }
  245. }
  246. bool MainWindow::perform (const InvocationInfo& info)
  247. {
  248. switch (info.commandID)
  249. {
  250. case CommandIDs::closeWindow:
  251. closeButtonPressed();
  252. break;
  253. default:
  254. return false;
  255. }
  256. return true;
  257. }
  258. //==============================================================================
  259. MainWindowList::MainWindowList()
  260. {
  261. }
  262. void MainWindowList::forceCloseAllWindows()
  263. {
  264. windows.clear();
  265. }
  266. bool MainWindowList::askAllWindowsToClose()
  267. {
  268. saveCurrentlyOpenProjectList();
  269. while (windows.size() > 0)
  270. {
  271. if (! windows[0]->closeCurrentProject())
  272. return false;
  273. windows.remove (0);
  274. }
  275. return true;
  276. }
  277. void MainWindowList::createWindowIfNoneAreOpen()
  278. {
  279. if (windows.size() == 0)
  280. createNewMainWindow()->makeVisible();
  281. }
  282. void MainWindowList::closeWindow (MainWindow* w)
  283. {
  284. jassert (windows.contains (w));
  285. #if ! JUCE_MAC
  286. if (windows.size() == 1)
  287. {
  288. JUCEApplication::getInstance()->systemRequestedQuit();
  289. }
  290. else
  291. #endif
  292. {
  293. if (w->closeCurrentProject())
  294. {
  295. windows.removeObject (w);
  296. saveCurrentlyOpenProjectList();
  297. }
  298. }
  299. }
  300. void MainWindowList::openDocument (OpenDocumentManager::Document* doc, bool grabFocus)
  301. {
  302. MainWindow* w = getOrCreateFrontmostWindow();
  303. w->getProjectContentComponent()->showDocument (doc, grabFocus);
  304. }
  305. bool MainWindowList::openFile (const File& file)
  306. {
  307. for (int i = windows.size(); --i >= 0;)
  308. {
  309. MainWindow* const w = windows.getUnchecked(i);
  310. if (w->getProject() != nullptr && w->getProject()->getFile() == file)
  311. {
  312. w->toFront (true);
  313. return true;
  314. }
  315. }
  316. if (file.hasFileExtension (Project::projectFileExtension))
  317. {
  318. ScopedPointer <Project> newDoc (new Project (file));
  319. if (newDoc->loadFrom (file, true))
  320. {
  321. MainWindow* const w = getOrCreateEmptyWindow();
  322. w->setProject (newDoc);
  323. newDoc.release()->setChangedFlag (false);
  324. w->makeVisible();
  325. avoidSuperimposedWindows (w);
  326. jassert (w->getProjectContentComponent() != nullptr);
  327. w->getProjectContentComponent()->reloadLastOpenDocuments();
  328. return true;
  329. }
  330. }
  331. else if (file.exists())
  332. {
  333. MainWindow* const w = getOrCreateFrontmostWindow();
  334. return w->openFile (file);
  335. }
  336. return false;
  337. }
  338. MainWindow* MainWindowList::createNewMainWindow()
  339. {
  340. MainWindow* const w = new MainWindow();
  341. windows.add (w);
  342. w->restoreWindowPosition();
  343. avoidSuperimposedWindows (w);
  344. return w;
  345. }
  346. MainWindow* MainWindowList::getOrCreateFrontmostWindow()
  347. {
  348. if (windows.size() == 0)
  349. {
  350. MainWindow* w = createNewMainWindow();
  351. avoidSuperimposedWindows (w);
  352. w->makeVisible();
  353. return w;
  354. }
  355. for (int i = Desktop::getInstance().getNumComponents(); --i >= 0;)
  356. {
  357. MainWindow* mw = dynamic_cast <MainWindow*> (Desktop::getInstance().getComponent (i));
  358. if (windows.contains (mw))
  359. return mw;
  360. }
  361. return windows.getLast();
  362. }
  363. MainWindow* MainWindowList::getOrCreateEmptyWindow()
  364. {
  365. if (windows.size() == 0)
  366. return createNewMainWindow();
  367. for (int i = Desktop::getInstance().getNumComponents(); --i >= 0;)
  368. {
  369. MainWindow* mw = dynamic_cast <MainWindow*> (Desktop::getInstance().getComponent (i));
  370. if (windows.contains (mw) && mw->getProject() == nullptr)
  371. return mw;
  372. }
  373. return createNewMainWindow();
  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. }