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.

578 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. const StringArray getMenuBarNames()
  138. {
  139. const char* const names[] = { "File", "Edit", "View", "Window", "Tools", 0 };
  140. return StringArray ((const char**) names);
  141. }
  142. const 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.addCommandItem (commandManager, CommandIDs::saveDocumentAs);
  157. menu.addSeparator();
  158. menu.addCommandItem (commandManager, CommandIDs::closeProject);
  159. menu.addCommandItem (commandManager, CommandIDs::saveProject);
  160. menu.addCommandItem (commandManager, CommandIDs::saveProjectAs);
  161. menu.addSeparator();
  162. menu.addCommandItem (commandManager, CommandIDs::openInIDE);
  163. menu.addCommandItem (commandManager, CommandIDs::saveAndOpenInIDE);
  164. #if ! JUCE_MAC
  165. menu.addSeparator();
  166. menu.addCommandItem (commandManager, StandardApplicationCommandIDs::quit);
  167. #endif
  168. }
  169. else if (topLevelMenuIndex == 1) // "Edit" menu
  170. {
  171. menu.addCommandItem (commandManager, CommandIDs::undo);
  172. menu.addCommandItem (commandManager, CommandIDs::redo);
  173. menu.addSeparator();
  174. menu.addCommandItem (commandManager, StandardApplicationCommandIDs::cut);
  175. menu.addCommandItem (commandManager, StandardApplicationCommandIDs::copy);
  176. menu.addCommandItem (commandManager, StandardApplicationCommandIDs::paste);
  177. menu.addCommandItem (commandManager, StandardApplicationCommandIDs::del);
  178. menu.addCommandItem (commandManager, StandardApplicationCommandIDs::selectAll);
  179. menu.addCommandItem (commandManager, StandardApplicationCommandIDs::deselectAll);
  180. menu.addSeparator();
  181. menu.addCommandItem (commandManager, CommandIDs::toFront);
  182. menu.addCommandItem (commandManager, CommandIDs::toBack);
  183. menu.addSeparator();
  184. menu.addCommandItem (commandManager, CommandIDs::group);
  185. menu.addCommandItem (commandManager, CommandIDs::ungroup);
  186. menu.addSeparator();
  187. menu.addCommandItem (commandManager, CommandIDs::bringBackLostItems);
  188. }
  189. else if (topLevelMenuIndex == 2) // "View" menu
  190. {
  191. menu.addCommandItem (commandManager, CommandIDs::showProjectSettings);
  192. menu.addSeparator();
  193. menu.addCommandItem (commandManager, CommandIDs::test);
  194. menu.addSeparator();
  195. menu.addCommandItem (commandManager, CommandIDs::showGrid);
  196. menu.addCommandItem (commandManager, CommandIDs::enableSnapToGrid);
  197. menu.addSeparator();
  198. menu.addCommandItem (commandManager, CommandIDs::zoomIn);
  199. menu.addCommandItem (commandManager, CommandIDs::zoomOut);
  200. menu.addCommandItem (commandManager, CommandIDs::zoomNormal);
  201. menu.addSeparator();
  202. menu.addCommandItem (commandManager, CommandIDs::useTabbedWindows);
  203. }
  204. else if (topLevelMenuIndex == 3) // "Window" menu
  205. {
  206. menu.addCommandItem (commandManager, CommandIDs::closeWindow);
  207. menu.addSeparator();
  208. const int numDocs = jmin (50, OpenDocumentManager::getInstance()->getNumOpenDocuments());
  209. for (int i = 0; i < numDocs; ++i)
  210. {
  211. OpenDocumentManager::Document* doc = OpenDocumentManager::getInstance()->getOpenDocument(i);
  212. menu.addItem (300 + i, doc->getName());
  213. }
  214. menu.addSeparator();
  215. menu.addCommandItem (commandManager, CommandIDs::closeAllDocuments);
  216. }
  217. else if (topLevelMenuIndex == 4) // "Tools" menu
  218. {
  219. menu.addCommandItem (commandManager, CommandIDs::updateModules);
  220. menu.addCommandItem (commandManager, CommandIDs::showUTF8Tool);
  221. }
  222. return menu;
  223. }
  224. void menuItemSelected (int menuItemID, int /*topLevelMenuIndex*/)
  225. {
  226. if (menuItemID >= 100 && menuItemID < 200)
  227. {
  228. // open a file from the "recent files" menu
  229. const File file (StoredSettings::getInstance()->recentFiles.getFile (menuItemID - 100));
  230. getApp()->openFile (file);
  231. }
  232. else if (menuItemID >= 300 && menuItemID < 400)
  233. {
  234. OpenDocumentManager::Document* doc = OpenDocumentManager::getInstance()->getOpenDocument (menuItemID - 300);
  235. MainWindow* w = getApp()->getOrCreateFrontmostWindow();
  236. w->makeVisible();
  237. w->getProjectContentComponent()->showDocument (doc);
  238. getApp()->avoidSuperimposedWindows (w);
  239. }
  240. }
  241. };
  242. //==============================================================================
  243. void getAllCommands (Array <CommandID>& commands)
  244. {
  245. JUCEApplication::getAllCommands (commands);
  246. const CommandID ids[] = { CommandIDs::newProject,
  247. CommandIDs::open,
  248. CommandIDs::showPrefs,
  249. CommandIDs::closeAllDocuments,
  250. CommandIDs::saveAll,
  251. CommandIDs::updateModules,
  252. CommandIDs::showUTF8Tool };
  253. commands.addArray (ids, numElementsInArray (ids));
  254. }
  255. void getCommandInfo (CommandID commandID, ApplicationCommandInfo& result)
  256. {
  257. switch (commandID)
  258. {
  259. case CommandIDs::newProject:
  260. result.setInfo ("New Project...", "Creates a new Jucer project", CommandCategories::general, 0);
  261. result.defaultKeypresses.add (KeyPress ('n', ModifierKeys::commandModifier, 0));
  262. break;
  263. case CommandIDs::open:
  264. result.setInfo ("Open...", "Opens a Jucer project", CommandCategories::general, 0);
  265. result.defaultKeypresses.add (KeyPress ('o', ModifierKeys::commandModifier, 0));
  266. break;
  267. case CommandIDs::showPrefs:
  268. result.setInfo ("Preferences...", "Shows the preferences panel.", CommandCategories::general, 0);
  269. result.defaultKeypresses.add (KeyPress (',', ModifierKeys::commandModifier, 0));
  270. break;
  271. case CommandIDs::closeAllDocuments:
  272. result.setInfo ("Close All Documents", "Closes all open documents", CommandCategories::general, 0);
  273. result.setActive (OpenDocumentManager::getInstance()->getNumOpenDocuments() > 0);
  274. break;
  275. case CommandIDs::saveAll:
  276. result.setInfo ("Save All", "Saves all open documents", CommandCategories::general, 0);
  277. result.setActive (OpenDocumentManager::getInstance()->anyFilesNeedSaving());
  278. break;
  279. case CommandIDs::updateModules:
  280. result.setInfo ("Download the latest JUCE modules", "Checks online for any JUCE modules updates and installs them", CommandCategories::general, 0);
  281. break;
  282. case CommandIDs::showUTF8Tool:
  283. result.setInfo ("UTF-8 String-Literal Helper", "Shows the UTF-8 string literal utility", CommandCategories::general, 0);
  284. break;
  285. default:
  286. JUCEApplication::getCommandInfo (commandID, result);
  287. break;
  288. }
  289. }
  290. bool perform (const InvocationInfo& info)
  291. {
  292. switch (info.commandID)
  293. {
  294. case CommandIDs::newProject: createNewProject(); break;
  295. case CommandIDs::open: askUserToOpenFile(); break;
  296. case CommandIDs::showPrefs: showPrefsPanel(); break;
  297. case CommandIDs::saveAll: OpenDocumentManager::getInstance()->saveAll(); break;
  298. case CommandIDs::closeAllDocuments: closeAllDocuments (true); break;
  299. case CommandIDs::showUTF8Tool: showUTF8ToolWindow(); break;
  300. case CommandIDs::updateModules: runModuleUpdate (String::empty); break;
  301. default: return JUCEApplication::perform (info);
  302. }
  303. return true;
  304. }
  305. //==============================================================================
  306. void showPrefsPanel()
  307. {
  308. jassertfalse;
  309. }
  310. void createNewProject()
  311. {
  312. if (makeSureUserHasSelectedModuleFolder())
  313. {
  314. MainWindow* mw = getOrCreateEmptyWindow();
  315. mw->showNewProjectWizard();
  316. avoidSuperimposedWindows (mw);
  317. }
  318. }
  319. void askUserToOpenFile()
  320. {
  321. FileChooser fc ("Open File");
  322. if (fc.browseForFileToOpen())
  323. openFile (fc.getResult());
  324. }
  325. bool openFile (const File& file)
  326. {
  327. for (int j = mainWindows.size(); --j >= 0;)
  328. {
  329. if (mainWindows.getUnchecked(j)->getProject() != nullptr
  330. && mainWindows.getUnchecked(j)->getProject()->getFile() == file)
  331. {
  332. mainWindows.getUnchecked(j)->toFront (true);
  333. return true;
  334. }
  335. }
  336. if (file.hasFileExtension (Project::projectFileExtension))
  337. {
  338. ScopedPointer <Project> newDoc (new Project (file));
  339. if (newDoc->loadFrom (file, true))
  340. {
  341. MainWindow* w = getOrCreateEmptyWindow();
  342. w->setProject (newDoc.release());
  343. w->makeVisible();
  344. avoidSuperimposedWindows (w);
  345. return true;
  346. }
  347. }
  348. else if (file.exists())
  349. {
  350. MainWindow* w = getOrCreateFrontmostWindow();
  351. const bool ok = w->openFile (file);
  352. w->makeVisible();
  353. avoidSuperimposedWindows (w);
  354. return ok;
  355. }
  356. return false;
  357. }
  358. bool closeAllDocuments (bool askUserToSave)
  359. {
  360. return OpenDocumentManager::getInstance()->closeAll (askUserToSave);
  361. }
  362. void updateRecentProjectList()
  363. {
  364. Array<File> projects;
  365. for (int i = 0; i < mainWindows.size(); ++i)
  366. {
  367. MainWindow* mw = mainWindows[i];
  368. if (mw != nullptr && mw->getProject() != nullptr)
  369. projects.add (mw->getProject()->getFile());
  370. }
  371. StoredSettings::getInstance()->setLastProjects (projects);
  372. }
  373. bool makeSureUserHasSelectedModuleFolder()
  374. {
  375. if (! ModuleList::isLocalModulesFolderValid())
  376. {
  377. if (! runModuleUpdate ("Please select a location to store your local set of JUCE modules,\n"
  378. "and download the ones that you'd like to use!"))
  379. {
  380. AlertWindow::showMessageBox (AlertWindow::WarningIcon,
  381. "Introjucer",
  382. "Unless you create a local JUCE folder containing some modules, you'll be unable to save any projects correctly!\n\n"
  383. "Use the option on the 'Tools' menu to set this up!");
  384. return false;
  385. }
  386. }
  387. return true;
  388. }
  389. bool runModuleUpdate (const String& message)
  390. {
  391. ModuleList list;
  392. list.rescan (ModuleList::getDefaultModulesFolder (nullptr));
  393. JuceUpdater::show (list, mainWindows[0], message);
  394. ModuleList::setLocalModulesFolder (list.getModulesFolder());
  395. return ModuleList::isJuceOrModulesFolder (list.getModulesFolder());
  396. }
  397. ScopedPointer<MainMenuModel> menuModel;
  398. private:
  399. OwnedArray <MainWindow> mainWindows;
  400. MainWindow* createNewMainWindow()
  401. {
  402. MainWindow* mw = new MainWindow();
  403. mainWindows.add (mw);
  404. mw->restoreWindowPosition();
  405. avoidSuperimposedWindows (mw);
  406. return mw;
  407. }
  408. MainWindow* getOrCreateFrontmostWindow()
  409. {
  410. if (mainWindows.size() == 0)
  411. return createNewMainWindow();
  412. for (int i = Desktop::getInstance().getNumComponents(); --i >= 0;)
  413. {
  414. MainWindow* mw = dynamic_cast <MainWindow*> (Desktop::getInstance().getComponent (i));
  415. if (mainWindows.contains (mw))
  416. return mw;
  417. }
  418. return mainWindows.getLast();
  419. }
  420. MainWindow* getOrCreateEmptyWindow()
  421. {
  422. if (mainWindows.size() == 0)
  423. return createNewMainWindow();
  424. for (int i = Desktop::getInstance().getNumComponents(); --i >= 0;)
  425. {
  426. MainWindow* mw = dynamic_cast <MainWindow*> (Desktop::getInstance().getComponent (i));
  427. if (mainWindows.contains (mw) && mw->getProject() == nullptr)
  428. return mw;
  429. }
  430. return createNewMainWindow();
  431. }
  432. void avoidSuperimposedWindows (MainWindow* const mw)
  433. {
  434. for (int i = mainWindows.size(); --i >= 0;)
  435. {
  436. MainWindow* const other = mainWindows.getUnchecked(i);
  437. const Rectangle<int> b1 (mw->getBounds());
  438. const Rectangle<int> b2 (other->getBounds());
  439. if (mw != other
  440. && std::abs (b1.getX() - b2.getX()) < 3
  441. && std::abs (b1.getY() - b2.getY()) < 3
  442. && std::abs (b1.getRight() - b2.getRight()) < 3
  443. && std::abs (b1.getBottom() - b2.getBottom()) < 3)
  444. {
  445. int dx = 40, dy = 30;
  446. if (b1.getCentreX() >= mw->getScreenBounds().getCentreX()) dx = -dx;
  447. if (b1.getCentreY() >= mw->getScreenBounds().getCentreY()) dy = -dy;
  448. mw->setBounds (b1.translated (dx, dy));
  449. }
  450. }
  451. }
  452. //==============================================================================
  453. class AsyncQuitRetrier : public Timer
  454. {
  455. public:
  456. AsyncQuitRetrier() { startTimer (500); }
  457. void timerCallback()
  458. {
  459. stopTimer();
  460. delete this;
  461. if (getApp() != nullptr)
  462. getApp()->systemRequestedQuit();
  463. }
  464. JUCE_DECLARE_NON_COPYABLE (AsyncQuitRetrier);
  465. };
  466. };
  467. #endif // __JUCER_APPLICATION_JUCEHEADER__