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.

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