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.

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