Audio plugin host https://kx.studio/carla
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.

juce_ios_UIViewComponentPeer.mm 33KB

10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005
  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. class UIViewComponentPeer;
  18. namespace Orientations
  19. {
  20. static Desktop::DisplayOrientation convertToJuce (UIInterfaceOrientation orientation)
  21. {
  22. switch (orientation)
  23. {
  24. case UIInterfaceOrientationPortrait: return Desktop::upright;
  25. case UIInterfaceOrientationPortraitUpsideDown: return Desktop::upsideDown;
  26. case UIInterfaceOrientationLandscapeLeft: return Desktop::rotatedClockwise;
  27. case UIInterfaceOrientationLandscapeRight: return Desktop::rotatedAntiClockwise;
  28. default: jassertfalse; // unknown orientation!
  29. }
  30. return Desktop::upright;
  31. }
  32. static CGAffineTransform getCGTransformFor (const Desktop::DisplayOrientation orientation) noexcept
  33. {
  34. switch (orientation)
  35. {
  36. case Desktop::upsideDown: return CGAffineTransformMake (-1, 0, 0, -1, 0, 0);
  37. case Desktop::rotatedClockwise: return CGAffineTransformMake (0, -1, 1, 0, 0, 0);
  38. case Desktop::rotatedAntiClockwise: return CGAffineTransformMake (0, 1, -1, 0, 0, 0);
  39. default: break;
  40. }
  41. return CGAffineTransformIdentity;
  42. }
  43. static NSUInteger getSupportedOrientations()
  44. {
  45. NSUInteger allowed = 0;
  46. Desktop& d = Desktop::getInstance();
  47. if (d.isOrientationEnabled (Desktop::upright)) allowed |= UIInterfaceOrientationMaskPortrait;
  48. if (d.isOrientationEnabled (Desktop::upsideDown)) allowed |= UIInterfaceOrientationMaskPortraitUpsideDown;
  49. if (d.isOrientationEnabled (Desktop::rotatedClockwise)) allowed |= UIInterfaceOrientationMaskLandscapeLeft;
  50. if (d.isOrientationEnabled (Desktop::rotatedAntiClockwise)) allowed |= UIInterfaceOrientationMaskLandscapeRight;
  51. return allowed;
  52. }
  53. }
  54. //==============================================================================
  55. } // (juce namespace)
  56. using namespace juce;
  57. @interface JuceUIView : UIView <UITextViewDelegate>
  58. {
  59. @public
  60. UIViewComponentPeer* owner;
  61. UITextView* hiddenTextView;
  62. }
  63. - (JuceUIView*) initWithOwner: (UIViewComponentPeer*) owner withFrame: (CGRect) frame;
  64. - (void) dealloc;
  65. - (void) drawRect: (CGRect) r;
  66. - (void) touchesBegan: (NSSet*) touches withEvent: (UIEvent*) event;
  67. - (void) touchesMoved: (NSSet*) touches withEvent: (UIEvent*) event;
  68. - (void) touchesEnded: (NSSet*) touches withEvent: (UIEvent*) event;
  69. - (void) touchesCancelled: (NSSet*) touches withEvent: (UIEvent*) event;
  70. - (BOOL) becomeFirstResponder;
  71. - (BOOL) resignFirstResponder;
  72. - (BOOL) canBecomeFirstResponder;
  73. - (BOOL) textView: (UITextView*) textView shouldChangeTextInRange: (NSRange) range replacementText: (NSString*) text;
  74. @end
  75. //==============================================================================
  76. @interface JuceUIViewController : UIViewController
  77. {
  78. }
  79. - (NSUInteger) supportedInterfaceOrientations;
  80. - (BOOL) shouldAutorotateToInterfaceOrientation: (UIInterfaceOrientation) interfaceOrientation;
  81. - (void) willRotateToInterfaceOrientation: (UIInterfaceOrientation) toInterfaceOrientation duration: (NSTimeInterval) duration;
  82. - (void) didRotateFromInterfaceOrientation: (UIInterfaceOrientation) fromInterfaceOrientation;
  83. - (void) viewDidLoad;
  84. - (void) viewWillAppear: (BOOL) animated;
  85. - (void) viewDidAppear: (BOOL) animated;
  86. - (void) viewWillLayoutSubviews;
  87. - (void) viewDidLayoutSubviews;
  88. @end
  89. //==============================================================================
  90. @interface JuceUIWindow : UIWindow
  91. {
  92. @private
  93. UIViewComponentPeer* owner;
  94. }
  95. - (void) setOwner: (UIViewComponentPeer*) owner;
  96. - (void) becomeKeyWindow;
  97. @end
  98. //==============================================================================
  99. //==============================================================================
  100. namespace juce
  101. {
  102. class UIViewComponentPeer : public ComponentPeer,
  103. public FocusChangeListener
  104. {
  105. public:
  106. UIViewComponentPeer (Component&, int windowStyleFlags, UIView* viewToAttachTo);
  107. ~UIViewComponentPeer();
  108. //==============================================================================
  109. void* getNativeHandle() const override { return view; }
  110. void setVisible (bool shouldBeVisible) override;
  111. void setTitle (const String& title) override;
  112. void setBounds (const Rectangle<int>&, bool isNowFullScreen) override;
  113. Rectangle<int> getBounds() const override { return getBounds (! isSharedWindow); }
  114. Rectangle<int> getBounds (bool global) const;
  115. Point<float> localToGlobal (Point<float> relativePosition) override;
  116. Point<float> globalToLocal (Point<float> screenPosition) override;
  117. void setAlpha (float newAlpha) override;
  118. void setMinimised (bool) override {}
  119. bool isMinimised() const override { return false; }
  120. void setFullScreen (bool shouldBeFullScreen) override;
  121. bool isFullScreen() const override { return fullScreen; }
  122. bool contains (Point<int> localPos, bool trueIfInAChildWindow) const override;
  123. BorderSize<int> getFrameSize() const override { return BorderSize<int>(); }
  124. bool setAlwaysOnTop (bool alwaysOnTop) override;
  125. void toFront (bool makeActiveWindow) override;
  126. void toBehind (ComponentPeer* other) override;
  127. void setIcon (const Image& newIcon) override;
  128. StringArray getAvailableRenderingEngines() override { return StringArray ("CoreGraphics Renderer"); }
  129. void drawRect (CGRect);
  130. bool canBecomeKeyWindow();
  131. //==============================================================================
  132. void viewFocusGain();
  133. void viewFocusLoss();
  134. bool isFocused() const override;
  135. void grabFocus() override;
  136. void textInputRequired (Point<int>, TextInputTarget&) override;
  137. BOOL textViewReplaceCharacters (Range<int>, const String&);
  138. void updateHiddenTextContent (TextInputTarget*);
  139. void globalFocusChanged (Component*) override;
  140. void updateTransformAndScreenBounds();
  141. void handleTouches (UIEvent*, bool isDown, bool isUp, bool isCancel);
  142. //==============================================================================
  143. void repaint (const Rectangle<int>& area) override;
  144. void performAnyPendingRepaintsNow() override;
  145. //==============================================================================
  146. UIWindow* window;
  147. JuceUIView* view;
  148. JuceUIViewController* controller;
  149. bool isSharedWindow, fullScreen, insideDrawRect;
  150. static ModifierKeys currentModifiers;
  151. static int64 getMouseTime (UIEvent* e)
  152. {
  153. return (Time::currentTimeMillis() - Time::getMillisecondCounter())
  154. + (int64) ([e timestamp] * 1000.0);
  155. }
  156. static Rectangle<int> rotatedScreenPosToReal (const Rectangle<int>& r)
  157. {
  158. const Rectangle<int> screen (convertToRectInt ([UIScreen mainScreen].bounds));
  159. switch ([[UIApplication sharedApplication] statusBarOrientation])
  160. {
  161. case UIInterfaceOrientationPortrait:
  162. return r;
  163. case UIInterfaceOrientationPortraitUpsideDown:
  164. return Rectangle<int> (screen.getWidth() - r.getRight(), screen.getHeight() - r.getBottom(),
  165. r.getWidth(), r.getHeight());
  166. case UIInterfaceOrientationLandscapeLeft:
  167. return Rectangle<int> (r.getY(), screen.getHeight() - r.getRight(),
  168. r.getHeight(), r.getWidth());
  169. case UIInterfaceOrientationLandscapeRight:
  170. return Rectangle<int> (screen.getWidth() - r.getBottom(), r.getX(),
  171. r.getHeight(), r.getWidth());
  172. default: jassertfalse; // unknown orientation!
  173. }
  174. return r;
  175. }
  176. static Rectangle<int> realScreenPosToRotated (const Rectangle<int>& r)
  177. {
  178. const Rectangle<int> screen (convertToRectInt ([UIScreen mainScreen].bounds));
  179. switch ([[UIApplication sharedApplication] statusBarOrientation])
  180. {
  181. case UIInterfaceOrientationPortrait:
  182. return r;
  183. case UIInterfaceOrientationPortraitUpsideDown:
  184. return Rectangle<int> (screen.getWidth() - r.getRight(), screen.getHeight() - r.getBottom(),
  185. r.getWidth(), r.getHeight());
  186. case UIInterfaceOrientationLandscapeLeft:
  187. return Rectangle<int> (screen.getHeight() - r.getBottom(), r.getX(),
  188. r.getHeight(), r.getWidth());
  189. case UIInterfaceOrientationLandscapeRight:
  190. return Rectangle<int> (r.getY(), screen.getWidth() - r.getRight(),
  191. r.getHeight(), r.getWidth());
  192. default: jassertfalse; // unknown orientation!
  193. }
  194. return r;
  195. }
  196. MultiTouchMapper<UITouch*> currentTouches;
  197. private:
  198. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (UIViewComponentPeer)
  199. class AsyncRepaintMessage : public CallbackMessage
  200. {
  201. public:
  202. UIViewComponentPeer* const peer;
  203. const Rectangle<int> rect;
  204. AsyncRepaintMessage (UIViewComponentPeer* const p, const Rectangle<int>& r)
  205. : peer (p), rect (r)
  206. {
  207. }
  208. void messageCallback() override
  209. {
  210. if (ComponentPeer::isValidPeer (peer))
  211. peer->repaint (rect);
  212. }
  213. };
  214. };
  215. } // (juce namespace)
  216. //==============================================================================
  217. //==============================================================================
  218. @implementation JuceUIViewController
  219. - (NSUInteger) supportedInterfaceOrientations
  220. {
  221. return Orientations::getSupportedOrientations();
  222. }
  223. - (BOOL) shouldAutorotateToInterfaceOrientation: (UIInterfaceOrientation) interfaceOrientation
  224. {
  225. return Desktop::getInstance().isOrientationEnabled (Orientations::convertToJuce (interfaceOrientation));
  226. }
  227. - (void) willRotateToInterfaceOrientation: (UIInterfaceOrientation) toInterfaceOrientation
  228. duration: (NSTimeInterval) duration
  229. {
  230. (void) toInterfaceOrientation;
  231. (void) duration;
  232. [UIView setAnimationsEnabled: NO]; // disable this because it goes the wrong way and looks like crap.
  233. }
  234. - (void) didRotateFromInterfaceOrientation: (UIInterfaceOrientation) fromInterfaceOrientation
  235. {
  236. (void) fromInterfaceOrientation;
  237. JuceUIView* juceView = (JuceUIView*) [self view];
  238. jassert (juceView != nil && juceView->owner != nullptr);
  239. juceView->owner->updateTransformAndScreenBounds();
  240. [UIView setAnimationsEnabled: YES];
  241. }
  242. - (void) viewDidLoad
  243. {
  244. JuceUIView* juceView = (JuceUIView*) [self view];
  245. jassert (juceView != nil && juceView->owner != nullptr);
  246. juceView->owner->updateTransformAndScreenBounds();
  247. }
  248. - (void) viewWillAppear: (BOOL) animated
  249. {
  250. (void) animated;
  251. [self viewDidLoad];
  252. }
  253. - (void) viewDidAppear: (BOOL) animated
  254. {
  255. (void) animated;
  256. [self viewDidLoad];
  257. }
  258. - (void) viewWillLayoutSubviews
  259. {
  260. [self viewDidLoad];
  261. }
  262. - (void) viewDidLayoutSubviews
  263. {
  264. [self viewDidLoad];
  265. }
  266. @end
  267. @implementation JuceUIView
  268. - (JuceUIView*) initWithOwner: (UIViewComponentPeer*) peer
  269. withFrame: (CGRect) frame
  270. {
  271. [super initWithFrame: frame];
  272. owner = peer;
  273. hiddenTextView = [[UITextView alloc] initWithFrame: CGRectZero];
  274. [self addSubview: hiddenTextView];
  275. hiddenTextView.delegate = self;
  276. hiddenTextView.autocapitalizationType = UITextAutocapitalizationTypeNone;
  277. hiddenTextView.autocorrectionType = UITextAutocorrectionTypeNo;
  278. return self;
  279. }
  280. - (void) dealloc
  281. {
  282. [hiddenTextView removeFromSuperview];
  283. [hiddenTextView release];
  284. [super dealloc];
  285. }
  286. //==============================================================================
  287. - (void) drawRect: (CGRect) r
  288. {
  289. if (owner != nullptr)
  290. owner->drawRect (r);
  291. }
  292. //==============================================================================
  293. - (void) touchesBegan: (NSSet*) touches withEvent: (UIEvent*) event
  294. {
  295. (void) touches;
  296. if (owner != nullptr)
  297. owner->handleTouches (event, true, false, false);
  298. }
  299. - (void) touchesMoved: (NSSet*) touches withEvent: (UIEvent*) event
  300. {
  301. (void) touches;
  302. if (owner != nullptr)
  303. owner->handleTouches (event, false, false, false);
  304. }
  305. - (void) touchesEnded: (NSSet*) touches withEvent: (UIEvent*) event
  306. {
  307. (void) touches;
  308. if (owner != nullptr)
  309. owner->handleTouches (event, false, true, false);
  310. }
  311. - (void) touchesCancelled: (NSSet*) touches withEvent: (UIEvent*) event
  312. {
  313. if (owner != nullptr)
  314. owner->handleTouches (event, false, true, true);
  315. [self touchesEnded: touches withEvent: event];
  316. }
  317. //==============================================================================
  318. - (BOOL) becomeFirstResponder
  319. {
  320. if (owner != nullptr)
  321. owner->viewFocusGain();
  322. return true;
  323. }
  324. - (BOOL) resignFirstResponder
  325. {
  326. if (owner != nullptr)
  327. owner->viewFocusLoss();
  328. return true;
  329. }
  330. - (BOOL) canBecomeFirstResponder
  331. {
  332. return owner != nullptr && owner->canBecomeKeyWindow();
  333. }
  334. - (BOOL) textView: (UITextView*) textView shouldChangeTextInRange: (NSRange) range replacementText: (NSString*) text
  335. {
  336. (void) textView;
  337. return owner->textViewReplaceCharacters (Range<int> ((int) range.location, (int) (range.location + range.length)),
  338. nsStringToJuce (text));
  339. }
  340. @end
  341. //==============================================================================
  342. @implementation JuceUIWindow
  343. - (void) setOwner: (UIViewComponentPeer*) peer
  344. {
  345. owner = peer;
  346. }
  347. - (void) becomeKeyWindow
  348. {
  349. [super becomeKeyWindow];
  350. if (owner != nullptr)
  351. owner->grabFocus();
  352. }
  353. @end
  354. //==============================================================================
  355. //==============================================================================
  356. namespace juce
  357. {
  358. bool KeyPress::isKeyCurrentlyDown (int)
  359. {
  360. return false;
  361. }
  362. ModifierKeys UIViewComponentPeer::currentModifiers;
  363. ModifierKeys ModifierKeys::getCurrentModifiersRealtime() noexcept
  364. {
  365. return UIViewComponentPeer::currentModifiers;
  366. }
  367. void ModifierKeys::updateCurrentModifiers() noexcept
  368. {
  369. currentModifiers = UIViewComponentPeer::currentModifiers;
  370. }
  371. Point<float> juce_lastMousePos;
  372. //==============================================================================
  373. UIViewComponentPeer::UIViewComponentPeer (Component& comp, const int windowStyleFlags, UIView* viewToAttachTo)
  374. : ComponentPeer (comp, windowStyleFlags),
  375. window (nil),
  376. view (nil),
  377. controller (nil),
  378. isSharedWindow (viewToAttachTo != nil),
  379. fullScreen (false),
  380. insideDrawRect (false)
  381. {
  382. CGRect r = convertToCGRect (component.getBounds());
  383. view = [[JuceUIView alloc] initWithOwner: this withFrame: r];
  384. view.multipleTouchEnabled = YES;
  385. view.hidden = true;
  386. view.opaque = component.isOpaque();
  387. view.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent: 0];
  388. view.transform = CGAffineTransformIdentity;
  389. if (isSharedWindow)
  390. {
  391. window = [viewToAttachTo window];
  392. [viewToAttachTo addSubview: view];
  393. }
  394. else
  395. {
  396. r = convertToCGRect (rotatedScreenPosToReal (component.getBounds()));
  397. r.origin.y = [UIScreen mainScreen].bounds.size.height - (r.origin.y + r.size.height);
  398. window = [[JuceUIWindow alloc] initWithFrame: r];
  399. [((JuceUIWindow*) window) setOwner: this];
  400. controller = [[JuceUIViewController alloc] init];
  401. controller.view = view;
  402. window.rootViewController = controller;
  403. window.hidden = true;
  404. window.autoresizesSubviews = NO;
  405. window.transform = Orientations::getCGTransformFor (Desktop::getInstance().getCurrentOrientation());
  406. window.opaque = component.isOpaque();
  407. window.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent: 0];
  408. if (component.isAlwaysOnTop())
  409. window.windowLevel = UIWindowLevelAlert;
  410. view.frame = CGRectMake (0, 0, r.size.width, r.size.height);
  411. [window addSubview: view];
  412. }
  413. setTitle (component.getName());
  414. setVisible (component.isVisible());
  415. Desktop::getInstance().addFocusChangeListener (this);
  416. }
  417. UIViewComponentPeer::~UIViewComponentPeer()
  418. {
  419. Desktop::getInstance().removeFocusChangeListener (this);
  420. view->owner = nullptr;
  421. [view removeFromSuperview];
  422. [view release];
  423. [controller release];
  424. if (! isSharedWindow)
  425. {
  426. [((JuceUIWindow*) window) setOwner: nil];
  427. [window release];
  428. }
  429. }
  430. //==============================================================================
  431. void UIViewComponentPeer::setVisible (bool shouldBeVisible)
  432. {
  433. if (! isSharedWindow)
  434. window.hidden = ! shouldBeVisible;
  435. view.hidden = ! shouldBeVisible;
  436. }
  437. void UIViewComponentPeer::setTitle (const String&)
  438. {
  439. // xxx is this possible?
  440. }
  441. void UIViewComponentPeer::setBounds (const Rectangle<int>& newBounds, const bool isNowFullScreen)
  442. {
  443. fullScreen = isNowFullScreen;
  444. if (isSharedWindow)
  445. {
  446. CGRect r = convertToCGRect (newBounds);
  447. if (view.frame.size.width != r.size.width || view.frame.size.height != r.size.height)
  448. [view setNeedsDisplay];
  449. view.frame = r;
  450. }
  451. else
  452. {
  453. window.frame = convertToCGRect (rotatedScreenPosToReal (newBounds));
  454. view.frame = CGRectMake (0, 0, (CGFloat) newBounds.getWidth(), (CGFloat) newBounds.getHeight());
  455. handleMovedOrResized();
  456. }
  457. }
  458. Rectangle<int> UIViewComponentPeer::getBounds (const bool global) const
  459. {
  460. CGRect r = view.frame;
  461. if (global && view.window != nil)
  462. {
  463. r = [view convertRect: r toView: view.window];
  464. r = [view.window convertRect: r toWindow: nil];
  465. return realScreenPosToRotated (convertToRectInt (r));
  466. }
  467. return convertToRectInt (r);
  468. }
  469. Point<float> UIViewComponentPeer::localToGlobal (Point<float> relativePosition)
  470. {
  471. return relativePosition + getBounds (true).getPosition().toFloat();
  472. }
  473. Point<float> UIViewComponentPeer::globalToLocal (Point<float> screenPosition)
  474. {
  475. return screenPosition - getBounds (true).getPosition().toFloat();
  476. }
  477. void UIViewComponentPeer::setAlpha (float newAlpha)
  478. {
  479. [view.window setAlpha: (CGFloat) newAlpha];
  480. }
  481. void UIViewComponentPeer::setFullScreen (bool shouldBeFullScreen)
  482. {
  483. if (! isSharedWindow)
  484. {
  485. Rectangle<int> r (shouldBeFullScreen ? Desktop::getInstance().getDisplays().getMainDisplay().userArea
  486. : lastNonFullscreenBounds);
  487. if ((! shouldBeFullScreen) && r.isEmpty())
  488. r = getBounds();
  489. // (can't call the component's setBounds method because that'll reset our fullscreen flag)
  490. if (! r.isEmpty())
  491. setBounds (r, shouldBeFullScreen);
  492. component.repaint();
  493. }
  494. }
  495. void UIViewComponentPeer::updateTransformAndScreenBounds()
  496. {
  497. Desktop& desktop = Desktop::getInstance();
  498. const Rectangle<int> oldArea (component.getBounds());
  499. const Rectangle<int> oldDesktop (desktop.getDisplays().getMainDisplay().userArea);
  500. const_cast<Desktop::Displays&> (desktop.getDisplays()).refresh();
  501. window.transform = Orientations::getCGTransformFor (desktop.getCurrentOrientation());
  502. view.transform = CGAffineTransformIdentity;
  503. if (fullScreen)
  504. {
  505. fullScreen = false;
  506. setFullScreen (true);
  507. }
  508. else if (! isSharedWindow)
  509. {
  510. // this will re-centre the window, but leave its size unchanged
  511. const float centreRelX = oldArea.getCentreX() / (float) oldDesktop.getWidth();
  512. const float centreRelY = oldArea.getCentreY() / (float) oldDesktop.getHeight();
  513. const Rectangle<int> newDesktop (desktop.getDisplays().getMainDisplay().userArea);
  514. const int x = ((int) (newDesktop.getWidth() * centreRelX)) - (oldArea.getWidth() / 2);
  515. const int y = ((int) (newDesktop.getHeight() * centreRelY)) - (oldArea.getHeight() / 2);
  516. setBounds (oldArea.withPosition (x, y), false);
  517. }
  518. [view setNeedsDisplay];
  519. }
  520. bool UIViewComponentPeer::contains (Point<int> localPos, bool trueIfInAChildWindow) const
  521. {
  522. if (! component.getLocalBounds().contains (localPos))
  523. return false;
  524. UIView* v = [view hitTest: convertToCGPoint (localPos)
  525. withEvent: nil];
  526. if (trueIfInAChildWindow)
  527. return v != nil;
  528. return v == view;
  529. }
  530. bool UIViewComponentPeer::setAlwaysOnTop (bool alwaysOnTop)
  531. {
  532. if (! isSharedWindow)
  533. window.windowLevel = alwaysOnTop ? UIWindowLevelAlert : UIWindowLevelNormal;
  534. return true;
  535. }
  536. void UIViewComponentPeer::toFront (bool makeActiveWindow)
  537. {
  538. if (isSharedWindow)
  539. [[view superview] bringSubviewToFront: view];
  540. if (makeActiveWindow && window != nil && component.isVisible())
  541. [window makeKeyAndVisible];
  542. }
  543. void UIViewComponentPeer::toBehind (ComponentPeer* other)
  544. {
  545. if (UIViewComponentPeer* const otherPeer = dynamic_cast<UIViewComponentPeer*> (other))
  546. {
  547. if (isSharedWindow)
  548. {
  549. [[view superview] insertSubview: view belowSubview: otherPeer->view];
  550. }
  551. else
  552. {
  553. // don't know how to do this
  554. }
  555. }
  556. else
  557. {
  558. jassertfalse; // wrong type of window?
  559. }
  560. }
  561. void UIViewComponentPeer::setIcon (const Image& /*newIcon*/)
  562. {
  563. // to do..
  564. }
  565. //==============================================================================
  566. void UIViewComponentPeer::handleTouches (UIEvent* event, const bool isDown, const bool isUp, bool isCancel)
  567. {
  568. NSArray* touches = [[event touchesForView: view] allObjects];
  569. for (unsigned int i = 0; i < [touches count]; ++i)
  570. {
  571. UITouch* touch = [touches objectAtIndex: i];
  572. if ([touch phase] == UITouchPhaseStationary)
  573. continue;
  574. CGPoint p = [touch locationInView: view];
  575. const Point<float> pos (p.x, p.y);
  576. juce_lastMousePos = pos + getBounds (true).getPosition().toFloat();
  577. const int64 time = getMouseTime (event);
  578. const int touchIndex = currentTouches.getIndexOfTouch (touch);
  579. ModifierKeys modsToSend (currentModifiers);
  580. if (isDown)
  581. {
  582. if ([touch phase] != UITouchPhaseBegan)
  583. continue;
  584. currentModifiers = currentModifiers.withoutMouseButtons().withFlags (ModifierKeys::leftButtonModifier);
  585. modsToSend = currentModifiers;
  586. // this forces a mouse-enter/up event, in case for some reason we didn't get a mouse-up before.
  587. handleMouseEvent (touchIndex, pos, modsToSend.withoutMouseButtons(), time);
  588. if (! isValidPeer (this)) // (in case this component was deleted by the event)
  589. return;
  590. }
  591. else if (isUp)
  592. {
  593. if (! ([touch phase] == UITouchPhaseEnded || [touch phase] == UITouchPhaseCancelled))
  594. continue;
  595. modsToSend = modsToSend.withoutMouseButtons();
  596. currentTouches.clearTouch (touchIndex);
  597. if (! currentTouches.areAnyTouchesActive())
  598. isCancel = true;
  599. }
  600. if (isCancel)
  601. {
  602. currentTouches.clearTouch (touchIndex);
  603. modsToSend = currentModifiers = currentModifiers.withoutMouseButtons();
  604. }
  605. handleMouseEvent (touchIndex, pos, modsToSend, time);
  606. if (! isValidPeer (this)) // (in case this component was deleted by the event)
  607. return;
  608. if (isUp || isCancel)
  609. {
  610. handleMouseEvent (touchIndex, Point<float> (-1.0f, -1.0f), modsToSend, time);
  611. if (! isValidPeer (this))
  612. return;
  613. }
  614. }
  615. }
  616. //==============================================================================
  617. static UIViewComponentPeer* currentlyFocusedPeer = nullptr;
  618. void UIViewComponentPeer::viewFocusGain()
  619. {
  620. if (currentlyFocusedPeer != this)
  621. {
  622. if (ComponentPeer::isValidPeer (currentlyFocusedPeer))
  623. currentlyFocusedPeer->handleFocusLoss();
  624. currentlyFocusedPeer = this;
  625. handleFocusGain();
  626. }
  627. }
  628. void UIViewComponentPeer::viewFocusLoss()
  629. {
  630. if (currentlyFocusedPeer == this)
  631. {
  632. currentlyFocusedPeer = nullptr;
  633. handleFocusLoss();
  634. }
  635. }
  636. bool UIViewComponentPeer::isFocused() const
  637. {
  638. return isSharedWindow ? this == currentlyFocusedPeer
  639. : (window != nil && [window isKeyWindow]);
  640. }
  641. void UIViewComponentPeer::grabFocus()
  642. {
  643. if (window != nil)
  644. {
  645. [window makeKeyWindow];
  646. viewFocusGain();
  647. }
  648. }
  649. void UIViewComponentPeer::textInputRequired (Point<int>, TextInputTarget&)
  650. {
  651. }
  652. static UIKeyboardType getUIKeyboardType (TextInputTarget::VirtualKeyboardType type) noexcept
  653. {
  654. switch (type)
  655. {
  656. case TextInputTarget::textKeyboard: return UIKeyboardTypeAlphabet;
  657. case TextInputTarget::numericKeyboard: return UIKeyboardTypeNumbersAndPunctuation;
  658. case TextInputTarget::urlKeyboard: return UIKeyboardTypeURL;
  659. case TextInputTarget::emailAddressKeyboard: return UIKeyboardTypeEmailAddress;
  660. case TextInputTarget::phoneNumberKeyboard: return UIKeyboardTypePhonePad;
  661. default: jassertfalse; break;
  662. }
  663. return UIKeyboardTypeDefault;
  664. }
  665. void UIViewComponentPeer::updateHiddenTextContent (TextInputTarget* target)
  666. {
  667. view->hiddenTextView.keyboardType = getUIKeyboardType (target->getKeyboardType());
  668. view->hiddenTextView.text = juceStringToNS (target->getTextInRange (Range<int> (0, target->getHighlightedRegion().getStart())));
  669. view->hiddenTextView.selectedRange = NSMakeRange (target->getHighlightedRegion().getStart(), 0);
  670. }
  671. BOOL UIViewComponentPeer::textViewReplaceCharacters (Range<int> range, const String& text)
  672. {
  673. if (TextInputTarget* const target = findCurrentTextInputTarget())
  674. {
  675. const Range<int> currentSelection (target->getHighlightedRegion());
  676. if (range.getLength() == 1 && text.isEmpty()) // (detect backspace)
  677. if (currentSelection.isEmpty())
  678. target->setHighlightedRegion (currentSelection.withStart (currentSelection.getStart() - 1));
  679. if (text == "\r" || text == "\n" || text == "\r\n")
  680. handleKeyPress (KeyPress::returnKey, text[0]);
  681. else
  682. target->insertTextAtCaret (text);
  683. updateHiddenTextContent (target);
  684. }
  685. return NO;
  686. }
  687. void UIViewComponentPeer::globalFocusChanged (Component*)
  688. {
  689. if (TextInputTarget* const target = findCurrentTextInputTarget())
  690. {
  691. Component* comp = dynamic_cast<Component*> (target);
  692. Point<int> pos (component.getLocalPoint (comp, Point<int>()));
  693. view->hiddenTextView.frame = CGRectMake (pos.x, pos.y, 0, 0);
  694. updateHiddenTextContent (target);
  695. [view->hiddenTextView becomeFirstResponder];
  696. }
  697. else
  698. {
  699. [view->hiddenTextView resignFirstResponder];
  700. }
  701. }
  702. //==============================================================================
  703. void UIViewComponentPeer::drawRect (CGRect r)
  704. {
  705. if (r.size.width < 1.0f || r.size.height < 1.0f)
  706. return;
  707. CGContextRef cg = UIGraphicsGetCurrentContext();
  708. if (! component.isOpaque())
  709. CGContextClearRect (cg, CGContextGetClipBoundingBox (cg));
  710. CGContextConcatCTM (cg, CGAffineTransformMake (1, 0, 0, -1, 0, getComponent().getHeight()));
  711. CoreGraphicsContext g (cg, getComponent().getHeight(), [UIScreen mainScreen].scale);
  712. insideDrawRect = true;
  713. handlePaint (g);
  714. insideDrawRect = false;
  715. }
  716. bool UIViewComponentPeer::canBecomeKeyWindow()
  717. {
  718. return (getStyleFlags() & juce::ComponentPeer::windowIgnoresKeyPresses) == 0;
  719. }
  720. //==============================================================================
  721. void Desktop::setKioskComponent (Component* kioskModeComp, bool enableOrDisable, bool /*allowMenusAndBars*/)
  722. {
  723. [[UIApplication sharedApplication] setStatusBarHidden: enableOrDisable
  724. withAnimation: UIStatusBarAnimationSlide];
  725. displays->refresh();
  726. if (ComponentPeer* const peer = kioskModeComp->getPeer())
  727. peer->setFullScreen (enableOrDisable);
  728. }
  729. //==============================================================================
  730. void UIViewComponentPeer::repaint (const Rectangle<int>& area)
  731. {
  732. if (insideDrawRect || ! MessageManager::getInstance()->isThisTheMessageThread())
  733. (new AsyncRepaintMessage (this, area))->post();
  734. else
  735. [view setNeedsDisplayInRect: convertToCGRect (area)];
  736. }
  737. void UIViewComponentPeer::performAnyPendingRepaintsNow()
  738. {
  739. }
  740. ComponentPeer* Component::createNewPeer (int styleFlags, void* windowToAttachTo)
  741. {
  742. return new UIViewComponentPeer (*this, styleFlags, (UIView*) windowToAttachTo);
  743. }
  744. //==============================================================================
  745. const int KeyPress::spaceKey = ' ';
  746. const int KeyPress::returnKey = 0x0d;
  747. const int KeyPress::escapeKey = 0x1b;
  748. const int KeyPress::backspaceKey = 0x7f;
  749. const int KeyPress::leftKey = 0x1000;
  750. const int KeyPress::rightKey = 0x1001;
  751. const int KeyPress::upKey = 0x1002;
  752. const int KeyPress::downKey = 0x1003;
  753. const int KeyPress::pageUpKey = 0x1004;
  754. const int KeyPress::pageDownKey = 0x1005;
  755. const int KeyPress::endKey = 0x1006;
  756. const int KeyPress::homeKey = 0x1007;
  757. const int KeyPress::deleteKey = 0x1008;
  758. const int KeyPress::insertKey = -1;
  759. const int KeyPress::tabKey = 9;
  760. const int KeyPress::F1Key = 0x2001;
  761. const int KeyPress::F2Key = 0x2002;
  762. const int KeyPress::F3Key = 0x2003;
  763. const int KeyPress::F4Key = 0x2004;
  764. const int KeyPress::F5Key = 0x2005;
  765. const int KeyPress::F6Key = 0x2006;
  766. const int KeyPress::F7Key = 0x2007;
  767. const int KeyPress::F8Key = 0x2008;
  768. const int KeyPress::F9Key = 0x2009;
  769. const int KeyPress::F10Key = 0x200a;
  770. const int KeyPress::F11Key = 0x200b;
  771. const int KeyPress::F12Key = 0x200c;
  772. const int KeyPress::F13Key = 0x200d;
  773. const int KeyPress::F14Key = 0x200e;
  774. const int KeyPress::F15Key = 0x200f;
  775. const int KeyPress::F16Key = 0x2010;
  776. const int KeyPress::numberPad0 = 0x30020;
  777. const int KeyPress::numberPad1 = 0x30021;
  778. const int KeyPress::numberPad2 = 0x30022;
  779. const int KeyPress::numberPad3 = 0x30023;
  780. const int KeyPress::numberPad4 = 0x30024;
  781. const int KeyPress::numberPad5 = 0x30025;
  782. const int KeyPress::numberPad6 = 0x30026;
  783. const int KeyPress::numberPad7 = 0x30027;
  784. const int KeyPress::numberPad8 = 0x30028;
  785. const int KeyPress::numberPad9 = 0x30029;
  786. const int KeyPress::numberPadAdd = 0x3002a;
  787. const int KeyPress::numberPadSubtract = 0x3002b;
  788. const int KeyPress::numberPadMultiply = 0x3002c;
  789. const int KeyPress::numberPadDivide = 0x3002d;
  790. const int KeyPress::numberPadSeparator = 0x3002e;
  791. const int KeyPress::numberPadDecimalPoint = 0x3002f;
  792. const int KeyPress::numberPadEquals = 0x30030;
  793. const int KeyPress::numberPadDelete = 0x30031;
  794. const int KeyPress::playKey = 0x30000;
  795. const int KeyPress::stopKey = 0x30001;
  796. const int KeyPress::fastForwardKey = 0x30002;
  797. const int KeyPress::rewindKey = 0x30003;