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.

627 lines
22KB

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