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.

581 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. static JucerApplication* getApp()
  126. {
  127. return dynamic_cast<JucerApplication*> (JUCEApplication::getInstance());
  128. }
  129. //==============================================================================
  130. class MainMenuModel : public MenuBarModel
  131. {
  132. public:
  133. MainMenuModel()
  134. {
  135. setApplicationCommandManagerToWatch (commandManager);
  136. }
  137. StringArray getMenuBarNames()
  138. {
  139. const char* const names[] = { "File", "Edit", "View", "Window", "Tools", 0 };
  140. return StringArray ((const char**) names);
  141. }
  142. PopupMenu getMenuForIndex (int topLevelMenuIndex, const String& /*menuName*/)
  143. {
  144. PopupMenu menu;
  145. if (topLevelMenuIndex == 0) // "File" menu
  146. {
  147. menu.addCommandItem (commandManager, CommandIDs::newProject);
  148. menu.addSeparator();
  149. menu.addCommandItem (commandManager, CommandIDs::open);
  150. PopupMenu recentFiles;
  151. StoredSettings::getInstance()->recentFiles.createPopupMenuItems (recentFiles, 100, true, true);
  152. menu.addSubMenu ("Open recent file", recentFiles);
  153. menu.addSeparator();
  154. menu.addCommandItem (commandManager, CommandIDs::closeDocument);
  155. menu.addCommandItem (commandManager, CommandIDs::saveDocument);
  156. menu.addSeparator();
  157. menu.addCommandItem (commandManager, CommandIDs::closeProject);
  158. menu.addCommandItem (commandManager, CommandIDs::saveProject);
  159. menu.addSeparator();
  160. menu.addCommandItem (commandManager, CommandIDs::openInIDE);
  161. menu.addCommandItem (commandManager, CommandIDs::saveAndOpenInIDE);
  162. #if ! JUCE_MAC
  163. menu.addSeparator();
  164. menu.addCommandItem (commandManager, StandardApplicationCommandIDs::quit);
  165. #endif
  166. }
  167. else if (topLevelMenuIndex == 1) // "Edit" menu
  168. {
  169. menu.addCommandItem (commandManager, CommandIDs::undo);
  170. menu.addCommandItem (commandManager, CommandIDs::redo);
  171. menu.addSeparator();
  172. menu.addCommandItem (commandManager, StandardApplicationCommandIDs::cut);
  173. menu.addCommandItem (commandManager, StandardApplicationCommandIDs::copy);
  174. menu.addCommandItem (commandManager, StandardApplicationCommandIDs::paste);
  175. menu.addCommandItem (commandManager, StandardApplicationCommandIDs::del);
  176. menu.addCommandItem (commandManager, StandardApplicationCommandIDs::selectAll);
  177. menu.addCommandItem (commandManager, StandardApplicationCommandIDs::deselectAll);
  178. menu.addSeparator();
  179. menu.addCommandItem (commandManager, CommandIDs::toFront);
  180. menu.addCommandItem (commandManager, CommandIDs::toBack);
  181. menu.addSeparator();
  182. menu.addCommandItem (commandManager, CommandIDs::group);
  183. menu.addCommandItem (commandManager, CommandIDs::ungroup);
  184. menu.addSeparator();
  185. menu.addCommandItem (commandManager, CommandIDs::bringBackLostItems);
  186. }
  187. else if (topLevelMenuIndex == 2) // "View" menu
  188. {
  189. menu.addCommandItem (commandManager, CommandIDs::showProjectSettings);
  190. menu.addSeparator();
  191. menu.addCommandItem (commandManager, CommandIDs::test);
  192. menu.addSeparator();
  193. menu.addCommandItem (commandManager, CommandIDs::showGrid);
  194. menu.addCommandItem (commandManager, CommandIDs::enableSnapToGrid);
  195. menu.addSeparator();
  196. menu.addCommandItem (commandManager, CommandIDs::zoomIn);
  197. menu.addCommandItem (commandManager, CommandIDs::zoomOut);
  198. menu.addCommandItem (commandManager, CommandIDs::zoomNormal);
  199. menu.addSeparator();
  200. menu.addCommandItem (commandManager, CommandIDs::useTabbedWindows);
  201. }
  202. else if (topLevelMenuIndex == 3) // "Window" menu
  203. {
  204. menu.addCommandItem (commandManager, CommandIDs::closeWindow);
  205. menu.addSeparator();
  206. const int numDocs = jmin (50, OpenDocumentManager::getInstance()->getNumOpenDocuments());
  207. for (int i = 0; i < numDocs; ++i)
  208. {
  209. OpenDocumentManager::Document* doc = OpenDocumentManager::getInstance()->getOpenDocument(i);
  210. menu.addItem (300 + i, doc->getName());
  211. }
  212. menu.addSeparator();
  213. menu.addCommandItem (commandManager, CommandIDs::closeAllDocuments);
  214. }
  215. else if (topLevelMenuIndex == 4) // "Tools" menu
  216. {
  217. menu.addCommandItem (commandManager, CommandIDs::updateModules);
  218. menu.addCommandItem (commandManager, CommandIDs::showUTF8Tool);
  219. }
  220. return menu;
  221. }
  222. void menuItemSelected (int menuItemID, int /*topLevelMenuIndex*/)
  223. {
  224. if (menuItemID >= 100 && menuItemID < 200)
  225. {
  226. // open a file from the "recent files" menu
  227. const File file (StoredSettings::getInstance()->recentFiles.getFile (menuItemID - 100));
  228. getApp()->openFile (file);
  229. }
  230. else if (menuItemID >= 300 && menuItemID < 400)
  231. {
  232. OpenDocumentManager::Document* doc = OpenDocumentManager::getInstance()->getOpenDocument (menuItemID - 300);
  233. MainWindow* w = getApp()->getOrCreateFrontmostWindow();
  234. w->makeVisible();
  235. w->getProjectContentComponent()->showDocument (doc);
  236. getApp()->avoidSuperimposedWindows (w);
  237. }
  238. }
  239. };
  240. //==============================================================================
  241. void getAllCommands (Array <CommandID>& commands)
  242. {
  243. JUCEApplication::getAllCommands (commands);
  244. const CommandID ids[] = { CommandIDs::newProject,
  245. CommandIDs::open,
  246. CommandIDs::showPrefs,
  247. CommandIDs::closeAllDocuments,
  248. CommandIDs::saveAll,
  249. CommandIDs::updateModules,
  250. CommandIDs::showUTF8Tool };
  251. commands.addArray (ids, numElementsInArray (ids));
  252. }
  253. void getCommandInfo (CommandID commandID, ApplicationCommandInfo& result)
  254. {
  255. switch (commandID)
  256. {
  257. case CommandIDs::newProject:
  258. result.setInfo ("New Project...", "Creates a new Jucer project", CommandCategories::general, 0);
  259. result.defaultKeypresses.add (KeyPress ('n', ModifierKeys::commandModifier, 0));
  260. break;
  261. case CommandIDs::open:
  262. result.setInfo ("Open...", "Opens a Jucer project", CommandCategories::general, 0);
  263. result.defaultKeypresses.add (KeyPress ('o', ModifierKeys::commandModifier, 0));
  264. break;
  265. case CommandIDs::showPrefs:
  266. result.setInfo ("Preferences...", "Shows the preferences panel.", CommandCategories::general, 0);
  267. result.defaultKeypresses.add (KeyPress (',', ModifierKeys::commandModifier, 0));
  268. break;
  269. case CommandIDs::closeAllDocuments:
  270. result.setInfo ("Close All Documents", "Closes all open documents", CommandCategories::general, 0);
  271. result.setActive (OpenDocumentManager::getInstance()->getNumOpenDocuments() > 0);
  272. break;
  273. case CommandIDs::saveAll:
  274. result.setInfo ("Save All", "Saves all open documents", CommandCategories::general, 0);
  275. result.setActive (OpenDocumentManager::getInstance()->anyFilesNeedSaving());
  276. break;
  277. case CommandIDs::updateModules:
  278. result.setInfo ("Download the latest JUCE modules", "Checks online for any JUCE modules updates and installs them", CommandCategories::general, 0);
  279. break;
  280. case CommandIDs::showUTF8Tool:
  281. result.setInfo ("UTF-8 String-Literal Helper", "Shows the UTF-8 string literal utility", CommandCategories::general, 0);
  282. break;
  283. default:
  284. JUCEApplication::getCommandInfo (commandID, result);
  285. break;
  286. }
  287. }
  288. bool perform (const InvocationInfo& info)
  289. {
  290. switch (info.commandID)
  291. {
  292. case CommandIDs::newProject: createNewProject(); break;
  293. case CommandIDs::open: askUserToOpenFile(); break;
  294. case CommandIDs::showPrefs: showPrefsPanel(); break;
  295. case CommandIDs::saveAll: OpenDocumentManager::getInstance()->saveAll(); break;
  296. case CommandIDs::closeAllDocuments: closeAllDocuments (true); break;
  297. case CommandIDs::showUTF8Tool: showUTF8ToolWindow(); break;
  298. case CommandIDs::updateModules: runModuleUpdate (String::empty); break;
  299. default: return JUCEApplication::perform (info);
  300. }
  301. return true;
  302. }
  303. //==============================================================================
  304. void showPrefsPanel()
  305. {
  306. jassertfalse;
  307. }
  308. void createNewProject()
  309. {
  310. if (makeSureUserHasSelectedModuleFolder())
  311. {
  312. MainWindow* mw = getOrCreateEmptyWindow();
  313. mw->showNewProjectWizard();
  314. avoidSuperimposedWindows (mw);
  315. }
  316. }
  317. void askUserToOpenFile()
  318. {
  319. FileChooser fc ("Open File");
  320. if (fc.browseForFileToOpen())
  321. openFile (fc.getResult());
  322. }
  323. bool openFile (const File& file)
  324. {
  325. for (int j = mainWindows.size(); --j >= 0;)
  326. {
  327. if (mainWindows.getUnchecked(j)->getProject() != nullptr
  328. && mainWindows.getUnchecked(j)->getProject()->getFile() == file)
  329. {
  330. mainWindows.getUnchecked(j)->toFront (true);
  331. return true;
  332. }
  333. }
  334. if (file.hasFileExtension (Project::projectFileExtension))
  335. {
  336. ScopedPointer <Project> newDoc (new Project (file));
  337. if (newDoc->loadFrom (file, true))
  338. {
  339. MainWindow* w = getOrCreateEmptyWindow();
  340. w->setProject (newDoc.release());
  341. w->makeVisible();
  342. avoidSuperimposedWindows (w);
  343. return true;
  344. }
  345. }
  346. else if (file.exists())
  347. {
  348. MainWindow* w = getOrCreateFrontmostWindow();
  349. const bool ok = w->openFile (file);
  350. w->makeVisible();
  351. avoidSuperimposedWindows (w);
  352. return ok;
  353. }
  354. return false;
  355. }
  356. bool closeAllDocuments (bool askUserToSave)
  357. {
  358. return OpenDocumentManager::getInstance()->closeAll (askUserToSave);
  359. }
  360. void updateRecentProjectList()
  361. {
  362. Array<File> projects;
  363. for (int i = 0; i < mainWindows.size(); ++i)
  364. {
  365. MainWindow* mw = mainWindows[i];
  366. if (mw != nullptr && mw->getProject() != nullptr)
  367. projects.add (mw->getProject()->getFile());
  368. }
  369. StoredSettings::getInstance()->setLastProjects (projects);
  370. }
  371. bool makeSureUserHasSelectedModuleFolder()
  372. {
  373. if (! ModuleList::isLocalModulesFolderValid())
  374. {
  375. if (! runModuleUpdate ("Please select a location to store your local set of JUCE modules,\n"
  376. "and download the ones that you'd like to use!"))
  377. {
  378. AlertWindow::showMessageBox (AlertWindow::WarningIcon,
  379. "Introjucer",
  380. "Unless you create a local JUCE folder containing some modules, you'll be unable to save any projects correctly!\n\n"
  381. "Use the option on the 'Tools' menu to set this up!");
  382. return false;
  383. }
  384. }
  385. return true;
  386. }
  387. bool runModuleUpdate (const String& message)
  388. {
  389. ModuleList list;
  390. list.rescan (ModuleList::getDefaultModulesFolder (nullptr));
  391. JuceUpdater::show (list, mainWindows[0], message);
  392. ModuleList::setLocalModulesFolder (list.getModulesFolder());
  393. return ModuleList::isJuceOrModulesFolder (list.getModulesFolder());
  394. }
  395. ScopedPointer<MainMenuModel> menuModel;
  396. MainWindow* createNewMainWindow()
  397. {
  398. MainWindow* mw = new MainWindow();
  399. mainWindows.add (mw);
  400. mw->restoreWindowPosition();
  401. avoidSuperimposedWindows (mw);
  402. return mw;
  403. }
  404. virtual Component* createProjectContentComponent() const
  405. {
  406. return new ProjectContentComponent();
  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__