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.

468 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. menu.addCommandItem (commandManager, CommandIDs::goToPreviousDoc);
  186. menu.addCommandItem (commandManager, CommandIDs::goToNextDoc);
  187. menu.addSeparator();
  188. const int numDocs = jmin (50, getApp()->openDocumentManager.getNumOpenDocuments());
  189. for (int i = 0; i < numDocs; ++i)
  190. {
  191. OpenDocumentManager::Document* doc = getApp()->openDocumentManager.getOpenDocument(i);
  192. menu.addItem (300 + i, doc->getName());
  193. }
  194. menu.addSeparator();
  195. menu.addCommandItem (commandManager, CommandIDs::closeAllDocuments);
  196. }
  197. else if (topLevelMenuIndex == 4) // "Tools" menu
  198. {
  199. menu.addCommandItem (commandManager, CommandIDs::updateModules);
  200. menu.addCommandItem (commandManager, CommandIDs::showUTF8Tool);
  201. }
  202. return menu;
  203. }
  204. void menuItemSelected (int menuItemID, int /*topLevelMenuIndex*/)
  205. {
  206. if (menuItemID >= 100 && menuItemID < 200)
  207. {
  208. // open a file from the "recent files" menu
  209. const File file (getAppSettings().recentFiles.getFile (menuItemID - 100));
  210. getApp()->openFile (file);
  211. }
  212. else if (menuItemID >= 300 && menuItemID < 400)
  213. {
  214. OpenDocumentManager::Document* doc = getApp()->openDocumentManager.getOpenDocument (menuItemID - 300);
  215. jassert (doc != nullptr);
  216. getApp()->mainWindowList.openDocument (doc);
  217. }
  218. }
  219. };
  220. //==============================================================================
  221. void getAllCommands (Array <CommandID>& commands)
  222. {
  223. JUCEApplication::getAllCommands (commands);
  224. const CommandID ids[] = { CommandIDs::newProject,
  225. CommandIDs::open,
  226. CommandIDs::showPrefs,
  227. CommandIDs::closeAllDocuments,
  228. CommandIDs::saveAll,
  229. CommandIDs::updateModules,
  230. CommandIDs::showAppearanceSettings,
  231. CommandIDs::showUTF8Tool };
  232. commands.addArray (ids, numElementsInArray (ids));
  233. }
  234. void getCommandInfo (CommandID commandID, ApplicationCommandInfo& result)
  235. {
  236. switch (commandID)
  237. {
  238. case CommandIDs::newProject:
  239. result.setInfo ("New Project...", "Creates a new Jucer project", CommandCategories::general, 0);
  240. result.defaultKeypresses.add (KeyPress ('n', ModifierKeys::commandModifier, 0));
  241. break;
  242. case CommandIDs::open:
  243. result.setInfo ("Open...", "Opens a Jucer project", CommandCategories::general, 0);
  244. result.defaultKeypresses.add (KeyPress ('o', ModifierKeys::commandModifier, 0));
  245. break;
  246. case CommandIDs::showPrefs:
  247. result.setInfo ("Preferences...", "Shows the preferences panel.", CommandCategories::general, 0);
  248. result.defaultKeypresses.add (KeyPress (',', ModifierKeys::commandModifier, 0));
  249. break;
  250. case CommandIDs::showAppearanceSettings:
  251. result.setInfo ("Fonts and Colours...", "Shows the appearance settings window.", CommandCategories::general, 0);
  252. break;
  253. case CommandIDs::closeAllDocuments:
  254. result.setInfo ("Close All Documents", "Closes all open documents", CommandCategories::general, 0);
  255. result.setActive (openDocumentManager.getNumOpenDocuments() > 0);
  256. break;
  257. case CommandIDs::saveAll:
  258. result.setInfo ("Save All", "Saves all open documents", CommandCategories::general, 0);
  259. result.setActive (openDocumentManager.anyFilesNeedSaving());
  260. break;
  261. case CommandIDs::updateModules:
  262. result.setInfo ("Download the latest JUCE modules", "Checks online for any JUCE modules updates and installs them", CommandCategories::general, 0);
  263. break;
  264. case CommandIDs::showUTF8Tool:
  265. result.setInfo ("UTF-8 String-Literal Helper", "Shows the UTF-8 string literal utility", CommandCategories::general, 0);
  266. break;
  267. default:
  268. JUCEApplication::getCommandInfo (commandID, result);
  269. break;
  270. }
  271. }
  272. bool perform (const InvocationInfo& info)
  273. {
  274. switch (info.commandID)
  275. {
  276. case CommandIDs::newProject: createNewProject(); break;
  277. case CommandIDs::open: askUserToOpenFile(); break;
  278. case CommandIDs::showPrefs: showPrefsPanel(); break;
  279. case CommandIDs::saveAll: openDocumentManager.saveAll(); break;
  280. case CommandIDs::closeAllDocuments: closeAllDocuments (true); break;
  281. case CommandIDs::showUTF8Tool: showUTF8ToolWindow(); break;
  282. case CommandIDs::showAppearanceSettings: showAppearanceEditorWindow(); break;
  283. case CommandIDs::updateModules: runModuleUpdate (String::empty); break;
  284. default:
  285. return JUCEApplication::perform (info);
  286. }
  287. return true;
  288. }
  289. //==============================================================================
  290. void showPrefsPanel()
  291. {
  292. jassertfalse;
  293. }
  294. void createNewProject()
  295. {
  296. if (makeSureUserHasSelectedModuleFolder())
  297. {
  298. MainWindow* mw = mainWindowList.getOrCreateEmptyWindow();
  299. mw->showNewProjectWizard();
  300. mainWindowList.avoidSuperimposedWindows (mw);
  301. }
  302. }
  303. void askUserToOpenFile()
  304. {
  305. FileChooser fc ("Open File");
  306. if (fc.browseForFileToOpen())
  307. openFile (fc.getResult());
  308. }
  309. bool openFile (const File& file)
  310. {
  311. return mainWindowList.openFile (file);
  312. }
  313. bool closeAllDocuments (bool askUserToSave)
  314. {
  315. return openDocumentManager.closeAll (askUserToSave);
  316. }
  317. bool makeSureUserHasSelectedModuleFolder()
  318. {
  319. if (! ModuleList::isLocalModulesFolderValid())
  320. {
  321. if (! runModuleUpdate ("Please select a location to store your local set of JUCE modules,\n"
  322. "and download the ones that you'd like to use!"))
  323. {
  324. AlertWindow::showMessageBox (AlertWindow::WarningIcon,
  325. "Introjucer",
  326. "Unless you create a local JUCE folder containing some modules, you'll be unable to save any projects correctly!\n\n"
  327. "Use the option on the 'Tools' menu to set this up!");
  328. return false;
  329. }
  330. }
  331. return true;
  332. }
  333. bool runModuleUpdate (const String& message)
  334. {
  335. ModuleList list;
  336. list.rescan (ModuleList::getDefaultModulesFolder (nullptr));
  337. JuceUpdater::show (list, mainWindowList.windows[0], message);
  338. ModuleList::setLocalModulesFolder (list.getModulesFolder());
  339. return ModuleList::isJuceOrModulesFolder (list.getModulesFolder());
  340. }
  341. void showAppearanceEditorWindow()
  342. {
  343. if (appearanceEditorWindow == nullptr)
  344. appearanceEditorWindow = AppearanceSettings::createEditorWindow();
  345. appearanceEditorWindow->toFront (true);
  346. }
  347. //==============================================================================
  348. virtual void doExtraInitialisation() {}
  349. virtual void addExtraConfigItems (Project&, TreeViewItem&) {}
  350. virtual Component* createProjectContentComponent() const
  351. {
  352. return new ProjectContentComponent();
  353. }
  354. //==============================================================================
  355. StoredSettings settings;
  356. Icons icons;
  357. ScopedPointer<MainMenuModel> menuModel;
  358. MainWindowList mainWindowList;
  359. OpenDocumentManager openDocumentManager;
  360. ScopedPointer<Component> appearanceEditorWindow;
  361. private:
  362. class AsyncQuitRetrier : public Timer
  363. {
  364. public:
  365. AsyncQuitRetrier() { startTimer (500); }
  366. void timerCallback()
  367. {
  368. stopTimer();
  369. delete this;
  370. if (getApp() != nullptr)
  371. getApp()->systemRequestedQuit();
  372. }
  373. JUCE_DECLARE_NON_COPYABLE (AsyncQuitRetrier);
  374. };
  375. };
  376. #endif // __JUCER_APPLICATION_JUCEHEADER__