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.

580 lines
20KB

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