|
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099 |
- /*
- ==============================================================================
-
- This file is part of the JUCE library.
- Copyright (c) 2015 - ROLI Ltd.
-
- Permission is granted to use this software under the terms of either:
- a) the GPL v2 (or any later version)
- b) the Affero GPL v3
-
- Details of these licenses can be found at: www.gnu.org/licenses
-
- JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
- WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
- A PARTICULAR PURPOSE. See the GNU General Public License for more details.
-
- ------------------------------------------------------------------------------
-
- To release a closed-source product which uses JUCE, commercial licenses are
- available: visit www.juce.com for more information.
-
- ==============================================================================
- */
-
- class UIViewComponentPeer;
-
- // The way rotation works changed in iOS8..
- static bool isUsingOldRotationMethod() noexcept
- {
- static bool isPreV8 = ([[[UIDevice currentDevice] systemVersion] compare: @"8.0"
- options: NSNumericSearch] == NSOrderedAscending);
- return isPreV8;
- }
-
- namespace Orientations
- {
- static Desktop::DisplayOrientation convertToJuce (UIInterfaceOrientation orientation)
- {
- switch (orientation)
- {
- case UIInterfaceOrientationPortrait: return Desktop::upright;
- case UIInterfaceOrientationPortraitUpsideDown: return Desktop::upsideDown;
- case UIInterfaceOrientationLandscapeLeft: return Desktop::rotatedClockwise;
- case UIInterfaceOrientationLandscapeRight: return Desktop::rotatedAntiClockwise;
- default: jassertfalse; // unknown orientation!
- }
-
- return Desktop::upright;
- }
-
- static CGAffineTransform getCGTransformFor (const Desktop::DisplayOrientation orientation) noexcept
- {
- if (isUsingOldRotationMethod())
- {
- switch (orientation)
- {
- case Desktop::upsideDown: return CGAffineTransformMake (-1, 0, 0, -1, 0, 0);
- case Desktop::rotatedClockwise: return CGAffineTransformMake (0, -1, 1, 0, 0, 0);
- case Desktop::rotatedAntiClockwise: return CGAffineTransformMake (0, 1, -1, 0, 0, 0);
- default: break;
- }
- }
-
- return CGAffineTransformIdentity;
- }
-
- static NSUInteger getSupportedOrientations()
- {
- NSUInteger allowed = 0;
- Desktop& d = Desktop::getInstance();
-
- if (d.isOrientationEnabled (Desktop::upright)) allowed |= UIInterfaceOrientationMaskPortrait;
- if (d.isOrientationEnabled (Desktop::upsideDown)) allowed |= UIInterfaceOrientationMaskPortraitUpsideDown;
- if (d.isOrientationEnabled (Desktop::rotatedClockwise)) allowed |= UIInterfaceOrientationMaskLandscapeLeft;
- if (d.isOrientationEnabled (Desktop::rotatedAntiClockwise)) allowed |= UIInterfaceOrientationMaskLandscapeRight;
-
- return allowed;
- }
- }
-
- //==============================================================================
- } // (juce namespace)
-
- using namespace juce;
-
-
- @interface JuceUIView : UIView <UITextViewDelegate>
- {
- @public
- UIViewComponentPeer* owner;
- UITextView* hiddenTextView;
- }
-
- - (JuceUIView*) initWithOwner: (UIViewComponentPeer*) owner withFrame: (CGRect) frame;
- - (void) dealloc;
-
- - (void) drawRect: (CGRect) r;
-
- - (void) touchesBegan: (NSSet*) touches withEvent: (UIEvent*) event;
- - (void) touchesMoved: (NSSet*) touches withEvent: (UIEvent*) event;
- - (void) touchesEnded: (NSSet*) touches withEvent: (UIEvent*) event;
- - (void) touchesCancelled: (NSSet*) touches withEvent: (UIEvent*) event;
-
- - (BOOL) becomeFirstResponder;
- - (BOOL) resignFirstResponder;
- - (BOOL) canBecomeFirstResponder;
-
- - (BOOL) textView: (UITextView*) textView shouldChangeTextInRange: (NSRange) range replacementText: (NSString*) text;
- @end
-
- //==============================================================================
- @interface JuceUIViewController : UIViewController
- {
- }
-
- - (NSUInteger) supportedInterfaceOrientations;
- - (BOOL) shouldAutorotateToInterfaceOrientation: (UIInterfaceOrientation) interfaceOrientation;
- - (void) willRotateToInterfaceOrientation: (UIInterfaceOrientation) toInterfaceOrientation duration: (NSTimeInterval) duration;
- - (void) didRotateFromInterfaceOrientation: (UIInterfaceOrientation) fromInterfaceOrientation;
- - (void) viewWillTransitionToSize: (CGSize) size withTransitionCoordinator: (id<UIViewControllerTransitionCoordinator>) coordinator;
- - (BOOL) prefersStatusBarHidden;
- - (UIStatusBarStyle) preferredStatusBarStyle;
-
- - (void) viewDidLoad;
- - (void) viewWillAppear: (BOOL) animated;
- - (void) viewDidAppear: (BOOL) animated;
- - (void) viewWillLayoutSubviews;
- - (void) viewDidLayoutSubviews;
- @end
-
- //==============================================================================
- @interface JuceUIWindow : UIWindow
- {
- @private
- UIViewComponentPeer* owner;
- }
-
- - (void) setOwner: (UIViewComponentPeer*) owner;
- - (void) becomeKeyWindow;
- @end
-
- //==============================================================================
- //==============================================================================
- namespace juce
- {
-
- class UIViewComponentPeer : public ComponentPeer,
- public FocusChangeListener
- {
- public:
- UIViewComponentPeer (Component&, int windowStyleFlags, UIView* viewToAttachTo);
- ~UIViewComponentPeer();
-
- //==============================================================================
- void* getNativeHandle() const override { return view; }
- void setVisible (bool shouldBeVisible) override;
- void setTitle (const String& title) override;
- void setBounds (const Rectangle<int>&, bool isNowFullScreen) override;
-
- Rectangle<int> getBounds() const override { return getBounds (! isSharedWindow); }
- Rectangle<int> getBounds (bool global) const;
- Point<float> localToGlobal (Point<float> relativePosition) override;
- Point<float> globalToLocal (Point<float> screenPosition) override;
- void setAlpha (float newAlpha) override;
- void setMinimised (bool) override {}
- bool isMinimised() const override { return false; }
- void setFullScreen (bool shouldBeFullScreen) override;
- bool isFullScreen() const override { return fullScreen; }
- bool contains (Point<int> localPos, bool trueIfInAChildWindow) const override;
- BorderSize<int> getFrameSize() const override { return BorderSize<int>(); }
- bool setAlwaysOnTop (bool alwaysOnTop) override;
- void toFront (bool makeActiveWindow) override;
- void toBehind (ComponentPeer* other) override;
- void setIcon (const Image& newIcon) override;
- StringArray getAvailableRenderingEngines() override { return StringArray ("CoreGraphics Renderer"); }
-
- void drawRect (CGRect);
- bool canBecomeKeyWindow();
-
- //==============================================================================
- void viewFocusGain();
- void viewFocusLoss();
- bool isFocused() const override;
- void grabFocus() override;
- void textInputRequired (Point<int>, TextInputTarget&) override;
-
- BOOL textViewReplaceCharacters (Range<int>, const String&);
- void updateHiddenTextContent (TextInputTarget*);
- void globalFocusChanged (Component*) override;
-
- void updateTransformAndScreenBounds();
-
- void handleTouches (UIEvent*, bool isDown, bool isUp, bool isCancel);
-
- //==============================================================================
- void repaint (const Rectangle<int>& area) override;
- void performAnyPendingRepaintsNow() override;
-
- //==============================================================================
- UIWindow* window;
- JuceUIView* view;
- JuceUIViewController* controller;
- bool isSharedWindow, fullScreen, insideDrawRect;
- static ModifierKeys currentModifiers;
-
- static int64 getMouseTime (UIEvent* e) noexcept
- {
- return (Time::currentTimeMillis() - Time::getMillisecondCounter())
- + (int64) ([e timestamp] * 1000.0);
- }
-
- static Rectangle<int> rotatedScreenPosToReal (const Rectangle<int>& r)
- {
- if (isUsingOldRotationMethod())
- {
- const Rectangle<int> screen (convertToRectInt ([UIScreen mainScreen].bounds));
-
- switch ([[UIApplication sharedApplication] statusBarOrientation])
- {
- case UIInterfaceOrientationPortrait:
- return r;
-
- case UIInterfaceOrientationPortraitUpsideDown:
- return Rectangle<int> (screen.getWidth() - r.getRight(), screen.getHeight() - r.getBottom(),
- r.getWidth(), r.getHeight());
-
- case UIInterfaceOrientationLandscapeLeft:
- return Rectangle<int> (r.getY(), screen.getHeight() - r.getRight(),
- r.getHeight(), r.getWidth());
-
- case UIInterfaceOrientationLandscapeRight:
- return Rectangle<int> (screen.getWidth() - r.getBottom(), r.getX(),
- r.getHeight(), r.getWidth());
-
- default: jassertfalse; // unknown orientation!
- }
- }
-
- return r;
- }
-
- static Rectangle<int> realScreenPosToRotated (const Rectangle<int>& r)
- {
- if (isUsingOldRotationMethod())
- {
- const Rectangle<int> screen (convertToRectInt ([UIScreen mainScreen].bounds));
-
- switch ([[UIApplication sharedApplication] statusBarOrientation])
- {
- case UIInterfaceOrientationPortrait:
- return r;
-
- case UIInterfaceOrientationPortraitUpsideDown:
- return Rectangle<int> (screen.getWidth() - r.getRight(), screen.getHeight() - r.getBottom(),
- r.getWidth(), r.getHeight());
-
- case UIInterfaceOrientationLandscapeLeft:
- return Rectangle<int> (screen.getHeight() - r.getBottom(), r.getX(),
- r.getHeight(), r.getWidth());
-
- case UIInterfaceOrientationLandscapeRight:
- return Rectangle<int> (r.getY(), screen.getWidth() - r.getRight(),
- r.getHeight(), r.getWidth());
-
- default: jassertfalse; // unknown orientation!
- }
- }
-
- return r;
- }
-
- MultiTouchMapper<UITouch*> currentTouches;
-
- private:
- JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (UIViewComponentPeer)
-
- class AsyncRepaintMessage : public CallbackMessage
- {
- public:
- UIViewComponentPeer* const peer;
- const Rectangle<int> rect;
-
- AsyncRepaintMessage (UIViewComponentPeer* const p, const Rectangle<int>& r)
- : peer (p), rect (r)
- {
- }
-
- void messageCallback() override
- {
- if (ComponentPeer::isValidPeer (peer))
- peer->repaint (rect);
- }
- };
- };
-
- static void sendScreenBoundsUpdate (JuceUIViewController* c)
- {
- JuceUIView* juceView = (JuceUIView*) [c view];
- jassert (juceView != nil && juceView->owner != nullptr);
- juceView->owner->updateTransformAndScreenBounds();
- }
-
- static bool isKioskModeView (JuceUIViewController* c)
- {
- JuceUIView* juceView = (JuceUIView*) [c view];
- jassert (juceView != nil && juceView->owner != nullptr);
-
- return Desktop::getInstance().getKioskModeComponent() == &(juceView->owner->getComponent());
- }
-
-
- } // (juce namespace)
-
- //==============================================================================
- //==============================================================================
- @implementation JuceUIViewController
-
- - (NSUInteger) supportedInterfaceOrientations
- {
- return Orientations::getSupportedOrientations();
- }
-
- - (BOOL) shouldAutorotateToInterfaceOrientation: (UIInterfaceOrientation) interfaceOrientation
- {
- return Desktop::getInstance().isOrientationEnabled (Orientations::convertToJuce (interfaceOrientation));
- }
-
- - (void) willRotateToInterfaceOrientation: (UIInterfaceOrientation) toInterfaceOrientation
- duration: (NSTimeInterval) duration
- {
- ignoreUnused (toInterfaceOrientation, duration);
-
- [UIView setAnimationsEnabled: NO]; // disable this because it goes the wrong way and looks like crap.
- }
-
- - (void) didRotateFromInterfaceOrientation: (UIInterfaceOrientation) fromInterfaceOrientation
- {
- ignoreUnused (fromInterfaceOrientation);
- sendScreenBoundsUpdate (self);
- [UIView setAnimationsEnabled: YES];
- }
-
- - (void) viewWillTransitionToSize: (CGSize) size withTransitionCoordinator: (id<UIViewControllerTransitionCoordinator>) coordinator
- {
- [super viewWillTransitionToSize: size withTransitionCoordinator: coordinator];
- sendScreenBoundsUpdate (self);
-
- // On some devices the screen-size isn't yet updated at this point, so also trigger another
- // async update to double-check..
- MessageManager::callAsync ([=]() { sendScreenBoundsUpdate (self); });
- }
-
- - (BOOL) prefersStatusBarHidden
- {
- return isKioskModeView (self);
- }
-
- - (UIStatusBarStyle) preferredStatusBarStyle
- {
- return UIStatusBarStyleDefault;
- }
-
- - (void) viewDidLoad
- {
- sendScreenBoundsUpdate (self);
- }
-
- - (void) viewWillAppear: (BOOL) animated
- {
- ignoreUnused (animated);
- [self viewDidLoad];
- }
-
- - (void) viewDidAppear: (BOOL) animated
- {
- ignoreUnused (animated);
- [self viewDidLoad];
- }
-
- - (void) viewWillLayoutSubviews
- {
- [self viewDidLoad];
- }
-
- - (void) viewDidLayoutSubviews
- {
- [self viewDidLoad];
- }
-
- @end
-
- @implementation JuceUIView
-
- - (JuceUIView*) initWithOwner: (UIViewComponentPeer*) peer
- withFrame: (CGRect) frame
- {
- [super initWithFrame: frame];
- owner = peer;
-
- hiddenTextView = [[UITextView alloc] initWithFrame: CGRectZero];
- [self addSubview: hiddenTextView];
- hiddenTextView.delegate = self;
-
- hiddenTextView.autocapitalizationType = UITextAutocapitalizationTypeNone;
- hiddenTextView.autocorrectionType = UITextAutocorrectionTypeNo;
-
- return self;
- }
-
- - (void) dealloc
- {
- [hiddenTextView removeFromSuperview];
- [hiddenTextView release];
-
- [super dealloc];
- }
-
- //==============================================================================
- - (void) drawRect: (CGRect) r
- {
- if (owner != nullptr)
- owner->drawRect (r);
- }
-
- //==============================================================================
- - (void) touchesBegan: (NSSet*) touches withEvent: (UIEvent*) event
- {
- ignoreUnused (touches);
-
- if (owner != nullptr)
- owner->handleTouches (event, true, false, false);
- }
-
- - (void) touchesMoved: (NSSet*) touches withEvent: (UIEvent*) event
- {
- ignoreUnused (touches);
-
- if (owner != nullptr)
- owner->handleTouches (event, false, false, false);
- }
-
- - (void) touchesEnded: (NSSet*) touches withEvent: (UIEvent*) event
- {
- ignoreUnused (touches);
-
- if (owner != nullptr)
- owner->handleTouches (event, false, true, false);
- }
-
- - (void) touchesCancelled: (NSSet*) touches withEvent: (UIEvent*) event
- {
- if (owner != nullptr)
- owner->handleTouches (event, false, true, true);
-
- [self touchesEnded: touches withEvent: event];
- }
-
- //==============================================================================
- - (BOOL) becomeFirstResponder
- {
- if (owner != nullptr)
- owner->viewFocusGain();
-
- return true;
- }
-
- - (BOOL) resignFirstResponder
- {
- if (owner != nullptr)
- owner->viewFocusLoss();
-
- return true;
- }
-
- - (BOOL) canBecomeFirstResponder
- {
- return owner != nullptr && owner->canBecomeKeyWindow();
- }
-
- - (BOOL) textView: (UITextView*) textView shouldChangeTextInRange: (NSRange) range replacementText: (NSString*) text
- {
- ignoreUnused (textView);
- return owner->textViewReplaceCharacters (Range<int> ((int) range.location, (int) (range.location + range.length)),
- nsStringToJuce (text));
- }
-
- @end
-
- //==============================================================================
- @implementation JuceUIWindow
-
- - (void) setOwner: (UIViewComponentPeer*) peer
- {
- owner = peer;
- }
-
- - (void) becomeKeyWindow
- {
- [super becomeKeyWindow];
-
- if (owner != nullptr)
- owner->grabFocus();
- }
-
- @end
-
- //==============================================================================
- //==============================================================================
- namespace juce
- {
-
- bool KeyPress::isKeyCurrentlyDown (int)
- {
- return false;
- }
-
- ModifierKeys UIViewComponentPeer::currentModifiers;
-
- ModifierKeys ModifierKeys::getCurrentModifiersRealtime() noexcept
- {
- return UIViewComponentPeer::currentModifiers;
- }
-
- void ModifierKeys::updateCurrentModifiers() noexcept
- {
- currentModifiers = UIViewComponentPeer::currentModifiers;
- }
-
- Point<float> juce_lastMousePos;
-
- //==============================================================================
- UIViewComponentPeer::UIViewComponentPeer (Component& comp, const int windowStyleFlags, UIView* viewToAttachTo)
- : ComponentPeer (comp, windowStyleFlags),
- window (nil),
- view (nil),
- controller (nil),
- isSharedWindow (viewToAttachTo != nil),
- fullScreen (false),
- insideDrawRect (false)
- {
- CGRect r = convertToCGRect (component.getBounds());
-
- view = [[JuceUIView alloc] initWithOwner: this withFrame: r];
-
- view.multipleTouchEnabled = YES;
- view.hidden = true;
- view.opaque = component.isOpaque();
- view.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent: 0];
- view.transform = CGAffineTransformIdentity;
-
- if (isSharedWindow)
- {
- window = [viewToAttachTo window];
- [viewToAttachTo addSubview: view];
- }
- else
- {
- r = convertToCGRect (rotatedScreenPosToReal (component.getBounds()));
- r.origin.y = [UIScreen mainScreen].bounds.size.height - (r.origin.y + r.size.height);
-
- window = [[JuceUIWindow alloc] initWithFrame: r];
- [((JuceUIWindow*) window) setOwner: this];
-
- controller = [[JuceUIViewController alloc] init];
- controller.view = view;
- window.rootViewController = controller;
-
- window.hidden = true;
- window.autoresizesSubviews = NO;
- window.transform = Orientations::getCGTransformFor (Desktop::getInstance().getCurrentOrientation());
- window.opaque = component.isOpaque();
- window.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent: 0];
-
- if (component.isAlwaysOnTop())
- window.windowLevel = UIWindowLevelAlert;
-
- view.frame = CGRectMake (0, 0, r.size.width, r.size.height);
-
- [window addSubview: view];
- }
-
- setTitle (component.getName());
- setVisible (component.isVisible());
-
- Desktop::getInstance().addFocusChangeListener (this);
- }
-
- UIViewComponentPeer::~UIViewComponentPeer()
- {
- Desktop::getInstance().removeFocusChangeListener (this);
-
- view->owner = nullptr;
- [view removeFromSuperview];
- [view release];
- [controller release];
-
- if (! isSharedWindow)
- {
- [((JuceUIWindow*) window) setOwner: nil];
- [window release];
- }
- }
-
- //==============================================================================
- void UIViewComponentPeer::setVisible (bool shouldBeVisible)
- {
- if (! isSharedWindow)
- window.hidden = ! shouldBeVisible;
-
- view.hidden = ! shouldBeVisible;
- }
-
- void UIViewComponentPeer::setTitle (const String&)
- {
- // xxx is this possible?
- }
-
- void UIViewComponentPeer::setBounds (const Rectangle<int>& newBounds, const bool isNowFullScreen)
- {
- fullScreen = isNowFullScreen;
-
- if (isSharedWindow)
- {
- CGRect r = convertToCGRect (newBounds);
-
- if (view.frame.size.width != r.size.width || view.frame.size.height != r.size.height)
- [view setNeedsDisplay];
-
- view.frame = r;
- }
- else
- {
- window.frame = convertToCGRect (rotatedScreenPosToReal (newBounds));
- view.frame = CGRectMake (0, 0, (CGFloat) newBounds.getWidth(), (CGFloat) newBounds.getHeight());
-
- handleMovedOrResized();
- }
- }
-
- Rectangle<int> UIViewComponentPeer::getBounds (const bool global) const
- {
- CGRect r = view.frame;
-
- if (global && view.window != nil)
- {
- r = [view convertRect: r toView: view.window];
- r = [view.window convertRect: r toWindow: nil];
-
- return realScreenPosToRotated (convertToRectInt (r));
- }
-
- return convertToRectInt (r);
- }
-
- Point<float> UIViewComponentPeer::localToGlobal (Point<float> relativePosition)
- {
- return relativePosition + getBounds (true).getPosition().toFloat();
- }
-
- Point<float> UIViewComponentPeer::globalToLocal (Point<float> screenPosition)
- {
- return screenPosition - getBounds (true).getPosition().toFloat();
- }
-
- void UIViewComponentPeer::setAlpha (float newAlpha)
- {
- [view.window setAlpha: (CGFloat) newAlpha];
- }
-
- void UIViewComponentPeer::setFullScreen (bool shouldBeFullScreen)
- {
- if (! isSharedWindow)
- {
- Rectangle<int> r (shouldBeFullScreen ? Desktop::getInstance().getDisplays().getMainDisplay().userArea
- : lastNonFullscreenBounds);
-
- if ((! shouldBeFullScreen) && r.isEmpty())
- r = getBounds();
-
- // (can't call the component's setBounds method because that'll reset our fullscreen flag)
- if (! r.isEmpty())
- setBounds (ScalingHelpers::scaledScreenPosToUnscaled (component, r), shouldBeFullScreen);
-
- component.repaint();
- }
- }
-
- void UIViewComponentPeer::updateTransformAndScreenBounds()
- {
- Desktop& desktop = Desktop::getInstance();
- const Rectangle<int> oldArea (component.getBounds());
- const Rectangle<int> oldDesktop (desktop.getDisplays().getMainDisplay().userArea);
-
- const_cast<Desktop::Displays&> (desktop.getDisplays()).refresh();
-
- window.transform = Orientations::getCGTransformFor (desktop.getCurrentOrientation());
- view.transform = CGAffineTransformIdentity;
-
- if (fullScreen)
- {
- fullScreen = false;
- setFullScreen (true);
- }
- else if (! isSharedWindow)
- {
- // this will re-centre the window, but leave its size unchanged
- const float centreRelX = oldArea.getCentreX() / (float) oldDesktop.getWidth();
- const float centreRelY = oldArea.getCentreY() / (float) oldDesktop.getHeight();
-
- const Rectangle<int> newDesktop (desktop.getDisplays().getMainDisplay().userArea);
-
- const int x = ((int) (newDesktop.getWidth() * centreRelX)) - (oldArea.getWidth() / 2);
- const int y = ((int) (newDesktop.getHeight() * centreRelY)) - (oldArea.getHeight() / 2);
-
- setBounds (oldArea.withPosition (x, y), false);
- }
-
- [view setNeedsDisplay];
- }
-
- bool UIViewComponentPeer::contains (Point<int> localPos, bool trueIfInAChildWindow) const
- {
- {
- Rectangle<int> localBounds =
- ScalingHelpers::scaledScreenPosToUnscaled (component, component.getLocalBounds());
-
- if (! localBounds.contains (localPos))
- return false;
- }
-
- UIView* v = [view hitTest: convertToCGPoint (localPos)
- withEvent: nil];
-
- if (trueIfInAChildWindow)
- return v != nil;
-
- return v == view;
- }
-
- bool UIViewComponentPeer::setAlwaysOnTop (bool alwaysOnTop)
- {
- if (! isSharedWindow)
- window.windowLevel = alwaysOnTop ? UIWindowLevelAlert : UIWindowLevelNormal;
-
- return true;
- }
-
- void UIViewComponentPeer::toFront (bool makeActiveWindow)
- {
- if (isSharedWindow)
- [[view superview] bringSubviewToFront: view];
-
- if (makeActiveWindow && window != nil && component.isVisible())
- [window makeKeyAndVisible];
- }
-
- void UIViewComponentPeer::toBehind (ComponentPeer* other)
- {
- if (UIViewComponentPeer* const otherPeer = dynamic_cast<UIViewComponentPeer*> (other))
- {
- if (isSharedWindow)
- {
- [[view superview] insertSubview: view belowSubview: otherPeer->view];
- }
- else
- {
- // don't know how to do this
- }
- }
- else
- {
- jassertfalse; // wrong type of window?
- }
- }
-
- void UIViewComponentPeer::setIcon (const Image& /*newIcon*/)
- {
- // to do..
- }
-
- //==============================================================================
- static float getMaximumTouchForce (UITouch* touch) noexcept
- {
- #if defined (__IPHONE_9_0) && __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_9_0
- if ([touch respondsToSelector: @selector (maximumPossibleForce)])
- return (float) touch.maximumPossibleForce;
- #endif
-
- return 0.0f;
- }
-
- static float getTouchForce (UITouch* touch) noexcept
- {
- #if defined (__IPHONE_9_0) && __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_9_0
- if ([touch respondsToSelector: @selector (force)])
- return (float) touch.force;
- #endif
-
- return 0.0f;
- }
-
- void UIViewComponentPeer::handleTouches (UIEvent* event, const bool isDown, const bool isUp, bool isCancel)
- {
- NSArray* touches = [[event touchesForView: view] allObjects];
-
- for (unsigned int i = 0; i < [touches count]; ++i)
- {
- UITouch* touch = [touches objectAtIndex: i];
- const float maximumForce = getMaximumTouchForce (touch);
-
- if ([touch phase] == UITouchPhaseStationary && maximumForce <= 0)
- continue;
-
- CGPoint p = [touch locationInView: view];
- const Point<float> pos (static_cast<float> (p.x), static_cast<float> (p.y));
- juce_lastMousePos = pos + getBounds (true).getPosition().toFloat();
-
- const int64 time = getMouseTime (event);
- const int touchIndex = currentTouches.getIndexOfTouch (touch);
-
- ModifierKeys modsToSend (currentModifiers);
-
- if (isDown)
- {
- if ([touch phase] != UITouchPhaseBegan)
- continue;
-
- currentModifiers = currentModifiers.withoutMouseButtons().withFlags (ModifierKeys::leftButtonModifier);
- modsToSend = currentModifiers;
-
- // this forces a mouse-enter/up event, in case for some reason we didn't get a mouse-up before.
- handleMouseEvent (touchIndex, pos, modsToSend.withoutMouseButtons(),
- MouseInputSource::invalidPressure, time);
-
- if (! isValidPeer (this)) // (in case this component was deleted by the event)
- return;
- }
- else if (isUp)
- {
- if (! ([touch phase] == UITouchPhaseEnded || [touch phase] == UITouchPhaseCancelled))
- continue;
-
- modsToSend = modsToSend.withoutMouseButtons();
- currentTouches.clearTouch (touchIndex);
-
- if (! currentTouches.areAnyTouchesActive())
- isCancel = true;
- }
-
- if (isCancel)
- {
- currentTouches.clearTouch (touchIndex);
- modsToSend = currentModifiers = currentModifiers.withoutMouseButtons();
- }
-
- // NB: some devices return 0 or 1.0 if pressure is unknown, so we'll clip our value to a believable range:
- float pressure = maximumForce > 0 ? jlimit (0.0001f, 0.9999f, getTouchForce (touch) / maximumForce)
- : MouseInputSource::invalidPressure;
-
- handleMouseEvent (touchIndex, pos, modsToSend, pressure, time);
-
- if (! isValidPeer (this)) // (in case this component was deleted by the event)
- return;
-
- if (isUp || isCancel)
- {
- handleMouseEvent (touchIndex, Point<float> (-1.0f, -1.0f),
- modsToSend, MouseInputSource::invalidPressure, time);
-
- if (! isValidPeer (this))
- return;
- }
- }
- }
-
- //==============================================================================
- static UIViewComponentPeer* currentlyFocusedPeer = nullptr;
-
- void UIViewComponentPeer::viewFocusGain()
- {
- if (currentlyFocusedPeer != this)
- {
- if (ComponentPeer::isValidPeer (currentlyFocusedPeer))
- currentlyFocusedPeer->handleFocusLoss();
-
- currentlyFocusedPeer = this;
-
- handleFocusGain();
- }
- }
-
- void UIViewComponentPeer::viewFocusLoss()
- {
- if (currentlyFocusedPeer == this)
- {
- currentlyFocusedPeer = nullptr;
- handleFocusLoss();
- }
- }
-
- bool UIViewComponentPeer::isFocused() const
- {
- return isSharedWindow ? this == currentlyFocusedPeer
- : (window != nil && [window isKeyWindow]);
- }
-
- void UIViewComponentPeer::grabFocus()
- {
- if (window != nil)
- {
- [window makeKeyWindow];
- viewFocusGain();
- }
- }
-
- void UIViewComponentPeer::textInputRequired (Point<int>, TextInputTarget&)
- {
- }
-
- static bool isIOS4_1() noexcept
- {
- return [[[UIDevice currentDevice] systemVersion] doubleValue] >= 4.1;
- }
-
- static UIKeyboardType getUIKeyboardType (TextInputTarget::VirtualKeyboardType type) noexcept
- {
- switch (type)
- {
- case TextInputTarget::textKeyboard: return UIKeyboardTypeAlphabet;
- case TextInputTarget::numericKeyboard: return isIOS4_1() ? UIKeyboardTypeNumberPad : UIKeyboardTypeNumbersAndPunctuation;
- case TextInputTarget::decimalKeyboard: return isIOS4_1() ? UIKeyboardTypeDecimalPad : UIKeyboardTypeNumbersAndPunctuation;
- case TextInputTarget::urlKeyboard: return UIKeyboardTypeURL;
- case TextInputTarget::emailAddressKeyboard: return UIKeyboardTypeEmailAddress;
- case TextInputTarget::phoneNumberKeyboard: return UIKeyboardTypePhonePad;
- default: jassertfalse; break;
- }
-
- return UIKeyboardTypeDefault;
- }
-
- void UIViewComponentPeer::updateHiddenTextContent (TextInputTarget* target)
- {
- view->hiddenTextView.keyboardType = getUIKeyboardType (target->getKeyboardType());
- view->hiddenTextView.text = juceStringToNS (target->getTextInRange (Range<int> (0, target->getHighlightedRegion().getStart())));
- view->hiddenTextView.selectedRange = NSMakeRange ((NSUInteger) target->getHighlightedRegion().getStart(), 0);
- }
-
- BOOL UIViewComponentPeer::textViewReplaceCharacters (Range<int> range, const String& text)
- {
- if (TextInputTarget* const target = findCurrentTextInputTarget())
- {
- const Range<int> currentSelection (target->getHighlightedRegion());
-
- if (range.getLength() == 1 && text.isEmpty()) // (detect backspace)
- if (currentSelection.isEmpty())
- target->setHighlightedRegion (currentSelection.withStart (currentSelection.getStart() - 1));
-
- if (text == "\r" || text == "\n" || text == "\r\n")
- handleKeyPress (KeyPress::returnKey, text[0]);
- else
- target->insertTextAtCaret (text);
-
- updateHiddenTextContent (target);
- }
-
- return NO;
- }
-
- void UIViewComponentPeer::globalFocusChanged (Component*)
- {
- if (TextInputTarget* const target = findCurrentTextInputTarget())
- {
- Component* comp = dynamic_cast<Component*> (target);
-
- Point<int> pos (component.getLocalPoint (comp, Point<int>()));
- view->hiddenTextView.frame = CGRectMake (pos.x, pos.y, 0, 0);
-
- updateHiddenTextContent (target);
- [view->hiddenTextView becomeFirstResponder];
- }
- else
- {
- [view->hiddenTextView resignFirstResponder];
- }
- }
-
-
- //==============================================================================
- void UIViewComponentPeer::drawRect (CGRect r)
- {
- if (r.size.width < 1.0f || r.size.height < 1.0f)
- return;
-
- CGContextRef cg = UIGraphicsGetCurrentContext();
-
- if (! component.isOpaque())
- CGContextClearRect (cg, CGContextGetClipBoundingBox (cg));
-
- CGContextConcatCTM (cg, CGAffineTransformMake (1, 0, 0, -1, 0, getComponent().getHeight()));
- CoreGraphicsContext g (cg, getComponent().getHeight(), static_cast<float> ([UIScreen mainScreen].scale));
-
- insideDrawRect = true;
- handlePaint (g);
- insideDrawRect = false;
- }
-
- bool UIViewComponentPeer::canBecomeKeyWindow()
- {
- return (getStyleFlags() & juce::ComponentPeer::windowIgnoresKeyPresses) == 0;
- }
-
- //==============================================================================
- void Desktop::setKioskComponent (Component* kioskModeComp, bool enableOrDisable, bool /*allowMenusAndBars*/)
- {
- displays->refresh();
-
- if (ComponentPeer* peer = kioskModeComp->getPeer())
- {
- if (UIViewComponentPeer* uiViewPeer = dynamic_cast<UIViewComponentPeer*> (peer))
- [uiViewPeer->controller setNeedsStatusBarAppearanceUpdate];
-
- peer->setFullScreen (enableOrDisable);
- }
- }
-
- void Desktop::allowedOrientationsChanged() {}
-
- //==============================================================================
- void UIViewComponentPeer::repaint (const Rectangle<int>& area)
- {
- if (insideDrawRect || ! MessageManager::getInstance()->isThisTheMessageThread())
- (new AsyncRepaintMessage (this, area))->post();
- else
- [view setNeedsDisplayInRect: convertToCGRect (area)];
- }
-
- void UIViewComponentPeer::performAnyPendingRepaintsNow()
- {
- }
-
- ComponentPeer* Component::createNewPeer (int styleFlags, void* windowToAttachTo)
- {
- return new UIViewComponentPeer (*this, styleFlags, (UIView*) windowToAttachTo);
- }
-
- //==============================================================================
- const int KeyPress::spaceKey = ' ';
- const int KeyPress::returnKey = 0x0d;
- const int KeyPress::escapeKey = 0x1b;
- const int KeyPress::backspaceKey = 0x7f;
- const int KeyPress::leftKey = 0x1000;
- const int KeyPress::rightKey = 0x1001;
- const int KeyPress::upKey = 0x1002;
- const int KeyPress::downKey = 0x1003;
- const int KeyPress::pageUpKey = 0x1004;
- const int KeyPress::pageDownKey = 0x1005;
- const int KeyPress::endKey = 0x1006;
- const int KeyPress::homeKey = 0x1007;
- const int KeyPress::deleteKey = 0x1008;
- const int KeyPress::insertKey = -1;
- const int KeyPress::tabKey = 9;
- const int KeyPress::F1Key = 0x2001;
- const int KeyPress::F2Key = 0x2002;
- const int KeyPress::F3Key = 0x2003;
- const int KeyPress::F4Key = 0x2004;
- const int KeyPress::F5Key = 0x2005;
- const int KeyPress::F6Key = 0x2006;
- const int KeyPress::F7Key = 0x2007;
- const int KeyPress::F8Key = 0x2008;
- const int KeyPress::F9Key = 0x2009;
- const int KeyPress::F10Key = 0x200a;
- const int KeyPress::F11Key = 0x200b;
- const int KeyPress::F12Key = 0x200c;
- const int KeyPress::F13Key = 0x200d;
- const int KeyPress::F14Key = 0x200e;
- const int KeyPress::F15Key = 0x200f;
- const int KeyPress::F16Key = 0x2010;
- const int KeyPress::numberPad0 = 0x30020;
- const int KeyPress::numberPad1 = 0x30021;
- const int KeyPress::numberPad2 = 0x30022;
- const int KeyPress::numberPad3 = 0x30023;
- const int KeyPress::numberPad4 = 0x30024;
- const int KeyPress::numberPad5 = 0x30025;
- const int KeyPress::numberPad6 = 0x30026;
- const int KeyPress::numberPad7 = 0x30027;
- const int KeyPress::numberPad8 = 0x30028;
- const int KeyPress::numberPad9 = 0x30029;
- const int KeyPress::numberPadAdd = 0x3002a;
- const int KeyPress::numberPadSubtract = 0x3002b;
- const int KeyPress::numberPadMultiply = 0x3002c;
- const int KeyPress::numberPadDivide = 0x3002d;
- const int KeyPress::numberPadSeparator = 0x3002e;
- const int KeyPress::numberPadDecimalPoint = 0x3002f;
- const int KeyPress::numberPadEquals = 0x30030;
- const int KeyPress::numberPadDelete = 0x30031;
- const int KeyPress::playKey = 0x30000;
- const int KeyPress::stopKey = 0x30001;
- const int KeyPress::fastForwardKey = 0x30002;
- const int KeyPress::rewindKey = 0x30003;
|