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.

268 lines
10KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2013 - Raw Material Software Ltd.
  5. Permission is granted to use this software under the terms of either:
  6. a) the GPL v2 (or any later version)
  7. b) the Affero GPL v3
  8. Details of these licenses can be found at: www.gnu.org/licenses
  9. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  11. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  12. ------------------------------------------------------------------------------
  13. To release a closed-source product which uses JUCE, commercial licenses are
  14. available: visit www.juce.com for more information.
  15. ==============================================================================
  16. */
  17. #ifndef __JUCE_APPLICATION_JUCEHEADER__
  18. #define __JUCE_APPLICATION_JUCEHEADER__
  19. //==============================================================================
  20. /**
  21. An instance of this class is used to specify initialisation and shutdown
  22. code for the application.
  23. An application that wants to run in the JUCE framework needs to declare a
  24. subclass of JUCEApplication and implement its various pure virtual methods.
  25. It then needs to use the START_JUCE_APPLICATION macro somewhere in a cpp file
  26. to declare an instance of this class and generate a suitable platform-specific
  27. main() function.
  28. e.g. @code
  29. class MyJUCEApp : public JUCEApplication
  30. {
  31. public:
  32. MyJUCEApp()
  33. {
  34. }
  35. ~MyJUCEApp()
  36. {
  37. }
  38. void initialise (const String& commandLine)
  39. {
  40. myMainWindow = new MyApplicationWindow();
  41. myMainWindow->setBounds (100, 100, 400, 500);
  42. myMainWindow->setVisible (true);
  43. }
  44. void shutdown()
  45. {
  46. myMainWindow = 0;
  47. }
  48. const String getApplicationName()
  49. {
  50. return "Super JUCE-o-matic";
  51. }
  52. const String getApplicationVersion()
  53. {
  54. return "1.0";
  55. }
  56. private:
  57. ScopedPointer <MyApplicationWindow> myMainWindow;
  58. };
  59. // this creates wrapper code to actually launch the app properly.
  60. START_JUCE_APPLICATION (MyJUCEApp)
  61. @endcode
  62. @see MessageManager
  63. */
  64. class JUCE_API JUCEApplication : public JUCEApplicationBase,
  65. public ApplicationCommandTarget
  66. {
  67. protected:
  68. //==============================================================================
  69. /** Constructs a JUCE app object.
  70. If subclasses implement a constructor or destructor, they shouldn't call any
  71. JUCE code in there - put your startup/shutdown code in initialise() and
  72. shutdown() instead.
  73. */
  74. JUCEApplication();
  75. public:
  76. /** Destructor.
  77. If subclasses implement a constructor or destructor, they shouldn't call any
  78. JUCE code in there - put your startup/shutdown code in initialise() and
  79. shutdown() instead.
  80. */
  81. virtual ~JUCEApplication();
  82. //==============================================================================
  83. /** Returns the global instance of the application object being run. */
  84. static JUCEApplication* getInstance() noexcept { return dynamic_cast <JUCEApplication*> (JUCEApplicationBase::getInstance()); }
  85. //==============================================================================
  86. /** Returns true if the application hasn't yet completed its initialise() method
  87. and entered the main event loop.
  88. This is handy for things like splash screens to know when the app's up-and-running
  89. properly.
  90. */
  91. bool isInitialising() const noexcept { return stillInitialising; }
  92. //==============================================================================
  93. /** Returns the application's name.
  94. An application must implement this to name itself.
  95. */
  96. virtual const String getApplicationName() = 0;
  97. /** Returns the application's version number.
  98. */
  99. virtual const String getApplicationVersion() = 0;
  100. /** Checks whether multiple instances of the app are allowed.
  101. If you application class returns true for this, more than one instance is
  102. permitted to run (except on OSX where the OS automatically stops you launching
  103. a second instance of an app without explicitly starting it from the command-line).
  104. If it's false, the second instance won't start, but it you will still get a
  105. callback to anotherInstanceStarted() to tell you about this - which
  106. gives you a chance to react to what the user was trying to do.
  107. */
  108. virtual bool moreThanOneInstanceAllowed();
  109. /** Indicates that the user has tried to start up another instance of the app.
  110. This will get called even if moreThanOneInstanceAllowed() is false.
  111. */
  112. virtual void anotherInstanceStarted (const String& commandLine);
  113. /** Called when the operating system is trying to close the application.
  114. The default implementation of this method is to call quit(), but it may
  115. be overloaded to ignore the request or do some other special behaviour
  116. instead. For example, you might want to offer the user the chance to save
  117. their changes before quitting, and give them the chance to cancel.
  118. If you want to send a quit signal to your app, this is the correct method
  119. to call, because it means that requests that come from the system get handled
  120. in the same way as those from your own application code. So e.g. you'd
  121. call this method from a "quit" item on a menu bar.
  122. */
  123. virtual void systemRequestedQuit();
  124. /** This method is called when the application is being put into background mode
  125. by the operating system.
  126. */
  127. virtual void suspended();
  128. /** This method is called when the application is being woken from background mode
  129. by the operating system.
  130. */
  131. virtual void resumed();
  132. /** If any unhandled exceptions make it through to the message dispatch loop, this
  133. callback will be triggered, in case you want to log them or do some other
  134. type of error-handling.
  135. If the type of exception is derived from the std::exception class, the pointer
  136. passed-in will be valid. If the exception is of unknown type, this pointer
  137. will be null.
  138. */
  139. virtual void unhandledException (const std::exception* e,
  140. const String& sourceFilename,
  141. int lineNumber);
  142. //==============================================================================
  143. /** Signals that the main message loop should stop and the application should terminate.
  144. This isn't synchronous, it just posts a quit message to the main queue, and
  145. when this message arrives, the message loop will stop, the shutdown() method
  146. will be called, and the app will exit.
  147. Note that this will cause an unconditional quit to happen, so if you need an
  148. extra level before this, e.g. to give the user the chance to save their work
  149. and maybe cancel the quit, you'll need to handle this in the systemRequestedQuit()
  150. method - see that method's help for more info.
  151. @see MessageManager
  152. */
  153. static void quit();
  154. /** Sets the value that should be returned as the application's exit code when the
  155. app quits.
  156. This is the value that's returned by the main() function. Normally you'd leave this
  157. as 0 unless you want to indicate an error code.
  158. @see getApplicationReturnValue
  159. */
  160. void setApplicationReturnValue (int newReturnValue) noexcept;
  161. /** Returns the value that has been set as the application's exit code.
  162. @see setApplicationReturnValue
  163. */
  164. int getApplicationReturnValue() const noexcept { return appReturnValue; }
  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. /** Returns true if this executable is running as an app (as opposed to being a plugin
  174. or other kind of shared library. */
  175. static inline bool isStandaloneApp() noexcept { return createInstance != nullptr; }
  176. //==============================================================================
  177. /** @internal */
  178. ApplicationCommandTarget* getNextCommandTarget();
  179. /** @internal */
  180. void getCommandInfo (CommandID, ApplicationCommandInfo&);
  181. /** @internal */
  182. void getAllCommands (Array <CommandID>&);
  183. /** @internal */
  184. bool perform (const InvocationInfo&);
  185. //==============================================================================
  186. #ifndef DOXYGEN
  187. // The following methods are internal calls - not for public use.
  188. static int main();
  189. static int main (int argc, const char* argv[]);
  190. static void sendUnhandledException (const std::exception*, const char* sourceFile, int lineNumber);
  191. bool initialiseApp();
  192. int shutdownApp();
  193. protected:
  194. bool sendCommandLineToPreexistingInstance();
  195. #endif
  196. private:
  197. //==============================================================================
  198. struct MultipleInstanceHandler;
  199. friend struct MultipleInstanceHandler;
  200. friend class ScopedPointer<MultipleInstanceHandler>;
  201. ScopedPointer<MultipleInstanceHandler> multipleInstanceHandler;
  202. int appReturnValue;
  203. bool stillInitialising;
  204. JUCE_DECLARE_NON_COPYABLE (JUCEApplication)
  205. };
  206. #endif // __JUCE_APPLICATION_JUCEHEADER__