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.

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