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.

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