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.

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