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