From 2f0271392a5cd6f7064c2db96f74ccfc6cc13d2e Mon Sep 17 00:00:00 2001
From: Wowfunhappy <Wowfunhappy@gmail.com>
Date: Mon, 24 May 2021 16:45:00 -0400
Subject: [PATCH] Initial Mavericks Compat Fixes

---
 .../global/qoperatingsystemversion_darwin.mm       |  53 +++++++-
 src/corelib/io/qfilesystemengine_unix.cpp          |   4 +-
 src/corelib/kernel/qcore_foundation.mm             |   8 +-
 src/gui/painting/qcoregraphics.mm                  | 133 +++++++++++----------
 .../platforms/cocoa/images/copyarrowcursor.png     | Bin 0 -> 1976 bytes
 .../platforms/cocoa/images/forbiddencursor.png     | Bin 0 -> 1745 bytes
 .../cocoa/images/leopard-unified-toolbar-on.png    | Bin 0 -> 356 bytes
 src/plugins/platforms/cocoa/qcocoaresources.qrc    |  17 ++-
 src/plugins/platforms/cocoa/qcocoawindow.mm        |   8 +-
 9 files changed, 138 insertions(+), 85 deletions(-)
 create mode 100644 src/plugins/platforms/cocoa/images/copyarrowcursor.png
 create mode 100644 src/plugins/platforms/cocoa/images/forbiddencursor.png
 create mode 100644 src/plugins/platforms/cocoa/images/leopard-unified-toolbar-on.png

diff --git src/corelib/global/qoperatingsystemversion_darwin.mm src/corelib/global/qoperatingsystemversion_darwin.mm
index d8b927ff5d..3dd007cbb3 100644
--- src/corelib/global/qoperatingsystemversion_darwin.mm
+++ src/corelib/global/qoperatingsystemversion_darwin.mm
@@ -40,16 +40,61 @@
 #include "qoperatingsystemversion_p.h"
 #import <Foundation/Foundation.h>
 
+#ifdef Q_OS_IOS
+#import <UIKit/UIKit.h>
+#endif
+
 QT_BEGIN_NAMESPACE
 
+typedef qint16 (*GestaltFunction)(quint32 selector, qint32 *response);
+
 QOperatingSystemVersion QOperatingSystemVersion::current()
 {
-    NSOperatingSystemVersion osv = NSProcessInfo.processInfo.operatingSystemVersion;
     QOperatingSystemVersion v;
     v.m_os = currentType();
-    v.m_major = osv.majorVersion;
-    v.m_minor = osv.minorVersion;
-    v.m_micro = osv.patchVersion;
+    v.m_major = -1;
+    v.m_minor = -1;
+    v.m_micro = -1;
+#if QT_MACOS_IOS_PLATFORM_SDK_EQUAL_OR_ABOVE(__MAC_10_10, __IPHONE_8_0) || defined(Q_OS_TVOS) || defined(Q_OS_WATCHOS)
+    if ([NSProcessInfo instancesRespondToSelector:@selector(operatingSystemVersion)]) {
+        NSOperatingSystemVersion osv = NSProcessInfo.processInfo.operatingSystemVersion;
+        v.m_major = osv.majorVersion;
+        v.m_minor = osv.minorVersion;
+        v.m_micro = osv.patchVersion;
+        return v;
+    }
+#endif
+    // Use temporary variables so we can return 0.0.0 (unknown version)
+    // in case of an error partway through determining the OS version
+    qint32 major = 0, minor = 0, patch = 0;
+#if QT_MACOS_IOS_DEPLOYMENT_TARGET_BELOW(__MAC_10_10, __IPHONE_8_0)
+#if defined(Q_OS_IOS)
+    @autoreleasepool {
+        NSArray *parts = [UIDevice.currentDevice.systemVersion componentsSeparatedByString:@"."];
+        major = parts.count > 0 ? [[parts objectAtIndex:0] intValue] : 0;
+        minor = parts.count > 1 ? [[parts objectAtIndex:1] intValue] : 0;
+        patch = parts.count > 2 ? [[parts objectAtIndex:2] intValue] : 0;
+    }
+#elif defined(Q_OS_MACOS)
+    static GestaltFunction pGestalt = 0;
+    if (!pGestalt) {
+        CFBundleRef b = CFBundleGetBundleWithIdentifier(CFSTR("com.apple.CoreServices"));
+        pGestalt = reinterpret_cast<GestaltFunction>(CFBundleGetFunctionPointerForName(b,
+                                                     CFSTR("Gestalt")));
+    }
+    if (!pGestalt)
+        return v;
+    if (pGestalt('sys1', &major) != 0)
+        return v;
+    if (pGestalt('sys2', &minor) != 0)
+        return v;
+    if (pGestalt('sys3', &patch) != 0)
+        return v;
+#endif
+#endif
+    v.m_major = major;
+    v.m_minor = minor;
+    v.m_micro = patch;
     return v;
 }
 
diff --git src/corelib/io/qfilesystemengine_unix.cpp src/corelib/io/qfilesystemengine_unix.cpp
index 7fed54f733..9e43da8b0d 100644
--- src/corelib/io/qfilesystemengine_unix.cpp
+++ src/corelib/io/qfilesystemengine_unix.cpp
@@ -126,7 +126,7 @@ static bool isPackage(const QFileSystemMetaData &data, const QFileSystemEntry &e
 
 #ifdef Q_OS_MACOS
         // Find if an application other than Finder claims to know how to handle the package
-        QCFType<CFURLRef> application = LSCopyDefaultApplicationURLForURL(url,
+        /*QCFType<CFURLRef> application = LSCopyDefaultApplicationURLForURL(url,
             kLSRolesEditor | kLSRolesViewer, nullptr);
 
         if (application) {
@@ -135,7 +135,7 @@ static bool isPackage(const QFileSystemMetaData &data, const QFileSystemEntry &e
             QString applicationId = QString::fromCFString(identifier);
             if (applicationId != QLatin1String("com.apple.finder"))
                 return true;
-        }
+        }*/
 #endif
     }
 
diff --git src/corelib/kernel/qcore_foundation.mm src/corelib/kernel/qcore_foundation.mm
index 56eabc4b8c..fc448be38f 100644
--- src/corelib/kernel/qcore_foundation.mm
+++ src/corelib/kernel/qcore_foundation.mm
@@ -486,10 +486,10 @@ QTimeZone QTimeZone::fromNSTimeZone(const NSTimeZone *timeZone)
 
     \sa fromNSTimeZone()
 */
-NSTimeZone *QTimeZone::toNSTimeZone() const
-{
-    return [static_cast<NSTimeZone *>(toCFTimeZone()) autorelease];
-}
+//NSTimeZone *QTimeZone::toNSTimeZone() const
+//{
+//    return [static_cast<NSTimeZone *>(toCFTimeZone()) autorelease];
+//}
 #endif
 
 // ----------------------------------------------------------------------------
diff --git src/gui/painting/qcoregraphics.mm src/gui/painting/qcoregraphics.mm
index c4fb8afc64..bf2d028b99 100644
--- src/gui/painting/qcoregraphics.mm
+++ src/gui/painting/qcoregraphics.mm
@@ -65,11 +65,11 @@ CGImageRef qt_mac_toCGImageMask(const QImage &image)
 {
     static const auto deleter = [](void *image, const void *, size_t) { delete static_cast<QImage *>(image); };
     QCFType<CGDataProviderRef> dataProvider =
-            CGDataProviderCreateWithData(new QImage(image), image.bits(),
-                                                    image.byteCount(), deleter);
-
+    CGDataProviderCreateWithData(new QImage(image), image.bits(),
+                                 image.byteCount(), deleter);
+    
     return CGImageMaskCreate(image.width(), image.height(), 8, image.depth(),
-                              image.bytesPerLine(), dataProvider, NULL, false);
+                             image.bytesPerLine(), dataProvider, NULL, false);
 }
 
 void qt_mac_drawCGImage(CGContextRef inContext, const CGRect *inBounds, CGImageRef inImage)
@@ -77,16 +77,16 @@ void qt_mac_drawCGImage(CGContextRef inContext, const CGRect *inBounds, CGImageR
     CGContextSaveGState( inContext );
     CGContextTranslateCTM (inContext, 0, inBounds->origin.y + CGRectGetMaxY(*inBounds));
     CGContextScaleCTM(inContext, 1, -1);
-
+    
     CGContextDrawImage(inContext, *inBounds, inImage);
-
+    
     CGContextRestoreGState(inContext);
 }
 
 QImage qt_mac_toQImage(CGImageRef image)
 {
     const size_t w = CGImageGetWidth(image),
-                 h = CGImageGetHeight(image);
+    h = CGImageGetHeight(image);
     QImage ret(w, h, QImage::Format_ARGB32_Premultiplied);
     ret.fill(Qt::transparent);
     CGRect rect = CGRectMake(0, 0, w, h);
@@ -147,7 +147,10 @@ QPixmap qt_mac_toQPixmap(const NSImage *image, const QSizeF &size)
     QMacCGContext ctx(&pixmap);
     if (!ctx)
         return QPixmap();
-    NSGraphicsContext *gc = [NSGraphicsContext graphicsContextWithCGContext:ctx flipped:YES];
+    
+    //NSGraphicsContext *gc = [NSGraphicsContext graphicsContextWithCGContext:ctx flipped:YES];
+    NSGraphicsContext *gc = [NSGraphicsContext graphicsContextWithGraphicsPort:ctx flipped:YES];
+    
     if (!gc)
         return QPixmap();
     [NSGraphicsContext saveGraphicsState];
@@ -324,14 +327,14 @@ static CGColorSpaceRef qt_mac_displayColorSpace(const QWindow *window)
     } else {
         displayID = CGMainDisplayID();
         /*
-        ### get correct display
-        const QRect &qrect = window->geometry();
-        CGRect rect = CGRectMake(qrect.x(), qrect.y(), qrect.width(), qrect.height());
-        CGDisplayCount throwAway;
-        CGDisplayErr dErr = CGGetDisplaysWithRect(rect, 1, &displayID, &throwAway);
-        if (dErr != kCGErrorSuccess)
-            return macDisplayColorSpace(0); // fall back on main display
-        */
+         ### get correct display
+         const QRect &qrect = window->geometry();
+         CGRect rect = CGRectMake(qrect.x(), qrect.y(), qrect.width(), qrect.height());
+         CGDisplayCount throwAway;
+         CGDisplayErr dErr = CGGetDisplaysWithRect(rect, 1, &displayID, &throwAway);
+         if (dErr != kCGErrorSuccess)
+         return macDisplayColorSpace(0); // fall back on main display
+         */
     }
     if ((colorSpace = m_displayColorSpaceHash.value(displayID)))
         return colorSpace;
@@ -442,7 +445,7 @@ QMacCGContext::QMacCGContext(QPaintDevice *paintDevice) : context(0)
     if (paintDevice->devType() == QInternal::Image) {
         image = static_cast<QImage *>(paintDevice);
     } else if (paintDevice->devType() == QInternal::Pixmap) {
-
+        
         const QPixmap *pm = static_cast<const QPixmap*>(paintDevice);
         QPlatformPixmap *data = const_cast<QPixmap *>(pm)->data_ptr().data();
         if (data && data->classId() == QPlatformPixmap::RasterClass) {
@@ -463,7 +466,7 @@ QMacCGContext::QMacCGContext(QPaintDevice *paintDevice) : context(0)
     flags |= kCGBitmapByteOrder32Host;
 
     context = CGBitmapContextCreate(image->bits(), image->width(), image->height(),
-                                8, image->bytesPerLine(), colorspace, flags);
+                                    8, image->bytesPerLine(), colorspace, flags);
     CGContextTranslateCTM(context, 0, image->height());
     const qreal devicePixelRatio = paintDevice->devicePixelRatioF();
     CGContextScaleCTM(context, devicePixelRatio, devicePixelRatio);
@@ -477,64 +480,64 @@ QMacCGContext::QMacCGContext(QPainter *painter) : context(0)
     // Handle the case of QMacPrintEngine, which has an internal QCoreGraphicsPaintEngine
     while (QPaintEngine *aggregateEngine = QPaintEnginePrivate::get(paintEngine)->aggregateEngine())
         paintEngine = aggregateEngine;
-
+    
     paintEngine->syncState();
-
+    
     if (Qt::HANDLE handle = QPaintEnginePrivate::get(paintEngine)->nativeHandle()) {
         context = static_cast<CGContextRef>(handle);
         return;
     }
-
+    
     int devType = painter->device()->devType();
     if (paintEngine->type() == QPaintEngine::Raster
-            && (devType == QInternal::Widget ||
-                devType == QInternal::Pixmap ||
-                devType == QInternal::Image)) {
-
-        CGColorSpaceRef colorspace = qt_mac_colorSpaceForDeviceType(paintEngine->paintDevice());
-        uint flags = kCGImageAlphaPremultipliedFirst;
+        && (devType == QInternal::Widget ||
+            devType == QInternal::Pixmap ||
+            devType == QInternal::Image)) {
+            
+            CGColorSpaceRef colorspace = qt_mac_colorSpaceForDeviceType(paintEngine->paintDevice());
+            uint flags = kCGImageAlphaPremultipliedFirst;
 #ifdef kCGBitmapByteOrder32Host //only needed because CGImage.h added symbols in the minor version
-        flags |= kCGBitmapByteOrder32Host;
+            flags |= kCGBitmapByteOrder32Host;
 #endif
-        const QImage *image = static_cast<const QImage *>(paintEngine->paintDevice());
-
-        context = CGBitmapContextCreate((void *)image->bits(), image->width(), image->height(),
-                                        8, image->bytesPerLine(), colorspace, flags);
-
-        // Invert y axis
-        CGContextTranslateCTM(context, 0, image->height());
-        CGContextScaleCTM(context, 1, -1);
-
-        const qreal devicePixelRatio = image->devicePixelRatio();
-
-        if (devType == QInternal::Widget) {
-            // Set the clip rect which is an intersection of the system clip
-            // and the painter clip. To make matters more interesting these
-            // are in device pixels and device-independent pixels, respectively.
-            QRegion clip = painter->paintEngine()->systemClip(); // get system clip in device pixels
-            QTransform native = painter->deviceTransform();      // get device transform. dx/dy is in device pixels
-
-            if (painter->hasClipping()) {
-                QRegion r = painter->clipRegion();               // get painter clip, which is in device-independent pixels
-                qt_mac_scale_region(&r, devicePixelRatio); // scale painter clip to device pixels
-                r.translate(native.dx(), native.dy());
-                if (clip.isEmpty())
-                    clip = r;
-                else
-                    clip &= r;
+            const QImage *image = static_cast<const QImage *>(paintEngine->paintDevice());
+            
+            context = CGBitmapContextCreate((void *)image->bits(), image->width(), image->height(),
+                                            8, image->bytesPerLine(), colorspace, flags);
+            
+            // Invert y axis
+            CGContextTranslateCTM(context, 0, image->height());
+            CGContextScaleCTM(context, 1, -1);
+            
+            const qreal devicePixelRatio = image->devicePixelRatio();
+            
+            if (devType == QInternal::Widget) {
+                // Set the clip rect which is an intersection of the system clip
+                // and the painter clip. To make matters more interesting these
+                // are in device pixels and device-independent pixels, respectively.
+                QRegion clip = painter->paintEngine()->systemClip(); // get system clip in device pixels
+                QTransform native = painter->deviceTransform();      // get device transform. dx/dy is in device pixels
+                
+                if (painter->hasClipping()) {
+                    QRegion r = painter->clipRegion();               // get painter clip, which is in device-independent pixels
+                    qt_mac_scale_region(&r, devicePixelRatio); // scale painter clip to device pixels
+                    r.translate(native.dx(), native.dy());
+                    if (clip.isEmpty())
+                        clip = r;
+                    else
+                        clip &= r;
+                }
+                qt_mac_clip_cg(context, clip, 0); // clip in device pixels
+                
+                // Scale the context so that painting happens in device-independent pixels
+                CGContextScaleCTM(context, devicePixelRatio, devicePixelRatio);
+                CGContextTranslateCTM(context, native.dx() / devicePixelRatio, native.dy() / devicePixelRatio);
+            } else {
+                // Scale to paint in device-independent pixels
+                CGContextScaleCTM(context, devicePixelRatio, devicePixelRatio);
             }
-            qt_mac_clip_cg(context, clip, 0); // clip in device pixels
-
-            // Scale the context so that painting happens in device-independent pixels
-            CGContextScaleCTM(context, devicePixelRatio, devicePixelRatio);
-            CGContextTranslateCTM(context, native.dx() / devicePixelRatio, native.dy() / devicePixelRatio);
         } else {
-            // Scale to paint in device-independent pixels
-            CGContextScaleCTM(context, devicePixelRatio, devicePixelRatio);
+            qDebug() << "QMacCGContext:: Unsupported painter devtype type" << devType;
         }
-    } else {
-        qDebug() << "QMacCGContext:: Unsupported painter devtype type" << devType;
-    }
 }
 
-QT_END_NAMESPACE
+QT_END_NAMESPACE
\ No newline at end of file
diff --git src/plugins/platforms/cocoa/qcocoawindow.mm src/plugins/platforms/cocoa/qcocoawindow.mm
index 86fd7b8a9f..2f45347127 100644
--- src/plugins/platforms/cocoa/qcocoawindow.mm
+++ src/plugins/platforms/cocoa/qcocoawindow.mm
@@ -324,7 +324,7 @@ static void qt_closePopups()
 
 + (void)applicationActivationChanged:(NSNotification*)notification
 {
-    const id sender = self;
+    /*const id sender = self;
     NSEnumerator<NSWindow*> *windowEnumerator = nullptr;
     NSApplication *application = [NSApplication sharedApplication];
 
@@ -384,7 +384,7 @@ static void qt_closePopups()
         // end up triggering a bug in AppKit where the tool windows would disappear behind
         // the application window.
         [window orderFront:sender];
-    }
+    }*/
 }
 
 - (id)initWithContentRect:(NSRect)contentRect
@@ -2181,7 +2181,7 @@ void QCocoaWindow::applyContentBorderThickness(NSWindow *window)
     if (!m_drawContentBorderGradient) {
         [window setStyleMask:[window styleMask] & ~NSTexturedBackgroundWindowMask];
         [[[window contentView] superview] setNeedsDisplay:YES];
-        window.titlebarAppearsTransparent = NO;
+        //window.titlebarAppearsTransparent = NO;
         return;
     }
 
@@ -2206,7 +2206,7 @@ void QCocoaWindow::applyContentBorderThickness(NSWindow *window)
     int effectiveBottomContentBorderThickness = m_bottomContentBorderThickness;
 
     [window setStyleMask:[window styleMask] | NSTexturedBackgroundWindowMask];
-    window.titlebarAppearsTransparent = YES;
+    //window.titlebarAppearsTransparent = YES;
 
     [window setContentBorderThickness:effectiveTopContentBorderThickness forEdge:NSMaxYEdge];
     [window setAutorecalculatesContentBorderThickness:NO forEdge:NSMaxYEdge];
-- 
2.14.1

