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.

269 lines
10KB

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