PageRenderTime 49ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/JuceLibraryCode/modules/juce_gui_basics/application/juce_Application.cpp

https://bitbucket.org/teamaxe/markingtool
C++ | 303 lines | 217 code | 52 blank | 34 comment | 25 complexity | 265df787a2f184fe5a3a06e9546a377a MD5 | raw file
  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. #if JUCE_MAC
  19. extern void juce_initialiseMacMainMenu();
  20. #endif
  21. #if ! (JUCE_IOS || JUCE_ANDROID)
  22. #define JUCE_HANDLE_MULTIPLE_INSTANCES 1
  23. #endif
  24. //==============================================================================
  25. #if JUCE_HANDLE_MULTIPLE_INSTANCES
  26. struct JUCEApplication::MultipleInstanceHandler : public ActionListener
  27. {
  28. public:
  29. MultipleInstanceHandler (const String& appName)
  30. : appLock ("juceAppLock_" + appName)
  31. {
  32. }
  33. bool sendCommandLineToPreexistingInstance()
  34. {
  35. if (appLock.enter (0))
  36. return false;
  37. JUCEApplication* const app = JUCEApplication::getInstance();
  38. jassert (app != nullptr);
  39. MessageManager::broadcastMessage (app->getApplicationName()
  40. + "/" + app->getCommandLineParameters());
  41. return true;
  42. }
  43. void actionListenerCallback (const String& message)
  44. {
  45. if (JUCEApplication* const app = JUCEApplication::getInstance())
  46. {
  47. const String appName (app->getApplicationName());
  48. if (message.startsWith (appName + "/"))
  49. app->anotherInstanceStarted (message.substring (appName.length() + 1));
  50. }
  51. }
  52. private:
  53. InterProcessLock appLock;
  54. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (MultipleInstanceHandler)
  55. };
  56. #else
  57. struct JUCEApplication::MultipleInstanceHandler {};
  58. #endif
  59. //==============================================================================
  60. JUCEApplication::JUCEApplication()
  61. : appReturnValue (0),
  62. stillInitialising (true)
  63. {
  64. }
  65. JUCEApplication::~JUCEApplication()
  66. {
  67. }
  68. //==============================================================================
  69. bool JUCEApplication::moreThanOneInstanceAllowed() { return true; }
  70. void JUCEApplication::anotherInstanceStarted (const String&) {}
  71. void JUCEApplication::suspended() {}
  72. void JUCEApplication::resumed() {}
  73. void JUCEApplication::systemRequestedQuit()
  74. {
  75. quit();
  76. }
  77. void JUCEApplication::quit()
  78. {
  79. MessageManager::getInstance()->stopDispatchLoop();
  80. }
  81. void JUCEApplication::setApplicationReturnValue (const int newReturnValue) noexcept
  82. {
  83. appReturnValue = newReturnValue;
  84. }
  85. //==============================================================================
  86. void JUCEApplication::unhandledException (const std::exception*, const String&, int)
  87. {
  88. jassertfalse;
  89. }
  90. void JUCEApplication::sendUnhandledException (const std::exception* const e,
  91. const char* const sourceFile,
  92. const int lineNumber)
  93. {
  94. if (JUCEApplicationBase* const app = JUCEApplicationBase::getInstance())
  95. app->unhandledException (e, sourceFile, lineNumber);
  96. }
  97. //==============================================================================
  98. ApplicationCommandTarget* JUCEApplication::getNextCommandTarget()
  99. {
  100. return nullptr;
  101. }
  102. void JUCEApplication::getAllCommands (Array <CommandID>& commands)
  103. {
  104. commands.add (StandardApplicationCommandIDs::quit);
  105. }
  106. void JUCEApplication::getCommandInfo (const CommandID commandID, ApplicationCommandInfo& result)
  107. {
  108. if (commandID == StandardApplicationCommandIDs::quit)
  109. {
  110. result.setInfo (TRANS("Quit"),
  111. TRANS("Quits the application"),
  112. "Application",
  113. 0);
  114. result.defaultKeypresses.add (KeyPress ('q', ModifierKeys::commandModifier, 0));
  115. }
  116. }
  117. bool JUCEApplication::perform (const InvocationInfo& info)
  118. {
  119. if (info.commandID == StandardApplicationCommandIDs::quit)
  120. {
  121. systemRequestedQuit();
  122. return true;
  123. }
  124. return false;
  125. }
  126. #if JUCE_HANDLE_MULTIPLE_INSTANCES
  127. bool JUCEApplication::sendCommandLineToPreexistingInstance()
  128. {
  129. jassert (multipleInstanceHandler == nullptr); // this must only be called once!
  130. multipleInstanceHandler = new MultipleInstanceHandler (getApplicationName());
  131. return multipleInstanceHandler->sendCommandLineToPreexistingInstance();
  132. }
  133. #endif
  134. //==============================================================================
  135. bool JUCEApplication::initialiseApp()
  136. {
  137. #if JUCE_HANDLE_MULTIPLE_INSTANCES
  138. if ((! moreThanOneInstanceAllowed()) && sendCommandLineToPreexistingInstance())
  139. {
  140. DBG ("Another instance is running - quitting...");
  141. return false;
  142. }
  143. #endif
  144. // let the app do its setting-up..
  145. initialise (getCommandLineParameters());
  146. stillInitialising = false;
  147. if (! MessageManager::getInstance()->hasStopMessageBeenSent())
  148. {
  149. #if JUCE_MAC
  150. juce_initialiseMacMainMenu(); // (needs to get the app's name)
  151. #endif
  152. #if JUCE_HANDLE_MULTIPLE_INSTANCES
  153. if (multipleInstanceHandler != nullptr)
  154. MessageManager::getInstance()->registerBroadcastListener (multipleInstanceHandler);
  155. #endif
  156. }
  157. return true;
  158. }
  159. int JUCEApplication::shutdownApp()
  160. {
  161. jassert (JUCEApplicationBase::getInstance() == this);
  162. #if JUCE_HANDLE_MULTIPLE_INSTANCES
  163. if (multipleInstanceHandler != nullptr)
  164. MessageManager::getInstance()->deregisterBroadcastListener (multipleInstanceHandler);
  165. #endif
  166. JUCE_TRY
  167. {
  168. // give the app a chance to clean up..
  169. shutdown();
  170. }
  171. JUCE_CATCH_EXCEPTION
  172. multipleInstanceHandler = nullptr;
  173. return getApplicationReturnValue();
  174. }
  175. //==============================================================================
  176. #if JUCE_ANDROID
  177. StringArray JUCEApplication::getCommandLineParameterArray() { return StringArray(); }
  178. String JUCEApplication::getCommandLineParameters() { return String::empty; }
  179. #else
  180. int JUCEApplication::main()
  181. {
  182. ScopedJuceInitialiser_GUI libraryInitialiser;
  183. jassert (createInstance != nullptr);
  184. const ScopedPointer<JUCEApplication> app (dynamic_cast <JUCEApplication*> (createInstance()));
  185. jassert (app != nullptr);
  186. if (! app->initialiseApp())
  187. return 0;
  188. JUCE_TRY
  189. {
  190. // loop until a quit message is received..
  191. MessageManager::getInstance()->runDispatchLoop();
  192. }
  193. JUCE_CATCH_EXCEPTION
  194. return app->shutdownApp();
  195. }
  196. #if ! JUCE_WINDOWS
  197. #if JUCE_IOS
  198. extern int juce_iOSMain (int argc, const char* argv[]);
  199. #endif
  200. #if JUCE_MAC
  201. extern void initialiseNSApplication();
  202. #endif
  203. extern const char** juce_argv; // declared in juce_core
  204. extern int juce_argc;
  205. StringArray JUCEApplication::getCommandLineParameterArray()
  206. {
  207. return StringArray (juce_argv + 1, juce_argc - 1);
  208. }
  209. String JUCEApplication::getCommandLineParameters()
  210. {
  211. String argString;
  212. for (int i = 1; i < juce_argc; ++i)
  213. {
  214. String arg (juce_argv[i]);
  215. if (arg.containsChar (' ') && ! arg.isQuotedString())
  216. arg = arg.quoted ('"');
  217. argString << arg << ' ';
  218. }
  219. return argString.trim();
  220. }
  221. int JUCEApplication::main (int argc, const char* argv[])
  222. {
  223. JUCE_AUTORELEASEPOOL
  224. {
  225. juce_argc = argc;
  226. juce_argv = argv;
  227. #if JUCE_MAC
  228. initialiseNSApplication();
  229. #endif
  230. #if JUCE_IOS
  231. return juce_iOSMain (argc, argv);
  232. #else
  233. return JUCEApplication::main();
  234. #endif
  235. }
  236. }
  237. #endif
  238. #endif