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.

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