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.

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