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.

593 lines
21KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2015 - ROLI Ltd.
  5. Permission is granted to use this software under the terms of either:
  6. a) the GPL v2 (or any later version)
  7. b) the Affero GPL v3
  8. Details of these licenses can be found at: www.gnu.org/licenses
  9. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  11. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  12. ------------------------------------------------------------------------------
  13. To release a closed-source product which uses JUCE, commercial licenses are
  14. available: visit www.juce.com for more information.
  15. ==============================================================================
  16. */
  17. #ifndef JUCER_APPLICATION_H_INCLUDED
  18. #define JUCER_APPLICATION_H_INCLUDED
  19. #include "../jucer_Headers.h"
  20. #include "jucer_MainWindow.h"
  21. #include "jucer_CommandLine.h"
  22. #include "../Project/jucer_Module.h"
  23. #include "jucer_AutoUpdater.h"
  24. #include "../Code Editor/jucer_SourceCodeEditor.h"
  25. void createGUIEditorMenu (PopupMenu&);
  26. void handleGUIEditorMenuCommand (int);
  27. void registerGUIEditorCommands();
  28. //==============================================================================
  29. class IntrojucerApp : public JUCEApplication
  30. {
  31. public:
  32. //==============================================================================
  33. IntrojucerApp() : isRunningCommandLine (false) {}
  34. //==============================================================================
  35. void initialise (const String& commandLine) override
  36. {
  37. LookAndFeel::setDefaultLookAndFeel (&lookAndFeel);
  38. settings = new StoredSettings();
  39. if (commandLine.isNotEmpty())
  40. {
  41. const int appReturnCode = performCommandLine (commandLine);
  42. if (appReturnCode != commandLineNotPerformed)
  43. {
  44. isRunningCommandLine = true;
  45. setApplicationReturnValue (appReturnCode);
  46. quit();
  47. return;
  48. }
  49. }
  50. if (sendCommandLineToPreexistingInstance())
  51. {
  52. DBG ("Another instance is running - quitting...");
  53. quit();
  54. return;
  55. }
  56. initialiseLogger ("log_");
  57. icons = new Icons();
  58. if (! doExtraInitialisation())
  59. {
  60. quit();
  61. return;
  62. }
  63. initCommandManager();
  64. menuModel = new MainMenuModel();
  65. settings->appearance.refreshPresetSchemeList();
  66. ImageCache::setCacheTimeout (30 * 1000);
  67. if (commandLine.trim().isNotEmpty() && ! commandLine.trim().startsWithChar ('-'))
  68. anotherInstanceStarted (commandLine);
  69. else
  70. mainWindowList.reopenLastProjects();
  71. mainWindowList.createWindowIfNoneAreOpen();
  72. #if JUCE_MAC
  73. MenuBarModel::setMacMainMenu (menuModel, nullptr, "Open Recent");
  74. #endif
  75. versionChecker = new LatestVersionChecker();
  76. }
  77. void shutdown() override
  78. {
  79. versionChecker = nullptr;
  80. appearanceEditorWindow = nullptr;
  81. globalPreferencesWindow = nullptr;
  82. utf8Window = nullptr;
  83. svgPathWindow = nullptr;
  84. mainWindowList.forceCloseAllWindows();
  85. openDocumentManager.clear();
  86. #if JUCE_MAC
  87. MenuBarModel::setMacMainMenu (nullptr);
  88. #endif
  89. menuModel = nullptr;
  90. commandManager = nullptr;
  91. settings = nullptr;
  92. LookAndFeel::setDefaultLookAndFeel (nullptr);
  93. if (! isRunningCommandLine)
  94. Logger::writeToLog ("Shutdown");
  95. deleteLogger();
  96. }
  97. //==============================================================================
  98. void systemRequestedQuit() override
  99. {
  100. if (ModalComponentManager::getInstance()->cancelAllModalComponents())
  101. {
  102. new AsyncQuitRetrier();
  103. }
  104. else
  105. {
  106. if (closeAllMainWindows())
  107. quit();
  108. }
  109. }
  110. //==============================================================================
  111. const String getApplicationName() override { return "Introjucer"; }
  112. const String getApplicationVersion() override { return ProjectInfo::versionString; }
  113. bool moreThanOneInstanceAllowed() override
  114. {
  115. return true; // this is handled manually in initialise()
  116. }
  117. void anotherInstanceStarted (const String& commandLine) override
  118. {
  119. openFile (File (commandLine.unquoted()));
  120. }
  121. static IntrojucerApp& getApp()
  122. {
  123. IntrojucerApp* const app = dynamic_cast<IntrojucerApp*> (JUCEApplication::getInstance());
  124. jassert (app != nullptr);
  125. return *app;
  126. }
  127. static ApplicationCommandManager& getCommandManager()
  128. {
  129. ApplicationCommandManager* cm = IntrojucerApp::getApp().commandManager;
  130. jassert (cm != nullptr);
  131. return *cm;
  132. }
  133. //==============================================================================
  134. class MainMenuModel : public MenuBarModel
  135. {
  136. public:
  137. MainMenuModel()
  138. {
  139. setApplicationCommandManagerToWatch (&getCommandManager());
  140. }
  141. StringArray getMenuBarNames() override
  142. {
  143. return getApp().getMenuNames();
  144. }
  145. PopupMenu getMenuForIndex (int /*topLevelMenuIndex*/, const String& menuName) override
  146. {
  147. PopupMenu menu;
  148. getApp().createMenu (menu, menuName);
  149. return menu;
  150. }
  151. void menuItemSelected (int menuItemID, int /*topLevelMenuIndex*/) override
  152. {
  153. getApp().handleMainMenuCommand (menuItemID);
  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", "GUI Editor", "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 if (menuName == "GUI Editor") createGUIEditorMenu (menu);
  175. else jassertfalse; // names have changed?
  176. }
  177. virtual void createFileMenu (PopupMenu& menu)
  178. {
  179. menu.addCommandItem (commandManager, CommandIDs::newProject);
  180. menu.addSeparator();
  181. menu.addCommandItem (commandManager, CommandIDs::open);
  182. PopupMenu recentFiles;
  183. settings->recentFiles.createPopupMenuItems (recentFiles, recentProjectsBaseID, true, true);
  184. menu.addSubMenu ("Open Recent", recentFiles);
  185. menu.addSeparator();
  186. menu.addCommandItem (commandManager, CommandIDs::closeDocument);
  187. menu.addCommandItem (commandManager, CommandIDs::saveDocument);
  188. menu.addCommandItem (commandManager, CommandIDs::saveDocumentAs);
  189. menu.addCommandItem (commandManager, CommandIDs::saveAll);
  190. menu.addSeparator();
  191. menu.addCommandItem (commandManager, CommandIDs::closeProject);
  192. menu.addCommandItem (commandManager, CommandIDs::saveProject);
  193. menu.addSeparator();
  194. menu.addCommandItem (commandManager, CommandIDs::openInIDE);
  195. menu.addCommandItem (commandManager, CommandIDs::saveAndOpenInIDE);
  196. #if ! JUCE_MAC
  197. menu.addSeparator();
  198. menu.addCommandItem (commandManager, StandardApplicationCommandIDs::quit);
  199. #endif
  200. }
  201. virtual void createEditMenu (PopupMenu& menu)
  202. {
  203. menu.addCommandItem (commandManager, StandardApplicationCommandIDs::undo);
  204. menu.addCommandItem (commandManager, StandardApplicationCommandIDs::redo);
  205. menu.addSeparator();
  206. menu.addCommandItem (commandManager, StandardApplicationCommandIDs::cut);
  207. menu.addCommandItem (commandManager, StandardApplicationCommandIDs::copy);
  208. menu.addCommandItem (commandManager, StandardApplicationCommandIDs::paste);
  209. menu.addCommandItem (commandManager, StandardApplicationCommandIDs::del);
  210. menu.addCommandItem (commandManager, StandardApplicationCommandIDs::selectAll);
  211. menu.addCommandItem (commandManager, StandardApplicationCommandIDs::deselectAll);
  212. menu.addSeparator();
  213. menu.addCommandItem (commandManager, CommandIDs::showFindPanel);
  214. menu.addCommandItem (commandManager, CommandIDs::findSelection);
  215. menu.addCommandItem (commandManager, CommandIDs::findNext);
  216. menu.addCommandItem (commandManager, CommandIDs::findPrevious);
  217. }
  218. virtual void createViewMenu (PopupMenu& menu)
  219. {
  220. menu.addCommandItem (commandManager, CommandIDs::showFilePanel);
  221. menu.addCommandItem (commandManager, CommandIDs::showConfigPanel);
  222. menu.addCommandItem (commandManager, CommandIDs::showProjectSettings);
  223. menu.addCommandItem (commandManager, CommandIDs::showProjectModules);
  224. menu.addSeparator();
  225. createColourSchemeItems (menu);
  226. }
  227. void createColourSchemeItems (PopupMenu& menu)
  228. {
  229. const StringArray presetSchemes (settings->appearance.getPresetSchemes());
  230. if (presetSchemes.size() > 0)
  231. {
  232. PopupMenu schemes;
  233. for (int i = 0; i < presetSchemes.size(); ++i)
  234. schemes.addItem (colourSchemeBaseID + i, presetSchemes[i]);
  235. menu.addSubMenu ("Colour Scheme", schemes);
  236. }
  237. }
  238. virtual void createWindowMenu (PopupMenu& menu)
  239. {
  240. menu.addCommandItem (commandManager, CommandIDs::closeWindow);
  241. menu.addSeparator();
  242. menu.addCommandItem (commandManager, CommandIDs::goToPreviousDoc);
  243. menu.addCommandItem (commandManager, CommandIDs::goToNextDoc);
  244. menu.addCommandItem (commandManager, CommandIDs::goToCounterpart);
  245. menu.addSeparator();
  246. const int numDocs = jmin (50, openDocumentManager.getNumOpenDocuments());
  247. for (int i = 0; i < numDocs; ++i)
  248. {
  249. OpenDocumentManager::Document* doc = openDocumentManager.getOpenDocument(i);
  250. menu.addItem (activeDocumentsBaseID + i, doc->getName());
  251. }
  252. menu.addSeparator();
  253. menu.addCommandItem (commandManager, CommandIDs::closeAllDocuments);
  254. }
  255. virtual void createToolsMenu (PopupMenu& menu)
  256. {
  257. menu.addCommandItem (commandManager, CommandIDs::showGlobalPreferences);
  258. menu.addSeparator();
  259. menu.addCommandItem (commandManager, CommandIDs::showUTF8Tool);
  260. menu.addCommandItem (commandManager, CommandIDs::showSVGPathTool);
  261. menu.addCommandItem (commandManager, CommandIDs::showTranslationTool);
  262. }
  263. virtual void handleMainMenuCommand (int menuItemID)
  264. {
  265. if (menuItemID >= recentProjectsBaseID && menuItemID < recentProjectsBaseID + 100)
  266. {
  267. // open a file from the "recent files" menu
  268. openFile (settings->recentFiles.getFile (menuItemID - recentProjectsBaseID));
  269. }
  270. else if (menuItemID >= activeDocumentsBaseID && menuItemID < activeDocumentsBaseID + 200)
  271. {
  272. if (OpenDocumentManager::Document* doc = openDocumentManager.getOpenDocument (menuItemID - activeDocumentsBaseID))
  273. mainWindowList.openDocument (doc, true);
  274. else
  275. jassertfalse;
  276. }
  277. else if (menuItemID >= colourSchemeBaseID && menuItemID < colourSchemeBaseID + 200)
  278. {
  279. settings->appearance.selectPresetScheme (menuItemID - colourSchemeBaseID);
  280. }
  281. else
  282. {
  283. handleGUIEditorMenuCommand (menuItemID);
  284. }
  285. }
  286. //==============================================================================
  287. void getAllCommands (Array <CommandID>& commands) override
  288. {
  289. JUCEApplication::getAllCommands (commands);
  290. const CommandID ids[] = { CommandIDs::newProject,
  291. CommandIDs::open,
  292. CommandIDs::closeAllDocuments,
  293. CommandIDs::saveAll,
  294. CommandIDs::showGlobalPreferences,
  295. CommandIDs::showUTF8Tool,
  296. CommandIDs::showSVGPathTool };
  297. commands.addArray (ids, numElementsInArray (ids));
  298. }
  299. void getCommandInfo (CommandID commandID, ApplicationCommandInfo& result) override
  300. {
  301. switch (commandID)
  302. {
  303. case CommandIDs::newProject:
  304. result.setInfo ("New Project...", "Creates a new Jucer project", CommandCategories::general, 0);
  305. result.defaultKeypresses.add (KeyPress ('n', ModifierKeys::commandModifier, 0));
  306. break;
  307. case CommandIDs::open:
  308. result.setInfo ("Open...", "Opens a Jucer project", CommandCategories::general, 0);
  309. result.defaultKeypresses.add (KeyPress ('o', ModifierKeys::commandModifier, 0));
  310. break;
  311. case CommandIDs::showGlobalPreferences:
  312. result.setInfo ("Global Preferences...", "Shows the global preferences window.", CommandCategories::general, 0);
  313. break;
  314. case CommandIDs::closeAllDocuments:
  315. result.setInfo ("Close All Documents", "Closes all open documents", CommandCategories::general, 0);
  316. result.setActive (openDocumentManager.getNumOpenDocuments() > 0);
  317. break;
  318. case CommandIDs::saveAll:
  319. result.setInfo ("Save All", "Saves all open documents", CommandCategories::general, 0);
  320. result.defaultKeypresses.add (KeyPress ('s', ModifierKeys::commandModifier | ModifierKeys::altModifier, 0));
  321. break;
  322. case CommandIDs::showUTF8Tool:
  323. result.setInfo ("UTF-8 String-Literal Helper", "Shows the UTF-8 string literal utility", CommandCategories::general, 0);
  324. break;
  325. case CommandIDs::showSVGPathTool:
  326. result.setInfo ("SVG Path Helper", "Shows the SVG->Path data conversion utility", CommandCategories::general, 0);
  327. break;
  328. default:
  329. JUCEApplication::getCommandInfo (commandID, result);
  330. break;
  331. }
  332. }
  333. bool perform (const InvocationInfo& info) override
  334. {
  335. switch (info.commandID)
  336. {
  337. case CommandIDs::newProject: createNewProject(); break;
  338. case CommandIDs::open: askUserToOpenFile(); break;
  339. case CommandIDs::saveAll: openDocumentManager.saveAll(); break;
  340. case CommandIDs::closeAllDocuments: closeAllDocuments (true); break;
  341. case CommandIDs::showUTF8Tool: showUTF8ToolWindow (utf8Window); break;
  342. case CommandIDs::showSVGPathTool: showSVGPathDataToolWindow (svgPathWindow); break;
  343. case CommandIDs::showGlobalPreferences: AppearanceSettings::showGlobalPreferences (globalPreferencesWindow); break;
  344. default: return JUCEApplication::perform (info);
  345. }
  346. return true;
  347. }
  348. //==============================================================================
  349. void createNewProject()
  350. {
  351. MainWindow* mw = mainWindowList.getOrCreateEmptyWindow();
  352. mw->showNewProjectWizard();
  353. mainWindowList.avoidSuperimposedWindows (mw);
  354. }
  355. virtual void updateNewlyOpenedProject (Project&) {}
  356. void askUserToOpenFile()
  357. {
  358. FileChooser fc ("Open File");
  359. if (fc.browseForFileToOpen())
  360. openFile (fc.getResult());
  361. }
  362. bool openFile (const File& file)
  363. {
  364. return mainWindowList.openFile (file);
  365. }
  366. bool closeAllDocuments (bool askUserToSave)
  367. {
  368. return openDocumentManager.closeAll (askUserToSave);
  369. }
  370. virtual bool closeAllMainWindows()
  371. {
  372. return mainWindowList.askAllWindowsToClose();
  373. }
  374. //==============================================================================
  375. void initialiseLogger (const char* filePrefix)
  376. {
  377. if (logger == nullptr)
  378. {
  379. logger = FileLogger::createDateStampedLogger (getLogFolderName(), filePrefix, ".txt",
  380. getApplicationName() + " " + getApplicationVersion()
  381. + " --- Build date: " __DATE__);
  382. Logger::setCurrentLogger (logger);
  383. }
  384. }
  385. struct FileWithTime
  386. {
  387. FileWithTime (const File& f) : file (f), time (f.getLastModificationTime()) {}
  388. FileWithTime() {}
  389. bool operator< (const FileWithTime& other) const { return time < other.time; }
  390. bool operator== (const FileWithTime& other) const { return time == other.time; }
  391. File file;
  392. Time time;
  393. };
  394. void deleteLogger()
  395. {
  396. const int maxNumLogFilesToKeep = 50;
  397. Logger::setCurrentLogger (nullptr);
  398. if (logger != nullptr)
  399. {
  400. Array<File> logFiles;
  401. logger->getLogFile().getParentDirectory().findChildFiles (logFiles, File::findFiles, false);
  402. if (logFiles.size() > maxNumLogFilesToKeep)
  403. {
  404. Array <FileWithTime> files;
  405. for (int i = 0; i < logFiles.size(); ++i)
  406. files.addUsingDefaultSort (logFiles.getReference(i));
  407. for (int i = 0; i < files.size() - maxNumLogFilesToKeep; ++i)
  408. files.getReference(i).file.deleteFile();
  409. }
  410. }
  411. logger = nullptr;
  412. }
  413. virtual bool doExtraInitialisation() { return true; }
  414. virtual void addExtraConfigItems (Project&, TreeViewItem&) {}
  415. #if JUCE_LINUX
  416. virtual String getLogFolderName() const { return "~/.config/Introjucer/Logs"; }
  417. #else
  418. virtual String getLogFolderName() const { return "com.juce.introjucer"; }
  419. #endif
  420. virtual PropertiesFile::Options getPropertyFileOptionsFor (const String& filename)
  421. {
  422. PropertiesFile::Options options;
  423. options.applicationName = filename;
  424. options.filenameSuffix = "settings";
  425. options.osxLibrarySubFolder = "Application Support";
  426. #if JUCE_LINUX
  427. options.folderName = "~/.config/Introjucer";
  428. #else
  429. options.folderName = "Introjucer";
  430. #endif
  431. return options;
  432. }
  433. virtual Component* createProjectContentComponent() const
  434. {
  435. return new ProjectContentComponent();
  436. }
  437. //==============================================================================
  438. IntrojucerLookAndFeel lookAndFeel;
  439. ScopedPointer<StoredSettings> settings;
  440. ScopedPointer<Icons> icons;
  441. ScopedPointer<MainMenuModel> menuModel;
  442. MainWindowList mainWindowList;
  443. OpenDocumentManager openDocumentManager;
  444. ScopedPointer<ApplicationCommandManager> commandManager;
  445. ScopedPointer<Component> appearanceEditorWindow, globalPreferencesWindow, utf8Window, svgPathWindow;
  446. ScopedPointer<FileLogger> logger;
  447. bool isRunningCommandLine;
  448. private:
  449. ScopedPointer<LatestVersionChecker> versionChecker;
  450. class AsyncQuitRetrier : private Timer
  451. {
  452. public:
  453. AsyncQuitRetrier() { startTimer (500); }
  454. void timerCallback() override
  455. {
  456. stopTimer();
  457. delete this;
  458. if (JUCEApplicationBase* app = JUCEApplicationBase::getInstance())
  459. app->systemRequestedQuit();
  460. }
  461. JUCE_DECLARE_NON_COPYABLE (AsyncQuitRetrier)
  462. };
  463. void initCommandManager()
  464. {
  465. commandManager = new ApplicationCommandManager();
  466. commandManager->registerAllCommandsForTarget (this);
  467. {
  468. CodeDocument doc;
  469. CppCodeEditorComponent ed (File::nonexistent, doc);
  470. commandManager->registerAllCommandsForTarget (&ed);
  471. }
  472. registerGUIEditorCommands();
  473. }
  474. };
  475. #endif // JUCER_APPLICATION_H_INCLUDED