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.

517 lines
18KB

  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. #include "../Code Editor/jucer_SourceCodeEditor.h"
  25. //==============================================================================
  26. class JucerApplication : public JUCEApplication
  27. {
  28. public:
  29. //==============================================================================
  30. JucerApplication() {}
  31. ~JucerApplication() {}
  32. //==============================================================================
  33. void initialise (const String& commandLine)
  34. {
  35. LookAndFeel::setDefaultLookAndFeel (&lookAndFeel);
  36. settings = new StoredSettings();
  37. settings->initialise();
  38. if (commandLine.isNotEmpty())
  39. {
  40. const int appReturnCode = performCommandLine (commandLine);
  41. if (appReturnCode != commandLineNotPerformed)
  42. {
  43. setApplicationReturnValue (appReturnCode);
  44. quit();
  45. return;
  46. }
  47. }
  48. if (sendCommandLineToPreexistingInstance())
  49. {
  50. DBG ("Another instance is running - quitting...");
  51. quit();
  52. return;
  53. }
  54. icons = new Icons();
  55. commandManager = new ApplicationCommandManager();
  56. commandManager->registerAllCommandsForTarget (this);
  57. menuModel = new MainMenuModel();
  58. doExtraInitialisation();
  59. settings->appearance.refreshPresetSchemeList();
  60. ImageCache::setCacheTimeout (30 * 1000);
  61. if (commandLine.trim().isNotEmpty() && ! commandLine.trim().startsWithChar ('-'))
  62. anotherInstanceStarted (commandLine);
  63. else
  64. mainWindowList.reopenLastProjects();
  65. makeSureUserHasSelectedModuleFolder();
  66. mainWindowList.createWindowIfNoneAreOpen();
  67. #if JUCE_MAC
  68. MenuBarModel::setMacMainMenu (menuModel);
  69. #endif
  70. }
  71. void shutdown()
  72. {
  73. appearanceEditorWindow = nullptr;
  74. #if JUCE_MAC
  75. MenuBarModel::setMacMainMenu (nullptr);
  76. #endif
  77. menuModel = nullptr;
  78. mainWindowList.forceCloseAllWindows();
  79. openDocumentManager.clear();
  80. commandManager = nullptr;
  81. settings = nullptr;
  82. LookAndFeel::setDefaultLookAndFeel (nullptr);
  83. }
  84. //==============================================================================
  85. void systemRequestedQuit()
  86. {
  87. if (cancelAnyModalComponents())
  88. {
  89. new AsyncQuitRetrier();
  90. return;
  91. }
  92. if (mainWindowList.askAllWindowsToClose())
  93. quit();
  94. }
  95. //==============================================================================
  96. const String getApplicationName()
  97. {
  98. return String (ProjectInfo::projectName) + " " + getApplicationVersion();
  99. }
  100. const String getApplicationVersion()
  101. {
  102. return ProjectInfo::versionString;
  103. }
  104. bool moreThanOneInstanceAllowed()
  105. {
  106. return true; // this is handled manually in initialise()
  107. }
  108. void anotherInstanceStarted (const String& commandLine)
  109. {
  110. openFile (commandLine.unquoted());
  111. }
  112. static JucerApplication& getApp()
  113. {
  114. JucerApplication* const app = dynamic_cast<JucerApplication*> (JUCEApplication::getInstance());
  115. jassert (app != nullptr);
  116. return *app;
  117. }
  118. //==============================================================================
  119. class MainMenuModel : public MenuBarModel
  120. {
  121. public:
  122. MainMenuModel()
  123. {
  124. setApplicationCommandManagerToWatch (commandManager);
  125. }
  126. StringArray getMenuBarNames()
  127. {
  128. return getApp().getMenuNames();
  129. }
  130. PopupMenu getMenuForIndex (int /*topLevelMenuIndex*/, const String& menuName)
  131. {
  132. PopupMenu menu;
  133. getApp().createMenu (menu, menuName);
  134. return menu;
  135. }
  136. void menuItemSelected (int menuItemID, int /*topLevelMenuIndex*/)
  137. {
  138. if (menuItemID >= recentProjectsBaseID && menuItemID < recentProjectsBaseID + 100)
  139. {
  140. // open a file from the "recent files" menu
  141. getApp().openFile (getAppSettings().recentFiles.getFile (menuItemID - recentProjectsBaseID));
  142. }
  143. else if (menuItemID >= activeDocumentsBaseID && menuItemID < activeDocumentsBaseID + 200)
  144. {
  145. OpenDocumentManager::Document* doc = getApp().openDocumentManager.getOpenDocument (menuItemID - activeDocumentsBaseID);
  146. jassert (doc != nullptr);
  147. getApp().mainWindowList.openDocument (doc, true);
  148. }
  149. else if (menuItemID >= colourSchemeBaseID && menuItemID < colourSchemeBaseID + 200)
  150. {
  151. getAppSettings().appearance.selectPresetScheme (menuItemID - colourSchemeBaseID);
  152. }
  153. }
  154. };
  155. enum
  156. {
  157. recentProjectsBaseID = 100,
  158. activeDocumentsBaseID = 300,
  159. colourSchemeBaseID = 1000
  160. };
  161. virtual StringArray getMenuNames()
  162. {
  163. const char* const names[] = { "File", "Edit", "View", "Window", "Tools", nullptr };
  164. return StringArray (names);
  165. }
  166. virtual void createMenu (PopupMenu& menu, const String& menuName)
  167. {
  168. if (menuName == "File") createFileMenu (menu);
  169. else if (menuName == "Edit") createEditMenu (menu);
  170. else if (menuName == "View") createViewMenu (menu);
  171. else if (menuName == "Window") createWindowMenu (menu);
  172. else if (menuName == "Tools") createToolsMenu (menu);
  173. else jassertfalse; // names have changed?
  174. }
  175. virtual void createFileMenu (PopupMenu& menu)
  176. {
  177. menu.addCommandItem (commandManager, CommandIDs::newProject);
  178. menu.addSeparator();
  179. menu.addCommandItem (commandManager, CommandIDs::open);
  180. PopupMenu recentFiles;
  181. getAppSettings().recentFiles.createPopupMenuItems (recentFiles, recentProjectsBaseID, true, true);
  182. menu.addSubMenu ("Open recent file", recentFiles);
  183. menu.addSeparator();
  184. menu.addCommandItem (commandManager, CommandIDs::closeDocument);
  185. menu.addCommandItem (commandManager, CommandIDs::saveDocument);
  186. menu.addSeparator();
  187. menu.addCommandItem (commandManager, CommandIDs::closeProject);
  188. menu.addCommandItem (commandManager, CommandIDs::saveProject);
  189. menu.addSeparator();
  190. menu.addCommandItem (commandManager, CommandIDs::openInIDE);
  191. menu.addCommandItem (commandManager, CommandIDs::saveAndOpenInIDE);
  192. #if ! JUCE_MAC
  193. menu.addSeparator();
  194. menu.addCommandItem (commandManager, StandardApplicationCommandIDs::quit);
  195. #endif
  196. }
  197. virtual void createEditMenu (PopupMenu& menu)
  198. {
  199. menu.addCommandItem (commandManager, StandardApplicationCommandIDs::undo);
  200. menu.addCommandItem (commandManager, StandardApplicationCommandIDs::redo);
  201. menu.addSeparator();
  202. menu.addCommandItem (commandManager, StandardApplicationCommandIDs::cut);
  203. menu.addCommandItem (commandManager, StandardApplicationCommandIDs::copy);
  204. menu.addCommandItem (commandManager, StandardApplicationCommandIDs::paste);
  205. menu.addCommandItem (commandManager, StandardApplicationCommandIDs::del);
  206. menu.addCommandItem (commandManager, StandardApplicationCommandIDs::selectAll);
  207. menu.addCommandItem (commandManager, StandardApplicationCommandIDs::deselectAll);
  208. menu.addSeparator();
  209. menu.addCommandItem (commandManager, CommandIDs::toFront);
  210. menu.addCommandItem (commandManager, CommandIDs::toBack);
  211. menu.addSeparator();
  212. menu.addCommandItem (commandManager, CommandIDs::group);
  213. menu.addCommandItem (commandManager, CommandIDs::ungroup);
  214. menu.addSeparator();
  215. menu.addCommandItem (commandManager, CommandIDs::bringBackLostItems);
  216. }
  217. virtual void createViewMenu (PopupMenu& menu)
  218. {
  219. menu.addCommandItem (commandManager, CommandIDs::showFilePanel);
  220. menu.addCommandItem (commandManager, CommandIDs::showConfigPanel);
  221. menu.addSeparator();
  222. createColourSchemeItems (menu);
  223. }
  224. void createColourSchemeItems (PopupMenu& menu)
  225. {
  226. menu.addCommandItem (commandManager, CommandIDs::showAppearanceSettings);
  227. const StringArray presetSchemes (settings->appearance.getPresetSchemes());
  228. if (presetSchemes.size() > 0)
  229. {
  230. PopupMenu schemes;
  231. for (int i = 0; i < presetSchemes.size(); ++i)
  232. schemes.addItem (colourSchemeBaseID + i, presetSchemes[i]);
  233. menu.addSubMenu ("Colour Scheme", schemes);
  234. }
  235. }
  236. virtual void createWindowMenu (PopupMenu& menu)
  237. {
  238. menu.addCommandItem (commandManager, CommandIDs::closeWindow);
  239. menu.addSeparator();
  240. menu.addCommandItem (commandManager, CommandIDs::goToPreviousDoc);
  241. menu.addCommandItem (commandManager, CommandIDs::goToNextDoc);
  242. menu.addSeparator();
  243. const int numDocs = jmin (50, getApp().openDocumentManager.getNumOpenDocuments());
  244. for (int i = 0; i < numDocs; ++i)
  245. {
  246. OpenDocumentManager::Document* doc = getApp().openDocumentManager.getOpenDocument(i);
  247. menu.addItem (activeDocumentsBaseID + i, doc->getName());
  248. }
  249. menu.addSeparator();
  250. menu.addCommandItem (commandManager, CommandIDs::closeAllDocuments);
  251. }
  252. virtual void createToolsMenu (PopupMenu& menu)
  253. {
  254. menu.addCommandItem (commandManager, CommandIDs::updateModules);
  255. menu.addCommandItem (commandManager, CommandIDs::showUTF8Tool);
  256. }
  257. //==============================================================================
  258. void getAllCommands (Array <CommandID>& commands)
  259. {
  260. JUCEApplication::getAllCommands (commands);
  261. const CommandID ids[] = { CommandIDs::newProject,
  262. CommandIDs::open,
  263. CommandIDs::showPrefs,
  264. CommandIDs::closeAllDocuments,
  265. CommandIDs::saveAll,
  266. CommandIDs::updateModules,
  267. CommandIDs::showAppearanceSettings,
  268. CommandIDs::showUTF8Tool };
  269. commands.addArray (ids, numElementsInArray (ids));
  270. }
  271. void getCommandInfo (CommandID commandID, ApplicationCommandInfo& result)
  272. {
  273. switch (commandID)
  274. {
  275. case CommandIDs::newProject:
  276. result.setInfo ("New Project...", "Creates a new Jucer project", CommandCategories::general, 0);
  277. result.defaultKeypresses.add (KeyPress ('n', ModifierKeys::commandModifier, 0));
  278. break;
  279. case CommandIDs::open:
  280. result.setInfo ("Open...", "Opens a Jucer project", CommandCategories::general, 0);
  281. result.defaultKeypresses.add (KeyPress ('o', ModifierKeys::commandModifier, 0));
  282. break;
  283. case CommandIDs::showPrefs:
  284. result.setInfo ("Preferences...", "Shows the preferences panel.", CommandCategories::general, 0);
  285. result.defaultKeypresses.add (KeyPress (',', ModifierKeys::commandModifier, 0));
  286. break;
  287. case CommandIDs::showAppearanceSettings:
  288. result.setInfo ("Fonts and Colours...", "Shows the appearance settings window.", CommandCategories::general, 0);
  289. break;
  290. case CommandIDs::closeAllDocuments:
  291. result.setInfo ("Close All Documents", "Closes all open documents", CommandCategories::general, 0);
  292. result.setActive (openDocumentManager.getNumOpenDocuments() > 0);
  293. break;
  294. case CommandIDs::saveAll:
  295. result.setInfo ("Save All", "Saves all open documents", CommandCategories::general, 0);
  296. result.setActive (openDocumentManager.anyFilesNeedSaving());
  297. break;
  298. case CommandIDs::updateModules:
  299. result.setInfo ("Download the latest JUCE modules", "Checks online for any JUCE modules updates and installs them", CommandCategories::general, 0);
  300. break;
  301. case CommandIDs::showUTF8Tool:
  302. result.setInfo ("UTF-8 String-Literal Helper", "Shows the UTF-8 string literal utility", CommandCategories::general, 0);
  303. break;
  304. default:
  305. JUCEApplication::getCommandInfo (commandID, result);
  306. break;
  307. }
  308. }
  309. bool perform (const InvocationInfo& info)
  310. {
  311. switch (info.commandID)
  312. {
  313. case CommandIDs::newProject: createNewProject(); break;
  314. case CommandIDs::open: askUserToOpenFile(); break;
  315. case CommandIDs::showPrefs: showPrefsPanel(); break;
  316. case CommandIDs::saveAll: openDocumentManager.saveAll(); break;
  317. case CommandIDs::closeAllDocuments: closeAllDocuments (true); break;
  318. case CommandIDs::showUTF8Tool: showUTF8ToolWindow(); break;
  319. case CommandIDs::showAppearanceSettings: showAppearanceEditorWindow(); break;
  320. case CommandIDs::updateModules: runModuleUpdate (String::empty); break;
  321. default: return JUCEApplication::perform (info);
  322. }
  323. return true;
  324. }
  325. //==============================================================================
  326. void showPrefsPanel()
  327. {
  328. jassertfalse;
  329. }
  330. void createNewProject()
  331. {
  332. if (makeSureUserHasSelectedModuleFolder())
  333. {
  334. MainWindow* mw = mainWindowList.getOrCreateEmptyWindow();
  335. mw->showNewProjectWizard();
  336. mainWindowList.avoidSuperimposedWindows (mw);
  337. }
  338. }
  339. void askUserToOpenFile()
  340. {
  341. FileChooser fc ("Open File");
  342. if (fc.browseForFileToOpen())
  343. openFile (fc.getResult());
  344. }
  345. bool openFile (const File& file)
  346. {
  347. return mainWindowList.openFile (file);
  348. }
  349. bool closeAllDocuments (bool askUserToSave)
  350. {
  351. return openDocumentManager.closeAll (askUserToSave);
  352. }
  353. bool makeSureUserHasSelectedModuleFolder()
  354. {
  355. if (! ModuleList::isLocalModulesFolderValid())
  356. {
  357. if (! runModuleUpdate ("Please select a location to store your local set of JUCE modules,\n"
  358. "and download the ones that you'd like to use!"))
  359. {
  360. AlertWindow::showMessageBox (AlertWindow::WarningIcon,
  361. "Introjucer",
  362. "Unless you create a local JUCE folder containing some modules, you'll be unable to save any projects correctly!\n\n"
  363. "Use the option on the 'Tools' menu to set this up!");
  364. return false;
  365. }
  366. }
  367. return true;
  368. }
  369. bool runModuleUpdate (const String& message)
  370. {
  371. ModuleList list;
  372. list.rescan (ModuleList::getDefaultModulesFolder (nullptr));
  373. JuceUpdater::show (list, mainWindowList.windows[0], message);
  374. ModuleList::setLocalModulesFolder (list.getModulesFolder());
  375. return ModuleList::isJuceOrModulesFolder (list.getModulesFolder());
  376. }
  377. void showAppearanceEditorWindow()
  378. {
  379. if (appearanceEditorWindow == nullptr)
  380. appearanceEditorWindow = AppearanceSettings::createEditorWindow();
  381. appearanceEditorWindow->toFront (true);
  382. }
  383. //==============================================================================
  384. virtual void doExtraInitialisation() {}
  385. virtual void addExtraConfigItems (Project&, TreeViewItem&) {}
  386. virtual Component* createProjectContentComponent() const
  387. {
  388. return new ProjectContentComponent();
  389. }
  390. //==============================================================================
  391. IntrojucerLookAndFeel lookAndFeel;
  392. ScopedPointer<StoredSettings> settings;
  393. ScopedPointer<Icons> icons;
  394. ScopedPointer<MainMenuModel> menuModel;
  395. MainWindowList mainWindowList;
  396. OpenDocumentManager openDocumentManager;
  397. ScopedPointer<Component> appearanceEditorWindow;
  398. private:
  399. class AsyncQuitRetrier : private Timer
  400. {
  401. public:
  402. AsyncQuitRetrier() { startTimer (500); }
  403. void timerCallback()
  404. {
  405. stopTimer();
  406. delete this;
  407. JUCEApplication* app = JUCEApplication::getInstance();
  408. if (app != nullptr)
  409. app->systemRequestedQuit();
  410. }
  411. JUCE_DECLARE_NON_COPYABLE (AsyncQuitRetrier);
  412. };
  413. };
  414. #endif // __JUCER_APPLICATION_JUCEHEADER__