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.

913 lines
32KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2017 - ROLI Ltd.
  5. JUCE is an open source library subject to commercial or open-source
  6. licensing.
  7. By using JUCE, you agree to the terms of both the JUCE 5 End-User License
  8. Agreement and JUCE 5 Privacy Policy (both updated and effective as of the
  9. 27th April 2017).
  10. End User License Agreement: www.juce.com/juce-5-licence
  11. Privacy Policy: www.juce.com/juce-5-privacy-policy
  12. Or: You may also use this code under the terms of the GPL v3 (see
  13. www.gnu.org/licenses).
  14. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  15. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  16. DISCLAIMED.
  17. ==============================================================================
  18. */
  19. void createGUIEditorMenu (PopupMenu&);
  20. void handleGUIEditorMenuCommand (int);
  21. void registerGUIEditorCommands();
  22. //==============================================================================
  23. struct ProjucerApplication::MainMenuModel : public MenuBarModel
  24. {
  25. MainMenuModel()
  26. {
  27. setApplicationCommandManagerToWatch (&getCommandManager());
  28. }
  29. StringArray getMenuBarNames() override
  30. {
  31. return getApp().getMenuNames();
  32. }
  33. PopupMenu getMenuForIndex (int /*topLevelMenuIndex*/, const String& menuName) override
  34. {
  35. PopupMenu menu;
  36. getApp().createMenu (menu, menuName);
  37. return menu;
  38. }
  39. void menuItemSelected (int menuItemID, int /*topLevelMenuIndex*/) override
  40. {
  41. getApp().handleMainMenuCommand (menuItemID);
  42. }
  43. };
  44. //==============================================================================
  45. ProjucerApplication::ProjucerApplication() : isRunningCommandLine (false)
  46. {
  47. }
  48. void ProjucerApplication::initialise (const String& commandLine)
  49. {
  50. if (commandLine.trimStart().startsWith ("--server"))
  51. {
  52. initialiseLogger ("Compiler_Log_");
  53. LookAndFeel::setDefaultLookAndFeel (&lookAndFeel);
  54. #if JUCE_MAC
  55. Process::setDockIconVisible (false);
  56. #endif
  57. server = createClangServer (commandLine);
  58. }
  59. else
  60. {
  61. initialiseLogger ("IDE_Log_");
  62. Logger::writeToLog (SystemStats::getOperatingSystemName());
  63. Logger::writeToLog ("CPU: " + String (SystemStats::getCpuSpeedInMegaherz())
  64. + "MHz Cores: " + String (SystemStats::getNumCpus())
  65. + " " + String (SystemStats::getMemorySizeInMegabytes()) + "MB");
  66. initialiseBasics();
  67. isRunningCommandLine = commandLine.isNotEmpty();
  68. licenseController = new LicenseController;
  69. licenseController->addLicenseStatusChangedCallback (this);
  70. if (isRunningCommandLine)
  71. {
  72. const int appReturnCode = performCommandLine (commandLine);
  73. if (appReturnCode != commandLineNotPerformed)
  74. {
  75. setApplicationReturnValue (appReturnCode);
  76. quit();
  77. return;
  78. }
  79. isRunningCommandLine = false;
  80. }
  81. if (sendCommandLineToPreexistingInstance())
  82. {
  83. DBG ("Another instance is running - quitting...");
  84. quit();
  85. return;
  86. }
  87. openDocumentManager.registerType (new ProjucerAppClasses::LiveBuildCodeEditorDocument::Type(), 2);
  88. childProcessCache = new ChildProcessCache();
  89. initCommandManager();
  90. menuModel = new MainMenuModel();
  91. settings->appearance.refreshPresetSchemeList();
  92. setColourScheme (settings->getGlobalProperties().getIntValue ("COLOUR SCHEME"), false);
  93. setEditorColourScheme (settings->getGlobalProperties().getIntValue ("EDITOR COLOUR SCHEME"), false);
  94. updateEditorColourSchemeIfNeeded();
  95. // do further initialisation in a moment when the message loop has started
  96. triggerAsyncUpdate();
  97. }
  98. }
  99. void ProjucerApplication::initialiseBasics()
  100. {
  101. LookAndFeel::setDefaultLookAndFeel (&lookAndFeel);
  102. settings = new StoredSettings();
  103. ImageCache::setCacheTimeout (30 * 1000);
  104. icons = new Icons();
  105. tooltipWindow.setMillisecondsBeforeTipAppears (1200);
  106. }
  107. bool ProjucerApplication::initialiseLogger (const char* filePrefix)
  108. {
  109. if (logger == nullptr)
  110. {
  111. #if JUCE_LINUX
  112. String folder = "~/.config/Projucer/Logs";
  113. #else
  114. String folder = "com.juce.projucer";
  115. #endif
  116. logger = FileLogger::createDateStampedLogger (folder, filePrefix, ".txt",
  117. getApplicationName() + " " + getApplicationVersion()
  118. + " --- Build date: " __DATE__);
  119. Logger::setCurrentLogger (logger);
  120. }
  121. return logger != nullptr;
  122. }
  123. void ProjucerApplication::handleAsyncUpdate()
  124. {
  125. if (licenseController != nullptr)
  126. licenseController->startWebviewIfNeeded();
  127. #if JUCE_MAC
  128. PopupMenu extraAppleMenuItems;
  129. createExtraAppleMenuItems (extraAppleMenuItems);
  130. // workaround broken "Open Recent" submenu: not passing the
  131. // submenu's title here avoids the defect in JuceMainMenuHandler::addMenuItem
  132. MenuBarModel::setMacMainMenu (menuModel, &extraAppleMenuItems); //, "Open Recent");
  133. #endif
  134. versionChecker = new LatestVersionChecker();
  135. }
  136. void ProjucerApplication::initialiseWindows (const String& commandLine)
  137. {
  138. const String commandLineWithoutNSDebug (commandLine.replace ("-NSDocumentRevisionsDebugMode YES", StringRef()));
  139. if (commandLineWithoutNSDebug.trim().isNotEmpty() && ! commandLineWithoutNSDebug.trim().startsWithChar ('-'))
  140. anotherInstanceStarted (commandLine);
  141. else
  142. mainWindowList.reopenLastProjects();
  143. mainWindowList.createWindowIfNoneAreOpen();
  144. if (licenseController->getState().applicationUsageDataState == LicenseState::ApplicationUsageData::notChosenYet)
  145. showApplicationUsageDataAgreementPopup();
  146. }
  147. void ProjucerApplication::shutdown()
  148. {
  149. if (server != nullptr)
  150. {
  151. destroyClangServer (server);
  152. Logger::writeToLog ("Server shutdown cleanly");
  153. }
  154. versionChecker = nullptr;
  155. appearanceEditorWindow = nullptr;
  156. globalPreferencesWindow = nullptr;
  157. utf8Window = nullptr;
  158. svgPathWindow = nullptr;
  159. aboutWindow = nullptr;
  160. if (licenseController != nullptr)
  161. {
  162. licenseController->removeLicenseStatusChangedCallback (this);
  163. licenseController = nullptr;
  164. }
  165. mainWindowList.forceCloseAllWindows();
  166. openDocumentManager.clear();
  167. childProcessCache = nullptr;
  168. #if JUCE_MAC
  169. MenuBarModel::setMacMainMenu (nullptr);
  170. #endif
  171. menuModel = nullptr;
  172. commandManager = nullptr;
  173. settings = nullptr;
  174. LookAndFeel::setDefaultLookAndFeel (nullptr);
  175. if (! isRunningCommandLine)
  176. Logger::writeToLog ("Shutdown");
  177. deleteLogger();
  178. }
  179. struct AsyncQuitRetrier : private Timer
  180. {
  181. AsyncQuitRetrier() { startTimer (500); }
  182. void timerCallback() override
  183. {
  184. stopTimer();
  185. delete this;
  186. if (JUCEApplicationBase* app = JUCEApplicationBase::getInstance())
  187. app->systemRequestedQuit();
  188. }
  189. JUCE_DECLARE_NON_COPYABLE (AsyncQuitRetrier)
  190. };
  191. void ProjucerApplication::systemRequestedQuit()
  192. {
  193. if (server != nullptr)
  194. {
  195. sendQuitMessageToIDE (server);
  196. }
  197. else if (ModalComponentManager::getInstance()->cancelAllModalComponents())
  198. {
  199. new AsyncQuitRetrier();
  200. }
  201. else
  202. {
  203. if (closeAllMainWindows())
  204. quit();
  205. }
  206. }
  207. //==============================================================================
  208. void ProjucerApplication::licenseStateChanged (const LicenseState& state)
  209. {
  210. #if ! JUCER_ENABLE_GPL_MODE
  211. if (state.type != LicenseState::Type::notLoggedIn
  212. && state.type != LicenseState::Type::noLicenseChosenYet)
  213. #else
  214. ignoreUnused (state);
  215. #endif
  216. {
  217. initialiseWindows (getCommandLineParameters());
  218. }
  219. }
  220. void ProjucerApplication::doLogout()
  221. {
  222. if (licenseController != nullptr)
  223. {
  224. const LicenseState& state = licenseController->getState();
  225. if (state.type != LicenseState::Type::notLoggedIn && closeAllMainWindows())
  226. licenseController->logout();
  227. }
  228. }
  229. //==============================================================================
  230. String ProjucerApplication::getVersionDescription() const
  231. {
  232. String s;
  233. const Time buildDate (Time::getCompilationDate());
  234. s << "Projucer " << ProjectInfo::versionString
  235. << newLine
  236. << "Build date: " << buildDate.getDayOfMonth()
  237. << " " << Time::getMonthName (buildDate.getMonth(), true)
  238. << " " << buildDate.getYear();
  239. return s;
  240. }
  241. void ProjucerApplication::anotherInstanceStarted (const String& commandLine)
  242. {
  243. if (server == nullptr && ! commandLine.trim().startsWithChar ('-'))
  244. openFile (File (commandLine.unquoted()));
  245. }
  246. ProjucerApplication& ProjucerApplication::getApp()
  247. {
  248. ProjucerApplication* const app = dynamic_cast<ProjucerApplication*> (JUCEApplication::getInstance());
  249. jassert (app != nullptr);
  250. return *app;
  251. }
  252. ApplicationCommandManager& ProjucerApplication::getCommandManager()
  253. {
  254. ApplicationCommandManager* cm = ProjucerApplication::getApp().commandManager;
  255. jassert (cm != nullptr);
  256. return *cm;
  257. }
  258. //==============================================================================
  259. enum
  260. {
  261. recentProjectsBaseID = 100,
  262. activeDocumentsBaseID = 300,
  263. colourSchemeBaseID = 1000,
  264. codeEditorColourSchemeBaseID = 2000,
  265. };
  266. MenuBarModel* ProjucerApplication::getMenuModel()
  267. {
  268. return menuModel.get();
  269. }
  270. StringArray ProjucerApplication::getMenuNames()
  271. {
  272. const char* const names[] = { "File", "Edit", "View", "Build", "Window", "GUI Editor", "Tools", nullptr };
  273. return StringArray (names);
  274. }
  275. void ProjucerApplication::createMenu (PopupMenu& menu, const String& menuName)
  276. {
  277. if (menuName == "File") createFileMenu (menu);
  278. else if (menuName == "Edit") createEditMenu (menu);
  279. else if (menuName == "View") createViewMenu (menu);
  280. else if (menuName == "Build") createBuildMenu (menu);
  281. else if (menuName == "Window") createWindowMenu (menu);
  282. else if (menuName == "Tools") createToolsMenu (menu);
  283. else if (menuName == "GUI Editor") createGUIEditorMenu (menu);
  284. else jassertfalse; // names have changed?
  285. }
  286. void ProjucerApplication::createFileMenu (PopupMenu& menu)
  287. {
  288. menu.addCommandItem (commandManager, CommandIDs::newProject);
  289. menu.addSeparator();
  290. menu.addCommandItem (commandManager, CommandIDs::open);
  291. PopupMenu recentFiles;
  292. settings->recentFiles.createPopupMenuItems (recentFiles, recentProjectsBaseID, true, true);
  293. menu.addSubMenu ("Open Recent", recentFiles);
  294. menu.addSeparator();
  295. menu.addCommandItem (commandManager, CommandIDs::closeDocument);
  296. menu.addCommandItem (commandManager, CommandIDs::saveDocument);
  297. menu.addCommandItem (commandManager, CommandIDs::saveDocumentAs);
  298. menu.addCommandItem (commandManager, CommandIDs::saveAll);
  299. menu.addSeparator();
  300. menu.addCommandItem (commandManager, CommandIDs::closeProject);
  301. menu.addCommandItem (commandManager, CommandIDs::saveProject);
  302. menu.addSeparator();
  303. menu.addCommandItem (commandManager, CommandIDs::openInIDE);
  304. menu.addCommandItem (commandManager, CommandIDs::saveAndOpenInIDE);
  305. menu.addSeparator();
  306. #if ! JUCER_ENABLE_GPL_MODE
  307. menu.addCommandItem (commandManager, CommandIDs::loginLogout);
  308. #endif
  309. #if ! JUCE_MAC
  310. menu.addCommandItem (commandManager, CommandIDs::showAboutWindow);
  311. menu.addCommandItem (commandManager, CommandIDs::showAppUsageWindow);
  312. menu.addCommandItem (commandManager, CommandIDs::showGlobalPreferences);
  313. menu.addSeparator();
  314. menu.addCommandItem (commandManager, StandardApplicationCommandIDs::quit);
  315. #endif
  316. }
  317. void ProjucerApplication::createEditMenu (PopupMenu& menu)
  318. {
  319. menu.addCommandItem (commandManager, StandardApplicationCommandIDs::undo);
  320. menu.addCommandItem (commandManager, StandardApplicationCommandIDs::redo);
  321. menu.addSeparator();
  322. menu.addCommandItem (commandManager, StandardApplicationCommandIDs::cut);
  323. menu.addCommandItem (commandManager, StandardApplicationCommandIDs::copy);
  324. menu.addCommandItem (commandManager, StandardApplicationCommandIDs::paste);
  325. menu.addCommandItem (commandManager, StandardApplicationCommandIDs::del);
  326. menu.addCommandItem (commandManager, StandardApplicationCommandIDs::selectAll);
  327. menu.addCommandItem (commandManager, StandardApplicationCommandIDs::deselectAll);
  328. menu.addSeparator();
  329. menu.addCommandItem (commandManager, CommandIDs::showFindPanel);
  330. menu.addCommandItem (commandManager, CommandIDs::findSelection);
  331. menu.addCommandItem (commandManager, CommandIDs::findNext);
  332. menu.addCommandItem (commandManager, CommandIDs::findPrevious);
  333. }
  334. void ProjucerApplication::createViewMenu (PopupMenu& menu)
  335. {
  336. menu.addCommandItem (commandManager, CommandIDs::showProjectSettings);
  337. menu.addCommandItem (commandManager, CommandIDs::showProjectTab);
  338. menu.addCommandItem (commandManager, CommandIDs::showBuildTab);
  339. menu.addCommandItem (commandManager, CommandIDs::showFileExplorerPanel);
  340. menu.addCommandItem (commandManager, CommandIDs::showModulesPanel);
  341. menu.addCommandItem (commandManager, CommandIDs::showExportersPanel);
  342. menu.addCommandItem (commandManager, CommandIDs::showExporterSettings);
  343. menu.addSeparator();
  344. createColourSchemeItems (menu);
  345. }
  346. void ProjucerApplication::createBuildMenu (PopupMenu& menu)
  347. {
  348. menu.addCommandItem (commandManager, CommandIDs::toggleBuildEnabled);
  349. menu.addCommandItem (commandManager, CommandIDs::buildNow);
  350. menu.addCommandItem (commandManager, CommandIDs::toggleContinuousBuild);
  351. menu.addSeparator();
  352. menu.addCommandItem (commandManager, CommandIDs::launchApp);
  353. menu.addCommandItem (commandManager, CommandIDs::killApp);
  354. menu.addCommandItem (commandManager, CommandIDs::cleanAll);
  355. menu.addSeparator();
  356. menu.addCommandItem (commandManager, CommandIDs::reinstantiateComp);
  357. menu.addCommandItem (commandManager, CommandIDs::showWarnings);
  358. menu.addSeparator();
  359. menu.addCommandItem (commandManager, CommandIDs::nextError);
  360. menu.addCommandItem (commandManager, CommandIDs::prevError);
  361. }
  362. void ProjucerApplication::createColourSchemeItems (PopupMenu& menu)
  363. {
  364. PopupMenu colourSchemes;
  365. colourSchemes.addItem (colourSchemeBaseID + 0, "Dark", true, selectedColourSchemeIndex == 0);
  366. colourSchemes.addItem (colourSchemeBaseID + 1, "Grey", true, selectedColourSchemeIndex == 1);
  367. colourSchemes.addItem (colourSchemeBaseID + 2, "Light", true, selectedColourSchemeIndex == 2);
  368. menu.addSubMenu ("Colour Scheme", colourSchemes);
  369. //==========================================================================
  370. PopupMenu editorColourSchemes;
  371. auto& appearanceSettings = getAppSettings().appearance;
  372. appearanceSettings.refreshPresetSchemeList();
  373. auto schemes = appearanceSettings.getPresetSchemes();
  374. auto i = 0;
  375. for (auto s : schemes)
  376. {
  377. editorColourSchemes.addItem (codeEditorColourSchemeBaseID + i, s,
  378. globalPreferencesWindow == nullptr,
  379. selectedEditorColourSchemeIndex == i);
  380. ++i;
  381. }
  382. numEditorColourSchemes = i;
  383. editorColourSchemes.addSeparator();
  384. editorColourSchemes.addItem (codeEditorColourSchemeBaseID + numEditorColourSchemes,
  385. "Create...", globalPreferencesWindow == nullptr);
  386. menu.addSubMenu ("Editor Colour Scheme", editorColourSchemes);
  387. }
  388. void ProjucerApplication::createWindowMenu (PopupMenu& menu)
  389. {
  390. menu.addCommandItem (commandManager, CommandIDs::closeWindow);
  391. menu.addSeparator();
  392. menu.addCommandItem (commandManager, CommandIDs::goToPreviousDoc);
  393. menu.addCommandItem (commandManager, CommandIDs::goToNextDoc);
  394. menu.addCommandItem (commandManager, CommandIDs::goToCounterpart);
  395. menu.addSeparator();
  396. const int numDocs = jmin (50, openDocumentManager.getNumOpenDocuments());
  397. for (int i = 0; i < numDocs; ++i)
  398. {
  399. OpenDocumentManager::Document* doc = openDocumentManager.getOpenDocument(i);
  400. menu.addItem (activeDocumentsBaseID + i, doc->getName());
  401. }
  402. menu.addSeparator();
  403. menu.addCommandItem (commandManager, CommandIDs::closeAllDocuments);
  404. }
  405. void ProjucerApplication::createToolsMenu (PopupMenu& menu)
  406. {
  407. menu.addCommandItem (commandManager, CommandIDs::showUTF8Tool);
  408. menu.addCommandItem (commandManager, CommandIDs::showSVGPathTool);
  409. menu.addCommandItem (commandManager, CommandIDs::showTranslationTool);
  410. }
  411. void ProjucerApplication::createExtraAppleMenuItems (PopupMenu& menu)
  412. {
  413. menu.addCommandItem (commandManager, CommandIDs::showAboutWindow);
  414. menu.addCommandItem (commandManager, CommandIDs::showAppUsageWindow);
  415. menu.addSeparator();
  416. menu.addCommandItem (commandManager, CommandIDs::showGlobalPreferences);
  417. }
  418. void ProjucerApplication::handleMainMenuCommand (int menuItemID)
  419. {
  420. if (menuItemID >= recentProjectsBaseID && menuItemID < (recentProjectsBaseID + 100))
  421. {
  422. // open a file from the "recent files" menu
  423. openFile (settings->recentFiles.getFile (menuItemID - recentProjectsBaseID));
  424. }
  425. else if (menuItemID >= activeDocumentsBaseID && menuItemID < (activeDocumentsBaseID + 200))
  426. {
  427. if (OpenDocumentManager::Document* doc = openDocumentManager.getOpenDocument (menuItemID - activeDocumentsBaseID))
  428. mainWindowList.openDocument (doc, true);
  429. else
  430. jassertfalse;
  431. }
  432. else if (menuItemID >= colourSchemeBaseID && menuItemID < (colourSchemeBaseID + 3))
  433. {
  434. setColourScheme (menuItemID - colourSchemeBaseID, true);
  435. updateEditorColourSchemeIfNeeded();
  436. }
  437. else if (menuItemID >= codeEditorColourSchemeBaseID && menuItemID < (codeEditorColourSchemeBaseID + numEditorColourSchemes))
  438. {
  439. setEditorColourScheme (menuItemID - codeEditorColourSchemeBaseID, true);
  440. }
  441. else if (menuItemID == (codeEditorColourSchemeBaseID + numEditorColourSchemes))
  442. {
  443. AppearanceSettings::showGlobalPreferences (globalPreferencesWindow, true);
  444. }
  445. else
  446. {
  447. handleGUIEditorMenuCommand (menuItemID);
  448. }
  449. }
  450. //==============================================================================
  451. void ProjucerApplication::getAllCommands (Array <CommandID>& commands)
  452. {
  453. JUCEApplication::getAllCommands (commands);
  454. const CommandID ids[] = { CommandIDs::newProject,
  455. CommandIDs::open,
  456. CommandIDs::closeAllDocuments,
  457. CommandIDs::saveAll,
  458. CommandIDs::showGlobalPreferences,
  459. CommandIDs::showUTF8Tool,
  460. CommandIDs::showSVGPathTool,
  461. CommandIDs::showAboutWindow,
  462. CommandIDs::showAppUsageWindow,
  463. CommandIDs::loginLogout };
  464. commands.addArray (ids, numElementsInArray (ids));
  465. }
  466. void ProjucerApplication::getCommandInfo (CommandID commandID, ApplicationCommandInfo& result)
  467. {
  468. switch (commandID)
  469. {
  470. case CommandIDs::newProject:
  471. result.setInfo ("New Project...", "Creates a new Jucer project", CommandCategories::general, 0);
  472. result.defaultKeypresses.add (KeyPress ('n', ModifierKeys::commandModifier, 0));
  473. break;
  474. case CommandIDs::open:
  475. result.setInfo ("Open...", "Opens a Jucer project", CommandCategories::general, 0);
  476. result.defaultKeypresses.add (KeyPress ('o', ModifierKeys::commandModifier, 0));
  477. break;
  478. case CommandIDs::showGlobalPreferences:
  479. result.setInfo ("Preferences...", "Shows the preferences window.", CommandCategories::general, 0);
  480. result.defaultKeypresses.add (KeyPress (',', ModifierKeys::commandModifier, 0));
  481. break;
  482. case CommandIDs::closeAllDocuments:
  483. result.setInfo ("Close All Documents", "Closes all open documents", CommandCategories::general, 0);
  484. result.setActive (openDocumentManager.getNumOpenDocuments() > 0);
  485. break;
  486. case CommandIDs::saveAll:
  487. result.setInfo ("Save All", "Saves all open documents", CommandCategories::general, 0);
  488. result.defaultKeypresses.add (KeyPress ('s', ModifierKeys::commandModifier | ModifierKeys::altModifier, 0));
  489. break;
  490. case CommandIDs::showUTF8Tool:
  491. result.setInfo ("UTF-8 String-Literal Helper", "Shows the UTF-8 string literal utility", CommandCategories::general, 0);
  492. break;
  493. case CommandIDs::showSVGPathTool:
  494. result.setInfo ("SVG Path Converter", "Shows the SVG->Path data conversion utility", CommandCategories::general, 0);
  495. break;
  496. case CommandIDs::showAboutWindow:
  497. result.setInfo ("About Projucer", "Shows the Projucer's 'About' page.", CommandCategories::general, 0);
  498. break;
  499. case CommandIDs::showAppUsageWindow:
  500. result.setInfo ("Application Usage Data", "Shows the application usage data agreement window", CommandCategories::general, 0);
  501. break;
  502. case CommandIDs::loginLogout:
  503. {
  504. bool isLoggedIn = false;
  505. String username;
  506. if (licenseController != nullptr)
  507. {
  508. const LicenseState state = licenseController->getState();
  509. isLoggedIn = (state.type != LicenseState::Type::notLoggedIn && state.type != LicenseState::Type::GPL);
  510. username = state.username;
  511. }
  512. result.setInfo (isLoggedIn
  513. ? String ("Sign out ") + username + "..."
  514. : String ("Sign in..."),
  515. "Log out of your JUCE account", CommandCategories::general, 0);
  516. }
  517. break;
  518. default:
  519. JUCEApplication::getCommandInfo (commandID, result);
  520. break;
  521. }
  522. }
  523. bool ProjucerApplication::perform (const InvocationInfo& info)
  524. {
  525. switch (info.commandID)
  526. {
  527. case CommandIDs::newProject: createNewProject(); break;
  528. case CommandIDs::open: askUserToOpenFile(); break;
  529. case CommandIDs::saveAll: openDocumentManager.saveAll(); break;
  530. case CommandIDs::closeAllDocuments: closeAllDocuments (true); break;
  531. case CommandIDs::showUTF8Tool: showUTF8ToolWindow(); break;
  532. case CommandIDs::showSVGPathTool: showSVGPathDataToolWindow(); break;
  533. case CommandIDs::showGlobalPreferences: AppearanceSettings::showGlobalPreferences (globalPreferencesWindow); break;
  534. case CommandIDs::showAboutWindow: showAboutWindow(); break;
  535. case CommandIDs::showAppUsageWindow: showApplicationUsageDataAgreementPopup(); break;
  536. case CommandIDs::loginLogout: doLogout(); break;
  537. default: return JUCEApplication::perform (info);
  538. }
  539. return true;
  540. }
  541. //==============================================================================
  542. void ProjucerApplication::createNewProject()
  543. {
  544. MainWindow* mw = mainWindowList.getOrCreateEmptyWindow();
  545. mw->showNewProjectWizard();
  546. mainWindowList.avoidSuperimposedWindows (mw);
  547. }
  548. void ProjucerApplication::updateNewlyOpenedProject (Project& p)
  549. {
  550. LiveBuildProjectSettings::updateNewlyOpenedProject (p);
  551. }
  552. void ProjucerApplication::askUserToOpenFile()
  553. {
  554. FileChooser fc ("Open File");
  555. if (fc.browseForFileToOpen())
  556. openFile (fc.getResult());
  557. }
  558. bool ProjucerApplication::openFile (const File& file)
  559. {
  560. return mainWindowList.openFile (file);
  561. }
  562. bool ProjucerApplication::closeAllDocuments (bool askUserToSave)
  563. {
  564. return openDocumentManager.closeAll (askUserToSave);
  565. }
  566. bool ProjucerApplication::closeAllMainWindows()
  567. {
  568. return server != nullptr || mainWindowList.askAllWindowsToClose();
  569. }
  570. //==============================================================================
  571. void ProjucerApplication::showUTF8ToolWindow()
  572. {
  573. if (utf8Window != nullptr)
  574. utf8Window->toFront (true);
  575. else
  576. new FloatingToolWindow ("UTF-8 String Literal Converter",
  577. "utf8WindowPos",
  578. new UTF8Component(), utf8Window, true,
  579. 500, 500, 300, 300, 1000, 1000);
  580. }
  581. void ProjucerApplication::showSVGPathDataToolWindow()
  582. {
  583. if (svgPathWindow != nullptr)
  584. svgPathWindow->toFront (true);
  585. else
  586. new FloatingToolWindow ("SVG Path Converter",
  587. "svgPathWindowPos",
  588. new SVGPathDataComponent(), svgPathWindow, true,
  589. 500, 500, 300, 300, 1000, 1000);
  590. }
  591. void ProjucerApplication::showAboutWindow()
  592. {
  593. if (aboutWindow != nullptr)
  594. aboutWindow->toFront (true);
  595. else
  596. new FloatingToolWindow ({}, {}, new AboutWindowComponent(),
  597. aboutWindow, false,
  598. 500, 300, 500, 300, 500, 300);
  599. }
  600. void ProjucerApplication::showApplicationUsageDataAgreementPopup()
  601. {
  602. if (applicationUsageDataWindow != nullptr)
  603. applicationUsageDataWindow->toFront (true);
  604. else
  605. new FloatingToolWindow ("Application Usage Analytics",
  606. {}, new ApplicationUsageDataWindowComponent (isPaidOrGPL()),
  607. applicationUsageDataWindow, false,
  608. 400, 300, 400, 300, 400, 300);
  609. }
  610. void ProjucerApplication::dismissApplicationUsageDataAgreementPopup()
  611. {
  612. if (applicationUsageDataWindow != nullptr)
  613. applicationUsageDataWindow = nullptr;
  614. }
  615. //==============================================================================
  616. struct FileWithTime
  617. {
  618. FileWithTime (const File& f) : file (f), time (f.getLastModificationTime()) {}
  619. FileWithTime() {}
  620. bool operator< (const FileWithTime& other) const { return time < other.time; }
  621. bool operator== (const FileWithTime& other) const { return time == other.time; }
  622. File file;
  623. Time time;
  624. };
  625. void ProjucerApplication::deleteLogger()
  626. {
  627. const int maxNumLogFilesToKeep = 50;
  628. Logger::setCurrentLogger (nullptr);
  629. if (logger != nullptr)
  630. {
  631. Array<File> logFiles;
  632. logger->getLogFile().getParentDirectory().findChildFiles (logFiles, File::findFiles, false);
  633. if (logFiles.size() > maxNumLogFilesToKeep)
  634. {
  635. Array <FileWithTime> files;
  636. for (int i = 0; i < logFiles.size(); ++i)
  637. files.addUsingDefaultSort (logFiles.getReference(i));
  638. for (int i = 0; i < files.size() - maxNumLogFilesToKeep; ++i)
  639. files.getReference(i).file.deleteFile();
  640. }
  641. }
  642. logger = nullptr;
  643. }
  644. PropertiesFile::Options ProjucerApplication::getPropertyFileOptionsFor (const String& filename, bool isProjectSettings)
  645. {
  646. PropertiesFile::Options options;
  647. options.applicationName = filename;
  648. options.filenameSuffix = "settings";
  649. options.osxLibrarySubFolder = "Application Support";
  650. #if JUCE_LINUX
  651. options.folderName = "~/.config/Projucer";
  652. #else
  653. options.folderName = "Projucer";
  654. #endif
  655. if (isProjectSettings)
  656. options.folderName += "/ProjectSettings";
  657. return options;
  658. }
  659. void ProjucerApplication::updateAllBuildTabs()
  660. {
  661. for (int i = 0; i < mainWindowList.windows.size(); ++i)
  662. if (ProjectContentComponent* p = mainWindowList.windows.getUnchecked(i)->getProjectContentComponent())
  663. p->rebuildProjectTabs();
  664. }
  665. void ProjucerApplication::initCommandManager()
  666. {
  667. commandManager = new ApplicationCommandManager();
  668. commandManager->registerAllCommandsForTarget (this);
  669. {
  670. CodeDocument doc;
  671. CppCodeEditorComponent ed (File(), doc);
  672. commandManager->registerAllCommandsForTarget (&ed);
  673. }
  674. registerGUIEditorCommands();
  675. }
  676. void ProjucerApplication::selectEditorColourSchemeWithName (const String& schemeName)
  677. {
  678. auto& appearanceSettings = getAppSettings().appearance;
  679. auto schemes = appearanceSettings.getPresetSchemes();
  680. auto schemeIndex = schemes.indexOf (schemeName);
  681. if (schemeIndex >= 0)
  682. setEditorColourScheme (schemeIndex, true);
  683. }
  684. void ProjucerApplication::setColourScheme (int index, bool saveSetting)
  685. {
  686. switch (index)
  687. {
  688. case 0: lookAndFeel.setColourScheme (LookAndFeel_V4::getDarkColourScheme()); break;
  689. case 1: lookAndFeel.setColourScheme (LookAndFeel_V4::getGreyColourScheme()); break;
  690. case 2: lookAndFeel.setColourScheme (LookAndFeel_V4::getLightColourScheme()); break;
  691. default: break;
  692. }
  693. lookAndFeel.setupColours();
  694. mainWindowList.sendLookAndFeelChange();
  695. if (utf8Window != nullptr) utf8Window->sendLookAndFeelChange();
  696. if (svgPathWindow != nullptr) svgPathWindow->sendLookAndFeelChange();
  697. if (globalPreferencesWindow != nullptr) globalPreferencesWindow->sendLookAndFeelChange();
  698. if (aboutWindow != nullptr) aboutWindow->sendLookAndFeelChange();
  699. if (applicationUsageDataWindow != nullptr) applicationUsageDataWindow->sendLookAndFeelChange();
  700. auto* mcm = ModalComponentManager::getInstance();
  701. for (auto i = 0; i < mcm->getNumModalComponents(); ++i)
  702. mcm->getModalComponent (i)->sendLookAndFeelChange();
  703. if (saveSetting)
  704. {
  705. auto& properties = settings->getGlobalProperties();
  706. properties.setValue ("COLOUR SCHEME", index);
  707. }
  708. selectedColourSchemeIndex = index;
  709. getCommandManager().commandStatusChanged();
  710. }
  711. void ProjucerApplication::setEditorColourScheme (int index, bool saveSetting)
  712. {
  713. auto& appearanceSettings = getAppSettings().appearance;
  714. auto schemes = appearanceSettings.getPresetSchemes();
  715. index = jmin (index, schemes.size() - 1);
  716. appearanceSettings.selectPresetScheme (index);
  717. if (saveSetting)
  718. {
  719. auto& properties = settings->getGlobalProperties();
  720. properties.setValue ("EDITOR COLOUR SCHEME", index);
  721. }
  722. selectedEditorColourSchemeIndex = index;
  723. getCommandManager().commandStatusChanged();
  724. }
  725. bool ProjucerApplication::isEditorColourSchemeADefaultScheme (const StringArray& schemes, int editorColourSchemeIndex)
  726. {
  727. auto& schemeName = schemes[editorColourSchemeIndex];
  728. return (schemeName == "Default (Dark)" || schemeName == "Default (Light)");
  729. }
  730. int ProjucerApplication::getEditorColourSchemeForGUIColourScheme (const StringArray& schemes, int guiColourSchemeIndex)
  731. {
  732. auto defaultDarkEditorIndex = schemes.indexOf ("Default (Dark)");
  733. auto defaultLightEditorIndex = schemes.indexOf ("Default (Light)");
  734. // Can't find default code editor colour schemes!
  735. jassert (defaultDarkEditorIndex != -1 && defaultLightEditorIndex != -1);
  736. return (guiColourSchemeIndex == 2 ? defaultLightEditorIndex : defaultDarkEditorIndex);
  737. }
  738. void ProjucerApplication::updateEditorColourSchemeIfNeeded()
  739. {
  740. auto& appearanceSettings = getAppSettings().appearance;
  741. auto schemes = appearanceSettings.getPresetSchemes();
  742. if (isEditorColourSchemeADefaultScheme (schemes, selectedEditorColourSchemeIndex))
  743. setEditorColourScheme (getEditorColourSchemeForGUIColourScheme (schemes, selectedColourSchemeIndex), true);
  744. }