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.

1062 lines
34KB

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