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.

508 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. {
  58. CodeDocument doc;
  59. CppCodeEditorComponent ed (File::nonexistent, doc);
  60. commandManager->registerAllCommandsForTarget (&ed);
  61. }
  62. menuModel = new MainMenuModel();
  63. doExtraInitialisation();
  64. settings->appearance.refreshPresetSchemeList();
  65. ImageCache::setCacheTimeout (30 * 1000);
  66. if (commandLine.trim().isNotEmpty() && ! commandLine.trim().startsWithChar ('-'))
  67. anotherInstanceStarted (commandLine);
  68. else
  69. mainWindowList.reopenLastProjects();
  70. makeSureUserHasSelectedModuleFolder();
  71. mainWindowList.createWindowIfNoneAreOpen();
  72. #if JUCE_MAC
  73. MenuBarModel::setMacMainMenu (menuModel);
  74. #endif
  75. }
  76. void shutdown()
  77. {
  78. appearanceEditorWindow = nullptr;
  79. utf8Window = nullptr;
  80. #if JUCE_MAC
  81. MenuBarModel::setMacMainMenu (nullptr);
  82. #endif
  83. menuModel = nullptr;
  84. mainWindowList.forceCloseAllWindows();
  85. openDocumentManager.clear();
  86. commandManager = nullptr;
  87. settings = nullptr;
  88. LookAndFeel::setDefaultLookAndFeel (nullptr);
  89. }
  90. //==============================================================================
  91. void systemRequestedQuit()
  92. {
  93. if ((! triggerAsyncQuitIfModalCompsActive())
  94. && mainWindowList.askAllWindowsToClose())
  95. quit();
  96. }
  97. bool triggerAsyncQuitIfModalCompsActive()
  98. {
  99. if (cancelAnyModalComponents())
  100. {
  101. new AsyncQuitRetrier();
  102. return true;
  103. }
  104. return false;
  105. }
  106. //==============================================================================
  107. const String getApplicationName()
  108. {
  109. return "Introjucer";
  110. }
  111. const String getApplicationVersion()
  112. {
  113. return ProjectInfo::versionString;
  114. }
  115. bool moreThanOneInstanceAllowed()
  116. {
  117. return true; // this is handled manually in initialise()
  118. }
  119. void anotherInstanceStarted (const String& commandLine)
  120. {
  121. openFile (commandLine.unquoted());
  122. }
  123. static IntrojucerApp& getApp()
  124. {
  125. IntrojucerApp* const app = dynamic_cast<IntrojucerApp*> (JUCEApplication::getInstance());
  126. jassert (app != nullptr);
  127. return *app;
  128. }
  129. //==============================================================================
  130. class MainMenuModel : public MenuBarModel
  131. {
  132. public:
  133. MainMenuModel()
  134. {
  135. setApplicationCommandManagerToWatch (commandManager);
  136. }
  137. StringArray getMenuBarNames()
  138. {
  139. return getApp().getMenuNames();
  140. }
  141. PopupMenu getMenuForIndex (int /*topLevelMenuIndex*/, const String& menuName)
  142. {
  143. PopupMenu menu;
  144. getApp().createMenu (menu, menuName);
  145. return menu;
  146. }
  147. void menuItemSelected (int menuItemID, int /*topLevelMenuIndex*/)
  148. {
  149. if (menuItemID >= recentProjectsBaseID && menuItemID < recentProjectsBaseID + 100)
  150. {
  151. // open a file from the "recent files" menu
  152. getApp().openFile (getAppSettings().recentFiles.getFile (menuItemID - recentProjectsBaseID));
  153. }
  154. else if (menuItemID >= activeDocumentsBaseID && menuItemID < activeDocumentsBaseID + 200)
  155. {
  156. OpenDocumentManager::Document* doc = getApp().openDocumentManager.getOpenDocument (menuItemID - activeDocumentsBaseID);
  157. jassert (doc != nullptr);
  158. getApp().mainWindowList.openDocument (doc, true);
  159. }
  160. else if (menuItemID >= colourSchemeBaseID && menuItemID < colourSchemeBaseID + 200)
  161. {
  162. getAppSettings().appearance.selectPresetScheme (menuItemID - colourSchemeBaseID);
  163. }
  164. }
  165. };
  166. enum
  167. {
  168. recentProjectsBaseID = 100,
  169. activeDocumentsBaseID = 300,
  170. colourSchemeBaseID = 1000
  171. };
  172. virtual StringArray getMenuNames()
  173. {
  174. const char* const names[] = { "File", "Edit", "View", "Window", "Tools", nullptr };
  175. return StringArray (names);
  176. }
  177. virtual void createMenu (PopupMenu& menu, const String& menuName)
  178. {
  179. if (menuName == "File") createFileMenu (menu);
  180. else if (menuName == "Edit") createEditMenu (menu);
  181. else if (menuName == "View") createViewMenu (menu);
  182. else if (menuName == "Window") createWindowMenu (menu);
  183. else if (menuName == "Tools") createToolsMenu (menu);
  184. else jassertfalse; // names have changed?
  185. }
  186. virtual void createFileMenu (PopupMenu& menu)
  187. {
  188. menu.addCommandItem (commandManager, CommandIDs::newProject);
  189. menu.addSeparator();
  190. menu.addCommandItem (commandManager, CommandIDs::open);
  191. PopupMenu recentFiles;
  192. getAppSettings().recentFiles.createPopupMenuItems (recentFiles, recentProjectsBaseID, true, true);
  193. menu.addSubMenu ("Open recent file", recentFiles);
  194. menu.addSeparator();
  195. menu.addCommandItem (commandManager, CommandIDs::closeDocument);
  196. menu.addCommandItem (commandManager, CommandIDs::saveDocument);
  197. menu.addSeparator();
  198. menu.addCommandItem (commandManager, CommandIDs::closeProject);
  199. menu.addCommandItem (commandManager, CommandIDs::saveProject);
  200. menu.addSeparator();
  201. menu.addCommandItem (commandManager, CommandIDs::openInIDE);
  202. menu.addCommandItem (commandManager, CommandIDs::saveAndOpenInIDE);
  203. #if ! JUCE_MAC
  204. menu.addSeparator();
  205. menu.addCommandItem (commandManager, StandardApplicationCommandIDs::quit);
  206. #endif
  207. }
  208. virtual void createEditMenu (PopupMenu& menu)
  209. {
  210. menu.addCommandItem (commandManager, StandardApplicationCommandIDs::undo);
  211. menu.addCommandItem (commandManager, StandardApplicationCommandIDs::redo);
  212. menu.addSeparator();
  213. menu.addCommandItem (commandManager, StandardApplicationCommandIDs::cut);
  214. menu.addCommandItem (commandManager, StandardApplicationCommandIDs::copy);
  215. menu.addCommandItem (commandManager, StandardApplicationCommandIDs::paste);
  216. menu.addCommandItem (commandManager, StandardApplicationCommandIDs::del);
  217. menu.addCommandItem (commandManager, StandardApplicationCommandIDs::selectAll);
  218. menu.addCommandItem (commandManager, StandardApplicationCommandIDs::deselectAll);
  219. menu.addSeparator();
  220. menu.addCommandItem (commandManager, CommandIDs::showFindPanel);
  221. menu.addCommandItem (commandManager, CommandIDs::findSelection);
  222. menu.addCommandItem (commandManager, CommandIDs::findNext);
  223. menu.addCommandItem (commandManager, CommandIDs::findPrevious);
  224. }
  225. virtual void createViewMenu (PopupMenu& menu)
  226. {
  227. menu.addCommandItem (commandManager, CommandIDs::showFilePanel);
  228. menu.addCommandItem (commandManager, CommandIDs::showConfigPanel);
  229. menu.addSeparator();
  230. createColourSchemeItems (menu);
  231. }
  232. void createColourSchemeItems (PopupMenu& menu)
  233. {
  234. menu.addCommandItem (commandManager, CommandIDs::showAppearanceSettings);
  235. const StringArray presetSchemes (settings->appearance.getPresetSchemes());
  236. if (presetSchemes.size() > 0)
  237. {
  238. PopupMenu schemes;
  239. for (int i = 0; i < presetSchemes.size(); ++i)
  240. schemes.addItem (colourSchemeBaseID + i, presetSchemes[i]);
  241. menu.addSubMenu ("Colour Scheme", schemes);
  242. }
  243. }
  244. virtual void createWindowMenu (PopupMenu& menu)
  245. {
  246. menu.addCommandItem (commandManager, CommandIDs::closeWindow);
  247. menu.addSeparator();
  248. menu.addCommandItem (commandManager, CommandIDs::goToPreviousDoc);
  249. menu.addCommandItem (commandManager, CommandIDs::goToNextDoc);
  250. menu.addCommandItem (commandManager, CommandIDs::goToCounterpart);
  251. menu.addSeparator();
  252. const int numDocs = jmin (50, getApp().openDocumentManager.getNumOpenDocuments());
  253. for (int i = 0; i < numDocs; ++i)
  254. {
  255. OpenDocumentManager::Document* doc = getApp().openDocumentManager.getOpenDocument(i);
  256. menu.addItem (activeDocumentsBaseID + i, doc->getName());
  257. }
  258. menu.addSeparator();
  259. menu.addCommandItem (commandManager, CommandIDs::closeAllDocuments);
  260. }
  261. virtual void createToolsMenu (PopupMenu& menu)
  262. {
  263. menu.addCommandItem (commandManager, CommandIDs::updateModules);
  264. menu.addCommandItem (commandManager, CommandIDs::showUTF8Tool);
  265. }
  266. //==============================================================================
  267. void getAllCommands (Array <CommandID>& commands)
  268. {
  269. JUCEApplication::getAllCommands (commands);
  270. const CommandID ids[] = { CommandIDs::newProject,
  271. CommandIDs::open,
  272. CommandIDs::closeAllDocuments,
  273. CommandIDs::saveAll,
  274. CommandIDs::updateModules,
  275. CommandIDs::showAppearanceSettings,
  276. CommandIDs::showUTF8Tool };
  277. commands.addArray (ids, numElementsInArray (ids));
  278. }
  279. void getCommandInfo (CommandID commandID, ApplicationCommandInfo& result)
  280. {
  281. switch (commandID)
  282. {
  283. case CommandIDs::newProject:
  284. result.setInfo ("New Project...", "Creates a new Jucer project", CommandCategories::general, 0);
  285. result.defaultKeypresses.add (KeyPress ('n', ModifierKeys::commandModifier, 0));
  286. break;
  287. case CommandIDs::open:
  288. result.setInfo ("Open...", "Opens a Jucer project", CommandCategories::general, 0);
  289. result.defaultKeypresses.add (KeyPress ('o', ModifierKeys::commandModifier, 0));
  290. break;
  291. case CommandIDs::showAppearanceSettings:
  292. result.setInfo ("Fonts and Colours...", "Shows the appearance settings window.", CommandCategories::general, 0);
  293. break;
  294. case CommandIDs::closeAllDocuments:
  295. result.setInfo ("Close All Documents", "Closes all open documents", CommandCategories::general, 0);
  296. result.setActive (openDocumentManager.getNumOpenDocuments() > 0);
  297. break;
  298. case CommandIDs::saveAll:
  299. result.setInfo ("Save All", "Saves all open documents", CommandCategories::general, 0);
  300. result.setActive (openDocumentManager.anyFilesNeedSaving());
  301. break;
  302. case CommandIDs::updateModules:
  303. result.setInfo ("Download the latest JUCE modules", "Checks online for any JUCE modules updates and installs them", CommandCategories::general, 0);
  304. break;
  305. case CommandIDs::showUTF8Tool:
  306. result.setInfo ("UTF-8 String-Literal Helper", "Shows the UTF-8 string literal utility", CommandCategories::general, 0);
  307. break;
  308. default:
  309. JUCEApplication::getCommandInfo (commandID, result);
  310. break;
  311. }
  312. }
  313. bool perform (const InvocationInfo& info)
  314. {
  315. switch (info.commandID)
  316. {
  317. case CommandIDs::newProject: createNewProject(); break;
  318. case CommandIDs::open: askUserToOpenFile(); break;
  319. case CommandIDs::saveAll: openDocumentManager.saveAll(); break;
  320. case CommandIDs::closeAllDocuments: closeAllDocuments (true); break;
  321. case CommandIDs::showUTF8Tool: showUTF8ToolWindow (utf8Window); break;
  322. case CommandIDs::showAppearanceSettings: AppearanceSettings::showEditorWindow (appearanceEditorWindow); break;
  323. case CommandIDs::updateModules: runModuleUpdate (String::empty); break;
  324. default: return JUCEApplication::perform (info);
  325. }
  326. return true;
  327. }
  328. //==============================================================================
  329. void createNewProject()
  330. {
  331. if (makeSureUserHasSelectedModuleFolder())
  332. {
  333. MainWindow* mw = mainWindowList.getOrCreateEmptyWindow();
  334. mw->showNewProjectWizard();
  335. mainWindowList.avoidSuperimposedWindows (mw);
  336. }
  337. }
  338. virtual void updateNewlyOpenedProject (Project&) {}
  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. //==============================================================================
  378. virtual void doExtraInitialisation() {}
  379. virtual void addExtraConfigItems (Project&, TreeViewItem&) {}
  380. virtual Component* createProjectContentComponent() const
  381. {
  382. return new ProjectContentComponent();
  383. }
  384. //==============================================================================
  385. IntrojucerLookAndFeel lookAndFeel;
  386. ScopedPointer<StoredSettings> settings;
  387. ScopedPointer<Icons> icons;
  388. ScopedPointer<MainMenuModel> menuModel;
  389. MainWindowList mainWindowList;
  390. OpenDocumentManager openDocumentManager;
  391. ScopedPointer<Component> appearanceEditorWindow, utf8Window;
  392. private:
  393. class AsyncQuitRetrier : private Timer
  394. {
  395. public:
  396. AsyncQuitRetrier() { startTimer (500); }
  397. void timerCallback()
  398. {
  399. stopTimer();
  400. delete this;
  401. if (JUCEApplication* app = JUCEApplication::getInstance())
  402. app->systemRequestedQuit();
  403. }
  404. JUCE_DECLARE_NON_COPYABLE (AsyncQuitRetrier);
  405. };
  406. };
  407. #endif // __JUCER_APPLICATION_JUCEHEADER__