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.

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