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.

313 lines
9.6KB

  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. } // (juce namespace)
  19. @interface JuceAppStartupDelegate : NSObject <UIApplicationDelegate>
  20. {
  21. }
  22. - (void) applicationDidFinishLaunching: (UIApplication*) application;
  23. - (void) applicationWillTerminate: (UIApplication*) application;
  24. - (void) applicationDidEnterBackground: (UIApplication*) application;
  25. - (void) applicationWillEnterForeground: (UIApplication*) application;
  26. @end
  27. @implementation JuceAppStartupDelegate
  28. - (void) applicationDidFinishLaunching: (UIApplication*) application
  29. {
  30. initialiseJuce_GUI();
  31. JUCEApplication* app = dynamic_cast <JUCEApplication*> (JUCEApplicationBase::createInstance());
  32. if (! app->initialiseApp())
  33. exit (0);
  34. }
  35. - (void) applicationWillTerminate: (UIApplication*) application
  36. {
  37. JUCEApplicationBase::appWillTerminateByForce();
  38. }
  39. - (void) applicationDidEnterBackground: (UIApplication*) application
  40. {
  41. JUCEApplicationBase* const app = JUCEApplicationBase::getInstance();
  42. if (app != nullptr)
  43. app->suspended();
  44. }
  45. - (void) applicationWillEnterForeground: (UIApplication*) application
  46. {
  47. JUCEApplicationBase* const app = JUCEApplicationBase::getInstance();
  48. if (app != nullptr)
  49. app->resumed();
  50. }
  51. @end
  52. namespace juce
  53. {
  54. int juce_iOSMain (int argc, const char* argv[])
  55. {
  56. return UIApplicationMain (argc, const_cast<char**> (argv), nil, @"JuceAppStartupDelegate");
  57. }
  58. //==============================================================================
  59. void LookAndFeel::playAlertSound()
  60. {
  61. //xxx
  62. //AudioServicesPlaySystemSound ();
  63. }
  64. //==============================================================================
  65. class iOSMessageBox;
  66. } // (juce namespace)
  67. @interface JuceAlertBoxDelegate : NSObject
  68. {
  69. @public
  70. iOSMessageBox* owner;
  71. }
  72. - (void) alertView: (UIAlertView*) alertView clickedButtonAtIndex: (NSInteger) buttonIndex;
  73. @end
  74. namespace juce
  75. {
  76. class iOSMessageBox
  77. {
  78. public:
  79. iOSMessageBox (const String& title, const String& message,
  80. NSString* button1, NSString* button2, NSString* button3,
  81. ModalComponentManager::Callback* callback_, const bool isAsync_)
  82. : result (0), delegate (nil), alert (nil),
  83. callback (callback_), isYesNo (button3 != nil), isAsync (isAsync_)
  84. {
  85. delegate = [[JuceAlertBoxDelegate alloc] init];
  86. delegate->owner = this;
  87. alert = [[UIAlertView alloc] initWithTitle: juceStringToNS (title)
  88. message: juceStringToNS (message)
  89. delegate: delegate
  90. cancelButtonTitle: button1
  91. otherButtonTitles: button2, button3, nil];
  92. [alert retain];
  93. [alert show];
  94. }
  95. ~iOSMessageBox()
  96. {
  97. [alert release];
  98. [delegate release];
  99. }
  100. int getResult()
  101. {
  102. jassert (callback == nullptr);
  103. JUCE_AUTORELEASEPOOL
  104. while (! alert.hidden && alert.superview != nil)
  105. [[NSRunLoop mainRunLoop] runUntilDate: [NSDate dateWithTimeIntervalSinceNow: 0.01]];
  106. return result;
  107. }
  108. void buttonClicked (const int buttonIndex) noexcept
  109. {
  110. result = buttonIndex;
  111. if (callback != nullptr)
  112. callback->modalStateFinished (result);
  113. if (isAsync)
  114. delete this;
  115. }
  116. private:
  117. int result;
  118. JuceAlertBoxDelegate* delegate;
  119. UIAlertView* alert;
  120. ModalComponentManager::Callback* callback;
  121. const bool isYesNo, isAsync;
  122. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (iOSMessageBox);
  123. };
  124. } // (juce namespace)
  125. @implementation JuceAlertBoxDelegate
  126. - (void) alertView: (UIAlertView*) alertView clickedButtonAtIndex: (NSInteger) buttonIndex
  127. {
  128. owner->buttonClicked (buttonIndex);
  129. alertView.hidden = true;
  130. }
  131. @end
  132. namespace juce
  133. {
  134. //==============================================================================
  135. void JUCE_CALLTYPE NativeMessageBox::showMessageBox (AlertWindow::AlertIconType iconType,
  136. const String& title, const String& message,
  137. Component* associatedComponent)
  138. {
  139. JUCE_AUTORELEASEPOOL
  140. iOSMessageBox mb (title, message, @"OK", nil, nil, 0, false);
  141. (void) mb.getResult();
  142. }
  143. void JUCE_CALLTYPE NativeMessageBox::showMessageBoxAsync (AlertWindow::AlertIconType iconType,
  144. const String& title, const String& message,
  145. Component* associatedComponent)
  146. {
  147. JUCE_AUTORELEASEPOOL
  148. new iOSMessageBox (title, message, @"OK", nil, nil, 0, true);
  149. }
  150. bool JUCE_CALLTYPE NativeMessageBox::showOkCancelBox (AlertWindow::AlertIconType iconType,
  151. const String& title, const String& message,
  152. Component* associatedComponent,
  153. ModalComponentManager::Callback* callback)
  154. {
  155. ScopedPointer<iOSMessageBox> mb (new iOSMessageBox (title, message, @"Cancel", @"OK", nil, callback, callback != nullptr));
  156. if (callback == nullptr)
  157. return mb->getResult() == 1;
  158. mb.release();
  159. return false;
  160. }
  161. int JUCE_CALLTYPE NativeMessageBox::showYesNoCancelBox (AlertWindow::AlertIconType iconType,
  162. const String& title, const String& message,
  163. Component* associatedComponent,
  164. ModalComponentManager::Callback* callback)
  165. {
  166. ScopedPointer<iOSMessageBox> mb (new iOSMessageBox (title, message, @"Cancel", @"Yes", @"No", callback, callback != nullptr));
  167. if (callback == nullptr)
  168. return mb->getResult();
  169. mb.release();
  170. return 0;
  171. }
  172. //==============================================================================
  173. bool DragAndDropContainer::performExternalDragDropOfFiles (const StringArray& files, const bool canMoveFiles)
  174. {
  175. jassertfalse; // no such thing on the iphone!
  176. return false;
  177. }
  178. bool DragAndDropContainer::performExternalDragDropOfText (const String& text)
  179. {
  180. jassertfalse; // no such thing on the iphone!
  181. return false;
  182. }
  183. //==============================================================================
  184. void Desktop::setScreenSaverEnabled (const bool isEnabled)
  185. {
  186. [[UIApplication sharedApplication] setIdleTimerDisabled: ! isEnabled];
  187. }
  188. bool Desktop::isScreenSaverEnabled()
  189. {
  190. return ! [[UIApplication sharedApplication] isIdleTimerDisabled];
  191. }
  192. //==============================================================================
  193. Image juce_createIconForFile (const File& file)
  194. {
  195. return Image::null;
  196. }
  197. //==============================================================================
  198. void SystemClipboard::copyTextToClipboard (const String& text)
  199. {
  200. [[UIPasteboard generalPasteboard] setValue: juceStringToNS (text)
  201. forPasteboardType: @"public.text"];
  202. }
  203. String SystemClipboard::getTextFromClipboard()
  204. {
  205. NSString* text = [[UIPasteboard generalPasteboard] valueForPasteboardType: @"public.text"];
  206. return text == nil ? String::empty
  207. : nsStringToJuce (text);
  208. }
  209. //==============================================================================
  210. void Desktop::createMouseInputSources()
  211. {
  212. for (int i = 0; i < 10; ++i)
  213. mouseSources.add (new MouseInputSource (i, false));
  214. }
  215. bool Desktop::canUseSemiTransparentWindows() noexcept
  216. {
  217. return true;
  218. }
  219. Point<int> MouseInputSource::getCurrentMousePosition()
  220. {
  221. return juce_lastMousePos;
  222. }
  223. void Desktop::setMousePosition (const Point<int>&)
  224. {
  225. }
  226. Desktop::DisplayOrientation Desktop::getCurrentOrientation() const
  227. {
  228. return convertToJuceOrientation ([[UIApplication sharedApplication] statusBarOrientation]);
  229. }
  230. void Desktop::Displays::findDisplays()
  231. {
  232. JUCE_AUTORELEASEPOOL
  233. UIScreen* s = [UIScreen mainScreen];
  234. Display d;
  235. d.userArea = UIViewComponentPeer::realScreenPosToRotated (convertToRectInt ([s applicationFrame]));
  236. d.totalArea = UIViewComponentPeer::realScreenPosToRotated (convertToRectInt ([s bounds]));
  237. d.isMain = true;
  238. if ([s respondsToSelector: @selector (scale)])
  239. d.scale = s.scale;
  240. else
  241. d.scale = 1.0;
  242. displays.add (d);
  243. }