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.

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