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.

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