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.

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