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.

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