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.

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