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.

589 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. initCommandManager();
  59. menuModel = new MainMenuModel();
  60. doExtraInitialisation();
  61. settings->appearance.refreshPresetSchemeList();
  62. ImageCache::setCacheTimeout (30 * 1000);
  63. if (commandLine.trim().isNotEmpty() && ! commandLine.trim().startsWithChar ('-'))
  64. anotherInstanceStarted (commandLine);
  65. else
  66. mainWindowList.reopenLastProjects();
  67. mainWindowList.createWindowIfNoneAreOpen();
  68. #if JUCE_MAC
  69. MenuBarModel::setMacMainMenu (menuModel, nullptr, "Open Recent");
  70. #endif
  71. versionChecker = new LatestVersionChecker();
  72. }
  73. void shutdown() override
  74. {
  75. versionChecker = nullptr;
  76. appearanceEditorWindow = nullptr;
  77. globalPreferencesWindow = nullptr;
  78. utf8Window = nullptr;
  79. svgPathWindow = nullptr;
  80. mainWindowList.forceCloseAllWindows();
  81. openDocumentManager.clear();
  82. #if JUCE_MAC
  83. MenuBarModel::setMacMainMenu (nullptr);
  84. #endif
  85. menuModel = nullptr;
  86. commandManager = nullptr;
  87. settings = nullptr;
  88. LookAndFeel::setDefaultLookAndFeel (nullptr);
  89. if (! isRunningCommandLine)
  90. Logger::writeToLog ("Shutdown");
  91. deleteLogger();
  92. }
  93. //==============================================================================
  94. void systemRequestedQuit() override
  95. {
  96. if (ModalComponentManager::getInstance()->cancelAllModalComponents())
  97. {
  98. new AsyncQuitRetrier();
  99. }
  100. else
  101. {
  102. if (closeAllMainWindows())
  103. quit();
  104. }
  105. }
  106. //==============================================================================
  107. const String getApplicationName() override { return "Introjucer"; }
  108. const String getApplicationVersion() override { return ProjectInfo::versionString; }
  109. bool moreThanOneInstanceAllowed() override
  110. {
  111. return true; // this is handled manually in initialise()
  112. }
  113. void anotherInstanceStarted (const String& commandLine) override
  114. {
  115. openFile (File (commandLine.unquoted()));
  116. }
  117. static IntrojucerApp& getApp()
  118. {
  119. IntrojucerApp* const app = dynamic_cast<IntrojucerApp*> (JUCEApplication::getInstance());
  120. jassert (app != nullptr);
  121. return *app;
  122. }
  123. static ApplicationCommandManager& getCommandManager()
  124. {
  125. ApplicationCommandManager* cm = IntrojucerApp::getApp().commandManager;
  126. jassert (cm != nullptr);
  127. return *cm;
  128. }
  129. //==============================================================================
  130. class MainMenuModel : public MenuBarModel
  131. {
  132. public:
  133. MainMenuModel()
  134. {
  135. setApplicationCommandManagerToWatch (&getCommandManager());
  136. }
  137. StringArray getMenuBarNames() override
  138. {
  139. return getApp().getMenuNames();
  140. }
  141. PopupMenu getMenuForIndex (int /*topLevelMenuIndex*/, const String& menuName) override
  142. {
  143. PopupMenu menu;
  144. getApp().createMenu (menu, menuName);
  145. return menu;
  146. }
  147. void menuItemSelected (int menuItemID, int /*topLevelMenuIndex*/) override
  148. {
  149. getApp().handleMainMenuCommand (menuItemID);
  150. }
  151. };
  152. enum
  153. {
  154. recentProjectsBaseID = 100,
  155. activeDocumentsBaseID = 300,
  156. colourSchemeBaseID = 1000
  157. };
  158. virtual StringArray getMenuNames()
  159. {
  160. const char* const names[] = { "File", "Edit", "View", "Window", "GUI Editor", "Tools", nullptr };
  161. return StringArray (names);
  162. }
  163. virtual void createMenu (PopupMenu& menu, const String& menuName)
  164. {
  165. if (menuName == "File") createFileMenu (menu);
  166. else if (menuName == "Edit") createEditMenu (menu);
  167. else if (menuName == "View") createViewMenu (menu);
  168. else if (menuName == "Window") createWindowMenu (menu);
  169. else if (menuName == "Tools") createToolsMenu (menu);
  170. else if (menuName == "GUI Editor") createGUIEditorMenu (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. settings->recentFiles.createPopupMenuItems (recentFiles, recentProjectsBaseID, true, true);
  180. menu.addSubMenu ("Open Recent", recentFiles);
  181. menu.addSeparator();
  182. menu.addCommandItem (commandManager, CommandIDs::closeDocument);
  183. menu.addCommandItem (commandManager, CommandIDs::saveDocument);
  184. menu.addCommandItem (commandManager, CommandIDs::saveDocumentAs);
  185. menu.addCommandItem (commandManager, CommandIDs::saveAll);
  186. menu.addSeparator();
  187. menu.addCommandItem (commandManager, CommandIDs::closeProject);
  188. menu.addCommandItem (commandManager, CommandIDs::saveProject);
  189. menu.addSeparator();
  190. menu.addCommandItem (commandManager, CommandIDs::openInIDE);
  191. menu.addCommandItem (commandManager, CommandIDs::saveAndOpenInIDE);
  192. #if ! JUCE_MAC
  193. menu.addSeparator();
  194. menu.addCommandItem (commandManager, StandardApplicationCommandIDs::quit);
  195. #endif
  196. }
  197. virtual void createEditMenu (PopupMenu& menu)
  198. {
  199. menu.addCommandItem (commandManager, StandardApplicationCommandIDs::undo);
  200. menu.addCommandItem (commandManager, StandardApplicationCommandIDs::redo);
  201. menu.addSeparator();
  202. menu.addCommandItem (commandManager, StandardApplicationCommandIDs::cut);
  203. menu.addCommandItem (commandManager, StandardApplicationCommandIDs::copy);
  204. menu.addCommandItem (commandManager, StandardApplicationCommandIDs::paste);
  205. menu.addCommandItem (commandManager, StandardApplicationCommandIDs::del);
  206. menu.addCommandItem (commandManager, StandardApplicationCommandIDs::selectAll);
  207. menu.addCommandItem (commandManager, StandardApplicationCommandIDs::deselectAll);
  208. menu.addSeparator();
  209. menu.addCommandItem (commandManager, CommandIDs::showFindPanel);
  210. menu.addCommandItem (commandManager, CommandIDs::findSelection);
  211. menu.addCommandItem (commandManager, CommandIDs::findNext);
  212. menu.addCommandItem (commandManager, CommandIDs::findPrevious);
  213. }
  214. virtual void createViewMenu (PopupMenu& menu)
  215. {
  216. menu.addCommandItem (commandManager, CommandIDs::showFilePanel);
  217. menu.addCommandItem (commandManager, CommandIDs::showConfigPanel);
  218. menu.addCommandItem (commandManager, CommandIDs::showProjectSettings);
  219. menu.addCommandItem (commandManager, CommandIDs::showProjectModules);
  220. menu.addSeparator();
  221. createColourSchemeItems (menu);
  222. }
  223. void createColourSchemeItems (PopupMenu& menu)
  224. {
  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.addCommandItem (commandManager, CommandIDs::goToCounterpart);
  241. menu.addSeparator();
  242. const int numDocs = jmin (50, openDocumentManager.getNumOpenDocuments());
  243. for (int i = 0; i < numDocs; ++i)
  244. {
  245. OpenDocumentManager::Document* doc = openDocumentManager.getOpenDocument(i);
  246. menu.addItem (activeDocumentsBaseID + i, doc->getName());
  247. }
  248. menu.addSeparator();
  249. menu.addCommandItem (commandManager, CommandIDs::closeAllDocuments);
  250. }
  251. virtual void createToolsMenu (PopupMenu& menu)
  252. {
  253. menu.addCommandItem (commandManager, CommandIDs::showGlobalPreferences);
  254. menu.addSeparator();
  255. menu.addCommandItem (commandManager, CommandIDs::showUTF8Tool);
  256. menu.addCommandItem (commandManager, CommandIDs::showSVGPathTool);
  257. menu.addCommandItem (commandManager, CommandIDs::showTranslationTool);
  258. }
  259. virtual void handleMainMenuCommand (int menuItemID)
  260. {
  261. if (menuItemID >= recentProjectsBaseID && menuItemID < recentProjectsBaseID + 100)
  262. {
  263. // open a file from the "recent files" menu
  264. openFile (settings->recentFiles.getFile (menuItemID - recentProjectsBaseID));
  265. }
  266. else if (menuItemID >= activeDocumentsBaseID && menuItemID < activeDocumentsBaseID + 200)
  267. {
  268. if (OpenDocumentManager::Document* doc = openDocumentManager.getOpenDocument (menuItemID - activeDocumentsBaseID))
  269. mainWindowList.openDocument (doc, true);
  270. else
  271. jassertfalse;
  272. }
  273. else if (menuItemID >= colourSchemeBaseID && menuItemID < colourSchemeBaseID + 200)
  274. {
  275. settings->appearance.selectPresetScheme (menuItemID - colourSchemeBaseID);
  276. }
  277. else
  278. {
  279. handleGUIEditorMenuCommand (menuItemID);
  280. }
  281. }
  282. //==============================================================================
  283. void getAllCommands (Array <CommandID>& commands) override
  284. {
  285. JUCEApplication::getAllCommands (commands);
  286. const CommandID ids[] = { CommandIDs::newProject,
  287. CommandIDs::open,
  288. CommandIDs::closeAllDocuments,
  289. CommandIDs::saveAll,
  290. CommandIDs::showGlobalPreferences,
  291. CommandIDs::showUTF8Tool,
  292. CommandIDs::showSVGPathTool };
  293. commands.addArray (ids, numElementsInArray (ids));
  294. }
  295. void getCommandInfo (CommandID commandID, ApplicationCommandInfo& result) override
  296. {
  297. switch (commandID)
  298. {
  299. case CommandIDs::newProject:
  300. result.setInfo ("New Project...", "Creates a new Jucer project", CommandCategories::general, 0);
  301. result.defaultKeypresses.add (KeyPress ('n', ModifierKeys::commandModifier, 0));
  302. break;
  303. case CommandIDs::open:
  304. result.setInfo ("Open...", "Opens a Jucer project", CommandCategories::general, 0);
  305. result.defaultKeypresses.add (KeyPress ('o', ModifierKeys::commandModifier, 0));
  306. break;
  307. case CommandIDs::showGlobalPreferences:
  308. result.setInfo ("Global Preferences...", "Shows the global preferences window.", CommandCategories::general, 0);
  309. break;
  310. case CommandIDs::closeAllDocuments:
  311. result.setInfo ("Close All Documents", "Closes all open documents", CommandCategories::general, 0);
  312. result.setActive (openDocumentManager.getNumOpenDocuments() > 0);
  313. break;
  314. case CommandIDs::saveAll:
  315. result.setInfo ("Save All", "Saves all open documents", CommandCategories::general, 0);
  316. result.defaultKeypresses.add (KeyPress ('s', ModifierKeys::commandModifier | ModifierKeys::altModifier, 0));
  317. break;
  318. case CommandIDs::showUTF8Tool:
  319. result.setInfo ("UTF-8 String-Literal Helper", "Shows the UTF-8 string literal utility", CommandCategories::general, 0);
  320. break;
  321. case CommandIDs::showSVGPathTool:
  322. result.setInfo ("SVG Path Helper", "Shows the SVG->Path data conversion utility", CommandCategories::general, 0);
  323. break;
  324. default:
  325. JUCEApplication::getCommandInfo (commandID, result);
  326. break;
  327. }
  328. }
  329. bool perform (const InvocationInfo& info) override
  330. {
  331. switch (info.commandID)
  332. {
  333. case CommandIDs::newProject: createNewProject(); break;
  334. case CommandIDs::open: askUserToOpenFile(); break;
  335. case CommandIDs::saveAll: openDocumentManager.saveAll(); break;
  336. case CommandIDs::closeAllDocuments: closeAllDocuments (true); break;
  337. case CommandIDs::showUTF8Tool: showUTF8ToolWindow (utf8Window); break;
  338. case CommandIDs::showSVGPathTool: showSVGPathDataToolWindow (svgPathWindow); break;
  339. case CommandIDs::showGlobalPreferences: AppearanceSettings::showGlobalPreferences (globalPreferencesWindow); break;
  340. default: return JUCEApplication::perform (info);
  341. }
  342. return true;
  343. }
  344. //==============================================================================
  345. void createNewProject()
  346. {
  347. MainWindow* mw = mainWindowList.getOrCreateEmptyWindow();
  348. mw->showNewProjectWizard();
  349. mainWindowList.avoidSuperimposedWindows (mw);
  350. }
  351. virtual void updateNewlyOpenedProject (Project&) {}
  352. void askUserToOpenFile()
  353. {
  354. FileChooser fc ("Open File");
  355. if (fc.browseForFileToOpen())
  356. openFile (fc.getResult());
  357. }
  358. bool openFile (const File& file)
  359. {
  360. return mainWindowList.openFile (file);
  361. }
  362. bool closeAllDocuments (bool askUserToSave)
  363. {
  364. return openDocumentManager.closeAll (askUserToSave);
  365. }
  366. virtual bool closeAllMainWindows()
  367. {
  368. return mainWindowList.askAllWindowsToClose();
  369. }
  370. //==============================================================================
  371. void initialiseLogger (const char* filePrefix)
  372. {
  373. if (logger == nullptr)
  374. {
  375. logger = FileLogger::createDateStampedLogger (getLogFolderName(), filePrefix, ".txt",
  376. getApplicationName() + " " + getApplicationVersion()
  377. + " --- Build date: " __DATE__);
  378. Logger::setCurrentLogger (logger);
  379. }
  380. }
  381. struct FileWithTime
  382. {
  383. FileWithTime (const File& f) : file (f), time (f.getLastModificationTime()) {}
  384. FileWithTime() {}
  385. bool operator< (const FileWithTime& other) const { return time < other.time; }
  386. bool operator== (const FileWithTime& other) const { return time == other.time; }
  387. File file;
  388. Time time;
  389. };
  390. void deleteLogger()
  391. {
  392. const int maxNumLogFilesToKeep = 50;
  393. Logger::setCurrentLogger (nullptr);
  394. if (logger != nullptr)
  395. {
  396. Array<File> logFiles;
  397. logger->getLogFile().getParentDirectory().findChildFiles (logFiles, File::findFiles, false);
  398. if (logFiles.size() > maxNumLogFilesToKeep)
  399. {
  400. Array <FileWithTime> files;
  401. for (int i = 0; i < logFiles.size(); ++i)
  402. files.addUsingDefaultSort (logFiles.getReference(i));
  403. for (int i = 0; i < files.size() - maxNumLogFilesToKeep; ++i)
  404. files.getReference(i).file.deleteFile();
  405. }
  406. }
  407. logger = nullptr;
  408. }
  409. virtual void doExtraInitialisation() {}
  410. virtual void addExtraConfigItems (Project&, TreeViewItem&) {}
  411. #if JUCE_LINUX
  412. virtual String getLogFolderName() const { return "~/.config/Introjucer/Logs"; }
  413. #else
  414. virtual String getLogFolderName() const { return "com.juce.introjucer"; }
  415. #endif
  416. virtual PropertiesFile::Options getPropertyFileOptionsFor (const String& filename)
  417. {
  418. PropertiesFile::Options options;
  419. options.applicationName = filename;
  420. options.filenameSuffix = "settings";
  421. options.osxLibrarySubFolder = "Application Support";
  422. #if JUCE_LINUX
  423. options.folderName = "~/.config/Introjucer";
  424. #else
  425. options.folderName = "Introjucer";
  426. #endif
  427. return options;
  428. }
  429. virtual Component* createProjectContentComponent() const
  430. {
  431. return new ProjectContentComponent();
  432. }
  433. //==============================================================================
  434. IntrojucerLookAndFeel lookAndFeel;
  435. ScopedPointer<StoredSettings> settings;
  436. ScopedPointer<Icons> icons;
  437. ScopedPointer<MainMenuModel> menuModel;
  438. MainWindowList mainWindowList;
  439. OpenDocumentManager openDocumentManager;
  440. ScopedPointer<ApplicationCommandManager> commandManager;
  441. ScopedPointer<Component> appearanceEditorWindow, globalPreferencesWindow, utf8Window, svgPathWindow;
  442. ScopedPointer<FileLogger> logger;
  443. bool isRunningCommandLine;
  444. private:
  445. ScopedPointer<LatestVersionChecker> versionChecker;
  446. class AsyncQuitRetrier : private Timer
  447. {
  448. public:
  449. AsyncQuitRetrier() { startTimer (500); }
  450. void timerCallback() override
  451. {
  452. stopTimer();
  453. delete this;
  454. if (JUCEApplicationBase* app = JUCEApplicationBase::getInstance())
  455. app->systemRequestedQuit();
  456. }
  457. JUCE_DECLARE_NON_COPYABLE (AsyncQuitRetrier)
  458. };
  459. void initCommandManager()
  460. {
  461. commandManager = new ApplicationCommandManager();
  462. commandManager->registerAllCommandsForTarget (this);
  463. {
  464. CodeDocument doc;
  465. CppCodeEditorComponent ed (File::nonexistent, doc);
  466. commandManager->registerAllCommandsForTarget (&ed);
  467. }
  468. registerGUIEditorCommands();
  469. }
  470. };
  471. #endif // JUCER_APPLICATION_H_INCLUDED