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 32KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988
  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<int> localToGlobal (Point<int> relativePosition) override;
  116. Point<int> globalToLocal (Point<int> 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 (const Point<int>&) 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<int> 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<int> UIViewComponentPeer::localToGlobal (Point<int> relativePosition)
  470. {
  471. return relativePosition + getBounds (true).getPosition();
  472. }
  473. Point<int> UIViewComponentPeer::globalToLocal (Point<int> screenPosition)
  474. {
  475. return screenPosition - getBounds (true).getPosition();
  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. UIViewComponentPeer* const otherPeer = dynamic_cast <UIViewComponentPeer*> (other);
  546. jassert (otherPeer != nullptr); // wrong type of window?
  547. if (otherPeer != nullptr)
  548. {
  549. if (isSharedWindow)
  550. {
  551. [[view superview] insertSubview: view belowSubview: otherPeer->view];
  552. }
  553. else
  554. {
  555. // don't know how to do this
  556. }
  557. }
  558. }
  559. void UIViewComponentPeer::setIcon (const Image& /*newIcon*/)
  560. {
  561. // to do..
  562. }
  563. //==============================================================================
  564. void UIViewComponentPeer::handleTouches (UIEvent* event, const bool isDown, const bool isUp, bool isCancel)
  565. {
  566. NSArray* touches = [[event touchesForView: view] allObjects];
  567. for (unsigned int i = 0; i < [touches count]; ++i)
  568. {
  569. UITouch* touch = [touches objectAtIndex: i];
  570. if ([touch phase] == UITouchPhaseStationary)
  571. continue;
  572. CGPoint p = [touch locationInView: view];
  573. const Point<int> pos ((int) p.x, (int) p.y);
  574. juce_lastMousePos = pos + getBounds (true).getPosition();
  575. const int64 time = getMouseTime (event);
  576. const int touchIndex = currentTouches.getIndexOfTouch (touch);
  577. ModifierKeys modsToSend (currentModifiers);
  578. if (isDown)
  579. {
  580. if ([touch phase] != UITouchPhaseBegan)
  581. continue;
  582. currentModifiers = currentModifiers.withoutMouseButtons().withFlags (ModifierKeys::leftButtonModifier);
  583. modsToSend = currentModifiers;
  584. // this forces a mouse-enter/up event, in case for some reason we didn't get a mouse-up before.
  585. handleMouseEvent (touchIndex, pos, modsToSend.withoutMouseButtons(), time);
  586. if (! isValidPeer (this)) // (in case this component was deleted by the event)
  587. return;
  588. }
  589. else if (isUp)
  590. {
  591. if (! ([touch phase] == UITouchPhaseEnded || [touch phase] == UITouchPhaseCancelled))
  592. continue;
  593. modsToSend = modsToSend.withoutMouseButtons();
  594. currentTouches.clearTouch (touchIndex);
  595. if (! currentTouches.areAnyTouchesActive())
  596. isCancel = true;
  597. }
  598. if (isCancel)
  599. {
  600. currentTouches.clearTouch (touchIndex);
  601. modsToSend = currentModifiers = currentModifiers.withoutMouseButtons();
  602. }
  603. handleMouseEvent (touchIndex, pos, modsToSend, time);
  604. if (! isValidPeer (this)) // (in case this component was deleted by the event)
  605. return;
  606. if (isUp || isCancel)
  607. {
  608. handleMouseEvent (touchIndex, Point<int> (-1, -1), modsToSend, time);
  609. if (! isValidPeer (this))
  610. return;
  611. }
  612. }
  613. }
  614. //==============================================================================
  615. static UIViewComponentPeer* currentlyFocusedPeer = nullptr;
  616. void UIViewComponentPeer::viewFocusGain()
  617. {
  618. if (currentlyFocusedPeer != this)
  619. {
  620. if (ComponentPeer::isValidPeer (currentlyFocusedPeer))
  621. currentlyFocusedPeer->handleFocusLoss();
  622. currentlyFocusedPeer = this;
  623. handleFocusGain();
  624. }
  625. }
  626. void UIViewComponentPeer::viewFocusLoss()
  627. {
  628. if (currentlyFocusedPeer == this)
  629. {
  630. currentlyFocusedPeer = nullptr;
  631. handleFocusLoss();
  632. }
  633. }
  634. bool UIViewComponentPeer::isFocused() const
  635. {
  636. return isSharedWindow ? this == currentlyFocusedPeer
  637. : (window != nil && [window isKeyWindow]);
  638. }
  639. void UIViewComponentPeer::grabFocus()
  640. {
  641. if (window != nil)
  642. {
  643. [window makeKeyWindow];
  644. viewFocusGain();
  645. }
  646. }
  647. void UIViewComponentPeer::textInputRequired (const Point<int>&)
  648. {
  649. }
  650. void UIViewComponentPeer::updateHiddenTextContent (TextInputTarget* target)
  651. {
  652. view->hiddenTextView.text = juceStringToNS (target->getTextInRange (Range<int> (0, target->getHighlightedRegion().getStart())));
  653. view->hiddenTextView.selectedRange = NSMakeRange (target->getHighlightedRegion().getStart(), 0);
  654. }
  655. BOOL UIViewComponentPeer::textViewReplaceCharacters (Range<int> range, const String& text)
  656. {
  657. if (TextInputTarget* const target = findCurrentTextInputTarget())
  658. {
  659. const Range<int> currentSelection (target->getHighlightedRegion());
  660. if (range.getLength() == 1 && text.isEmpty()) // (detect backspace)
  661. if (currentSelection.isEmpty())
  662. target->setHighlightedRegion (currentSelection.withStart (currentSelection.getStart() - 1));
  663. if (text == "\r" || text == "\n" || text == "\r\n")
  664. handleKeyPress (KeyPress::returnKey, text[0]);
  665. else
  666. target->insertTextAtCaret (text);
  667. updateHiddenTextContent (target);
  668. }
  669. return NO;
  670. }
  671. void UIViewComponentPeer::globalFocusChanged (Component*)
  672. {
  673. if (TextInputTarget* const target = findCurrentTextInputTarget())
  674. {
  675. Component* comp = dynamic_cast<Component*> (target);
  676. Point<int> pos (component.getLocalPoint (comp, Point<int>()));
  677. view->hiddenTextView.frame = CGRectMake (pos.x, pos.y, 0, 0);
  678. updateHiddenTextContent (target);
  679. [view->hiddenTextView becomeFirstResponder];
  680. }
  681. else
  682. {
  683. [view->hiddenTextView resignFirstResponder];
  684. }
  685. }
  686. //==============================================================================
  687. void UIViewComponentPeer::drawRect (CGRect r)
  688. {
  689. if (r.size.width < 1.0f || r.size.height < 1.0f)
  690. return;
  691. CGContextRef cg = UIGraphicsGetCurrentContext();
  692. if (! component.isOpaque())
  693. CGContextClearRect (cg, CGContextGetClipBoundingBox (cg));
  694. CGContextConcatCTM (cg, CGAffineTransformMake (1, 0, 0, -1, 0, getComponent().getHeight()));
  695. CoreGraphicsContext g (cg, getComponent().getHeight(), [UIScreen mainScreen].scale);
  696. insideDrawRect = true;
  697. handlePaint (g);
  698. insideDrawRect = false;
  699. }
  700. bool UIViewComponentPeer::canBecomeKeyWindow()
  701. {
  702. return (getStyleFlags() & juce::ComponentPeer::windowIgnoresKeyPresses) == 0;
  703. }
  704. //==============================================================================
  705. void Desktop::setKioskComponent (Component* kioskModeComp, bool enableOrDisable, bool /*allowMenusAndBars*/)
  706. {
  707. [[UIApplication sharedApplication] setStatusBarHidden: enableOrDisable
  708. withAnimation: UIStatusBarAnimationSlide];
  709. displays->refresh();
  710. if (ComponentPeer* const peer = kioskModeComp->getPeer())
  711. peer->setFullScreen (enableOrDisable);
  712. }
  713. //==============================================================================
  714. void UIViewComponentPeer::repaint (const Rectangle<int>& area)
  715. {
  716. if (insideDrawRect || ! MessageManager::getInstance()->isThisTheMessageThread())
  717. (new AsyncRepaintMessage (this, area))->post();
  718. else
  719. [view setNeedsDisplayInRect: convertToCGRect (area)];
  720. }
  721. void UIViewComponentPeer::performAnyPendingRepaintsNow()
  722. {
  723. }
  724. ComponentPeer* Component::createNewPeer (int styleFlags, void* windowToAttachTo)
  725. {
  726. return new UIViewComponentPeer (*this, styleFlags, (UIView*) windowToAttachTo);
  727. }
  728. //==============================================================================
  729. const int KeyPress::spaceKey = ' ';
  730. const int KeyPress::returnKey = 0x0d;
  731. const int KeyPress::escapeKey = 0x1b;
  732. const int KeyPress::backspaceKey = 0x7f;
  733. const int KeyPress::leftKey = 0x1000;
  734. const int KeyPress::rightKey = 0x1001;
  735. const int KeyPress::upKey = 0x1002;
  736. const int KeyPress::downKey = 0x1003;
  737. const int KeyPress::pageUpKey = 0x1004;
  738. const int KeyPress::pageDownKey = 0x1005;
  739. const int KeyPress::endKey = 0x1006;
  740. const int KeyPress::homeKey = 0x1007;
  741. const int KeyPress::deleteKey = 0x1008;
  742. const int KeyPress::insertKey = -1;
  743. const int KeyPress::tabKey = 9;
  744. const int KeyPress::F1Key = 0x2001;
  745. const int KeyPress::F2Key = 0x2002;
  746. const int KeyPress::F3Key = 0x2003;
  747. const int KeyPress::F4Key = 0x2004;
  748. const int KeyPress::F5Key = 0x2005;
  749. const int KeyPress::F6Key = 0x2006;
  750. const int KeyPress::F7Key = 0x2007;
  751. const int KeyPress::F8Key = 0x2008;
  752. const int KeyPress::F9Key = 0x2009;
  753. const int KeyPress::F10Key = 0x200a;
  754. const int KeyPress::F11Key = 0x200b;
  755. const int KeyPress::F12Key = 0x200c;
  756. const int KeyPress::F13Key = 0x200d;
  757. const int KeyPress::F14Key = 0x200e;
  758. const int KeyPress::F15Key = 0x200f;
  759. const int KeyPress::F16Key = 0x2010;
  760. const int KeyPress::numberPad0 = 0x30020;
  761. const int KeyPress::numberPad1 = 0x30021;
  762. const int KeyPress::numberPad2 = 0x30022;
  763. const int KeyPress::numberPad3 = 0x30023;
  764. const int KeyPress::numberPad4 = 0x30024;
  765. const int KeyPress::numberPad5 = 0x30025;
  766. const int KeyPress::numberPad6 = 0x30026;
  767. const int KeyPress::numberPad7 = 0x30027;
  768. const int KeyPress::numberPad8 = 0x30028;
  769. const int KeyPress::numberPad9 = 0x30029;
  770. const int KeyPress::numberPadAdd = 0x3002a;
  771. const int KeyPress::numberPadSubtract = 0x3002b;
  772. const int KeyPress::numberPadMultiply = 0x3002c;
  773. const int KeyPress::numberPadDivide = 0x3002d;
  774. const int KeyPress::numberPadSeparator = 0x3002e;
  775. const int KeyPress::numberPadDecimalPoint = 0x3002f;
  776. const int KeyPress::numberPadEquals = 0x30030;
  777. const int KeyPress::numberPadDelete = 0x30031;
  778. const int KeyPress::playKey = 0x30000;
  779. const int KeyPress::stopKey = 0x30001;
  780. const int KeyPress::fastForwardKey = 0x30002;
  781. const int KeyPress::rewindKey = 0x30003;