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.

574 lines
20KB

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