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. 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.addCommandItem (commandManager, CommandIDs::saveDocumentAs);
  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__