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.

306 lines
12KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library - "Jules' Utility Class Extensions"
  4. Copyright 2004-9 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. #include "juce_ApplicationCommandTarget.h"
  21. #include "../events/juce_ActionListener.h"
  22. #include "../threads/juce_InterProcessLock.h"
  23. //==============================================================================
  24. /**
  25. An instance of this class is used to specify initialisation and shutdown
  26. code for the application.
  27. An application that wants to run in the JUCE framework needs to declare a
  28. subclass of JUCEApplication 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 a suitable platform-specific
  31. main() function.
  32. e.g. @code
  33. class MyJUCEApp : public JUCEApplication
  34. {
  35. // NEVER put objects inside a JUCEApplication class - only use pointers to
  36. // objects, which you must create in the initialise() method.
  37. MyApplicationWindow* myMainWindow;
  38. public:
  39. MyJUCEApp()
  40. : myMainWindow (0)
  41. {
  42. // never create any Juce objects in the constructor - do all your initialisation
  43. // in the initialise() method.
  44. }
  45. ~MyJUCEApp()
  46. {
  47. // all your shutdown code must have already been done in the shutdown() method -
  48. // nothing should happen in this destructor.
  49. }
  50. void initialise (const String& commandLine)
  51. {
  52. myMainWindow = new MyApplicationWindow();
  53. myMainWindow->setBounds (100, 100, 400, 500);
  54. myMainWindow->setVisible (true);
  55. }
  56. void shutdown()
  57. {
  58. delete myMainWindow;
  59. }
  60. const String getApplicationName()
  61. {
  62. return T("Super JUCE-o-matic");
  63. }
  64. const String getApplicationVersion()
  65. {
  66. return T("1.0");
  67. }
  68. };
  69. // this creates wrapper code to actually launch the app properly.
  70. START_JUCE_APPLICATION (MyJUCEApp)
  71. @endcode
  72. Because this object will be created before Juce has properly initialised, you must
  73. NEVER add any member variable objects that will be automatically constructed. Likewise
  74. don't put ANY code in the constructor that could call Juce functions. Any objects that
  75. you want to add to the class must be pointers, which you should instantiate during the
  76. initialise() method, and delete in the shutdown() method.
  77. @see MessageManager, DeletedAtShutdown
  78. */
  79. class JUCE_API JUCEApplication : public ApplicationCommandTarget,
  80. private ActionListener
  81. {
  82. protected:
  83. //==============================================================================
  84. /** Constructs a JUCE app object.
  85. If subclasses implement a constructor or destructor, they shouldn't call any
  86. JUCE code in there - put your startup/shutdown code in initialise() and
  87. shutdown() instead.
  88. */
  89. JUCEApplication();
  90. public:
  91. /** Destructor.
  92. If subclasses implement a constructor or destructor, they shouldn't call any
  93. JUCE code in there - put your startup/shutdown code in initialise() and
  94. shutdown() instead.
  95. */
  96. virtual ~JUCEApplication();
  97. //==============================================================================
  98. /** Returns the global instance of the application object being run. */
  99. static JUCEApplication* getInstance() throw();
  100. //==============================================================================
  101. /** Called when the application starts.
  102. This will be called once to let the application do whatever initialisation
  103. it needs, create its windows, etc.
  104. After the method returns, the normal event-dispatch loop will be run,
  105. until the quit() method is called, at which point the shutdown()
  106. method will be called to let the application clear up anything it needs
  107. to delete.
  108. If during the initialise() method, the application decides not to start-up
  109. after all, it can just call the quit() method and the event loop won't be run.
  110. @param commandLineParameters the line passed in does not include the
  111. name of the executable, just the parameter list.
  112. @see shutdown, quit
  113. */
  114. virtual void initialise (const String& commandLineParameters) = 0;
  115. /** Returns true if the application hasn't yet completed its initialise() method
  116. and entered the main event loop.
  117. This is handy for things like splash screens to know when the app's up-and-running
  118. properly.
  119. */
  120. bool isInitialising() const throw();
  121. /* Called to allow the application to clear up before exiting.
  122. After JUCEApplication::quit() has been called, the event-dispatch loop will
  123. terminate, and this method will get called to allow the app to sort itself
  124. out.
  125. Be careful that nothing happens in this method that might rely on messages
  126. being sent, or any kind of window activity, because the message loop is no
  127. longer running at this point.
  128. @see DeletedAtShutdown
  129. */
  130. virtual void shutdown() = 0;
  131. //==============================================================================
  132. /** Returns the application's name.
  133. An application must implement this to name itself.
  134. */
  135. virtual const String getApplicationName() = 0;
  136. /** Returns the application's version number.
  137. An application can implement this to give itself a version.
  138. (The default implementation of this just returns an empty string).
  139. */
  140. virtual const String getApplicationVersion();
  141. /** Checks whether multiple instances of the app are allowed.
  142. If you application class returns true for this, more than one instance is
  143. permitted to run (except on the Mac where this isn't possible).
  144. If it's false, the second instance won't start, but it you will still get a
  145. callback to anotherInstanceStarted() to tell you about this - which
  146. gives you a chance to react to what the user was trying to do.
  147. */
  148. virtual bool moreThanOneInstanceAllowed();
  149. /** Indicates that the user has tried to start up another instance of the app.
  150. This will get called even if moreThanOneInstanceAllowed() is false.
  151. */
  152. virtual void anotherInstanceStarted (const String& commandLine);
  153. /** Called when the operating system is trying to close the application.
  154. The default implementation of this method is to call quit(), but it may
  155. be overloaded to ignore the request or do some other special behaviour
  156. instead. For example, you might want to offer the user the chance to save
  157. their changes before quitting, and give them the chance to cancel.
  158. If you want to send a quit signal to your app, this is the correct method
  159. to call, because it means that requests that come from the system get handled
  160. in the same way as those from your own application code. So e.g. you'd
  161. call this method from a "quit" item on a menu bar.
  162. */
  163. virtual void systemRequestedQuit();
  164. /** If any unhandled exceptions make it through to the message dispatch loop, this
  165. callback will be triggered, in case you want to log them or do some other
  166. type of error-handling.
  167. If the type of exception is derived from the std::exception class, the pointer
  168. passed-in will be valid. If the exception is of unknown type, this pointer
  169. will be null.
  170. */
  171. virtual void unhandledException (const std::exception* e,
  172. const String& sourceFilename,
  173. const int lineNumber);
  174. //==============================================================================
  175. /** Signals that the main message loop should stop and the application should terminate.
  176. This isn't synchronous, it just posts a quit message to the main queue, and
  177. when this message arrives, the message loop will stop, the shutdown() method
  178. will be called, and the app will exit.
  179. Note that this will cause an unconditional quit to happen, so if you need an
  180. extra level before this, e.g. to give the user the chance to save their work
  181. and maybe cancel the quit, you'll need to handle this in the systemRequestedQuit()
  182. method - see that method's help for more info.
  183. @see MessageManager, DeletedAtShutdown
  184. */
  185. static void quit();
  186. /** Sets the value that should be returned as the application's exit code when the
  187. app quits.
  188. This is the value that's returned by the main() function. Normally you'd leave this
  189. as 0 unless you want to indicate an error code.
  190. @see getApplicationReturnValue
  191. */
  192. void setApplicationReturnValue (const int newReturnValue) throw();
  193. /** Returns the value that has been set as the application's exit code.
  194. @see setApplicationReturnValue
  195. */
  196. int getApplicationReturnValue() const throw() { return appReturnValue; }
  197. /** Returns the application's command line params.
  198. */
  199. const String getCommandLineParameters() const throw() { return commandLineParameters; }
  200. //==============================================================================
  201. // These are used by the START_JUCE_APPLICATION() macro and aren't for public use.
  202. /** @internal */
  203. static int main (String& commandLine, JUCEApplication* const newApp);
  204. /** @internal */
  205. static int main (int argc, const char* argv[], JUCEApplication* const newApp);
  206. /** @internal */
  207. static void sendUnhandledException (const std::exception* const e,
  208. const char* const sourceFile,
  209. const int lineNumber);
  210. //==============================================================================
  211. /** @internal */
  212. ApplicationCommandTarget* getNextCommandTarget();
  213. /** @internal */
  214. void getCommandInfo (const CommandID commandID, ApplicationCommandInfo& result);
  215. /** @internal */
  216. void getAllCommands (Array <CommandID>& commands);
  217. /** @internal */
  218. bool perform (const InvocationInfo& info);
  219. /** @internal */
  220. void actionListenerCallback (const String& message);
  221. private:
  222. //==============================================================================
  223. String commandLineParameters;
  224. int appReturnValue;
  225. bool stillInitialising;
  226. ScopedPointer<InterProcessLock> appLock;
  227. JUCEApplication (const JUCEApplication&);
  228. JUCEApplication& operator= (const JUCEApplication&);
  229. public:
  230. /** @internal */
  231. bool initialiseApp (String& commandLine);
  232. /** @internal */
  233. static int shutdownAppAndClearUp();
  234. };
  235. #endif // __JUCE_APPLICATION_JUCEHEADER__