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.

652 lines
23KB

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