Audio plugin host https://kx.studio/carla
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.

juce_ApplicationBase.h 13KB

7 years ago
10 years ago
10 years ago
10 years ago
10 years ago
7 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2017 - ROLI Ltd.
  5. JUCE is an open source library subject to commercial or open-source
  6. licensing.
  7. The code included in this file is provided under the terms of the ISC license
  8. http://www.isc.org/downloads/software-support-policy/isc-license. Permission
  9. To use, copy, modify, and/or distribute this software for any purpose with or
  10. without fee is hereby granted provided that the above copyright notice and
  11. this permission notice appear in all copies.
  12. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  13. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  14. DISCLAIMED.
  15. ==============================================================================
  16. */
  17. namespace juce
  18. {
  19. //==============================================================================
  20. /**
  21. Abstract base class for application classes.
  22. Note that in the juce_gui_basics module, there's a utility class JUCEApplication
  23. which derives from JUCEApplicationBase, and takes care of a few chores. Most
  24. of the time you'll want to derive your class from JUCEApplication rather than
  25. using JUCEApplicationBase directly, but if you're not using the juce_gui_basics
  26. module then you might need to go straight to this base class.
  27. Any application that wants to run an event loop must declare a subclass of
  28. JUCEApplicationBase, and implement its various pure virtual methods.
  29. It then needs to use the START_JUCE_APPLICATION macro somewhere in a CPP file
  30. to declare an instance of this class and generate suitable platform-specific
  31. boilerplate code to launch the app.
  32. e.g. @code
  33. class MyJUCEApp : public JUCEApplication
  34. {
  35. public:
  36. MyJUCEApp() {}
  37. ~MyJUCEApp() {}
  38. void initialise (const String& commandLine) override
  39. {
  40. myMainWindow = new MyApplicationWindow();
  41. myMainWindow->setBounds (100, 100, 400, 500);
  42. myMainWindow->setVisible (true);
  43. }
  44. void shutdown() override
  45. {
  46. myMainWindow = nullptr;
  47. }
  48. const String getApplicationName() override
  49. {
  50. return "Super JUCE-o-matic";
  51. }
  52. const String getApplicationVersion() override
  53. {
  54. return "1.0";
  55. }
  56. private:
  57. ScopedPointer<MyApplicationWindow> myMainWindow;
  58. };
  59. // this generates boilerplate code to launch our app class:
  60. START_JUCE_APPLICATION (MyJUCEApp)
  61. @endcode
  62. @see JUCEApplication, START_JUCE_APPLICATION
  63. */
  64. class JUCE_API JUCEApplicationBase
  65. {
  66. protected:
  67. //==============================================================================
  68. JUCEApplicationBase();
  69. public:
  70. /** Destructor. */
  71. virtual ~JUCEApplicationBase();
  72. //==============================================================================
  73. /** Returns the global instance of the application object that's running. */
  74. static JUCEApplicationBase* getInstance() noexcept { return appInstance; }
  75. //==============================================================================
  76. /** Returns the application's name. */
  77. virtual const String getApplicationName() = 0;
  78. /** Returns the application's version number. */
  79. virtual const String getApplicationVersion() = 0;
  80. /** Checks whether multiple instances of the app are allowed.
  81. If you application class returns true for this, more than one instance is
  82. permitted to run (except on the Mac where this isn't possible).
  83. If it's false, the second instance won't start, but it you will still get a
  84. callback to anotherInstanceStarted() to tell you about this - which
  85. gives you a chance to react to what the user was trying to do.
  86. */
  87. virtual bool moreThanOneInstanceAllowed() = 0;
  88. /** Called when the application starts.
  89. This will be called once to let the application do whatever initialisation
  90. it needs, create its windows, etc.
  91. After the method returns, the normal event-dispatch loop will be run,
  92. until the quit() method is called, at which point the shutdown()
  93. method will be called to let the application clear up anything it needs
  94. to delete.
  95. If during the initialise() method, the application decides not to start-up
  96. after all, it can just call the quit() method and the event loop won't be run.
  97. @param commandLineParameters the line passed in does not include the name of
  98. the executable, just the parameter list. To get the
  99. parameters as an array, you can call
  100. JUCEApplication::getCommandLineParameters()
  101. @see shutdown, quit
  102. */
  103. virtual void initialise (const String& commandLineParameters) = 0;
  104. /* Called to allow the application to clear up before exiting.
  105. After JUCEApplication::quit() has been called, the event-dispatch loop will
  106. terminate, and this method will get called to allow the app to sort itself
  107. out.
  108. Be careful that nothing happens in this method that might rely on messages
  109. being sent, or any kind of window activity, because the message loop is no
  110. longer running at this point.
  111. @see DeletedAtShutdown
  112. */
  113. virtual void shutdown() = 0;
  114. /** Indicates that the user has tried to start up another instance of the app.
  115. This will get called even if moreThanOneInstanceAllowed() is false.
  116. */
  117. virtual void anotherInstanceStarted (const String& commandLine) = 0;
  118. /** Called when the operating system is trying to close the application.
  119. The default implementation of this method is to call quit(), but it may
  120. be overloaded to ignore the request or do some other special behaviour
  121. instead. For example, you might want to offer the user the chance to save
  122. their changes before quitting, and give them the chance to cancel.
  123. If you want to send a quit signal to your app, this is the correct method
  124. to call, because it means that requests that come from the system get handled
  125. in the same way as those from your own application code. So e.g. you'd
  126. call this method from a "quit" item on a menu bar.
  127. */
  128. virtual void systemRequestedQuit() = 0;
  129. /** This method is called when the application is being put into background mode
  130. by the operating system.
  131. */
  132. virtual void suspended() = 0;
  133. /** This method is called when the application is being woken from background mode
  134. by the operating system.
  135. */
  136. virtual void resumed() = 0;
  137. /** If any unhandled exceptions make it through to the message dispatch loop, this
  138. callback will be triggered, in case you want to log them or do some other
  139. type of error-handling.
  140. If the type of exception is derived from the std::exception class, the pointer
  141. passed-in will be valid. If the exception is of unknown type, this pointer
  142. will be null.
  143. */
  144. virtual void unhandledException (const std::exception*,
  145. const String& sourceFilename,
  146. int lineNumber) = 0;
  147. //==============================================================================
  148. /** Override this method to be informed when the back button is pressed on a device.
  149. This is currently only implemented on Android devices.
  150. */
  151. virtual void backButtonPressed() { }
  152. //==============================================================================
  153. /** Signals that the main message loop should stop and the application should terminate.
  154. This isn't synchronous, it just posts a quit message to the main queue, and
  155. when this message arrives, the message loop will stop, the shutdown() method
  156. will be called, and the app will exit.
  157. Note that this will cause an unconditional quit to happen, so if you need an
  158. extra level before this, e.g. to give the user the chance to save their work
  159. and maybe cancel the quit, you'll need to handle this in the systemRequestedQuit()
  160. method - see that method's help for more info.
  161. @see MessageManager
  162. */
  163. static void quit();
  164. //==============================================================================
  165. /** Returns the application's command line parameters as a set of strings.
  166. @see getCommandLineParameters
  167. */
  168. static StringArray JUCE_CALLTYPE getCommandLineParameterArray();
  169. /** Returns the application's command line parameters as a single string.
  170. @see getCommandLineParameterArray
  171. */
  172. static String JUCE_CALLTYPE getCommandLineParameters();
  173. //==============================================================================
  174. /** Sets the value that should be returned as the application's exit code when the
  175. app quits.
  176. This is the value that's returned by the main() function. Normally you'd leave this
  177. as 0 unless you want to indicate an error code.
  178. @see getApplicationReturnValue
  179. */
  180. void setApplicationReturnValue (int newReturnValue) noexcept;
  181. /** Returns the value that has been set as the application's exit code.
  182. @see setApplicationReturnValue
  183. */
  184. int getApplicationReturnValue() const noexcept { return appReturnValue; }
  185. //==============================================================================
  186. /** Returns true if this executable is running as an app (as opposed to being a plugin
  187. or other kind of shared library. */
  188. static bool isStandaloneApp() noexcept { return createInstance != nullptr; }
  189. /** Returns true if the application hasn't yet completed its initialise() method
  190. and entered the main event loop.
  191. This is handy for things like splash screens to know when the app's up-and-running
  192. properly.
  193. */
  194. bool isInitialising() const noexcept { return stillInitialising; }
  195. //==============================================================================
  196. #ifndef DOXYGEN
  197. // The following methods are for internal use only...
  198. static int main();
  199. static int main (int argc, const char* argv[]);
  200. static void appWillTerminateByForce();
  201. typedef JUCEApplicationBase* (*CreateInstanceFunction)();
  202. static CreateInstanceFunction createInstance;
  203. #if JUCE_IOS
  204. static void* iOSCustomDelegate;
  205. #endif
  206. virtual bool initialiseApp();
  207. int shutdownApp();
  208. static void JUCE_CALLTYPE sendUnhandledException (const std::exception*, const char* sourceFile, int lineNumber);
  209. bool sendCommandLineToPreexistingInstance();
  210. #endif
  211. private:
  212. //==============================================================================
  213. static JUCEApplicationBase* appInstance;
  214. int appReturnValue;
  215. bool stillInitialising;
  216. struct MultipleInstanceHandler;
  217. friend struct MultipleInstanceHandler;
  218. friend struct ContainerDeletePolicy<MultipleInstanceHandler>;
  219. ScopedPointer<MultipleInstanceHandler> multipleInstanceHandler;
  220. JUCE_DECLARE_NON_COPYABLE (JUCEApplicationBase)
  221. };
  222. //==============================================================================
  223. #if JUCE_CATCH_UNHANDLED_EXCEPTIONS || defined (DOXYGEN)
  224. /** The JUCE_TRY/JUCE_CATCH_EXCEPTION wrappers can be used to pass any uncaught exceptions to
  225. the JUCEApplicationBase::sendUnhandledException() method.
  226. This functionality can be enabled with the JUCE_CATCH_UNHANDLED_EXCEPTIONS macro.
  227. */
  228. #define JUCE_TRY try
  229. /** The JUCE_TRY/JUCE_CATCH_EXCEPTION wrappers can be used to pass any uncaught exceptions to
  230. the JUCEApplicationBase::sendUnhandledException() method.
  231. This functionality can be enabled with the JUCE_CATCH_UNHANDLED_EXCEPTIONS macro.
  232. */
  233. #define JUCE_CATCH_EXCEPTION \
  234. catch (const std::exception& e) { juce::JUCEApplicationBase::sendUnhandledException (&e, __FILE__, __LINE__); } \
  235. catch (...) { juce::JUCEApplicationBase::sendUnhandledException (nullptr, __FILE__, __LINE__); }
  236. #else
  237. #define JUCE_TRY
  238. #define JUCE_CATCH_EXCEPTION
  239. #endif
  240. } // namespace juce