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.

628 lines
22KB

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