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.

580 lines
20KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library - "Jules' Utility Class Extensions"
  4. Copyright 2004-11 by Raw Material Software Ltd.
  5. ------------------------------------------------------------------------------
  6. JUCE can be redistributed and/or modified under the terms of the GNU General
  7. Public License (Version 2), as published by the Free Software Foundation.
  8. A copy of the license is included in the JUCE distribution, or can be found
  9. online at www.gnu.org/licenses.
  10. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  11. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  12. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  13. ------------------------------------------------------------------------------
  14. To release a closed-source product which uses JUCE, commercial licenses are
  15. available: visit www.rawmaterialsoftware.com/juce for more information.
  16. ==============================================================================
  17. */
  18. #ifndef __JUCER_APPLICATION_JUCEHEADER__
  19. #define __JUCER_APPLICATION_JUCEHEADER__
  20. #include "../jucer_Headers.h"
  21. #include "jucer_MainWindow.h"
  22. #include "jucer_JuceUpdater.h"
  23. #include "jucer_CommandLine.h"
  24. //==============================================================================
  25. class JucerApplication : public JUCEApplication
  26. {
  27. public:
  28. //==============================================================================
  29. JucerApplication() {}
  30. ~JucerApplication() {}
  31. //==============================================================================
  32. void initialise (const String& commandLine)
  33. {
  34. if (commandLine.isNotEmpty())
  35. {
  36. const int appReturnCode = performCommandLine (commandLine);
  37. if (appReturnCode != commandLineNotPerformed)
  38. {
  39. setApplicationReturnValue (appReturnCode);
  40. quit();
  41. return;
  42. }
  43. }
  44. commandManager = new ApplicationCommandManager();
  45. commandManager->registerAllCommandsForTarget (this);
  46. menuModel = new MainMenuModel();
  47. doExtraInitialisation();
  48. ImageCache::setCacheTimeout (30 * 1000);
  49. if (commandLine.trim().isNotEmpty() && ! commandLine.trim().startsWithChar ('-'))
  50. {
  51. anotherInstanceStarted (commandLine);
  52. }
  53. else
  54. {
  55. Array<File> projects (StoredSettings::getInstance()->getLastProjects());
  56. for (int i = 0; i < projects.size(); ++ i)
  57. openFile (projects.getReference(i));
  58. }
  59. makeSureUserHasSelectedModuleFolder();
  60. if (mainWindows.size() == 0)
  61. createNewMainWindow()->makeVisible();
  62. #if JUCE_MAC
  63. MenuBarModel::setMacMainMenu (menuModel);
  64. #endif
  65. }
  66. void shutdown()
  67. {
  68. #if JUCE_MAC
  69. MenuBarModel::setMacMainMenu (nullptr);
  70. #endif
  71. menuModel = nullptr;
  72. StoredSettings::deleteInstance();
  73. mainWindows.clear();
  74. OpenDocumentManager::deleteInstance();
  75. commandManager = nullptr;
  76. }
  77. //==============================================================================
  78. void systemRequestedQuit()
  79. {
  80. if (cancelAnyModalComponents())
  81. {
  82. new AsyncQuitRetrier();
  83. return;
  84. }
  85. while (mainWindows.size() > 0)
  86. {
  87. if (! mainWindows[0]->closeCurrentProject())
  88. return;
  89. mainWindows.remove (0);
  90. }
  91. quit();
  92. }
  93. void closeWindow (MainWindow* w)
  94. {
  95. jassert (mainWindows.contains (w));
  96. mainWindows.removeObject (w);
  97. #if ! JUCE_MAC
  98. if (mainWindows.size() == 0)
  99. systemRequestedQuit();
  100. #endif
  101. updateRecentProjectList();
  102. }
  103. //==============================================================================
  104. const String getApplicationName()
  105. {
  106. return String (ProjectInfo::projectName) + " " + getApplicationVersion();
  107. }
  108. const String getApplicationVersion()
  109. {
  110. return ProjectInfo::versionString;
  111. }
  112. bool moreThanOneInstanceAllowed()
  113. {
  114. #ifndef JUCE_LINUX
  115. return false;
  116. #else
  117. return true; //xxx should be false but doesn't work on linux..
  118. #endif
  119. }
  120. void anotherInstanceStarted (const String& commandLine)
  121. {
  122. openFile (commandLine.unquoted());
  123. }
  124. virtual void doExtraInitialisation() {}
  125. virtual void projectOpened (Project*) {}
  126. virtual void projectClosed (Project*) {}
  127. static JucerApplication* getApp()
  128. {
  129. return dynamic_cast<JucerApplication*> (JUCEApplication::getInstance());
  130. }
  131. //==============================================================================
  132. class MainMenuModel : public MenuBarModel
  133. {
  134. public:
  135. MainMenuModel()
  136. {
  137. setApplicationCommandManagerToWatch (commandManager);
  138. }
  139. StringArray getMenuBarNames()
  140. {
  141. const char* const names[] = { "File", "Edit", "View", "Window", "Tools", 0 };
  142. return StringArray ((const char**) names);
  143. }
  144. PopupMenu getMenuForIndex (int topLevelMenuIndex, const String& /*menuName*/)
  145. {
  146. PopupMenu menu;
  147. if (topLevelMenuIndex == 0) // "File" menu
  148. {
  149. menu.addCommandItem (commandManager, CommandIDs::newProject);
  150. menu.addSeparator();
  151. menu.addCommandItem (commandManager, CommandIDs::open);
  152. PopupMenu recentFiles;
  153. StoredSettings::getInstance()->recentFiles.createPopupMenuItems (recentFiles, 100, true, true);
  154. menu.addSubMenu ("Open recent file", recentFiles);
  155. menu.addSeparator();
  156. menu.addCommandItem (commandManager, CommandIDs::closeDocument);
  157. menu.addCommandItem (commandManager, CommandIDs::saveDocument);
  158. menu.addCommandItem (commandManager, CommandIDs::saveDocumentAs);
  159. menu.addSeparator();
  160. menu.addCommandItem (commandManager, CommandIDs::closeProject);
  161. menu.addCommandItem (commandManager, CommandIDs::saveProject);
  162. menu.addCommandItem (commandManager, CommandIDs::saveProjectAs);
  163. menu.addSeparator();
  164. menu.addCommandItem (commandManager, CommandIDs::openInIDE);
  165. menu.addCommandItem (commandManager, CommandIDs::saveAndOpenInIDE);
  166. #if ! JUCE_MAC
  167. menu.addSeparator();
  168. menu.addCommandItem (commandManager, StandardApplicationCommandIDs::quit);
  169. #endif
  170. }
  171. else if (topLevelMenuIndex == 1) // "Edit" menu
  172. {
  173. menu.addCommandItem (commandManager, CommandIDs::undo);
  174. menu.addCommandItem (commandManager, CommandIDs::redo);
  175. menu.addSeparator();
  176. menu.addCommandItem (commandManager, StandardApplicationCommandIDs::cut);
  177. menu.addCommandItem (commandManager, StandardApplicationCommandIDs::copy);
  178. menu.addCommandItem (commandManager, StandardApplicationCommandIDs::paste);
  179. menu.addCommandItem (commandManager, StandardApplicationCommandIDs::del);
  180. menu.addCommandItem (commandManager, StandardApplicationCommandIDs::selectAll);
  181. menu.addCommandItem (commandManager, StandardApplicationCommandIDs::deselectAll);
  182. menu.addSeparator();
  183. menu.addCommandItem (commandManager, CommandIDs::toFront);
  184. menu.addCommandItem (commandManager, CommandIDs::toBack);
  185. menu.addSeparator();
  186. menu.addCommandItem (commandManager, CommandIDs::group);
  187. menu.addCommandItem (commandManager, CommandIDs::ungroup);
  188. menu.addSeparator();
  189. menu.addCommandItem (commandManager, CommandIDs::bringBackLostItems);
  190. }
  191. else if (topLevelMenuIndex == 2) // "View" menu
  192. {
  193. menu.addCommandItem (commandManager, CommandIDs::showProjectSettings);
  194. menu.addSeparator();
  195. menu.addCommandItem (commandManager, CommandIDs::test);
  196. menu.addSeparator();
  197. menu.addCommandItem (commandManager, CommandIDs::showGrid);
  198. menu.addCommandItem (commandManager, CommandIDs::enableSnapToGrid);
  199. menu.addSeparator();
  200. menu.addCommandItem (commandManager, CommandIDs::zoomIn);
  201. menu.addCommandItem (commandManager, CommandIDs::zoomOut);
  202. menu.addCommandItem (commandManager, CommandIDs::zoomNormal);
  203. menu.addSeparator();
  204. menu.addCommandItem (commandManager, CommandIDs::useTabbedWindows);
  205. }
  206. else if (topLevelMenuIndex == 3) // "Window" menu
  207. {
  208. menu.addCommandItem (commandManager, CommandIDs::closeWindow);
  209. menu.addSeparator();
  210. const int numDocs = jmin (50, OpenDocumentManager::getInstance()->getNumOpenDocuments());
  211. for (int i = 0; i < numDocs; ++i)
  212. {
  213. OpenDocumentManager::Document* doc = OpenDocumentManager::getInstance()->getOpenDocument(i);
  214. menu.addItem (300 + i, doc->getName());
  215. }
  216. menu.addSeparator();
  217. menu.addCommandItem (commandManager, CommandIDs::closeAllDocuments);
  218. }
  219. else if (topLevelMenuIndex == 4) // "Tools" menu
  220. {
  221. menu.addCommandItem (commandManager, CommandIDs::updateModules);
  222. menu.addCommandItem (commandManager, CommandIDs::showUTF8Tool);
  223. }
  224. return menu;
  225. }
  226. void menuItemSelected (int menuItemID, int /*topLevelMenuIndex*/)
  227. {
  228. if (menuItemID >= 100 && menuItemID < 200)
  229. {
  230. // open a file from the "recent files" menu
  231. const File file (StoredSettings::getInstance()->recentFiles.getFile (menuItemID - 100));
  232. getApp()->openFile (file);
  233. }
  234. else if (menuItemID >= 300 && menuItemID < 400)
  235. {
  236. OpenDocumentManager::Document* doc = OpenDocumentManager::getInstance()->getOpenDocument (menuItemID - 300);
  237. MainWindow* w = getApp()->getOrCreateFrontmostWindow();
  238. w->makeVisible();
  239. w->getProjectContentComponent()->showDocument (doc);
  240. getApp()->avoidSuperimposedWindows (w);
  241. }
  242. }
  243. };
  244. //==============================================================================
  245. void getAllCommands (Array <CommandID>& commands)
  246. {
  247. JUCEApplication::getAllCommands (commands);
  248. const CommandID ids[] = { CommandIDs::newProject,
  249. CommandIDs::open,
  250. CommandIDs::showPrefs,
  251. CommandIDs::closeAllDocuments,
  252. CommandIDs::saveAll,
  253. CommandIDs::updateModules,
  254. CommandIDs::showUTF8Tool };
  255. commands.addArray (ids, numElementsInArray (ids));
  256. }
  257. void getCommandInfo (CommandID commandID, ApplicationCommandInfo& result)
  258. {
  259. switch (commandID)
  260. {
  261. case CommandIDs::newProject:
  262. result.setInfo ("New Project...", "Creates a new Jucer project", CommandCategories::general, 0);
  263. result.defaultKeypresses.add (KeyPress ('n', ModifierKeys::commandModifier, 0));
  264. break;
  265. case CommandIDs::open:
  266. result.setInfo ("Open...", "Opens a Jucer project", CommandCategories::general, 0);
  267. result.defaultKeypresses.add (KeyPress ('o', ModifierKeys::commandModifier, 0));
  268. break;
  269. case CommandIDs::showPrefs:
  270. result.setInfo ("Preferences...", "Shows the preferences panel.", CommandCategories::general, 0);
  271. result.defaultKeypresses.add (KeyPress (',', ModifierKeys::commandModifier, 0));
  272. break;
  273. case CommandIDs::closeAllDocuments:
  274. result.setInfo ("Close All Documents", "Closes all open documents", CommandCategories::general, 0);
  275. result.setActive (OpenDocumentManager::getInstance()->getNumOpenDocuments() > 0);
  276. break;
  277. case CommandIDs::saveAll:
  278. result.setInfo ("Save All", "Saves all open documents", CommandCategories::general, 0);
  279. result.setActive (OpenDocumentManager::getInstance()->anyFilesNeedSaving());
  280. break;
  281. case CommandIDs::updateModules:
  282. result.setInfo ("Download the latest JUCE modules", "Checks online for any JUCE modules updates and installs them", CommandCategories::general, 0);
  283. break;
  284. case CommandIDs::showUTF8Tool:
  285. result.setInfo ("UTF-8 String-Literal Helper", "Shows the UTF-8 string literal utility", CommandCategories::general, 0);
  286. break;
  287. default:
  288. JUCEApplication::getCommandInfo (commandID, result);
  289. break;
  290. }
  291. }
  292. bool perform (const InvocationInfo& info)
  293. {
  294. switch (info.commandID)
  295. {
  296. case CommandIDs::newProject: createNewProject(); break;
  297. case CommandIDs::open: askUserToOpenFile(); break;
  298. case CommandIDs::showPrefs: showPrefsPanel(); break;
  299. case CommandIDs::saveAll: OpenDocumentManager::getInstance()->saveAll(); break;
  300. case CommandIDs::closeAllDocuments: closeAllDocuments (true); break;
  301. case CommandIDs::showUTF8Tool: showUTF8ToolWindow(); break;
  302. case CommandIDs::updateModules: runModuleUpdate (String::empty); break;
  303. default: return JUCEApplication::perform (info);
  304. }
  305. return true;
  306. }
  307. //==============================================================================
  308. void showPrefsPanel()
  309. {
  310. jassertfalse;
  311. }
  312. void createNewProject()
  313. {
  314. if (makeSureUserHasSelectedModuleFolder())
  315. {
  316. MainWindow* mw = getOrCreateEmptyWindow();
  317. mw->showNewProjectWizard();
  318. avoidSuperimposedWindows (mw);
  319. }
  320. }
  321. void askUserToOpenFile()
  322. {
  323. FileChooser fc ("Open File");
  324. if (fc.browseForFileToOpen())
  325. openFile (fc.getResult());
  326. }
  327. bool openFile (const File& file)
  328. {
  329. for (int j = mainWindows.size(); --j >= 0;)
  330. {
  331. if (mainWindows.getUnchecked(j)->getProject() != nullptr
  332. && mainWindows.getUnchecked(j)->getProject()->getFile() == file)
  333. {
  334. mainWindows.getUnchecked(j)->toFront (true);
  335. return true;
  336. }
  337. }
  338. if (file.hasFileExtension (Project::projectFileExtension))
  339. {
  340. ScopedPointer <Project> newDoc (new Project (file));
  341. if (newDoc->loadFrom (file, true))
  342. {
  343. MainWindow* w = getOrCreateEmptyWindow();
  344. w->setProject (newDoc.release());
  345. w->makeVisible();
  346. avoidSuperimposedWindows (w);
  347. return true;
  348. }
  349. }
  350. else if (file.exists())
  351. {
  352. MainWindow* w = getOrCreateFrontmostWindow();
  353. const bool ok = w->openFile (file);
  354. w->makeVisible();
  355. avoidSuperimposedWindows (w);
  356. return ok;
  357. }
  358. return false;
  359. }
  360. bool closeAllDocuments (bool askUserToSave)
  361. {
  362. return OpenDocumentManager::getInstance()->closeAll (askUserToSave);
  363. }
  364. void updateRecentProjectList()
  365. {
  366. Array<File> projects;
  367. for (int i = 0; i < mainWindows.size(); ++i)
  368. {
  369. MainWindow* mw = mainWindows[i];
  370. if (mw != nullptr && mw->getProject() != nullptr)
  371. projects.add (mw->getProject()->getFile());
  372. }
  373. StoredSettings::getInstance()->setLastProjects (projects);
  374. }
  375. bool makeSureUserHasSelectedModuleFolder()
  376. {
  377. if (! ModuleList::isLocalModulesFolderValid())
  378. {
  379. if (! runModuleUpdate ("Please select a location to store your local set of JUCE modules,\n"
  380. "and download the ones that you'd like to use!"))
  381. {
  382. AlertWindow::showMessageBox (AlertWindow::WarningIcon,
  383. "Introjucer",
  384. "Unless you create a local JUCE folder containing some modules, you'll be unable to save any projects correctly!\n\n"
  385. "Use the option on the 'Tools' menu to set this up!");
  386. return false;
  387. }
  388. }
  389. return true;
  390. }
  391. bool runModuleUpdate (const String& message)
  392. {
  393. ModuleList list;
  394. list.rescan (ModuleList::getDefaultModulesFolder (nullptr));
  395. JuceUpdater::show (list, mainWindows[0], message);
  396. ModuleList::setLocalModulesFolder (list.getModulesFolder());
  397. return ModuleList::isJuceOrModulesFolder (list.getModulesFolder());
  398. }
  399. ScopedPointer<MainMenuModel> menuModel;
  400. virtual MainWindow* createNewMainWindow()
  401. {
  402. MainWindow* mw = new MainWindow();
  403. mainWindows.add (mw);
  404. mw->restoreWindowPosition();
  405. avoidSuperimposedWindows (mw);
  406. return mw;
  407. }
  408. private:
  409. OwnedArray <MainWindow> mainWindows;
  410. MainWindow* getOrCreateFrontmostWindow()
  411. {
  412. if (mainWindows.size() == 0)
  413. return createNewMainWindow();
  414. for (int i = Desktop::getInstance().getNumComponents(); --i >= 0;)
  415. {
  416. MainWindow* mw = dynamic_cast <MainWindow*> (Desktop::getInstance().getComponent (i));
  417. if (mainWindows.contains (mw))
  418. return mw;
  419. }
  420. return mainWindows.getLast();
  421. }
  422. MainWindow* getOrCreateEmptyWindow()
  423. {
  424. if (mainWindows.size() == 0)
  425. return createNewMainWindow();
  426. for (int i = Desktop::getInstance().getNumComponents(); --i >= 0;)
  427. {
  428. MainWindow* mw = dynamic_cast <MainWindow*> (Desktop::getInstance().getComponent (i));
  429. if (mainWindows.contains (mw) && mw->getProject() == nullptr)
  430. return mw;
  431. }
  432. return createNewMainWindow();
  433. }
  434. void avoidSuperimposedWindows (MainWindow* const mw)
  435. {
  436. for (int i = mainWindows.size(); --i >= 0;)
  437. {
  438. MainWindow* const other = mainWindows.getUnchecked(i);
  439. const Rectangle<int> b1 (mw->getBounds());
  440. const Rectangle<int> b2 (other->getBounds());
  441. if (mw != other
  442. && std::abs (b1.getX() - b2.getX()) < 3
  443. && std::abs (b1.getY() - b2.getY()) < 3
  444. && std::abs (b1.getRight() - b2.getRight()) < 3
  445. && std::abs (b1.getBottom() - b2.getBottom()) < 3)
  446. {
  447. int dx = 40, dy = 30;
  448. if (b1.getCentreX() >= mw->getScreenBounds().getCentreX()) dx = -dx;
  449. if (b1.getCentreY() >= mw->getScreenBounds().getCentreY()) dy = -dy;
  450. mw->setBounds (b1.translated (dx, dy));
  451. }
  452. }
  453. }
  454. //==============================================================================
  455. class AsyncQuitRetrier : public Timer
  456. {
  457. public:
  458. AsyncQuitRetrier() { startTimer (500); }
  459. void timerCallback()
  460. {
  461. stopTimer();
  462. delete this;
  463. if (getApp() != nullptr)
  464. getApp()->systemRequestedQuit();
  465. }
  466. JUCE_DECLARE_NON_COPYABLE (AsyncQuitRetrier);
  467. };
  468. };
  469. #endif // __JUCER_APPLICATION_JUCEHEADER__