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.

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