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.

311 lines
13KB

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