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.

1019 lines
32KB

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