diff --git a/5.12.0/bin/7z.exe b/5.12.0/bin/7z.exe deleted file mode 100644 index d3fe532..0000000 Binary files a/5.12.0/bin/7z.exe and /dev/null differ diff --git a/5.12.0/bin/wget.exe b/5.12.0/bin/wget.exe deleted file mode 100644 index cda6b94..0000000 Binary files a/5.12.0/bin/wget.exe and /dev/null differ diff --git a/5.12.0/compile_mac.sh b/5.12.0/compile_mac.sh deleted file mode 100644 index 795e82e..0000000 --- a/5.12.0/compile_mac.sh +++ /dev/null @@ -1,72 +0,0 @@ -#!/bin/bash - -# requirements: -# 1. brew -# 2. brew install llvm (https://bugreports.qt.io/browse/QTBUG-66353) - -export PATH=$PATH:/usr/local/Qt-5.12.0/bin - -cd qtbase - -if [[ $1 == openssl ]]; then - - # download openssl - curl -O https://www.openssl.org/source/old/1.0.2/openssl-1.0.2l.tar.gz - tar -xvzf openssl-1.0.2l.tar.gz - - # compile openssl - cd openssl-1.0.2l - ./Configure darwin64-x86_64-cc --prefix=$PWD/dist - make - # print arch info (optional) - lipo -info libssl.a - lipo -info libcrypto.a - make install - cd .. - - # continue - - OPENSSL_LIBS='-L$PWD/openssl-1.0.2l/dist/lib -lssl -lcrypto' ./configure -opensource -confirm-license -no-securetransport -nomake examples -nomake tests -openssl-linked -I $PWD/openssl-1.0.2l/dist/include -L $PWD/openssl-1.0.2l/dist/lib - -elif [[ $1 == securetransport ]]; then - - ./configure -opensource -confirm-license -nomake examples -nomake tests -no-openssl -securetransport - -else - - echo "Error: please specify which SSL layer to use (openssl or securetransport)" - exit 1 - -fi - -make -j 8 -echo maki | sudo -S sudo make install - -cd ../qttools -qmake -make -j 8 -echo maki | sudo -S sudo make install - -cd ../qtmacextras -qmake -make -j 8 -echo maki | sudo -S sudo make install - -cd ../qtdeclarative/src -qmake -make -j 8 sub-qmldevtools -echo maki | sudo -S sudo make install - -# make docs - currently doesnt work - -#cd ../qtbase -#make -j 8 docs -#cd ../qttools -#make -j 8 docs -#cd ../qtmacextras -#make -j 8 docs - -#echo maki | sudo -S cp -f -r ../qtbase/doc /usr/local/Qt-5.12.0/ - -cd /usr/local -zip -r ~/Desktop/qt5.12.0_mac.zip Qt-5.12.0/* \ No newline at end of file diff --git a/5.12.0/compile_win_docs_fail.pl b/5.12.0/compile_win_docs_fail.pl deleted file mode 100644 index 0c6c718..0000000 --- a/5.12.0/compile_win_docs_fail.pl +++ /dev/null @@ -1,113 +0,0 @@ -use strict; - -die "Cannot proceed without the 'bin' folder'" if (!-e "bin"); - -my $arch = $ARGV[0]; -my $openssl_v_major = "1.0.2"; # The 1.0.2 series is Long Term Support (LTS) release, supported until 31st December 2019 -my $openssl_v_minor = "l"; -my $openssl_version = "$openssl_v_major$openssl_v_minor"; -my $openssl_dir = "openssl-$openssl_version"; -my $openssl_download = "https://www.openssl.org/source/old/$openssl_v_major/openssl-$openssl_version.tar.gz"; -my $openssl_arch = $arch eq "amd64" ? "WIN64A" : "WIN32"; -my $openssl_do_ms = $arch eq "amd64" ? "do_win64a" : "do_ms"; - -$arch = "x86" if ($arch eq ''); # specify x86 is nothing is specified -die "Please specify architecture (x86 or amd64)" if ($arch ne "x86" && $arch ne "amd64"); # die if user specified anything except x86 or amd64 - -# will create a batch file - -my $batfile = 'compile_win.bat'; - -open BAT, '>', $batfile; - -printLineToBat ("SET PATH=%PATH%;%cd%\\bin"); # add bin folder to the path for 7z and wget -printLineToBat ("CALL \"C:\\Program Files (x86)\\Microsoft Visual Studio\\2017\\Community\\VC\\Auxiliary\\Build\\vcvarsall.bat\" $arch"); -printLineToBat ("SET _ROOT=%cd%"); -printLineToBat ("SET PATH=%_ROOT%\\qtbase\\bin;%_ROOT%\\gnuwin32\\bin;%PATH%"); # http://doc.qt.io/qt-5/windows-building.html -printLineToBat ("SET LLVM_INSTALL_DIR=C:\\LLVM"); # add clang bin folder to the path (to compile qdoc) -printLineToBat ("SET PATH=%PATH%;%LLVM_INSTALL_DIR%\\bin"); # must be added for qdoc because it requires libclang.dll -# todo: download python -printLineToBat ("SET PATH=%PATH%;C:\\Python27"); # add bin folder to the path for 7z and wget - -printLineToBat ("cd qtbase"); - -printLineToBat ("if \"%~1\"==\"step2\" goto step2"); - -# step1: compile openssl and do configure. For some reason, can't continue script execution after configure, have to make step2 - -printLineToBat ("wget --no-check-certificate $openssl_download"); -printLineToBat ("7z x openssl-$openssl_version.tar.gz"); -printLineToBat ("7z x openssl-$openssl_version.tar"); -printLineToBat ("rm openssl-$openssl_version.tar.gz"); -printLineToBat ("rm openssl-$openssl_version.tar"); -printLineToBat ("cd $openssl_dir"); -# build debug -printLineToBat ("perl Configure no-asm no-shared --prefix=%cd%\\Debug --openssldir=%cd%\\Debug debug-VC-$openssl_arch"); -printLineToBat ("call ms\\$openssl_do_ms"); -printLineToBat ("nmake -f ms\\nt.mak"); -printLineToBat ("nmake -f ms\\nt.mak install"); -printLineToBat ("xcopy tmp32.dbg\\lib.pdb Debug\\lib\\"); # Telegram does it. -printLineToBat ("nmake -f ms\\nt.mak clean"); -# now release -printLineToBat ("perl Configure no-asm no-shared --prefix=%cd%\\Release --openssldir=%cd%\\Release VC-$openssl_arch"); -printLineToBat ("call ms\\$openssl_do_ms"); -printLineToBat ("nmake -f ms\\nt.mak"); -printLineToBat ("nmake -f ms\\nt.mak install"); -printLineToBat ("xcopy tmp32\\lib.pdb Release\\lib\\"); # Telegram does it. -printLineToBat ("nmake -f ms\\nt.mak clean"); -# go back to qtbase -printLineToBat ("cd .."); - -# -developer-build creates an in-source build for developer usage. -printLineToBat ("configure -opensource -developer-build -confirm-license -opengl desktop -mp -nomake tests -nomake examples -I \"%cd%\\$openssl_dir\\Release\\include\" -openssl-linked OPENSSL_LIBS_DEBUG=\"%cd%\\$openssl_dir\\Debug\\lib\\ssleay32.lib %cd%\\$openssl_dir\\Debug\\lib\\libeay32.lib\" OPENSSL_LIBS_RELEASE=\"%cd%\\$openssl_dir\\Release\\lib\\ssleay32.lib %cd%\\$openssl_dir\\Release\\lib\\libeay32.lib\""); -printLineToBat ("goto :EOF"); - -# step 2: - -printLineToBat (":step2"); -printLineToBat ("nmake"); - -# QDoc depends on the QML DevTools library from qtdeclarative -# requires python as well - -printLineToBat ("cd ..\\qtdeclarative"); -printLineToBat ("..\\qtbase\\bin\\qmake"); -printLineToBat ("cd src"); -printLineToBat ("..\\..\\qtbase\\bin\\qmake"); -printLineToBat ("nmake sub-qmldevtools"); -printLineToBat ("cd .."); # go back to qtdeclarative - -printLineToBat ("cd ..\\qttools"); -printLineToBat ("cp mkspecs\\features\\qt_find_clang.prf ..\\qtbase\\mkspecs\\features"); # почему-то этот файл не находит, если он не тут -printLineToBat ("..\\qtbase\\bin\\qmake"); -printLineToBat ("nmake"); -printLineToBat ("cd ..\\qtwinextras"); -printLineToBat ("..\\qtbase\\bin\\qmake"); -printLineToBat ("nmake"); -printLineToBat ("cd ..\\qtbase"); -printLineToBat ("nmake docs"); -printLineToBat ("cd .."); # go up to qt dir -# openssl clean up -printLineToBat ("cd qtbase"); -printLineToBat ("cd $openssl_dir"); -printLineToBat ("del /s /f /q out32"); -printLineToBat ("del /s /f /q out32.dbg"); -printLineToBat ("cd .."); -printLineToBat ("cd .."); -# the rest -printLineToBat ("del *.obj /s /f"); -printLineToBat ("del *.ilk /s /f"); -printLineToBat ("del *.pch /s /f"); -printLineToBat ("del Makefile* /s /f"); - -close BAT; - -system ($batfile); -#system ("$batfile step2"); - -system ("pause"); - -sub printLineToBat -{ - print BAT "$_[0]\n"; -} \ No newline at end of file diff --git a/5.12.0/compile_win_no_docs.pl b/5.12.0/compile_win_no_docs.pl deleted file mode 100644 index e220a31..0000000 --- a/5.12.0/compile_win_no_docs.pl +++ /dev/null @@ -1,96 +0,0 @@ -use strict; - -die "Cannot proceed without the 'bin' folder'" if (!-e "bin"); - -my $arch = $ARGV[0]; -my $openssl_v_major = "1.0.2"; # The 1.0.2 series is Long Term Support (LTS) release, supported until 31st December 2019 -my $openssl_v_minor = "l"; -my $openssl_version = "$openssl_v_major$openssl_v_minor"; -my $openssl_dir = "openssl-$openssl_version"; -my $openssl_download = "https://www.openssl.org/source/old/$openssl_v_major/openssl-$openssl_version.tar.gz"; -my $openssl_arch = $arch eq "amd64" ? "WIN64A" : "WIN32"; -my $openssl_do_ms = $arch eq "amd64" ? "do_win64a" : "do_ms"; - -$arch = "x86" if ($arch eq ''); # specify x86 is nothing is specified -die "Please specify architecture (x86 or amd64)" if ($arch ne "x86" && $arch ne "amd64"); # die if user specified anything except x86 or amd64 - -# will create a batch file - -my $batfile = 'compile_win.bat'; - -open BAT, '>', $batfile; - -printLineToBat ("SET PATH=%PATH%;%cd%\\bin"); # add bin folder to the path for 7z and wget -printLineToBat ("CALL \"C:\\Program Files (x86)\\Microsoft Visual Studio\\2017\\Community\\VC\\Auxiliary\\Build\\vcvarsall.bat\" $arch"); -printLineToBat ("SET _ROOT=%cd%"); -printLineToBat ("SET PATH=%_ROOT%\\qtbase\\bin;%_ROOT%\\gnuwin32\\bin;%PATH%"); # http://doc.qt.io/qt-5/windows-building.html -printLineToBat ("SET LLVM_INSTALL_DIR=C:\\LLVM"); # add clang bin folder to the path (to compile qdoc) -printLineToBat ("SET PATH=%PATH%;%LLVM_INSTALL_DIR%\\bin"); # must be added for qdoc because it requires libclang.dll -# todo: download python -printLineToBat ("SET PATH=%PATH%;C:\\Python27"); # add bin folder to the path for 7z and wget - -printLineToBat ("cd qtbase"); -printLineToBat ("if \"%~1\"==\"step2\" goto step2"); - -# step1: compile openssl and do configure. For some reason, can't continue script execution after configure, have to make step2 - -printLineToBat ("wget --no-check-certificate $openssl_download"); -printLineToBat ("7z x openssl-$openssl_version.tar.gz"); -printLineToBat ("7z x openssl-$openssl_version.tar"); -printLineToBat ("rm openssl-$openssl_version.tar.gz"); -printLineToBat ("rm openssl-$openssl_version.tar"); -printLineToBat ("cd $openssl_dir"); -# build debug -printLineToBat ("perl Configure no-asm no-shared --prefix=%cd%\\Debug --openssldir=%cd%\\Debug debug-VC-$openssl_arch"); -printLineToBat ("call ms\\$openssl_do_ms"); -printLineToBat ("nmake -f ms\\nt.mak"); -printLineToBat ("nmake -f ms\\nt.mak install"); -printLineToBat ("xcopy tmp32.dbg\\lib.pdb Debug\\lib\\"); # Telegram does it. -printLineToBat ("nmake -f ms\\nt.mak clean"); -# now release -printLineToBat ("perl Configure no-asm no-shared --prefix=%cd%\\Release --openssldir=%cd%\\Release VC-$openssl_arch"); -printLineToBat ("call ms\\$openssl_do_ms"); -printLineToBat ("nmake -f ms\\nt.mak"); -printLineToBat ("nmake -f ms\\nt.mak install"); -printLineToBat ("xcopy tmp32\\lib.pdb Release\\lib\\"); # Telegram does it. -printLineToBat ("nmake -f ms\\nt.mak clean"); -# go back to qtbase -printLineToBat ("cd .."); - -# -developer-build creates an in-source build for developer usage. -printLineToBat ("configure -opensource -developer-build -confirm-license -opengl desktop -mp -nomake tests -nomake examples -I \"%cd%\\$openssl_dir\\Release\\include\" -openssl-linked OPENSSL_LIBS_DEBUG=\"%cd%\\$openssl_dir\\Debug\\lib\\ssleay32.lib %cd%\\$openssl_dir\\Debug\\lib\\libeay32.lib\" OPENSSL_LIBS_RELEASE=\"%cd%\\$openssl_dir\\Release\\lib\\ssleay32.lib %cd%\\$openssl_dir\\Release\\lib\\libeay32.lib\""); -printLineToBat ("goto :EOF"); - -# step 2: -printLineToBat (":step2"); - -printLineToBat ("nmake"); -printLineToBat ("cd ..\\qttools"); -printLineToBat ("..\\qtbase\\bin\\qmake"); -printLineToBat ("nmake"); -printLineToBat ("cd ..\\qtbase"); -printLineToBat ("cd .."); # go up to qt dir -# openssl clean up -printLineToBat ("cd qtbase"); -printLineToBat ("cd $openssl_dir"); -printLineToBat ("del /s /f /q out32"); -printLineToBat ("del /s /f /q out32.dbg"); -printLineToBat ("cd .."); -printLineToBat ("cd .."); -# the rest -printLineToBat ("del *.obj /s /f"); -printLineToBat ("del *.ilk /s /f"); -printLineToBat ("del *.pch /s /f"); -printLineToBat ("del Makefile* /s /f"); - -close BAT; - -system ($batfile); -system ("$batfile step2"); - -system ("pause"); - -sub printLineToBat -{ - print BAT "$_[0]\n"; -} \ No newline at end of file diff --git a/5.12.0/open_ssl_and_vc2017_problem_with_setargv.obj/after_problem/x64/setargv.obj b/5.12.0/open_ssl_and_vc2017_problem_with_setargv.obj/after_problem/x64/setargv.obj deleted file mode 100644 index 27a63e1..0000000 Binary files a/5.12.0/open_ssl_and_vc2017_problem_with_setargv.obj/after_problem/x64/setargv.obj and /dev/null differ diff --git a/5.12.0/open_ssl_and_vc2017_problem_with_setargv.obj/after_problem/x86/setargv.obj b/5.12.0/open_ssl_and_vc2017_problem_with_setargv.obj/after_problem/x86/setargv.obj deleted file mode 100644 index 3e38771..0000000 Binary files a/5.12.0/open_ssl_and_vc2017_problem_with_setargv.obj/after_problem/x86/setargv.obj and /dev/null differ diff --git a/5.12.0/open_ssl_and_vc2017_problem_with_setargv.obj/before_problem_compiled/openssl-1.0.2l.rar b/5.12.0/open_ssl_and_vc2017_problem_with_setargv.obj/before_problem_compiled/openssl-1.0.2l.rar deleted file mode 100644 index e050a39..0000000 Binary files a/5.12.0/open_ssl_and_vc2017_problem_with_setargv.obj/before_problem_compiled/openssl-1.0.2l.rar and /dev/null differ diff --git a/5.12.0/open_ssl_and_vc2017_problem_with_setargv.obj/before_problem_compiled/openssl-1.0.2l_x64.rar b/5.12.0/open_ssl_and_vc2017_problem_with_setargv.obj/before_problem_compiled/openssl-1.0.2l_x64.rar deleted file mode 100644 index 4de3b0b..0000000 Binary files a/5.12.0/open_ssl_and_vc2017_problem_with_setargv.obj/before_problem_compiled/openssl-1.0.2l_x64.rar and /dev/null differ diff --git a/5.12.0/open_ssl_and_vc2017_problem_with_setargv.obj/readme.txt b/5.12.0/open_ssl_and_vc2017_problem_with_setargv.obj/readme.txt deleted file mode 100644 index d502977..0000000 --- a/5.12.0/open_ssl_and_vc2017_problem_with_setargv.obj/readme.txt +++ /dev/null @@ -1,11 +0,0 @@ -пришлось скомпилировать и скопировать файл сюда -C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.15.26726\lib\x64\setargv.obj -C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.15.26726\lib\x86\setargv.obj - -использовал вот это -https://perldoc.pl/perlwin32 - -В итоге: - -cd C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.15.26726\crt\src\linkopts\ -cl.exe /c /I. /D_CRTBLD setargv.cpp diff --git a/5.12.0/qtbase/mkspecs/common/msvc-desktop.conf b/5.12.0/qtbase/mkspecs/common/msvc-desktop.conf deleted file mode 100644 index 5ccc4f0..0000000 --- a/5.12.0/qtbase/mkspecs/common/msvc-desktop.conf +++ /dev/null @@ -1,118 +0,0 @@ -# -# This file is used as a basis for the following compilers: -# -# - Microsoft C/C++ Optimizing Compiler (all desktop versions) -# - Intel C++ Compiler on Windows -# - Clang-cl -# -# Baseline: -# -# - Visual Studio 2005 (8.0), VC++ 14.0 -# -# Version-specific settings go in msvc-version.conf (loaded by default_pre) -# - -MAKEFILE_GENERATOR = MSVC.NET -QMAKE_PLATFORM = win32 -QMAKE_COMPILER = msvc -CONFIG += flat debug_and_release debug_and_release_target precompile_header autogen_precompile_source embed_manifest_dll embed_manifest_exe -# MSVC 2017 15.8+ fixed std::aligned_storage but compilation fails without -# _ENABLE_EXTENDED_ALIGNED_STORAGE flag since the fix breaks binary compatibility. -DEFINES += UNICODE _UNICODE WIN32 _ENABLE_EXTENDED_ALIGNED_STORAGE -QMAKE_COMPILER_DEFINES += _WIN32 -contains(QMAKE_TARGET.arch, x86_64) { - DEFINES += WIN64 - QMAKE_COMPILER_DEFINES += _WIN64 -} - -QMAKE_CFLAGS_OPTIMIZE_DEBUG = -Od -QMAKE_CFLAGS_OPTIMIZE = -O2 -QMAKE_CFLAGS_OPTIMIZE_SIZE = -O1 - -QMAKE_CC = cl -QMAKE_LEX = flex -QMAKE_LEXFLAGS = -QMAKE_YACC = bison -y -QMAKE_YACCFLAGS = -d -QMAKE_CFLAGS = -nologo -Zc:wchar_t -QMAKE_CFLAGS_WARN_ON = -W3 -QMAKE_CFLAGS_WARN_OFF = -W0 -QMAKE_CFLAGS_RELEASE = $$QMAKE_CFLAGS_OPTIMIZE -MD -Zi -QMAKE_CFLAGS_RELEASE_WITH_DEBUGINFO += $$QMAKE_CFLAGS_OPTIMIZE -Zi -MD -QMAKE_CFLAGS_DEBUG = -Zi -MDd -QMAKE_CFLAGS_YACC = -QMAKE_CFLAGS_LTCG = -GL - -contains(QMAKE_TARGET.arch, x86_64) { - # SSE2 is mandatory on 64-bit mode, so skip the option. It triggers: - # cl : Command line warning D9002 : ignoring unknown option '-arch:SSE2' - QMAKE_CFLAGS_SSE2 = -} else { - QMAKE_CFLAGS_SSE2 = -arch:SSE2 -} -QMAKE_CFLAGS_SSE3 = $$QMAKE_CFLAGS_SSE2 -QMAKE_CFLAGS_SSSE3 = $$QMAKE_CFLAGS_SSE2 -QMAKE_CFLAGS_SSE4_1 = $$QMAKE_CFLAGS_SSE2 -QMAKE_CFLAGS_SSE4_2 = $$QMAKE_CFLAGS_SSE2 -QMAKE_CFLAGS_AESNI = $$QMAKE_CFLAGS_SSE2 -QMAKE_CFLAGS_SHANI = $$QMAKE_CFLAGS_SSE2 - -QMAKE_CXX = $$QMAKE_CC -QMAKE_CXXFLAGS = $$QMAKE_CFLAGS -QMAKE_CXXFLAGS_WARN_ON = $$QMAKE_CFLAGS_WARN_ON -w34100 -w34189 -w44996 -QMAKE_CXXFLAGS_WARN_OFF = $$QMAKE_CFLAGS_WARN_OFF -QMAKE_CXXFLAGS_RELEASE = $$QMAKE_CFLAGS_RELEASE -QMAKE_CXXFLAGS_RELEASE_WITH_DEBUGINFO += $$QMAKE_CFLAGS_RELEASE_WITH_DEBUGINFO -QMAKE_CXXFLAGS_DEBUG = $$QMAKE_CFLAGS_DEBUG -QMAKE_CXXFLAGS_YACC = $$QMAKE_CFLAGS_YACC -QMAKE_CXXFLAGS_LTCG = $$QMAKE_CFLAGS_LTCG -QMAKE_CXXFLAGS_STL_ON = -EHsc -QMAKE_CXXFLAGS_STL_OFF = -QMAKE_CXXFLAGS_RTTI_ON = -GR -QMAKE_CXXFLAGS_RTTI_OFF = -QMAKE_CXXFLAGS_EXCEPTIONS_ON = -EHsc -QMAKE_CXXFLAGS_EXCEPTIONS_OFF = - -QMAKE_INCDIR = - -QMAKE_RUN_CC = $(CC) -c $(CFLAGS) $(INCPATH) -Fo$obj $src -QMAKE_RUN_CC_IMP = $(CC) -c $(CFLAGS) $(INCPATH) -Fo$@ $< -QMAKE_RUN_CC_IMP_BATCH = $(CC) -c $(CFLAGS) $(INCPATH) -Fo$@ @<< -QMAKE_RUN_CXX = $(CXX) -c $(CXXFLAGS) $(INCPATH) -Fo$obj $src -QMAKE_RUN_CXX_IMP = $(CXX) -c $(CXXFLAGS) $(INCPATH) -Fo$@ $< -QMAKE_RUN_CXX_IMP_BATCH = $(CXX) -c $(CXXFLAGS) $(INCPATH) -Fo$@ @<< - -QMAKE_LINK = link -QMAKE_LFLAGS = /NOLOGO /DYNAMICBASE /NXCOMPAT -QMAKE_LFLAGS_RELEASE = /INCREMENTAL:NO -QMAKE_LFLAGS_RELEASE_WITH_DEBUGINFO = /DEBUG /OPT:REF /INCREMENTAL:NO -QMAKE_LFLAGS_DEBUG = /DEBUG -QMAKE_LFLAGS_CONSOLE = /SUBSYSTEM:CONSOLE -QMAKE_LFLAGS_WINDOWS = /SUBSYSTEM:WINDOWS -QMAKE_LFLAGS_EXE = \"/MANIFESTDEPENDENCY:type=\'win32\' name=\'Microsoft.Windows.Common-Controls\' version=\'6.0.0.0\' publicKeyToken=\'6595b64144ccf1df\' language=\'*\' processorArchitecture=\'*\'\" -QMAKE_LFLAGS_DLL = /DLL -QMAKE_LFLAGS_LTCG = /LTCG -QMAKE_PREFIX_SHLIB = -QMAKE_EXTENSION_SHLIB = dll -QMAKE_PREFIX_STATICLIB = -QMAKE_EXTENSION_STATICLIB = lib - -QMAKE_LIBS = -QMAKE_LIBS_GUI = gdi32.lib comdlg32.lib oleaut32.lib imm32.lib winmm.lib ws2_32.lib ole32.lib uuid.lib user32.lib advapi32.lib -QMAKE_LIBS_NETWORK = ws2_32.lib user32.lib gdi32.lib -QMAKE_LIBS_OPENGL = glu32.lib opengl32.lib gdi32.lib user32.lib -QMAKE_LIBS_OPENGL_ES2 = gdi32.lib user32.lib -QMAKE_LIBS_OPENGL_ES2_DEBUG = gdi32.lib user32.lib -QMAKE_LIBS_COMPAT = advapi32.lib shell32.lib comdlg32.lib user32.lib gdi32.lib ws2_32.lib -QMAKE_LIBS_QT_ENTRY = -lqtmain - -QMAKE_IDL = midl /NOLOGO -QMAKE_LIB = lib /NOLOGO -QMAKE_RC = rc /NOLOGO - -VCPROJ_EXTENSION = .vcproj -VCSOLUTION_EXTENSION = .sln -VCPROJ_KEYWORD = Qt4VSv1.0 - -include(angle.conf) -include(windows-vulkan.conf) diff --git a/5.12.0/qtbase/src/plugins/platforms/cocoa/qcocoaapplicationdelegate.mm b/5.12.0/qtbase/src/plugins/platforms/cocoa/qcocoaapplicationdelegate.mm deleted file mode 100644 index 48fb16f..0000000 --- a/5.12.0/qtbase/src/plugins/platforms/cocoa/qcocoaapplicationdelegate.mm +++ /dev/null @@ -1,503 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2018 The Qt Company Ltd. -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the plugins of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:LGPL$ -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and The Qt Company. For licensing terms -** and conditions see https://www.qt.io/terms-conditions. For further -** information use the contact form at https://www.qt.io/contact-us. -** -** GNU Lesser General Public License Usage -** Alternatively, this file may be used under the terms of the GNU Lesser -** General Public License version 3 as published by the Free Software -** Foundation and appearing in the file LICENSE.LGPL3 included in the -** packaging of this file. Please review the following information to -** ensure the GNU Lesser General Public License version 3 requirements -** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. -** -** GNU General Public License Usage -** Alternatively, this file may be used under the terms of the GNU -** General Public License version 2.0 or (at your option) the GNU General -** Public license version 3 or any later version approved by the KDE Free -** Qt Foundation. The licenses are as published by the Free Software -** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 -** included in the packaging of this file. Please review the following -** information to ensure the GNU General Public License requirements will -** be met: https://www.gnu.org/licenses/gpl-2.0.html and -** https://www.gnu.org/licenses/gpl-3.0.html. -** -** $QT_END_LICENSE$ -** -****************************************************************************/ - -/**************************************************************************** - ** - ** Copyright (c) 2007-2008, Apple, Inc. - ** - ** All rights reserved. - ** - ** Redistribution and use in source and binary forms, with or without - ** modification, are permitted provided that the following conditions are met: - ** - ** * Redistributions of source code must retain the above copyright notice, - ** this list of conditions and the following disclaimer. - ** - ** * Redistributions in binary form must reproduce the above copyright notice, - ** this list of conditions and the following disclaimer in the documentation - ** and/or other materials provided with the distribution. - ** - ** * Neither the name of Apple, Inc. nor the names of its contributors - ** may be used to endorse or promote products derived from this software - ** without specific prior written permission. - ** - ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR - ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR - ** CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, - ** EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, - ** PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR - ** PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF - ** LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING - ** NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS - ** SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - ** - ****************************************************************************/ - - -#import "qcocoaapplicationdelegate.h" -#include "qcocoaintegration.h" -#include "qcocoamenu.h" -#include "qcocoamenuloader.h" -#include "qcocoamenuitem.h" -#include "qcocoansmenu.h" - -#include -#include -#include -#include -#include -#include "qt_mac_p.h" -#include -#include - -QT_USE_NAMESPACE - -@implementation QCocoaApplicationDelegate { - bool startedQuit; - NSObject *reflectionDelegate; - bool inLaunch; - QWindowList hiddenWindows; -} - -+ (instancetype)sharedDelegate -{ - static QCocoaApplicationDelegate *shared = nil; - static dispatch_once_t onceToken; - dispatch_once(&onceToken, ^{ - shared = [[self alloc] init]; - atexit_b(^{ - [shared release]; - shared = nil; - }); - }); - return shared; -} - -- (instancetype)init -{ - self = [super init]; - if (self) { - inLaunch = true; - [[NSNotificationCenter defaultCenter] - addObserver:self - selector:@selector(updateScreens:) - name:NSApplicationDidChangeScreenParametersNotification - object:NSApp]; - } - return self; -} - -- (void)updateScreens:(NSNotification *)notification -{ - Q_UNUSED(notification); - if (QCocoaIntegration *ci = QCocoaIntegration::instance()) - ci->updateScreens(); -} - -- (void)dealloc -{ - [_dockMenu release]; - if (reflectionDelegate) { - [[NSApplication sharedApplication] setDelegate:reflectionDelegate]; - [reflectionDelegate release]; - } - [[NSNotificationCenter defaultCenter] removeObserver:self]; - - [super dealloc]; -} - -- (NSMenu *)applicationDockMenu:(NSApplication *)sender -{ - Q_UNUSED(sender); - // Manually invoke the delegate's -menuWillOpen: method. - // See QTBUG-39604 (and its fix) for details. - [self.dockMenu.delegate menuWillOpen:self.dockMenu]; - return [[self.dockMenu retain] autorelease]; -} - -- (BOOL)canQuit -{ - [[NSApp mainMenu] cancelTracking]; - - bool handle_quit = true; - NSMenuItem *quitMenuItem = [[QT_MANGLE_NAMESPACE(QCocoaMenuLoader) sharedMenuLoader] quitMenuItem]; - if (!QGuiApplicationPrivate::instance()->modalWindowList.isEmpty() - && [quitMenuItem isEnabled]) { - int visible = 0; - const QWindowList tlws = QGuiApplication::topLevelWindows(); - for (int i = 0; i < tlws.size(); ++i) { - if (tlws.at(i)->isVisible()) - ++visible; - } - handle_quit = (visible <= 1); - } - - if (handle_quit) { - QCloseEvent ev; - QGuiApplication::sendEvent(qGuiApp, &ev); - if (ev.isAccepted()) { - return YES; - } - } - - return NO; -} - -// This function will only be called when NSApp is actually running. -- (NSApplicationTerminateReply)applicationShouldTerminate:(NSApplication *)sender -{ - // The reflection delegate gets precedence - if (reflectionDelegate) { - if ([reflectionDelegate respondsToSelector:@selector(applicationShouldTerminate:)]) - return [reflectionDelegate applicationShouldTerminate:sender]; - return NSTerminateNow; - } - - if ([self canQuit]) { - if (!startedQuit) { - startedQuit = true; - // Close open windows. This is done in order to deliver de-expose - // events while the event loop is still running. - const QWindowList topLevels = QGuiApplication::topLevelWindows(); - for (int i = 0; i < topLevels.size(); ++i) { - QWindow *topLevelWindow = topLevels.at(i); - // Already closed windows will not have a platform window, skip those - if (topLevelWindow->handle()) - QWindowSystemInterface::handleCloseEvent(topLevelWindow); - } - QWindowSystemInterface::flushWindowSystemEvents(); - - QGuiApplication::exit(0); - startedQuit = false; - } - } - - if (QGuiApplicationPrivate::instance()->threadData->eventLoops.isEmpty()) { - // INVARIANT: No event loop is executing. This probably - // means that Qt is used as a plugin, or as a part of a native - // Cocoa application. In any case it should be fine to - // terminate now: - return NSTerminateNow; - } - - return NSTerminateCancel; -} - -- (void)applicationWillFinishLaunching:(NSNotification *)notification -{ - Q_UNUSED(notification); - - /* - From the Cocoa documentation: "A good place to install event handlers - is in the applicationWillFinishLaunching: method of the application - delegate. At that point, the Application Kit has installed its default - event handlers, so if you install a handler for one of the same events, - it will replace the Application Kit version." - */ - - /* - If Qt is used as a plugin, we let the 3rd party application handle - events like quit and open file events. Otherwise, if we install our own - handlers, we easily end up breaking functionality the 3rd party - application depends on. - */ - NSAppleEventManager *eventManager = [NSAppleEventManager sharedAppleEventManager]; - /*[eventManager setEventHandler:self - andSelector:@selector(appleEventQuit:withReplyEvent:) - forEventClass:kCoreEventClass - andEventID:kAEQuitApplication];*/ - [eventManager setEventHandler:self - andSelector:@selector(getUrl:withReplyEvent:) - forEventClass:kInternetEventClass - andEventID:kAEGetURL]; -} - -// called by QCocoaIntegration's destructor before resetting the application delegate to nil -- (void)removeAppleEventHandlers -{ - NSAppleEventManager *eventManager = [NSAppleEventManager sharedAppleEventManager]; - //[eventManager removeEventHandlerForEventClass:kCoreEventClass andEventID:kAEQuitApplication]; - [eventManager removeEventHandlerForEventClass:kInternetEventClass andEventID:kAEGetURL]; -} - -- (bool)inLaunch -{ - return inLaunch; -} - -- (void)applicationDidFinishLaunching:(NSNotification *)aNotification -{ - Q_UNUSED(aNotification); - inLaunch = false; - - if (qEnvironmentVariableIsEmpty("QT_MAC_DISABLE_FOREGROUND_APPLICATION_TRANSFORM")) { - // Move the application window to front to avoid launching behind the terminal. - // Ignoring other apps is necessary (we must ignore the terminal), but makes - // Qt apps play slightly less nice with other apps when lanching from Finder - // (See the activateIgnoringOtherApps docs.) - [[NSApplication sharedApplication] activateIgnoringOtherApps:YES]; - } -} - -- (void)application:(NSApplication *)sender openFiles:(NSArray *)filenames -{ - Q_UNUSED(filenames); - Q_UNUSED(sender); - - for (NSString *fileName in filenames) { - QString qtFileName = QString::fromNSString(fileName); - if (inLaunch) { - // We need to be careful because Cocoa will be nice enough to take - // command line arguments and send them to us as events. Given the history - // of Qt Applications, this will result in behavior people don't want, as - // they might be doing the opening themselves with the command line parsing. - if (qApp->arguments().contains(qtFileName)) - continue; - } - QWindowSystemInterface::handleFileOpenEvent(qtFileName); - } - - if (reflectionDelegate && - [reflectionDelegate respondsToSelector:@selector(application:openFiles:)]) - [reflectionDelegate application:sender openFiles:filenames]; - -} - -- (BOOL)applicationShouldTerminateAfterLastWindowClosed:(NSApplication *)sender -{ - // If we have a reflection delegate, that will get to call the shots. - if (reflectionDelegate - && [reflectionDelegate respondsToSelector: - @selector(applicationShouldTerminateAfterLastWindowClosed:)]) - return [reflectionDelegate applicationShouldTerminateAfterLastWindowClosed:sender]; - return NO; // Someday qApp->quitOnLastWindowClosed(); when QApp and NSApp work closer together. -} - -- (void)applicationWillHide:(NSNotification *)notification -{ - if (reflectionDelegate - && [reflectionDelegate respondsToSelector:@selector(applicationWillHide:)]) { - [reflectionDelegate applicationWillHide:notification]; - } - - // When the application is hidden Qt will hide the popup windows associated with - // it when it has lost the activation for the application. However, when it gets - // to this point it believes the popup windows to be hidden already due to the - // fact that the application itself is hidden, which will cause a problem when - // the application is made visible again. - const QWindowList topLevelWindows = QGuiApplication::topLevelWindows(); - for (QWindow *topLevelWindow : topLevelWindows) { - if ((topLevelWindow->type() & Qt::Popup) == Qt::Popup && topLevelWindow->isVisible()) { - topLevelWindow->hide(); - - if ((topLevelWindow->type() & Qt::Tool) == Qt::Tool) - hiddenWindows << topLevelWindow; - } - } -} - -- (void)applicationDidUnhide:(NSNotification *)notification -{ - if (reflectionDelegate - && [reflectionDelegate respondsToSelector:@selector(applicationDidUnhide:)]) - [reflectionDelegate applicationDidUnhide:notification]; - - for (QWindow *window : qAsConst(hiddenWindows)) - window->show(); - - hiddenWindows.clear(); -} - -- (void)applicationDidBecomeActive:(NSNotification *)notification -{ - if (reflectionDelegate - && [reflectionDelegate respondsToSelector:@selector(applicationDidBecomeActive:)]) - [reflectionDelegate applicationDidBecomeActive:notification]; - - QWindowSystemInterface::handleApplicationStateChanged(Qt::ApplicationActive); -/* - onApplicationChangedActivation(true); - - if (!QWidget::mouseGrabber()){ - // Update enter/leave immidiatly, don't wait for a move event. But only - // if no grab exists (even if the grab points to this widget, it seems, ref X11) - QPoint qlocal, qglobal; - QWidget *widgetUnderMouse = 0; - qt_mac_getTargetForMouseEvent(0, QEvent::Enter, qlocal, qglobal, 0, &widgetUnderMouse); - QApplicationPrivate::dispatchEnterLeave(widgetUnderMouse, 0); - qt_last_mouse_receiver = widgetUnderMouse; - qt_last_native_mouse_receiver = widgetUnderMouse ? - (widgetUnderMouse->internalWinId() ? widgetUnderMouse : widgetUnderMouse->nativeParentWidget()) : 0; - } -*/ -} - -- (void)applicationDidResignActive:(NSNotification *)notification -{ - if (reflectionDelegate - && [reflectionDelegate respondsToSelector:@selector(applicationDidResignActive:)]) - [reflectionDelegate applicationDidResignActive:notification]; - - QWindowSystemInterface::handleApplicationStateChanged(Qt::ApplicationInactive); -/* - onApplicationChangedActivation(false); - - if (!QWidget::mouseGrabber()) - QApplicationPrivate::dispatchEnterLeave(0, qt_last_mouse_receiver); - qt_last_mouse_receiver = 0; - qt_last_native_mouse_receiver = 0; - qt_button_down = 0; -*/ -} - -- (BOOL)applicationShouldHandleReopen:(NSApplication *)theApplication hasVisibleWindows:(BOOL)flag -{ - Q_UNUSED(theApplication); - Q_UNUSED(flag); - if (reflectionDelegate - && [reflectionDelegate respondsToSelector:@selector(applicationShouldHandleReopen:hasVisibleWindows:)]) - return [reflectionDelegate applicationShouldHandleReopen:theApplication hasVisibleWindows:flag]; - - /* - true to force delivery of the event even if the application state is already active, - because rapp (handle reopen) events are sent each time the dock icon is clicked regardless - of the active state of the application or number of visible windows. For example, a browser - app that has no windows opened would need the event be to delivered even if it was already - active in order to create a new window as per OS X conventions. - */ - QWindowSystemInterface::handleApplicationStateChanged(Qt::ApplicationActive, true /*forcePropagate*/); - - return YES; -} - -- (void)setReflectionDelegate:(NSObject *)oldDelegate -{ - [oldDelegate retain]; - [reflectionDelegate release]; - reflectionDelegate = oldDelegate; -} - -- (NSMethodSignature *)methodSignatureForSelector:(SEL)aSelector -{ - NSMethodSignature *result = [super methodSignatureForSelector:aSelector]; - if (!result && reflectionDelegate) { - result = [reflectionDelegate methodSignatureForSelector:aSelector]; - } - return result; -} - -- (BOOL)respondsToSelector:(SEL)aSelector -{ - BOOL result = [super respondsToSelector:aSelector]; - if (!result && reflectionDelegate) - result = [reflectionDelegate respondsToSelector:aSelector]; - return result; -} - -- (void)forwardInvocation:(NSInvocation *)invocation -{ - SEL invocationSelector = [invocation selector]; - if (reflectionDelegate && [reflectionDelegate respondsToSelector:invocationSelector]) - [invocation invokeWithTarget:reflectionDelegate]; - else - [self doesNotRecognizeSelector:invocationSelector]; -} - -- (void)getUrl:(NSAppleEventDescriptor *)event withReplyEvent:(NSAppleEventDescriptor *)replyEvent -{ - Q_UNUSED(replyEvent); - NSString *urlString = [[event paramDescriptorForKeyword:keyDirectObject] stringValue]; - QWindowSystemInterface::handleFileOpenEvent(QUrl(QString::fromNSString(urlString))); -} - -- (void)appleEventQuit:(NSAppleEventDescriptor *)event withReplyEvent:(NSAppleEventDescriptor *)replyEvent -{ - Q_UNUSED(event); - Q_UNUSED(replyEvent); - [NSApp terminate:self]; -} - -@end - -@implementation QCocoaApplicationDelegate (Menus) - -- (BOOL)validateMenuItem:(NSMenuItem*)item -{ - auto *nativeItem = qt_objc_cast(item); - if (!nativeItem) - return item.enabled; // FIXME Test with with Qt as plugin or embedded QWindow. - - auto *platformItem = nativeItem.platformMenuItem; - if (!platformItem) // Try a bit harder with orphan menu itens - return item.hasSubmenu || (item.enabled && (item.action != @selector(qt_itemFired:))); - - // Menu-holding items are always enabled, as it's conventional in Cocoa - if (platformItem->menu()) - return YES; - - return platformItem->isEnabled(); -} - -@end - -@implementation QCocoaApplicationDelegate (MenuAPI) - -- (void)qt_itemFired:(QCocoaNSMenuItem *)item -{ - if (item.hasSubmenu) - return; - - auto *nativeItem = qt_objc_cast(item); - Q_ASSERT_X(nativeItem, qPrintable(__FUNCTION__), "Triggered menu item is not a QCocoaNSMenuItem."); - auto *platformItem = nativeItem.platformMenuItem; - // Menu-holding items also get a target to play nicely - // with NSMenuValidation but should not trigger. - if (!platformItem || platformItem->menu()) - return; - - QScopedScopeLevelCounter scopeLevelCounter(QGuiApplicationPrivate::instance()->threadData); - QGuiApplicationPrivate::modifier_buttons = [QNSView convertKeyModifiers:[NSEvent modifierFlags]]; - - static QMetaMethod activatedSignal = QMetaMethod::fromSignal(&QCocoaMenuItem::activated); - activatedSignal.invoke(platformItem, Qt::QueuedConnection); -} - -@end diff --git a/5.12.0/qtbase/src/plugins/platforms/cocoa/qcocoasystemsettings.mm b/5.12.0/qtbase/src/plugins/platforms/cocoa/qcocoasystemsettings.mm deleted file mode 100644 index 29377c1..0000000 --- a/5.12.0/qtbase/src/plugins/platforms/cocoa/qcocoasystemsettings.mm +++ /dev/null @@ -1,241 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2017 The Qt Company Ltd. -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the plugins of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:LGPL$ -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and The Qt Company. For licensing terms -** and conditions see https://www.qt.io/terms-conditions. For further -** information use the contact form at https://www.qt.io/contact-us. -** -** GNU Lesser General Public License Usage -** Alternatively, this file may be used under the terms of the GNU Lesser -** General Public License version 3 as published by the Free Software -** Foundation and appearing in the file LICENSE.LGPL3 included in the -** packaging of this file. Please review the following information to -** ensure the GNU Lesser General Public License version 3 requirements -** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. -** -** GNU General Public License Usage -** Alternatively, this file may be used under the terms of the GNU -** General Public License version 2.0 or (at your option) the GNU General -** Public license version 3 or any later version approved by the KDE Free -** Qt Foundation. The licenses are as published by the Free Software -** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 -** included in the packaging of this file. Please review the following -** information to ensure the GNU General Public License requirements will -** be met: https://www.gnu.org/licenses/gpl-2.0.html and -** https://www.gnu.org/licenses/gpl-3.0.html. -** -** $QT_END_LICENSE$ -** -****************************************************************************/ - -#include "qcocoasystemsettings.h" - -#include "qcocoahelpers.h" - -#include -#include -#include - -#if !QT_MACOS_PLATFORM_SDK_EQUAL_OR_ABOVE(__MAC_10_14) -@interface NSColor (MojaveForwardDeclarations) -@property (class, strong, readonly) NSColor *selectedContentBackgroundColor NS_AVAILABLE_MAC(10_14); -@property (class, strong, readonly) NSColor *unemphasizedSelectedTextBackgroundColor NS_AVAILABLE_MAC(10_14); -@property (class, strong, readonly) NSColor *unemphasizedSelectedTextColor NS_AVAILABLE_MAC(10_14); -@property (class, strong, readonly) NSColor *unemphasizedSelectedContentBackgroundColor NS_AVAILABLE_MAC(10_14); -@property (class, strong, readonly) NSArray *alternatingContentBackgroundColors NS_AVAILABLE_MAC(10_14); -@end -#endif - -QT_BEGIN_NAMESPACE - -QPalette * qt_mac_createSystemPalette() -{ - QColor qc; - - // Standard palette initialization (copied from Qt 4 styles) - QBrush backgroundBrush = qt_mac_toQBrush([NSColor windowBackgroundColor]); - QColor background = backgroundBrush.color(); - QColor light(background.lighter(110)); - QColor dark(background.darker(160)); - QColor mid(background.darker(140)); - QPalette *palette = new QPalette(Qt::black, background, light, dark, mid, Qt::black, Qt::white); - - palette->setBrush(QPalette::Window, backgroundBrush); - - palette->setBrush(QPalette::Disabled, QPalette::WindowText, dark); - palette->setBrush(QPalette::Disabled, QPalette::Text, dark); - palette->setBrush(QPalette::Disabled, QPalette::ButtonText, dark); - palette->setBrush(QPalette::Disabled, QPalette::Base, backgroundBrush); - QBrush textBackgroundBrush = qt_mac_toQBrush([NSColor textBackgroundColor]); - palette->setBrush(QPalette::Active, QPalette::Base, textBackgroundBrush); - palette->setBrush(QPalette::Inactive, QPalette::Base, textBackgroundBrush); - palette->setColor(QPalette::Disabled, QPalette::Dark, QColor(191, 191, 191)); - palette->setColor(QPalette::Active, QPalette::Dark, QColor(191, 191, 191)); - palette->setColor(QPalette::Inactive, QPalette::Dark, QColor(191, 191, 191)); - - // System palette initialization: - QBrush br = qt_mac_toQBrush([NSColor selectedControlColor]); - palette->setBrush(QPalette::Active, QPalette::Highlight, br); - if (__builtin_available(macOS 10.14, *)) { - const auto inactiveHighlight = qt_mac_toQBrush([NSColor unemphasizedSelectedContentBackgroundColor]); - palette->setBrush(QPalette::Inactive, QPalette::Highlight, inactiveHighlight); - palette->setBrush(QPalette::Disabled, QPalette::Highlight, inactiveHighlight); - } else { - palette->setBrush(QPalette::Inactive, QPalette::Highlight, br); - palette->setBrush(QPalette::Disabled, QPalette::Highlight, br); - } - - palette->setBrush(QPalette::Shadow, qt_mac_toQColor([NSColor shadowColor])); - - qc = qt_mac_toQColor([NSColor controlTextColor]); - palette->setColor(QPalette::Active, QPalette::Text, qc); - palette->setColor(QPalette::Active, QPalette::WindowText, qc); - palette->setColor(QPalette::Active, QPalette::HighlightedText, qc); - palette->setColor(QPalette::Inactive, QPalette::Text, qc); - palette->setColor(QPalette::Inactive, QPalette::WindowText, qc); - palette->setColor(QPalette::Inactive, QPalette::HighlightedText, qc); - - qc = qt_mac_toQColor([NSColor disabledControlTextColor]); - palette->setColor(QPalette::Disabled, QPalette::Text, qc); - palette->setColor(QPalette::Disabled, QPalette::WindowText, qc); - palette->setColor(QPalette::Disabled, QPalette::HighlightedText, qc); - - palette->setBrush(QPalette::ToolTipBase, qt_mac_toQBrush([NSColor controlColor])); - - // fix for https://bugreports.qt.io/browse/QTBUG-71740 - palette->setColor(QPalette::Normal, QPalette::Link, qt_mac_toQColor([NSColor linkColor])); - - return palette; -} - -struct QMacPaletteMap { - inline QMacPaletteMap(QPlatformTheme::Palette p, NSColor *a, NSColor *i) : - active(a), inactive(i), paletteRole(p) { } - - NSColor *active; - NSColor *inactive; - QPlatformTheme::Palette paletteRole; -}; - -#define MAC_PALETTE_ENTRY(pal, active, inactive) \ - QMacPaletteMap(pal, [NSColor active], [NSColor inactive]) -static QMacPaletteMap mac_widget_colors[] = { - MAC_PALETTE_ENTRY(QPlatformTheme::ToolButtonPalette, controlTextColor, disabledControlTextColor), - MAC_PALETTE_ENTRY(QPlatformTheme::ButtonPalette, controlTextColor, disabledControlTextColor), - MAC_PALETTE_ENTRY(QPlatformTheme::HeaderPalette, headerTextColor, disabledControlTextColor), - MAC_PALETTE_ENTRY(QPlatformTheme::ComboBoxPalette, controlTextColor, disabledControlTextColor), - MAC_PALETTE_ENTRY(QPlatformTheme::ItemViewPalette, textColor, disabledControlTextColor), - MAC_PALETTE_ENTRY(QPlatformTheme::MessageBoxLabelPalette, textColor, disabledControlTextColor), - MAC_PALETTE_ENTRY(QPlatformTheme::TabBarPalette, controlTextColor, disabledControlTextColor), - MAC_PALETTE_ENTRY(QPlatformTheme::LabelPalette, textColor, disabledControlTextColor), - MAC_PALETTE_ENTRY(QPlatformTheme::GroupBoxPalette, textColor, disabledControlTextColor), - MAC_PALETTE_ENTRY(QPlatformTheme::MenuPalette, controlTextColor, disabledControlTextColor), - MAC_PALETTE_ENTRY(QPlatformTheme::MenuBarPalette, controlTextColor, disabledControlTextColor), - MAC_PALETTE_ENTRY(QPlatformTheme::TextEditPalette, textColor, disabledControlTextColor), - MAC_PALETTE_ENTRY(QPlatformTheme::TextLineEditPalette, textColor, disabledControlTextColor) -}; -#undef MAC_PALETTE_ENTRY - -static const int mac_widget_colors_count = sizeof(mac_widget_colors) / sizeof(mac_widget_colors[0]); - -QHash qt_mac_createRolePalettes() -{ - QHash palettes; - QColor qc; - for (int i = 0; i < mac_widget_colors_count; i++) { - QPalette &pal = *qt_mac_createSystemPalette(); - if (mac_widget_colors[i].active) { - qc = qt_mac_toQColor(mac_widget_colors[i].active); - pal.setColor(QPalette::Active, QPalette::Text, qc); - pal.setColor(QPalette::Inactive, QPalette::Text, qc); - pal.setColor(QPalette::Active, QPalette::WindowText, qc); - pal.setColor(QPalette::Inactive, QPalette::WindowText, qc); - pal.setColor(QPalette::Active, QPalette::HighlightedText, qc); - pal.setColor(QPalette::Inactive, QPalette::HighlightedText, qc); - qc = qt_mac_toQColor(mac_widget_colors[i].inactive); - pal.setColor(QPalette::Disabled, QPalette::Text, qc); - pal.setColor(QPalette::Disabled, QPalette::WindowText, qc); - pal.setColor(QPalette::Disabled, QPalette::HighlightedText, qc); - } - if (mac_widget_colors[i].paletteRole == QPlatformTheme::MenuPalette - || mac_widget_colors[i].paletteRole == QPlatformTheme::MenuBarPalette) { - NSColor *selectedMenuItemColor = nil; - if (__builtin_available(macOS 10.14, *)) { - // Cheap approximation for NSVisualEffectView (see deprecation note for selectedMenuItemTextColor) - selectedMenuItemColor = [[NSColor selectedContentBackgroundColor] highlightWithLevel:0.4]; - } else { - // selectedMenuItemColor would presumably be the correct color to use as the background - // for selected menu items. But that color is always blue, and doesn't follow the - // appearance color in system preferences. So we therefore deliberatly choose to use - // keyboardFocusIndicatorColor instead, which appears to have the same color value. - selectedMenuItemColor = [NSColor keyboardFocusIndicatorColor]; - } - pal.setBrush(QPalette::Highlight, qt_mac_toQColor(selectedMenuItemColor)); - qc = qt_mac_toQColor([NSColor labelColor]); - pal.setBrush(QPalette::ButtonText, qc); - pal.setBrush(QPalette::Text, qc); - qc = qt_mac_toQColor([NSColor selectedMenuItemTextColor]); - pal.setBrush(QPalette::HighlightedText, qc); - qc = qt_mac_toQColor([NSColor disabledControlTextColor]); - pal.setBrush(QPalette::Disabled, QPalette::Text, qc); - } else if ((mac_widget_colors[i].paletteRole == QPlatformTheme::ButtonPalette) - || (mac_widget_colors[i].paletteRole == QPlatformTheme::HeaderPalette) - || (mac_widget_colors[i].paletteRole == QPlatformTheme::TabBarPalette)) { - pal.setColor(QPalette::Disabled, QPalette::ButtonText, - pal.color(QPalette::Disabled, QPalette::Text)); - pal.setColor(QPalette::Inactive, QPalette::ButtonText, - pal.color(QPalette::Inactive, QPalette::Text)); - pal.setColor(QPalette::Active, QPalette::ButtonText, - pal.color(QPalette::Active, QPalette::Text)); - } else if (mac_widget_colors[i].paletteRole == QPlatformTheme::ItemViewPalette) { - NSArray *baseColors = nil; - NSColor *activeHighlightColor = nil; - if (__builtin_available(macOS 10.14, *)) { - baseColors = [NSColor alternatingContentBackgroundColors]; - activeHighlightColor = [NSColor selectedContentBackgroundColor]; - pal.setBrush(QPalette::Inactive, QPalette::HighlightedText, - qt_mac_toQBrush([NSColor unemphasizedSelectedTextColor])); - } else { - baseColors = [NSColor controlAlternatingRowBackgroundColors]; - activeHighlightColor = [NSColor alternateSelectedControlColor]; - pal.setBrush(QPalette::Inactive, QPalette::HighlightedText, - pal.brush(QPalette::Active, QPalette::Text)); - } - pal.setBrush(QPalette::Base, qt_mac_toQBrush(baseColors[0])); - pal.setBrush(QPalette::AlternateBase, qt_mac_toQBrush(baseColors[1])); - pal.setBrush(QPalette::Active, QPalette::Highlight, - qt_mac_toQBrush(activeHighlightColor)); - pal.setBrush(QPalette::Active, QPalette::HighlightedText, - qt_mac_toQBrush([NSColor alternateSelectedControlTextColor])); - pal.setBrush(QPalette::Inactive, QPalette::Text, - pal.brush(QPalette::Active, QPalette::Text)); - } else if (mac_widget_colors[i].paletteRole == QPlatformTheme::TextEditPalette) { - pal.setBrush(QPalette::Active, QPalette::Base, qt_mac_toQColor([NSColor textBackgroundColor])); - pal.setBrush(QPalette::Inactive, QPalette::Text, - pal.brush(QPalette::Active, QPalette::Text)); - pal.setBrush(QPalette::Inactive, QPalette::HighlightedText, - pal.brush(QPalette::Active, QPalette::Text)); - } else if (mac_widget_colors[i].paletteRole == QPlatformTheme::TextLineEditPalette - || mac_widget_colors[i].paletteRole == QPlatformTheme::ComboBoxPalette) { - pal.setBrush(QPalette::Active, QPalette::Base, qt_mac_toQColor([NSColor textBackgroundColor])); - pal.setBrush(QPalette::Disabled, QPalette::Base, - pal.brush(QPalette::Active, QPalette::Base)); - } else if (mac_widget_colors[i].paletteRole == QPlatformTheme::LabelPalette) { - qc = qt_mac_toQColor([NSColor labelColor]); - pal.setBrush(QPalette::Inactive, QPalette::ToolTipText, qc); - } - palettes.insert(mac_widget_colors[i].paletteRole, &pal); - } - return palettes; -} - -QT_END_NAMESPACE diff --git a/5.12.0/qtbase/src/plugins/styles/mac/qmacstyle_mac.mm b/5.12.0/qtbase/src/plugins/styles/mac/qmacstyle_mac.mm deleted file mode 100644 index c2b6fbf..0000000 --- a/5.12.0/qtbase/src/plugins/styles/mac/qmacstyle_mac.mm +++ /dev/null @@ -1,6475 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2016 The Qt Company Ltd. -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the QtWidgets module of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:LGPL$ -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and The Qt Company. For licensing terms -** and conditions see https://www.qt.io/terms-conditions. For further -** information use the contact form at https://www.qt.io/contact-us. -** -** GNU Lesser General Public License Usage -** Alternatively, this file may be used under the terms of the GNU Lesser -** General Public License version 3 as published by the Free Software -** Foundation and appearing in the file LICENSE.LGPL3 included in the -** packaging of this file. Please review the following information to -** ensure the GNU Lesser General Public License version 3 requirements -** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. -** -** GNU General Public License Usage -** Alternatively, this file may be used under the terms of the GNU -** General Public License version 2.0 or (at your option) the GNU General -** Public license version 3 or any later version approved by the KDE Free -** Qt Foundation. The licenses are as published by the Free Software -** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 -** included in the packaging of this file. Please review the following -** information to ensure the GNU General Public License requirements will -** be met: https://www.gnu.org/licenses/gpl-2.0.html and -** https://www.gnu.org/licenses/gpl-3.0.html. -** -** $QT_END_LICENSE$ -** -****************************************************************************/ - -/* - Note: The qdoc comments for QMacStyle are contained in - .../doc/src/qstyles.qdoc. -*/ - -#include - -#include "qmacstyle_mac_p.h" -#include "qmacstyle_mac_p_p.h" - -#define QMAC_QAQUASTYLE_SIZE_CONSTRAIN -//#define DEBUG_SIZE_CONSTRAINT - -#include -#if QT_CONFIG(tabbar) -#include -#endif -#include -#include -#include -#if QT_CONFIG(combobox) -#include -#include -#endif -#if QT_CONFIG(dialogbuttonbox) -#include -#endif -#if QT_CONFIG(dockwidget) -#include -#endif -#include -#include -#include -#include -#include -#include -#if QT_CONFIG(lineedit) -#include -#endif -#if QT_CONFIG(mainwindow) -#include -#endif -#if QT_CONFIG(mdiarea) -#include -#endif -#if QT_CONFIG(menubar) -#include -#endif -#include -#include -#include -#include -#if QT_CONFIG(progressbar) -#include -#endif -#if QT_CONFIG(pushbutton) -#include -#endif -#include -#if QT_CONFIG(rubberband) -#include -#endif -#if QT_CONFIG(scrollbar) -#include -#endif -#if QT_CONFIG(sizegrip) -#include -#endif -#include -#include -#if QT_CONFIG(toolbutton) -#include -#endif -#if QT_CONFIG(treeview) -#include -#endif -#if QT_CONFIG(tableview) -#include -#endif -#include -#if QT_CONFIG(wizard) -#include -#endif -#include -#if QT_CONFIG(datetimeedit) -#include -#endif -#include -#include -#if QT_CONFIG(graphicsview) -#include -#endif -#include -#include -#include -#include -#include -#include -#include - -QT_USE_NAMESPACE - -static QWindow *qt_getWindow(const QWidget *widget) -{ - return widget ? widget->window()->windowHandle() : 0; -} - -@interface QT_MANGLE_NAMESPACE(NotificationReceiver) : NSObject -@end - -QT_NAMESPACE_ALIAS_OBJC_CLASS(NotificationReceiver); - -@implementation NotificationReceiver - -- (void)scrollBarStyleDidChange:(NSNotification *)notification -{ - Q_UNUSED(notification); - - // purge destroyed scroll bars: - QMacStylePrivate::scrollBars.removeAll(QPointer()); - - QEvent event(QEvent::StyleChange); - for (const auto &o : QMacStylePrivate::scrollBars) - QCoreApplication::sendEvent(o, &event); -} -@end - -@interface QT_MANGLE_NAMESPACE(QIndeterminateProgressIndicator) : NSProgressIndicator - -@property (readonly, nonatomic) NSInteger animators; - -- (instancetype)init; - -- (void)startAnimation; -- (void)stopAnimation; - -- (void)drawWithFrame:(CGRect)rect inView:(NSView *)view; - -@end - -QT_NAMESPACE_ALIAS_OBJC_CLASS(QIndeterminateProgressIndicator); - -@implementation QIndeterminateProgressIndicator - -- (instancetype)init -{ - if ((self = [super init])) { - _animators = 0; - self.indeterminate = YES; - self.usesThreadedAnimation = NO; - self.alphaValue = 0.0; - } - - return self; -} - -- (void)startAnimation -{ - if (_animators == 0) { - self.hidden = NO; - [super startAnimation:self]; - } - ++_animators; -} - -- (void)stopAnimation -{ - --_animators; - if (_animators == 0) { - [super stopAnimation:self]; - self.hidden = YES; - [self removeFromSuperviewWithoutNeedingDisplay]; - } -} - -- (void)drawWithFrame:(CGRect)rect inView:(NSView *)view -{ - // The alphaValue change is not strictly necessary, but feels safer. - self.alphaValue = 1.0; - if (self.superview != view) - [view addSubview:self]; - if (!CGRectEqualToRect(self.frame, rect)) - self.frame = rect; - [self drawRect:rect]; - self.alphaValue = 0.0; -} - -@end - -@interface QT_MANGLE_NAMESPACE(QVerticalSplitView) : NSSplitView -- (BOOL)isVertical; -@end - -QT_NAMESPACE_ALIAS_OBJC_CLASS(QVerticalSplitView); - -@implementation QVerticalSplitView -- (BOOL)isVertical -{ - return YES; -} -@end - -// See render code in drawPrimitive(PE_FrameTabWidget) -@interface QT_MANGLE_NAMESPACE(QDarkNSBox) : NSBox -@end - -QT_NAMESPACE_ALIAS_OBJC_CLASS(QDarkNSBox); - -@implementation QDarkNSBox -- (instancetype)init -{ - if ((self = [super init])) { - self.title = @""; - self.titlePosition = NSNoTitle; - self.boxType = NSBoxCustom; - self.cornerRadius = 3; - self.borderColor = [NSColor.controlColor colorWithAlphaComponent:0.1]; - self.fillColor = [NSColor.darkGrayColor colorWithAlphaComponent:0.2]; - } - - return self; -} - -- (void)drawRect:(NSRect)rect -{ - [super drawRect:rect]; -} -@end - -QT_BEGIN_NAMESPACE - -// The following constants are used for adjusting the size -// of push buttons so that they are drawn inside their bounds. -const int QMacStylePrivate::PushButtonLeftOffset = 6; -const int QMacStylePrivate::PushButtonRightOffset = 12; -const int QMacStylePrivate::PushButtonContentPadding = 6; - -QVector > QMacStylePrivate::scrollBars; - -// Title bar gradient colors for Lion were determined by inspecting PSDs exported -// using CoreUI's CoreThemeDocument; there is no public API to retrieve them - -static QLinearGradient titlebarGradientActive() -{ - static QLinearGradient darkGradient = [](){ - QLinearGradient gradient; - // FIXME: colors are chosen somewhat arbitrarily and could be fine-tuned, - // or ideally determined by calling a native API. - gradient.setColorAt(0, QColor(47, 47, 47)); - return gradient; - }(); - static QLinearGradient lightGradient = [](){ - QLinearGradient gradient; - gradient.setColorAt(0, QColor(235, 235, 235)); - gradient.setColorAt(0.5, QColor(210, 210, 210)); - gradient.setColorAt(0.75, QColor(195, 195, 195)); - gradient.setColorAt(1, QColor(180, 180, 180)); - return gradient; - }(); - return qt_mac_applicationIsInDarkMode() ? darkGradient : lightGradient; -} - -static QLinearGradient titlebarGradientInactive() -{ - static QLinearGradient darkGradient = [](){ - QLinearGradient gradient; - gradient.setColorAt(1, QColor(42, 42, 42)); - return gradient; - }(); - static QLinearGradient lightGradient = [](){ - QLinearGradient gradient; - gradient.setColorAt(0, QColor(250, 250, 250)); - gradient.setColorAt(1, QColor(225, 225, 225)); - return gradient; - }(); - return qt_mac_applicationIsInDarkMode() ? darkGradient : lightGradient; -} - -static void clipTabBarFrame(const QStyleOption *option, const QMacStyle *style, CGContextRef ctx) -{ - Q_ASSERT(option); - Q_ASSERT(style); - Q_ASSERT(ctx); - - if (qt_mac_applicationIsInDarkMode()) { - QTabWidget *tabWidget = qobject_cast(option->styleObject); - Q_ASSERT(tabWidget); - - const QRect tabBarRect = style->subElementRect(QStyle::SE_TabWidgetTabBar, option, tabWidget).adjusted(2, 2, -3, -2); - const QRegion clipPath = QRegion(option->rect) - tabBarRect; - QVarLengthArray cgRects; - for (const QRect &qtRect : clipPath) - cgRects.push_back(qtRect.toCGRect()); - if (cgRects.size()) - CGContextClipToRects(ctx, &cgRects[0], size_t(cgRects.size())); - } -} - -static const QColor titlebarSeparatorLineActive(111, 111, 111); -static const QColor titlebarSeparatorLineInactive(131, 131, 131); -static const QColor darkModeSeparatorLine(88, 88, 88); - -// Gradient colors used for the dock widget title bar and -// non-unifed tool bar bacground. -static const QColor lightMainWindowGradientBegin(240, 240, 240); -static const QColor lightMainWindowGradientEnd(200, 200, 200); -static const QColor darkMainWindowGradientBegin(47, 47, 47); -static const QColor darkMainWindowGradientEnd(47, 47, 47); - -static const int DisclosureOffset = 4; - -static const qreal titleBarIconTitleSpacing = 5; -static const qreal titleBarTitleRightMargin = 12; -static const qreal titleBarButtonSpacing = 8; - -// Tab bar colors -// active: window is active -// selected: tab is selected -// hovered: tab is hovered -static const QColor tabBarTabBackgroundActive(190, 190, 190); -static const QColor tabBarTabBackgroundActiveHovered(178, 178, 178); -static const QColor tabBarTabBackgroundActiveSelected(211, 211, 211); -static const QColor tabBarTabBackground(227, 227, 227); -static const QColor tabBarTabBackgroundSelected(246, 246, 246); -static const QColor tabBarTabLineActive(160, 160, 160); -static const QColor tabBarTabLineActiveHovered(150, 150, 150); -static const QColor tabBarTabLine(210, 210, 210); -static const QColor tabBarTabLineSelected(189, 189, 189); -static const QColor tabBarCloseButtonBackgroundHovered(162, 162, 162); -static const QColor tabBarCloseButtonBackgroundPressed(153, 153, 153); -static const QColor tabBarCloseButtonBackgroundSelectedHovered(192, 192, 192); -static const QColor tabBarCloseButtonBackgroundSelectedPressed(181, 181, 181); -static const QColor tabBarCloseButtonCross(100, 100, 100); -static const QColor tabBarCloseButtonCrossSelected(115, 115, 115); - -static const int closeButtonSize = 14; -static const qreal closeButtonCornerRadius = 2.0; - -static const int headerSectionArrowHeight = 6; -static const int headerSectionSeparatorInset = 2; - -// One for each of QStyleHelper::WidgetSizePolicy -static const QMarginsF comboBoxFocusRingMargins[3] = { - { 0.5, 2, 3.5, 4 }, - { 0.5, 1, 2.5, 4 }, - { 0.5, 1.5, 2.5, 3.5 } -}; - -static const QMarginsF pullDownButtonShadowMargins[3] = { - { 0.5, -1, 0.5, 2 }, - { 0.5, -1.5, 0.5, 2.5 }, - { 0.5, 0, 0.5, 1 } -}; - -static const QMarginsF pushButtonShadowMargins[3] = { - { 1.5, -1.5, 1.5, 4.5 }, - { 1.5, -1, 1.5, 4 }, - { 1.5, 0.5, 1.5, 2.5 } -}; - -// These are frame heights as reported by Xcode 9's Interface Builder. -// Alignemnet rectangle's heights match for push and popup buttons -// with respective values 21, 18 and 15. - -static const qreal comboBoxDefaultHeight[3] = { - 26, 22, 19 -}; - -static const qreal pushButtonDefaultHeight[3] = { - 32, 28, 16 -}; - -static const qreal popupButtonDefaultHeight[3] = { - 26, 22, 15 -}; - -static const int toolButtonArrowSize = 7; -static const int toolButtonArrowMargin = 2; - -static const qreal focusRingWidth = 3.5; - -static bool setupScroller(NSScroller *scroller, const QStyleOptionSlider *sb) -{ - const qreal length = sb->maximum - sb->minimum + sb->pageStep; - if (qFuzzyIsNull(length)) - return false; - const qreal proportion = sb->pageStep / length; - const qreal range = qreal(sb->maximum - sb->minimum); - qreal value = range ? qreal(sb->sliderValue - sb->minimum) / range : 0; - if (sb->orientation == Qt::Horizontal && sb->direction == Qt::RightToLeft) - value = 1.0 - value; - - scroller.frame = sb->rect.toCGRect(); - scroller.floatValue = value; - scroller.knobProportion = proportion; - return true; -} - -static bool setupSlider(NSSlider *slider, const QStyleOptionSlider *sl) -{ - if (sl->minimum >= sl->maximum) - return false; - - slider.frame = sl->rect.toCGRect(); - slider.minValue = sl->minimum; - slider.maxValue = sl->maximum; - slider.intValue = sl->sliderPosition; - slider.enabled = sl->state & QStyle::State_Enabled; - if (sl->tickPosition != QSlider::NoTicks) { - // Set numberOfTickMarks, but TicksBothSides will be treated differently - int interval = sl->tickInterval; - if (interval == 0) { - interval = sl->pageStep; - if (interval == 0) - interval = sl->singleStep; - if (interval == 0) - interval = 1; // return false? - } - slider.numberOfTickMarks = 1 + ((sl->maximum - sl->minimum) / interval); - - const bool ticksAbove = sl->tickPosition == QSlider::TicksAbove; - if (sl->orientation == Qt::Horizontal) - slider.tickMarkPosition = ticksAbove ? NSTickMarkPositionAbove : NSTickMarkPositionBelow; - else - slider.tickMarkPosition = ticksAbove ? NSTickMarkPositionLeading : NSTickMarkPositionTrailing; - } else { - slider.numberOfTickMarks = 0; - } - - return true; -} - -static bool isInMacUnifiedToolbarArea(QWindow *window, int windowY) -{ - QPlatformNativeInterface *nativeInterface = QGuiApplication::platformNativeInterface(); - QPlatformNativeInterface::NativeResourceForIntegrationFunction function = - nativeInterface->nativeResourceFunctionForIntegration("testContentBorderPosition"); - if (!function) - return false; // Not Cocoa platform plugin. - - typedef bool (*TestContentBorderPositionFunction)(QWindow *, int); - return (reinterpret_cast(function))(window, windowY); -} - - -static void drawTabCloseButton(QPainter *p, bool hover, bool selected, bool pressed, bool documentMode) -{ - p->setRenderHints(QPainter::Antialiasing); - QRect rect(0, 0, closeButtonSize, closeButtonSize); - const int width = rect.width(); - const int height = rect.height(); - - if (hover) { - // draw background circle - QColor background; - if (selected) { - if (documentMode) - background = pressed ? tabBarCloseButtonBackgroundSelectedPressed : tabBarCloseButtonBackgroundSelectedHovered; - else - background = QColor(255, 255, 255, pressed ? 150 : 100); // Translucent white - } else { - background = pressed ? tabBarCloseButtonBackgroundPressed : tabBarCloseButtonBackgroundHovered; - if (!documentMode) - background = background.lighter(pressed ? 135 : 140); // Lighter tab background, lighter color - } - - p->setPen(Qt::transparent); - p->setBrush(background); - p->drawRoundedRect(rect, closeButtonCornerRadius, closeButtonCornerRadius); - } - - // draw cross - const int margin = 3; - QPen crossPen; - crossPen.setColor(selected ? (documentMode ? tabBarCloseButtonCrossSelected : Qt::white) : tabBarCloseButtonCross); - crossPen.setWidthF(1.1); - crossPen.setCapStyle(Qt::FlatCap); - p->setPen(crossPen); - p->drawLine(margin, margin, width - margin, height - margin); - p->drawLine(margin, height - margin, width - margin, margin); -} - -#if QT_CONFIG(tabbar) -QRect rotateTabPainter(QPainter *p, QTabBar::Shape shape, QRect tabRect) -{ - const auto tabDirection = QMacStylePrivate::tabDirection(shape); - if (QMacStylePrivate::verticalTabs(tabDirection)) { - int newX, newY, newRot; - if (tabDirection == QMacStylePrivate::East) { - newX = tabRect.width(); - newY = tabRect.y(); - newRot = 90; - } else { - newX = 0; - newY = tabRect.y() + tabRect.height(); - newRot = -90; - } - tabRect.setRect(0, 0, tabRect.height(), tabRect.width()); - QMatrix m; - m.translate(newX, newY); - m.rotate(newRot); - p->setMatrix(m, true); - } - return tabRect; -} - -void drawTabShape(QPainter *p, const QStyleOptionTab *tabOpt, bool isUnified, int tabOverlap) -{ - QRect rect = tabOpt->rect; - if (QMacStylePrivate::verticalTabs(QMacStylePrivate::tabDirection(tabOpt->shape))) - rect = rect.adjusted(-tabOverlap, 0, 0, 0); - else - rect = rect.adjusted(0, -tabOverlap, 0, 0); - - p->translate(rect.x(), rect.y()); - rect.moveLeft(0); - rect.moveTop(0); - const QRect tabRect = rotateTabPainter(p, tabOpt->shape, rect); - - const int width = tabRect.width(); - const int height = tabRect.height(); - const bool active = (tabOpt->state & QStyle::State_Active); - const bool selected = (tabOpt->state & QStyle::State_Selected); - - const QRect bodyRect(1, 1, width - 2, height - 2); - const QRect topLineRect(1, 0, width - 2, 1); - const QRect bottomLineRect(1, height - 1, width - 2, 1); - if (selected) { - // fill body - if (tabOpt->documentMode && isUnified) { - p->save(); - p->setCompositionMode(QPainter::CompositionMode_Source); - p->fillRect(tabRect, QColor(Qt::transparent)); - p->restore(); - } else if (active) { - p->fillRect(bodyRect, tabBarTabBackgroundActiveSelected); - // top line - p->fillRect(topLineRect, tabBarTabLineSelected); - } else { - p->fillRect(bodyRect, tabBarTabBackgroundSelected); - } - } else { - // when the mouse is over non selected tabs they get a new color - const bool hover = (tabOpt->state & QStyle::State_MouseOver); - if (hover) { - // fill body - p->fillRect(bodyRect, tabBarTabBackgroundActiveHovered); - // bottom line - p->fillRect(bottomLineRect, tabBarTabLineActiveHovered); - } - } - - // separator lines between tabs - const QRect leftLineRect(0, 1, 1, height - 2); - const QRect rightLineRect(width - 1, 1, 1, height - 2); - const QColor separatorLineColor = active ? tabBarTabLineActive : tabBarTabLine; - p->fillRect(leftLineRect, separatorLineColor); - p->fillRect(rightLineRect, separatorLineColor); -} - -void drawTabBase(QPainter *p, const QStyleOptionTabBarBase *tbb, const QWidget *w) -{ - QRect r = tbb->rect; - if (QMacStylePrivate::verticalTabs(QMacStylePrivate::tabDirection(tbb->shape))) - r.setWidth(w->width()); - else - r.setHeight(w->height()); - - const QRect tabRect = rotateTabPainter(p, tbb->shape, r); - const int width = tabRect.width(); - const int height = tabRect.height(); - const bool active = (tbb->state & QStyle::State_Active); - - // fill body - const QRect bodyRect(0, 1, width, height - 1); - const QColor bodyColor = active ? tabBarTabBackgroundActive : tabBarTabBackground; - p->fillRect(bodyRect, bodyColor); - - // top line - const QRect topLineRect(0, 0, width, 1); - const QColor topLineColor = active ? tabBarTabLineActive : tabBarTabLine; - p->fillRect(topLineRect, topLineColor); - - // bottom line - const QRect bottomLineRect(0, height - 1, width, 1); - const QColor bottomLineColor = active ? tabBarTabLineActive : tabBarTabLine; - p->fillRect(bottomLineRect, bottomLineColor); -} -#endif - -static QStyleHelper::WidgetSizePolicy getControlSize(const QStyleOption *option, const QWidget *widget) -{ - const auto wsp = QStyleHelper::widgetSizePolicy(widget, option); - if (wsp == QStyleHelper::SizeDefault) - return QStyleHelper::SizeLarge; - - return wsp; -} - -#if QT_CONFIG(treeview) -static inline bool isTreeView(const QWidget *widget) -{ - return (widget && widget->parentWidget() && - qobject_cast(widget->parentWidget())); -} -#endif - -static QString qt_mac_removeMnemonics(const QString &original) -{ - QString returnText(original.size(), 0); - int finalDest = 0; - int currPos = 0; - int l = original.length(); - while (l) { - if (original.at(currPos) == QLatin1Char('&')) { - ++currPos; - --l; - if (l == 0) - break; - } else if (original.at(currPos) == QLatin1Char('(') && l >= 4 && - original.at(currPos + 1) == QLatin1Char('&') && - original.at(currPos + 2) != QLatin1Char('&') && - original.at(currPos + 3) == QLatin1Char(')')) { - /* remove mnemonics its format is "\s*(&X)" */ - int n = 0; - while (finalDest > n && returnText.at(finalDest - n - 1).isSpace()) - ++n; - finalDest -= n; - currPos += 4; - l -= 4; - continue; - } - returnText[finalDest] = original.at(currPos); - ++currPos; - ++finalDest; - --l; - } - returnText.truncate(finalDest); - return returnText; -} - -static bool qt_macWindowMainWindow(const QWidget *window) -{ - if (QWindow *w = window->windowHandle()) { - if (w->handle()) { - if (NSWindow *nswindow = static_cast(QGuiApplication::platformNativeInterface()->nativeResourceForWindow(QByteArrayLiteral("nswindow"), w))) { - return [nswindow isMainWindow]; - } - } - } - return false; -} - -/***************************************************************************** - QMacCGStyle globals - *****************************************************************************/ -const int macItemFrame = 2; // menu item frame width -const int macItemHMargin = 3; // menu item hor text margin -const int macRightBorder = 12; // right border on mac - -/***************************************************************************** - QMacCGStyle utility functions - *****************************************************************************/ - -enum QAquaMetric { - // Prepend kThemeMetric to get the HIToolBox constant. - // Represents the values already used in QMacStyle. - CheckBoxHeight = 0, - CheckBoxWidth, - EditTextFrameOutset, - FocusRectOutset, - HSliderHeight, - HSliderTickHeight, - LargeProgressBarThickness, - ListHeaderHeight, - MenuSeparatorHeight, // GetThemeMenuSeparatorHeight - MiniCheckBoxHeight, - MiniCheckBoxWidth, - MiniHSliderHeight, - MiniHSliderTickHeight, - MiniPopupButtonHeight, - MiniPushButtonHeight, - MiniRadioButtonHeight, - MiniRadioButtonWidth, - MiniVSliderTickWidth, - MiniVSliderWidth, - NormalProgressBarThickness, - PopupButtonHeight, - ProgressBarShadowOutset, - PushButtonHeight, - RadioButtonHeight, - RadioButtonWidth, - SeparatorSize, - SmallCheckBoxHeight, - SmallCheckBoxWidth, - SmallHSliderHeight, - SmallHSliderTickHeight, - SmallPopupButtonHeight, - SmallProgressBarShadowOutset, - SmallPushButtonHeight, - SmallRadioButtonHeight, - SmallRadioButtonWidth, - SmallVSliderTickWidth, - SmallVSliderWidth, - VSliderTickWidth, - VSliderWidth -}; - -static const int qt_mac_aqua_metrics[] = { - // Values as of macOS 10.12.4 and Xcode 8.3.1 - 18 /* CheckBoxHeight */, - 18 /* CheckBoxWidth */, - 1 /* EditTextFrameOutset */, - 4 /* FocusRectOutset */, - 22 /* HSliderHeight */, - 5 /* HSliderTickHeight */, - 16 /* LargeProgressBarThickness */, - 17 /* ListHeaderHeight */, - 12 /* MenuSeparatorHeight, aka GetThemeMenuSeparatorHeight */, - 11 /* MiniCheckBoxHeight */, - 10 /* MiniCheckBoxWidth */, - 12 /* MiniHSliderHeight */, - 4 /* MiniHSliderTickHeight */, - 15 /* MiniPopupButtonHeight */, - 16 /* MiniPushButtonHeight */, - 11 /* MiniRadioButtonHeight */, - 10 /* MiniRadioButtonWidth */, - 4 /* MiniVSliderTickWidth */, - 12 /* MiniVSliderWidth */, - 12 /* NormalProgressBarThickness */, - 20 /* PopupButtonHeight */, - 4 /* ProgressBarShadowOutset */, - 20 /* PushButtonHeight */, - 18 /* RadioButtonHeight */, - 18 /* RadioButtonWidth */, - 1 /* SeparatorSize */, - 16 /* SmallCheckBoxHeight */, - 14 /* SmallCheckBoxWidth */, - 15 /* SmallHSliderHeight */, - 4 /* SmallHSliderTickHeight */, - 17 /* SmallPopupButtonHeight */, - 2 /* SmallProgressBarShadowOutset */, - 17 /* SmallPushButtonHeight */, - 15 /* SmallRadioButtonHeight */, - 14 /* SmallRadioButtonWidth */, - 4 /* SmallVSliderTickWidth */, - 15 /* SmallVSliderWidth */, - 5 /* VSliderTickWidth */, - 22 /* VSliderWidth */ -}; - -static inline int qt_mac_aqua_get_metric(QAquaMetric m) -{ - return qt_mac_aqua_metrics[m]; -} - -static QSize qt_aqua_get_known_size(QStyle::ContentsType ct, const QWidget *widg, QSize szHint, - QStyleHelper::WidgetSizePolicy sz) -{ - QSize ret(-1, -1); - if (sz != QStyleHelper::SizeSmall && sz != QStyleHelper::SizeLarge && sz != QStyleHelper::SizeMini) { - qDebug("Not sure how to return this..."); - return ret; - } - if ((widg && widg->testAttribute(Qt::WA_SetFont)) || !QApplication::desktopSettingsAware()) { - // If you're using a custom font and it's bigger than the default font, - // then no constraints for you. If you are smaller, we can try to help you out - QFont font = qt_app_fonts_hash()->value(widg->metaObject()->className(), QFont()); - if (widg->font().pointSize() > font.pointSize()) - return ret; - } - - if (ct == QStyle::CT_CustomBase && widg) { -#if QT_CONFIG(pushbutton) - if (qobject_cast(widg)) - ct = QStyle::CT_PushButton; -#endif - else if (qobject_cast(widg)) - ct = QStyle::CT_RadioButton; -#if QT_CONFIG(checkbox) - else if (qobject_cast(widg)) - ct = QStyle::CT_CheckBox; -#endif -#if QT_CONFIG(combobox) - else if (qobject_cast(widg)) - ct = QStyle::CT_ComboBox; -#endif -#if QT_CONFIG(toolbutton) - else if (qobject_cast(widg)) - ct = QStyle::CT_ToolButton; -#endif - else if (qobject_cast(widg)) - ct = QStyle::CT_Slider; -#if QT_CONFIG(progressbar) - else if (qobject_cast(widg)) - ct = QStyle::CT_ProgressBar; -#endif -#if QT_CONFIG(lineedit) - else if (qobject_cast(widg)) - ct = QStyle::CT_LineEdit; -#endif - else if (qobject_cast(widg)) - ct = QStyle::CT_HeaderSection; -#if QT_CONFIG(menubar) - else if (qobject_cast(widg)) - ct = QStyle::CT_MenuBar; -#endif -#if QT_CONFIG(sizegrip) - else if (qobject_cast(widg)) - ct = QStyle::CT_SizeGrip; -#endif - else - return ret; - } - - switch (ct) { -#if QT_CONFIG(pushbutton) - case QStyle::CT_PushButton: { - const QPushButton *psh = qobject_cast(widg); - // If this comparison is false, then the widget was not a push button. - // This is bad and there's very little we can do since we were requested to find a - // sensible size for a widget that pretends to be a QPushButton but is not. - if(psh) { - QString buttonText = qt_mac_removeMnemonics(psh->text()); - if (buttonText.contains(QLatin1Char('\n'))) - ret = QSize(-1, -1); - else if (sz == QStyleHelper::SizeLarge) - ret = QSize(-1, qt_mac_aqua_get_metric(PushButtonHeight)); - else if (sz == QStyleHelper::SizeSmall) - ret = QSize(-1, qt_mac_aqua_get_metric(SmallPushButtonHeight)); - else if (sz == QStyleHelper::SizeMini) - ret = QSize(-1, qt_mac_aqua_get_metric(MiniPushButtonHeight)); - - if (!psh->icon().isNull()){ - // If the button got an icon, and the icon is larger than the - // button, we can't decide on a default size - ret.setWidth(-1); - if (ret.height() < psh->iconSize().height()) - ret.setHeight(-1); - } - else if (buttonText == QLatin1String("OK") || buttonText == QLatin1String("Cancel")){ - // Aqua Style guidelines restrict the size of OK and Cancel buttons to 68 pixels. - // However, this doesn't work for German, therefore only do it for English, - // I suppose it would be better to do some sort of lookups for languages - // that like to have really long words. - // FIXME This is not exactly true. Out of context, OK buttons have their - // implicit size calculated the same way as any other button. Inside a - // QDialogButtonBox, their size should be calculated such that the action - // or accept button (i.e., rightmost) and cancel button have the same width. - ret.setWidth(69); - } - } else { - // The only sensible thing to do is to return whatever the style suggests... - if (sz == QStyleHelper::SizeLarge) - ret = QSize(-1, qt_mac_aqua_get_metric(PushButtonHeight)); - else if (sz == QStyleHelper::SizeSmall) - ret = QSize(-1, qt_mac_aqua_get_metric(SmallPushButtonHeight)); - else if (sz == QStyleHelper::SizeMini) - ret = QSize(-1, qt_mac_aqua_get_metric(MiniPushButtonHeight)); - else - // Since there's no default size we return the large size... - ret = QSize(-1, qt_mac_aqua_get_metric(PushButtonHeight)); - } -#endif -#if 0 //Not sure we are applying the rules correctly for RadioButtons/CheckBoxes --Sam - } else if (ct == QStyle::CT_RadioButton) { - QRadioButton *rdo = static_cast(widg); - // Exception for case where multiline radio button text requires no size constrainment - if (rdo->text().find('\n') != -1) - return ret; - if (sz == QStyleHelper::SizeLarge) - ret = QSize(-1, qt_mac_aqua_get_metric(RadioButtonHeight)); - else if (sz == QStyleHelper::SizeSmall) - ret = QSize(-1, qt_mac_aqua_get_metric(SmallRadioButtonHeight)); - else if (sz == QStyleHelper::SizeMini) - ret = QSize(-1, qt_mac_aqua_get_metric(MiniRadioButtonHeight)); - } else if (ct == QStyle::CT_CheckBox) { - if (sz == QStyleHelper::SizeLarge) - ret = QSize(-1, qt_mac_aqua_get_metric(CheckBoxHeight)); - else if (sz == QStyleHelper::SizeSmall) - ret = QSize(-1, qt_mac_aqua_get_metric(SmallCheckBoxHeight)); - else if (sz == QStyleHelper::SizeMini) - ret = QSize(-1, qt_mac_aqua_get_metric(MiniCheckBoxHeight)); -#endif - break; - } - case QStyle::CT_SizeGrip: - // Not HIG kosher: mimic what we were doing earlier until we support 4-edge resizing in MDI subwindows - if (sz == QStyleHelper::SizeLarge || sz == QStyleHelper::SizeSmall) { - int s = sz == QStyleHelper::SizeSmall ? 16 : 22; // large: pixel measured from HITheme, small: from my hat - int width = 0; -#if QT_CONFIG(mdiarea) - if (widg && qobject_cast(widg->parentWidget())) - width = s; -#endif - ret = QSize(width, s); - } - break; - case QStyle::CT_ComboBox: - switch (sz) { - case QStyleHelper::SizeLarge: - ret = QSize(-1, qt_mac_aqua_get_metric(PopupButtonHeight)); - break; - case QStyleHelper::SizeSmall: - ret = QSize(-1, qt_mac_aqua_get_metric(SmallPopupButtonHeight)); - break; - case QStyleHelper::SizeMini: - ret = QSize(-1, qt_mac_aqua_get_metric(MiniPopupButtonHeight)); - break; - default: - break; - } - break; - case QStyle::CT_ToolButton: - if (sz == QStyleHelper::SizeSmall) { - int width = 0, height = 0; - if (szHint == QSize(-1, -1)) { //just 'guess'.. -#if QT_CONFIG(toolbutton) - const QToolButton *bt = qobject_cast(widg); - // If this conversion fails then the widget was not what it claimed to be. - if(bt) { - if (!bt->icon().isNull()) { - QSize iconSize = bt->iconSize(); - QSize pmSize = bt->icon().actualSize(QSize(32, 32), QIcon::Normal); - width = qMax(width, qMax(iconSize.width(), pmSize.width())); - height = qMax(height, qMax(iconSize.height(), pmSize.height())); - } - if (!bt->text().isNull() && bt->toolButtonStyle() != Qt::ToolButtonIconOnly) { - int text_width = bt->fontMetrics().horizontalAdvance(bt->text()), - text_height = bt->fontMetrics().height(); - if (bt->toolButtonStyle() == Qt::ToolButtonTextUnderIcon) { - width = qMax(width, text_width); - height += text_height; - } else { - width += text_width; - width = qMax(height, text_height); - } - } - } else -#endif - { - // Let's return the size hint... - width = szHint.width(); - height = szHint.height(); - } - } else { - width = szHint.width(); - height = szHint.height(); - } - width = qMax(20, width + 5); //border - height = qMax(20, height + 5); //border - ret = QSize(width, height); - } - break; - case QStyle::CT_Slider: { - int w = -1; - const QSlider *sld = qobject_cast(widg); - // If this conversion fails then the widget was not what it claimed to be. - if(sld) { - if (sz == QStyleHelper::SizeLarge) { - if (sld->orientation() == Qt::Horizontal) { - w = qt_mac_aqua_get_metric(HSliderHeight); - if (sld->tickPosition() != QSlider::NoTicks) - w += qt_mac_aqua_get_metric(HSliderTickHeight); - } else { - w = qt_mac_aqua_get_metric(VSliderWidth); - if (sld->tickPosition() != QSlider::NoTicks) - w += qt_mac_aqua_get_metric(VSliderTickWidth); - } - } else if (sz == QStyleHelper::SizeSmall) { - if (sld->orientation() == Qt::Horizontal) { - w = qt_mac_aqua_get_metric(SmallHSliderHeight); - if (sld->tickPosition() != QSlider::NoTicks) - w += qt_mac_aqua_get_metric(SmallHSliderTickHeight); - } else { - w = qt_mac_aqua_get_metric(SmallVSliderWidth); - if (sld->tickPosition() != QSlider::NoTicks) - w += qt_mac_aqua_get_metric(SmallVSliderTickWidth); - } - } else if (sz == QStyleHelper::SizeMini) { - if (sld->orientation() == Qt::Horizontal) { - w = qt_mac_aqua_get_metric(MiniHSliderHeight); - if (sld->tickPosition() != QSlider::NoTicks) - w += qt_mac_aqua_get_metric(MiniHSliderTickHeight); - } else { - w = qt_mac_aqua_get_metric(MiniVSliderWidth); - if (sld->tickPosition() != QSlider::NoTicks) - w += qt_mac_aqua_get_metric(MiniVSliderTickWidth); - } - } - } else { - // This is tricky, we were requested to find a size for a slider which is not - // a slider. We don't know if this is vertical or horizontal or if we need to - // have tick marks or not. - // For this case we will return an horizontal slider without tick marks. - w = qt_mac_aqua_get_metric(HSliderHeight); - w += qt_mac_aqua_get_metric(HSliderTickHeight); - } - if (sld->orientation() == Qt::Horizontal) - ret.setHeight(w); - else - ret.setWidth(w); - break; - } -#if QT_CONFIG(progressbar) - case QStyle::CT_ProgressBar: { - int finalValue = -1; - Qt::Orientation orient = Qt::Horizontal; - if (const QProgressBar *pb = qobject_cast(widg)) - orient = pb->orientation(); - - if (sz == QStyleHelper::SizeLarge) - finalValue = qt_mac_aqua_get_metric(LargeProgressBarThickness) - + qt_mac_aqua_get_metric(ProgressBarShadowOutset); - else - finalValue = qt_mac_aqua_get_metric(NormalProgressBarThickness) - + qt_mac_aqua_get_metric(SmallProgressBarShadowOutset); - if (orient == Qt::Horizontal) - ret.setHeight(finalValue); - else - ret.setWidth(finalValue); - break; - } -#endif -#if QT_CONFIG(combobox) - case QStyle::CT_LineEdit: - if (!widg || !qobject_cast(widg->parentWidget())) { - //should I take into account the font dimentions of the lineedit? -Sam - if (sz == QStyleHelper::SizeLarge) - ret = QSize(-1, 21); - else - ret = QSize(-1, 19); - } - break; -#endif - case QStyle::CT_HeaderSection: -#if QT_CONFIG(treeview) - if (isTreeView(widg)) - ret = QSize(-1, qt_mac_aqua_get_metric(ListHeaderHeight)); -#endif - break; - case QStyle::CT_MenuBar: - if (sz == QStyleHelper::SizeLarge) { - ret = QSize(-1, [[NSApp mainMenu] menuBarHeight]); - // In the qt_mac_set_native_menubar(false) case, - // we come it here with a zero-height main menu, - // preventing the in-window menu from displaying. - // Use 22 pixels for the height, by observation. - if (ret.height() <= 0) - ret.setHeight(22); - } - break; - default: - break; - } - return ret; -} - - -#if defined(QMAC_QAQUASTYLE_SIZE_CONSTRAIN) || defined(DEBUG_SIZE_CONSTRAINT) -static QStyleHelper::WidgetSizePolicy qt_aqua_guess_size(const QWidget *widg, QSize large, QSize small, QSize mini) -{ - Q_UNUSED(widg); - - if (large == QSize(-1, -1)) { - if (small != QSize(-1, -1)) - return QStyleHelper::SizeSmall; - if (mini != QSize(-1, -1)) - return QStyleHelper::SizeMini; - return QStyleHelper::SizeDefault; - } else if (small == QSize(-1, -1)) { - if (mini != QSize(-1, -1)) - return QStyleHelper::SizeMini; - return QStyleHelper::SizeLarge; - } else if (mini == QSize(-1, -1)) { - return QStyleHelper::SizeLarge; - } - - if (qEnvironmentVariableIsSet("QWIDGET_ALL_SMALL")) - return QStyleHelper::SizeSmall; - else if (qEnvironmentVariableIsSet("QWIDGET_ALL_MINI")) - return QStyleHelper::SizeMini; - - return QStyleHelper::SizeLarge; -} -#endif - -void QMacStylePrivate::drawFocusRing(QPainter *p, const QRectF &targetRect, int hMargin, int vMargin, const CocoaControl &cw) const -{ - QPainterPath focusRingPath; - focusRingPath.setFillRule(Qt::OddEvenFill); - - qreal hOffset = 0.0; - qreal vOffset = 0.0; - switch (cw.type) { - case Box: - case Button_SquareButton: - case SegmentedControl_Middle: - case TextField: { - auto innerRect = targetRect; - if (cw.type == TextField) - innerRect = innerRect.adjusted(hMargin, vMargin, -hMargin, -vMargin).adjusted(0.5, 0.5, -0.5, -0.5); - const auto outerRect = innerRect.adjusted(-focusRingWidth, -focusRingWidth, focusRingWidth, focusRingWidth); - const auto outerRadius = focusRingWidth; - focusRingPath.addRect(innerRect); - focusRingPath.addRoundedRect(outerRect, outerRadius, outerRadius); - break; - } - case Button_CheckBox: { - const auto cbInnerRadius = (cw.size == QStyleHelper::SizeMini ? 2.0 : 3.0); - const auto cbSize = cw.size == QStyleHelper::SizeLarge ? 13 : - cw.size == QStyleHelper::SizeSmall ? 11 : 9; // As measured - hOffset = hMargin + (cw.size == QStyleHelper::SizeLarge ? 2.5 : - cw.size == QStyleHelper::SizeSmall ? 2.0 : 1.0); // As measured - vOffset = 0.5 * qreal(targetRect.height() - cbSize); - const auto cbInnerRect = QRectF(0, 0, cbSize, cbSize); - const auto cbOuterRadius = cbInnerRadius + focusRingWidth; - const auto cbOuterRect = cbInnerRect.adjusted(-focusRingWidth, -focusRingWidth, focusRingWidth, focusRingWidth); - focusRingPath.addRoundedRect(cbOuterRect, cbOuterRadius, cbOuterRadius); - focusRingPath.addRoundedRect(cbInnerRect, cbInnerRadius, cbInnerRadius); - break; - } - case Button_RadioButton: { - const auto rbSize = cw.size == QStyleHelper::SizeLarge ? 15 : - cw.size == QStyleHelper::SizeSmall ? 13 : 9; // As measured - hOffset = hMargin + (cw.size == QStyleHelper::SizeLarge ? 1.5 : - cw.size == QStyleHelper::SizeSmall ? 1.0 : 1.0); // As measured - vOffset = 0.5 * qreal(targetRect.height() - rbSize); - const auto rbInnerRect = QRectF(0, 0, rbSize, rbSize); - const auto rbOuterRect = rbInnerRect.adjusted(-focusRingWidth, -focusRingWidth, focusRingWidth, focusRingWidth); - focusRingPath.addEllipse(rbInnerRect); - focusRingPath.addEllipse(rbOuterRect); - break; - } - case Button_PopupButton: - case Button_PullDown: - case Button_PushButton: - case SegmentedControl_Single: { - const qreal innerRadius = cw.type == Button_PushButton ? 3 : 4; - const qreal outerRadius = innerRadius + focusRingWidth; - hOffset = targetRect.left(); - vOffset = targetRect.top(); - const auto innerRect = targetRect.translated(-targetRect.topLeft()); - const auto outerRect = innerRect.adjusted(-hMargin, -vMargin, hMargin, vMargin); - focusRingPath.addRoundedRect(innerRect, innerRadius, innerRadius); - focusRingPath.addRoundedRect(outerRect, outerRadius, outerRadius); - break; - } - case ComboBox: - case SegmentedControl_First: - case SegmentedControl_Last: { - hOffset = targetRect.left(); - vOffset = targetRect.top(); - const qreal innerRadius = 8; - const qreal outerRadius = innerRadius + focusRingWidth; - const auto innerRect = targetRect.translated(-targetRect.topLeft()); - const auto outerRect = innerRect.adjusted(-hMargin, -vMargin, hMargin, vMargin); - - const auto cbFocusFramePath = [](const QRectF &rect, qreal tRadius, qreal bRadius) { - QPainterPath path; - - if (tRadius > 0) { - const auto topLeftCorner = QRectF(rect.topLeft(), QSizeF(tRadius, tRadius)); - path.arcMoveTo(topLeftCorner, 180); - path.arcTo(topLeftCorner, 180, -90); - } else { - path.moveTo(rect.topLeft()); - } - const auto rightEdge = rect.right() - bRadius; - path.arcTo(rightEdge, rect.top(), bRadius, bRadius, 90, -90); - path.arcTo(rightEdge, rect.bottom() - bRadius, bRadius, bRadius, 0, -90); - if (tRadius > 0) - path.arcTo(rect.left(), rect.bottom() - tRadius, tRadius, tRadius, 270, -90); - else - path.lineTo(rect.bottomLeft()); - path.closeSubpath(); - - return path; - }; - - const auto innerPath = cbFocusFramePath(innerRect, 0, innerRadius); - focusRingPath.addPath(innerPath); - const auto outerPath = cbFocusFramePath(outerRect, 2 * focusRingWidth, outerRadius); - focusRingPath.addPath(outerPath); - break; - } - default: - Q_UNREACHABLE(); - } - - const auto focusRingColor = qt_mac_toQColor(NSColor.keyboardFocusIndicatorColor.CGColor); - - p->save(); - p->setRenderHint(QPainter::Antialiasing); - p->setOpacity(0.5); - if (cw.type == SegmentedControl_First) { - // TODO Flip left-right - } - p->translate(hOffset, vOffset); - p->fillPath(focusRingPath, focusRingColor); - p->restore(); -} - -QPainterPath QMacStylePrivate::windowPanelPath(const QRectF &r) const -{ - static const qreal CornerPointOffset = 5.5; - static const qreal CornerControlOffset = 2.1; - - QPainterPath path; - // Top-left corner - path.moveTo(r.left(), r.top() + CornerPointOffset); - path.cubicTo(r.left(), r.top() + CornerControlOffset, - r.left() + CornerControlOffset, r.top(), - r.left() + CornerPointOffset, r.top()); - // Top-right corner - path.lineTo(r.right() - CornerPointOffset, r.top()); - path.cubicTo(r.right() - CornerControlOffset, r.top(), - r.right(), r.top() + CornerControlOffset, - r.right(), r.top() + CornerPointOffset); - // Bottom-right corner - path.lineTo(r.right(), r.bottom() - CornerPointOffset); - path.cubicTo(r.right(), r.bottom() - CornerControlOffset, - r.right() - CornerControlOffset, r.bottom(), - r.right() - CornerPointOffset, r.bottom()); - // Bottom-right corner - path.lineTo(r.left() + CornerPointOffset, r.bottom()); - path.cubicTo(r.left() + CornerControlOffset, r.bottom(), - r.left(), r.bottom() - CornerControlOffset, - r.left(), r.bottom() - CornerPointOffset); - path.lineTo(r.left(), r.top() + CornerPointOffset); - - return path; -} - -QMacStylePrivate::CocoaControlType QMacStylePrivate::windowButtonCocoaControl(QStyle::SubControl sc) const -{ - struct WindowButtons { - QStyle::SubControl sc; - QMacStylePrivate::CocoaControlType ct; - }; - - static const WindowButtons buttons[] = { - { QStyle::SC_TitleBarCloseButton, QMacStylePrivate::Button_WindowClose }, - { QStyle::SC_TitleBarMinButton, QMacStylePrivate::Button_WindowMiniaturize }, - { QStyle::SC_TitleBarMaxButton, QMacStylePrivate::Button_WindowZoom } - }; - - for (const auto &wb : buttons) - if (wb.sc == sc) - return wb.ct; - - return NoControl; -} - - -#if QT_CONFIG(tabbar) -void QMacStylePrivate::tabLayout(const QStyleOptionTab *opt, const QWidget *widget, QRect *textRect, QRect *iconRect) const -{ - Q_ASSERT(textRect); - Q_ASSERT(iconRect); - QRect tr = opt->rect; - const bool verticalTabs = opt->shape == QTabBar::RoundedEast - || opt->shape == QTabBar::RoundedWest - || opt->shape == QTabBar::TriangularEast - || opt->shape == QTabBar::TriangularWest; - if (verticalTabs) - tr.setRect(0, 0, tr.height(), tr.width()); // 0, 0 as we will have a translate transform - - int verticalShift = proxyStyle->pixelMetric(QStyle::PM_TabBarTabShiftVertical, opt, widget); - int horizontalShift = proxyStyle->pixelMetric(QStyle::PM_TabBarTabShiftHorizontal, opt, widget); - const int hpadding = 4; - const int vpadding = proxyStyle->pixelMetric(QStyle::PM_TabBarTabVSpace, opt, widget) / 2; - if (opt->shape == QTabBar::RoundedSouth || opt->shape == QTabBar::TriangularSouth) - verticalShift = -verticalShift; - tr.adjust(hpadding, verticalShift - vpadding, horizontalShift - hpadding, vpadding); - - // left widget - if (!opt->leftButtonSize.isEmpty()) { - const int buttonSize = verticalTabs ? opt->leftButtonSize.height() : opt->leftButtonSize.width(); - tr.setLeft(tr.left() + 4 + buttonSize); - // make text aligned to center - if (opt->rightButtonSize.isEmpty()) - tr.setRight(tr.right() - 4 - buttonSize); - } - // right widget - if (!opt->rightButtonSize.isEmpty()) { - const int buttonSize = verticalTabs ? opt->rightButtonSize.height() : opt->rightButtonSize.width(); - tr.setRight(tr.right() - 4 - buttonSize); - // make text aligned to center - if (opt->leftButtonSize.isEmpty()) - tr.setLeft(tr.left() + 4 + buttonSize); - } - - // icon - if (!opt->icon.isNull()) { - QSize iconSize = opt->iconSize; - if (!iconSize.isValid()) { - int iconExtent = proxyStyle->pixelMetric(QStyle::PM_SmallIconSize); - iconSize = QSize(iconExtent, iconExtent); - } - QSize tabIconSize = opt->icon.actualSize(iconSize, - (opt->state & QStyle::State_Enabled) ? QIcon::Normal : QIcon::Disabled, - (opt->state & QStyle::State_Selected) ? QIcon::On : QIcon::Off); - // High-dpi icons do not need adjustment; make sure tabIconSize is not larger than iconSize - tabIconSize = QSize(qMin(tabIconSize.width(), iconSize.width()), qMin(tabIconSize.height(), iconSize.height())); - - *iconRect = QRect(tr.left(), tr.center().y() - tabIconSize.height() / 2, - tabIconSize.width(), tabIconSize.height()); - if (!verticalTabs) - *iconRect = proxyStyle->visualRect(opt->direction, opt->rect, *iconRect); - - int stylePadding = proxyStyle->pixelMetric(QStyle::PM_TabBarTabHSpace, opt, widget) / 2; - stylePadding -= hpadding; - - tr.setLeft(tr.left() + stylePadding + tabIconSize.width() + 4); - tr.setRight(tr.right() - stylePadding - tabIconSize.width() - 4); - } - - if (!verticalTabs) - tr = proxyStyle->visualRect(opt->direction, opt->rect, tr); - - *textRect = tr; -} - -QMacStylePrivate::Direction QMacStylePrivate::tabDirection(QTabBar::Shape shape) -{ - switch (shape) { - case QTabBar::RoundedSouth: - case QTabBar::TriangularSouth: - return South; - case QTabBar::RoundedNorth: - case QTabBar::TriangularNorth: - return North; - case QTabBar::RoundedWest: - case QTabBar::TriangularWest: - return West; - case QTabBar::RoundedEast: - case QTabBar::TriangularEast: - return East; - } -} - -bool QMacStylePrivate::verticalTabs(QMacStylePrivate::Direction direction) -{ - return (direction == QMacStylePrivate::East - || direction == QMacStylePrivate::West); -} - -#endif // QT_CONFIG(tabbar) - -QStyleHelper::WidgetSizePolicy QMacStylePrivate::effectiveAquaSizeConstrain(const QStyleOption *option, - const QWidget *widg, - QStyle::ContentsType ct, - QSize szHint, QSize *insz) const -{ - QStyleHelper::WidgetSizePolicy sz = aquaSizeConstrain(option, widg, ct, szHint, insz); - if (sz == QStyleHelper::SizeDefault) - return QStyleHelper::SizeLarge; - return sz; -} - -QStyleHelper::WidgetSizePolicy QMacStylePrivate::aquaSizeConstrain(const QStyleOption *option, const QWidget *widg, - QStyle::ContentsType ct, QSize szHint, QSize *insz) const -{ -#if defined(QMAC_QAQUASTYLE_SIZE_CONSTRAIN) || defined(DEBUG_SIZE_CONSTRAINT) - if (option) { - if (option->state & QStyle::State_Small) - return QStyleHelper::SizeSmall; - if (option->state & QStyle::State_Mini) - return QStyleHelper::SizeMini; - } - - if (!widg) { - if (insz) - *insz = QSize(); - if (qEnvironmentVariableIsSet("QWIDGET_ALL_SMALL")) - return QStyleHelper::SizeSmall; - if (qEnvironmentVariableIsSet("QWIDGET_ALL_MINI")) - return QStyleHelper::SizeMini; - return QStyleHelper::SizeDefault; - } - - QSize large = qt_aqua_get_known_size(ct, widg, szHint, QStyleHelper::SizeLarge), - small = qt_aqua_get_known_size(ct, widg, szHint, QStyleHelper::SizeSmall), - mini = qt_aqua_get_known_size(ct, widg, szHint, QStyleHelper::SizeMini); - bool guess_size = false; - QStyleHelper::WidgetSizePolicy ret = QStyleHelper::SizeDefault; - QStyleHelper::WidgetSizePolicy wsp = QStyleHelper::widgetSizePolicy(widg); - if (wsp == QStyleHelper::SizeDefault) - guess_size = true; - else if (wsp == QStyleHelper::SizeMini) - ret = QStyleHelper::SizeMini; - else if (wsp == QStyleHelper::SizeSmall) - ret = QStyleHelper::SizeSmall; - else if (wsp == QStyleHelper::SizeLarge) - ret = QStyleHelper::SizeLarge; - if (guess_size) - ret = qt_aqua_guess_size(widg, large, small, mini); - - QSize *sz = 0; - if (ret == QStyleHelper::SizeSmall) - sz = &small; - else if (ret == QStyleHelper::SizeLarge) - sz = &large; - else if (ret == QStyleHelper::SizeMini) - sz = &mini; - if (insz) - *insz = sz ? *sz : QSize(-1, -1); -#ifdef DEBUG_SIZE_CONSTRAINT - if (sz) { - const char *size_desc = "Unknown"; - if (sz == &small) - size_desc = "Small"; - else if (sz == &large) - size_desc = "Large"; - else if (sz == &mini) - size_desc = "Mini"; - qDebug("%s - %s: %s taken (%d, %d) [%d, %d]", - widg ? widg->objectName().toLatin1().constData() : "*Unknown*", - widg ? widg->metaObject()->className() : "*Unknown*", size_desc, widg->width(), widg->height(), - sz->width(), sz->height()); - } -#endif - return ret; -#else - if (insz) - *insz = QSize(); - Q_UNUSED(widg); - Q_UNUSED(ct); - Q_UNUSED(szHint); - return QStyleHelper::SizeDefault; -#endif -} - -uint qHash(const QMacStylePrivate::CocoaControl &cw, uint seed = 0) -{ - return ((cw.type << 2) | cw.size) ^ seed; -} - -QMacStylePrivate::CocoaControl::CocoaControl() - : type(NoControl), size(QStyleHelper::SizeDefault) -{ -} - -QMacStylePrivate::CocoaControl::CocoaControl(CocoaControlType t, QStyleHelper::WidgetSizePolicy s) - : type(t), size(s) -{ -} - -bool QMacStylePrivate::CocoaControl::operator==(const CocoaControl &other) const -{ - return other.type == type && other.size == size; -} - -QSizeF QMacStylePrivate::CocoaControl::defaultFrameSize() const -{ - // We need this because things like NSView.alignmentRectInsets - // or -[NSCell titleRectForBounds:] won't work unless the control - // has a reasonable frame set. IOW, it's a chicken and egg problem. - // These values are as observed in Xcode 9's Interface Builder. - - if (type == Button_PushButton) - return QSizeF(-1, pushButtonDefaultHeight[size]); - - if (type == Button_PopupButton - || type == Button_PullDown) - return QSizeF(-1, popupButtonDefaultHeight[size]); - - if (type == ComboBox) - return QSizeF(-1, comboBoxDefaultHeight[size]); - - return QSizeF(); -} - -QRectF QMacStylePrivate::CocoaControl::adjustedControlFrame(const QRectF &rect) const -{ - QRectF frameRect; - const auto frameSize = defaultFrameSize(); - if (type == QMacStylePrivate::Button_SquareButton) { - frameRect = rect.adjusted(3, 1, -3, -1) - .adjusted(focusRingWidth, focusRingWidth, -focusRingWidth, -focusRingWidth); - } else if (type == QMacStylePrivate::Button_PushButton) { - // Start from the style option's top-left corner. - frameRect = QRectF(rect.topLeft(), - QSizeF(rect.width(), frameSize.height())); - if (size == QStyleHelper::SizeSmall) - frameRect = frameRect.translated(0, 1.5); - else if (size == QStyleHelper::SizeMini) - frameRect = frameRect.adjusted(0, 0, -8, 0).translated(4, 4); - } else { - // Center in the style option's rect. - frameRect = QRectF(QPointF(0, (rect.height() - frameSize.height()) / 2.0), - QSizeF(rect.width(), frameSize.height())); - frameRect = frameRect.translated(rect.topLeft()); - if (type == QMacStylePrivate::Button_PullDown || type == QMacStylePrivate::Button_PopupButton) { - if (size == QStyleHelper::SizeLarge) - frameRect = frameRect.adjusted(0, 0, -6, 0).translated(3, 0); - else if (size == QStyleHelper::SizeSmall) - frameRect = frameRect.adjusted(0, 0, -4, 0).translated(2, 1); - else if (size == QStyleHelper::SizeMini) - frameRect = frameRect.adjusted(0, 0, -9, 0).translated(5, 0); - } else if (type == QMacStylePrivate::ComboBox) { - frameRect = frameRect.adjusted(0, 0, -6, 0).translated(4, 0); - } - } - - return frameRect; -} - -QMarginsF QMacStylePrivate::CocoaControl::titleMargins() const -{ - if (type == QMacStylePrivate::Button_PushButton) { - if (size == QStyleHelper::SizeLarge) - return QMarginsF(12, 5, 12, 9); - if (size == QStyleHelper::SizeSmall) - return QMarginsF(12, 4, 12, 9); - if (size == QStyleHelper::SizeMini) - return QMarginsF(10, 1, 10, 2); - } - - if (type == QMacStylePrivate::Button_PullDown) { - if (size == QStyleHelper::SizeLarge) - return QMarginsF(7.5, 2.5, 22.5, 5.5); - if (size == QStyleHelper::SizeSmall) - return QMarginsF(7.5, 2, 20.5, 4); - if (size == QStyleHelper::SizeMini) - return QMarginsF(4.5, 0, 16.5, 2); - } - - if (type == QMacStylePrivate::Button_SquareButton) - return QMarginsF(6, 1, 6, 2); - - return QMarginsF(); -} - -bool QMacStylePrivate::CocoaControl::getCocoaButtonTypeAndBezelStyle(NSButtonType *buttonType, NSBezelStyle *bezelStyle) const -{ - switch (type) { - case Button_CheckBox: - *buttonType = NSSwitchButton; - *bezelStyle = NSRegularSquareBezelStyle; - break; - case Button_Disclosure: - *buttonType = NSOnOffButton; - *bezelStyle = NSDisclosureBezelStyle; - break; - case Button_RadioButton: - *buttonType = NSRadioButton; - *bezelStyle = NSRegularSquareBezelStyle; - break; - case Button_SquareButton: - *buttonType = NSPushOnPushOffButton; - *bezelStyle = NSShadowlessSquareBezelStyle; - break; - case Button_PushButton: - *buttonType = NSPushOnPushOffButton; - *bezelStyle = NSRoundedBezelStyle; - break; - default: - return false; - } - - return true; -} - -QMacStylePrivate::CocoaControlType cocoaControlType(const QStyleOption *opt, const QWidget *w) -{ - if (const auto *btn = qstyleoption_cast(opt)) { - const bool hasMenu = btn->features & QStyleOptionButton::HasMenu; - // When the contents won't fit in a large sized button, - // and WA_MacNormalSize is not set, make the button square. - // Threshold used to be at 34, not 32. - const auto maxNonSquareHeight = pushButtonDefaultHeight[QStyleHelper::SizeLarge]; - const bool isSquare = (btn->features & QStyleOptionButton::Flat) - || (btn->rect.height() > maxNonSquareHeight - && !(w && w->testAttribute(Qt::WA_MacNormalSize))); - return (isSquare? QMacStylePrivate::Button_SquareButton : - hasMenu ? QMacStylePrivate::Button_PullDown : - QMacStylePrivate::Button_PushButton); - } - - if (const auto *combo = qstyleoption_cast(opt)) { - if (combo->editable) - return QMacStylePrivate::ComboBox; - // TODO Me may support square, non-editable combo boxes, but not more than that - return QMacStylePrivate::Button_PopupButton; - } - - return QMacStylePrivate::NoControl; -} - -/** - Carbon draws comboboxes (and other views) outside the rect given as argument. Use this function to obtain - the corresponding inner rect for drawing the same combobox so that it stays inside the given outerBounds. -*/ -CGRect QMacStylePrivate::comboboxInnerBounds(const CGRect &outerBounds, const CocoaControl &cocoaWidget) -{ - CGRect innerBounds = outerBounds; - // Carbon draw parts of the view outside the rect. - // So make the rect a bit smaller to compensate - // (I wish HIThemeGetButtonBackgroundBounds worked) - if (cocoaWidget.type == Button_PopupButton) { - switch (cocoaWidget.size) { - case QStyleHelper::SizeSmall: - innerBounds.origin.x += 3; - innerBounds.origin.y += 3; - innerBounds.size.width -= 6; - innerBounds.size.height -= 7; - break; - case QStyleHelper::SizeMini: - innerBounds.origin.x += 2; - innerBounds.origin.y += 2; - innerBounds.size.width -= 5; - innerBounds.size.height -= 6; - break; - case QStyleHelper::SizeLarge: - case QStyleHelper::SizeDefault: - innerBounds.origin.x += 2; - innerBounds.origin.y += 2; - innerBounds.size.width -= 5; - innerBounds.size.height -= 6; - } - } else if (cocoaWidget.type == ComboBox) { - switch (cocoaWidget.size) { - case QStyleHelper::SizeSmall: - innerBounds.origin.x += 3; - innerBounds.origin.y += 3; - innerBounds.size.width -= 7; - innerBounds.size.height -= 8; - break; - case QStyleHelper::SizeMini: - innerBounds.origin.x += 3; - innerBounds.origin.y += 3; - innerBounds.size.width -= 4; - innerBounds.size.height -= 8; - break; - case QStyleHelper::SizeLarge: - case QStyleHelper::SizeDefault: - innerBounds.origin.x += 3; - innerBounds.origin.y += 2; - innerBounds.size.width -= 6; - innerBounds.size.height -= 8; - } - } - - return innerBounds; -} - -/** - Inside a combobox Qt places a line edit widget. The size of this widget should depend on the kind - of combobox we choose to draw. This function calculates and returns this size. -*/ -QRectF QMacStylePrivate::comboboxEditBounds(const QRectF &outerBounds, const CocoaControl &cw) -{ - QRectF ret = outerBounds; - if (cw.type == ComboBox) { - switch (cw.size) { - case QStyleHelper::SizeLarge: - ret = ret.adjusted(0, 0, -28, 0).translated(3, 4.5); - ret.setHeight(16); - break; - case QStyleHelper::SizeSmall: - ret = ret.adjusted(0, 0, -24, 0).translated(3, 2); - ret.setHeight(14); - break; - case QStyleHelper::SizeMini: - ret = ret.adjusted(0, 0, -21, 0).translated(2, 3); - ret.setHeight(11); - break; - default: - break; - } - } else if (cw.type == Button_PopupButton) { - switch (cw.size) { - case QStyleHelper::SizeLarge: - ret.adjust(10, 1, -23, -4); - break; - case QStyleHelper::SizeSmall: - ret.adjust(10, 4, -20, -3); - break; - case QStyleHelper::SizeMini: - ret.adjust(9, 0, -19, 0); - ret.setHeight(13); - break; - default: - break; - } - } - return ret; -} - -QMacStylePrivate::QMacStylePrivate() - : backingStoreNSView(nil) -{ - if (auto *ssf = QGuiApplicationPrivate::platformTheme()->font(QPlatformTheme::SmallFont)) - smallSystemFont = *ssf; - if (auto *msf = QGuiApplicationPrivate::platformTheme()->font(QPlatformTheme::MiniFont)) - miniSystemFont = *msf; -} - -QMacStylePrivate::~QMacStylePrivate() -{ - QMacAutoReleasePool pool; - for (NSView *b : cocoaControls) - [b release]; - for (NSCell *cell : cocoaCells) - [cell release]; -} - -NSView *QMacStylePrivate::cocoaControl(CocoaControl widget) const -{ - if (widget.type == QMacStylePrivate::NoControl - || widget.size == QStyleHelper::SizeDefault) - return nil; - - if (widget.type == Box) { - if (__builtin_available(macOS 10.14, *)) { - if (qt_mac_applicationIsInDarkMode()) { - // See render code in drawPrimitive(PE_FrameTabWidget) - widget.type = Box_Dark; - } - } - } - - NSView *bv = cocoaControls.value(widget, nil); - if (!bv) { - switch (widget.type) { - case Box: { - NSBox *box = [[NSBox alloc] init]; - bv = box; - box.title = @""; - box.titlePosition = NSNoTitle; - break; - } - case Box_Dark: - bv = [[QDarkNSBox alloc] init]; - break; - case Button_CheckBox: - case Button_Disclosure: - case Button_PushButton: - case Button_RadioButton: - case Button_SquareButton: { - NSButton *bc = [[NSButton alloc] init]; - bc.title = @""; - // See below for style and bezel setting. - bv = bc; - break; - } - case Button_PopupButton: - case Button_PullDown: { - NSPopUpButton *bc = [[NSPopUpButton alloc] init]; - bc.title = @""; - if (widget.type == Button_PullDown) - bc.pullsDown = YES; - bv = bc; - break; - } - case Button_WindowClose: - case Button_WindowMiniaturize: - case Button_WindowZoom: { - const NSWindowButton button = [=] { - switch (widget.type) { - case Button_WindowClose: - return NSWindowCloseButton; - case Button_WindowMiniaturize: - return NSWindowMiniaturizeButton; - case Button_WindowZoom: - return NSWindowZoomButton; - default: - break; - } - Q_UNREACHABLE(); - } (); - const auto styleMask = NSWindowStyleMaskTitled - | NSWindowStyleMaskClosable - | NSWindowStyleMaskMiniaturizable - | NSWindowStyleMaskResizable; - bv = [NSWindow standardWindowButton:button forStyleMask:styleMask]; - [bv retain]; - break; - } - case ComboBox: - bv = [[NSComboBox alloc] init]; - break; - case ProgressIndicator_Determinate: - bv = [[NSProgressIndicator alloc] init]; - break; - case ProgressIndicator_Indeterminate: - bv = [[QIndeterminateProgressIndicator alloc] init]; - break; - case Scroller_Horizontal: - bv = [[NSScroller alloc] initWithFrame:NSMakeRect(0, 0, 200, 20)]; - break; - case Scroller_Vertical: - // Cocoa sets the orientation from the view's frame - // at construction time, and it cannot be changed later. - bv = [[NSScroller alloc] initWithFrame:NSMakeRect(0, 0, 20, 200)]; - break; - case Slider_Horizontal: - bv = [[NSSlider alloc] initWithFrame:NSMakeRect(0, 0, 200, 20)]; - break; - case Slider_Vertical: - // Cocoa sets the orientation from the view's frame - // at construction time, and it cannot be changed later. - bv = [[NSSlider alloc] initWithFrame:NSMakeRect(0, 0, 20, 200)]; - break; - case SplitView_Horizontal: - bv = [[NSSplitView alloc] init]; - break; - case SplitView_Vertical: - bv = [[QVerticalSplitView alloc] init]; - break; - case TextField: - bv = [[NSTextField alloc] init]; - break; - default: - break; - } - - if ([bv isKindOfClass:[NSControl class]]) { - auto *ctrl = static_cast(bv); - switch (widget.size) { - case QStyleHelper::SizeSmall: - ctrl.controlSize = NSControlSizeSmall; - break; - case QStyleHelper::SizeMini: - ctrl.controlSize = NSControlSizeMini; - break; - default: - break; - } - } else if (widget.type == ProgressIndicator_Determinate || - widget.type == ProgressIndicator_Indeterminate) { - auto *pi = static_cast(bv); - pi.indeterminate = (widget.type == ProgressIndicator_Indeterminate); - switch (widget.size) { - case QStyleHelper::SizeSmall: - pi.controlSize = NSControlSizeSmall; - break; - case QStyleHelper::SizeMini: - pi.controlSize = NSControlSizeMini; - break; - default: - break; - } - } - - cocoaControls.insert(widget, bv); - } - - NSButtonType buttonType; - NSBezelStyle bezelStyle; - if (widget.getCocoaButtonTypeAndBezelStyle(&buttonType, &bezelStyle)) { - // FIXME We need to reset the button's type and - // bezel style properties, even when cached. - auto *button = static_cast(bv); - button.buttonType = buttonType; - button.bezelStyle = bezelStyle; - if (widget.type == Button_CheckBox) - button.allowsMixedState = YES; - } - - return bv; -} - -NSCell *QMacStylePrivate::cocoaCell(CocoaControl widget) const -{ - NSCell *cell = cocoaCells[widget]; - if (!cell) { - switch (widget.type) { - case Stepper: - cell = [[NSStepperCell alloc] init]; - break; - case Button_Disclosure: { - NSButtonCell *bc = [[NSButtonCell alloc] init]; - bc.buttonType = NSOnOffButton; - bc.bezelStyle = NSDisclosureBezelStyle; - cell = bc; - break; - } - default: - break; - } - - switch (widget.size) { - case QStyleHelper::SizeSmall: - cell.controlSize = NSControlSizeSmall; - break; - case QStyleHelper::SizeMini: - cell.controlSize = NSControlSizeMini; - break; - default: - break; - } - - cocoaCells.insert(widget, cell); - } - - return cell; -} - -void QMacStylePrivate::drawNSViewInRect(NSView *view, const QRectF &rect, QPainter *p, - __attribute__((noescape)) DrawRectBlock drawRectBlock) const -{ - QMacCGContext ctx(p); - setupNSGraphicsContext(ctx, YES); - - // FIXME: The rect that we get in is relative to the widget that we're drawing - // style on behalf of, and doesn't take into account the offset of that widget - // to the widget that owns the backingstore, which we are placing the native - // view into below. This means most of the views are placed in the upper left - // corner of backingStoreNSView, which does not map to where the actual widget - // is, and which may cause problems such as triggering a setNeedsDisplay of the - // backingStoreNSView for the wrong rect. We work around this by making the view - // layer-backed, which prevents triggering display of the backingStoreNSView, but - // but there may be other issues lurking here due to the wrong position. QTBUG-68023 - view.wantsLayer = YES; - - // FIXME: We are also setting the frame of the incoming view a lot at the call - // sites of this function, making it unclear who's actually responsible for - // maintaining the size and position of the view. In theory the call sites - // should ensure the _size_ of the view is correct, and then let this code - // take care of _positioning_ the view at the right place inside backingStoreNSView. - // For now we pass on the rect as is, to prevent any regressions until this - // can be investigated properly. - view.frame = rect.toCGRect(); - - [backingStoreNSView addSubview:view]; - - // FIXME: Based on the code below, this method isn't drawing an NSView into - // a rect, it's drawing _part of the NSView_, defined by the incoming clip - // or dirty rect, into the current graphics context. We're doing some manual - // translations at the call sites that would indicate that this relationship - // is a bit fuzzy. - const CGRect dirtyRect = rect.toCGRect(); - - if (drawRectBlock) - drawRectBlock(ctx, dirtyRect); - else - [view drawRect:dirtyRect]; - - [view removeFromSuperviewWithoutNeedingDisplay]; - - restoreNSGraphicsContext(ctx); -} - -void QMacStylePrivate::resolveCurrentNSView(QWindow *window) const -{ - backingStoreNSView = window ? (NSView *)window->winId() : nil; -} - -QMacStyle::QMacStyle() - : QCommonStyle(*new QMacStylePrivate) -{ - Q_D(QMacStyle); - QMacAutoReleasePool pool; - - d->receiver = [[NotificationReceiver alloc] init]; - [[NSNotificationCenter defaultCenter] addObserver:d->receiver - selector:@selector(scrollBarStyleDidChange:) - name:NSPreferredScrollerStyleDidChangeNotification - object:nil]; -} - -QMacStyle::~QMacStyle() -{ - Q_D(QMacStyle); - QMacAutoReleasePool pool; - - [[NSNotificationCenter defaultCenter] removeObserver:d->receiver]; - [d->receiver release]; -} - -void QMacStyle::polish(QPalette &) -{ -} - -void QMacStyle::polish(QApplication *) -{ -} - -void QMacStyle::unpolish(QApplication *) -{ -} - -void QMacStyle::polish(QWidget* w) -{ - if (false -#if QT_CONFIG(menu) - || qobject_cast(w) -# if QT_CONFIG(combobox) - || qobject_cast(w) -# endif -#endif -#if QT_CONFIG(mdiarea) - || qobject_cast(w) -#endif - ) { - w->setAttribute(Qt::WA_TranslucentBackground, true); - w->setAutoFillBackground(false); - } - -#if QT_CONFIG(tabbar) - if (QTabBar *tb = qobject_cast(w)) { - if (tb->documentMode()) { - w->setAttribute(Qt::WA_Hover); - w->setFont(qt_app_fonts_hash()->value("QSmallFont", QFont())); - QPalette p = w->palette(); - p.setColor(QPalette::WindowText, QColor(17, 17, 17)); - w->setPalette(p); - w->setAttribute(Qt::WA_SetPalette, false); - w->setAttribute(Qt::WA_SetFont, false); - } - } -#endif - - QCommonStyle::polish(w); - - if (QRubberBand *rubber = qobject_cast(w)) { - rubber->setWindowOpacity(0.25); - rubber->setAttribute(Qt::WA_PaintOnScreen, false); - rubber->setAttribute(Qt::WA_NoSystemBackground, false); - } - - if (qobject_cast(w)) { - w->setAttribute(Qt::WA_OpaquePaintEvent, false); - w->setAttribute(Qt::WA_Hover, true); - w->setMouseTracking(true); - } -} - -void QMacStyle::unpolish(QWidget* w) -{ - if ( -#if QT_CONFIG(menu) - qobject_cast(w) && -#endif - !w->testAttribute(Qt::WA_SetPalette)) { - QPalette pal = qApp->palette(w); - w->setPalette(pal); - w->setAttribute(Qt::WA_SetPalette, false); - w->setWindowOpacity(1.0); - } - -#if QT_CONFIG(combobox) - if (QComboBox *combo = qobject_cast(w)) { - if (!combo->isEditable()) { - if (QWidget *widget = combo->findChild()) - widget->setWindowOpacity(1.0); - } - } -#endif - -#if QT_CONFIG(tabbar) - if (qobject_cast(w)) { - if (!w->testAttribute(Qt::WA_SetFont)) - w->setFont(qApp->font(w)); - if (!w->testAttribute(Qt::WA_SetPalette)) - w->setPalette(qApp->palette(w)); - } -#endif - - if (QRubberBand *rubber = qobject_cast(w)) { - rubber->setWindowOpacity(1.0); - rubber->setAttribute(Qt::WA_PaintOnScreen, true); - rubber->setAttribute(Qt::WA_NoSystemBackground, true); - } - - if (QFocusFrame *frame = qobject_cast(w)) - frame->setAttribute(Qt::WA_NoSystemBackground, true); - - QCommonStyle::unpolish(w); - - if (qobject_cast(w)) { - w->setAttribute(Qt::WA_OpaquePaintEvent, true); - w->setAttribute(Qt::WA_Hover, false); - w->setMouseTracking(false); - } -} - -int QMacStyle::pixelMetric(PixelMetric metric, const QStyleOption *opt, const QWidget *widget) const -{ - Q_D(const QMacStyle); - const int controlSize = getControlSize(opt, widget); - int ret = 0; - - switch (metric) { - case PM_TabCloseIndicatorWidth: - case PM_TabCloseIndicatorHeight: - ret = closeButtonSize; - break; - case PM_ToolBarIconSize: - ret = proxy()->pixelMetric(PM_LargeIconSize); - break; - case PM_FocusFrameVMargin: - case PM_FocusFrameHMargin: - ret = qt_mac_aqua_get_metric(FocusRectOutset); - break; - case PM_DialogButtonsSeparator: - ret = -5; - break; - case PM_DialogButtonsButtonHeight: { - QSize sz; - ret = d->aquaSizeConstrain(opt, 0, QStyle::CT_PushButton, QSize(-1, -1), &sz); - if (sz == QSize(-1, -1)) - ret = 32; - else - ret = sz.height(); - break; } - case PM_DialogButtonsButtonWidth: { - QSize sz; - ret = d->aquaSizeConstrain(opt, 0, QStyle::CT_PushButton, QSize(-1, -1), &sz); - if (sz == QSize(-1, -1)) - ret = 70; - else - ret = sz.width(); - break; } - - case PM_MenuBarHMargin: - ret = 8; - break; - - case PM_MenuBarVMargin: - ret = 0; - break; - - case PM_MenuBarPanelWidth: - ret = 0; - break; - - case PM_MenuButtonIndicator: - ret = toolButtonArrowSize; - break; - - case QStyle::PM_MenuDesktopFrameWidth: - ret = 5; - break; - - case PM_CheckBoxLabelSpacing: - case PM_RadioButtonLabelSpacing: - ret = [=] { - if (opt) { - if (opt->state & State_Mini) - return 4; - if (opt->state & State_Small) - return 3; - } - return 2; - } (); - break; - case PM_MenuScrollerHeight: - ret = 15; // I hate having magic numbers in here... - break; - case PM_DefaultFrameWidth: -#if QT_CONFIG(mainwindow) - if (widget && (widget->isWindow() || !widget->parentWidget() - || (qobject_cast(widget->parentWidget()) - && static_cast(widget->parentWidget())->centralWidget() == widget)) - && qobject_cast(widget)) - ret = 0; - else -#endif - // The combo box popup has no frame. - if (qstyleoption_cast(opt) != 0) - ret = 0; - else - ret = 1; - break; - case PM_MaximumDragDistance: - ret = -1; - break; - case PM_ScrollBarSliderMin: - ret = 24; - break; - case PM_SpinBoxFrameWidth: - ret = qt_mac_aqua_get_metric(EditTextFrameOutset); - break; - case PM_ButtonShiftHorizontal: - case PM_ButtonShiftVertical: - ret = 0; - break; - case PM_SliderLength: - ret = 17; - break; - // Returns the number of pixels to use for the business part of the - // slider (i.e., the non-tickmark portion). The remaining space is shared - // equally between the tickmark regions. - case PM_SliderControlThickness: - if (const QStyleOptionSlider *sl = qstyleoption_cast(opt)) { - int space = (sl->orientation == Qt::Horizontal) ? sl->rect.height() : sl->rect.width(); - int ticks = sl->tickPosition; - int n = 0; - if (ticks & QSlider::TicksAbove) - ++n; - if (ticks & QSlider::TicksBelow) - ++n; - if (!n) { - ret = space; - break; - } - - int thick = 6; // Magic constant to get 5 + 16 + 5 - if (ticks != QSlider::TicksBothSides && ticks != QSlider::NoTicks) - thick += proxy()->pixelMetric(PM_SliderLength, sl, widget) / 4; - - space -= thick; - if (space > 0) - thick += (space * 2) / (n + 2); - ret = thick; - } else { - ret = 0; - } - break; - case PM_SmallIconSize: - ret = int(QStyleHelper::dpiScaled(16.)); - break; - - case PM_LargeIconSize: - ret = int(QStyleHelper::dpiScaled(32.)); - break; - - case PM_IconViewIconSize: - ret = proxy()->pixelMetric(PM_LargeIconSize, opt, widget); - break; - - case PM_ButtonDefaultIndicator: - ret = 0; - break; - case PM_TitleBarHeight: { - NSUInteger style = NSWindowStyleMaskTitled; - if (widget && ((widget->windowFlags() & Qt::Tool) == Qt::Tool)) - style |= NSWindowStyleMaskUtilityWindow; - ret = int([NSWindow frameRectForContentRect:NSZeroRect - styleMask:style].size.height); - break; } - case QStyle::PM_TabBarTabHSpace: - switch (d->aquaSizeConstrain(opt, widget)) { - case QStyleHelper::SizeLarge: - ret = QCommonStyle::pixelMetric(metric, opt, widget); - break; - case QStyleHelper::SizeSmall: - ret = 20; - break; - case QStyleHelper::SizeMini: - ret = 16; - break; - case QStyleHelper::SizeDefault: - const QStyleOptionTab *tb = qstyleoption_cast(opt); - if (tb && tb->documentMode) - ret = 30; - else - ret = QCommonStyle::pixelMetric(metric, opt, widget); - break; - } - break; - case PM_TabBarTabVSpace: - ret = 4; - break; - case PM_TabBarTabShiftHorizontal: - case PM_TabBarTabShiftVertical: - ret = 0; - break; - case PM_TabBarBaseHeight: - ret = 0; - break; - case PM_TabBarTabOverlap: - ret = 1; - break; - case PM_TabBarBaseOverlap: - switch (d->aquaSizeConstrain(opt, widget)) { - case QStyleHelper::SizeDefault: - case QStyleHelper::SizeLarge: - ret = 11; - break; - case QStyleHelper::SizeSmall: - ret = 8; - break; - case QStyleHelper::SizeMini: - ret = 7; - break; - } - break; - case PM_ScrollBarExtent: { - const QStyleHelper::WidgetSizePolicy size = d->effectiveAquaSizeConstrain(opt, widget); - ret = static_cast([NSScroller - scrollerWidthForControlSize:static_cast(size) - scrollerStyle:[NSScroller preferredScrollerStyle]]); - break; } - case PM_IndicatorHeight: { - switch (d->aquaSizeConstrain(opt, widget)) { - case QStyleHelper::SizeDefault: - case QStyleHelper::SizeLarge: - ret = qt_mac_aqua_get_metric(CheckBoxHeight); - break; - case QStyleHelper::SizeMini: - ret = qt_mac_aqua_get_metric(MiniCheckBoxHeight); - break; - case QStyleHelper::SizeSmall: - ret = qt_mac_aqua_get_metric(SmallCheckBoxHeight); - break; - } - break; } - case PM_IndicatorWidth: { - switch (d->aquaSizeConstrain(opt, widget)) { - case QStyleHelper::SizeDefault: - case QStyleHelper::SizeLarge: - ret = qt_mac_aqua_get_metric(CheckBoxWidth); - break; - case QStyleHelper::SizeMini: - ret = qt_mac_aqua_get_metric(MiniCheckBoxWidth); - break; - case QStyleHelper::SizeSmall: - ret = qt_mac_aqua_get_metric(SmallCheckBoxWidth); - break; - } - ++ret; - break; } - case PM_ExclusiveIndicatorHeight: { - switch (d->aquaSizeConstrain(opt, widget)) { - case QStyleHelper::SizeDefault: - case QStyleHelper::SizeLarge: - ret = qt_mac_aqua_get_metric(RadioButtonHeight); - break; - case QStyleHelper::SizeMini: - ret = qt_mac_aqua_get_metric(MiniRadioButtonHeight); - break; - case QStyleHelper::SizeSmall: - ret = qt_mac_aqua_get_metric(SmallRadioButtonHeight); - break; - } - break; } - case PM_ExclusiveIndicatorWidth: { - switch (d->aquaSizeConstrain(opt, widget)) { - case QStyleHelper::SizeDefault: - case QStyleHelper::SizeLarge: - ret = qt_mac_aqua_get_metric(RadioButtonWidth); - break; - case QStyleHelper::SizeMini: - ret = qt_mac_aqua_get_metric(MiniRadioButtonWidth); - break; - case QStyleHelper::SizeSmall: - ret = qt_mac_aqua_get_metric(SmallRadioButtonWidth); - break; - } - ++ret; - break; } - case PM_MenuVMargin: - ret = 4; - break; - case PM_MenuPanelWidth: - ret = 0; - break; - case PM_ToolTipLabelFrameWidth: - ret = 0; - break; - case PM_SizeGripSize: { - QStyleHelper::WidgetSizePolicy aSize; - if (widget && widget->window()->windowType() == Qt::Tool) - aSize = QStyleHelper::SizeSmall; - else - aSize = QStyleHelper::SizeLarge; - const QSize size = qt_aqua_get_known_size(CT_SizeGrip, widget, QSize(), aSize); - ret = size.width(); - break; } - case PM_MdiSubWindowFrameWidth: - ret = 1; - break; - case PM_DockWidgetFrameWidth: - ret = 0; - break; - case PM_DockWidgetTitleMargin: - ret = 0; - break; - case PM_DockWidgetSeparatorExtent: - ret = 1; - break; - case PM_ToolBarHandleExtent: - ret = 11; - break; - case PM_ToolBarItemMargin: - ret = 0; - break; - case PM_ToolBarItemSpacing: - ret = 4; - break; - case PM_SplitterWidth: - ret = qMax(7, QApplication::globalStrut().width()); - break; - case PM_LayoutLeftMargin: - case PM_LayoutTopMargin: - case PM_LayoutRightMargin: - case PM_LayoutBottomMargin: - { - bool isWindow = false; - if (opt) { - isWindow = (opt->state & State_Window); - } else if (widget) { - isWindow = widget->isWindow(); - } - - if (isWindow) { - /* - AHIG would have (20, 8, 10) here but that makes - no sense. It would also have 14 for the top margin - but this contradicts both Builder and most - applications. - */ - return_SIZE(20, 10, 10); // AHIG - } else { - // hack to detect QTabWidget - if (widget && widget->parentWidget() - && widget->parentWidget()->sizePolicy().controlType() == QSizePolicy::TabWidget) { - if (metric == PM_LayoutTopMargin) { - /* - Builder would have 14 (= 20 - 6) instead of 12, - but that makes the tab look disproportionate. - */ - return_SIZE(12, 6, 6); // guess - } else { - return_SIZE(20 /* Builder */, 8 /* guess */, 8 /* guess */); - } - } else { - /* - Child margins are highly inconsistent in AHIG and Builder. - */ - return_SIZE(12, 8, 6); // guess - } - } - } - case PM_LayoutHorizontalSpacing: - case PM_LayoutVerticalSpacing: - return -1; - case PM_MenuHMargin: - ret = 0; - break; - case PM_ToolBarExtensionExtent: - ret = 21; - break; - case PM_ToolBarFrameWidth: - ret = 1; - break; - case PM_ScrollView_ScrollBarOverlap: - ret = [NSScroller preferredScrollerStyle] == NSScrollerStyleOverlay ? - pixelMetric(PM_ScrollBarExtent, opt, widget) : 0; - break; - default: - ret = QCommonStyle::pixelMetric(metric, opt, widget); - break; - } - return ret; -} - -QPalette QMacStyle::standardPalette() const -{ - QPalette pal = QCommonStyle::standardPalette(); - pal.setColor(QPalette::Disabled, QPalette::Dark, QColor(191, 191, 191)); - pal.setColor(QPalette::Active, QPalette::Dark, QColor(191, 191, 191)); - pal.setColor(QPalette::Inactive, QPalette::Dark, QColor(191, 191, 191)); - return pal; -} - -int QMacStyle::styleHint(StyleHint sh, const QStyleOption *opt, const QWidget *w, - QStyleHintReturn *hret) const -{ - QMacAutoReleasePool pool; - - int ret = 0; - switch (sh) { - case SH_Slider_SnapToValue: - case SH_PrintDialog_RightAlignButtons: - case SH_FontDialog_SelectAssociatedText: - case SH_MenuBar_MouseTracking: - case SH_Menu_MouseTracking: - case SH_ComboBox_ListMouseTracking: - case SH_MainWindow_SpaceBelowMenuBar: - case SH_ItemView_ChangeHighlightOnFocus: - ret = 1; - break; - case SH_ToolBox_SelectedPageTitleBold: - ret = 0; - break; - case SH_DialogButtonBox_ButtonsHaveIcons: - ret = 0; - break; - case SH_Menu_SelectionWrap: - ret = false; - break; - case SH_Menu_KeyboardSearch: - ret = true; - break; - case SH_Menu_SpaceActivatesItem: - ret = true; - break; - case SH_Slider_AbsoluteSetButtons: - ret = Qt::LeftButton|Qt::MidButton; - break; - case SH_Slider_PageSetButtons: - ret = 0; - break; - case SH_ScrollBar_ContextMenu: - ret = false; - break; - case SH_TitleBar_AutoRaise: - ret = true; - break; - case SH_Menu_AllowActiveAndDisabled: - ret = false; - break; - case SH_Menu_SubMenuPopupDelay: - ret = 100; - break; - case SH_Menu_SubMenuUniDirection: - ret = true; - break; - case SH_Menu_SubMenuSloppySelectOtherActions: - ret = false; - break; - case SH_Menu_SubMenuResetWhenReenteringParent: - ret = true; - break; - case SH_Menu_SubMenuDontStartSloppyOnLeave: - ret = true; - break; - - case SH_ScrollBar_LeftClickAbsolutePosition: { - NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults]; - bool result = [defaults boolForKey:@"AppleScrollerPagingBehavior"]; - if(QApplication::keyboardModifiers() & Qt::AltModifier) - ret = !result; - else - ret = result; - break; } - case SH_TabBar_PreferNoArrows: - ret = true; - break; - /* - case SH_DialogButtons_DefaultButton: - ret = QDialogButtons::Reject; - break; - */ - case SH_GroupBox_TextLabelVerticalAlignment: - ret = Qt::AlignTop; - break; - case SH_ScrollView_FrameOnlyAroundContents: - ret = QCommonStyle::styleHint(sh, opt, w, hret); - break; - case SH_Menu_FillScreenWithScroll: - ret = false; - break; - case SH_Menu_Scrollable: - ret = true; - break; - case SH_RichText_FullWidthSelection: - ret = true; - break; - case SH_BlinkCursorWhenTextSelected: - ret = false; - break; - case SH_ScrollBar_StopMouseOverSlider: - ret = true; - break; - case SH_ListViewExpand_SelectMouseType: - ret = QEvent::MouseButtonRelease; - break; - case SH_TabBar_SelectMouseType: -#if QT_CONFIG(tabbar) - if (const QStyleOptionTabBarBase *opt2 = qstyleoption_cast(opt)) { - ret = opt2->documentMode ? QEvent::MouseButtonPress : QEvent::MouseButtonRelease; - } else -#endif - { - ret = QEvent::MouseButtonRelease; - } - break; - case SH_ComboBox_Popup: - if (const QStyleOptionComboBox *cmb = qstyleoption_cast(opt)) - ret = !cmb->editable; - else - ret = 0; - break; - case SH_Workspace_FillSpaceOnMaximize: - ret = true; - break; - case SH_Widget_ShareActivation: - ret = true; - break; - case SH_Header_ArrowAlignment: - ret = Qt::AlignRight; - break; - case SH_TabBar_Alignment: { -#if QT_CONFIG(tabwidget) - if (const QTabWidget *tab = qobject_cast(w)) { - if (tab->documentMode()) { - ret = Qt::AlignLeft; - break; - } - } -#endif -#if QT_CONFIG(tabbar) - if (const QTabBar *tab = qobject_cast(w)) { - if (tab->documentMode()) { - ret = Qt::AlignLeft; - break; - } - } -#endif - ret = Qt::AlignCenter; - } break; - case SH_UnderlineShortcut: - ret = false; - break; - case SH_ToolTipLabel_Opacity: - ret = 242; // About 95% - break; - case SH_Button_FocusPolicy: - ret = Qt::TabFocus; - break; - case SH_EtchDisabledText: - ret = false; - break; - case SH_FocusFrame_Mask: { - ret = true; - if(QStyleHintReturnMask *mask = qstyleoption_cast(hret)) { - const uchar fillR = 192, fillG = 191, fillB = 190; - QImage img; - - QSize pixmapSize = opt->rect.size(); - if (!pixmapSize.isEmpty()) { - QPixmap pix(pixmapSize); - pix.fill(QColor(fillR, fillG, fillB)); - QPainter pix_paint(&pix); - proxy()->drawControl(CE_FocusFrame, opt, &pix_paint, w); - pix_paint.end(); - img = pix.toImage(); - } - - const QRgb *sptr = (QRgb*)img.bits(), *srow; - const int sbpl = img.bytesPerLine(); - const int w = sbpl/4, h = img.height(); - - QImage img_mask(img.width(), img.height(), QImage::Format_ARGB32); - QRgb *dptr = (QRgb*)img_mask.bits(), *drow; - const int dbpl = img_mask.bytesPerLine(); - - for (int y = 0; y < h; ++y) { - srow = sptr+((y*sbpl)/4); - drow = dptr+((y*dbpl)/4); - for (int x = 0; x < w; ++x) { - const int redDiff = qRed(*srow) - fillR; - const int greenDiff = qGreen(*srow) - fillG; - const int blueDiff = qBlue(*srow) - fillB; - const int diff = (redDiff * redDiff) + (greenDiff * greenDiff) + (blueDiff * blueDiff); - (*drow++) = (diff < 10) ? 0xffffffff : 0xff000000; - ++srow; - } - } - QBitmap qmask = QBitmap::fromImage(img_mask); - mask->region = QRegion(qmask); - } - break; } - case SH_TitleBar_NoBorder: - ret = 1; - break; - case SH_RubberBand_Mask: - ret = 0; - break; - case SH_ComboBox_LayoutDirection: - ret = Qt::LeftToRight; - break; - case SH_ItemView_EllipsisLocation: - ret = Qt::AlignHCenter; - break; - case SH_ItemView_ShowDecorationSelected: - ret = true; - break; - case SH_TitleBar_ModifyNotification: - ret = false; - break; - case SH_ScrollBar_RollBetweenButtons: - ret = true; - break; - case SH_WindowFrame_Mask: - ret = false; - break; - case SH_TabBar_ElideMode: - ret = Qt::ElideRight; - break; -#if QT_CONFIG(dialogbuttonbox) - case SH_DialogButtonLayout: - ret = QDialogButtonBox::MacLayout; - break; -#endif - case SH_FormLayoutWrapPolicy: - ret = QFormLayout::DontWrapRows; - break; - case SH_FormLayoutFieldGrowthPolicy: - ret = QFormLayout::FieldsStayAtSizeHint; - break; - case SH_FormLayoutFormAlignment: - ret = Qt::AlignHCenter | Qt::AlignTop; - break; - case SH_FormLayoutLabelAlignment: - ret = Qt::AlignRight; - break; - case SH_ComboBox_PopupFrameStyle: - ret = QFrame::NoFrame; - break; - case SH_MessageBox_TextInteractionFlags: - ret = Qt::TextSelectableByMouse | Qt::LinksAccessibleByMouse | Qt::TextSelectableByKeyboard; - break; - case SH_SpellCheckUnderlineStyle: - ret = QTextCharFormat::DashUnderline; - break; - case SH_MessageBox_CenterButtons: - ret = false; - break; - case SH_MenuBar_AltKeyNavigation: - ret = false; - break; - case SH_ItemView_MovementWithoutUpdatingSelection: - ret = false; - break; - case SH_FocusFrame_AboveWidget: - ret = true; - break; -#if QT_CONFIG(wizard) - case SH_WizardStyle: - ret = QWizard::MacStyle; - break; -#endif - case SH_ItemView_ArrowKeysNavigateIntoChildren: - ret = false; - break; - case SH_Menu_FlashTriggeredItem: - ret = true; - break; - case SH_Menu_FadeOutOnHide: - ret = true; - break; - case SH_ItemView_PaintAlternatingRowColorsForEmptyArea: - ret = true; - break; -#if QT_CONFIG(tabbar) - case SH_TabBar_CloseButtonPosition: - ret = QTabBar::LeftSide; - break; -#endif - case SH_DockWidget_ButtonsHaveFrame: - ret = false; - break; - case SH_ScrollBar_Transient: - if ((qobject_cast(w) && w->parent() && - qobject_cast(w->parent()->parent())) -#ifndef QT_NO_ACCESSIBILITY - || (opt && QStyleHelper::hasAncestor(opt->styleObject, QAccessible::ScrollBar)) -#endif - ) { - ret = [NSScroller preferredScrollerStyle] == NSScrollerStyleOverlay; - } - break; - case SH_ItemView_ScrollMode: - ret = QAbstractItemView::ScrollPerPixel; - break; - case SH_TitleBar_ShowToolTipsOnButtons: - // min/max/close buttons on windows don't show tool tips - ret = false; - break; - case SH_ComboBox_AllowWheelScrolling: - ret = false; - break; - case SH_SpinBox_ButtonsInsideFrame: - ret = false; - break; - case SH_Table_GridLineColor: - ret = int(qt_mac_toQColor(NSColor.gridColor).rgb()); - break; - default: - ret = QCommonStyle::styleHint(sh, opt, w, hret); - break; - } - return ret; -} - -QPixmap QMacStyle::generatedIconPixmap(QIcon::Mode iconMode, const QPixmap &pixmap, - const QStyleOption *opt) const -{ - switch (iconMode) { - case QIcon::Disabled: { - QImage img = pixmap.toImage().convertToFormat(QImage::Format_ARGB32); - int imgh = img.height(); - int imgw = img.width(); - QRgb pixel; - for (int y = 0; y < imgh; ++y) { - for (int x = 0; x < imgw; ++x) { - pixel = img.pixel(x, y); - img.setPixel(x, y, qRgba(qRed(pixel), qGreen(pixel), qBlue(pixel), - qAlpha(pixel) / 2)); - } - } - return QPixmap::fromImage(img); - } - default: - ; - } - return QCommonStyle::generatedIconPixmap(iconMode, pixmap, opt); -} - - -QPixmap QMacStyle::standardPixmap(StandardPixmap standardPixmap, const QStyleOption *opt, - const QWidget *widget) const -{ - // The default implementation of QStyle::standardIconImplementation() is to call standardPixmap() - // I don't want infinite recursion so if we do get in that situation, just return the Window's - // standard pixmap instead (since there is no mac-specific icon then). This should be fine until - // someone changes how Windows standard - // pixmap works. - static bool recursionGuard = false; - - if (recursionGuard) - return QCommonStyle::standardPixmap(standardPixmap, opt, widget); - - recursionGuard = true; - QIcon icon = proxy()->standardIcon(standardPixmap, opt, widget); - recursionGuard = false; - int size; - switch (standardPixmap) { - default: - size = 32; - break; - case SP_MessageBoxCritical: - case SP_MessageBoxQuestion: - case SP_MessageBoxInformation: - case SP_MessageBoxWarning: - size = 64; - break; - } - return icon.pixmap(qt_getWindow(widget), QSize(size, size)); -} - -void QMacStyle::drawPrimitive(PrimitiveElement pe, const QStyleOption *opt, QPainter *p, - const QWidget *w) const -{ - Q_D(const QMacStyle); - QMacCGContext cg(p); - QWindow *window = w && w->window() ? w->window()->windowHandle() : nullptr; - d->resolveCurrentNSView(window); - switch (pe) { - case PE_IndicatorArrowUp: - case PE_IndicatorArrowDown: - case PE_IndicatorArrowRight: - case PE_IndicatorArrowLeft: { - p->save(); - p->setRenderHint(QPainter::Antialiasing); - const int xOffset = 1; // FIXME: opt->direction == Qt::LeftToRight ? 2 : -1; - qreal halfSize = 0.5 * qMin(opt->rect.width(), opt->rect.height()); - const qreal penWidth = qMax(halfSize / 3.0, 1.25); -#if QT_CONFIG(toolbutton) - if (const QToolButton *tb = qobject_cast(w)) { - // When stroking the arrow, make sure it fits in the tool button - if (tb->arrowType() != Qt::NoArrow - || tb->popupMode() == QToolButton::MenuButtonPopup) - halfSize -= penWidth; - } -#endif - - QMatrix matrix; - matrix.translate(opt->rect.center().x() + xOffset, opt->rect.center().y() + 2); - QPainterPath path; - switch(pe) { - default: - case PE_IndicatorArrowDown: - break; - case PE_IndicatorArrowUp: - matrix.rotate(180); - break; - case PE_IndicatorArrowLeft: - matrix.rotate(90); - break; - case PE_IndicatorArrowRight: - matrix.rotate(-90); - break; - } - p->setMatrix(matrix); - - path.moveTo(-halfSize, -halfSize * 0.5); - path.lineTo(0.0, halfSize * 0.5); - path.lineTo(halfSize, -halfSize * 0.5); - - const QPen arrowPen(opt->palette.text(), penWidth, - Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin); - p->strokePath(path, arrowPen); - p->restore(); - break; } -#if QT_CONFIG(tabbar) - case PE_FrameTabBarBase: - if (const QStyleOptionTabBarBase *tbb - = qstyleoption_cast(opt)) { - if (tbb->documentMode) { - p->save(); - drawTabBase(p, tbb, w); - p->restore(); - return; - } - - QRegion region(tbb->rect); - region -= tbb->tabBarRect; - p->save(); - p->setClipRegion(region); - QStyleOptionTabWidgetFrame twf; - twf.QStyleOption::operator=(*tbb); - twf.shape = tbb->shape; - switch (QMacStylePrivate::tabDirection(twf.shape)) { - case QMacStylePrivate::North: - twf.rect = twf.rect.adjusted(0, 0, 0, 10); - break; - case QMacStylePrivate::South: - twf.rect = twf.rect.adjusted(0, -10, 0, 0); - break; - case QMacStylePrivate::West: - twf.rect = twf.rect.adjusted(0, 0, 10, 0); - break; - case QMacStylePrivate::East: - twf.rect = twf.rect.adjusted(0, -10, 0, 0); - break; - } - proxy()->drawPrimitive(PE_FrameTabWidget, &twf, p, w); - p->restore(); - } - break; -#endif - case PE_PanelTipLabel: - p->fillRect(opt->rect, opt->palette.brush(QPalette::ToolTipBase)); - break; - case PE_FrameGroupBox: - if (const auto *groupBox = qstyleoption_cast(opt)) - if (groupBox->features & QStyleOptionFrame::Flat) { - QCommonStyle::drawPrimitive(pe, groupBox, p, w); - break; - } -#if QT_CONFIG(tabwidget) - Q_FALLTHROUGH(); - case PE_FrameTabWidget: -#endif - { - const auto cw = QMacStylePrivate::CocoaControl(QMacStylePrivate::Box, QStyleHelper::SizeLarge); - auto *box = static_cast(d->cocoaControl(cw)); - // FIXME Since macOS 10.14, simply calling drawRect: won't display anything anymore. - // The AppKit team is aware of this and has proposed a couple of solutions. - // The first solution was to call displayRectIgnoringOpacity:inContext: instead. - // However, it doesn't seem to work on 10.13. More importantly, dark mode on 10.14 - // is extremely slow. Light mode works fine. - // The second solution is to subclass NSBox and reimplement a trivial drawRect: which - // would only call super. This works without any issue on 10.13, but a double border - // shows on 10.14 in both light and dark modes. - // The code below picks what works on each version and mode. On 10.13 and earlier, we - // simply call drawRect: on a regular NSBox. On 10.14, we call displayRectIgnoringOpacity: - // inContext:, but only in light mode. In dark mode, we use a custom NSBox subclass, - // QDarkNSBox, of type NSBoxCustom. Its appearance is close enough to the real thing so - // we can use this for now. - d->drawNSViewInRect(box, opt->rect, p, ^(CGContextRef ctx, const CGRect &rect) { - if (QTabWidget *tabWidget = qobject_cast(opt->styleObject)) - clipTabBarFrame(opt, this, ctx); - CGContextTranslateCTM(ctx, 0, rect.origin.y + rect.size.height); - CGContextScaleCTM(ctx, 1, -1); - if (QOperatingSystemVersion::current() < QOperatingSystemVersion::MacOSMojave - || [box isMemberOfClass:QDarkNSBox.class]) { - [box drawRect:rect]; - } else { - [box displayRectIgnoringOpacity:box.bounds inContext:NSGraphicsContext.currentContext]; - } - }); - break; - } - case PE_IndicatorToolBarSeparator: { - QPainterPath path; - if (opt->state & State_Horizontal) { - int xpoint = opt->rect.center().x(); - path.moveTo(xpoint + 0.5, opt->rect.top() + 1); - path.lineTo(xpoint + 0.5, opt->rect.bottom()); - } else { - int ypoint = opt->rect.center().y(); - path.moveTo(opt->rect.left() + 2 , ypoint + 0.5); - path.lineTo(opt->rect.right() + 1, ypoint + 0.5); - } - QPainterPathStroker theStroker; - theStroker.setCapStyle(Qt::FlatCap); - theStroker.setDashPattern(QVector() << 1 << 2); - path = theStroker.createStroke(path); - p->fillPath(path, QColor(0, 0, 0, 119)); - } - break; - case PE_FrameWindow: - if (const QStyleOptionFrame *frame = qstyleoption_cast(opt)) { - if (w && w->inherits("QMdiSubWindow")) { - p->save(); - p->setPen(QPen(frame->palette.dark().color(), frame->lineWidth)); - p->setBrush(frame->palette.window()); - p->drawRect(frame->rect); - p->restore(); - } - } - break; - case PE_IndicatorDockWidgetResizeHandle: { - // The docwidget resize handle is drawn as a one-pixel wide line. - p->save(); - if (opt->state & State_Horizontal) { - p->setPen(QColor(160, 160, 160)); - p->drawLine(opt->rect.topLeft(), opt->rect.topRight()); - } else { - p->setPen(QColor(145, 145, 145)); - p->drawLine(opt->rect.topRight(), opt->rect.bottomRight()); - } - p->restore(); - } break; - case PE_IndicatorToolBarHandle: { - p->save(); - QPainterPath path; - int x = opt->rect.x() + 6; - int y = opt->rect.y() + 7; - static const int RectHeight = 2; - if (opt->state & State_Horizontal) { - while (y < opt->rect.height() - RectHeight - 5) { - path.moveTo(x, y); - path.addEllipse(x, y, RectHeight, RectHeight); - y += 6; - } - } else { - while (x < opt->rect.width() - RectHeight - 5) { - path.moveTo(x, y); - path.addEllipse(x, y, RectHeight, RectHeight); - x += 6; - } - } - p->setPen(Qt::NoPen); - QColor dark = opt->palette.dark().color().darker(); - dark.setAlphaF(0.50); - p->fillPath(path, dark); - p->restore(); - - break; - } - case PE_IndicatorHeaderArrow: - if (const QStyleOptionHeader *header = qstyleoption_cast(opt)) { - // In HITheme, up is down, down is up and hamburgers eat people. - if (header->sortIndicator != QStyleOptionHeader::None) - proxy()->drawPrimitive( - (header->sortIndicator == QStyleOptionHeader::SortDown) ? - PE_IndicatorArrowUp : PE_IndicatorArrowDown, header, p, w); - } - break; - case PE_IndicatorMenuCheckMark: { - QColor pc; - if (opt->state & State_On) - pc = opt->palette.highlightedText().color(); - else - pc = opt->palette.text().color(); - - QCFType checkmarkColor = CGColorCreateGenericRGB(static_cast(pc.redF()), - static_cast(pc.greenF()), - static_cast(pc.blueF()), - static_cast(pc.alphaF())); - // kCTFontUIFontSystem and others give the same result - // as kCTFontUIFontMenuItemMark. However, the latter is - // more reminiscent to HITheme's kThemeMenuItemMarkFont. - // See also the font for small- and mini-sized widgets, - // where we end up using the generic system font type. - const CTFontUIFontType fontType = (opt->state & State_Mini) ? kCTFontUIFontMiniSystem : - (opt->state & State_Small) ? kCTFontUIFontSmallSystem : - kCTFontUIFontMenuItemMark; - // Similarly for the font size, where there is a small difference - // between regular combobox and item view items, and and menu items. - // However, we ignore any difference for small- and mini-sized widgets. - const CGFloat fontSize = fontType == kCTFontUIFontMenuItemMark ? opt->fontMetrics.height() : 0.0; - QCFType checkmarkFont = CTFontCreateUIFontForLanguage(fontType, fontSize, NULL); - - CGContextSaveGState(cg); - CGContextSetShouldSmoothFonts(cg, NO); // Same as HITheme and Cocoa menu checkmarks - - // Baseline alignment tweaks for QComboBox and QMenu - const CGFloat vOffset = (opt->state & State_Mini) ? 0.0 : - (opt->state & State_Small) ? 1.0 : - 0.75; - - CGContextTranslateCTM(cg, 0, opt->rect.bottom()); - CGContextScaleCTM(cg, 1, -1); - // Translate back to the original position and add rect origin and offset - CGContextTranslateCTM(cg, opt->rect.x(), vOffset); - - // CTFont has severe difficulties finding the checkmark character among its - // glyphs. Fortunately, CTLine knows its ways inside the Cocoa labyrinth. - static const CFStringRef keys[] = { kCTFontAttributeName, kCTForegroundColorAttributeName }; - static const int numValues = sizeof(keys) / sizeof(keys[0]); - const CFTypeRef values[] = { (CFTypeRef)checkmarkFont, (CFTypeRef)checkmarkColor }; - Q_STATIC_ASSERT((sizeof(values) / sizeof(values[0])) == numValues); - QCFType attributes = CFDictionaryCreate(kCFAllocatorDefault, (const void **)keys, (const void **)values, - numValues, NULL, NULL); - // U+2713: CHECK MARK - QCFType checkmarkString = CFAttributedStringCreate(kCFAllocatorDefault, (CFStringRef)@"\u2713", attributes); - QCFType line = CTLineCreateWithAttributedString(checkmarkString); - - CTLineDraw((CTLineRef)line, cg); - CGContextFlush(cg); // CTLineDraw's documentation says it doesn't flush - - CGContextRestoreGState(cg); - break; } - case PE_IndicatorViewItemCheck: - case PE_IndicatorRadioButton: - case PE_IndicatorCheckBox: { - const bool isEnabled = opt->state & State_Enabled; - const bool isPressed = opt->state & State_Sunken; - const bool isRadioButton = (pe == PE_IndicatorRadioButton); - const auto ct = isRadioButton ? QMacStylePrivate::Button_RadioButton : QMacStylePrivate::Button_CheckBox; - const auto cs = d->effectiveAquaSizeConstrain(opt, w); - const auto cw = QMacStylePrivate::CocoaControl(ct, cs); - auto *tb = static_cast(d->cocoaControl(cw)); - tb.enabled = isEnabled; - tb.state = (opt->state & State_NoChange) ? NSMixedState : - (opt->state & State_On) ? NSOnState : NSOffState; - [tb highlight:isPressed]; - const auto vOffset = [=] { - // As measured - if (cs == QStyleHelper::SizeMini) - return ct == QMacStylePrivate::Button_CheckBox ? -0.5 : 0.5; - - return cs == QStyleHelper::SizeSmall ? 0.5 : 0.0; - } (); - d->drawNSViewInRect(tb, opt->rect, p, ^(CGContextRef ctx, const CGRect &rect) { - CGContextTranslateCTM(ctx, 0, vOffset); - [tb.cell drawInteriorWithFrame:rect inView:tb]; - }); - break; } - case PE_FrameFocusRect: - // Use the our own focus widget stuff. - break; - case PE_IndicatorBranch: { - if (!(opt->state & State_Children)) - break; - const auto cw = QMacStylePrivate::CocoaControl(QMacStylePrivate::Button_Disclosure, QStyleHelper::SizeLarge); - NSButtonCell *triangleCell = static_cast(d->cocoaCell(cw)); - [triangleCell setState:(opt->state & State_Open) ? NSOnState : NSOffState]; - bool viewHasFocus = (w && w->hasFocus()) || (opt->state & State_HasFocus); - [triangleCell setBackgroundStyle:((opt->state & State_Selected) && viewHasFocus) ? NSBackgroundStyleDark : NSBackgroundStyleLight]; - - d->setupNSGraphicsContext(cg, NO); - - QRect qtRect = opt->rect.adjusted(DisclosureOffset, 0, -DisclosureOffset, 0); - CGRect rect = CGRectMake(qtRect.x() + 1, qtRect.y(), qtRect.width(), qtRect.height()); - CGContextTranslateCTM(cg, rect.origin.x, rect.origin.y + rect.size.height); - CGContextScaleCTM(cg, 1, -1); - CGContextTranslateCTM(cg, -rect.origin.x, -rect.origin.y); - - [triangleCell drawBezelWithFrame:NSRectFromCGRect(rect) inView:[triangleCell controlView]]; - - d->restoreNSGraphicsContext(cg); - break; } - - case PE_Frame: { - QPen oldPen = p->pen(); - p->setPen(opt->palette.base().color().darker(140)); - p->drawRect(opt->rect.adjusted(0, 0, -1, -1)); - p->setPen(opt->palette.base().color().darker(180)); - p->drawLine(opt->rect.topLeft(), opt->rect.topRight()); - p->setPen(oldPen); - break; } - - case PE_FrameLineEdit: - if (const QStyleOptionFrame *frame = qstyleoption_cast(opt)) { - if (frame->state & State_Sunken) { - const bool isEnabled = opt->state & State_Enabled; - const bool isReadOnly = opt->state & State_ReadOnly; - const bool isRounded = frame->features & QStyleOptionFrame::Rounded; - const auto cs = d->effectiveAquaSizeConstrain(opt, w, CT_LineEdit); - const auto cw = QMacStylePrivate::CocoaControl(QMacStylePrivate::TextField, cs); - auto *tf = static_cast(d->cocoaControl(cw)); - tf.enabled = isEnabled; - tf.editable = !isReadOnly; - tf.bezeled = YES; - static_cast(tf.cell).bezelStyle = isRounded ? NSTextFieldRoundedBezel : NSTextFieldSquareBezel; - tf.frame = opt->rect.toCGRect(); - d->drawNSViewInRect(tf, opt->rect, p, ^(CGContextRef, const CGRect &rect) { - [tf.cell drawWithFrame:rect inView:tf]; - }); - } else { - QCommonStyle::drawPrimitive(pe, opt, p, w); - } - } - break; - case PE_PanelLineEdit: - QCommonStyle::drawPrimitive(pe, opt, p, w); - // Draw the focus frame for widgets other than QLineEdit (e.g. for line edits in Webkit). - // Focus frame is drawn outside the rectangle passed in the option-rect. - if (const QStyleOptionFrame *panel = qstyleoption_cast(opt)) { -#if QT_CONFIG(lineedit) - if ((opt->state & State_HasFocus) && !qobject_cast(w)) { - int vmargin = pixelMetric(QStyle::PM_FocusFrameVMargin); - int hmargin = pixelMetric(QStyle::PM_FocusFrameHMargin); - QStyleOptionFrame focusFrame = *panel; - focusFrame.rect = panel->rect.adjusted(-hmargin, -vmargin, hmargin, vmargin); - drawControl(CE_FocusFrame, &focusFrame, p, w); - } -#endif - } - - break; - case PE_PanelScrollAreaCorner: { - const QBrush brush(opt->palette.brush(QPalette::Base)); - p->fillRect(opt->rect, brush); - p->setPen(QPen(QColor(217, 217, 217))); - p->drawLine(opt->rect.topLeft(), opt->rect.topRight()); - p->drawLine(opt->rect.topLeft(), opt->rect.bottomLeft()); - } break; - case PE_FrameStatusBarItem: - break; - case PE_IndicatorTabClose: { - // Make close button visible only on the hovered tab. - QTabBar *tabBar = qobject_cast(w->parentWidget()); - if (!tabBar) { - // QStyleSheetStyle instead of CloseButton (which has - // a QTabBar as a parent widget) uses the QTabBar itself: - tabBar = qobject_cast(const_cast(w)); - } - if (tabBar) { - const bool documentMode = tabBar->documentMode(); - const QTabBarPrivate *tabBarPrivate = static_cast(QObjectPrivate::get(tabBar)); - const int hoveredTabIndex = tabBarPrivate->hoveredTabIndex(); - if (!documentMode || - (hoveredTabIndex != -1 && ((w == tabBar->tabButton(hoveredTabIndex, QTabBar::LeftSide)) || - (w == tabBar->tabButton(hoveredTabIndex, QTabBar::RightSide))))) { - const bool hover = (opt->state & State_MouseOver); - const bool selected = (opt->state & State_Selected); - const bool pressed = (opt->state & State_Sunken); - drawTabCloseButton(p, hover, selected, pressed, documentMode); - } - } - } break; - case PE_PanelStatusBar: { - // Fill the status bar with the titlebar gradient. - QLinearGradient linearGrad; - if (w ? qt_macWindowMainWindow(w->window()) : (opt->state & QStyle::State_Active)) { - linearGrad = titlebarGradientActive(); - } else { - linearGrad = titlebarGradientInactive(); - } - - linearGrad.setStart(0, opt->rect.top()); - linearGrad.setFinalStop(0, opt->rect.bottom()); - p->fillRect(opt->rect, linearGrad); - - // Draw the black separator line at the top of the status bar. - if (w ? qt_macWindowMainWindow(w->window()) : (opt->state & QStyle::State_Active)) - p->setPen(titlebarSeparatorLineActive); - else - p->setPen(titlebarSeparatorLineInactive); - p->drawLine(opt->rect.left(), opt->rect.top(), opt->rect.right(), opt->rect.top()); - - break; - } - case PE_PanelMenu: { - p->save(); - p->fillRect(opt->rect, Qt::transparent); - p->setPen(Qt::transparent); - p->setBrush(opt->palette.window()); - p->setRenderHint(QPainter::Antialiasing, true); - const QPainterPath path = d->windowPanelPath(opt->rect); - p->drawPath(path); - p->restore(); - } break; - - default: - QCommonStyle::drawPrimitive(pe, opt, p, w); - break; - } -} - -static QPixmap darkenPixmap(const QPixmap &pixmap) -{ - QImage img = pixmap.toImage().convertToFormat(QImage::Format_ARGB32); - int imgh = img.height(); - int imgw = img.width(); - int h, s, v, a; - QRgb pixel; - for (int y = 0; y < imgh; ++y) { - for (int x = 0; x < imgw; ++x) { - pixel = img.pixel(x, y); - a = qAlpha(pixel); - QColor hsvColor(pixel); - hsvColor.getHsv(&h, &s, &v); - s = qMin(100, s * 2); - v = v / 2; - hsvColor.setHsv(h, s, v); - pixel = hsvColor.rgb(); - img.setPixel(x, y, qRgba(qRed(pixel), qGreen(pixel), qBlue(pixel), a)); - } - } - return QPixmap::fromImage(img); -} - -void QMacStylePrivate::setupVerticalInvertedXform(CGContextRef cg, bool reverse, bool vertical, const CGRect &rect) const -{ - if (vertical) { - CGContextTranslateCTM(cg, rect.size.height, 0); - CGContextRotateCTM(cg, M_PI_2); - } - if (vertical != reverse) { - CGContextTranslateCTM(cg, rect.size.width, 0); - CGContextScaleCTM(cg, -1, 1); - } -} - -void QMacStyle::drawControl(ControlElement ce, const QStyleOption *opt, QPainter *p, - const QWidget *w) const -{ - Q_D(const QMacStyle); - QMacCGContext cg(p); - QWindow *window = w && w->window() ? w->window()->windowHandle() : nullptr; - d->resolveCurrentNSView(window); - switch (ce) { - case CE_HeaderSection: - if (const QStyleOptionHeader *header = qstyleoption_cast(opt)) { - State flags = header->state; - QRect ir = header->rect; - - -#if 0 // FIXME: What's this solving exactly? - bool noVerticalHeader = true; -#if QT_CONFIG(tableview) - if (w) - if (const QTableView *table = qobject_cast(w->parentWidget())) - noVerticalHeader = !table->verticalHeader()->isVisible(); -#endif - - const bool drawLeftBorder = header->orientation == Qt::Vertical - || header->position == QStyleOptionHeader::OnlyOneSection - || (header->position == QStyleOptionHeader::Beginning && noVerticalHeader); -#endif - - const bool pressed = (flags & State_Sunken) && !(flags & State_On); - p->fillRect(ir, pressed ? header->palette.dark() : header->palette.button()); - p->setPen(QPen(header->palette.dark(), 1.0)); - if (header->orientation == Qt::Horizontal) - p->drawLine(QLineF(ir.right() + 0.5, ir.top() + headerSectionSeparatorInset, - ir.right() + 0.5, ir.bottom() - headerSectionSeparatorInset)); - else - p->drawLine(QLineF(ir.left() + headerSectionSeparatorInset, ir.bottom(), - ir.right() - headerSectionSeparatorInset, ir.bottom())); - } - - break; - case CE_HeaderLabel: - if (const QStyleOptionHeader *header = qstyleoption_cast(opt)) { - p->save(); - QRect textr = header->rect; - if (!header->icon.isNull()) { - QIcon::Mode mode = QIcon::Disabled; - if (opt->state & State_Enabled) - mode = QIcon::Normal; - int iconExtent = proxy()->pixelMetric(PM_SmallIconSize); - QPixmap pixmap = header->icon.pixmap(window, QSize(iconExtent, iconExtent), mode); - - QRect pixr = header->rect; - pixr.setY(header->rect.center().y() - (pixmap.height() / pixmap.devicePixelRatio() - 1) / 2); - proxy()->drawItemPixmap(p, pixr, Qt::AlignVCenter, pixmap); - textr.translate(pixmap.width() / pixmap.devicePixelRatio() + 2, 0); - } - - proxy()->drawItemText(p, textr, header->textAlignment | Qt::AlignVCenter, header->palette, - header->state & State_Enabled, header->text, QPalette::ButtonText); - p->restore(); - } - break; - case CE_ToolButtonLabel: - if (const QStyleOptionToolButton *tb = qstyleoption_cast(opt)) { - QStyleOptionToolButton myTb = *tb; - myTb.state &= ~State_AutoRaise; -#ifndef QT_NO_ACCESSIBILITY - if (QStyleHelper::hasAncestor(opt->styleObject, QAccessible::ToolBar)) { - QRect cr = tb->rect; - int shiftX = 0; - int shiftY = 0; - bool needText = false; - int alignment = 0; - bool down = tb->state & (State_Sunken | State_On); - if (down) { - shiftX = proxy()->pixelMetric(PM_ButtonShiftHorizontal, tb, w); - shiftY = proxy()->pixelMetric(PM_ButtonShiftVertical, tb, w); - } - // The down state is special for QToolButtons in a toolbar on the Mac - // The text is a bit bolder and gets a drop shadow and the icons are also darkened. - // This doesn't really fit into any particular case in QIcon, so we - // do the majority of the work ourselves. - if (!(tb->features & QStyleOptionToolButton::Arrow)) { - Qt::ToolButtonStyle tbstyle = tb->toolButtonStyle; - if (tb->icon.isNull() && !tb->text.isEmpty()) - tbstyle = Qt::ToolButtonTextOnly; - - switch (tbstyle) { - case Qt::ToolButtonTextOnly: { - needText = true; - alignment = Qt::AlignCenter; - break; } - case Qt::ToolButtonIconOnly: - case Qt::ToolButtonTextBesideIcon: - case Qt::ToolButtonTextUnderIcon: { - QRect pr = cr; - QIcon::Mode iconMode = (tb->state & State_Enabled) ? QIcon::Normal - : QIcon::Disabled; - QIcon::State iconState = (tb->state & State_On) ? QIcon::On - : QIcon::Off; - QPixmap pixmap = tb->icon.pixmap(window, - tb->rect.size().boundedTo(tb->iconSize), - iconMode, iconState); - - // Draw the text if it's needed. - if (tb->toolButtonStyle != Qt::ToolButtonIconOnly) { - needText = true; - if (tb->toolButtonStyle == Qt::ToolButtonTextUnderIcon) { - pr.setHeight(pixmap.size().height() / pixmap.devicePixelRatio() + 6); - cr.adjust(0, pr.bottom(), 0, -3); - alignment |= Qt::AlignCenter; - } else { - pr.setWidth(pixmap.width() / pixmap.devicePixelRatio() + 8); - cr.adjust(pr.right(), 0, 0, 0); - alignment |= Qt::AlignLeft | Qt::AlignVCenter; - } - } - if (opt->state & State_Sunken) { - pr.translate(shiftX, shiftY); - pixmap = darkenPixmap(pixmap); - } - proxy()->drawItemPixmap(p, pr, Qt::AlignCenter, pixmap); - break; } - default: - Q_ASSERT(false); - break; - } - - if (needText) { - QPalette pal = tb->palette; - QPalette::ColorRole role = QPalette::NoRole; - if (!proxy()->styleHint(SH_UnderlineShortcut, tb, w)) - alignment |= Qt::TextHideMnemonic; - if (down) - cr.translate(shiftX, shiftY); - if (tbstyle == Qt::ToolButtonTextOnly - || (tbstyle != Qt::ToolButtonTextOnly && !down)) { - QPen pen = p->pen(); - QColor light = down ? Qt::black : Qt::white; - light.setAlphaF(0.375f); - p->setPen(light); - p->drawText(cr.adjusted(0, 1, 0, 1), alignment, tb->text); - p->setPen(pen); - if (down && tbstyle == Qt::ToolButtonTextOnly) { - pal = QApplication::palette("QMenu"); - pal.setCurrentColorGroup(tb->palette.currentColorGroup()); - role = QPalette::HighlightedText; - } - } - proxy()->drawItemText(p, cr, alignment, pal, - tb->state & State_Enabled, tb->text, role); - } - } else { - QCommonStyle::drawControl(ce, &myTb, p, w); - } - } else -#endif // QT_NO_ACCESSIBILITY - { - QCommonStyle::drawControl(ce, &myTb, p, w); - } - } - break; - case CE_ToolBoxTabShape: - QCommonStyle::drawControl(ce, opt, p, w); - break; - case CE_PushButtonBevel: - if (const QStyleOptionButton *btn = qstyleoption_cast(opt)) { - if (!(btn->state & (State_Raised | State_Sunken | State_On))) - break; - - if (btn->features & QStyleOptionButton::CommandLinkButton) { - QCommonStyle::drawControl(ce, opt, p, w); - break; - } - - const bool hasFocus = btn->state & State_HasFocus; - const bool isActive = btn->state & State_Active; - - // a focused auto-default button within an active window - // takes precedence over a normal default button - if ((btn->features & QStyleOptionButton::AutoDefaultButton) - && isActive && hasFocus) - d->autoDefaultButton = btn->styleObject; - else if (d->autoDefaultButton == btn->styleObject) - d->autoDefaultButton = nullptr; - - const bool isEnabled = btn->state & State_Enabled; - const bool isPressed = btn->state & State_Sunken; - const bool isHighlighted = isActive && - ((btn->state & State_On) - || (btn->features & QStyleOptionButton::DefaultButton) - || (btn->features & QStyleOptionButton::AutoDefaultButton - && d->autoDefaultButton == btn->styleObject)); - const bool hasMenu = btn->features & QStyleOptionButton::HasMenu; - const auto ct = cocoaControlType(btn, w); - const auto cs = d->effectiveAquaSizeConstrain(btn, w); - const auto cw = QMacStylePrivate::CocoaControl(ct, cs); - auto *pb = static_cast(d->cocoaControl(cw)); - // Ensure same size and location as we used to have with HITheme. - // This is more convoluted than we initialy thought. See for example - // differences between plain and menu button frames. - const QRectF frameRect = cw.adjustedControlFrame(btn->rect); - pb.frame = frameRect.toCGRect(); - - pb.enabled = isEnabled; - [pb highlight:isPressed]; - pb.state = isHighlighted && !isPressed ? NSOnState : NSOffState; - d->drawNSViewInRect(pb, frameRect, p, ^(CGContextRef, const CGRect &r) { - [pb.cell drawBezelWithFrame:r inView:pb.superview]; - }); - [pb highlight:NO]; - - if (hasMenu && cw.type == QMacStylePrivate::Button_SquareButton) { - // Using -[NSPopuButtonCell drawWithFrame:inView:] above won't do - // it right because we don't set the text in the native button. - const int mbi = proxy()->pixelMetric(QStyle::PM_MenuButtonIndicator, btn, w); - const auto ir = frameRect.toRect(); - int arrowYOffset = 0; -#if 0 - // FIXME What's this for again? - if (!w) { - // adjustment for Qt Quick Controls - arrowYOffset -= ir.top(); - if (cw.second == QStyleHelper::SizeSmall) - arrowYOffset += 1; - } -#endif - const auto ar = visualRect(btn->direction, ir, QRect(ir.right() - mbi - 6, ir.height() / 2 - arrowYOffset, mbi, mbi)); - - QStyleOption arrowOpt = *opt; - arrowOpt.rect = ar; - proxy()->drawPrimitive(PE_IndicatorArrowDown, &arrowOpt, p, w); - } - - - if (btn->state & State_HasFocus) { - // TODO Remove and use QFocusFrame instead. - const int hMargin = proxy()->pixelMetric(QStyle::PM_FocusFrameHMargin, btn, w); - const int vMargin = proxy()->pixelMetric(QStyle::PM_FocusFrameVMargin, btn, w); - QRectF focusRect; - if (cw.type == QMacStylePrivate::Button_SquareButton) { - focusRect = frameRect; - } else { - focusRect = QRectF::fromCGRect([pb alignmentRectForFrame:pb.frame]); - if (cw.type == QMacStylePrivate::Button_PushButton) - focusRect -= pushButtonShadowMargins[cw.size]; - else if (cw.type == QMacStylePrivate::Button_PullDown) - focusRect -= pullDownButtonShadowMargins[cw.size]; - } - d->drawFocusRing(p, focusRect, hMargin, vMargin, cw); - } - } - break; - case CE_PushButtonLabel: - if (const QStyleOptionButton *b = qstyleoption_cast(opt)) { - QStyleOptionButton btn(*b); - // We really don't want the label to be drawn the same as on - // windows style if it has an icon and text, then it should be more like a - // tab. So, cheat a little here. However, if it *is* only an icon - // the windows style works great, so just use that implementation. - const bool isEnabled = btn.state & State_Enabled; - const bool hasMenu = btn.features & QStyleOptionButton::HasMenu; - const bool hasIcon = !btn.icon.isNull(); - const bool hasText = !btn.text.isEmpty(); - const bool isActive = btn.state & State_Active; - const bool isPressed = btn.state & State_Sunken; - - const auto ct = cocoaControlType(&btn, w); - - if (!hasMenu && ct != QMacStylePrivate::Button_SquareButton) { - if (isPressed - || (isActive && isEnabled - && ((btn.state & State_On) - || ((btn.features & QStyleOptionButton::DefaultButton) && !d->autoDefaultButton) - || d->autoDefaultButton == btn.styleObject))) - btn.palette.setColor(QPalette::ButtonText, Qt::white); - } - - if ((!hasIcon && !hasMenu) || (hasIcon && !hasText)) { - QCommonStyle::drawControl(ce, &btn, p, w); - } else { - QRect freeContentRect = btn.rect; - QRect textRect = itemTextRect( - btn.fontMetrics, freeContentRect, Qt::AlignCenter, isEnabled, btn.text); - if (hasMenu) { - textRect.moveTo(w ? 15 : 11, textRect.top()); // Supports Qt Quick Controls - } - // Draw the icon: - if (hasIcon) { - int contentW = textRect.width(); - if (hasMenu) - contentW += proxy()->pixelMetric(PM_MenuButtonIndicator) + 4; - QIcon::Mode mode = isEnabled ? QIcon::Normal : QIcon::Disabled; - if (mode == QIcon::Normal && btn.state & State_HasFocus) - mode = QIcon::Active; - // Decide if the icon is should be on or off: - QIcon::State state = QIcon::Off; - if (btn.state & State_On) - state = QIcon::On; - QPixmap pixmap = btn.icon.pixmap(window, btn.iconSize, mode, state); - int pixmapWidth = pixmap.width() / pixmap.devicePixelRatio(); - int pixmapHeight = pixmap.height() / pixmap.devicePixelRatio(); - contentW += pixmapWidth + QMacStylePrivate::PushButtonContentPadding; - int iconLeftOffset = freeContentRect.x() + (freeContentRect.width() - contentW) / 2; - int iconTopOffset = freeContentRect.y() + (freeContentRect.height() - pixmapHeight) / 2; - QRect iconDestRect(iconLeftOffset, iconTopOffset, pixmapWidth, pixmapHeight); - QRect visualIconDestRect = visualRect(btn.direction, freeContentRect, iconDestRect); - proxy()->drawItemPixmap(p, visualIconDestRect, Qt::AlignLeft | Qt::AlignVCenter, pixmap); - int newOffset = iconDestRect.x() + iconDestRect.width() - + QMacStylePrivate::PushButtonContentPadding - textRect.x(); - textRect.adjust(newOffset, 0, newOffset, 0); - } - // Draw the text: - if (hasText) { - textRect = visualRect(btn.direction, freeContentRect, textRect); - proxy()->drawItemText(p, textRect, Qt::AlignLeft | Qt::AlignVCenter | Qt::TextShowMnemonic, btn.palette, - isEnabled, btn.text, QPalette::ButtonText); - } - } - } - break; -#if QT_CONFIG(combobox) - case CE_ComboBoxLabel: - if (const auto *cb = qstyleoption_cast(opt)) { - auto comboCopy = *cb; - comboCopy.direction = Qt::LeftToRight; - // The rectangle will be adjusted to SC_ComboBoxEditField with comboboxEditBounds() - QCommonStyle::drawControl(CE_ComboBoxLabel, &comboCopy, p, w); - } - break; -#endif // #if QT_CONFIG(combobox) -#if QT_CONFIG(tabbar) - case CE_TabBarTabShape: - if (const auto *tabOpt = qstyleoption_cast(opt)) { - if (tabOpt->documentMode) { - p->save(); - bool isUnified = false; - if (w) { - QRect tabRect = tabOpt->rect; - QPoint windowTabStart = w->mapTo(w->window(), tabRect.topLeft()); - isUnified = isInMacUnifiedToolbarArea(w->window()->windowHandle(), windowTabStart.y()); - } - - const int tabOverlap = proxy()->pixelMetric(PM_TabBarTabOverlap, opt, w); - drawTabShape(p, tabOpt, isUnified, tabOverlap); - - p->restore(); - return; - } - - const bool isActive = tabOpt->state & State_Active; - const bool isEnabled = tabOpt->state & State_Enabled; - const bool isPressed = tabOpt->state & State_Sunken; - const bool isSelected = tabOpt->state & State_Selected; - const auto tabDirection = QMacStylePrivate::tabDirection(tabOpt->shape); - const bool verticalTabs = tabDirection == QMacStylePrivate::East - || tabDirection == QMacStylePrivate::West; - - QStyleOptionTab::TabPosition tp = tabOpt->position; - QStyleOptionTab::SelectedPosition sp = tabOpt->selectedPosition; - if (tabOpt->direction == Qt::RightToLeft && !verticalTabs) { - if (tp == QStyleOptionTab::Beginning) - tp = QStyleOptionTab::End; - else if (tp == QStyleOptionTab::End) - tp = QStyleOptionTab::Beginning; - - if (sp == QStyleOptionTab::NextIsSelected) - sp = QStyleOptionTab::PreviousIsSelected; - else if (sp == QStyleOptionTab::PreviousIsSelected) - sp = QStyleOptionTab::NextIsSelected; - } - - // Alas, NSSegmentedControl and NSSegmentedCell are letting us down. - // We're not able to draw it at will, either calling -[drawSegment: - // inFrame:withView:], -[drawRect:] or anything in between. Besides, - // there's no public API do draw the pressed state, AFAICS. We'll use - // a push NSButton instead and clip the CGContext. - // NOTE/TODO: this is not true. On 10.13 NSSegmentedControl works with - // some (black?) magic/magic dances, on 10.14 it simply works (was - // it fixed in AppKit?). But, indeed, we cannot make a tab 'pressed' - // with NSSegmentedControl (only selected), so we stay with buttons - // (mixing buttons and NSSegmentedControl for such a simple thing - // is too much work). - - const auto cs = d->effectiveAquaSizeConstrain(opt, w); - // Extra hacks to get the proper pressed appreance when not selected or selected and inactive - const bool needsInactiveHack = (!isActive && isSelected); - const auto ct = !needsInactiveHack && (isSelected || tp == QStyleOptionTab::OnlyOneTab) ? - QMacStylePrivate::Button_PushButton : - QMacStylePrivate::Button_PopupButton; - const bool isPopupButton = ct == QMacStylePrivate::Button_PopupButton; - const auto cw = QMacStylePrivate::CocoaControl(ct, cs); - auto *pb = static_cast(d->cocoaControl(cw)); - - auto vOffset = isPopupButton ? 1 : 2; - if (tabDirection == QMacStylePrivate::East) - vOffset -= 1; - const auto outerAdjust = isPopupButton ? 1 : 4; - const auto innerAdjust = isPopupButton ? 20 : 10; - QRectF frameRect = tabOpt->rect; - if (verticalTabs) - frameRect = QRectF(frameRect.y(), frameRect.x(), frameRect.height(), frameRect.width()); - // Adjust before clipping - frameRect = frameRect.translated(0, vOffset); - switch (tp) { - case QStyleOptionTab::Beginning: - // Pressed state hack: tweak adjustments in preparation for flip below - if (!isSelected && tabDirection == QMacStylePrivate::West) - frameRect = frameRect.adjusted(-innerAdjust, 0, outerAdjust, 0); - else - frameRect = frameRect.adjusted(-outerAdjust, 0, innerAdjust, 0); - break; - case QStyleOptionTab::Middle: - frameRect = frameRect.adjusted(-innerAdjust, 0, innerAdjust, 0); - break; - case QStyleOptionTab::End: - // Pressed state hack: tweak adjustments in preparation for flip below - if (isSelected || tabDirection == QMacStylePrivate::West) - frameRect = frameRect.adjusted(-innerAdjust, 0, outerAdjust, 0); - else - frameRect = frameRect.adjusted(-outerAdjust, 0, innerAdjust, 0); - break; - case QStyleOptionTab::OnlyOneTab: - frameRect = frameRect.adjusted(-outerAdjust, 0, outerAdjust, 0); - break; - } - pb.frame = frameRect.toCGRect(); - - pb.enabled = isEnabled; - [pb highlight:isPressed]; - // Set off state when inactive. See needsInactiveHack for when it's selected - pb.state = (isActive && isSelected && !isPressed) ? NSOnState : NSOffState; - - const auto drawBezelBlock = ^(CGContextRef ctx, const CGRect &r) { - CGContextClipToRect(ctx, opt->rect.toCGRect()); - if (!isSelected || needsInactiveHack) { - // Final stage of the pressed state hack: flip NSPopupButton rendering - if (!verticalTabs && tp == QStyleOptionTab::End) { - CGContextTranslateCTM(ctx, opt->rect.right(), 0); - CGContextScaleCTM(ctx, -1, 1); - CGContextTranslateCTM(ctx, -frameRect.left(), 0); - } else if (tabDirection == QMacStylePrivate::West && tp == QStyleOptionTab::Beginning) { - CGContextScaleCTM(ctx, 1, -1); - CGContextTranslateCTM(ctx, 0, -frameRect.right()); - } else if (tabDirection == QMacStylePrivate::East && tp == QStyleOptionTab::End) { - CGContextTranslateCTM(ctx, 0, opt->rect.bottom()); - CGContextScaleCTM(ctx, 1, -1); - CGContextTranslateCTM(ctx, 0, -frameRect.left()); - } - } - - // Rotate and translate CTM when vertical - // On macOS: positive angle is CW, negative is CCW - if (tabDirection == QMacStylePrivate::West) { - CGContextTranslateCTM(ctx, 0, frameRect.right()); - CGContextRotateCTM(ctx, -M_PI_2); - CGContextTranslateCTM(ctx, -frameRect.left(), 0); - } else if (tabDirection == QMacStylePrivate::East) { - CGContextTranslateCTM(ctx, opt->rect.right(), 0); - CGContextRotateCTM(ctx, M_PI_2); - } - - [pb.cell drawBezelWithFrame:r inView:pb.superview]; - }; - - if (needsInactiveHack) { - // First, render tab as non-selected tab on a pixamp - const qreal pixelRatio = p->device()->devicePixelRatioF(); - QImage tabPixmap(opt->rect.size() * pixelRatio, QImage::Format_ARGB32_Premultiplied); - tabPixmap.setDevicePixelRatio(pixelRatio); - tabPixmap.fill(Qt::transparent); - QPainter tabPainter(&tabPixmap); - d->drawNSViewInRect(pb, frameRect, &tabPainter, ^(CGContextRef ctx, const CGRect &r) { - CGContextTranslateCTM(ctx, -opt->rect.left(), -opt->rect.top()); - drawBezelBlock(ctx, r); - }); - tabPainter.end(); - - // Then, darken it with the proper shade of gray - const qreal inactiveGray = 0.898; // As measured - const int inactiveGray8 = qRound(inactiveGray * 255.0); - const QRgb inactiveGrayRGB = qRgb(inactiveGray8, inactiveGray8, inactiveGray8); - for (int l = 0; l < tabPixmap.height(); ++l) { - auto *line = reinterpret_cast(tabPixmap.scanLine(l)); - for (int i = 0; i < tabPixmap.width(); ++i) { - if (qAlpha(line[i]) == 255) { - line[i] = inactiveGrayRGB; - } else if (qAlpha(line[i]) > 128) { - const int g = qRound(inactiveGray * qRed(line[i])); - line[i] = qRgba(g, g, g, qAlpha(line[i])); - } - } - } - - // Finally, draw the tab pixmap on the current painter - p->drawImage(opt->rect, tabPixmap); - } else { - d->drawNSViewInRect(pb, frameRect, p, drawBezelBlock); - } - - if (!isSelected && sp != QStyleOptionTab::NextIsSelected - && tp != QStyleOptionTab::End - && tp != QStyleOptionTab::OnlyOneTab) { - static const QPen separatorPen(Qt::black, 1.0); - p->save(); - p->setOpacity(isEnabled ? 0.105 : 0.06); // As measured - p->setPen(separatorPen); - if (tabDirection == QMacStylePrivate::West) { - p->drawLine(QLineF(opt->rect.left() + 1.5, opt->rect.bottom(), - opt->rect.right() - 0.5, opt->rect.bottom())); - } else if (tabDirection == QMacStylePrivate::East) { - p->drawLine(QLineF(opt->rect.left(), opt->rect.bottom(), - opt->rect.right() - 0.5, opt->rect.bottom())); - } else { - p->drawLine(QLineF(opt->rect.right(), opt->rect.top() + 1.0, - opt->rect.right(), opt->rect.bottom() - 0.5)); - } - p->restore(); - } - - // TODO Needs size adjustment to fit the focus ring - if (tabOpt->state & State_HasFocus) { - QMacStylePrivate::CocoaControlType focusRingType; - switch (tp) { - case QStyleOptionTab::Beginning: - focusRingType = verticalTabs ? QMacStylePrivate::SegmentedControl_Last - : QMacStylePrivate::SegmentedControl_First; - break; - case QStyleOptionTab::Middle: - focusRingType = QMacStylePrivate::SegmentedControl_Middle; - break; - case QStyleOptionTab::End: - focusRingType = verticalTabs ? QMacStylePrivate::SegmentedControl_First - : QMacStylePrivate::SegmentedControl_Last; - break; - case QStyleOptionTab::OnlyOneTab: - focusRingType = QMacStylePrivate::SegmentedControl_Single; - break; - } - } - } - break; - case CE_TabBarTabLabel: - if (const auto *tab = qstyleoption_cast(opt)) { - QStyleOptionTab myTab = *tab; - const auto tabDirection = QMacStylePrivate::tabDirection(tab->shape); - const bool verticalTabs = tabDirection == QMacStylePrivate::East - || tabDirection == QMacStylePrivate::West; - - // Check to see if we use have the same as the system font - // (QComboMenuItem is internal and should never be seen by the - // outside world, unless they read the source, in which case, it's - // their own fault). - const bool nonDefaultFont = p->font() != qt_app_fonts_hash()->value("QComboMenuItem"); - - if (!myTab.documentMode && (myTab.state & State_Selected) && (myTab.state & State_Active)) - if (const auto *tabBar = qobject_cast(w)) - if (!tabBar->tabTextColor(tabBar->currentIndex()).isValid()) - myTab.palette.setColor(QPalette::WindowText, Qt::white); - - int heightOffset = 0; - if (verticalTabs) { - heightOffset = -1; - } else if (nonDefaultFont) { - if (p->fontMetrics().height() == myTab.rect.height()) - heightOffset = 2; - } - myTab.rect.setHeight(myTab.rect.height() + heightOffset); - - QCommonStyle::drawControl(ce, &myTab, p, w); - } - break; -#endif -#if QT_CONFIG(dockwidget) - case CE_DockWidgetTitle: - if (const auto *dwOpt = qstyleoption_cast(opt)) { - const bool isVertical = dwOpt->verticalTitleBar; - const auto effectiveRect = isVertical ? opt->rect.transposed() : opt->rect; - p->save(); - if (isVertical) { - p->translate(effectiveRect.left(), effectiveRect.top() + effectiveRect.width()); - p->rotate(-90); - p->translate(-effectiveRect.left(), -effectiveRect.top()); - } - - // fill title bar background - QLinearGradient linearGrad; - linearGrad.setStart(QPointF(0, 0)); - linearGrad.setFinalStop(QPointF(0, 2 * effectiveRect.height())); - linearGrad.setColorAt(0, opt->palette.button().color()); - linearGrad.setColorAt(1, opt->palette.dark().color()); - p->fillRect(effectiveRect, linearGrad); - - // draw horizontal line at bottom - p->setPen(opt->palette.dark().color()); - p->drawLine(effectiveRect.bottomLeft(), effectiveRect.bottomRight()); - - if (!dwOpt->title.isEmpty()) { - auto titleRect = proxy()->subElementRect(SE_DockWidgetTitleBarText, opt, w); - if (isVertical) - titleRect = QRect(effectiveRect.left() + opt->rect.bottom() - titleRect.bottom(), - effectiveRect.top() + titleRect.left() - opt->rect.left(), - titleRect.height(), - titleRect.width()); - - const auto text = p->fontMetrics().elidedText(dwOpt->title, Qt::ElideRight, titleRect.width()); - proxy()->drawItemText(p, titleRect, Qt::AlignCenter | Qt::TextShowMnemonic, dwOpt->palette, - dwOpt->state & State_Enabled, text, QPalette::WindowText); - } - p->restore(); - } - break; -#endif - case CE_FocusFrame: { - const auto *ff = qobject_cast(w); - const auto *ffw = ff ? ff->widget() : nullptr; - const auto ct = [=] { - if (ffw) { - if (ffw->inherits("QCheckBox")) - return QMacStylePrivate::Button_CheckBox; - if (ffw->inherits("QRadioButton")) - return QMacStylePrivate::Button_RadioButton; - if (ffw->inherits("QLineEdit") || ffw->inherits("QTextEdit")) - return QMacStylePrivate::TextField; - } - - return QMacStylePrivate::Box; // Not really, just make it the default - } (); - const auto cs = ffw ? (ffw->testAttribute(Qt::WA_MacMiniSize) ? QStyleHelper::SizeMini : - ffw->testAttribute(Qt::WA_MacSmallSize) ? QStyleHelper::SizeSmall : - QStyleHelper::SizeLarge) : - QStyleHelper::SizeLarge; - const int hMargin = proxy()->pixelMetric(QStyle::PM_FocusFrameHMargin, opt, w); - const int vMargin = proxy()->pixelMetric(QStyle::PM_FocusFrameVMargin, opt, w); - d->drawFocusRing(p, opt->rect, hMargin, vMargin, QMacStylePrivate::CocoaControl(ct, cs)); - break; } - case CE_MenuEmptyArea: - // Skip: PE_PanelMenu fills in everything - break; - case CE_MenuItem: - case CE_MenuHMargin: - case CE_MenuVMargin: - case CE_MenuTearoff: - case CE_MenuScroller: - if (const QStyleOptionMenuItem *mi = qstyleoption_cast(opt)) { - const bool active = mi->state & State_Selected; - if (active) - p->fillRect(mi->rect, mi->palette.highlight()); - - const QStyleHelper::WidgetSizePolicy widgetSize = d->aquaSizeConstrain(opt, w); - - if (ce == CE_MenuTearoff) { - p->setPen(QPen(mi->palette.dark().color(), 1, Qt::DashLine)); - p->drawLine(mi->rect.x() + 2, mi->rect.y() + mi->rect.height() / 2 - 1, - mi->rect.x() + mi->rect.width() - 4, - mi->rect.y() + mi->rect.height() / 2 - 1); - p->setPen(QPen(mi->palette.light().color(), 1, Qt::DashLine)); - p->drawLine(mi->rect.x() + 2, mi->rect.y() + mi->rect.height() / 2, - mi->rect.x() + mi->rect.width() - 4, - mi->rect.y() + mi->rect.height() / 2); - } else if (ce == CE_MenuScroller) { - const QSize scrollerSize = QSize(10, 8); - const int scrollerVOffset = 5; - const int left = mi->rect.x() + (mi->rect.width() - scrollerSize.width()) / 2; - const int right = left + scrollerSize.width(); - int top; - int bottom; - if (opt->state & State_DownArrow) { - bottom = mi->rect.y() + scrollerVOffset; - top = bottom + scrollerSize.height(); - } else { - bottom = mi->rect.bottom() - scrollerVOffset; - top = bottom - scrollerSize.height(); - } - p->save(); - p->setRenderHint(QPainter::Antialiasing); - QPainterPath path; - path.moveTo(left, bottom); - path.lineTo(right, bottom); - path.lineTo((left + right) / 2, top); - p->fillPath(path, opt->palette.buttonText()); - p->restore(); - } else if (ce != CE_MenuItem) { - break; - } - - if (mi->menuItemType == QStyleOptionMenuItem::Separator) { - CGColorRef separatorColor = [NSColor quaternaryLabelColor].CGColor; - const QRect separatorRect = QRect(mi->rect.left(), mi->rect.center().y(), mi->rect.width(), 2); - p->fillRect(separatorRect, qt_mac_toQColor(separatorColor)); - break; - } - - const int maxpmw = mi->maxIconWidth; - const bool enabled = mi->state & State_Enabled; - - int xpos = mi->rect.x() + 18; - int checkcol = maxpmw; - if (!enabled) - p->setPen(mi->palette.text().color()); - else if (active) - p->setPen(mi->palette.highlightedText().color()); - else - p->setPen(mi->palette.buttonText().color()); - - if (mi->checked) { - QStyleOption checkmarkOpt; - checkmarkOpt.initFrom(w); - - const int mw = checkcol + macItemFrame; - const int mh = mi->rect.height() + macItemFrame; - const int xp = mi->rect.x() + macItemFrame; - checkmarkOpt.rect = QRect(xp, mi->rect.y() - checkmarkOpt.fontMetrics.descent(), mw, mh); - - checkmarkOpt.state.setFlag(State_On, active); - checkmarkOpt.state.setFlag(State_Enabled, enabled); - if (widgetSize == QStyleHelper::SizeMini) - checkmarkOpt.state |= State_Mini; - else if (widgetSize == QStyleHelper::SizeSmall) - checkmarkOpt.state |= State_Small; - - // We let drawPrimitive(PE_IndicatorMenuCheckMark) pick the right color - checkmarkOpt.palette.setColor(QPalette::HighlightedText, p->pen().color()); - checkmarkOpt.palette.setColor(QPalette::Text, p->pen().color()); - - proxy()->drawPrimitive(PE_IndicatorMenuCheckMark, &checkmarkOpt, p, w); - } - if (!mi->icon.isNull()) { - QIcon::Mode mode = (mi->state & State_Enabled) ? QIcon::Normal - : QIcon::Disabled; - // Always be normal or disabled to follow the Mac style. - int smallIconSize = proxy()->pixelMetric(PM_SmallIconSize); - QSize iconSize(smallIconSize, smallIconSize); -#if QT_CONFIG(combobox) - if (const QComboBox *comboBox = qobject_cast(w)) { - iconSize = comboBox->iconSize(); - } -#endif - QPixmap pixmap = mi->icon.pixmap(window, iconSize, mode); - int pixw = pixmap.width() / pixmap.devicePixelRatio(); - int pixh = pixmap.height() / pixmap.devicePixelRatio(); - QRect cr(xpos, mi->rect.y(), checkcol, mi->rect.height()); - QRect pmr(0, 0, pixw, pixh); - pmr.moveCenter(cr.center()); - p->drawPixmap(pmr.topLeft(), pixmap); - xpos += pixw + 6; - } - - QString s = mi->text; - const auto text_flags = Qt::AlignVCenter | Qt::TextHideMnemonic - | Qt::TextSingleLine | Qt::AlignAbsolute; - int yPos = mi->rect.y(); - if (widgetSize == QStyleHelper::SizeMini) - yPos += 1; - - const bool isSubMenu = mi->menuItemType == QStyleOptionMenuItem::SubMenu; - const int tabwidth = isSubMenu ? 9 : mi->tabWidth; - - QString rightMarginText; - if (isSubMenu) - rightMarginText = QStringLiteral("\u25b6\ufe0e"); // U+25B6 U+FE0E: BLACK RIGHT-POINTING TRIANGLE - - // If present, save and remove embedded shorcut from text - const int tabIndex = s.indexOf(QLatin1Char('\t')); - if (tabIndex >= 0) { - if (!isSubMenu) // ... but ignore it if it's a submenu. - rightMarginText = s.mid(tabIndex + 1); - s = s.left(tabIndex); - } - - p->save(); - if (!rightMarginText.isEmpty()) { - p->setFont(qt_app_fonts_hash()->value("QMenuItem", p->font())); - int xp = mi->rect.right() - tabwidth - macRightBorder + 2; - if (!isSubMenu) - xp -= macItemHMargin + macItemFrame + 3; // Adjust for shortcut - p->drawText(xp, yPos, tabwidth, mi->rect.height(), text_flags | Qt::AlignRight, rightMarginText); - } - - if (!s.isEmpty()) { - const int xm = macItemFrame + maxpmw + macItemHMargin; - QFont myFont = mi->font; - // myFont may not have any "hard" flags set. We override - // the point size so that when it is resolved against the device, this font will win. - // This is mainly to handle cases where someone sets the font on the window - // and then the combo inherits it and passes it onward. At that point the resolve mask - // is very, very weak. This makes it stonger. - myFont.setPointSizeF(QFontInfo(mi->font).pointSizeF()); - - // QTBUG-65653: Our own text rendering doesn't look good enough, especially on non-retina - // displays. Worked around here while waiting for a proper fix in QCoreTextFontEngine. - // Only if we're not using QCoreTextFontEngine we do fallback to our own text rendering. - const auto *fontEngine = QFontPrivate::get(myFont)->engineForScript(QChar::Script_Common); - Q_ASSERT(fontEngine); - if (fontEngine->type() == QFontEngine::Multi) { - fontEngine = static_cast(fontEngine)->engine(0); - Q_ASSERT(fontEngine); - } - if (fontEngine->type() == QFontEngine::Mac) { - NSFont *f = (NSFont *)(CTFontRef)fontEngine->handle(); - - // Respect the menu item palette as set in the style option. - const auto pc = p->pen().color(); - NSColor *c = [NSColor colorWithSRGBRed:pc.redF() - green:pc.greenF() - blue:pc.blueF() - alpha:pc.alphaF()]; - - s = qt_mac_removeMnemonics(s); - const auto textRect = CGRectMake(xpos, yPos, mi->rect.width() - xm - tabwidth + 1, mi->rect.height()); - - QMacCGContext cgCtx(p); - d->setupNSGraphicsContext(cgCtx, YES); - - [s.toNSString() drawInRect:textRect - withAttributes:@{ NSFontAttributeName:f, NSForegroundColorAttributeName:c }]; - - d->restoreNSGraphicsContext(cgCtx); - } else { - p->setFont(myFont); - p->drawText(xpos, yPos, mi->rect.width() - xm - tabwidth + 1, - mi->rect.height(), text_flags, s); - } - } - p->restore(); - } - break; - case CE_MenuBarItem: - case CE_MenuBarEmptyArea: - if (const QStyleOptionMenuItem *mi = qstyleoption_cast(opt)) { - const bool selected = (opt->state & State_Selected) && (opt->state & State_Enabled) && (opt->state & State_Sunken); - const QBrush bg = selected ? mi->palette.highlight() : mi->palette.background(); - p->fillRect(mi->rect, bg); - - if (ce != CE_MenuBarItem) - break; - - if (!mi->icon.isNull()) { - int iconExtent = proxy()->pixelMetric(PM_SmallIconSize); - drawItemPixmap(p, mi->rect, - Qt::AlignCenter | Qt::TextHideMnemonic | Qt::TextDontClip - | Qt::TextSingleLine, - mi->icon.pixmap(window, QSize(iconExtent, iconExtent), - (mi->state & State_Enabled) ? QIcon::Normal : QIcon::Disabled)); - } else { - drawItemText(p, mi->rect, - Qt::AlignCenter | Qt::TextHideMnemonic | Qt::TextDontClip - | Qt::TextSingleLine, - mi->palette, mi->state & State_Enabled, - mi->text, selected ? QPalette::HighlightedText : QPalette::ButtonText); - } - } - break; - case CE_ProgressBarLabel: - case CE_ProgressBarGroove: - // Do nothing. All done in CE_ProgressBarContents. Only keep these for proxy style overrides. - break; - case CE_ProgressBarContents: - if (const QStyleOptionProgressBar *pb = qstyleoption_cast(opt)) { - QMacAutoReleasePool pool; - const bool isIndeterminate = (pb->minimum == 0 && pb->maximum == 0); - const bool vertical = pb->orientation == Qt::Vertical; - const bool inverted = pb->invertedAppearance; - bool reverse = (!vertical && (pb->direction == Qt::RightToLeft)); - if (inverted) - reverse = !reverse; - - QRect rect = pb->rect; - if (vertical) - rect = rect.transposed(); - const CGRect cgRect = rect.toCGRect(); - - const auto aquaSize = d->effectiveAquaSizeConstrain(opt, w); - const QProgressStyleAnimation *animation = qobject_cast(d->animation(opt->styleObject)); - QIndeterminateProgressIndicator *ipi = nil; - if (isIndeterminate || animation) - ipi = static_cast(d->cocoaControl({ QMacStylePrivate::ProgressIndicator_Indeterminate, aquaSize })); - if (isIndeterminate) { - // QIndeterminateProgressIndicator derives from NSProgressIndicator. We use a single - // instance that we start animating as soon as one of the progress bars is indeterminate. - // Since they will be in sync (as it's the case in Cocoa), we just need to draw it with - // the right geometry when the animation triggers an update. However, we can't hide it - // entirely between frames since that would stop the animation, so we just set its alpha - // value to 0. Same if we remove it from its superview. See QIndeterminateProgressIndicator - // implementation for details. - if (!animation && opt->styleObject) { - auto *animation = new QProgressStyleAnimation(d->animateSpeed(QMacStylePrivate::AquaProgressBar), opt->styleObject); - // NSProgressIndicator is heavier to draw than the HITheme API, so we reduce the frame rate a couple notches. - animation->setFrameRate(QStyleAnimation::FifteenFps); - d->startAnimation(animation); - [ipi startAnimation]; - } - - d->setupNSGraphicsContext(cg, NO); - d->setupVerticalInvertedXform(cg, reverse, vertical, cgRect); - [ipi drawWithFrame:cgRect inView:d->backingStoreNSView]; - d->restoreNSGraphicsContext(cg); - } else { - if (animation) { - d->stopAnimation(opt->styleObject); - [ipi stopAnimation]; - } - - const auto cw = QMacStylePrivate::CocoaControl(QMacStylePrivate::ProgressIndicator_Determinate, aquaSize); - auto *pi = static_cast(d->cocoaControl(cw)); - d->drawNSViewInRect(pi, rect, p, ^(CGContextRef ctx, const CGRect &rect) { - d->setupVerticalInvertedXform(ctx, reverse, vertical, rect); - pi.minValue = pb->minimum; - pi.maxValue = pb->maximum; - pi.doubleValue = pb->progress; - [pi drawRect:rect]; - }); - } - } - break; - case CE_SizeGrip: { - // This is not HIG kosher: Fall back to the old stuff until we decide what to do. -#ifndef QT_NO_MDIAREA - if (!w || !qobject_cast(w->parentWidget())) -#endif - break; - - if (w->testAttribute(Qt::WA_MacOpaqueSizeGrip)) - p->fillRect(opt->rect, opt->palette.window()); - - QPen lineColor = QColor(82, 82, 82, 192); - lineColor.setWidth(1); - p->save(); - p->setRenderHint(QPainter::Antialiasing); - p->setPen(lineColor); - const Qt::LayoutDirection layoutDirection = w ? w->layoutDirection() : qApp->layoutDirection(); - const int NumLines = 3; - for (int l = 0; l < NumLines; ++l) { - const int offset = (l * 4 + 3); - QPoint start, end; - if (layoutDirection == Qt::LeftToRight) { - start = QPoint(opt->rect.width() - offset, opt->rect.height() - 1); - end = QPoint(opt->rect.width() - 1, opt->rect.height() - offset); - } else { - start = QPoint(offset, opt->rect.height() - 1); - end = QPoint(1, opt->rect.height() - offset); - } - p->drawLine(start, end); - } - p->restore(); - break; - } - case CE_Splitter: - if (opt->rect.width() > 1 && opt->rect.height() > 1) { - const bool isVertical = !(opt->state & QStyle::State_Horizontal); - // Qt refers to the layout orientation, while Cocoa refers to the divider's. - const auto ct = isVertical ? QMacStylePrivate::SplitView_Horizontal : QMacStylePrivate::SplitView_Vertical; - const auto cw = QMacStylePrivate::CocoaControl(ct, QStyleHelper::SizeLarge); - auto *sv = static_cast(d->cocoaControl(cw)); - sv.frame = opt->rect.toCGRect(); - d->drawNSViewInRect(sv, opt->rect, p, ^(CGContextRef, const CGRect &rect) { - [sv drawDividerInRect:rect]; - }); - } else { - QPen oldPen = p->pen(); - p->setPen(opt->palette.dark().color()); - if (opt->state & QStyle::State_Horizontal) - p->drawLine(opt->rect.topLeft(), opt->rect.bottomLeft()); - else - p->drawLine(opt->rect.topLeft(), opt->rect.topRight()); - p->setPen(oldPen); - } - break; - case CE_RubberBand: - if (const QStyleOptionRubberBand *rubber = qstyleoption_cast(opt)) { - QColor fillColor(opt->palette.color(QPalette::Disabled, QPalette::Highlight)); - if (!rubber->opaque) { - QColor strokeColor; - // I retrieved these colors from the Carbon-Dev mailing list - strokeColor.setHsvF(0, 0, 0.86, 1.0); - fillColor.setHsvF(0, 0, 0.53, 0.25); - if (opt->rect.width() * opt->rect.height() <= 3) { - p->fillRect(opt->rect, strokeColor); - } else { - QPen oldPen = p->pen(); - QBrush oldBrush = p->brush(); - QPen pen(strokeColor); - p->setPen(pen); - p->setBrush(fillColor); - QRect adjusted = opt->rect.adjusted(1, 1, -1, -1); - if (adjusted.isValid()) - p->drawRect(adjusted); - p->setPen(oldPen); - p->setBrush(oldBrush); - } - } else { - p->fillRect(opt->rect, fillColor); - } - } - break; -#ifndef QT_NO_TOOLBAR - case CE_ToolBar: { - const QStyleOptionToolBar *toolBar = qstyleoption_cast(opt); - const bool isDarkMode = qt_mac_applicationIsInDarkMode(); - - // Unified title and toolbar drawing. In this mode the cocoa platform plugin will - // fill the top toolbar area part with a background gradient that "unifies" with - // the title bar. The following code fills the toolBar area with transparent pixels - // to make that gradient visible. - if (w) { -#if QT_CONFIG(mainwindow) - if (QMainWindow * mainWindow = qobject_cast(w->window())) { - if (toolBar && toolBar->toolBarArea == Qt::TopToolBarArea && mainWindow->unifiedTitleAndToolBarOnMac()) { - // fill with transparent pixels. - p->save(); - p->setCompositionMode(QPainter::CompositionMode_Source); - p->fillRect(opt->rect, Qt::transparent); - p->restore(); - - // Draw a horizontal separator line at the toolBar bottom if the "unified" area ends here. - // There might be additional toolbars or other widgets such as tab bars in document - // mode below. Determine this by making a unified toolbar area test for the row below - // this toolbar. - const QPoint windowToolbarEnd = w->mapTo(w->window(), opt->rect.bottomLeft()); - const bool isEndOfUnifiedArea = !isInMacUnifiedToolbarArea(w->window()->windowHandle(), windowToolbarEnd.y() + 1); - if (isEndOfUnifiedArea) { - const int margin = qt_mac_aqua_get_metric(SeparatorSize); - const auto separatorRect = QRect(opt->rect.left(), opt->rect.bottom(), opt->rect.width(), margin); - p->fillRect(separatorRect, isDarkMode ? darkModeSeparatorLine : opt->palette.dark().color()); - } - break; - } - } -#endif - } - - // draw background gradient - QLinearGradient linearGrad; - if (opt->state & State_Horizontal) - linearGrad = QLinearGradient(0, opt->rect.top(), 0, opt->rect.bottom()); - else - linearGrad = QLinearGradient(opt->rect.left(), 0, opt->rect.right(), 0); - - QColor mainWindowGradientBegin = isDarkMode ? darkMainWindowGradientBegin : lightMainWindowGradientBegin; - QColor mainWindowGradientEnd = isDarkMode ? darkMainWindowGradientEnd : lightMainWindowGradientEnd; - - linearGrad.setColorAt(0, mainWindowGradientBegin); - linearGrad.setColorAt(1, mainWindowGradientEnd); - p->fillRect(opt->rect, linearGrad); - - p->save(); - if (opt->state & State_Horizontal) { - p->setPen(isDarkMode ? darkModeSeparatorLine : mainWindowGradientBegin.lighter(114)); - p->drawLine(opt->rect.topLeft(), opt->rect.topRight()); - p->setPen(isDarkMode ? darkModeSeparatorLine :mainWindowGradientEnd.darker(114)); - p->drawLine(opt->rect.bottomLeft(), opt->rect.bottomRight()); - } else { - p->setPen(isDarkMode ? darkModeSeparatorLine : mainWindowGradientBegin.lighter(114)); - p->drawLine(opt->rect.topLeft(), opt->rect.bottomLeft()); - p->setPen(isDarkMode ? darkModeSeparatorLine : mainWindowGradientEnd.darker(114)); - p->drawLine(opt->rect.topRight(), opt->rect.bottomRight()); - } - p->restore(); - - - } break; -#endif - default: - QCommonStyle::drawControl(ce, opt, p, w); - break; - } -} - -static void setLayoutItemMargins(int left, int top, int right, int bottom, QRect *rect, Qt::LayoutDirection dir) -{ - if (dir == Qt::RightToLeft) { - rect->adjust(-right, top, -left, bottom); - } else { - rect->adjust(left, top, right, bottom); - } -} - -QRect QMacStyle::subElementRect(SubElement sr, const QStyleOption *opt, - const QWidget *widget) const -{ - Q_D(const QMacStyle); - QRect rect; - const int controlSize = getControlSize(opt, widget); - - switch (sr) { - case SE_ItemViewItemText: - if (const QStyleOptionViewItem *vopt = qstyleoption_cast(opt)) { - int fw = proxy()->pixelMetric(PM_FocusFrameHMargin, opt, widget); - // We add the focusframeargin between icon and text in commonstyle - rect = QCommonStyle::subElementRect(sr, opt, widget); - if (vopt->features & QStyleOptionViewItem::HasDecoration) - rect.adjust(-fw, 0, 0, 0); - } - break; - case SE_ToolBoxTabContents: - rect = QCommonStyle::subElementRect(sr, opt, widget); - break; - case SE_PushButtonContents: - if (const QStyleOptionButton *btn = qstyleoption_cast(opt)) { - // Comment from the old HITheme days: - // "Unlike Carbon, we want the button to always be drawn inside its bounds. - // Therefore, the button is a bit smaller, so that even if it got focus, - // the focus 'shadow' will be inside. Adjust the content rect likewise." - // In the future, we should consider using -[NSCell titleRectForBounds:]. - // Since it requires configuring the NSButton fully, i.e. frame, image, - // title and font, we keep things more manual until we are more familiar - // with side effects when changing NSButton state. - const auto ct = cocoaControlType(btn, widget); - const auto cs = d->effectiveAquaSizeConstrain(btn, widget); - const auto cw = QMacStylePrivate::CocoaControl(ct, cs); - const auto frameRect = cw.adjustedControlFrame(btn->rect); - const auto titleMargins = cw.titleMargins(); - rect = (frameRect - titleMargins).toRect(); - } - break; - case SE_HeaderLabel: { - int margin = proxy()->pixelMetric(QStyle::PM_HeaderMargin, opt, widget); - rect.setRect(opt->rect.x() + margin, opt->rect.y(), - opt->rect.width() - margin * 2, opt->rect.height() - 2); - if (const QStyleOptionHeader *header = qstyleoption_cast(opt)) { - // Subtract width needed for arrow, if there is one - if (header->sortIndicator != QStyleOptionHeader::None) { - if (opt->state & State_Horizontal) - rect.setWidth(rect.width() - (headerSectionArrowHeight) - (margin * 2)); - else - rect.setHeight(rect.height() - (headerSectionArrowHeight) - (margin * 2)); - } - } - rect = visualRect(opt->direction, opt->rect, rect); - break; - } - case SE_HeaderArrow: { - int h = opt->rect.height(); - int w = opt->rect.width(); - int x = opt->rect.x(); - int y = opt->rect.y(); - int margin = proxy()->pixelMetric(QStyle::PM_HeaderMargin, opt, widget); - - if (opt->state & State_Horizontal) { - rect.setRect(x + w - margin * 2 - headerSectionArrowHeight, y + 5, - headerSectionArrowHeight, h - margin * 2 - 5); - } else { - rect.setRect(x + 5, y + h - margin * 2 - headerSectionArrowHeight, - w - margin * 2 - 5, headerSectionArrowHeight); - } - rect = visualRect(opt->direction, opt->rect, rect); - break; - } - case SE_ProgressBarGroove: - // Wrong in the secondary dimension, but accurate enough in the main dimension. - rect = opt->rect; - break; - case SE_ProgressBarLabel: - break; - case SE_ProgressBarContents: - rect = opt->rect; - break; - case SE_TreeViewDisclosureItem: { - rect = opt->rect; - // As previously returned by HIThemeGetButtonContentBounds - rect.setLeft(rect.left() + 2 + DisclosureOffset); - break; - } -#if QT_CONFIG(tabwidget) - case SE_TabWidgetLeftCorner: - if (const QStyleOptionTabWidgetFrame *twf - = qstyleoption_cast(opt)) { - switch (twf->shape) { - case QTabBar::RoundedNorth: - case QTabBar::TriangularNorth: - rect = QRect(QPoint(0, 0), twf->leftCornerWidgetSize); - break; - case QTabBar::RoundedSouth: - case QTabBar::TriangularSouth: - rect = QRect(QPoint(0, twf->rect.height() - twf->leftCornerWidgetSize.height()), - twf->leftCornerWidgetSize); - break; - default: - break; - } - rect = visualRect(twf->direction, twf->rect, rect); - } - break; - case SE_TabWidgetRightCorner: - if (const QStyleOptionTabWidgetFrame *twf - = qstyleoption_cast(opt)) { - switch (twf->shape) { - case QTabBar::RoundedNorth: - case QTabBar::TriangularNorth: - rect = QRect(QPoint(twf->rect.width() - twf->rightCornerWidgetSize.width(), 0), - twf->rightCornerWidgetSize); - break; - case QTabBar::RoundedSouth: - case QTabBar::TriangularSouth: - rect = QRect(QPoint(twf->rect.width() - twf->rightCornerWidgetSize.width(), - twf->rect.height() - twf->rightCornerWidgetSize.height()), - twf->rightCornerWidgetSize); - break; - default: - break; - } - rect = visualRect(twf->direction, twf->rect, rect); - } - break; - case SE_TabWidgetTabContents: - rect = QCommonStyle::subElementRect(sr, opt, widget); - if (const auto *twf = qstyleoption_cast(opt)) { - if (twf->lineWidth != 0) { - switch (QMacStylePrivate::tabDirection(twf->shape)) { - case QMacStylePrivate::North: - rect.adjust(+1, +14, -1, -1); - break; - case QMacStylePrivate::South: - rect.adjust(+1, +1, -1, -14); - break; - case QMacStylePrivate::West: - rect.adjust(+14, +1, -1, -1); - break; - case QMacStylePrivate::East: - rect.adjust(+1, +1, -14, -1); - } - } - } - break; - case SE_TabBarTabText: - if (const QStyleOptionTab *tab = qstyleoption_cast(opt)) { - QRect dummyIconRect; - d->tabLayout(tab, widget, &rect, &dummyIconRect); - } - break; - case SE_TabBarTabLeftButton: - case SE_TabBarTabRightButton: - if (const QStyleOptionTab *tab = qstyleoption_cast(opt)) { - bool selected = tab->state & State_Selected; - int verticalShift = proxy()->pixelMetric(QStyle::PM_TabBarTabShiftVertical, tab, widget); - int horizontalShift = proxy()->pixelMetric(QStyle::PM_TabBarTabShiftHorizontal, tab, widget); - int hpadding = 5; - - bool verticalTabs = tab->shape == QTabBar::RoundedEast - || tab->shape == QTabBar::RoundedWest - || tab->shape == QTabBar::TriangularEast - || tab->shape == QTabBar::TriangularWest; - - QRect tr = tab->rect; - if (tab->shape == QTabBar::RoundedSouth || tab->shape == QTabBar::TriangularSouth) - verticalShift = -verticalShift; - if (verticalTabs) { - qSwap(horizontalShift, verticalShift); - horizontalShift *= -1; - verticalShift *= -1; - } - if (tab->shape == QTabBar::RoundedWest || tab->shape == QTabBar::TriangularWest) - horizontalShift = -horizontalShift; - - tr.adjust(0, 0, horizontalShift, verticalShift); - if (selected) - { - tr.setBottom(tr.bottom() - verticalShift); - tr.setRight(tr.right() - horizontalShift); - } - - QSize size = (sr == SE_TabBarTabLeftButton) ? tab->leftButtonSize : tab->rightButtonSize; - int w = size.width(); - int h = size.height(); - int midHeight = static_cast(qCeil(float(tr.height() - h) / 2)); - int midWidth = ((tr.width() - w) / 2); - - bool atTheTop = true; - switch (tab->shape) { - case QTabBar::RoundedWest: - case QTabBar::TriangularWest: - atTheTop = (sr == SE_TabBarTabLeftButton); - break; - case QTabBar::RoundedEast: - case QTabBar::TriangularEast: - atTheTop = (sr == SE_TabBarTabRightButton); - break; - default: - if (sr == SE_TabBarTabLeftButton) - rect = QRect(tab->rect.x() + hpadding, midHeight, w, h); - else - rect = QRect(tab->rect.right() - w - hpadding, midHeight, w, h); - rect = visualRect(tab->direction, tab->rect, rect); - } - if (verticalTabs) { - if (atTheTop) - rect = QRect(midWidth, tr.y() + tab->rect.height() - hpadding - h, w, h); - else - rect = QRect(midWidth, tr.y() + hpadding, w, h); - } - } - break; -#endif - case SE_LineEditContents: - rect = QCommonStyle::subElementRect(sr, opt, widget); -#if QT_CONFIG(combobox) - if (widget && qobject_cast(widget->parentWidget())) - rect.adjust(-1, -2, 0, 0); - else -#endif - rect.adjust(-1, -1, 0, +1); - break; - case SE_CheckBoxLayoutItem: - rect = opt->rect; - if (controlSize == QStyleHelper::SizeLarge) { - setLayoutItemMargins(+2, +3, -9, -4, &rect, opt->direction); - } else if (controlSize == QStyleHelper::SizeSmall) { - setLayoutItemMargins(+1, +5, 0 /* fix */, -6, &rect, opt->direction); - } else { - setLayoutItemMargins(0, +7, 0 /* fix */, -6, &rect, opt->direction); - } - break; - case SE_ComboBoxLayoutItem: -#ifndef QT_NO_TOOLBAR - if (widget && qobject_cast(widget->parentWidget())) { - // Do nothing, because QToolbar needs the entire widget rect. - // Otherwise it will be clipped. Equivalent to - // widget->setAttribute(Qt::WA_LayoutUsesWidgetRect), but without - // all the hassle. - } else -#endif - { - rect = opt->rect; - if (controlSize == QStyleHelper::SizeLarge) { - rect.adjust(+3, +2, -3, -4); - } else if (controlSize == QStyleHelper::SizeSmall) { - setLayoutItemMargins(+2, +1, -3, -4, &rect, opt->direction); - } else { - setLayoutItemMargins(+1, 0, -2, 0, &rect, opt->direction); - } - } - break; - case SE_LabelLayoutItem: - rect = opt->rect; - setLayoutItemMargins(+1, 0 /* SHOULD be -1, done for alignment */, 0, 0 /* SHOULD be -1, done for alignment */, &rect, opt->direction); - break; - case SE_ProgressBarLayoutItem: { - rect = opt->rect; - int bottom = SIZE(3, 8, 8); - if (opt->state & State_Horizontal) { - rect.adjust(0, +1, 0, -bottom); - } else { - setLayoutItemMargins(+1, 0, -bottom, 0, &rect, opt->direction); - } - break; - } - case SE_PushButtonLayoutItem: - if (const QStyleOptionButton *buttonOpt - = qstyleoption_cast(opt)) { - if ((buttonOpt->features & QStyleOptionButton::Flat)) - break; // leave rect alone - } - rect = opt->rect; - if (controlSize == QStyleHelper::SizeLarge) { - rect.adjust(+6, +4, -6, -8); - } else if (controlSize == QStyleHelper::SizeSmall) { - rect.adjust(+5, +4, -5, -6); - } else { - rect.adjust(+1, 0, -1, -2); - } - break; - case SE_RadioButtonLayoutItem: - rect = opt->rect; - if (controlSize == QStyleHelper::SizeLarge) { - setLayoutItemMargins(+2, +2 /* SHOULD BE +3, done for alignment */, - 0, -4 /* SHOULD BE -3, done for alignment */, &rect, opt->direction); - } else if (controlSize == QStyleHelper::SizeSmall) { - rect.adjust(0, +6, 0 /* fix */, -5); - } else { - rect.adjust(0, +6, 0 /* fix */, -7); - } - break; - case SE_SliderLayoutItem: - if (const QStyleOptionSlider *sliderOpt - = qstyleoption_cast(opt)) { - rect = opt->rect; - if (sliderOpt->tickPosition == QSlider::NoTicks) { - int above = SIZE(3, 0, 2); - int below = SIZE(4, 3, 0); - if (sliderOpt->orientation == Qt::Horizontal) { - rect.adjust(0, +above, 0, -below); - } else { - rect.adjust(+above, 0, -below, 0); //### Seems that QSlider flip the position of the ticks in reverse mode. - } - } else if (sliderOpt->tickPosition == QSlider::TicksAbove) { - int below = SIZE(3, 2, 0); - if (sliderOpt->orientation == Qt::Horizontal) { - rect.setHeight(rect.height() - below); - } else { - rect.setWidth(rect.width() - below); - } - } else if (sliderOpt->tickPosition == QSlider::TicksBelow) { - int above = SIZE(3, 2, 0); - if (sliderOpt->orientation == Qt::Horizontal) { - rect.setTop(rect.top() + above); - } else { - rect.setLeft(rect.left() + above); - } - } - } - break; - case SE_FrameLayoutItem: - // hack because QStyleOptionFrame doesn't have a frameStyle member - if (const QFrame *frame = qobject_cast(widget)) { - rect = opt->rect; - switch (frame->frameStyle() & QFrame::Shape_Mask) { - case QFrame::HLine: - rect.adjust(0, +1, 0, -1); - break; - case QFrame::VLine: - rect.adjust(+1, 0, -1, 0); - break; - default: - ; - } - } - break; - case SE_GroupBoxLayoutItem: - rect = opt->rect; - if (const QStyleOptionGroupBox *groupBoxOpt = - qstyleoption_cast(opt)) { - /* - AHIG is very inconsistent when it comes to group boxes. - Basically, we make sure that (non-checkable) group boxes - and tab widgets look good when laid out side by side. - */ - if (groupBoxOpt->subControls & (QStyle::SC_GroupBoxCheckBox - | QStyle::SC_GroupBoxLabel)) { - int delta; - if (groupBoxOpt->subControls & QStyle::SC_GroupBoxCheckBox) { - delta = SIZE(8, 4, 4); // guess - } else { - delta = SIZE(15, 12, 12); // guess - } - rect.setTop(rect.top() + delta); - } - } - rect.setBottom(rect.bottom() - 1); - break; -#if QT_CONFIG(tabwidget) - case SE_TabWidgetLayoutItem: - if (const QStyleOptionTabWidgetFrame *tabWidgetOpt = - qstyleoption_cast(opt)) { - /* - AHIG specifies "12 or 14" as the distance from the window - edge. We choose 14 and since the default top margin is 20, - the overlap is 6. - */ - rect = tabWidgetOpt->rect; - if (tabWidgetOpt->shape == QTabBar::RoundedNorth) - rect.setTop(rect.top() + SIZE(6 /* AHIG */, 3 /* guess */, 2 /* AHIG */)); - } - break; -#endif -#if QT_CONFIG(dockwidget) - case SE_DockWidgetCloseButton: - case SE_DockWidgetFloatButton: - case SE_DockWidgetTitleBarText: - case SE_DockWidgetIcon: { - int iconSize = proxy()->pixelMetric(PM_SmallIconSize, opt, widget); - int buttonMargin = proxy()->pixelMetric(PM_DockWidgetTitleBarButtonMargin, opt, widget); - QRect srect = opt->rect; - - const QStyleOptionDockWidget *dwOpt - = qstyleoption_cast(opt); - bool canClose = dwOpt == 0 ? true : dwOpt->closable; - bool canFloat = dwOpt == 0 ? false : dwOpt->floatable; - - const bool verticalTitleBar = dwOpt->verticalTitleBar; - - // If this is a vertical titlebar, we transpose and work as if it was - // horizontal, then transpose again. - if (verticalTitleBar) - srect = srect.transposed(); - - do { - int right = srect.right(); - int left = srect.left(); - - QRect closeRect; - if (canClose) { - QSize sz = proxy()->standardIcon(QStyle::SP_TitleBarCloseButton, - opt, widget).actualSize(QSize(iconSize, iconSize)); - sz += QSize(buttonMargin, buttonMargin); - if (verticalTitleBar) - sz = sz.transposed(); - closeRect = QRect(left, - srect.center().y() - sz.height()/2, - sz.width(), sz.height()); - left = closeRect.right() + 1; - } - if (sr == SE_DockWidgetCloseButton) { - rect = closeRect; - break; - } - - QRect floatRect; - if (canFloat) { - QSize sz = proxy()->standardIcon(QStyle::SP_TitleBarNormalButton, - opt, widget).actualSize(QSize(iconSize, iconSize)); - sz += QSize(buttonMargin, buttonMargin); - if (verticalTitleBar) - sz = sz.transposed(); - floatRect = QRect(left, - srect.center().y() - sz.height()/2, - sz.width(), sz.height()); - left = floatRect.right() + 1; - } - if (sr == SE_DockWidgetFloatButton) { - rect = floatRect; - break; - } - - QRect iconRect; - if (const QDockWidget *dw = qobject_cast(widget)) { - QIcon icon; - if (dw->isFloating()) - icon = dw->windowIcon(); - if (!icon.isNull() - && icon.cacheKey() != QApplication::windowIcon().cacheKey()) { - QSize sz = icon.actualSize(QSize(rect.height(), rect.height())); - if (verticalTitleBar) - sz = sz.transposed(); - iconRect = QRect(right - sz.width(), srect.center().y() - sz.height()/2, - sz.width(), sz.height()); - right = iconRect.left() - 1; - } - } - if (sr == SE_DockWidgetIcon) { - rect = iconRect; - break; - } - - QRect textRect = QRect(left, srect.top(), - right - left, srect.height()); - if (sr == SE_DockWidgetTitleBarText) { - rect = textRect; - break; - } - } while (false); - - if (verticalTitleBar) { - rect = QRect(srect.left() + rect.top() - srect.top(), - srect.top() + srect.right() - rect.right(), - rect.height(), rect.width()); - } else { - rect = visualRect(opt->direction, srect, rect); - } - break; - } -#endif - default: - rect = QCommonStyle::subElementRect(sr, opt, widget); - break; - } - return rect; -} - -void QMacStylePrivate::drawToolbarButtonArrow(const QStyleOption *opt, QPainter *p) const -{ - Q_Q(const QMacStyle); - QStyleOption arrowOpt = *opt; - arrowOpt.rect = QRect(opt->rect.right() - (toolButtonArrowSize + toolButtonArrowMargin), - opt->rect.bottom() - (toolButtonArrowSize + toolButtonArrowMargin), - toolButtonArrowSize, - toolButtonArrowSize); - q->proxy()->drawPrimitive(QStyle::PE_IndicatorArrowDown, &arrowOpt, p); -} - -void QMacStylePrivate::setupNSGraphicsContext(CGContextRef cg, bool flipped) const -{ - CGContextSaveGState(cg); - [NSGraphicsContext saveGraphicsState]; - - [NSGraphicsContext setCurrentContext: - [NSGraphicsContext graphicsContextWithCGContext:cg flipped:flipped]]; -} - -void QMacStylePrivate::restoreNSGraphicsContext(CGContextRef cg) const -{ - [NSGraphicsContext restoreGraphicsState]; - CGContextRestoreGState(cg); -} - -void QMacStyle::drawComplexControl(ComplexControl cc, const QStyleOptionComplex *opt, QPainter *p, - const QWidget *widget) const -{ - Q_D(const QMacStyle); - QMacCGContext cg(p); - QWindow *window = widget && widget->window() ? widget->window()->windowHandle() : nullptr; - d->resolveCurrentNSView(window); - switch (cc) { - case CC_ScrollBar: - if (const QStyleOptionSlider *sb = qstyleoption_cast(opt)) { - - const bool drawTrack = sb->subControls & SC_ScrollBarGroove; - const bool drawKnob = sb->subControls & SC_ScrollBarSlider; - if (!drawTrack && !drawKnob) - break; - - const bool isHorizontal = sb->orientation == Qt::Horizontal; - - if (opt && opt->styleObject && !QMacStylePrivate::scrollBars.contains(opt->styleObject)) - QMacStylePrivate::scrollBars.append(QPointer(opt->styleObject)); - - static const CGFloat knobWidths[] = { 7.0, 5.0, 5.0 }; - static const CGFloat expandedKnobWidths[] = { 11.0, 9.0, 9.0 }; - const auto cocoaSize = d->effectiveAquaSizeConstrain(opt, widget); - const CGFloat maxExpandScale = expandedKnobWidths[cocoaSize] / knobWidths[cocoaSize]; - - const bool isTransient = proxy()->styleHint(SH_ScrollBar_Transient, opt, widget); - if (!isTransient) - d->stopAnimation(opt->styleObject); - bool wasActive = false; - CGFloat opacity = 0.0; - CGFloat expandScale = 1.0; - CGFloat expandOffset = 0.0; - bool shouldExpand = false; - - if (QObject *styleObject = opt->styleObject) { - const int oldPos = styleObject->property("_q_stylepos").toInt(); - const int oldMin = styleObject->property("_q_stylemin").toInt(); - const int oldMax = styleObject->property("_q_stylemax").toInt(); - const QRect oldRect = styleObject->property("_q_stylerect").toRect(); - const QStyle::State oldState = static_cast(styleObject->property("_q_stylestate").value()); - const uint oldActiveControls = styleObject->property("_q_stylecontrols").toUInt(); - - // a scrollbar is transient when the scrollbar itself and - // its sibling are both inactive (ie. not pressed/hovered/moved) - const bool transient = isTransient && !opt->activeSubControls && !(sb->state & State_On); - - if (!transient || - oldPos != sb->sliderPosition || - oldMin != sb->minimum || - oldMax != sb->maximum || - oldRect != sb->rect || - oldState != sb->state || - oldActiveControls != sb->activeSubControls) { - - // if the scrollbar is transient or its attributes, geometry or - // state has changed, the opacity is reset back to 100% opaque - opacity = 1.0; - - styleObject->setProperty("_q_stylepos", sb->sliderPosition); - styleObject->setProperty("_q_stylemin", sb->minimum); - styleObject->setProperty("_q_stylemax", sb->maximum); - styleObject->setProperty("_q_stylerect", sb->rect); - styleObject->setProperty("_q_stylestate", static_cast(sb->state)); - styleObject->setProperty("_q_stylecontrols", static_cast(sb->activeSubControls)); - - QScrollbarStyleAnimation *anim = qobject_cast(d->animation(styleObject)); - if (transient) { - if (!anim) { - anim = new QScrollbarStyleAnimation(QScrollbarStyleAnimation::Deactivating, styleObject); - d->startAnimation(anim); - } else if (anim->mode() == QScrollbarStyleAnimation::Deactivating) { - // the scrollbar was already fading out while the - // state changed -> restart the fade out animation - anim->setCurrentTime(0); - } - } else if (anim && anim->mode() == QScrollbarStyleAnimation::Deactivating) { - d->stopAnimation(styleObject); - } - } - - QScrollbarStyleAnimation *anim = qobject_cast(d->animation(styleObject)); - if (anim && anim->mode() == QScrollbarStyleAnimation::Deactivating) { - // once a scrollbar was active (hovered/pressed), it retains - // the active look even if it's no longer active while fading out - if (oldActiveControls) - anim->setActive(true); - - wasActive = anim->wasActive(); - opacity = anim->currentValue(); - } - - shouldExpand = isTransient && (opt->activeSubControls || wasActive); - if (shouldExpand) { - if (!anim && !oldActiveControls) { - // Start expand animation only once and when entering - anim = new QScrollbarStyleAnimation(QScrollbarStyleAnimation::Activating, styleObject); - d->startAnimation(anim); - } - if (anim && anim->mode() == QScrollbarStyleAnimation::Activating) { - expandScale = 1.0 + (maxExpandScale - 1.0) * anim->currentValue(); - expandOffset = 5.5 * (1.0 - anim->currentValue()); - } else { - // Keep expanded state after the animation ends, and when fading out - expandScale = maxExpandScale; - expandOffset = 0.0; - } - } - } - - d->setupNSGraphicsContext(cg, NO /* flipped */); - - const auto controlType = isHorizontal ? QMacStylePrivate::Scroller_Horizontal : QMacStylePrivate::Scroller_Vertical; - const auto cw = QMacStylePrivate::CocoaControl(controlType, cocoaSize); - NSScroller *scroller = static_cast(d->cocoaControl(cw)); - - const QColor bgColor = QStyleHelper::backgroundColor(opt->palette, widget); - const bool hasDarkBg = bgColor.red() < 128 && bgColor.green() < 128 && bgColor.blue() < 128; - if (isTransient) { - // macOS behavior: as soon as one color channel is >= 128, - // the background is considered bright, scroller is dark. - scroller.knobStyle = hasDarkBg? NSScrollerKnobStyleLight : NSScrollerKnobStyleDark; - } else { - scroller.knobStyle = NSScrollerKnobStyleDefault; - } - - scroller.scrollerStyle = isTransient ? NSScrollerStyleOverlay : NSScrollerStyleLegacy; - - if (!setupScroller(scroller, sb)) - break; - - if (isTransient) { - CGContextBeginTransparencyLayerWithRect(cg, scroller.frame, nullptr); - CGContextSetAlpha(cg, opacity); - } - - if (drawTrack) { - // Draw the track when hovering. Expand by shifting the track rect. - if (!isTransient || opt->activeSubControls || wasActive) { - CGRect trackRect = scroller.bounds; - if (isHorizontal) - trackRect.origin.y += expandOffset; - else - trackRect.origin.x += expandOffset; - [scroller drawKnobSlotInRect:trackRect highlight:NO]; - } - } - - if (drawKnob) { - if (shouldExpand) { - // -[NSScroller drawKnob] is not useful here because any scaling applied - // will only be used to draw the hi-DPI artwork. And even if did scale, - // the stretched knob would look wrong, actually. So we need to draw the - // scroller manually when it's being hovered. - const CGFloat scrollerWidth = [NSScroller scrollerWidthForControlSize:scroller.controlSize scrollerStyle:scroller.scrollerStyle]; - const CGFloat knobWidth = knobWidths[cocoaSize] * expandScale; - // Cocoa can help get the exact knob length in the current orientation - const CGRect scrollerKnobRect = CGRectInset([scroller rectForPart:NSScrollerKnob], 1, 1); - const CGFloat knobLength = isHorizontal ? scrollerKnobRect.size.width : scrollerKnobRect.size.height; - const CGFloat knobPos = isHorizontal ? scrollerKnobRect.origin.x : scrollerKnobRect.origin.y; - const CGFloat knobOffset = qRound((scrollerWidth + expandOffset - knobWidth) / 2.0); - const CGFloat knobRadius = knobWidth / 2.0; - CGRect knobRect; - if (isHorizontal) - knobRect = CGRectMake(knobPos, knobOffset, knobLength, knobWidth); - else - knobRect = CGRectMake(knobOffset, knobPos, knobWidth, knobLength); - QCFType knobPath = CGPathCreateWithRoundedRect(knobRect, knobRadius, knobRadius, nullptr); - CGContextAddPath(cg, knobPath); - CGContextSetAlpha(cg, 0.5); - CGColorRef knobColor = hasDarkBg ? NSColor.whiteColor.CGColor : NSColor.blackColor.CGColor; - CGContextSetFillColorWithColor(cg, knobColor); - CGContextFillPath(cg); - } else { - [scroller drawKnob]; - - if (!isTransient && opt->activeSubControls) { - // The knob should appear darker (going from 0.76 down to 0.49). - // But no blending mode can help darken enough in a single pass, - // so we resort to drawing the knob twice with a small help from - // blending. This brings the gray level to a close enough 0.53. - CGContextSetBlendMode(cg, kCGBlendModePlusDarker); - [scroller drawKnob]; - } - } - } - - if (isTransient) - CGContextEndTransparencyLayer(cg); - - d->restoreNSGraphicsContext(cg); - } - break; - case CC_Slider: - if (const QStyleOptionSlider *sl = qstyleoption_cast(opt)) { - const bool isHorizontal = sl->orientation == Qt::Horizontal; - const auto ct = isHorizontal ? QMacStylePrivate::Slider_Horizontal : QMacStylePrivate::Slider_Vertical; - const auto cs = d->effectiveAquaSizeConstrain(opt, widget); - const auto cw = QMacStylePrivate::CocoaControl(ct, cs); - auto *slider = static_cast(d->cocoaControl(cw)); - if (!setupSlider(slider, sl)) - break; - - const bool hasTicks = sl->tickPosition != QSlider::NoTicks; - const bool hasDoubleTicks = sl->tickPosition == QSlider::TicksBothSides; - const bool drawKnob = sl->subControls & SC_SliderHandle; - const bool drawBar = sl->subControls & SC_SliderGroove; - const bool drawTicks = sl->subControls & SC_SliderTickmarks; - const bool isPressed = sl->state & State_Sunken; - - CGPoint pressPoint; - if (isPressed) { - const CGRect knobRect = [slider.cell knobRectFlipped:NO]; - pressPoint.x = CGRectGetMidX(knobRect); - pressPoint.y = CGRectGetMidY(knobRect); - [slider.cell startTrackingAt:pressPoint inView:slider]; - } - - d->drawNSViewInRect(slider, opt->rect, p, ^(CGContextRef ctx, const CGRect &rect) { - if (isHorizontal && sl->upsideDown) { - CGContextTranslateCTM(ctx, rect.size.width, 0); - CGContextScaleCTM(ctx, -1, 1); - } - - if (hasDoubleTicks) { - // This ain't HIG kosher: eye-proved constants - if (isHorizontal) - CGContextTranslateCTM(ctx, 0, 4); - else - CGContextTranslateCTM(ctx, 1, 0); - } - - // Since the GC is flipped, upsideDown means *not* inverted when vertical. - const bool verticalFlip = !isHorizontal && !sl->upsideDown; // FIXME: && !isSierraOrLater - -#if 0 - // FIXME: Sadly, this part doesn't work. It seems to somehow polute the - // NSSlider's internal state and, when we need to use the "else" part, - // the slider's frame is not in sync with its cell dimensions. - const bool drawAllParts = drawKnob && drawBar && (!hasTicks || drawTicks); - if (drawAllParts && !hasDoubleTicks && (!verticalFlip || drawTicks)) { - // Draw eveything at once if we're going to, except for inverted vertical - // sliders which need to be drawn part by part because of the shadow below - // the knob. Same for two-sided tickmarks. - if (verticalFlip && drawTicks) { - // Since tickmarks are always rendered symmetrically, a vertically - // flipped slider with tickmarks only needs to get its value flipped. - slider.intValue = slider.maxValue - slider.intValue + slider.minValue; - } - [slider drawRect:CGRectZero]; - } else -#endif - { - [slider calcSize]; - NSSliderCell *cell = slider.cell; - - const int numberOfTickMarks = slider.numberOfTickMarks; - // This ain't HIG kosher: force tick-less bar position. - if (hasDoubleTicks) - slider.numberOfTickMarks = 0; - - const CGRect barRect = [cell barRectFlipped:hasTicks]; - if (drawBar) { - // This ain't HIG kosher: force unfilled bar look. - if (hasDoubleTicks) - slider.numberOfTickMarks = numberOfTickMarks; - [cell drawBarInside:barRect flipped:!verticalFlip]; - } - - if (hasTicks && drawTicks) { - if (!drawBar && hasDoubleTicks) - slider.numberOfTickMarks = numberOfTickMarks; - - [cell drawTickMarks]; - - if (hasDoubleTicks) { - // This ain't HIG kosher: just slap a set of tickmarks on each side, like we used to. - CGAffineTransform tickMarksFlip; - const CGRect tickMarkRect = [cell rectOfTickMarkAtIndex:0]; - if (isHorizontal) { - tickMarksFlip = CGAffineTransformMakeTranslation(0, rect.size.height - tickMarkRect.size.height - 3); - tickMarksFlip = CGAffineTransformScale(tickMarksFlip, 1, -1); - } else { - tickMarksFlip = CGAffineTransformMakeTranslation(rect.size.width - tickMarkRect.size.width / 2, 0); - tickMarksFlip = CGAffineTransformScale(tickMarksFlip, -1, 1); - } - CGContextConcatCTM(ctx, tickMarksFlip); - [cell drawTickMarks]; - CGContextConcatCTM(ctx, CGAffineTransformInvert(tickMarksFlip)); - } - } - - if (drawKnob) { - // This ain't HIG kosher: force round knob look. - if (hasDoubleTicks) - slider.numberOfTickMarks = 0; - // Draw the knob in the symmetrical position instead of flipping. - if (verticalFlip) - slider.intValue = slider.maxValue - slider.intValue + slider.minValue; - [cell drawKnob]; - } - } - }); - - if (isPressed) - [slider.cell stopTracking:pressPoint at:pressPoint inView:slider mouseIsUp:NO]; - } - break; -#if QT_CONFIG(spinbox) - case CC_SpinBox: - if (const QStyleOptionSpinBox *sb = qstyleoption_cast(opt)) { - if (sb->frame && (sb->subControls & SC_SpinBoxFrame)) { - const auto lineEditRect = proxy()->subControlRect(CC_SpinBox, sb, SC_SpinBoxEditField, widget); - QStyleOptionFrame frame; - static_cast(frame) = *opt; - frame.rect = lineEditRect; - frame.state |= State_Sunken; - frame.lineWidth = 1; - frame.midLineWidth = 0; - frame.features = QStyleOptionFrame::None; - frame.frameShape = QFrame::Box; - drawPrimitive(PE_FrameLineEdit, &frame, p, widget); - } - if (sb->subControls & (SC_SpinBoxUp | SC_SpinBoxDown)) { - const QRect updown = proxy()->subControlRect(CC_SpinBox, sb, SC_SpinBoxUp, widget) - | proxy()->subControlRect(CC_SpinBox, sb, SC_SpinBoxDown, widget); - - d->setupNSGraphicsContext(cg, NO); - - const auto aquaSize = d->effectiveAquaSizeConstrain(opt, widget); - const auto cw = QMacStylePrivate::CocoaControl(QMacStylePrivate::Stepper, aquaSize); - NSStepperCell *cell = static_cast(d->cocoaCell(cw)); - cell.enabled = (sb->state & State_Enabled); - - const CGRect newRect = [cell drawingRectForBounds:updown.toCGRect()]; - - const bool upPressed = sb->activeSubControls == SC_SpinBoxUp && (sb->state & State_Sunken); - const bool downPressed = sb->activeSubControls == SC_SpinBoxDown && (sb->state & State_Sunken); - const CGFloat x = CGRectGetMidX(newRect); - const CGFloat y = upPressed ? -3 : 3; // Weird coordinate shift going on. Verified with Hopper - const CGPoint pressPoint = CGPointMake(x, y); - // Pretend we're pressing the mouse on the right button. Unfortunately, NSStepperCell has no - // API to highlight a specific button. The highlighted property works only on the down button. - if (upPressed || downPressed) - [cell startTrackingAt:pressPoint inView:d->backingStoreNSView]; - - [cell drawWithFrame:newRect inView:d->backingStoreNSView]; - - if (upPressed || downPressed) - [cell stopTracking:pressPoint at:pressPoint inView:d->backingStoreNSView mouseIsUp:NO]; - - d->restoreNSGraphicsContext(cg); - } - } - break; -#endif -#if QT_CONFIG(combobox) - case CC_ComboBox: - if (const auto *combo = qstyleoption_cast(opt)) { - const bool isEnabled = combo->state & State_Enabled; - const bool isPressed = combo->state & State_Sunken; - - const auto ct = cocoaControlType(combo, widget); - const auto cs = d->effectiveAquaSizeConstrain(combo, widget); - const auto cw = QMacStylePrivate::CocoaControl(ct, cs); - auto *cc = static_cast(d->cocoaControl(cw)); - cc.enabled = isEnabled; - QRectF frameRect = cw.adjustedControlFrame(combo->rect);; - if (cw.type == QMacStylePrivate::Button_PopupButton) { - // Non-editable QComboBox - auto *pb = static_cast(cc); - // FIXME Old offsets. Try to move to adjustedControlFrame() - if (cw.size == QStyleHelper::SizeSmall) { - frameRect = frameRect.translated(0, 1); - } else if (cw.size == QStyleHelper::SizeMini) { - // Same 0.5 pt misalignment as AppKit and fit the focus ring - frameRect = frameRect.translated(2, -0.5); - } - pb.frame = frameRect.toCGRect(); - [pb highlight:isPressed]; - d->drawNSViewInRect(pb, frameRect, p, ^(CGContextRef, const CGRect &r) { - [pb.cell drawBezelWithFrame:r inView:pb.superview]; - }); - } else if (cw.type == QMacStylePrivate::ComboBox) { - // Editable QComboBox - auto *cb = static_cast(cc); - const auto frameRect = cw.adjustedControlFrame(combo->rect); - cb.frame = frameRect.toCGRect(); - - // This API was requested to Apple in rdar #36197888. We know it's safe to use up to macOS 10.13.3 - if (NSButtonCell *cell = static_cast([cc.cell qt_valueForPrivateKey:@"_buttonCell"])) { - cell.highlighted = isPressed; - } else { - // TODO Render to pixmap and darken the button manually - } - - d->drawNSViewInRect(cb, frameRect, p, ^(CGContextRef, const CGRect &r) { - // FIXME This is usually drawn in the control's superview, but we wouldn't get inactive look in this case - [cb.cell drawWithFrame:r inView:cb]; - }); - } - - if (combo->state & State_HasFocus) { - // TODO Remove and use QFocusFrame instead. - const int hMargin = proxy()->pixelMetric(QStyle::PM_FocusFrameHMargin, combo, widget); - const int vMargin = proxy()->pixelMetric(QStyle::PM_FocusFrameVMargin, combo, widget); - QRectF focusRect; - if (cw.type == QMacStylePrivate::Button_PopupButton) { - focusRect = QRectF::fromCGRect([cc alignmentRectForFrame:cc.frame]); - focusRect -= pullDownButtonShadowMargins[cw.size]; - if (cw.size == QStyleHelper::SizeSmall) - focusRect = focusRect.translated(0, 1); - else if (cw.size == QStyleHelper::SizeMini) - focusRect = focusRect.translated(2, -1); - } else if (cw.type == QMacStylePrivate::ComboBox) { - focusRect = frameRect - comboBoxFocusRingMargins[cw.size]; - } - d->drawFocusRing(p, focusRect, hMargin, vMargin, cw); - } - } - break; -#endif // QT_CONFIG(combobox) - case CC_TitleBar: - if (const auto *titlebar = qstyleoption_cast(opt)) { - const bool isActive = (titlebar->state & State_Active) - && (titlebar->titleBarState & State_Active); - - p->fillRect(opt->rect, Qt::transparent); - p->setRenderHint(QPainter::Antialiasing); - p->setClipRect(opt->rect, Qt::IntersectClip); - - // FIXME A single drawPath() with 0-sized pen - // doesn't look as good as this double fillPath(). - const auto outerFrameRect = QRectF(opt->rect.adjusted(0, 0, 0, opt->rect.height())); - QPainterPath outerFramePath = d->windowPanelPath(outerFrameRect); - p->fillPath(outerFramePath, opt->palette.dark()); - - const auto frameAdjust = 1.0 / p->device()->devicePixelRatioF(); - const auto innerFrameRect = outerFrameRect.adjusted(frameAdjust, frameAdjust, -frameAdjust, 0); - QPainterPath innerFramePath = d->windowPanelPath(innerFrameRect); - if (isActive) { - QLinearGradient g; - g.setStart(QPointF(0, 0)); - g.setFinalStop(QPointF(0, 2 * opt->rect.height())); - g.setColorAt(0, opt->palette.button().color()); - g.setColorAt(1, opt->palette.dark().color()); - p->fillPath(innerFramePath, g); - } else { - p->fillPath(innerFramePath, opt->palette.button()); - } - - if (titlebar->subControls & (SC_TitleBarCloseButton - | SC_TitleBarMaxButton - | SC_TitleBarMinButton - | SC_TitleBarNormalButton)) { - const bool isHovered = (titlebar->state & State_MouseOver); - static const SubControl buttons[] = { - SC_TitleBarCloseButton, SC_TitleBarMinButton, SC_TitleBarMaxButton - }; - for (const auto sc : buttons) { - const auto ct = d->windowButtonCocoaControl(sc); - const auto cw = QMacStylePrivate::CocoaControl(ct, QStyleHelper::SizeLarge); - auto *wb = static_cast(d->cocoaControl(cw)); - wb.enabled = (sc & titlebar->subControls); - [wb highlight:(titlebar->state & State_Sunken) && (sc & titlebar->activeSubControls)]; - Q_UNUSED(isHovered); // FIXME No public API for this - - const auto buttonRect = proxy()->subControlRect(CC_TitleBar, titlebar, sc, widget); - d->drawNSViewInRect(wb, buttonRect, p, ^(CGContextRef, const CGRect &rect) { - auto *wbCell = static_cast(wb.cell); - [wbCell drawWithFrame:rect inView:wb]; - }); - } - } - - if (titlebar->subControls & SC_TitleBarLabel) { - const auto tr = proxy()->subControlRect(CC_TitleBar, titlebar, SC_TitleBarLabel, widget); - if (!titlebar->icon.isNull()) { - const auto iconExtent = proxy()->pixelMetric(PM_SmallIconSize); - const auto iconSize = QSize(iconExtent, iconExtent); - const auto iconPos = tr.x() - titlebar->icon.actualSize(iconSize).width() - qRound(titleBarIconTitleSpacing); - // Only render the icon if it'll be fully visible - if (iconPos < tr.right() - titleBarIconTitleSpacing) - p->drawPixmap(iconPos, tr.y(), titlebar->icon.pixmap(window, iconSize, QIcon::Normal)); - } - - if (!titlebar->text.isEmpty()) - drawItemText(p, tr, Qt::AlignCenter, opt->palette, isActive, titlebar->text, QPalette::Text); - } - } - break; - case CC_GroupBox: - if (const QStyleOptionGroupBox *gb - = qstyleoption_cast(opt)) { - - QStyleOptionGroupBox groupBox(*gb); - const bool flat = groupBox.features & QStyleOptionFrame::Flat; - if (!flat) - groupBox.state |= QStyle::State_Mini; // Force mini-sized checkbox to go with small-sized label - else - groupBox.subControls = groupBox.subControls & ~SC_GroupBoxFrame; // We don't like frames and ugly lines - - const bool didSetFont = widget && widget->testAttribute(Qt::WA_SetFont); - const bool didModifySubControls = !didSetFont && QApplication::desktopSettingsAware(); - if (didModifySubControls) - groupBox.subControls = groupBox.subControls & ~SC_GroupBoxLabel; - QCommonStyle::drawComplexControl(cc, &groupBox, p, widget); - if (didModifySubControls) { - const QRect rect = proxy()->subControlRect(CC_GroupBox, &groupBox, SC_GroupBoxLabel, widget); - const bool rtl = groupBox.direction == Qt::RightToLeft; - const int alignment = Qt::TextHideMnemonic | (rtl ? Qt::AlignRight : Qt::AlignLeft); - const QFont savedFont = p->font(); - if (!flat) - p->setFont(d->smallSystemFont); - proxy()->drawItemText(p, rect, alignment, groupBox.palette, groupBox.state & State_Enabled, groupBox.text, QPalette::WindowText); - if (!flat) - p->setFont(savedFont); - } - } - break; - case CC_ToolButton: - if (const QStyleOptionToolButton *tb - = qstyleoption_cast(opt)) { -#ifndef QT_NO_ACCESSIBILITY - if (QStyleHelper::hasAncestor(opt->styleObject, QAccessible::ToolBar)) { - if (tb->subControls & SC_ToolButtonMenu) { - QStyleOption arrowOpt = *tb; - arrowOpt.rect = proxy()->subControlRect(cc, tb, SC_ToolButtonMenu, widget); - arrowOpt.rect.setY(arrowOpt.rect.y() + arrowOpt.rect.height() / 2); - arrowOpt.rect.setHeight(arrowOpt.rect.height() / 2); - proxy()->drawPrimitive(PE_IndicatorArrowDown, &arrowOpt, p, widget); - } else if ((tb->features & QStyleOptionToolButton::HasMenu) - && (tb->toolButtonStyle != Qt::ToolButtonTextOnly && !tb->icon.isNull())) { - d->drawToolbarButtonArrow(tb, p); - } - if (tb->state & State_On) { - NSView *view = window ? (NSView *)window->winId() : nil; - bool isKey = false; - if (view) - isKey = [view.window isKeyWindow]; - - QBrush brush(isKey ? QColor(0, 0, 0, 28) - : QColor(0, 0, 0, 21)); - QPainterPath path; - path.addRoundedRect(QRectF(tb->rect.x(), tb->rect.y(), tb->rect.width(), tb->rect.height() + 4), 4, 4); - p->setRenderHint(QPainter::Antialiasing); - p->fillPath(path, brush); - } - proxy()->drawControl(CE_ToolButtonLabel, opt, p, widget); - } else -#endif // QT_NO_ACCESSIBILITY - { - auto bflags = tb->state; - if (tb->subControls & SC_ToolButton) - bflags |= State_Sunken; - auto mflags = tb->state; - if (tb->subControls & SC_ToolButtonMenu) - mflags |= State_Sunken; - - if (tb->subControls & SC_ToolButton) { - if (bflags & (State_Sunken | State_On | State_Raised)) { - const bool isEnabled = tb->state & State_Enabled; - const bool isPressed = tb->state & State_Sunken; - const bool isHighlighted = (tb->state & State_Active) && (tb->state & State_On); - const auto ct = QMacStylePrivate::Button_PushButton; - const auto cs = d->effectiveAquaSizeConstrain(opt, widget); - const auto cw = QMacStylePrivate::CocoaControl(ct, cs); - auto *pb = static_cast(d->cocoaControl(cw)); - pb.bezelStyle = NSShadowlessSquareBezelStyle; // TODO Use NSTexturedRoundedBezelStyle in the future. - pb.frame = opt->rect.toCGRect(); - pb.buttonType = NSPushOnPushOffButton; - pb.enabled = isEnabled; - [pb highlight:isPressed]; - pb.state = isHighlighted && !isPressed ? NSOnState : NSOffState; - const auto buttonRect = proxy()->subControlRect(cc, tb, SC_ToolButton, widget); - d->drawNSViewInRect(pb, buttonRect, p, ^(CGContextRef, const CGRect &rect) { - [pb.cell drawBezelWithFrame:rect inView:pb]; - }); - } - } - - if (tb->subControls & SC_ToolButtonMenu) { - const auto menuRect = proxy()->subControlRect(cc, tb, SC_ToolButtonMenu, widget); - QStyleOption arrowOpt = *tb; - arrowOpt.rect = QRect(menuRect.x() + ((menuRect.width() - toolButtonArrowSize) / 2), - menuRect.height() - (toolButtonArrowSize + toolButtonArrowMargin), - toolButtonArrowSize, - toolButtonArrowSize); - proxy()->drawPrimitive(PE_IndicatorArrowDown, &arrowOpt, p, widget); - } else if (tb->features & QStyleOptionToolButton::HasMenu) { - d->drawToolbarButtonArrow(tb, p); - } - QRect buttonRect = proxy()->subControlRect(CC_ToolButton, tb, SC_ToolButton, widget); - int fw = proxy()->pixelMetric(PM_DefaultFrameWidth, opt, widget); - QStyleOptionToolButton label = *tb; - label.rect = buttonRect.adjusted(fw, fw, -fw, -fw); - proxy()->drawControl(CE_ToolButtonLabel, &label, p, widget); - } - } - break; -#if QT_CONFIG(dial) - case CC_Dial: - if (const QStyleOptionSlider *dial = qstyleoption_cast(opt)) - QStyleHelper::drawDial(dial, p); - break; -#endif - default: - QCommonStyle::drawComplexControl(cc, opt, p, widget); - break; - } -} - -QStyle::SubControl QMacStyle::hitTestComplexControl(ComplexControl cc, - const QStyleOptionComplex *opt, - const QPoint &pt, const QWidget *widget) const -{ - Q_D(const QMacStyle); - SubControl sc = QStyle::SC_None; - switch (cc) { - case CC_ComboBox: - if (const QStyleOptionComboBox *cmb = qstyleoption_cast(opt)) { - sc = QCommonStyle::hitTestComplexControl(cc, cmb, pt, widget); - if (!cmb->editable && sc != QStyle::SC_None) - sc = SC_ComboBoxArrow; // A bit of a lie, but what we want - } - break; - case CC_Slider: - if (const QStyleOptionSlider *sl = qstyleoption_cast(opt)) { - if (!sl->rect.contains(pt)) - break; - - const bool hasTicks = sl->tickPosition != QSlider::NoTicks; - const bool isHorizontal = sl->orientation == Qt::Horizontal; - const auto ct = isHorizontal ? QMacStylePrivate::Slider_Horizontal : QMacStylePrivate::Slider_Vertical; - const auto cs = d->effectiveAquaSizeConstrain(opt, widget); - const auto cw = QMacStylePrivate::CocoaControl(ct, cs); - auto *slider = static_cast(d->cocoaControl(cw)); - if (!setupSlider(slider, sl)) - break; - - [slider calcSize]; - NSSliderCell *cell = slider.cell; - const auto barRect = QRectF::fromCGRect([cell barRectFlipped:hasTicks]); - const auto knobRect = QRectF::fromCGRect([cell knobRectFlipped:NO]); - if (knobRect.contains(pt)) { - sc = SC_SliderHandle; - } else if (barRect.contains(pt)) { - sc = SC_SliderGroove; - } else if (hasTicks) { - sc = SC_SliderTickmarks; - } - } - break; - case CC_ScrollBar: - if (const QStyleOptionSlider *sb = qstyleoption_cast(opt)) { - if (!sb->rect.contains(pt)) { - sc = SC_None; - break; - } - - const bool isHorizontal = sb->orientation == Qt::Horizontal; - const auto ct = isHorizontal ? QMacStylePrivate::Scroller_Horizontal : QMacStylePrivate::Scroller_Vertical; - const auto cs = d->effectiveAquaSizeConstrain(opt, widget); - const auto cw = QMacStylePrivate::CocoaControl(ct, cs); - auto *scroller = static_cast(d->cocoaControl(cw)); - if (!setupScroller(scroller, sb)) { - sc = SC_None; - break; - } - - // Since -[NSScroller testPart:] doesn't want to cooperate, we do it the - // straightforward way. In any case, macOS doesn't return line-sized changes - // with NSScroller since 10.7, according to the aforementioned method's doc. - const auto knobRect = QRectF::fromCGRect([scroller rectForPart:NSScrollerKnob]); - if (isHorizontal) { - const bool isReverse = sb->direction == Qt::RightToLeft; - if (pt.x() < knobRect.left()) - sc = isReverse ? SC_ScrollBarAddPage : SC_ScrollBarSubPage; - else if (pt.x() > knobRect.right()) - sc = isReverse ? SC_ScrollBarSubPage : SC_ScrollBarAddPage; - else - sc = SC_ScrollBarSlider; - } else { - if (pt.y() < knobRect.top()) - sc = SC_ScrollBarSubPage; - else if (pt.y() > knobRect.bottom()) - sc = SC_ScrollBarAddPage; - else - sc = SC_ScrollBarSlider; - } - } - break; - default: - sc = QCommonStyle::hitTestComplexControl(cc, opt, pt, widget); - break; - } - return sc; -} - -QRect QMacStyle::subControlRect(ComplexControl cc, const QStyleOptionComplex *opt, SubControl sc, - const QWidget *widget) const -{ - Q_D(const QMacStyle); - QRect ret; - switch (cc) { - case CC_ScrollBar: - if (const QStyleOptionSlider *sb = qstyleoption_cast(opt)) { - const bool isHorizontal = sb->orientation == Qt::Horizontal; - const bool isReverseHorizontal = isHorizontal && (sb->direction == Qt::RightToLeft); - - NSScrollerPart part = NSScrollerNoPart; - if (sc == SC_ScrollBarSlider) { - part = NSScrollerKnob; - } else if (sc == SC_ScrollBarGroove) { - part = NSScrollerKnobSlot; - } else if (sc == SC_ScrollBarSubPage || sc == SC_ScrollBarAddPage) { - if ((!isReverseHorizontal && sc == SC_ScrollBarSubPage) - || (isReverseHorizontal && sc == SC_ScrollBarAddPage)) - part = NSScrollerDecrementPage; - else - part = NSScrollerIncrementPage; - } - // And nothing else since 10.7 - - if (part != NSScrollerNoPart) { - const auto ct = isHorizontal ? QMacStylePrivate::Scroller_Horizontal : QMacStylePrivate::Scroller_Vertical; - const auto cs = d->effectiveAquaSizeConstrain(opt, widget); - const auto cw = QMacStylePrivate::CocoaControl(ct, cs); - auto *scroller = static_cast(d->cocoaControl(cw)); - if (setupScroller(scroller, sb)) - ret = QRectF::fromCGRect([scroller rectForPart:part]).toRect(); - } - } - break; - case CC_Slider: - if (const QStyleOptionSlider *sl = qstyleoption_cast(opt)) { - const bool hasTicks = sl->tickPosition != QSlider::NoTicks; - const bool isHorizontal = sl->orientation == Qt::Horizontal; - const auto ct = isHorizontal ? QMacStylePrivate::Slider_Horizontal : QMacStylePrivate::Slider_Vertical; - const auto cs = d->effectiveAquaSizeConstrain(opt, widget); - const auto cw = QMacStylePrivate::CocoaControl(ct, cs); - auto *slider = static_cast(d->cocoaControl(cw)); - if (!setupSlider(slider, sl)) - break; - - [slider calcSize]; - NSSliderCell *cell = slider.cell; - if (sc == SC_SliderHandle) { - ret = QRectF::fromCGRect([cell knobRectFlipped:NO]).toRect(); - if (isHorizontal) { - ret.setTop(sl->rect.top()); - ret.setBottom(sl->rect.bottom()); - } else { - ret.setLeft(sl->rect.left()); - ret.setRight(sl->rect.right()); - } - } else if (sc == SC_SliderGroove) { - ret = QRectF::fromCGRect([cell barRectFlipped:hasTicks]).toRect(); - } else if (hasTicks && sc == SC_SliderTickmarks) { - const auto tickMarkRect = QRectF::fromCGRect([cell rectOfTickMarkAtIndex:0]); - if (isHorizontal) - ret = QRect(sl->rect.left(), tickMarkRect.top(), sl->rect.width(), tickMarkRect.height()); - else - ret = QRect(tickMarkRect.left(), sl->rect.top(), tickMarkRect.width(), sl->rect.height()); - } - - // Invert if needed and extend to the actual bounds of the slider - if (isHorizontal) { - if (sl->upsideDown) { - ret = QRect(sl->rect.right() - ret.right(), sl->rect.top(), ret.width(), sl->rect.height()); - } else { - ret.setTop(sl->rect.top()); - ret.setBottom(sl->rect.bottom()); - } - } else { - if (!sl->upsideDown) { - ret = QRect(sl->rect.left(), sl->rect.bottom() - ret.bottom(), sl->rect.width(), ret.height()); - } else { - ret.setLeft(sl->rect.left()); - ret.setRight(sl->rect.right()); - } - } - } - break; - case CC_TitleBar: - if (const auto *titlebar = qstyleoption_cast(opt)) { - // The title bar layout is as follows: close, min, zoom, icon, title - // [ x _ + @ Window Title ] - // Center the icon and title until it starts to overlap with the buttons. - // The icon doesn't count towards SC_TitleBarLabel, but it's still rendered - // next to the title text. See drawComplexControl(). - if (sc == SC_TitleBarLabel) { - qreal labelWidth = titlebar->fontMetrics.horizontalAdvance(titlebar->text) + 1; // FIXME Rounding error? - qreal labelHeight = titlebar->fontMetrics.height(); - - const auto lastButtonRect = proxy()->subControlRect(CC_TitleBar, titlebar, SC_TitleBarMaxButton, widget); - qreal controlsSpacing = lastButtonRect.right() + titleBarButtonSpacing; - if (!titlebar->icon.isNull()) { - const auto iconSize = proxy()->pixelMetric(PM_SmallIconSize); - const auto actualIconSize = titlebar->icon.actualSize(QSize(iconSize, iconSize)).width();; - controlsSpacing += actualIconSize + titleBarIconTitleSpacing; - } - - const qreal labelPos = qMax(controlsSpacing, (opt->rect.width() - labelWidth) / 2.0); - labelWidth = qMin(labelWidth, opt->rect.width() - (labelPos + titleBarTitleRightMargin)); - ret = QRect(labelPos, (opt->rect.height() - labelHeight) / 2, - labelWidth, labelHeight); - } else { - const auto currentButton = d->windowButtonCocoaControl(sc); - if (currentButton == QMacStylePrivate::NoControl) - break; - - QPointF buttonPos = titlebar->rect.topLeft() + QPointF(titleBarButtonSpacing, 0); - QSizeF buttonSize; - for (int ct = QMacStylePrivate::Button_WindowClose; ct <= currentButton; ct++) { - const auto cw = QMacStylePrivate::CocoaControl(QMacStylePrivate::CocoaControlType(ct), - QStyleHelper::SizeLarge); - auto *wb = static_cast(d->cocoaControl(cw)); - if (ct == currentButton) - buttonSize = QSizeF::fromCGSize(wb.frame.size); - else - buttonPos.rx() += wb.frame.size.width + titleBarButtonSpacing; - } - - const auto vOffset = (opt->rect.height() - buttonSize.height()) / 2.0; - ret = QRectF(buttonPos, buttonSize).translated(0, vOffset).toRect(); - } - } - break; - case CC_ComboBox: - if (const QStyleOptionComboBox *combo = qstyleoption_cast(opt)) { - const auto ct = cocoaControlType(combo, widget); - const auto cs = d->effectiveAquaSizeConstrain(combo, widget); - const auto cw = QMacStylePrivate::CocoaControl(ct, cs); - const auto editRect = QMacStylePrivate::comboboxEditBounds(cw.adjustedControlFrame(combo->rect), cw); - - switch (sc) { - case SC_ComboBoxEditField:{ - ret = editRect.toAlignedRect(); - break; } - case SC_ComboBoxArrow:{ - ret = editRect.toAlignedRect(); - ret.setX(ret.x() + ret.width()); - ret.setWidth(combo->rect.right() - ret.right()); - break; } - case SC_ComboBoxListBoxPopup:{ - if (combo->editable) { - const CGRect inner = QMacStylePrivate::comboboxInnerBounds(combo->rect.toCGRect(), cw); - const int comboTop = combo->rect.top(); - ret = QRect(qRound(inner.origin.x), - comboTop, - qRound(inner.origin.x - combo->rect.left() + inner.size.width), - editRect.bottom() - comboTop + 2); - } else { - ret = QRect(combo->rect.x() + 4 - 11, - combo->rect.y() + 1, - editRect.width() + 10 + 11, - 1); - } - break; } - default: - break; - } - } - break; - case CC_GroupBox: - if (const QStyleOptionGroupBox *groupBox = qstyleoption_cast(opt)) { - bool checkable = groupBox->subControls & SC_GroupBoxCheckBox; - const bool flat = groupBox->features & QStyleOptionFrame::Flat; - bool hasNoText = !checkable && groupBox->text.isEmpty(); - switch (sc) { - case SC_GroupBoxLabel: - case SC_GroupBoxCheckBox: { - // Cheat and use the smaller font if we need to - const bool checkable = groupBox->subControls & SC_GroupBoxCheckBox; - const bool fontIsSet = (widget && widget->testAttribute(Qt::WA_SetFont)) - || !QApplication::desktopSettingsAware(); - const int margin = flat || hasNoText ? 0 : 9; - ret = groupBox->rect.adjusted(margin, 0, -margin, 0); - - const QFontMetricsF fm = flat || fontIsSet ? QFontMetricsF(groupBox->fontMetrics) : QFontMetricsF(d->smallSystemFont); - const QSizeF s = fm.size(Qt::AlignHCenter | Qt::AlignVCenter, qt_mac_removeMnemonics(groupBox->text), 0, nullptr); - const int tw = qCeil(s.width()); - const int h = qCeil(fm.height()); - ret.setHeight(h); - - QRect labelRect = alignedRect(groupBox->direction, groupBox->textAlignment, - QSize(tw, h), ret); - if (flat && checkable) - labelRect.moveLeft(labelRect.left() + 4); - int indicatorWidth = proxy()->pixelMetric(PM_IndicatorWidth, opt, widget); - bool rtl = groupBox->direction == Qt::RightToLeft; - if (sc == SC_GroupBoxLabel) { - if (checkable) { - int newSum = indicatorWidth + 1; - int newLeft = labelRect.left() + (rtl ? -newSum : newSum); - labelRect.moveLeft(newLeft); - if (flat) - labelRect.moveTop(labelRect.top() + 3); - else - labelRect.moveTop(labelRect.top() + 4); - } else if (flat) { - int newLeft = labelRect.left() - (rtl ? 3 : -3); - labelRect.moveLeft(newLeft); - labelRect.moveTop(labelRect.top() + 3); - } else { - int newLeft = labelRect.left() - (rtl ? 3 : 2); - labelRect.moveLeft(newLeft); - labelRect.moveTop(labelRect.top() + 4); - } - ret = labelRect; - } - - if (sc == SC_GroupBoxCheckBox) { - int left = rtl ? labelRect.right() - indicatorWidth : labelRect.left() - 1; - int top = flat ? ret.top() + 1 : ret.top() + 5; - ret.setRect(left, top, - indicatorWidth, proxy()->pixelMetric(PM_IndicatorHeight, opt, widget)); - } - break; - } - case SC_GroupBoxContents: - case SC_GroupBoxFrame: { - QFontMetrics fm = groupBox->fontMetrics; - int yOffset = 3; - if (!flat) { - if (widget && !widget->testAttribute(Qt::WA_SetFont) - && QApplication::desktopSettingsAware()) - fm = QFontMetrics(qt_app_fonts_hash()->value("QSmallFont", QFont())); - yOffset = 5; - } - - if (hasNoText) - yOffset = -qCeil(QFontMetricsF(fm).height()); - ret = opt->rect.adjusted(0, qCeil(QFontMetricsF(fm).height()) + yOffset, 0, 0); - if (sc == SC_GroupBoxContents) { - if (flat) - ret.adjust(3, -5, -3, -4); // guess too - else - ret.adjust(3, 3, -3, -4); // guess - } - } - break; - default: - ret = QCommonStyle::subControlRect(cc, groupBox, sc, widget); - break; - } - } - break; -#if QT_CONFIG(spinbox) - case CC_SpinBox: - if (const QStyleOptionSpinBox *spin = qstyleoption_cast(opt)) { - QStyleHelper::WidgetSizePolicy aquaSize = d->effectiveAquaSizeConstrain(spin, widget); - const auto fw = proxy()->pixelMetric(PM_SpinBoxFrameWidth, spin, widget); - int spinner_w; - int spinBoxSep; - switch (aquaSize) { - case QStyleHelper::SizeLarge: - spinner_w = 14; - spinBoxSep = 2; - break; - case QStyleHelper::SizeSmall: - spinner_w = 12; - spinBoxSep = 2; - break; - case QStyleHelper::SizeMini: - spinner_w = 10; - spinBoxSep = 1; - break; - default: - Q_UNREACHABLE(); - } - - switch (sc) { - case SC_SpinBoxUp: - case SC_SpinBoxDown: { - if (spin->buttonSymbols == QAbstractSpinBox::NoButtons) - break; - - const int y = fw; - const int x = spin->rect.width() - spinner_w; - ret.setRect(x + spin->rect.x(), y + spin->rect.y(), spinner_w, spin->rect.height() - y * 2); - int hackTranslateX; - switch (aquaSize) { - case QStyleHelper::SizeLarge: - hackTranslateX = 0; - break; - case QStyleHelper::SizeSmall: - hackTranslateX = -2; - break; - case QStyleHelper::SizeMini: - hackTranslateX = -1; - break; - default: - Q_UNREACHABLE(); - } - - const auto cw = QMacStylePrivate::CocoaControl(QMacStylePrivate::Stepper, aquaSize); - NSStepperCell *cell = static_cast(d->cocoaCell(cw)); - const CGRect outRect = [cell drawingRectForBounds:ret.toCGRect()]; - ret = QRectF::fromCGRect(outRect).toRect(); - - switch (sc) { - case SC_SpinBoxUp: - ret.setHeight(ret.height() / 2); - break; - case SC_SpinBoxDown: - ret.setY(ret.y() + ret.height() / 2); - break; - default: - Q_ASSERT(0); - break; - } - ret.translate(hackTranslateX, 0); // hack: position the buttons correctly (weird that we need this) - ret = visualRect(spin->direction, spin->rect, ret); - break; - } - case SC_SpinBoxEditField: - ret = spin->rect.adjusted(fw, fw, -fw, -fw); - if (spin->buttonSymbols != QAbstractSpinBox::NoButtons) { - ret.setWidth(spin->rect.width() - spinBoxSep - spinner_w); - ret = visualRect(spin->direction, spin->rect, ret); - } - break; - default: - ret = QCommonStyle::subControlRect(cc, spin, sc, widget); - break; - } - } - break; -#endif - case CC_ToolButton: - ret = QCommonStyle::subControlRect(cc, opt, sc, widget); - if (sc == SC_ToolButtonMenu) { -#ifndef QT_NO_ACCESSIBILITY - if (QStyleHelper::hasAncestor(opt->styleObject, QAccessible::ToolBar)) - ret.adjust(-toolButtonArrowMargin, 0, 0, 0); -#endif - ret.adjust(-1, 0, 0, 0); - } - break; - default: - ret = QCommonStyle::subControlRect(cc, opt, sc, widget); - break; - } - return ret; -} - -QSize QMacStyle::sizeFromContents(ContentsType ct, const QStyleOption *opt, - const QSize &csz, const QWidget *widget) const -{ - Q_D(const QMacStyle); - QSize sz(csz); - bool useAquaGuideline = true; - - switch (ct) { -#if QT_CONFIG(spinbox) - case CT_SpinBox: - if (qstyleoption_cast(opt)) { - const int buttonWidth = 20; // FIXME Use subControlRect() - sz += QSize(buttonWidth, 0); - } - break; -#endif - case QStyle::CT_TabWidget: - // the size between the pane and the "contentsRect" (+4,+4) - // (the "contentsRect" is on the inside of the pane) - sz = QCommonStyle::sizeFromContents(ct, opt, csz, widget); - /** - This is supposed to show the relationship between the tabBar and - the stack widget of a QTabWidget. - Unfortunately ascii is not a good way of representing graphics..... - PS: The '=' line is the painted frame. - - top ---+ - | - | - | - | vvv just outside the painted frame is the "pane" - - -|- - - - - - - - - - <-+ - TAB BAR +=====^============ | +2 pixels - - - -|- - -|- - - - - - - <-+ - | | ^ ^^^ just inside the painted frame is the "contentsRect" - | | | - | overlap | - | | | - bottom ------+ <-+ +14 pixels - | - v - ------------------------------ <- top of stack widget - - - To summarize: - * 2 is the distance between the pane and the contentsRect - * The 14 and the 1's are the distance from the contentsRect to the stack widget. - (same value as used in SE_TabWidgetTabContents) - * overlap is how much the pane should overlap the tab bar - */ - // then add the size between the stackwidget and the "contentsRect" -#if QT_CONFIG(tabwidget) - if (const QStyleOptionTabWidgetFrame *twf - = qstyleoption_cast(opt)) { - QSize extra(0,0); - const int overlap = pixelMetric(PM_TabBarBaseOverlap, opt, widget); - const int gapBetweenTabbarAndStackWidget = 2 + 14 - overlap; - - const auto tabDirection = QMacStylePrivate::tabDirection(twf->shape); - if (tabDirection == QMacStylePrivate::North - || tabDirection == QMacStylePrivate::South) { - extra = QSize(2, gapBetweenTabbarAndStackWidget + 1); - } else { - extra = QSize(gapBetweenTabbarAndStackWidget + 1, 2); - } - sz+= extra; - } -#endif - break; -#if QT_CONFIG(tabbar) - case QStyle::CT_TabBarTab: - if (const QStyleOptionTab *tab = qstyleoption_cast(opt)) { - const bool differentFont = (widget && widget->testAttribute(Qt::WA_SetFont)) - || !QApplication::desktopSettingsAware(); - const auto tabDirection = QMacStylePrivate::tabDirection(tab->shape); - const bool verticalTabs = tabDirection == QMacStylePrivate::East - || tabDirection == QMacStylePrivate::West; - if (verticalTabs) - sz = sz.transposed(); - - int defaultTabHeight; - const auto cs = d->effectiveAquaSizeConstrain(opt, widget); - switch (cs) { - case QStyleHelper::SizeLarge: - if (tab->documentMode) - defaultTabHeight = 24; - else - defaultTabHeight = 21; - break; - case QStyleHelper::SizeSmall: - defaultTabHeight = 18; - break; - case QStyleHelper::SizeMini: - defaultTabHeight = 16; - break; - default: - break; - } - - const bool widthSet = !differentFont && tab->icon.isNull(); - if (widthSet) { - const auto textSize = opt->fontMetrics.size(Qt::TextShowMnemonic, tab->text); - sz.rwidth() = textSize.width(); - sz.rheight() = qMax(defaultTabHeight, textSize.height()); - } else { - sz.rheight() = qMax(defaultTabHeight, sz.height()); - } - sz.rwidth() += proxy()->pixelMetric(PM_TabBarTabHSpace, tab, widget); - - if (verticalTabs) - sz = sz.transposed(); - - int maxWidgetHeight = qMax(tab->leftButtonSize.height(), tab->rightButtonSize.height()); - int maxWidgetWidth = qMax(tab->leftButtonSize.width(), tab->rightButtonSize.width()); - - int widgetWidth = 0; - int widgetHeight = 0; - int padding = 0; - if (tab->leftButtonSize.isValid()) { - padding += 8; - widgetWidth += tab->leftButtonSize.width(); - widgetHeight += tab->leftButtonSize.height(); - } - if (tab->rightButtonSize.isValid()) { - padding += 8; - widgetWidth += tab->rightButtonSize.width(); - widgetHeight += tab->rightButtonSize.height(); - } - - if (verticalTabs) { - sz.setWidth(qMax(sz.width(), maxWidgetWidth)); - sz.setHeight(sz.height() + widgetHeight + padding); - } else { - if (widthSet) - sz.setWidth(sz.width() + widgetWidth + padding); - sz.setHeight(qMax(sz.height(), maxWidgetHeight)); - } - } - break; -#endif - case QStyle::CT_PushButton: { - if (const QStyleOptionButton *btn = qstyleoption_cast(opt)) - if (btn->features & QStyleOptionButton::CommandLinkButton) - return QCommonStyle::sizeFromContents(ct, opt, sz, widget); - - // By default, we fit the contents inside a normal rounded push button. - // Do this by add enough space around the contents so that rounded - // borders (including highlighting when active) will show. - // TODO Use QFocusFrame and get rid of these horrors. - QSize macsz; - const auto controlSize = d->effectiveAquaSizeConstrain(opt, widget, CT_PushButton, sz, &macsz); - // FIXME See comment in CT_PushButton case in qt_aqua_get_known_size(). - if (macsz.width() != -1) - sz.setWidth(macsz.width()); - else - sz.rwidth() += QMacStylePrivate::PushButtonLeftOffset + QMacStylePrivate::PushButtonRightOffset + 12; - // All values as measured from HIThemeGetButtonBackgroundBounds() - if (controlSize != QStyleHelper::SizeMini) - sz.rwidth() += 12; // We like 12 over here. - if (controlSize == QStyleHelper::SizeLarge && sz.height() > 16) - sz.rheight() += pushButtonDefaultHeight[QStyleHelper::SizeLarge] - 16; - else if (controlSize == QStyleHelper::SizeMini) - sz.setHeight(24); // FIXME Our previous HITheme-based logic returned this. - else - sz.setHeight(pushButtonDefaultHeight[controlSize]); - break; - } - case QStyle::CT_MenuItem: - if (const QStyleOptionMenuItem *mi = qstyleoption_cast(opt)) { - int maxpmw = mi->maxIconWidth; -#if QT_CONFIG(combobox) - const QComboBox *comboBox = qobject_cast(widget); -#endif - int w = sz.width(), - h = sz.height(); - if (mi->menuItemType == QStyleOptionMenuItem::Separator) { - w = 10; - h = qt_mac_aqua_get_metric(MenuSeparatorHeight); - } else { - h = mi->fontMetrics.height() + 2; - if (!mi->icon.isNull()) { -#if QT_CONFIG(combobox) - if (comboBox) { - const QSize &iconSize = comboBox->iconSize(); - h = qMax(h, iconSize.height() + 4); - maxpmw = qMax(maxpmw, iconSize.width()); - } else -#endif - { - int iconExtent = proxy()->pixelMetric(PM_SmallIconSize); - h = qMax(h, mi->icon.actualSize(QSize(iconExtent, iconExtent)).height() + 4); - } - } - } - if (mi->text.contains(QLatin1Char('\t'))) - w += 12; - else if (mi->menuItemType == QStyleOptionMenuItem::SubMenu) - w += 35; // Not quite exactly as it seems to depend on other factors - if (maxpmw) - w += maxpmw + 6; - // add space for a check. All items have place for a check too. - w += 20; -#if QT_CONFIG(combobox) - if (comboBox && comboBox->isVisible()) { - QStyleOptionComboBox cmb; - cmb.initFrom(comboBox); - cmb.editable = false; - cmb.subControls = QStyle::SC_ComboBoxEditField; - cmb.activeSubControls = QStyle::SC_None; - w = qMax(w, subControlRect(QStyle::CC_ComboBox, &cmb, - QStyle::SC_ComboBoxEditField, - comboBox).width()); - } else -#endif - { - w += 12; - } - sz = QSize(w, h); - } - break; - case CT_MenuBarItem: - if (!sz.isEmpty()) - sz += QSize(12, 4); // Constants from QWindowsStyle - break; - case CT_ToolButton: - sz.rwidth() += 10; - sz.rheight() += 10; - if (const auto *tb = qstyleoption_cast(opt)) - if (tb->features & QStyleOptionToolButton::Menu) - sz.rwidth() += toolButtonArrowMargin; - return sz; - case CT_ComboBox: - if (const auto *cb = qstyleoption_cast(opt)) { - const auto controlSize = d->effectiveAquaSizeConstrain(opt, widget); - if (!cb->editable) { - // Same as CT_PushButton, because we have to fit the focus - // ring and a non-editable combo box is a NSPopUpButton. - sz.rwidth() += QMacStylePrivate::PushButtonLeftOffset + QMacStylePrivate::PushButtonRightOffset + 12; - // All values as measured from HIThemeGetButtonBackgroundBounds() - if (controlSize != QStyleHelper::SizeMini) - sz.rwidth() += 12; // We like 12 over here. -#if 0 - // TODO Maybe support square combo boxes - if (controlSize == QStyleHelper::SizeLarge && sz.height() > 16) - sz.rheight() += popupButtonDefaultHeight[QStyleHelper::SizeLarge] - 16; - else -#endif - } else { - sz.rwidth() += 50; // FIXME Double check this - } - - // This should be enough to fit the focus ring - if (controlSize == QStyleHelper::SizeMini) - sz.setHeight(24); // FIXME Our previous HITheme-based logic returned this for CT_PushButton. - else - sz.setHeight(pushButtonDefaultHeight[controlSize]); - - return sz; - } - break; - case CT_Menu: { - if (proxy() == this) { - sz = csz; - } else { - QStyleHintReturnMask menuMask; - QStyleOption myOption = *opt; - myOption.rect.setSize(sz); - if (proxy()->styleHint(SH_Menu_Mask, &myOption, widget, &menuMask)) - sz = menuMask.region.boundingRect().size(); - } - break; } - case CT_HeaderSection:{ - const QStyleOptionHeader *header = qstyleoption_cast(opt); - sz = QCommonStyle::sizeFromContents(ct, opt, csz, widget); - if (header->text.contains(QLatin1Char('\n'))) - useAquaGuideline = false; - break; } - case CT_ScrollBar : - // Make sure that the scroll bar is large enough to display the thumb indicator. - if (const QStyleOptionSlider *slider = qstyleoption_cast(opt)) { - const int minimumSize = 24; // Smallest knob size, but Cocoa doesn't seem to care - if (slider->orientation == Qt::Horizontal) - sz = sz.expandedTo(QSize(minimumSize, sz.height())); - else - sz = sz.expandedTo(QSize(sz.width(), minimumSize)); - } - break; - case CT_ItemViewItem: - if (const QStyleOptionViewItem *vopt = qstyleoption_cast(opt)) { - sz = QCommonStyle::sizeFromContents(ct, vopt, csz, widget); - sz.setHeight(sz.height() + 2); - } - break; - - default: - sz = QCommonStyle::sizeFromContents(ct, opt, csz, widget); - } - - if (useAquaGuideline && ct != CT_PushButton) { - // TODO Probably going away at some point - QSize macsz; - if (d->aquaSizeConstrain(opt, widget, ct, sz, &macsz) != QStyleHelper::SizeDefault) { - if (macsz.width() != -1) - sz.setWidth(macsz.width()); - if (macsz.height() != -1) - sz.setHeight(macsz.height()); - } - } - - // The sizes that Carbon and the guidelines gives us excludes the focus frame. - // We compensate for this by adding some extra space here to make room for the frame when drawing: - if (const QStyleOptionComboBox *combo = qstyleoption_cast(opt)){ - if (combo->editable) { - const auto widgetSize = d->aquaSizeConstrain(opt, widget); - QMacStylePrivate::CocoaControl cw; - cw.type = combo->editable ? QMacStylePrivate::ComboBox : QMacStylePrivate::Button_PopupButton; - cw.size = widgetSize; - const CGRect diffRect = QMacStylePrivate::comboboxInnerBounds(CGRectZero, cw); - sz.rwidth() -= qRound(diffRect.size.width); - sz.rheight() -= qRound(diffRect.size.height); - } - } - return sz; -} - -void QMacStyle::drawItemText(QPainter *p, const QRect &r, int flags, const QPalette &pal, - bool enabled, const QString &text, QPalette::ColorRole textRole) const -{ - if(flags & Qt::TextShowMnemonic) - flags |= Qt::TextHideMnemonic; - QCommonStyle::drawItemText(p, r, flags, pal, enabled, text, textRole); -} - -bool QMacStyle::event(QEvent *e) -{ - Q_D(QMacStyle); - if(e->type() == QEvent::FocusIn) { - QWidget *f = 0; - QWidget *focusWidget = QApplication::focusWidget(); -#if QT_CONFIG(graphicsview) - if (QGraphicsView *graphicsView = qobject_cast(focusWidget)) { - QGraphicsItem *focusItem = graphicsView->scene() ? graphicsView->scene()->focusItem() : 0; - if (focusItem && focusItem->type() == QGraphicsProxyWidget::Type) { - QGraphicsProxyWidget *proxy = static_cast(focusItem); - if (proxy->widget()) - focusWidget = proxy->widget()->focusWidget(); - } - } -#endif - - if (focusWidget && focusWidget->testAttribute(Qt::WA_MacShowFocusRect)) { -#if QT_CONFIG(spinbox) - if (const auto sb = qobject_cast(focusWidget)) - f = sb->property("_q_spinbox_lineedit").value(); - else -#endif - f = focusWidget; - } - if (f) { - if(!d->focusWidget) - d->focusWidget = new QFocusFrame(f); - d->focusWidget->setWidget(f); - } else if(d->focusWidget) { - d->focusWidget->setWidget(0); - } - } else if(e->type() == QEvent::FocusOut) { - if(d->focusWidget) - d->focusWidget->setWidget(0); - } - return false; -} - -QIcon QMacStyle::standardIcon(StandardPixmap standardIcon, const QStyleOption *opt, - const QWidget *widget) const -{ - switch (standardIcon) { - default: - return QCommonStyle::standardIcon(standardIcon, opt, widget); - case SP_ToolBarHorizontalExtensionButton: - case SP_ToolBarVerticalExtensionButton: { - QPixmap pixmap(QLatin1String(":/qt-project.org/styles/macstyle/images/toolbar-ext.png")); - if (standardIcon == SP_ToolBarVerticalExtensionButton) { - QPixmap pix2(pixmap.height(), pixmap.width()); - pix2.setDevicePixelRatio(pixmap.devicePixelRatio()); - pix2.fill(Qt::transparent); - QPainter p(&pix2); - p.translate(pix2.width(), 0); - p.rotate(90); - p.drawPixmap(0, 0, pixmap); - return pix2; - } - return pixmap; - } - } -} - -int QMacStyle::layoutSpacing(QSizePolicy::ControlType control1, - QSizePolicy::ControlType control2, - Qt::Orientation orientation, - const QStyleOption *option, - const QWidget *widget) const -{ - const int ButtonMask = QSizePolicy::ButtonBox | QSizePolicy::PushButton; - const int controlSize = getControlSize(option, widget); - - if (control2 == QSizePolicy::ButtonBox) { - /* - AHIG seems to prefer a 12-pixel margin between group - boxes and the row of buttons. The 20 pixel comes from - Builder. - */ - if (control1 & (QSizePolicy::Frame // guess - | QSizePolicy::GroupBox // (AHIG, guess, guess) - | QSizePolicy::TabWidget // guess - | ButtonMask)) { // AHIG - return_SIZE(14, 8, 8); - } else if (control1 == QSizePolicy::LineEdit) { - return_SIZE(8, 8, 8); // Interface Builder - } else { - return_SIZE(20, 7, 7); // Interface Builder - } - } - - if ((control1 | control2) & ButtonMask) { - if (control1 == QSizePolicy::LineEdit) - return_SIZE(8, 8, 8); // Interface Builder - else if (control2 == QSizePolicy::LineEdit) { - if (orientation == Qt::Vertical) - return_SIZE(20, 7, 7); // Interface Builder - else - return_SIZE(20, 8, 8); - } - return_SIZE(14, 8, 8); // Interface Builder - } - - switch (CT2(control1, control2)) { - case CT1(QSizePolicy::Label): // guess - case CT2(QSizePolicy::Label, QSizePolicy::DefaultType): // guess - case CT2(QSizePolicy::Label, QSizePolicy::CheckBox): // AHIG - case CT2(QSizePolicy::Label, QSizePolicy::ComboBox): // AHIG - case CT2(QSizePolicy::Label, QSizePolicy::LineEdit): // guess - case CT2(QSizePolicy::Label, QSizePolicy::RadioButton): // AHIG - case CT2(QSizePolicy::Label, QSizePolicy::Slider): // guess - case CT2(QSizePolicy::Label, QSizePolicy::SpinBox): // guess - case CT2(QSizePolicy::Label, QSizePolicy::ToolButton): // guess - return_SIZE(8, 6, 5); - case CT1(QSizePolicy::ToolButton): - return 8; // AHIG - case CT1(QSizePolicy::CheckBox): - case CT2(QSizePolicy::CheckBox, QSizePolicy::RadioButton): - case CT2(QSizePolicy::RadioButton, QSizePolicy::CheckBox): - if (orientation == Qt::Vertical) - return_SIZE(8, 8, 7); // AHIG and Builder - break; - case CT1(QSizePolicy::RadioButton): - if (orientation == Qt::Vertical) - return 5; // (Builder, guess, AHIG) - } - - if (orientation == Qt::Horizontal - && (control2 & (QSizePolicy::CheckBox | QSizePolicy::RadioButton))) - return_SIZE(12, 10, 8); // guess - - if ((control1 | control2) & (QSizePolicy::Frame - | QSizePolicy::GroupBox - | QSizePolicy::TabWidget)) { - /* - These values were chosen so that nested container widgets - look good side by side. Builder uses 8, which looks way - too small, and AHIG doesn't say anything. - */ - return_SIZE(16, 10, 10); // guess - } - - if ((control1 | control2) & (QSizePolicy::Line | QSizePolicy::Slider)) - return_SIZE(12, 10, 8); // AHIG - - if ((control1 | control2) & QSizePolicy::LineEdit) - return_SIZE(10, 8, 8); // AHIG - - /* - AHIG and Builder differ by up to 4 pixels for stacked editable - comboboxes. We use some values that work fairly well in all - cases. - */ - if ((control1 | control2) & QSizePolicy::ComboBox) - return_SIZE(10, 8, 7); // guess - - /* - Builder defaults to 8, 6, 5 in lots of cases, but most of the time the - result looks too cramped. - */ - return_SIZE(10, 8, 6); // guess -} - -QT_END_NAMESPACE diff --git a/5.12.0/qtbase/src/widgets/kernel/qwidgetbackingstore.cpp b/5.12.0/qtbase/src/widgets/kernel/qwidgetbackingstore.cpp deleted file mode 100644 index a32eb2a..0000000 --- a/5.12.0/qtbase/src/widgets/kernel/qwidgetbackingstore.cpp +++ /dev/null @@ -1,1696 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2016 The Qt Company Ltd. -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the QtWidgets module of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:LGPL$ -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and The Qt Company. For licensing terms -** and conditions see https://www.qt.io/terms-conditions. For further -** information use the contact form at https://www.qt.io/contact-us. -** -** GNU Lesser General Public License Usage -** Alternatively, this file may be used under the terms of the GNU Lesser -** General Public License version 3 as published by the Free Software -** Foundation and appearing in the file LICENSE.LGPL3 included in the -** packaging of this file. Please review the following information to -** ensure the GNU Lesser General Public License version 3 requirements -** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. -** -** GNU General Public License Usage -** Alternatively, this file may be used under the terms of the GNU -** General Public License version 2.0 or (at your option) the GNU General -** Public license version 3 or any later version approved by the KDE Free -** Qt Foundation. The licenses are as published by the Free Software -** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 -** included in the packaging of this file. Please review the following -** information to ensure the GNU General Public License requirements will -** be met: https://www.gnu.org/licenses/gpl-2.0.html and -** https://www.gnu.org/licenses/gpl-3.0.html. -** -** $QT_END_LICENSE$ -** -****************************************************************************/ - - -#include "qplatformdefs.h" - -#include "qwidgetbackingstore_p.h" - -#include -#include -#include -#include -#include -#include -#if QT_CONFIG(graphicsview) -#include -#endif - -#include -#include -#include -#if QT_CONFIG(graphicseffect) -#include -#endif -#include -#include - -#include - -#if defined(Q_OS_WIN) && !defined(QT_NO_PAINT_DEBUG) -# include -# include -#endif - -QT_BEGIN_NAMESPACE - -extern QRegion qt_dirtyRegion(QWidget *); - -#ifndef QT_NO_OPENGL -Q_GLOBAL_STATIC(QPlatformTextureList, qt_dummy_platformTextureList) -#endif - -/** - * Flushes the contents of the \a backingStore into the screen area of \a widget. - * \a region is the region to be updated in \a widget coordinates. - */ -void QWidgetBackingStore::qt_flush(QWidget *widget, const QRegion ®ion, QBackingStore *backingStore, - QWidget *tlw, QPlatformTextureList *widgetTextures, - QWidgetBackingStore *widgetBackingStore) -{ -#ifdef QT_NO_OPENGL - Q_UNUSED(widgetTextures); - Q_ASSERT(!region.isEmpty()); -#else - Q_ASSERT(!region.isEmpty() || widgetTextures); -#endif - Q_ASSERT(widget); - Q_ASSERT(backingStore); - Q_ASSERT(tlw); -#if !defined(QT_NO_PAINT_DEBUG) - static int flushUpdate = qEnvironmentVariableIntValue("QT_FLUSH_UPDATE"); - if (flushUpdate > 0) - QWidgetBackingStore::showYellowThing(widget, region, flushUpdate * 10, false); -#endif - - if (tlw->testAttribute(Qt::WA_DontShowOnScreen) || widget->testAttribute(Qt::WA_DontShowOnScreen)) - return; - - // Foreign Windows do not have backing store content and must not be flushed - if (QWindow *widgetWindow = widget->windowHandle()) { - if (widgetWindow->type() == Qt::ForeignWindow) - return; - } - - static bool fpsDebug = qEnvironmentVariableIntValue("QT_DEBUG_FPS"); - if (fpsDebug) { - if (!widgetBackingStore->perfFrames++) - widgetBackingStore->perfTime.start(); - if (widgetBackingStore->perfTime.elapsed() > 5000) { - double fps = double(widgetBackingStore->perfFrames * 1000) / widgetBackingStore->perfTime.restart(); - qDebug("FPS: %.1f\n", fps); - widgetBackingStore->perfFrames = 0; - } - } - - QPoint offset; - if (widget != tlw) - offset += widget->mapTo(tlw, QPoint()); - - QRegion effectiveRegion = region; -#ifndef QT_NO_OPENGL - const bool compositionWasActive = widget->d_func()->renderToTextureComposeActive; - if (!widgetTextures) { - widget->d_func()->renderToTextureComposeActive = false; - // Detect the case of falling back to the normal flush path when no - // render-to-texture widgets are visible anymore. We will force one - // last flush to go through the OpenGL-based composition to prevent - // artifacts. The next flush after this one will use the normal path. - if (compositionWasActive) - widgetTextures = qt_dummy_platformTextureList; - } else { - widget->d_func()->renderToTextureComposeActive = true; - } - // When changing the composition status, make sure the dirty region covers - // the entire widget. Just having e.g. the shown/hidden render-to-texture - // widget's area marked as dirty is incorrect when changing flush paths. - if (compositionWasActive != widget->d_func()->renderToTextureComposeActive) - effectiveRegion = widget->rect(); - - // re-test since we may have been forced to this path via the dummy texture list above - if (widgetTextures) { - qt_window_private(tlw->windowHandle())->compositing = true; - widget->window()->d_func()->sendComposeStatus(widget->window(), false); - // A window may have alpha even when the app did not request - // WA_TranslucentBackground. Therefore the compositor needs to know whether the app intends - // to rely on translucency, in order to decide if it should clear to transparent or opaque. - const bool translucentBackground = widget->testAttribute(Qt::WA_TranslucentBackground); - backingStore->handle()->composeAndFlush(widget->windowHandle(), effectiveRegion, offset, - widgetTextures, translucentBackground); - widget->window()->d_func()->sendComposeStatus(widget->window(), true); - } else -#endif - backingStore->flush(effectiveRegion, widget->windowHandle(), offset); -} - -#ifndef QT_NO_PAINT_DEBUG -#if defined(Q_OS_WIN) && !defined(Q_OS_WINRT) - -static void showYellowThing_win(QWidget *widget, const QRegion ®ion, int msec) -{ - // We expect to be passed a native parent. - QWindow *nativeWindow = widget->windowHandle(); - if (!nativeWindow) - return; - void *hdcV = QGuiApplication::platformNativeInterface()->nativeResourceForWindow(QByteArrayLiteral("getDC"), nativeWindow); - if (!hdcV) - return; - const HDC hdc = reinterpret_cast(hdcV); - - static const COLORREF colors[] = {RGB(255, 255, 0), RGB(255, 200, 55), RGB(200, 255, 55), RGB(200, 200, 0)}; - - static size_t i = 0; - const HBRUSH brush = CreateSolidBrush(colors[i]); - i = (i + 1) % (sizeof(colors) / sizeof(colors[0])); - - for (const QRect &rect : region) { - RECT winRect; - SetRect(&winRect, rect.left(), rect.top(), rect.right(), rect.bottom()); - FillRect(hdc, &winRect, brush); - } - DeleteObject(brush); - QGuiApplication::platformNativeInterface()->nativeResourceForWindow(QByteArrayLiteral("releaseDC"), nativeWindow); - ::Sleep(msec); -} -#endif // defined(Q_OS_WIN) && !defined(Q_OS_WINRT) - -void QWidgetBackingStore::showYellowThing(QWidget *widget, const QRegion &toBePainted, int msec, bool unclipped) -{ -#ifdef Q_OS_WINRT - Q_UNUSED(msec) -#endif - QRegion paintRegion = toBePainted; - QRect widgetRect = widget->rect(); - - if (!widget->internalWinId()) { - QWidget *nativeParent = widget->nativeParentWidget(); - const QPoint offset = widget->mapTo(nativeParent, QPoint(0, 0)); - paintRegion.translate(offset); - widgetRect.translate(offset); - widget = nativeParent; - } - -#if defined(Q_OS_WIN) && !defined(Q_OS_WINRT) - Q_UNUSED(unclipped); - showYellowThing_win(widget, paintRegion, msec); -#else - //flags to fool painter - bool paintUnclipped = widget->testAttribute(Qt::WA_PaintUnclipped); - if (unclipped && !widget->d_func()->paintOnScreen()) - widget->setAttribute(Qt::WA_PaintUnclipped); - - const bool setFlag = !widget->testAttribute(Qt::WA_WState_InPaintEvent); - if (setFlag) - widget->setAttribute(Qt::WA_WState_InPaintEvent); - - //setup the engine - QPaintEngine *pe = widget->paintEngine(); - if (pe) { - pe->setSystemClip(paintRegion); - { - QPainter p(widget); - p.setClipRegion(paintRegion); - static int i = 0; - switch (i) { - case 0: - p.fillRect(widgetRect, QColor(255,255,0)); - break; - case 1: - p.fillRect(widgetRect, QColor(255,200,55)); - break; - case 2: - p.fillRect(widgetRect, QColor(200,255,55)); - break; - case 3: - p.fillRect(widgetRect, QColor(200,200,0)); - break; - } - i = (i+1) & 3; - p.end(); - } - } - - if (setFlag) - widget->setAttribute(Qt::WA_WState_InPaintEvent, false); - - //restore - widget->setAttribute(Qt::WA_PaintUnclipped, paintUnclipped); - - if (pe) - pe->setSystemClip(QRegion()); - -#if defined(Q_OS_UNIX) - ::usleep(1000 * msec); -#endif -#endif // !Q_OS_WIN -} - -bool QWidgetBackingStore::flushPaint(QWidget *widget, const QRegion &rgn) -{ - if (!widget) - return false; - - int delay = 0; - if (widget->testAttribute(Qt::WA_WState_InPaintEvent)) { - static int flushPaintEvent = qEnvironmentVariableIntValue("QT_FLUSH_PAINT_EVENT"); - if (!flushPaintEvent) - return false; - delay = flushPaintEvent; - } else { - static int flushPaint = qEnvironmentVariableIntValue("QT_FLUSH_PAINT"); - if (!flushPaint) - return false; - delay = flushPaint; - } - - QWidgetBackingStore::showYellowThing(widget, rgn, delay * 10, true); - return true; -} - -void QWidgetBackingStore::unflushPaint(QWidget *widget, const QRegion &rgn) -{ - if (widget->d_func()->paintOnScreen() || rgn.isEmpty()) - return; - - QWidget *tlw = widget->window(); - QTLWExtra *tlwExtra = tlw->d_func()->maybeTopData(); - if (!tlwExtra) - return; - - qt_flush(widget, rgn, tlwExtra->backingStoreTracker->store, tlw, 0, tlw->d_func()->maybeBackingStore()); -} -#endif // QT_NO_PAINT_DEBUG - -/* - Moves the whole rect by (dx, dy) in widget's coordinate system. - Doesn't generate any updates. -*/ -bool QWidgetBackingStore::bltRect(const QRect &rect, int dx, int dy, QWidget *widget) -{ - const QPoint pos(widget->mapTo(tlw, rect.topLeft())); - const QRect tlwRect(QRect(pos, rect.size())); - if (dirty.intersects(tlwRect)) - return false; // We don't want to scroll junk. - return store->scroll(tlwRect, dx, dy); -} - -void QWidgetBackingStore::releaseBuffer() -{ - if (store) - store->resize(QSize()); -} - -/*! - Prepares the window surface to paint a\ toClean region of the \a widget and - updates the BeginPaintInfo struct accordingly. - - The \a toClean region might be clipped by the window surface. -*/ -void QWidgetBackingStore::beginPaint(QRegion &toClean, QWidget *widget, QBackingStore *backingStore, - BeginPaintInfo *returnInfo, bool toCleanIsInTopLevelCoordinates) -{ - Q_UNUSED(widget); - Q_UNUSED(toCleanIsInTopLevelCoordinates); - - // Always flush repainted areas. - dirtyOnScreen += toClean; - -#ifdef QT_NO_PAINT_DEBUG - backingStore->beginPaint(toClean); -#else - returnInfo->wasFlushed = QWidgetBackingStore::flushPaint(tlw, toClean); - // Avoid deadlock with QT_FLUSH_PAINT: the server will wait for - // the BackingStore lock, so if we hold that, the server will - // never release the Communication lock that we are waiting for in - // sendSynchronousCommand - if (!returnInfo->wasFlushed) - backingStore->beginPaint(toClean); -#endif - - Q_UNUSED(returnInfo); -} - -void QWidgetBackingStore::endPaint(const QRegion &cleaned, QBackingStore *backingStore, - BeginPaintInfo *beginPaintInfo) -{ -#ifndef QT_NO_PAINT_DEBUG - if (!beginPaintInfo->wasFlushed) - backingStore->endPaint(); - else - QWidgetBackingStore::unflushPaint(tlw, cleaned); -#else - Q_UNUSED(beginPaintInfo); - Q_UNUSED(cleaned); - backingStore->endPaint(); -#endif - - flush(); -} - -/*! - Returns the region (in top-level coordinates) that needs repaint and/or flush. - - If the widget is non-zero, only the dirty region for the widget is returned - and the region will be in widget coordinates. -*/ -QRegion QWidgetBackingStore::dirtyRegion(QWidget *widget) const -{ - const bool widgetDirty = widget && widget != tlw; - const QRect tlwRect(topLevelRect()); - const QRect surfaceGeometry(tlwRect.topLeft(), store->size()); - if (surfaceGeometry != tlwRect && surfaceGeometry.size() != tlwRect.size()) { - if (widgetDirty) { - const QRect dirtyTlwRect = QRect(QPoint(), tlwRect.size()); - const QPoint offset(widget->mapTo(tlw, QPoint())); - const QRect dirtyWidgetRect(dirtyTlwRect & widget->rect().translated(offset)); - return dirtyWidgetRect.translated(-offset); - } - return QRect(QPoint(), tlwRect.size()); - } - - // Calculate the region that needs repaint. - QRegion r(dirty); - for (int i = 0; i < dirtyWidgets.size(); ++i) { - QWidget *w = dirtyWidgets.at(i); - if (widgetDirty && w != widget && !widget->isAncestorOf(w)) - continue; - r += w->d_func()->dirty.translated(w->mapTo(tlw, QPoint())); - } - - // Append the region that needs flush. - r += dirtyOnScreen; - - if (dirtyOnScreenWidgets) { // Only in use with native child widgets. - for (int i = 0; i < dirtyOnScreenWidgets->size(); ++i) { - QWidget *w = dirtyOnScreenWidgets->at(i); - if (widgetDirty && w != widget && !widget->isAncestorOf(w)) - continue; - QWidgetPrivate *wd = w->d_func(); - Q_ASSERT(wd->needsFlush); - r += wd->needsFlush->translated(w->mapTo(tlw, QPoint())); - } - } - - if (widgetDirty) { - // Intersect with the widget geometry and translate to its coordinates. - const QPoint offset(widget->mapTo(tlw, QPoint())); - r &= widget->rect().translated(offset); - r.translate(-offset); - } - return r; -} - -/*! - Returns the static content inside the \a parent if non-zero; otherwise the static content - for the entire backing store is returned. The content will be clipped to \a withinClipRect - if non-empty. -*/ -QRegion QWidgetBackingStore::staticContents(QWidget *parent, const QRect &withinClipRect) const -{ - if (!parent && tlw->testAttribute(Qt::WA_StaticContents)) { - const QSize surfaceGeometry(store->size()); - QRect surfaceRect(0, 0, surfaceGeometry.width(), surfaceGeometry.height()); - if (!withinClipRect.isEmpty()) - surfaceRect &= withinClipRect; - return QRegion(surfaceRect); - } - - QRegion region; - if (parent && parent->d_func()->children.isEmpty()) - return region; - - const bool clipToRect = !withinClipRect.isEmpty(); - const int count = staticWidgets.count(); - for (int i = 0; i < count; ++i) { - QWidget *w = staticWidgets.at(i); - QWidgetPrivate *wd = w->d_func(); - if (!wd->isOpaque || !wd->extra || wd->extra->staticContentsSize.isEmpty() - || !w->isVisible() || (parent && !parent->isAncestorOf(w))) { - continue; - } - - QRect rect(0, 0, wd->extra->staticContentsSize.width(), wd->extra->staticContentsSize.height()); - const QPoint offset = w->mapTo(parent ? parent : tlw, QPoint()); - if (clipToRect) - rect &= withinClipRect.translated(-offset); - if (rect.isEmpty()) - continue; - - rect &= wd->clipRect(); - if (rect.isEmpty()) - continue; - - QRegion visible(rect); - wd->clipToEffectiveMask(visible); - if (visible.isEmpty()) - continue; - wd->subtractOpaqueSiblings(visible, 0, /*alsoNonOpaque=*/true); - - visible.translate(offset); - region += visible; - } - - return region; -} - -void QWidgetBackingStore::sendUpdateRequest(QWidget *widget, UpdateTime updateTime) -{ - if (!widget) - return; - -#ifndef QT_NO_OPENGL - // Having every repaint() leading to a sync/flush is bad as it causes - // compositing and waiting for vsync each and every time. Change to - // UpdateLater, except for approx. once per frame to prevent starvation in - // case the control does not get back to the event loop. - QWidget *w = widget->window(); - if (updateTime == UpdateNow && w && w->windowHandle() && QWindowPrivate::get(w->windowHandle())->compositing) { - int refresh = 60; - QScreen *ws = w->windowHandle()->screen(); - if (ws) - refresh = ws->refreshRate(); - QWindowPrivate *wd = QWindowPrivate::get(w->windowHandle()); - if (wd->lastComposeTime.isValid()) { - const qint64 elapsed = wd->lastComposeTime.elapsed(); - if (elapsed <= qint64(1000.0f / refresh)) - updateTime = UpdateLater; - } - } -#endif - - switch (updateTime) { - case UpdateLater: - updateRequestSent = true; - QApplication::postEvent(widget, new QEvent(QEvent::UpdateRequest), Qt::LowEventPriority); - break; - case UpdateNow: { - QEvent event(QEvent::UpdateRequest); - QApplication::sendEvent(widget, &event); - break; - } - } -} - -/*! - Marks the region of the widget as dirty (if not already marked as dirty) and - posts an UpdateRequest event to the top-level widget (if not already posted). - - If updateTime is UpdateNow, the event is sent immediately instead of posted. - - If bufferState is BufferInvalid, all widgets intersecting with the region will be dirty. - - If the widget paints directly on screen, the event is sent to the widget - instead of the top-level widget, and bufferState is completely ignored. - - ### Qt 4.6: Merge into a template function (after MSVC isn't supported anymore). -*/ -void QWidgetBackingStore::markDirty(const QRegion &rgn, QWidget *widget, - UpdateTime updateTime, BufferState bufferState) -{ - Q_ASSERT(tlw->d_func()->extra); - Q_ASSERT(tlw->d_func()->extra->topextra); - Q_ASSERT(!tlw->d_func()->extra->topextra->inTopLevelResize); - Q_ASSERT(widget->isVisible() && widget->updatesEnabled()); - Q_ASSERT(widget->window() == tlw); - Q_ASSERT(!rgn.isEmpty()); - -#if QT_CONFIG(graphicseffect) - widget->d_func()->invalidateGraphicsEffectsRecursively(); -#endif // QT_CONFIG(graphicseffect) - - if (widget->d_func()->paintOnScreen()) { - if (widget->d_func()->dirty.isEmpty()) { - widget->d_func()->dirty = rgn; - sendUpdateRequest(widget, updateTime); - return; - } else if (qt_region_strictContains(widget->d_func()->dirty, widget->rect())) { - if (updateTime == UpdateNow) - sendUpdateRequest(widget, updateTime); - return; // Already dirty. - } - - const bool eventAlreadyPosted = !widget->d_func()->dirty.isEmpty(); - widget->d_func()->dirty += rgn; - if (!eventAlreadyPosted || updateTime == UpdateNow) - sendUpdateRequest(widget, updateTime); - return; - } - - const QPoint offset = widget->mapTo(tlw, QPoint()); - - if (QWidgetPrivate::get(widget)->renderToTexture) { - if (!widget->d_func()->inDirtyList) - addDirtyRenderToTextureWidget(widget); - if (!updateRequestSent || updateTime == UpdateNow) - sendUpdateRequest(tlw, updateTime); - return; - } - - const QRect widgetRect = widget->d_func()->effectiveRectFor(widget->rect()); - if (qt_region_strictContains(dirty, widgetRect.translated(offset))) { - if (updateTime == UpdateNow) - sendUpdateRequest(tlw, updateTime); - return; // Already dirty. - } - - if (bufferState == BufferInvalid) { - const bool eventAlreadyPosted = !dirty.isEmpty() || updateRequestSent; -#if QT_CONFIG(graphicseffect) - if (widget->d_func()->graphicsEffect) - dirty += widget->d_func()->effectiveRectFor(rgn.boundingRect()).translated(offset); - else -#endif // QT_CONFIG(graphicseffect) - dirty += rgn.translated(offset); - if (!eventAlreadyPosted || updateTime == UpdateNow) - sendUpdateRequest(tlw, updateTime); - return; - } - - if (dirtyWidgets.isEmpty()) { - addDirtyWidget(widget, rgn); - sendUpdateRequest(tlw, updateTime); - return; - } - - if (widget->d_func()->inDirtyList) { - if (!qt_region_strictContains(widget->d_func()->dirty, widgetRect)) { -#if QT_CONFIG(graphicseffect) - if (widget->d_func()->graphicsEffect) - widget->d_func()->dirty += widget->d_func()->effectiveRectFor(rgn.boundingRect()); - else -#endif // QT_CONFIG(graphicseffect) - widget->d_func()->dirty += rgn; - } - } else { - addDirtyWidget(widget, rgn); - } - - if (updateTime == UpdateNow) - sendUpdateRequest(tlw, updateTime); -} - -/*! - This function is equivalent to calling markDirty(QRegion(rect), ...), but - is more efficient as it eliminates QRegion operations/allocations and can - use the rect more precisely for additional cut-offs. - - ### Qt 4.6: Merge into a template function (after MSVC isn't supported anymore). -*/ -void QWidgetBackingStore::markDirty(const QRect &rect, QWidget *widget, - UpdateTime updateTime, BufferState bufferState) -{ - Q_ASSERT(tlw->d_func()->extra); - Q_ASSERT(tlw->d_func()->extra->topextra); - Q_ASSERT(!tlw->d_func()->extra->topextra->inTopLevelResize); - Q_ASSERT(widget->isVisible() && widget->updatesEnabled()); - Q_ASSERT(widget->window() == tlw); - Q_ASSERT(!rect.isEmpty()); - -#if QT_CONFIG(graphicseffect) - widget->d_func()->invalidateGraphicsEffectsRecursively(); -#endif // QT_CONFIG(graphicseffect) - - if (widget->d_func()->paintOnScreen()) { - if (widget->d_func()->dirty.isEmpty()) { - widget->d_func()->dirty = QRegion(rect); - sendUpdateRequest(widget, updateTime); - return; - } else if (qt_region_strictContains(widget->d_func()->dirty, rect)) { - if (updateTime == UpdateNow) - sendUpdateRequest(widget, updateTime); - return; // Already dirty. - } - - const bool eventAlreadyPosted = !widget->d_func()->dirty.isEmpty(); - widget->d_func()->dirty += rect; - if (!eventAlreadyPosted || updateTime == UpdateNow) - sendUpdateRequest(widget, updateTime); - return; - } - - if (QWidgetPrivate::get(widget)->renderToTexture) { - if (!widget->d_func()->inDirtyList) - addDirtyRenderToTextureWidget(widget); - if (!updateRequestSent || updateTime == UpdateNow) - sendUpdateRequest(tlw, updateTime); - return; - } - - - const QRect widgetRect = widget->d_func()->effectiveRectFor(rect); - QRect translatedRect = widgetRect; - if (widget != tlw) - translatedRect.translate(widget->mapTo(tlw, QPoint())); - // Graphics effects may exceed window size, clamp. - translatedRect = translatedRect.intersected(QRect(QPoint(), tlw->size())); - if (qt_region_strictContains(dirty, translatedRect)) { - if (updateTime == UpdateNow) - sendUpdateRequest(tlw, updateTime); - return; // Already dirty - } - - if (bufferState == BufferInvalid) { - const bool eventAlreadyPosted = !dirty.isEmpty(); - dirty += translatedRect; - if (!eventAlreadyPosted || updateTime == UpdateNow) - sendUpdateRequest(tlw, updateTime); - return; - } - - if (dirtyWidgets.isEmpty()) { - addDirtyWidget(widget, rect); - sendUpdateRequest(tlw, updateTime); - return; - } - - if (widget->d_func()->inDirtyList) { - if (!qt_region_strictContains(widget->d_func()->dirty, widgetRect)) - widget->d_func()->dirty += widgetRect; - } else { - addDirtyWidget(widget, rect); - } - - if (updateTime == UpdateNow) - sendUpdateRequest(tlw, updateTime); -} - -/*! - Marks the \a region of the \a widget as dirty on screen. The \a region will be copied from - the backing store to the \a widget's native parent next time flush() is called. - - Paint on screen widgets are ignored. -*/ -void QWidgetBackingStore::markDirtyOnScreen(const QRegion ®ion, QWidget *widget, const QPoint &topLevelOffset) -{ - if (!widget || widget->d_func()->paintOnScreen() || region.isEmpty()) - return; - -#if 0 // Used to be included in Qt4 for Q_WS_MAC - if (!widget->testAttribute(Qt::WA_WState_InPaintEvent)) - dirtyOnScreen += region.translated(topLevelOffset); - return; -#endif - - // Top-level. - if (widget == tlw) { - if (!widget->testAttribute(Qt::WA_WState_InPaintEvent)) - dirtyOnScreen += region; - return; - } - - // Alien widgets. - if (!widget->internalWinId() && !widget->isWindow()) { - QWidget *nativeParent = widget->nativeParentWidget(); // Alien widgets with the top-level as the native parent (common case). - if (nativeParent == tlw) { - if (!widget->testAttribute(Qt::WA_WState_InPaintEvent)) - dirtyOnScreen += region.translated(topLevelOffset); - return; - } - - // Alien widgets with native parent != tlw. - QWidgetPrivate *nativeParentPrivate = nativeParent->d_func(); - if (!nativeParentPrivate->needsFlush) - nativeParentPrivate->needsFlush = new QRegion; - const QPoint nativeParentOffset = widget->mapTo(nativeParent, QPoint()); - *nativeParentPrivate->needsFlush += region.translated(nativeParentOffset); - appendDirtyOnScreenWidget(nativeParent); - return; - } - - // Native child widgets. - QWidgetPrivate *widgetPrivate = widget->d_func(); - if (!widgetPrivate->needsFlush) - widgetPrivate->needsFlush = new QRegion; - *widgetPrivate->needsFlush += region; - appendDirtyOnScreenWidget(widget); -} - -void QWidgetBackingStore::removeDirtyWidget(QWidget *w) -{ - if (!w) - return; - - dirtyWidgetsRemoveAll(w); - dirtyOnScreenWidgetsRemoveAll(w); - dirtyRenderToTextureWidgets.removeAll(w); - resetWidget(w); - - QWidgetPrivate *wd = w->d_func(); - const int n = wd->children.count(); - for (int i = 0; i < n; ++i) { - if (QWidget *child = qobject_cast(wd->children.at(i))) - removeDirtyWidget(child); - } -} - -void QWidgetBackingStore::updateLists(QWidget *cur) -{ - if (!cur) - return; - - QList children = cur->children(); - for (int i = 0; i < children.size(); ++i) { - QWidget *child = qobject_cast(children.at(i)); - if (!child || child->isWindow()) - continue; - - updateLists(child); - } - - if (cur->testAttribute(Qt::WA_StaticContents)) - addStaticWidget(cur); -} - -QWidgetBackingStore::QWidgetBackingStore(QWidget *topLevel) - : tlw(topLevel), - dirtyOnScreenWidgets(0), - updateRequestSent(0), - textureListWatcher(0), - perfFrames(0) -{ - store = tlw->backingStore(); - Q_ASSERT(store); - - // Ensure all existing subsurfaces and static widgets are added to their respective lists. - updateLists(topLevel); -} - -QWidgetBackingStore::~QWidgetBackingStore() -{ - for (int c = 0; c < dirtyWidgets.size(); ++c) - resetWidget(dirtyWidgets.at(c)); - for (int c = 0; c < dirtyRenderToTextureWidgets.size(); ++c) - resetWidget(dirtyRenderToTextureWidgets.at(c)); - - delete dirtyOnScreenWidgets; -} - -static QVector getSortedRectsToScroll(const QRegion ®ion, int dx, int dy) -{ - QVector rects; - std::copy(region.begin(), region.end(), std::back_inserter(rects)); - if (rects.count() > 1) { - std::sort(rects.begin(), rects.end(), [=](const QRect &r1, const QRect &r2) { - if (r1.y() == r2.y()) { - if (dx > 0) - return r1.x() > r2.x(); - return r1.x() < r2.x(); - } - if (dy > 0) - return r1.y() > r2.y(); - return r1.y() < r2.y(); - }); - } - return rects; -} - -//parent's coordinates; move whole rect; update parent and widget -//assume the screen blt has already been done, so we don't need to refresh that part -void QWidgetPrivate::moveRect(const QRect &rect, int dx, int dy) -{ - Q_Q(QWidget); - if (!q->isVisible() || (dx == 0 && dy == 0)) - return; - - QWidget *tlw = q->window(); - QTLWExtra* x = tlw->d_func()->topData(); - if (x->inTopLevelResize) - return; - - static const bool accelEnv = qEnvironmentVariableIntValue("QT_NO_FAST_MOVE") == 0; - - QWidget *pw = q->parentWidget(); - QPoint toplevelOffset = pw->mapTo(tlw, QPoint()); - QWidgetPrivate *pd = pw->d_func(); - QRect clipR(pd->clipRect()); - const QRect newRect(rect.translated(dx, dy)); - QRect destRect = rect.intersected(clipR); - if (destRect.isValid()) - destRect = destRect.translated(dx, dy).intersected(clipR); - const QRect sourceRect(destRect.translated(-dx, -dy)); - const QRect parentRect(rect & clipR); - const bool nativeWithTextureChild = textureChildSeen && q->internalWinId(); - - const bool accelerateMove = accelEnv && isOpaque && !nativeWithTextureChild -#if QT_CONFIG(graphicsview) - // No accelerate move for proxy widgets. - && !tlw->d_func()->extra->proxyWidget -#endif - ; - - if (!accelerateMove) { - QRegion parentR(effectiveRectFor(parentRect)); - if (!extra || !extra->hasMask) { - parentR -= newRect; - } else { - // invalidateBuffer() excludes anything outside the mask - parentR += newRect & clipR; - } - pd->invalidateBuffer(parentR); - invalidateBuffer((newRect & clipR).translated(-data.crect.topLeft())); - } else { - - QWidgetBackingStore *wbs = x->backingStoreTracker.data(); - QRegion childExpose(newRect & clipR); - QRegion overlappedExpose; - - if (sourceRect.isValid()) { - overlappedExpose = (overlappedRegion(sourceRect) | overlappedRegion(destRect)) & clipR; - - const qreal factor = QHighDpiScaling::factor(q->windowHandle()); - if (overlappedExpose.isEmpty() || qFloor(factor) == factor) { - const QVector rectsToScroll - = getSortedRectsToScroll(QRegion(sourceRect) - overlappedExpose, dx, dy); - for (QRect rect : rectsToScroll) { - if (wbs->bltRect(rect, dx, dy, pw)) { - childExpose -= rect.translated(dx, dy); - } - } - } - - childExpose -= overlappedExpose; - } - - if (!pw->updatesEnabled()) - return; - - const bool childUpdatesEnabled = q->updatesEnabled(); - if (childUpdatesEnabled) { - if (!overlappedExpose.isEmpty()) { - overlappedExpose.translate(-data.crect.topLeft()); - invalidateBuffer(overlappedExpose); - } - if (!childExpose.isEmpty()) { - childExpose.translate(-data.crect.topLeft()); - wbs->markDirty(childExpose, q); - isMoved = true; - } - } - - QRegion parentExpose(parentRect); - parentExpose -= newRect; - if (extra && extra->hasMask) - parentExpose += QRegion(newRect) - extra->mask.translated(data.crect.topLeft()); - - if (!parentExpose.isEmpty()) { - wbs->markDirty(parentExpose, pw); - pd->isMoved = true; - } - - if (childUpdatesEnabled) { - QRegion needsFlush(sourceRect); - needsFlush += destRect; - wbs->markDirtyOnScreen(needsFlush, pw, toplevelOffset); - } - } -} - -//widget's coordinates; scroll within rect; only update widget -void QWidgetPrivate::scrollRect(const QRect &rect, int dx, int dy) -{ - Q_Q(QWidget); - QWidget *tlw = q->window(); - QTLWExtra* x = tlw->d_func()->topData(); - if (x->inTopLevelResize) - return; - - QWidgetBackingStore *wbs = x->backingStoreTracker.data(); - if (!wbs) - return; - - static const bool accelEnv = qEnvironmentVariableIntValue("QT_NO_FAST_SCROLL") == 0; - - const QRect clipR = clipRect(); - const QRect scrollRect = rect & clipR; - const bool accelerateScroll = accelEnv && isOpaque && !q_func()->testAttribute(Qt::WA_WState_InPaintEvent); - - if (!accelerateScroll) { - if (!overlappedRegion(scrollRect.translated(data.crect.topLeft()), true).isEmpty()) { - QRegion region(scrollRect); - subtractOpaqueSiblings(region); - invalidateBuffer(region); - }else { - invalidateBuffer(scrollRect); - } - } else { - const QPoint toplevelOffset = q->mapTo(tlw, QPoint()); - const QRect destRect = scrollRect.translated(dx, dy) & scrollRect; - const QRect sourceRect = destRect.translated(-dx, -dy); - - const QRegion overlappedExpose = (overlappedRegion(scrollRect.translated(data.crect.topLeft()))) - .translated(-data.crect.topLeft()) & clipR; - QRegion childExpose(scrollRect); - - const qreal factor = QHighDpiScaling::factor(q->windowHandle()); - if (overlappedExpose.isEmpty() || qFloor(factor) == factor) { - const QVector rectsToScroll - = getSortedRectsToScroll(QRegion(sourceRect) - overlappedExpose, dx, dy); - for (const QRect &rect : rectsToScroll) { - if (wbs->bltRect(rect, dx, dy, q)) { - childExpose -= rect.translated(dx, dy); - } - } - } - - childExpose -= overlappedExpose; - - if (inDirtyList) { - if (rect == q->rect()) { - dirty.translate(dx, dy); - } else { - QRegion dirtyScrollRegion = dirty.intersected(scrollRect); - if (!dirtyScrollRegion.isEmpty()) { - dirty -= dirtyScrollRegion; - dirtyScrollRegion.translate(dx, dy); - dirty += dirtyScrollRegion; - } - } - } - - if (!q->updatesEnabled()) - return; - - if (!overlappedExpose.isEmpty()) - invalidateBuffer(overlappedExpose); - if (!childExpose.isEmpty()) { - wbs->markDirty(childExpose, q); - isScrolled = true; - } - - // Instead of using native scroll-on-screen, we copy from - // backingstore, giving only one screen update for each - // scroll, and a solid appearance - wbs->markDirtyOnScreen(destRect, q, toplevelOffset); - } -} - -#ifndef QT_NO_OPENGL -static void findTextureWidgetsRecursively(QWidget *tlw, QWidget *widget, QPlatformTextureList *widgetTextures, QVector *nativeChildren) -{ - QWidgetPrivate *wd = QWidgetPrivate::get(widget); - if (wd->renderToTexture) { - QPlatformTextureList::Flags flags = wd->textureListFlags(); - const QRect rect(widget->mapTo(tlw, QPoint()), widget->size()); - widgetTextures->appendTexture(widget, wd->textureId(), rect, wd->clipRect(), flags); - } - - for (int i = 0; i < wd->children.size(); ++i) { - QWidget *w = qobject_cast(wd->children.at(i)); - // Stop at native widgets but store them. Stop at hidden widgets too. - if (w && !w->isWindow() && w->internalWinId()) - nativeChildren->append(w); - if (w && !w->isWindow() && !w->internalWinId() && !w->isHidden() && QWidgetPrivate::get(w)->textureChildSeen) - findTextureWidgetsRecursively(tlw, w, widgetTextures, nativeChildren); - } -} - -static void findAllTextureWidgetsRecursively(QWidget *tlw, QWidget *widget) -{ - // textureChildSeen does not take native child widgets into account and that's good. - if (QWidgetPrivate::get(widget)->textureChildSeen) { - QVector nativeChildren; - QScopedPointer tl(new QPlatformTextureList); - // Look for texture widgets (incl. widget itself) from 'widget' down, - // but skip subtrees with a parent of a native child widget. - findTextureWidgetsRecursively(tlw, widget, tl.data(), &nativeChildren); - // tl may be empty regardless of textureChildSeen if we have native or hidden children. - if (!tl->isEmpty()) - QWidgetPrivate::get(tlw)->topData()->widgetTextures.append(tl.take()); - // Native child widgets, if there was any, get their own separate QPlatformTextureList. - foreach (QWidget *ncw, nativeChildren) { - if (QWidgetPrivate::get(ncw)->textureChildSeen) - findAllTextureWidgetsRecursively(tlw, ncw); - } - } -} - -static QPlatformTextureList *widgetTexturesFor(QWidget *tlw, QWidget *widget) -{ - foreach (QPlatformTextureList *tl, QWidgetPrivate::get(tlw)->topData()->widgetTextures) { - Q_ASSERT(!tl->isEmpty()); - for (int i = 0; i < tl->count(); ++i) { - QWidget *w = static_cast(tl->source(i)); - if ((w->internalWinId() && w == widget) || (!w->internalWinId() && w->nativeParentWidget() == widget)) - return tl; - } - } - - if (QWidgetPrivate::get(widget)->textureChildSeen) { - // No render-to-texture widgets in the (sub-)tree due to hidden or native - // children. Returning null results in using the normal backingstore flush path - // without OpenGL-based compositing. This is very desirable normally. However, - // some platforms cannot handle switching between the non-GL and GL paths for - // their windows so it has to be opt-in. - static bool switchableWidgetComposition = - QGuiApplicationPrivate::instance()->platformIntegration() - ->hasCapability(QPlatformIntegration::SwitchableWidgetComposition); - if (!switchableWidgetComposition) - return qt_dummy_platformTextureList(); - } - - return 0; -} - -// Watches one or more QPlatformTextureLists for changes in the lock state and -// triggers a backingstore sync when all the registered lists turn into -// unlocked state. This is essential when a custom composeAndFlush() -// implementation in a platform plugin is not synchronous and keeps -// holding on to the textures for some time even after returning from there. -QPlatformTextureListWatcher::QPlatformTextureListWatcher(QWidgetBackingStore *backingStore) - : m_backingStore(backingStore) -{ -} - -void QPlatformTextureListWatcher::watch(QPlatformTextureList *textureList) -{ - connect(textureList, SIGNAL(locked(bool)), SLOT(onLockStatusChanged(bool))); - m_locked[textureList] = textureList->isLocked(); -} - -bool QPlatformTextureListWatcher::isLocked() const -{ - foreach (bool v, m_locked) { - if (v) - return true; - } - return false; -} - -void QPlatformTextureListWatcher::onLockStatusChanged(bool locked) -{ - QPlatformTextureList *tl = static_cast(sender()); - m_locked[tl] = locked; - if (!isLocked()) - m_backingStore->sync(); -} - -#else - -static QPlatformTextureList *widgetTexturesFor(QWidget *tlw, QWidget *widget) -{ - Q_UNUSED(tlw); - Q_UNUSED(widget); - return nullptr; -} - -#endif // QT_NO_OPENGL - -static inline bool discardSyncRequest(QWidget *tlw, QTLWExtra *tlwExtra) -{ - if (!tlw || !tlwExtra || !tlw->testAttribute(Qt::WA_Mapped) || !tlw->isVisible()) - return true; - - return false; -} - -bool QWidgetBackingStore::syncAllowed() -{ -#ifndef QT_NO_OPENGL - QTLWExtra *tlwExtra = tlw->d_func()->maybeTopData(); - if (textureListWatcher && !textureListWatcher->isLocked()) { - textureListWatcher->deleteLater(); - textureListWatcher = 0; - } else if (!tlwExtra->widgetTextures.isEmpty()) { - bool skipSync = false; - foreach (QPlatformTextureList *tl, tlwExtra->widgetTextures) { - if (tl->isLocked()) { - if (!textureListWatcher) - textureListWatcher = new QPlatformTextureListWatcher(this); - if (!textureListWatcher->isLocked()) - textureListWatcher->watch(tl); - skipSync = true; - } - } - if (skipSync) // cannot compose due to widget textures being in use - return false; - } -#endif - return true; -} - -/*! - Synchronizes the \a exposedRegion of the \a exposedWidget with the backing store. - - If there's nothing to repaint, the area is flushed and painting does not occur; - otherwise the area is marked as dirty on screen and will be flushed right after - we are done with all painting. -*/ -void QWidgetBackingStore::sync(QWidget *exposedWidget, const QRegion &exposedRegion) -{ - QTLWExtra *tlwExtra = tlw->d_func()->maybeTopData(); - if (!tlw->isVisible() || !tlwExtra || tlwExtra->inTopLevelResize) - return; - - if (!exposedWidget || !exposedWidget->internalWinId() || !exposedWidget->isVisible() || !exposedWidget->testAttribute(Qt::WA_Mapped) - || !exposedWidget->updatesEnabled() || exposedRegion.isEmpty()) { - return; - } - - // Nothing to repaint. - if (!isDirty() && store->size().isValid()) { - QPlatformTextureList *tl = widgetTexturesFor(tlw, exposedWidget); - qt_flush(exposedWidget, tl ? QRegion() : exposedRegion, store, tlw, tl, this); - return; - } - - if (exposedWidget != tlw) - markDirtyOnScreen(exposedRegion, exposedWidget, exposedWidget->mapTo(tlw, QPoint())); - else - markDirtyOnScreen(exposedRegion, exposedWidget, QPoint()); - - if (syncAllowed()) - doSync(); -} - -/*! - Synchronizes the backing store, i.e. dirty areas are repainted and flushed. -*/ -void QWidgetBackingStore::sync() -{ - updateRequestSent = false; - QTLWExtra *tlwExtra = tlw->d_func()->maybeTopData(); - if (discardSyncRequest(tlw, tlwExtra)) { - // If the top-level is minimized, it's not visible on the screen so we can delay the - // update until it's shown again. In order to do that we must keep the dirty states. - // These will be cleared when we receive the first expose after showNormal(). - // However, if the widget is not visible (isVisible() returns false), everything will - // be invalidated once the widget is shown again, so clear all dirty states. - if (!tlw->isVisible()) { - dirty = QRegion(); - for (int i = 0; i < dirtyWidgets.size(); ++i) - resetWidget(dirtyWidgets.at(i)); - dirtyWidgets.clear(); - } - return; - } - - if (syncAllowed()) - doSync(); -} - -void QWidgetBackingStore::doSync() -{ - const bool updatesDisabled = !tlw->updatesEnabled(); - bool repaintAllWidgets = false; - - const bool inTopLevelResize = tlw->d_func()->maybeTopData()->inTopLevelResize; - const QRect tlwRect(topLevelRect()); - const QRect surfaceGeometry(tlwRect.topLeft(), store->size()); - if ((inTopLevelResize || surfaceGeometry.size() != tlwRect.size()) && !updatesDisabled) { - if (hasStaticContents() && !store->size().isEmpty() ) { - // Repaint existing dirty area and newly visible area. - const QRect clipRect(0, 0, surfaceGeometry.width(), surfaceGeometry.height()); - const QRegion staticRegion(staticContents(0, clipRect)); - QRegion newVisible(0, 0, tlwRect.width(), tlwRect.height()); - newVisible -= staticRegion; - dirty += newVisible; - store->setStaticContents(staticRegion); - } else { - // Repaint everything. - dirty = QRegion(0, 0, tlwRect.width(), tlwRect.height()); - for (int i = 0; i < dirtyWidgets.size(); ++i) - resetWidget(dirtyWidgets.at(i)); - dirtyWidgets.clear(); - repaintAllWidgets = true; - } - } - - if (inTopLevelResize || surfaceGeometry.size() != tlwRect.size()) - store->resize(tlwRect.size()); - - if (updatesDisabled) - return; - - // Contains everything that needs repaint. - QRegion toClean(dirty); - - // Loop through all update() widgets and remove them from the list before they are - // painted (in case someone calls update() in paintEvent). If the widget is opaque - // and does not have transparent overlapping siblings, append it to the - // opaqueNonOverlappedWidgets list and paint it directly without composition. - QVarLengthArray opaqueNonOverlappedWidgets; - for (int i = 0; i < dirtyWidgets.size(); ++i) { - QWidget *w = dirtyWidgets.at(i); - QWidgetPrivate *wd = w->d_func(); - if (wd->data.in_destructor) - continue; - - // Clip with mask() and clipRect(). - wd->dirty &= wd->clipRect(); - wd->clipToEffectiveMask(wd->dirty); - - // Subtract opaque siblings and children. - bool hasDirtySiblingsAbove = false; - // We know for sure that the widget isn't overlapped if 'isMoved' is true. - if (!wd->isMoved) - wd->subtractOpaqueSiblings(wd->dirty, &hasDirtySiblingsAbove); - - // Make a copy of the widget's dirty region, to restore it in case there is an opaque - // render-to-texture child that completely covers the widget, because otherwise the - // render-to-texture child won't be visible, due to its parent widget not being redrawn - // with a proper blending mask. - const QRegion dirtyBeforeSubtractedOpaqueChildren = wd->dirty; - - // Scrolled and moved widgets must draw all children. - if (!wd->isScrolled && !wd->isMoved) - wd->subtractOpaqueChildren(wd->dirty, w->rect()); - - if (wd->dirty.isEmpty() && wd->textureChildSeen) - wd->dirty = dirtyBeforeSubtractedOpaqueChildren; - - if (wd->dirty.isEmpty()) { - resetWidget(w); - continue; - } - - const QRegion widgetDirty(w != tlw ? wd->dirty.translated(w->mapTo(tlw, QPoint())) - : wd->dirty); - toClean += widgetDirty; - -#if QT_CONFIG(graphicsview) - if (tlw->d_func()->extra->proxyWidget) { - resetWidget(w); - continue; - } -#endif - - if (!hasDirtySiblingsAbove && wd->isOpaque && !dirty.intersects(widgetDirty.boundingRect())) { - opaqueNonOverlappedWidgets.append(w); - } else { - resetWidget(w); - dirty += widgetDirty; - } - } - dirtyWidgets.clear(); - -#ifndef QT_NO_OPENGL - // Find all render-to-texture child widgets (including self). - // The search is cut at native widget boundaries, meaning that each native child widget - // has its own list for the subtree below it. - QTLWExtra *tlwExtra = tlw->d_func()->topData(); - qDeleteAll(tlwExtra->widgetTextures); - tlwExtra->widgetTextures.clear(); - findAllTextureWidgetsRecursively(tlw, tlw); - qt_window_private(tlw->windowHandle())->compositing = false; // will get updated in qt_flush() -#endif - - if (toClean.isEmpty()) { - // Nothing to repaint. However renderToTexture widgets are handled - // specially, they are not in the regular dirty list, in order to - // prevent triggering unnecessary backingstore painting when only the - // OpenGL content changes. Check if we have such widgets in the special - // dirty list. - QVarLengthArray paintPending; - const int numPaintPending = dirtyRenderToTextureWidgets.count(); - paintPending.reserve(numPaintPending); - for (int i = 0; i < numPaintPending; ++i) { - QWidget *w = dirtyRenderToTextureWidgets.at(i); - paintPending << w; - resetWidget(w); - } - dirtyRenderToTextureWidgets.clear(); - for (int i = 0; i < numPaintPending; ++i) { - QWidget *w = paintPending[i]; - w->d_func()->sendPaintEvent(w->rect()); - if (w != tlw) { - QWidget *npw = w->nativeParentWidget(); - if (w->internalWinId() || (npw && npw != tlw)) { - if (!w->internalWinId()) - w = npw; - QWidgetPrivate *wPrivate = w->d_func(); - if (!wPrivate->needsFlush) - wPrivate->needsFlush = new QRegion; - appendDirtyOnScreenWidget(w); - } - } - } - - // We might have newly exposed areas on the screen if this function was - // called from sync(QWidget *, QRegion)), so we have to make sure those - // are flushed. We also need to composite the renderToTexture widgets. - flush(); - - return; - } - -#ifndef QT_NO_OPENGL - foreach (QPlatformTextureList *tl, tlwExtra->widgetTextures) { - for (int i = 0; i < tl->count(); ++i) { - QWidget *w = static_cast(tl->source(i)); - if (dirtyRenderToTextureWidgets.contains(w)) { - const QRect rect = tl->geometry(i); // mapped to the tlw already - // Set a flag to indicate that the paint event for this - // render-to-texture widget must not to be optimized away. - w->d_func()->renderToTextureReallyDirty = 1; - dirty += rect; - toClean += rect; - } - } - } - for (int i = 0; i < dirtyRenderToTextureWidgets.count(); ++i) - resetWidget(dirtyRenderToTextureWidgets.at(i)); - dirtyRenderToTextureWidgets.clear(); -#endif - -#if QT_CONFIG(graphicsview) - if (tlw->d_func()->extra->proxyWidget) { - updateStaticContentsSize(); - dirty = QRegion(); - updateRequestSent = false; - for (const QRect &rect : toClean) - tlw->d_func()->extra->proxyWidget->update(rect); - return; - } -#endif - - BeginPaintInfo beginPaintInfo; - beginPaint(toClean, tlw, store, &beginPaintInfo); - if (beginPaintInfo.nothingToPaint) { - for (int i = 0; i < opaqueNonOverlappedWidgets.size(); ++i) - resetWidget(opaqueNonOverlappedWidgets[i]); - dirty = QRegion(); - updateRequestSent = false; - return; - } - - // Must do this before sending any paint events because - // the size may change in the paint event. - updateStaticContentsSize(); - const QRegion dirtyCopy(dirty); - dirty = QRegion(); - updateRequestSent = false; - - // Paint opaque non overlapped widgets. - for (int i = 0; i < opaqueNonOverlappedWidgets.size(); ++i) { - QWidget *w = opaqueNonOverlappedWidgets[i]; - QWidgetPrivate *wd = w->d_func(); - - int flags = QWidgetPrivate::DrawRecursive; - // Scrolled and moved widgets must draw all children. - if (!wd->isScrolled && !wd->isMoved) - flags |= QWidgetPrivate::DontDrawOpaqueChildren; - if (w == tlw) - flags |= QWidgetPrivate::DrawAsRoot; - - QRegion toBePainted(wd->dirty); - resetWidget(w); - - QPoint offset; - if (w != tlw) - offset += w->mapTo(tlw, QPoint()); - wd->drawWidget(store->paintDevice(), toBePainted, offset, flags, 0, this); - } - - // Paint the rest with composition. - if (repaintAllWidgets || !dirtyCopy.isEmpty()) { - const int flags = QWidgetPrivate::DrawAsRoot | QWidgetPrivate::DrawRecursive; - tlw->d_func()->drawWidget(store->paintDevice(), dirtyCopy, QPoint(), flags, 0, this); - } - - endPaint(toClean, store, &beginPaintInfo); -} - -/*! - Flushes the contents of the backing store into the top-level widget. - If the \a widget is non-zero, the content is flushed to the \a widget. - If the \a surface is non-zero, the content of the \a surface is flushed. -*/ -void QWidgetBackingStore::flush(QWidget *widget) -{ - const bool hasDirtyOnScreenWidgets = dirtyOnScreenWidgets && !dirtyOnScreenWidgets->isEmpty(); - bool flushed = false; - - // Flush the region in dirtyOnScreen. - if (!dirtyOnScreen.isEmpty()) { - QWidget *target = widget ? widget : tlw; - qt_flush(target, dirtyOnScreen, store, tlw, widgetTexturesFor(tlw, tlw), this); - dirtyOnScreen = QRegion(); - flushed = true; - } - - // Render-to-texture widgets are not in dirtyOnScreen so flush if we have not done it above. - if (!flushed && !hasDirtyOnScreenWidgets) { -#ifndef QT_NO_OPENGL - if (!tlw->d_func()->topData()->widgetTextures.isEmpty()) { - QPlatformTextureList *tl = widgetTexturesFor(tlw, tlw); - if (tl) { - QWidget *target = widget ? widget : tlw; - qt_flush(target, QRegion(), store, tlw, tl, this); - } - } -#endif - } - - if (!hasDirtyOnScreenWidgets) - return; - - for (int i = 0; i < dirtyOnScreenWidgets->size(); ++i) { - QWidget *w = dirtyOnScreenWidgets->at(i); - QWidgetPrivate *wd = w->d_func(); - Q_ASSERT(wd->needsFlush); - QPlatformTextureList *widgetTexturesForNative = wd->textureChildSeen ? widgetTexturesFor(tlw, w) : 0; - qt_flush(w, *wd->needsFlush, store, tlw, widgetTexturesForNative, this); - *wd->needsFlush = QRegion(); - } - dirtyOnScreenWidgets->clear(); -} - -static inline bool discardInvalidateBufferRequest(QWidget *widget, QTLWExtra *tlwExtra) -{ - Q_ASSERT(widget); - if (QApplication::closingDown()) - return true; - - if (!tlwExtra || tlwExtra->inTopLevelResize || !tlwExtra->backingStore) - return true; - - if (!widget->isVisible() || !widget->updatesEnabled()) - return true; - - return false; -} - -/*! - Invalidates the buffer when the widget is resized. - Static areas are never invalidated unless absolutely needed. -*/ -void QWidgetPrivate::invalidateBuffer_resizeHelper(const QPoint &oldPos, const QSize &oldSize) -{ - Q_Q(QWidget); - Q_ASSERT(!q->isWindow()); - Q_ASSERT(q->parentWidget()); - - const bool staticContents = q->testAttribute(Qt::WA_StaticContents); - const bool sizeDecreased = (data.crect.width() < oldSize.width()) - || (data.crect.height() < oldSize.height()); - - const QPoint offset(data.crect.x() - oldPos.x(), data.crect.y() - oldPos.y()); - const bool parentAreaExposed = !offset.isNull() || sizeDecreased; - const QRect newWidgetRect(q->rect()); - const QRect oldWidgetRect(0, 0, oldSize.width(), oldSize.height()); - - if (!staticContents || graphicsEffect) { - QRegion staticChildren; - QWidgetBackingStore *bs = 0; - if (offset.isNull() && (bs = maybeBackingStore())) - staticChildren = bs->staticContents(q, oldWidgetRect); - const bool hasStaticChildren = !staticChildren.isEmpty(); - - if (hasStaticChildren) { - QRegion dirty(newWidgetRect); - dirty -= staticChildren; - invalidateBuffer(dirty); - } else { - // Entire widget needs repaint. - invalidateBuffer(newWidgetRect); - } - - if (!parentAreaExposed) - return; - - // Invalidate newly exposed area of the parent. - if (!graphicsEffect && extra && extra->hasMask) { - QRegion parentExpose(extra->mask.translated(oldPos)); - parentExpose &= QRect(oldPos, oldSize); - if (hasStaticChildren) - parentExpose -= data.crect; // Offset is unchanged, safe to do this. - q->parentWidget()->d_func()->invalidateBuffer(parentExpose); - } else { - if (hasStaticChildren && !graphicsEffect) { - QRegion parentExpose(QRect(oldPos, oldSize)); - parentExpose -= data.crect; // Offset is unchanged, safe to do this. - q->parentWidget()->d_func()->invalidateBuffer(parentExpose); - } else { - q->parentWidget()->d_func()->invalidateBuffer(effectiveRectFor(QRect(oldPos, oldSize))); - } - } - return; - } - - // Move static content to its new position. - if (!offset.isNull()) { - if (sizeDecreased) { - const QSize minSize(qMin(oldSize.width(), data.crect.width()), - qMin(oldSize.height(), data.crect.height())); - moveRect(QRect(oldPos, minSize), offset.x(), offset.y()); - } else { - moveRect(QRect(oldPos, oldSize), offset.x(), offset.y()); - } - } - - // Invalidate newly visible area of the widget. - if (!sizeDecreased || !oldWidgetRect.contains(newWidgetRect)) { - QRegion newVisible(newWidgetRect); - newVisible -= oldWidgetRect; - invalidateBuffer(newVisible); - } - - if (!parentAreaExposed) - return; - - // Invalidate newly exposed area of the parent. - const QRect oldRect(oldPos, oldSize); - if (extra && extra->hasMask) { - QRegion parentExpose(oldRect); - parentExpose &= extra->mask.translated(oldPos); - parentExpose -= (extra->mask.translated(data.crect.topLeft()) & data.crect); - q->parentWidget()->d_func()->invalidateBuffer(parentExpose); - } else { - QRegion parentExpose(oldRect); - parentExpose -= data.crect; - q->parentWidget()->d_func()->invalidateBuffer(parentExpose); - } -} - -/*! - Invalidates the \a rgn (in widget's coordinates) of the backing store, i.e. - all widgets intersecting with the region will be repainted when the backing store - is synced. - - ### Qt 4.6: Merge into a template function (after MSVC isn't supported anymore). -*/ -void QWidgetPrivate::invalidateBuffer(const QRegion &rgn) -{ - Q_Q(QWidget); - - QTLWExtra *tlwExtra = q->window()->d_func()->maybeTopData(); - if (discardInvalidateBufferRequest(q, tlwExtra) || rgn.isEmpty()) - return; - - QRegion wrgn(rgn); - wrgn &= clipRect(); - if (!graphicsEffect && extra && extra->hasMask) - wrgn &= extra->mask; - if (wrgn.isEmpty()) - return; - - tlwExtra->backingStoreTracker->markDirty(wrgn, q, - QWidgetBackingStore::UpdateLater, QWidgetBackingStore::BufferInvalid); -} - -/*! - This function is equivalent to calling invalidateBuffer(QRegion(rect), ...), but - is more efficient as it eliminates QRegion operations/allocations and can - use the rect more precisely for additional cut-offs. - - ### Qt 4.6: Merge into a template function (after MSVC isn't supported anymore). -*/ -void QWidgetPrivate::invalidateBuffer(const QRect &rect) -{ - Q_Q(QWidget); - - QTLWExtra *tlwExtra = q->window()->d_func()->maybeTopData(); - if (discardInvalidateBufferRequest(q, tlwExtra) || rect.isEmpty()) - return; - - QRect wRect(rect); - wRect &= clipRect(); - if (wRect.isEmpty()) - return; - - if (graphicsEffect || !extra || !extra->hasMask) { - tlwExtra->backingStoreTracker->markDirty(wRect, q, - QWidgetBackingStore::UpdateLater, QWidgetBackingStore::BufferInvalid); - return; - } - - QRegion wRgn(extra->mask); - wRgn &= wRect; - if (wRgn.isEmpty()) - return; - - tlwExtra->backingStoreTracker->markDirty(wRgn, q, - QWidgetBackingStore::UpdateLater, QWidgetBackingStore::BufferInvalid); -} - -void QWidgetPrivate::repaint_sys(const QRegion &rgn) -{ - if (data.in_destructor) - return; - - Q_Q(QWidget); - if (discardSyncRequest(q, maybeTopData())) - return; - - if (q->testAttribute(Qt::WA_StaticContents)) { - if (!extra) - createExtra(); - extra->staticContentsSize = data.crect.size(); - } - - QPaintEngine *engine = q->paintEngine(); - - // QGLWidget does not support partial updates if: - // 1) The context is double buffered - // 2) The context is single buffered and auto-fill background is enabled. - const bool noPartialUpdateSupport = (engine && (engine->type() == QPaintEngine::OpenGL - || engine->type() == QPaintEngine::OpenGL2)) - && (usesDoubleBufferedGLContext || q->autoFillBackground()); - QRegion toBePainted(noPartialUpdateSupport ? q->rect() : rgn); - -#if 0 // Used to be included in Qt4 for Q_WS_MAC - // No difference between update() and repaint() on the Mac. - update_sys(toBePainted); - return; -#endif - - toBePainted &= clipRect(); - clipToEffectiveMask(toBePainted); - if (toBePainted.isEmpty()) - return; // Nothing to repaint. - -#ifndef QT_NO_PAINT_DEBUG - bool flushed = QWidgetBackingStore::flushPaint(q, toBePainted); -#endif - - drawWidget(q, toBePainted, QPoint(), QWidgetPrivate::DrawAsRoot | QWidgetPrivate::DrawPaintOnScreen, 0); - -#ifndef QT_NO_PAINT_DEBUG - if (flushed) - QWidgetBackingStore::unflushPaint(q, toBePainted); -#endif - - if (Q_UNLIKELY(q->paintingActive())) - qWarning("QWidget::repaint: It is dangerous to leave painters active on a widget outside of the PaintEvent"); -} - - -QT_END_NAMESPACE - -#include "moc_qwidgetbackingstore_p.cpp" diff --git a/5.12.1/bin/7z.exe b/5.12.1/bin/7z.exe deleted file mode 100644 index d3fe532..0000000 Binary files a/5.12.1/bin/7z.exe and /dev/null differ diff --git a/5.12.1/bin/wget.exe b/5.12.1/bin/wget.exe deleted file mode 100644 index cda6b94..0000000 Binary files a/5.12.1/bin/wget.exe and /dev/null differ diff --git a/5.12.1/compile_mac.sh b/5.12.1/compile_mac.sh deleted file mode 100644 index 567aa36..0000000 --- a/5.12.1/compile_mac.sh +++ /dev/null @@ -1,72 +0,0 @@ -#!/bin/bash - -# requirements: -# 1. brew -# 2. brew install llvm (https://bugreports.qt.io/browse/QTBUG-66353) - -export PATH=$PATH:/usr/local/Qt-5.12.1/bin - -cd qtbase - -if [[ $1 == openssl ]]; then - - # download openssl - curl -O https://www.openssl.org/source/openssl-1.1.1a.tar.gz - tar -xvzf openssl-1.1.1a.tar.gz - - # compile openssl - cd openssl-1.1.1a - ./Configure darwin64-x86_64-cc --prefix=$PWD/dist - make - # print arch info (optional) - lipo -info libssl.a - lipo -info libcrypto.a - make install - cd .. - - # continue - - OPENSSL_LIBS='-L$PWD/openssl-1.1.1a/dist/lib -lssl -lcrypto' ./configure -opensource -confirm-license -no-securetransport -nomake examples -nomake tests -openssl-linked -I $PWD/openssl-1.1.1a/dist/include -L $PWD/openssl-1.1.1a/dist/lib - -elif [[ $1 == securetransport ]]; then - - ./configure -opensource -confirm-license -nomake examples -nomake tests -no-openssl -securetransport - -else - - echo "Error: please specify which SSL layer to use (openssl or securetransport)" - exit 1 - -fi - -make -j 12 -echo maki | sudo -S sudo make install - -cd ../qttools -qmake -make -j 12 -echo maki | sudo -S sudo make install - -cd ../qtmacextras -qmake -make -j 12 -echo maki | sudo -S sudo make install - -cd ../qtdeclarative/src -qmake -make -j 12 sub-qmldevtools -echo maki | sudo -S sudo make install - -# make docs - currently doesnt work - -#cd ../qtbase -#make -j 12 docs -#cd ../qttools -#make -j 12 docs -#cd ../qtmacextras -#make -j 12 docs - -#echo maki | sudo -S cp -f -r ../qtbase/doc /usr/local/Qt-5.12.1/ - -cd /usr/local -zip -r ~/Desktop/qt5.12.1_mac.zip Qt-5.12.1/* \ No newline at end of file diff --git a/5.12.1/compile_win.pl b/5.12.1/compile_win.pl deleted file mode 100644 index 04ad716..0000000 --- a/5.12.1/compile_win.pl +++ /dev/null @@ -1,86 +0,0 @@ -use strict; - -die "Cannot proceed without the 'bin' folder'" if (!-e "bin"); - -my $arch = $ARGV[0]; -my $openssl_v_major = "1.1.1"; # The 1.1.1 series is Long Term Support (LTS) release, supported until 11th September 2023 -my $openssl_v_minor = "a"; -my $openssl_version = "$openssl_v_major$openssl_v_minor"; -my $openssl_dir = "openssl-$openssl_version"; -my $openssl_download = "https://www.openssl.org/source/openssl-$openssl_version.tar.gz"; -my $openssl_arch = $arch eq "amd64" ? "WIN64A" : "WIN32"; - -$arch = "x86" if ($arch eq ''); # specify x86 is nothing is specified -die "Please specify architecture (x86 or amd64)" if ($arch ne "x86" && $arch ne "amd64"); # die if user specified anything except x86 or amd64 - -# will create a batch file - -my $batfile = 'compile_win.bat'; - -open BAT, '>', $batfile; - -printLineToBat ("SET PATH=%PATH%;%cd%\\bin"); # add bin folder to the path for 7z and wget -printLineToBat ("CALL \"C:\\Program Files (x86)\\Microsoft Visual Studio\\2017\\Community\\VC\\Auxiliary\\Build\\vcvarsall.bat\" $arch"); -printLineToBat ("SET _ROOT=%cd%"); -printLineToBat ("SET PATH=%_ROOT%\\qtbase\\bin;%_ROOT%\\gnuwin32\\bin;%PATH%"); # http://doc.qt.io/qt-5/windows-building.html - -printLineToBat ("cd qtbase"); -printLineToBat ("if \"%~1\"==\"step2\" goto step2"); - -# step1: compile openssl and do configure. For some reason, can't continue script execution after configure, have to make step2 -printLineToBat ("IF EXIST $openssl_dir\\build GOTO OPENSSL_ALREAD_COMPILED"); -printLineToBat ("wget --no-check-certificate $openssl_download"); -printLineToBat ("7z x openssl-$openssl_version.tar.gz"); -printLineToBat ("7z x openssl-$openssl_version.tar"); -printLineToBat ("rm openssl-$openssl_version.tar.gz"); -printLineToBat ("rm openssl-$openssl_version.tar"); -printLineToBat ("cd $openssl_dir"); -printLineToBat ("perl Configure VC-$openssl_arch no-asm no-shared no-tests --prefix=%cd%\\build --openssldir=%cd%\\build"); -printLineToBat ("nmake"); -printLineToBat ("nmake install"); -# do little clean up -printLineToBat ("rm test\\*.exe"); -printLineToBat ("rm test\\*.pdb"); -printLineToBat ("rm test\\*.obj"); -printLineToBat (":OPENSSL_ALREAD_COMPILED"); -# go back to qtbase -printLineToBat ("cd .."); - -# -developer-build creates an in-source build for developer usage. -# openssl: see https://bugreports.qt.io/browse/QTBUG-65501 -printLineToBat ("configure -opensource -developer-build -confirm-license -opengl desktop -mp -nomake tests -nomake examples -I \"%cd%\\$openssl_dir\\build\\include\" -openssl-linked OPENSSL_LIBS=\"%cd%\\$openssl_dir\\build\\lib\\libssl.lib %cd%\\$openssl_dir\\build\\lib\\libcrypto.lib -lcrypt32 -lws2_32 -lAdvapi32 -luser32\""); -printLineToBat ("goto :EOF"); - -# step 2: -printLineToBat (":step2"); - -printLineToBat ("nmake"); -printLineToBat ("cd ..\\qttools"); -printLineToBat ("..\\qtbase\\bin\\qmake"); -printLineToBat ("nmake"); -printLineToBat ("cd ..\\qtbase"); -printLineToBat ("cd .."); # go up to qt dir -# openssl clean up -printLineToBat ("cd qtbase"); -printLineToBat ("cd $openssl_dir"); -printLineToBat ("del /s /f /q out32"); -printLineToBat ("del /s /f /q out32.dbg"); -printLineToBat ("cd .."); -printLineToBat ("cd .."); -# the rest -printLineToBat ("del *.obj /s /f"); -printLineToBat ("del *.ilk /s /f"); -printLineToBat ("del *.pch /s /f"); -printLineToBat ("del Makefile* /s /f"); - -close BAT; - -system ($batfile); -system ("$batfile step2"); - -system ("pause"); - -sub printLineToBat -{ - print BAT "$_[0]\n"; -} \ No newline at end of file diff --git a/5.12.1/qtbase/mkspecs/common/msvc-desktop.conf b/5.12.1/qtbase/mkspecs/common/msvc-desktop.conf deleted file mode 100644 index a4fadeb..0000000 --- a/5.12.1/qtbase/mkspecs/common/msvc-desktop.conf +++ /dev/null @@ -1,118 +0,0 @@ -# -# This file is used as a basis for the following compilers: -# -# - Microsoft C/C++ Optimizing Compiler (all desktop versions) -# - Intel C++ Compiler on Windows -# - Clang-cl -# -# Baseline: -# -# - Visual Studio 2005 (8.0), VC++ 14.0 -# -# Version-specific settings go in msvc-version.conf (loaded by default_pre) -# - -MAKEFILE_GENERATOR = MSVC.NET -QMAKE_PLATFORM = win32 -QMAKE_COMPILER = msvc -CONFIG += flat debug_and_release debug_and_release_target precompile_header autogen_precompile_source embed_manifest_dll embed_manifest_exe -# MSVC 2017 15.8+ fixed std::aligned_storage but compilation fails without -# _ENABLE_EXTENDED_ALIGNED_STORAGE flag since the fix breaks binary compatibility. -DEFINES += UNICODE _UNICODE WIN32 _ENABLE_EXTENDED_ALIGNED_STORAGE -QMAKE_COMPILER_DEFINES += _WIN32 -contains(QMAKE_TARGET.arch, x86_64) { - DEFINES += WIN64 - QMAKE_COMPILER_DEFINES += _WIN64 -} - -QMAKE_CFLAGS_OPTIMIZE_DEBUG = -Od -QMAKE_CFLAGS_OPTIMIZE = -O2 -QMAKE_CFLAGS_OPTIMIZE_SIZE = -O1 - -QMAKE_CC = cl -QMAKE_LEX = flex -QMAKE_LEXFLAGS = -QMAKE_YACC = bison -y -QMAKE_YACCFLAGS = -d -QMAKE_CFLAGS = -nologo -Zc:wchar_t -QMAKE_CFLAGS_WARN_ON = -W3 -QMAKE_CFLAGS_WARN_OFF = -W0 -QMAKE_CFLAGS_RELEASE = $$QMAKE_CFLAGS_OPTIMIZE -MD -QMAKE_CFLAGS_RELEASE_WITH_DEBUGINFO += $$QMAKE_CFLAGS_OPTIMIZE -Zi -MD -QMAKE_CFLAGS_DEBUG = -Zi -MDd -QMAKE_CFLAGS_YACC = -QMAKE_CFLAGS_LTCG = -GL - -contains(QMAKE_TARGET.arch, x86_64) { - # SSE2 is mandatory on 64-bit mode, so skip the option. It triggers: - # cl : Command line warning D9002 : ignoring unknown option '-arch:SSE2' - QMAKE_CFLAGS_SSE2 = -} else { - QMAKE_CFLAGS_SSE2 = -arch:SSE2 -} -QMAKE_CFLAGS_SSE3 = $$QMAKE_CFLAGS_SSE2 -QMAKE_CFLAGS_SSSE3 = $$QMAKE_CFLAGS_SSE2 -QMAKE_CFLAGS_SSE4_1 = $$QMAKE_CFLAGS_SSE2 -QMAKE_CFLAGS_SSE4_2 = $$QMAKE_CFLAGS_SSE2 -QMAKE_CFLAGS_AESNI = $$QMAKE_CFLAGS_SSE2 -QMAKE_CFLAGS_SHANI = $$QMAKE_CFLAGS_SSE2 - -QMAKE_CXX = $$QMAKE_CC -QMAKE_CXXFLAGS = $$QMAKE_CFLAGS -QMAKE_CXXFLAGS_WARN_ON = $$QMAKE_CFLAGS_WARN_ON -w34100 -w34189 -w44996 -QMAKE_CXXFLAGS_WARN_OFF = $$QMAKE_CFLAGS_WARN_OFF -QMAKE_CXXFLAGS_RELEASE = $$QMAKE_CFLAGS_RELEASE -QMAKE_CXXFLAGS_RELEASE_WITH_DEBUGINFO += $$QMAKE_CFLAGS_RELEASE_WITH_DEBUGINFO -QMAKE_CXXFLAGS_DEBUG = $$QMAKE_CFLAGS_DEBUG -QMAKE_CXXFLAGS_YACC = $$QMAKE_CFLAGS_YACC -QMAKE_CXXFLAGS_LTCG = $$QMAKE_CFLAGS_LTCG -QMAKE_CXXFLAGS_STL_ON = -EHsc -QMAKE_CXXFLAGS_STL_OFF = -QMAKE_CXXFLAGS_RTTI_ON = -GR -QMAKE_CXXFLAGS_RTTI_OFF = -QMAKE_CXXFLAGS_EXCEPTIONS_ON = -EHsc -QMAKE_CXXFLAGS_EXCEPTIONS_OFF = - -QMAKE_INCDIR = - -QMAKE_RUN_CC = $(CC) -c $(CFLAGS) $(INCPATH) -Fo$obj $src -QMAKE_RUN_CC_IMP = $(CC) -c $(CFLAGS) $(INCPATH) -Fo$@ $< -QMAKE_RUN_CC_IMP_BATCH = $(CC) -c $(CFLAGS) $(INCPATH) -Fo$@ @<< -QMAKE_RUN_CXX = $(CXX) -c $(CXXFLAGS) $(INCPATH) -Fo$obj $src -QMAKE_RUN_CXX_IMP = $(CXX) -c $(CXXFLAGS) $(INCPATH) -Fo$@ $< -QMAKE_RUN_CXX_IMP_BATCH = $(CXX) -c $(CXXFLAGS) $(INCPATH) -Fo$@ @<< - -QMAKE_LINK = link -QMAKE_LFLAGS = /NOLOGO /DYNAMICBASE /NXCOMPAT -QMAKE_LFLAGS_RELEASE = /INCREMENTAL:NO -QMAKE_LFLAGS_RELEASE_WITH_DEBUGINFO = /DEBUG /OPT:REF /INCREMENTAL:NO -QMAKE_LFLAGS_DEBUG = /DEBUG -QMAKE_LFLAGS_CONSOLE = /SUBSYSTEM:CONSOLE -QMAKE_LFLAGS_WINDOWS = /SUBSYSTEM:WINDOWS -QMAKE_LFLAGS_EXE = \"/MANIFESTDEPENDENCY:type=\'win32\' name=\'Microsoft.Windows.Common-Controls\' version=\'6.0.0.0\' publicKeyToken=\'6595b64144ccf1df\' language=\'*\' processorArchitecture=\'*\'\" -QMAKE_LFLAGS_DLL = /DLL -QMAKE_LFLAGS_LTCG = /LTCG -QMAKE_PREFIX_SHLIB = -QMAKE_EXTENSION_SHLIB = dll -QMAKE_PREFIX_STATICLIB = -QMAKE_EXTENSION_STATICLIB = lib - -QMAKE_LIBS = -QMAKE_LIBS_GUI = gdi32.lib comdlg32.lib oleaut32.lib imm32.lib winmm.lib ws2_32.lib ole32.lib uuid.lib user32.lib advapi32.lib -QMAKE_LIBS_NETWORK = ws2_32.lib -QMAKE_LIBS_OPENGL = glu32.lib opengl32.lib gdi32.lib user32.lib -QMAKE_LIBS_OPENGL_ES2 = gdi32.lib user32.lib -QMAKE_LIBS_OPENGL_ES2_DEBUG = gdi32.lib user32.lib -QMAKE_LIBS_COMPAT = advapi32.lib shell32.lib comdlg32.lib user32.lib gdi32.lib ws2_32.lib -QMAKE_LIBS_QT_ENTRY = -lqtmain - -QMAKE_IDL = midl /NOLOGO -QMAKE_LIB = lib /NOLOGO -QMAKE_RC = rc /NOLOGO - -VCPROJ_EXTENSION = .vcproj -VCSOLUTION_EXTENSION = .sln -VCPROJ_KEYWORD = Qt4VSv1.0 - -include(angle.conf) -include(windows-vulkan.conf) diff --git a/5.12.1/qtbase/src/plugins/platforms/cocoa/qcocoaapplicationdelegate.mm b/5.12.1/qtbase/src/plugins/platforms/cocoa/qcocoaapplicationdelegate.mm deleted file mode 100644 index 48fb16f..0000000 --- a/5.12.1/qtbase/src/plugins/platforms/cocoa/qcocoaapplicationdelegate.mm +++ /dev/null @@ -1,503 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2018 The Qt Company Ltd. -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the plugins of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:LGPL$ -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and The Qt Company. For licensing terms -** and conditions see https://www.qt.io/terms-conditions. For further -** information use the contact form at https://www.qt.io/contact-us. -** -** GNU Lesser General Public License Usage -** Alternatively, this file may be used under the terms of the GNU Lesser -** General Public License version 3 as published by the Free Software -** Foundation and appearing in the file LICENSE.LGPL3 included in the -** packaging of this file. Please review the following information to -** ensure the GNU Lesser General Public License version 3 requirements -** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. -** -** GNU General Public License Usage -** Alternatively, this file may be used under the terms of the GNU -** General Public License version 2.0 or (at your option) the GNU General -** Public license version 3 or any later version approved by the KDE Free -** Qt Foundation. The licenses are as published by the Free Software -** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 -** included in the packaging of this file. Please review the following -** information to ensure the GNU General Public License requirements will -** be met: https://www.gnu.org/licenses/gpl-2.0.html and -** https://www.gnu.org/licenses/gpl-3.0.html. -** -** $QT_END_LICENSE$ -** -****************************************************************************/ - -/**************************************************************************** - ** - ** Copyright (c) 2007-2008, Apple, Inc. - ** - ** All rights reserved. - ** - ** Redistribution and use in source and binary forms, with or without - ** modification, are permitted provided that the following conditions are met: - ** - ** * Redistributions of source code must retain the above copyright notice, - ** this list of conditions and the following disclaimer. - ** - ** * Redistributions in binary form must reproduce the above copyright notice, - ** this list of conditions and the following disclaimer in the documentation - ** and/or other materials provided with the distribution. - ** - ** * Neither the name of Apple, Inc. nor the names of its contributors - ** may be used to endorse or promote products derived from this software - ** without specific prior written permission. - ** - ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR - ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR - ** CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, - ** EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, - ** PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR - ** PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF - ** LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING - ** NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS - ** SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - ** - ****************************************************************************/ - - -#import "qcocoaapplicationdelegate.h" -#include "qcocoaintegration.h" -#include "qcocoamenu.h" -#include "qcocoamenuloader.h" -#include "qcocoamenuitem.h" -#include "qcocoansmenu.h" - -#include -#include -#include -#include -#include -#include "qt_mac_p.h" -#include -#include - -QT_USE_NAMESPACE - -@implementation QCocoaApplicationDelegate { - bool startedQuit; - NSObject *reflectionDelegate; - bool inLaunch; - QWindowList hiddenWindows; -} - -+ (instancetype)sharedDelegate -{ - static QCocoaApplicationDelegate *shared = nil; - static dispatch_once_t onceToken; - dispatch_once(&onceToken, ^{ - shared = [[self alloc] init]; - atexit_b(^{ - [shared release]; - shared = nil; - }); - }); - return shared; -} - -- (instancetype)init -{ - self = [super init]; - if (self) { - inLaunch = true; - [[NSNotificationCenter defaultCenter] - addObserver:self - selector:@selector(updateScreens:) - name:NSApplicationDidChangeScreenParametersNotification - object:NSApp]; - } - return self; -} - -- (void)updateScreens:(NSNotification *)notification -{ - Q_UNUSED(notification); - if (QCocoaIntegration *ci = QCocoaIntegration::instance()) - ci->updateScreens(); -} - -- (void)dealloc -{ - [_dockMenu release]; - if (reflectionDelegate) { - [[NSApplication sharedApplication] setDelegate:reflectionDelegate]; - [reflectionDelegate release]; - } - [[NSNotificationCenter defaultCenter] removeObserver:self]; - - [super dealloc]; -} - -- (NSMenu *)applicationDockMenu:(NSApplication *)sender -{ - Q_UNUSED(sender); - // Manually invoke the delegate's -menuWillOpen: method. - // See QTBUG-39604 (and its fix) for details. - [self.dockMenu.delegate menuWillOpen:self.dockMenu]; - return [[self.dockMenu retain] autorelease]; -} - -- (BOOL)canQuit -{ - [[NSApp mainMenu] cancelTracking]; - - bool handle_quit = true; - NSMenuItem *quitMenuItem = [[QT_MANGLE_NAMESPACE(QCocoaMenuLoader) sharedMenuLoader] quitMenuItem]; - if (!QGuiApplicationPrivate::instance()->modalWindowList.isEmpty() - && [quitMenuItem isEnabled]) { - int visible = 0; - const QWindowList tlws = QGuiApplication::topLevelWindows(); - for (int i = 0; i < tlws.size(); ++i) { - if (tlws.at(i)->isVisible()) - ++visible; - } - handle_quit = (visible <= 1); - } - - if (handle_quit) { - QCloseEvent ev; - QGuiApplication::sendEvent(qGuiApp, &ev); - if (ev.isAccepted()) { - return YES; - } - } - - return NO; -} - -// This function will only be called when NSApp is actually running. -- (NSApplicationTerminateReply)applicationShouldTerminate:(NSApplication *)sender -{ - // The reflection delegate gets precedence - if (reflectionDelegate) { - if ([reflectionDelegate respondsToSelector:@selector(applicationShouldTerminate:)]) - return [reflectionDelegate applicationShouldTerminate:sender]; - return NSTerminateNow; - } - - if ([self canQuit]) { - if (!startedQuit) { - startedQuit = true; - // Close open windows. This is done in order to deliver de-expose - // events while the event loop is still running. - const QWindowList topLevels = QGuiApplication::topLevelWindows(); - for (int i = 0; i < topLevels.size(); ++i) { - QWindow *topLevelWindow = topLevels.at(i); - // Already closed windows will not have a platform window, skip those - if (topLevelWindow->handle()) - QWindowSystemInterface::handleCloseEvent(topLevelWindow); - } - QWindowSystemInterface::flushWindowSystemEvents(); - - QGuiApplication::exit(0); - startedQuit = false; - } - } - - if (QGuiApplicationPrivate::instance()->threadData->eventLoops.isEmpty()) { - // INVARIANT: No event loop is executing. This probably - // means that Qt is used as a plugin, or as a part of a native - // Cocoa application. In any case it should be fine to - // terminate now: - return NSTerminateNow; - } - - return NSTerminateCancel; -} - -- (void)applicationWillFinishLaunching:(NSNotification *)notification -{ - Q_UNUSED(notification); - - /* - From the Cocoa documentation: "A good place to install event handlers - is in the applicationWillFinishLaunching: method of the application - delegate. At that point, the Application Kit has installed its default - event handlers, so if you install a handler for one of the same events, - it will replace the Application Kit version." - */ - - /* - If Qt is used as a plugin, we let the 3rd party application handle - events like quit and open file events. Otherwise, if we install our own - handlers, we easily end up breaking functionality the 3rd party - application depends on. - */ - NSAppleEventManager *eventManager = [NSAppleEventManager sharedAppleEventManager]; - /*[eventManager setEventHandler:self - andSelector:@selector(appleEventQuit:withReplyEvent:) - forEventClass:kCoreEventClass - andEventID:kAEQuitApplication];*/ - [eventManager setEventHandler:self - andSelector:@selector(getUrl:withReplyEvent:) - forEventClass:kInternetEventClass - andEventID:kAEGetURL]; -} - -// called by QCocoaIntegration's destructor before resetting the application delegate to nil -- (void)removeAppleEventHandlers -{ - NSAppleEventManager *eventManager = [NSAppleEventManager sharedAppleEventManager]; - //[eventManager removeEventHandlerForEventClass:kCoreEventClass andEventID:kAEQuitApplication]; - [eventManager removeEventHandlerForEventClass:kInternetEventClass andEventID:kAEGetURL]; -} - -- (bool)inLaunch -{ - return inLaunch; -} - -- (void)applicationDidFinishLaunching:(NSNotification *)aNotification -{ - Q_UNUSED(aNotification); - inLaunch = false; - - if (qEnvironmentVariableIsEmpty("QT_MAC_DISABLE_FOREGROUND_APPLICATION_TRANSFORM")) { - // Move the application window to front to avoid launching behind the terminal. - // Ignoring other apps is necessary (we must ignore the terminal), but makes - // Qt apps play slightly less nice with other apps when lanching from Finder - // (See the activateIgnoringOtherApps docs.) - [[NSApplication sharedApplication] activateIgnoringOtherApps:YES]; - } -} - -- (void)application:(NSApplication *)sender openFiles:(NSArray *)filenames -{ - Q_UNUSED(filenames); - Q_UNUSED(sender); - - for (NSString *fileName in filenames) { - QString qtFileName = QString::fromNSString(fileName); - if (inLaunch) { - // We need to be careful because Cocoa will be nice enough to take - // command line arguments and send them to us as events. Given the history - // of Qt Applications, this will result in behavior people don't want, as - // they might be doing the opening themselves with the command line parsing. - if (qApp->arguments().contains(qtFileName)) - continue; - } - QWindowSystemInterface::handleFileOpenEvent(qtFileName); - } - - if (reflectionDelegate && - [reflectionDelegate respondsToSelector:@selector(application:openFiles:)]) - [reflectionDelegate application:sender openFiles:filenames]; - -} - -- (BOOL)applicationShouldTerminateAfterLastWindowClosed:(NSApplication *)sender -{ - // If we have a reflection delegate, that will get to call the shots. - if (reflectionDelegate - && [reflectionDelegate respondsToSelector: - @selector(applicationShouldTerminateAfterLastWindowClosed:)]) - return [reflectionDelegate applicationShouldTerminateAfterLastWindowClosed:sender]; - return NO; // Someday qApp->quitOnLastWindowClosed(); when QApp and NSApp work closer together. -} - -- (void)applicationWillHide:(NSNotification *)notification -{ - if (reflectionDelegate - && [reflectionDelegate respondsToSelector:@selector(applicationWillHide:)]) { - [reflectionDelegate applicationWillHide:notification]; - } - - // When the application is hidden Qt will hide the popup windows associated with - // it when it has lost the activation for the application. However, when it gets - // to this point it believes the popup windows to be hidden already due to the - // fact that the application itself is hidden, which will cause a problem when - // the application is made visible again. - const QWindowList topLevelWindows = QGuiApplication::topLevelWindows(); - for (QWindow *topLevelWindow : topLevelWindows) { - if ((topLevelWindow->type() & Qt::Popup) == Qt::Popup && topLevelWindow->isVisible()) { - topLevelWindow->hide(); - - if ((topLevelWindow->type() & Qt::Tool) == Qt::Tool) - hiddenWindows << topLevelWindow; - } - } -} - -- (void)applicationDidUnhide:(NSNotification *)notification -{ - if (reflectionDelegate - && [reflectionDelegate respondsToSelector:@selector(applicationDidUnhide:)]) - [reflectionDelegate applicationDidUnhide:notification]; - - for (QWindow *window : qAsConst(hiddenWindows)) - window->show(); - - hiddenWindows.clear(); -} - -- (void)applicationDidBecomeActive:(NSNotification *)notification -{ - if (reflectionDelegate - && [reflectionDelegate respondsToSelector:@selector(applicationDidBecomeActive:)]) - [reflectionDelegate applicationDidBecomeActive:notification]; - - QWindowSystemInterface::handleApplicationStateChanged(Qt::ApplicationActive); -/* - onApplicationChangedActivation(true); - - if (!QWidget::mouseGrabber()){ - // Update enter/leave immidiatly, don't wait for a move event. But only - // if no grab exists (even if the grab points to this widget, it seems, ref X11) - QPoint qlocal, qglobal; - QWidget *widgetUnderMouse = 0; - qt_mac_getTargetForMouseEvent(0, QEvent::Enter, qlocal, qglobal, 0, &widgetUnderMouse); - QApplicationPrivate::dispatchEnterLeave(widgetUnderMouse, 0); - qt_last_mouse_receiver = widgetUnderMouse; - qt_last_native_mouse_receiver = widgetUnderMouse ? - (widgetUnderMouse->internalWinId() ? widgetUnderMouse : widgetUnderMouse->nativeParentWidget()) : 0; - } -*/ -} - -- (void)applicationDidResignActive:(NSNotification *)notification -{ - if (reflectionDelegate - && [reflectionDelegate respondsToSelector:@selector(applicationDidResignActive:)]) - [reflectionDelegate applicationDidResignActive:notification]; - - QWindowSystemInterface::handleApplicationStateChanged(Qt::ApplicationInactive); -/* - onApplicationChangedActivation(false); - - if (!QWidget::mouseGrabber()) - QApplicationPrivate::dispatchEnterLeave(0, qt_last_mouse_receiver); - qt_last_mouse_receiver = 0; - qt_last_native_mouse_receiver = 0; - qt_button_down = 0; -*/ -} - -- (BOOL)applicationShouldHandleReopen:(NSApplication *)theApplication hasVisibleWindows:(BOOL)flag -{ - Q_UNUSED(theApplication); - Q_UNUSED(flag); - if (reflectionDelegate - && [reflectionDelegate respondsToSelector:@selector(applicationShouldHandleReopen:hasVisibleWindows:)]) - return [reflectionDelegate applicationShouldHandleReopen:theApplication hasVisibleWindows:flag]; - - /* - true to force delivery of the event even if the application state is already active, - because rapp (handle reopen) events are sent each time the dock icon is clicked regardless - of the active state of the application or number of visible windows. For example, a browser - app that has no windows opened would need the event be to delivered even if it was already - active in order to create a new window as per OS X conventions. - */ - QWindowSystemInterface::handleApplicationStateChanged(Qt::ApplicationActive, true /*forcePropagate*/); - - return YES; -} - -- (void)setReflectionDelegate:(NSObject *)oldDelegate -{ - [oldDelegate retain]; - [reflectionDelegate release]; - reflectionDelegate = oldDelegate; -} - -- (NSMethodSignature *)methodSignatureForSelector:(SEL)aSelector -{ - NSMethodSignature *result = [super methodSignatureForSelector:aSelector]; - if (!result && reflectionDelegate) { - result = [reflectionDelegate methodSignatureForSelector:aSelector]; - } - return result; -} - -- (BOOL)respondsToSelector:(SEL)aSelector -{ - BOOL result = [super respondsToSelector:aSelector]; - if (!result && reflectionDelegate) - result = [reflectionDelegate respondsToSelector:aSelector]; - return result; -} - -- (void)forwardInvocation:(NSInvocation *)invocation -{ - SEL invocationSelector = [invocation selector]; - if (reflectionDelegate && [reflectionDelegate respondsToSelector:invocationSelector]) - [invocation invokeWithTarget:reflectionDelegate]; - else - [self doesNotRecognizeSelector:invocationSelector]; -} - -- (void)getUrl:(NSAppleEventDescriptor *)event withReplyEvent:(NSAppleEventDescriptor *)replyEvent -{ - Q_UNUSED(replyEvent); - NSString *urlString = [[event paramDescriptorForKeyword:keyDirectObject] stringValue]; - QWindowSystemInterface::handleFileOpenEvent(QUrl(QString::fromNSString(urlString))); -} - -- (void)appleEventQuit:(NSAppleEventDescriptor *)event withReplyEvent:(NSAppleEventDescriptor *)replyEvent -{ - Q_UNUSED(event); - Q_UNUSED(replyEvent); - [NSApp terminate:self]; -} - -@end - -@implementation QCocoaApplicationDelegate (Menus) - -- (BOOL)validateMenuItem:(NSMenuItem*)item -{ - auto *nativeItem = qt_objc_cast(item); - if (!nativeItem) - return item.enabled; // FIXME Test with with Qt as plugin or embedded QWindow. - - auto *platformItem = nativeItem.platformMenuItem; - if (!platformItem) // Try a bit harder with orphan menu itens - return item.hasSubmenu || (item.enabled && (item.action != @selector(qt_itemFired:))); - - // Menu-holding items are always enabled, as it's conventional in Cocoa - if (platformItem->menu()) - return YES; - - return platformItem->isEnabled(); -} - -@end - -@implementation QCocoaApplicationDelegate (MenuAPI) - -- (void)qt_itemFired:(QCocoaNSMenuItem *)item -{ - if (item.hasSubmenu) - return; - - auto *nativeItem = qt_objc_cast(item); - Q_ASSERT_X(nativeItem, qPrintable(__FUNCTION__), "Triggered menu item is not a QCocoaNSMenuItem."); - auto *platformItem = nativeItem.platformMenuItem; - // Menu-holding items also get a target to play nicely - // with NSMenuValidation but should not trigger. - if (!platformItem || platformItem->menu()) - return; - - QScopedScopeLevelCounter scopeLevelCounter(QGuiApplicationPrivate::instance()->threadData); - QGuiApplicationPrivate::modifier_buttons = [QNSView convertKeyModifiers:[NSEvent modifierFlags]]; - - static QMetaMethod activatedSignal = QMetaMethod::fromSignal(&QCocoaMenuItem::activated); - activatedSignal.invoke(platformItem, Qt::QueuedConnection); -} - -@end diff --git a/5.12.1/qtbase/src/plugins/platforms/cocoa/qcocoasystemsettings.mm b/5.12.1/qtbase/src/plugins/platforms/cocoa/qcocoasystemsettings.mm deleted file mode 100644 index 29377c1..0000000 --- a/5.12.1/qtbase/src/plugins/platforms/cocoa/qcocoasystemsettings.mm +++ /dev/null @@ -1,241 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2017 The Qt Company Ltd. -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the plugins of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:LGPL$ -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and The Qt Company. For licensing terms -** and conditions see https://www.qt.io/terms-conditions. For further -** information use the contact form at https://www.qt.io/contact-us. -** -** GNU Lesser General Public License Usage -** Alternatively, this file may be used under the terms of the GNU Lesser -** General Public License version 3 as published by the Free Software -** Foundation and appearing in the file LICENSE.LGPL3 included in the -** packaging of this file. Please review the following information to -** ensure the GNU Lesser General Public License version 3 requirements -** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. -** -** GNU General Public License Usage -** Alternatively, this file may be used under the terms of the GNU -** General Public License version 2.0 or (at your option) the GNU General -** Public license version 3 or any later version approved by the KDE Free -** Qt Foundation. The licenses are as published by the Free Software -** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 -** included in the packaging of this file. Please review the following -** information to ensure the GNU General Public License requirements will -** be met: https://www.gnu.org/licenses/gpl-2.0.html and -** https://www.gnu.org/licenses/gpl-3.0.html. -** -** $QT_END_LICENSE$ -** -****************************************************************************/ - -#include "qcocoasystemsettings.h" - -#include "qcocoahelpers.h" - -#include -#include -#include - -#if !QT_MACOS_PLATFORM_SDK_EQUAL_OR_ABOVE(__MAC_10_14) -@interface NSColor (MojaveForwardDeclarations) -@property (class, strong, readonly) NSColor *selectedContentBackgroundColor NS_AVAILABLE_MAC(10_14); -@property (class, strong, readonly) NSColor *unemphasizedSelectedTextBackgroundColor NS_AVAILABLE_MAC(10_14); -@property (class, strong, readonly) NSColor *unemphasizedSelectedTextColor NS_AVAILABLE_MAC(10_14); -@property (class, strong, readonly) NSColor *unemphasizedSelectedContentBackgroundColor NS_AVAILABLE_MAC(10_14); -@property (class, strong, readonly) NSArray *alternatingContentBackgroundColors NS_AVAILABLE_MAC(10_14); -@end -#endif - -QT_BEGIN_NAMESPACE - -QPalette * qt_mac_createSystemPalette() -{ - QColor qc; - - // Standard palette initialization (copied from Qt 4 styles) - QBrush backgroundBrush = qt_mac_toQBrush([NSColor windowBackgroundColor]); - QColor background = backgroundBrush.color(); - QColor light(background.lighter(110)); - QColor dark(background.darker(160)); - QColor mid(background.darker(140)); - QPalette *palette = new QPalette(Qt::black, background, light, dark, mid, Qt::black, Qt::white); - - palette->setBrush(QPalette::Window, backgroundBrush); - - palette->setBrush(QPalette::Disabled, QPalette::WindowText, dark); - palette->setBrush(QPalette::Disabled, QPalette::Text, dark); - palette->setBrush(QPalette::Disabled, QPalette::ButtonText, dark); - palette->setBrush(QPalette::Disabled, QPalette::Base, backgroundBrush); - QBrush textBackgroundBrush = qt_mac_toQBrush([NSColor textBackgroundColor]); - palette->setBrush(QPalette::Active, QPalette::Base, textBackgroundBrush); - palette->setBrush(QPalette::Inactive, QPalette::Base, textBackgroundBrush); - palette->setColor(QPalette::Disabled, QPalette::Dark, QColor(191, 191, 191)); - palette->setColor(QPalette::Active, QPalette::Dark, QColor(191, 191, 191)); - palette->setColor(QPalette::Inactive, QPalette::Dark, QColor(191, 191, 191)); - - // System palette initialization: - QBrush br = qt_mac_toQBrush([NSColor selectedControlColor]); - palette->setBrush(QPalette::Active, QPalette::Highlight, br); - if (__builtin_available(macOS 10.14, *)) { - const auto inactiveHighlight = qt_mac_toQBrush([NSColor unemphasizedSelectedContentBackgroundColor]); - palette->setBrush(QPalette::Inactive, QPalette::Highlight, inactiveHighlight); - palette->setBrush(QPalette::Disabled, QPalette::Highlight, inactiveHighlight); - } else { - palette->setBrush(QPalette::Inactive, QPalette::Highlight, br); - palette->setBrush(QPalette::Disabled, QPalette::Highlight, br); - } - - palette->setBrush(QPalette::Shadow, qt_mac_toQColor([NSColor shadowColor])); - - qc = qt_mac_toQColor([NSColor controlTextColor]); - palette->setColor(QPalette::Active, QPalette::Text, qc); - palette->setColor(QPalette::Active, QPalette::WindowText, qc); - palette->setColor(QPalette::Active, QPalette::HighlightedText, qc); - palette->setColor(QPalette::Inactive, QPalette::Text, qc); - palette->setColor(QPalette::Inactive, QPalette::WindowText, qc); - palette->setColor(QPalette::Inactive, QPalette::HighlightedText, qc); - - qc = qt_mac_toQColor([NSColor disabledControlTextColor]); - palette->setColor(QPalette::Disabled, QPalette::Text, qc); - palette->setColor(QPalette::Disabled, QPalette::WindowText, qc); - palette->setColor(QPalette::Disabled, QPalette::HighlightedText, qc); - - palette->setBrush(QPalette::ToolTipBase, qt_mac_toQBrush([NSColor controlColor])); - - // fix for https://bugreports.qt.io/browse/QTBUG-71740 - palette->setColor(QPalette::Normal, QPalette::Link, qt_mac_toQColor([NSColor linkColor])); - - return palette; -} - -struct QMacPaletteMap { - inline QMacPaletteMap(QPlatformTheme::Palette p, NSColor *a, NSColor *i) : - active(a), inactive(i), paletteRole(p) { } - - NSColor *active; - NSColor *inactive; - QPlatformTheme::Palette paletteRole; -}; - -#define MAC_PALETTE_ENTRY(pal, active, inactive) \ - QMacPaletteMap(pal, [NSColor active], [NSColor inactive]) -static QMacPaletteMap mac_widget_colors[] = { - MAC_PALETTE_ENTRY(QPlatformTheme::ToolButtonPalette, controlTextColor, disabledControlTextColor), - MAC_PALETTE_ENTRY(QPlatformTheme::ButtonPalette, controlTextColor, disabledControlTextColor), - MAC_PALETTE_ENTRY(QPlatformTheme::HeaderPalette, headerTextColor, disabledControlTextColor), - MAC_PALETTE_ENTRY(QPlatformTheme::ComboBoxPalette, controlTextColor, disabledControlTextColor), - MAC_PALETTE_ENTRY(QPlatformTheme::ItemViewPalette, textColor, disabledControlTextColor), - MAC_PALETTE_ENTRY(QPlatformTheme::MessageBoxLabelPalette, textColor, disabledControlTextColor), - MAC_PALETTE_ENTRY(QPlatformTheme::TabBarPalette, controlTextColor, disabledControlTextColor), - MAC_PALETTE_ENTRY(QPlatformTheme::LabelPalette, textColor, disabledControlTextColor), - MAC_PALETTE_ENTRY(QPlatformTheme::GroupBoxPalette, textColor, disabledControlTextColor), - MAC_PALETTE_ENTRY(QPlatformTheme::MenuPalette, controlTextColor, disabledControlTextColor), - MAC_PALETTE_ENTRY(QPlatformTheme::MenuBarPalette, controlTextColor, disabledControlTextColor), - MAC_PALETTE_ENTRY(QPlatformTheme::TextEditPalette, textColor, disabledControlTextColor), - MAC_PALETTE_ENTRY(QPlatformTheme::TextLineEditPalette, textColor, disabledControlTextColor) -}; -#undef MAC_PALETTE_ENTRY - -static const int mac_widget_colors_count = sizeof(mac_widget_colors) / sizeof(mac_widget_colors[0]); - -QHash qt_mac_createRolePalettes() -{ - QHash palettes; - QColor qc; - for (int i = 0; i < mac_widget_colors_count; i++) { - QPalette &pal = *qt_mac_createSystemPalette(); - if (mac_widget_colors[i].active) { - qc = qt_mac_toQColor(mac_widget_colors[i].active); - pal.setColor(QPalette::Active, QPalette::Text, qc); - pal.setColor(QPalette::Inactive, QPalette::Text, qc); - pal.setColor(QPalette::Active, QPalette::WindowText, qc); - pal.setColor(QPalette::Inactive, QPalette::WindowText, qc); - pal.setColor(QPalette::Active, QPalette::HighlightedText, qc); - pal.setColor(QPalette::Inactive, QPalette::HighlightedText, qc); - qc = qt_mac_toQColor(mac_widget_colors[i].inactive); - pal.setColor(QPalette::Disabled, QPalette::Text, qc); - pal.setColor(QPalette::Disabled, QPalette::WindowText, qc); - pal.setColor(QPalette::Disabled, QPalette::HighlightedText, qc); - } - if (mac_widget_colors[i].paletteRole == QPlatformTheme::MenuPalette - || mac_widget_colors[i].paletteRole == QPlatformTheme::MenuBarPalette) { - NSColor *selectedMenuItemColor = nil; - if (__builtin_available(macOS 10.14, *)) { - // Cheap approximation for NSVisualEffectView (see deprecation note for selectedMenuItemTextColor) - selectedMenuItemColor = [[NSColor selectedContentBackgroundColor] highlightWithLevel:0.4]; - } else { - // selectedMenuItemColor would presumably be the correct color to use as the background - // for selected menu items. But that color is always blue, and doesn't follow the - // appearance color in system preferences. So we therefore deliberatly choose to use - // keyboardFocusIndicatorColor instead, which appears to have the same color value. - selectedMenuItemColor = [NSColor keyboardFocusIndicatorColor]; - } - pal.setBrush(QPalette::Highlight, qt_mac_toQColor(selectedMenuItemColor)); - qc = qt_mac_toQColor([NSColor labelColor]); - pal.setBrush(QPalette::ButtonText, qc); - pal.setBrush(QPalette::Text, qc); - qc = qt_mac_toQColor([NSColor selectedMenuItemTextColor]); - pal.setBrush(QPalette::HighlightedText, qc); - qc = qt_mac_toQColor([NSColor disabledControlTextColor]); - pal.setBrush(QPalette::Disabled, QPalette::Text, qc); - } else if ((mac_widget_colors[i].paletteRole == QPlatformTheme::ButtonPalette) - || (mac_widget_colors[i].paletteRole == QPlatformTheme::HeaderPalette) - || (mac_widget_colors[i].paletteRole == QPlatformTheme::TabBarPalette)) { - pal.setColor(QPalette::Disabled, QPalette::ButtonText, - pal.color(QPalette::Disabled, QPalette::Text)); - pal.setColor(QPalette::Inactive, QPalette::ButtonText, - pal.color(QPalette::Inactive, QPalette::Text)); - pal.setColor(QPalette::Active, QPalette::ButtonText, - pal.color(QPalette::Active, QPalette::Text)); - } else if (mac_widget_colors[i].paletteRole == QPlatformTheme::ItemViewPalette) { - NSArray *baseColors = nil; - NSColor *activeHighlightColor = nil; - if (__builtin_available(macOS 10.14, *)) { - baseColors = [NSColor alternatingContentBackgroundColors]; - activeHighlightColor = [NSColor selectedContentBackgroundColor]; - pal.setBrush(QPalette::Inactive, QPalette::HighlightedText, - qt_mac_toQBrush([NSColor unemphasizedSelectedTextColor])); - } else { - baseColors = [NSColor controlAlternatingRowBackgroundColors]; - activeHighlightColor = [NSColor alternateSelectedControlColor]; - pal.setBrush(QPalette::Inactive, QPalette::HighlightedText, - pal.brush(QPalette::Active, QPalette::Text)); - } - pal.setBrush(QPalette::Base, qt_mac_toQBrush(baseColors[0])); - pal.setBrush(QPalette::AlternateBase, qt_mac_toQBrush(baseColors[1])); - pal.setBrush(QPalette::Active, QPalette::Highlight, - qt_mac_toQBrush(activeHighlightColor)); - pal.setBrush(QPalette::Active, QPalette::HighlightedText, - qt_mac_toQBrush([NSColor alternateSelectedControlTextColor])); - pal.setBrush(QPalette::Inactive, QPalette::Text, - pal.brush(QPalette::Active, QPalette::Text)); - } else if (mac_widget_colors[i].paletteRole == QPlatformTheme::TextEditPalette) { - pal.setBrush(QPalette::Active, QPalette::Base, qt_mac_toQColor([NSColor textBackgroundColor])); - pal.setBrush(QPalette::Inactive, QPalette::Text, - pal.brush(QPalette::Active, QPalette::Text)); - pal.setBrush(QPalette::Inactive, QPalette::HighlightedText, - pal.brush(QPalette::Active, QPalette::Text)); - } else if (mac_widget_colors[i].paletteRole == QPlatformTheme::TextLineEditPalette - || mac_widget_colors[i].paletteRole == QPlatformTheme::ComboBoxPalette) { - pal.setBrush(QPalette::Active, QPalette::Base, qt_mac_toQColor([NSColor textBackgroundColor])); - pal.setBrush(QPalette::Disabled, QPalette::Base, - pal.brush(QPalette::Active, QPalette::Base)); - } else if (mac_widget_colors[i].paletteRole == QPlatformTheme::LabelPalette) { - qc = qt_mac_toQColor([NSColor labelColor]); - pal.setBrush(QPalette::Inactive, QPalette::ToolTipText, qc); - } - palettes.insert(mac_widget_colors[i].paletteRole, &pal); - } - return palettes; -} - -QT_END_NAMESPACE diff --git a/5.12.1/qtbase/src/plugins/styles/mac/qmacstyle_mac.mm b/5.12.1/qtbase/src/plugins/styles/mac/qmacstyle_mac.mm deleted file mode 100644 index 13ef98b..0000000 --- a/5.12.1/qtbase/src/plugins/styles/mac/qmacstyle_mac.mm +++ /dev/null @@ -1,6528 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2016 The Qt Company Ltd. -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the QtWidgets module of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:LGPL$ -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and The Qt Company. For licensing terms -** and conditions see https://www.qt.io/terms-conditions. For further -** information use the contact form at https://www.qt.io/contact-us. -** -** GNU Lesser General Public License Usage -** Alternatively, this file may be used under the terms of the GNU Lesser -** General Public License version 3 as published by the Free Software -** Foundation and appearing in the file LICENSE.LGPL3 included in the -** packaging of this file. Please review the following information to -** ensure the GNU Lesser General Public License version 3 requirements -** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. -** -** GNU General Public License Usage -** Alternatively, this file may be used under the terms of the GNU -** General Public License version 2.0 or (at your option) the GNU General -** Public license version 3 or any later version approved by the KDE Free -** Qt Foundation. The licenses are as published by the Free Software -** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 -** included in the packaging of this file. Please review the following -** information to ensure the GNU General Public License requirements will -** be met: https://www.gnu.org/licenses/gpl-2.0.html and -** https://www.gnu.org/licenses/gpl-3.0.html. -** -** $QT_END_LICENSE$ -** -****************************************************************************/ - -/* - Note: The qdoc comments for QMacStyle are contained in - .../doc/src/qstyles.qdoc. -*/ - -#include - -#include "qmacstyle_mac_p.h" -#include "qmacstyle_mac_p_p.h" - -#define QMAC_QAQUASTYLE_SIZE_CONSTRAIN -//#define DEBUG_SIZE_CONSTRAINT - -#include -#if QT_CONFIG(tabbar) -#include -#endif -#include -#include -#include -#if QT_CONFIG(combobox) -#include -#include -#endif -#if QT_CONFIG(dialogbuttonbox) -#include -#endif -#if QT_CONFIG(dockwidget) -#include -#endif -#include -#include -#include -#include -#include -#include -#if QT_CONFIG(lineedit) -#include -#endif -#if QT_CONFIG(mainwindow) -#include -#endif -#if QT_CONFIG(mdiarea) -#include -#endif -#if QT_CONFIG(menubar) -#include -#endif -#include -#include -#include -#include -#if QT_CONFIG(progressbar) -#include -#endif -#if QT_CONFIG(pushbutton) -#include -#endif -#include -#if QT_CONFIG(rubberband) -#include -#endif -#if QT_CONFIG(scrollbar) -#include -#endif -#if QT_CONFIG(sizegrip) -#include -#endif -#include -#include -#if QT_CONFIG(toolbutton) -#include -#endif -#if QT_CONFIG(treeview) -#include -#endif -#if QT_CONFIG(tableview) -#include -#endif -#include -#if QT_CONFIG(wizard) -#include -#endif -#include -#if QT_CONFIG(datetimeedit) -#include -#endif -#include -#include -#if QT_CONFIG(graphicsview) -#include -#endif -#include -#include -#include -#include -#include -#include -#include - -QT_USE_NAMESPACE - -static QWindow *qt_getWindow(const QWidget *widget) -{ - return widget ? widget->window()->windowHandle() : 0; -} - -@interface QT_MANGLE_NAMESPACE(NotificationReceiver) : NSObject -@end - -QT_NAMESPACE_ALIAS_OBJC_CLASS(NotificationReceiver); - -@implementation NotificationReceiver - -- (void)scrollBarStyleDidChange:(NSNotification *)notification -{ - Q_UNUSED(notification); - - // purge destroyed scroll bars: - QMacStylePrivate::scrollBars.removeAll(QPointer()); - - QEvent event(QEvent::StyleChange); - for (const auto &o : QMacStylePrivate::scrollBars) - QCoreApplication::sendEvent(o, &event); -} -@end - -@interface QT_MANGLE_NAMESPACE(QIndeterminateProgressIndicator) : NSProgressIndicator - -@property (readonly, nonatomic) NSInteger animators; - -- (instancetype)init; - -- (void)startAnimation; -- (void)stopAnimation; - -- (void)drawWithFrame:(CGRect)rect inView:(NSView *)view; - -@end - -QT_NAMESPACE_ALIAS_OBJC_CLASS(QIndeterminateProgressIndicator); - -@implementation QIndeterminateProgressIndicator - -- (instancetype)init -{ - if ((self = [super init])) { - _animators = 0; - self.indeterminate = YES; - self.usesThreadedAnimation = NO; - self.alphaValue = 0.0; - } - - return self; -} - -- (void)startAnimation -{ - if (_animators == 0) { - self.hidden = NO; - [super startAnimation:self]; - } - ++_animators; -} - -- (void)stopAnimation -{ - --_animators; - if (_animators == 0) { - [super stopAnimation:self]; - self.hidden = YES; - [self removeFromSuperviewWithoutNeedingDisplay]; - } -} - -- (void)drawWithFrame:(CGRect)rect inView:(NSView *)view -{ - // The alphaValue change is not strictly necessary, but feels safer. - self.alphaValue = 1.0; - if (self.superview != view) - [view addSubview:self]; - if (!CGRectEqualToRect(self.frame, rect)) - self.frame = rect; - [self drawRect:rect]; - self.alphaValue = 0.0; -} - -@end - -@interface QT_MANGLE_NAMESPACE(QVerticalSplitView) : NSSplitView -- (BOOL)isVertical; -@end - -QT_NAMESPACE_ALIAS_OBJC_CLASS(QVerticalSplitView); - -@implementation QVerticalSplitView -- (BOOL)isVertical -{ - return YES; -} -@end - -// See render code in drawPrimitive(PE_FrameTabWidget) -@interface QT_MANGLE_NAMESPACE(QDarkNSBox) : NSBox -@end - -QT_NAMESPACE_ALIAS_OBJC_CLASS(QDarkNSBox); - -@implementation QDarkNSBox -- (instancetype)init -{ - if ((self = [super init])) { - self.title = @""; - self.titlePosition = NSNoTitle; - self.boxType = NSBoxCustom; - self.cornerRadius = 3; - self.borderColor = [NSColor.controlColor colorWithAlphaComponent:0.1]; - self.fillColor = [NSColor.darkGrayColor colorWithAlphaComponent:0.2]; - } - - return self; -} - -- (void)drawRect:(NSRect)rect -{ - [super drawRect:rect]; -} -@end - -QT_BEGIN_NAMESPACE - -// The following constants are used for adjusting the size -// of push buttons so that they are drawn inside their bounds. -const int QMacStylePrivate::PushButtonLeftOffset = 6; -const int QMacStylePrivate::PushButtonRightOffset = 12; -const int QMacStylePrivate::PushButtonContentPadding = 6; - -QVector > QMacStylePrivate::scrollBars; - -// Title bar gradient colors for Lion were determined by inspecting PSDs exported -// using CoreUI's CoreThemeDocument; there is no public API to retrieve them - -static QLinearGradient titlebarGradientActive() -{ - static QLinearGradient darkGradient = [](){ - QLinearGradient gradient; - // FIXME: colors are chosen somewhat arbitrarily and could be fine-tuned, - // or ideally determined by calling a native API. - gradient.setColorAt(0, QColor(47, 47, 47)); - return gradient; - }(); - static QLinearGradient lightGradient = [](){ - QLinearGradient gradient; - gradient.setColorAt(0, QColor(235, 235, 235)); - gradient.setColorAt(0.5, QColor(210, 210, 210)); - gradient.setColorAt(0.75, QColor(195, 195, 195)); - gradient.setColorAt(1, QColor(180, 180, 180)); - return gradient; - }(); - return qt_mac_applicationIsInDarkMode() ? darkGradient : lightGradient; -} - -static QLinearGradient titlebarGradientInactive() -{ - static QLinearGradient darkGradient = [](){ - QLinearGradient gradient; - gradient.setColorAt(1, QColor(42, 42, 42)); - return gradient; - }(); - static QLinearGradient lightGradient = [](){ - QLinearGradient gradient; - gradient.setColorAt(0, QColor(250, 250, 250)); - gradient.setColorAt(1, QColor(225, 225, 225)); - return gradient; - }(); - return qt_mac_applicationIsInDarkMode() ? darkGradient : lightGradient; -} - -static void clipTabBarFrame(const QStyleOption *option, const QMacStyle *style, CGContextRef ctx) -{ - Q_ASSERT(option); - Q_ASSERT(style); - Q_ASSERT(ctx); - - if (qt_mac_applicationIsInDarkMode()) { - QTabWidget *tabWidget = qobject_cast(option->styleObject); - Q_ASSERT(tabWidget); - - const QRect tabBarRect = style->subElementRect(QStyle::SE_TabWidgetTabBar, option, tabWidget).adjusted(2, 2, -3, -2); - const QRegion clipPath = QRegion(option->rect) - tabBarRect; - QVarLengthArray cgRects; - for (const QRect &qtRect : clipPath) - cgRects.push_back(qtRect.toCGRect()); - if (cgRects.size()) - CGContextClipToRects(ctx, &cgRects[0], size_t(cgRects.size())); - } -} - -static const QColor titlebarSeparatorLineActive(111, 111, 111); -static const QColor titlebarSeparatorLineInactive(131, 131, 131); -static const QColor darkModeSeparatorLine(88, 88, 88); - -// Gradient colors used for the dock widget title bar and -// non-unifed tool bar bacground. -static const QColor lightMainWindowGradientBegin(240, 240, 240); -static const QColor lightMainWindowGradientEnd(200, 200, 200); -static const QColor darkMainWindowGradientBegin(47, 47, 47); -static const QColor darkMainWindowGradientEnd(47, 47, 47); - -static const int DisclosureOffset = 4; - -static const qreal titleBarIconTitleSpacing = 5; -static const qreal titleBarTitleRightMargin = 12; -static const qreal titleBarButtonSpacing = 8; - -// Tab bar colors -// active: window is active -// selected: tab is selected -// hovered: tab is hovered -static const QColor tabBarTabBackgroundActive(190, 190, 190); -static const QColor tabBarTabBackgroundActiveHovered(178, 178, 178); -static const QColor tabBarTabBackgroundActiveSelected(211, 211, 211); -static const QColor tabBarTabBackground(227, 227, 227); -static const QColor tabBarTabBackgroundSelected(246, 246, 246); -static const QColor tabBarTabLineActive(160, 160, 160); -static const QColor tabBarTabLineActiveHovered(150, 150, 150); -static const QColor tabBarTabLine(210, 210, 210); -static const QColor tabBarTabLineSelected(189, 189, 189); -static const QColor tabBarCloseButtonBackgroundHovered(162, 162, 162); -static const QColor tabBarCloseButtonBackgroundPressed(153, 153, 153); -static const QColor tabBarCloseButtonBackgroundSelectedHovered(192, 192, 192); -static const QColor tabBarCloseButtonBackgroundSelectedPressed(181, 181, 181); -static const QColor tabBarCloseButtonCross(100, 100, 100); -static const QColor tabBarCloseButtonCrossSelected(115, 115, 115); - -static const int closeButtonSize = 14; -static const qreal closeButtonCornerRadius = 2.0; - -static const int headerSectionArrowHeight = 6; -static const int headerSectionSeparatorInset = 2; - -// One for each of QStyleHelper::WidgetSizePolicy -static const QMarginsF comboBoxFocusRingMargins[3] = { - { 0.5, 2, 3.5, 4 }, - { 0.5, 1, 2.5, 4 }, - { 0.5, 1.5, 2.5, 3.5 } -}; - -static const QMarginsF pullDownButtonShadowMargins[3] = { - { 0.5, -1, 0.5, 2 }, - { 0.5, -1.5, 0.5, 2.5 }, - { 0.5, 0, 0.5, 1 } -}; - -static const QMarginsF pushButtonShadowMargins[3] = { - { 1.5, -1.5, 1.5, 4.5 }, - { 1.5, -1, 1.5, 4 }, - { 1.5, 0.5, 1.5, 2.5 } -}; - -// These are frame heights as reported by Xcode 9's Interface Builder. -// Alignemnet rectangle's heights match for push and popup buttons -// with respective values 21, 18 and 15. - -static const qreal comboBoxDefaultHeight[3] = { - 26, 22, 19 -}; - -static const qreal pushButtonDefaultHeight[3] = { - 32, 28, 16 -}; - -static const qreal popupButtonDefaultHeight[3] = { - 26, 22, 15 -}; - -static const int toolButtonArrowSize = 7; -static const int toolButtonArrowMargin = 2; - -static const qreal focusRingWidth = 3.5; - -static bool setupScroller(NSScroller *scroller, const QStyleOptionSlider *sb) -{ - const qreal length = sb->maximum - sb->minimum + sb->pageStep; - if (qFuzzyIsNull(length)) - return false; - const qreal proportion = sb->pageStep / length; - const qreal range = qreal(sb->maximum - sb->minimum); - qreal value = range ? qreal(sb->sliderValue - sb->minimum) / range : 0; - if (sb->orientation == Qt::Horizontal && sb->direction == Qt::RightToLeft) - value = 1.0 - value; - - scroller.frame = sb->rect.toCGRect(); - scroller.floatValue = value; - scroller.knobProportion = proportion; - return true; -} - -static bool setupSlider(NSSlider *slider, const QStyleOptionSlider *sl) -{ - if (sl->minimum >= sl->maximum) - return false; - - slider.frame = sl->rect.toCGRect(); - slider.minValue = sl->minimum; - slider.maxValue = sl->maximum; - slider.intValue = sl->sliderPosition; - slider.enabled = sl->state & QStyle::State_Enabled; - if (sl->tickPosition != QSlider::NoTicks) { - // Set numberOfTickMarks, but TicksBothSides will be treated differently - int interval = sl->tickInterval; - if (interval == 0) { - interval = sl->pageStep; - if (interval == 0) - interval = sl->singleStep; - if (interval == 0) - interval = 1; // return false? - } - slider.numberOfTickMarks = 1 + ((sl->maximum - sl->minimum) / interval); - - const bool ticksAbove = sl->tickPosition == QSlider::TicksAbove; - if (sl->orientation == Qt::Horizontal) - slider.tickMarkPosition = ticksAbove ? NSTickMarkPositionAbove : NSTickMarkPositionBelow; - else - slider.tickMarkPosition = ticksAbove ? NSTickMarkPositionLeading : NSTickMarkPositionTrailing; - } else { - slider.numberOfTickMarks = 0; - } - - return true; -} - -static bool isInMacUnifiedToolbarArea(QWindow *window, int windowY) -{ - QPlatformNativeInterface *nativeInterface = QGuiApplication::platformNativeInterface(); - QPlatformNativeInterface::NativeResourceForIntegrationFunction function = - nativeInterface->nativeResourceFunctionForIntegration("testContentBorderPosition"); - if (!function) - return false; // Not Cocoa platform plugin. - - typedef bool (*TestContentBorderPositionFunction)(QWindow *, int); - return (reinterpret_cast(function))(window, windowY); -} - - -static void drawTabCloseButton(QPainter *p, bool hover, bool selected, bool pressed, bool documentMode) -{ - p->setRenderHints(QPainter::Antialiasing); - QRect rect(0, 0, closeButtonSize, closeButtonSize); - const int width = rect.width(); - const int height = rect.height(); - - if (hover) { - // draw background circle - QColor background; - if (selected) { - if (documentMode) - background = pressed ? tabBarCloseButtonBackgroundSelectedPressed : tabBarCloseButtonBackgroundSelectedHovered; - else - background = QColor(255, 255, 255, pressed ? 150 : 100); // Translucent white - } else { - background = pressed ? tabBarCloseButtonBackgroundPressed : tabBarCloseButtonBackgroundHovered; - if (!documentMode) - background = background.lighter(pressed ? 135 : 140); // Lighter tab background, lighter color - } - - p->setPen(Qt::transparent); - p->setBrush(background); - p->drawRoundedRect(rect, closeButtonCornerRadius, closeButtonCornerRadius); - } - - // draw cross - const int margin = 3; - QPen crossPen; - crossPen.setColor(selected ? (documentMode ? tabBarCloseButtonCrossSelected : Qt::white) : tabBarCloseButtonCross); - crossPen.setWidthF(1.1); - crossPen.setCapStyle(Qt::FlatCap); - p->setPen(crossPen); - p->drawLine(margin, margin, width - margin, height - margin); - p->drawLine(margin, height - margin, width - margin, margin); -} - -#if QT_CONFIG(tabbar) -QRect rotateTabPainter(QPainter *p, QTabBar::Shape shape, QRect tabRect) -{ - const auto tabDirection = QMacStylePrivate::tabDirection(shape); - if (QMacStylePrivate::verticalTabs(tabDirection)) { - int newX, newY, newRot; - if (tabDirection == QMacStylePrivate::East) { - newX = tabRect.width(); - newY = tabRect.y(); - newRot = 90; - } else { - newX = 0; - newY = tabRect.y() + tabRect.height(); - newRot = -90; - } - tabRect.setRect(0, 0, tabRect.height(), tabRect.width()); - QMatrix m; - m.translate(newX, newY); - m.rotate(newRot); - p->setMatrix(m, true); - } - return tabRect; -} - -void drawTabShape(QPainter *p, const QStyleOptionTab *tabOpt, bool isUnified, int tabOverlap) -{ - QRect rect = tabOpt->rect; - if (QMacStylePrivate::verticalTabs(QMacStylePrivate::tabDirection(tabOpt->shape))) - rect = rect.adjusted(-tabOverlap, 0, 0, 0); - else - rect = rect.adjusted(0, -tabOverlap, 0, 0); - - p->translate(rect.x(), rect.y()); - rect.moveLeft(0); - rect.moveTop(0); - const QRect tabRect = rotateTabPainter(p, tabOpt->shape, rect); - - const int width = tabRect.width(); - const int height = tabRect.height(); - const bool active = (tabOpt->state & QStyle::State_Active); - const bool selected = (tabOpt->state & QStyle::State_Selected); - - const QRect bodyRect(1, 1, width - 2, height - 2); - const QRect topLineRect(1, 0, width - 2, 1); - const QRect bottomLineRect(1, height - 1, width - 2, 1); - if (selected) { - // fill body - if (tabOpt->documentMode && isUnified) { - p->save(); - p->setCompositionMode(QPainter::CompositionMode_Source); - p->fillRect(tabRect, QColor(Qt::transparent)); - p->restore(); - } else if (active) { - p->fillRect(bodyRect, tabBarTabBackgroundActiveSelected); - // top line - p->fillRect(topLineRect, tabBarTabLineSelected); - } else { - p->fillRect(bodyRect, tabBarTabBackgroundSelected); - } - } else { - // when the mouse is over non selected tabs they get a new color - const bool hover = (tabOpt->state & QStyle::State_MouseOver); - if (hover) { - // fill body - p->fillRect(bodyRect, tabBarTabBackgroundActiveHovered); - // bottom line - p->fillRect(bottomLineRect, tabBarTabLineActiveHovered); - } - } - - // separator lines between tabs - const QRect leftLineRect(0, 1, 1, height - 2); - const QRect rightLineRect(width - 1, 1, 1, height - 2); - const QColor separatorLineColor = active ? tabBarTabLineActive : tabBarTabLine; - p->fillRect(leftLineRect, separatorLineColor); - p->fillRect(rightLineRect, separatorLineColor); -} - -void drawTabBase(QPainter *p, const QStyleOptionTabBarBase *tbb, const QWidget *w) -{ - QRect r = tbb->rect; - if (QMacStylePrivate::verticalTabs(QMacStylePrivate::tabDirection(tbb->shape))) - r.setWidth(w->width()); - else - r.setHeight(w->height()); - - const QRect tabRect = rotateTabPainter(p, tbb->shape, r); - const int width = tabRect.width(); - const int height = tabRect.height(); - const bool active = (tbb->state & QStyle::State_Active); - - // fill body - const QRect bodyRect(0, 1, width, height - 1); - const QColor bodyColor = active ? tabBarTabBackgroundActive : tabBarTabBackground; - p->fillRect(bodyRect, bodyColor); - - // top line - const QRect topLineRect(0, 0, width, 1); - const QColor topLineColor = active ? tabBarTabLineActive : tabBarTabLine; - p->fillRect(topLineRect, topLineColor); - - // bottom line - const QRect bottomLineRect(0, height - 1, width, 1); - const QColor bottomLineColor = active ? tabBarTabLineActive : tabBarTabLine; - p->fillRect(bottomLineRect, bottomLineColor); -} -#endif - -static QStyleHelper::WidgetSizePolicy getControlSize(const QStyleOption *option, const QWidget *widget) -{ - const auto wsp = QStyleHelper::widgetSizePolicy(widget, option); - if (wsp == QStyleHelper::SizeDefault) - return QStyleHelper::SizeLarge; - - return wsp; -} - -#if QT_CONFIG(treeview) -static inline bool isTreeView(const QWidget *widget) -{ - return (widget && widget->parentWidget() && - qobject_cast(widget->parentWidget())); -} -#endif - -static QString qt_mac_removeMnemonics(const QString &original) -{ - QString returnText(original.size(), 0); - int finalDest = 0; - int currPos = 0; - int l = original.length(); - while (l) { - if (original.at(currPos) == QLatin1Char('&')) { - ++currPos; - --l; - if (l == 0) - break; - } else if (original.at(currPos) == QLatin1Char('(') && l >= 4 && - original.at(currPos + 1) == QLatin1Char('&') && - original.at(currPos + 2) != QLatin1Char('&') && - original.at(currPos + 3) == QLatin1Char(')')) { - /* remove mnemonics its format is "\s*(&X)" */ - int n = 0; - while (finalDest > n && returnText.at(finalDest - n - 1).isSpace()) - ++n; - finalDest -= n; - currPos += 4; - l -= 4; - continue; - } - returnText[finalDest] = original.at(currPos); - ++currPos; - ++finalDest; - --l; - } - returnText.truncate(finalDest); - return returnText; -} - -static bool qt_macWindowMainWindow(const QWidget *window) -{ - if (QWindow *w = window->windowHandle()) { - if (w->handle()) { - if (NSWindow *nswindow = static_cast(QGuiApplication::platformNativeInterface()->nativeResourceForWindow(QByteArrayLiteral("nswindow"), w))) { - return [nswindow isMainWindow]; - } - } - } - return false; -} - -/***************************************************************************** - QMacCGStyle globals - *****************************************************************************/ -const int macItemFrame = 2; // menu item frame width -const int macItemHMargin = 3; // menu item hor text margin -const int macRightBorder = 12; // right border on mac - -/***************************************************************************** - QMacCGStyle utility functions - *****************************************************************************/ - -enum QAquaMetric { - // Prepend kThemeMetric to get the HIToolBox constant. - // Represents the values already used in QMacStyle. - CheckBoxHeight = 0, - CheckBoxWidth, - EditTextFrameOutset, - FocusRectOutset, - HSliderHeight, - HSliderTickHeight, - LargeProgressBarThickness, - ListHeaderHeight, - MenuSeparatorHeight, // GetThemeMenuSeparatorHeight - MiniCheckBoxHeight, - MiniCheckBoxWidth, - MiniHSliderHeight, - MiniHSliderTickHeight, - MiniPopupButtonHeight, - MiniPushButtonHeight, - MiniRadioButtonHeight, - MiniRadioButtonWidth, - MiniVSliderTickWidth, - MiniVSliderWidth, - NormalProgressBarThickness, - PopupButtonHeight, - ProgressBarShadowOutset, - PushButtonHeight, - RadioButtonHeight, - RadioButtonWidth, - SeparatorSize, - SmallCheckBoxHeight, - SmallCheckBoxWidth, - SmallHSliderHeight, - SmallHSliderTickHeight, - SmallPopupButtonHeight, - SmallProgressBarShadowOutset, - SmallPushButtonHeight, - SmallRadioButtonHeight, - SmallRadioButtonWidth, - SmallVSliderTickWidth, - SmallVSliderWidth, - VSliderTickWidth, - VSliderWidth -}; - -static const int qt_mac_aqua_metrics[] = { - // Values as of macOS 10.12.4 and Xcode 8.3.1 - 18 /* CheckBoxHeight */, - 18 /* CheckBoxWidth */, - 1 /* EditTextFrameOutset */, - 4 /* FocusRectOutset */, - 22 /* HSliderHeight */, - 5 /* HSliderTickHeight */, - 16 /* LargeProgressBarThickness */, - 17 /* ListHeaderHeight */, - 12 /* MenuSeparatorHeight, aka GetThemeMenuSeparatorHeight */, - 11 /* MiniCheckBoxHeight */, - 10 /* MiniCheckBoxWidth */, - 12 /* MiniHSliderHeight */, - 4 /* MiniHSliderTickHeight */, - 15 /* MiniPopupButtonHeight */, - 16 /* MiniPushButtonHeight */, - 11 /* MiniRadioButtonHeight */, - 10 /* MiniRadioButtonWidth */, - 4 /* MiniVSliderTickWidth */, - 12 /* MiniVSliderWidth */, - 12 /* NormalProgressBarThickness */, - 20 /* PopupButtonHeight */, - 4 /* ProgressBarShadowOutset */, - 20 /* PushButtonHeight */, - 18 /* RadioButtonHeight */, - 18 /* RadioButtonWidth */, - 1 /* SeparatorSize */, - 16 /* SmallCheckBoxHeight */, - 14 /* SmallCheckBoxWidth */, - 15 /* SmallHSliderHeight */, - 4 /* SmallHSliderTickHeight */, - 17 /* SmallPopupButtonHeight */, - 2 /* SmallProgressBarShadowOutset */, - 17 /* SmallPushButtonHeight */, - 15 /* SmallRadioButtonHeight */, - 14 /* SmallRadioButtonWidth */, - 4 /* SmallVSliderTickWidth */, - 15 /* SmallVSliderWidth */, - 5 /* VSliderTickWidth */, - 22 /* VSliderWidth */ -}; - -static inline int qt_mac_aqua_get_metric(QAquaMetric m) -{ - return qt_mac_aqua_metrics[m]; -} - -static QSize qt_aqua_get_known_size(QStyle::ContentsType ct, const QWidget *widg, QSize szHint, - QStyleHelper::WidgetSizePolicy sz) -{ - QSize ret(-1, -1); - if (sz != QStyleHelper::SizeSmall && sz != QStyleHelper::SizeLarge && sz != QStyleHelper::SizeMini) { - qDebug("Not sure how to return this..."); - return ret; - } - if ((widg && widg->testAttribute(Qt::WA_SetFont)) || !QApplication::desktopSettingsAware()) { - // If you're using a custom font and it's bigger than the default font, - // then no constraints for you. If you are smaller, we can try to help you out - QFont font = qt_app_fonts_hash()->value(widg->metaObject()->className(), QFont()); - if (widg->font().pointSize() > font.pointSize()) - return ret; - } - - if (ct == QStyle::CT_CustomBase && widg) { -#if QT_CONFIG(pushbutton) - if (qobject_cast(widg)) - ct = QStyle::CT_PushButton; -#endif - else if (qobject_cast(widg)) - ct = QStyle::CT_RadioButton; -#if QT_CONFIG(checkbox) - else if (qobject_cast(widg)) - ct = QStyle::CT_CheckBox; -#endif -#if QT_CONFIG(combobox) - else if (qobject_cast(widg)) - ct = QStyle::CT_ComboBox; -#endif -#if QT_CONFIG(toolbutton) - else if (qobject_cast(widg)) - ct = QStyle::CT_ToolButton; -#endif - else if (qobject_cast(widg)) - ct = QStyle::CT_Slider; -#if QT_CONFIG(progressbar) - else if (qobject_cast(widg)) - ct = QStyle::CT_ProgressBar; -#endif -#if QT_CONFIG(lineedit) - else if (qobject_cast(widg)) - ct = QStyle::CT_LineEdit; -#endif - else if (qobject_cast(widg)) - ct = QStyle::CT_HeaderSection; -#if QT_CONFIG(menubar) - else if (qobject_cast(widg)) - ct = QStyle::CT_MenuBar; -#endif -#if QT_CONFIG(sizegrip) - else if (qobject_cast(widg)) - ct = QStyle::CT_SizeGrip; -#endif - else - return ret; - } - - switch (ct) { -#if QT_CONFIG(pushbutton) - case QStyle::CT_PushButton: { - const QPushButton *psh = qobject_cast(widg); - // If this comparison is false, then the widget was not a push button. - // This is bad and there's very little we can do since we were requested to find a - // sensible size for a widget that pretends to be a QPushButton but is not. - if(psh) { - QString buttonText = qt_mac_removeMnemonics(psh->text()); - if (buttonText.contains(QLatin1Char('\n'))) - ret = QSize(-1, -1); - else if (sz == QStyleHelper::SizeLarge) - ret = QSize(-1, qt_mac_aqua_get_metric(PushButtonHeight)); - else if (sz == QStyleHelper::SizeSmall) - ret = QSize(-1, qt_mac_aqua_get_metric(SmallPushButtonHeight)); - else if (sz == QStyleHelper::SizeMini) - ret = QSize(-1, qt_mac_aqua_get_metric(MiniPushButtonHeight)); - - if (!psh->icon().isNull()){ - // If the button got an icon, and the icon is larger than the - // button, we can't decide on a default size - ret.setWidth(-1); - if (ret.height() < psh->iconSize().height()) - ret.setHeight(-1); - } - else if (buttonText == QLatin1String("OK") || buttonText == QLatin1String("Cancel")){ - // Aqua Style guidelines restrict the size of OK and Cancel buttons to 68 pixels. - // However, this doesn't work for German, therefore only do it for English, - // I suppose it would be better to do some sort of lookups for languages - // that like to have really long words. - // FIXME This is not exactly true. Out of context, OK buttons have their - // implicit size calculated the same way as any other button. Inside a - // QDialogButtonBox, their size should be calculated such that the action - // or accept button (i.e., rightmost) and cancel button have the same width. - ret.setWidth(69); - } - } else { - // The only sensible thing to do is to return whatever the style suggests... - if (sz == QStyleHelper::SizeLarge) - ret = QSize(-1, qt_mac_aqua_get_metric(PushButtonHeight)); - else if (sz == QStyleHelper::SizeSmall) - ret = QSize(-1, qt_mac_aqua_get_metric(SmallPushButtonHeight)); - else if (sz == QStyleHelper::SizeMini) - ret = QSize(-1, qt_mac_aqua_get_metric(MiniPushButtonHeight)); - else - // Since there's no default size we return the large size... - ret = QSize(-1, qt_mac_aqua_get_metric(PushButtonHeight)); - } -#endif -#if 0 //Not sure we are applying the rules correctly for RadioButtons/CheckBoxes --Sam - } else if (ct == QStyle::CT_RadioButton) { - QRadioButton *rdo = static_cast(widg); - // Exception for case where multiline radio button text requires no size constrainment - if (rdo->text().find('\n') != -1) - return ret; - if (sz == QStyleHelper::SizeLarge) - ret = QSize(-1, qt_mac_aqua_get_metric(RadioButtonHeight)); - else if (sz == QStyleHelper::SizeSmall) - ret = QSize(-1, qt_mac_aqua_get_metric(SmallRadioButtonHeight)); - else if (sz == QStyleHelper::SizeMini) - ret = QSize(-1, qt_mac_aqua_get_metric(MiniRadioButtonHeight)); - } else if (ct == QStyle::CT_CheckBox) { - if (sz == QStyleHelper::SizeLarge) - ret = QSize(-1, qt_mac_aqua_get_metric(CheckBoxHeight)); - else if (sz == QStyleHelper::SizeSmall) - ret = QSize(-1, qt_mac_aqua_get_metric(SmallCheckBoxHeight)); - else if (sz == QStyleHelper::SizeMini) - ret = QSize(-1, qt_mac_aqua_get_metric(MiniCheckBoxHeight)); -#endif - break; - } - case QStyle::CT_SizeGrip: - // Not HIG kosher: mimic what we were doing earlier until we support 4-edge resizing in MDI subwindows - if (sz == QStyleHelper::SizeLarge || sz == QStyleHelper::SizeSmall) { - int s = sz == QStyleHelper::SizeSmall ? 16 : 22; // large: pixel measured from HITheme, small: from my hat - int width = 0; -#if QT_CONFIG(mdiarea) - if (widg && qobject_cast(widg->parentWidget())) - width = s; -#endif - ret = QSize(width, s); - } - break; - case QStyle::CT_ComboBox: - switch (sz) { - case QStyleHelper::SizeLarge: - ret = QSize(-1, qt_mac_aqua_get_metric(PopupButtonHeight)); - break; - case QStyleHelper::SizeSmall: - ret = QSize(-1, qt_mac_aqua_get_metric(SmallPopupButtonHeight)); - break; - case QStyleHelper::SizeMini: - ret = QSize(-1, qt_mac_aqua_get_metric(MiniPopupButtonHeight)); - break; - default: - break; - } - break; - case QStyle::CT_ToolButton: - if (sz == QStyleHelper::SizeSmall) { - int width = 0, height = 0; - if (szHint == QSize(-1, -1)) { //just 'guess'.. -#if QT_CONFIG(toolbutton) - const QToolButton *bt = qobject_cast(widg); - // If this conversion fails then the widget was not what it claimed to be. - if(bt) { - if (!bt->icon().isNull()) { - QSize iconSize = bt->iconSize(); - QSize pmSize = bt->icon().actualSize(QSize(32, 32), QIcon::Normal); - width = qMax(width, qMax(iconSize.width(), pmSize.width())); - height = qMax(height, qMax(iconSize.height(), pmSize.height())); - } - if (!bt->text().isNull() && bt->toolButtonStyle() != Qt::ToolButtonIconOnly) { - int text_width = bt->fontMetrics().horizontalAdvance(bt->text()), - text_height = bt->fontMetrics().height(); - if (bt->toolButtonStyle() == Qt::ToolButtonTextUnderIcon) { - width = qMax(width, text_width); - height += text_height; - } else { - width += text_width; - width = qMax(height, text_height); - } - } - } else -#endif - { - // Let's return the size hint... - width = szHint.width(); - height = szHint.height(); - } - } else { - width = szHint.width(); - height = szHint.height(); - } - width = qMax(20, width + 5); //border - height = qMax(20, height + 5); //border - ret = QSize(width, height); - } - break; - case QStyle::CT_Slider: { - int w = -1; - const QSlider *sld = qobject_cast(widg); - // If this conversion fails then the widget was not what it claimed to be. - if(sld) { - if (sz == QStyleHelper::SizeLarge) { - if (sld->orientation() == Qt::Horizontal) { - w = qt_mac_aqua_get_metric(HSliderHeight); - if (sld->tickPosition() != QSlider::NoTicks) - w += qt_mac_aqua_get_metric(HSliderTickHeight); - } else { - w = qt_mac_aqua_get_metric(VSliderWidth); - if (sld->tickPosition() != QSlider::NoTicks) - w += qt_mac_aqua_get_metric(VSliderTickWidth); - } - } else if (sz == QStyleHelper::SizeSmall) { - if (sld->orientation() == Qt::Horizontal) { - w = qt_mac_aqua_get_metric(SmallHSliderHeight); - if (sld->tickPosition() != QSlider::NoTicks) - w += qt_mac_aqua_get_metric(SmallHSliderTickHeight); - } else { - w = qt_mac_aqua_get_metric(SmallVSliderWidth); - if (sld->tickPosition() != QSlider::NoTicks) - w += qt_mac_aqua_get_metric(SmallVSliderTickWidth); - } - } else if (sz == QStyleHelper::SizeMini) { - if (sld->orientation() == Qt::Horizontal) { - w = qt_mac_aqua_get_metric(MiniHSliderHeight); - if (sld->tickPosition() != QSlider::NoTicks) - w += qt_mac_aqua_get_metric(MiniHSliderTickHeight); - } else { - w = qt_mac_aqua_get_metric(MiniVSliderWidth); - if (sld->tickPosition() != QSlider::NoTicks) - w += qt_mac_aqua_get_metric(MiniVSliderTickWidth); - } - } - } else { - // This is tricky, we were requested to find a size for a slider which is not - // a slider. We don't know if this is vertical or horizontal or if we need to - // have tick marks or not. - // For this case we will return an horizontal slider without tick marks. - w = qt_mac_aqua_get_metric(HSliderHeight); - w += qt_mac_aqua_get_metric(HSliderTickHeight); - } - if (sld->orientation() == Qt::Horizontal) - ret.setHeight(w); - else - ret.setWidth(w); - break; - } -#if QT_CONFIG(progressbar) - case QStyle::CT_ProgressBar: { - int finalValue = -1; - Qt::Orientation orient = Qt::Horizontal; - if (const QProgressBar *pb = qobject_cast(widg)) - orient = pb->orientation(); - - if (sz == QStyleHelper::SizeLarge) - finalValue = qt_mac_aqua_get_metric(LargeProgressBarThickness) - + qt_mac_aqua_get_metric(ProgressBarShadowOutset); - else - finalValue = qt_mac_aqua_get_metric(NormalProgressBarThickness) - + qt_mac_aqua_get_metric(SmallProgressBarShadowOutset); - if (orient == Qt::Horizontal) - ret.setHeight(finalValue); - else - ret.setWidth(finalValue); - break; - } -#endif -#if QT_CONFIG(combobox) - case QStyle::CT_LineEdit: - if (!widg || !qobject_cast(widg->parentWidget())) { - //should I take into account the font dimentions of the lineedit? -Sam - if (sz == QStyleHelper::SizeLarge) - ret = QSize(-1, 21); - else - ret = QSize(-1, 19); - } - break; -#endif - case QStyle::CT_HeaderSection: -#if QT_CONFIG(treeview) - if (isTreeView(widg)) - ret = QSize(-1, qt_mac_aqua_get_metric(ListHeaderHeight)); -#endif - break; - case QStyle::CT_MenuBar: - if (sz == QStyleHelper::SizeLarge) { - ret = QSize(-1, [[NSApp mainMenu] menuBarHeight]); - // In the qt_mac_set_native_menubar(false) case, - // we come it here with a zero-height main menu, - // preventing the in-window menu from displaying. - // Use 22 pixels for the height, by observation. - if (ret.height() <= 0) - ret.setHeight(22); - } - break; - default: - break; - } - return ret; -} - - -#if defined(QMAC_QAQUASTYLE_SIZE_CONSTRAIN) || defined(DEBUG_SIZE_CONSTRAINT) -static QStyleHelper::WidgetSizePolicy qt_aqua_guess_size(const QWidget *widg, QSize large, QSize small, QSize mini) -{ - Q_UNUSED(widg); - - if (large == QSize(-1, -1)) { - if (small != QSize(-1, -1)) - return QStyleHelper::SizeSmall; - if (mini != QSize(-1, -1)) - return QStyleHelper::SizeMini; - return QStyleHelper::SizeDefault; - } else if (small == QSize(-1, -1)) { - if (mini != QSize(-1, -1)) - return QStyleHelper::SizeMini; - return QStyleHelper::SizeLarge; - } else if (mini == QSize(-1, -1)) { - return QStyleHelper::SizeLarge; - } - - if (qEnvironmentVariableIsSet("QWIDGET_ALL_SMALL")) - return QStyleHelper::SizeSmall; - else if (qEnvironmentVariableIsSet("QWIDGET_ALL_MINI")) - return QStyleHelper::SizeMini; - - return QStyleHelper::SizeLarge; -} -#endif - -void QMacStylePrivate::drawFocusRing(QPainter *p, const QRectF &targetRect, int hMargin, int vMargin, const CocoaControl &cw) const -{ - QPainterPath focusRingPath; - focusRingPath.setFillRule(Qt::OddEvenFill); - - qreal hOffset = 0.0; - qreal vOffset = 0.0; - switch (cw.type) { - case Box: - case Button_SquareButton: - case SegmentedControl_Middle: - case TextField: { - auto innerRect = targetRect; - if (cw.type == TextField) - innerRect = innerRect.adjusted(hMargin, vMargin, -hMargin, -vMargin).adjusted(0.5, 0.5, -0.5, -0.5); - const auto outerRect = innerRect.adjusted(-focusRingWidth, -focusRingWidth, focusRingWidth, focusRingWidth); - const auto outerRadius = focusRingWidth; - focusRingPath.addRect(innerRect); - focusRingPath.addRoundedRect(outerRect, outerRadius, outerRadius); - break; - } - case Button_CheckBox: { - const auto cbInnerRadius = (cw.size == QStyleHelper::SizeMini ? 2.0 : 3.0); - const auto cbSize = cw.size == QStyleHelper::SizeLarge ? 13 : - cw.size == QStyleHelper::SizeSmall ? 11 : 9; // As measured - hOffset = hMargin + (cw.size == QStyleHelper::SizeLarge ? 2.5 : - cw.size == QStyleHelper::SizeSmall ? 2.0 : 1.0); // As measured - vOffset = 0.5 * qreal(targetRect.height() - cbSize); - const auto cbInnerRect = QRectF(0, 0, cbSize, cbSize); - const auto cbOuterRadius = cbInnerRadius + focusRingWidth; - const auto cbOuterRect = cbInnerRect.adjusted(-focusRingWidth, -focusRingWidth, focusRingWidth, focusRingWidth); - focusRingPath.addRoundedRect(cbOuterRect, cbOuterRadius, cbOuterRadius); - focusRingPath.addRoundedRect(cbInnerRect, cbInnerRadius, cbInnerRadius); - break; - } - case Button_RadioButton: { - const auto rbSize = cw.size == QStyleHelper::SizeLarge ? 15 : - cw.size == QStyleHelper::SizeSmall ? 13 : 9; // As measured - hOffset = hMargin + (cw.size == QStyleHelper::SizeLarge ? 1.5 : - cw.size == QStyleHelper::SizeSmall ? 1.0 : 1.0); // As measured - vOffset = 0.5 * qreal(targetRect.height() - rbSize); - const auto rbInnerRect = QRectF(0, 0, rbSize, rbSize); - const auto rbOuterRect = rbInnerRect.adjusted(-focusRingWidth, -focusRingWidth, focusRingWidth, focusRingWidth); - focusRingPath.addEllipse(rbInnerRect); - focusRingPath.addEllipse(rbOuterRect); - break; - } - case Button_PopupButton: - case Button_PullDown: - case Button_PushButton: - case SegmentedControl_Single: { - const qreal innerRadius = cw.type == Button_PushButton ? 3 : 4; - const qreal outerRadius = innerRadius + focusRingWidth; - hOffset = targetRect.left(); - vOffset = targetRect.top(); - const auto innerRect = targetRect.translated(-targetRect.topLeft()); - const auto outerRect = innerRect.adjusted(-hMargin, -vMargin, hMargin, vMargin); - focusRingPath.addRoundedRect(innerRect, innerRadius, innerRadius); - focusRingPath.addRoundedRect(outerRect, outerRadius, outerRadius); - break; - } - case ComboBox: - case SegmentedControl_First: - case SegmentedControl_Last: { - hOffset = targetRect.left(); - vOffset = targetRect.top(); - const qreal innerRadius = 8; - const qreal outerRadius = innerRadius + focusRingWidth; - const auto innerRect = targetRect.translated(-targetRect.topLeft()); - const auto outerRect = innerRect.adjusted(-hMargin, -vMargin, hMargin, vMargin); - - const auto cbFocusFramePath = [](const QRectF &rect, qreal tRadius, qreal bRadius) { - QPainterPath path; - - if (tRadius > 0) { - const auto topLeftCorner = QRectF(rect.topLeft(), QSizeF(tRadius, tRadius)); - path.arcMoveTo(topLeftCorner, 180); - path.arcTo(topLeftCorner, 180, -90); - } else { - path.moveTo(rect.topLeft()); - } - const auto rightEdge = rect.right() - bRadius; - path.arcTo(rightEdge, rect.top(), bRadius, bRadius, 90, -90); - path.arcTo(rightEdge, rect.bottom() - bRadius, bRadius, bRadius, 0, -90); - if (tRadius > 0) - path.arcTo(rect.left(), rect.bottom() - tRadius, tRadius, tRadius, 270, -90); - else - path.lineTo(rect.bottomLeft()); - path.closeSubpath(); - - return path; - }; - - const auto innerPath = cbFocusFramePath(innerRect, 0, innerRadius); - focusRingPath.addPath(innerPath); - const auto outerPath = cbFocusFramePath(outerRect, 2 * focusRingWidth, outerRadius); - focusRingPath.addPath(outerPath); - break; - } - default: - Q_UNREACHABLE(); - } - - const auto focusRingColor = qt_mac_toQColor(NSColor.keyboardFocusIndicatorColor.CGColor); - - p->save(); - p->setRenderHint(QPainter::Antialiasing); - p->setOpacity(0.5); - if (cw.type == SegmentedControl_First) { - // TODO Flip left-right - } - p->translate(hOffset, vOffset); - p->fillPath(focusRingPath, focusRingColor); - p->restore(); -} - -QPainterPath QMacStylePrivate::windowPanelPath(const QRectF &r) const -{ - static const qreal CornerPointOffset = 5.5; - static const qreal CornerControlOffset = 2.1; - - QPainterPath path; - // Top-left corner - path.moveTo(r.left(), r.top() + CornerPointOffset); - path.cubicTo(r.left(), r.top() + CornerControlOffset, - r.left() + CornerControlOffset, r.top(), - r.left() + CornerPointOffset, r.top()); - // Top-right corner - path.lineTo(r.right() - CornerPointOffset, r.top()); - path.cubicTo(r.right() - CornerControlOffset, r.top(), - r.right(), r.top() + CornerControlOffset, - r.right(), r.top() + CornerPointOffset); - // Bottom-right corner - path.lineTo(r.right(), r.bottom() - CornerPointOffset); - path.cubicTo(r.right(), r.bottom() - CornerControlOffset, - r.right() - CornerControlOffset, r.bottom(), - r.right() - CornerPointOffset, r.bottom()); - // Bottom-right corner - path.lineTo(r.left() + CornerPointOffset, r.bottom()); - path.cubicTo(r.left() + CornerControlOffset, r.bottom(), - r.left(), r.bottom() - CornerControlOffset, - r.left(), r.bottom() - CornerPointOffset); - path.lineTo(r.left(), r.top() + CornerPointOffset); - - return path; -} - -QMacStylePrivate::CocoaControlType QMacStylePrivate::windowButtonCocoaControl(QStyle::SubControl sc) const -{ - struct WindowButtons { - QStyle::SubControl sc; - QMacStylePrivate::CocoaControlType ct; - }; - - static const WindowButtons buttons[] = { - { QStyle::SC_TitleBarCloseButton, QMacStylePrivate::Button_WindowClose }, - { QStyle::SC_TitleBarMinButton, QMacStylePrivate::Button_WindowMiniaturize }, - { QStyle::SC_TitleBarMaxButton, QMacStylePrivate::Button_WindowZoom } - }; - - for (const auto &wb : buttons) - if (wb.sc == sc) - return wb.ct; - - return NoControl; -} - - -#if QT_CONFIG(tabbar) -void QMacStylePrivate::tabLayout(const QStyleOptionTab *opt, const QWidget *widget, QRect *textRect, QRect *iconRect) const -{ - Q_ASSERT(textRect); - Q_ASSERT(iconRect); - QRect tr = opt->rect; - const bool verticalTabs = opt->shape == QTabBar::RoundedEast - || opt->shape == QTabBar::RoundedWest - || opt->shape == QTabBar::TriangularEast - || opt->shape == QTabBar::TriangularWest; - if (verticalTabs) - tr.setRect(0, 0, tr.height(), tr.width()); // 0, 0 as we will have a translate transform - - int verticalShift = proxyStyle->pixelMetric(QStyle::PM_TabBarTabShiftVertical, opt, widget); - int horizontalShift = proxyStyle->pixelMetric(QStyle::PM_TabBarTabShiftHorizontal, opt, widget); - const int hpadding = 4; - const int vpadding = proxyStyle->pixelMetric(QStyle::PM_TabBarTabVSpace, opt, widget) / 2; - if (opt->shape == QTabBar::RoundedSouth || opt->shape == QTabBar::TriangularSouth) - verticalShift = -verticalShift; - tr.adjust(hpadding, verticalShift - vpadding, horizontalShift - hpadding, vpadding); - - // left widget - if (!opt->leftButtonSize.isEmpty()) { - const int buttonSize = verticalTabs ? opt->leftButtonSize.height() : opt->leftButtonSize.width(); - tr.setLeft(tr.left() + 4 + buttonSize); - // make text aligned to center - if (opt->rightButtonSize.isEmpty()) - tr.setRight(tr.right() - 4 - buttonSize); - } - // right widget - if (!opt->rightButtonSize.isEmpty()) { - const int buttonSize = verticalTabs ? opt->rightButtonSize.height() : opt->rightButtonSize.width(); - tr.setRight(tr.right() - 4 - buttonSize); - // make text aligned to center - if (opt->leftButtonSize.isEmpty()) - tr.setLeft(tr.left() + 4 + buttonSize); - } - - // icon - if (!opt->icon.isNull()) { - QSize iconSize = opt->iconSize; - if (!iconSize.isValid()) { - int iconExtent = proxyStyle->pixelMetric(QStyle::PM_SmallIconSize); - iconSize = QSize(iconExtent, iconExtent); - } - QSize tabIconSize = opt->icon.actualSize(iconSize, - (opt->state & QStyle::State_Enabled) ? QIcon::Normal : QIcon::Disabled, - (opt->state & QStyle::State_Selected) ? QIcon::On : QIcon::Off); - // High-dpi icons do not need adjustment; make sure tabIconSize is not larger than iconSize - tabIconSize = QSize(qMin(tabIconSize.width(), iconSize.width()), qMin(tabIconSize.height(), iconSize.height())); - - *iconRect = QRect(tr.left(), tr.center().y() - tabIconSize.height() / 2, - tabIconSize.width(), tabIconSize.height()); - if (!verticalTabs) - *iconRect = proxyStyle->visualRect(opt->direction, opt->rect, *iconRect); - - int stylePadding = proxyStyle->pixelMetric(QStyle::PM_TabBarTabHSpace, opt, widget) / 2; - stylePadding -= hpadding; - - tr.setLeft(tr.left() + stylePadding + tabIconSize.width() + 4); - tr.setRight(tr.right() - stylePadding - tabIconSize.width() - 4); - } - - if (!verticalTabs) - tr = proxyStyle->visualRect(opt->direction, opt->rect, tr); - - *textRect = tr; -} - -QMacStylePrivate::Direction QMacStylePrivate::tabDirection(QTabBar::Shape shape) -{ - switch (shape) { - case QTabBar::RoundedSouth: - case QTabBar::TriangularSouth: - return South; - case QTabBar::RoundedNorth: - case QTabBar::TriangularNorth: - return North; - case QTabBar::RoundedWest: - case QTabBar::TriangularWest: - return West; - case QTabBar::RoundedEast: - case QTabBar::TriangularEast: - return East; - } -} - -bool QMacStylePrivate::verticalTabs(QMacStylePrivate::Direction direction) -{ - return (direction == QMacStylePrivate::East - || direction == QMacStylePrivate::West); -} - -#endif // QT_CONFIG(tabbar) - -QStyleHelper::WidgetSizePolicy QMacStylePrivate::effectiveAquaSizeConstrain(const QStyleOption *option, - const QWidget *widg, - QStyle::ContentsType ct, - QSize szHint, QSize *insz) const -{ - QStyleHelper::WidgetSizePolicy sz = aquaSizeConstrain(option, widg, ct, szHint, insz); - if (sz == QStyleHelper::SizeDefault) - return QStyleHelper::SizeLarge; - return sz; -} - -QStyleHelper::WidgetSizePolicy QMacStylePrivate::aquaSizeConstrain(const QStyleOption *option, const QWidget *widg, - QStyle::ContentsType ct, QSize szHint, QSize *insz) const -{ -#if defined(QMAC_QAQUASTYLE_SIZE_CONSTRAIN) || defined(DEBUG_SIZE_CONSTRAINT) - if (option) { - if (option->state & QStyle::State_Small) - return QStyleHelper::SizeSmall; - if (option->state & QStyle::State_Mini) - return QStyleHelper::SizeMini; - } - - if (!widg) { - if (insz) - *insz = QSize(); - if (qEnvironmentVariableIsSet("QWIDGET_ALL_SMALL")) - return QStyleHelper::SizeSmall; - if (qEnvironmentVariableIsSet("QWIDGET_ALL_MINI")) - return QStyleHelper::SizeMini; - return QStyleHelper::SizeDefault; - } - - QSize large = qt_aqua_get_known_size(ct, widg, szHint, QStyleHelper::SizeLarge), - small = qt_aqua_get_known_size(ct, widg, szHint, QStyleHelper::SizeSmall), - mini = qt_aqua_get_known_size(ct, widg, szHint, QStyleHelper::SizeMini); - bool guess_size = false; - QStyleHelper::WidgetSizePolicy ret = QStyleHelper::SizeDefault; - QStyleHelper::WidgetSizePolicy wsp = QStyleHelper::widgetSizePolicy(widg); - if (wsp == QStyleHelper::SizeDefault) - guess_size = true; - else if (wsp == QStyleHelper::SizeMini) - ret = QStyleHelper::SizeMini; - else if (wsp == QStyleHelper::SizeSmall) - ret = QStyleHelper::SizeSmall; - else if (wsp == QStyleHelper::SizeLarge) - ret = QStyleHelper::SizeLarge; - if (guess_size) - ret = qt_aqua_guess_size(widg, large, small, mini); - - QSize *sz = 0; - if (ret == QStyleHelper::SizeSmall) - sz = &small; - else if (ret == QStyleHelper::SizeLarge) - sz = &large; - else if (ret == QStyleHelper::SizeMini) - sz = &mini; - if (insz) - *insz = sz ? *sz : QSize(-1, -1); -#ifdef DEBUG_SIZE_CONSTRAINT - if (sz) { - const char *size_desc = "Unknown"; - if (sz == &small) - size_desc = "Small"; - else if (sz == &large) - size_desc = "Large"; - else if (sz == &mini) - size_desc = "Mini"; - qDebug("%s - %s: %s taken (%d, %d) [%d, %d]", - widg ? widg->objectName().toLatin1().constData() : "*Unknown*", - widg ? widg->metaObject()->className() : "*Unknown*", size_desc, widg->width(), widg->height(), - sz->width(), sz->height()); - } -#endif - return ret; -#else - if (insz) - *insz = QSize(); - Q_UNUSED(widg); - Q_UNUSED(ct); - Q_UNUSED(szHint); - return QStyleHelper::SizeDefault; -#endif -} - -uint qHash(const QMacStylePrivate::CocoaControl &cw, uint seed = 0) -{ - return ((cw.type << 2) | cw.size) ^ seed; -} - -QMacStylePrivate::CocoaControl::CocoaControl() - : type(NoControl), size(QStyleHelper::SizeDefault) -{ -} - -QMacStylePrivate::CocoaControl::CocoaControl(CocoaControlType t, QStyleHelper::WidgetSizePolicy s) - : type(t), size(s) -{ -} - -bool QMacStylePrivate::CocoaControl::operator==(const CocoaControl &other) const -{ - return other.type == type && other.size == size; -} - -QSizeF QMacStylePrivate::CocoaControl::defaultFrameSize() const -{ - // We need this because things like NSView.alignmentRectInsets - // or -[NSCell titleRectForBounds:] won't work unless the control - // has a reasonable frame set. IOW, it's a chicken and egg problem. - // These values are as observed in Xcode 9's Interface Builder. - - if (type == Button_PushButton) - return QSizeF(-1, pushButtonDefaultHeight[size]); - - if (type == Button_PopupButton - || type == Button_PullDown) - return QSizeF(-1, popupButtonDefaultHeight[size]); - - if (type == ComboBox) - return QSizeF(-1, comboBoxDefaultHeight[size]); - - return QSizeF(); -} - -QRectF QMacStylePrivate::CocoaControl::adjustedControlFrame(const QRectF &rect) const -{ - QRectF frameRect; - const auto frameSize = defaultFrameSize(); - if (type == QMacStylePrivate::Button_SquareButton) { - frameRect = rect.adjusted(3, 1, -3, -1) - .adjusted(focusRingWidth, focusRingWidth, -focusRingWidth, -focusRingWidth); - } else if (type == QMacStylePrivate::Button_PushButton) { - // Start from the style option's top-left corner. - frameRect = QRectF(rect.topLeft(), - QSizeF(rect.width(), frameSize.height())); - if (size == QStyleHelper::SizeSmall) - frameRect = frameRect.translated(0, 1.5); - else if (size == QStyleHelper::SizeMini) - frameRect = frameRect.adjusted(0, 0, -8, 0).translated(4, 4); - } else { - // Center in the style option's rect. - frameRect = QRectF(QPointF(0, (rect.height() - frameSize.height()) / 2.0), - QSizeF(rect.width(), frameSize.height())); - frameRect = frameRect.translated(rect.topLeft()); - if (type == QMacStylePrivate::Button_PullDown || type == QMacStylePrivate::Button_PopupButton) { - if (size == QStyleHelper::SizeLarge) - frameRect = frameRect.adjusted(0, 0, -6, 0).translated(3, 0); - else if (size == QStyleHelper::SizeSmall) - frameRect = frameRect.adjusted(0, 0, -4, 0).translated(2, 1); - else if (size == QStyleHelper::SizeMini) - frameRect = frameRect.adjusted(0, 0, -9, 0).translated(5, 0); - } else if (type == QMacStylePrivate::ComboBox) { - frameRect = frameRect.adjusted(0, 0, -6, 0).translated(4, 0); - } - } - - return frameRect; -} - -QMarginsF QMacStylePrivate::CocoaControl::titleMargins() const -{ - if (type == QMacStylePrivate::Button_PushButton) { - if (size == QStyleHelper::SizeLarge) - return QMarginsF(12, 5, 12, 9); - if (size == QStyleHelper::SizeSmall) - return QMarginsF(12, 4, 12, 9); - if (size == QStyleHelper::SizeMini) - return QMarginsF(10, 1, 10, 2); - } - - if (type == QMacStylePrivate::Button_PullDown) { - if (size == QStyleHelper::SizeLarge) - return QMarginsF(7.5, 2.5, 22.5, 5.5); - if (size == QStyleHelper::SizeSmall) - return QMarginsF(7.5, 2, 20.5, 4); - if (size == QStyleHelper::SizeMini) - return QMarginsF(4.5, 0, 16.5, 2); - } - - if (type == QMacStylePrivate::Button_SquareButton) - return QMarginsF(6, 1, 6, 2); - - return QMarginsF(); -} - -bool QMacStylePrivate::CocoaControl::getCocoaButtonTypeAndBezelStyle(NSButtonType *buttonType, NSBezelStyle *bezelStyle) const -{ - switch (type) { - case Button_CheckBox: - *buttonType = NSSwitchButton; - *bezelStyle = NSRegularSquareBezelStyle; - break; - case Button_Disclosure: - *buttonType = NSOnOffButton; - *bezelStyle = NSDisclosureBezelStyle; - break; - case Button_RadioButton: - *buttonType = NSRadioButton; - *bezelStyle = NSRegularSquareBezelStyle; - break; - case Button_SquareButton: - *buttonType = NSPushOnPushOffButton; - *bezelStyle = NSShadowlessSquareBezelStyle; - break; - case Button_PushButton: - *buttonType = NSPushOnPushOffButton; - *bezelStyle = NSRoundedBezelStyle; - break; - default: - return false; - } - - return true; -} - -QMacStylePrivate::CocoaControlType cocoaControlType(const QStyleOption *opt, const QWidget *w) -{ - if (const auto *btn = qstyleoption_cast(opt)) { - const bool hasMenu = btn->features & QStyleOptionButton::HasMenu; - // When the contents won't fit in a large sized button, - // and WA_MacNormalSize is not set, make the button square. - // Threshold used to be at 34, not 32. - const auto maxNonSquareHeight = pushButtonDefaultHeight[QStyleHelper::SizeLarge]; - const bool isSquare = (btn->features & QStyleOptionButton::Flat) - || (btn->rect.height() > maxNonSquareHeight - && !(w && w->testAttribute(Qt::WA_MacNormalSize))); - return (isSquare? QMacStylePrivate::Button_SquareButton : - hasMenu ? QMacStylePrivate::Button_PullDown : - QMacStylePrivate::Button_PushButton); - } - - if (const auto *combo = qstyleoption_cast(opt)) { - if (combo->editable) - return QMacStylePrivate::ComboBox; - // TODO Me may support square, non-editable combo boxes, but not more than that - return QMacStylePrivate::Button_PopupButton; - } - - return QMacStylePrivate::NoControl; -} - -/** - Carbon draws comboboxes (and other views) outside the rect given as argument. Use this function to obtain - the corresponding inner rect for drawing the same combobox so that it stays inside the given outerBounds. -*/ -CGRect QMacStylePrivate::comboboxInnerBounds(const CGRect &outerBounds, const CocoaControl &cocoaWidget) -{ - CGRect innerBounds = outerBounds; - // Carbon draw parts of the view outside the rect. - // So make the rect a bit smaller to compensate - // (I wish HIThemeGetButtonBackgroundBounds worked) - if (cocoaWidget.type == Button_PopupButton) { - switch (cocoaWidget.size) { - case QStyleHelper::SizeSmall: - innerBounds.origin.x += 3; - innerBounds.origin.y += 3; - innerBounds.size.width -= 6; - innerBounds.size.height -= 7; - break; - case QStyleHelper::SizeMini: - innerBounds.origin.x += 2; - innerBounds.origin.y += 2; - innerBounds.size.width -= 5; - innerBounds.size.height -= 6; - break; - case QStyleHelper::SizeLarge: - case QStyleHelper::SizeDefault: - innerBounds.origin.x += 2; - innerBounds.origin.y += 2; - innerBounds.size.width -= 5; - innerBounds.size.height -= 6; - } - } else if (cocoaWidget.type == ComboBox) { - switch (cocoaWidget.size) { - case QStyleHelper::SizeSmall: - innerBounds.origin.x += 3; - innerBounds.origin.y += 3; - innerBounds.size.width -= 7; - innerBounds.size.height -= 8; - break; - case QStyleHelper::SizeMini: - innerBounds.origin.x += 3; - innerBounds.origin.y += 3; - innerBounds.size.width -= 4; - innerBounds.size.height -= 8; - break; - case QStyleHelper::SizeLarge: - case QStyleHelper::SizeDefault: - innerBounds.origin.x += 3; - innerBounds.origin.y += 2; - innerBounds.size.width -= 6; - innerBounds.size.height -= 8; - } - } - - return innerBounds; -} - -/** - Inside a combobox Qt places a line edit widget. The size of this widget should depend on the kind - of combobox we choose to draw. This function calculates and returns this size. -*/ -QRectF QMacStylePrivate::comboboxEditBounds(const QRectF &outerBounds, const CocoaControl &cw) -{ - QRectF ret = outerBounds; - if (cw.type == ComboBox) { - switch (cw.size) { - case QStyleHelper::SizeLarge: - ret = ret.adjusted(0, 0, -28, 0).translated(3, 4.5); - ret.setHeight(16); - break; - case QStyleHelper::SizeSmall: - ret = ret.adjusted(0, 0, -24, 0).translated(3, 2); - ret.setHeight(14); - break; - case QStyleHelper::SizeMini: - ret = ret.adjusted(0, 0, -21, 0).translated(2, 3); - ret.setHeight(11); - break; - default: - break; - } - } else if (cw.type == Button_PopupButton) { - switch (cw.size) { - case QStyleHelper::SizeLarge: - ret.adjust(10, 1, -23, -4); - break; - case QStyleHelper::SizeSmall: - ret.adjust(10, 4, -20, -3); - break; - case QStyleHelper::SizeMini: - ret.adjust(9, 0, -19, 0); - ret.setHeight(13); - break; - default: - break; - } - } - return ret; -} - -QMacStylePrivate::QMacStylePrivate() - : backingStoreNSView(nil) -{ - if (auto *ssf = QGuiApplicationPrivate::platformTheme()->font(QPlatformTheme::SmallFont)) - smallSystemFont = *ssf; - if (auto *msf = QGuiApplicationPrivate::platformTheme()->font(QPlatformTheme::MiniFont)) - miniSystemFont = *msf; -} - -QMacStylePrivate::~QMacStylePrivate() -{ - QMacAutoReleasePool pool; - for (NSView *b : cocoaControls) - [b release]; - for (NSCell *cell : cocoaCells) - [cell release]; -} - -NSView *QMacStylePrivate::cocoaControl(CocoaControl widget) const -{ - if (widget.type == QMacStylePrivate::NoControl - || widget.size == QStyleHelper::SizeDefault) - return nil; - - if (widget.type == Box) { - if (__builtin_available(macOS 10.14, *)) { - if (qt_mac_applicationIsInDarkMode()) { - // See render code in drawPrimitive(PE_FrameTabWidget) - widget.type = Box_Dark; - } - } - } - - NSView *bv = cocoaControls.value(widget, nil); - if (!bv) { - switch (widget.type) { - case Box: { - NSBox *box = [[NSBox alloc] init]; - bv = box; - box.title = @""; - box.titlePosition = NSNoTitle; - break; - } - case Box_Dark: - bv = [[QDarkNSBox alloc] init]; - break; - case Button_CheckBox: - case Button_Disclosure: - case Button_PushButton: - case Button_RadioButton: - case Button_SquareButton: { - NSButton *bc = [[NSButton alloc] init]; - bc.title = @""; - // See below for style and bezel setting. - bv = bc; - break; - } - case Button_PopupButton: - case Button_PullDown: { - NSPopUpButton *bc = [[NSPopUpButton alloc] init]; - bc.title = @""; - if (widget.type == Button_PullDown) - bc.pullsDown = YES; - bv = bc; - break; - } - case Button_WindowClose: - case Button_WindowMiniaturize: - case Button_WindowZoom: { - const NSWindowButton button = [=] { - switch (widget.type) { - case Button_WindowClose: - return NSWindowCloseButton; - case Button_WindowMiniaturize: - return NSWindowMiniaturizeButton; - case Button_WindowZoom: - return NSWindowZoomButton; - default: - break; - } - Q_UNREACHABLE(); - } (); - const auto styleMask = NSWindowStyleMaskTitled - | NSWindowStyleMaskClosable - | NSWindowStyleMaskMiniaturizable - | NSWindowStyleMaskResizable; - bv = [NSWindow standardWindowButton:button forStyleMask:styleMask]; - [bv retain]; - break; - } - case ComboBox: - bv = [[NSComboBox alloc] init]; - break; - case ProgressIndicator_Determinate: - bv = [[NSProgressIndicator alloc] init]; - break; - case ProgressIndicator_Indeterminate: - bv = [[QIndeterminateProgressIndicator alloc] init]; - break; - case Scroller_Horizontal: - bv = [[NSScroller alloc] initWithFrame:NSMakeRect(0, 0, 200, 20)]; - break; - case Scroller_Vertical: - // Cocoa sets the orientation from the view's frame - // at construction time, and it cannot be changed later. - bv = [[NSScroller alloc] initWithFrame:NSMakeRect(0, 0, 20, 200)]; - break; - case Slider_Horizontal: - bv = [[NSSlider alloc] initWithFrame:NSMakeRect(0, 0, 200, 20)]; - break; - case Slider_Vertical: - // Cocoa sets the orientation from the view's frame - // at construction time, and it cannot be changed later. - bv = [[NSSlider alloc] initWithFrame:NSMakeRect(0, 0, 20, 200)]; - break; - case SplitView_Horizontal: - bv = [[NSSplitView alloc] init]; - break; - case SplitView_Vertical: - bv = [[QVerticalSplitView alloc] init]; - break; - case TextField: - bv = [[NSTextField alloc] init]; - break; - default: - break; - } - - if ([bv isKindOfClass:[NSControl class]]) { - auto *ctrl = static_cast(bv); - switch (widget.size) { - case QStyleHelper::SizeSmall: - ctrl.controlSize = NSControlSizeSmall; - break; - case QStyleHelper::SizeMini: - ctrl.controlSize = NSControlSizeMini; - break; - default: - break; - } - } else if (widget.type == ProgressIndicator_Determinate || - widget.type == ProgressIndicator_Indeterminate) { - auto *pi = static_cast(bv); - pi.indeterminate = (widget.type == ProgressIndicator_Indeterminate); - switch (widget.size) { - case QStyleHelper::SizeSmall: - pi.controlSize = NSControlSizeSmall; - break; - case QStyleHelper::SizeMini: - pi.controlSize = NSControlSizeMini; - break; - default: - break; - } - } - - cocoaControls.insert(widget, bv); - } - - NSButtonType buttonType; - NSBezelStyle bezelStyle; - if (widget.getCocoaButtonTypeAndBezelStyle(&buttonType, &bezelStyle)) { - // FIXME We need to reset the button's type and - // bezel style properties, even when cached. - auto *button = static_cast(bv); - button.buttonType = buttonType; - button.bezelStyle = bezelStyle; - if (widget.type == Button_CheckBox) - button.allowsMixedState = YES; - } - - return bv; -} - -NSCell *QMacStylePrivate::cocoaCell(CocoaControl widget) const -{ - NSCell *cell = cocoaCells[widget]; - if (!cell) { - switch (widget.type) { - case Stepper: - cell = [[NSStepperCell alloc] init]; - break; - case Button_Disclosure: { - NSButtonCell *bc = [[NSButtonCell alloc] init]; - bc.buttonType = NSOnOffButton; - bc.bezelStyle = NSDisclosureBezelStyle; - cell = bc; - break; - } - default: - break; - } - - switch (widget.size) { - case QStyleHelper::SizeSmall: - cell.controlSize = NSControlSizeSmall; - break; - case QStyleHelper::SizeMini: - cell.controlSize = NSControlSizeMini; - break; - default: - break; - } - - cocoaCells.insert(widget, cell); - } - - return cell; -} - -void QMacStylePrivate::drawNSViewInRect(NSView *view, const QRectF &rect, QPainter *p, - __attribute__((noescape)) DrawRectBlock drawRectBlock) const -{ - QMacCGContext ctx(p); - setupNSGraphicsContext(ctx, YES); - - // FIXME: The rect that we get in is relative to the widget that we're drawing - // style on behalf of, and doesn't take into account the offset of that widget - // to the widget that owns the backingstore, which we are placing the native - // view into below. This means most of the views are placed in the upper left - // corner of backingStoreNSView, which does not map to where the actual widget - // is, and which may cause problems such as triggering a setNeedsDisplay of the - // backingStoreNSView for the wrong rect. We work around this by making the view - // layer-backed, which prevents triggering display of the backingStoreNSView, but - // but there may be other issues lurking here due to the wrong position. QTBUG-68023 - view.wantsLayer = YES; - - // FIXME: We are also setting the frame of the incoming view a lot at the call - // sites of this function, making it unclear who's actually responsible for - // maintaining the size and position of the view. In theory the call sites - // should ensure the _size_ of the view is correct, and then let this code - // take care of _positioning_ the view at the right place inside backingStoreNSView. - // For now we pass on the rect as is, to prevent any regressions until this - // can be investigated properly. - view.frame = rect.toCGRect(); - - [backingStoreNSView addSubview:view]; - - // FIXME: Based on the code below, this method isn't drawing an NSView into - // a rect, it's drawing _part of the NSView_, defined by the incoming clip - // or dirty rect, into the current graphics context. We're doing some manual - // translations at the call sites that would indicate that this relationship - // is a bit fuzzy. - const CGRect dirtyRect = rect.toCGRect(); - - if (drawRectBlock) - drawRectBlock(ctx, dirtyRect); - else - [view drawRect:dirtyRect]; - - [view removeFromSuperviewWithoutNeedingDisplay]; - - restoreNSGraphicsContext(ctx); -} - -void QMacStylePrivate::resolveCurrentNSView(QWindow *window) const -{ - backingStoreNSView = window ? (NSView *)window->winId() : nil; -} - -QMacStyle::QMacStyle() - : QCommonStyle(*new QMacStylePrivate) -{ - Q_D(QMacStyle); - QMacAutoReleasePool pool; - - d->receiver = [[NotificationReceiver alloc] init]; - [[NSNotificationCenter defaultCenter] addObserver:d->receiver - selector:@selector(scrollBarStyleDidChange:) - name:NSPreferredScrollerStyleDidChangeNotification - object:nil]; -} - -QMacStyle::~QMacStyle() -{ - Q_D(QMacStyle); - QMacAutoReleasePool pool; - - [[NSNotificationCenter defaultCenter] removeObserver:d->receiver]; - [d->receiver release]; -} - -void QMacStyle::polish(QPalette &) -{ -} - -void QMacStyle::polish(QApplication *) -{ -} - -void QMacStyle::unpolish(QApplication *) -{ -} - -void QMacStyle::polish(QWidget* w) -{ - if (false -#if QT_CONFIG(menu) - || qobject_cast(w) -# if QT_CONFIG(combobox) - || qobject_cast(w) -# endif -#endif -#if QT_CONFIG(mdiarea) - || qobject_cast(w) -#endif - ) { - w->setAttribute(Qt::WA_TranslucentBackground, true); - w->setAutoFillBackground(false); - } - -#if QT_CONFIG(tabbar) - if (QTabBar *tb = qobject_cast(w)) { - if (tb->documentMode()) { - w->setAttribute(Qt::WA_Hover); - w->setFont(qt_app_fonts_hash()->value("QSmallFont", QFont())); - QPalette p = w->palette(); - p.setColor(QPalette::WindowText, QColor(17, 17, 17)); - w->setPalette(p); - w->setAttribute(Qt::WA_SetPalette, false); - w->setAttribute(Qt::WA_SetFont, false); - } - } -#endif - - QCommonStyle::polish(w); - - if (QRubberBand *rubber = qobject_cast(w)) { - rubber->setWindowOpacity(0.25); - rubber->setAttribute(Qt::WA_PaintOnScreen, false); - rubber->setAttribute(Qt::WA_NoSystemBackground, false); - } - - if (qobject_cast(w)) { - w->setAttribute(Qt::WA_OpaquePaintEvent, false); - w->setAttribute(Qt::WA_Hover, true); - w->setMouseTracking(true); - } -} - -void QMacStyle::unpolish(QWidget* w) -{ - if ( -#if QT_CONFIG(menu) - qobject_cast(w) && -#endif - !w->testAttribute(Qt::WA_SetPalette)) { - QPalette pal = qApp->palette(w); - w->setPalette(pal); - w->setAttribute(Qt::WA_SetPalette, false); - w->setWindowOpacity(1.0); - } - -#if QT_CONFIG(combobox) - if (QComboBox *combo = qobject_cast(w)) { - if (!combo->isEditable()) { - if (QWidget *widget = combo->findChild()) - widget->setWindowOpacity(1.0); - } - } -#endif - -#if QT_CONFIG(tabbar) - if (qobject_cast(w)) { - if (!w->testAttribute(Qt::WA_SetFont)) - w->setFont(qApp->font(w)); - if (!w->testAttribute(Qt::WA_SetPalette)) - w->setPalette(qApp->palette(w)); - } -#endif - - if (QRubberBand *rubber = qobject_cast(w)) { - rubber->setWindowOpacity(1.0); - rubber->setAttribute(Qt::WA_PaintOnScreen, true); - rubber->setAttribute(Qt::WA_NoSystemBackground, true); - } - - if (QFocusFrame *frame = qobject_cast(w)) - frame->setAttribute(Qt::WA_NoSystemBackground, true); - - QCommonStyle::unpolish(w); - - if (qobject_cast(w)) { - w->setAttribute(Qt::WA_OpaquePaintEvent, true); - w->setAttribute(Qt::WA_Hover, false); - w->setMouseTracking(false); - } -} - -int QMacStyle::pixelMetric(PixelMetric metric, const QStyleOption *opt, const QWidget *widget) const -{ - Q_D(const QMacStyle); - const int controlSize = getControlSize(opt, widget); - int ret = 0; - - switch (metric) { - case PM_TabCloseIndicatorWidth: - case PM_TabCloseIndicatorHeight: - ret = closeButtonSize; - break; - case PM_ToolBarIconSize: - ret = proxy()->pixelMetric(PM_LargeIconSize); - break; - case PM_FocusFrameVMargin: - case PM_FocusFrameHMargin: - ret = qt_mac_aqua_get_metric(FocusRectOutset); - break; - case PM_DialogButtonsSeparator: - ret = -5; - break; - case PM_DialogButtonsButtonHeight: { - QSize sz; - ret = d->aquaSizeConstrain(opt, 0, QStyle::CT_PushButton, QSize(-1, -1), &sz); - if (sz == QSize(-1, -1)) - ret = 32; - else - ret = sz.height(); - break; } - case PM_DialogButtonsButtonWidth: { - QSize sz; - ret = d->aquaSizeConstrain(opt, 0, QStyle::CT_PushButton, QSize(-1, -1), &sz); - if (sz == QSize(-1, -1)) - ret = 70; - else - ret = sz.width(); - break; } - - case PM_MenuBarHMargin: - ret = 8; - break; - - case PM_MenuBarVMargin: - ret = 0; - break; - - case PM_MenuBarPanelWidth: - ret = 0; - break; - - case PM_MenuButtonIndicator: - ret = toolButtonArrowSize; - break; - - case QStyle::PM_MenuDesktopFrameWidth: - ret = 5; - break; - - case PM_CheckBoxLabelSpacing: - case PM_RadioButtonLabelSpacing: - ret = [=] { - if (opt) { - if (opt->state & State_Mini) - return 4; - if (opt->state & State_Small) - return 3; - } - return 2; - } (); - break; - case PM_MenuScrollerHeight: - ret = 15; // I hate having magic numbers in here... - break; - case PM_DefaultFrameWidth: -#if QT_CONFIG(mainwindow) - if (widget && (widget->isWindow() || !widget->parentWidget() - || (qobject_cast(widget->parentWidget()) - && static_cast(widget->parentWidget())->centralWidget() == widget)) - && qobject_cast(widget)) - ret = 0; - else -#endif - // The combo box popup has no frame. - if (qstyleoption_cast(opt) != 0) - ret = 0; - else - ret = 1; - break; - case PM_MaximumDragDistance: - ret = -1; - break; - case PM_ScrollBarSliderMin: - ret = 24; - break; - case PM_SpinBoxFrameWidth: - ret = qt_mac_aqua_get_metric(EditTextFrameOutset); - break; - case PM_ButtonShiftHorizontal: - case PM_ButtonShiftVertical: - ret = 0; - break; - case PM_SliderLength: - ret = 17; - break; - // Returns the number of pixels to use for the business part of the - // slider (i.e., the non-tickmark portion). The remaining space is shared - // equally between the tickmark regions. - case PM_SliderControlThickness: - if (const QStyleOptionSlider *sl = qstyleoption_cast(opt)) { - int space = (sl->orientation == Qt::Horizontal) ? sl->rect.height() : sl->rect.width(); - int ticks = sl->tickPosition; - int n = 0; - if (ticks & QSlider::TicksAbove) - ++n; - if (ticks & QSlider::TicksBelow) - ++n; - if (!n) { - ret = space; - break; - } - - int thick = 6; // Magic constant to get 5 + 16 + 5 - if (ticks != QSlider::TicksBothSides && ticks != QSlider::NoTicks) - thick += proxy()->pixelMetric(PM_SliderLength, sl, widget) / 4; - - space -= thick; - if (space > 0) - thick += (space * 2) / (n + 2); - ret = thick; - } else { - ret = 0; - } - break; - case PM_SmallIconSize: - ret = int(QStyleHelper::dpiScaled(16.)); - break; - - case PM_LargeIconSize: - ret = int(QStyleHelper::dpiScaled(32.)); - break; - - case PM_IconViewIconSize: - ret = proxy()->pixelMetric(PM_LargeIconSize, opt, widget); - break; - - case PM_ButtonDefaultIndicator: - ret = 0; - break; - case PM_TitleBarHeight: { - NSUInteger style = NSWindowStyleMaskTitled; - if (widget && ((widget->windowFlags() & Qt::Tool) == Qt::Tool)) - style |= NSWindowStyleMaskUtilityWindow; - ret = int([NSWindow frameRectForContentRect:NSZeroRect - styleMask:style].size.height); - break; } - case QStyle::PM_TabBarTabHSpace: - switch (d->aquaSizeConstrain(opt, widget)) { - case QStyleHelper::SizeLarge: - ret = QCommonStyle::pixelMetric(metric, opt, widget); - break; - case QStyleHelper::SizeSmall: - ret = 20; - break; - case QStyleHelper::SizeMini: - ret = 16; - break; - case QStyleHelper::SizeDefault: - const QStyleOptionTab *tb = qstyleoption_cast(opt); - if (tb && tb->documentMode) - ret = 30; - else - ret = QCommonStyle::pixelMetric(metric, opt, widget); - break; - } - break; - case PM_TabBarTabVSpace: - ret = 4; - break; - case PM_TabBarTabShiftHorizontal: - case PM_TabBarTabShiftVertical: - ret = 0; - break; - case PM_TabBarBaseHeight: - ret = 0; - break; - case PM_TabBarTabOverlap: - ret = 1; - break; - case PM_TabBarBaseOverlap: - switch (d->aquaSizeConstrain(opt, widget)) { - case QStyleHelper::SizeDefault: - case QStyleHelper::SizeLarge: - ret = 11; - break; - case QStyleHelper::SizeSmall: - ret = 8; - break; - case QStyleHelper::SizeMini: - ret = 7; - break; - } - break; - case PM_ScrollBarExtent: { - const QStyleHelper::WidgetSizePolicy size = d->effectiveAquaSizeConstrain(opt, widget); - ret = static_cast([NSScroller - scrollerWidthForControlSize:static_cast(size) - scrollerStyle:[NSScroller preferredScrollerStyle]]); - break; } - case PM_IndicatorHeight: { - switch (d->aquaSizeConstrain(opt, widget)) { - case QStyleHelper::SizeDefault: - case QStyleHelper::SizeLarge: - ret = qt_mac_aqua_get_metric(CheckBoxHeight); - break; - case QStyleHelper::SizeMini: - ret = qt_mac_aqua_get_metric(MiniCheckBoxHeight); - break; - case QStyleHelper::SizeSmall: - ret = qt_mac_aqua_get_metric(SmallCheckBoxHeight); - break; - } - break; } - case PM_IndicatorWidth: { - switch (d->aquaSizeConstrain(opt, widget)) { - case QStyleHelper::SizeDefault: - case QStyleHelper::SizeLarge: - ret = qt_mac_aqua_get_metric(CheckBoxWidth); - break; - case QStyleHelper::SizeMini: - ret = qt_mac_aqua_get_metric(MiniCheckBoxWidth); - break; - case QStyleHelper::SizeSmall: - ret = qt_mac_aqua_get_metric(SmallCheckBoxWidth); - break; - } - ++ret; - break; } - case PM_ExclusiveIndicatorHeight: { - switch (d->aquaSizeConstrain(opt, widget)) { - case QStyleHelper::SizeDefault: - case QStyleHelper::SizeLarge: - ret = qt_mac_aqua_get_metric(RadioButtonHeight); - break; - case QStyleHelper::SizeMini: - ret = qt_mac_aqua_get_metric(MiniRadioButtonHeight); - break; - case QStyleHelper::SizeSmall: - ret = qt_mac_aqua_get_metric(SmallRadioButtonHeight); - break; - } - break; } - case PM_ExclusiveIndicatorWidth: { - switch (d->aquaSizeConstrain(opt, widget)) { - case QStyleHelper::SizeDefault: - case QStyleHelper::SizeLarge: - ret = qt_mac_aqua_get_metric(RadioButtonWidth); - break; - case QStyleHelper::SizeMini: - ret = qt_mac_aqua_get_metric(MiniRadioButtonWidth); - break; - case QStyleHelper::SizeSmall: - ret = qt_mac_aqua_get_metric(SmallRadioButtonWidth); - break; - } - ++ret; - break; } - case PM_MenuVMargin: - ret = 4; - break; - case PM_MenuPanelWidth: - ret = 0; - break; - case PM_ToolTipLabelFrameWidth: - ret = 0; - break; - case PM_SizeGripSize: { - QStyleHelper::WidgetSizePolicy aSize; - if (widget && widget->window()->windowType() == Qt::Tool) - aSize = QStyleHelper::SizeSmall; - else - aSize = QStyleHelper::SizeLarge; - const QSize size = qt_aqua_get_known_size(CT_SizeGrip, widget, QSize(), aSize); - ret = size.width(); - break; } - case PM_MdiSubWindowFrameWidth: - ret = 1; - break; - case PM_DockWidgetFrameWidth: - ret = 0; - break; - case PM_DockWidgetTitleMargin: - ret = 0; - break; - case PM_DockWidgetSeparatorExtent: - ret = 1; - break; - case PM_ToolBarHandleExtent: - ret = 11; - break; - case PM_ToolBarItemMargin: - ret = 0; - break; - case PM_ToolBarItemSpacing: - ret = 4; - break; - case PM_SplitterWidth: - ret = qMax(7, QApplication::globalStrut().width()); - break; - case PM_LayoutLeftMargin: - case PM_LayoutTopMargin: - case PM_LayoutRightMargin: - case PM_LayoutBottomMargin: - { - bool isWindow = false; - if (opt) { - isWindow = (opt->state & State_Window); - } else if (widget) { - isWindow = widget->isWindow(); - } - - if (isWindow) { - /* - AHIG would have (20, 8, 10) here but that makes - no sense. It would also have 14 for the top margin - but this contradicts both Builder and most - applications. - */ - return_SIZE(20, 10, 10); // AHIG - } else { - // hack to detect QTabWidget - if (widget && widget->parentWidget() - && widget->parentWidget()->sizePolicy().controlType() == QSizePolicy::TabWidget) { - if (metric == PM_LayoutTopMargin) { - /* - Builder would have 14 (= 20 - 6) instead of 12, - but that makes the tab look disproportionate. - */ - return_SIZE(12, 6, 6); // guess - } else { - return_SIZE(20 /* Builder */, 8 /* guess */, 8 /* guess */); - } - } else { - /* - Child margins are highly inconsistent in AHIG and Builder. - */ - return_SIZE(12, 8, 6); // guess - } - } - } - case PM_LayoutHorizontalSpacing: - case PM_LayoutVerticalSpacing: - return -1; - case PM_MenuHMargin: - ret = 0; - break; - case PM_ToolBarExtensionExtent: - ret = 21; - break; - case PM_ToolBarFrameWidth: - ret = 1; - break; - case PM_ScrollView_ScrollBarOverlap: - ret = [NSScroller preferredScrollerStyle] == NSScrollerStyleOverlay ? - pixelMetric(PM_ScrollBarExtent, opt, widget) : 0; - break; - default: - ret = QCommonStyle::pixelMetric(metric, opt, widget); - break; - } - return ret; -} - -QPalette QMacStyle::standardPalette() const -{ - QPalette pal = QCommonStyle::standardPalette(); - pal.setColor(QPalette::Disabled, QPalette::Dark, QColor(191, 191, 191)); - pal.setColor(QPalette::Active, QPalette::Dark, QColor(191, 191, 191)); - pal.setColor(QPalette::Inactive, QPalette::Dark, QColor(191, 191, 191)); - return pal; -} - -int QMacStyle::styleHint(StyleHint sh, const QStyleOption *opt, const QWidget *w, - QStyleHintReturn *hret) const -{ - QMacAutoReleasePool pool; - - int ret = 0; - switch (sh) { - case SH_Slider_SnapToValue: - case SH_PrintDialog_RightAlignButtons: - case SH_FontDialog_SelectAssociatedText: - case SH_MenuBar_MouseTracking: - case SH_Menu_MouseTracking: - case SH_ComboBox_ListMouseTracking: - case SH_MainWindow_SpaceBelowMenuBar: - case SH_ItemView_ChangeHighlightOnFocus: - ret = 1; - break; - case SH_ToolBox_SelectedPageTitleBold: - ret = 0; - break; - case SH_DialogButtonBox_ButtonsHaveIcons: - ret = 0; - break; - case SH_Menu_SelectionWrap: - ret = false; - break; - case SH_Menu_KeyboardSearch: - ret = true; - break; - case SH_Menu_SpaceActivatesItem: - ret = true; - break; - case SH_Slider_AbsoluteSetButtons: - ret = Qt::LeftButton|Qt::MidButton; - break; - case SH_Slider_PageSetButtons: - ret = 0; - break; - case SH_ScrollBar_ContextMenu: - ret = false; - break; - case SH_TitleBar_AutoRaise: - ret = true; - break; - case SH_Menu_AllowActiveAndDisabled: - ret = false; - break; - case SH_Menu_SubMenuPopupDelay: - ret = 100; - break; - case SH_Menu_SubMenuUniDirection: - ret = true; - break; - case SH_Menu_SubMenuSloppySelectOtherActions: - ret = false; - break; - case SH_Menu_SubMenuResetWhenReenteringParent: - ret = true; - break; - case SH_Menu_SubMenuDontStartSloppyOnLeave: - ret = true; - break; - - case SH_ScrollBar_LeftClickAbsolutePosition: { - NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults]; - bool result = [defaults boolForKey:@"AppleScrollerPagingBehavior"]; - if(QApplication::keyboardModifiers() & Qt::AltModifier) - ret = !result; - else - ret = result; - break; } - case SH_TabBar_PreferNoArrows: - ret = true; - break; - /* - case SH_DialogButtons_DefaultButton: - ret = QDialogButtons::Reject; - break; - */ - case SH_GroupBox_TextLabelVerticalAlignment: - ret = Qt::AlignTop; - break; - case SH_ScrollView_FrameOnlyAroundContents: - ret = QCommonStyle::styleHint(sh, opt, w, hret); - break; - case SH_Menu_FillScreenWithScroll: - ret = false; - break; - case SH_Menu_Scrollable: - ret = true; - break; - case SH_RichText_FullWidthSelection: - ret = true; - break; - case SH_BlinkCursorWhenTextSelected: - ret = false; - break; - case SH_ScrollBar_StopMouseOverSlider: - ret = true; - break; - case SH_ListViewExpand_SelectMouseType: - ret = QEvent::MouseButtonRelease; - break; - case SH_TabBar_SelectMouseType: -#if QT_CONFIG(tabbar) - if (const QStyleOptionTabBarBase *opt2 = qstyleoption_cast(opt)) { - ret = opt2->documentMode ? QEvent::MouseButtonPress : QEvent::MouseButtonRelease; - } else -#endif - { - ret = QEvent::MouseButtonRelease; - } - break; - case SH_ComboBox_Popup: - if (const QStyleOptionComboBox *cmb = qstyleoption_cast(opt)) - ret = !cmb->editable; - else - ret = 0; - break; - case SH_Workspace_FillSpaceOnMaximize: - ret = true; - break; - case SH_Widget_ShareActivation: - ret = true; - break; - case SH_Header_ArrowAlignment: - ret = Qt::AlignRight; - break; - case SH_TabBar_Alignment: { -#if QT_CONFIG(tabwidget) - if (const QTabWidget *tab = qobject_cast(w)) { - if (tab->documentMode()) { - ret = Qt::AlignLeft; - break; - } - } -#endif -#if QT_CONFIG(tabbar) - if (const QTabBar *tab = qobject_cast(w)) { - if (tab->documentMode()) { - ret = Qt::AlignLeft; - break; - } - } -#endif - ret = Qt::AlignCenter; - } break; - case SH_UnderlineShortcut: - ret = false; - break; - case SH_ToolTipLabel_Opacity: - ret = 242; // About 95% - break; - case SH_Button_FocusPolicy: - ret = Qt::TabFocus; - break; - case SH_EtchDisabledText: - ret = false; - break; - case SH_FocusFrame_Mask: { - ret = true; - if(QStyleHintReturnMask *mask = qstyleoption_cast(hret)) { - const uchar fillR = 192, fillG = 191, fillB = 190; - QImage img; - - QSize pixmapSize = opt->rect.size(); - if (!pixmapSize.isEmpty()) { - QPixmap pix(pixmapSize); - pix.fill(QColor(fillR, fillG, fillB)); - QPainter pix_paint(&pix); - proxy()->drawControl(CE_FocusFrame, opt, &pix_paint, w); - pix_paint.end(); - img = pix.toImage(); - } - - const QRgb *sptr = (QRgb*)img.bits(), *srow; - const int sbpl = img.bytesPerLine(); - const int w = sbpl/4, h = img.height(); - - QImage img_mask(img.width(), img.height(), QImage::Format_ARGB32); - QRgb *dptr = (QRgb*)img_mask.bits(), *drow; - const int dbpl = img_mask.bytesPerLine(); - - for (int y = 0; y < h; ++y) { - srow = sptr+((y*sbpl)/4); - drow = dptr+((y*dbpl)/4); - for (int x = 0; x < w; ++x) { - const int redDiff = qRed(*srow) - fillR; - const int greenDiff = qGreen(*srow) - fillG; - const int blueDiff = qBlue(*srow) - fillB; - const int diff = (redDiff * redDiff) + (greenDiff * greenDiff) + (blueDiff * blueDiff); - (*drow++) = (diff < 10) ? 0xffffffff : 0xff000000; - ++srow; - } - } - QBitmap qmask = QBitmap::fromImage(img_mask); - mask->region = QRegion(qmask); - } - break; } - case SH_TitleBar_NoBorder: - ret = 1; - break; - case SH_RubberBand_Mask: - ret = 0; - break; - case SH_ComboBox_LayoutDirection: - ret = Qt::LeftToRight; - break; - case SH_ItemView_EllipsisLocation: - ret = Qt::AlignHCenter; - break; - case SH_ItemView_ShowDecorationSelected: - ret = true; - break; - case SH_TitleBar_ModifyNotification: - ret = false; - break; - case SH_ScrollBar_RollBetweenButtons: - ret = true; - break; - case SH_WindowFrame_Mask: - ret = false; - break; - case SH_TabBar_ElideMode: - ret = Qt::ElideRight; - break; -#if QT_CONFIG(dialogbuttonbox) - case SH_DialogButtonLayout: - ret = QDialogButtonBox::MacLayout; - break; -#endif - case SH_FormLayoutWrapPolicy: - ret = QFormLayout::DontWrapRows; - break; - case SH_FormLayoutFieldGrowthPolicy: - ret = QFormLayout::FieldsStayAtSizeHint; - break; - case SH_FormLayoutFormAlignment: - ret = Qt::AlignHCenter | Qt::AlignTop; - break; - case SH_FormLayoutLabelAlignment: - ret = Qt::AlignRight; - break; - case SH_ComboBox_PopupFrameStyle: - ret = QFrame::NoFrame; - break; - case SH_MessageBox_TextInteractionFlags: - ret = Qt::TextSelectableByMouse | Qt::LinksAccessibleByMouse | Qt::TextSelectableByKeyboard; - break; - case SH_SpellCheckUnderlineStyle: - ret = QTextCharFormat::DashUnderline; - break; - case SH_MessageBox_CenterButtons: - ret = false; - break; - case SH_MenuBar_AltKeyNavigation: - ret = false; - break; - case SH_ItemView_MovementWithoutUpdatingSelection: - ret = false; - break; - case SH_FocusFrame_AboveWidget: - ret = true; - break; -#if QT_CONFIG(wizard) - case SH_WizardStyle: - ret = QWizard::MacStyle; - break; -#endif - case SH_ItemView_ArrowKeysNavigateIntoChildren: - ret = false; - break; - case SH_Menu_FlashTriggeredItem: - ret = true; - break; - case SH_Menu_FadeOutOnHide: - ret = true; - break; - case SH_ItemView_PaintAlternatingRowColorsForEmptyArea: - ret = true; - break; -#if QT_CONFIG(tabbar) - case SH_TabBar_CloseButtonPosition: - ret = QTabBar::LeftSide; - break; -#endif - case SH_DockWidget_ButtonsHaveFrame: - ret = false; - break; - case SH_ScrollBar_Transient: - if ((qobject_cast(w) && w->parent() && - qobject_cast(w->parent()->parent())) -#ifndef QT_NO_ACCESSIBILITY - || (opt && QStyleHelper::hasAncestor(opt->styleObject, QAccessible::ScrollBar)) -#endif - ) { - ret = [NSScroller preferredScrollerStyle] == NSScrollerStyleOverlay; - } - break; - case SH_ItemView_ScrollMode: - ret = QAbstractItemView::ScrollPerPixel; - break; - case SH_TitleBar_ShowToolTipsOnButtons: - // min/max/close buttons on windows don't show tool tips - ret = false; - break; - case SH_ComboBox_AllowWheelScrolling: - ret = false; - break; - case SH_SpinBox_ButtonsInsideFrame: - ret = false; - break; - case SH_Table_GridLineColor: - ret = int(qt_mac_toQColor(NSColor.gridColor).rgb()); - break; - default: - ret = QCommonStyle::styleHint(sh, opt, w, hret); - break; - } - return ret; -} - -QPixmap QMacStyle::generatedIconPixmap(QIcon::Mode iconMode, const QPixmap &pixmap, - const QStyleOption *opt) const -{ - switch (iconMode) { - case QIcon::Disabled: { - QImage img = pixmap.toImage().convertToFormat(QImage::Format_ARGB32); - int imgh = img.height(); - int imgw = img.width(); - QRgb pixel; - for (int y = 0; y < imgh; ++y) { - for (int x = 0; x < imgw; ++x) { - pixel = img.pixel(x, y); - img.setPixel(x, y, qRgba(qRed(pixel), qGreen(pixel), qBlue(pixel), - qAlpha(pixel) / 2)); - } - } - return QPixmap::fromImage(img); - } - default: - ; - } - return QCommonStyle::generatedIconPixmap(iconMode, pixmap, opt); -} - - -QPixmap QMacStyle::standardPixmap(StandardPixmap standardPixmap, const QStyleOption *opt, - const QWidget *widget) const -{ - // The default implementation of QStyle::standardIconImplementation() is to call standardPixmap() - // I don't want infinite recursion so if we do get in that situation, just return the Window's - // standard pixmap instead (since there is no mac-specific icon then). This should be fine until - // someone changes how Windows standard - // pixmap works. - static bool recursionGuard = false; - - if (recursionGuard) - return QCommonStyle::standardPixmap(standardPixmap, opt, widget); - - recursionGuard = true; - QIcon icon = proxy()->standardIcon(standardPixmap, opt, widget); - recursionGuard = false; - int size; - switch (standardPixmap) { - default: - size = 32; - break; - case SP_MessageBoxCritical: - case SP_MessageBoxQuestion: - case SP_MessageBoxInformation: - case SP_MessageBoxWarning: - size = 64; - break; - } - return icon.pixmap(qt_getWindow(widget), QSize(size, size)); -} - -void QMacStyle::drawPrimitive(PrimitiveElement pe, const QStyleOption *opt, QPainter *p, - const QWidget *w) const -{ - Q_D(const QMacStyle); - QMacCGContext cg(p); - QWindow *window = w && w->window() ? w->window()->windowHandle() : nullptr; - d->resolveCurrentNSView(window); - switch (pe) { - case PE_IndicatorArrowUp: - case PE_IndicatorArrowDown: - case PE_IndicatorArrowRight: - case PE_IndicatorArrowLeft: { - p->save(); - p->setRenderHint(QPainter::Antialiasing); - const int xOffset = 1; // FIXME: opt->direction == Qt::LeftToRight ? 2 : -1; - qreal halfSize = 0.5 * qMin(opt->rect.width(), opt->rect.height()); - const qreal penWidth = qMax(halfSize / 3.0, 1.25); -#if QT_CONFIG(toolbutton) - if (const QToolButton *tb = qobject_cast(w)) { - // When stroking the arrow, make sure it fits in the tool button - if (tb->arrowType() != Qt::NoArrow - || tb->popupMode() == QToolButton::MenuButtonPopup) - halfSize -= penWidth; - } -#endif - - QMatrix matrix; - matrix.translate(opt->rect.center().x() + xOffset, opt->rect.center().y() + 2); - QPainterPath path; - switch(pe) { - default: - case PE_IndicatorArrowDown: - break; - case PE_IndicatorArrowUp: - matrix.rotate(180); - break; - case PE_IndicatorArrowLeft: - matrix.rotate(90); - break; - case PE_IndicatorArrowRight: - matrix.rotate(-90); - break; - } - p->setMatrix(matrix); - - path.moveTo(-halfSize, -halfSize * 0.5); - path.lineTo(0.0, halfSize * 0.5); - path.lineTo(halfSize, -halfSize * 0.5); - - const QPen arrowPen(opt->palette.text(), penWidth, - Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin); - p->strokePath(path, arrowPen); - p->restore(); - break; } -#if QT_CONFIG(tabbar) - case PE_FrameTabBarBase: - if (const QStyleOptionTabBarBase *tbb - = qstyleoption_cast(opt)) { - if (tbb->documentMode) { - p->save(); - drawTabBase(p, tbb, w); - p->restore(); - return; - } - - QRegion region(tbb->rect); - region -= tbb->tabBarRect; - p->save(); - p->setClipRegion(region); - QStyleOptionTabWidgetFrame twf; - twf.QStyleOption::operator=(*tbb); - twf.shape = tbb->shape; - switch (QMacStylePrivate::tabDirection(twf.shape)) { - case QMacStylePrivate::North: - twf.rect = twf.rect.adjusted(0, 0, 0, 10); - break; - case QMacStylePrivate::South: - twf.rect = twf.rect.adjusted(0, -10, 0, 0); - break; - case QMacStylePrivate::West: - twf.rect = twf.rect.adjusted(0, 0, 10, 0); - break; - case QMacStylePrivate::East: - twf.rect = twf.rect.adjusted(0, -10, 0, 0); - break; - } - proxy()->drawPrimitive(PE_FrameTabWidget, &twf, p, w); - p->restore(); - } - break; -#endif - case PE_PanelTipLabel: - p->fillRect(opt->rect, opt->palette.brush(QPalette::ToolTipBase)); - break; - case PE_FrameGroupBox: - if (const auto *groupBox = qstyleoption_cast(opt)) - if (groupBox->features & QStyleOptionFrame::Flat) { - QCommonStyle::drawPrimitive(pe, groupBox, p, w); - break; - } -#if QT_CONFIG(tabwidget) - Q_FALLTHROUGH(); - case PE_FrameTabWidget: -#endif - { - const auto cw = QMacStylePrivate::CocoaControl(QMacStylePrivate::Box, QStyleHelper::SizeLarge); - auto *box = static_cast(d->cocoaControl(cw)); - // FIXME Since macOS 10.14, simply calling drawRect: won't display anything anymore. - // The AppKit team is aware of this and has proposed a couple of solutions. - // The first solution was to call displayRectIgnoringOpacity:inContext: instead. - // However, it doesn't seem to work on 10.13. More importantly, dark mode on 10.14 - // is extremely slow. Light mode works fine. - // The second solution is to subclass NSBox and reimplement a trivial drawRect: which - // would only call super. This works without any issue on 10.13, but a double border - // shows on 10.14 in both light and dark modes. - // The code below picks what works on each version and mode. On 10.13 and earlier, we - // simply call drawRect: on a regular NSBox. On 10.14, we call displayRectIgnoringOpacity: - // inContext:, but only in light mode. In dark mode, we use a custom NSBox subclass, - // QDarkNSBox, of type NSBoxCustom. Its appearance is close enough to the real thing so - // we can use this for now. - auto adjustedRect = opt->rect; - bool needTranslation = false; - if (QOperatingSystemVersion::current() >= QOperatingSystemVersion::MacOSMojave - && !qt_mac_applicationIsInDarkMode()) { - // Another surprise from AppKit (SDK 10.14) - -displayRectIgnoringOpacity: - // is different from drawRect: for some Apple-known reason box is smaller - // in height than we need, resulting in tab buttons sitting too high/not - // centered. Attempts to play with insets etc did not work - the same wrong - // height. Simple translation is not working (too much space "at bottom"), - // so we make it bigger and translate (otherwise it's clipped at bottom btw). - adjustedRect.adjust(0, 0, 0, 3); - needTranslation = true; - } - d->drawNSViewInRect(box, adjustedRect, p, ^(CGContextRef ctx, const CGRect &rect) { - if (QTabWidget *tabWidget = qobject_cast(opt->styleObject)) - clipTabBarFrame(opt, this, ctx); - CGContextTranslateCTM(ctx, 0, rect.origin.y + rect.size.height); - CGContextScaleCTM(ctx, 1, -1); - if (QOperatingSystemVersion::current() < QOperatingSystemVersion::MacOSMojave - || [box isMemberOfClass:QDarkNSBox.class]) { - [box drawRect:rect]; - } else { - if (needTranslation) - CGContextTranslateCTM(ctx, 0.0, 4.0); - [box displayRectIgnoringOpacity:box.bounds inContext:NSGraphicsContext.currentContext]; - } - }); - break; - } - case PE_IndicatorToolBarSeparator: { - QPainterPath path; - if (opt->state & State_Horizontal) { - int xpoint = opt->rect.center().x(); - path.moveTo(xpoint + 0.5, opt->rect.top() + 1); - path.lineTo(xpoint + 0.5, opt->rect.bottom()); - } else { - int ypoint = opt->rect.center().y(); - path.moveTo(opt->rect.left() + 2 , ypoint + 0.5); - path.lineTo(opt->rect.right() + 1, ypoint + 0.5); - } - QPainterPathStroker theStroker; - theStroker.setCapStyle(Qt::FlatCap); - theStroker.setDashPattern(QVector() << 1 << 2); - path = theStroker.createStroke(path); - p->fillPath(path, QColor(0, 0, 0, 119)); - } - break; - case PE_FrameWindow: - if (const QStyleOptionFrame *frame = qstyleoption_cast(opt)) { - if (w && w->inherits("QMdiSubWindow")) { - p->save(); - p->setPen(QPen(frame->palette.dark().color(), frame->lineWidth)); - p->setBrush(frame->palette.window()); - p->drawRect(frame->rect); - p->restore(); - } - } - break; - case PE_IndicatorDockWidgetResizeHandle: { - // The docwidget resize handle is drawn as a one-pixel wide line. - p->save(); - if (opt->state & State_Horizontal) { - p->setPen(QColor(160, 160, 160)); - p->drawLine(opt->rect.topLeft(), opt->rect.topRight()); - } else { - p->setPen(QColor(145, 145, 145)); - p->drawLine(opt->rect.topRight(), opt->rect.bottomRight()); - } - p->restore(); - } break; - case PE_IndicatorToolBarHandle: { - p->save(); - QPainterPath path; - int x = opt->rect.x() + 6; - int y = opt->rect.y() + 7; - static const int RectHeight = 2; - if (opt->state & State_Horizontal) { - while (y < opt->rect.height() - RectHeight - 5) { - path.moveTo(x, y); - path.addEllipse(x, y, RectHeight, RectHeight); - y += 6; - } - } else { - while (x < opt->rect.width() - RectHeight - 5) { - path.moveTo(x, y); - path.addEllipse(x, y, RectHeight, RectHeight); - x += 6; - } - } - p->setPen(Qt::NoPen); - QColor dark = opt->palette.dark().color().darker(); - dark.setAlphaF(0.50); - p->fillPath(path, dark); - p->restore(); - - break; - } - case PE_IndicatorHeaderArrow: - if (const QStyleOptionHeader *header = qstyleoption_cast(opt)) { - // In HITheme, up is down, down is up and hamburgers eat people. - if (header->sortIndicator != QStyleOptionHeader::None) - proxy()->drawPrimitive( - (header->sortIndicator == QStyleOptionHeader::SortDown) ? - PE_IndicatorArrowUp : PE_IndicatorArrowDown, header, p, w); - } - break; - case PE_IndicatorMenuCheckMark: { - QColor pc; - if (opt->state & State_On) - pc = opt->palette.highlightedText().color(); - else - pc = opt->palette.text().color(); - - QCFType checkmarkColor = CGColorCreateGenericRGB(static_cast(pc.redF()), - static_cast(pc.greenF()), - static_cast(pc.blueF()), - static_cast(pc.alphaF())); - // kCTFontUIFontSystem and others give the same result - // as kCTFontUIFontMenuItemMark. However, the latter is - // more reminiscent to HITheme's kThemeMenuItemMarkFont. - // See also the font for small- and mini-sized widgets, - // where we end up using the generic system font type. - const CTFontUIFontType fontType = (opt->state & State_Mini) ? kCTFontUIFontMiniSystem : - (opt->state & State_Small) ? kCTFontUIFontSmallSystem : - kCTFontUIFontMenuItemMark; - // Similarly for the font size, where there is a small difference - // between regular combobox and item view items, and and menu items. - // However, we ignore any difference for small- and mini-sized widgets. - const CGFloat fontSize = fontType == kCTFontUIFontMenuItemMark ? opt->fontMetrics.height() : 0.0; - QCFType checkmarkFont = CTFontCreateUIFontForLanguage(fontType, fontSize, NULL); - - CGContextSaveGState(cg); - CGContextSetShouldSmoothFonts(cg, NO); // Same as HITheme and Cocoa menu checkmarks - - // Baseline alignment tweaks for QComboBox and QMenu - const CGFloat vOffset = (opt->state & State_Mini) ? 0.0 : - (opt->state & State_Small) ? 1.0 : - 0.75; - - CGContextTranslateCTM(cg, 0, opt->rect.bottom()); - CGContextScaleCTM(cg, 1, -1); - // Translate back to the original position and add rect origin and offset - CGContextTranslateCTM(cg, opt->rect.x(), vOffset); - - // CTFont has severe difficulties finding the checkmark character among its - // glyphs. Fortunately, CTLine knows its ways inside the Cocoa labyrinth. - static const CFStringRef keys[] = { kCTFontAttributeName, kCTForegroundColorAttributeName }; - static const int numValues = sizeof(keys) / sizeof(keys[0]); - const CFTypeRef values[] = { (CFTypeRef)checkmarkFont, (CFTypeRef)checkmarkColor }; - Q_STATIC_ASSERT((sizeof(values) / sizeof(values[0])) == numValues); - QCFType attributes = CFDictionaryCreate(kCFAllocatorDefault, (const void **)keys, (const void **)values, - numValues, NULL, NULL); - // U+2713: CHECK MARK - QCFType checkmarkString = CFAttributedStringCreate(kCFAllocatorDefault, (CFStringRef)@"\u2713", attributes); - QCFType line = CTLineCreateWithAttributedString(checkmarkString); - - CTLineDraw((CTLineRef)line, cg); - CGContextFlush(cg); // CTLineDraw's documentation says it doesn't flush - - CGContextRestoreGState(cg); - break; } - case PE_IndicatorViewItemCheck: - case PE_IndicatorRadioButton: - case PE_IndicatorCheckBox: { - const bool isEnabled = opt->state & State_Enabled; - const bool isPressed = opt->state & State_Sunken; - const bool isRadioButton = (pe == PE_IndicatorRadioButton); - const auto ct = isRadioButton ? QMacStylePrivate::Button_RadioButton : QMacStylePrivate::Button_CheckBox; - const auto cs = d->effectiveAquaSizeConstrain(opt, w); - const auto cw = QMacStylePrivate::CocoaControl(ct, cs); - auto *tb = static_cast(d->cocoaControl(cw)); - tb.enabled = isEnabled; - tb.state = (opt->state & State_NoChange) ? NSMixedState : - (opt->state & State_On) ? NSOnState : NSOffState; - [tb highlight:isPressed]; - const auto vOffset = [=] { - // As measured - if (cs == QStyleHelper::SizeMini) - return ct == QMacStylePrivate::Button_CheckBox ? -0.5 : 0.5; - - return cs == QStyleHelper::SizeSmall ? 0.5 : 0.0; - } (); - d->drawNSViewInRect(tb, opt->rect, p, ^(CGContextRef ctx, const CGRect &rect) { - CGContextTranslateCTM(ctx, 0, vOffset); - [tb.cell drawInteriorWithFrame:rect inView:tb]; - }); - break; } - case PE_FrameFocusRect: - // Use the our own focus widget stuff. - break; - case PE_IndicatorBranch: { - if (!(opt->state & State_Children)) - break; - const auto cw = QMacStylePrivate::CocoaControl(QMacStylePrivate::Button_Disclosure, QStyleHelper::SizeLarge); - NSButtonCell *triangleCell = static_cast(d->cocoaCell(cw)); - [triangleCell setState:(opt->state & State_Open) ? NSOnState : NSOffState]; - bool viewHasFocus = (w && w->hasFocus()) || (opt->state & State_HasFocus); - [triangleCell setBackgroundStyle:((opt->state & State_Selected) && viewHasFocus) ? NSBackgroundStyleDark : NSBackgroundStyleLight]; - - d->setupNSGraphicsContext(cg, NO); - - QRect qtRect = opt->rect.adjusted(DisclosureOffset, 0, -DisclosureOffset, 0); - CGRect rect = CGRectMake(qtRect.x() + 1, qtRect.y(), qtRect.width(), qtRect.height()); - CGContextTranslateCTM(cg, rect.origin.x, rect.origin.y + rect.size.height); - CGContextScaleCTM(cg, 1, -1); - CGContextTranslateCTM(cg, -rect.origin.x, -rect.origin.y); - - [triangleCell drawBezelWithFrame:NSRectFromCGRect(rect) inView:[triangleCell controlView]]; - - d->restoreNSGraphicsContext(cg); - break; } - - case PE_Frame: { - QPen oldPen = p->pen(); - p->setPen(opt->palette.base().color().darker(140)); - p->drawRect(opt->rect.adjusted(0, 0, -1, -1)); - p->setPen(opt->palette.base().color().darker(180)); - p->drawLine(opt->rect.topLeft(), opt->rect.topRight()); - p->setPen(oldPen); - break; } - - case PE_FrameLineEdit: - if (const QStyleOptionFrame *frame = qstyleoption_cast(opt)) { - if (frame->state & State_Sunken) { - const bool isEnabled = opt->state & State_Enabled; - const bool isReadOnly = opt->state & State_ReadOnly; - const bool isRounded = frame->features & QStyleOptionFrame::Rounded; - const auto cs = d->effectiveAquaSizeConstrain(opt, w, CT_LineEdit); - const auto cw = QMacStylePrivate::CocoaControl(QMacStylePrivate::TextField, cs); - auto *tf = static_cast(d->cocoaControl(cw)); - tf.enabled = isEnabled; - tf.editable = !isReadOnly; - tf.bezeled = YES; - static_cast(tf.cell).bezelStyle = isRounded ? NSTextFieldRoundedBezel : NSTextFieldSquareBezel; - tf.frame = opt->rect.toCGRect(); - d->drawNSViewInRect(tf, opt->rect, p, ^(CGContextRef, const CGRect &rect) { - if (!qt_mac_applicationIsInDarkMode()) { - // In 'Dark' mode controls are transparent, so we do not - // over-paint the (potentially custom) color in the background. - // In 'Light' mode we have to care about the correct - // background color. See the comments below for PE_PanelLineEdit. - CGContextRef cgContext = NSGraphicsContext.currentContext.CGContext; - // See QMacCGContext, here we expect bitmap context created with - // color space 'kCGColorSpaceSRGB', if it's something else - we - // give up. - if (cgContext ? bool(CGBitmapContextGetColorSpace(cgContext)) : false) { - tf.drawsBackground = YES; - const QColor bgColor = frame->palette.brush(QPalette::Base).color(); - tf.backgroundColor = [NSColor colorWithSRGBRed:bgColor.redF() - green:bgColor.greenF() - blue:bgColor.blueF() - alpha:bgColor.alphaF()]; - if (bgColor.alpha() != 255) { - // No way we can have it bezeled and transparent ... - tf.bordered = YES; - } - } - } - - [tf.cell drawWithFrame:rect inView:tf]; - }); - } else { - QCommonStyle::drawPrimitive(pe, opt, p, w); - } - } - break; - case PE_PanelLineEdit: - { - const QStyleOptionFrame *panel = qstyleoption_cast(opt); - if (qt_mac_applicationIsInDarkMode() || (panel && panel->lineWidth <= 0)) { - // QCommonStyle::drawPrimitive(PE_PanelLineEdit) fill the background with - // a proper color, defined in opt->palette and then, if lineWidth > 0, it - // calls QMacStyle::drawPrimitive(PE_FrameLineEdit). We use NSTextFieldCell - // to handle PE_FrameLineEdit, which will use system-default background. - // In 'Dark' mode it's transparent and thus it's not over-painted. - QCommonStyle::drawPrimitive(pe, opt, p, w); - } else { - // In 'Light' mode, if panel->lineWidth > 0, we have to use the correct - // background color when drawing PE_FrameLineEdit, so let's call it - // directly and set the proper color there. - drawPrimitive(PE_FrameLineEdit, opt, p, w); - } - - // Draw the focus frame for widgets other than QLineEdit (e.g. for line edits in Webkit). - // Focus frame is drawn outside the rectangle passed in the option-rect. - if (panel) { -#if QT_CONFIG(lineedit) - if ((opt->state & State_HasFocus) && !qobject_cast(w)) { - int vmargin = pixelMetric(QStyle::PM_FocusFrameVMargin); - int hmargin = pixelMetric(QStyle::PM_FocusFrameHMargin); - QStyleOptionFrame focusFrame = *panel; - focusFrame.rect = panel->rect.adjusted(-hmargin, -vmargin, hmargin, vmargin); - drawControl(CE_FocusFrame, &focusFrame, p, w); - } -#endif - } - } - break; - case PE_PanelScrollAreaCorner: { - const QBrush brush(opt->palette.brush(QPalette::Base)); - p->fillRect(opt->rect, brush); - p->setPen(QPen(QColor(217, 217, 217))); - p->drawLine(opt->rect.topLeft(), opt->rect.topRight()); - p->drawLine(opt->rect.topLeft(), opt->rect.bottomLeft()); - } break; - case PE_FrameStatusBarItem: - break; - case PE_IndicatorTabClose: { - // Make close button visible only on the hovered tab. - QTabBar *tabBar = qobject_cast(w->parentWidget()); - if (!tabBar) { - // QStyleSheetStyle instead of CloseButton (which has - // a QTabBar as a parent widget) uses the QTabBar itself: - tabBar = qobject_cast(const_cast(w)); - } - if (tabBar) { - const bool documentMode = tabBar->documentMode(); - const QTabBarPrivate *tabBarPrivate = static_cast(QObjectPrivate::get(tabBar)); - const int hoveredTabIndex = tabBarPrivate->hoveredTabIndex(); - if (!documentMode || - (hoveredTabIndex != -1 && ((w == tabBar->tabButton(hoveredTabIndex, QTabBar::LeftSide)) || - (w == tabBar->tabButton(hoveredTabIndex, QTabBar::RightSide))))) { - const bool hover = (opt->state & State_MouseOver); - const bool selected = (opt->state & State_Selected); - const bool pressed = (opt->state & State_Sunken); - drawTabCloseButton(p, hover, selected, pressed, documentMode); - } - } - } break; - case PE_PanelStatusBar: { - // Fill the status bar with the titlebar gradient. - QLinearGradient linearGrad; - if (w ? qt_macWindowMainWindow(w->window()) : (opt->state & QStyle::State_Active)) { - linearGrad = titlebarGradientActive(); - } else { - linearGrad = titlebarGradientInactive(); - } - - linearGrad.setStart(0, opt->rect.top()); - linearGrad.setFinalStop(0, opt->rect.bottom()); - p->fillRect(opt->rect, linearGrad); - - // Draw the black separator line at the top of the status bar. - if (w ? qt_macWindowMainWindow(w->window()) : (opt->state & QStyle::State_Active)) - p->setPen(titlebarSeparatorLineActive); - else - p->setPen(titlebarSeparatorLineInactive); - p->drawLine(opt->rect.left(), opt->rect.top(), opt->rect.right(), opt->rect.top()); - - break; - } - case PE_PanelMenu: { - p->save(); - p->fillRect(opt->rect, Qt::transparent); - p->setPen(Qt::transparent); - p->setBrush(opt->palette.window()); - p->setRenderHint(QPainter::Antialiasing, true); - const QPainterPath path = d->windowPanelPath(opt->rect); - p->drawPath(path); - p->restore(); - } break; - - default: - QCommonStyle::drawPrimitive(pe, opt, p, w); - break; - } -} - -static QPixmap darkenPixmap(const QPixmap &pixmap) -{ - QImage img = pixmap.toImage().convertToFormat(QImage::Format_ARGB32); - int imgh = img.height(); - int imgw = img.width(); - int h, s, v, a; - QRgb pixel; - for (int y = 0; y < imgh; ++y) { - for (int x = 0; x < imgw; ++x) { - pixel = img.pixel(x, y); - a = qAlpha(pixel); - QColor hsvColor(pixel); - hsvColor.getHsv(&h, &s, &v); - s = qMin(100, s * 2); - v = v / 2; - hsvColor.setHsv(h, s, v); - pixel = hsvColor.rgb(); - img.setPixel(x, y, qRgba(qRed(pixel), qGreen(pixel), qBlue(pixel), a)); - } - } - return QPixmap::fromImage(img); -} - -void QMacStylePrivate::setupVerticalInvertedXform(CGContextRef cg, bool reverse, bool vertical, const CGRect &rect) const -{ - if (vertical) { - CGContextTranslateCTM(cg, rect.size.height, 0); - CGContextRotateCTM(cg, M_PI_2); - } - if (vertical != reverse) { - CGContextTranslateCTM(cg, rect.size.width, 0); - CGContextScaleCTM(cg, -1, 1); - } -} - -void QMacStyle::drawControl(ControlElement ce, const QStyleOption *opt, QPainter *p, - const QWidget *w) const -{ - Q_D(const QMacStyle); - QMacCGContext cg(p); - QWindow *window = w && w->window() ? w->window()->windowHandle() : nullptr; - d->resolveCurrentNSView(window); - switch (ce) { - case CE_HeaderSection: - if (const QStyleOptionHeader *header = qstyleoption_cast(opt)) { - State flags = header->state; - QRect ir = header->rect; - - -#if 0 // FIXME: What's this solving exactly? - bool noVerticalHeader = true; -#if QT_CONFIG(tableview) - if (w) - if (const QTableView *table = qobject_cast(w->parentWidget())) - noVerticalHeader = !table->verticalHeader()->isVisible(); -#endif - - const bool drawLeftBorder = header->orientation == Qt::Vertical - || header->position == QStyleOptionHeader::OnlyOneSection - || (header->position == QStyleOptionHeader::Beginning && noVerticalHeader); -#endif - - const bool pressed = (flags & State_Sunken) && !(flags & State_On); - p->fillRect(ir, pressed ? header->palette.dark() : header->palette.button()); - p->setPen(QPen(header->palette.dark(), 1.0)); - if (header->orientation == Qt::Horizontal) - p->drawLine(QLineF(ir.right() + 0.5, ir.top() + headerSectionSeparatorInset, - ir.right() + 0.5, ir.bottom() - headerSectionSeparatorInset)); - else - p->drawLine(QLineF(ir.left() + headerSectionSeparatorInset, ir.bottom(), - ir.right() - headerSectionSeparatorInset, ir.bottom())); - } - - break; - case CE_HeaderLabel: - if (const QStyleOptionHeader *header = qstyleoption_cast(opt)) { - p->save(); - QRect textr = header->rect; - if (!header->icon.isNull()) { - QIcon::Mode mode = QIcon::Disabled; - if (opt->state & State_Enabled) - mode = QIcon::Normal; - int iconExtent = proxy()->pixelMetric(PM_SmallIconSize); - QPixmap pixmap = header->icon.pixmap(window, QSize(iconExtent, iconExtent), mode); - - QRect pixr = header->rect; - pixr.setY(header->rect.center().y() - (pixmap.height() / pixmap.devicePixelRatio() - 1) / 2); - proxy()->drawItemPixmap(p, pixr, Qt::AlignVCenter, pixmap); - textr.translate(pixmap.width() / pixmap.devicePixelRatio() + 2, 0); - } - - proxy()->drawItemText(p, textr, header->textAlignment | Qt::AlignVCenter, header->palette, - header->state & State_Enabled, header->text, QPalette::ButtonText); - p->restore(); - } - break; - case CE_ToolButtonLabel: - if (const QStyleOptionToolButton *tb = qstyleoption_cast(opt)) { - QStyleOptionToolButton myTb = *tb; - myTb.state &= ~State_AutoRaise; -#ifndef QT_NO_ACCESSIBILITY - if (QStyleHelper::hasAncestor(opt->styleObject, QAccessible::ToolBar)) { - QRect cr = tb->rect; - int shiftX = 0; - int shiftY = 0; - bool needText = false; - int alignment = 0; - bool down = tb->state & (State_Sunken | State_On); - if (down) { - shiftX = proxy()->pixelMetric(PM_ButtonShiftHorizontal, tb, w); - shiftY = proxy()->pixelMetric(PM_ButtonShiftVertical, tb, w); - } - // The down state is special for QToolButtons in a toolbar on the Mac - // The text is a bit bolder and gets a drop shadow and the icons are also darkened. - // This doesn't really fit into any particular case in QIcon, so we - // do the majority of the work ourselves. - if (!(tb->features & QStyleOptionToolButton::Arrow)) { - Qt::ToolButtonStyle tbstyle = tb->toolButtonStyle; - if (tb->icon.isNull() && !tb->text.isEmpty()) - tbstyle = Qt::ToolButtonTextOnly; - - switch (tbstyle) { - case Qt::ToolButtonTextOnly: { - needText = true; - alignment = Qt::AlignCenter; - break; } - case Qt::ToolButtonIconOnly: - case Qt::ToolButtonTextBesideIcon: - case Qt::ToolButtonTextUnderIcon: { - QRect pr = cr; - QIcon::Mode iconMode = (tb->state & State_Enabled) ? QIcon::Normal - : QIcon::Disabled; - QIcon::State iconState = (tb->state & State_On) ? QIcon::On - : QIcon::Off; - QPixmap pixmap = tb->icon.pixmap(window, - tb->rect.size().boundedTo(tb->iconSize), - iconMode, iconState); - - // Draw the text if it's needed. - if (tb->toolButtonStyle != Qt::ToolButtonIconOnly) { - needText = true; - if (tb->toolButtonStyle == Qt::ToolButtonTextUnderIcon) { - pr.setHeight(pixmap.size().height() / pixmap.devicePixelRatio() + 6); - cr.adjust(0, pr.bottom(), 0, -3); - alignment |= Qt::AlignCenter; - } else { - pr.setWidth(pixmap.width() / pixmap.devicePixelRatio() + 8); - cr.adjust(pr.right(), 0, 0, 0); - alignment |= Qt::AlignLeft | Qt::AlignVCenter; - } - } - if (opt->state & State_Sunken) { - pr.translate(shiftX, shiftY); - pixmap = darkenPixmap(pixmap); - } - proxy()->drawItemPixmap(p, pr, Qt::AlignCenter, pixmap); - break; } - default: - Q_ASSERT(false); - break; - } - - if (needText) { - QPalette pal = tb->palette; - QPalette::ColorRole role = QPalette::NoRole; - if (!proxy()->styleHint(SH_UnderlineShortcut, tb, w)) - alignment |= Qt::TextHideMnemonic; - if (down) - cr.translate(shiftX, shiftY); - if (tbstyle == Qt::ToolButtonTextOnly - || (tbstyle != Qt::ToolButtonTextOnly && !down)) { - QPen pen = p->pen(); - QColor light = down ? Qt::black : Qt::white; - light.setAlphaF(0.375f); - p->setPen(light); - p->drawText(cr.adjusted(0, 1, 0, 1), alignment, tb->text); - p->setPen(pen); - if (down && tbstyle == Qt::ToolButtonTextOnly) { - pal = QApplication::palette("QMenu"); - pal.setCurrentColorGroup(tb->palette.currentColorGroup()); - role = QPalette::HighlightedText; - } - } - proxy()->drawItemText(p, cr, alignment, pal, - tb->state & State_Enabled, tb->text, role); - } - } else { - QCommonStyle::drawControl(ce, &myTb, p, w); - } - } else -#endif // QT_NO_ACCESSIBILITY - { - QCommonStyle::drawControl(ce, &myTb, p, w); - } - } - break; - case CE_ToolBoxTabShape: - QCommonStyle::drawControl(ce, opt, p, w); - break; - case CE_PushButtonBevel: - if (const QStyleOptionButton *btn = qstyleoption_cast(opt)) { - if (!(btn->state & (State_Raised | State_Sunken | State_On))) - break; - - if (btn->features & QStyleOptionButton::CommandLinkButton) { - QCommonStyle::drawControl(ce, opt, p, w); - break; - } - - const bool hasFocus = btn->state & State_HasFocus; - const bool isActive = btn->state & State_Active; - - // a focused auto-default button within an active window - // takes precedence over a normal default button - if ((btn->features & QStyleOptionButton::AutoDefaultButton) - && isActive && hasFocus) - d->autoDefaultButton = btn->styleObject; - else if (d->autoDefaultButton == btn->styleObject) - d->autoDefaultButton = nullptr; - - const bool isEnabled = btn->state & State_Enabled; - const bool isPressed = btn->state & State_Sunken; - const bool isHighlighted = isActive && - ((btn->state & State_On) - || (btn->features & QStyleOptionButton::DefaultButton) - || (btn->features & QStyleOptionButton::AutoDefaultButton - && d->autoDefaultButton == btn->styleObject)); - const bool hasMenu = btn->features & QStyleOptionButton::HasMenu; - const auto ct = cocoaControlType(btn, w); - const auto cs = d->effectiveAquaSizeConstrain(btn, w); - const auto cw = QMacStylePrivate::CocoaControl(ct, cs); - auto *pb = static_cast(d->cocoaControl(cw)); - // Ensure same size and location as we used to have with HITheme. - // This is more convoluted than we initialy thought. See for example - // differences between plain and menu button frames. - const QRectF frameRect = cw.adjustedControlFrame(btn->rect); - pb.frame = frameRect.toCGRect(); - - pb.enabled = isEnabled; - [pb highlight:isPressed]; - pb.state = isHighlighted && !isPressed ? NSOnState : NSOffState; - d->drawNSViewInRect(pb, frameRect, p, ^(CGContextRef, const CGRect &r) { - [pb.cell drawBezelWithFrame:r inView:pb.superview]; - }); - [pb highlight:NO]; - - if (hasMenu && cw.type == QMacStylePrivate::Button_SquareButton) { - // Using -[NSPopuButtonCell drawWithFrame:inView:] above won't do - // it right because we don't set the text in the native button. - const int mbi = proxy()->pixelMetric(QStyle::PM_MenuButtonIndicator, btn, w); - const auto ir = frameRect.toRect(); - int arrowYOffset = 0; -#if 0 - // FIXME What's this for again? - if (!w) { - // adjustment for Qt Quick Controls - arrowYOffset -= ir.top(); - if (cw.second == QStyleHelper::SizeSmall) - arrowYOffset += 1; - } -#endif - const auto ar = visualRect(btn->direction, ir, QRect(ir.right() - mbi - 6, ir.height() / 2 - arrowYOffset, mbi, mbi)); - - QStyleOption arrowOpt = *opt; - arrowOpt.rect = ar; - proxy()->drawPrimitive(PE_IndicatorArrowDown, &arrowOpt, p, w); - } - - - if (btn->state & State_HasFocus) { - // TODO Remove and use QFocusFrame instead. - const int hMargin = proxy()->pixelMetric(QStyle::PM_FocusFrameHMargin, btn, w); - const int vMargin = proxy()->pixelMetric(QStyle::PM_FocusFrameVMargin, btn, w); - QRectF focusRect; - if (cw.type == QMacStylePrivate::Button_SquareButton) { - focusRect = frameRect; - } else { - focusRect = QRectF::fromCGRect([pb alignmentRectForFrame:pb.frame]); - if (cw.type == QMacStylePrivate::Button_PushButton) - focusRect -= pushButtonShadowMargins[cw.size]; - else if (cw.type == QMacStylePrivate::Button_PullDown) - focusRect -= pullDownButtonShadowMargins[cw.size]; - } - d->drawFocusRing(p, focusRect, hMargin, vMargin, cw); - } - } - break; - case CE_PushButtonLabel: - if (const QStyleOptionButton *b = qstyleoption_cast(opt)) { - QStyleOptionButton btn(*b); - // We really don't want the label to be drawn the same as on - // windows style if it has an icon and text, then it should be more like a - // tab. So, cheat a little here. However, if it *is* only an icon - // the windows style works great, so just use that implementation. - const bool isEnabled = btn.state & State_Enabled; - const bool hasMenu = btn.features & QStyleOptionButton::HasMenu; - const bool hasIcon = !btn.icon.isNull(); - const bool hasText = !btn.text.isEmpty(); - const bool isActive = btn.state & State_Active; - const bool isPressed = btn.state & State_Sunken; - - const auto ct = cocoaControlType(&btn, w); - - if (!hasMenu && ct != QMacStylePrivate::Button_SquareButton) { - if (isPressed - || (isActive && isEnabled - && ((btn.state & State_On) - || ((btn.features & QStyleOptionButton::DefaultButton) && !d->autoDefaultButton) - || d->autoDefaultButton == btn.styleObject))) - btn.palette.setColor(QPalette::ButtonText, Qt::white); - } - - if ((!hasIcon && !hasMenu) || (hasIcon && !hasText)) { - QCommonStyle::drawControl(ce, &btn, p, w); - } else { - QRect freeContentRect = btn.rect; - QRect textRect = itemTextRect( - btn.fontMetrics, freeContentRect, Qt::AlignCenter, isEnabled, btn.text); - if (hasMenu) { - textRect.moveTo(w ? 15 : 11, textRect.top()); // Supports Qt Quick Controls - } - // Draw the icon: - if (hasIcon) { - int contentW = textRect.width(); - if (hasMenu) - contentW += proxy()->pixelMetric(PM_MenuButtonIndicator) + 4; - QIcon::Mode mode = isEnabled ? QIcon::Normal : QIcon::Disabled; - if (mode == QIcon::Normal && btn.state & State_HasFocus) - mode = QIcon::Active; - // Decide if the icon is should be on or off: - QIcon::State state = QIcon::Off; - if (btn.state & State_On) - state = QIcon::On; - QPixmap pixmap = btn.icon.pixmap(window, btn.iconSize, mode, state); - int pixmapWidth = pixmap.width() / pixmap.devicePixelRatio(); - int pixmapHeight = pixmap.height() / pixmap.devicePixelRatio(); - contentW += pixmapWidth + QMacStylePrivate::PushButtonContentPadding; - int iconLeftOffset = freeContentRect.x() + (freeContentRect.width() - contentW) / 2; - int iconTopOffset = freeContentRect.y() + (freeContentRect.height() - pixmapHeight) / 2; - QRect iconDestRect(iconLeftOffset, iconTopOffset, pixmapWidth, pixmapHeight); - QRect visualIconDestRect = visualRect(btn.direction, freeContentRect, iconDestRect); - proxy()->drawItemPixmap(p, visualIconDestRect, Qt::AlignLeft | Qt::AlignVCenter, pixmap); - int newOffset = iconDestRect.x() + iconDestRect.width() - + QMacStylePrivate::PushButtonContentPadding - textRect.x(); - textRect.adjust(newOffset, 0, newOffset, 0); - } - // Draw the text: - if (hasText) { - textRect = visualRect(btn.direction, freeContentRect, textRect); - proxy()->drawItemText(p, textRect, Qt::AlignLeft | Qt::AlignVCenter | Qt::TextShowMnemonic, btn.palette, - isEnabled, btn.text, QPalette::ButtonText); - } - } - } - break; -#if QT_CONFIG(combobox) - case CE_ComboBoxLabel: - if (const auto *cb = qstyleoption_cast(opt)) { - auto comboCopy = *cb; - comboCopy.direction = Qt::LeftToRight; - // The rectangle will be adjusted to SC_ComboBoxEditField with comboboxEditBounds() - QCommonStyle::drawControl(CE_ComboBoxLabel, &comboCopy, p, w); - } - break; -#endif // #if QT_CONFIG(combobox) -#if QT_CONFIG(tabbar) - case CE_TabBarTabShape: - if (const auto *tabOpt = qstyleoption_cast(opt)) { - if (tabOpt->documentMode) { - p->save(); - bool isUnified = false; - if (w) { - QRect tabRect = tabOpt->rect; - QPoint windowTabStart = w->mapTo(w->window(), tabRect.topLeft()); - isUnified = isInMacUnifiedToolbarArea(w->window()->windowHandle(), windowTabStart.y()); - } - - const int tabOverlap = proxy()->pixelMetric(PM_TabBarTabOverlap, opt, w); - drawTabShape(p, tabOpt, isUnified, tabOverlap); - - p->restore(); - return; - } - - const bool isActive = tabOpt->state & State_Active; - const bool isEnabled = tabOpt->state & State_Enabled; - const bool isPressed = tabOpt->state & State_Sunken; - const bool isSelected = tabOpt->state & State_Selected; - const auto tabDirection = QMacStylePrivate::tabDirection(tabOpt->shape); - const bool verticalTabs = tabDirection == QMacStylePrivate::East - || tabDirection == QMacStylePrivate::West; - - QStyleOptionTab::TabPosition tp = tabOpt->position; - QStyleOptionTab::SelectedPosition sp = tabOpt->selectedPosition; - if (tabOpt->direction == Qt::RightToLeft && !verticalTabs) { - if (tp == QStyleOptionTab::Beginning) - tp = QStyleOptionTab::End; - else if (tp == QStyleOptionTab::End) - tp = QStyleOptionTab::Beginning; - - if (sp == QStyleOptionTab::NextIsSelected) - sp = QStyleOptionTab::PreviousIsSelected; - else if (sp == QStyleOptionTab::PreviousIsSelected) - sp = QStyleOptionTab::NextIsSelected; - } - - // Alas, NSSegmentedControl and NSSegmentedCell are letting us down. - // We're not able to draw it at will, either calling -[drawSegment: - // inFrame:withView:], -[drawRect:] or anything in between. Besides, - // there's no public API do draw the pressed state, AFAICS. We'll use - // a push NSButton instead and clip the CGContext. - // NOTE/TODO: this is not true. On 10.13 NSSegmentedControl works with - // some (black?) magic/magic dances, on 10.14 it simply works (was - // it fixed in AppKit?). But, indeed, we cannot make a tab 'pressed' - // with NSSegmentedControl (only selected), so we stay with buttons - // (mixing buttons and NSSegmentedControl for such a simple thing - // is too much work). - - const auto cs = d->effectiveAquaSizeConstrain(opt, w); - // Extra hacks to get the proper pressed appreance when not selected or selected and inactive - const bool needsInactiveHack = (!isActive && isSelected); - const auto ct = !needsInactiveHack && (isSelected || tp == QStyleOptionTab::OnlyOneTab) ? - QMacStylePrivate::Button_PushButton : - QMacStylePrivate::Button_PopupButton; - const bool isPopupButton = ct == QMacStylePrivate::Button_PopupButton; - const auto cw = QMacStylePrivate::CocoaControl(ct, cs); - auto *pb = static_cast(d->cocoaControl(cw)); - - auto vOffset = isPopupButton ? 1 : 2; - if (tabDirection == QMacStylePrivate::East) - vOffset -= 1; - const auto outerAdjust = isPopupButton ? 1 : 4; - const auto innerAdjust = isPopupButton ? 20 : 10; - QRectF frameRect = tabOpt->rect; - if (verticalTabs) - frameRect = QRectF(frameRect.y(), frameRect.x(), frameRect.height(), frameRect.width()); - // Adjust before clipping - frameRect = frameRect.translated(0, vOffset); - switch (tp) { - case QStyleOptionTab::Beginning: - // Pressed state hack: tweak adjustments in preparation for flip below - if (!isSelected && tabDirection == QMacStylePrivate::West) - frameRect = frameRect.adjusted(-innerAdjust, 0, outerAdjust, 0); - else - frameRect = frameRect.adjusted(-outerAdjust, 0, innerAdjust, 0); - break; - case QStyleOptionTab::Middle: - frameRect = frameRect.adjusted(-innerAdjust, 0, innerAdjust, 0); - break; - case QStyleOptionTab::End: - // Pressed state hack: tweak adjustments in preparation for flip below - if (isSelected || tabDirection == QMacStylePrivate::West) - frameRect = frameRect.adjusted(-innerAdjust, 0, outerAdjust, 0); - else - frameRect = frameRect.adjusted(-outerAdjust, 0, innerAdjust, 0); - break; - case QStyleOptionTab::OnlyOneTab: - frameRect = frameRect.adjusted(-outerAdjust, 0, outerAdjust, 0); - break; - } - pb.frame = frameRect.toCGRect(); - - pb.enabled = isEnabled; - [pb highlight:isPressed]; - // Set off state when inactive. See needsInactiveHack for when it's selected - pb.state = (isActive && isSelected && !isPressed) ? NSOnState : NSOffState; - - const auto drawBezelBlock = ^(CGContextRef ctx, const CGRect &r) { - CGContextClipToRect(ctx, opt->rect.toCGRect()); - if (!isSelected || needsInactiveHack) { - // Final stage of the pressed state hack: flip NSPopupButton rendering - if (!verticalTabs && tp == QStyleOptionTab::End) { - CGContextTranslateCTM(ctx, opt->rect.right(), 0); - CGContextScaleCTM(ctx, -1, 1); - CGContextTranslateCTM(ctx, -frameRect.left(), 0); - } else if (tabDirection == QMacStylePrivate::West && tp == QStyleOptionTab::Beginning) { - CGContextScaleCTM(ctx, 1, -1); - CGContextTranslateCTM(ctx, 0, -frameRect.right()); - } else if (tabDirection == QMacStylePrivate::East && tp == QStyleOptionTab::End) { - CGContextTranslateCTM(ctx, 0, opt->rect.bottom()); - CGContextScaleCTM(ctx, 1, -1); - CGContextTranslateCTM(ctx, 0, -frameRect.left()); - } - } - - // Rotate and translate CTM when vertical - // On macOS: positive angle is CW, negative is CCW - if (tabDirection == QMacStylePrivate::West) { - CGContextTranslateCTM(ctx, 0, frameRect.right()); - CGContextRotateCTM(ctx, -M_PI_2); - CGContextTranslateCTM(ctx, -frameRect.left(), 0); - } else if (tabDirection == QMacStylePrivate::East) { - CGContextTranslateCTM(ctx, opt->rect.right(), 0); - CGContextRotateCTM(ctx, M_PI_2); - } - - [pb.cell drawBezelWithFrame:r inView:pb.superview]; - }; - - if (needsInactiveHack) { - // First, render tab as non-selected tab on a pixamp - const qreal pixelRatio = p->device()->devicePixelRatioF(); - QImage tabPixmap(opt->rect.size() * pixelRatio, QImage::Format_ARGB32_Premultiplied); - tabPixmap.setDevicePixelRatio(pixelRatio); - tabPixmap.fill(Qt::transparent); - QPainter tabPainter(&tabPixmap); - d->drawNSViewInRect(pb, frameRect, &tabPainter, ^(CGContextRef ctx, const CGRect &r) { - CGContextTranslateCTM(ctx, -opt->rect.left(), -opt->rect.top()); - drawBezelBlock(ctx, r); - }); - tabPainter.end(); - - // Then, darken it with the proper shade of gray - const qreal inactiveGray = 0.898; // As measured - const int inactiveGray8 = qRound(inactiveGray * 255.0); - const QRgb inactiveGrayRGB = qRgb(inactiveGray8, inactiveGray8, inactiveGray8); - for (int l = 0; l < tabPixmap.height(); ++l) { - auto *line = reinterpret_cast(tabPixmap.scanLine(l)); - for (int i = 0; i < tabPixmap.width(); ++i) { - if (qAlpha(line[i]) == 255) { - line[i] = inactiveGrayRGB; - } else if (qAlpha(line[i]) > 128) { - const int g = qRound(inactiveGray * qRed(line[i])); - line[i] = qRgba(g, g, g, qAlpha(line[i])); - } - } - } - - // Finally, draw the tab pixmap on the current painter - p->drawImage(opt->rect, tabPixmap); - } else { - d->drawNSViewInRect(pb, frameRect, p, drawBezelBlock); - } - - if (!isSelected && sp != QStyleOptionTab::NextIsSelected - && tp != QStyleOptionTab::End - && tp != QStyleOptionTab::OnlyOneTab) { - static const QPen separatorPen(Qt::black, 1.0); - p->save(); - p->setOpacity(isEnabled ? 0.105 : 0.06); // As measured - p->setPen(separatorPen); - if (tabDirection == QMacStylePrivate::West) { - p->drawLine(QLineF(opt->rect.left() + 1.5, opt->rect.bottom(), - opt->rect.right() - 0.5, opt->rect.bottom())); - } else if (tabDirection == QMacStylePrivate::East) { - p->drawLine(QLineF(opt->rect.left(), opt->rect.bottom(), - opt->rect.right() - 0.5, opt->rect.bottom())); - } else { - p->drawLine(QLineF(opt->rect.right(), opt->rect.top() + 1.0, - opt->rect.right(), opt->rect.bottom() - 0.5)); - } - p->restore(); - } - - // TODO Needs size adjustment to fit the focus ring - if (tabOpt->state & State_HasFocus) { - QMacStylePrivate::CocoaControlType focusRingType; - switch (tp) { - case QStyleOptionTab::Beginning: - focusRingType = verticalTabs ? QMacStylePrivate::SegmentedControl_Last - : QMacStylePrivate::SegmentedControl_First; - break; - case QStyleOptionTab::Middle: - focusRingType = QMacStylePrivate::SegmentedControl_Middle; - break; - case QStyleOptionTab::End: - focusRingType = verticalTabs ? QMacStylePrivate::SegmentedControl_First - : QMacStylePrivate::SegmentedControl_Last; - break; - case QStyleOptionTab::OnlyOneTab: - focusRingType = QMacStylePrivate::SegmentedControl_Single; - break; - } - } - } - break; - case CE_TabBarTabLabel: - if (const auto *tab = qstyleoption_cast(opt)) { - QStyleOptionTab myTab = *tab; - const auto tabDirection = QMacStylePrivate::tabDirection(tab->shape); - const bool verticalTabs = tabDirection == QMacStylePrivate::East - || tabDirection == QMacStylePrivate::West; - - // Check to see if we use have the same as the system font - // (QComboMenuItem is internal and should never be seen by the - // outside world, unless they read the source, in which case, it's - // their own fault). - const bool nonDefaultFont = p->font() != qt_app_fonts_hash()->value("QComboMenuItem"); - - if (!myTab.documentMode && (myTab.state & State_Selected) && (myTab.state & State_Active)) - if (const auto *tabBar = qobject_cast(w)) - if (!tabBar->tabTextColor(tabBar->currentIndex()).isValid()) - myTab.palette.setColor(QPalette::WindowText, Qt::white); - - int heightOffset = 0; - if (verticalTabs) { - heightOffset = -1; - } else if (nonDefaultFont) { - if (p->fontMetrics().height() == myTab.rect.height()) - heightOffset = 2; - } - myTab.rect.setHeight(myTab.rect.height() + heightOffset); - - QCommonStyle::drawControl(ce, &myTab, p, w); - } - break; -#endif -#if QT_CONFIG(dockwidget) - case CE_DockWidgetTitle: - if (const auto *dwOpt = qstyleoption_cast(opt)) { - const bool isVertical = dwOpt->verticalTitleBar; - const auto effectiveRect = isVertical ? opt->rect.transposed() : opt->rect; - p->save(); - if (isVertical) { - p->translate(effectiveRect.left(), effectiveRect.top() + effectiveRect.width()); - p->rotate(-90); - p->translate(-effectiveRect.left(), -effectiveRect.top()); - } - - // fill title bar background - QLinearGradient linearGrad; - linearGrad.setStart(QPointF(0, 0)); - linearGrad.setFinalStop(QPointF(0, 2 * effectiveRect.height())); - linearGrad.setColorAt(0, opt->palette.button().color()); - linearGrad.setColorAt(1, opt->palette.dark().color()); - p->fillRect(effectiveRect, linearGrad); - - // draw horizontal line at bottom - p->setPen(opt->palette.dark().color()); - p->drawLine(effectiveRect.bottomLeft(), effectiveRect.bottomRight()); - - if (!dwOpt->title.isEmpty()) { - auto titleRect = proxy()->subElementRect(SE_DockWidgetTitleBarText, opt, w); - if (isVertical) - titleRect = QRect(effectiveRect.left() + opt->rect.bottom() - titleRect.bottom(), - effectiveRect.top() + titleRect.left() - opt->rect.left(), - titleRect.height(), - titleRect.width()); - - const auto text = p->fontMetrics().elidedText(dwOpt->title, Qt::ElideRight, titleRect.width()); - proxy()->drawItemText(p, titleRect, Qt::AlignCenter | Qt::TextShowMnemonic, dwOpt->palette, - dwOpt->state & State_Enabled, text, QPalette::WindowText); - } - p->restore(); - } - break; -#endif - case CE_FocusFrame: { - const auto *ff = qobject_cast(w); - const auto *ffw = ff ? ff->widget() : nullptr; - const auto ct = [=] { - if (ffw) { - if (ffw->inherits("QCheckBox")) - return QMacStylePrivate::Button_CheckBox; - if (ffw->inherits("QRadioButton")) - return QMacStylePrivate::Button_RadioButton; - if (ffw->inherits("QLineEdit") || ffw->inherits("QTextEdit")) - return QMacStylePrivate::TextField; - } - - return QMacStylePrivate::Box; // Not really, just make it the default - } (); - const auto cs = ffw ? (ffw->testAttribute(Qt::WA_MacMiniSize) ? QStyleHelper::SizeMini : - ffw->testAttribute(Qt::WA_MacSmallSize) ? QStyleHelper::SizeSmall : - QStyleHelper::SizeLarge) : - QStyleHelper::SizeLarge; - const int hMargin = proxy()->pixelMetric(QStyle::PM_FocusFrameHMargin, opt, w); - const int vMargin = proxy()->pixelMetric(QStyle::PM_FocusFrameVMargin, opt, w); - d->drawFocusRing(p, opt->rect, hMargin, vMargin, QMacStylePrivate::CocoaControl(ct, cs)); - break; } - case CE_MenuEmptyArea: - // Skip: PE_PanelMenu fills in everything - break; - case CE_MenuItem: - case CE_MenuHMargin: - case CE_MenuVMargin: - case CE_MenuTearoff: - case CE_MenuScroller: - if (const QStyleOptionMenuItem *mi = qstyleoption_cast(opt)) { - const bool active = mi->state & State_Selected; - if (active) - p->fillRect(mi->rect, mi->palette.highlight()); - - const QStyleHelper::WidgetSizePolicy widgetSize = d->aquaSizeConstrain(opt, w); - - if (ce == CE_MenuTearoff) { - p->setPen(QPen(mi->palette.dark().color(), 1, Qt::DashLine)); - p->drawLine(mi->rect.x() + 2, mi->rect.y() + mi->rect.height() / 2 - 1, - mi->rect.x() + mi->rect.width() - 4, - mi->rect.y() + mi->rect.height() / 2 - 1); - p->setPen(QPen(mi->palette.light().color(), 1, Qt::DashLine)); - p->drawLine(mi->rect.x() + 2, mi->rect.y() + mi->rect.height() / 2, - mi->rect.x() + mi->rect.width() - 4, - mi->rect.y() + mi->rect.height() / 2); - } else if (ce == CE_MenuScroller) { - const QSize scrollerSize = QSize(10, 8); - const int scrollerVOffset = 5; - const int left = mi->rect.x() + (mi->rect.width() - scrollerSize.width()) / 2; - const int right = left + scrollerSize.width(); - int top; - int bottom; - if (opt->state & State_DownArrow) { - bottom = mi->rect.y() + scrollerVOffset; - top = bottom + scrollerSize.height(); - } else { - bottom = mi->rect.bottom() - scrollerVOffset; - top = bottom - scrollerSize.height(); - } - p->save(); - p->setRenderHint(QPainter::Antialiasing); - QPainterPath path; - path.moveTo(left, bottom); - path.lineTo(right, bottom); - path.lineTo((left + right) / 2, top); - p->fillPath(path, opt->palette.buttonText()); - p->restore(); - } else if (ce != CE_MenuItem) { - break; - } - - if (mi->menuItemType == QStyleOptionMenuItem::Separator) { - CGColorRef separatorColor = [NSColor quaternaryLabelColor].CGColor; - const QRect separatorRect = QRect(mi->rect.left(), mi->rect.center().y(), mi->rect.width(), 2); - p->fillRect(separatorRect, qt_mac_toQColor(separatorColor)); - break; - } - - const int maxpmw = mi->maxIconWidth; - const bool enabled = mi->state & State_Enabled; - - int xpos = mi->rect.x() + 18; - int checkcol = maxpmw; - if (!enabled) - p->setPen(mi->palette.text().color()); - else if (active) - p->setPen(mi->palette.highlightedText().color()); - else - p->setPen(mi->palette.buttonText().color()); - - if (mi->checked) { - QStyleOption checkmarkOpt; - checkmarkOpt.initFrom(w); - - const int mw = checkcol + macItemFrame; - const int mh = mi->rect.height() + macItemFrame; - const int xp = mi->rect.x() + macItemFrame; - checkmarkOpt.rect = QRect(xp, mi->rect.y() - checkmarkOpt.fontMetrics.descent(), mw, mh); - - checkmarkOpt.state.setFlag(State_On, active); - checkmarkOpt.state.setFlag(State_Enabled, enabled); - if (widgetSize == QStyleHelper::SizeMini) - checkmarkOpt.state |= State_Mini; - else if (widgetSize == QStyleHelper::SizeSmall) - checkmarkOpt.state |= State_Small; - - // We let drawPrimitive(PE_IndicatorMenuCheckMark) pick the right color - checkmarkOpt.palette.setColor(QPalette::HighlightedText, p->pen().color()); - checkmarkOpt.palette.setColor(QPalette::Text, p->pen().color()); - - proxy()->drawPrimitive(PE_IndicatorMenuCheckMark, &checkmarkOpt, p, w); - } - if (!mi->icon.isNull()) { - QIcon::Mode mode = (mi->state & State_Enabled) ? QIcon::Normal - : QIcon::Disabled; - // Always be normal or disabled to follow the Mac style. - int smallIconSize = proxy()->pixelMetric(PM_SmallIconSize); - QSize iconSize(smallIconSize, smallIconSize); -#if QT_CONFIG(combobox) - if (const QComboBox *comboBox = qobject_cast(w)) { - iconSize = comboBox->iconSize(); - } -#endif - QPixmap pixmap = mi->icon.pixmap(window, iconSize, mode); - int pixw = pixmap.width() / pixmap.devicePixelRatio(); - int pixh = pixmap.height() / pixmap.devicePixelRatio(); - QRect cr(xpos, mi->rect.y(), checkcol, mi->rect.height()); - QRect pmr(0, 0, pixw, pixh); - pmr.moveCenter(cr.center()); - p->drawPixmap(pmr.topLeft(), pixmap); - xpos += pixw + 6; - } - - QString s = mi->text; - const auto text_flags = Qt::AlignVCenter | Qt::TextHideMnemonic - | Qt::TextSingleLine | Qt::AlignAbsolute; - int yPos = mi->rect.y(); - if (widgetSize == QStyleHelper::SizeMini) - yPos += 1; - - const bool isSubMenu = mi->menuItemType == QStyleOptionMenuItem::SubMenu; - const int tabwidth = isSubMenu ? 9 : mi->tabWidth; - - QString rightMarginText; - if (isSubMenu) - rightMarginText = QStringLiteral("\u25b6\ufe0e"); // U+25B6 U+FE0E: BLACK RIGHT-POINTING TRIANGLE - - // If present, save and remove embedded shorcut from text - const int tabIndex = s.indexOf(QLatin1Char('\t')); - if (tabIndex >= 0) { - if (!isSubMenu) // ... but ignore it if it's a submenu. - rightMarginText = s.mid(tabIndex + 1); - s = s.left(tabIndex); - } - - p->save(); - if (!rightMarginText.isEmpty()) { - p->setFont(qt_app_fonts_hash()->value("QMenuItem", p->font())); - int xp = mi->rect.right() - tabwidth - macRightBorder + 2; - if (!isSubMenu) - xp -= macItemHMargin + macItemFrame + 3; // Adjust for shortcut - p->drawText(xp, yPos, tabwidth, mi->rect.height(), text_flags | Qt::AlignRight, rightMarginText); - } - - if (!s.isEmpty()) { - const int xm = macItemFrame + maxpmw + macItemHMargin; - QFont myFont = mi->font; - // myFont may not have any "hard" flags set. We override - // the point size so that when it is resolved against the device, this font will win. - // This is mainly to handle cases where someone sets the font on the window - // and then the combo inherits it and passes it onward. At that point the resolve mask - // is very, very weak. This makes it stonger. - myFont.setPointSizeF(QFontInfo(mi->font).pointSizeF()); - - // QTBUG-65653: Our own text rendering doesn't look good enough, especially on non-retina - // displays. Worked around here while waiting for a proper fix in QCoreTextFontEngine. - // Only if we're not using QCoreTextFontEngine we do fallback to our own text rendering. - const auto *fontEngine = QFontPrivate::get(myFont)->engineForScript(QChar::Script_Common); - Q_ASSERT(fontEngine); - if (fontEngine->type() == QFontEngine::Multi) { - fontEngine = static_cast(fontEngine)->engine(0); - Q_ASSERT(fontEngine); - } - if (fontEngine->type() == QFontEngine::Mac) { - NSFont *f = (NSFont *)(CTFontRef)fontEngine->handle(); - - // Respect the menu item palette as set in the style option. - const auto pc = p->pen().color(); - NSColor *c = [NSColor colorWithSRGBRed:pc.redF() - green:pc.greenF() - blue:pc.blueF() - alpha:pc.alphaF()]; - - s = qt_mac_removeMnemonics(s); - const auto textRect = CGRectMake(xpos, yPos, mi->rect.width() - xm - tabwidth + 1, mi->rect.height()); - - QMacCGContext cgCtx(p); - d->setupNSGraphicsContext(cgCtx, YES); - - [s.toNSString() drawInRect:textRect - withAttributes:@{ NSFontAttributeName:f, NSForegroundColorAttributeName:c }]; - - d->restoreNSGraphicsContext(cgCtx); - } else { - p->setFont(myFont); - p->drawText(xpos, yPos, mi->rect.width() - xm - tabwidth + 1, - mi->rect.height(), text_flags, s); - } - } - p->restore(); - } - break; - case CE_MenuBarItem: - case CE_MenuBarEmptyArea: - if (const QStyleOptionMenuItem *mi = qstyleoption_cast(opt)) { - const bool selected = (opt->state & State_Selected) && (opt->state & State_Enabled) && (opt->state & State_Sunken); - const QBrush bg = selected ? mi->palette.highlight() : mi->palette.window(); - p->fillRect(mi->rect, bg); - - if (ce != CE_MenuBarItem) - break; - - if (!mi->icon.isNull()) { - int iconExtent = proxy()->pixelMetric(PM_SmallIconSize); - drawItemPixmap(p, mi->rect, - Qt::AlignCenter | Qt::TextHideMnemonic | Qt::TextDontClip - | Qt::TextSingleLine, - mi->icon.pixmap(window, QSize(iconExtent, iconExtent), - (mi->state & State_Enabled) ? QIcon::Normal : QIcon::Disabled)); - } else { - drawItemText(p, mi->rect, - Qt::AlignCenter | Qt::TextHideMnemonic | Qt::TextDontClip - | Qt::TextSingleLine, - mi->palette, mi->state & State_Enabled, - mi->text, selected ? QPalette::HighlightedText : QPalette::ButtonText); - } - } - break; - case CE_ProgressBarLabel: - case CE_ProgressBarGroove: - // Do nothing. All done in CE_ProgressBarContents. Only keep these for proxy style overrides. - break; - case CE_ProgressBarContents: - if (const QStyleOptionProgressBar *pb = qstyleoption_cast(opt)) { - QMacAutoReleasePool pool; - const bool isIndeterminate = (pb->minimum == 0 && pb->maximum == 0); - const bool vertical = pb->orientation == Qt::Vertical; - const bool inverted = pb->invertedAppearance; - bool reverse = (!vertical && (pb->direction == Qt::RightToLeft)); - if (inverted) - reverse = !reverse; - - QRect rect = pb->rect; - if (vertical) - rect = rect.transposed(); - const CGRect cgRect = rect.toCGRect(); - - const auto aquaSize = d->effectiveAquaSizeConstrain(opt, w); - const QProgressStyleAnimation *animation = qobject_cast(d->animation(opt->styleObject)); - QIndeterminateProgressIndicator *ipi = nil; - if (isIndeterminate || animation) - ipi = static_cast(d->cocoaControl({ QMacStylePrivate::ProgressIndicator_Indeterminate, aquaSize })); - if (isIndeterminate) { - // QIndeterminateProgressIndicator derives from NSProgressIndicator. We use a single - // instance that we start animating as soon as one of the progress bars is indeterminate. - // Since they will be in sync (as it's the case in Cocoa), we just need to draw it with - // the right geometry when the animation triggers an update. However, we can't hide it - // entirely between frames since that would stop the animation, so we just set its alpha - // value to 0. Same if we remove it from its superview. See QIndeterminateProgressIndicator - // implementation for details. - if (!animation && opt->styleObject) { - auto *animation = new QProgressStyleAnimation(d->animateSpeed(QMacStylePrivate::AquaProgressBar), opt->styleObject); - // NSProgressIndicator is heavier to draw than the HITheme API, so we reduce the frame rate a couple notches. - animation->setFrameRate(QStyleAnimation::FifteenFps); - d->startAnimation(animation); - [ipi startAnimation]; - } - - d->setupNSGraphicsContext(cg, NO); - d->setupVerticalInvertedXform(cg, reverse, vertical, cgRect); - [ipi drawWithFrame:cgRect inView:d->backingStoreNSView]; - d->restoreNSGraphicsContext(cg); - } else { - if (animation) { - d->stopAnimation(opt->styleObject); - [ipi stopAnimation]; - } - - const auto cw = QMacStylePrivate::CocoaControl(QMacStylePrivate::ProgressIndicator_Determinate, aquaSize); - auto *pi = static_cast(d->cocoaControl(cw)); - d->drawNSViewInRect(pi, rect, p, ^(CGContextRef ctx, const CGRect &rect) { - d->setupVerticalInvertedXform(ctx, reverse, vertical, rect); - pi.minValue = pb->minimum; - pi.maxValue = pb->maximum; - pi.doubleValue = pb->progress; - [pi drawRect:rect]; - }); - } - } - break; - case CE_SizeGrip: { - // This is not HIG kosher: Fall back to the old stuff until we decide what to do. -#ifndef QT_NO_MDIAREA - if (!w || !qobject_cast(w->parentWidget())) -#endif - break; - - if (w->testAttribute(Qt::WA_MacOpaqueSizeGrip)) - p->fillRect(opt->rect, opt->palette.window()); - - QPen lineColor = QColor(82, 82, 82, 192); - lineColor.setWidth(1); - p->save(); - p->setRenderHint(QPainter::Antialiasing); - p->setPen(lineColor); - const Qt::LayoutDirection layoutDirection = w ? w->layoutDirection() : qApp->layoutDirection(); - const int NumLines = 3; - for (int l = 0; l < NumLines; ++l) { - const int offset = (l * 4 + 3); - QPoint start, end; - if (layoutDirection == Qt::LeftToRight) { - start = QPoint(opt->rect.width() - offset, opt->rect.height() - 1); - end = QPoint(opt->rect.width() - 1, opt->rect.height() - offset); - } else { - start = QPoint(offset, opt->rect.height() - 1); - end = QPoint(1, opt->rect.height() - offset); - } - p->drawLine(start, end); - } - p->restore(); - break; - } - case CE_Splitter: - if (opt->rect.width() > 1 && opt->rect.height() > 1) { - const bool isVertical = !(opt->state & QStyle::State_Horizontal); - // Qt refers to the layout orientation, while Cocoa refers to the divider's. - const auto ct = isVertical ? QMacStylePrivate::SplitView_Horizontal : QMacStylePrivate::SplitView_Vertical; - const auto cw = QMacStylePrivate::CocoaControl(ct, QStyleHelper::SizeLarge); - auto *sv = static_cast(d->cocoaControl(cw)); - sv.frame = opt->rect.toCGRect(); - d->drawNSViewInRect(sv, opt->rect, p, ^(CGContextRef, const CGRect &rect) { - [sv drawDividerInRect:rect]; - }); - } else { - QPen oldPen = p->pen(); - p->setPen(opt->palette.dark().color()); - if (opt->state & QStyle::State_Horizontal) - p->drawLine(opt->rect.topLeft(), opt->rect.bottomLeft()); - else - p->drawLine(opt->rect.topLeft(), opt->rect.topRight()); - p->setPen(oldPen); - } - break; - case CE_RubberBand: - if (const QStyleOptionRubberBand *rubber = qstyleoption_cast(opt)) { - QColor fillColor(opt->palette.color(QPalette::Disabled, QPalette::Highlight)); - if (!rubber->opaque) { - QColor strokeColor; - // I retrieved these colors from the Carbon-Dev mailing list - strokeColor.setHsvF(0, 0, 0.86, 1.0); - fillColor.setHsvF(0, 0, 0.53, 0.25); - if (opt->rect.width() * opt->rect.height() <= 3) { - p->fillRect(opt->rect, strokeColor); - } else { - QPen oldPen = p->pen(); - QBrush oldBrush = p->brush(); - QPen pen(strokeColor); - p->setPen(pen); - p->setBrush(fillColor); - QRect adjusted = opt->rect.adjusted(1, 1, -1, -1); - if (adjusted.isValid()) - p->drawRect(adjusted); - p->setPen(oldPen); - p->setBrush(oldBrush); - } - } else { - p->fillRect(opt->rect, fillColor); - } - } - break; -#ifndef QT_NO_TOOLBAR - case CE_ToolBar: { - const QStyleOptionToolBar *toolBar = qstyleoption_cast(opt); - const bool isDarkMode = qt_mac_applicationIsInDarkMode(); - - // Unified title and toolbar drawing. In this mode the cocoa platform plugin will - // fill the top toolbar area part with a background gradient that "unifies" with - // the title bar. The following code fills the toolBar area with transparent pixels - // to make that gradient visible. - if (w) { -#if QT_CONFIG(mainwindow) - if (QMainWindow * mainWindow = qobject_cast(w->window())) { - if (toolBar && toolBar->toolBarArea == Qt::TopToolBarArea && mainWindow->unifiedTitleAndToolBarOnMac()) { - // fill with transparent pixels. - p->save(); - p->setCompositionMode(QPainter::CompositionMode_Source); - p->fillRect(opt->rect, Qt::transparent); - p->restore(); - - // Draw a horizontal separator line at the toolBar bottom if the "unified" area ends here. - // There might be additional toolbars or other widgets such as tab bars in document - // mode below. Determine this by making a unified toolbar area test for the row below - // this toolbar. - const QPoint windowToolbarEnd = w->mapTo(w->window(), opt->rect.bottomLeft()); - const bool isEndOfUnifiedArea = !isInMacUnifiedToolbarArea(w->window()->windowHandle(), windowToolbarEnd.y() + 1); - if (isEndOfUnifiedArea) { - const int margin = qt_mac_aqua_get_metric(SeparatorSize); - const auto separatorRect = QRect(opt->rect.left(), opt->rect.bottom(), opt->rect.width(), margin); - p->fillRect(separatorRect, isDarkMode ? darkModeSeparatorLine : opt->palette.dark().color()); - } - break; - } - } -#endif - } - - // draw background gradient - QLinearGradient linearGrad; - if (opt->state & State_Horizontal) - linearGrad = QLinearGradient(0, opt->rect.top(), 0, opt->rect.bottom()); - else - linearGrad = QLinearGradient(opt->rect.left(), 0, opt->rect.right(), 0); - - QColor mainWindowGradientBegin = isDarkMode ? darkMainWindowGradientBegin : lightMainWindowGradientBegin; - QColor mainWindowGradientEnd = isDarkMode ? darkMainWindowGradientEnd : lightMainWindowGradientEnd; - - linearGrad.setColorAt(0, mainWindowGradientBegin); - linearGrad.setColorAt(1, mainWindowGradientEnd); - p->fillRect(opt->rect, linearGrad); - - p->save(); - if (opt->state & State_Horizontal) { - p->setPen(isDarkMode ? darkModeSeparatorLine : mainWindowGradientBegin.lighter(114)); - p->drawLine(opt->rect.topLeft(), opt->rect.topRight()); - p->setPen(isDarkMode ? darkModeSeparatorLine :mainWindowGradientEnd.darker(114)); - p->drawLine(opt->rect.bottomLeft(), opt->rect.bottomRight()); - } else { - p->setPen(isDarkMode ? darkModeSeparatorLine : mainWindowGradientBegin.lighter(114)); - p->drawLine(opt->rect.topLeft(), opt->rect.bottomLeft()); - p->setPen(isDarkMode ? darkModeSeparatorLine : mainWindowGradientEnd.darker(114)); - p->drawLine(opt->rect.topRight(), opt->rect.bottomRight()); - } - p->restore(); - - - } break; -#endif - default: - QCommonStyle::drawControl(ce, opt, p, w); - break; - } -} - -static void setLayoutItemMargins(int left, int top, int right, int bottom, QRect *rect, Qt::LayoutDirection dir) -{ - if (dir == Qt::RightToLeft) { - rect->adjust(-right, top, -left, bottom); - } else { - rect->adjust(left, top, right, bottom); - } -} - -QRect QMacStyle::subElementRect(SubElement sr, const QStyleOption *opt, - const QWidget *widget) const -{ - Q_D(const QMacStyle); - QRect rect; - const int controlSize = getControlSize(opt, widget); - - switch (sr) { - case SE_ItemViewItemText: - if (const QStyleOptionViewItem *vopt = qstyleoption_cast(opt)) { - int fw = proxy()->pixelMetric(PM_FocusFrameHMargin, opt, widget); - // We add the focusframeargin between icon and text in commonstyle - rect = QCommonStyle::subElementRect(sr, opt, widget); - if (vopt->features & QStyleOptionViewItem::HasDecoration) - rect.adjust(-fw, 0, 0, 0); - } - break; - case SE_ToolBoxTabContents: - rect = QCommonStyle::subElementRect(sr, opt, widget); - break; - case SE_PushButtonContents: - if (const QStyleOptionButton *btn = qstyleoption_cast(opt)) { - // Comment from the old HITheme days: - // "Unlike Carbon, we want the button to always be drawn inside its bounds. - // Therefore, the button is a bit smaller, so that even if it got focus, - // the focus 'shadow' will be inside. Adjust the content rect likewise." - // In the future, we should consider using -[NSCell titleRectForBounds:]. - // Since it requires configuring the NSButton fully, i.e. frame, image, - // title and font, we keep things more manual until we are more familiar - // with side effects when changing NSButton state. - const auto ct = cocoaControlType(btn, widget); - const auto cs = d->effectiveAquaSizeConstrain(btn, widget); - const auto cw = QMacStylePrivate::CocoaControl(ct, cs); - const auto frameRect = cw.adjustedControlFrame(btn->rect); - const auto titleMargins = cw.titleMargins(); - rect = (frameRect - titleMargins).toRect(); - } - break; - case SE_HeaderLabel: { - int margin = proxy()->pixelMetric(QStyle::PM_HeaderMargin, opt, widget); - rect.setRect(opt->rect.x() + margin, opt->rect.y(), - opt->rect.width() - margin * 2, opt->rect.height() - 2); - if (const QStyleOptionHeader *header = qstyleoption_cast(opt)) { - // Subtract width needed for arrow, if there is one - if (header->sortIndicator != QStyleOptionHeader::None) { - if (opt->state & State_Horizontal) - rect.setWidth(rect.width() - (headerSectionArrowHeight) - (margin * 2)); - else - rect.setHeight(rect.height() - (headerSectionArrowHeight) - (margin * 2)); - } - } - rect = visualRect(opt->direction, opt->rect, rect); - break; - } - case SE_HeaderArrow: { - int h = opt->rect.height(); - int w = opt->rect.width(); - int x = opt->rect.x(); - int y = opt->rect.y(); - int margin = proxy()->pixelMetric(QStyle::PM_HeaderMargin, opt, widget); - - if (opt->state & State_Horizontal) { - rect.setRect(x + w - margin * 2 - headerSectionArrowHeight, y + 5, - headerSectionArrowHeight, h - margin * 2 - 5); - } else { - rect.setRect(x + 5, y + h - margin * 2 - headerSectionArrowHeight, - w - margin * 2 - 5, headerSectionArrowHeight); - } - rect = visualRect(opt->direction, opt->rect, rect); - break; - } - case SE_ProgressBarGroove: - // Wrong in the secondary dimension, but accurate enough in the main dimension. - rect = opt->rect; - break; - case SE_ProgressBarLabel: - break; - case SE_ProgressBarContents: - rect = opt->rect; - break; - case SE_TreeViewDisclosureItem: { - rect = opt->rect; - // As previously returned by HIThemeGetButtonContentBounds - rect.setLeft(rect.left() + 2 + DisclosureOffset); - break; - } -#if QT_CONFIG(tabwidget) - case SE_TabWidgetLeftCorner: - if (const QStyleOptionTabWidgetFrame *twf - = qstyleoption_cast(opt)) { - switch (twf->shape) { - case QTabBar::RoundedNorth: - case QTabBar::TriangularNorth: - rect = QRect(QPoint(0, 0), twf->leftCornerWidgetSize); - break; - case QTabBar::RoundedSouth: - case QTabBar::TriangularSouth: - rect = QRect(QPoint(0, twf->rect.height() - twf->leftCornerWidgetSize.height()), - twf->leftCornerWidgetSize); - break; - default: - break; - } - rect = visualRect(twf->direction, twf->rect, rect); - } - break; - case SE_TabWidgetRightCorner: - if (const QStyleOptionTabWidgetFrame *twf - = qstyleoption_cast(opt)) { - switch (twf->shape) { - case QTabBar::RoundedNorth: - case QTabBar::TriangularNorth: - rect = QRect(QPoint(twf->rect.width() - twf->rightCornerWidgetSize.width(), 0), - twf->rightCornerWidgetSize); - break; - case QTabBar::RoundedSouth: - case QTabBar::TriangularSouth: - rect = QRect(QPoint(twf->rect.width() - twf->rightCornerWidgetSize.width(), - twf->rect.height() - twf->rightCornerWidgetSize.height()), - twf->rightCornerWidgetSize); - break; - default: - break; - } - rect = visualRect(twf->direction, twf->rect, rect); - } - break; - case SE_TabWidgetTabContents: - rect = QCommonStyle::subElementRect(sr, opt, widget); - if (const auto *twf = qstyleoption_cast(opt)) { - if (twf->lineWidth != 0) { - switch (QMacStylePrivate::tabDirection(twf->shape)) { - case QMacStylePrivate::North: - rect.adjust(+1, +14, -1, -1); - break; - case QMacStylePrivate::South: - rect.adjust(+1, +1, -1, -14); - break; - case QMacStylePrivate::West: - rect.adjust(+14, +1, -1, -1); - break; - case QMacStylePrivate::East: - rect.adjust(+1, +1, -14, -1); - } - } - } - break; - case SE_TabBarTabText: - if (const QStyleOptionTab *tab = qstyleoption_cast(opt)) { - QRect dummyIconRect; - d->tabLayout(tab, widget, &rect, &dummyIconRect); - } - break; - case SE_TabBarTabLeftButton: - case SE_TabBarTabRightButton: - if (const QStyleOptionTab *tab = qstyleoption_cast(opt)) { - bool selected = tab->state & State_Selected; - int verticalShift = proxy()->pixelMetric(QStyle::PM_TabBarTabShiftVertical, tab, widget); - int horizontalShift = proxy()->pixelMetric(QStyle::PM_TabBarTabShiftHorizontal, tab, widget); - int hpadding = 5; - - bool verticalTabs = tab->shape == QTabBar::RoundedEast - || tab->shape == QTabBar::RoundedWest - || tab->shape == QTabBar::TriangularEast - || tab->shape == QTabBar::TriangularWest; - - QRect tr = tab->rect; - if (tab->shape == QTabBar::RoundedSouth || tab->shape == QTabBar::TriangularSouth) - verticalShift = -verticalShift; - if (verticalTabs) { - qSwap(horizontalShift, verticalShift); - horizontalShift *= -1; - verticalShift *= -1; - } - if (tab->shape == QTabBar::RoundedWest || tab->shape == QTabBar::TriangularWest) - horizontalShift = -horizontalShift; - - tr.adjust(0, 0, horizontalShift, verticalShift); - if (selected) - { - tr.setBottom(tr.bottom() - verticalShift); - tr.setRight(tr.right() - horizontalShift); - } - - QSize size = (sr == SE_TabBarTabLeftButton) ? tab->leftButtonSize : tab->rightButtonSize; - int w = size.width(); - int h = size.height(); - int midHeight = static_cast(qCeil(float(tr.height() - h) / 2)); - int midWidth = ((tr.width() - w) / 2); - - bool atTheTop = true; - switch (tab->shape) { - case QTabBar::RoundedWest: - case QTabBar::TriangularWest: - atTheTop = (sr == SE_TabBarTabLeftButton); - break; - case QTabBar::RoundedEast: - case QTabBar::TriangularEast: - atTheTop = (sr == SE_TabBarTabRightButton); - break; - default: - if (sr == SE_TabBarTabLeftButton) - rect = QRect(tab->rect.x() + hpadding, midHeight, w, h); - else - rect = QRect(tab->rect.right() - w - hpadding, midHeight, w, h); - rect = visualRect(tab->direction, tab->rect, rect); - } - if (verticalTabs) { - if (atTheTop) - rect = QRect(midWidth, tr.y() + tab->rect.height() - hpadding - h, w, h); - else - rect = QRect(midWidth, tr.y() + hpadding, w, h); - } - } - break; -#endif - case SE_LineEditContents: - rect = QCommonStyle::subElementRect(sr, opt, widget); -#if QT_CONFIG(combobox) - if (widget && qobject_cast(widget->parentWidget())) - rect.adjust(-1, -2, 0, 0); - else -#endif - rect.adjust(-1, -1, 0, +1); - break; - case SE_CheckBoxLayoutItem: - rect = opt->rect; - if (controlSize == QStyleHelper::SizeLarge) { - setLayoutItemMargins(+2, +3, -9, -4, &rect, opt->direction); - } else if (controlSize == QStyleHelper::SizeSmall) { - setLayoutItemMargins(+1, +5, 0 /* fix */, -6, &rect, opt->direction); - } else { - setLayoutItemMargins(0, +7, 0 /* fix */, -6, &rect, opt->direction); - } - break; - case SE_ComboBoxLayoutItem: -#ifndef QT_NO_TOOLBAR - if (widget && qobject_cast(widget->parentWidget())) { - // Do nothing, because QToolbar needs the entire widget rect. - // Otherwise it will be clipped. Equivalent to - // widget->setAttribute(Qt::WA_LayoutUsesWidgetRect), but without - // all the hassle. - } else -#endif - { - rect = opt->rect; - if (controlSize == QStyleHelper::SizeLarge) { - rect.adjust(+3, +2, -3, -4); - } else if (controlSize == QStyleHelper::SizeSmall) { - setLayoutItemMargins(+2, +1, -3, -4, &rect, opt->direction); - } else { - setLayoutItemMargins(+1, 0, -2, 0, &rect, opt->direction); - } - } - break; - case SE_LabelLayoutItem: - rect = opt->rect; - setLayoutItemMargins(+1, 0 /* SHOULD be -1, done for alignment */, 0, 0 /* SHOULD be -1, done for alignment */, &rect, opt->direction); - break; - case SE_ProgressBarLayoutItem: { - rect = opt->rect; - int bottom = SIZE(3, 8, 8); - if (opt->state & State_Horizontal) { - rect.adjust(0, +1, 0, -bottom); - } else { - setLayoutItemMargins(+1, 0, -bottom, 0, &rect, opt->direction); - } - break; - } - case SE_PushButtonLayoutItem: - if (const QStyleOptionButton *buttonOpt - = qstyleoption_cast(opt)) { - if ((buttonOpt->features & QStyleOptionButton::Flat)) - break; // leave rect alone - } - rect = opt->rect; - if (controlSize == QStyleHelper::SizeLarge) { - rect.adjust(+6, +4, -6, -8); - } else if (controlSize == QStyleHelper::SizeSmall) { - rect.adjust(+5, +4, -5, -6); - } else { - rect.adjust(+1, 0, -1, -2); - } - break; - case SE_RadioButtonLayoutItem: - rect = opt->rect; - if (controlSize == QStyleHelper::SizeLarge) { - setLayoutItemMargins(+2, +2 /* SHOULD BE +3, done for alignment */, - 0, -4 /* SHOULD BE -3, done for alignment */, &rect, opt->direction); - } else if (controlSize == QStyleHelper::SizeSmall) { - rect.adjust(0, +6, 0 /* fix */, -5); - } else { - rect.adjust(0, +6, 0 /* fix */, -7); - } - break; - case SE_SliderLayoutItem: - if (const QStyleOptionSlider *sliderOpt - = qstyleoption_cast(opt)) { - rect = opt->rect; - if (sliderOpt->tickPosition == QSlider::NoTicks) { - int above = SIZE(3, 0, 2); - int below = SIZE(4, 3, 0); - if (sliderOpt->orientation == Qt::Horizontal) { - rect.adjust(0, +above, 0, -below); - } else { - rect.adjust(+above, 0, -below, 0); //### Seems that QSlider flip the position of the ticks in reverse mode. - } - } else if (sliderOpt->tickPosition == QSlider::TicksAbove) { - int below = SIZE(3, 2, 0); - if (sliderOpt->orientation == Qt::Horizontal) { - rect.setHeight(rect.height() - below); - } else { - rect.setWidth(rect.width() - below); - } - } else if (sliderOpt->tickPosition == QSlider::TicksBelow) { - int above = SIZE(3, 2, 0); - if (sliderOpt->orientation == Qt::Horizontal) { - rect.setTop(rect.top() + above); - } else { - rect.setLeft(rect.left() + above); - } - } - } - break; - case SE_FrameLayoutItem: - // hack because QStyleOptionFrame doesn't have a frameStyle member - if (const QFrame *frame = qobject_cast(widget)) { - rect = opt->rect; - switch (frame->frameStyle() & QFrame::Shape_Mask) { - case QFrame::HLine: - rect.adjust(0, +1, 0, -1); - break; - case QFrame::VLine: - rect.adjust(+1, 0, -1, 0); - break; - default: - ; - } - } - break; - case SE_GroupBoxLayoutItem: - rect = opt->rect; - if (const QStyleOptionGroupBox *groupBoxOpt = - qstyleoption_cast(opt)) { - /* - AHIG is very inconsistent when it comes to group boxes. - Basically, we make sure that (non-checkable) group boxes - and tab widgets look good when laid out side by side. - */ - if (groupBoxOpt->subControls & (QStyle::SC_GroupBoxCheckBox - | QStyle::SC_GroupBoxLabel)) { - int delta; - if (groupBoxOpt->subControls & QStyle::SC_GroupBoxCheckBox) { - delta = SIZE(8, 4, 4); // guess - } else { - delta = SIZE(15, 12, 12); // guess - } - rect.setTop(rect.top() + delta); - } - } - rect.setBottom(rect.bottom() - 1); - break; -#if QT_CONFIG(tabwidget) - case SE_TabWidgetLayoutItem: - if (const QStyleOptionTabWidgetFrame *tabWidgetOpt = - qstyleoption_cast(opt)) { - /* - AHIG specifies "12 or 14" as the distance from the window - edge. We choose 14 and since the default top margin is 20, - the overlap is 6. - */ - rect = tabWidgetOpt->rect; - if (tabWidgetOpt->shape == QTabBar::RoundedNorth) - rect.setTop(rect.top() + SIZE(6 /* AHIG */, 3 /* guess */, 2 /* AHIG */)); - } - break; -#endif -#if QT_CONFIG(dockwidget) - case SE_DockWidgetCloseButton: - case SE_DockWidgetFloatButton: - case SE_DockWidgetTitleBarText: - case SE_DockWidgetIcon: { - int iconSize = proxy()->pixelMetric(PM_SmallIconSize, opt, widget); - int buttonMargin = proxy()->pixelMetric(PM_DockWidgetTitleBarButtonMargin, opt, widget); - QRect srect = opt->rect; - - const QStyleOptionDockWidget *dwOpt - = qstyleoption_cast(opt); - bool canClose = dwOpt == 0 ? true : dwOpt->closable; - bool canFloat = dwOpt == 0 ? false : dwOpt->floatable; - - const bool verticalTitleBar = dwOpt->verticalTitleBar; - - // If this is a vertical titlebar, we transpose and work as if it was - // horizontal, then transpose again. - if (verticalTitleBar) - srect = srect.transposed(); - - do { - int right = srect.right(); - int left = srect.left(); - - QRect closeRect; - if (canClose) { - QSize sz = proxy()->standardIcon(QStyle::SP_TitleBarCloseButton, - opt, widget).actualSize(QSize(iconSize, iconSize)); - sz += QSize(buttonMargin, buttonMargin); - if (verticalTitleBar) - sz = sz.transposed(); - closeRect = QRect(left, - srect.center().y() - sz.height()/2, - sz.width(), sz.height()); - left = closeRect.right() + 1; - } - if (sr == SE_DockWidgetCloseButton) { - rect = closeRect; - break; - } - - QRect floatRect; - if (canFloat) { - QSize sz = proxy()->standardIcon(QStyle::SP_TitleBarNormalButton, - opt, widget).actualSize(QSize(iconSize, iconSize)); - sz += QSize(buttonMargin, buttonMargin); - if (verticalTitleBar) - sz = sz.transposed(); - floatRect = QRect(left, - srect.center().y() - sz.height()/2, - sz.width(), sz.height()); - left = floatRect.right() + 1; - } - if (sr == SE_DockWidgetFloatButton) { - rect = floatRect; - break; - } - - QRect iconRect; - if (const QDockWidget *dw = qobject_cast(widget)) { - QIcon icon; - if (dw->isFloating()) - icon = dw->windowIcon(); - if (!icon.isNull() - && icon.cacheKey() != QApplication::windowIcon().cacheKey()) { - QSize sz = icon.actualSize(QSize(rect.height(), rect.height())); - if (verticalTitleBar) - sz = sz.transposed(); - iconRect = QRect(right - sz.width(), srect.center().y() - sz.height()/2, - sz.width(), sz.height()); - right = iconRect.left() - 1; - } - } - if (sr == SE_DockWidgetIcon) { - rect = iconRect; - break; - } - - QRect textRect = QRect(left, srect.top(), - right - left, srect.height()); - if (sr == SE_DockWidgetTitleBarText) { - rect = textRect; - break; - } - } while (false); - - if (verticalTitleBar) { - rect = QRect(srect.left() + rect.top() - srect.top(), - srect.top() + srect.right() - rect.right(), - rect.height(), rect.width()); - } else { - rect = visualRect(opt->direction, srect, rect); - } - break; - } -#endif - default: - rect = QCommonStyle::subElementRect(sr, opt, widget); - break; - } - return rect; -} - -void QMacStylePrivate::drawToolbarButtonArrow(const QStyleOption *opt, QPainter *p) const -{ - Q_Q(const QMacStyle); - QStyleOption arrowOpt = *opt; - arrowOpt.rect = QRect(opt->rect.right() - (toolButtonArrowSize + toolButtonArrowMargin), - opt->rect.bottom() - (toolButtonArrowSize + toolButtonArrowMargin), - toolButtonArrowSize, - toolButtonArrowSize); - q->proxy()->drawPrimitive(QStyle::PE_IndicatorArrowDown, &arrowOpt, p); -} - -void QMacStylePrivate::setupNSGraphicsContext(CGContextRef cg, bool flipped) const -{ - CGContextSaveGState(cg); - [NSGraphicsContext saveGraphicsState]; - - [NSGraphicsContext setCurrentContext: - [NSGraphicsContext graphicsContextWithCGContext:cg flipped:flipped]]; -} - -void QMacStylePrivate::restoreNSGraphicsContext(CGContextRef cg) const -{ - [NSGraphicsContext restoreGraphicsState]; - CGContextRestoreGState(cg); -} - -void QMacStyle::drawComplexControl(ComplexControl cc, const QStyleOptionComplex *opt, QPainter *p, - const QWidget *widget) const -{ - Q_D(const QMacStyle); - QMacCGContext cg(p); - QWindow *window = widget && widget->window() ? widget->window()->windowHandle() : nullptr; - d->resolveCurrentNSView(window); - switch (cc) { - case CC_ScrollBar: - if (const QStyleOptionSlider *sb = qstyleoption_cast(opt)) { - - const bool drawTrack = sb->subControls & SC_ScrollBarGroove; - const bool drawKnob = sb->subControls & SC_ScrollBarSlider; - if (!drawTrack && !drawKnob) - break; - - const bool isHorizontal = sb->orientation == Qt::Horizontal; - - if (opt && opt->styleObject && !QMacStylePrivate::scrollBars.contains(opt->styleObject)) - QMacStylePrivate::scrollBars.append(QPointer(opt->styleObject)); - - static const CGFloat knobWidths[] = { 7.0, 5.0, 5.0 }; - static const CGFloat expandedKnobWidths[] = { 11.0, 9.0, 9.0 }; - const auto cocoaSize = d->effectiveAquaSizeConstrain(opt, widget); - const CGFloat maxExpandScale = expandedKnobWidths[cocoaSize] / knobWidths[cocoaSize]; - - const bool isTransient = proxy()->styleHint(SH_ScrollBar_Transient, opt, widget); - if (!isTransient) - d->stopAnimation(opt->styleObject); - bool wasActive = false; - CGFloat opacity = 0.0; - CGFloat expandScale = 1.0; - CGFloat expandOffset = 0.0; - bool shouldExpand = false; - - if (QObject *styleObject = opt->styleObject) { - const int oldPos = styleObject->property("_q_stylepos").toInt(); - const int oldMin = styleObject->property("_q_stylemin").toInt(); - const int oldMax = styleObject->property("_q_stylemax").toInt(); - const QRect oldRect = styleObject->property("_q_stylerect").toRect(); - const QStyle::State oldState = static_cast(styleObject->property("_q_stylestate").value()); - const uint oldActiveControls = styleObject->property("_q_stylecontrols").toUInt(); - - // a scrollbar is transient when the scrollbar itself and - // its sibling are both inactive (ie. not pressed/hovered/moved) - const bool transient = isTransient && !opt->activeSubControls && !(sb->state & State_On); - - if (!transient || - oldPos != sb->sliderPosition || - oldMin != sb->minimum || - oldMax != sb->maximum || - oldRect != sb->rect || - oldState != sb->state || - oldActiveControls != sb->activeSubControls) { - - // if the scrollbar is transient or its attributes, geometry or - // state has changed, the opacity is reset back to 100% opaque - opacity = 1.0; - - styleObject->setProperty("_q_stylepos", sb->sliderPosition); - styleObject->setProperty("_q_stylemin", sb->minimum); - styleObject->setProperty("_q_stylemax", sb->maximum); - styleObject->setProperty("_q_stylerect", sb->rect); - styleObject->setProperty("_q_stylestate", static_cast(sb->state)); - styleObject->setProperty("_q_stylecontrols", static_cast(sb->activeSubControls)); - - QScrollbarStyleAnimation *anim = qobject_cast(d->animation(styleObject)); - if (transient) { - if (!anim) { - anim = new QScrollbarStyleAnimation(QScrollbarStyleAnimation::Deactivating, styleObject); - d->startAnimation(anim); - } else if (anim->mode() == QScrollbarStyleAnimation::Deactivating) { - // the scrollbar was already fading out while the - // state changed -> restart the fade out animation - anim->setCurrentTime(0); - } - } else if (anim && anim->mode() == QScrollbarStyleAnimation::Deactivating) { - d->stopAnimation(styleObject); - } - } - - QScrollbarStyleAnimation *anim = qobject_cast(d->animation(styleObject)); - if (anim && anim->mode() == QScrollbarStyleAnimation::Deactivating) { - // once a scrollbar was active (hovered/pressed), it retains - // the active look even if it's no longer active while fading out - if (oldActiveControls) - anim->setActive(true); - - wasActive = anim->wasActive(); - opacity = anim->currentValue(); - } - - shouldExpand = isTransient && (opt->activeSubControls || wasActive); - if (shouldExpand) { - if (!anim && !oldActiveControls) { - // Start expand animation only once and when entering - anim = new QScrollbarStyleAnimation(QScrollbarStyleAnimation::Activating, styleObject); - d->startAnimation(anim); - } - if (anim && anim->mode() == QScrollbarStyleAnimation::Activating) { - expandScale = 1.0 + (maxExpandScale - 1.0) * anim->currentValue(); - expandOffset = 5.5 * (1.0 - anim->currentValue()); - } else { - // Keep expanded state after the animation ends, and when fading out - expandScale = maxExpandScale; - expandOffset = 0.0; - } - } - } - - d->setupNSGraphicsContext(cg, NO /* flipped */); - - const auto controlType = isHorizontal ? QMacStylePrivate::Scroller_Horizontal : QMacStylePrivate::Scroller_Vertical; - const auto cw = QMacStylePrivate::CocoaControl(controlType, cocoaSize); - NSScroller *scroller = static_cast(d->cocoaControl(cw)); - - const QColor bgColor = QStyleHelper::backgroundColor(opt->palette, widget); - const bool hasDarkBg = bgColor.red() < 128 && bgColor.green() < 128 && bgColor.blue() < 128; - if (isTransient) { - // macOS behavior: as soon as one color channel is >= 128, - // the background is considered bright, scroller is dark. - scroller.knobStyle = hasDarkBg? NSScrollerKnobStyleLight : NSScrollerKnobStyleDark; - } else { - scroller.knobStyle = NSScrollerKnobStyleDefault; - } - - scroller.scrollerStyle = isTransient ? NSScrollerStyleOverlay : NSScrollerStyleLegacy; - - if (!setupScroller(scroller, sb)) - break; - - if (isTransient) { - CGContextBeginTransparencyLayerWithRect(cg, scroller.frame, nullptr); - CGContextSetAlpha(cg, opacity); - } - - if (drawTrack) { - // Draw the track when hovering. Expand by shifting the track rect. - if (!isTransient || opt->activeSubControls || wasActive) { - CGRect trackRect = scroller.bounds; - if (isHorizontal) - trackRect.origin.y += expandOffset; - else - trackRect.origin.x += expandOffset; - [scroller drawKnobSlotInRect:trackRect highlight:NO]; - } - } - - if (drawKnob) { - if (shouldExpand) { - // -[NSScroller drawKnob] is not useful here because any scaling applied - // will only be used to draw the hi-DPI artwork. And even if did scale, - // the stretched knob would look wrong, actually. So we need to draw the - // scroller manually when it's being hovered. - const CGFloat scrollerWidth = [NSScroller scrollerWidthForControlSize:scroller.controlSize scrollerStyle:scroller.scrollerStyle]; - const CGFloat knobWidth = knobWidths[cocoaSize] * expandScale; - // Cocoa can help get the exact knob length in the current orientation - const CGRect scrollerKnobRect = CGRectInset([scroller rectForPart:NSScrollerKnob], 1, 1); - const CGFloat knobLength = isHorizontal ? scrollerKnobRect.size.width : scrollerKnobRect.size.height; - const CGFloat knobPos = isHorizontal ? scrollerKnobRect.origin.x : scrollerKnobRect.origin.y; - const CGFloat knobOffset = qRound((scrollerWidth + expandOffset - knobWidth) / 2.0); - const CGFloat knobRadius = knobWidth / 2.0; - CGRect knobRect; - if (isHorizontal) - knobRect = CGRectMake(knobPos, knobOffset, knobLength, knobWidth); - else - knobRect = CGRectMake(knobOffset, knobPos, knobWidth, knobLength); - QCFType knobPath = CGPathCreateWithRoundedRect(knobRect, knobRadius, knobRadius, nullptr); - CGContextAddPath(cg, knobPath); - CGContextSetAlpha(cg, 0.5); - CGColorRef knobColor = hasDarkBg ? NSColor.whiteColor.CGColor : NSColor.blackColor.CGColor; - CGContextSetFillColorWithColor(cg, knobColor); - CGContextFillPath(cg); - } else { - [scroller drawKnob]; - - if (!isTransient && opt->activeSubControls) { - // The knob should appear darker (going from 0.76 down to 0.49). - // But no blending mode can help darken enough in a single pass, - // so we resort to drawing the knob twice with a small help from - // blending. This brings the gray level to a close enough 0.53. - CGContextSetBlendMode(cg, kCGBlendModePlusDarker); - [scroller drawKnob]; - } - } - } - - if (isTransient) - CGContextEndTransparencyLayer(cg); - - d->restoreNSGraphicsContext(cg); - } - break; - case CC_Slider: - if (const QStyleOptionSlider *sl = qstyleoption_cast(opt)) { - const bool isHorizontal = sl->orientation == Qt::Horizontal; - const auto ct = isHorizontal ? QMacStylePrivate::Slider_Horizontal : QMacStylePrivate::Slider_Vertical; - const auto cs = d->effectiveAquaSizeConstrain(opt, widget); - const auto cw = QMacStylePrivate::CocoaControl(ct, cs); - auto *slider = static_cast(d->cocoaControl(cw)); - if (!setupSlider(slider, sl)) - break; - - const bool hasTicks = sl->tickPosition != QSlider::NoTicks; - const bool hasDoubleTicks = sl->tickPosition == QSlider::TicksBothSides; - const bool drawKnob = sl->subControls & SC_SliderHandle; - const bool drawBar = sl->subControls & SC_SliderGroove; - const bool drawTicks = sl->subControls & SC_SliderTickmarks; - const bool isPressed = sl->state & State_Sunken; - - CGPoint pressPoint; - if (isPressed) { - const CGRect knobRect = [slider.cell knobRectFlipped:NO]; - pressPoint.x = CGRectGetMidX(knobRect); - pressPoint.y = CGRectGetMidY(knobRect); - [slider.cell startTrackingAt:pressPoint inView:slider]; - } - - d->drawNSViewInRect(slider, opt->rect, p, ^(CGContextRef ctx, const CGRect &rect) { - if (isHorizontal && sl->upsideDown) { - CGContextTranslateCTM(ctx, rect.size.width, 0); - CGContextScaleCTM(ctx, -1, 1); - } - - if (hasDoubleTicks) { - // This ain't HIG kosher: eye-proved constants - if (isHorizontal) - CGContextTranslateCTM(ctx, 0, 4); - else - CGContextTranslateCTM(ctx, 1, 0); - } - - // Since the GC is flipped, upsideDown means *not* inverted when vertical. - const bool verticalFlip = !isHorizontal && !sl->upsideDown; // FIXME: && !isSierraOrLater - -#if 0 - // FIXME: Sadly, this part doesn't work. It seems to somehow polute the - // NSSlider's internal state and, when we need to use the "else" part, - // the slider's frame is not in sync with its cell dimensions. - const bool drawAllParts = drawKnob && drawBar && (!hasTicks || drawTicks); - if (drawAllParts && !hasDoubleTicks && (!verticalFlip || drawTicks)) { - // Draw eveything at once if we're going to, except for inverted vertical - // sliders which need to be drawn part by part because of the shadow below - // the knob. Same for two-sided tickmarks. - if (verticalFlip && drawTicks) { - // Since tickmarks are always rendered symmetrically, a vertically - // flipped slider with tickmarks only needs to get its value flipped. - slider.intValue = slider.maxValue - slider.intValue + slider.minValue; - } - [slider drawRect:CGRectZero]; - } else -#endif - { - [slider calcSize]; - NSSliderCell *cell = slider.cell; - - const int numberOfTickMarks = slider.numberOfTickMarks; - // This ain't HIG kosher: force tick-less bar position. - if (hasDoubleTicks) - slider.numberOfTickMarks = 0; - - const CGRect barRect = [cell barRectFlipped:hasTicks]; - if (drawBar) { - // This ain't HIG kosher: force unfilled bar look. - if (hasDoubleTicks) - slider.numberOfTickMarks = numberOfTickMarks; - [cell drawBarInside:barRect flipped:!verticalFlip]; - } - - if (hasTicks && drawTicks) { - if (!drawBar && hasDoubleTicks) - slider.numberOfTickMarks = numberOfTickMarks; - - [cell drawTickMarks]; - - if (hasDoubleTicks) { - // This ain't HIG kosher: just slap a set of tickmarks on each side, like we used to. - CGAffineTransform tickMarksFlip; - const CGRect tickMarkRect = [cell rectOfTickMarkAtIndex:0]; - if (isHorizontal) { - tickMarksFlip = CGAffineTransformMakeTranslation(0, rect.size.height - tickMarkRect.size.height - 3); - tickMarksFlip = CGAffineTransformScale(tickMarksFlip, 1, -1); - } else { - tickMarksFlip = CGAffineTransformMakeTranslation(rect.size.width - tickMarkRect.size.width / 2, 0); - tickMarksFlip = CGAffineTransformScale(tickMarksFlip, -1, 1); - } - CGContextConcatCTM(ctx, tickMarksFlip); - [cell drawTickMarks]; - CGContextConcatCTM(ctx, CGAffineTransformInvert(tickMarksFlip)); - } - } - - if (drawKnob) { - // This ain't HIG kosher: force round knob look. - if (hasDoubleTicks) - slider.numberOfTickMarks = 0; - // Draw the knob in the symmetrical position instead of flipping. - if (verticalFlip) - slider.intValue = slider.maxValue - slider.intValue + slider.minValue; - [cell drawKnob]; - } - } - }); - - if (isPressed) - [slider.cell stopTracking:pressPoint at:pressPoint inView:slider mouseIsUp:NO]; - } - break; -#if QT_CONFIG(spinbox) - case CC_SpinBox: - if (const QStyleOptionSpinBox *sb = qstyleoption_cast(opt)) { - if (sb->frame && (sb->subControls & SC_SpinBoxFrame)) { - const auto lineEditRect = proxy()->subControlRect(CC_SpinBox, sb, SC_SpinBoxEditField, widget); - QStyleOptionFrame frame; - static_cast(frame) = *opt; - frame.rect = lineEditRect; - frame.state |= State_Sunken; - frame.lineWidth = 1; - frame.midLineWidth = 0; - frame.features = QStyleOptionFrame::None; - frame.frameShape = QFrame::Box; - drawPrimitive(PE_FrameLineEdit, &frame, p, widget); - } - if (sb->subControls & (SC_SpinBoxUp | SC_SpinBoxDown)) { - const QRect updown = proxy()->subControlRect(CC_SpinBox, sb, SC_SpinBoxUp, widget) - | proxy()->subControlRect(CC_SpinBox, sb, SC_SpinBoxDown, widget); - - d->setupNSGraphicsContext(cg, NO); - - const auto aquaSize = d->effectiveAquaSizeConstrain(opt, widget); - const auto cw = QMacStylePrivate::CocoaControl(QMacStylePrivate::Stepper, aquaSize); - NSStepperCell *cell = static_cast(d->cocoaCell(cw)); - cell.enabled = (sb->state & State_Enabled); - - const CGRect newRect = [cell drawingRectForBounds:updown.toCGRect()]; - - const bool upPressed = sb->activeSubControls == SC_SpinBoxUp && (sb->state & State_Sunken); - const bool downPressed = sb->activeSubControls == SC_SpinBoxDown && (sb->state & State_Sunken); - const CGFloat x = CGRectGetMidX(newRect); - const CGFloat y = upPressed ? -3 : 3; // Weird coordinate shift going on. Verified with Hopper - const CGPoint pressPoint = CGPointMake(x, y); - // Pretend we're pressing the mouse on the right button. Unfortunately, NSStepperCell has no - // API to highlight a specific button. The highlighted property works only on the down button. - if (upPressed || downPressed) - [cell startTrackingAt:pressPoint inView:d->backingStoreNSView]; - - [cell drawWithFrame:newRect inView:d->backingStoreNSView]; - - if (upPressed || downPressed) - [cell stopTracking:pressPoint at:pressPoint inView:d->backingStoreNSView mouseIsUp:NO]; - - d->restoreNSGraphicsContext(cg); - } - } - break; -#endif -#if QT_CONFIG(combobox) - case CC_ComboBox: - if (const auto *combo = qstyleoption_cast(opt)) { - const bool isEnabled = combo->state & State_Enabled; - const bool isPressed = combo->state & State_Sunken; - - const auto ct = cocoaControlType(combo, widget); - const auto cs = d->effectiveAquaSizeConstrain(combo, widget); - const auto cw = QMacStylePrivate::CocoaControl(ct, cs); - auto *cc = static_cast(d->cocoaControl(cw)); - cc.enabled = isEnabled; - QRectF frameRect = cw.adjustedControlFrame(combo->rect);; - if (cw.type == QMacStylePrivate::Button_PopupButton) { - // Non-editable QComboBox - auto *pb = static_cast(cc); - // FIXME Old offsets. Try to move to adjustedControlFrame() - if (cw.size == QStyleHelper::SizeSmall) { - frameRect = frameRect.translated(0, 1); - } else if (cw.size == QStyleHelper::SizeMini) { - // Same 0.5 pt misalignment as AppKit and fit the focus ring - frameRect = frameRect.translated(2, -0.5); - } - pb.frame = frameRect.toCGRect(); - [pb highlight:isPressed]; - d->drawNSViewInRect(pb, frameRect, p, ^(CGContextRef, const CGRect &r) { - [pb.cell drawBezelWithFrame:r inView:pb.superview]; - }); - } else if (cw.type == QMacStylePrivate::ComboBox) { - // Editable QComboBox - auto *cb = static_cast(cc); - const auto frameRect = cw.adjustedControlFrame(combo->rect); - cb.frame = frameRect.toCGRect(); - - // This API was requested to Apple in rdar #36197888. We know it's safe to use up to macOS 10.13.3 - if (NSButtonCell *cell = static_cast([cc.cell qt_valueForPrivateKey:@"_buttonCell"])) { - cell.highlighted = isPressed; - } else { - // TODO Render to pixmap and darken the button manually - } - - d->drawNSViewInRect(cb, frameRect, p, ^(CGContextRef, const CGRect &r) { - // FIXME This is usually drawn in the control's superview, but we wouldn't get inactive look in this case - [cb.cell drawWithFrame:r inView:cb]; - }); - } - - if (combo->state & State_HasFocus) { - // TODO Remove and use QFocusFrame instead. - const int hMargin = proxy()->pixelMetric(QStyle::PM_FocusFrameHMargin, combo, widget); - const int vMargin = proxy()->pixelMetric(QStyle::PM_FocusFrameVMargin, combo, widget); - QRectF focusRect; - if (cw.type == QMacStylePrivate::Button_PopupButton) { - focusRect = QRectF::fromCGRect([cc alignmentRectForFrame:cc.frame]); - focusRect -= pullDownButtonShadowMargins[cw.size]; - if (cw.size == QStyleHelper::SizeSmall) - focusRect = focusRect.translated(0, 1); - else if (cw.size == QStyleHelper::SizeMini) - focusRect = focusRect.translated(2, -1); - } else if (cw.type == QMacStylePrivate::ComboBox) { - focusRect = frameRect - comboBoxFocusRingMargins[cw.size]; - } - d->drawFocusRing(p, focusRect, hMargin, vMargin, cw); - } - } - break; -#endif // QT_CONFIG(combobox) - case CC_TitleBar: - if (const auto *titlebar = qstyleoption_cast(opt)) { - const bool isActive = (titlebar->state & State_Active) - && (titlebar->titleBarState & State_Active); - - p->fillRect(opt->rect, Qt::transparent); - p->setRenderHint(QPainter::Antialiasing); - p->setClipRect(opt->rect, Qt::IntersectClip); - - // FIXME A single drawPath() with 0-sized pen - // doesn't look as good as this double fillPath(). - const auto outerFrameRect = QRectF(opt->rect.adjusted(0, 0, 0, opt->rect.height())); - QPainterPath outerFramePath = d->windowPanelPath(outerFrameRect); - p->fillPath(outerFramePath, opt->palette.dark()); - - const auto frameAdjust = 1.0 / p->device()->devicePixelRatioF(); - const auto innerFrameRect = outerFrameRect.adjusted(frameAdjust, frameAdjust, -frameAdjust, 0); - QPainterPath innerFramePath = d->windowPanelPath(innerFrameRect); - if (isActive) { - QLinearGradient g; - g.setStart(QPointF(0, 0)); - g.setFinalStop(QPointF(0, 2 * opt->rect.height())); - g.setColorAt(0, opt->palette.button().color()); - g.setColorAt(1, opt->palette.dark().color()); - p->fillPath(innerFramePath, g); - } else { - p->fillPath(innerFramePath, opt->palette.button()); - } - - if (titlebar->subControls & (SC_TitleBarCloseButton - | SC_TitleBarMaxButton - | SC_TitleBarMinButton - | SC_TitleBarNormalButton)) { - const bool isHovered = (titlebar->state & State_MouseOver); - static const SubControl buttons[] = { - SC_TitleBarCloseButton, SC_TitleBarMinButton, SC_TitleBarMaxButton - }; - for (const auto sc : buttons) { - const auto ct = d->windowButtonCocoaControl(sc); - const auto cw = QMacStylePrivate::CocoaControl(ct, QStyleHelper::SizeLarge); - auto *wb = static_cast(d->cocoaControl(cw)); - wb.enabled = (sc & titlebar->subControls); - [wb highlight:(titlebar->state & State_Sunken) && (sc & titlebar->activeSubControls)]; - Q_UNUSED(isHovered); // FIXME No public API for this - - const auto buttonRect = proxy()->subControlRect(CC_TitleBar, titlebar, sc, widget); - d->drawNSViewInRect(wb, buttonRect, p, ^(CGContextRef, const CGRect &rect) { - auto *wbCell = static_cast(wb.cell); - [wbCell drawWithFrame:rect inView:wb]; - }); - } - } - - if (titlebar->subControls & SC_TitleBarLabel) { - const auto tr = proxy()->subControlRect(CC_TitleBar, titlebar, SC_TitleBarLabel, widget); - if (!titlebar->icon.isNull()) { - const auto iconExtent = proxy()->pixelMetric(PM_SmallIconSize); - const auto iconSize = QSize(iconExtent, iconExtent); - const auto iconPos = tr.x() - titlebar->icon.actualSize(iconSize).width() - qRound(titleBarIconTitleSpacing); - // Only render the icon if it'll be fully visible - if (iconPos < tr.right() - titleBarIconTitleSpacing) - p->drawPixmap(iconPos, tr.y(), titlebar->icon.pixmap(window, iconSize, QIcon::Normal)); - } - - if (!titlebar->text.isEmpty()) - drawItemText(p, tr, Qt::AlignCenter, opt->palette, isActive, titlebar->text, QPalette::Text); - } - } - break; - case CC_GroupBox: - if (const QStyleOptionGroupBox *gb - = qstyleoption_cast(opt)) { - - QStyleOptionGroupBox groupBox(*gb); - const bool flat = groupBox.features & QStyleOptionFrame::Flat; - if (!flat) - groupBox.state |= QStyle::State_Mini; // Force mini-sized checkbox to go with small-sized label - else - groupBox.subControls = groupBox.subControls & ~SC_GroupBoxFrame; // We don't like frames and ugly lines - - const bool didSetFont = widget && widget->testAttribute(Qt::WA_SetFont); - const bool didModifySubControls = !didSetFont && QApplication::desktopSettingsAware(); - if (didModifySubControls) - groupBox.subControls = groupBox.subControls & ~SC_GroupBoxLabel; - QCommonStyle::drawComplexControl(cc, &groupBox, p, widget); - if (didModifySubControls) { - const QRect rect = proxy()->subControlRect(CC_GroupBox, &groupBox, SC_GroupBoxLabel, widget); - const bool rtl = groupBox.direction == Qt::RightToLeft; - const int alignment = Qt::TextHideMnemonic | (rtl ? Qt::AlignRight : Qt::AlignLeft); - const QFont savedFont = p->font(); - if (!flat) - p->setFont(d->smallSystemFont); - proxy()->drawItemText(p, rect, alignment, groupBox.palette, groupBox.state & State_Enabled, groupBox.text, QPalette::WindowText); - if (!flat) - p->setFont(savedFont); - } - } - break; - case CC_ToolButton: - if (const QStyleOptionToolButton *tb - = qstyleoption_cast(opt)) { -#ifndef QT_NO_ACCESSIBILITY - if (QStyleHelper::hasAncestor(opt->styleObject, QAccessible::ToolBar)) { - if (tb->subControls & SC_ToolButtonMenu) { - QStyleOption arrowOpt = *tb; - arrowOpt.rect = proxy()->subControlRect(cc, tb, SC_ToolButtonMenu, widget); - arrowOpt.rect.setY(arrowOpt.rect.y() + arrowOpt.rect.height() / 2); - arrowOpt.rect.setHeight(arrowOpt.rect.height() / 2); - proxy()->drawPrimitive(PE_IndicatorArrowDown, &arrowOpt, p, widget); - } else if ((tb->features & QStyleOptionToolButton::HasMenu) - && (tb->toolButtonStyle != Qt::ToolButtonTextOnly && !tb->icon.isNull())) { - d->drawToolbarButtonArrow(tb, p); - } - if (tb->state & State_On) { - NSView *view = window ? (NSView *)window->winId() : nil; - bool isKey = false; - if (view) - isKey = [view.window isKeyWindow]; - - QBrush brush(isKey ? QColor(0, 0, 0, 28) - : QColor(0, 0, 0, 21)); - QPainterPath path; - path.addRoundedRect(QRectF(tb->rect.x(), tb->rect.y(), tb->rect.width(), tb->rect.height() + 4), 4, 4); - p->setRenderHint(QPainter::Antialiasing); - p->fillPath(path, brush); - } - proxy()->drawControl(CE_ToolButtonLabel, opt, p, widget); - } else -#endif // QT_NO_ACCESSIBILITY - { - auto bflags = tb->state; - if (tb->subControls & SC_ToolButton) - bflags |= State_Sunken; - auto mflags = tb->state; - if (tb->subControls & SC_ToolButtonMenu) - mflags |= State_Sunken; - - if (tb->subControls & SC_ToolButton) { - if (bflags & (State_Sunken | State_On | State_Raised)) { - const bool isEnabled = tb->state & State_Enabled; - const bool isPressed = tb->state & State_Sunken; - const bool isHighlighted = (tb->state & State_Active) && (tb->state & State_On); - const auto ct = QMacStylePrivate::Button_PushButton; - const auto cs = d->effectiveAquaSizeConstrain(opt, widget); - const auto cw = QMacStylePrivate::CocoaControl(ct, cs); - auto *pb = static_cast(d->cocoaControl(cw)); - pb.bezelStyle = NSShadowlessSquareBezelStyle; // TODO Use NSTexturedRoundedBezelStyle in the future. - pb.frame = opt->rect.toCGRect(); - pb.buttonType = NSPushOnPushOffButton; - pb.enabled = isEnabled; - [pb highlight:isPressed]; - pb.state = isHighlighted && !isPressed ? NSOnState : NSOffState; - const auto buttonRect = proxy()->subControlRect(cc, tb, SC_ToolButton, widget); - d->drawNSViewInRect(pb, buttonRect, p, ^(CGContextRef, const CGRect &rect) { - [pb.cell drawBezelWithFrame:rect inView:pb]; - }); - } - } - - if (tb->subControls & SC_ToolButtonMenu) { - const auto menuRect = proxy()->subControlRect(cc, tb, SC_ToolButtonMenu, widget); - QStyleOption arrowOpt = *tb; - arrowOpt.rect = QRect(menuRect.x() + ((menuRect.width() - toolButtonArrowSize) / 2), - menuRect.height() - (toolButtonArrowSize + toolButtonArrowMargin), - toolButtonArrowSize, - toolButtonArrowSize); - proxy()->drawPrimitive(PE_IndicatorArrowDown, &arrowOpt, p, widget); - } else if (tb->features & QStyleOptionToolButton::HasMenu) { - d->drawToolbarButtonArrow(tb, p); - } - QRect buttonRect = proxy()->subControlRect(CC_ToolButton, tb, SC_ToolButton, widget); - int fw = proxy()->pixelMetric(PM_DefaultFrameWidth, opt, widget); - QStyleOptionToolButton label = *tb; - label.rect = buttonRect.adjusted(fw, fw, -fw, -fw); - proxy()->drawControl(CE_ToolButtonLabel, &label, p, widget); - } - } - break; -#if QT_CONFIG(dial) - case CC_Dial: - if (const QStyleOptionSlider *dial = qstyleoption_cast(opt)) - QStyleHelper::drawDial(dial, p); - break; -#endif - default: - QCommonStyle::drawComplexControl(cc, opt, p, widget); - break; - } -} - -QStyle::SubControl QMacStyle::hitTestComplexControl(ComplexControl cc, - const QStyleOptionComplex *opt, - const QPoint &pt, const QWidget *widget) const -{ - Q_D(const QMacStyle); - SubControl sc = QStyle::SC_None; - switch (cc) { - case CC_ComboBox: - if (const QStyleOptionComboBox *cmb = qstyleoption_cast(opt)) { - sc = QCommonStyle::hitTestComplexControl(cc, cmb, pt, widget); - if (!cmb->editable && sc != QStyle::SC_None) - sc = SC_ComboBoxArrow; // A bit of a lie, but what we want - } - break; - case CC_Slider: - if (const QStyleOptionSlider *sl = qstyleoption_cast(opt)) { - if (!sl->rect.contains(pt)) - break; - - const bool hasTicks = sl->tickPosition != QSlider::NoTicks; - const bool isHorizontal = sl->orientation == Qt::Horizontal; - const auto ct = isHorizontal ? QMacStylePrivate::Slider_Horizontal : QMacStylePrivate::Slider_Vertical; - const auto cs = d->effectiveAquaSizeConstrain(opt, widget); - const auto cw = QMacStylePrivate::CocoaControl(ct, cs); - auto *slider = static_cast(d->cocoaControl(cw)); - if (!setupSlider(slider, sl)) - break; - - [slider calcSize]; - NSSliderCell *cell = slider.cell; - const auto barRect = QRectF::fromCGRect([cell barRectFlipped:hasTicks]); - const auto knobRect = QRectF::fromCGRect([cell knobRectFlipped:NO]); - if (knobRect.contains(pt)) { - sc = SC_SliderHandle; - } else if (barRect.contains(pt)) { - sc = SC_SliderGroove; - } else if (hasTicks) { - sc = SC_SliderTickmarks; - } - } - break; - case CC_ScrollBar: - if (const QStyleOptionSlider *sb = qstyleoption_cast(opt)) { - if (!sb->rect.contains(pt)) { - sc = SC_None; - break; - } - - const bool isHorizontal = sb->orientation == Qt::Horizontal; - const auto ct = isHorizontal ? QMacStylePrivate::Scroller_Horizontal : QMacStylePrivate::Scroller_Vertical; - const auto cs = d->effectiveAquaSizeConstrain(opt, widget); - const auto cw = QMacStylePrivate::CocoaControl(ct, cs); - auto *scroller = static_cast(d->cocoaControl(cw)); - if (!setupScroller(scroller, sb)) { - sc = SC_None; - break; - } - - // Since -[NSScroller testPart:] doesn't want to cooperate, we do it the - // straightforward way. In any case, macOS doesn't return line-sized changes - // with NSScroller since 10.7, according to the aforementioned method's doc. - const auto knobRect = QRectF::fromCGRect([scroller rectForPart:NSScrollerKnob]); - if (isHorizontal) { - const bool isReverse = sb->direction == Qt::RightToLeft; - if (pt.x() < knobRect.left()) - sc = isReverse ? SC_ScrollBarAddPage : SC_ScrollBarSubPage; - else if (pt.x() > knobRect.right()) - sc = isReverse ? SC_ScrollBarSubPage : SC_ScrollBarAddPage; - else - sc = SC_ScrollBarSlider; - } else { - if (pt.y() < knobRect.top()) - sc = SC_ScrollBarSubPage; - else if (pt.y() > knobRect.bottom()) - sc = SC_ScrollBarAddPage; - else - sc = SC_ScrollBarSlider; - } - } - break; - default: - sc = QCommonStyle::hitTestComplexControl(cc, opt, pt, widget); - break; - } - return sc; -} - -QRect QMacStyle::subControlRect(ComplexControl cc, const QStyleOptionComplex *opt, SubControl sc, - const QWidget *widget) const -{ - Q_D(const QMacStyle); - QRect ret; - switch (cc) { - case CC_ScrollBar: - if (const QStyleOptionSlider *sb = qstyleoption_cast(opt)) { - const bool isHorizontal = sb->orientation == Qt::Horizontal; - const bool isReverseHorizontal = isHorizontal && (sb->direction == Qt::RightToLeft); - - NSScrollerPart part = NSScrollerNoPart; - if (sc == SC_ScrollBarSlider) { - part = NSScrollerKnob; - } else if (sc == SC_ScrollBarGroove) { - part = NSScrollerKnobSlot; - } else if (sc == SC_ScrollBarSubPage || sc == SC_ScrollBarAddPage) { - if ((!isReverseHorizontal && sc == SC_ScrollBarSubPage) - || (isReverseHorizontal && sc == SC_ScrollBarAddPage)) - part = NSScrollerDecrementPage; - else - part = NSScrollerIncrementPage; - } - // And nothing else since 10.7 - - if (part != NSScrollerNoPart) { - const auto ct = isHorizontal ? QMacStylePrivate::Scroller_Horizontal : QMacStylePrivate::Scroller_Vertical; - const auto cs = d->effectiveAquaSizeConstrain(opt, widget); - const auto cw = QMacStylePrivate::CocoaControl(ct, cs); - auto *scroller = static_cast(d->cocoaControl(cw)); - if (setupScroller(scroller, sb)) - ret = QRectF::fromCGRect([scroller rectForPart:part]).toRect(); - } - } - break; - case CC_Slider: - if (const QStyleOptionSlider *sl = qstyleoption_cast(opt)) { - const bool hasTicks = sl->tickPosition != QSlider::NoTicks; - const bool isHorizontal = sl->orientation == Qt::Horizontal; - const auto ct = isHorizontal ? QMacStylePrivate::Slider_Horizontal : QMacStylePrivate::Slider_Vertical; - const auto cs = d->effectiveAquaSizeConstrain(opt, widget); - const auto cw = QMacStylePrivate::CocoaControl(ct, cs); - auto *slider = static_cast(d->cocoaControl(cw)); - if (!setupSlider(slider, sl)) - break; - - [slider calcSize]; - NSSliderCell *cell = slider.cell; - if (sc == SC_SliderHandle) { - ret = QRectF::fromCGRect([cell knobRectFlipped:NO]).toRect(); - if (isHorizontal) { - ret.setTop(sl->rect.top()); - ret.setBottom(sl->rect.bottom()); - } else { - ret.setLeft(sl->rect.left()); - ret.setRight(sl->rect.right()); - } - } else if (sc == SC_SliderGroove) { - ret = QRectF::fromCGRect([cell barRectFlipped:hasTicks]).toRect(); - } else if (hasTicks && sc == SC_SliderTickmarks) { - const auto tickMarkRect = QRectF::fromCGRect([cell rectOfTickMarkAtIndex:0]); - if (isHorizontal) - ret = QRect(sl->rect.left(), tickMarkRect.top(), sl->rect.width(), tickMarkRect.height()); - else - ret = QRect(tickMarkRect.left(), sl->rect.top(), tickMarkRect.width(), sl->rect.height()); - } - - // Invert if needed and extend to the actual bounds of the slider - if (isHorizontal) { - if (sl->upsideDown) { - ret = QRect(sl->rect.right() - ret.right(), sl->rect.top(), ret.width(), sl->rect.height()); - } else { - ret.setTop(sl->rect.top()); - ret.setBottom(sl->rect.bottom()); - } - } else { - if (!sl->upsideDown) { - ret = QRect(sl->rect.left(), sl->rect.bottom() - ret.bottom(), sl->rect.width(), ret.height()); - } else { - ret.setLeft(sl->rect.left()); - ret.setRight(sl->rect.right()); - } - } - } - break; - case CC_TitleBar: - if (const auto *titlebar = qstyleoption_cast(opt)) { - // The title bar layout is as follows: close, min, zoom, icon, title - // [ x _ + @ Window Title ] - // Center the icon and title until it starts to overlap with the buttons. - // The icon doesn't count towards SC_TitleBarLabel, but it's still rendered - // next to the title text. See drawComplexControl(). - if (sc == SC_TitleBarLabel) { - qreal labelWidth = titlebar->fontMetrics.horizontalAdvance(titlebar->text) + 1; // FIXME Rounding error? - qreal labelHeight = titlebar->fontMetrics.height(); - - const auto lastButtonRect = proxy()->subControlRect(CC_TitleBar, titlebar, SC_TitleBarMaxButton, widget); - qreal controlsSpacing = lastButtonRect.right() + titleBarButtonSpacing; - if (!titlebar->icon.isNull()) { - const auto iconSize = proxy()->pixelMetric(PM_SmallIconSize); - const auto actualIconSize = titlebar->icon.actualSize(QSize(iconSize, iconSize)).width();; - controlsSpacing += actualIconSize + titleBarIconTitleSpacing; - } - - const qreal labelPos = qMax(controlsSpacing, (opt->rect.width() - labelWidth) / 2.0); - labelWidth = qMin(labelWidth, opt->rect.width() - (labelPos + titleBarTitleRightMargin)); - ret = QRect(labelPos, (opt->rect.height() - labelHeight) / 2, - labelWidth, labelHeight); - } else { - const auto currentButton = d->windowButtonCocoaControl(sc); - if (currentButton == QMacStylePrivate::NoControl) - break; - - QPointF buttonPos = titlebar->rect.topLeft() + QPointF(titleBarButtonSpacing, 0); - QSizeF buttonSize; - for (int ct = QMacStylePrivate::Button_WindowClose; ct <= currentButton; ct++) { - const auto cw = QMacStylePrivate::CocoaControl(QMacStylePrivate::CocoaControlType(ct), - QStyleHelper::SizeLarge); - auto *wb = static_cast(d->cocoaControl(cw)); - if (ct == currentButton) - buttonSize = QSizeF::fromCGSize(wb.frame.size); - else - buttonPos.rx() += wb.frame.size.width + titleBarButtonSpacing; - } - - const auto vOffset = (opt->rect.height() - buttonSize.height()) / 2.0; - ret = QRectF(buttonPos, buttonSize).translated(0, vOffset).toRect(); - } - } - break; - case CC_ComboBox: - if (const QStyleOptionComboBox *combo = qstyleoption_cast(opt)) { - const auto ct = cocoaControlType(combo, widget); - const auto cs = d->effectiveAquaSizeConstrain(combo, widget); - const auto cw = QMacStylePrivate::CocoaControl(ct, cs); - const auto editRect = QMacStylePrivate::comboboxEditBounds(cw.adjustedControlFrame(combo->rect), cw); - - switch (sc) { - case SC_ComboBoxEditField:{ - ret = editRect.toAlignedRect(); - break; } - case SC_ComboBoxArrow:{ - ret = editRect.toAlignedRect(); - ret.setX(ret.x() + ret.width()); - ret.setWidth(combo->rect.right() - ret.right()); - break; } - case SC_ComboBoxListBoxPopup:{ - if (combo->editable) { - const CGRect inner = QMacStylePrivate::comboboxInnerBounds(combo->rect.toCGRect(), cw); - const int comboTop = combo->rect.top(); - ret = QRect(qRound(inner.origin.x), - comboTop, - qRound(inner.origin.x - combo->rect.left() + inner.size.width), - editRect.bottom() - comboTop + 2); - } else { - ret = QRect(combo->rect.x() + 4 - 11, - combo->rect.y() + 1, - editRect.width() + 10 + 11, - 1); - } - break; } - default: - break; - } - } - break; - case CC_GroupBox: - if (const QStyleOptionGroupBox *groupBox = qstyleoption_cast(opt)) { - bool checkable = groupBox->subControls & SC_GroupBoxCheckBox; - const bool flat = groupBox->features & QStyleOptionFrame::Flat; - bool hasNoText = !checkable && groupBox->text.isEmpty(); - switch (sc) { - case SC_GroupBoxLabel: - case SC_GroupBoxCheckBox: { - // Cheat and use the smaller font if we need to - const bool checkable = groupBox->subControls & SC_GroupBoxCheckBox; - const bool fontIsSet = (widget && widget->testAttribute(Qt::WA_SetFont)) - || !QApplication::desktopSettingsAware(); - const int margin = flat || hasNoText ? 0 : 9; - ret = groupBox->rect.adjusted(margin, 0, -margin, 0); - - const QFontMetricsF fm = flat || fontIsSet ? QFontMetricsF(groupBox->fontMetrics) : QFontMetricsF(d->smallSystemFont); - const QSizeF s = fm.size(Qt::AlignHCenter | Qt::AlignVCenter, qt_mac_removeMnemonics(groupBox->text), 0, nullptr); - const int tw = qCeil(s.width()); - const int h = qCeil(fm.height()); - ret.setHeight(h); - - QRect labelRect = alignedRect(groupBox->direction, groupBox->textAlignment, - QSize(tw, h), ret); - if (flat && checkable) - labelRect.moveLeft(labelRect.left() + 4); - int indicatorWidth = proxy()->pixelMetric(PM_IndicatorWidth, opt, widget); - bool rtl = groupBox->direction == Qt::RightToLeft; - if (sc == SC_GroupBoxLabel) { - if (checkable) { - int newSum = indicatorWidth + 1; - int newLeft = labelRect.left() + (rtl ? -newSum : newSum); - labelRect.moveLeft(newLeft); - if (flat) - labelRect.moveTop(labelRect.top() + 3); - else - labelRect.moveTop(labelRect.top() + 4); - } else if (flat) { - int newLeft = labelRect.left() - (rtl ? 3 : -3); - labelRect.moveLeft(newLeft); - labelRect.moveTop(labelRect.top() + 3); - } else { - int newLeft = labelRect.left() - (rtl ? 3 : 2); - labelRect.moveLeft(newLeft); - labelRect.moveTop(labelRect.top() + 4); - } - ret = labelRect; - } - - if (sc == SC_GroupBoxCheckBox) { - int left = rtl ? labelRect.right() - indicatorWidth : labelRect.left() - 1; - int top = flat ? ret.top() + 1 : ret.top() + 5; - ret.setRect(left, top, - indicatorWidth, proxy()->pixelMetric(PM_IndicatorHeight, opt, widget)); - } - break; - } - case SC_GroupBoxContents: - case SC_GroupBoxFrame: { - QFontMetrics fm = groupBox->fontMetrics; - int yOffset = 3; - if (!flat) { - if (widget && !widget->testAttribute(Qt::WA_SetFont) - && QApplication::desktopSettingsAware()) - fm = QFontMetrics(qt_app_fonts_hash()->value("QSmallFont", QFont())); - yOffset = 5; - } - - if (hasNoText) - yOffset = -qCeil(QFontMetricsF(fm).height()); - ret = opt->rect.adjusted(0, qCeil(QFontMetricsF(fm).height()) + yOffset, 0, 0); - if (sc == SC_GroupBoxContents) { - if (flat) - ret.adjust(3, -5, -3, -4); // guess too - else - ret.adjust(3, 3, -3, -4); // guess - } - } - break; - default: - ret = QCommonStyle::subControlRect(cc, groupBox, sc, widget); - break; - } - } - break; -#if QT_CONFIG(spinbox) - case CC_SpinBox: - if (const QStyleOptionSpinBox *spin = qstyleoption_cast(opt)) { - QStyleHelper::WidgetSizePolicy aquaSize = d->effectiveAquaSizeConstrain(spin, widget); - const auto fw = proxy()->pixelMetric(PM_SpinBoxFrameWidth, spin, widget); - int spinner_w; - int spinBoxSep; - switch (aquaSize) { - case QStyleHelper::SizeLarge: - spinner_w = 14; - spinBoxSep = 2; - break; - case QStyleHelper::SizeSmall: - spinner_w = 12; - spinBoxSep = 2; - break; - case QStyleHelper::SizeMini: - spinner_w = 10; - spinBoxSep = 1; - break; - default: - Q_UNREACHABLE(); - } - - switch (sc) { - case SC_SpinBoxUp: - case SC_SpinBoxDown: { - if (spin->buttonSymbols == QAbstractSpinBox::NoButtons) - break; - - const int y = fw; - const int x = spin->rect.width() - spinner_w; - ret.setRect(x + spin->rect.x(), y + spin->rect.y(), spinner_w, spin->rect.height() - y * 2); - int hackTranslateX; - switch (aquaSize) { - case QStyleHelper::SizeLarge: - hackTranslateX = 0; - break; - case QStyleHelper::SizeSmall: - hackTranslateX = -2; - break; - case QStyleHelper::SizeMini: - hackTranslateX = -1; - break; - default: - Q_UNREACHABLE(); - } - - const auto cw = QMacStylePrivate::CocoaControl(QMacStylePrivate::Stepper, aquaSize); - NSStepperCell *cell = static_cast(d->cocoaCell(cw)); - const CGRect outRect = [cell drawingRectForBounds:ret.toCGRect()]; - ret = QRectF::fromCGRect(outRect).toRect(); - - switch (sc) { - case SC_SpinBoxUp: - ret.setHeight(ret.height() / 2); - break; - case SC_SpinBoxDown: - ret.setY(ret.y() + ret.height() / 2); - break; - default: - Q_ASSERT(0); - break; - } - ret.translate(hackTranslateX, 0); // hack: position the buttons correctly (weird that we need this) - ret = visualRect(spin->direction, spin->rect, ret); - break; - } - case SC_SpinBoxEditField: - ret = spin->rect.adjusted(fw, fw, -fw, -fw); - if (spin->buttonSymbols != QAbstractSpinBox::NoButtons) { - ret.setWidth(spin->rect.width() - spinBoxSep - spinner_w); - ret = visualRect(spin->direction, spin->rect, ret); - } - break; - default: - ret = QCommonStyle::subControlRect(cc, spin, sc, widget); - break; - } - } - break; -#endif - case CC_ToolButton: - ret = QCommonStyle::subControlRect(cc, opt, sc, widget); - if (sc == SC_ToolButtonMenu) { -#ifndef QT_NO_ACCESSIBILITY - if (QStyleHelper::hasAncestor(opt->styleObject, QAccessible::ToolBar)) - ret.adjust(-toolButtonArrowMargin, 0, 0, 0); -#endif - ret.adjust(-1, 0, 0, 0); - } - break; - default: - ret = QCommonStyle::subControlRect(cc, opt, sc, widget); - break; - } - return ret; -} - -QSize QMacStyle::sizeFromContents(ContentsType ct, const QStyleOption *opt, - const QSize &csz, const QWidget *widget) const -{ - Q_D(const QMacStyle); - QSize sz(csz); - bool useAquaGuideline = true; - - switch (ct) { -#if QT_CONFIG(spinbox) - case CT_SpinBox: - if (qstyleoption_cast(opt)) { - const int buttonWidth = 20; // FIXME Use subControlRect() - sz += QSize(buttonWidth, 0); - } - break; -#endif - case QStyle::CT_TabWidget: - // the size between the pane and the "contentsRect" (+4,+4) - // (the "contentsRect" is on the inside of the pane) - sz = QCommonStyle::sizeFromContents(ct, opt, csz, widget); - /** - This is supposed to show the relationship between the tabBar and - the stack widget of a QTabWidget. - Unfortunately ascii is not a good way of representing graphics..... - PS: The '=' line is the painted frame. - - top ---+ - | - | - | - | vvv just outside the painted frame is the "pane" - - -|- - - - - - - - - - <-+ - TAB BAR +=====^============ | +2 pixels - - - -|- - -|- - - - - - - <-+ - | | ^ ^^^ just inside the painted frame is the "contentsRect" - | | | - | overlap | - | | | - bottom ------+ <-+ +14 pixels - | - v - ------------------------------ <- top of stack widget - - - To summarize: - * 2 is the distance between the pane and the contentsRect - * The 14 and the 1's are the distance from the contentsRect to the stack widget. - (same value as used in SE_TabWidgetTabContents) - * overlap is how much the pane should overlap the tab bar - */ - // then add the size between the stackwidget and the "contentsRect" -#if QT_CONFIG(tabwidget) - if (const QStyleOptionTabWidgetFrame *twf - = qstyleoption_cast(opt)) { - QSize extra(0,0); - const int overlap = pixelMetric(PM_TabBarBaseOverlap, opt, widget); - const int gapBetweenTabbarAndStackWidget = 2 + 14 - overlap; - - const auto tabDirection = QMacStylePrivate::tabDirection(twf->shape); - if (tabDirection == QMacStylePrivate::North - || tabDirection == QMacStylePrivate::South) { - extra = QSize(2, gapBetweenTabbarAndStackWidget + 1); - } else { - extra = QSize(gapBetweenTabbarAndStackWidget + 1, 2); - } - sz+= extra; - } -#endif - break; -#if QT_CONFIG(tabbar) - case QStyle::CT_TabBarTab: - if (const QStyleOptionTab *tab = qstyleoption_cast(opt)) { - const bool differentFont = (widget && widget->testAttribute(Qt::WA_SetFont)) - || !QApplication::desktopSettingsAware(); - const auto tabDirection = QMacStylePrivate::tabDirection(tab->shape); - const bool verticalTabs = tabDirection == QMacStylePrivate::East - || tabDirection == QMacStylePrivate::West; - if (verticalTabs) - sz = sz.transposed(); - - int defaultTabHeight; - const auto cs = d->effectiveAquaSizeConstrain(opt, widget); - switch (cs) { - case QStyleHelper::SizeLarge: - if (tab->documentMode) - defaultTabHeight = 24; - else - defaultTabHeight = 21; - break; - case QStyleHelper::SizeSmall: - defaultTabHeight = 18; - break; - case QStyleHelper::SizeMini: - defaultTabHeight = 16; - break; - default: - break; - } - - const bool widthSet = !differentFont && tab->icon.isNull(); - if (widthSet) { - const auto textSize = opt->fontMetrics.size(Qt::TextShowMnemonic, tab->text); - sz.rwidth() = textSize.width(); - sz.rheight() = qMax(defaultTabHeight, textSize.height()); - } else { - sz.rheight() = qMax(defaultTabHeight, sz.height()); - } - sz.rwidth() += proxy()->pixelMetric(PM_TabBarTabHSpace, tab, widget); - - if (verticalTabs) - sz = sz.transposed(); - - int maxWidgetHeight = qMax(tab->leftButtonSize.height(), tab->rightButtonSize.height()); - int maxWidgetWidth = qMax(tab->leftButtonSize.width(), tab->rightButtonSize.width()); - - int widgetWidth = 0; - int widgetHeight = 0; - int padding = 0; - if (tab->leftButtonSize.isValid()) { - padding += 8; - widgetWidth += tab->leftButtonSize.width(); - widgetHeight += tab->leftButtonSize.height(); - } - if (tab->rightButtonSize.isValid()) { - padding += 8; - widgetWidth += tab->rightButtonSize.width(); - widgetHeight += tab->rightButtonSize.height(); - } - - if (verticalTabs) { - sz.setWidth(qMax(sz.width(), maxWidgetWidth)); - sz.setHeight(sz.height() + widgetHeight + padding); - } else { - if (widthSet) - sz.setWidth(sz.width() + widgetWidth + padding); - sz.setHeight(qMax(sz.height(), maxWidgetHeight)); - } - } - break; -#endif - case QStyle::CT_PushButton: { - if (const QStyleOptionButton *btn = qstyleoption_cast(opt)) - if (btn->features & QStyleOptionButton::CommandLinkButton) - return QCommonStyle::sizeFromContents(ct, opt, sz, widget); - - // By default, we fit the contents inside a normal rounded push button. - // Do this by add enough space around the contents so that rounded - // borders (including highlighting when active) will show. - // TODO Use QFocusFrame and get rid of these horrors. - QSize macsz; - const auto controlSize = d->effectiveAquaSizeConstrain(opt, widget, CT_PushButton, sz, &macsz); - // FIXME See comment in CT_PushButton case in qt_aqua_get_known_size(). - if (macsz.width() != -1) - sz.setWidth(macsz.width()); - else - sz.rwidth() += QMacStylePrivate::PushButtonLeftOffset + QMacStylePrivate::PushButtonRightOffset + 12; - // All values as measured from HIThemeGetButtonBackgroundBounds() - if (controlSize != QStyleHelper::SizeMini) - sz.rwidth() += 12; // We like 12 over here. - if (controlSize == QStyleHelper::SizeLarge && sz.height() > 16) - sz.rheight() += pushButtonDefaultHeight[QStyleHelper::SizeLarge] - 16; - else if (controlSize == QStyleHelper::SizeMini) - sz.setHeight(24); // FIXME Our previous HITheme-based logic returned this. - else - sz.setHeight(pushButtonDefaultHeight[controlSize]); - break; - } - case QStyle::CT_MenuItem: - if (const QStyleOptionMenuItem *mi = qstyleoption_cast(opt)) { - int maxpmw = mi->maxIconWidth; -#if QT_CONFIG(combobox) - const QComboBox *comboBox = qobject_cast(widget); -#endif - int w = sz.width(), - h = sz.height(); - if (mi->menuItemType == QStyleOptionMenuItem::Separator) { - w = 10; - h = qt_mac_aqua_get_metric(MenuSeparatorHeight); - } else { - h = mi->fontMetrics.height() + 2; - if (!mi->icon.isNull()) { -#if QT_CONFIG(combobox) - if (comboBox) { - const QSize &iconSize = comboBox->iconSize(); - h = qMax(h, iconSize.height() + 4); - maxpmw = qMax(maxpmw, iconSize.width()); - } else -#endif - { - int iconExtent = proxy()->pixelMetric(PM_SmallIconSize); - h = qMax(h, mi->icon.actualSize(QSize(iconExtent, iconExtent)).height() + 4); - } - } - } - if (mi->text.contains(QLatin1Char('\t'))) - w += 12; - else if (mi->menuItemType == QStyleOptionMenuItem::SubMenu) - w += 35; // Not quite exactly as it seems to depend on other factors - if (maxpmw) - w += maxpmw + 6; - // add space for a check. All items have place for a check too. - w += 20; -#if QT_CONFIG(combobox) - if (comboBox && comboBox->isVisible()) { - QStyleOptionComboBox cmb; - cmb.initFrom(comboBox); - cmb.editable = false; - cmb.subControls = QStyle::SC_ComboBoxEditField; - cmb.activeSubControls = QStyle::SC_None; - w = qMax(w, subControlRect(QStyle::CC_ComboBox, &cmb, - QStyle::SC_ComboBoxEditField, - comboBox).width()); - } else -#endif - { - w += 12; - } - sz = QSize(w, h); - } - break; - case CT_MenuBarItem: - if (!sz.isEmpty()) - sz += QSize(12, 4); // Constants from QWindowsStyle - break; - case CT_ToolButton: - sz.rwidth() += 10; - sz.rheight() += 10; - if (const auto *tb = qstyleoption_cast(opt)) - if (tb->features & QStyleOptionToolButton::Menu) - sz.rwidth() += toolButtonArrowMargin; - return sz; - case CT_ComboBox: - if (const auto *cb = qstyleoption_cast(opt)) { - const auto controlSize = d->effectiveAquaSizeConstrain(opt, widget); - if (!cb->editable) { - // Same as CT_PushButton, because we have to fit the focus - // ring and a non-editable combo box is a NSPopUpButton. - sz.rwidth() += QMacStylePrivate::PushButtonLeftOffset + QMacStylePrivate::PushButtonRightOffset + 12; - // All values as measured from HIThemeGetButtonBackgroundBounds() - if (controlSize != QStyleHelper::SizeMini) - sz.rwidth() += 12; // We like 12 over here. -#if 0 - // TODO Maybe support square combo boxes - if (controlSize == QStyleHelper::SizeLarge && sz.height() > 16) - sz.rheight() += popupButtonDefaultHeight[QStyleHelper::SizeLarge] - 16; - else -#endif - } else { - sz.rwidth() += 50; // FIXME Double check this - } - - // This should be enough to fit the focus ring - if (controlSize == QStyleHelper::SizeMini) - sz.setHeight(24); // FIXME Our previous HITheme-based logic returned this for CT_PushButton. - else - sz.setHeight(pushButtonDefaultHeight[controlSize]); - - return sz; - } - break; - case CT_Menu: { - if (proxy() == this) { - sz = csz; - } else { - QStyleHintReturnMask menuMask; - QStyleOption myOption = *opt; - myOption.rect.setSize(sz); - if (proxy()->styleHint(SH_Menu_Mask, &myOption, widget, &menuMask)) - sz = menuMask.region.boundingRect().size(); - } - break; } - case CT_HeaderSection:{ - const QStyleOptionHeader *header = qstyleoption_cast(opt); - sz = QCommonStyle::sizeFromContents(ct, opt, csz, widget); - if (header->text.contains(QLatin1Char('\n'))) - useAquaGuideline = false; - break; } - case CT_ScrollBar : - // Make sure that the scroll bar is large enough to display the thumb indicator. - if (const QStyleOptionSlider *slider = qstyleoption_cast(opt)) { - const int minimumSize = 24; // Smallest knob size, but Cocoa doesn't seem to care - if (slider->orientation == Qt::Horizontal) - sz = sz.expandedTo(QSize(minimumSize, sz.height())); - else - sz = sz.expandedTo(QSize(sz.width(), minimumSize)); - } - break; - case CT_ItemViewItem: - if (const QStyleOptionViewItem *vopt = qstyleoption_cast(opt)) { - sz = QCommonStyle::sizeFromContents(ct, vopt, csz, widget); - sz.setHeight(sz.height() + 2); - } - break; - - default: - sz = QCommonStyle::sizeFromContents(ct, opt, csz, widget); - } - - if (useAquaGuideline && ct != CT_PushButton) { - // TODO Probably going away at some point - QSize macsz; - if (d->aquaSizeConstrain(opt, widget, ct, sz, &macsz) != QStyleHelper::SizeDefault) { - if (macsz.width() != -1) - sz.setWidth(macsz.width()); - if (macsz.height() != -1) - sz.setHeight(macsz.height()); - } - } - - // The sizes that Carbon and the guidelines gives us excludes the focus frame. - // We compensate for this by adding some extra space here to make room for the frame when drawing: - if (const QStyleOptionComboBox *combo = qstyleoption_cast(opt)){ - if (combo->editable) { - const auto widgetSize = d->aquaSizeConstrain(opt, widget); - QMacStylePrivate::CocoaControl cw; - cw.type = combo->editable ? QMacStylePrivate::ComboBox : QMacStylePrivate::Button_PopupButton; - cw.size = widgetSize; - const CGRect diffRect = QMacStylePrivate::comboboxInnerBounds(CGRectZero, cw); - sz.rwidth() -= qRound(diffRect.size.width); - sz.rheight() -= qRound(diffRect.size.height); - } - } - return sz; -} - -void QMacStyle::drawItemText(QPainter *p, const QRect &r, int flags, const QPalette &pal, - bool enabled, const QString &text, QPalette::ColorRole textRole) const -{ - if(flags & Qt::TextShowMnemonic) - flags |= Qt::TextHideMnemonic; - QCommonStyle::drawItemText(p, r, flags, pal, enabled, text, textRole); -} - -bool QMacStyle::event(QEvent *e) -{ - Q_D(QMacStyle); - if(e->type() == QEvent::FocusIn) { - QWidget *f = 0; - QWidget *focusWidget = QApplication::focusWidget(); -#if QT_CONFIG(graphicsview) - if (QGraphicsView *graphicsView = qobject_cast(focusWidget)) { - QGraphicsItem *focusItem = graphicsView->scene() ? graphicsView->scene()->focusItem() : 0; - if (focusItem && focusItem->type() == QGraphicsProxyWidget::Type) { - QGraphicsProxyWidget *proxy = static_cast(focusItem); - if (proxy->widget()) - focusWidget = proxy->widget()->focusWidget(); - } - } -#endif - - if (focusWidget && focusWidget->testAttribute(Qt::WA_MacShowFocusRect)) { -#if QT_CONFIG(spinbox) - if (const auto sb = qobject_cast(focusWidget)) - f = sb->property("_q_spinbox_lineedit").value(); - else -#endif - f = focusWidget; - } - if (f) { - if(!d->focusWidget) - d->focusWidget = new QFocusFrame(f); - d->focusWidget->setWidget(f); - } else if(d->focusWidget) { - d->focusWidget->setWidget(0); - } - } else if(e->type() == QEvent::FocusOut) { - if(d->focusWidget) - d->focusWidget->setWidget(0); - } - return false; -} - -QIcon QMacStyle::standardIcon(StandardPixmap standardIcon, const QStyleOption *opt, - const QWidget *widget) const -{ - switch (standardIcon) { - default: - return QCommonStyle::standardIcon(standardIcon, opt, widget); - case SP_ToolBarHorizontalExtensionButton: - case SP_ToolBarVerticalExtensionButton: { - QPixmap pixmap(QLatin1String(":/qt-project.org/styles/macstyle/images/toolbar-ext.png")); - if (standardIcon == SP_ToolBarVerticalExtensionButton) { - QPixmap pix2(pixmap.height(), pixmap.width()); - pix2.setDevicePixelRatio(pixmap.devicePixelRatio()); - pix2.fill(Qt::transparent); - QPainter p(&pix2); - p.translate(pix2.width(), 0); - p.rotate(90); - p.drawPixmap(0, 0, pixmap); - return pix2; - } - return pixmap; - } - } -} - -int QMacStyle::layoutSpacing(QSizePolicy::ControlType control1, - QSizePolicy::ControlType control2, - Qt::Orientation orientation, - const QStyleOption *option, - const QWidget *widget) const -{ - const int ButtonMask = QSizePolicy::ButtonBox | QSizePolicy::PushButton; - const int controlSize = getControlSize(option, widget); - - if (control2 == QSizePolicy::ButtonBox) { - /* - AHIG seems to prefer a 12-pixel margin between group - boxes and the row of buttons. The 20 pixel comes from - Builder. - */ - if (control1 & (QSizePolicy::Frame // guess - | QSizePolicy::GroupBox // (AHIG, guess, guess) - | QSizePolicy::TabWidget // guess - | ButtonMask)) { // AHIG - return_SIZE(14, 8, 8); - } else if (control1 == QSizePolicy::LineEdit) { - return_SIZE(8, 8, 8); // Interface Builder - } else { - return_SIZE(20, 7, 7); // Interface Builder - } - } - - if ((control1 | control2) & ButtonMask) { - if (control1 == QSizePolicy::LineEdit) - return_SIZE(8, 8, 8); // Interface Builder - else if (control2 == QSizePolicy::LineEdit) { - if (orientation == Qt::Vertical) - return_SIZE(20, 7, 7); // Interface Builder - else - return_SIZE(20, 8, 8); - } - return_SIZE(14, 8, 8); // Interface Builder - } - - switch (CT2(control1, control2)) { - case CT1(QSizePolicy::Label): // guess - case CT2(QSizePolicy::Label, QSizePolicy::DefaultType): // guess - case CT2(QSizePolicy::Label, QSizePolicy::CheckBox): // AHIG - case CT2(QSizePolicy::Label, QSizePolicy::ComboBox): // AHIG - case CT2(QSizePolicy::Label, QSizePolicy::LineEdit): // guess - case CT2(QSizePolicy::Label, QSizePolicy::RadioButton): // AHIG - case CT2(QSizePolicy::Label, QSizePolicy::Slider): // guess - case CT2(QSizePolicy::Label, QSizePolicy::SpinBox): // guess - case CT2(QSizePolicy::Label, QSizePolicy::ToolButton): // guess - return_SIZE(8, 6, 5); - case CT1(QSizePolicy::ToolButton): - return 8; // AHIG - case CT1(QSizePolicy::CheckBox): - case CT2(QSizePolicy::CheckBox, QSizePolicy::RadioButton): - case CT2(QSizePolicy::RadioButton, QSizePolicy::CheckBox): - if (orientation == Qt::Vertical) - return_SIZE(8, 8, 7); // AHIG and Builder - break; - case CT1(QSizePolicy::RadioButton): - if (orientation == Qt::Vertical) - return 5; // (Builder, guess, AHIG) - } - - if (orientation == Qt::Horizontal - && (control2 & (QSizePolicy::CheckBox | QSizePolicy::RadioButton))) - return_SIZE(12, 10, 8); // guess - - if ((control1 | control2) & (QSizePolicy::Frame - | QSizePolicy::GroupBox - | QSizePolicy::TabWidget)) { - /* - These values were chosen so that nested container widgets - look good side by side. Builder uses 8, which looks way - too small, and AHIG doesn't say anything. - */ - return_SIZE(16, 10, 10); // guess - } - - if ((control1 | control2) & (QSizePolicy::Line | QSizePolicy::Slider)) - return_SIZE(12, 10, 8); // AHIG - - if ((control1 | control2) & QSizePolicy::LineEdit) - return_SIZE(10, 8, 8); // AHIG - - /* - AHIG and Builder differ by up to 4 pixels for stacked editable - comboboxes. We use some values that work fairly well in all - cases. - */ - if ((control1 | control2) & QSizePolicy::ComboBox) - return_SIZE(10, 8, 7); // guess - - /* - Builder defaults to 8, 6, 5 in lots of cases, but most of the time the - result looks too cramped. - */ - return_SIZE(10, 8, 6); // guess -} - -QT_END_NAMESPACE diff --git a/5.12.3/bin/7z.exe b/5.12.3/bin/7z.exe deleted file mode 100644 index d3fe532..0000000 Binary files a/5.12.3/bin/7z.exe and /dev/null differ diff --git a/5.12.3/bin/wget.exe b/5.12.3/bin/wget.exe deleted file mode 100644 index cda6b94..0000000 Binary files a/5.12.3/bin/wget.exe and /dev/null differ diff --git a/5.12.3/compile_mac.sh b/5.12.3/compile_mac.sh deleted file mode 100644 index 0412225..0000000 --- a/5.12.3/compile_mac.sh +++ /dev/null @@ -1,72 +0,0 @@ -#!/bin/bash - -# requirements: -# 1. brew -# 2. brew install llvm (https://bugreports.qt.io/browse/QTBUG-66353) - -export PATH=$PATH:/usr/local/Qt-5.12.3/bin - -cd qtbase - -if [[ $1 == openssl ]]; then - - # download openssl - curl -O https://www.openssl.org/source/openssl-1.1.1a.tar.gz - tar -xvzf openssl-1.1.1a.tar.gz - - # compile openssl - cd openssl-1.1.1a - ./Configure darwin64-x86_64-cc --prefix=$PWD/dist - make - # print arch info (optional) - lipo -info libssl.a - lipo -info libcrypto.a - make install - cd .. - - # continue - - OPENSSL_LIBS='-L$PWD/openssl-1.1.1a/dist/lib -lssl -lcrypto' ./configure -opensource -confirm-license -no-securetransport -nomake examples -nomake tests -openssl-linked -I $PWD/openssl-1.1.1a/dist/include -L $PWD/openssl-1.1.1a/dist/lib - -elif [[ $1 == securetransport ]]; then - - ./configure -opensource -confirm-license -nomake examples -nomake tests -no-openssl -securetransport - -else - - echo "Error: please specify which SSL layer to use (openssl or securetransport)" - exit 1 - -fi - -make -j 12 -echo maki | sudo -S sudo make install - -cd ../qttools -qmake -make -j 12 -echo maki | sudo -S sudo make install - -cd ../qtmacextras -qmake -make -j 12 -echo maki | sudo -S sudo make install - -cd ../qtdeclarative/src -qmake -make -j 12 sub-qmldevtools -echo maki | sudo -S sudo make install - -# make docs - currently doesnt work - -#cd ../qtbase -#make -j 12 docs -#cd ../qttools -#make -j 12 docs -#cd ../qtmacextras -#make -j 12 docs - -#echo maki | sudo -S cp -f -r ../qtbase/doc /usr/local/Qt-5.12.3/ - -cd /usr/local -zip -r ~/Desktop/qt5.12.3_mac.zip Qt-5.12.3/* \ No newline at end of file diff --git a/5.12.3/compile_win.pl b/5.12.3/compile_win.pl deleted file mode 100644 index 04ad716..0000000 --- a/5.12.3/compile_win.pl +++ /dev/null @@ -1,86 +0,0 @@ -use strict; - -die "Cannot proceed without the 'bin' folder'" if (!-e "bin"); - -my $arch = $ARGV[0]; -my $openssl_v_major = "1.1.1"; # The 1.1.1 series is Long Term Support (LTS) release, supported until 11th September 2023 -my $openssl_v_minor = "a"; -my $openssl_version = "$openssl_v_major$openssl_v_minor"; -my $openssl_dir = "openssl-$openssl_version"; -my $openssl_download = "https://www.openssl.org/source/openssl-$openssl_version.tar.gz"; -my $openssl_arch = $arch eq "amd64" ? "WIN64A" : "WIN32"; - -$arch = "x86" if ($arch eq ''); # specify x86 is nothing is specified -die "Please specify architecture (x86 or amd64)" if ($arch ne "x86" && $arch ne "amd64"); # die if user specified anything except x86 or amd64 - -# will create a batch file - -my $batfile = 'compile_win.bat'; - -open BAT, '>', $batfile; - -printLineToBat ("SET PATH=%PATH%;%cd%\\bin"); # add bin folder to the path for 7z and wget -printLineToBat ("CALL \"C:\\Program Files (x86)\\Microsoft Visual Studio\\2017\\Community\\VC\\Auxiliary\\Build\\vcvarsall.bat\" $arch"); -printLineToBat ("SET _ROOT=%cd%"); -printLineToBat ("SET PATH=%_ROOT%\\qtbase\\bin;%_ROOT%\\gnuwin32\\bin;%PATH%"); # http://doc.qt.io/qt-5/windows-building.html - -printLineToBat ("cd qtbase"); -printLineToBat ("if \"%~1\"==\"step2\" goto step2"); - -# step1: compile openssl and do configure. For some reason, can't continue script execution after configure, have to make step2 -printLineToBat ("IF EXIST $openssl_dir\\build GOTO OPENSSL_ALREAD_COMPILED"); -printLineToBat ("wget --no-check-certificate $openssl_download"); -printLineToBat ("7z x openssl-$openssl_version.tar.gz"); -printLineToBat ("7z x openssl-$openssl_version.tar"); -printLineToBat ("rm openssl-$openssl_version.tar.gz"); -printLineToBat ("rm openssl-$openssl_version.tar"); -printLineToBat ("cd $openssl_dir"); -printLineToBat ("perl Configure VC-$openssl_arch no-asm no-shared no-tests --prefix=%cd%\\build --openssldir=%cd%\\build"); -printLineToBat ("nmake"); -printLineToBat ("nmake install"); -# do little clean up -printLineToBat ("rm test\\*.exe"); -printLineToBat ("rm test\\*.pdb"); -printLineToBat ("rm test\\*.obj"); -printLineToBat (":OPENSSL_ALREAD_COMPILED"); -# go back to qtbase -printLineToBat ("cd .."); - -# -developer-build creates an in-source build for developer usage. -# openssl: see https://bugreports.qt.io/browse/QTBUG-65501 -printLineToBat ("configure -opensource -developer-build -confirm-license -opengl desktop -mp -nomake tests -nomake examples -I \"%cd%\\$openssl_dir\\build\\include\" -openssl-linked OPENSSL_LIBS=\"%cd%\\$openssl_dir\\build\\lib\\libssl.lib %cd%\\$openssl_dir\\build\\lib\\libcrypto.lib -lcrypt32 -lws2_32 -lAdvapi32 -luser32\""); -printLineToBat ("goto :EOF"); - -# step 2: -printLineToBat (":step2"); - -printLineToBat ("nmake"); -printLineToBat ("cd ..\\qttools"); -printLineToBat ("..\\qtbase\\bin\\qmake"); -printLineToBat ("nmake"); -printLineToBat ("cd ..\\qtbase"); -printLineToBat ("cd .."); # go up to qt dir -# openssl clean up -printLineToBat ("cd qtbase"); -printLineToBat ("cd $openssl_dir"); -printLineToBat ("del /s /f /q out32"); -printLineToBat ("del /s /f /q out32.dbg"); -printLineToBat ("cd .."); -printLineToBat ("cd .."); -# the rest -printLineToBat ("del *.obj /s /f"); -printLineToBat ("del *.ilk /s /f"); -printLineToBat ("del *.pch /s /f"); -printLineToBat ("del Makefile* /s /f"); - -close BAT; - -system ($batfile); -system ("$batfile step2"); - -system ("pause"); - -sub printLineToBat -{ - print BAT "$_[0]\n"; -} \ No newline at end of file diff --git a/5.12.3/qtbase/mkspecs/common/msvc-desktop.conf b/5.12.3/qtbase/mkspecs/common/msvc-desktop.conf deleted file mode 100644 index 3832831..0000000 --- a/5.12.3/qtbase/mkspecs/common/msvc-desktop.conf +++ /dev/null @@ -1,118 +0,0 @@ -# -# This file is used as a basis for the following compilers: -# -# - Microsoft C/C++ Optimizing Compiler (all desktop versions) -# - Intel C++ Compiler on Windows -# - Clang-cl -# -# Baseline: -# -# - Visual Studio 2005 (8.0), VC++ 14.0 -# -# Version-specific settings go in msvc-version.conf (loaded by default_pre) -# - -MAKEFILE_GENERATOR = MSVC.NET -QMAKE_PLATFORM = win32 -QMAKE_COMPILER = msvc -CONFIG += flat debug_and_release debug_and_release_target precompile_header autogen_precompile_source embed_manifest_dll embed_manifest_exe -# MSVC 2017 15.8+ fixed std::aligned_storage but compilation fails without -# _ENABLE_EXTENDED_ALIGNED_STORAGE flag since the fix breaks binary compatibility. -DEFINES += UNICODE _UNICODE WIN32 _ENABLE_EXTENDED_ALIGNED_STORAGE -QMAKE_COMPILER_DEFINES += _WIN32 -contains(QMAKE_TARGET.arch, x86_64) { - DEFINES += WIN64 - QMAKE_COMPILER_DEFINES += _WIN64 -} - -QMAKE_CFLAGS_OPTIMIZE_DEBUG = -Od -QMAKE_CFLAGS_OPTIMIZE = -O2 -QMAKE_CFLAGS_OPTIMIZE_SIZE = -O1 - -QMAKE_CC = cl -QMAKE_LEX = flex -QMAKE_LEXFLAGS = -QMAKE_YACC = bison -y -QMAKE_YACCFLAGS = -d -QMAKE_CFLAGS = -nologo -Zc:wchar_t -QMAKE_CFLAGS_WARN_ON = -W3 -QMAKE_CFLAGS_WARN_OFF = -W0 -QMAKE_CFLAGS_RELEASE = $$QMAKE_CFLAGS_OPTIMIZE -MD -Zi -QMAKE_CFLAGS_RELEASE_WITH_DEBUGINFO += $$QMAKE_CFLAGS_OPTIMIZE -Zi -MD -QMAKE_CFLAGS_DEBUG = -Zi -MDd -QMAKE_CFLAGS_YACC = -QMAKE_CFLAGS_LTCG = -GL - -contains(QMAKE_TARGET.arch, x86_64) { - # SSE2 is mandatory on 64-bit mode, so skip the option. It triggers: - # cl : Command line warning D9002 : ignoring unknown option '-arch:SSE2' - QMAKE_CFLAGS_SSE2 = -} else { - QMAKE_CFLAGS_SSE2 = -arch:SSE2 -} -QMAKE_CFLAGS_SSE3 = $$QMAKE_CFLAGS_SSE2 -QMAKE_CFLAGS_SSSE3 = $$QMAKE_CFLAGS_SSE2 -QMAKE_CFLAGS_SSE4_1 = $$QMAKE_CFLAGS_SSE2 -QMAKE_CFLAGS_SSE4_2 = $$QMAKE_CFLAGS_SSE2 -QMAKE_CFLAGS_AESNI = $$QMAKE_CFLAGS_SSE2 -QMAKE_CFLAGS_SHANI = $$QMAKE_CFLAGS_SSE2 - -QMAKE_CXX = $$QMAKE_CC -QMAKE_CXXFLAGS = $$QMAKE_CFLAGS -QMAKE_CXXFLAGS_WARN_ON = $$QMAKE_CFLAGS_WARN_ON -w34100 -w34189 -w44996 -QMAKE_CXXFLAGS_WARN_OFF = $$QMAKE_CFLAGS_WARN_OFF -QMAKE_CXXFLAGS_RELEASE = $$QMAKE_CFLAGS_RELEASE -QMAKE_CXXFLAGS_RELEASE_WITH_DEBUGINFO += $$QMAKE_CFLAGS_RELEASE_WITH_DEBUGINFO -QMAKE_CXXFLAGS_DEBUG = $$QMAKE_CFLAGS_DEBUG -QMAKE_CXXFLAGS_YACC = $$QMAKE_CFLAGS_YACC -QMAKE_CXXFLAGS_LTCG = $$QMAKE_CFLAGS_LTCG -QMAKE_CXXFLAGS_STL_ON = -EHsc -QMAKE_CXXFLAGS_STL_OFF = -QMAKE_CXXFLAGS_RTTI_ON = -GR -QMAKE_CXXFLAGS_RTTI_OFF = -QMAKE_CXXFLAGS_EXCEPTIONS_ON = -EHsc -QMAKE_CXXFLAGS_EXCEPTIONS_OFF = - -QMAKE_INCDIR = - -QMAKE_RUN_CC = $(CC) -c $(CFLAGS) $(INCPATH) -Fo$obj $src -QMAKE_RUN_CC_IMP = $(CC) -c $(CFLAGS) $(INCPATH) -Fo$@ $< -QMAKE_RUN_CC_IMP_BATCH = $(CC) -c $(CFLAGS) $(INCPATH) -Fo$@ @<< -QMAKE_RUN_CXX = $(CXX) -c $(CXXFLAGS) $(INCPATH) -Fo$obj $src -QMAKE_RUN_CXX_IMP = $(CXX) -c $(CXXFLAGS) $(INCPATH) -Fo$@ $< -QMAKE_RUN_CXX_IMP_BATCH = $(CXX) -c $(CXXFLAGS) $(INCPATH) -Fo$@ @<< - -QMAKE_LINK = link -QMAKE_LFLAGS = /NOLOGO /DYNAMICBASE /NXCOMPAT -QMAKE_LFLAGS_RELEASE = /INCREMENTAL:NO -QMAKE_LFLAGS_RELEASE_WITH_DEBUGINFO = /DEBUG /OPT:REF /INCREMENTAL:NO -QMAKE_LFLAGS_DEBUG = /DEBUG -QMAKE_LFLAGS_CONSOLE = /SUBSYSTEM:CONSOLE -QMAKE_LFLAGS_WINDOWS = /SUBSYSTEM:WINDOWS -QMAKE_LFLAGS_EXE = \"/MANIFESTDEPENDENCY:type=\'win32\' name=\'Microsoft.Windows.Common-Controls\' version=\'6.0.0.0\' publicKeyToken=\'6595b64144ccf1df\' language=\'*\' processorArchitecture=\'*\'\" -QMAKE_LFLAGS_DLL = /DLL -QMAKE_LFLAGS_LTCG = /LTCG -QMAKE_PREFIX_SHLIB = -QMAKE_EXTENSION_SHLIB = dll -QMAKE_PREFIX_STATICLIB = -QMAKE_EXTENSION_STATICLIB = lib - -QMAKE_LIBS = -QMAKE_LIBS_GUI = gdi32.lib comdlg32.lib oleaut32.lib imm32.lib winmm.lib ws2_32.lib ole32.lib uuid.lib user32.lib advapi32.lib -QMAKE_LIBS_NETWORK = ws2_32.lib user32.lib gdi32.lib -QMAKE_LIBS_OPENGL = glu32.lib opengl32.lib gdi32.lib user32.lib -QMAKE_LIBS_OPENGL_ES2 = gdi32.lib user32.lib -QMAKE_LIBS_OPENGL_ES2_DEBUG = gdi32.lib user32.lib -QMAKE_LIBS_COMPAT = advapi32.lib shell32.lib comdlg32.lib user32.lib gdi32.lib ws2_32.lib -QMAKE_LIBS_QT_ENTRY = -lqtmain - -QMAKE_IDL = midl -QMAKE_LIB = lib /NOLOGO -QMAKE_RC = rc /NOLOGO - -VCPROJ_EXTENSION = .vcproj -VCSOLUTION_EXTENSION = .sln -VCPROJ_KEYWORD = Qt4VSv1.0 - -include(angle.conf) -include(windows-vulkan.conf) diff --git a/5.12.3/qtbase/src/plugins/platforms/cocoa/qcocoaapplicationdelegate.mm b/5.12.3/qtbase/src/plugins/platforms/cocoa/qcocoaapplicationdelegate.mm deleted file mode 100644 index a183406..0000000 --- a/5.12.3/qtbase/src/plugins/platforms/cocoa/qcocoaapplicationdelegate.mm +++ /dev/null @@ -1,431 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2018 The Qt Company Ltd. -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the plugins of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:LGPL$ -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and The Qt Company. For licensing terms -** and conditions see https://www.qt.io/terms-conditions. For further -** information use the contact form at https://www.qt.io/contact-us. -** -** GNU Lesser General Public License Usage -** Alternatively, this file may be used under the terms of the GNU Lesser -** General Public License version 3 as published by the Free Software -** Foundation and appearing in the file LICENSE.LGPL3 included in the -** packaging of this file. Please review the following information to -** ensure the GNU Lesser General Public License version 3 requirements -** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. -** -** GNU General Public License Usage -** Alternatively, this file may be used under the terms of the GNU -** General Public License version 2.0 or (at your option) the GNU General -** Public license version 3 or any later version approved by the KDE Free -** Qt Foundation. The licenses are as published by the Free Software -** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 -** included in the packaging of this file. Please review the following -** information to ensure the GNU General Public License requirements will -** be met: https://www.gnu.org/licenses/gpl-2.0.html and -** https://www.gnu.org/licenses/gpl-3.0.html. -** -** $QT_END_LICENSE$ -** -****************************************************************************/ - -/**************************************************************************** - ** - ** Copyright (c) 2007-2008, Apple, Inc. - ** - ** All rights reserved. - ** - ** Redistribution and use in source and binary forms, with or without - ** modification, are permitted provided that the following conditions are met: - ** - ** * Redistributions of source code must retain the above copyright notice, - ** this list of conditions and the following disclaimer. - ** - ** * Redistributions in binary form must reproduce the above copyright notice, - ** this list of conditions and the following disclaimer in the documentation - ** and/or other materials provided with the distribution. - ** - ** * Neither the name of Apple, Inc. nor the names of its contributors - ** may be used to endorse or promote products derived from this software - ** without specific prior written permission. - ** - ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR - ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR - ** CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, - ** EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, - ** PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR - ** PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF - ** LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING - ** NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS - ** SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - ** - ****************************************************************************/ - - -#import "qcocoaapplicationdelegate.h" -#include "qcocoaintegration.h" -#include "qcocoamenu.h" -#include "qcocoamenuloader.h" -#include "qcocoamenuitem.h" -#include "qcocoansmenu.h" - -#include -#include -#include -#include -#include -#include "qt_mac_p.h" -#include -#include - -QT_USE_NAMESPACE - -@implementation QCocoaApplicationDelegate { - bool startedQuit; - NSObject *reflectionDelegate; - bool inLaunch; -} - -+ (instancetype)sharedDelegate -{ - static QCocoaApplicationDelegate *shared = nil; - static dispatch_once_t onceToken; - dispatch_once(&onceToken, ^{ - shared = [[self alloc] init]; - atexit_b(^{ - [shared release]; - shared = nil; - }); - }); - return shared; -} - -- (instancetype)init -{ - self = [super init]; - if (self) { - inLaunch = true; - } - return self; -} - -- (void)dealloc -{ - [_dockMenu release]; - if (reflectionDelegate) { - [[NSApplication sharedApplication] setDelegate:reflectionDelegate]; - [reflectionDelegate release]; - } - [[NSNotificationCenter defaultCenter] removeObserver:self]; - - [super dealloc]; -} - -- (NSMenu *)applicationDockMenu:(NSApplication *)sender -{ - Q_UNUSED(sender); - // Manually invoke the delegate's -menuWillOpen: method. - // See QTBUG-39604 (and its fix) for details. - [self.dockMenu.delegate menuWillOpen:self.dockMenu]; - return [[self.dockMenu retain] autorelease]; -} - -- (BOOL)canQuit -{ - [[NSApp mainMenu] cancelTracking]; - - bool handle_quit = true; - NSMenuItem *quitMenuItem = [[QT_MANGLE_NAMESPACE(QCocoaMenuLoader) sharedMenuLoader] quitMenuItem]; - if (!QGuiApplicationPrivate::instance()->modalWindowList.isEmpty() - && [quitMenuItem isEnabled]) { - int visible = 0; - const QWindowList tlws = QGuiApplication::topLevelWindows(); - for (int i = 0; i < tlws.size(); ++i) { - if (tlws.at(i)->isVisible()) - ++visible; - } - handle_quit = (visible <= 1); - } - - if (handle_quit) { - QCloseEvent ev; - QGuiApplication::sendEvent(qGuiApp, &ev); - if (ev.isAccepted()) { - return YES; - } - } - - return NO; -} - -// This function will only be called when NSApp is actually running. -- (NSApplicationTerminateReply)applicationShouldTerminate:(NSApplication *)sender -{ - // The reflection delegate gets precedence - if (reflectionDelegate) { - if ([reflectionDelegate respondsToSelector:@selector(applicationShouldTerminate:)]) - return [reflectionDelegate applicationShouldTerminate:sender]; - return NSTerminateNow; - } - - if ([self canQuit]) { - if (!startedQuit) { - startedQuit = true; - // Close open windows. This is done in order to deliver de-expose - // events while the event loop is still running. - const QWindowList topLevels = QGuiApplication::topLevelWindows(); - for (int i = 0; i < topLevels.size(); ++i) { - QWindow *topLevelWindow = topLevels.at(i); - // Already closed windows will not have a platform window, skip those - if (topLevelWindow->handle()) - QWindowSystemInterface::handleCloseEvent(topLevelWindow); - } - QWindowSystemInterface::flushWindowSystemEvents(); - - QGuiApplication::exit(0); - startedQuit = false; - } - } - - if (QGuiApplicationPrivate::instance()->threadData->eventLoops.isEmpty()) { - // INVARIANT: No event loop is executing. This probably - // means that Qt is used as a plugin, or as a part of a native - // Cocoa application. In any case it should be fine to - // terminate now: - return NSTerminateNow; - } - - return NSTerminateCancel; -} - -- (void)applicationWillFinishLaunching:(NSNotification *)notification -{ - Q_UNUSED(notification); - - /* - From the Cocoa documentation: "A good place to install event handlers - is in the applicationWillFinishLaunching: method of the application - delegate. At that point, the Application Kit has installed its default - event handlers, so if you install a handler for one of the same events, - it will replace the Application Kit version." - */ - - /* - If Qt is used as a plugin, we let the 3rd party application handle - events like quit and open file events. Otherwise, if we install our own - handlers, we easily end up breaking functionality the 3rd party - application depends on. - */ - NSAppleEventManager *eventManager = [NSAppleEventManager sharedAppleEventManager]; - /*[eventManager setEventHandler:self - andSelector:@selector(appleEventQuit:withReplyEvent:) - forEventClass:kCoreEventClass - andEventID:kAEQuitApplication];*/ - [eventManager setEventHandler:self - andSelector:@selector(getUrl:withReplyEvent:) - forEventClass:kInternetEventClass - andEventID:kAEGetURL]; -} - -// called by QCocoaIntegration's destructor before resetting the application delegate to nil -- (void)removeAppleEventHandlers -{ - NSAppleEventManager *eventManager = [NSAppleEventManager sharedAppleEventManager]; - //[eventManager removeEventHandlerForEventClass:kCoreEventClass andEventID:kAEQuitApplication]; - [eventManager removeEventHandlerForEventClass:kInternetEventClass andEventID:kAEGetURL]; -} - -- (bool)inLaunch -{ - return inLaunch; -} - -- (void)applicationDidFinishLaunching:(NSNotification *)aNotification -{ - Q_UNUSED(aNotification); - inLaunch = false; - - if (qEnvironmentVariableIsEmpty("QT_MAC_DISABLE_FOREGROUND_APPLICATION_TRANSFORM")) { - // Move the application window to front to avoid launching behind the terminal. - // Ignoring other apps is necessary (we must ignore the terminal), but makes - // Qt apps play slightly less nice with other apps when lanching from Finder - // (See the activateIgnoringOtherApps docs.) - [[NSApplication sharedApplication] activateIgnoringOtherApps:YES]; - } -} - -- (void)application:(NSApplication *)sender openFiles:(NSArray *)filenames -{ - Q_UNUSED(filenames); - Q_UNUSED(sender); - - for (NSString *fileName in filenames) { - QString qtFileName = QString::fromNSString(fileName); - if (inLaunch) { - // We need to be careful because Cocoa will be nice enough to take - // command line arguments and send them to us as events. Given the history - // of Qt Applications, this will result in behavior people don't want, as - // they might be doing the opening themselves with the command line parsing. - if (qApp->arguments().contains(qtFileName)) - continue; - } - QWindowSystemInterface::handleFileOpenEvent(qtFileName); - } - - if (reflectionDelegate && - [reflectionDelegate respondsToSelector:@selector(application:openFiles:)]) - [reflectionDelegate application:sender openFiles:filenames]; - -} - -- (BOOL)applicationShouldTerminateAfterLastWindowClosed:(NSApplication *)sender -{ - // If we have a reflection delegate, that will get to call the shots. - if (reflectionDelegate - && [reflectionDelegate respondsToSelector: - @selector(applicationShouldTerminateAfterLastWindowClosed:)]) - return [reflectionDelegate applicationShouldTerminateAfterLastWindowClosed:sender]; - return NO; // Someday qApp->quitOnLastWindowClosed(); when QApp and NSApp work closer together. -} - -- (void)applicationDidBecomeActive:(NSNotification *)notification -{ - if (reflectionDelegate - && [reflectionDelegate respondsToSelector:@selector(applicationDidBecomeActive:)]) - [reflectionDelegate applicationDidBecomeActive:notification]; - - QWindowSystemInterface::handleApplicationStateChanged(Qt::ApplicationActive); -} - -- (void)applicationDidResignActive:(NSNotification *)notification -{ - if (reflectionDelegate - && [reflectionDelegate respondsToSelector:@selector(applicationDidResignActive:)]) - [reflectionDelegate applicationDidResignActive:notification]; - - QWindowSystemInterface::handleApplicationStateChanged(Qt::ApplicationInactive); -} - -- (BOOL)applicationShouldHandleReopen:(NSApplication *)theApplication hasVisibleWindows:(BOOL)flag -{ - Q_UNUSED(theApplication); - Q_UNUSED(flag); - if (reflectionDelegate - && [reflectionDelegate respondsToSelector:@selector(applicationShouldHandleReopen:hasVisibleWindows:)]) - return [reflectionDelegate applicationShouldHandleReopen:theApplication hasVisibleWindows:flag]; - - /* - true to force delivery of the event even if the application state is already active, - because rapp (handle reopen) events are sent each time the dock icon is clicked regardless - of the active state of the application or number of visible windows. For example, a browser - app that has no windows opened would need the event be to delivered even if it was already - active in order to create a new window as per OS X conventions. - */ - QWindowSystemInterface::handleApplicationStateChanged(Qt::ApplicationActive, true /*forcePropagate*/); - - return YES; -} - -- (void)setReflectionDelegate:(NSObject *)oldDelegate -{ - [oldDelegate retain]; - [reflectionDelegate release]; - reflectionDelegate = oldDelegate; -} - -- (NSMethodSignature *)methodSignatureForSelector:(SEL)aSelector -{ - NSMethodSignature *result = [super methodSignatureForSelector:aSelector]; - if (!result && reflectionDelegate) { - result = [reflectionDelegate methodSignatureForSelector:aSelector]; - } - return result; -} - -- (BOOL)respondsToSelector:(SEL)aSelector -{ - BOOL result = [super respondsToSelector:aSelector]; - if (!result && reflectionDelegate) - result = [reflectionDelegate respondsToSelector:aSelector]; - return result; -} - -- (void)forwardInvocation:(NSInvocation *)invocation -{ - SEL invocationSelector = [invocation selector]; - if (reflectionDelegate && [reflectionDelegate respondsToSelector:invocationSelector]) - [invocation invokeWithTarget:reflectionDelegate]; - else - [self doesNotRecognizeSelector:invocationSelector]; -} - -- (void)getUrl:(NSAppleEventDescriptor *)event withReplyEvent:(NSAppleEventDescriptor *)replyEvent -{ - Q_UNUSED(replyEvent); - NSString *urlString = [[event paramDescriptorForKeyword:keyDirectObject] stringValue]; - QWindowSystemInterface::handleFileOpenEvent(QUrl(QString::fromNSString(urlString))); -} - -- (void)appleEventQuit:(NSAppleEventDescriptor *)event withReplyEvent:(NSAppleEventDescriptor *)replyEvent -{ - Q_UNUSED(event); - Q_UNUSED(replyEvent); - [NSApp terminate:self]; -} - -@end - -@implementation QCocoaApplicationDelegate (Menus) - -- (BOOL)validateMenuItem:(NSMenuItem*)item -{ - auto *nativeItem = qt_objc_cast(item); - if (!nativeItem) - return item.enabled; // FIXME Test with with Qt as plugin or embedded QWindow. - - auto *platformItem = nativeItem.platformMenuItem; - if (!platformItem) // Try a bit harder with orphan menu itens - return item.hasSubmenu || (item.enabled && (item.action != @selector(qt_itemFired:))); - - // Menu-holding items are always enabled, as it's conventional in Cocoa - if (platformItem->menu()) - return YES; - - return platformItem->isEnabled(); -} - -@end - -@implementation QCocoaApplicationDelegate (MenuAPI) - -- (void)qt_itemFired:(QCocoaNSMenuItem *)item -{ - if (item.hasSubmenu) - return; - - auto *nativeItem = qt_objc_cast(item); - Q_ASSERT_X(nativeItem, qPrintable(__FUNCTION__), "Triggered menu item is not a QCocoaNSMenuItem."); - auto *platformItem = nativeItem.platformMenuItem; - // Menu-holding items also get a target to play nicely - // with NSMenuValidation but should not trigger. - if (!platformItem || platformItem->menu()) - return; - - QScopedScopeLevelCounter scopeLevelCounter(QGuiApplicationPrivate::instance()->threadData); - QGuiApplicationPrivate::modifier_buttons = [QNSView convertKeyModifiers:[NSEvent modifierFlags]]; - - static QMetaMethod activatedSignal = QMetaMethod::fromSignal(&QCocoaMenuItem::activated); - activatedSignal.invoke(platformItem, Qt::QueuedConnection); -} - -@end diff --git a/5.12.3/qtbase/src/plugins/platforms/cocoa/qcocoasystemsettings.mm b/5.12.3/qtbase/src/plugins/platforms/cocoa/qcocoasystemsettings.mm deleted file mode 100644 index e20470a..0000000 --- a/5.12.3/qtbase/src/plugins/platforms/cocoa/qcocoasystemsettings.mm +++ /dev/null @@ -1,244 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2017 The Qt Company Ltd. -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the plugins of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:LGPL$ -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and The Qt Company. For licensing terms -** and conditions see https://www.qt.io/terms-conditions. For further -** information use the contact form at https://www.qt.io/contact-us. -** -** GNU Lesser General Public License Usage -** Alternatively, this file may be used under the terms of the GNU Lesser -** General Public License version 3 as published by the Free Software -** Foundation and appearing in the file LICENSE.LGPL3 included in the -** packaging of this file. Please review the following information to -** ensure the GNU Lesser General Public License version 3 requirements -** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. -** -** GNU General Public License Usage -** Alternatively, this file may be used under the terms of the GNU -** General Public License version 2.0 or (at your option) the GNU General -** Public license version 3 or any later version approved by the KDE Free -** Qt Foundation. The licenses are as published by the Free Software -** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 -** included in the packaging of this file. Please review the following -** information to ensure the GNU General Public License requirements will -** be met: https://www.gnu.org/licenses/gpl-2.0.html and -** https://www.gnu.org/licenses/gpl-3.0.html. -** -** $QT_END_LICENSE$ -** -****************************************************************************/ - -#include "qcocoasystemsettings.h" - -#include "qcocoahelpers.h" - -#include -#include -#include - -#if !QT_MACOS_PLATFORM_SDK_EQUAL_OR_ABOVE(__MAC_10_14) -@interface NSColor (MojaveForwardDeclarations) -@property (class, strong, readonly) NSColor *selectedContentBackgroundColor NS_AVAILABLE_MAC(10_14); -@property (class, strong, readonly) NSColor *unemphasizedSelectedTextBackgroundColor NS_AVAILABLE_MAC(10_14); -@property (class, strong, readonly) NSColor *unemphasizedSelectedTextColor NS_AVAILABLE_MAC(10_14); -@property (class, strong, readonly) NSColor *unemphasizedSelectedContentBackgroundColor NS_AVAILABLE_MAC(10_14); -@property (class, strong, readonly) NSArray *alternatingContentBackgroundColors NS_AVAILABLE_MAC(10_14); -@end -#endif - -QT_BEGIN_NAMESPACE - -QPalette * qt_mac_createSystemPalette() -{ - QColor qc; - - // Standard palette initialization (copied from Qt 4 styles) - QBrush backgroundBrush = qt_mac_toQBrush([NSColor windowBackgroundColor]); - QColor background = backgroundBrush.color(); - QColor light(background.lighter(110)); - QColor dark(background.darker(160)); - QColor mid(background.darker(140)); - QPalette *palette = new QPalette(Qt::black, background, light, dark, mid, Qt::black, Qt::white); - - palette->setBrush(QPalette::Window, backgroundBrush); - - palette->setBrush(QPalette::Disabled, QPalette::WindowText, dark); - palette->setBrush(QPalette::Disabled, QPalette::Text, dark); - palette->setBrush(QPalette::Disabled, QPalette::ButtonText, dark); - palette->setBrush(QPalette::Disabled, QPalette::Base, backgroundBrush); - QBrush textBackgroundBrush = qt_mac_toQBrush([NSColor textBackgroundColor]); - palette->setBrush(QPalette::Active, QPalette::Base, textBackgroundBrush); - palette->setBrush(QPalette::Inactive, QPalette::Base, textBackgroundBrush); - palette->setColor(QPalette::Disabled, QPalette::Dark, QColor(191, 191, 191)); - palette->setColor(QPalette::Active, QPalette::Dark, QColor(191, 191, 191)); - palette->setColor(QPalette::Inactive, QPalette::Dark, QColor(191, 191, 191)); - - // System palette initialization: - QBrush br = qt_mac_toQBrush([NSColor selectedControlColor]); - palette->setBrush(QPalette::Active, QPalette::Highlight, br); - if (__builtin_available(macOS 10.14, *)) { - const auto inactiveHighlight = qt_mac_toQBrush([NSColor unemphasizedSelectedContentBackgroundColor]); - palette->setBrush(QPalette::Inactive, QPalette::Highlight, inactiveHighlight); - palette->setBrush(QPalette::Disabled, QPalette::Highlight, inactiveHighlight); - } else { - palette->setBrush(QPalette::Inactive, QPalette::Highlight, br); - palette->setBrush(QPalette::Disabled, QPalette::Highlight, br); - } - - palette->setBrush(QPalette::Shadow, qt_mac_toQColor([NSColor shadowColor])); - - qc = qt_mac_toQColor([NSColor controlTextColor]); - palette->setColor(QPalette::Active, QPalette::Text, qc); - palette->setColor(QPalette::Active, QPalette::WindowText, qc); - palette->setColor(QPalette::Active, QPalette::HighlightedText, qc); - palette->setColor(QPalette::Inactive, QPalette::Text, qc); - palette->setColor(QPalette::Inactive, QPalette::WindowText, qc); - palette->setColor(QPalette::Inactive, QPalette::HighlightedText, qc); - - qc = qt_mac_toQColor([NSColor disabledControlTextColor]); - palette->setColor(QPalette::Disabled, QPalette::Text, qc); - palette->setColor(QPalette::Disabled, QPalette::WindowText, qc); - palette->setColor(QPalette::Disabled, QPalette::HighlightedText, qc); - - palette->setBrush(QPalette::ToolTipBase, qt_mac_toQBrush([NSColor controlColor])); - - // fix for https://bugreports.qt.io/browse/QTBUG-71740 - palette->setColor(QPalette::Normal, QPalette::Link, qt_mac_toQColor([NSColor linkColor])); - - return palette; -} - -struct QMacPaletteMap { - inline QMacPaletteMap(QPlatformTheme::Palette p, NSColor *a, NSColor *i) : - active(a), inactive(i), paletteRole(p) { } - - NSColor *active; - NSColor *inactive; - QPlatformTheme::Palette paletteRole; -}; - -#define MAC_PALETTE_ENTRY(pal, active, inactive) \ - QMacPaletteMap(pal, [NSColor active], [NSColor inactive]) -static QMacPaletteMap mac_widget_colors[] = { - MAC_PALETTE_ENTRY(QPlatformTheme::ToolButtonPalette, controlTextColor, disabledControlTextColor), - MAC_PALETTE_ENTRY(QPlatformTheme::ButtonPalette, controlTextColor, disabledControlTextColor), - MAC_PALETTE_ENTRY(QPlatformTheme::HeaderPalette, headerTextColor, disabledControlTextColor), - MAC_PALETTE_ENTRY(QPlatformTheme::ComboBoxPalette, controlTextColor, disabledControlTextColor), - MAC_PALETTE_ENTRY(QPlatformTheme::ItemViewPalette, textColor, disabledControlTextColor), - MAC_PALETTE_ENTRY(QPlatformTheme::MessageBoxLabelPalette, textColor, disabledControlTextColor), - MAC_PALETTE_ENTRY(QPlatformTheme::TabBarPalette, controlTextColor, disabledControlTextColor), - MAC_PALETTE_ENTRY(QPlatformTheme::LabelPalette, textColor, disabledControlTextColor), - MAC_PALETTE_ENTRY(QPlatformTheme::GroupBoxPalette, textColor, disabledControlTextColor), - MAC_PALETTE_ENTRY(QPlatformTheme::MenuPalette, controlTextColor, disabledControlTextColor), - MAC_PALETTE_ENTRY(QPlatformTheme::MenuBarPalette, controlTextColor, disabledControlTextColor), - MAC_PALETTE_ENTRY(QPlatformTheme::TextEditPalette, textColor, disabledControlTextColor), - MAC_PALETTE_ENTRY(QPlatformTheme::TextLineEditPalette, textColor, disabledControlTextColor) -}; -#undef MAC_PALETTE_ENTRY - -static const int mac_widget_colors_count = sizeof(mac_widget_colors) / sizeof(mac_widget_colors[0]); - -QHash qt_mac_createRolePalettes() -{ - QHash palettes; - QColor qc; - for (int i = 0; i < mac_widget_colors_count; i++) { - QPalette &pal = *qt_mac_createSystemPalette(); - if (mac_widget_colors[i].active) { - qc = qt_mac_toQColor(mac_widget_colors[i].active); - pal.setColor(QPalette::Active, QPalette::Text, qc); - pal.setColor(QPalette::Inactive, QPalette::Text, qc); - pal.setColor(QPalette::Active, QPalette::WindowText, qc); - pal.setColor(QPalette::Inactive, QPalette::WindowText, qc); - pal.setColor(QPalette::Active, QPalette::HighlightedText, qc); - pal.setColor(QPalette::Inactive, QPalette::HighlightedText, qc); - pal.setColor(QPalette::Active, QPalette::ButtonText, qc); - pal.setColor(QPalette::Inactive, QPalette::ButtonText, qc); - qc = qt_mac_toQColor(mac_widget_colors[i].inactive); - pal.setColor(QPalette::Disabled, QPalette::Text, qc); - pal.setColor(QPalette::Disabled, QPalette::WindowText, qc); - pal.setColor(QPalette::Disabled, QPalette::HighlightedText, qc); - pal.setColor(QPalette::Disabled, QPalette::ButtonText, qc); - } - if (mac_widget_colors[i].paletteRole == QPlatformTheme::MenuPalette - || mac_widget_colors[i].paletteRole == QPlatformTheme::MenuBarPalette) { - NSColor *selectedMenuItemColor = nil; - if (__builtin_available(macOS 10.14, *)) { - // Cheap approximation for NSVisualEffectView (see deprecation note for selectedMenuItemTextColor) - selectedMenuItemColor = [[NSColor selectedContentBackgroundColor] highlightWithLevel:0.4]; - } else { - // selectedMenuItemColor would presumably be the correct color to use as the background - // for selected menu items. But that color is always blue, and doesn't follow the - // appearance color in system preferences. So we therefore deliberatly choose to use - // keyboardFocusIndicatorColor instead, which appears to have the same color value. - selectedMenuItemColor = [NSColor keyboardFocusIndicatorColor]; - } - pal.setBrush(QPalette::Highlight, qt_mac_toQColor(selectedMenuItemColor)); - qc = qt_mac_toQColor([NSColor labelColor]); - pal.setBrush(QPalette::ButtonText, qc); - pal.setBrush(QPalette::Text, qc); - qc = qt_mac_toQColor([NSColor selectedMenuItemTextColor]); - pal.setBrush(QPalette::HighlightedText, qc); - qc = qt_mac_toQColor([NSColor disabledControlTextColor]); - pal.setBrush(QPalette::Disabled, QPalette::Text, qc); - } else if ((mac_widget_colors[i].paletteRole == QPlatformTheme::ButtonPalette) - || (mac_widget_colors[i].paletteRole == QPlatformTheme::HeaderPalette) - || (mac_widget_colors[i].paletteRole == QPlatformTheme::TabBarPalette)) { - pal.setColor(QPalette::Disabled, QPalette::ButtonText, - pal.color(QPalette::Disabled, QPalette::Text)); - pal.setColor(QPalette::Inactive, QPalette::ButtonText, - pal.color(QPalette::Inactive, QPalette::Text)); - pal.setColor(QPalette::Active, QPalette::ButtonText, - pal.color(QPalette::Active, QPalette::Text)); - } else if (mac_widget_colors[i].paletteRole == QPlatformTheme::ItemViewPalette) { - NSArray *baseColors = nil; - NSColor *activeHighlightColor = nil; - if (__builtin_available(macOS 10.14, *)) { - baseColors = [NSColor alternatingContentBackgroundColors]; - activeHighlightColor = [NSColor selectedContentBackgroundColor]; - pal.setBrush(QPalette::Inactive, QPalette::HighlightedText, - qt_mac_toQBrush([NSColor unemphasizedSelectedTextColor])); - } else { - baseColors = [NSColor controlAlternatingRowBackgroundColors]; - activeHighlightColor = [NSColor alternateSelectedControlColor]; - pal.setBrush(QPalette::Inactive, QPalette::HighlightedText, - pal.brush(QPalette::Active, QPalette::Text)); - } - pal.setBrush(QPalette::Base, qt_mac_toQBrush(baseColors[0])); - pal.setBrush(QPalette::AlternateBase, qt_mac_toQBrush(baseColors[1])); - pal.setBrush(QPalette::Active, QPalette::Highlight, - qt_mac_toQBrush(activeHighlightColor)); - pal.setBrush(QPalette::Active, QPalette::HighlightedText, - qt_mac_toQBrush([NSColor alternateSelectedControlTextColor])); - pal.setBrush(QPalette::Inactive, QPalette::Text, - pal.brush(QPalette::Active, QPalette::Text)); - } else if (mac_widget_colors[i].paletteRole == QPlatformTheme::TextEditPalette) { - pal.setBrush(QPalette::Active, QPalette::Base, qt_mac_toQColor([NSColor textBackgroundColor])); - pal.setBrush(QPalette::Inactive, QPalette::Text, - pal.brush(QPalette::Active, QPalette::Text)); - pal.setBrush(QPalette::Inactive, QPalette::HighlightedText, - pal.brush(QPalette::Active, QPalette::Text)); - } else if (mac_widget_colors[i].paletteRole == QPlatformTheme::TextLineEditPalette - || mac_widget_colors[i].paletteRole == QPlatformTheme::ComboBoxPalette) { - pal.setBrush(QPalette::Active, QPalette::Base, qt_mac_toQColor([NSColor textBackgroundColor])); - pal.setBrush(QPalette::Disabled, QPalette::Base, - pal.brush(QPalette::Active, QPalette::Base)); - } else if (mac_widget_colors[i].paletteRole == QPlatformTheme::LabelPalette) { - qc = qt_mac_toQColor([NSColor labelColor]); - pal.setBrush(QPalette::Inactive, QPalette::ToolTipText, qc); - } - palettes.insert(mac_widget_colors[i].paletteRole, &pal); - } - return palettes; -} - -QT_END_NAMESPACE diff --git a/5.12.3/qtbase/src/plugins/styles/mac/qmacstyle_mac.mm b/5.12.3/qtbase/src/plugins/styles/mac/qmacstyle_mac.mm deleted file mode 100644 index 9a11c4d..0000000 --- a/5.12.3/qtbase/src/plugins/styles/mac/qmacstyle_mac.mm +++ /dev/null @@ -1,6639 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2016 The Qt Company Ltd. -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the QtWidgets module of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:LGPL$ -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and The Qt Company. For licensing terms -** and conditions see https://www.qt.io/terms-conditions. For further -** information use the contact form at https://www.qt.io/contact-us. -** -** GNU Lesser General Public License Usage -** Alternatively, this file may be used under the terms of the GNU Lesser -** General Public License version 3 as published by the Free Software -** Foundation and appearing in the file LICENSE.LGPL3 included in the -** packaging of this file. Please review the following information to -** ensure the GNU Lesser General Public License version 3 requirements -** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. -** -** GNU General Public License Usage -** Alternatively, this file may be used under the terms of the GNU -** General Public License version 2.0 or (at your option) the GNU General -** Public license version 3 or any later version approved by the KDE Free -** Qt Foundation. The licenses are as published by the Free Software -** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 -** included in the packaging of this file. Please review the following -** information to ensure the GNU General Public License requirements will -** be met: https://www.gnu.org/licenses/gpl-2.0.html and -** https://www.gnu.org/licenses/gpl-3.0.html. -** -** $QT_END_LICENSE$ -** -****************************************************************************/ - -/* - Note: The qdoc comments for QMacStyle are contained in - .../doc/src/qstyles.qdoc. -*/ - -#include - -#include "qmacstyle_mac_p.h" -#include "qmacstyle_mac_p_p.h" - -#define QMAC_QAQUASTYLE_SIZE_CONSTRAIN -//#define DEBUG_SIZE_CONSTRAINT - -#include -#if QT_CONFIG(tabbar) -#include -#endif -#include -#include -#include -#if QT_CONFIG(combobox) -#include -#include -#endif -#if QT_CONFIG(dialogbuttonbox) -#include -#endif -#if QT_CONFIG(dockwidget) -#include -#endif -#include -#include -#include -#include -#include -#include -#if QT_CONFIG(lineedit) -#include -#endif -#if QT_CONFIG(mainwindow) -#include -#endif -#if QT_CONFIG(mdiarea) -#include -#endif -#if QT_CONFIG(menubar) -#include -#endif -#include -#include -#include -#include -#if QT_CONFIG(progressbar) -#include -#endif -#if QT_CONFIG(pushbutton) -#include -#endif -#include -#if QT_CONFIG(rubberband) -#include -#endif -#if QT_CONFIG(scrollbar) -#include -#endif -#if QT_CONFIG(sizegrip) -#include -#endif -#include -#include -#if QT_CONFIG(toolbutton) -#include -#endif -#if QT_CONFIG(treeview) -#include -#endif -#if QT_CONFIG(tableview) -#include -#endif -#include -#if QT_CONFIG(wizard) -#include -#endif -#include -#if QT_CONFIG(datetimeedit) -#include -#endif -#include -#include -#if QT_CONFIG(graphicsview) -#include -#endif -#include -#include -#include -#include -#include -#include -#include - -QT_USE_NAMESPACE - -static QWindow *qt_getWindow(const QWidget *widget) -{ - return widget ? widget->window()->windowHandle() : 0; -} - -@interface QT_MANGLE_NAMESPACE(NotificationReceiver) : NSObject -@end - -QT_NAMESPACE_ALIAS_OBJC_CLASS(NotificationReceiver); - -@implementation NotificationReceiver - -- (void)scrollBarStyleDidChange:(NSNotification *)notification -{ - Q_UNUSED(notification); - - // purge destroyed scroll bars: - QMacStylePrivate::scrollBars.removeAll(QPointer()); - - QEvent event(QEvent::StyleChange); - for (const auto &o : QMacStylePrivate::scrollBars) - QCoreApplication::sendEvent(o, &event); -} -@end - -@interface QT_MANGLE_NAMESPACE(QIndeterminateProgressIndicator) : NSProgressIndicator - -@property (readonly, nonatomic) NSInteger animators; - -- (instancetype)init; - -- (void)startAnimation; -- (void)stopAnimation; - -- (void)drawWithFrame:(CGRect)rect inView:(NSView *)view; - -@end - -QT_NAMESPACE_ALIAS_OBJC_CLASS(QIndeterminateProgressIndicator); - -@implementation QIndeterminateProgressIndicator - -- (instancetype)init -{ - if ((self = [super init])) { - _animators = 0; - self.indeterminate = YES; - self.usesThreadedAnimation = NO; - self.alphaValue = 0.0; - } - - return self; -} - -- (void)startAnimation -{ - if (_animators == 0) { - self.hidden = NO; - [super startAnimation:self]; - } - ++_animators; -} - -- (void)stopAnimation -{ - --_animators; - if (_animators == 0) { - [super stopAnimation:self]; - self.hidden = YES; - [self removeFromSuperviewWithoutNeedingDisplay]; - } -} - -- (void)drawWithFrame:(CGRect)rect inView:(NSView *)view -{ - // The alphaValue change is not strictly necessary, but feels safer. - self.alphaValue = 1.0; - if (self.superview != view) - [view addSubview:self]; - if (!CGRectEqualToRect(self.frame, rect)) - self.frame = rect; - [self drawRect:rect]; - self.alphaValue = 0.0; -} - -@end - -@interface QT_MANGLE_NAMESPACE(QVerticalSplitView) : NSSplitView -- (BOOL)isVertical; -@end - -QT_NAMESPACE_ALIAS_OBJC_CLASS(QVerticalSplitView); - -@implementation QVerticalSplitView -- (BOOL)isVertical -{ - return YES; -} -@end - -// See render code in drawPrimitive(PE_FrameTabWidget) -@interface QT_MANGLE_NAMESPACE(QDarkNSBox) : NSBox -@end - -QT_NAMESPACE_ALIAS_OBJC_CLASS(QDarkNSBox); - -@implementation QDarkNSBox -- (instancetype)init -{ - if ((self = [super init])) { - self.title = @""; - self.titlePosition = NSNoTitle; - self.boxType = NSBoxCustom; - self.cornerRadius = 3; - self.borderColor = [NSColor.controlColor colorWithAlphaComponent:0.1]; - self.fillColor = [NSColor.darkGrayColor colorWithAlphaComponent:0.2]; - } - - return self; -} - -- (void)drawRect:(NSRect)rect -{ - [super drawRect:rect]; -} -@end - -QT_BEGIN_NAMESPACE - -// The following constants are used for adjusting the size -// of push buttons so that they are drawn inside their bounds. -const int QMacStylePrivate::PushButtonLeftOffset = 6; -const int QMacStylePrivate::PushButtonRightOffset = 12; -const int QMacStylePrivate::PushButtonContentPadding = 6; - -QVector > QMacStylePrivate::scrollBars; - -// Title bar gradient colors for Lion were determined by inspecting PSDs exported -// using CoreUI's CoreThemeDocument; there is no public API to retrieve them - -static QLinearGradient titlebarGradientActive() -{ - static QLinearGradient darkGradient = [](){ - QLinearGradient gradient; - // FIXME: colors are chosen somewhat arbitrarily and could be fine-tuned, - // or ideally determined by calling a native API. - gradient.setColorAt(0, QColor(47, 47, 47)); - return gradient; - }(); - static QLinearGradient lightGradient = [](){ - QLinearGradient gradient; - gradient.setColorAt(0, QColor(235, 235, 235)); - gradient.setColorAt(0.5, QColor(210, 210, 210)); - gradient.setColorAt(0.75, QColor(195, 195, 195)); - gradient.setColorAt(1, QColor(180, 180, 180)); - return gradient; - }(); - return qt_mac_applicationIsInDarkMode() ? darkGradient : lightGradient; -} - -static QLinearGradient titlebarGradientInactive() -{ - static QLinearGradient darkGradient = [](){ - QLinearGradient gradient; - gradient.setColorAt(1, QColor(42, 42, 42)); - return gradient; - }(); - static QLinearGradient lightGradient = [](){ - QLinearGradient gradient; - gradient.setColorAt(0, QColor(250, 250, 250)); - gradient.setColorAt(1, QColor(225, 225, 225)); - return gradient; - }(); - return qt_mac_applicationIsInDarkMode() ? darkGradient : lightGradient; -} - -static void clipTabBarFrame(const QStyleOption *option, const QMacStyle *style, CGContextRef ctx) -{ - Q_ASSERT(option); - Q_ASSERT(style); - Q_ASSERT(ctx); - - if (qt_mac_applicationIsInDarkMode()) { - QTabWidget *tabWidget = qobject_cast(option->styleObject); - Q_ASSERT(tabWidget); - - const QRect tabBarRect = style->subElementRect(QStyle::SE_TabWidgetTabBar, option, tabWidget).adjusted(2, 2, -3, -2); - const QRegion clipPath = QRegion(option->rect) - tabBarRect; - QVarLengthArray cgRects; - for (const QRect &qtRect : clipPath) - cgRects.push_back(qtRect.toCGRect()); - if (cgRects.size()) - CGContextClipToRects(ctx, &cgRects[0], size_t(cgRects.size())); - } -} - -static const QColor titlebarSeparatorLineActive(111, 111, 111); -static const QColor titlebarSeparatorLineInactive(131, 131, 131); -static const QColor darkModeSeparatorLine(88, 88, 88); - -// Gradient colors used for the dock widget title bar and -// non-unifed tool bar bacground. -static const QColor lightMainWindowGradientBegin(240, 240, 240); -static const QColor lightMainWindowGradientEnd(200, 200, 200); -static const QColor darkMainWindowGradientBegin(47, 47, 47); -static const QColor darkMainWindowGradientEnd(47, 47, 47); - -static const int DisclosureOffset = 4; - -static const qreal titleBarIconTitleSpacing = 5; -static const qreal titleBarTitleRightMargin = 12; -static const qreal titleBarButtonSpacing = 8; - -// Tab bar colors -// active: window is active -// selected: tab is selected -// hovered: tab is hovered -bool isDarkMode() { return qt_mac_applicationIsInDarkMode(); } - -static const QColor lightTabBarTabBackgroundActive(190, 190, 190); -static const QColor darkTabBarTabBackgroundActive(38, 38, 38); -static const QColor tabBarTabBackgroundActive() { return isDarkMode() ? darkTabBarTabBackgroundActive : lightTabBarTabBackgroundActive; } - -static const QColor lightTabBarTabBackgroundActiveHovered(178, 178, 178); -static const QColor darkTabBarTabBackgroundActiveHovered(32, 32, 32); -static const QColor tabBarTabBackgroundActiveHovered() { return isDarkMode() ? darkTabBarTabBackgroundActiveHovered : lightTabBarTabBackgroundActiveHovered; } - -static const QColor lightTabBarTabBackgroundActiveSelected(211, 211, 211); -static const QColor darkTabBarTabBackgroundActiveSelected(52, 52, 52); -static const QColor tabBarTabBackgroundActiveSelected() { return isDarkMode() ? darkTabBarTabBackgroundActiveSelected : lightTabBarTabBackgroundActiveSelected; } - -static const QColor lightTabBarTabBackground(227, 227, 227); -static const QColor darkTabBarTabBackground(38, 38, 38); -static const QColor tabBarTabBackground() { return isDarkMode() ? darkTabBarTabBackground : lightTabBarTabBackground; } - -static const QColor lightTabBarTabBackgroundSelected(246, 246, 246); -static const QColor darkTabBarTabBackgroundSelected(52, 52, 52); -static const QColor tabBarTabBackgroundSelected() { return isDarkMode() ? darkTabBarTabBackgroundSelected : lightTabBarTabBackgroundSelected; } - -static const QColor lightTabBarTabLineActive(160, 160, 160); -static const QColor darkTabBarTabLineActive(90, 90, 90); -static const QColor tabBarTabLineActive() { return isDarkMode() ? darkTabBarTabLineActive : lightTabBarTabLineActive; } - -static const QColor lightTabBarTabLineActiveHovered(150, 150, 150); -static const QColor darkTabBarTabLineActiveHovered(90, 90, 90); -static const QColor tabBarTabLineActiveHovered() { return isDarkMode() ? darkTabBarTabLineActiveHovered : lightTabBarTabLineActiveHovered; } - -static const QColor lightTabBarTabLine(210, 210, 210); -static const QColor darkTabBarTabLine(90, 90, 90); -static const QColor tabBarTabLine() { return isDarkMode() ? darkTabBarTabLine : lightTabBarTabLine; } - -static const QColor lightTabBarTabLineSelected(189, 189, 189); -static const QColor darkTabBarTabLineSelected(90, 90, 90); -static const QColor tabBarTabLineSelected() { return isDarkMode() ? darkTabBarTabLineSelected : lightTabBarTabLineSelected; } - -static const QColor tabBarCloseButtonBackgroundHovered(162, 162, 162); -static const QColor tabBarCloseButtonBackgroundPressed(153, 153, 153); -static const QColor tabBarCloseButtonBackgroundSelectedHovered(192, 192, 192); -static const QColor tabBarCloseButtonBackgroundSelectedPressed(181, 181, 181); -static const QColor tabBarCloseButtonCross(100, 100, 100); -static const QColor tabBarCloseButtonCrossSelected(115, 115, 115); - -static const int closeButtonSize = 14; -static const qreal closeButtonCornerRadius = 2.0; - -static const int headerSectionArrowHeight = 6; -static const int headerSectionSeparatorInset = 2; - -// One for each of QStyleHelper::WidgetSizePolicy -static const QMarginsF comboBoxFocusRingMargins[3] = { - { 0.5, 2, 3.5, 4 }, - { 0.5, 1, 2.5, 4 }, - { 0.5, 1.5, 2.5, 3.5 } -}; - -static const QMarginsF pullDownButtonShadowMargins[3] = { - { 0.5, -1, 0.5, 2 }, - { 0.5, -1.5, 0.5, 2.5 }, - { 0.5, 0, 0.5, 1 } -}; - -static const QMarginsF pushButtonShadowMargins[3] = { - { 1.5, -1.5, 1.5, 4.5 }, - { 1.5, -1, 1.5, 4 }, - { 1.5, 0.5, 1.5, 2.5 } -}; - -// These are frame heights as reported by Xcode 9's Interface Builder. -// Alignemnet rectangle's heights match for push and popup buttons -// with respective values 21, 18 and 15. - -static const qreal comboBoxDefaultHeight[3] = { - 26, 22, 19 -}; - -static const qreal pushButtonDefaultHeight[3] = { - 32, 28, 16 -}; - -static const qreal popupButtonDefaultHeight[3] = { - 26, 22, 15 -}; - -static const int toolButtonArrowSize = 7; -static const int toolButtonArrowMargin = 2; - -static const qreal focusRingWidth = 3.5; - -static bool setupScroller(NSScroller *scroller, const QStyleOptionSlider *sb) -{ - const qreal length = sb->maximum - sb->minimum + sb->pageStep; - if (qFuzzyIsNull(length)) - return false; - const qreal proportion = sb->pageStep / length; - const qreal range = qreal(sb->maximum - sb->minimum); - qreal value = range ? qreal(sb->sliderValue - sb->minimum) / range : 0; - if (sb->orientation == Qt::Horizontal && sb->direction == Qt::RightToLeft) - value = 1.0 - value; - - scroller.frame = sb->rect.toCGRect(); - scroller.floatValue = value; - scroller.knobProportion = proportion; - return true; -} - -static bool setupSlider(NSSlider *slider, const QStyleOptionSlider *sl) -{ - if (sl->minimum >= sl->maximum) - return false; - - slider.frame = sl->rect.toCGRect(); - slider.minValue = sl->minimum; - slider.maxValue = sl->maximum; - slider.intValue = sl->sliderPosition; - slider.enabled = sl->state & QStyle::State_Enabled; - if (sl->tickPosition != QSlider::NoTicks) { - // Set numberOfTickMarks, but TicksBothSides will be treated differently - int interval = sl->tickInterval; - if (interval == 0) { - interval = sl->pageStep; - if (interval == 0) - interval = sl->singleStep; - if (interval == 0) - interval = 1; // return false? - } - slider.numberOfTickMarks = 1 + ((sl->maximum - sl->minimum) / interval); - - const bool ticksAbove = sl->tickPosition == QSlider::TicksAbove; - if (sl->orientation == Qt::Horizontal) - slider.tickMarkPosition = ticksAbove ? NSTickMarkPositionAbove : NSTickMarkPositionBelow; - else - slider.tickMarkPosition = ticksAbove ? NSTickMarkPositionLeading : NSTickMarkPositionTrailing; - } else { - slider.numberOfTickMarks = 0; - } - - return true; -} - -static bool isInMacUnifiedToolbarArea(QWindow *window, int windowY) -{ - QPlatformNativeInterface *nativeInterface = QGuiApplication::platformNativeInterface(); - QPlatformNativeInterface::NativeResourceForIntegrationFunction function = - nativeInterface->nativeResourceFunctionForIntegration("testContentBorderPosition"); - if (!function) - return false; // Not Cocoa platform plugin. - - typedef bool (*TestContentBorderPositionFunction)(QWindow *, int); - return (reinterpret_cast(function))(window, windowY); -} - - -static void drawTabCloseButton(QPainter *p, bool hover, bool selected, bool pressed, bool documentMode) -{ - p->setRenderHints(QPainter::Antialiasing); - QRect rect(0, 0, closeButtonSize, closeButtonSize); - const int width = rect.width(); - const int height = rect.height(); - - if (hover) { - // draw background circle - QColor background; - if (selected) { - if (documentMode) - background = pressed ? tabBarCloseButtonBackgroundSelectedPressed : tabBarCloseButtonBackgroundSelectedHovered; - else - background = QColor(255, 255, 255, pressed ? 150 : 100); // Translucent white - } else { - background = pressed ? tabBarCloseButtonBackgroundPressed : tabBarCloseButtonBackgroundHovered; - if (!documentMode) - background = background.lighter(pressed ? 135 : 140); // Lighter tab background, lighter color - } - - p->setPen(Qt::transparent); - p->setBrush(background); - p->drawRoundedRect(rect, closeButtonCornerRadius, closeButtonCornerRadius); - } - - // draw cross - const int margin = 3; - QPen crossPen; - crossPen.setColor(selected ? (documentMode ? tabBarCloseButtonCrossSelected : Qt::white) : tabBarCloseButtonCross); - crossPen.setWidthF(1.1); - crossPen.setCapStyle(Qt::FlatCap); - p->setPen(crossPen); - p->drawLine(margin, margin, width - margin, height - margin); - p->drawLine(margin, height - margin, width - margin, margin); -} - -#if QT_CONFIG(tabbar) -QRect rotateTabPainter(QPainter *p, QTabBar::Shape shape, QRect tabRect) -{ - const auto tabDirection = QMacStylePrivate::tabDirection(shape); - if (QMacStylePrivate::verticalTabs(tabDirection)) { - int newX, newY, newRot; - if (tabDirection == QMacStylePrivate::East) { - newX = tabRect.width(); - newY = tabRect.y(); - newRot = 90; - } else { - newX = 0; - newY = tabRect.y() + tabRect.height(); - newRot = -90; - } - tabRect.setRect(0, 0, tabRect.height(), tabRect.width()); - QMatrix m; - m.translate(newX, newY); - m.rotate(newRot); - p->setMatrix(m, true); - } - return tabRect; -} - -void drawTabShape(QPainter *p, const QStyleOptionTab *tabOpt, bool isUnified, int tabOverlap) -{ - QRect rect = tabOpt->rect; - if (QMacStylePrivate::verticalTabs(QMacStylePrivate::tabDirection(tabOpt->shape))) - rect = rect.adjusted(-tabOverlap, 0, 0, 0); - else - rect = rect.adjusted(0, -tabOverlap, 0, 0); - - p->translate(rect.x(), rect.y()); - rect.moveLeft(0); - rect.moveTop(0); - const QRect tabRect = rotateTabPainter(p, tabOpt->shape, rect); - - const int width = tabRect.width(); - const int height = tabRect.height(); - const bool active = (tabOpt->state & QStyle::State_Active); - const bool selected = (tabOpt->state & QStyle::State_Selected); - - const QRect bodyRect(1, 2, width - 2, height - 3); - const QRect topLineRect(1, 0, width - 2, 1); - const QRect bottomLineRect(1, height - 1, width - 2, 1); - if (selected) { - // fill body - if (tabOpt->documentMode && isUnified) { - p->save(); - p->setCompositionMode(QPainter::CompositionMode_Source); - p->fillRect(tabRect, QColor(Qt::transparent)); - p->restore(); - } else if (active) { - p->fillRect(bodyRect, tabBarTabBackgroundActiveSelected()); - // top line - p->fillRect(topLineRect, tabBarTabLineSelected()); - } else { - p->fillRect(bodyRect, tabBarTabBackgroundSelected()); - } - } else { - // when the mouse is over non selected tabs they get a new color - const bool hover = (tabOpt->state & QStyle::State_MouseOver); - if (hover) { - // fill body - p->fillRect(bodyRect, tabBarTabBackgroundActiveHovered()); - // bottom line - p->fillRect(bottomLineRect, isDarkMode() ? QColor(Qt::black) : tabBarTabLineActiveHovered()); - } - } - - // separator lines between tabs - const QRect leftLineRect(0, 1, 1, height - 2); - const QRect rightLineRect(width - 1, 1, 1, height - 2); - const QColor separatorLineColor = active ? tabBarTabLineActive() : tabBarTabLine(); - p->fillRect(leftLineRect, separatorLineColor); - p->fillRect(rightLineRect, separatorLineColor); -} - -void drawTabBase(QPainter *p, const QStyleOptionTabBarBase *tbb, const QWidget *w) -{ - QRect r = tbb->rect; - if (QMacStylePrivate::verticalTabs(QMacStylePrivate::tabDirection(tbb->shape))) - r.setWidth(w->width()); - else - r.setHeight(w->height()); - - const QRect tabRect = rotateTabPainter(p, tbb->shape, r); - const int width = tabRect.width(); - const int height = tabRect.height(); - const bool active = (tbb->state & QStyle::State_Active); - - // fill body - const QRect bodyRect(0, 1, width, height - 1); - const QColor bodyColor = active ? tabBarTabBackgroundActive() : tabBarTabBackground(); - p->fillRect(bodyRect, bodyColor); - - // top line - const QRect topLineRect(0, 0, width, 1); - const QColor topLineColor = active ? tabBarTabLineActive() : tabBarTabLine(); - p->fillRect(topLineRect, topLineColor); - - // bottom line - const QRect bottomLineRect(0, height - 1, width, 1); - bool isDocument = false; - if (const QTabBar *tabBar = qobject_cast(w)) - isDocument = tabBar->documentMode(); - const QColor bottomLineColor = isDocument && isDarkMode() ? QColor(Qt::black) : active ? tabBarTabLineActive() : tabBarTabLine(); - p->fillRect(bottomLineRect, bottomLineColor); -} -#endif - -static QStyleHelper::WidgetSizePolicy getControlSize(const QStyleOption *option, const QWidget *widget) -{ - const auto wsp = QStyleHelper::widgetSizePolicy(widget, option); - if (wsp == QStyleHelper::SizeDefault) - return QStyleHelper::SizeLarge; - - return wsp; -} - -#if QT_CONFIG(treeview) -static inline bool isTreeView(const QWidget *widget) -{ - return (widget && widget->parentWidget() && - qobject_cast(widget->parentWidget())); -} -#endif - -static QString qt_mac_removeMnemonics(const QString &original) -{ - QString returnText(original.size(), 0); - int finalDest = 0; - int currPos = 0; - int l = original.length(); - while (l) { - if (original.at(currPos) == QLatin1Char('&')) { - ++currPos; - --l; - if (l == 0) - break; - } else if (original.at(currPos) == QLatin1Char('(') && l >= 4 && - original.at(currPos + 1) == QLatin1Char('&') && - original.at(currPos + 2) != QLatin1Char('&') && - original.at(currPos + 3) == QLatin1Char(')')) { - /* remove mnemonics its format is "\s*(&X)" */ - int n = 0; - while (finalDest > n && returnText.at(finalDest - n - 1).isSpace()) - ++n; - finalDest -= n; - currPos += 4; - l -= 4; - continue; - } - returnText[finalDest] = original.at(currPos); - ++currPos; - ++finalDest; - --l; - } - returnText.truncate(finalDest); - return returnText; -} - -static bool qt_macWindowMainWindow(const QWidget *window) -{ - if (QWindow *w = window->windowHandle()) { - if (w->handle()) { - if (NSWindow *nswindow = static_cast(QGuiApplication::platformNativeInterface()->nativeResourceForWindow(QByteArrayLiteral("nswindow"), w))) { - return [nswindow isMainWindow]; - } - } - } - return false; -} - -/***************************************************************************** - QMacCGStyle globals - *****************************************************************************/ -const int macItemFrame = 2; // menu item frame width -const int macItemHMargin = 3; // menu item hor text margin -const int macRightBorder = 12; // right border on mac - -/***************************************************************************** - QMacCGStyle utility functions - *****************************************************************************/ - -enum QAquaMetric { - // Prepend kThemeMetric to get the HIToolBox constant. - // Represents the values already used in QMacStyle. - CheckBoxHeight = 0, - CheckBoxWidth, - EditTextFrameOutset, - FocusRectOutset, - HSliderHeight, - HSliderTickHeight, - LargeProgressBarThickness, - ListHeaderHeight, - MenuSeparatorHeight, // GetThemeMenuSeparatorHeight - MiniCheckBoxHeight, - MiniCheckBoxWidth, - MiniHSliderHeight, - MiniHSliderTickHeight, - MiniPopupButtonHeight, - MiniPushButtonHeight, - MiniRadioButtonHeight, - MiniRadioButtonWidth, - MiniVSliderTickWidth, - MiniVSliderWidth, - NormalProgressBarThickness, - PopupButtonHeight, - ProgressBarShadowOutset, - PushButtonHeight, - RadioButtonHeight, - RadioButtonWidth, - SeparatorSize, - SmallCheckBoxHeight, - SmallCheckBoxWidth, - SmallHSliderHeight, - SmallHSliderTickHeight, - SmallPopupButtonHeight, - SmallProgressBarShadowOutset, - SmallPushButtonHeight, - SmallRadioButtonHeight, - SmallRadioButtonWidth, - SmallVSliderTickWidth, - SmallVSliderWidth, - VSliderTickWidth, - VSliderWidth -}; - -static const int qt_mac_aqua_metrics[] = { - // Values as of macOS 10.12.4 and Xcode 8.3.1 - 18 /* CheckBoxHeight */, - 18 /* CheckBoxWidth */, - 1 /* EditTextFrameOutset */, - 4 /* FocusRectOutset */, - 22 /* HSliderHeight */, - 5 /* HSliderTickHeight */, - 16 /* LargeProgressBarThickness */, - 17 /* ListHeaderHeight */, - 12 /* MenuSeparatorHeight, aka GetThemeMenuSeparatorHeight */, - 11 /* MiniCheckBoxHeight */, - 10 /* MiniCheckBoxWidth */, - 12 /* MiniHSliderHeight */, - 4 /* MiniHSliderTickHeight */, - 15 /* MiniPopupButtonHeight */, - 16 /* MiniPushButtonHeight */, - 11 /* MiniRadioButtonHeight */, - 10 /* MiniRadioButtonWidth */, - 4 /* MiniVSliderTickWidth */, - 12 /* MiniVSliderWidth */, - 12 /* NormalProgressBarThickness */, - 20 /* PopupButtonHeight */, - 4 /* ProgressBarShadowOutset */, - 20 /* PushButtonHeight */, - 18 /* RadioButtonHeight */, - 18 /* RadioButtonWidth */, - 1 /* SeparatorSize */, - 16 /* SmallCheckBoxHeight */, - 14 /* SmallCheckBoxWidth */, - 15 /* SmallHSliderHeight */, - 4 /* SmallHSliderTickHeight */, - 17 /* SmallPopupButtonHeight */, - 2 /* SmallProgressBarShadowOutset */, - 17 /* SmallPushButtonHeight */, - 15 /* SmallRadioButtonHeight */, - 14 /* SmallRadioButtonWidth */, - 4 /* SmallVSliderTickWidth */, - 15 /* SmallVSliderWidth */, - 5 /* VSliderTickWidth */, - 22 /* VSliderWidth */ -}; - -static inline int qt_mac_aqua_get_metric(QAquaMetric m) -{ - return qt_mac_aqua_metrics[m]; -} - -static QSize qt_aqua_get_known_size(QStyle::ContentsType ct, const QWidget *widg, QSize szHint, - QStyleHelper::WidgetSizePolicy sz) -{ - QSize ret(-1, -1); - if (sz != QStyleHelper::SizeSmall && sz != QStyleHelper::SizeLarge && sz != QStyleHelper::SizeMini) { - qDebug("Not sure how to return this..."); - return ret; - } - if ((widg && widg->testAttribute(Qt::WA_SetFont)) || !QApplication::desktopSettingsAware()) { - // If you're using a custom font and it's bigger than the default font, - // then no constraints for you. If you are smaller, we can try to help you out - QFont font = qt_app_fonts_hash()->value(widg->metaObject()->className(), QFont()); - if (widg->font().pointSize() > font.pointSize()) - return ret; - } - - if (ct == QStyle::CT_CustomBase && widg) { -#if QT_CONFIG(pushbutton) - if (qobject_cast(widg)) - ct = QStyle::CT_PushButton; -#endif - else if (qobject_cast(widg)) - ct = QStyle::CT_RadioButton; -#if QT_CONFIG(checkbox) - else if (qobject_cast(widg)) - ct = QStyle::CT_CheckBox; -#endif -#if QT_CONFIG(combobox) - else if (qobject_cast(widg)) - ct = QStyle::CT_ComboBox; -#endif -#if QT_CONFIG(toolbutton) - else if (qobject_cast(widg)) - ct = QStyle::CT_ToolButton; -#endif - else if (qobject_cast(widg)) - ct = QStyle::CT_Slider; -#if QT_CONFIG(progressbar) - else if (qobject_cast(widg)) - ct = QStyle::CT_ProgressBar; -#endif -#if QT_CONFIG(lineedit) - else if (qobject_cast(widg)) - ct = QStyle::CT_LineEdit; -#endif - else if (qobject_cast(widg)) - ct = QStyle::CT_HeaderSection; -#if QT_CONFIG(menubar) - else if (qobject_cast(widg)) - ct = QStyle::CT_MenuBar; -#endif -#if QT_CONFIG(sizegrip) - else if (qobject_cast(widg)) - ct = QStyle::CT_SizeGrip; -#endif - else - return ret; - } - - switch (ct) { -#if QT_CONFIG(pushbutton) - case QStyle::CT_PushButton: { - const QPushButton *psh = qobject_cast(widg); - // If this comparison is false, then the widget was not a push button. - // This is bad and there's very little we can do since we were requested to find a - // sensible size for a widget that pretends to be a QPushButton but is not. - if(psh) { - QString buttonText = qt_mac_removeMnemonics(psh->text()); - if (buttonText.contains(QLatin1Char('\n'))) - ret = QSize(-1, -1); - else if (sz == QStyleHelper::SizeLarge) - ret = QSize(-1, qt_mac_aqua_get_metric(PushButtonHeight)); - else if (sz == QStyleHelper::SizeSmall) - ret = QSize(-1, qt_mac_aqua_get_metric(SmallPushButtonHeight)); - else if (sz == QStyleHelper::SizeMini) - ret = QSize(-1, qt_mac_aqua_get_metric(MiniPushButtonHeight)); - - if (!psh->icon().isNull()){ - // If the button got an icon, and the icon is larger than the - // button, we can't decide on a default size - ret.setWidth(-1); - if (ret.height() < psh->iconSize().height()) - ret.setHeight(-1); - } - else if (buttonText == QLatin1String("OK") || buttonText == QLatin1String("Cancel")){ - // Aqua Style guidelines restrict the size of OK and Cancel buttons to 68 pixels. - // However, this doesn't work for German, therefore only do it for English, - // I suppose it would be better to do some sort of lookups for languages - // that like to have really long words. - // FIXME This is not exactly true. Out of context, OK buttons have their - // implicit size calculated the same way as any other button. Inside a - // QDialogButtonBox, their size should be calculated such that the action - // or accept button (i.e., rightmost) and cancel button have the same width. - ret.setWidth(69); - } - } else { - // The only sensible thing to do is to return whatever the style suggests... - if (sz == QStyleHelper::SizeLarge) - ret = QSize(-1, qt_mac_aqua_get_metric(PushButtonHeight)); - else if (sz == QStyleHelper::SizeSmall) - ret = QSize(-1, qt_mac_aqua_get_metric(SmallPushButtonHeight)); - else if (sz == QStyleHelper::SizeMini) - ret = QSize(-1, qt_mac_aqua_get_metric(MiniPushButtonHeight)); - else - // Since there's no default size we return the large size... - ret = QSize(-1, qt_mac_aqua_get_metric(PushButtonHeight)); - } -#endif -#if 0 //Not sure we are applying the rules correctly for RadioButtons/CheckBoxes --Sam - } else if (ct == QStyle::CT_RadioButton) { - QRadioButton *rdo = static_cast(widg); - // Exception for case where multiline radio button text requires no size constrainment - if (rdo->text().find('\n') != -1) - return ret; - if (sz == QStyleHelper::SizeLarge) - ret = QSize(-1, qt_mac_aqua_get_metric(RadioButtonHeight)); - else if (sz == QStyleHelper::SizeSmall) - ret = QSize(-1, qt_mac_aqua_get_metric(SmallRadioButtonHeight)); - else if (sz == QStyleHelper::SizeMini) - ret = QSize(-1, qt_mac_aqua_get_metric(MiniRadioButtonHeight)); - } else if (ct == QStyle::CT_CheckBox) { - if (sz == QStyleHelper::SizeLarge) - ret = QSize(-1, qt_mac_aqua_get_metric(CheckBoxHeight)); - else if (sz == QStyleHelper::SizeSmall) - ret = QSize(-1, qt_mac_aqua_get_metric(SmallCheckBoxHeight)); - else if (sz == QStyleHelper::SizeMini) - ret = QSize(-1, qt_mac_aqua_get_metric(MiniCheckBoxHeight)); -#endif - break; - } - case QStyle::CT_SizeGrip: - // Not HIG kosher: mimic what we were doing earlier until we support 4-edge resizing in MDI subwindows - if (sz == QStyleHelper::SizeLarge || sz == QStyleHelper::SizeSmall) { - int s = sz == QStyleHelper::SizeSmall ? 16 : 22; // large: pixel measured from HITheme, small: from my hat - int width = 0; -#if QT_CONFIG(mdiarea) - if (widg && qobject_cast(widg->parentWidget())) - width = s; -#endif - ret = QSize(width, s); - } - break; - case QStyle::CT_ComboBox: - switch (sz) { - case QStyleHelper::SizeLarge: - ret = QSize(-1, qt_mac_aqua_get_metric(PopupButtonHeight)); - break; - case QStyleHelper::SizeSmall: - ret = QSize(-1, qt_mac_aqua_get_metric(SmallPopupButtonHeight)); - break; - case QStyleHelper::SizeMini: - ret = QSize(-1, qt_mac_aqua_get_metric(MiniPopupButtonHeight)); - break; - default: - break; - } - break; - case QStyle::CT_ToolButton: - if (sz == QStyleHelper::SizeSmall) { - int width = 0, height = 0; - if (szHint == QSize(-1, -1)) { //just 'guess'.. -#if QT_CONFIG(toolbutton) - const QToolButton *bt = qobject_cast(widg); - // If this conversion fails then the widget was not what it claimed to be. - if(bt) { - if (!bt->icon().isNull()) { - QSize iconSize = bt->iconSize(); - QSize pmSize = bt->icon().actualSize(QSize(32, 32), QIcon::Normal); - width = qMax(width, qMax(iconSize.width(), pmSize.width())); - height = qMax(height, qMax(iconSize.height(), pmSize.height())); - } - if (!bt->text().isNull() && bt->toolButtonStyle() != Qt::ToolButtonIconOnly) { - int text_width = bt->fontMetrics().horizontalAdvance(bt->text()), - text_height = bt->fontMetrics().height(); - if (bt->toolButtonStyle() == Qt::ToolButtonTextUnderIcon) { - width = qMax(width, text_width); - height += text_height; - } else { - width += text_width; - width = qMax(height, text_height); - } - } - } else -#endif - { - // Let's return the size hint... - width = szHint.width(); - height = szHint.height(); - } - } else { - width = szHint.width(); - height = szHint.height(); - } - width = qMax(20, width + 5); //border - height = qMax(20, height + 5); //border - ret = QSize(width, height); - } - break; - case QStyle::CT_Slider: { - int w = -1; - const QSlider *sld = qobject_cast(widg); - // If this conversion fails then the widget was not what it claimed to be. - if(sld) { - if (sz == QStyleHelper::SizeLarge) { - if (sld->orientation() == Qt::Horizontal) { - w = qt_mac_aqua_get_metric(HSliderHeight); - if (sld->tickPosition() != QSlider::NoTicks) - w += qt_mac_aqua_get_metric(HSliderTickHeight); - } else { - w = qt_mac_aqua_get_metric(VSliderWidth); - if (sld->tickPosition() != QSlider::NoTicks) - w += qt_mac_aqua_get_metric(VSliderTickWidth); - } - } else if (sz == QStyleHelper::SizeSmall) { - if (sld->orientation() == Qt::Horizontal) { - w = qt_mac_aqua_get_metric(SmallHSliderHeight); - if (sld->tickPosition() != QSlider::NoTicks) - w += qt_mac_aqua_get_metric(SmallHSliderTickHeight); - } else { - w = qt_mac_aqua_get_metric(SmallVSliderWidth); - if (sld->tickPosition() != QSlider::NoTicks) - w += qt_mac_aqua_get_metric(SmallVSliderTickWidth); - } - } else if (sz == QStyleHelper::SizeMini) { - if (sld->orientation() == Qt::Horizontal) { - w = qt_mac_aqua_get_metric(MiniHSliderHeight); - if (sld->tickPosition() != QSlider::NoTicks) - w += qt_mac_aqua_get_metric(MiniHSliderTickHeight); - } else { - w = qt_mac_aqua_get_metric(MiniVSliderWidth); - if (sld->tickPosition() != QSlider::NoTicks) - w += qt_mac_aqua_get_metric(MiniVSliderTickWidth); - } - } - } else { - // This is tricky, we were requested to find a size for a slider which is not - // a slider. We don't know if this is vertical or horizontal or if we need to - // have tick marks or not. - // For this case we will return an horizontal slider without tick marks. - w = qt_mac_aqua_get_metric(HSliderHeight); - w += qt_mac_aqua_get_metric(HSliderTickHeight); - } - if (sld->orientation() == Qt::Horizontal) - ret.setHeight(w); - else - ret.setWidth(w); - break; - } -#if QT_CONFIG(progressbar) - case QStyle::CT_ProgressBar: { - int finalValue = -1; - Qt::Orientation orient = Qt::Horizontal; - if (const QProgressBar *pb = qobject_cast(widg)) - orient = pb->orientation(); - - if (sz == QStyleHelper::SizeLarge) - finalValue = qt_mac_aqua_get_metric(LargeProgressBarThickness) - + qt_mac_aqua_get_metric(ProgressBarShadowOutset); - else - finalValue = qt_mac_aqua_get_metric(NormalProgressBarThickness) - + qt_mac_aqua_get_metric(SmallProgressBarShadowOutset); - if (orient == Qt::Horizontal) - ret.setHeight(finalValue); - else - ret.setWidth(finalValue); - break; - } -#endif -#if QT_CONFIG(combobox) - case QStyle::CT_LineEdit: - if (!widg || !qobject_cast(widg->parentWidget())) { - //should I take into account the font dimentions of the lineedit? -Sam - if (sz == QStyleHelper::SizeLarge) - ret = QSize(-1, 21); - else - ret = QSize(-1, 19); - } - break; -#endif - case QStyle::CT_HeaderSection: -#if QT_CONFIG(treeview) - if (isTreeView(widg)) - ret = QSize(-1, qt_mac_aqua_get_metric(ListHeaderHeight)); -#endif - break; - case QStyle::CT_MenuBar: - if (sz == QStyleHelper::SizeLarge) { - ret = QSize(-1, [[NSApp mainMenu] menuBarHeight]); - // In the qt_mac_set_native_menubar(false) case, - // we come it here with a zero-height main menu, - // preventing the in-window menu from displaying. - // Use 22 pixels for the height, by observation. - if (ret.height() <= 0) - ret.setHeight(22); - } - break; - default: - break; - } - return ret; -} - - -#if defined(QMAC_QAQUASTYLE_SIZE_CONSTRAIN) || defined(DEBUG_SIZE_CONSTRAINT) -static QStyleHelper::WidgetSizePolicy qt_aqua_guess_size(const QWidget *widg, QSize large, QSize small, QSize mini) -{ - Q_UNUSED(widg); - - if (large == QSize(-1, -1)) { - if (small != QSize(-1, -1)) - return QStyleHelper::SizeSmall; - if (mini != QSize(-1, -1)) - return QStyleHelper::SizeMini; - return QStyleHelper::SizeDefault; - } else if (small == QSize(-1, -1)) { - if (mini != QSize(-1, -1)) - return QStyleHelper::SizeMini; - return QStyleHelper::SizeLarge; - } else if (mini == QSize(-1, -1)) { - return QStyleHelper::SizeLarge; - } - - if (qEnvironmentVariableIsSet("QWIDGET_ALL_SMALL")) - return QStyleHelper::SizeSmall; - else if (qEnvironmentVariableIsSet("QWIDGET_ALL_MINI")) - return QStyleHelper::SizeMini; - - return QStyleHelper::SizeLarge; -} -#endif - -static NSColor *qt_convertColorForContext(CGContextRef context, NSColor *color) -{ - Q_ASSERT(color); - Q_ASSERT(context); - - CGColorSpaceRef targetCGColorSpace = CGBitmapContextGetColorSpace(context); - NSColorSpace *targetNSColorSpace = [[NSColorSpace alloc] initWithCGColorSpace:targetCGColorSpace]; - NSColor *adjusted = [color colorUsingColorSpace:targetNSColorSpace]; - [targetNSColorSpace release]; - - return adjusted; -} - -static NSColor *qt_colorForContext(CGContextRef context, const CGFloat (&rgba)[4]) -{ - Q_ASSERT(context); - - auto colorSpace = CGBitmapContextGetColorSpace(context); - if (!colorSpace) - return nil; - - return qt_convertColorForContext(context, [NSColor colorWithSRGBRed:rgba[0] green:rgba[1] blue:rgba[2] alpha:rgba[3]]); -} - -static void qt_drawDisclosureButton(CGContextRef context, NSInteger state, bool selected, CGRect rect) -{ - Q_ASSERT(context); - - static const CGFloat gray[] = {0.55, 0.55, 0.55, 0.97}; - static const CGFloat white[] = {1.0, 1.0, 1.0, 0.9}; - - NSColor *fillColor = qt_colorForContext(context, selected ? white : gray); - [fillColor setFill]; - - if (state == NSOffState) { - static NSBezierPath *triangle = [[NSBezierPath alloc] init]; - [triangle removeAllPoints]; - // In off state, a disclosure button is an equilateral triangle - // ('pointing' to the right) with a bound rect that can be described - // as NSMakeRect(0, 0, 8, 9). Inside the 'rect' it's translated by - // (2, 4). - [triangle moveToPoint:NSMakePoint(rect.origin.x + 2, rect.origin.y + 4)]; - [triangle lineToPoint:NSMakePoint(rect.origin.x + 2, rect.origin.y + 4 + 9)]; - [triangle lineToPoint:NSMakePoint(rect.origin.x + 2 + 8, rect.origin.y + 4 + 4.5)]; - [triangle closePath]; - [triangle fill]; - } else { - static NSBezierPath *openTriangle = [[NSBezierPath alloc] init]; - [openTriangle removeAllPoints]; - // In 'on' state, the button is an equilateral triangle (looking down) - // with the bounding rect NSMakeRect(0, 0, 9, 8). Inside the 'rect' - // it's translated by (1, 4). - [openTriangle moveToPoint:NSMakePoint(rect.origin.x + 1, rect.origin.y + 4 + 8)]; - [openTriangle lineToPoint:NSMakePoint(rect.origin.x + 1 + 9, rect.origin.y + 4 + 8)]; - [openTriangle lineToPoint:NSMakePoint(rect.origin.x + 1 + 4.5, rect.origin.y + 4)]; - [openTriangle closePath]; - [openTriangle fill]; - } -} - -void QMacStylePrivate::drawFocusRing(QPainter *p, const QRectF &targetRect, int hMargin, int vMargin, const CocoaControl &cw) const -{ - QPainterPath focusRingPath; - focusRingPath.setFillRule(Qt::OddEvenFill); - - qreal hOffset = 0.0; - qreal vOffset = 0.0; - switch (cw.type) { - case Box: - case Button_SquareButton: - case SegmentedControl_Middle: - case TextField: { - auto innerRect = targetRect; - if (cw.type == TextField) - innerRect = innerRect.adjusted(hMargin, vMargin, -hMargin, -vMargin).adjusted(0.5, 0.5, -0.5, -0.5); - const auto outerRect = innerRect.adjusted(-focusRingWidth, -focusRingWidth, focusRingWidth, focusRingWidth); - const auto outerRadius = focusRingWidth; - focusRingPath.addRect(innerRect); - focusRingPath.addRoundedRect(outerRect, outerRadius, outerRadius); - break; - } - case Button_CheckBox: { - const auto cbInnerRadius = (cw.size == QStyleHelper::SizeMini ? 2.0 : 3.0); - const auto cbSize = cw.size == QStyleHelper::SizeLarge ? 13 : - cw.size == QStyleHelper::SizeSmall ? 11 : 9; // As measured - hOffset = hMargin + (cw.size == QStyleHelper::SizeLarge ? 2.5 : - cw.size == QStyleHelper::SizeSmall ? 2.0 : 1.0); // As measured - vOffset = 0.5 * qreal(targetRect.height() - cbSize); - const auto cbInnerRect = QRectF(0, 0, cbSize, cbSize); - const auto cbOuterRadius = cbInnerRadius + focusRingWidth; - const auto cbOuterRect = cbInnerRect.adjusted(-focusRingWidth, -focusRingWidth, focusRingWidth, focusRingWidth); - focusRingPath.addRoundedRect(cbOuterRect, cbOuterRadius, cbOuterRadius); - focusRingPath.addRoundedRect(cbInnerRect, cbInnerRadius, cbInnerRadius); - break; - } - case Button_RadioButton: { - const auto rbSize = cw.size == QStyleHelper::SizeLarge ? 15 : - cw.size == QStyleHelper::SizeSmall ? 13 : 9; // As measured - hOffset = hMargin + (cw.size == QStyleHelper::SizeLarge ? 1.5 : - cw.size == QStyleHelper::SizeSmall ? 1.0 : 1.0); // As measured - vOffset = 0.5 * qreal(targetRect.height() - rbSize); - const auto rbInnerRect = QRectF(0, 0, rbSize, rbSize); - const auto rbOuterRect = rbInnerRect.adjusted(-focusRingWidth, -focusRingWidth, focusRingWidth, focusRingWidth); - focusRingPath.addEllipse(rbInnerRect); - focusRingPath.addEllipse(rbOuterRect); - break; - } - case Button_PopupButton: - case Button_PullDown: - case Button_PushButton: - case SegmentedControl_Single: { - const qreal innerRadius = cw.type == Button_PushButton ? 3 : 4; - const qreal outerRadius = innerRadius + focusRingWidth; - hOffset = targetRect.left(); - vOffset = targetRect.top(); - const auto innerRect = targetRect.translated(-targetRect.topLeft()); - const auto outerRect = innerRect.adjusted(-hMargin, -vMargin, hMargin, vMargin); - focusRingPath.addRoundedRect(innerRect, innerRadius, innerRadius); - focusRingPath.addRoundedRect(outerRect, outerRadius, outerRadius); - break; - } - case ComboBox: - case SegmentedControl_First: - case SegmentedControl_Last: { - hOffset = targetRect.left(); - vOffset = targetRect.top(); - const qreal innerRadius = 8; - const qreal outerRadius = innerRadius + focusRingWidth; - const auto innerRect = targetRect.translated(-targetRect.topLeft()); - const auto outerRect = innerRect.adjusted(-hMargin, -vMargin, hMargin, vMargin); - - const auto cbFocusFramePath = [](const QRectF &rect, qreal tRadius, qreal bRadius) { - QPainterPath path; - - if (tRadius > 0) { - const auto topLeftCorner = QRectF(rect.topLeft(), QSizeF(tRadius, tRadius)); - path.arcMoveTo(topLeftCorner, 180); - path.arcTo(topLeftCorner, 180, -90); - } else { - path.moveTo(rect.topLeft()); - } - const auto rightEdge = rect.right() - bRadius; - path.arcTo(rightEdge, rect.top(), bRadius, bRadius, 90, -90); - path.arcTo(rightEdge, rect.bottom() - bRadius, bRadius, bRadius, 0, -90); - if (tRadius > 0) - path.arcTo(rect.left(), rect.bottom() - tRadius, tRadius, tRadius, 270, -90); - else - path.lineTo(rect.bottomLeft()); - path.closeSubpath(); - - return path; - }; - - const auto innerPath = cbFocusFramePath(innerRect, 0, innerRadius); - focusRingPath.addPath(innerPath); - const auto outerPath = cbFocusFramePath(outerRect, 2 * focusRingWidth, outerRadius); - focusRingPath.addPath(outerPath); - break; - } - default: - Q_UNREACHABLE(); - } - - auto focusRingColor = qt_mac_toQColor(NSColor.keyboardFocusIndicatorColor.CGColor); - if (!qt_mac_applicationIsInDarkMode()) { - // This color already has alpha ~ 0.25, this value is too small - the ring is - // very pale and nothing like the native one. 0.39 makes it better (not ideal - // anyway). The color seems to be correct in dark more without any modification. - focusRingColor.setAlphaF(0.39); - } - - p->save(); - p->setRenderHint(QPainter::Antialiasing); - - if (cw.type == SegmentedControl_First) { - // TODO Flip left-right - } - p->translate(hOffset, vOffset); - p->fillPath(focusRingPath, focusRingColor); - p->restore(); -} - -QPainterPath QMacStylePrivate::windowPanelPath(const QRectF &r) const -{ - static const qreal CornerPointOffset = 5.5; - static const qreal CornerControlOffset = 2.1; - - QPainterPath path; - // Top-left corner - path.moveTo(r.left(), r.top() + CornerPointOffset); - path.cubicTo(r.left(), r.top() + CornerControlOffset, - r.left() + CornerControlOffset, r.top(), - r.left() + CornerPointOffset, r.top()); - // Top-right corner - path.lineTo(r.right() - CornerPointOffset, r.top()); - path.cubicTo(r.right() - CornerControlOffset, r.top(), - r.right(), r.top() + CornerControlOffset, - r.right(), r.top() + CornerPointOffset); - // Bottom-right corner - path.lineTo(r.right(), r.bottom() - CornerPointOffset); - path.cubicTo(r.right(), r.bottom() - CornerControlOffset, - r.right() - CornerControlOffset, r.bottom(), - r.right() - CornerPointOffset, r.bottom()); - // Bottom-right corner - path.lineTo(r.left() + CornerPointOffset, r.bottom()); - path.cubicTo(r.left() + CornerControlOffset, r.bottom(), - r.left(), r.bottom() - CornerControlOffset, - r.left(), r.bottom() - CornerPointOffset); - path.lineTo(r.left(), r.top() + CornerPointOffset); - - return path; -} - -QMacStylePrivate::CocoaControlType QMacStylePrivate::windowButtonCocoaControl(QStyle::SubControl sc) const -{ - struct WindowButtons { - QStyle::SubControl sc; - QMacStylePrivate::CocoaControlType ct; - }; - - static const WindowButtons buttons[] = { - { QStyle::SC_TitleBarCloseButton, QMacStylePrivate::Button_WindowClose }, - { QStyle::SC_TitleBarMinButton, QMacStylePrivate::Button_WindowMiniaturize }, - { QStyle::SC_TitleBarMaxButton, QMacStylePrivate::Button_WindowZoom } - }; - - for (const auto &wb : buttons) - if (wb.sc == sc) - return wb.ct; - - return NoControl; -} - - -#if QT_CONFIG(tabbar) -void QMacStylePrivate::tabLayout(const QStyleOptionTab *opt, const QWidget *widget, QRect *textRect, QRect *iconRect) const -{ - Q_ASSERT(textRect); - Q_ASSERT(iconRect); - QRect tr = opt->rect; - const bool verticalTabs = opt->shape == QTabBar::RoundedEast - || opt->shape == QTabBar::RoundedWest - || opt->shape == QTabBar::TriangularEast - || opt->shape == QTabBar::TriangularWest; - if (verticalTabs) - tr.setRect(0, 0, tr.height(), tr.width()); // 0, 0 as we will have a translate transform - - int verticalShift = proxyStyle->pixelMetric(QStyle::PM_TabBarTabShiftVertical, opt, widget); - int horizontalShift = proxyStyle->pixelMetric(QStyle::PM_TabBarTabShiftHorizontal, opt, widget); - const int hpadding = 4; - const int vpadding = proxyStyle->pixelMetric(QStyle::PM_TabBarTabVSpace, opt, widget) / 2; - if (opt->shape == QTabBar::RoundedSouth || opt->shape == QTabBar::TriangularSouth) - verticalShift = -verticalShift; - tr.adjust(hpadding, verticalShift - vpadding, horizontalShift - hpadding, vpadding); - - // left widget - if (!opt->leftButtonSize.isEmpty()) { - const int buttonSize = verticalTabs ? opt->leftButtonSize.height() : opt->leftButtonSize.width(); - tr.setLeft(tr.left() + 4 + buttonSize); - // make text aligned to center - if (opt->rightButtonSize.isEmpty()) - tr.setRight(tr.right() - 4 - buttonSize); - } - // right widget - if (!opt->rightButtonSize.isEmpty()) { - const int buttonSize = verticalTabs ? opt->rightButtonSize.height() : opt->rightButtonSize.width(); - tr.setRight(tr.right() - 4 - buttonSize); - // make text aligned to center - if (opt->leftButtonSize.isEmpty()) - tr.setLeft(tr.left() + 4 + buttonSize); - } - - // icon - if (!opt->icon.isNull()) { - QSize iconSize = opt->iconSize; - if (!iconSize.isValid()) { - int iconExtent = proxyStyle->pixelMetric(QStyle::PM_SmallIconSize); - iconSize = QSize(iconExtent, iconExtent); - } - QSize tabIconSize = opt->icon.actualSize(iconSize, - (opt->state & QStyle::State_Enabled) ? QIcon::Normal : QIcon::Disabled, - (opt->state & QStyle::State_Selected) ? QIcon::On : QIcon::Off); - // High-dpi icons do not need adjustment; make sure tabIconSize is not larger than iconSize - tabIconSize = QSize(qMin(tabIconSize.width(), iconSize.width()), qMin(tabIconSize.height(), iconSize.height())); - - *iconRect = QRect(tr.left(), tr.center().y() - tabIconSize.height() / 2, - tabIconSize.width(), tabIconSize.height()); - if (!verticalTabs) - *iconRect = proxyStyle->visualRect(opt->direction, opt->rect, *iconRect); - - int stylePadding = proxyStyle->pixelMetric(QStyle::PM_TabBarTabHSpace, opt, widget) / 2; - stylePadding -= hpadding; - - tr.setLeft(tr.left() + stylePadding + tabIconSize.width() + 4); - tr.setRight(tr.right() - stylePadding - tabIconSize.width() - 4); - } - - if (!verticalTabs) - tr = proxyStyle->visualRect(opt->direction, opt->rect, tr); - - *textRect = tr; -} - -QMacStylePrivate::Direction QMacStylePrivate::tabDirection(QTabBar::Shape shape) -{ - switch (shape) { - case QTabBar::RoundedSouth: - case QTabBar::TriangularSouth: - return South; - case QTabBar::RoundedNorth: - case QTabBar::TriangularNorth: - return North; - case QTabBar::RoundedWest: - case QTabBar::TriangularWest: - return West; - case QTabBar::RoundedEast: - case QTabBar::TriangularEast: - return East; - } -} - -bool QMacStylePrivate::verticalTabs(QMacStylePrivate::Direction direction) -{ - return (direction == QMacStylePrivate::East - || direction == QMacStylePrivate::West); -} - -#endif // QT_CONFIG(tabbar) - -QStyleHelper::WidgetSizePolicy QMacStylePrivate::effectiveAquaSizeConstrain(const QStyleOption *option, - const QWidget *widg, - QStyle::ContentsType ct, - QSize szHint, QSize *insz) const -{ - QStyleHelper::WidgetSizePolicy sz = aquaSizeConstrain(option, widg, ct, szHint, insz); - if (sz == QStyleHelper::SizeDefault) - return QStyleHelper::SizeLarge; - return sz; -} - -QStyleHelper::WidgetSizePolicy QMacStylePrivate::aquaSizeConstrain(const QStyleOption *option, const QWidget *widg, - QStyle::ContentsType ct, QSize szHint, QSize *insz) const -{ -#if defined(QMAC_QAQUASTYLE_SIZE_CONSTRAIN) || defined(DEBUG_SIZE_CONSTRAINT) - if (option) { - if (option->state & QStyle::State_Small) - return QStyleHelper::SizeSmall; - if (option->state & QStyle::State_Mini) - return QStyleHelper::SizeMini; - } - - if (!widg) { - if (insz) - *insz = QSize(); - if (qEnvironmentVariableIsSet("QWIDGET_ALL_SMALL")) - return QStyleHelper::SizeSmall; - if (qEnvironmentVariableIsSet("QWIDGET_ALL_MINI")) - return QStyleHelper::SizeMini; - return QStyleHelper::SizeDefault; - } - - QSize large = qt_aqua_get_known_size(ct, widg, szHint, QStyleHelper::SizeLarge), - small = qt_aqua_get_known_size(ct, widg, szHint, QStyleHelper::SizeSmall), - mini = qt_aqua_get_known_size(ct, widg, szHint, QStyleHelper::SizeMini); - bool guess_size = false; - QStyleHelper::WidgetSizePolicy ret = QStyleHelper::SizeDefault; - QStyleHelper::WidgetSizePolicy wsp = QStyleHelper::widgetSizePolicy(widg); - if (wsp == QStyleHelper::SizeDefault) - guess_size = true; - else if (wsp == QStyleHelper::SizeMini) - ret = QStyleHelper::SizeMini; - else if (wsp == QStyleHelper::SizeSmall) - ret = QStyleHelper::SizeSmall; - else if (wsp == QStyleHelper::SizeLarge) - ret = QStyleHelper::SizeLarge; - if (guess_size) - ret = qt_aqua_guess_size(widg, large, small, mini); - - QSize *sz = 0; - if (ret == QStyleHelper::SizeSmall) - sz = &small; - else if (ret == QStyleHelper::SizeLarge) - sz = &large; - else if (ret == QStyleHelper::SizeMini) - sz = &mini; - if (insz) - *insz = sz ? *sz : QSize(-1, -1); -#ifdef DEBUG_SIZE_CONSTRAINT - if (sz) { - const char *size_desc = "Unknown"; - if (sz == &small) - size_desc = "Small"; - else if (sz == &large) - size_desc = "Large"; - else if (sz == &mini) - size_desc = "Mini"; - qDebug("%s - %s: %s taken (%d, %d) [%d, %d]", - widg ? widg->objectName().toLatin1().constData() : "*Unknown*", - widg ? widg->metaObject()->className() : "*Unknown*", size_desc, widg->width(), widg->height(), - sz->width(), sz->height()); - } -#endif - return ret; -#else - if (insz) - *insz = QSize(); - Q_UNUSED(widg); - Q_UNUSED(ct); - Q_UNUSED(szHint); - return QStyleHelper::SizeDefault; -#endif -} - -uint qHash(const QMacStylePrivate::CocoaControl &cw, uint seed = 0) -{ - return ((cw.type << 2) | cw.size) ^ seed; -} - -QMacStylePrivate::CocoaControl::CocoaControl() - : type(NoControl), size(QStyleHelper::SizeDefault) -{ -} - -QMacStylePrivate::CocoaControl::CocoaControl(CocoaControlType t, QStyleHelper::WidgetSizePolicy s) - : type(t), size(s) -{ -} - -bool QMacStylePrivate::CocoaControl::operator==(const CocoaControl &other) const -{ - return other.type == type && other.size == size; -} - -QSizeF QMacStylePrivate::CocoaControl::defaultFrameSize() const -{ - // We need this because things like NSView.alignmentRectInsets - // or -[NSCell titleRectForBounds:] won't work unless the control - // has a reasonable frame set. IOW, it's a chicken and egg problem. - // These values are as observed in Xcode 9's Interface Builder. - - if (type == Button_PushButton) - return QSizeF(-1, pushButtonDefaultHeight[size]); - - if (type == Button_PopupButton - || type == Button_PullDown) - return QSizeF(-1, popupButtonDefaultHeight[size]); - - if (type == ComboBox) - return QSizeF(-1, comboBoxDefaultHeight[size]); - - return QSizeF(); -} - -QRectF QMacStylePrivate::CocoaControl::adjustedControlFrame(const QRectF &rect) const -{ - QRectF frameRect; - const auto frameSize = defaultFrameSize(); - if (type == QMacStylePrivate::Button_SquareButton) { - frameRect = rect.adjusted(3, 1, -3, -1) - .adjusted(focusRingWidth, focusRingWidth, -focusRingWidth, -focusRingWidth); - } else if (type == QMacStylePrivate::Button_PushButton) { - // Start from the style option's top-left corner. - frameRect = QRectF(rect.topLeft(), - QSizeF(rect.width(), frameSize.height())); - if (size == QStyleHelper::SizeSmall) - frameRect = frameRect.translated(0, 1.5); - else if (size == QStyleHelper::SizeMini) - frameRect = frameRect.adjusted(0, 0, -8, 0).translated(4, 4); - } else { - // Center in the style option's rect. - frameRect = QRectF(QPointF(0, (rect.height() - frameSize.height()) / 2.0), - QSizeF(rect.width(), frameSize.height())); - frameRect = frameRect.translated(rect.topLeft()); - if (type == QMacStylePrivate::Button_PullDown || type == QMacStylePrivate::Button_PopupButton) { - if (size == QStyleHelper::SizeLarge) - frameRect = frameRect.adjusted(0, 0, -6, 0).translated(3, 0); - else if (size == QStyleHelper::SizeSmall) - frameRect = frameRect.adjusted(0, 0, -4, 0).translated(2, 1); - else if (size == QStyleHelper::SizeMini) - frameRect = frameRect.adjusted(0, 0, -9, 0).translated(5, 0); - } else if (type == QMacStylePrivate::ComboBox) { - frameRect = frameRect.adjusted(0, 0, -6, 0).translated(4, 0); - } - } - - return frameRect; -} - -QMarginsF QMacStylePrivate::CocoaControl::titleMargins() const -{ - if (type == QMacStylePrivate::Button_PushButton) { - if (size == QStyleHelper::SizeLarge) - return QMarginsF(12, 5, 12, 9); - if (size == QStyleHelper::SizeSmall) - return QMarginsF(12, 4, 12, 9); - if (size == QStyleHelper::SizeMini) - return QMarginsF(10, 1, 10, 2); - } - - if (type == QMacStylePrivate::Button_PullDown) { - if (size == QStyleHelper::SizeLarge) - return QMarginsF(7.5, 2.5, 22.5, 5.5); - if (size == QStyleHelper::SizeSmall) - return QMarginsF(7.5, 2, 20.5, 4); - if (size == QStyleHelper::SizeMini) - return QMarginsF(4.5, 0, 16.5, 2); - } - - if (type == QMacStylePrivate::Button_SquareButton) - return QMarginsF(6, 1, 6, 2); - - return QMarginsF(); -} - -bool QMacStylePrivate::CocoaControl::getCocoaButtonTypeAndBezelStyle(NSButtonType *buttonType, NSBezelStyle *bezelStyle) const -{ - switch (type) { - case Button_CheckBox: - *buttonType = NSSwitchButton; - *bezelStyle = NSRegularSquareBezelStyle; - break; - case Button_Disclosure: - *buttonType = NSOnOffButton; - *bezelStyle = NSDisclosureBezelStyle; - break; - case Button_RadioButton: - *buttonType = NSRadioButton; - *bezelStyle = NSRegularSquareBezelStyle; - break; - case Button_SquareButton: - *buttonType = NSPushOnPushOffButton; - *bezelStyle = NSShadowlessSquareBezelStyle; - break; - case Button_PushButton: - *buttonType = NSPushOnPushOffButton; - *bezelStyle = NSRoundedBezelStyle; - break; - default: - return false; - } - - return true; -} - -QMacStylePrivate::CocoaControlType cocoaControlType(const QStyleOption *opt, const QWidget *w) -{ - if (const auto *btn = qstyleoption_cast(opt)) { - const bool hasMenu = btn->features & QStyleOptionButton::HasMenu; - // When the contents won't fit in a large sized button, - // and WA_MacNormalSize is not set, make the button square. - // Threshold used to be at 34, not 32. - const auto maxNonSquareHeight = pushButtonDefaultHeight[QStyleHelper::SizeLarge]; - const bool isSquare = (btn->features & QStyleOptionButton::Flat) - || (btn->rect.height() > maxNonSquareHeight - && !(w && w->testAttribute(Qt::WA_MacNormalSize))); - return (isSquare? QMacStylePrivate::Button_SquareButton : - hasMenu ? QMacStylePrivate::Button_PullDown : - QMacStylePrivate::Button_PushButton); - } - - if (const auto *combo = qstyleoption_cast(opt)) { - if (combo->editable) - return QMacStylePrivate::ComboBox; - // TODO Me may support square, non-editable combo boxes, but not more than that - return QMacStylePrivate::Button_PopupButton; - } - - return QMacStylePrivate::NoControl; -} - -/** - Carbon draws comboboxes (and other views) outside the rect given as argument. Use this function to obtain - the corresponding inner rect for drawing the same combobox so that it stays inside the given outerBounds. -*/ -CGRect QMacStylePrivate::comboboxInnerBounds(const CGRect &outerBounds, const CocoaControl &cocoaWidget) -{ - CGRect innerBounds = outerBounds; - // Carbon draw parts of the view outside the rect. - // So make the rect a bit smaller to compensate - // (I wish HIThemeGetButtonBackgroundBounds worked) - if (cocoaWidget.type == Button_PopupButton) { - switch (cocoaWidget.size) { - case QStyleHelper::SizeSmall: - innerBounds.origin.x += 3; - innerBounds.origin.y += 3; - innerBounds.size.width -= 6; - innerBounds.size.height -= 7; - break; - case QStyleHelper::SizeMini: - innerBounds.origin.x += 2; - innerBounds.origin.y += 2; - innerBounds.size.width -= 5; - innerBounds.size.height -= 6; - break; - case QStyleHelper::SizeLarge: - case QStyleHelper::SizeDefault: - innerBounds.origin.x += 2; - innerBounds.origin.y += 2; - innerBounds.size.width -= 5; - innerBounds.size.height -= 6; - } - } else if (cocoaWidget.type == ComboBox) { - switch (cocoaWidget.size) { - case QStyleHelper::SizeSmall: - innerBounds.origin.x += 3; - innerBounds.origin.y += 3; - innerBounds.size.width -= 7; - innerBounds.size.height -= 8; - break; - case QStyleHelper::SizeMini: - innerBounds.origin.x += 3; - innerBounds.origin.y += 3; - innerBounds.size.width -= 4; - innerBounds.size.height -= 8; - break; - case QStyleHelper::SizeLarge: - case QStyleHelper::SizeDefault: - innerBounds.origin.x += 3; - innerBounds.origin.y += 2; - innerBounds.size.width -= 6; - innerBounds.size.height -= 8; - } - } - - return innerBounds; -} - -/** - Inside a combobox Qt places a line edit widget. The size of this widget should depend on the kind - of combobox we choose to draw. This function calculates and returns this size. -*/ -QRectF QMacStylePrivate::comboboxEditBounds(const QRectF &outerBounds, const CocoaControl &cw) -{ - QRectF ret = outerBounds; - if (cw.type == ComboBox) { - switch (cw.size) { - case QStyleHelper::SizeLarge: - ret = ret.adjusted(0, 0, -28, 0).translated(3, 4.5); - ret.setHeight(16); - break; - case QStyleHelper::SizeSmall: - ret = ret.adjusted(0, 0, -24, 0).translated(3, 2); - ret.setHeight(14); - break; - case QStyleHelper::SizeMini: - ret = ret.adjusted(0, 0, -21, 0).translated(2, 3); - ret.setHeight(11); - break; - default: - break; - } - } else if (cw.type == Button_PopupButton) { - switch (cw.size) { - case QStyleHelper::SizeLarge: - ret.adjust(10, 1, -23, -4); - break; - case QStyleHelper::SizeSmall: - ret.adjust(10, 4, -20, -3); - break; - case QStyleHelper::SizeMini: - ret.adjust(9, 0, -19, 0); - ret.setHeight(13); - break; - default: - break; - } - } - return ret; -} - -QMacStylePrivate::QMacStylePrivate() - : backingStoreNSView(nil) -{ - if (auto *ssf = QGuiApplicationPrivate::platformTheme()->font(QPlatformTheme::SmallFont)) - smallSystemFont = *ssf; - if (auto *msf = QGuiApplicationPrivate::platformTheme()->font(QPlatformTheme::MiniFont)) - miniSystemFont = *msf; -} - -QMacStylePrivate::~QMacStylePrivate() -{ - QMacAutoReleasePool pool; - for (NSView *b : cocoaControls) - [b release]; - for (NSCell *cell : cocoaCells) - [cell release]; -} - -NSView *QMacStylePrivate::cocoaControl(CocoaControl widget) const -{ - if (widget.type == QMacStylePrivate::NoControl - || widget.size == QStyleHelper::SizeDefault) - return nil; - - if (widget.type == Box) { - if (__builtin_available(macOS 10.14, *)) { - if (qt_mac_applicationIsInDarkMode()) { - // See render code in drawPrimitive(PE_FrameTabWidget) - widget.type = Box_Dark; - } - } - } - - NSView *bv = cocoaControls.value(widget, nil); - if (!bv) { - switch (widget.type) { - case Box: { - NSBox *box = [[NSBox alloc] init]; - bv = box; - box.title = @""; - box.titlePosition = NSNoTitle; - break; - } - case Box_Dark: - bv = [[QDarkNSBox alloc] init]; - break; - case Button_CheckBox: - case Button_Disclosure: - case Button_PushButton: - case Button_RadioButton: - case Button_SquareButton: { - NSButton *bc = [[NSButton alloc] init]; - bc.title = @""; - // See below for style and bezel setting. - bv = bc; - break; - } - case Button_PopupButton: - case Button_PullDown: { - NSPopUpButton *bc = [[NSPopUpButton alloc] init]; - bc.title = @""; - if (widget.type == Button_PullDown) - bc.pullsDown = YES; - bv = bc; - break; - } - case Button_WindowClose: - case Button_WindowMiniaturize: - case Button_WindowZoom: { - const NSWindowButton button = [=] { - switch (widget.type) { - case Button_WindowClose: - return NSWindowCloseButton; - case Button_WindowMiniaturize: - return NSWindowMiniaturizeButton; - case Button_WindowZoom: - return NSWindowZoomButton; - default: - break; - } - Q_UNREACHABLE(); - } (); - const auto styleMask = NSWindowStyleMaskTitled - | NSWindowStyleMaskClosable - | NSWindowStyleMaskMiniaturizable - | NSWindowStyleMaskResizable; - bv = [NSWindow standardWindowButton:button forStyleMask:styleMask]; - [bv retain]; - break; - } - case ComboBox: - bv = [[NSComboBox alloc] init]; - break; - case ProgressIndicator_Determinate: - bv = [[NSProgressIndicator alloc] init]; - break; - case ProgressIndicator_Indeterminate: - bv = [[QIndeterminateProgressIndicator alloc] init]; - break; - case Scroller_Horizontal: - bv = [[NSScroller alloc] initWithFrame:NSMakeRect(0, 0, 200, 20)]; - break; - case Scroller_Vertical: - // Cocoa sets the orientation from the view's frame - // at construction time, and it cannot be changed later. - bv = [[NSScroller alloc] initWithFrame:NSMakeRect(0, 0, 20, 200)]; - break; - case Slider_Horizontal: - bv = [[NSSlider alloc] initWithFrame:NSMakeRect(0, 0, 200, 20)]; - break; - case Slider_Vertical: - // Cocoa sets the orientation from the view's frame - // at construction time, and it cannot be changed later. - bv = [[NSSlider alloc] initWithFrame:NSMakeRect(0, 0, 20, 200)]; - break; - case SplitView_Horizontal: - bv = [[NSSplitView alloc] init]; - break; - case SplitView_Vertical: - bv = [[QVerticalSplitView alloc] init]; - break; - case TextField: - bv = [[NSTextField alloc] init]; - break; - default: - break; - } - - if ([bv isKindOfClass:[NSControl class]]) { - auto *ctrl = static_cast(bv); - switch (widget.size) { - case QStyleHelper::SizeSmall: - ctrl.controlSize = NSControlSizeSmall; - break; - case QStyleHelper::SizeMini: - ctrl.controlSize = NSControlSizeMini; - break; - default: - break; - } - } else if (widget.type == ProgressIndicator_Determinate || - widget.type == ProgressIndicator_Indeterminate) { - auto *pi = static_cast(bv); - pi.indeterminate = (widget.type == ProgressIndicator_Indeterminate); - switch (widget.size) { - case QStyleHelper::SizeSmall: - pi.controlSize = NSControlSizeSmall; - break; - case QStyleHelper::SizeMini: - pi.controlSize = NSControlSizeMini; - break; - default: - break; - } - } - - cocoaControls.insert(widget, bv); - } - - NSButtonType buttonType; - NSBezelStyle bezelStyle; - if (widget.getCocoaButtonTypeAndBezelStyle(&buttonType, &bezelStyle)) { - // FIXME We need to reset the button's type and - // bezel style properties, even when cached. - auto *button = static_cast(bv); - button.buttonType = buttonType; - button.bezelStyle = bezelStyle; - if (widget.type == Button_CheckBox) - button.allowsMixedState = YES; - } - - return bv; -} - -NSCell *QMacStylePrivate::cocoaCell(CocoaControl widget) const -{ - NSCell *cell = cocoaCells[widget]; - if (!cell) { - switch (widget.type) { - case Stepper: - cell = [[NSStepperCell alloc] init]; - break; - case Button_Disclosure: { - NSButtonCell *bc = [[NSButtonCell alloc] init]; - bc.buttonType = NSOnOffButton; - bc.bezelStyle = NSDisclosureBezelStyle; - cell = bc; - break; - } - default: - break; - } - - switch (widget.size) { - case QStyleHelper::SizeSmall: - cell.controlSize = NSControlSizeSmall; - break; - case QStyleHelper::SizeMini: - cell.controlSize = NSControlSizeMini; - break; - default: - break; - } - - cocoaCells.insert(widget, cell); - } - - return cell; -} - -void QMacStylePrivate::drawNSViewInRect(NSView *view, const QRectF &rect, QPainter *p, - __attribute__((noescape)) DrawRectBlock drawRectBlock) const -{ - QMacCGContext ctx(p); - setupNSGraphicsContext(ctx, YES); - - // FIXME: The rect that we get in is relative to the widget that we're drawing - // style on behalf of, and doesn't take into account the offset of that widget - // to the widget that owns the backingstore, which we are placing the native - // view into below. This means most of the views are placed in the upper left - // corner of backingStoreNSView, which does not map to where the actual widget - // is, and which may cause problems such as triggering a setNeedsDisplay of the - // backingStoreNSView for the wrong rect. We work around this by making the view - // layer-backed, which prevents triggering display of the backingStoreNSView, but - // but there may be other issues lurking here due to the wrong position. QTBUG-68023 - view.wantsLayer = YES; - - // FIXME: We are also setting the frame of the incoming view a lot at the call - // sites of this function, making it unclear who's actually responsible for - // maintaining the size and position of the view. In theory the call sites - // should ensure the _size_ of the view is correct, and then let this code - // take care of _positioning_ the view at the right place inside backingStoreNSView. - // For now we pass on the rect as is, to prevent any regressions until this - // can be investigated properly. - view.frame = rect.toCGRect(); - - [backingStoreNSView addSubview:view]; - - // FIXME: Based on the code below, this method isn't drawing an NSView into - // a rect, it's drawing _part of the NSView_, defined by the incoming clip - // or dirty rect, into the current graphics context. We're doing some manual - // translations at the call sites that would indicate that this relationship - // is a bit fuzzy. - const CGRect dirtyRect = rect.toCGRect(); - - if (drawRectBlock) - drawRectBlock(ctx, dirtyRect); - else - [view drawRect:dirtyRect]; - - [view removeFromSuperviewWithoutNeedingDisplay]; - - restoreNSGraphicsContext(ctx); -} - -void QMacStylePrivate::resolveCurrentNSView(QWindow *window) const -{ - backingStoreNSView = window ? (NSView *)window->winId() : nil; -} - -QMacStyle::QMacStyle() - : QCommonStyle(*new QMacStylePrivate) -{ - Q_D(QMacStyle); - QMacAutoReleasePool pool; - - d->receiver = [[NotificationReceiver alloc] init]; - [[NSNotificationCenter defaultCenter] addObserver:d->receiver - selector:@selector(scrollBarStyleDidChange:) - name:NSPreferredScrollerStyleDidChangeNotification - object:nil]; -} - -QMacStyle::~QMacStyle() -{ - Q_D(QMacStyle); - QMacAutoReleasePool pool; - - [[NSNotificationCenter defaultCenter] removeObserver:d->receiver]; - [d->receiver release]; -} - -void QMacStyle::polish(QPalette &) -{ -} - -void QMacStyle::polish(QApplication *) -{ -} - -void QMacStyle::unpolish(QApplication *) -{ -} - -void QMacStyle::polish(QWidget* w) -{ - if (false -#if QT_CONFIG(menu) - || qobject_cast(w) -# if QT_CONFIG(combobox) - || qobject_cast(w) -# endif -#endif -#if QT_CONFIG(mdiarea) - || qobject_cast(w) -#endif - ) { - w->setAttribute(Qt::WA_TranslucentBackground, true); - w->setAutoFillBackground(false); - } - -#if QT_CONFIG(tabbar) - if (QTabBar *tb = qobject_cast(w)) { - if (tb->documentMode()) { - w->setAttribute(Qt::WA_Hover); - w->setFont(qt_app_fonts_hash()->value("QSmallFont", QFont())); - QPalette p = w->palette(); - p.setColor(QPalette::WindowText, QColor(17, 17, 17)); - w->setPalette(p); - w->setAttribute(Qt::WA_SetPalette, false); - w->setAttribute(Qt::WA_SetFont, false); - } - } -#endif - - QCommonStyle::polish(w); - - if (QRubberBand *rubber = qobject_cast(w)) { - rubber->setWindowOpacity(0.25); - rubber->setAttribute(Qt::WA_PaintOnScreen, false); - rubber->setAttribute(Qt::WA_NoSystemBackground, false); - } - - if (qobject_cast(w)) { - w->setAttribute(Qt::WA_OpaquePaintEvent, false); - w->setAttribute(Qt::WA_Hover, true); - w->setMouseTracking(true); - } -} - -void QMacStyle::unpolish(QWidget* w) -{ - if ( -#if QT_CONFIG(menu) - qobject_cast(w) && -#endif - !w->testAttribute(Qt::WA_SetPalette)) { - QPalette pal = qApp->palette(w); - w->setPalette(pal); - w->setAttribute(Qt::WA_SetPalette, false); - w->setWindowOpacity(1.0); - } - -#if QT_CONFIG(combobox) - if (QComboBox *combo = qobject_cast(w)) { - if (!combo->isEditable()) { - if (QWidget *widget = combo->findChild()) - widget->setWindowOpacity(1.0); - } - } -#endif - -#if QT_CONFIG(tabbar) - if (qobject_cast(w)) { - if (!w->testAttribute(Qt::WA_SetFont)) - w->setFont(qApp->font(w)); - if (!w->testAttribute(Qt::WA_SetPalette)) - w->setPalette(qApp->palette(w)); - } -#endif - - if (QRubberBand *rubber = qobject_cast(w)) { - rubber->setWindowOpacity(1.0); - rubber->setAttribute(Qt::WA_PaintOnScreen, true); - rubber->setAttribute(Qt::WA_NoSystemBackground, true); - } - - if (QFocusFrame *frame = qobject_cast(w)) - frame->setAttribute(Qt::WA_NoSystemBackground, true); - - QCommonStyle::unpolish(w); - - if (qobject_cast(w)) { - w->setAttribute(Qt::WA_OpaquePaintEvent, true); - w->setAttribute(Qt::WA_Hover, false); - w->setMouseTracking(false); - } -} - -int QMacStyle::pixelMetric(PixelMetric metric, const QStyleOption *opt, const QWidget *widget) const -{ - Q_D(const QMacStyle); - const int controlSize = getControlSize(opt, widget); - int ret = 0; - - switch (metric) { - case PM_TabCloseIndicatorWidth: - case PM_TabCloseIndicatorHeight: - ret = closeButtonSize; - break; - case PM_ToolBarIconSize: - ret = proxy()->pixelMetric(PM_LargeIconSize); - break; - case PM_FocusFrameVMargin: - case PM_FocusFrameHMargin: - ret = qt_mac_aqua_get_metric(FocusRectOutset); - break; - case PM_DialogButtonsSeparator: - ret = -5; - break; - case PM_DialogButtonsButtonHeight: { - QSize sz; - ret = d->aquaSizeConstrain(opt, 0, QStyle::CT_PushButton, QSize(-1, -1), &sz); - if (sz == QSize(-1, -1)) - ret = 32; - else - ret = sz.height(); - break; } - case PM_DialogButtonsButtonWidth: { - QSize sz; - ret = d->aquaSizeConstrain(opt, 0, QStyle::CT_PushButton, QSize(-1, -1), &sz); - if (sz == QSize(-1, -1)) - ret = 70; - else - ret = sz.width(); - break; } - - case PM_MenuBarHMargin: - ret = 8; - break; - - case PM_MenuBarVMargin: - ret = 0; - break; - - case PM_MenuBarPanelWidth: - ret = 0; - break; - - case PM_MenuButtonIndicator: - ret = toolButtonArrowSize; - break; - - case QStyle::PM_MenuDesktopFrameWidth: - ret = 5; - break; - - case PM_CheckBoxLabelSpacing: - case PM_RadioButtonLabelSpacing: - ret = [=] { - if (opt) { - if (opt->state & State_Mini) - return 4; - if (opt->state & State_Small) - return 3; - } - return 2; - } (); - break; - case PM_MenuScrollerHeight: - ret = 15; // I hate having magic numbers in here... - break; - case PM_DefaultFrameWidth: -#if QT_CONFIG(mainwindow) - if (widget && (widget->isWindow() || !widget->parentWidget() - || (qobject_cast(widget->parentWidget()) - && static_cast(widget->parentWidget())->centralWidget() == widget)) - && qobject_cast(widget)) - ret = 0; - else -#endif - // The combo box popup has no frame. - if (qstyleoption_cast(opt) != 0) - ret = 0; - else - ret = 1; - break; - case PM_MaximumDragDistance: - ret = -1; - break; - case PM_ScrollBarSliderMin: - ret = 24; - break; - case PM_SpinBoxFrameWidth: - ret = qt_mac_aqua_get_metric(EditTextFrameOutset); - break; - case PM_ButtonShiftHorizontal: - case PM_ButtonShiftVertical: - ret = 0; - break; - case PM_SliderLength: - ret = 17; - break; - // Returns the number of pixels to use for the business part of the - // slider (i.e., the non-tickmark portion). The remaining space is shared - // equally between the tickmark regions. - case PM_SliderControlThickness: - if (const QStyleOptionSlider *sl = qstyleoption_cast(opt)) { - int space = (sl->orientation == Qt::Horizontal) ? sl->rect.height() : sl->rect.width(); - int ticks = sl->tickPosition; - int n = 0; - if (ticks & QSlider::TicksAbove) - ++n; - if (ticks & QSlider::TicksBelow) - ++n; - if (!n) { - ret = space; - break; - } - - int thick = 6; // Magic constant to get 5 + 16 + 5 - if (ticks != QSlider::TicksBothSides && ticks != QSlider::NoTicks) - thick += proxy()->pixelMetric(PM_SliderLength, sl, widget) / 4; - - space -= thick; - if (space > 0) - thick += (space * 2) / (n + 2); - ret = thick; - } else { - ret = 0; - } - break; - case PM_SmallIconSize: - ret = int(QStyleHelper::dpiScaled(16.)); - break; - - case PM_LargeIconSize: - ret = int(QStyleHelper::dpiScaled(32.)); - break; - - case PM_IconViewIconSize: - ret = proxy()->pixelMetric(PM_LargeIconSize, opt, widget); - break; - - case PM_ButtonDefaultIndicator: - ret = 0; - break; - case PM_TitleBarHeight: { - NSUInteger style = NSWindowStyleMaskTitled; - if (widget && ((widget->windowFlags() & Qt::Tool) == Qt::Tool)) - style |= NSWindowStyleMaskUtilityWindow; - ret = int([NSWindow frameRectForContentRect:NSZeroRect - styleMask:style].size.height); - break; } - case QStyle::PM_TabBarTabHSpace: - switch (d->aquaSizeConstrain(opt, widget)) { - case QStyleHelper::SizeLarge: - ret = QCommonStyle::pixelMetric(metric, opt, widget); - break; - case QStyleHelper::SizeSmall: - ret = 20; - break; - case QStyleHelper::SizeMini: - ret = 16; - break; - case QStyleHelper::SizeDefault: - const QStyleOptionTab *tb = qstyleoption_cast(opt); - if (tb && tb->documentMode) - ret = 30; - else - ret = QCommonStyle::pixelMetric(metric, opt, widget); - break; - } - break; - case PM_TabBarTabVSpace: - ret = 4; - break; - case PM_TabBarTabShiftHorizontal: - case PM_TabBarTabShiftVertical: - ret = 0; - break; - case PM_TabBarBaseHeight: - ret = 0; - break; - case PM_TabBarTabOverlap: - ret = 1; - break; - case PM_TabBarBaseOverlap: - switch (d->aquaSizeConstrain(opt, widget)) { - case QStyleHelper::SizeDefault: - case QStyleHelper::SizeLarge: - ret = 11; - break; - case QStyleHelper::SizeSmall: - ret = 8; - break; - case QStyleHelper::SizeMini: - ret = 7; - break; - } - break; - case PM_ScrollBarExtent: { - const QStyleHelper::WidgetSizePolicy size = d->effectiveAquaSizeConstrain(opt, widget); - ret = static_cast([NSScroller - scrollerWidthForControlSize:static_cast(size) - scrollerStyle:[NSScroller preferredScrollerStyle]]); - break; } - case PM_IndicatorHeight: { - switch (d->aquaSizeConstrain(opt, widget)) { - case QStyleHelper::SizeDefault: - case QStyleHelper::SizeLarge: - ret = qt_mac_aqua_get_metric(CheckBoxHeight); - break; - case QStyleHelper::SizeMini: - ret = qt_mac_aqua_get_metric(MiniCheckBoxHeight); - break; - case QStyleHelper::SizeSmall: - ret = qt_mac_aqua_get_metric(SmallCheckBoxHeight); - break; - } - break; } - case PM_IndicatorWidth: { - switch (d->aquaSizeConstrain(opt, widget)) { - case QStyleHelper::SizeDefault: - case QStyleHelper::SizeLarge: - ret = qt_mac_aqua_get_metric(CheckBoxWidth); - break; - case QStyleHelper::SizeMini: - ret = qt_mac_aqua_get_metric(MiniCheckBoxWidth); - break; - case QStyleHelper::SizeSmall: - ret = qt_mac_aqua_get_metric(SmallCheckBoxWidth); - break; - } - ++ret; - break; } - case PM_ExclusiveIndicatorHeight: { - switch (d->aquaSizeConstrain(opt, widget)) { - case QStyleHelper::SizeDefault: - case QStyleHelper::SizeLarge: - ret = qt_mac_aqua_get_metric(RadioButtonHeight); - break; - case QStyleHelper::SizeMini: - ret = qt_mac_aqua_get_metric(MiniRadioButtonHeight); - break; - case QStyleHelper::SizeSmall: - ret = qt_mac_aqua_get_metric(SmallRadioButtonHeight); - break; - } - break; } - case PM_ExclusiveIndicatorWidth: { - switch (d->aquaSizeConstrain(opt, widget)) { - case QStyleHelper::SizeDefault: - case QStyleHelper::SizeLarge: - ret = qt_mac_aqua_get_metric(RadioButtonWidth); - break; - case QStyleHelper::SizeMini: - ret = qt_mac_aqua_get_metric(MiniRadioButtonWidth); - break; - case QStyleHelper::SizeSmall: - ret = qt_mac_aqua_get_metric(SmallRadioButtonWidth); - break; - } - ++ret; - break; } - case PM_MenuVMargin: - ret = 4; - break; - case PM_MenuPanelWidth: - ret = 0; - break; - case PM_ToolTipLabelFrameWidth: - ret = 0; - break; - case PM_SizeGripSize: { - QStyleHelper::WidgetSizePolicy aSize; - if (widget && widget->window()->windowType() == Qt::Tool) - aSize = QStyleHelper::SizeSmall; - else - aSize = QStyleHelper::SizeLarge; - const QSize size = qt_aqua_get_known_size(CT_SizeGrip, widget, QSize(), aSize); - ret = size.width(); - break; } - case PM_MdiSubWindowFrameWidth: - ret = 1; - break; - case PM_DockWidgetFrameWidth: - ret = 0; - break; - case PM_DockWidgetTitleMargin: - ret = 0; - break; - case PM_DockWidgetSeparatorExtent: - ret = 1; - break; - case PM_ToolBarHandleExtent: - ret = 11; - break; - case PM_ToolBarItemMargin: - ret = 0; - break; - case PM_ToolBarItemSpacing: - ret = 4; - break; - case PM_SplitterWidth: - ret = qMax(7, QApplication::globalStrut().width()); - break; - case PM_LayoutLeftMargin: - case PM_LayoutTopMargin: - case PM_LayoutRightMargin: - case PM_LayoutBottomMargin: - { - bool isWindow = false; - if (opt) { - isWindow = (opt->state & State_Window); - } else if (widget) { - isWindow = widget->isWindow(); - } - - if (isWindow) { - /* - AHIG would have (20, 8, 10) here but that makes - no sense. It would also have 14 for the top margin - but this contradicts both Builder and most - applications. - */ - return_SIZE(20, 10, 10); // AHIG - } else { - // hack to detect QTabWidget - if (widget && widget->parentWidget() - && widget->parentWidget()->sizePolicy().controlType() == QSizePolicy::TabWidget) { - if (metric == PM_LayoutTopMargin) { - /* - Builder would have 14 (= 20 - 6) instead of 12, - but that makes the tab look disproportionate. - */ - return_SIZE(12, 6, 6); // guess - } else { - return_SIZE(20 /* Builder */, 8 /* guess */, 8 /* guess */); - } - } else { - /* - Child margins are highly inconsistent in AHIG and Builder. - */ - return_SIZE(12, 8, 6); // guess - } - } - } - case PM_LayoutHorizontalSpacing: - case PM_LayoutVerticalSpacing: - return -1; - case PM_MenuHMargin: - ret = 0; - break; - case PM_ToolBarExtensionExtent: - ret = 21; - break; - case PM_ToolBarFrameWidth: - ret = 1; - break; - case PM_ScrollView_ScrollBarOverlap: - ret = [NSScroller preferredScrollerStyle] == NSScrollerStyleOverlay ? - pixelMetric(PM_ScrollBarExtent, opt, widget) : 0; - break; - default: - ret = QCommonStyle::pixelMetric(metric, opt, widget); - break; - } - return ret; -} - -QPalette QMacStyle::standardPalette() const -{ - QPalette pal = QCommonStyle::standardPalette(); - pal.setColor(QPalette::Disabled, QPalette::Dark, QColor(191, 191, 191)); - pal.setColor(QPalette::Active, QPalette::Dark, QColor(191, 191, 191)); - pal.setColor(QPalette::Inactive, QPalette::Dark, QColor(191, 191, 191)); - return pal; -} - -int QMacStyle::styleHint(StyleHint sh, const QStyleOption *opt, const QWidget *w, - QStyleHintReturn *hret) const -{ - QMacAutoReleasePool pool; - - int ret = 0; - switch (sh) { - case SH_Slider_SnapToValue: - case SH_PrintDialog_RightAlignButtons: - case SH_FontDialog_SelectAssociatedText: - case SH_MenuBar_MouseTracking: - case SH_Menu_MouseTracking: - case SH_ComboBox_ListMouseTracking: - case SH_MainWindow_SpaceBelowMenuBar: - case SH_ItemView_ChangeHighlightOnFocus: - ret = 1; - break; - case SH_ToolBox_SelectedPageTitleBold: - ret = 0; - break; - case SH_DialogButtonBox_ButtonsHaveIcons: - ret = 0; - break; - case SH_Menu_SelectionWrap: - ret = false; - break; - case SH_Menu_KeyboardSearch: - ret = true; - break; - case SH_Menu_SpaceActivatesItem: - ret = true; - break; - case SH_Slider_AbsoluteSetButtons: - ret = Qt::LeftButton|Qt::MidButton; - break; - case SH_Slider_PageSetButtons: - ret = 0; - break; - case SH_ScrollBar_ContextMenu: - ret = false; - break; - case SH_TitleBar_AutoRaise: - ret = true; - break; - case SH_Menu_AllowActiveAndDisabled: - ret = false; - break; - case SH_Menu_SubMenuPopupDelay: - ret = 100; - break; - case SH_Menu_SubMenuUniDirection: - ret = true; - break; - case SH_Menu_SubMenuSloppySelectOtherActions: - ret = false; - break; - case SH_Menu_SubMenuResetWhenReenteringParent: - ret = true; - break; - case SH_Menu_SubMenuDontStartSloppyOnLeave: - ret = true; - break; - - case SH_ScrollBar_LeftClickAbsolutePosition: { - NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults]; - bool result = [defaults boolForKey:@"AppleScrollerPagingBehavior"]; - if(QApplication::keyboardModifiers() & Qt::AltModifier) - ret = !result; - else - ret = result; - break; } - case SH_TabBar_PreferNoArrows: - ret = true; - break; - /* - case SH_DialogButtons_DefaultButton: - ret = QDialogButtons::Reject; - break; - */ - case SH_GroupBox_TextLabelVerticalAlignment: - ret = Qt::AlignTop; - break; - case SH_ScrollView_FrameOnlyAroundContents: - ret = QCommonStyle::styleHint(sh, opt, w, hret); - break; - case SH_Menu_FillScreenWithScroll: - ret = false; - break; - case SH_Menu_Scrollable: - ret = true; - break; - case SH_RichText_FullWidthSelection: - ret = true; - break; - case SH_BlinkCursorWhenTextSelected: - ret = false; - break; - case SH_ScrollBar_StopMouseOverSlider: - ret = true; - break; - case SH_ListViewExpand_SelectMouseType: - ret = QEvent::MouseButtonRelease; - break; - case SH_TabBar_SelectMouseType: -#if QT_CONFIG(tabbar) - if (const QStyleOptionTabBarBase *opt2 = qstyleoption_cast(opt)) { - ret = opt2->documentMode ? QEvent::MouseButtonPress : QEvent::MouseButtonRelease; - } else -#endif - { - ret = QEvent::MouseButtonRelease; - } - break; - case SH_ComboBox_Popup: - if (const QStyleOptionComboBox *cmb = qstyleoption_cast(opt)) - ret = !cmb->editable; - else - ret = 0; - break; - case SH_Workspace_FillSpaceOnMaximize: - ret = true; - break; - case SH_Widget_ShareActivation: - ret = true; - break; - case SH_Header_ArrowAlignment: - ret = Qt::AlignRight; - break; - case SH_TabBar_Alignment: { -#if QT_CONFIG(tabwidget) - if (const QTabWidget *tab = qobject_cast(w)) { - if (tab->documentMode()) { - ret = Qt::AlignLeft; - break; - } - } -#endif -#if QT_CONFIG(tabbar) - if (const QTabBar *tab = qobject_cast(w)) { - if (tab->documentMode()) { - ret = Qt::AlignLeft; - break; - } - } -#endif - ret = Qt::AlignCenter; - } break; - case SH_UnderlineShortcut: - ret = false; - break; - case SH_ToolTipLabel_Opacity: - ret = 242; // About 95% - break; - case SH_Button_FocusPolicy: - ret = Qt::TabFocus; - break; - case SH_EtchDisabledText: - ret = false; - break; - case SH_FocusFrame_Mask: { - ret = true; - if(QStyleHintReturnMask *mask = qstyleoption_cast(hret)) { - const uchar fillR = 192, fillG = 191, fillB = 190; - QImage img; - - QSize pixmapSize = opt->rect.size(); - if (!pixmapSize.isEmpty()) { - QPixmap pix(pixmapSize); - pix.fill(QColor(fillR, fillG, fillB)); - QPainter pix_paint(&pix); - proxy()->drawControl(CE_FocusFrame, opt, &pix_paint, w); - pix_paint.end(); - img = pix.toImage(); - } - - const QRgb *sptr = (QRgb*)img.bits(), *srow; - const int sbpl = img.bytesPerLine(); - const int w = sbpl/4, h = img.height(); - - QImage img_mask(img.width(), img.height(), QImage::Format_ARGB32); - QRgb *dptr = (QRgb*)img_mask.bits(), *drow; - const int dbpl = img_mask.bytesPerLine(); - - for (int y = 0; y < h; ++y) { - srow = sptr+((y*sbpl)/4); - drow = dptr+((y*dbpl)/4); - for (int x = 0; x < w; ++x) { - const int redDiff = qRed(*srow) - fillR; - const int greenDiff = qGreen(*srow) - fillG; - const int blueDiff = qBlue(*srow) - fillB; - const int diff = (redDiff * redDiff) + (greenDiff * greenDiff) + (blueDiff * blueDiff); - (*drow++) = (diff < 10) ? 0xffffffff : 0xff000000; - ++srow; - } - } - QBitmap qmask = QBitmap::fromImage(img_mask); - mask->region = QRegion(qmask); - } - break; } - case SH_TitleBar_NoBorder: - ret = 1; - break; - case SH_RubberBand_Mask: - ret = 0; - break; - case SH_ComboBox_LayoutDirection: - ret = Qt::LeftToRight; - break; - case SH_ItemView_EllipsisLocation: - ret = Qt::AlignHCenter; - break; - case SH_ItemView_ShowDecorationSelected: - ret = true; - break; - case SH_TitleBar_ModifyNotification: - ret = false; - break; - case SH_ScrollBar_RollBetweenButtons: - ret = true; - break; - case SH_WindowFrame_Mask: - ret = false; - break; - case SH_TabBar_ElideMode: - ret = Qt::ElideRight; - break; -#if QT_CONFIG(dialogbuttonbox) - case SH_DialogButtonLayout: - ret = QDialogButtonBox::MacLayout; - break; -#endif - case SH_FormLayoutWrapPolicy: - ret = QFormLayout::DontWrapRows; - break; - case SH_FormLayoutFieldGrowthPolicy: - ret = QFormLayout::FieldsStayAtSizeHint; - break; - case SH_FormLayoutFormAlignment: - ret = Qt::AlignHCenter | Qt::AlignTop; - break; - case SH_FormLayoutLabelAlignment: - ret = Qt::AlignRight; - break; - case SH_ComboBox_PopupFrameStyle: - ret = QFrame::NoFrame; - break; - case SH_MessageBox_TextInteractionFlags: - ret = Qt::TextSelectableByMouse | Qt::LinksAccessibleByMouse | Qt::TextSelectableByKeyboard; - break; - case SH_SpellCheckUnderlineStyle: - ret = QTextCharFormat::DashUnderline; - break; - case SH_MessageBox_CenterButtons: - ret = false; - break; - case SH_MenuBar_AltKeyNavigation: - ret = false; - break; - case SH_ItemView_MovementWithoutUpdatingSelection: - ret = false; - break; - case SH_FocusFrame_AboveWidget: - ret = true; - break; -#if QT_CONFIG(wizard) - case SH_WizardStyle: - ret = QWizard::MacStyle; - break; -#endif - case SH_ItemView_ArrowKeysNavigateIntoChildren: - ret = false; - break; - case SH_Menu_FlashTriggeredItem: - ret = true; - break; - case SH_Menu_FadeOutOnHide: - ret = true; - break; - case SH_ItemView_PaintAlternatingRowColorsForEmptyArea: - ret = true; - break; -#if QT_CONFIG(tabbar) - case SH_TabBar_CloseButtonPosition: - ret = QTabBar::LeftSide; - break; -#endif - case SH_DockWidget_ButtonsHaveFrame: - ret = false; - break; - case SH_ScrollBar_Transient: - if ((qobject_cast(w) && w->parent() && - qobject_cast(w->parent()->parent())) -#ifndef QT_NO_ACCESSIBILITY - || (opt && QStyleHelper::hasAncestor(opt->styleObject, QAccessible::ScrollBar)) -#endif - ) { - ret = [NSScroller preferredScrollerStyle] == NSScrollerStyleOverlay; - } - break; - case SH_ItemView_ScrollMode: - ret = QAbstractItemView::ScrollPerPixel; - break; - case SH_TitleBar_ShowToolTipsOnButtons: - // min/max/close buttons on windows don't show tool tips - ret = false; - break; - case SH_ComboBox_AllowWheelScrolling: - ret = false; - break; - case SH_SpinBox_ButtonsInsideFrame: - ret = false; - break; - case SH_Table_GridLineColor: - ret = int(qt_mac_toQColor(NSColor.gridColor).rgb()); - break; - default: - ret = QCommonStyle::styleHint(sh, opt, w, hret); - break; - } - return ret; -} - -QPixmap QMacStyle::generatedIconPixmap(QIcon::Mode iconMode, const QPixmap &pixmap, - const QStyleOption *opt) const -{ - switch (iconMode) { - case QIcon::Disabled: { - QImage img = pixmap.toImage().convertToFormat(QImage::Format_ARGB32); - int imgh = img.height(); - int imgw = img.width(); - QRgb pixel; - for (int y = 0; y < imgh; ++y) { - for (int x = 0; x < imgw; ++x) { - pixel = img.pixel(x, y); - img.setPixel(x, y, qRgba(qRed(pixel), qGreen(pixel), qBlue(pixel), - qAlpha(pixel) / 2)); - } - } - return QPixmap::fromImage(img); - } - default: - ; - } - return QCommonStyle::generatedIconPixmap(iconMode, pixmap, opt); -} - - -QPixmap QMacStyle::standardPixmap(StandardPixmap standardPixmap, const QStyleOption *opt, - const QWidget *widget) const -{ - // The default implementation of QStyle::standardIconImplementation() is to call standardPixmap() - // I don't want infinite recursion so if we do get in that situation, just return the Window's - // standard pixmap instead (since there is no mac-specific icon then). This should be fine until - // someone changes how Windows standard - // pixmap works. - static bool recursionGuard = false; - - if (recursionGuard) - return QCommonStyle::standardPixmap(standardPixmap, opt, widget); - - recursionGuard = true; - QIcon icon = proxy()->standardIcon(standardPixmap, opt, widget); - recursionGuard = false; - int size; - switch (standardPixmap) { - default: - size = 32; - break; - case SP_MessageBoxCritical: - case SP_MessageBoxQuestion: - case SP_MessageBoxInformation: - case SP_MessageBoxWarning: - size = 64; - break; - } - return icon.pixmap(qt_getWindow(widget), QSize(size, size)); -} - -void QMacStyle::drawPrimitive(PrimitiveElement pe, const QStyleOption *opt, QPainter *p, - const QWidget *w) const -{ - Q_D(const QMacStyle); - QMacCGContext cg(p); - QWindow *window = w && w->window() ? w->window()->windowHandle() : nullptr; - d->resolveCurrentNSView(window); - switch (pe) { - case PE_IndicatorArrowUp: - case PE_IndicatorArrowDown: - case PE_IndicatorArrowRight: - case PE_IndicatorArrowLeft: { - p->save(); - p->setRenderHint(QPainter::Antialiasing); - const int xOffset = 1; // FIXME: opt->direction == Qt::LeftToRight ? 2 : -1; - qreal halfSize = 0.5 * qMin(opt->rect.width(), opt->rect.height()); - const qreal penWidth = qMax(halfSize / 3.0, 1.25); -#if QT_CONFIG(toolbutton) - if (const QToolButton *tb = qobject_cast(w)) { - // When stroking the arrow, make sure it fits in the tool button - if (tb->arrowType() != Qt::NoArrow - || tb->popupMode() == QToolButton::MenuButtonPopup) - halfSize -= penWidth; - } -#endif - - QMatrix matrix; - matrix.translate(opt->rect.center().x() + xOffset, opt->rect.center().y() + 2); - QPainterPath path; - switch(pe) { - default: - case PE_IndicatorArrowDown: - break; - case PE_IndicatorArrowUp: - matrix.rotate(180); - break; - case PE_IndicatorArrowLeft: - matrix.rotate(90); - break; - case PE_IndicatorArrowRight: - matrix.rotate(-90); - break; - } - p->setMatrix(matrix); - - path.moveTo(-halfSize, -halfSize * 0.5); - path.lineTo(0.0, halfSize * 0.5); - path.lineTo(halfSize, -halfSize * 0.5); - - const QPen arrowPen(opt->palette.text(), penWidth, - Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin); - p->strokePath(path, arrowPen); - p->restore(); - break; } -#if QT_CONFIG(tabbar) - case PE_FrameTabBarBase: - if (const QStyleOptionTabBarBase *tbb - = qstyleoption_cast(opt)) { - if (tbb->documentMode) { - p->save(); - drawTabBase(p, tbb, w); - p->restore(); - return; - } - - QRegion region(tbb->rect); - region -= tbb->tabBarRect; - p->save(); - p->setClipRegion(region); - QStyleOptionTabWidgetFrame twf; - twf.QStyleOption::operator=(*tbb); - twf.shape = tbb->shape; - switch (QMacStylePrivate::tabDirection(twf.shape)) { - case QMacStylePrivate::North: - twf.rect = twf.rect.adjusted(0, 0, 0, 10); - break; - case QMacStylePrivate::South: - twf.rect = twf.rect.adjusted(0, -10, 0, 0); - break; - case QMacStylePrivate::West: - twf.rect = twf.rect.adjusted(0, 0, 10, 0); - break; - case QMacStylePrivate::East: - twf.rect = twf.rect.adjusted(0, -10, 0, 0); - break; - } - proxy()->drawPrimitive(PE_FrameTabWidget, &twf, p, w); - p->restore(); - } - break; -#endif - case PE_PanelTipLabel: - p->fillRect(opt->rect, opt->palette.brush(QPalette::ToolTipBase)); - break; - case PE_FrameGroupBox: - if (const auto *groupBox = qstyleoption_cast(opt)) - if (groupBox->features & QStyleOptionFrame::Flat) { - QCommonStyle::drawPrimitive(pe, groupBox, p, w); - break; - } -#if QT_CONFIG(tabwidget) - Q_FALLTHROUGH(); - case PE_FrameTabWidget: -#endif - { - const auto cw = QMacStylePrivate::CocoaControl(QMacStylePrivate::Box, QStyleHelper::SizeLarge); - auto *box = static_cast(d->cocoaControl(cw)); - // FIXME Since macOS 10.14, simply calling drawRect: won't display anything anymore. - // The AppKit team is aware of this and has proposed a couple of solutions. - // The first solution was to call displayRectIgnoringOpacity:inContext: instead. - // However, it doesn't seem to work on 10.13. More importantly, dark mode on 10.14 - // is extremely slow. Light mode works fine. - // The second solution is to subclass NSBox and reimplement a trivial drawRect: which - // would only call super. This works without any issue on 10.13, but a double border - // shows on 10.14 in both light and dark modes. - // The code below picks what works on each version and mode. On 10.13 and earlier, we - // simply call drawRect: on a regular NSBox. On 10.14, we call displayRectIgnoringOpacity: - // inContext:, but only in light mode. In dark mode, we use a custom NSBox subclass, - // QDarkNSBox, of type NSBoxCustom. Its appearance is close enough to the real thing so - // we can use this for now. - auto adjustedRect = opt->rect; - bool needTranslation = false; - if (QOperatingSystemVersion::current() >= QOperatingSystemVersion::MacOSMojave - && !qt_mac_applicationIsInDarkMode()) { - // Another surprise from AppKit (SDK 10.14) - -displayRectIgnoringOpacity: - // is different from drawRect: for some Apple-known reason box is smaller - // in height than we need, resulting in tab buttons sitting too high/not - // centered. Attempts to play with insets etc did not work - the same wrong - // height. Simple translation is not working (too much space "at bottom"), - // so we make it bigger and translate (otherwise it's clipped at bottom btw). - adjustedRect.adjust(0, 0, 0, 3); - needTranslation = true; - } - d->drawNSViewInRect(box, adjustedRect, p, ^(CGContextRef ctx, const CGRect &rect) { - if (QTabWidget *tabWidget = qobject_cast(opt->styleObject)) - clipTabBarFrame(opt, this, ctx); - CGContextTranslateCTM(ctx, 0, rect.origin.y + rect.size.height); - CGContextScaleCTM(ctx, 1, -1); - if (QOperatingSystemVersion::current() < QOperatingSystemVersion::MacOSMojave - || [box isMemberOfClass:QDarkNSBox.class]) { - [box drawRect:rect]; - } else { - if (needTranslation) - CGContextTranslateCTM(ctx, 0.0, 4.0); - [box displayRectIgnoringOpacity:box.bounds inContext:NSGraphicsContext.currentContext]; - } - }); - break; - } - case PE_IndicatorToolBarSeparator: { - QPainterPath path; - if (opt->state & State_Horizontal) { - int xpoint = opt->rect.center().x(); - path.moveTo(xpoint + 0.5, opt->rect.top() + 1); - path.lineTo(xpoint + 0.5, opt->rect.bottom()); - } else { - int ypoint = opt->rect.center().y(); - path.moveTo(opt->rect.left() + 2 , ypoint + 0.5); - path.lineTo(opt->rect.right() + 1, ypoint + 0.5); - } - QPainterPathStroker theStroker; - theStroker.setCapStyle(Qt::FlatCap); - theStroker.setDashPattern(QVector() << 1 << 2); - path = theStroker.createStroke(path); - p->fillPath(path, QColor(0, 0, 0, 119)); - } - break; - case PE_FrameWindow: - if (const QStyleOptionFrame *frame = qstyleoption_cast(opt)) { - if (w && w->inherits("QMdiSubWindow")) { - p->save(); - p->setPen(QPen(frame->palette.dark().color(), frame->lineWidth)); - p->setBrush(frame->palette.window()); - p->drawRect(frame->rect); - p->restore(); - } - } - break; - case PE_IndicatorDockWidgetResizeHandle: { - // The docwidget resize handle is drawn as a one-pixel wide line. - p->save(); - if (opt->state & State_Horizontal) { - p->setPen(QColor(160, 160, 160)); - p->drawLine(opt->rect.topLeft(), opt->rect.topRight()); - } else { - p->setPen(QColor(145, 145, 145)); - p->drawLine(opt->rect.topRight(), opt->rect.bottomRight()); - } - p->restore(); - } break; - case PE_IndicatorToolBarHandle: { - p->save(); - QPainterPath path; - int x = opt->rect.x() + 6; - int y = opt->rect.y() + 7; - static const int RectHeight = 2; - if (opt->state & State_Horizontal) { - while (y < opt->rect.height() - RectHeight - 5) { - path.moveTo(x, y); - path.addEllipse(x, y, RectHeight, RectHeight); - y += 6; - } - } else { - while (x < opt->rect.width() - RectHeight - 5) { - path.moveTo(x, y); - path.addEllipse(x, y, RectHeight, RectHeight); - x += 6; - } - } - p->setPen(Qt::NoPen); - QColor dark = opt->palette.dark().color().darker(); - dark.setAlphaF(0.50); - p->fillPath(path, dark); - p->restore(); - - break; - } - case PE_IndicatorHeaderArrow: - if (const QStyleOptionHeader *header = qstyleoption_cast(opt)) { - // In HITheme, up is down, down is up and hamburgers eat people. - if (header->sortIndicator != QStyleOptionHeader::None) - proxy()->drawPrimitive( - (header->sortIndicator == QStyleOptionHeader::SortDown) ? - PE_IndicatorArrowUp : PE_IndicatorArrowDown, header, p, w); - } - break; - case PE_IndicatorMenuCheckMark: { - QColor pc; - if (opt->state & State_On) - pc = opt->palette.highlightedText().color(); - else - pc = opt->palette.text().color(); - - QCFType checkmarkColor = CGColorCreateGenericRGB(static_cast(pc.redF()), - static_cast(pc.greenF()), - static_cast(pc.blueF()), - static_cast(pc.alphaF())); - // kCTFontUIFontSystem and others give the same result - // as kCTFontUIFontMenuItemMark. However, the latter is - // more reminiscent to HITheme's kThemeMenuItemMarkFont. - // See also the font for small- and mini-sized widgets, - // where we end up using the generic system font type. - const CTFontUIFontType fontType = (opt->state & State_Mini) ? kCTFontUIFontMiniSystem : - (opt->state & State_Small) ? kCTFontUIFontSmallSystem : - kCTFontUIFontMenuItemMark; - // Similarly for the font size, where there is a small difference - // between regular combobox and item view items, and and menu items. - // However, we ignore any difference for small- and mini-sized widgets. - const CGFloat fontSize = fontType == kCTFontUIFontMenuItemMark ? opt->fontMetrics.height() : 0.0; - QCFType checkmarkFont = CTFontCreateUIFontForLanguage(fontType, fontSize, NULL); - - CGContextSaveGState(cg); - CGContextSetShouldSmoothFonts(cg, NO); // Same as HITheme and Cocoa menu checkmarks - - // Baseline alignment tweaks for QComboBox and QMenu - const CGFloat vOffset = (opt->state & State_Mini) ? 0.0 : - (opt->state & State_Small) ? 1.0 : - 0.75; - - CGContextTranslateCTM(cg, 0, opt->rect.bottom()); - CGContextScaleCTM(cg, 1, -1); - // Translate back to the original position and add rect origin and offset - CGContextTranslateCTM(cg, opt->rect.x(), vOffset); - - // CTFont has severe difficulties finding the checkmark character among its - // glyphs. Fortunately, CTLine knows its ways inside the Cocoa labyrinth. - static const CFStringRef keys[] = { kCTFontAttributeName, kCTForegroundColorAttributeName }; - static const int numValues = sizeof(keys) / sizeof(keys[0]); - const CFTypeRef values[] = { (CFTypeRef)checkmarkFont, (CFTypeRef)checkmarkColor }; - Q_STATIC_ASSERT((sizeof(values) / sizeof(values[0])) == numValues); - QCFType attributes = CFDictionaryCreate(kCFAllocatorDefault, (const void **)keys, (const void **)values, - numValues, NULL, NULL); - // U+2713: CHECK MARK - QCFType checkmarkString = CFAttributedStringCreate(kCFAllocatorDefault, (CFStringRef)@"\u2713", attributes); - QCFType line = CTLineCreateWithAttributedString(checkmarkString); - - CTLineDraw((CTLineRef)line, cg); - CGContextFlush(cg); // CTLineDraw's documentation says it doesn't flush - - CGContextRestoreGState(cg); - break; } - case PE_IndicatorViewItemCheck: - case PE_IndicatorRadioButton: - case PE_IndicatorCheckBox: { - const bool isEnabled = opt->state & State_Enabled; - const bool isPressed = opt->state & State_Sunken; - const bool isRadioButton = (pe == PE_IndicatorRadioButton); - const auto ct = isRadioButton ? QMacStylePrivate::Button_RadioButton : QMacStylePrivate::Button_CheckBox; - const auto cs = d->effectiveAquaSizeConstrain(opt, w); - const auto cw = QMacStylePrivate::CocoaControl(ct, cs); - auto *tb = static_cast(d->cocoaControl(cw)); - tb.enabled = isEnabled; - tb.state = (opt->state & State_NoChange) ? NSMixedState : - (opt->state & State_On) ? NSOnState : NSOffState; - [tb highlight:isPressed]; - const auto vOffset = [=] { - // As measured - if (cs == QStyleHelper::SizeMini) - return ct == QMacStylePrivate::Button_CheckBox ? -0.5 : 0.5; - - return cs == QStyleHelper::SizeSmall ? 0.5 : 0.0; - } (); - d->drawNSViewInRect(tb, opt->rect, p, ^(CGContextRef ctx, const CGRect &rect) { - CGContextTranslateCTM(ctx, 0, vOffset); - [tb.cell drawInteriorWithFrame:rect inView:tb]; - }); - break; } - case PE_FrameFocusRect: - // Use the our own focus widget stuff. - break; - case PE_IndicatorBranch: { - if (!(opt->state & State_Children)) - break; - const auto cw = QMacStylePrivate::CocoaControl(QMacStylePrivate::Button_Disclosure, QStyleHelper::SizeLarge); - NSButtonCell *triangleCell = static_cast(d->cocoaCell(cw)); - [triangleCell setState:(opt->state & State_Open) ? NSOnState : NSOffState]; - bool viewHasFocus = (w && w->hasFocus()) || (opt->state & State_HasFocus); - [triangleCell setBackgroundStyle:((opt->state & State_Selected) && viewHasFocus) ? NSBackgroundStyleDark : NSBackgroundStyleLight]; - - d->setupNSGraphicsContext(cg, NO); - - QRect qtRect = opt->rect.adjusted(DisclosureOffset, 0, -DisclosureOffset, 0); - CGRect rect = CGRectMake(qtRect.x() + 1, qtRect.y(), qtRect.width(), qtRect.height()); - CGContextTranslateCTM(cg, rect.origin.x, rect.origin.y + rect.size.height); - CGContextScaleCTM(cg, 1, -1); - CGContextTranslateCTM(cg, -rect.origin.x, -rect.origin.y); - - if (QOperatingSystemVersion::current() >= QOperatingSystemVersion::MacOSMojave && !qt_mac_applicationIsInDarkMode()) { - // When the real system theme is one of the 'Dark' themes, and an application forces the 'Aqua' theme, - // under some conditions (see QTBUG-74515 for more details) NSButtonCell seems to select the 'Dark' - // code path and is becoming transparent, thus 'invisible' on the white background. To workaround this, - // we draw the disclose triangle manually: - qt_drawDisclosureButton(cg, triangleCell.state, (opt->state & State_Selected) && viewHasFocus, rect); - } else { - [triangleCell drawBezelWithFrame:NSRectFromCGRect(rect) inView:[triangleCell controlView]]; - } - d->restoreNSGraphicsContext(cg); - break; } - - case PE_Frame: { - QPen oldPen = p->pen(); - p->setPen(opt->palette.base().color().darker(140)); - p->drawRect(opt->rect.adjusted(0, 0, -1, -1)); - p->setPen(opt->palette.base().color().darker(180)); - p->drawLine(opt->rect.topLeft(), opt->rect.topRight()); - p->setPen(oldPen); - break; } - - case PE_FrameLineEdit: - if (const QStyleOptionFrame *frame = qstyleoption_cast(opt)) { - if (frame->state & State_Sunken) { - const bool isEnabled = opt->state & State_Enabled; - const bool isReadOnly = opt->state & State_ReadOnly; - const bool isRounded = frame->features & QStyleOptionFrame::Rounded; - const auto cs = d->effectiveAquaSizeConstrain(opt, w, CT_LineEdit); - const auto cw = QMacStylePrivate::CocoaControl(QMacStylePrivate::TextField, cs); - auto *tf = static_cast(d->cocoaControl(cw)); - tf.enabled = isEnabled; - tf.editable = !isReadOnly; - tf.bezeled = YES; - static_cast(tf.cell).bezelStyle = isRounded ? NSTextFieldRoundedBezel : NSTextFieldSquareBezel; - tf.frame = opt->rect.toCGRect(); - d->drawNSViewInRect(tf, opt->rect, p, ^(CGContextRef, const CGRect &rect) { - if (!qt_mac_applicationIsInDarkMode()) { - // In 'Dark' mode controls are transparent, so we do not - // over-paint the (potentially custom) color in the background. - // In 'Light' mode we have to care about the correct - // background color. See the comments below for PE_PanelLineEdit. - CGContextRef cgContext = NSGraphicsContext.currentContext.CGContext; - // See QMacCGContext, here we expect bitmap context created with - // color space 'kCGColorSpaceSRGB', if it's something else - we - // give up. - if (cgContext ? bool(CGBitmapContextGetColorSpace(cgContext)) : false) { - tf.drawsBackground = YES; - const QColor bgColor = frame->palette.brush(QPalette::Base).color(); - tf.backgroundColor = [NSColor colorWithSRGBRed:bgColor.redF() - green:bgColor.greenF() - blue:bgColor.blueF() - alpha:bgColor.alphaF()]; - if (bgColor.alpha() != 255) { - // No way we can have it bezeled and transparent ... - tf.bordered = YES; - } - } - } - - [tf.cell drawWithFrame:rect inView:tf]; - }); - } else { - QCommonStyle::drawPrimitive(pe, opt, p, w); - } - } - break; - case PE_PanelLineEdit: - { - const QStyleOptionFrame *panel = qstyleoption_cast(opt); - if (qt_mac_applicationIsInDarkMode() || (panel && panel->lineWidth <= 0)) { - // QCommonStyle::drawPrimitive(PE_PanelLineEdit) fill the background with - // a proper color, defined in opt->palette and then, if lineWidth > 0, it - // calls QMacStyle::drawPrimitive(PE_FrameLineEdit). We use NSTextFieldCell - // to handle PE_FrameLineEdit, which will use system-default background. - // In 'Dark' mode it's transparent and thus it's not over-painted. - QCommonStyle::drawPrimitive(pe, opt, p, w); - } else { - // In 'Light' mode, if panel->lineWidth > 0, we have to use the correct - // background color when drawing PE_FrameLineEdit, so let's call it - // directly and set the proper color there. - drawPrimitive(PE_FrameLineEdit, opt, p, w); - } - - // Draw the focus frame for widgets other than QLineEdit (e.g. for line edits in Webkit). - // Focus frame is drawn outside the rectangle passed in the option-rect. - if (panel) { -#if QT_CONFIG(lineedit) - if ((opt->state & State_HasFocus) && !qobject_cast(w)) { - int vmargin = pixelMetric(QStyle::PM_FocusFrameVMargin); - int hmargin = pixelMetric(QStyle::PM_FocusFrameHMargin); - QStyleOptionFrame focusFrame = *panel; - focusFrame.rect = panel->rect.adjusted(-hmargin, -vmargin, hmargin, vmargin); - drawControl(CE_FocusFrame, &focusFrame, p, w); - } -#endif - } - } - break; - case PE_PanelScrollAreaCorner: { - const QBrush brush(opt->palette.brush(QPalette::Base)); - p->fillRect(opt->rect, brush); - p->setPen(QPen(QColor(217, 217, 217))); - p->drawLine(opt->rect.topLeft(), opt->rect.topRight()); - p->drawLine(opt->rect.topLeft(), opt->rect.bottomLeft()); - } break; - case PE_FrameStatusBarItem: - break; - case PE_IndicatorTabClose: { - // Make close button visible only on the hovered tab. - QTabBar *tabBar = qobject_cast(w->parentWidget()); - if (!tabBar) { - // QStyleSheetStyle instead of CloseButton (which has - // a QTabBar as a parent widget) uses the QTabBar itself: - tabBar = qobject_cast(const_cast(w)); - } - if (tabBar) { - const bool documentMode = tabBar->documentMode(); - const QTabBarPrivate *tabBarPrivate = static_cast(QObjectPrivate::get(tabBar)); - const int hoveredTabIndex = tabBarPrivate->hoveredTabIndex(); - if (!documentMode || - (hoveredTabIndex != -1 && ((w == tabBar->tabButton(hoveredTabIndex, QTabBar::LeftSide)) || - (w == tabBar->tabButton(hoveredTabIndex, QTabBar::RightSide))))) { - const bool hover = (opt->state & State_MouseOver); - const bool selected = (opt->state & State_Selected); - const bool pressed = (opt->state & State_Sunken); - drawTabCloseButton(p, hover, selected, pressed, documentMode); - } - } - } break; - case PE_PanelStatusBar: { - // Fill the status bar with the titlebar gradient. - QLinearGradient linearGrad; - if (w ? qt_macWindowMainWindow(w->window()) : (opt->state & QStyle::State_Active)) { - linearGrad = titlebarGradientActive(); - } else { - linearGrad = titlebarGradientInactive(); - } - - linearGrad.setStart(0, opt->rect.top()); - linearGrad.setFinalStop(0, opt->rect.bottom()); - p->fillRect(opt->rect, linearGrad); - - // Draw the black separator line at the top of the status bar. - if (w ? qt_macWindowMainWindow(w->window()) : (opt->state & QStyle::State_Active)) - p->setPen(titlebarSeparatorLineActive); - else - p->setPen(titlebarSeparatorLineInactive); - p->drawLine(opt->rect.left(), opt->rect.top(), opt->rect.right(), opt->rect.top()); - - break; - } - case PE_PanelMenu: { - p->save(); - p->fillRect(opt->rect, Qt::transparent); - p->setPen(Qt::transparent); - p->setBrush(opt->palette.window()); - p->setRenderHint(QPainter::Antialiasing, true); - const QPainterPath path = d->windowPanelPath(opt->rect); - p->drawPath(path); - p->restore(); - } break; - - default: - QCommonStyle::drawPrimitive(pe, opt, p, w); - break; - } -} - -static QPixmap darkenPixmap(const QPixmap &pixmap) -{ - QImage img = pixmap.toImage().convertToFormat(QImage::Format_ARGB32); - int imgh = img.height(); - int imgw = img.width(); - int h, s, v, a; - QRgb pixel; - for (int y = 0; y < imgh; ++y) { - for (int x = 0; x < imgw; ++x) { - pixel = img.pixel(x, y); - a = qAlpha(pixel); - QColor hsvColor(pixel); - hsvColor.getHsv(&h, &s, &v); - s = qMin(100, s * 2); - v = v / 2; - hsvColor.setHsv(h, s, v); - pixel = hsvColor.rgb(); - img.setPixel(x, y, qRgba(qRed(pixel), qGreen(pixel), qBlue(pixel), a)); - } - } - return QPixmap::fromImage(img); -} - -void QMacStylePrivate::setupVerticalInvertedXform(CGContextRef cg, bool reverse, bool vertical, const CGRect &rect) const -{ - if (vertical) { - CGContextTranslateCTM(cg, rect.size.height, 0); - CGContextRotateCTM(cg, M_PI_2); - } - if (vertical != reverse) { - CGContextTranslateCTM(cg, rect.size.width, 0); - CGContextScaleCTM(cg, -1, 1); - } -} - -void QMacStyle::drawControl(ControlElement ce, const QStyleOption *opt, QPainter *p, - const QWidget *w) const -{ - Q_D(const QMacStyle); - QMacCGContext cg(p); - QWindow *window = w && w->window() ? w->window()->windowHandle() : nullptr; - d->resolveCurrentNSView(window); - switch (ce) { - case CE_HeaderSection: - if (const QStyleOptionHeader *header = qstyleoption_cast(opt)) { - State flags = header->state; - QRect ir = header->rect; - - -#if 0 // FIXME: What's this solving exactly? - bool noVerticalHeader = true; -#if QT_CONFIG(tableview) - if (w) - if (const QTableView *table = qobject_cast(w->parentWidget())) - noVerticalHeader = !table->verticalHeader()->isVisible(); -#endif - - const bool drawLeftBorder = header->orientation == Qt::Vertical - || header->position == QStyleOptionHeader::OnlyOneSection - || (header->position == QStyleOptionHeader::Beginning && noVerticalHeader); -#endif - - const bool pressed = (flags & State_Sunken) && !(flags & State_On); - p->fillRect(ir, pressed ? header->palette.dark() : header->palette.button()); - p->setPen(QPen(header->palette.dark(), 1.0)); - if (header->orientation == Qt::Horizontal) - p->drawLine(QLineF(ir.right() + 0.5, ir.top() + headerSectionSeparatorInset, - ir.right() + 0.5, ir.bottom() - headerSectionSeparatorInset)); - else - p->drawLine(QLineF(ir.left() + headerSectionSeparatorInset, ir.bottom(), - ir.right() - headerSectionSeparatorInset, ir.bottom())); - } - - break; - case CE_HeaderLabel: - if (const QStyleOptionHeader *header = qstyleoption_cast(opt)) { - p->save(); - QRect textr = header->rect; - if (!header->icon.isNull()) { - QIcon::Mode mode = QIcon::Disabled; - if (opt->state & State_Enabled) - mode = QIcon::Normal; - int iconExtent = proxy()->pixelMetric(PM_SmallIconSize); - QPixmap pixmap = header->icon.pixmap(window, QSize(iconExtent, iconExtent), mode); - - QRect pixr = header->rect; - pixr.setY(header->rect.center().y() - (pixmap.height() / pixmap.devicePixelRatio() - 1) / 2); - proxy()->drawItemPixmap(p, pixr, Qt::AlignVCenter, pixmap); - textr.translate(pixmap.width() / pixmap.devicePixelRatio() + 2, 0); - } - - proxy()->drawItemText(p, textr, header->textAlignment | Qt::AlignVCenter, header->palette, - header->state & State_Enabled, header->text, QPalette::ButtonText); - p->restore(); - } - break; - case CE_ToolButtonLabel: - if (const QStyleOptionToolButton *tb = qstyleoption_cast(opt)) { - QStyleOptionToolButton myTb = *tb; - myTb.state &= ~State_AutoRaise; -#ifndef QT_NO_ACCESSIBILITY - if (QStyleHelper::hasAncestor(opt->styleObject, QAccessible::ToolBar)) { - QRect cr = tb->rect; - int shiftX = 0; - int shiftY = 0; - bool needText = false; - int alignment = 0; - bool down = tb->state & (State_Sunken | State_On); - if (down) { - shiftX = proxy()->pixelMetric(PM_ButtonShiftHorizontal, tb, w); - shiftY = proxy()->pixelMetric(PM_ButtonShiftVertical, tb, w); - } - // The down state is special for QToolButtons in a toolbar on the Mac - // The text is a bit bolder and gets a drop shadow and the icons are also darkened. - // This doesn't really fit into any particular case in QIcon, so we - // do the majority of the work ourselves. - if (!(tb->features & QStyleOptionToolButton::Arrow)) { - Qt::ToolButtonStyle tbstyle = tb->toolButtonStyle; - if (tb->icon.isNull() && !tb->text.isEmpty()) - tbstyle = Qt::ToolButtonTextOnly; - - switch (tbstyle) { - case Qt::ToolButtonTextOnly: { - needText = true; - alignment = Qt::AlignCenter; - break; } - case Qt::ToolButtonIconOnly: - case Qt::ToolButtonTextBesideIcon: - case Qt::ToolButtonTextUnderIcon: { - QRect pr = cr; - QIcon::Mode iconMode = (tb->state & State_Enabled) ? QIcon::Normal - : QIcon::Disabled; - QIcon::State iconState = (tb->state & State_On) ? QIcon::On - : QIcon::Off; - QPixmap pixmap = tb->icon.pixmap(window, - tb->rect.size().boundedTo(tb->iconSize), - iconMode, iconState); - - // Draw the text if it's needed. - if (tb->toolButtonStyle != Qt::ToolButtonIconOnly) { - needText = true; - if (tb->toolButtonStyle == Qt::ToolButtonTextUnderIcon) { - pr.setHeight(pixmap.size().height() / pixmap.devicePixelRatio() + 6); - cr.adjust(0, pr.bottom(), 0, -3); - alignment |= Qt::AlignCenter; - } else { - pr.setWidth(pixmap.width() / pixmap.devicePixelRatio() + 8); - cr.adjust(pr.right(), 0, 0, 0); - alignment |= Qt::AlignLeft | Qt::AlignVCenter; - } - } - if (opt->state & State_Sunken) { - pr.translate(shiftX, shiftY); - pixmap = darkenPixmap(pixmap); - } - proxy()->drawItemPixmap(p, pr, Qt::AlignCenter, pixmap); - break; } - default: - Q_ASSERT(false); - break; - } - - if (needText) { - QPalette pal = tb->palette; - QPalette::ColorRole role = QPalette::NoRole; - if (!proxy()->styleHint(SH_UnderlineShortcut, tb, w)) - alignment |= Qt::TextHideMnemonic; - if (down) - cr.translate(shiftX, shiftY); - if (tbstyle == Qt::ToolButtonTextOnly - || (tbstyle != Qt::ToolButtonTextOnly && !down)) { - QPen pen = p->pen(); - QColor light = down || isDarkMode() ? Qt::black : Qt::white; - light.setAlphaF(0.375f); - p->setPen(light); - p->drawText(cr.adjusted(0, 1, 0, 1), alignment, tb->text); - p->setPen(pen); - if (down && tbstyle == Qt::ToolButtonTextOnly) { - pal = QApplication::palette("QMenu"); - pal.setCurrentColorGroup(tb->palette.currentColorGroup()); - role = QPalette::HighlightedText; - } - } - proxy()->drawItemText(p, cr, alignment, pal, - tb->state & State_Enabled, tb->text, role); - } - } else { - QCommonStyle::drawControl(ce, &myTb, p, w); - } - } else -#endif // QT_NO_ACCESSIBILITY - { - QCommonStyle::drawControl(ce, &myTb, p, w); - } - } - break; - case CE_ToolBoxTabShape: - QCommonStyle::drawControl(ce, opt, p, w); - break; - case CE_PushButtonBevel: - if (const QStyleOptionButton *btn = qstyleoption_cast(opt)) { - if (!(btn->state & (State_Raised | State_Sunken | State_On))) - break; - - if (btn->features & QStyleOptionButton::CommandLinkButton) { - QCommonStyle::drawControl(ce, opt, p, w); - break; - } - - const bool hasFocus = btn->state & State_HasFocus; - const bool isActive = btn->state & State_Active; - - // a focused auto-default button within an active window - // takes precedence over a normal default button - if ((btn->features & QStyleOptionButton::AutoDefaultButton) - && isActive && hasFocus) - d->autoDefaultButton = btn->styleObject; - else if (d->autoDefaultButton == btn->styleObject) - d->autoDefaultButton = nullptr; - - const bool isEnabled = btn->state & State_Enabled; - const bool isPressed = btn->state & State_Sunken; - const bool isHighlighted = isActive && - ((btn->state & State_On) - || (btn->features & QStyleOptionButton::DefaultButton) - || (btn->features & QStyleOptionButton::AutoDefaultButton - && d->autoDefaultButton == btn->styleObject)); - const bool hasMenu = btn->features & QStyleOptionButton::HasMenu; - const auto ct = cocoaControlType(btn, w); - const auto cs = d->effectiveAquaSizeConstrain(btn, w); - const auto cw = QMacStylePrivate::CocoaControl(ct, cs); - auto *pb = static_cast(d->cocoaControl(cw)); - // Ensure same size and location as we used to have with HITheme. - // This is more convoluted than we initialy thought. See for example - // differences between plain and menu button frames. - const QRectF frameRect = cw.adjustedControlFrame(btn->rect); - pb.frame = frameRect.toCGRect(); - - pb.enabled = isEnabled; - [pb highlight:isPressed]; - pb.state = isHighlighted && !isPressed ? NSOnState : NSOffState; - d->drawNSViewInRect(pb, frameRect, p, ^(CGContextRef, const CGRect &r) { - [pb.cell drawBezelWithFrame:r inView:pb.superview]; - }); - [pb highlight:NO]; - - if (hasMenu && cw.type == QMacStylePrivate::Button_SquareButton) { - // Using -[NSPopuButtonCell drawWithFrame:inView:] above won't do - // it right because we don't set the text in the native button. - const int mbi = proxy()->pixelMetric(QStyle::PM_MenuButtonIndicator, btn, w); - const auto ir = frameRect.toRect(); - int arrowYOffset = 0; -#if 0 - // FIXME What's this for again? - if (!w) { - // adjustment for Qt Quick Controls - arrowYOffset -= ir.top(); - if (cw.second == QStyleHelper::SizeSmall) - arrowYOffset += 1; - } -#endif - const auto ar = visualRect(btn->direction, ir, QRect(ir.right() - mbi - 6, ir.height() / 2 - arrowYOffset, mbi, mbi)); - - QStyleOption arrowOpt = *opt; - arrowOpt.rect = ar; - proxy()->drawPrimitive(PE_IndicatorArrowDown, &arrowOpt, p, w); - } - - - if (btn->state & State_HasFocus) { - // TODO Remove and use QFocusFrame instead. - const int hMargin = proxy()->pixelMetric(QStyle::PM_FocusFrameHMargin, btn, w); - const int vMargin = proxy()->pixelMetric(QStyle::PM_FocusFrameVMargin, btn, w); - QRectF focusRect; - if (cw.type == QMacStylePrivate::Button_SquareButton) { - focusRect = frameRect; - } else { - focusRect = QRectF::fromCGRect([pb alignmentRectForFrame:pb.frame]); - if (cw.type == QMacStylePrivate::Button_PushButton) - focusRect -= pushButtonShadowMargins[cw.size]; - else if (cw.type == QMacStylePrivate::Button_PullDown) - focusRect -= pullDownButtonShadowMargins[cw.size]; - } - d->drawFocusRing(p, focusRect, hMargin, vMargin, cw); - } - } - break; - case CE_PushButtonLabel: - if (const QStyleOptionButton *b = qstyleoption_cast(opt)) { - QStyleOptionButton btn(*b); - // We really don't want the label to be drawn the same as on - // windows style if it has an icon and text, then it should be more like a - // tab. So, cheat a little here. However, if it *is* only an icon - // the windows style works great, so just use that implementation. - const bool isEnabled = btn.state & State_Enabled; - const bool hasMenu = btn.features & QStyleOptionButton::HasMenu; - const bool hasIcon = !btn.icon.isNull(); - const bool hasText = !btn.text.isEmpty(); - const bool isActive = btn.state & State_Active; - const bool isPressed = btn.state & State_Sunken; - - const auto ct = cocoaControlType(&btn, w); - - if (!hasMenu && ct != QMacStylePrivate::Button_SquareButton) { - if (isPressed - || (isActive && isEnabled - && ((btn.state & State_On) - || ((btn.features & QStyleOptionButton::DefaultButton) && !d->autoDefaultButton) - || d->autoDefaultButton == btn.styleObject))) - btn.palette.setColor(QPalette::ButtonText, Qt::white); - } - - if ((!hasIcon && !hasMenu) || (hasIcon && !hasText)) { - QCommonStyle::drawControl(ce, &btn, p, w); - } else { - QRect freeContentRect = btn.rect; - QRect textRect = itemTextRect( - btn.fontMetrics, freeContentRect, Qt::AlignCenter, isEnabled, btn.text); - if (hasMenu) { - textRect.moveTo(w ? 15 : 11, textRect.top()); // Supports Qt Quick Controls - } - // Draw the icon: - if (hasIcon) { - int contentW = textRect.width(); - if (hasMenu) - contentW += proxy()->pixelMetric(PM_MenuButtonIndicator) + 4; - QIcon::Mode mode = isEnabled ? QIcon::Normal : QIcon::Disabled; - if (mode == QIcon::Normal && btn.state & State_HasFocus) - mode = QIcon::Active; - // Decide if the icon is should be on or off: - QIcon::State state = QIcon::Off; - if (btn.state & State_On) - state = QIcon::On; - QPixmap pixmap = btn.icon.pixmap(window, btn.iconSize, mode, state); - int pixmapWidth = pixmap.width() / pixmap.devicePixelRatio(); - int pixmapHeight = pixmap.height() / pixmap.devicePixelRatio(); - contentW += pixmapWidth + QMacStylePrivate::PushButtonContentPadding; - int iconLeftOffset = freeContentRect.x() + (freeContentRect.width() - contentW) / 2; - int iconTopOffset = freeContentRect.y() + (freeContentRect.height() - pixmapHeight) / 2; - QRect iconDestRect(iconLeftOffset, iconTopOffset, pixmapWidth, pixmapHeight); - QRect visualIconDestRect = visualRect(btn.direction, freeContentRect, iconDestRect); - proxy()->drawItemPixmap(p, visualIconDestRect, Qt::AlignLeft | Qt::AlignVCenter, pixmap); - int newOffset = iconDestRect.x() + iconDestRect.width() - + QMacStylePrivate::PushButtonContentPadding - textRect.x(); - textRect.adjust(newOffset, 0, newOffset, 0); - } - // Draw the text: - if (hasText) { - textRect = visualRect(btn.direction, freeContentRect, textRect); - proxy()->drawItemText(p, textRect, Qt::AlignLeft | Qt::AlignVCenter | Qt::TextShowMnemonic, btn.palette, - isEnabled, btn.text, QPalette::ButtonText); - } - } - } - break; -#if QT_CONFIG(combobox) - case CE_ComboBoxLabel: - if (const auto *cb = qstyleoption_cast(opt)) { - auto comboCopy = *cb; - comboCopy.direction = Qt::LeftToRight; - // The rectangle will be adjusted to SC_ComboBoxEditField with comboboxEditBounds() - QCommonStyle::drawControl(CE_ComboBoxLabel, &comboCopy, p, w); - } - break; -#endif // #if QT_CONFIG(combobox) -#if QT_CONFIG(tabbar) - case CE_TabBarTabShape: - if (const auto *tabOpt = qstyleoption_cast(opt)) { - if (tabOpt->documentMode) { - p->save(); - bool isUnified = false; - if (w) { - QRect tabRect = tabOpt->rect; - QPoint windowTabStart = w->mapTo(w->window(), tabRect.topLeft()); - isUnified = isInMacUnifiedToolbarArea(w->window()->windowHandle(), windowTabStart.y()); - } - - const int tabOverlap = proxy()->pixelMetric(PM_TabBarTabOverlap, opt, w); - drawTabShape(p, tabOpt, isUnified, tabOverlap); - - p->restore(); - return; - } - - const bool isActive = tabOpt->state & State_Active; - const bool isEnabled = tabOpt->state & State_Enabled; - const bool isPressed = tabOpt->state & State_Sunken; - const bool isSelected = tabOpt->state & State_Selected; - const auto tabDirection = QMacStylePrivate::tabDirection(tabOpt->shape); - const bool verticalTabs = tabDirection == QMacStylePrivate::East - || tabDirection == QMacStylePrivate::West; - - QStyleOptionTab::TabPosition tp = tabOpt->position; - QStyleOptionTab::SelectedPosition sp = tabOpt->selectedPosition; - if (tabOpt->direction == Qt::RightToLeft && !verticalTabs) { - if (tp == QStyleOptionTab::Beginning) - tp = QStyleOptionTab::End; - else if (tp == QStyleOptionTab::End) - tp = QStyleOptionTab::Beginning; - - if (sp == QStyleOptionTab::NextIsSelected) - sp = QStyleOptionTab::PreviousIsSelected; - else if (sp == QStyleOptionTab::PreviousIsSelected) - sp = QStyleOptionTab::NextIsSelected; - } - - // Alas, NSSegmentedControl and NSSegmentedCell are letting us down. - // We're not able to draw it at will, either calling -[drawSegment: - // inFrame:withView:], -[drawRect:] or anything in between. Besides, - // there's no public API do draw the pressed state, AFAICS. We'll use - // a push NSButton instead and clip the CGContext. - // NOTE/TODO: this is not true. On 10.13 NSSegmentedControl works with - // some (black?) magic/magic dances, on 10.14 it simply works (was - // it fixed in AppKit?). But, indeed, we cannot make a tab 'pressed' - // with NSSegmentedControl (only selected), so we stay with buttons - // (mixing buttons and NSSegmentedControl for such a simple thing - // is too much work). - - const auto cs = d->effectiveAquaSizeConstrain(opt, w); - // Extra hacks to get the proper pressed appreance when not selected or selected and inactive - const bool needsInactiveHack = (!isActive && isSelected); - const auto ct = !needsInactiveHack && (isSelected || tp == QStyleOptionTab::OnlyOneTab) ? - QMacStylePrivate::Button_PushButton : - QMacStylePrivate::Button_PopupButton; - const bool isPopupButton = ct == QMacStylePrivate::Button_PopupButton; - const auto cw = QMacStylePrivate::CocoaControl(ct, cs); - auto *pb = static_cast(d->cocoaControl(cw)); - - auto vOffset = isPopupButton ? 1 : 2; - if (tabDirection == QMacStylePrivate::East) - vOffset -= 1; - const auto outerAdjust = isPopupButton ? 1 : 4; - const auto innerAdjust = isPopupButton ? 20 : 10; - QRectF frameRect = tabOpt->rect; - if (verticalTabs) - frameRect = QRectF(frameRect.y(), frameRect.x(), frameRect.height(), frameRect.width()); - // Adjust before clipping - frameRect = frameRect.translated(0, vOffset); - switch (tp) { - case QStyleOptionTab::Beginning: - // Pressed state hack: tweak adjustments in preparation for flip below - if (!isSelected && tabDirection == QMacStylePrivate::West) - frameRect = frameRect.adjusted(-innerAdjust, 0, outerAdjust, 0); - else - frameRect = frameRect.adjusted(-outerAdjust, 0, innerAdjust, 0); - break; - case QStyleOptionTab::Middle: - frameRect = frameRect.adjusted(-innerAdjust, 0, innerAdjust, 0); - break; - case QStyleOptionTab::End: - // Pressed state hack: tweak adjustments in preparation for flip below - if (isSelected || tabDirection == QMacStylePrivate::West) - frameRect = frameRect.adjusted(-innerAdjust, 0, outerAdjust, 0); - else - frameRect = frameRect.adjusted(-outerAdjust, 0, innerAdjust, 0); - break; - case QStyleOptionTab::OnlyOneTab: - frameRect = frameRect.adjusted(-outerAdjust, 0, outerAdjust, 0); - break; - } - pb.frame = frameRect.toCGRect(); - - pb.enabled = isEnabled; - [pb highlight:isPressed]; - // Set off state when inactive. See needsInactiveHack for when it's selected - pb.state = (isActive && isSelected && !isPressed) ? NSOnState : NSOffState; - - const auto drawBezelBlock = ^(CGContextRef ctx, const CGRect &r) { - CGContextClipToRect(ctx, opt->rect.toCGRect()); - if (!isSelected || needsInactiveHack) { - // Final stage of the pressed state hack: flip NSPopupButton rendering - if (!verticalTabs && tp == QStyleOptionTab::End) { - CGContextTranslateCTM(ctx, opt->rect.right(), 0); - CGContextScaleCTM(ctx, -1, 1); - CGContextTranslateCTM(ctx, -frameRect.left(), 0); - } else if (tabDirection == QMacStylePrivate::West && tp == QStyleOptionTab::Beginning) { - CGContextScaleCTM(ctx, 1, -1); - CGContextTranslateCTM(ctx, 0, -frameRect.right()); - } else if (tabDirection == QMacStylePrivate::East && tp == QStyleOptionTab::End) { - CGContextTranslateCTM(ctx, 0, opt->rect.bottom()); - CGContextScaleCTM(ctx, 1, -1); - CGContextTranslateCTM(ctx, 0, -frameRect.left()); - } - } - - // Rotate and translate CTM when vertical - // On macOS: positive angle is CW, negative is CCW - if (tabDirection == QMacStylePrivate::West) { - CGContextTranslateCTM(ctx, 0, frameRect.right()); - CGContextRotateCTM(ctx, -M_PI_2); - CGContextTranslateCTM(ctx, -frameRect.left(), 0); - } else if (tabDirection == QMacStylePrivate::East) { - CGContextTranslateCTM(ctx, opt->rect.right(), 0); - CGContextRotateCTM(ctx, M_PI_2); - } - - [pb.cell drawBezelWithFrame:r inView:pb.superview]; - }; - - if (needsInactiveHack) { - // First, render tab as non-selected tab on a pixamp - const qreal pixelRatio = p->device()->devicePixelRatioF(); - QImage tabPixmap(opt->rect.size() * pixelRatio, QImage::Format_ARGB32_Premultiplied); - tabPixmap.setDevicePixelRatio(pixelRatio); - tabPixmap.fill(Qt::transparent); - QPainter tabPainter(&tabPixmap); - d->drawNSViewInRect(pb, frameRect, &tabPainter, ^(CGContextRef ctx, const CGRect &r) { - CGContextTranslateCTM(ctx, -opt->rect.left(), -opt->rect.top()); - drawBezelBlock(ctx, r); - }); - tabPainter.end(); - - // Then, darken it with the proper shade of gray - const qreal inactiveGray = 0.898; // As measured - const int inactiveGray8 = qRound(inactiveGray * 255.0); - const QRgb inactiveGrayRGB = qRgb(inactiveGray8, inactiveGray8, inactiveGray8); - for (int l = 0; l < tabPixmap.height(); ++l) { - auto *line = reinterpret_cast(tabPixmap.scanLine(l)); - for (int i = 0; i < tabPixmap.width(); ++i) { - if (qAlpha(line[i]) == 255) { - line[i] = inactiveGrayRGB; - } else if (qAlpha(line[i]) > 128) { - const int g = qRound(inactiveGray * qRed(line[i])); - line[i] = qRgba(g, g, g, qAlpha(line[i])); - } - } - } - - // Finally, draw the tab pixmap on the current painter - p->drawImage(opt->rect, tabPixmap); - } else { - d->drawNSViewInRect(pb, frameRect, p, drawBezelBlock); - } - - if (!isSelected && sp != QStyleOptionTab::NextIsSelected - && tp != QStyleOptionTab::End - && tp != QStyleOptionTab::OnlyOneTab) { - static const QPen separatorPen(Qt::black, 1.0); - p->save(); - p->setOpacity(isEnabled ? 0.105 : 0.06); // As measured - p->setPen(separatorPen); - if (tabDirection == QMacStylePrivate::West) { - p->drawLine(QLineF(opt->rect.left() + 1.5, opt->rect.bottom(), - opt->rect.right() - 0.5, opt->rect.bottom())); - } else if (tabDirection == QMacStylePrivate::East) { - p->drawLine(QLineF(opt->rect.left(), opt->rect.bottom(), - opt->rect.right() - 0.5, opt->rect.bottom())); - } else { - p->drawLine(QLineF(opt->rect.right(), opt->rect.top() + 1.0, - opt->rect.right(), opt->rect.bottom() - 0.5)); - } - p->restore(); - } - - // TODO Needs size adjustment to fit the focus ring - if (tabOpt->state & State_HasFocus) { - QMacStylePrivate::CocoaControlType focusRingType; - switch (tp) { - case QStyleOptionTab::Beginning: - focusRingType = verticalTabs ? QMacStylePrivate::SegmentedControl_Last - : QMacStylePrivate::SegmentedControl_First; - break; - case QStyleOptionTab::Middle: - focusRingType = QMacStylePrivate::SegmentedControl_Middle; - break; - case QStyleOptionTab::End: - focusRingType = verticalTabs ? QMacStylePrivate::SegmentedControl_First - : QMacStylePrivate::SegmentedControl_Last; - break; - case QStyleOptionTab::OnlyOneTab: - focusRingType = QMacStylePrivate::SegmentedControl_Single; - break; - } - } - } - break; - case CE_TabBarTabLabel: - if (const auto *tab = qstyleoption_cast(opt)) { - QStyleOptionTab myTab = *tab; - const auto tabDirection = QMacStylePrivate::tabDirection(tab->shape); - const bool verticalTabs = tabDirection == QMacStylePrivate::East - || tabDirection == QMacStylePrivate::West; - - // Check to see if we use have the same as the system font - // (QComboMenuItem is internal and should never be seen by the - // outside world, unless they read the source, in which case, it's - // their own fault). - const bool nonDefaultFont = p->font() != qt_app_fonts_hash()->value("QComboMenuItem"); - - if (!myTab.documentMode && (myTab.state & State_Selected) && (myTab.state & State_Active)) - if (const auto *tabBar = qobject_cast(w)) - if (!tabBar->tabTextColor(tabBar->currentIndex()).isValid()) - myTab.palette.setColor(QPalette::WindowText, Qt::white); - - if (myTab.documentMode && isDarkMode()) { - bool active = (myTab.state & State_Selected) && (myTab.state & State_Active); - myTab.palette.setColor(QPalette::WindowText, active ? Qt::white : Qt::gray); - } - - int heightOffset = 0; - if (verticalTabs) { - heightOffset = -1; - } else if (nonDefaultFont) { - if (p->fontMetrics().height() == myTab.rect.height()) - heightOffset = 2; - } - myTab.rect.setHeight(myTab.rect.height() + heightOffset); - - QCommonStyle::drawControl(ce, &myTab, p, w); - } - break; -#endif -#if QT_CONFIG(dockwidget) - case CE_DockWidgetTitle: - if (const auto *dwOpt = qstyleoption_cast(opt)) { - const bool isVertical = dwOpt->verticalTitleBar; - const auto effectiveRect = isVertical ? opt->rect.transposed() : opt->rect; - p->save(); - if (isVertical) { - p->translate(effectiveRect.left(), effectiveRect.top() + effectiveRect.width()); - p->rotate(-90); - p->translate(-effectiveRect.left(), -effectiveRect.top()); - } - - // fill title bar background - QLinearGradient linearGrad; - linearGrad.setStart(QPointF(0, 0)); - linearGrad.setFinalStop(QPointF(0, 2 * effectiveRect.height())); - linearGrad.setColorAt(0, opt->palette.button().color()); - linearGrad.setColorAt(1, opt->palette.dark().color()); - p->fillRect(effectiveRect, linearGrad); - - // draw horizontal line at bottom - p->setPen(opt->palette.dark().color()); - p->drawLine(effectiveRect.bottomLeft(), effectiveRect.bottomRight()); - - if (!dwOpt->title.isEmpty()) { - auto titleRect = proxy()->subElementRect(SE_DockWidgetTitleBarText, opt, w); - if (isVertical) - titleRect = QRect(effectiveRect.left() + opt->rect.bottom() - titleRect.bottom(), - effectiveRect.top() + titleRect.left() - opt->rect.left(), - titleRect.height(), - titleRect.width()); - - const auto text = p->fontMetrics().elidedText(dwOpt->title, Qt::ElideRight, titleRect.width()); - proxy()->drawItemText(p, titleRect, Qt::AlignCenter | Qt::TextShowMnemonic, dwOpt->palette, - dwOpt->state & State_Enabled, text, QPalette::WindowText); - } - p->restore(); - } - break; -#endif - case CE_FocusFrame: { - const auto *ff = qobject_cast(w); - const auto *ffw = ff ? ff->widget() : nullptr; - const auto ct = [=] { - if (ffw) { - if (ffw->inherits("QCheckBox")) - return QMacStylePrivate::Button_CheckBox; - if (ffw->inherits("QRadioButton")) - return QMacStylePrivate::Button_RadioButton; - if (ffw->inherits("QLineEdit") || ffw->inherits("QTextEdit")) - return QMacStylePrivate::TextField; - } - - return QMacStylePrivate::Box; // Not really, just make it the default - } (); - const auto cs = ffw ? (ffw->testAttribute(Qt::WA_MacMiniSize) ? QStyleHelper::SizeMini : - ffw->testAttribute(Qt::WA_MacSmallSize) ? QStyleHelper::SizeSmall : - QStyleHelper::SizeLarge) : - QStyleHelper::SizeLarge; - const int hMargin = proxy()->pixelMetric(QStyle::PM_FocusFrameHMargin, opt, w); - const int vMargin = proxy()->pixelMetric(QStyle::PM_FocusFrameVMargin, opt, w); - d->drawFocusRing(p, opt->rect, hMargin, vMargin, QMacStylePrivate::CocoaControl(ct, cs)); - break; } - case CE_MenuEmptyArea: - // Skip: PE_PanelMenu fills in everything - break; - case CE_MenuItem: - case CE_MenuHMargin: - case CE_MenuVMargin: - case CE_MenuTearoff: - case CE_MenuScroller: - if (const QStyleOptionMenuItem *mi = qstyleoption_cast(opt)) { - const bool active = mi->state & State_Selected; - if (active) - p->fillRect(mi->rect, mi->palette.highlight()); - - const QStyleHelper::WidgetSizePolicy widgetSize = d->aquaSizeConstrain(opt, w); - - if (ce == CE_MenuTearoff) { - p->setPen(QPen(mi->palette.dark().color(), 1, Qt::DashLine)); - p->drawLine(mi->rect.x() + 2, mi->rect.y() + mi->rect.height() / 2 - 1, - mi->rect.x() + mi->rect.width() - 4, - mi->rect.y() + mi->rect.height() / 2 - 1); - p->setPen(QPen(mi->palette.light().color(), 1, Qt::DashLine)); - p->drawLine(mi->rect.x() + 2, mi->rect.y() + mi->rect.height() / 2, - mi->rect.x() + mi->rect.width() - 4, - mi->rect.y() + mi->rect.height() / 2); - } else if (ce == CE_MenuScroller) { - const QSize scrollerSize = QSize(10, 8); - const int scrollerVOffset = 5; - const int left = mi->rect.x() + (mi->rect.width() - scrollerSize.width()) / 2; - const int right = left + scrollerSize.width(); - int top; - int bottom; - if (opt->state & State_DownArrow) { - bottom = mi->rect.y() + scrollerVOffset; - top = bottom + scrollerSize.height(); - } else { - bottom = mi->rect.bottom() - scrollerVOffset; - top = bottom - scrollerSize.height(); - } - p->save(); - p->setRenderHint(QPainter::Antialiasing); - QPainterPath path; - path.moveTo(left, bottom); - path.lineTo(right, bottom); - path.lineTo((left + right) / 2, top); - p->fillPath(path, opt->palette.buttonText()); - p->restore(); - } else if (ce != CE_MenuItem) { - break; - } - - if (mi->menuItemType == QStyleOptionMenuItem::Separator) { - CGColorRef separatorColor = [NSColor quaternaryLabelColor].CGColor; - const QRect separatorRect = QRect(mi->rect.left(), mi->rect.center().y(), mi->rect.width(), 2); - p->fillRect(separatorRect, qt_mac_toQColor(separatorColor)); - break; - } - - const int maxpmw = mi->maxIconWidth; - const bool enabled = mi->state & State_Enabled; - - int xpos = mi->rect.x() + 18; - int checkcol = maxpmw; - if (!enabled) - p->setPen(mi->palette.text().color()); - else if (active) - p->setPen(mi->palette.highlightedText().color()); - else - p->setPen(mi->palette.buttonText().color()); - - if (mi->checked) { - QStyleOption checkmarkOpt; - checkmarkOpt.initFrom(w); - - const int mw = checkcol + macItemFrame; - const int mh = mi->rect.height() + macItemFrame; - const int xp = mi->rect.x() + macItemFrame; - checkmarkOpt.rect = QRect(xp, mi->rect.y() - checkmarkOpt.fontMetrics.descent(), mw, mh); - - checkmarkOpt.state.setFlag(State_On, active); - checkmarkOpt.state.setFlag(State_Enabled, enabled); - if (widgetSize == QStyleHelper::SizeMini) - checkmarkOpt.state |= State_Mini; - else if (widgetSize == QStyleHelper::SizeSmall) - checkmarkOpt.state |= State_Small; - - // We let drawPrimitive(PE_IndicatorMenuCheckMark) pick the right color - checkmarkOpt.palette.setColor(QPalette::HighlightedText, p->pen().color()); - checkmarkOpt.palette.setColor(QPalette::Text, p->pen().color()); - - proxy()->drawPrimitive(PE_IndicatorMenuCheckMark, &checkmarkOpt, p, w); - } - if (!mi->icon.isNull()) { - QIcon::Mode mode = (mi->state & State_Enabled) ? QIcon::Normal - : QIcon::Disabled; - // Always be normal or disabled to follow the Mac style. - int smallIconSize = proxy()->pixelMetric(PM_SmallIconSize); - QSize iconSize(smallIconSize, smallIconSize); -#if QT_CONFIG(combobox) - if (const QComboBox *comboBox = qobject_cast(w)) { - iconSize = comboBox->iconSize(); - } -#endif - QPixmap pixmap = mi->icon.pixmap(window, iconSize, mode); - int pixw = pixmap.width() / pixmap.devicePixelRatio(); - int pixh = pixmap.height() / pixmap.devicePixelRatio(); - QRect cr(xpos, mi->rect.y(), checkcol, mi->rect.height()); - QRect pmr(0, 0, pixw, pixh); - pmr.moveCenter(cr.center()); - p->drawPixmap(pmr.topLeft(), pixmap); - xpos += pixw + 6; - } - - QString s = mi->text; - const auto text_flags = Qt::AlignVCenter | Qt::TextHideMnemonic - | Qt::TextSingleLine | Qt::AlignAbsolute; - int yPos = mi->rect.y(); - if (widgetSize == QStyleHelper::SizeMini) - yPos += 1; - - const bool isSubMenu = mi->menuItemType == QStyleOptionMenuItem::SubMenu; - const int tabwidth = isSubMenu ? 9 : mi->tabWidth; - - QString rightMarginText; - if (isSubMenu) - rightMarginText = QStringLiteral("\u25b6\ufe0e"); // U+25B6 U+FE0E: BLACK RIGHT-POINTING TRIANGLE - - // If present, save and remove embedded shorcut from text - const int tabIndex = s.indexOf(QLatin1Char('\t')); - if (tabIndex >= 0) { - if (!isSubMenu) // ... but ignore it if it's a submenu. - rightMarginText = s.mid(tabIndex + 1); - s = s.left(tabIndex); - } - - p->save(); - if (!rightMarginText.isEmpty()) { - p->setFont(qt_app_fonts_hash()->value("QMenuItem", p->font())); - int xp = mi->rect.right() - tabwidth - macRightBorder + 2; - if (!isSubMenu) - xp -= macItemHMargin + macItemFrame + 3; // Adjust for shortcut - p->drawText(xp, yPos, tabwidth, mi->rect.height(), text_flags | Qt::AlignRight, rightMarginText); - } - - if (!s.isEmpty()) { - const int xm = macItemFrame + maxpmw + macItemHMargin; - QFont myFont = mi->font; - // myFont may not have any "hard" flags set. We override - // the point size so that when it is resolved against the device, this font will win. - // This is mainly to handle cases where someone sets the font on the window - // and then the combo inherits it and passes it onward. At that point the resolve mask - // is very, very weak. This makes it stonger. - myFont.setPointSizeF(QFontInfo(mi->font).pointSizeF()); - - // QTBUG-65653: Our own text rendering doesn't look good enough, especially on non-retina - // displays. Worked around here while waiting for a proper fix in QCoreTextFontEngine. - // Only if we're not using QCoreTextFontEngine we do fallback to our own text rendering. - const auto *fontEngine = QFontPrivate::get(myFont)->engineForScript(QChar::Script_Common); - Q_ASSERT(fontEngine); - if (fontEngine->type() == QFontEngine::Multi) { - fontEngine = static_cast(fontEngine)->engine(0); - Q_ASSERT(fontEngine); - } - if (fontEngine->type() == QFontEngine::Mac) { - NSFont *f = (NSFont *)(CTFontRef)fontEngine->handle(); - - // Respect the menu item palette as set in the style option. - const auto pc = p->pen().color(); - NSColor *c = [NSColor colorWithSRGBRed:pc.redF() - green:pc.greenF() - blue:pc.blueF() - alpha:pc.alphaF()]; - - s = qt_mac_removeMnemonics(s); - const auto textRect = CGRectMake(xpos, yPos, mi->rect.width() - xm - tabwidth + 1, mi->rect.height()); - - QMacCGContext cgCtx(p); - d->setupNSGraphicsContext(cgCtx, YES); - - [s.toNSString() drawInRect:textRect - withAttributes:@{ NSFontAttributeName:f, NSForegroundColorAttributeName:c }]; - - d->restoreNSGraphicsContext(cgCtx); - } else { - p->setFont(myFont); - p->drawText(xpos, yPos, mi->rect.width() - xm - tabwidth + 1, - mi->rect.height(), text_flags, s); - } - } - p->restore(); - } - break; - case CE_MenuBarItem: - case CE_MenuBarEmptyArea: - if (const QStyleOptionMenuItem *mi = qstyleoption_cast(opt)) { - const bool selected = (opt->state & State_Selected) && (opt->state & State_Enabled) && (opt->state & State_Sunken); - const QBrush bg = selected ? mi->palette.highlight() : mi->palette.window(); - p->fillRect(mi->rect, bg); - - if (ce != CE_MenuBarItem) - break; - - if (!mi->icon.isNull()) { - int iconExtent = proxy()->pixelMetric(PM_SmallIconSize); - drawItemPixmap(p, mi->rect, - Qt::AlignCenter | Qt::TextHideMnemonic | Qt::TextDontClip - | Qt::TextSingleLine, - mi->icon.pixmap(window, QSize(iconExtent, iconExtent), - (mi->state & State_Enabled) ? QIcon::Normal : QIcon::Disabled)); - } else { - drawItemText(p, mi->rect, - Qt::AlignCenter | Qt::TextHideMnemonic | Qt::TextDontClip - | Qt::TextSingleLine, - mi->palette, mi->state & State_Enabled, - mi->text, selected ? QPalette::HighlightedText : QPalette::ButtonText); - } - } - break; - case CE_ProgressBarLabel: - case CE_ProgressBarGroove: - // Do nothing. All done in CE_ProgressBarContents. Only keep these for proxy style overrides. - break; - case CE_ProgressBarContents: - if (const QStyleOptionProgressBar *pb = qstyleoption_cast(opt)) { - QMacAutoReleasePool pool; - const bool isIndeterminate = (pb->minimum == 0 && pb->maximum == 0); - const bool vertical = pb->orientation == Qt::Vertical; - const bool inverted = pb->invertedAppearance; - bool reverse = (!vertical && (pb->direction == Qt::RightToLeft)); - if (inverted) - reverse = !reverse; - - QRect rect = pb->rect; - if (vertical) - rect = rect.transposed(); - const CGRect cgRect = rect.toCGRect(); - - const auto aquaSize = d->effectiveAquaSizeConstrain(opt, w); - const QProgressStyleAnimation *animation = qobject_cast(d->animation(opt->styleObject)); - QIndeterminateProgressIndicator *ipi = nil; - if (isIndeterminate || animation) - ipi = static_cast(d->cocoaControl({ QMacStylePrivate::ProgressIndicator_Indeterminate, aquaSize })); - if (isIndeterminate) { - // QIndeterminateProgressIndicator derives from NSProgressIndicator. We use a single - // instance that we start animating as soon as one of the progress bars is indeterminate. - // Since they will be in sync (as it's the case in Cocoa), we just need to draw it with - // the right geometry when the animation triggers an update. However, we can't hide it - // entirely between frames since that would stop the animation, so we just set its alpha - // value to 0. Same if we remove it from its superview. See QIndeterminateProgressIndicator - // implementation for details. - if (!animation && opt->styleObject) { - auto *animation = new QProgressStyleAnimation(d->animateSpeed(QMacStylePrivate::AquaProgressBar), opt->styleObject); - // NSProgressIndicator is heavier to draw than the HITheme API, so we reduce the frame rate a couple notches. - animation->setFrameRate(QStyleAnimation::FifteenFps); - d->startAnimation(animation); - [ipi startAnimation]; - } - - d->setupNSGraphicsContext(cg, NO); - d->setupVerticalInvertedXform(cg, reverse, vertical, cgRect); - [ipi drawWithFrame:cgRect inView:d->backingStoreNSView]; - d->restoreNSGraphicsContext(cg); - } else { - if (animation) { - d->stopAnimation(opt->styleObject); - [ipi stopAnimation]; - } - - const auto cw = QMacStylePrivate::CocoaControl(QMacStylePrivate::ProgressIndicator_Determinate, aquaSize); - auto *pi = static_cast(d->cocoaControl(cw)); - d->drawNSViewInRect(pi, rect, p, ^(CGContextRef ctx, const CGRect &rect) { - d->setupVerticalInvertedXform(ctx, reverse, vertical, rect); - pi.minValue = pb->minimum; - pi.maxValue = pb->maximum; - pi.doubleValue = pb->progress; - [pi drawRect:rect]; - }); - } - } - break; - case CE_SizeGrip: { - // This is not HIG kosher: Fall back to the old stuff until we decide what to do. -#ifndef QT_NO_MDIAREA - if (!w || !qobject_cast(w->parentWidget())) -#endif - break; - - if (w->testAttribute(Qt::WA_MacOpaqueSizeGrip)) - p->fillRect(opt->rect, opt->palette.window()); - - QPen lineColor = QColor(82, 82, 82, 192); - lineColor.setWidth(1); - p->save(); - p->setRenderHint(QPainter::Antialiasing); - p->setPen(lineColor); - const Qt::LayoutDirection layoutDirection = w ? w->layoutDirection() : qApp->layoutDirection(); - const int NumLines = 3; - for (int l = 0; l < NumLines; ++l) { - const int offset = (l * 4 + 3); - QPoint start, end; - if (layoutDirection == Qt::LeftToRight) { - start = QPoint(opt->rect.width() - offset, opt->rect.height() - 1); - end = QPoint(opt->rect.width() - 1, opt->rect.height() - offset); - } else { - start = QPoint(offset, opt->rect.height() - 1); - end = QPoint(1, opt->rect.height() - offset); - } - p->drawLine(start, end); - } - p->restore(); - break; - } - case CE_Splitter: - if (opt->rect.width() > 1 && opt->rect.height() > 1) { - const bool isVertical = !(opt->state & QStyle::State_Horizontal); - // Qt refers to the layout orientation, while Cocoa refers to the divider's. - const auto ct = isVertical ? QMacStylePrivate::SplitView_Horizontal : QMacStylePrivate::SplitView_Vertical; - const auto cw = QMacStylePrivate::CocoaControl(ct, QStyleHelper::SizeLarge); - auto *sv = static_cast(d->cocoaControl(cw)); - sv.frame = opt->rect.toCGRect(); - d->drawNSViewInRect(sv, opt->rect, p, ^(CGContextRef, const CGRect &rect) { - [sv drawDividerInRect:rect]; - }); - } else { - QPen oldPen = p->pen(); - p->setPen(opt->palette.dark().color()); - if (opt->state & QStyle::State_Horizontal) - p->drawLine(opt->rect.topLeft(), opt->rect.bottomLeft()); - else - p->drawLine(opt->rect.topLeft(), opt->rect.topRight()); - p->setPen(oldPen); - } - break; - case CE_RubberBand: - if (const QStyleOptionRubberBand *rubber = qstyleoption_cast(opt)) { - QColor fillColor(opt->palette.color(QPalette::Disabled, QPalette::Highlight)); - if (!rubber->opaque) { - QColor strokeColor; - // I retrieved these colors from the Carbon-Dev mailing list - strokeColor.setHsvF(0, 0, 0.86, 1.0); - fillColor.setHsvF(0, 0, 0.53, 0.25); - if (opt->rect.width() * opt->rect.height() <= 3) { - p->fillRect(opt->rect, strokeColor); - } else { - QPen oldPen = p->pen(); - QBrush oldBrush = p->brush(); - QPen pen(strokeColor); - p->setPen(pen); - p->setBrush(fillColor); - QRect adjusted = opt->rect.adjusted(1, 1, -1, -1); - if (adjusted.isValid()) - p->drawRect(adjusted); - p->setPen(oldPen); - p->setBrush(oldBrush); - } - } else { - p->fillRect(opt->rect, fillColor); - } - } - break; -#ifndef QT_NO_TOOLBAR - case CE_ToolBar: { - const QStyleOptionToolBar *toolBar = qstyleoption_cast(opt); - const bool isDarkMode = qt_mac_applicationIsInDarkMode(); - - // Unified title and toolbar drawing. In this mode the cocoa platform plugin will - // fill the top toolbar area part with a background gradient that "unifies" with - // the title bar. The following code fills the toolBar area with transparent pixels - // to make that gradient visible. - if (w) { -#if QT_CONFIG(mainwindow) - if (QMainWindow * mainWindow = qobject_cast(w->window())) { - if (toolBar && toolBar->toolBarArea == Qt::TopToolBarArea && mainWindow->unifiedTitleAndToolBarOnMac()) { - // fill with transparent pixels. - p->save(); - p->setCompositionMode(QPainter::CompositionMode_Source); - p->fillRect(opt->rect, Qt::transparent); - p->restore(); - - // Draw a horizontal separator line at the toolBar bottom if the "unified" area ends here. - // There might be additional toolbars or other widgets such as tab bars in document - // mode below. Determine this by making a unified toolbar area test for the row below - // this toolbar. - const QPoint windowToolbarEnd = w->mapTo(w->window(), opt->rect.bottomLeft()); - const bool isEndOfUnifiedArea = !isInMacUnifiedToolbarArea(w->window()->windowHandle(), windowToolbarEnd.y() + 1); - if (isEndOfUnifiedArea) { - const int margin = qt_mac_aqua_get_metric(SeparatorSize); - const auto separatorRect = QRect(opt->rect.left(), opt->rect.bottom(), opt->rect.width(), margin); - p->fillRect(separatorRect, isDarkMode ? darkModeSeparatorLine : opt->palette.dark().color()); - } - break; - } - } -#endif - } - - // draw background gradient - QLinearGradient linearGrad; - if (opt->state & State_Horizontal) - linearGrad = QLinearGradient(0, opt->rect.top(), 0, opt->rect.bottom()); - else - linearGrad = QLinearGradient(opt->rect.left(), 0, opt->rect.right(), 0); - - QColor mainWindowGradientBegin = isDarkMode ? darkMainWindowGradientBegin : lightMainWindowGradientBegin; - QColor mainWindowGradientEnd = isDarkMode ? darkMainWindowGradientEnd : lightMainWindowGradientEnd; - - linearGrad.setColorAt(0, mainWindowGradientBegin); - linearGrad.setColorAt(1, mainWindowGradientEnd); - p->fillRect(opt->rect, linearGrad); - - p->save(); - QRect toolbarRect = isDarkMode ? opt->rect.adjusted(0, 0, 0, 1) : opt->rect; - if (opt->state & State_Horizontal) { - p->setPen(isDarkMode ? darkModeSeparatorLine : mainWindowGradientBegin.lighter(114)); - p->drawLine(toolbarRect.topLeft(), toolbarRect.topRight()); - p->setPen(isDarkMode ? darkModeSeparatorLine :mainWindowGradientEnd.darker(114)); - p->drawLine(toolbarRect.bottomLeft(), toolbarRect.bottomRight()); - } else { - p->setPen(isDarkMode ? darkModeSeparatorLine : mainWindowGradientBegin.lighter(114)); - p->drawLine(toolbarRect.topLeft(), toolbarRect.bottomLeft()); - p->setPen(isDarkMode ? darkModeSeparatorLine : mainWindowGradientEnd.darker(114)); - p->drawLine(toolbarRect.topRight(), toolbarRect.bottomRight()); - } - p->restore(); - - - } break; -#endif - default: - QCommonStyle::drawControl(ce, opt, p, w); - break; - } -} - -static void setLayoutItemMargins(int left, int top, int right, int bottom, QRect *rect, Qt::LayoutDirection dir) -{ - if (dir == Qt::RightToLeft) { - rect->adjust(-right, top, -left, bottom); - } else { - rect->adjust(left, top, right, bottom); - } -} - -QRect QMacStyle::subElementRect(SubElement sr, const QStyleOption *opt, - const QWidget *widget) const -{ - Q_D(const QMacStyle); - QRect rect; - const int controlSize = getControlSize(opt, widget); - - switch (sr) { - case SE_ItemViewItemText: - if (const QStyleOptionViewItem *vopt = qstyleoption_cast(opt)) { - int fw = proxy()->pixelMetric(PM_FocusFrameHMargin, opt, widget); - // We add the focusframeargin between icon and text in commonstyle - rect = QCommonStyle::subElementRect(sr, opt, widget); - if (vopt->features & QStyleOptionViewItem::HasDecoration) - rect.adjust(-fw, 0, 0, 0); - } - break; - case SE_ToolBoxTabContents: - rect = QCommonStyle::subElementRect(sr, opt, widget); - break; - case SE_PushButtonContents: - if (const QStyleOptionButton *btn = qstyleoption_cast(opt)) { - // Comment from the old HITheme days: - // "Unlike Carbon, we want the button to always be drawn inside its bounds. - // Therefore, the button is a bit smaller, so that even if it got focus, - // the focus 'shadow' will be inside. Adjust the content rect likewise." - // In the future, we should consider using -[NSCell titleRectForBounds:]. - // Since it requires configuring the NSButton fully, i.e. frame, image, - // title and font, we keep things more manual until we are more familiar - // with side effects when changing NSButton state. - const auto ct = cocoaControlType(btn, widget); - const auto cs = d->effectiveAquaSizeConstrain(btn, widget); - const auto cw = QMacStylePrivate::CocoaControl(ct, cs); - const auto frameRect = cw.adjustedControlFrame(btn->rect); - const auto titleMargins = cw.titleMargins(); - rect = (frameRect - titleMargins).toRect(); - } - break; - case SE_HeaderLabel: { - int margin = proxy()->pixelMetric(QStyle::PM_HeaderMargin, opt, widget); - rect.setRect(opt->rect.x() + margin, opt->rect.y(), - opt->rect.width() - margin * 2, opt->rect.height() - 2); - if (const QStyleOptionHeader *header = qstyleoption_cast(opt)) { - // Subtract width needed for arrow, if there is one - if (header->sortIndicator != QStyleOptionHeader::None) { - if (opt->state & State_Horizontal) - rect.setWidth(rect.width() - (headerSectionArrowHeight) - (margin * 2)); - else - rect.setHeight(rect.height() - (headerSectionArrowHeight) - (margin * 2)); - } - } - rect = visualRect(opt->direction, opt->rect, rect); - break; - } - case SE_HeaderArrow: { - int h = opt->rect.height(); - int w = opt->rect.width(); - int x = opt->rect.x(); - int y = opt->rect.y(); - int margin = proxy()->pixelMetric(QStyle::PM_HeaderMargin, opt, widget); - - if (opt->state & State_Horizontal) { - rect.setRect(x + w - margin * 2 - headerSectionArrowHeight, y + 5, - headerSectionArrowHeight, h - margin * 2 - 5); - } else { - rect.setRect(x + 5, y + h - margin * 2 - headerSectionArrowHeight, - w - margin * 2 - 5, headerSectionArrowHeight); - } - rect = visualRect(opt->direction, opt->rect, rect); - break; - } - case SE_ProgressBarGroove: - // Wrong in the secondary dimension, but accurate enough in the main dimension. - rect = opt->rect; - break; - case SE_ProgressBarLabel: - break; - case SE_ProgressBarContents: - rect = opt->rect; - break; - case SE_TreeViewDisclosureItem: { - rect = opt->rect; - // As previously returned by HIThemeGetButtonContentBounds - rect.setLeft(rect.left() + 2 + DisclosureOffset); - break; - } -#if QT_CONFIG(tabwidget) - case SE_TabWidgetLeftCorner: - if (const QStyleOptionTabWidgetFrame *twf - = qstyleoption_cast(opt)) { - switch (twf->shape) { - case QTabBar::RoundedNorth: - case QTabBar::TriangularNorth: - rect = QRect(QPoint(0, 0), twf->leftCornerWidgetSize); - break; - case QTabBar::RoundedSouth: - case QTabBar::TriangularSouth: - rect = QRect(QPoint(0, twf->rect.height() - twf->leftCornerWidgetSize.height()), - twf->leftCornerWidgetSize); - break; - default: - break; - } - rect = visualRect(twf->direction, twf->rect, rect); - } - break; - case SE_TabWidgetRightCorner: - if (const QStyleOptionTabWidgetFrame *twf - = qstyleoption_cast(opt)) { - switch (twf->shape) { - case QTabBar::RoundedNorth: - case QTabBar::TriangularNorth: - rect = QRect(QPoint(twf->rect.width() - twf->rightCornerWidgetSize.width(), 0), - twf->rightCornerWidgetSize); - break; - case QTabBar::RoundedSouth: - case QTabBar::TriangularSouth: - rect = QRect(QPoint(twf->rect.width() - twf->rightCornerWidgetSize.width(), - twf->rect.height() - twf->rightCornerWidgetSize.height()), - twf->rightCornerWidgetSize); - break; - default: - break; - } - rect = visualRect(twf->direction, twf->rect, rect); - } - break; - case SE_TabWidgetTabContents: - rect = QCommonStyle::subElementRect(sr, opt, widget); - if (const auto *twf = qstyleoption_cast(opt)) { - if (twf->lineWidth != 0) { - switch (QMacStylePrivate::tabDirection(twf->shape)) { - case QMacStylePrivate::North: - rect.adjust(+1, +14, -1, -1); - break; - case QMacStylePrivate::South: - rect.adjust(+1, +1, -1, -14); - break; - case QMacStylePrivate::West: - rect.adjust(+14, +1, -1, -1); - break; - case QMacStylePrivate::East: - rect.adjust(+1, +1, -14, -1); - } - } - } - break; - case SE_TabBarTabText: - if (const QStyleOptionTab *tab = qstyleoption_cast(opt)) { - QRect dummyIconRect; - d->tabLayout(tab, widget, &rect, &dummyIconRect); - } - break; - case SE_TabBarTabLeftButton: - case SE_TabBarTabRightButton: - if (const QStyleOptionTab *tab = qstyleoption_cast(opt)) { - bool selected = tab->state & State_Selected; - int verticalShift = proxy()->pixelMetric(QStyle::PM_TabBarTabShiftVertical, tab, widget); - int horizontalShift = proxy()->pixelMetric(QStyle::PM_TabBarTabShiftHorizontal, tab, widget); - int hpadding = 5; - - bool verticalTabs = tab->shape == QTabBar::RoundedEast - || tab->shape == QTabBar::RoundedWest - || tab->shape == QTabBar::TriangularEast - || tab->shape == QTabBar::TriangularWest; - - QRect tr = tab->rect; - if (tab->shape == QTabBar::RoundedSouth || tab->shape == QTabBar::TriangularSouth) - verticalShift = -verticalShift; - if (verticalTabs) { - qSwap(horizontalShift, verticalShift); - horizontalShift *= -1; - verticalShift *= -1; - } - if (tab->shape == QTabBar::RoundedWest || tab->shape == QTabBar::TriangularWest) - horizontalShift = -horizontalShift; - - tr.adjust(0, 0, horizontalShift, verticalShift); - if (selected) - { - tr.setBottom(tr.bottom() - verticalShift); - tr.setRight(tr.right() - horizontalShift); - } - - QSize size = (sr == SE_TabBarTabLeftButton) ? tab->leftButtonSize : tab->rightButtonSize; - int w = size.width(); - int h = size.height(); - int midHeight = static_cast(qCeil(float(tr.height() - h) / 2)); - int midWidth = ((tr.width() - w) / 2); - - bool atTheTop = true; - switch (tab->shape) { - case QTabBar::RoundedWest: - case QTabBar::TriangularWest: - atTheTop = (sr == SE_TabBarTabLeftButton); - break; - case QTabBar::RoundedEast: - case QTabBar::TriangularEast: - atTheTop = (sr == SE_TabBarTabRightButton); - break; - default: - if (sr == SE_TabBarTabLeftButton) - rect = QRect(tab->rect.x() + hpadding, midHeight, w, h); - else - rect = QRect(tab->rect.right() - w - hpadding, midHeight, w, h); - rect = visualRect(tab->direction, tab->rect, rect); - } - if (verticalTabs) { - if (atTheTop) - rect = QRect(midWidth, tr.y() + tab->rect.height() - hpadding - h, w, h); - else - rect = QRect(midWidth, tr.y() + hpadding, w, h); - } - } - break; -#endif - case SE_LineEditContents: - rect = QCommonStyle::subElementRect(sr, opt, widget); -#if QT_CONFIG(combobox) - if (widget && qobject_cast(widget->parentWidget())) - rect.adjust(-1, -2, 0, 0); - else -#endif - rect.adjust(-1, -1, 0, +1); - break; - case SE_CheckBoxLayoutItem: - rect = opt->rect; - if (controlSize == QStyleHelper::SizeLarge) { - setLayoutItemMargins(+2, +3, -9, -4, &rect, opt->direction); - } else if (controlSize == QStyleHelper::SizeSmall) { - setLayoutItemMargins(+1, +5, 0 /* fix */, -6, &rect, opt->direction); - } else { - setLayoutItemMargins(0, +7, 0 /* fix */, -6, &rect, opt->direction); - } - break; - case SE_ComboBoxLayoutItem: -#ifndef QT_NO_TOOLBAR - if (widget && qobject_cast(widget->parentWidget())) { - // Do nothing, because QToolbar needs the entire widget rect. - // Otherwise it will be clipped. Equivalent to - // widget->setAttribute(Qt::WA_LayoutUsesWidgetRect), but without - // all the hassle. - } else -#endif - { - rect = opt->rect; - if (controlSize == QStyleHelper::SizeLarge) { - rect.adjust(+3, +2, -3, -4); - } else if (controlSize == QStyleHelper::SizeSmall) { - setLayoutItemMargins(+2, +1, -3, -4, &rect, opt->direction); - } else { - setLayoutItemMargins(+1, 0, -2, 0, &rect, opt->direction); - } - } - break; - case SE_LabelLayoutItem: - rect = opt->rect; - setLayoutItemMargins(+1, 0 /* SHOULD be -1, done for alignment */, 0, 0 /* SHOULD be -1, done for alignment */, &rect, opt->direction); - break; - case SE_ProgressBarLayoutItem: { - rect = opt->rect; - int bottom = SIZE(3, 8, 8); - if (opt->state & State_Horizontal) { - rect.adjust(0, +1, 0, -bottom); - } else { - setLayoutItemMargins(+1, 0, -bottom, 0, &rect, opt->direction); - } - break; - } - case SE_PushButtonLayoutItem: - if (const QStyleOptionButton *buttonOpt - = qstyleoption_cast(opt)) { - if ((buttonOpt->features & QStyleOptionButton::Flat)) - break; // leave rect alone - } - rect = opt->rect; - if (controlSize == QStyleHelper::SizeLarge) { - rect.adjust(+6, +4, -6, -8); - } else if (controlSize == QStyleHelper::SizeSmall) { - rect.adjust(+5, +4, -5, -6); - } else { - rect.adjust(+1, 0, -1, -2); - } - break; - case SE_RadioButtonLayoutItem: - rect = opt->rect; - if (controlSize == QStyleHelper::SizeLarge) { - setLayoutItemMargins(+2, +2 /* SHOULD BE +3, done for alignment */, - 0, -4 /* SHOULD BE -3, done for alignment */, &rect, opt->direction); - } else if (controlSize == QStyleHelper::SizeSmall) { - rect.adjust(0, +6, 0 /* fix */, -5); - } else { - rect.adjust(0, +6, 0 /* fix */, -7); - } - break; - case SE_SliderLayoutItem: - if (const QStyleOptionSlider *sliderOpt - = qstyleoption_cast(opt)) { - rect = opt->rect; - if (sliderOpt->tickPosition == QSlider::NoTicks) { - int above = SIZE(3, 0, 2); - int below = SIZE(4, 3, 0); - if (sliderOpt->orientation == Qt::Horizontal) { - rect.adjust(0, +above, 0, -below); - } else { - rect.adjust(+above, 0, -below, 0); //### Seems that QSlider flip the position of the ticks in reverse mode. - } - } else if (sliderOpt->tickPosition == QSlider::TicksAbove) { - int below = SIZE(3, 2, 0); - if (sliderOpt->orientation == Qt::Horizontal) { - rect.setHeight(rect.height() - below); - } else { - rect.setWidth(rect.width() - below); - } - } else if (sliderOpt->tickPosition == QSlider::TicksBelow) { - int above = SIZE(3, 2, 0); - if (sliderOpt->orientation == Qt::Horizontal) { - rect.setTop(rect.top() + above); - } else { - rect.setLeft(rect.left() + above); - } - } - } - break; - case SE_FrameLayoutItem: - // hack because QStyleOptionFrame doesn't have a frameStyle member - if (const QFrame *frame = qobject_cast(widget)) { - rect = opt->rect; - switch (frame->frameStyle() & QFrame::Shape_Mask) { - case QFrame::HLine: - rect.adjust(0, +1, 0, -1); - break; - case QFrame::VLine: - rect.adjust(+1, 0, -1, 0); - break; - default: - ; - } - } - break; - case SE_GroupBoxLayoutItem: - rect = opt->rect; - if (const QStyleOptionGroupBox *groupBoxOpt = - qstyleoption_cast(opt)) { - /* - AHIG is very inconsistent when it comes to group boxes. - Basically, we make sure that (non-checkable) group boxes - and tab widgets look good when laid out side by side. - */ - if (groupBoxOpt->subControls & (QStyle::SC_GroupBoxCheckBox - | QStyle::SC_GroupBoxLabel)) { - int delta; - if (groupBoxOpt->subControls & QStyle::SC_GroupBoxCheckBox) { - delta = SIZE(8, 4, 4); // guess - } else { - delta = SIZE(15, 12, 12); // guess - } - rect.setTop(rect.top() + delta); - } - } - rect.setBottom(rect.bottom() - 1); - break; -#if QT_CONFIG(tabwidget) - case SE_TabWidgetLayoutItem: - if (const QStyleOptionTabWidgetFrame *tabWidgetOpt = - qstyleoption_cast(opt)) { - /* - AHIG specifies "12 or 14" as the distance from the window - edge. We choose 14 and since the default top margin is 20, - the overlap is 6. - */ - rect = tabWidgetOpt->rect; - if (tabWidgetOpt->shape == QTabBar::RoundedNorth) - rect.setTop(rect.top() + SIZE(6 /* AHIG */, 3 /* guess */, 2 /* AHIG */)); - } - break; -#endif -#if QT_CONFIG(dockwidget) - case SE_DockWidgetCloseButton: - case SE_DockWidgetFloatButton: - case SE_DockWidgetTitleBarText: - case SE_DockWidgetIcon: { - int iconSize = proxy()->pixelMetric(PM_SmallIconSize, opt, widget); - int buttonMargin = proxy()->pixelMetric(PM_DockWidgetTitleBarButtonMargin, opt, widget); - QRect srect = opt->rect; - - const QStyleOptionDockWidget *dwOpt - = qstyleoption_cast(opt); - bool canClose = dwOpt == 0 ? true : dwOpt->closable; - bool canFloat = dwOpt == 0 ? false : dwOpt->floatable; - - const bool verticalTitleBar = dwOpt->verticalTitleBar; - - // If this is a vertical titlebar, we transpose and work as if it was - // horizontal, then transpose again. - if (verticalTitleBar) - srect = srect.transposed(); - - do { - int right = srect.right(); - int left = srect.left(); - - QRect closeRect; - if (canClose) { - QSize sz = proxy()->standardIcon(QStyle::SP_TitleBarCloseButton, - opt, widget).actualSize(QSize(iconSize, iconSize)); - sz += QSize(buttonMargin, buttonMargin); - if (verticalTitleBar) - sz = sz.transposed(); - closeRect = QRect(left, - srect.center().y() - sz.height()/2, - sz.width(), sz.height()); - left = closeRect.right() + 1; - } - if (sr == SE_DockWidgetCloseButton) { - rect = closeRect; - break; - } - - QRect floatRect; - if (canFloat) { - QSize sz = proxy()->standardIcon(QStyle::SP_TitleBarNormalButton, - opt, widget).actualSize(QSize(iconSize, iconSize)); - sz += QSize(buttonMargin, buttonMargin); - if (verticalTitleBar) - sz = sz.transposed(); - floatRect = QRect(left, - srect.center().y() - sz.height()/2, - sz.width(), sz.height()); - left = floatRect.right() + 1; - } - if (sr == SE_DockWidgetFloatButton) { - rect = floatRect; - break; - } - - QRect iconRect; - if (const QDockWidget *dw = qobject_cast(widget)) { - QIcon icon; - if (dw->isFloating()) - icon = dw->windowIcon(); - if (!icon.isNull() - && icon.cacheKey() != QApplication::windowIcon().cacheKey()) { - QSize sz = icon.actualSize(QSize(rect.height(), rect.height())); - if (verticalTitleBar) - sz = sz.transposed(); - iconRect = QRect(right - sz.width(), srect.center().y() - sz.height()/2, - sz.width(), sz.height()); - right = iconRect.left() - 1; - } - } - if (sr == SE_DockWidgetIcon) { - rect = iconRect; - break; - } - - QRect textRect = QRect(left, srect.top(), - right - left, srect.height()); - if (sr == SE_DockWidgetTitleBarText) { - rect = textRect; - break; - } - } while (false); - - if (verticalTitleBar) { - rect = QRect(srect.left() + rect.top() - srect.top(), - srect.top() + srect.right() - rect.right(), - rect.height(), rect.width()); - } else { - rect = visualRect(opt->direction, srect, rect); - } - break; - } -#endif - default: - rect = QCommonStyle::subElementRect(sr, opt, widget); - break; - } - return rect; -} - -void QMacStylePrivate::drawToolbarButtonArrow(const QStyleOption *opt, QPainter *p) const -{ - Q_Q(const QMacStyle); - QStyleOption arrowOpt = *opt; - arrowOpt.rect = QRect(opt->rect.right() - (toolButtonArrowSize + toolButtonArrowMargin), - opt->rect.bottom() - (toolButtonArrowSize + toolButtonArrowMargin), - toolButtonArrowSize, - toolButtonArrowSize); - q->proxy()->drawPrimitive(QStyle::PE_IndicatorArrowDown, &arrowOpt, p); -} - -void QMacStylePrivate::setupNSGraphicsContext(CGContextRef cg, bool flipped) const -{ - CGContextSaveGState(cg); - [NSGraphicsContext saveGraphicsState]; - - [NSGraphicsContext setCurrentContext: - [NSGraphicsContext graphicsContextWithCGContext:cg flipped:flipped]]; -} - -void QMacStylePrivate::restoreNSGraphicsContext(CGContextRef cg) const -{ - [NSGraphicsContext restoreGraphicsState]; - CGContextRestoreGState(cg); -} - -void QMacStyle::drawComplexControl(ComplexControl cc, const QStyleOptionComplex *opt, QPainter *p, - const QWidget *widget) const -{ - Q_D(const QMacStyle); - QMacCGContext cg(p); - QWindow *window = widget && widget->window() ? widget->window()->windowHandle() : nullptr; - d->resolveCurrentNSView(window); - switch (cc) { - case CC_ScrollBar: - if (const QStyleOptionSlider *sb = qstyleoption_cast(opt)) { - - const bool drawTrack = sb->subControls & SC_ScrollBarGroove; - const bool drawKnob = sb->subControls & SC_ScrollBarSlider; - if (!drawTrack && !drawKnob) - break; - - const bool isHorizontal = sb->orientation == Qt::Horizontal; - - if (opt && opt->styleObject && !QMacStylePrivate::scrollBars.contains(opt->styleObject)) - QMacStylePrivate::scrollBars.append(QPointer(opt->styleObject)); - - static const CGFloat knobWidths[] = { 7.0, 5.0, 5.0 }; - static const CGFloat expandedKnobWidths[] = { 11.0, 9.0, 9.0 }; - const auto cocoaSize = d->effectiveAquaSizeConstrain(opt, widget); - const CGFloat maxExpandScale = expandedKnobWidths[cocoaSize] / knobWidths[cocoaSize]; - - const bool isTransient = proxy()->styleHint(SH_ScrollBar_Transient, opt, widget); - if (!isTransient) - d->stopAnimation(opt->styleObject); - bool wasActive = false; - CGFloat opacity = 0.0; - CGFloat expandScale = 1.0; - CGFloat expandOffset = 0.0; - bool shouldExpand = false; - - if (QObject *styleObject = opt->styleObject) { - const int oldPos = styleObject->property("_q_stylepos").toInt(); - const int oldMin = styleObject->property("_q_stylemin").toInt(); - const int oldMax = styleObject->property("_q_stylemax").toInt(); - const QRect oldRect = styleObject->property("_q_stylerect").toRect(); - const QStyle::State oldState = static_cast(styleObject->property("_q_stylestate").value()); - const uint oldActiveControls = styleObject->property("_q_stylecontrols").toUInt(); - - // a scrollbar is transient when the scrollbar itself and - // its sibling are both inactive (ie. not pressed/hovered/moved) - const bool transient = isTransient && !opt->activeSubControls && !(sb->state & State_On); - - if (!transient || - oldPos != sb->sliderPosition || - oldMin != sb->minimum || - oldMax != sb->maximum || - oldRect != sb->rect || - oldState != sb->state || - oldActiveControls != sb->activeSubControls) { - - // if the scrollbar is transient or its attributes, geometry or - // state has changed, the opacity is reset back to 100% opaque - opacity = 1.0; - - styleObject->setProperty("_q_stylepos", sb->sliderPosition); - styleObject->setProperty("_q_stylemin", sb->minimum); - styleObject->setProperty("_q_stylemax", sb->maximum); - styleObject->setProperty("_q_stylerect", sb->rect); - styleObject->setProperty("_q_stylestate", static_cast(sb->state)); - styleObject->setProperty("_q_stylecontrols", static_cast(sb->activeSubControls)); - - QScrollbarStyleAnimation *anim = qobject_cast(d->animation(styleObject)); - if (transient) { - if (!anim) { - anim = new QScrollbarStyleAnimation(QScrollbarStyleAnimation::Deactivating, styleObject); - d->startAnimation(anim); - } else if (anim->mode() == QScrollbarStyleAnimation::Deactivating) { - // the scrollbar was already fading out while the - // state changed -> restart the fade out animation - anim->setCurrentTime(0); - } - } else if (anim && anim->mode() == QScrollbarStyleAnimation::Deactivating) { - d->stopAnimation(styleObject); - } - } - - QScrollbarStyleAnimation *anim = qobject_cast(d->animation(styleObject)); - if (anim && anim->mode() == QScrollbarStyleAnimation::Deactivating) { - // once a scrollbar was active (hovered/pressed), it retains - // the active look even if it's no longer active while fading out - if (oldActiveControls) - anim->setActive(true); - - wasActive = anim->wasActive(); - opacity = anim->currentValue(); - } - - shouldExpand = isTransient && (opt->activeSubControls || wasActive); - if (shouldExpand) { - if (!anim && !oldActiveControls) { - // Start expand animation only once and when entering - anim = new QScrollbarStyleAnimation(QScrollbarStyleAnimation::Activating, styleObject); - d->startAnimation(anim); - } - if (anim && anim->mode() == QScrollbarStyleAnimation::Activating) { - expandScale = 1.0 + (maxExpandScale - 1.0) * anim->currentValue(); - expandOffset = 5.5 * (1.0 - anim->currentValue()); - } else { - // Keep expanded state after the animation ends, and when fading out - expandScale = maxExpandScale; - expandOffset = 0.0; - } - } - } - - d->setupNSGraphicsContext(cg, NO /* flipped */); - - const auto controlType = isHorizontal ? QMacStylePrivate::Scroller_Horizontal : QMacStylePrivate::Scroller_Vertical; - const auto cw = QMacStylePrivate::CocoaControl(controlType, cocoaSize); - NSScroller *scroller = static_cast(d->cocoaControl(cw)); - - const QColor bgColor = QStyleHelper::backgroundColor(opt->palette, widget); - const bool hasDarkBg = bgColor.red() < 128 && bgColor.green() < 128 && bgColor.blue() < 128; - if (isTransient) { - // macOS behavior: as soon as one color channel is >= 128, - // the background is considered bright, scroller is dark. - scroller.knobStyle = hasDarkBg? NSScrollerKnobStyleLight : NSScrollerKnobStyleDark; - } else { - scroller.knobStyle = NSScrollerKnobStyleDefault; - } - - scroller.scrollerStyle = isTransient ? NSScrollerStyleOverlay : NSScrollerStyleLegacy; - - if (!setupScroller(scroller, sb)) - break; - - if (isTransient) { - CGContextBeginTransparencyLayerWithRect(cg, scroller.frame, nullptr); - CGContextSetAlpha(cg, opacity); - } - - if (drawTrack) { - // Draw the track when hovering. Expand by shifting the track rect. - if (!isTransient || opt->activeSubControls || wasActive) { - CGRect trackRect = scroller.bounds; - if (isHorizontal) - trackRect.origin.y += expandOffset; - else - trackRect.origin.x += expandOffset; - [scroller drawKnobSlotInRect:trackRect highlight:NO]; - } - } - - if (drawKnob) { - if (shouldExpand) { - // -[NSScroller drawKnob] is not useful here because any scaling applied - // will only be used to draw the hi-DPI artwork. And even if did scale, - // the stretched knob would look wrong, actually. So we need to draw the - // scroller manually when it's being hovered. - const CGFloat scrollerWidth = [NSScroller scrollerWidthForControlSize:scroller.controlSize scrollerStyle:scroller.scrollerStyle]; - const CGFloat knobWidth = knobWidths[cocoaSize] * expandScale; - // Cocoa can help get the exact knob length in the current orientation - const CGRect scrollerKnobRect = CGRectInset([scroller rectForPart:NSScrollerKnob], 1, 1); - const CGFloat knobLength = isHorizontal ? scrollerKnobRect.size.width : scrollerKnobRect.size.height; - const CGFloat knobPos = isHorizontal ? scrollerKnobRect.origin.x : scrollerKnobRect.origin.y; - const CGFloat knobOffset = qRound((scrollerWidth + expandOffset - knobWidth) / 2.0); - const CGFloat knobRadius = knobWidth / 2.0; - CGRect knobRect; - if (isHorizontal) - knobRect = CGRectMake(knobPos, knobOffset, knobLength, knobWidth); - else - knobRect = CGRectMake(knobOffset, knobPos, knobWidth, knobLength); - QCFType knobPath = CGPathCreateWithRoundedRect(knobRect, knobRadius, knobRadius, nullptr); - CGContextAddPath(cg, knobPath); - CGContextSetAlpha(cg, 0.5); - CGColorRef knobColor = hasDarkBg ? NSColor.whiteColor.CGColor : NSColor.blackColor.CGColor; - CGContextSetFillColorWithColor(cg, knobColor); - CGContextFillPath(cg); - } else { - [scroller drawKnob]; - - if (!isTransient && opt->activeSubControls) { - // The knob should appear darker (going from 0.76 down to 0.49). - // But no blending mode can help darken enough in a single pass, - // so we resort to drawing the knob twice with a small help from - // blending. This brings the gray level to a close enough 0.53. - CGContextSetBlendMode(cg, kCGBlendModePlusDarker); - [scroller drawKnob]; - } - } - } - - if (isTransient) - CGContextEndTransparencyLayer(cg); - - d->restoreNSGraphicsContext(cg); - } - break; - case CC_Slider: - if (const QStyleOptionSlider *sl = qstyleoption_cast(opt)) { - const bool isHorizontal = sl->orientation == Qt::Horizontal; - const auto ct = isHorizontal ? QMacStylePrivate::Slider_Horizontal : QMacStylePrivate::Slider_Vertical; - const auto cs = d->effectiveAquaSizeConstrain(opt, widget); - const auto cw = QMacStylePrivate::CocoaControl(ct, cs); - auto *slider = static_cast(d->cocoaControl(cw)); - if (!setupSlider(slider, sl)) - break; - - const bool hasTicks = sl->tickPosition != QSlider::NoTicks; - const bool hasDoubleTicks = sl->tickPosition == QSlider::TicksBothSides; - const bool drawKnob = sl->subControls & SC_SliderHandle; - const bool drawBar = sl->subControls & SC_SliderGroove; - const bool drawTicks = sl->subControls & SC_SliderTickmarks; - const bool isPressed = sl->state & State_Sunken; - - CGPoint pressPoint; - if (isPressed) { - const CGRect knobRect = [slider.cell knobRectFlipped:NO]; - pressPoint.x = CGRectGetMidX(knobRect); - pressPoint.y = CGRectGetMidY(knobRect); - [slider.cell startTrackingAt:pressPoint inView:slider]; - } - - d->drawNSViewInRect(slider, opt->rect, p, ^(CGContextRef ctx, const CGRect &rect) { - if (isHorizontal && sl->upsideDown) { - CGContextTranslateCTM(ctx, rect.size.width, 0); - CGContextScaleCTM(ctx, -1, 1); - } - - if (hasDoubleTicks) { - // This ain't HIG kosher: eye-proved constants - if (isHorizontal) - CGContextTranslateCTM(ctx, 0, 4); - else - CGContextTranslateCTM(ctx, 1, 0); - } - - // Since the GC is flipped, upsideDown means *not* inverted when vertical. - const bool verticalFlip = !isHorizontal && !sl->upsideDown; // FIXME: && !isSierraOrLater - -#if 0 - // FIXME: Sadly, this part doesn't work. It seems to somehow polute the - // NSSlider's internal state and, when we need to use the "else" part, - // the slider's frame is not in sync with its cell dimensions. - const bool drawAllParts = drawKnob && drawBar && (!hasTicks || drawTicks); - if (drawAllParts && !hasDoubleTicks && (!verticalFlip || drawTicks)) { - // Draw eveything at once if we're going to, except for inverted vertical - // sliders which need to be drawn part by part because of the shadow below - // the knob. Same for two-sided tickmarks. - if (verticalFlip && drawTicks) { - // Since tickmarks are always rendered symmetrically, a vertically - // flipped slider with tickmarks only needs to get its value flipped. - slider.intValue = slider.maxValue - slider.intValue + slider.minValue; - } - [slider drawRect:CGRectZero]; - } else -#endif - { - [slider calcSize]; - NSSliderCell *cell = slider.cell; - - const int numberOfTickMarks = slider.numberOfTickMarks; - // This ain't HIG kosher: force tick-less bar position. - if (hasDoubleTicks) - slider.numberOfTickMarks = 0; - - const CGRect barRect = [cell barRectFlipped:hasTicks]; - if (drawBar) { - // This ain't HIG kosher: force unfilled bar look. - if (hasDoubleTicks) - slider.numberOfTickMarks = numberOfTickMarks; - [cell drawBarInside:barRect flipped:!verticalFlip]; - } - - if (hasTicks && drawTicks) { - if (!drawBar && hasDoubleTicks) - slider.numberOfTickMarks = numberOfTickMarks; - - [cell drawTickMarks]; - - if (hasDoubleTicks) { - // This ain't HIG kosher: just slap a set of tickmarks on each side, like we used to. - CGAffineTransform tickMarksFlip; - const CGRect tickMarkRect = [cell rectOfTickMarkAtIndex:0]; - if (isHorizontal) { - tickMarksFlip = CGAffineTransformMakeTranslation(0, rect.size.height - tickMarkRect.size.height - 3); - tickMarksFlip = CGAffineTransformScale(tickMarksFlip, 1, -1); - } else { - tickMarksFlip = CGAffineTransformMakeTranslation(rect.size.width - tickMarkRect.size.width / 2, 0); - tickMarksFlip = CGAffineTransformScale(tickMarksFlip, -1, 1); - } - CGContextConcatCTM(ctx, tickMarksFlip); - [cell drawTickMarks]; - CGContextConcatCTM(ctx, CGAffineTransformInvert(tickMarksFlip)); - } - } - - if (drawKnob) { - // This ain't HIG kosher: force round knob look. - if (hasDoubleTicks) - slider.numberOfTickMarks = 0; - // Draw the knob in the symmetrical position instead of flipping. - if (verticalFlip) - slider.intValue = slider.maxValue - slider.intValue + slider.minValue; - [cell drawKnob]; - } - } - }); - - if (isPressed) - [slider.cell stopTracking:pressPoint at:pressPoint inView:slider mouseIsUp:NO]; - } - break; -#if QT_CONFIG(spinbox) - case CC_SpinBox: - if (const QStyleOptionSpinBox *sb = qstyleoption_cast(opt)) { - if (sb->frame && (sb->subControls & SC_SpinBoxFrame)) { - const auto lineEditRect = proxy()->subControlRect(CC_SpinBox, sb, SC_SpinBoxEditField, widget); - QStyleOptionFrame frame; - static_cast(frame) = *opt; - frame.rect = lineEditRect; - frame.state |= State_Sunken; - frame.lineWidth = 1; - frame.midLineWidth = 0; - frame.features = QStyleOptionFrame::None; - frame.frameShape = QFrame::Box; - drawPrimitive(PE_FrameLineEdit, &frame, p, widget); - } - if (sb->subControls & (SC_SpinBoxUp | SC_SpinBoxDown)) { - const QRect updown = proxy()->subControlRect(CC_SpinBox, sb, SC_SpinBoxUp, widget) - | proxy()->subControlRect(CC_SpinBox, sb, SC_SpinBoxDown, widget); - - d->setupNSGraphicsContext(cg, NO); - - const auto aquaSize = d->effectiveAquaSizeConstrain(opt, widget); - const auto cw = QMacStylePrivate::CocoaControl(QMacStylePrivate::Stepper, aquaSize); - NSStepperCell *cell = static_cast(d->cocoaCell(cw)); - cell.enabled = (sb->state & State_Enabled); - - const CGRect newRect = [cell drawingRectForBounds:updown.toCGRect()]; - - const bool upPressed = sb->activeSubControls == SC_SpinBoxUp && (sb->state & State_Sunken); - const bool downPressed = sb->activeSubControls == SC_SpinBoxDown && (sb->state & State_Sunken); - const CGFloat x = CGRectGetMidX(newRect); - const CGFloat y = upPressed ? -3 : 3; // Weird coordinate shift going on. Verified with Hopper - const CGPoint pressPoint = CGPointMake(x, y); - // Pretend we're pressing the mouse on the right button. Unfortunately, NSStepperCell has no - // API to highlight a specific button. The highlighted property works only on the down button. - if (upPressed || downPressed) - [cell startTrackingAt:pressPoint inView:d->backingStoreNSView]; - - [cell drawWithFrame:newRect inView:d->backingStoreNSView]; - - if (upPressed || downPressed) - [cell stopTracking:pressPoint at:pressPoint inView:d->backingStoreNSView mouseIsUp:NO]; - - d->restoreNSGraphicsContext(cg); - } - } - break; -#endif -#if QT_CONFIG(combobox) - case CC_ComboBox: - if (const auto *combo = qstyleoption_cast(opt)) { - const bool isEnabled = combo->state & State_Enabled; - const bool isPressed = combo->state & State_Sunken; - - const auto ct = cocoaControlType(combo, widget); - const auto cs = d->effectiveAquaSizeConstrain(combo, widget); - const auto cw = QMacStylePrivate::CocoaControl(ct, cs); - auto *cc = static_cast(d->cocoaControl(cw)); - cc.enabled = isEnabled; - QRectF frameRect = cw.adjustedControlFrame(combo->rect);; - if (cw.type == QMacStylePrivate::Button_PopupButton) { - // Non-editable QComboBox - auto *pb = static_cast(cc); - // FIXME Old offsets. Try to move to adjustedControlFrame() - if (cw.size == QStyleHelper::SizeSmall) { - frameRect = frameRect.translated(0, 1); - } else if (cw.size == QStyleHelper::SizeMini) { - // Same 0.5 pt misalignment as AppKit and fit the focus ring - frameRect = frameRect.translated(2, -0.5); - } - pb.frame = frameRect.toCGRect(); - [pb highlight:isPressed]; - d->drawNSViewInRect(pb, frameRect, p, ^(CGContextRef, const CGRect &r) { - [pb.cell drawBezelWithFrame:r inView:pb.superview]; - }); - } else if (cw.type == QMacStylePrivate::ComboBox) { - // Editable QComboBox - auto *cb = static_cast(cc); - const auto frameRect = cw.adjustedControlFrame(combo->rect); - cb.frame = frameRect.toCGRect(); - - // This API was requested to Apple in rdar #36197888. We know it's safe to use up to macOS 10.13.3 - if (NSButtonCell *cell = static_cast([cc.cell qt_valueForPrivateKey:@"_buttonCell"])) { - cell.highlighted = isPressed; - } else { - // TODO Render to pixmap and darken the button manually - } - - d->drawNSViewInRect(cb, frameRect, p, ^(CGContextRef, const CGRect &r) { - // FIXME This is usually drawn in the control's superview, but we wouldn't get inactive look in this case - [cb.cell drawWithFrame:r inView:cb]; - }); - } - - if (combo->state & State_HasFocus) { - // TODO Remove and use QFocusFrame instead. - const int hMargin = proxy()->pixelMetric(QStyle::PM_FocusFrameHMargin, combo, widget); - const int vMargin = proxy()->pixelMetric(QStyle::PM_FocusFrameVMargin, combo, widget); - QRectF focusRect; - if (cw.type == QMacStylePrivate::Button_PopupButton) { - focusRect = QRectF::fromCGRect([cc alignmentRectForFrame:cc.frame]); - focusRect -= pullDownButtonShadowMargins[cw.size]; - if (cw.size == QStyleHelper::SizeSmall) - focusRect = focusRect.translated(0, 1); - else if (cw.size == QStyleHelper::SizeMini) - focusRect = focusRect.translated(2, -1); - } else if (cw.type == QMacStylePrivate::ComboBox) { - focusRect = frameRect - comboBoxFocusRingMargins[cw.size]; - } - d->drawFocusRing(p, focusRect, hMargin, vMargin, cw); - } - } - break; -#endif // QT_CONFIG(combobox) - case CC_TitleBar: - if (const auto *titlebar = qstyleoption_cast(opt)) { - const bool isActive = (titlebar->state & State_Active) - && (titlebar->titleBarState & State_Active); - - p->fillRect(opt->rect, Qt::transparent); - p->setRenderHint(QPainter::Antialiasing); - p->setClipRect(opt->rect, Qt::IntersectClip); - - // FIXME A single drawPath() with 0-sized pen - // doesn't look as good as this double fillPath(). - const auto outerFrameRect = QRectF(opt->rect.adjusted(0, 0, 0, opt->rect.height())); - QPainterPath outerFramePath = d->windowPanelPath(outerFrameRect); - p->fillPath(outerFramePath, opt->palette.dark()); - - const auto frameAdjust = 1.0 / p->device()->devicePixelRatioF(); - const auto innerFrameRect = outerFrameRect.adjusted(frameAdjust, frameAdjust, -frameAdjust, 0); - QPainterPath innerFramePath = d->windowPanelPath(innerFrameRect); - if (isActive) { - QLinearGradient g; - g.setStart(QPointF(0, 0)); - g.setFinalStop(QPointF(0, 2 * opt->rect.height())); - g.setColorAt(0, opt->palette.button().color()); - g.setColorAt(1, opt->palette.dark().color()); - p->fillPath(innerFramePath, g); - } else { - p->fillPath(innerFramePath, opt->palette.button()); - } - - if (titlebar->subControls & (SC_TitleBarCloseButton - | SC_TitleBarMaxButton - | SC_TitleBarMinButton - | SC_TitleBarNormalButton)) { - const bool isHovered = (titlebar->state & State_MouseOver); - static const SubControl buttons[] = { - SC_TitleBarCloseButton, SC_TitleBarMinButton, SC_TitleBarMaxButton - }; - for (const auto sc : buttons) { - const auto ct = d->windowButtonCocoaControl(sc); - const auto cw = QMacStylePrivate::CocoaControl(ct, QStyleHelper::SizeLarge); - auto *wb = static_cast(d->cocoaControl(cw)); - wb.enabled = (sc & titlebar->subControls); - [wb highlight:(titlebar->state & State_Sunken) && (sc & titlebar->activeSubControls)]; - Q_UNUSED(isHovered); // FIXME No public API for this - - const auto buttonRect = proxy()->subControlRect(CC_TitleBar, titlebar, sc, widget); - d->drawNSViewInRect(wb, buttonRect, p, ^(CGContextRef, const CGRect &rect) { - auto *wbCell = static_cast(wb.cell); - [wbCell drawWithFrame:rect inView:wb]; - }); - } - } - - if (titlebar->subControls & SC_TitleBarLabel) { - const auto tr = proxy()->subControlRect(CC_TitleBar, titlebar, SC_TitleBarLabel, widget); - if (!titlebar->icon.isNull()) { - const auto iconExtent = proxy()->pixelMetric(PM_SmallIconSize); - const auto iconSize = QSize(iconExtent, iconExtent); - const auto iconPos = tr.x() - titlebar->icon.actualSize(iconSize).width() - qRound(titleBarIconTitleSpacing); - // Only render the icon if it'll be fully visible - if (iconPos < tr.right() - titleBarIconTitleSpacing) - p->drawPixmap(iconPos, tr.y(), titlebar->icon.pixmap(window, iconSize, QIcon::Normal)); - } - - if (!titlebar->text.isEmpty()) - drawItemText(p, tr, Qt::AlignCenter, opt->palette, isActive, titlebar->text, QPalette::Text); - } - } - break; - case CC_GroupBox: - if (const QStyleOptionGroupBox *gb - = qstyleoption_cast(opt)) { - - QStyleOptionGroupBox groupBox(*gb); - const bool flat = groupBox.features & QStyleOptionFrame::Flat; - if (!flat) - groupBox.state |= QStyle::State_Mini; // Force mini-sized checkbox to go with small-sized label - else - groupBox.subControls = groupBox.subControls & ~SC_GroupBoxFrame; // We don't like frames and ugly lines - - const bool didSetFont = widget && widget->testAttribute(Qt::WA_SetFont); - const bool didModifySubControls = !didSetFont && QApplication::desktopSettingsAware(); - if (didModifySubControls) - groupBox.subControls = groupBox.subControls & ~SC_GroupBoxLabel; - QCommonStyle::drawComplexControl(cc, &groupBox, p, widget); - if (didModifySubControls) { - const QRect rect = proxy()->subControlRect(CC_GroupBox, &groupBox, SC_GroupBoxLabel, widget); - const bool rtl = groupBox.direction == Qt::RightToLeft; - const int alignment = Qt::TextHideMnemonic | (rtl ? Qt::AlignRight : Qt::AlignLeft); - const QFont savedFont = p->font(); - if (!flat) - p->setFont(d->smallSystemFont); - proxy()->drawItemText(p, rect, alignment, groupBox.palette, groupBox.state & State_Enabled, groupBox.text, QPalette::WindowText); - if (!flat) - p->setFont(savedFont); - } - } - break; - case CC_ToolButton: - if (const QStyleOptionToolButton *tb - = qstyleoption_cast(opt)) { -#ifndef QT_NO_ACCESSIBILITY - if (QStyleHelper::hasAncestor(opt->styleObject, QAccessible::ToolBar)) { - if (tb->subControls & SC_ToolButtonMenu) { - QStyleOption arrowOpt = *tb; - arrowOpt.rect = proxy()->subControlRect(cc, tb, SC_ToolButtonMenu, widget); - arrowOpt.rect.setY(arrowOpt.rect.y() + arrowOpt.rect.height() / 2); - arrowOpt.rect.setHeight(arrowOpt.rect.height() / 2); - proxy()->drawPrimitive(PE_IndicatorArrowDown, &arrowOpt, p, widget); - } else if ((tb->features & QStyleOptionToolButton::HasMenu) - && (tb->toolButtonStyle != Qt::ToolButtonTextOnly && !tb->icon.isNull())) { - d->drawToolbarButtonArrow(tb, p); - } - if (tb->state & State_On) { - NSView *view = window ? (NSView *)window->winId() : nil; - bool isKey = false; - if (view) - isKey = [view.window isKeyWindow]; - - QBrush brush(isKey ? QColor(0, 0, 0, 28) - : QColor(0, 0, 0, 21)); - QPainterPath path; - path.addRoundedRect(QRectF(tb->rect.x(), tb->rect.y(), tb->rect.width(), tb->rect.height() + 4), 4, 4); - p->setRenderHint(QPainter::Antialiasing); - p->fillPath(path, brush); - } - proxy()->drawControl(CE_ToolButtonLabel, opt, p, widget); - } else -#endif // QT_NO_ACCESSIBILITY - { - auto bflags = tb->state; - if (tb->subControls & SC_ToolButton) - bflags |= State_Sunken; - auto mflags = tb->state; - if (tb->subControls & SC_ToolButtonMenu) - mflags |= State_Sunken; - - if (tb->subControls & SC_ToolButton) { - if (bflags & (State_Sunken | State_On | State_Raised)) { - const bool isEnabled = tb->state & State_Enabled; - const bool isPressed = tb->state & State_Sunken; - const bool isHighlighted = (tb->state & State_Active) && (tb->state & State_On); - const auto ct = QMacStylePrivate::Button_PushButton; - const auto cs = d->effectiveAquaSizeConstrain(opt, widget); - const auto cw = QMacStylePrivate::CocoaControl(ct, cs); - auto *pb = static_cast(d->cocoaControl(cw)); - pb.bezelStyle = NSShadowlessSquareBezelStyle; // TODO Use NSTexturedRoundedBezelStyle in the future. - pb.frame = opt->rect.toCGRect(); - pb.buttonType = NSPushOnPushOffButton; - pb.enabled = isEnabled; - [pb highlight:isPressed]; - pb.state = isHighlighted && !isPressed ? NSOnState : NSOffState; - const auto buttonRect = proxy()->subControlRect(cc, tb, SC_ToolButton, widget); - d->drawNSViewInRect(pb, buttonRect, p, ^(CGContextRef, const CGRect &rect) { - [pb.cell drawBezelWithFrame:rect inView:pb]; - }); - } - } - - if (tb->subControls & SC_ToolButtonMenu) { - const auto menuRect = proxy()->subControlRect(cc, tb, SC_ToolButtonMenu, widget); - QStyleOption arrowOpt = *tb; - arrowOpt.rect = QRect(menuRect.x() + ((menuRect.width() - toolButtonArrowSize) / 2), - menuRect.height() - (toolButtonArrowSize + toolButtonArrowMargin), - toolButtonArrowSize, - toolButtonArrowSize); - proxy()->drawPrimitive(PE_IndicatorArrowDown, &arrowOpt, p, widget); - } else if (tb->features & QStyleOptionToolButton::HasMenu) { - d->drawToolbarButtonArrow(tb, p); - } - QRect buttonRect = proxy()->subControlRect(CC_ToolButton, tb, SC_ToolButton, widget); - int fw = proxy()->pixelMetric(PM_DefaultFrameWidth, opt, widget); - QStyleOptionToolButton label = *tb; - label.rect = buttonRect.adjusted(fw, fw, -fw, -fw); - proxy()->drawControl(CE_ToolButtonLabel, &label, p, widget); - } - } - break; -#if QT_CONFIG(dial) - case CC_Dial: - if (const QStyleOptionSlider *dial = qstyleoption_cast(opt)) - QStyleHelper::drawDial(dial, p); - break; -#endif - default: - QCommonStyle::drawComplexControl(cc, opt, p, widget); - break; - } -} - -QStyle::SubControl QMacStyle::hitTestComplexControl(ComplexControl cc, - const QStyleOptionComplex *opt, - const QPoint &pt, const QWidget *widget) const -{ - Q_D(const QMacStyle); - SubControl sc = QStyle::SC_None; - switch (cc) { - case CC_ComboBox: - if (const QStyleOptionComboBox *cmb = qstyleoption_cast(opt)) { - sc = QCommonStyle::hitTestComplexControl(cc, cmb, pt, widget); - if (!cmb->editable && sc != QStyle::SC_None) - sc = SC_ComboBoxArrow; // A bit of a lie, but what we want - } - break; - case CC_Slider: - if (const QStyleOptionSlider *sl = qstyleoption_cast(opt)) { - if (!sl->rect.contains(pt)) - break; - - const bool hasTicks = sl->tickPosition != QSlider::NoTicks; - const bool isHorizontal = sl->orientation == Qt::Horizontal; - const auto ct = isHorizontal ? QMacStylePrivate::Slider_Horizontal : QMacStylePrivate::Slider_Vertical; - const auto cs = d->effectiveAquaSizeConstrain(opt, widget); - const auto cw = QMacStylePrivate::CocoaControl(ct, cs); - auto *slider = static_cast(d->cocoaControl(cw)); - if (!setupSlider(slider, sl)) - break; - - [slider calcSize]; - NSSliderCell *cell = slider.cell; - const auto barRect = QRectF::fromCGRect([cell barRectFlipped:hasTicks]); - const auto knobRect = QRectF::fromCGRect([cell knobRectFlipped:NO]); - if (knobRect.contains(pt)) { - sc = SC_SliderHandle; - } else if (barRect.contains(pt)) { - sc = SC_SliderGroove; - } else if (hasTicks) { - sc = SC_SliderTickmarks; - } - } - break; - case CC_ScrollBar: - if (const QStyleOptionSlider *sb = qstyleoption_cast(opt)) { - if (!sb->rect.contains(pt)) { - sc = SC_None; - break; - } - - const bool isHorizontal = sb->orientation == Qt::Horizontal; - const auto ct = isHorizontal ? QMacStylePrivate::Scroller_Horizontal : QMacStylePrivate::Scroller_Vertical; - const auto cs = d->effectiveAquaSizeConstrain(opt, widget); - const auto cw = QMacStylePrivate::CocoaControl(ct, cs); - auto *scroller = static_cast(d->cocoaControl(cw)); - if (!setupScroller(scroller, sb)) { - sc = SC_None; - break; - } - - // Since -[NSScroller testPart:] doesn't want to cooperate, we do it the - // straightforward way. In any case, macOS doesn't return line-sized changes - // with NSScroller since 10.7, according to the aforementioned method's doc. - const auto knobRect = QRectF::fromCGRect([scroller rectForPart:NSScrollerKnob]); - if (isHorizontal) { - const bool isReverse = sb->direction == Qt::RightToLeft; - if (pt.x() < knobRect.left()) - sc = isReverse ? SC_ScrollBarAddPage : SC_ScrollBarSubPage; - else if (pt.x() > knobRect.right()) - sc = isReverse ? SC_ScrollBarSubPage : SC_ScrollBarAddPage; - else - sc = SC_ScrollBarSlider; - } else { - if (pt.y() < knobRect.top()) - sc = SC_ScrollBarSubPage; - else if (pt.y() > knobRect.bottom()) - sc = SC_ScrollBarAddPage; - else - sc = SC_ScrollBarSlider; - } - } - break; - default: - sc = QCommonStyle::hitTestComplexControl(cc, opt, pt, widget); - break; - } - return sc; -} - -QRect QMacStyle::subControlRect(ComplexControl cc, const QStyleOptionComplex *opt, SubControl sc, - const QWidget *widget) const -{ - Q_D(const QMacStyle); - QRect ret; - switch (cc) { - case CC_ScrollBar: - if (const QStyleOptionSlider *sb = qstyleoption_cast(opt)) { - const bool isHorizontal = sb->orientation == Qt::Horizontal; - const bool isReverseHorizontal = isHorizontal && (sb->direction == Qt::RightToLeft); - - NSScrollerPart part = NSScrollerNoPart; - if (sc == SC_ScrollBarSlider) { - part = NSScrollerKnob; - } else if (sc == SC_ScrollBarGroove) { - part = NSScrollerKnobSlot; - } else if (sc == SC_ScrollBarSubPage || sc == SC_ScrollBarAddPage) { - if ((!isReverseHorizontal && sc == SC_ScrollBarSubPage) - || (isReverseHorizontal && sc == SC_ScrollBarAddPage)) - part = NSScrollerDecrementPage; - else - part = NSScrollerIncrementPage; - } - // And nothing else since 10.7 - - if (part != NSScrollerNoPart) { - const auto ct = isHorizontal ? QMacStylePrivate::Scroller_Horizontal : QMacStylePrivate::Scroller_Vertical; - const auto cs = d->effectiveAquaSizeConstrain(opt, widget); - const auto cw = QMacStylePrivate::CocoaControl(ct, cs); - auto *scroller = static_cast(d->cocoaControl(cw)); - if (setupScroller(scroller, sb)) - ret = QRectF::fromCGRect([scroller rectForPart:part]).toRect(); - } - } - break; - case CC_Slider: - if (const QStyleOptionSlider *sl = qstyleoption_cast(opt)) { - const bool hasTicks = sl->tickPosition != QSlider::NoTicks; - const bool isHorizontal = sl->orientation == Qt::Horizontal; - const auto ct = isHorizontal ? QMacStylePrivate::Slider_Horizontal : QMacStylePrivate::Slider_Vertical; - const auto cs = d->effectiveAquaSizeConstrain(opt, widget); - const auto cw = QMacStylePrivate::CocoaControl(ct, cs); - auto *slider = static_cast(d->cocoaControl(cw)); - if (!setupSlider(slider, sl)) - break; - - [slider calcSize]; - NSSliderCell *cell = slider.cell; - if (sc == SC_SliderHandle) { - ret = QRectF::fromCGRect([cell knobRectFlipped:NO]).toRect(); - if (isHorizontal) { - ret.setTop(sl->rect.top()); - ret.setBottom(sl->rect.bottom()); - } else { - ret.setLeft(sl->rect.left()); - ret.setRight(sl->rect.right()); - } - } else if (sc == SC_SliderGroove) { - ret = QRectF::fromCGRect([cell barRectFlipped:hasTicks]).toRect(); - } else if (hasTicks && sc == SC_SliderTickmarks) { - const auto tickMarkRect = QRectF::fromCGRect([cell rectOfTickMarkAtIndex:0]); - if (isHorizontal) - ret = QRect(sl->rect.left(), tickMarkRect.top(), sl->rect.width(), tickMarkRect.height()); - else - ret = QRect(tickMarkRect.left(), sl->rect.top(), tickMarkRect.width(), sl->rect.height()); - } - - // Invert if needed and extend to the actual bounds of the slider - if (isHorizontal) { - if (sl->upsideDown) { - ret = QRect(sl->rect.right() - ret.right(), sl->rect.top(), ret.width(), sl->rect.height()); - } else { - ret.setTop(sl->rect.top()); - ret.setBottom(sl->rect.bottom()); - } - } else { - if (!sl->upsideDown) { - ret = QRect(sl->rect.left(), sl->rect.bottom() - ret.bottom(), sl->rect.width(), ret.height()); - } else { - ret.setLeft(sl->rect.left()); - ret.setRight(sl->rect.right()); - } - } - } - break; - case CC_TitleBar: - if (const auto *titlebar = qstyleoption_cast(opt)) { - // The title bar layout is as follows: close, min, zoom, icon, title - // [ x _ + @ Window Title ] - // Center the icon and title until it starts to overlap with the buttons. - // The icon doesn't count towards SC_TitleBarLabel, but it's still rendered - // next to the title text. See drawComplexControl(). - if (sc == SC_TitleBarLabel) { - qreal labelWidth = titlebar->fontMetrics.horizontalAdvance(titlebar->text) + 1; // FIXME Rounding error? - qreal labelHeight = titlebar->fontMetrics.height(); - - const auto lastButtonRect = proxy()->subControlRect(CC_TitleBar, titlebar, SC_TitleBarMaxButton, widget); - qreal controlsSpacing = lastButtonRect.right() + titleBarButtonSpacing; - if (!titlebar->icon.isNull()) { - const auto iconSize = proxy()->pixelMetric(PM_SmallIconSize); - const auto actualIconSize = titlebar->icon.actualSize(QSize(iconSize, iconSize)).width();; - controlsSpacing += actualIconSize + titleBarIconTitleSpacing; - } - - const qreal labelPos = qMax(controlsSpacing, (opt->rect.width() - labelWidth) / 2.0); - labelWidth = qMin(labelWidth, opt->rect.width() - (labelPos + titleBarTitleRightMargin)); - ret = QRect(labelPos, (opt->rect.height() - labelHeight) / 2, - labelWidth, labelHeight); - } else { - const auto currentButton = d->windowButtonCocoaControl(sc); - if (currentButton == QMacStylePrivate::NoControl) - break; - - QPointF buttonPos = titlebar->rect.topLeft() + QPointF(titleBarButtonSpacing, 0); - QSizeF buttonSize; - for (int ct = QMacStylePrivate::Button_WindowClose; ct <= currentButton; ct++) { - const auto cw = QMacStylePrivate::CocoaControl(QMacStylePrivate::CocoaControlType(ct), - QStyleHelper::SizeLarge); - auto *wb = static_cast(d->cocoaControl(cw)); - if (ct == currentButton) - buttonSize = QSizeF::fromCGSize(wb.frame.size); - else - buttonPos.rx() += wb.frame.size.width + titleBarButtonSpacing; - } - - const auto vOffset = (opt->rect.height() - buttonSize.height()) / 2.0; - ret = QRectF(buttonPos, buttonSize).translated(0, vOffset).toRect(); - } - } - break; - case CC_ComboBox: - if (const QStyleOptionComboBox *combo = qstyleoption_cast(opt)) { - const auto ct = cocoaControlType(combo, widget); - const auto cs = d->effectiveAquaSizeConstrain(combo, widget); - const auto cw = QMacStylePrivate::CocoaControl(ct, cs); - const auto editRect = QMacStylePrivate::comboboxEditBounds(cw.adjustedControlFrame(combo->rect), cw); - - switch (sc) { - case SC_ComboBoxEditField:{ - ret = editRect.toAlignedRect(); - break; } - case SC_ComboBoxArrow:{ - ret = editRect.toAlignedRect(); - ret.setX(ret.x() + ret.width()); - ret.setWidth(combo->rect.right() - ret.right()); - break; } - case SC_ComboBoxListBoxPopup:{ - if (combo->editable) { - const CGRect inner = QMacStylePrivate::comboboxInnerBounds(combo->rect.toCGRect(), cw); - const int comboTop = combo->rect.top(); - ret = QRect(qRound(inner.origin.x), - comboTop, - qRound(inner.origin.x - combo->rect.left() + inner.size.width), - editRect.bottom() - comboTop + 2); - } else { - ret = QRect(combo->rect.x() + 4 - 11, - combo->rect.y() + 1, - editRect.width() + 10 + 11, - 1); - } - break; } - default: - break; - } - } - break; - case CC_GroupBox: - if (const QStyleOptionGroupBox *groupBox = qstyleoption_cast(opt)) { - bool checkable = groupBox->subControls & SC_GroupBoxCheckBox; - const bool flat = groupBox->features & QStyleOptionFrame::Flat; - bool hasNoText = !checkable && groupBox->text.isEmpty(); - switch (sc) { - case SC_GroupBoxLabel: - case SC_GroupBoxCheckBox: { - // Cheat and use the smaller font if we need to - const bool checkable = groupBox->subControls & SC_GroupBoxCheckBox; - const bool fontIsSet = (widget && widget->testAttribute(Qt::WA_SetFont)) - || !QApplication::desktopSettingsAware(); - const int margin = flat || hasNoText ? 0 : 9; - ret = groupBox->rect.adjusted(margin, 0, -margin, 0); - - const QFontMetricsF fm = flat || fontIsSet ? QFontMetricsF(groupBox->fontMetrics) : QFontMetricsF(d->smallSystemFont); - const QSizeF s = fm.size(Qt::AlignHCenter | Qt::AlignVCenter, qt_mac_removeMnemonics(groupBox->text), 0, nullptr); - const int tw = qCeil(s.width()); - const int h = qCeil(fm.height()); - ret.setHeight(h); - - QRect labelRect = alignedRect(groupBox->direction, groupBox->textAlignment, - QSize(tw, h), ret); - if (flat && checkable) - labelRect.moveLeft(labelRect.left() + 4); - int indicatorWidth = proxy()->pixelMetric(PM_IndicatorWidth, opt, widget); - bool rtl = groupBox->direction == Qt::RightToLeft; - if (sc == SC_GroupBoxLabel) { - if (checkable) { - int newSum = indicatorWidth + 1; - int newLeft = labelRect.left() + (rtl ? -newSum : newSum); - labelRect.moveLeft(newLeft); - if (flat) - labelRect.moveTop(labelRect.top() + 3); - else - labelRect.moveTop(labelRect.top() + 4); - } else if (flat) { - int newLeft = labelRect.left() - (rtl ? 3 : -3); - labelRect.moveLeft(newLeft); - labelRect.moveTop(labelRect.top() + 3); - } else { - int newLeft = labelRect.left() - (rtl ? 3 : 2); - labelRect.moveLeft(newLeft); - labelRect.moveTop(labelRect.top() + 4); - } - ret = labelRect; - } - - if (sc == SC_GroupBoxCheckBox) { - int left = rtl ? labelRect.right() - indicatorWidth : labelRect.left() - 1; - int top = flat ? ret.top() + 1 : ret.top() + 5; - ret.setRect(left, top, - indicatorWidth, proxy()->pixelMetric(PM_IndicatorHeight, opt, widget)); - } - break; - } - case SC_GroupBoxContents: - case SC_GroupBoxFrame: { - QFontMetrics fm = groupBox->fontMetrics; - int yOffset = 3; - if (!flat) { - if (widget && !widget->testAttribute(Qt::WA_SetFont) - && QApplication::desktopSettingsAware()) - fm = QFontMetrics(qt_app_fonts_hash()->value("QSmallFont", QFont())); - yOffset = 5; - } - - if (hasNoText) - yOffset = -qCeil(QFontMetricsF(fm).height()); - ret = opt->rect.adjusted(0, qCeil(QFontMetricsF(fm).height()) + yOffset, 0, 0); - if (sc == SC_GroupBoxContents) { - if (flat) - ret.adjust(3, -5, -3, -4); // guess too - else - ret.adjust(3, 3, -3, -4); // guess - } - } - break; - default: - ret = QCommonStyle::subControlRect(cc, groupBox, sc, widget); - break; - } - } - break; -#if QT_CONFIG(spinbox) - case CC_SpinBox: - if (const QStyleOptionSpinBox *spin = qstyleoption_cast(opt)) { - QStyleHelper::WidgetSizePolicy aquaSize = d->effectiveAquaSizeConstrain(spin, widget); - const auto fw = proxy()->pixelMetric(PM_SpinBoxFrameWidth, spin, widget); - int spinner_w; - int spinBoxSep; - switch (aquaSize) { - case QStyleHelper::SizeLarge: - spinner_w = 14; - spinBoxSep = 2; - break; - case QStyleHelper::SizeSmall: - spinner_w = 12; - spinBoxSep = 2; - break; - case QStyleHelper::SizeMini: - spinner_w = 10; - spinBoxSep = 1; - break; - default: - Q_UNREACHABLE(); - } - - switch (sc) { - case SC_SpinBoxUp: - case SC_SpinBoxDown: { - if (spin->buttonSymbols == QAbstractSpinBox::NoButtons) - break; - - const int y = fw; - const int x = spin->rect.width() - spinner_w; - ret.setRect(x + spin->rect.x(), y + spin->rect.y(), spinner_w, spin->rect.height() - y * 2); - int hackTranslateX; - switch (aquaSize) { - case QStyleHelper::SizeLarge: - hackTranslateX = 0; - break; - case QStyleHelper::SizeSmall: - hackTranslateX = -2; - break; - case QStyleHelper::SizeMini: - hackTranslateX = -1; - break; - default: - Q_UNREACHABLE(); - } - - const auto cw = QMacStylePrivate::CocoaControl(QMacStylePrivate::Stepper, aquaSize); - NSStepperCell *cell = static_cast(d->cocoaCell(cw)); - const CGRect outRect = [cell drawingRectForBounds:ret.toCGRect()]; - ret = QRectF::fromCGRect(outRect).toRect(); - - switch (sc) { - case SC_SpinBoxUp: - ret.setHeight(ret.height() / 2); - break; - case SC_SpinBoxDown: - ret.setY(ret.y() + ret.height() / 2); - break; - default: - Q_ASSERT(0); - break; - } - ret.translate(hackTranslateX, 0); // hack: position the buttons correctly (weird that we need this) - ret = visualRect(spin->direction, spin->rect, ret); - break; - } - case SC_SpinBoxEditField: - ret = spin->rect.adjusted(fw, fw, -fw, -fw); - if (spin->buttonSymbols != QAbstractSpinBox::NoButtons) { - ret.setWidth(spin->rect.width() - spinBoxSep - spinner_w); - ret = visualRect(spin->direction, spin->rect, ret); - } - break; - default: - ret = QCommonStyle::subControlRect(cc, spin, sc, widget); - break; - } - } - break; -#endif - case CC_ToolButton: - ret = QCommonStyle::subControlRect(cc, opt, sc, widget); - if (sc == SC_ToolButtonMenu) { -#ifndef QT_NO_ACCESSIBILITY - if (QStyleHelper::hasAncestor(opt->styleObject, QAccessible::ToolBar)) - ret.adjust(-toolButtonArrowMargin, 0, 0, 0); -#endif - ret.adjust(-1, 0, 0, 0); - } - break; - default: - ret = QCommonStyle::subControlRect(cc, opt, sc, widget); - break; - } - return ret; -} - -QSize QMacStyle::sizeFromContents(ContentsType ct, const QStyleOption *opt, - const QSize &csz, const QWidget *widget) const -{ - Q_D(const QMacStyle); - QSize sz(csz); - bool useAquaGuideline = true; - - switch (ct) { -#if QT_CONFIG(spinbox) - case CT_SpinBox: - if (qstyleoption_cast(opt)) { - const int buttonWidth = 20; // FIXME Use subControlRect() - sz += QSize(buttonWidth, 0); - } - break; -#endif - case QStyle::CT_TabWidget: - // the size between the pane and the "contentsRect" (+4,+4) - // (the "contentsRect" is on the inside of the pane) - sz = QCommonStyle::sizeFromContents(ct, opt, csz, widget); - /** - This is supposed to show the relationship between the tabBar and - the stack widget of a QTabWidget. - Unfortunately ascii is not a good way of representing graphics..... - PS: The '=' line is the painted frame. - - top ---+ - | - | - | - | vvv just outside the painted frame is the "pane" - - -|- - - - - - - - - - <-+ - TAB BAR +=====^============ | +2 pixels - - - -|- - -|- - - - - - - <-+ - | | ^ ^^^ just inside the painted frame is the "contentsRect" - | | | - | overlap | - | | | - bottom ------+ <-+ +14 pixels - | - v - ------------------------------ <- top of stack widget - - - To summarize: - * 2 is the distance between the pane and the contentsRect - * The 14 and the 1's are the distance from the contentsRect to the stack widget. - (same value as used in SE_TabWidgetTabContents) - * overlap is how much the pane should overlap the tab bar - */ - // then add the size between the stackwidget and the "contentsRect" -#if QT_CONFIG(tabwidget) - if (const QStyleOptionTabWidgetFrame *twf - = qstyleoption_cast(opt)) { - QSize extra(0,0); - const int overlap = pixelMetric(PM_TabBarBaseOverlap, opt, widget); - const int gapBetweenTabbarAndStackWidget = 2 + 14 - overlap; - - const auto tabDirection = QMacStylePrivate::tabDirection(twf->shape); - if (tabDirection == QMacStylePrivate::North - || tabDirection == QMacStylePrivate::South) { - extra = QSize(2, gapBetweenTabbarAndStackWidget + 1); - } else { - extra = QSize(gapBetweenTabbarAndStackWidget + 1, 2); - } - sz+= extra; - } -#endif - break; -#if QT_CONFIG(tabbar) - case QStyle::CT_TabBarTab: - if (const QStyleOptionTab *tab = qstyleoption_cast(opt)) { - const bool differentFont = (widget && widget->testAttribute(Qt::WA_SetFont)) - || !QApplication::desktopSettingsAware(); - const auto tabDirection = QMacStylePrivate::tabDirection(tab->shape); - const bool verticalTabs = tabDirection == QMacStylePrivate::East - || tabDirection == QMacStylePrivate::West; - if (verticalTabs) - sz = sz.transposed(); - - int defaultTabHeight; - const auto cs = d->effectiveAquaSizeConstrain(opt, widget); - switch (cs) { - case QStyleHelper::SizeLarge: - if (tab->documentMode) - defaultTabHeight = 24; - else - defaultTabHeight = 21; - break; - case QStyleHelper::SizeSmall: - defaultTabHeight = 18; - break; - case QStyleHelper::SizeMini: - defaultTabHeight = 16; - break; - default: - break; - } - - const bool widthSet = !differentFont && tab->icon.isNull(); - if (widthSet) { - const auto textSize = opt->fontMetrics.size(Qt::TextShowMnemonic, tab->text); - sz.rwidth() = textSize.width(); - sz.rheight() = qMax(defaultTabHeight, textSize.height()); - } else { - sz.rheight() = qMax(defaultTabHeight, sz.height()); - } - sz.rwidth() += proxy()->pixelMetric(PM_TabBarTabHSpace, tab, widget); - - if (verticalTabs) - sz = sz.transposed(); - - int maxWidgetHeight = qMax(tab->leftButtonSize.height(), tab->rightButtonSize.height()); - int maxWidgetWidth = qMax(tab->leftButtonSize.width(), tab->rightButtonSize.width()); - - int widgetWidth = 0; - int widgetHeight = 0; - int padding = 0; - if (tab->leftButtonSize.isValid()) { - padding += 8; - widgetWidth += tab->leftButtonSize.width(); - widgetHeight += tab->leftButtonSize.height(); - } - if (tab->rightButtonSize.isValid()) { - padding += 8; - widgetWidth += tab->rightButtonSize.width(); - widgetHeight += tab->rightButtonSize.height(); - } - - if (verticalTabs) { - sz.setWidth(qMax(sz.width(), maxWidgetWidth)); - sz.setHeight(sz.height() + widgetHeight + padding); - } else { - if (widthSet) - sz.setWidth(sz.width() + widgetWidth + padding); - sz.setHeight(qMax(sz.height(), maxWidgetHeight)); - } - } - break; -#endif - case QStyle::CT_PushButton: { - if (const QStyleOptionButton *btn = qstyleoption_cast(opt)) - if (btn->features & QStyleOptionButton::CommandLinkButton) - return QCommonStyle::sizeFromContents(ct, opt, sz, widget); - - // By default, we fit the contents inside a normal rounded push button. - // Do this by add enough space around the contents so that rounded - // borders (including highlighting when active) will show. - // TODO Use QFocusFrame and get rid of these horrors. - QSize macsz; - const auto controlSize = d->effectiveAquaSizeConstrain(opt, widget, CT_PushButton, sz, &macsz); - // FIXME See comment in CT_PushButton case in qt_aqua_get_known_size(). - if (macsz.width() != -1) - sz.setWidth(macsz.width()); - else - sz.rwidth() += QMacStylePrivate::PushButtonLeftOffset + QMacStylePrivate::PushButtonRightOffset + 12; - // All values as measured from HIThemeGetButtonBackgroundBounds() - if (controlSize != QStyleHelper::SizeMini) - sz.rwidth() += 12; // We like 12 over here. - if (controlSize == QStyleHelper::SizeLarge && sz.height() > 16) - sz.rheight() += pushButtonDefaultHeight[QStyleHelper::SizeLarge] - 16; - else if (controlSize == QStyleHelper::SizeMini) - sz.setHeight(24); // FIXME Our previous HITheme-based logic returned this. - else - sz.setHeight(pushButtonDefaultHeight[controlSize]); - break; - } - case QStyle::CT_MenuItem: - if (const QStyleOptionMenuItem *mi = qstyleoption_cast(opt)) { - int maxpmw = mi->maxIconWidth; -#if QT_CONFIG(combobox) - const QComboBox *comboBox = qobject_cast(widget); -#endif - int w = sz.width(), - h = sz.height(); - if (mi->menuItemType == QStyleOptionMenuItem::Separator) { - w = 10; - h = qt_mac_aqua_get_metric(MenuSeparatorHeight); - } else { - h = mi->fontMetrics.height() + 2; - if (!mi->icon.isNull()) { -#if QT_CONFIG(combobox) - if (comboBox) { - const QSize &iconSize = comboBox->iconSize(); - h = qMax(h, iconSize.height() + 4); - maxpmw = qMax(maxpmw, iconSize.width()); - } else -#endif - { - int iconExtent = proxy()->pixelMetric(PM_SmallIconSize); - h = qMax(h, mi->icon.actualSize(QSize(iconExtent, iconExtent)).height() + 4); - } - } - } - if (mi->text.contains(QLatin1Char('\t'))) - w += 12; - else if (mi->menuItemType == QStyleOptionMenuItem::SubMenu) - w += 35; // Not quite exactly as it seems to depend on other factors - if (maxpmw) - w += maxpmw + 6; - // add space for a check. All items have place for a check too. - w += 20; -#if QT_CONFIG(combobox) - if (comboBox && comboBox->isVisible()) { - QStyleOptionComboBox cmb; - cmb.initFrom(comboBox); - cmb.editable = false; - cmb.subControls = QStyle::SC_ComboBoxEditField; - cmb.activeSubControls = QStyle::SC_None; - w = qMax(w, subControlRect(QStyle::CC_ComboBox, &cmb, - QStyle::SC_ComboBoxEditField, - comboBox).width()); - } else -#endif - { - w += 12; - } - sz = QSize(w, h); - } - break; - case CT_MenuBarItem: - if (!sz.isEmpty()) - sz += QSize(12, 4); // Constants from QWindowsStyle - break; - case CT_ToolButton: - sz.rwidth() += 10; - sz.rheight() += 10; - if (const auto *tb = qstyleoption_cast(opt)) - if (tb->features & QStyleOptionToolButton::Menu) - sz.rwidth() += toolButtonArrowMargin; - return sz; - case CT_ComboBox: - if (const auto *cb = qstyleoption_cast(opt)) { - const auto controlSize = d->effectiveAquaSizeConstrain(opt, widget); - if (!cb->editable) { - // Same as CT_PushButton, because we have to fit the focus - // ring and a non-editable combo box is a NSPopUpButton. - sz.rwidth() += QMacStylePrivate::PushButtonLeftOffset + QMacStylePrivate::PushButtonRightOffset + 12; - // All values as measured from HIThemeGetButtonBackgroundBounds() - if (controlSize != QStyleHelper::SizeMini) - sz.rwidth() += 12; // We like 12 over here. -#if 0 - // TODO Maybe support square combo boxes - if (controlSize == QStyleHelper::SizeLarge && sz.height() > 16) - sz.rheight() += popupButtonDefaultHeight[QStyleHelper::SizeLarge] - 16; - else -#endif - } else { - sz.rwidth() += 50; // FIXME Double check this - } - - // This should be enough to fit the focus ring - if (controlSize == QStyleHelper::SizeMini) - sz.setHeight(24); // FIXME Our previous HITheme-based logic returned this for CT_PushButton. - else - sz.setHeight(pushButtonDefaultHeight[controlSize]); - - return sz; - } - break; - case CT_Menu: { - if (proxy() == this) { - sz = csz; - } else { - QStyleHintReturnMask menuMask; - QStyleOption myOption = *opt; - myOption.rect.setSize(sz); - if (proxy()->styleHint(SH_Menu_Mask, &myOption, widget, &menuMask)) - sz = menuMask.region.boundingRect().size(); - } - break; } - case CT_HeaderSection:{ - const QStyleOptionHeader *header = qstyleoption_cast(opt); - sz = QCommonStyle::sizeFromContents(ct, opt, csz, widget); - if (header->text.contains(QLatin1Char('\n'))) - useAquaGuideline = false; - break; } - case CT_ScrollBar : - // Make sure that the scroll bar is large enough to display the thumb indicator. - if (const QStyleOptionSlider *slider = qstyleoption_cast(opt)) { - const int minimumSize = 24; // Smallest knob size, but Cocoa doesn't seem to care - if (slider->orientation == Qt::Horizontal) - sz = sz.expandedTo(QSize(minimumSize, sz.height())); - else - sz = sz.expandedTo(QSize(sz.width(), minimumSize)); - } - break; - case CT_ItemViewItem: - if (const QStyleOptionViewItem *vopt = qstyleoption_cast(opt)) { - sz = QCommonStyle::sizeFromContents(ct, vopt, csz, widget); - sz.setHeight(sz.height() + 2); - } - break; - - default: - sz = QCommonStyle::sizeFromContents(ct, opt, csz, widget); - } - - if (useAquaGuideline && ct != CT_PushButton) { - // TODO Probably going away at some point - QSize macsz; - if (d->aquaSizeConstrain(opt, widget, ct, sz, &macsz) != QStyleHelper::SizeDefault) { - if (macsz.width() != -1) - sz.setWidth(macsz.width()); - if (macsz.height() != -1) - sz.setHeight(macsz.height()); - } - } - - // The sizes that Carbon and the guidelines gives us excludes the focus frame. - // We compensate for this by adding some extra space here to make room for the frame when drawing: - if (const QStyleOptionComboBox *combo = qstyleoption_cast(opt)){ - if (combo->editable) { - const auto widgetSize = d->aquaSizeConstrain(opt, widget); - QMacStylePrivate::CocoaControl cw; - cw.type = combo->editable ? QMacStylePrivate::ComboBox : QMacStylePrivate::Button_PopupButton; - cw.size = widgetSize; - const CGRect diffRect = QMacStylePrivate::comboboxInnerBounds(CGRectZero, cw); - sz.rwidth() -= qRound(diffRect.size.width); - sz.rheight() -= qRound(diffRect.size.height); - } - } - return sz; -} - -void QMacStyle::drawItemText(QPainter *p, const QRect &r, int flags, const QPalette &pal, - bool enabled, const QString &text, QPalette::ColorRole textRole) const -{ - if(flags & Qt::TextShowMnemonic) - flags |= Qt::TextHideMnemonic; - QCommonStyle::drawItemText(p, r, flags, pal, enabled, text, textRole); -} - -bool QMacStyle::event(QEvent *e) -{ - Q_D(QMacStyle); - if(e->type() == QEvent::FocusIn) { - QWidget *f = 0; - QWidget *focusWidget = QApplication::focusWidget(); -#if QT_CONFIG(graphicsview) - if (QGraphicsView *graphicsView = qobject_cast(focusWidget)) { - QGraphicsItem *focusItem = graphicsView->scene() ? graphicsView->scene()->focusItem() : 0; - if (focusItem && focusItem->type() == QGraphicsProxyWidget::Type) { - QGraphicsProxyWidget *proxy = static_cast(focusItem); - if (proxy->widget()) - focusWidget = proxy->widget()->focusWidget(); - } - } -#endif - - if (focusWidget && focusWidget->testAttribute(Qt::WA_MacShowFocusRect)) { -#if QT_CONFIG(spinbox) - if (const auto sb = qobject_cast(focusWidget)) - f = sb->property("_q_spinbox_lineedit").value(); - else -#endif - f = focusWidget; - } - if (f) { - if(!d->focusWidget) - d->focusWidget = new QFocusFrame(f); - d->focusWidget->setWidget(f); - } else if(d->focusWidget) { - d->focusWidget->setWidget(0); - } - } else if(e->type() == QEvent::FocusOut) { - if(d->focusWidget) - d->focusWidget->setWidget(0); - } - return false; -} - -QIcon QMacStyle::standardIcon(StandardPixmap standardIcon, const QStyleOption *opt, - const QWidget *widget) const -{ - switch (standardIcon) { - default: - return QCommonStyle::standardIcon(standardIcon, opt, widget); - case SP_ToolBarHorizontalExtensionButton: - case SP_ToolBarVerticalExtensionButton: { - QPixmap pixmap(QLatin1String(":/qt-project.org/styles/macstyle/images/toolbar-ext.png")); - if (standardIcon == SP_ToolBarVerticalExtensionButton) { - QPixmap pix2(pixmap.height(), pixmap.width()); - pix2.setDevicePixelRatio(pixmap.devicePixelRatio()); - pix2.fill(Qt::transparent); - QPainter p(&pix2); - p.translate(pix2.width(), 0); - p.rotate(90); - p.drawPixmap(0, 0, pixmap); - return pix2; - } - return pixmap; - } - } -} - -int QMacStyle::layoutSpacing(QSizePolicy::ControlType control1, - QSizePolicy::ControlType control2, - Qt::Orientation orientation, - const QStyleOption *option, - const QWidget *widget) const -{ - const int ButtonMask = QSizePolicy::ButtonBox | QSizePolicy::PushButton; - const int controlSize = getControlSize(option, widget); - - if (control2 == QSizePolicy::ButtonBox) { - /* - AHIG seems to prefer a 12-pixel margin between group - boxes and the row of buttons. The 20 pixel comes from - Builder. - */ - if (control1 & (QSizePolicy::Frame // guess - | QSizePolicy::GroupBox // (AHIG, guess, guess) - | QSizePolicy::TabWidget // guess - | ButtonMask)) { // AHIG - return_SIZE(14, 8, 8); - } else if (control1 == QSizePolicy::LineEdit) { - return_SIZE(8, 8, 8); // Interface Builder - } else { - return_SIZE(20, 7, 7); // Interface Builder - } - } - - if ((control1 | control2) & ButtonMask) { - if (control1 == QSizePolicy::LineEdit) - return_SIZE(8, 8, 8); // Interface Builder - else if (control2 == QSizePolicy::LineEdit) { - if (orientation == Qt::Vertical) - return_SIZE(20, 7, 7); // Interface Builder - else - return_SIZE(20, 8, 8); - } - return_SIZE(14, 8, 8); // Interface Builder - } - - switch (CT2(control1, control2)) { - case CT1(QSizePolicy::Label): // guess - case CT2(QSizePolicy::Label, QSizePolicy::DefaultType): // guess - case CT2(QSizePolicy::Label, QSizePolicy::CheckBox): // AHIG - case CT2(QSizePolicy::Label, QSizePolicy::ComboBox): // AHIG - case CT2(QSizePolicy::Label, QSizePolicy::LineEdit): // guess - case CT2(QSizePolicy::Label, QSizePolicy::RadioButton): // AHIG - case CT2(QSizePolicy::Label, QSizePolicy::Slider): // guess - case CT2(QSizePolicy::Label, QSizePolicy::SpinBox): // guess - case CT2(QSizePolicy::Label, QSizePolicy::ToolButton): // guess - return_SIZE(8, 6, 5); - case CT1(QSizePolicy::ToolButton): - return 8; // AHIG - case CT1(QSizePolicy::CheckBox): - case CT2(QSizePolicy::CheckBox, QSizePolicy::RadioButton): - case CT2(QSizePolicy::RadioButton, QSizePolicy::CheckBox): - if (orientation == Qt::Vertical) - return_SIZE(8, 8, 7); // AHIG and Builder - break; - case CT1(QSizePolicy::RadioButton): - if (orientation == Qt::Vertical) - return 5; // (Builder, guess, AHIG) - } - - if (orientation == Qt::Horizontal - && (control2 & (QSizePolicy::CheckBox | QSizePolicy::RadioButton))) - return_SIZE(12, 10, 8); // guess - - if ((control1 | control2) & (QSizePolicy::Frame - | QSizePolicy::GroupBox - | QSizePolicy::TabWidget)) { - /* - These values were chosen so that nested container widgets - look good side by side. Builder uses 8, which looks way - too small, and AHIG doesn't say anything. - */ - return_SIZE(16, 10, 10); // guess - } - - if ((control1 | control2) & (QSizePolicy::Line | QSizePolicy::Slider)) - return_SIZE(12, 10, 8); // AHIG - - if ((control1 | control2) & QSizePolicy::LineEdit) - return_SIZE(10, 8, 8); // AHIG - - /* - AHIG and Builder differ by up to 4 pixels for stacked editable - comboboxes. We use some values that work fairly well in all - cases. - */ - if ((control1 | control2) & QSizePolicy::ComboBox) - return_SIZE(10, 8, 7); // guess - - /* - Builder defaults to 8, 6, 5 in lots of cases, but most of the time the - result looks too cramped. - */ - return_SIZE(10, 8, 6); // guess -} - -QT_END_NAMESPACE