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.

1033 lines
33KB

  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& comp, int windowStyleFlags, UIView* viewToAttachTo);
  64. ~UIViewComponentPeer();
  65. //==============================================================================
  66. void* getNativeHandle() const;
  67. void setVisible (bool shouldBeVisible);
  68. void setTitle (const String& title);
  69. void setPosition (int x, int y);
  70. void setSize (int w, int h);
  71. void setBounds (int x, int y, int w, int h, bool isNowFullScreen);
  72. Rectangle<int> getBounds() const;
  73. Rectangle<int> getBounds (const bool global) const;
  74. Point<int> getScreenPosition() const;
  75. Point<int> localToGlobal (const Point<int>& relativePosition);
  76. Point<int> globalToLocal (const Point<int>& screenPosition);
  77. void setAlpha (float newAlpha);
  78. void setMinimised (bool shouldBeMinimised);
  79. bool isMinimised() const;
  80. void setFullScreen (bool shouldBeFullScreen);
  81. bool isFullScreen() const;
  82. bool contains (const Point<int>& position, bool trueIfInAChildWindow) const;
  83. BorderSize<int> getFrameSize() const;
  84. bool setAlwaysOnTop (bool alwaysOnTop);
  85. void toFront (bool makeActiveWindow);
  86. void toBehind (ComponentPeer* other);
  87. void setIcon (const Image& newIcon);
  88. virtual void drawRect (CGRect r);
  89. virtual bool canBecomeKeyWindow();
  90. virtual bool windowShouldClose();
  91. virtual void redirectMovedOrResized();
  92. virtual CGRect constrainRect (CGRect r);
  93. //==============================================================================
  94. virtual void viewFocusGain();
  95. virtual void viewFocusLoss();
  96. bool isFocused() const;
  97. void grabFocus();
  98. void textInputRequired (const Point<int>& position);
  99. virtual BOOL textViewReplaceCharacters (const Range<int>& range, const String& text);
  100. void updateHiddenTextContent (TextInputTarget* target);
  101. void globalFocusChanged (Component*);
  102. virtual BOOL shouldRotate (UIInterfaceOrientation interfaceOrientation);
  103. virtual void displayRotated();
  104. void handleTouches (UIEvent* e, bool isDown, bool isUp, bool isCancel);
  105. //==============================================================================
  106. void repaint (const Rectangle<int>& area);
  107. void performAnyPendingRepaintsNow();
  108. //==============================================================================
  109. UIWindow* window;
  110. JuceUIView* view;
  111. JuceUIViewController* controller;
  112. bool isSharedWindow, fullScreen, insideDrawRect;
  113. static ModifierKeys currentModifiers;
  114. static int64 getMouseTime (UIEvent* e)
  115. {
  116. return (Time::currentTimeMillis() - Time::getMillisecondCounter())
  117. + (int64) ([e timestamp] * 1000.0);
  118. }
  119. static Rectangle<int> rotatedScreenPosToReal (const Rectangle<int>& r)
  120. {
  121. const Rectangle<int> screen (convertToRectInt ([UIScreen mainScreen].bounds));
  122. switch ([[UIApplication sharedApplication] statusBarOrientation])
  123. {
  124. case UIInterfaceOrientationPortrait:
  125. return r;
  126. case UIInterfaceOrientationPortraitUpsideDown:
  127. return Rectangle<int> (screen.getWidth() - r.getRight(), screen.getHeight() - r.getBottom(),
  128. r.getWidth(), r.getHeight());
  129. case UIInterfaceOrientationLandscapeLeft:
  130. return Rectangle<int> (r.getY(), screen.getHeight() - r.getRight(),
  131. r.getHeight(), r.getWidth());
  132. case UIInterfaceOrientationLandscapeRight:
  133. return Rectangle<int> (screen.getWidth() - r.getBottom(), r.getX(),
  134. r.getHeight(), r.getWidth());
  135. default: jassertfalse; // unknown orientation!
  136. }
  137. return r;
  138. }
  139. static Rectangle<int> realScreenPosToRotated (const Rectangle<int>& r)
  140. {
  141. const Rectangle<int> screen (convertToRectInt ([UIScreen mainScreen].bounds));
  142. switch ([[UIApplication sharedApplication] statusBarOrientation])
  143. {
  144. case UIInterfaceOrientationPortrait:
  145. return r;
  146. case UIInterfaceOrientationPortraitUpsideDown:
  147. return Rectangle<int> (screen.getWidth() - r.getRight(), screen.getHeight() - r.getBottom(),
  148. r.getWidth(), r.getHeight());
  149. case UIInterfaceOrientationLandscapeLeft:
  150. return Rectangle<int> (screen.getHeight() - r.getBottom(), r.getX(),
  151. r.getHeight(), r.getWidth());
  152. case UIInterfaceOrientationLandscapeRight:
  153. return Rectangle<int> (r.getY(), screen.getWidth() - r.getRight(),
  154. r.getHeight(), r.getWidth());
  155. default: jassertfalse; // unknown orientation!
  156. }
  157. return r;
  158. }
  159. MultiTouchMapper<UITouch*> currentTouches;
  160. private:
  161. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (UIViewComponentPeer);
  162. };
  163. //==============================================================================
  164. } // (juce namespace)
  165. @implementation JuceUIViewController
  166. - (BOOL) shouldAutorotateToInterfaceOrientation: (UIInterfaceOrientation) interfaceOrientation
  167. {
  168. JuceUIView* juceView = (JuceUIView*) [self view];
  169. jassert (juceView != nil && juceView->owner != nullptr);
  170. return juceView->owner->shouldRotate (interfaceOrientation);
  171. }
  172. - (void) didRotateFromInterfaceOrientation: (UIInterfaceOrientation) fromInterfaceOrientation
  173. {
  174. JuceUIView* juceView = (JuceUIView*) [self view];
  175. jassert (juceView != nil && juceView->owner != nullptr);
  176. juceView->owner->displayRotated();
  177. [UIView setAnimationsEnabled: YES];
  178. }
  179. - (void) willRotateToInterfaceOrientation: (UIInterfaceOrientation) toInterfaceOrientation
  180. duration: (NSTimeInterval) duration
  181. {
  182. [UIView setAnimationsEnabled: NO]; // disable this because it goes the wrong way and looks like crap.
  183. }
  184. @end
  185. @implementation JuceUIView
  186. - (JuceUIView*) initWithOwner: (UIViewComponentPeer*) owner_
  187. withFrame: (CGRect) frame
  188. {
  189. [super initWithFrame: frame];
  190. owner = owner_;
  191. hiddenTextView = [[UITextView alloc] initWithFrame: CGRectMake (0, 0, 0, 0)];
  192. [self addSubview: hiddenTextView];
  193. hiddenTextView.delegate = self;
  194. hiddenTextView.autocapitalizationType = UITextAutocapitalizationTypeNone;
  195. hiddenTextView.autocorrectionType = UITextAutocorrectionTypeNo;
  196. return self;
  197. }
  198. - (void) dealloc
  199. {
  200. [hiddenTextView removeFromSuperview];
  201. [hiddenTextView release];
  202. [super dealloc];
  203. }
  204. //==============================================================================
  205. - (void) drawRect: (CGRect) r
  206. {
  207. if (owner != nullptr)
  208. owner->drawRect (r);
  209. }
  210. //==============================================================================
  211. bool KeyPress::isKeyCurrentlyDown (const int keyCode)
  212. {
  213. return false;
  214. }
  215. ModifierKeys UIViewComponentPeer::currentModifiers;
  216. ModifierKeys ModifierKeys::getCurrentModifiersRealtime() noexcept
  217. {
  218. return UIViewComponentPeer::currentModifiers;
  219. }
  220. void ModifierKeys::updateCurrentModifiers() noexcept
  221. {
  222. currentModifiers = UIViewComponentPeer::currentModifiers;
  223. }
  224. juce::Point<int> juce_lastMousePos;
  225. //==============================================================================
  226. - (void) touchesBegan: (NSSet*) touches withEvent: (UIEvent*) event
  227. {
  228. if (owner != nullptr)
  229. owner->handleTouches (event, true, false, false);
  230. }
  231. - (void) touchesMoved: (NSSet*) touches withEvent: (UIEvent*) event
  232. {
  233. if (owner != nullptr)
  234. owner->handleTouches (event, false, false, false);
  235. }
  236. - (void) touchesEnded: (NSSet*) touches withEvent: (UIEvent*) event
  237. {
  238. if (owner != nullptr)
  239. owner->handleTouches (event, false, true, false);
  240. }
  241. - (void) touchesCancelled: (NSSet*) touches withEvent: (UIEvent*) event
  242. {
  243. if (owner != nullptr)
  244. owner->handleTouches (event, false, true, true);
  245. [self touchesEnded: touches withEvent: event];
  246. }
  247. //==============================================================================
  248. - (BOOL) becomeFirstResponder
  249. {
  250. if (owner != nullptr)
  251. owner->viewFocusGain();
  252. return true;
  253. }
  254. - (BOOL) resignFirstResponder
  255. {
  256. if (owner != nullptr)
  257. owner->viewFocusLoss();
  258. return true;
  259. }
  260. - (BOOL) canBecomeFirstResponder
  261. {
  262. return owner != nullptr && owner->canBecomeKeyWindow();
  263. }
  264. - (BOOL) textView: (UITextView*) textView shouldChangeTextInRange: (NSRange) range replacementText: (NSString*) text
  265. {
  266. return owner->textViewReplaceCharacters (Range<int> (range.location, range.location + range.length),
  267. nsStringToJuce (text));
  268. }
  269. @end
  270. //==============================================================================
  271. @implementation JuceUIWindow
  272. - (void) setOwner: (UIViewComponentPeer*) owner_
  273. {
  274. owner = owner_;
  275. isZooming = false;
  276. }
  277. - (void) becomeKeyWindow
  278. {
  279. [super becomeKeyWindow];
  280. if (owner != nullptr)
  281. owner->grabFocus();
  282. }
  283. @end
  284. //==============================================================================
  285. //==============================================================================
  286. namespace juce
  287. {
  288. //==============================================================================
  289. UIViewComponentPeer::UIViewComponentPeer (Component& comp, const int windowStyleFlags, UIView* viewToAttachTo)
  290. : ComponentPeer (comp, 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) w, (CGFloat) h);
  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: view.window];
  392. r = [view.window convertRect: r toWindow: nil];
  393. return realScreenPosToRotated (convertToRectInt (r));
  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. static 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. static CGAffineTransform getCGTransformForDisplayOrientation (const Desktop::DisplayOrientation orientation) noexcept
  480. {
  481. switch (orientation)
  482. {
  483. case Desktop::upsideDown: return CGAffineTransformMake (-1, 0, 0, -1, 0, 0);
  484. case Desktop::rotatedClockwise: return CGAffineTransformMake (0, -1, 1, 0, 0, 0);
  485. case Desktop::rotatedAntiClockwise: return CGAffineTransformMake (0, 1, -1, 0, 0, 0);
  486. default: break;
  487. }
  488. return CGAffineTransformIdentity;
  489. }
  490. }
  491. BOOL UIViewComponentPeer::shouldRotate (UIInterfaceOrientation interfaceOrientation)
  492. {
  493. return Desktop::getInstance().isOrientationEnabled (convertToJuceOrientation (interfaceOrientation));
  494. }
  495. void UIViewComponentPeer::displayRotated()
  496. {
  497. Desktop& desktop = Desktop::getInstance();
  498. const Rectangle<int> oldArea (component.getBounds());
  499. const Rectangle<int> oldDesktop (desktop.getDisplays().getMainDisplay().userArea);
  500. const_cast <Desktop::Displays&> (desktop.getDisplays()).refresh();
  501. window.transform = getCGTransformForDisplayOrientation (desktop.getCurrentOrientation());
  502. view.transform = CGAffineTransformIdentity;
  503. if (fullScreen)
  504. {
  505. fullScreen = false;
  506. setFullScreen (true);
  507. }
  508. else if (! isSharedWindow)
  509. {
  510. // this will re-centre the window, but leave its size unchanged
  511. const float centreRelX = oldArea.getCentreX() / (float) oldDesktop.getWidth();
  512. const float centreRelY = oldArea.getCentreY() / (float) oldDesktop.getHeight();
  513. const Rectangle<int> newDesktop (desktop.getDisplays().getMainDisplay().userArea);
  514. const int x = ((int) (newDesktop.getWidth() * centreRelX)) - (oldArea.getWidth() / 2);
  515. const int y = ((int) (newDesktop.getHeight() * centreRelY)) - (oldArea.getHeight() / 2);
  516. setBounds (x, y, oldArea.getWidth(), oldArea.getHeight(), false);
  517. }
  518. }
  519. bool UIViewComponentPeer::contains (const Point<int>& position, bool trueIfInAChildWindow) const
  520. {
  521. if (! (isPositiveAndBelow (position.getX(), component.getWidth())
  522. && isPositiveAndBelow (position.getY(), component.getHeight())))
  523. return false;
  524. UIView* v = [view hitTest: CGPointMake ((CGFloat) position.getX(), (CGFloat) position.getY())
  525. withEvent: nil];
  526. if (trueIfInAChildWindow)
  527. return v != nil;
  528. return v == view;
  529. }
  530. BorderSize<int> UIViewComponentPeer::getFrameSize() const
  531. {
  532. return BorderSize<int>();
  533. }
  534. bool UIViewComponentPeer::setAlwaysOnTop (bool alwaysOnTop)
  535. {
  536. if (! isSharedWindow)
  537. window.windowLevel = alwaysOnTop ? UIWindowLevelAlert : UIWindowLevelNormal;
  538. return true;
  539. }
  540. void UIViewComponentPeer::toFront (bool makeActiveWindow)
  541. {
  542. if (isSharedWindow)
  543. [[view superview] bringSubviewToFront: view];
  544. if (window != nil && component.isVisible())
  545. [window makeKeyAndVisible];
  546. }
  547. void UIViewComponentPeer::toBehind (ComponentPeer* other)
  548. {
  549. UIViewComponentPeer* const otherPeer = dynamic_cast <UIViewComponentPeer*> (other);
  550. jassert (otherPeer != nullptr); // wrong type of window?
  551. if (otherPeer != nullptr)
  552. {
  553. if (isSharedWindow)
  554. {
  555. [[view superview] insertSubview: view belowSubview: otherPeer->view];
  556. }
  557. else
  558. {
  559. // don't know how to do this
  560. }
  561. }
  562. }
  563. void UIViewComponentPeer::setIcon (const Image& /*newIcon*/)
  564. {
  565. // to do..
  566. }
  567. //==============================================================================
  568. void UIViewComponentPeer::handleTouches (UIEvent* event, const bool isDown, const bool isUp, bool isCancel)
  569. {
  570. NSArray* touches = [[event touchesForView: view] allObjects];
  571. for (unsigned int i = 0; i < [touches count]; ++i)
  572. {
  573. UITouch* touch = [touches objectAtIndex: i];
  574. if ([touch phase] == UITouchPhaseStationary)
  575. continue;
  576. CGPoint p = [touch locationInView: view];
  577. const Point<int> pos ((int) p.x, (int) p.y);
  578. juce_lastMousePos = pos + getScreenPosition();
  579. const int64 time = getMouseTime (event);
  580. const int touchIndex = currentTouches.getIndexOfTouch (touch);
  581. ModifierKeys modsToSend (currentModifiers);
  582. if (isDown)
  583. {
  584. if ([touch phase] != UITouchPhaseBegan)
  585. continue;
  586. currentModifiers = currentModifiers.withoutMouseButtons().withFlags (ModifierKeys::leftButtonModifier);
  587. modsToSend = currentModifiers;
  588. // this forces a mouse-enter/up event, in case for some reason we didn't get a mouse-up before.
  589. handleMouseEvent (touchIndex, pos, modsToSend.withoutMouseButtons(), time);
  590. if (! isValidPeer (this)) // (in case this component was deleted by the event)
  591. return;
  592. }
  593. else if (isUp)
  594. {
  595. if (! ([touch phase] == UITouchPhaseEnded || [touch phase] == UITouchPhaseCancelled))
  596. continue;
  597. modsToSend = modsToSend.withoutMouseButtons();
  598. currentTouches.clearTouch (touchIndex);
  599. if (! currentTouches.areAnyTouchesActive())
  600. isCancel = true;
  601. }
  602. if (isCancel)
  603. {
  604. currentTouches.clear();
  605. modsToSend = currentModifiers = currentModifiers.withoutMouseButtons();
  606. }
  607. handleMouseEvent (touchIndex, pos, modsToSend, time);
  608. if (! isValidPeer (this)) // (in case this component was deleted by the event)
  609. return;
  610. if (isUp || isCancel)
  611. {
  612. handleMouseEvent (touchIndex, Point<int> (-1, -1), modsToSend, time);
  613. if (! isValidPeer (this))
  614. return;
  615. }
  616. }
  617. }
  618. //==============================================================================
  619. static UIViewComponentPeer* currentlyFocusedPeer = nullptr;
  620. void UIViewComponentPeer::viewFocusGain()
  621. {
  622. if (currentlyFocusedPeer != this)
  623. {
  624. if (ComponentPeer::isValidPeer (currentlyFocusedPeer))
  625. currentlyFocusedPeer->handleFocusLoss();
  626. currentlyFocusedPeer = this;
  627. handleFocusGain();
  628. }
  629. }
  630. void UIViewComponentPeer::viewFocusLoss()
  631. {
  632. if (currentlyFocusedPeer == this)
  633. {
  634. currentlyFocusedPeer = nullptr;
  635. handleFocusLoss();
  636. }
  637. }
  638. bool UIViewComponentPeer::isFocused() const
  639. {
  640. return isSharedWindow ? this == currentlyFocusedPeer
  641. : (window != nil && [window isKeyWindow]);
  642. }
  643. void UIViewComponentPeer::grabFocus()
  644. {
  645. if (window != nil)
  646. {
  647. [window makeKeyWindow];
  648. viewFocusGain();
  649. }
  650. }
  651. void UIViewComponentPeer::textInputRequired (const Point<int>&)
  652. {
  653. }
  654. void UIViewComponentPeer::updateHiddenTextContent (TextInputTarget* target)
  655. {
  656. view->hiddenTextView.text = juceStringToNS (target->getTextInRange (Range<int> (0, target->getHighlightedRegion().getStart())));
  657. view->hiddenTextView.selectedRange = NSMakeRange (target->getHighlightedRegion().getStart(), 0);
  658. }
  659. BOOL UIViewComponentPeer::textViewReplaceCharacters (const Range<int>& range, const String& text)
  660. {
  661. TextInputTarget* const target = findCurrentTextInputTarget();
  662. if (target != nullptr)
  663. {
  664. const Range<int> currentSelection (target->getHighlightedRegion());
  665. if (range.getLength() == 1 && text.isEmpty()) // (detect backspace)
  666. if (currentSelection.isEmpty())
  667. target->setHighlightedRegion (currentSelection.withStart (currentSelection.getStart() - 1));
  668. if (text == "\r" || text == "\n" || text == "\r\n")
  669. handleKeyPress (KeyPress::returnKey, text[0]);
  670. else
  671. target->insertTextAtCaret (text);
  672. updateHiddenTextContent (target);
  673. }
  674. return NO;
  675. }
  676. void UIViewComponentPeer::globalFocusChanged (Component*)
  677. {
  678. TextInputTarget* const target = findCurrentTextInputTarget();
  679. if (target != nullptr)
  680. {
  681. Component* comp = dynamic_cast<Component*> (target);
  682. Point<int> pos (component.getLocalPoint (comp, Point<int>()));
  683. view->hiddenTextView.frame = CGRectMake (pos.getX(), pos.getY(), 0, 0);
  684. updateHiddenTextContent (target);
  685. [view->hiddenTextView becomeFirstResponder];
  686. }
  687. else
  688. {
  689. [view->hiddenTextView resignFirstResponder];
  690. }
  691. }
  692. //==============================================================================
  693. void UIViewComponentPeer::drawRect (CGRect r)
  694. {
  695. if (r.size.width < 1.0f || r.size.height < 1.0f)
  696. return;
  697. CGContextRef cg = UIGraphicsGetCurrentContext();
  698. if (! component.isOpaque())
  699. CGContextClearRect (cg, CGContextGetClipBoundingBox (cg));
  700. CGContextConcatCTM (cg, CGAffineTransformMake (1, 0, 0, -1, 0, view.bounds.size.height));
  701. CoreGraphicsContext g (cg, view.bounds.size.height, [UIScreen mainScreen].scale);
  702. insideDrawRect = true;
  703. handlePaint (g);
  704. insideDrawRect = false;
  705. }
  706. bool UIViewComponentPeer::canBecomeKeyWindow()
  707. {
  708. return (getStyleFlags() & juce::ComponentPeer::windowIgnoresKeyPresses) == 0;
  709. }
  710. bool UIViewComponentPeer::windowShouldClose()
  711. {
  712. if (! isValidPeer (this))
  713. return YES;
  714. handleUserClosingWindow();
  715. return NO;
  716. }
  717. void UIViewComponentPeer::redirectMovedOrResized()
  718. {
  719. handleMovedOrResized();
  720. }
  721. //==============================================================================
  722. void Desktop::setKioskComponent (Component* kioskModeComponent, bool enableOrDisable, bool allowMenusAndBars)
  723. {
  724. [[UIApplication sharedApplication] setStatusBarHidden: enableOrDisable
  725. withAnimation: UIStatusBarAnimationSlide];
  726. displays.refresh();
  727. ComponentPeer* const peer = kioskModeComponent->getPeer();
  728. if (peer != nullptr)
  729. peer->setFullScreen (enableOrDisable);
  730. }
  731. //==============================================================================
  732. class AsyncRepaintMessage : public CallbackMessage
  733. {
  734. public:
  735. UIViewComponentPeer* const peer;
  736. const Rectangle<int> rect;
  737. AsyncRepaintMessage (UIViewComponentPeer* const peer_, const Rectangle<int>& rect_)
  738. : peer (peer_), rect (rect_)
  739. {
  740. }
  741. void messageCallback()
  742. {
  743. if (ComponentPeer::isValidPeer (peer))
  744. peer->repaint (rect);
  745. }
  746. };
  747. void UIViewComponentPeer::repaint (const Rectangle<int>& area)
  748. {
  749. if (insideDrawRect || ! MessageManager::getInstance()->isThisTheMessageThread())
  750. {
  751. (new AsyncRepaintMessage (this, area))->post();
  752. }
  753. else
  754. {
  755. [view setNeedsDisplayInRect: convertToCGRect (area)];
  756. }
  757. }
  758. void UIViewComponentPeer::performAnyPendingRepaintsNow()
  759. {
  760. }
  761. ComponentPeer* Component::createNewPeer (int styleFlags, void* windowToAttachTo)
  762. {
  763. return new UIViewComponentPeer (*this, styleFlags, (UIView*) windowToAttachTo);
  764. }
  765. //==============================================================================
  766. const int KeyPress::spaceKey = ' ';
  767. const int KeyPress::returnKey = 0x0d;
  768. const int KeyPress::escapeKey = 0x1b;
  769. const int KeyPress::backspaceKey = 0x7f;
  770. const int KeyPress::leftKey = 0x1000;
  771. const int KeyPress::rightKey = 0x1001;
  772. const int KeyPress::upKey = 0x1002;
  773. const int KeyPress::downKey = 0x1003;
  774. const int KeyPress::pageUpKey = 0x1004;
  775. const int KeyPress::pageDownKey = 0x1005;
  776. const int KeyPress::endKey = 0x1006;
  777. const int KeyPress::homeKey = 0x1007;
  778. const int KeyPress::deleteKey = 0x1008;
  779. const int KeyPress::insertKey = -1;
  780. const int KeyPress::tabKey = 9;
  781. const int KeyPress::F1Key = 0x2001;
  782. const int KeyPress::F2Key = 0x2002;
  783. const int KeyPress::F3Key = 0x2003;
  784. const int KeyPress::F4Key = 0x2004;
  785. const int KeyPress::F5Key = 0x2005;
  786. const int KeyPress::F6Key = 0x2006;
  787. const int KeyPress::F7Key = 0x2007;
  788. const int KeyPress::F8Key = 0x2008;
  789. const int KeyPress::F9Key = 0x2009;
  790. const int KeyPress::F10Key = 0x200a;
  791. const int KeyPress::F11Key = 0x200b;
  792. const int KeyPress::F12Key = 0x200c;
  793. const int KeyPress::F13Key = 0x200d;
  794. const int KeyPress::F14Key = 0x200e;
  795. const int KeyPress::F15Key = 0x200f;
  796. const int KeyPress::F16Key = 0x2010;
  797. const int KeyPress::numberPad0 = 0x30020;
  798. const int KeyPress::numberPad1 = 0x30021;
  799. const int KeyPress::numberPad2 = 0x30022;
  800. const int KeyPress::numberPad3 = 0x30023;
  801. const int KeyPress::numberPad4 = 0x30024;
  802. const int KeyPress::numberPad5 = 0x30025;
  803. const int KeyPress::numberPad6 = 0x30026;
  804. const int KeyPress::numberPad7 = 0x30027;
  805. const int KeyPress::numberPad8 = 0x30028;
  806. const int KeyPress::numberPad9 = 0x30029;
  807. const int KeyPress::numberPadAdd = 0x3002a;
  808. const int KeyPress::numberPadSubtract = 0x3002b;
  809. const int KeyPress::numberPadMultiply = 0x3002c;
  810. const int KeyPress::numberPadDivide = 0x3002d;
  811. const int KeyPress::numberPadSeparator = 0x3002e;
  812. const int KeyPress::numberPadDecimalPoint = 0x3002f;
  813. const int KeyPress::numberPadEquals = 0x30030;
  814. const int KeyPress::numberPadDelete = 0x30031;
  815. const int KeyPress::playKey = 0x30000;
  816. const int KeyPress::stopKey = 0x30001;
  817. const int KeyPress::fastForwardKey = 0x30002;
  818. const int KeyPress::rewindKey = 0x30003;