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.

464 lines
17KB

  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 JucerApplication : public JUCEApplication
  27. {
  28. public:
  29. //==============================================================================
  30. JucerApplication() {}
  31. ~JucerApplication() {}
  32. //==============================================================================
  33. void initialise (const String& commandLine)
  34. {
  35. if (commandLine.isNotEmpty())
  36. {
  37. const int appReturnCode = performCommandLine (commandLine);
  38. if (appReturnCode != commandLineNotPerformed)
  39. {
  40. setApplicationReturnValue (appReturnCode);
  41. quit();
  42. return;
  43. }
  44. }
  45. commandManager = new ApplicationCommandManager();
  46. commandManager->registerAllCommandsForTarget (this);
  47. menuModel = new MainMenuModel();
  48. doExtraInitialisation();
  49. ImageCache::setCacheTimeout (30 * 1000);
  50. if (commandLine.trim().isNotEmpty() && ! commandLine.trim().startsWithChar ('-'))
  51. anotherInstanceStarted (commandLine);
  52. else
  53. mainWindowList.reopenLastProjects();
  54. makeSureUserHasSelectedModuleFolder();
  55. mainWindowList.createWindowIfNoneAreOpen();
  56. #if JUCE_MAC
  57. MenuBarModel::setMacMainMenu (menuModel);
  58. #endif
  59. }
  60. void shutdown()
  61. {
  62. appearanceEditorWindow = nullptr;
  63. #if JUCE_MAC
  64. MenuBarModel::setMacMainMenu (nullptr);
  65. #endif
  66. menuModel = nullptr;
  67. mainWindowList.forceCloseAllWindows();
  68. openDocumentManager.clear();
  69. commandManager = nullptr;
  70. settings.flush();
  71. }
  72. //==============================================================================
  73. void systemRequestedQuit()
  74. {
  75. if (cancelAnyModalComponents())
  76. {
  77. new AsyncQuitRetrier();
  78. return;
  79. }
  80. if (mainWindowList.askAllWindowsToClose())
  81. quit();
  82. }
  83. //==============================================================================
  84. const String getApplicationName()
  85. {
  86. return String (ProjectInfo::projectName) + " " + getApplicationVersion();
  87. }
  88. const String getApplicationVersion()
  89. {
  90. return ProjectInfo::versionString;
  91. }
  92. bool moreThanOneInstanceAllowed()
  93. {
  94. #ifndef JUCE_LINUX
  95. return false;
  96. #else
  97. return true; //xxx should be false but doesn't work on linux..
  98. #endif
  99. }
  100. void anotherInstanceStarted (const String& commandLine)
  101. {
  102. openFile (commandLine.unquoted());
  103. }
  104. static JucerApplication* getApp()
  105. {
  106. return dynamic_cast<JucerApplication*> (JUCEApplication::getInstance());
  107. }
  108. //==============================================================================
  109. class MainMenuModel : public MenuBarModel
  110. {
  111. public:
  112. MainMenuModel()
  113. {
  114. setApplicationCommandManagerToWatch (commandManager);
  115. }
  116. StringArray getMenuBarNames()
  117. {
  118. const char* const names[] = { "File", "Edit", "View", "Window", "Tools", 0 };
  119. return StringArray ((const char**) names);
  120. }
  121. PopupMenu getMenuForIndex (int topLevelMenuIndex, const String& /*menuName*/)
  122. {
  123. PopupMenu menu;
  124. if (topLevelMenuIndex == 0) // "File" menu
  125. {
  126. menu.addCommandItem (commandManager, CommandIDs::newProject);
  127. menu.addSeparator();
  128. menu.addCommandItem (commandManager, CommandIDs::open);
  129. PopupMenu recentFiles;
  130. getAppSettings().recentFiles.createPopupMenuItems (recentFiles, 100, true, true);
  131. menu.addSubMenu ("Open recent file", recentFiles);
  132. menu.addSeparator();
  133. menu.addCommandItem (commandManager, CommandIDs::closeDocument);
  134. menu.addCommandItem (commandManager, CommandIDs::saveDocument);
  135. menu.addSeparator();
  136. menu.addCommandItem (commandManager, CommandIDs::closeProject);
  137. menu.addCommandItem (commandManager, CommandIDs::saveProject);
  138. menu.addSeparator();
  139. menu.addCommandItem (commandManager, CommandIDs::openInIDE);
  140. menu.addCommandItem (commandManager, CommandIDs::saveAndOpenInIDE);
  141. #if ! JUCE_MAC
  142. menu.addSeparator();
  143. menu.addCommandItem (commandManager, StandardApplicationCommandIDs::quit);
  144. #endif
  145. }
  146. else if (topLevelMenuIndex == 1) // "Edit" menu
  147. {
  148. menu.addCommandItem (commandManager, StandardApplicationCommandIDs::undo);
  149. menu.addCommandItem (commandManager, StandardApplicationCommandIDs::redo);
  150. menu.addSeparator();
  151. menu.addCommandItem (commandManager, StandardApplicationCommandIDs::cut);
  152. menu.addCommandItem (commandManager, StandardApplicationCommandIDs::copy);
  153. menu.addCommandItem (commandManager, StandardApplicationCommandIDs::paste);
  154. menu.addCommandItem (commandManager, StandardApplicationCommandIDs::del);
  155. menu.addCommandItem (commandManager, StandardApplicationCommandIDs::selectAll);
  156. menu.addCommandItem (commandManager, StandardApplicationCommandIDs::deselectAll);
  157. menu.addSeparator();
  158. menu.addCommandItem (commandManager, CommandIDs::toFront);
  159. menu.addCommandItem (commandManager, CommandIDs::toBack);
  160. menu.addSeparator();
  161. menu.addCommandItem (commandManager, CommandIDs::group);
  162. menu.addCommandItem (commandManager, CommandIDs::ungroup);
  163. menu.addSeparator();
  164. menu.addCommandItem (commandManager, CommandIDs::bringBackLostItems);
  165. }
  166. else if (topLevelMenuIndex == 2) // "View" menu
  167. {
  168. menu.addCommandItem (commandManager, CommandIDs::showProjectSettings);
  169. menu.addSeparator();
  170. menu.addCommandItem (commandManager, CommandIDs::showAppearanceSettings);
  171. menu.addSeparator();
  172. menu.addCommandItem (commandManager, CommandIDs::showGrid);
  173. menu.addCommandItem (commandManager, CommandIDs::enableSnapToGrid);
  174. menu.addSeparator();
  175. menu.addCommandItem (commandManager, CommandIDs::zoomIn);
  176. menu.addCommandItem (commandManager, CommandIDs::zoomOut);
  177. menu.addCommandItem (commandManager, CommandIDs::zoomNormal);
  178. menu.addSeparator();
  179. menu.addCommandItem (commandManager, CommandIDs::useTabbedWindows);
  180. }
  181. else if (topLevelMenuIndex == 3) // "Window" menu
  182. {
  183. menu.addCommandItem (commandManager, CommandIDs::closeWindow);
  184. menu.addSeparator();
  185. const int numDocs = jmin (50, getApp()->openDocumentManager.getNumOpenDocuments());
  186. for (int i = 0; i < numDocs; ++i)
  187. {
  188. OpenDocumentManager::Document* doc = getApp()->openDocumentManager.getOpenDocument(i);
  189. menu.addItem (300 + i, doc->getName());
  190. }
  191. menu.addSeparator();
  192. menu.addCommandItem (commandManager, CommandIDs::closeAllDocuments);
  193. }
  194. else if (topLevelMenuIndex == 4) // "Tools" menu
  195. {
  196. menu.addCommandItem (commandManager, CommandIDs::updateModules);
  197. menu.addCommandItem (commandManager, CommandIDs::showUTF8Tool);
  198. }
  199. return menu;
  200. }
  201. void menuItemSelected (int menuItemID, int /*topLevelMenuIndex*/)
  202. {
  203. if (menuItemID >= 100 && menuItemID < 200)
  204. {
  205. // open a file from the "recent files" menu
  206. const File file (getAppSettings().recentFiles.getFile (menuItemID - 100));
  207. getApp()->openFile (file);
  208. }
  209. else if (menuItemID >= 300 && menuItemID < 400)
  210. {
  211. OpenDocumentManager::Document* doc = getApp()->openDocumentManager.getOpenDocument (menuItemID - 300);
  212. jassert (doc != nullptr);
  213. getApp()->mainWindowList.openDocument (doc);
  214. }
  215. }
  216. };
  217. //==============================================================================
  218. void getAllCommands (Array <CommandID>& commands)
  219. {
  220. JUCEApplication::getAllCommands (commands);
  221. const CommandID ids[] = { CommandIDs::newProject,
  222. CommandIDs::open,
  223. CommandIDs::showPrefs,
  224. CommandIDs::closeAllDocuments,
  225. CommandIDs::saveAll,
  226. CommandIDs::updateModules,
  227. CommandIDs::showAppearanceSettings,
  228. CommandIDs::showUTF8Tool };
  229. commands.addArray (ids, numElementsInArray (ids));
  230. }
  231. void getCommandInfo (CommandID commandID, ApplicationCommandInfo& result)
  232. {
  233. switch (commandID)
  234. {
  235. case CommandIDs::newProject:
  236. result.setInfo ("New Project...", "Creates a new Jucer project", CommandCategories::general, 0);
  237. result.defaultKeypresses.add (KeyPress ('n', ModifierKeys::commandModifier, 0));
  238. break;
  239. case CommandIDs::open:
  240. result.setInfo ("Open...", "Opens a Jucer project", CommandCategories::general, 0);
  241. result.defaultKeypresses.add (KeyPress ('o', ModifierKeys::commandModifier, 0));
  242. break;
  243. case CommandIDs::showPrefs:
  244. result.setInfo ("Preferences...", "Shows the preferences panel.", CommandCategories::general, 0);
  245. result.defaultKeypresses.add (KeyPress (',', ModifierKeys::commandModifier, 0));
  246. break;
  247. case CommandIDs::showAppearanceSettings:
  248. result.setInfo ("Fonts and Colours...", "Shows the appearance settings window.", CommandCategories::general, 0);
  249. break;
  250. case CommandIDs::closeAllDocuments:
  251. result.setInfo ("Close All Documents", "Closes all open documents", CommandCategories::general, 0);
  252. result.setActive (openDocumentManager.getNumOpenDocuments() > 0);
  253. break;
  254. case CommandIDs::saveAll:
  255. result.setInfo ("Save All", "Saves all open documents", CommandCategories::general, 0);
  256. result.setActive (openDocumentManager.anyFilesNeedSaving());
  257. break;
  258. case CommandIDs::updateModules:
  259. result.setInfo ("Download the latest JUCE modules", "Checks online for any JUCE modules updates and installs them", CommandCategories::general, 0);
  260. break;
  261. case CommandIDs::showUTF8Tool:
  262. result.setInfo ("UTF-8 String-Literal Helper", "Shows the UTF-8 string literal utility", CommandCategories::general, 0);
  263. break;
  264. default:
  265. JUCEApplication::getCommandInfo (commandID, result);
  266. break;
  267. }
  268. }
  269. bool perform (const InvocationInfo& info)
  270. {
  271. switch (info.commandID)
  272. {
  273. case CommandIDs::newProject: createNewProject(); break;
  274. case CommandIDs::open: askUserToOpenFile(); break;
  275. case CommandIDs::showPrefs: showPrefsPanel(); break;
  276. case CommandIDs::saveAll: openDocumentManager.saveAll(); break;
  277. case CommandIDs::closeAllDocuments: closeAllDocuments (true); break;
  278. case CommandIDs::showUTF8Tool: showUTF8ToolWindow(); break;
  279. case CommandIDs::showAppearanceSettings: showAppearanceEditorWindow(); break;
  280. case CommandIDs::updateModules: runModuleUpdate (String::empty); break;
  281. default:
  282. return JUCEApplication::perform (info);
  283. }
  284. return true;
  285. }
  286. //==============================================================================
  287. void showPrefsPanel()
  288. {
  289. jassertfalse;
  290. }
  291. void createNewProject()
  292. {
  293. if (makeSureUserHasSelectedModuleFolder())
  294. {
  295. MainWindow* mw = mainWindowList.getOrCreateEmptyWindow();
  296. mw->showNewProjectWizard();
  297. mainWindowList.avoidSuperimposedWindows (mw);
  298. }
  299. }
  300. void askUserToOpenFile()
  301. {
  302. FileChooser fc ("Open File");
  303. if (fc.browseForFileToOpen())
  304. openFile (fc.getResult());
  305. }
  306. bool openFile (const File& file)
  307. {
  308. return mainWindowList.openFile (file);
  309. }
  310. bool closeAllDocuments (bool askUserToSave)
  311. {
  312. return openDocumentManager.closeAll (askUserToSave);
  313. }
  314. bool makeSureUserHasSelectedModuleFolder()
  315. {
  316. if (! ModuleList::isLocalModulesFolderValid())
  317. {
  318. if (! runModuleUpdate ("Please select a location to store your local set of JUCE modules,\n"
  319. "and download the ones that you'd like to use!"))
  320. {
  321. AlertWindow::showMessageBox (AlertWindow::WarningIcon,
  322. "Introjucer",
  323. "Unless you create a local JUCE folder containing some modules, you'll be unable to save any projects correctly!\n\n"
  324. "Use the option on the 'Tools' menu to set this up!");
  325. return false;
  326. }
  327. }
  328. return true;
  329. }
  330. bool runModuleUpdate (const String& message)
  331. {
  332. ModuleList list;
  333. list.rescan (ModuleList::getDefaultModulesFolder (nullptr));
  334. JuceUpdater::show (list, mainWindowList.windows[0], message);
  335. ModuleList::setLocalModulesFolder (list.getModulesFolder());
  336. return ModuleList::isJuceOrModulesFolder (list.getModulesFolder());
  337. }
  338. void showAppearanceEditorWindow()
  339. {
  340. if (appearanceEditorWindow == nullptr)
  341. appearanceEditorWindow = AppearanceSettings::createEditorWindow();
  342. appearanceEditorWindow->toFront (true);
  343. }
  344. //==============================================================================
  345. virtual void doExtraInitialisation() {}
  346. virtual void addExtraConfigItems (Project&, TreeViewItem&) {}
  347. virtual Component* createProjectContentComponent() const
  348. {
  349. return new ProjectContentComponent();
  350. }
  351. //==============================================================================
  352. StoredSettings settings;
  353. Icons icons;
  354. ScopedPointer<MainMenuModel> menuModel;
  355. MainWindowList mainWindowList;
  356. OpenDocumentManager openDocumentManager;
  357. ScopedPointer<Component> appearanceEditorWindow;
  358. private:
  359. class AsyncQuitRetrier : public Timer
  360. {
  361. public:
  362. AsyncQuitRetrier() { startTimer (500); }
  363. void timerCallback()
  364. {
  365. stopTimer();
  366. delete this;
  367. if (getApp() != nullptr)
  368. getApp()->systemRequestedQuit();
  369. }
  370. JUCE_DECLARE_NON_COPYABLE (AsyncQuitRetrier);
  371. };
  372. };
  373. #endif // __JUCER_APPLICATION_JUCEHEADER__