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.

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