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.

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