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.

1025 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().getDisplays().getTotalBounds (true),
  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().getDisplays().getMainDisplay().userArea
  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. Desktop& desktop = Desktop::getInstance();
  489. const Rectangle<int> oldArea (component->getBounds());
  490. const Rectangle<int> oldDesktop (desktop.getDisplays().getMainDisplay().userArea);
  491. const_cast <Desktop::Displays&> (desktop.getDisplays()).refresh();
  492. if (fullScreen)
  493. {
  494. fullScreen = false;
  495. setFullScreen (true);
  496. }
  497. else if (! isSharedWindow)
  498. {
  499. const float l = oldArea.getX() / (float) oldDesktop.getWidth();
  500. const float r = oldArea.getRight() / (float) oldDesktop.getWidth();
  501. const float t = oldArea.getY() / (float) oldDesktop.getHeight();
  502. const float b = oldArea.getBottom() / (float) oldDesktop.getHeight();
  503. const Rectangle<int> newDesktop (desktop.getDisplays().getMainDisplay().userArea);
  504. setBounds ((int) (l * newDesktop.getWidth()),
  505. (int) (t * newDesktop.getHeight()),
  506. (int) ((r - l) * newDesktop.getWidth()),
  507. (int) ((b - t) * newDesktop.getHeight()),
  508. false);
  509. }
  510. }
  511. bool UIViewComponentPeer::contains (const Point<int>& position, bool trueIfInAChildWindow) const
  512. {
  513. if (! (isPositiveAndBelow (position.getX(), component->getWidth())
  514. && isPositiveAndBelow (position.getY(), component->getHeight())))
  515. return false;
  516. UIView* v = [view hitTest: CGPointMake ((CGFloat) position.getX(), (CGFloat) position.getY())
  517. withEvent: nil];
  518. if (trueIfInAChildWindow)
  519. return v != nil;
  520. return v == view;
  521. }
  522. BorderSize<int> UIViewComponentPeer::getFrameSize() const
  523. {
  524. return BorderSize<int>();
  525. }
  526. bool UIViewComponentPeer::setAlwaysOnTop (bool alwaysOnTop)
  527. {
  528. if (! isSharedWindow)
  529. window.windowLevel = alwaysOnTop ? UIWindowLevelAlert : UIWindowLevelNormal;
  530. return true;
  531. }
  532. void UIViewComponentPeer::toFront (bool makeActiveWindow)
  533. {
  534. if (isSharedWindow)
  535. [[view superview] bringSubviewToFront: view];
  536. if (window != nil && component->isVisible())
  537. [window makeKeyAndVisible];
  538. }
  539. void UIViewComponentPeer::toBehind (ComponentPeer* other)
  540. {
  541. UIViewComponentPeer* const otherPeer = dynamic_cast <UIViewComponentPeer*> (other);
  542. jassert (otherPeer != nullptr); // wrong type of window?
  543. if (otherPeer != nullptr)
  544. {
  545. if (isSharedWindow)
  546. {
  547. [[view superview] insertSubview: view belowSubview: otherPeer->view];
  548. }
  549. else
  550. {
  551. // don't know how to do this
  552. }
  553. }
  554. }
  555. void UIViewComponentPeer::setIcon (const Image& /*newIcon*/)
  556. {
  557. // to do..
  558. }
  559. //==============================================================================
  560. void UIViewComponentPeer::handleTouches (UIEvent* event, const bool isDown, const bool isUp, bool isCancel)
  561. {
  562. NSArray* touches = [[event touchesForView: view] allObjects];
  563. for (unsigned int i = 0; i < [touches count]; ++i)
  564. {
  565. UITouch* touch = [touches objectAtIndex: i];
  566. if ([touch phase] == UITouchPhaseStationary)
  567. continue;
  568. CGPoint p = [touch locationInView: view];
  569. const Point<int> pos ((int) p.x, (int) p.y);
  570. juce_lastMousePos = pos + getScreenPosition();
  571. const int64 time = getMouseTime (event);
  572. const int touchIndex = currentTouches.getIndexOfTouch (touch);
  573. ModifierKeys modsToSend (currentModifiers);
  574. if (isDown)
  575. {
  576. if ([touch phase] != UITouchPhaseBegan)
  577. continue;
  578. currentModifiers = currentModifiers.withoutMouseButtons().withFlags (ModifierKeys::leftButtonModifier);
  579. modsToSend = currentModifiers;
  580. // this forces a mouse-enter/up event, in case for some reason we didn't get a mouse-up before.
  581. handleMouseEvent (touchIndex, pos, modsToSend.withoutMouseButtons(), time);
  582. if (! isValidPeer (this)) // (in case this component was deleted by the event)
  583. return;
  584. }
  585. else if (isUp)
  586. {
  587. if (! ([touch phase] == UITouchPhaseEnded || [touch phase] == UITouchPhaseCancelled))
  588. continue;
  589. modsToSend = modsToSend.withoutMouseButtons();
  590. currentTouches.clearTouch (touchIndex);
  591. if (! currentTouches.areAnyTouchesActive())
  592. isCancel = true;
  593. }
  594. if (isCancel)
  595. {
  596. currentTouches.clear();
  597. currentModifiers = currentModifiers.withoutMouseButtons();
  598. }
  599. handleMouseEvent (touchIndex, pos, modsToSend, time);
  600. if (! isValidPeer (this)) // (in case this component was deleted by the event)
  601. return;
  602. if (isUp || isCancel)
  603. {
  604. handleMouseEvent (touchIndex, Point<int> (-1, -1), currentModifiers, time);
  605. if (! isValidPeer (this))
  606. return;
  607. }
  608. }
  609. }
  610. //==============================================================================
  611. static UIViewComponentPeer* currentlyFocusedPeer = nullptr;
  612. void UIViewComponentPeer::viewFocusGain()
  613. {
  614. if (currentlyFocusedPeer != this)
  615. {
  616. if (ComponentPeer::isValidPeer (currentlyFocusedPeer))
  617. currentlyFocusedPeer->handleFocusLoss();
  618. currentlyFocusedPeer = this;
  619. handleFocusGain();
  620. }
  621. }
  622. void UIViewComponentPeer::viewFocusLoss()
  623. {
  624. if (currentlyFocusedPeer == this)
  625. {
  626. currentlyFocusedPeer = nullptr;
  627. handleFocusLoss();
  628. }
  629. }
  630. bool UIViewComponentPeer::isFocused() const
  631. {
  632. return isSharedWindow ? this == currentlyFocusedPeer
  633. : (window != nil && [window isKeyWindow]);
  634. }
  635. void UIViewComponentPeer::grabFocus()
  636. {
  637. if (window != nil)
  638. {
  639. [window makeKeyWindow];
  640. viewFocusGain();
  641. }
  642. }
  643. void UIViewComponentPeer::textInputRequired (const Point<int>&)
  644. {
  645. }
  646. void UIViewComponentPeer::updateHiddenTextContent (TextInputTarget* target)
  647. {
  648. view->hiddenTextView.text = juceStringToNS (target->getTextInRange (Range<int> (0, target->getHighlightedRegion().getStart())));
  649. view->hiddenTextView.selectedRange = NSMakeRange (target->getHighlightedRegion().getStart(), 0);
  650. }
  651. BOOL UIViewComponentPeer::textViewReplaceCharacters (const Range<int>& range, const String& text)
  652. {
  653. TextInputTarget* const target = findCurrentTextInputTarget();
  654. if (target != nullptr)
  655. {
  656. const Range<int> currentSelection (target->getHighlightedRegion());
  657. if (range.getLength() == 1 && text.isEmpty()) // (detect backspace)
  658. if (currentSelection.isEmpty())
  659. target->setHighlightedRegion (currentSelection.withStart (currentSelection.getStart() - 1));
  660. if (text == "\r" || text == "\n" || text == "\r\n")
  661. handleKeyPress (KeyPress::returnKey, text[0]);
  662. else
  663. target->insertTextAtCaret (text);
  664. updateHiddenTextContent (target);
  665. }
  666. return NO;
  667. }
  668. void UIViewComponentPeer::globalFocusChanged (Component*)
  669. {
  670. TextInputTarget* const target = findCurrentTextInputTarget();
  671. if (target != nullptr)
  672. {
  673. Component* comp = dynamic_cast<Component*> (target);
  674. Point<int> pos (component->getLocalPoint (comp, Point<int>()));
  675. view->hiddenTextView.frame = CGRectMake (pos.getX(), pos.getY(), 0, 0);
  676. updateHiddenTextContent (target);
  677. [view->hiddenTextView becomeFirstResponder];
  678. }
  679. else
  680. {
  681. [view->hiddenTextView resignFirstResponder];
  682. }
  683. }
  684. //==============================================================================
  685. void UIViewComponentPeer::drawRect (CGRect r)
  686. {
  687. if (r.size.width < 1.0f || r.size.height < 1.0f)
  688. return;
  689. CGContextRef cg = UIGraphicsGetCurrentContext();
  690. if (! component->isOpaque())
  691. CGContextClearRect (cg, CGContextGetClipBoundingBox (cg));
  692. CGContextConcatCTM (cg, CGAffineTransformMake (1, 0, 0, -1, 0, view.bounds.size.height));
  693. CoreGraphicsContext g (cg, view.bounds.size.height);
  694. insideDrawRect = true;
  695. handlePaint (g);
  696. insideDrawRect = false;
  697. }
  698. bool UIViewComponentPeer::canBecomeKeyWindow()
  699. {
  700. return (getStyleFlags() & juce::ComponentPeer::windowIgnoresKeyPresses) == 0;
  701. }
  702. bool UIViewComponentPeer::windowShouldClose()
  703. {
  704. if (! isValidPeer (this))
  705. return YES;
  706. handleUserClosingWindow();
  707. return NO;
  708. }
  709. void UIViewComponentPeer::redirectMovedOrResized()
  710. {
  711. handleMovedOrResized();
  712. }
  713. //==============================================================================
  714. void Desktop::setKioskComponent (Component* kioskModeComponent, bool enableOrDisable, bool allowMenusAndBars)
  715. {
  716. [[UIApplication sharedApplication] setStatusBarHidden: enableOrDisable
  717. withAnimation: UIStatusBarAnimationSlide];
  718. displays.refresh();
  719. ComponentPeer* const peer = kioskModeComponent->getPeer();
  720. if (peer != nullptr)
  721. peer->setFullScreen (enableOrDisable);
  722. }
  723. //==============================================================================
  724. class AsyncRepaintMessage : public CallbackMessage
  725. {
  726. public:
  727. UIViewComponentPeer* const peer;
  728. const Rectangle<int> rect;
  729. AsyncRepaintMessage (UIViewComponentPeer* const peer_, const Rectangle<int>& rect_)
  730. : peer (peer_), rect (rect_)
  731. {
  732. }
  733. void messageCallback()
  734. {
  735. if (ComponentPeer::isValidPeer (peer))
  736. peer->repaint (rect);
  737. }
  738. };
  739. void UIViewComponentPeer::repaint (const Rectangle<int>& area)
  740. {
  741. if (insideDrawRect || ! MessageManager::getInstance()->isThisTheMessageThread())
  742. {
  743. (new AsyncRepaintMessage (this, area))->post();
  744. }
  745. else
  746. {
  747. [view setNeedsDisplayInRect: convertToCGRect (area)];
  748. }
  749. }
  750. void UIViewComponentPeer::performAnyPendingRepaintsNow()
  751. {
  752. }
  753. ComponentPeer* Component::createNewPeer (int styleFlags, void* windowToAttachTo)
  754. {
  755. return new UIViewComponentPeer (this, styleFlags, (UIView*) windowToAttachTo);
  756. }
  757. //==============================================================================
  758. const int KeyPress::spaceKey = ' ';
  759. const int KeyPress::returnKey = 0x0d;
  760. const int KeyPress::escapeKey = 0x1b;
  761. const int KeyPress::backspaceKey = 0x7f;
  762. const int KeyPress::leftKey = 0x1000;
  763. const int KeyPress::rightKey = 0x1001;
  764. const int KeyPress::upKey = 0x1002;
  765. const int KeyPress::downKey = 0x1003;
  766. const int KeyPress::pageUpKey = 0x1004;
  767. const int KeyPress::pageDownKey = 0x1005;
  768. const int KeyPress::endKey = 0x1006;
  769. const int KeyPress::homeKey = 0x1007;
  770. const int KeyPress::deleteKey = 0x1008;
  771. const int KeyPress::insertKey = -1;
  772. const int KeyPress::tabKey = 9;
  773. const int KeyPress::F1Key = 0x2001;
  774. const int KeyPress::F2Key = 0x2002;
  775. const int KeyPress::F3Key = 0x2003;
  776. const int KeyPress::F4Key = 0x2004;
  777. const int KeyPress::F5Key = 0x2005;
  778. const int KeyPress::F6Key = 0x2006;
  779. const int KeyPress::F7Key = 0x2007;
  780. const int KeyPress::F8Key = 0x2008;
  781. const int KeyPress::F9Key = 0x2009;
  782. const int KeyPress::F10Key = 0x200a;
  783. const int KeyPress::F11Key = 0x200b;
  784. const int KeyPress::F12Key = 0x200c;
  785. const int KeyPress::F13Key = 0x200d;
  786. const int KeyPress::F14Key = 0x200e;
  787. const int KeyPress::F15Key = 0x200f;
  788. const int KeyPress::F16Key = 0x2010;
  789. const int KeyPress::numberPad0 = 0x30020;
  790. const int KeyPress::numberPad1 = 0x30021;
  791. const int KeyPress::numberPad2 = 0x30022;
  792. const int KeyPress::numberPad3 = 0x30023;
  793. const int KeyPress::numberPad4 = 0x30024;
  794. const int KeyPress::numberPad5 = 0x30025;
  795. const int KeyPress::numberPad6 = 0x30026;
  796. const int KeyPress::numberPad7 = 0x30027;
  797. const int KeyPress::numberPad8 = 0x30028;
  798. const int KeyPress::numberPad9 = 0x30029;
  799. const int KeyPress::numberPadAdd = 0x3002a;
  800. const int KeyPress::numberPadSubtract = 0x3002b;
  801. const int KeyPress::numberPadMultiply = 0x3002c;
  802. const int KeyPress::numberPadDivide = 0x3002d;
  803. const int KeyPress::numberPadSeparator = 0x3002e;
  804. const int KeyPress::numberPadDecimalPoint = 0x3002f;
  805. const int KeyPress::numberPadEquals = 0x30030;
  806. const int KeyPress::numberPadDelete = 0x30031;
  807. const int KeyPress::playKey = 0x30000;
  808. const int KeyPress::stopKey = 0x30001;
  809. const int KeyPress::fastForwardKey = 0x30002;
  810. const int KeyPress::rewindKey = 0x30003;