diff --git a/Modern.Forms.Mac.Backend.csproj b/Modern.Forms.Mac.Backend.csproj index d92b7d5..a7998da 100644 --- a/Modern.Forms.Mac.Backend.csproj +++ b/Modern.Forms.Mac.Backend.csproj @@ -6,12 +6,12 @@ /usr/local/bin/castxml $(MSBuildThisFileDirectory)/Generated true - 0.0.5 + 0.0.6-preview01 Jonathan Pobst Native interop bits used by Modern.Forms on OSX. Modern.Forms.Mac.Backend true - 0.0.5 + 0.0.6 MIT diff --git a/inc/.gitignore b/inc/.gitignore new file mode 100644 index 0000000..e7aa7fc --- /dev/null +++ b/inc/.gitignore @@ -0,0 +1 @@ +avalonia-native.h diff --git a/inc/avalonia-native-guids.h b/inc/avalonia-native-guids.h index 439008f..64fec72 100644 --- a/inc/avalonia-native-guids.h +++ b/inc/avalonia-native-guids.h @@ -1,5 +1,2 @@ -// Copyright (c) The Avalonia Project. All rights reserved. -// Licensed under the MIT license. See licence.md file in the project root for full license information. - #define COM_GUIDS_MATERIALIZE #include "avalonia-native.h" diff --git a/inc/avalonia-native.h b/inc/avalonia-native.h deleted file mode 100644 index 677fa2f..0000000 --- a/inc/avalonia-native.h +++ /dev/null @@ -1,415 +0,0 @@ -// Copyright (c) The Avalonia Project. All rights reserved. -// Licensed under the MIT license. See licence.md file in the project root for full license information. - -#include "com.h" -#include "key.h" - -#define AVNCOM(name, id) COMINTERFACE(name, 2e2cda0a, 9ae5, 4f1b, 8e, 20, 08, 1a, 04, 27, 9f, id) - -struct IAvnWindowEvents; -struct IAvnWindow; -struct IAvnPopup; -struct IAvnMacOptions; -struct IAvnPlatformThreadingInterface; -struct IAvnSystemDialogEvents; -struct IAvnSystemDialogs; -struct IAvnScreens; -struct IAvnClipboard; -struct IAvnCursor; -struct IAvnCursorFactory; -struct IAvnGlFeature; -struct IAvnGlContext; -struct IAvnGlDisplay; -struct IAvnGlSurfaceRenderTarget; -struct IAvnGlSurfaceRenderingSession; -struct IAvnAppMenu; -struct IAvnAppMenuItem; - -struct AvnSize -{ - double Width, Height; -}; - -struct AvnPixelSize -{ - int Width, Height; -}; - -struct AvnRect -{ - double X, Y, Width, Height; -}; - -struct AvnVector -{ - double X, Y; -}; - -struct AvnPoint -{ - double X, Y; -}; - -struct AvnScreen -{ - AvnRect Bounds; - AvnRect WorkingArea; - float PixelDensity; - bool Primary; -}; - -enum AvnPixelFormat -{ - kAvnRgb565, - kAvnRgba8888, - kAvnBgra8888 -}; - -struct AvnFramebuffer -{ - void* Data; - int Width; - int Height; - int Stride; - AvnVector Dpi; - AvnPixelFormat PixelFormat; -}; - -struct AvnColor -{ - unsigned char Alpha; - unsigned char Red; - unsigned char Green; - unsigned char Blue; -}; - -enum AvnRawMouseEventType -{ - LeaveWindow, - LeftButtonDown, - LeftButtonUp, - RightButtonDown, - RightButtonUp, - MiddleButtonDown, - MiddleButtonUp, - XButton1Down, - XButton1Up, - XButton2Down, - XButton2Up, - Move, - Wheel, - NonClientLeftButtonDown, - TouchBegin, - TouchUpdate, - TouchEnd, - TouchCancel -}; - -enum AvnRawKeyEventType -{ - KeyDown, - KeyUp -}; - -enum AvnInputModifiers -{ - AvnInputModifiersNone = 0, - Alt = 1, - Control = 2, - Shift = 4, - Windows = 8, - LeftMouseButton = 16, - RightMouseButton = 32, - MiddleMouseButton = 64, - XButton1MouseButton = 128, - XButton2MouseButton = 256 -}; - -enum AvnWindowState -{ - Normal, - Minimized, - Maximized, -}; - -enum AvnStandardCursorType -{ - CursorArrow, - CursorIbeam, - CursorWait, - CursorCross, - CursorUpArrow, - CursorSizeWestEast, - CursorSizeNorthSouth, - CursorSizeAll, - CursorNo, - CursorHand, - CursorAppStarting, - CursorHelp, - CursorTopSide, - CursorBottomSize, - CursorLeftSide, - CursorRightSide, - CursorTopLeftCorner, - CursorTopRightCorner, - CursorBottomLeftCorner, - CursorBottomRightCorner, - CursorDragMove, - CursorDragCopy, - CursorDragLink, - CursorNone -}; - -enum AvnWindowEdge -{ - WindowEdgeNorthWest, - WindowEdgeNorth, - WindowEdgeNorthEast, - WindowEdgeWest, - WindowEdgeEast, - WindowEdgeSouthWest, - WindowEdgeSouth, - WindowEdgeSouthEast -}; - -AVNCOM(IAvaloniaNativeFactory, 01) : IUnknown -{ -public: - virtual HRESULT Initialize() = 0; - virtual IAvnMacOptions* GetMacOptions() = 0; - virtual HRESULT CreateWindow(IAvnWindowEvents* cb, IAvnWindow** ppv) = 0; - virtual HRESULT CreatePopup (IAvnWindowEvents* cb, IAvnPopup** ppv) = 0; - virtual HRESULT CreatePlatformThreadingInterface(IAvnPlatformThreadingInterface** ppv) = 0; - virtual HRESULT CreateSystemDialogs (IAvnSystemDialogs** ppv) = 0; - virtual HRESULT CreateScreens (IAvnScreens** ppv) = 0; - virtual HRESULT CreateClipboard(IAvnClipboard** ppv) = 0; - virtual HRESULT CreateCursorFactory(IAvnCursorFactory** ppv) = 0; - virtual HRESULT ObtainGlFeature(IAvnGlFeature** ppv) = 0; - virtual HRESULT ObtainAppMenu(IAvnAppMenu** retOut) = 0; - virtual HRESULT SetAppMenu(IAvnAppMenu* menu) = 0; - virtual HRESULT CreateMenu (IAvnAppMenu** ppv) = 0; - virtual HRESULT CreateMenuItem (IAvnAppMenuItem** ppv) = 0; - virtual HRESULT CreateMenuItemSeperator (IAvnAppMenuItem** ppv) = 0; -}; - -AVNCOM(IAvnString, 17) : IUnknown -{ - virtual HRESULT Pointer(void**retOut) = 0; - virtual HRESULT Length(int*ret) = 0; -}; - -AVNCOM(IAvnWindowBase, 02) : IUnknown -{ - virtual HRESULT Show() = 0; - virtual HRESULT Hide () = 0; - virtual HRESULT Close() = 0; - virtual HRESULT Activate () = 0; - virtual HRESULT GetClientSize(AvnSize*ret) = 0; - virtual HRESULT GetScaling(double*ret)=0; - virtual HRESULT SetMinMaxSize(AvnSize minSize, AvnSize maxSize) = 0; - virtual HRESULT Resize(double width, double height) = 0; - virtual HRESULT Invalidate (AvnRect rect) = 0; - virtual HRESULT BeginMoveDrag () = 0; - virtual HRESULT BeginResizeDrag (AvnWindowEdge edge) = 0; - virtual HRESULT GetPosition (AvnPoint*ret) = 0; - virtual HRESULT SetPosition (AvnPoint point) = 0; - virtual HRESULT PointToClient (AvnPoint point, AvnPoint*ret) = 0; - virtual HRESULT PointToScreen (AvnPoint point, AvnPoint*ret) = 0; - virtual HRESULT ThreadSafeSetSwRenderedFrame(AvnFramebuffer* fb, IUnknown* dispose) = 0; - virtual HRESULT SetTopMost (bool value) = 0; - virtual HRESULT SetCursor(IAvnCursor* cursor) = 0; - virtual HRESULT CreateGlRenderTarget(IAvnGlSurfaceRenderTarget** ret) = 0; - virtual HRESULT GetSoftwareFramebuffer(AvnFramebuffer*ret) = 0; - virtual HRESULT SetMainMenu(IAvnAppMenu* menu) = 0; - virtual HRESULT ObtainMainMenu(IAvnAppMenu** retOut) = 0; - virtual HRESULT ObtainNSWindowHandle(void** retOut) = 0; - virtual HRESULT ObtainNSWindowHandleRetained(void** retOut) = 0; - virtual HRESULT ObtainNSViewHandle(void** retOut) = 0; - virtual HRESULT ObtainNSViewHandleRetained(void** retOut) = 0; - virtual bool TryLock() = 0; - virtual void Unlock() = 0; -}; - -AVNCOM(IAvnPopup, 03) : virtual IAvnWindowBase -{ - -}; - -AVNCOM(IAvnWindow, 04) : virtual IAvnWindowBase -{ - virtual HRESULT ShowDialog (IAvnWindow* parent) = 0; - virtual HRESULT SetCanResize(bool value) = 0; - virtual HRESULT SetHasDecorations(bool value) = 0; - virtual HRESULT SetTitle (void* utf8Title) = 0; - virtual HRESULT SetTitleBarColor (AvnColor color) = 0; - virtual HRESULT SetWindowState(AvnWindowState state) = 0; - virtual HRESULT GetWindowState(AvnWindowState*ret) = 0; -}; - -AVNCOM(IAvnWindowBaseEvents, 05) : IUnknown -{ - virtual HRESULT Paint() = 0; - virtual void Closed() = 0; - virtual void Activated() = 0; - virtual void Deactivated() = 0; - virtual void Resized(const AvnSize& size) = 0; - virtual void PositionChanged (AvnPoint position) = 0; - virtual void RawMouseEvent (AvnRawMouseEventType type, - unsigned int timeStamp, - AvnInputModifiers modifiers, - AvnPoint point, - AvnVector delta) = 0; - virtual bool RawKeyEvent (AvnRawKeyEventType type, unsigned int timeStamp, AvnInputModifiers modifiers, unsigned int key) = 0; - virtual bool RawTextInputEvent (unsigned int timeStamp, const char* text) = 0; - virtual void ScalingChanged(double scaling) = 0; - virtual void RunRenderPriorityJobs() = 0; -}; - - -AVNCOM(IAvnWindowEvents, 06) : IAvnWindowBaseEvents -{ - /** - * Closing Event - * Called when the user presses the OS window close button. - * return true to allow the close, return false to prevent close. - */ - virtual bool Closing () = 0; - - virtual void WindowStateChanged (AvnWindowState state) = 0; -}; - -AVNCOM(IAvnMacOptions, 07) : IUnknown -{ - virtual HRESULT SetShowInDock(int show) = 0; - virtual HRESULT SetApplicationTitle (void* utf8string) = 0; -}; - -AVNCOM(IAvnActionCallback, 08) : IUnknown -{ - virtual void Run() = 0; -}; - -AVNCOM(IAvnSignaledCallback, 09) : IUnknown -{ - virtual void Signaled(int priority, bool priorityContainsMeaningfulValue) = 0; -}; - -AVNCOM(IAvnLoopCancellation, 0a) : IUnknown -{ - virtual void Cancel() = 0; -}; - -AVNCOM(IAvnPlatformThreadingInterface, 0b) : IUnknown -{ - virtual bool GetCurrentThreadIsLoopThread() = 0; - virtual void SetSignaledCallback(IAvnSignaledCallback* cb) = 0; - virtual IAvnLoopCancellation* CreateLoopCancellation() = 0; - virtual HRESULT RunLoop(IAvnLoopCancellation* cancel) = 0; - // Can't pass int* to sharpgentools for some reason - virtual void Signal(int priority) = 0; - virtual IUnknown* StartTimer(int priority, int ms, IAvnActionCallback* callback) = 0; -}; - -AVNCOM(IAvnSystemDialogEvents, 0c) : IUnknown -{ - virtual void OnCompleted (int numResults, void* ptrFirstResult) = 0; -}; - -AVNCOM(IAvnSystemDialogs, 0d) : IUnknown -{ - virtual void SelectFolderDialog (IAvnWindow* parentWindowHandle, - IAvnSystemDialogEvents* events, - const char* title, - const char* initialPath) = 0; - - virtual void OpenFileDialog (IAvnWindow* parentWindowHandle, - IAvnSystemDialogEvents* events, - bool allowMultiple, - const char* title, - const char* initialDirectory, - const char* initialFile, - const char* filters) = 0; - - virtual void SaveFileDialog (IAvnWindow* parentWindowHandle, - IAvnSystemDialogEvents* events, - const char* title, - const char* initialDirectory, - const char* initialFile, - const char* filters) = 0; -}; - -AVNCOM(IAvnScreens, 0e) : IUnknown -{ - virtual HRESULT GetScreenCount (int* ret) = 0; - virtual HRESULT GetScreen (int index, AvnScreen* ret) = 0; -}; - -AVNCOM(IAvnClipboard, 0f) : IUnknown -{ - virtual HRESULT GetText (IAvnString**ppv) = 0; - virtual HRESULT SetText (void* utf8Text) = 0; - virtual HRESULT Clear() = 0; -}; - -AVNCOM(IAvnCursor, 10) : IUnknown -{ -}; - -AVNCOM(IAvnCursorFactory, 11) : IUnknown -{ - virtual HRESULT GetCursor (AvnStandardCursorType cursorType, IAvnCursor** retOut) = 0; -}; - - -AVNCOM(IAvnGlFeature, 12) : IUnknown -{ - virtual HRESULT ObtainDisplay(IAvnGlDisplay**retOut) = 0; - virtual HRESULT ObtainImmediateContext(IAvnGlContext**retOut) = 0; -}; - -AVNCOM(IAvnGlDisplay, 13) : IUnknown -{ - virtual HRESULT GetSampleCount(int* ret) = 0; - virtual HRESULT GetStencilSize(int* ret) = 0; - virtual HRESULT ClearContext() = 0; - virtual void* GetProcAddress(char* proc) = 0; -}; - -AVNCOM(IAvnGlContext, 14) : IUnknown -{ - virtual HRESULT MakeCurrent() = 0; -}; - -AVNCOM(IAvnGlSurfaceRenderTarget, 15) : IUnknown -{ - virtual HRESULT BeginDrawing(IAvnGlSurfaceRenderingSession** ret) = 0; -}; - -AVNCOM(IAvnGlSurfaceRenderingSession, 16) : IUnknown -{ - virtual HRESULT GetPixelSize(AvnPixelSize* ret) = 0; - virtual HRESULT GetScaling(double* ret) = 0; -}; - -AVNCOM(IAvnAppMenu, 17) : IUnknown -{ - virtual HRESULT AddItem (IAvnAppMenuItem* item) = 0; - virtual HRESULT RemoveItem (IAvnAppMenuItem* item) = 0; - virtual HRESULT SetTitle (void* utf8String) = 0; - virtual HRESULT Clear () = 0; -}; - -AVNCOM(IAvnPredicateCallback, 18) : IUnknown -{ - virtual bool Evaluate() = 0; -}; - -AVNCOM(IAvnAppMenuItem, 19) : IUnknown -{ - virtual HRESULT SetSubMenu (IAvnAppMenu* menu) = 0; - virtual HRESULT SetTitle (void* utf8String) = 0; - virtual HRESULT SetGesture (void* utf8String, AvnInputModifiers modifiers) = 0; - virtual HRESULT SetAction (IAvnPredicateCallback* predicate, IAvnActionCallback* callback) = 0; -}; - -extern "C" IAvaloniaNativeFactory* CreateAvaloniaNative(); diff --git a/inc/com.h b/inc/com.h index 22fb4a1..df25151 100644 --- a/inc/com.h +++ b/inc/com.h @@ -1,6 +1,3 @@ -// Copyright (c) The Avalonia Project. All rights reserved. -// Licensed under the MIT license. See licence.md file in the project root for full license information. - #pragma clang diagnostic push #pragma ide diagnostic ignored "OCUnusedGlobalDeclarationInspection" #ifndef COM_H_INCLUDED diff --git a/inc/comimpl.h b/inc/comimpl.h index cf1aa4c..0ff64b7 100644 --- a/inc/comimpl.h +++ b/inc/comimpl.h @@ -162,6 +162,19 @@ class ComPtr return _obj; } + TInterface* getRetainedReference() + { + if(_obj == NULL) + return NULL; + _obj->AddRef(); + return _obj; + } + + TInterface** getPPV() + { + return &_obj; + } + operator TInterface*() const { return _obj; diff --git a/inc/key.h b/inc/key.h deleted file mode 100644 index cdc9658..0000000 --- a/inc/key.h +++ /dev/null @@ -1,1023 +0,0 @@ -// Copyright (c) The Avalonia Project. All rights reserved. -// Licensed under the MIT license. See licence.md file in the project root for full license information. - -#ifndef _KEY_H_ -#define _KEY_H_ - -/// -/// Defines the keys available on a keyboard. -/// -enum AvnKey -{ - /// - /// No key pressed. - /// - AvnKeyNone = 0, - - /// - /// The Cancel key. - /// - AvnKeyCancel = 1, - - /// - /// The Back key. - /// - AvnKeyBack = 2, - - /// - /// The Tab key. - /// - AvnKeyTab = 3, - - /// - /// The Linefeed key. - /// - AvnKeyLineFeed = 4, - - /// - /// The Clear key. - /// - AvnKeyClear = 5, - - /// - /// The Return key. - /// - AvnKeyReturn = 6, - - /// - /// The Enter key. - /// - AvnKeyEnter = 6, - - /// - /// The Pause key. - /// - AvnKeyPause = 7, - - /// - /// The Caps Lock key. - /// - AvnKeyCapsLock = 8, - - /// - /// The Caps Lock key. - /// - AvnKeyCapital = 8, - - /// - /// The IME Hangul mode key. - /// - AvnKeyHangulMode = 9, - - /// - /// The IME Kana mode key. - /// - AvnKeyKanaMode = 9, - - /// - /// The IME Junja mode key. - /// - AvnKeyJunjaMode = 10, - - /// - /// The IME Final mode key. - /// - AvnKeyFinalMode = 11, - - /// - /// The IME Kanji mode key. - /// - AvnKeyKanjiMode = 12, - - /// - /// The IME Hanja mode key. - /// - HanjaMode = 12, - - /// - /// The Escape key. - /// - Escape = 13, - - /// - /// The IME Convert key. - /// - ImeConvert = 14, - - /// - /// The IME NonConvert key. - /// - ImeNonConvert = 15, - - /// - /// The IME Accept key. - /// - ImeAccept = 16, - - /// - /// The IME Mode change key. - /// - ImeModeChange = 17, - - /// - /// The space bar. - /// - Space = 18, - - /// - /// The Page Up key. - /// - PageUp = 19, - - /// - /// The Page Up key. - /// - Prior = 19, - - /// - /// The Page Down key. - /// - PageDown = 20, - - /// - /// The Page Down key. - /// - Next = 20, - - /// - /// The End key. - /// - End = 21, - - /// - /// The Home key. - /// - Home = 22, - - /// - /// The Left arrow key. - /// - Left = 23, - - /// - /// The Up arrow key. - /// - Up = 24, - - /// - /// The Right arrow key. - /// - Right = 25, - - /// - /// The Down arrow key. - /// - Down = 26, - - /// - /// The Select key. - /// - Select = 27, - - /// - /// The Print key. - /// - Print = 28, - - /// - /// The Execute key. - /// - Execute = 29, - - /// - /// The Print Screen key. - /// - Snapshot = 30, - - /// - /// The Print Screen key. - /// - PrintScreen = 30, - - /// - /// The Insert key. - /// - Insert = 31, - - /// - /// The Delete key. - /// - Delete = 32, - - /// - /// The Help key. - /// - Help = 33, - - /// - /// The 0 key. - /// - D0 = 34, - - /// - /// The 1 key. - /// - D1 = 35, - - /// - /// The 2 key. - /// - D2 = 36, - - /// - /// The 3 key. - /// - D3 = 37, - - /// - /// The 4 key. - /// - D4 = 38, - - /// - /// The 5 key. - /// - D5 = 39, - - /// - /// The 6 key. - /// - D6 = 40, - - /// - /// The 7 key. - /// - D7 = 41, - - /// - /// The 8 key. - /// - D8 = 42, - - /// - /// The 9 key. - /// - D9 = 43, - - /// - /// The A key. - /// - A = 44, - - /// - /// The B key. - /// - B = 45, - - /// - /// The C key. - /// - C = 46, - - /// - /// The D key. - /// - D = 47, - - /// - /// The E key. - /// - E = 48, - - /// - /// The F key. - /// - F = 49, - - /// - /// The G key. - /// - G = 50, - - /// - /// The H key. - /// - H = 51, - - /// - /// The I key. - /// - I = 52, - - /// - /// The J key. - /// - J = 53, - - /// - /// The K key. - /// - AvnKeyK = 54, - - /// - /// The L key. - /// - L = 55, - - /// - /// The M key. - /// - M = 56, - - /// - /// The N key. - /// - N = 57, - - /// - /// The O key. - /// - O = 58, - - /// - /// The P key. - /// - P = 59, - - /// - /// The Q key. - /// - Q = 60, - - /// - /// The R key. - /// - R = 61, - - /// - /// The S key. - /// - S = 62, - - /// - /// The T key. - /// - T = 63, - - /// - /// The U key. - /// - U = 64, - - /// - /// The V key. - /// - V = 65, - - /// - /// The W key. - /// - W = 66, - - /// - /// The X key. - /// - X = 67, - - /// - /// The Y key. - /// - Y = 68, - - /// - /// The Z key. - /// - Z = 69, - - /// - /// The left Windows key. - /// - LWin = 70, - - /// - /// The right Windows key. - /// - RWin = 71, - - /// - /// The Application key. - /// - Apps = 72, - - /// - /// The Sleep key. - /// - Sleep = 73, - - /// - /// The 0 key on the numeric keypad. - /// - NumPad0 = 74, - - /// - /// The 1 key on the numeric keypad. - /// - NumPad1 = 75, - - /// - /// The 2 key on the numeric keypad. - /// - NumPad2 = 76, - - /// - /// The 3 key on the numeric keypad. - /// - NumPad3 = 77, - - /// - /// The 4 key on the numeric keypad. - /// - NumPad4 = 78, - - /// - /// The 5 key on the numeric keypad. - /// - NumPad5 = 79, - - /// - /// The 6 key on the numeric keypad. - /// - NumPad6 = 80, - - /// - /// The 7 key on the numeric keypad. - /// - NumPad7 = 81, - - /// - /// The 8 key on the numeric keypad. - /// - NumPad8 = 82, - - /// - /// The 9 key on the numeric keypad. - /// - NumPad9 = 83, - - /// - /// The Multiply key. - /// - Multiply = 84, - - /// - /// The Add key. - /// - Add = 85, - - /// - /// The Separator key. - /// - Separator = 86, - - /// - /// The Subtract key. - /// - Subtract = 87, - - /// - /// The Decimal key. - /// - Decimal = 88, - - /// - /// The Divide key. - /// - Divide = 89, - - /// - /// The F1 key. - /// - F1 = 90, - - /// - /// The F2 key. - /// - F2 = 91, - - /// - /// The F3 key. - /// - F3 = 92, - - /// - /// The F4 key. - /// - F4 = 93, - - /// - /// The F5 key. - /// - F5 = 94, - - /// - /// The F6 key. - /// - F6 = 95, - - /// - /// The F7 key. - /// - F7 = 96, - - /// - /// The F8 key. - /// - F8 = 97, - - /// - /// The F9 key. - /// - F9 = 98, - - /// - /// The F10 key. - /// - F10 = 99, - - /// - /// The F11 key. - /// - F11 = 100, - - /// - /// The F12 key. - /// - F12 = 101, - - /// - /// The F13 key. - /// - F13 = 102, - - /// - /// The F14 key. - /// - F14 = 103, - - /// - /// The F15 key. - /// - F15 = 104, - - /// - /// The F16 key. - /// - F16 = 105, - - /// - /// The F17 key. - /// - F17 = 106, - - /// - /// The F18 key. - /// - F18 = 107, - - /// - /// The F19 key. - /// - F19 = 108, - - /// - /// The F20 key. - /// - F20 = 109, - - /// - /// The F21 key. - /// - F21 = 110, - - /// - /// The F22 key. - /// - F22 = 111, - - /// - /// The F23 key. - /// - F23 = 112, - - /// - /// The F24 key. - /// - F24 = 113, - - /// - /// The Numlock key. - /// - NumLock = 114, - - /// - /// The Scroll key. - /// - Scroll = 115, - - /// - /// The left Shift key. - /// - LeftShift = 116, - - /// - /// The right Shift key. - /// - RightShift = 117, - - /// - /// The left Ctrl key. - /// - LeftCtrl = 118, - - /// - /// The right Ctrl key. - /// - RightCtrl = 119, - - /// - /// The left Alt key. - /// - LeftAlt = 120, - - /// - /// The right Alt key. - /// - RightAlt = 121, - - /// - /// The browser Back key. - /// - BrowserBack = 122, - - /// - /// The browser Forward key. - /// - BrowserForward = 123, - - /// - /// The browser Refresh key. - /// - BrowserRefresh = 124, - - /// - /// The browser Stop key. - /// - BrowserStop = 125, - - /// - /// The browser Search key. - /// - BrowserSearch = 126, - - /// - /// The browser Favorites key. - /// - BrowserFavorites = 127, - - /// - /// The browser Home key. - /// - BrowserHome = 128, - - /// - /// The Volume Mute key. - /// - VolumeMute = 129, - - /// - /// The Volume Down key. - /// - VolumeDown = 130, - - /// - /// The Volume Up key. - /// - VolumeUp = 131, - - /// - /// The media Next Track key. - /// - MediaNextTrack = 132, - - /// - /// The media Previous Track key. - /// - MediaPreviousTrack = 133, - - /// - /// The media Stop key. - /// - MediaStop = 134, - - /// - /// The media Play/Pause key. - /// - MediaPlayPause = 135, - - /// - /// The Launch Mail key. - /// - LaunchMail = 136, - - /// - /// The Select Media key. - /// - SelectMedia = 137, - - /// - /// The Launch Application 1 key. - /// - LaunchApplication1 = 138, - - /// - /// The Launch Application 2 key. - /// - LaunchApplication2 = 139, - - /// - /// The OEM Semicolon key. - /// - OemSemicolon = 140, - - /// - /// The OEM 1 key. - /// - Oem1 = 140, - - /// - /// The OEM Plus key. - /// - OemPlus = 141, - - /// - /// The OEM Comma key. - /// - OemComma = 142, - - /// - /// The OEM Minus key. - /// - OemMinus = 143, - - /// - /// The OEM Period key. - /// - OemPeriod = 144, - - /// - /// The OEM Question Mark key. - /// - OemQuestion = 145, - - /// - /// The OEM 2 key. - /// - Oem2 = 145, - - /// - /// The OEM Tilde key. - /// - OemTilde = 146, - - /// - /// The OEM 3 key. - /// - Oem3 = 146, - - /// - /// The ABNT_C1 (Brazilian) key. - /// - AbntC1 = 147, - - /// - /// The ABNT_C2 (Brazilian) key. - /// - AbntC2 = 148, - - /// - /// The OEM Open Brackets key. - /// - OemOpenBrackets = 149, - - /// - /// The OEM 4 key. - /// - Oem4 = 149, - - /// - /// The OEM Pipe key. - /// - OemPipe = 150, - - /// - /// The OEM 5 key. - /// - Oem5 = 150, - - /// - /// The OEM Close Brackets key. - /// - OemCloseBrackets = 151, - - /// - /// The OEM 6 key. - /// - Oem6 = 151, - - /// - /// The OEM Quotes key. - /// - OemQuotes = 152, - - /// - /// The OEM 7 key. - /// - Oem7 = 152, - - /// - /// The OEM 8 key. - /// - Oem8 = 153, - - /// - /// The OEM Backslash key. - /// - OemBackslash = 154, - - /// - /// The OEM 3 key. - /// - Oem102 = 154, - - /// - /// A special key masking the real key being processed by an IME. - /// - ImeProcessed = 155, - - /// - /// A special key masking the real key being processed as a system key. - /// - System = 156, - - /// - /// The OEM ATTN key. - /// - OemAttn = 157, - - /// - /// The DBE_ALPHANUMERIC key. - /// - DbeAlphanumeric = 157, - - /// - /// The OEM Finish key. - /// - OemFinish = 158, - - /// - /// The DBE_KATAKANA key. - /// - DbeKatakana = 158, - - /// - /// The DBE_HIRAGANA key. - /// - DbeHiragana = 159, - - /// - /// The OEM Copy key. - /// - OemCopy = 159, - - /// - /// The DBE_SBCSCHAR key. - /// - DbeSbcsChar = 160, - - /// - /// The OEM Auto key. - /// - OemAuto = 160, - - /// - /// The DBE_DBCSCHAR key. - /// - DbeDbcsChar = 161, - - /// - /// The OEM ENLW key. - /// - OemEnlw = 161, - - /// - /// The OEM BackTab key. - /// - OemBackTab = 162, - - /// - /// The DBE_ROMAN key. - /// - DbeRoman = 162, - - /// - /// The DBE_NOROMAN key. - /// - DbeNoRoman = 163, - - /// - /// The ATTN key. - /// - Attn = 163, - - /// - /// The CRSEL key. - /// - CrSel = 164, - - /// - /// The DBE_ENTERWORDREGISTERMODE key. - /// - DbeEnterWordRegisterMode = 164, - - /// - /// The EXSEL key. - /// - ExSel = 165, - - /// - /// The DBE_ENTERIMECONFIGMODE key. - /// - DbeEnterImeConfigureMode = 165, - - /// - /// The ERASE EOF Key. - /// - EraseEof = 166, - - /// - /// The DBE_FLUSHSTRING key. - /// - DbeFlushString = 166, - - /// - /// The Play key. - /// - Play = 167, - - /// - /// The DBE_CODEINPUT key. - /// - DbeCodeInput = 167, - - /// - /// The DBE_NOCODEINPUT key. - /// - DbeNoCodeInput = 168, - - /// - /// The Zoom key. - /// - Zoom = 168, - - /// - /// Reserved for future use. - /// - NoName = 169, - - /// - /// The DBE_DETERMINESTRING key. - /// - DbeDetermineString = 169, - - /// - /// The DBE_ENTERDLGCONVERSIONMODE key. - /// - DbeEnterDialogConversionMode = 170, - - /// - /// The PA1 key. - /// - Pa1 = 170, - - /// - /// The OEM Clear key. - /// - OemClear = 171, - - /// - /// The key is used with another key to create a single combined character. - /// - DeadCharProcessed = 172, -}; - -#endif diff --git a/inc/rendertarget.h b/inc/rendertarget.h new file mode 100644 index 0000000..2b0338d --- /dev/null +++ b/inc/rendertarget.h @@ -0,0 +1,12 @@ + +@protocol IRenderTarget +-(void) setNewLayer: (CALayer*) layer; +-(HRESULT) setSwFrame: (AvnFramebuffer*) fb; +-(void) resize: (AvnPixelSize) size withScale: (float) scale; +-(AvnPixelSize) pixelSize; +-(IAvnGlSurfaceRenderTarget*) createSurfaceRenderTarget; +@end + +@interface IOSurfaceRenderTarget : NSObject +-(IOSurfaceRenderTarget*) initWithOpenGlContext: (IAvnGlContext*) context; +@end diff --git a/src/OSX/Avalonia.Native.OSX.xcodeproj/project.pbxproj b/src/OSX/Avalonia.Native.OSX.xcodeproj/project.pbxproj index c0a4938..dba3ee6 100644 --- a/src/OSX/Avalonia.Native.OSX.xcodeproj/project.pbxproj +++ b/src/OSX/Avalonia.Native.OSX.xcodeproj/project.pbxproj @@ -8,17 +8,24 @@ /* Begin PBXBuildFile section */ 1A002B9E232135EE00021753 /* app.mm in Sources */ = {isa = PBXBuildFile; fileRef = 1A002B9D232135EE00021753 /* app.mm */; }; + 1A1852DC23E05814008F0DED /* deadlock.mm in Sources */ = {isa = PBXBuildFile; fileRef = 1A1852DB23E05814008F0DED /* deadlock.mm */; }; + 1A3E5EA823E9E83B00EDE661 /* rendertarget.mm in Sources */ = {isa = PBXBuildFile; fileRef = 1A3E5EA723E9E83B00EDE661 /* rendertarget.mm */; }; + 1A3E5EAA23E9F26C00EDE661 /* IOSurface.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 1A3E5EA923E9F26C00EDE661 /* IOSurface.framework */; }; + 1A3E5EAE23E9FB1300EDE661 /* cgl.mm in Sources */ = {isa = PBXBuildFile; fileRef = 1A3E5EAD23E9FB1300EDE661 /* cgl.mm */; }; + 1A3E5EB023E9FE8300EDE661 /* QuartzCore.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 1A3E5EAF23E9FE8300EDE661 /* QuartzCore.framework */; }; + 1A465D10246AB61600C5858B /* dnd.mm in Sources */ = {isa = PBXBuildFile; fileRef = 1A465D0F246AB61600C5858B /* dnd.mm */; }; + 1AFD334123E03C4F0042899B /* controlhost.mm in Sources */ = {isa = PBXBuildFile; fileRef = 1AFD334023E03C4F0042899B /* controlhost.mm */; }; 37155CE4233C00EB0034DCE9 /* menu.h in Headers */ = {isa = PBXBuildFile; fileRef = 37155CE3233C00EB0034DCE9 /* menu.h */; }; 37A517B32159597E00FBA241 /* Screens.mm in Sources */ = {isa = PBXBuildFile; fileRef = 37A517B22159597E00FBA241 /* Screens.mm */; }; 37C09D8821580FE4006A6758 /* SystemDialogs.mm in Sources */ = {isa = PBXBuildFile; fileRef = 37C09D8721580FE4006A6758 /* SystemDialogs.mm */; }; 37DDA9B0219330F8002E132B /* AvnString.mm in Sources */ = {isa = PBXBuildFile; fileRef = 37DDA9AF219330F8002E132B /* AvnString.mm */; }; 37E2330F21583241000CB7E2 /* KeyTransform.mm in Sources */ = {isa = PBXBuildFile; fileRef = 37E2330E21583241000CB7E2 /* KeyTransform.mm */; }; 520624B322973F4100C4DCEF /* menu.mm in Sources */ = {isa = PBXBuildFile; fileRef = 520624B222973F4100C4DCEF /* menu.mm */; }; + 522D5959258159C1006F7F7A /* Carbon.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 522D5958258159C1006F7F7A /* Carbon.framework */; }; 5B21A982216530F500CEE36E /* cursor.mm in Sources */ = {isa = PBXBuildFile; fileRef = 5B21A981216530F500CEE36E /* cursor.mm */; }; 5B8BD94F215BFEA6005ED2A7 /* clipboard.mm in Sources */ = {isa = PBXBuildFile; fileRef = 5B8BD94E215BFEA6005ED2A7 /* clipboard.mm */; }; AB00E4F72147CA920032A60A /* main.mm in Sources */ = {isa = PBXBuildFile; fileRef = AB00E4F62147CA920032A60A /* main.mm */; }; AB1E522C217613570091CD71 /* OpenGL.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = AB1E522B217613570091CD71 /* OpenGL.framework */; }; - AB573DC4217605E400D389A2 /* gl.mm in Sources */ = {isa = PBXBuildFile; fileRef = AB573DC3217605E400D389A2 /* gl.mm */; }; AB661C1E2148230F00291242 /* AppKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = AB661C1D2148230F00291242 /* AppKit.framework */; }; AB661C202148286E00291242 /* window.mm in Sources */ = {isa = PBXBuildFile; fileRef = AB661C1F2148286E00291242 /* window.mm */; }; AB8F7D6B21482D7F0057DBA5 /* platformthreading.mm in Sources */ = {isa = PBXBuildFile; fileRef = AB8F7D6A21482D7F0057DBA5 /* platformthreading.mm */; }; @@ -26,6 +33,13 @@ /* Begin PBXFileReference section */ 1A002B9D232135EE00021753 /* app.mm */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.objcpp; path = app.mm; sourceTree = ""; }; + 1A1852DB23E05814008F0DED /* deadlock.mm */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.objcpp; path = deadlock.mm; sourceTree = ""; }; + 1A3E5EA723E9E83B00EDE661 /* rendertarget.mm */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.objcpp; path = rendertarget.mm; sourceTree = ""; }; + 1A3E5EA923E9F26C00EDE661 /* IOSurface.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = IOSurface.framework; path = System/Library/Frameworks/IOSurface.framework; sourceTree = SDKROOT; }; + 1A3E5EAD23E9FB1300EDE661 /* cgl.mm */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.objcpp; path = cgl.mm; sourceTree = ""; }; + 1A3E5EAF23E9FE8300EDE661 /* QuartzCore.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = QuartzCore.framework; path = System/Library/Frameworks/QuartzCore.framework; sourceTree = SDKROOT; }; + 1A465D0F246AB61600C5858B /* dnd.mm */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.objcpp; path = dnd.mm; sourceTree = ""; }; + 1AFD334023E03C4F0042899B /* controlhost.mm */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.objcpp; path = controlhost.mm; sourceTree = ""; }; 37155CE3233C00EB0034DCE9 /* menu.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = menu.h; sourceTree = ""; }; 379860FE214DA0C000CD0246 /* KeyTransform.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = KeyTransform.h; sourceTree = ""; }; 37A4E71A2178846A00EACBCD /* headers */ = {isa = PBXFileReference; lastKnownFileType = folder; name = headers; path = ../../inc; sourceTree = ""; }; @@ -36,12 +50,12 @@ 37DDA9B121933371002E132B /* AvnString.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = AvnString.h; sourceTree = ""; }; 37E2330E21583241000CB7E2 /* KeyTransform.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = KeyTransform.mm; sourceTree = ""; }; 520624B222973F4100C4DCEF /* menu.mm */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.objcpp; path = menu.mm; sourceTree = ""; }; + 522D5958258159C1006F7F7A /* Carbon.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Carbon.framework; path = System/Library/Frameworks/Carbon.framework; sourceTree = SDKROOT; }; 5B21A981216530F500CEE36E /* cursor.mm */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.objcpp; path = cursor.mm; sourceTree = ""; }; 5B8BD94E215BFEA6005ED2A7 /* clipboard.mm */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.objcpp; path = clipboard.mm; sourceTree = ""; }; 5BF943652167AD1D009CAE35 /* cursor.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = cursor.h; sourceTree = ""; }; AB00E4F62147CA920032A60A /* main.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = main.mm; sourceTree = ""; }; AB1E522B217613570091CD71 /* OpenGL.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = OpenGL.framework; path = System/Library/Frameworks/OpenGL.framework; sourceTree = SDKROOT; }; - AB573DC3217605E400D389A2 /* gl.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = gl.mm; sourceTree = ""; }; AB661C1D2148230F00291242 /* AppKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = AppKit.framework; path = System/Library/Frameworks/AppKit.framework; sourceTree = SDKROOT; }; AB661C1F2148286E00291242 /* window.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = window.mm; sourceTree = ""; }; AB661C212148288600291242 /* common.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = common.h; sourceTree = ""; }; @@ -54,7 +68,10 @@ isa = PBXFrameworksBuildPhase; buildActionMask = 2147483647; files = ( + 1A3E5EB023E9FE8300EDE661 /* QuartzCore.framework in Frameworks */, + 1A3E5EAA23E9F26C00EDE661 /* IOSurface.framework in Frameworks */, AB1E522C217613570091CD71 /* OpenGL.framework in Frameworks */, + 522D5959258159C1006F7F7A /* Carbon.framework in Frameworks */, AB661C1E2148230F00291242 /* AppKit.framework in Frameworks */, ); runOnlyForDeploymentPostprocessing = 0; @@ -65,6 +82,9 @@ AB661C1C2148230E00291242 /* Frameworks */ = { isa = PBXGroup; children = ( + 522D5958258159C1006F7F7A /* Carbon.framework */, + 1A3E5EAF23E9FE8300EDE661 /* QuartzCore.framework */, + 1A3E5EA923E9F26C00EDE661 /* IOSurface.framework */, AB1E522B217613570091CD71 /* OpenGL.framework */, AB661C1D2148230F00291242 /* AppKit.framework */, ); @@ -74,14 +94,17 @@ AB7A61E62147C814003C5833 = { isa = PBXGroup; children = ( + 1A1852DB23E05814008F0DED /* deadlock.mm */, 1A002B9D232135EE00021753 /* app.mm */, 37DDA9B121933371002E132B /* AvnString.h */, 37DDA9AF219330F8002E132B /* AvnString.mm */, 37A4E71A2178846A00EACBCD /* headers */, - AB573DC3217605E400D389A2 /* gl.mm */, + 1A3E5EAD23E9FB1300EDE661 /* cgl.mm */, + 1AFD334023E03C4F0042899B /* controlhost.mm */, 5BF943652167AD1D009CAE35 /* cursor.h */, 5B21A981216530F500CEE36E /* cursor.mm */, 5B8BD94E215BFEA6005ED2A7 /* clipboard.mm */, + 1A465D0F246AB61600C5858B /* dnd.mm */, AB8F7D6A21482D7F0057DBA5 /* platformthreading.mm */, AB661C212148288600291242 /* common.h */, 379860FE214DA0C000CD0246 /* KeyTransform.h */, @@ -91,6 +114,7 @@ AB00E4F62147CA920032A60A /* main.mm */, 37155CE3233C00EB0034DCE9 /* menu.h */, 520624B222973F4100C4DCEF /* menu.mm */, + 1A3E5EA723E9E83B00EDE661 /* rendertarget.mm */, 37A517B22159597E00FBA241 /* Screens.mm */, 37C09D8721580FE4006A6758 /* SystemDialogs.mm */, AB7A61F02147C815003C5833 /* Products */, @@ -177,15 +201,19 @@ files = ( 1A002B9E232135EE00021753 /* app.mm in Sources */, 5B8BD94F215BFEA6005ED2A7 /* clipboard.mm in Sources */, + 1A1852DC23E05814008F0DED /* deadlock.mm in Sources */, 5B21A982216530F500CEE36E /* cursor.mm in Sources */, 37DDA9B0219330F8002E132B /* AvnString.mm in Sources */, AB8F7D6B21482D7F0057DBA5 /* platformthreading.mm in Sources */, + 1A3E5EA823E9E83B00EDE661 /* rendertarget.mm in Sources */, + 1A3E5EAE23E9FB1300EDE661 /* cgl.mm in Sources */, 37E2330F21583241000CB7E2 /* KeyTransform.mm in Sources */, 520624B322973F4100C4DCEF /* menu.mm in Sources */, 37A517B32159597E00FBA241 /* Screens.mm in Sources */, + 1AFD334123E03C4F0042899B /* controlhost.mm in Sources */, + 1A465D10246AB61600C5858B /* dnd.mm in Sources */, AB00E4F72147CA920032A60A /* main.mm in Sources */, 37C09D8821580FE4006A6758 /* SystemDialogs.mm in Sources */, - AB573DC4217605E400D389A2 /* gl.mm in Sources */, AB661C202148286E00291242 /* window.mm in Sources */, ); runOnlyForDeploymentPostprocessing = 0; diff --git a/src/OSX/Avalonia.Native.OSX.xcodeproj/xcshareddata/xcschemes/Avalonia.Native.OSX.xcscheme b/src/OSX/Avalonia.Native.OSX.xcodeproj/xcshareddata/xcschemes/Avalonia.Native.OSX.xcscheme index 1a665d3..5d20a13 100644 --- a/src/OSX/Avalonia.Native.OSX.xcodeproj/xcshareddata/xcschemes/Avalonia.Native.OSX.xcscheme +++ b/src/OSX/Avalonia.Native.OSX.xcodeproj/xcshareddata/xcschemes/Avalonia.Native.OSX.xcscheme @@ -29,8 +29,6 @@ shouldUseLaunchSchemeArgsEnv = "YES"> - - - - * array); +extern IAvnStringArray* CreateAvnStringArray(NSString* string); +extern IAvnString* CreateByteArray(void* data, int len); #endif /* AvnString_h */ diff --git a/src/OSX/AvnString.mm b/src/OSX/AvnString.mm index b62fe8a..00b748e 100644 --- a/src/OSX/AvnString.mm +++ b/src/OSX/AvnString.mm @@ -7,6 +7,7 @@ // #include "common.h" +#include class AvnStringImpl : public virtual ComSingleObject { @@ -28,6 +29,13 @@ memcpy((void*)_cstring, (void*)cstring, _length); } + AvnStringImpl(void*ptr, int len) + { + _length = len; + _cstring = (const char*)malloc(_length); + memcpy((void*)_cstring, ptr, len); + } + virtual ~AvnStringImpl() { free((void*)_cstring); @@ -61,7 +69,60 @@ virtual HRESULT Length(int*retOut) override } }; +class AvnStringArrayImpl : public virtual ComSingleObject +{ +private: + std::vector> _list; +public: + FORWARD_IUNKNOWN() + AvnStringArrayImpl(NSArray* array) + { + for(int c = 0; c < [array count]; c++) + { + ComPtr s; + *s.getPPV() = new AvnStringImpl([array objectAtIndex:c]); + _list.push_back(s); + } + } + + AvnStringArrayImpl(NSString* string) + { + ComPtr s; + *s.getPPV() = new AvnStringImpl(string); + _list.push_back(s); + } + + virtual unsigned int GetCount() override + { + return (unsigned int)_list.size(); + } + + virtual HRESULT Get(unsigned int index, IAvnString**ppv) override + { + if(_list.size() <= index) + return E_INVALIDARG; + *ppv = _list[index].getRetainedReference(); + return S_OK; + } +}; + IAvnString* CreateAvnString(NSString* string) { return new AvnStringImpl(string); } + + +IAvnStringArray* CreateAvnStringArray(NSArray * array) +{ + return new AvnStringArrayImpl(array); +} + +IAvnStringArray* CreateAvnStringArray(NSString* string) +{ + return new AvnStringArrayImpl(string); +} + +IAvnString* CreateByteArray(void* data, int len) +{ + return new AvnStringImpl(data, len); +} diff --git a/src/OSX/KeyTransform.h b/src/OSX/KeyTransform.h index c446602..2f43457 100644 --- a/src/OSX/KeyTransform.h +++ b/src/OSX/KeyTransform.h @@ -1,12 +1,14 @@ -// Copyright (c) The Avalonia Project. All rights reserved. -// Licensed under the MIT license. See licence.md file in the project root for full license information. - #ifndef keytransform_h #define keytransform_h #include "common.h" -#include "key.h" #include extern std::map s_KeyMap; +extern std::map s_AvnKeyMap; + +extern std::map s_QwertyKeyMap; + +extern std::map s_UnicodeKeyMap; + #endif diff --git a/src/OSX/KeyTransform.mm b/src/OSX/KeyTransform.mm index 971bcf2..6b7d95b 100644 --- a/src/OSX/KeyTransform.mm +++ b/src/OSX/KeyTransform.mm @@ -1,6 +1,3 @@ -// Copyright (c) The Avalonia Project. All rights reserved. -// Licensed under the MIT license. See licence.md file in the project root for full license information. - #include "KeyTransform.h" const int kVK_ANSI_A = 0x00; @@ -123,6 +120,138 @@ //const int kVK_JIS_Eisu = 0x66; const int kVK_JIS_Kana = 0x68; +// converts from AvaloniaKeys to UnicodeSpecial keys. +std::map s_UnicodeKeyMap = +{ + { Up, NSUpArrowFunctionKey }, + { Down, NSDownArrowFunctionKey }, + { Left, NSLeftArrowFunctionKey }, + { Right, NSRightArrowFunctionKey }, + { F1, NSF1FunctionKey }, + { F2, NSF2FunctionKey }, + { F3, NSF3FunctionKey }, + { F4, NSF4FunctionKey }, + { F5, NSF5FunctionKey }, + { F6, NSF6FunctionKey }, + { F7, NSF7FunctionKey }, + { F8, NSF8FunctionKey }, + { F9, NSF9FunctionKey }, + { F10, NSF10FunctionKey }, + { F11, NSF11FunctionKey }, + { F12, NSF12FunctionKey }, + { F13, NSF13FunctionKey }, + { F14, NSF14FunctionKey }, + { F15, NSF15FunctionKey }, + { F16, NSF16FunctionKey }, + { F17, NSF17FunctionKey }, + { F18, NSF18FunctionKey }, + { F19, NSF19FunctionKey }, + { F20, NSF20FunctionKey }, + { F21, NSF21FunctionKey }, + { F22, NSF22FunctionKey }, + { F23, NSF23FunctionKey }, + { F24, NSF24FunctionKey }, + { Insert, NSInsertFunctionKey }, + { Delete, NSDeleteFunctionKey }, + { Home, NSHomeFunctionKey }, + //{ Begin, NSBeginFunctionKey }, + { End, NSEndFunctionKey }, + { PageUp, NSPageUpFunctionKey }, + { PageDown, NSPageDownFunctionKey }, + { PrintScreen, NSPrintScreenFunctionKey }, + { Scroll, NSScrollLockFunctionKey }, + //{ SysReq, NSSysReqFunctionKey }, + //{ Break, NSBreakFunctionKey }, + //{ Reset, NSResetFunctionKey }, + //{ Stop, NSStopFunctionKey }, + //{ Menu, NSMenuFunctionKey }, + //{ UserFunction, NSUserFunctionKey }, + //{ SystemFunction, NSSystemFunctionKey }, + { Print, NSPrintFunctionKey }, + //{ ClearLine, NSClearLineFunctionKey }, + //{ ClearDisplay, NSClearDisplayFunctionKey }, +}; + +// Converts from Ansi virtual keys to Qwerty Keyboard map. +std::map s_QwertyKeyMap = +{ + { 0, "a" }, + { 1, "s" }, + { 2, "d" }, + { 3, "f" }, + { 4, "h" }, + { 5, "g" }, + { 6, "z" }, + { 7, "x" }, + { 8, "c" }, + { 9, "v" }, + { 10, "ยง" }, + { 11, "b" }, + { 12, "q" }, + { 13, "w" }, + { 14, "e" }, + { 15, "r" }, + { 16, "y" }, + { 17, "t" }, + { 18, "1" }, + { 19, "2" }, + { 20, "3" }, + { 21, "4" }, + { 22, "6" }, + { 23, "5" }, + { 24, "=" }, + { 25, "9" }, + { 26, "7" }, + { 27, "-" }, + { 28, "8" }, + { 29, "0" }, + { 30, "]" }, + { 31, "o" }, + { 32, "u" }, + { 33, "[" }, + { 34, "i" }, + { 35, "p" }, + { 37, "l" }, + { 38, "j" }, + { 39, "'" }, + { 40, "k" }, + { 41, ";" }, + { 42, "\\" }, + { 43, "," }, + { 44, "/" }, + { 45, "n" }, + { 46, "m" }, + { 47, "." }, + { 49, " " }, + { 50, "`" }, + { 51, "" }, + { 52, "" }, + { 53, "" }, + { 65, "." }, + { 66, "" }, + { 67, "*" }, + { 69, "+" }, + { 70, "" }, + { 71, "" }, + { 72, "" }, + { 75, "/" }, + { 76, "" }, + { 77, "" }, + { 78, "-" }, + { 81, "=" }, + { 82, "0" }, + { 83, "1" }, + { 84, "2" }, + { 85, "3" }, + { 86, "4" }, + { 87, "5" }, + { 88, "6" }, + { 89, "7" }, + { 91, "8" }, + { 92, "9" } +}; + +// converts from ansi virtualkeys to AvnKeys. std::map s_KeyMap = { {kVK_ANSI_A, A}, @@ -240,3 +369,22 @@ {kVK_UpArrow, Up}, {kVK_JIS_Kana, AvnKeyKanaMode}, }; + +static std::map BuildAvnKeyMap () +{ + std::map result; + + for( auto it = s_KeyMap.begin(); it != s_KeyMap.end(); ++it ) + { + int key = it->first; + AvnKey value = it->second; + + result[value] = key; + } + + return result; +} + +// Converts AvnKeys to Ansi VirtualKeys +std::map s_AvnKeyMap = BuildAvnKeyMap(); + diff --git a/src/OSX/Screens.mm b/src/OSX/Screens.mm index e7f0097..10f698f 100644 --- a/src/OSX/Screens.mm +++ b/src/OSX/Screens.mm @@ -1,12 +1,11 @@ -// Copyright (c) The Avalonia Project. All rights reserved. -// Licensed under the MIT license. See licence.md file in the project root for full license information. - #include "common.h" class Screens : public ComSingleObject { public: FORWARD_IUNKNOWN() + +public: virtual HRESULT GetScreenCount (int* ret) override { @autoreleasepool @@ -28,15 +27,15 @@ virtual HRESULT GetScreen (int index, AvnScreen* ret) override auto screen = [[NSScreen screens] objectAtIndex:index]; - ret->Bounds.X = [screen frame].origin.x; - ret->Bounds.Y = [screen frame].origin.y; ret->Bounds.Height = [screen frame].size.height; ret->Bounds.Width = [screen frame].size.width; + ret->Bounds.X = [screen frame].origin.x; + ret->Bounds.Y = ConvertPointY(ToAvnPoint([screen frame].origin)).Y - ret->Bounds.Height; - ret->WorkingArea.X = [screen visibleFrame].origin.x; - ret->WorkingArea.Y = [screen visibleFrame].origin.y; ret->WorkingArea.Height = [screen visibleFrame].size.height; ret->WorkingArea.Width = [screen visibleFrame].size.width; + ret->WorkingArea.X = [screen visibleFrame].origin.x; + ret->WorkingArea.Y = ConvertPointY(ToAvnPoint([screen visibleFrame].origin)).Y - ret->WorkingArea.Height; ret->PixelDensity = [screen backingScaleFactor]; diff --git a/src/OSX/SystemDialogs.mm b/src/OSX/SystemDialogs.mm index 567dd7f..a472210 100644 --- a/src/OSX/SystemDialogs.mm +++ b/src/OSX/SystemDialogs.mm @@ -1,6 +1,3 @@ -// Copyright (c) The Avalonia Project. All rights reserved. -// Licensed under the MIT license. See licence.md file in the project root for full license information. - #include "common.h" #include "window.h" @@ -23,6 +20,7 @@ virtual void SelectFolderDialog (IAvnWindow* parentWindowHandle, if(title != nullptr) { + panel.message = [NSString stringWithUTF8String:title]; panel.title = [NSString stringWithUTF8String:title]; } @@ -97,6 +95,7 @@ virtual void OpenFileDialog (IAvnWindow* parentWindowHandle, if(title != nullptr) { + panel.message = [NSString stringWithUTF8String:title]; panel.title = [NSString stringWithUTF8String:title]; } @@ -185,6 +184,7 @@ virtual void SaveFileDialog (IAvnWindow* parentWindowHandle, if(title != nullptr) { + panel.message = [NSString stringWithUTF8String:title]; panel.title = [NSString stringWithUTF8String:title]; } diff --git a/src/OSX/app.mm b/src/OSX/app.mm index 5c50aad..814b91c 100644 --- a/src/OSX/app.mm +++ b/src/OSX/app.mm @@ -2,7 +2,8 @@ @interface AvnAppDelegate : NSObject @end -extern NSApplicationActivationPolicy AvnDesiredActivationPolicy = NSApplicationActivationPolicyRegular; +NSApplicationActivationPolicy AvnDesiredActivationPolicy = NSApplicationActivationPolicyRegular; + @implementation AvnAppDelegate - (void)applicationWillFinishLaunching:(NSNotification *)notification { @@ -14,6 +15,10 @@ - (void)applicationWillFinishLaunching:(NSNotification *)notification } [[NSApplication sharedApplication] setActivationPolicy: AvnDesiredActivationPolicy]; + + [[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"NSFullScreenMenuItemEverywhere"]; + + [[NSApplication sharedApplication] setHelpMenu: [[NSMenu new] initWithTitle:@""]]; } } @@ -24,9 +29,43 @@ - (void)applicationDidFinishLaunching:(NSNotification *)notification @end +@interface AvnApplication : NSApplication + + +@end + +@implementation AvnApplication +{ + BOOL _isHandlingSendEvent; +} + +- (void)sendEvent:(NSEvent *)event +{ + bool oldHandling = _isHandlingSendEvent; + _isHandlingSendEvent = true; + @try { + [super sendEvent: event]; + } @finally { + _isHandlingSendEvent = oldHandling; + } +} + +// This is needed for certain embedded controls +- (BOOL) isHandlingSendEvent +{ + return _isHandlingSendEvent; +} + +- (void)setHandlingSendEvent:(BOOL)handlingSendEvent +{ + _isHandlingSendEvent = handlingSendEvent; +} + +@end + extern void InitializeAvnApp() { - NSApplication* app = [NSApplication sharedApplication]; + NSApplication* app = [AvnApplication sharedApplication]; id delegate = [AvnAppDelegate new]; [app setDelegate:delegate]; } diff --git a/src/OSX/cgl.mm b/src/OSX/cgl.mm new file mode 100644 index 0000000..a9d94cd --- /dev/null +++ b/src/OSX/cgl.mm @@ -0,0 +1,166 @@ +#include "common.h" +#include + +static CGLContextObj CreateCglContext(CGLContextObj share) +{ + int attributes[] = { + kCGLPFAAccelerated, + kCGLPFAOpenGLProfile, (CGLPixelFormatAttribute)kCGLOGLPVersion_3_2_Core, + kCGLPFADepthSize, 8, + kCGLPFAStencilSize, 8, + kCGLPFAColorSize, 32, + 0 + }; + + CGLPixelFormatObj pix; + CGLError errorCode; + GLint num; // stores the number of possible pixel formats + errorCode = CGLChoosePixelFormat( (CGLPixelFormatAttribute*)attributes, &pix, &num ); + if(errorCode != 0) + return nil; + CGLContextObj ctx = nil; + errorCode = CGLCreateContext(pix, share, &ctx ); + CGLDestroyPixelFormat( pix ); + if(errorCode != 0) + return nil; + return ctx; +}; + + + +class AvnGlContext : public virtual ComSingleObject +{ + // Debug + int _usageCount = 0; +public: + CGLContextObj Context; + int SampleCount = 0, StencilBits = 0; + FORWARD_IUNKNOWN() + + class SavedGlContext : public virtual ComUnknownObject + { + CGLContextObj _savedContext; + ComPtr _parent; + public: + SavedGlContext(CGLContextObj saved, AvnGlContext* parent) + { + _savedContext = saved; + _parent = parent; + _parent->_usageCount++; + } + + ~SavedGlContext() + { + if(_parent->Context == CGLGetCurrentContext()) + CGLSetCurrentContext(_savedContext); + _parent->_usageCount--; + CGLUnlockContext(_parent->Context); + } + }; + + AvnGlContext(CGLContextObj context) + { + Context = context; + CGLPixelFormatObj fmt = CGLGetPixelFormat(context); + CGLDescribePixelFormat(fmt, 0, kCGLPFASamples, &SampleCount); + CGLDescribePixelFormat(fmt, 0, kCGLPFAStencilSize, &StencilBits); + + } + + virtual HRESULT LegacyMakeCurrent() override + { + if(CGLSetCurrentContext(Context) != 0) + return E_FAIL; + return S_OK; + } + + virtual HRESULT MakeCurrent(IUnknown** ppv) override + { + CGLContextObj saved = CGLGetCurrentContext(); + CGLLockContext(Context); + if(CGLSetCurrentContext(Context) != 0) + { + CGLUnlockContext(Context); + return E_FAIL; + } + *ppv = new SavedGlContext(saved, this); + + return S_OK; + } + + virtual int GetSampleCount() override + { + return SampleCount; + } + + virtual int GetStencilSize() override + { + return StencilBits; + } + + virtual void* GetNativeHandle() override + { + return Context; + } + + ~AvnGlContext() + { + CGLReleaseContext(Context); + } +}; + +class AvnGlDisplay : public virtual ComSingleObject +{ + void* _libgl; + +public: + FORWARD_IUNKNOWN() + + AvnGlDisplay() + { + _libgl = dlopen("/System/Library/Frameworks/OpenGL.framework/Versions/A/Libraries/libGL.dylib", RTLD_LAZY); + } + + virtual void* GetProcAddress(char* proc) override + { + return dlsym(_libgl, proc); + } + + virtual HRESULT CreateContext(IAvnGlContext* share, IAvnGlContext**ppv) override + { + CGLContextObj shareContext = nil; + if(share != nil) + { + AvnGlContext* shareCtx = dynamic_cast(share); + if(shareCtx != nil) + shareContext = shareCtx->Context; + } + CGLContextObj ctx = ::CreateCglContext(shareContext); + if(ctx == nil) + return E_FAIL; + *ppv = new AvnGlContext(ctx); + return S_OK; + } + + virtual HRESULT WrapContext(void* native, IAvnGlContext**ppv) override + { + if(native == nil) + return E_INVALIDARG; + *ppv = new AvnGlContext((CGLContextObj) native); + return S_OK; + } + + virtual void LegacyClearCurrentContext() override + { + CGLSetCurrentContext(nil); + } +}; + +static IAvnGlDisplay* GlDisplay = new AvnGlDisplay(); + + +extern IAvnGlDisplay* GetGlDisplay() +{ + return GlDisplay; +}; + diff --git a/src/OSX/clipboard.mm b/src/OSX/clipboard.mm index 6e4d3ce..303f727 100644 --- a/src/OSX/clipboard.mm +++ b/src/OSX/clipboard.mm @@ -1,21 +1,29 @@ -// Copyright (c) The Avalonia Project. All rights reserved. -// Licensed under the MIT license. See licence.md file in the project root for full license information. - #include "common.h" #include "AvnString.h" class Clipboard : public ComSingleObject { +private: + NSPasteboard* _pb; + NSPasteboardItem* _item; public: FORWARD_IUNKNOWN() - Clipboard() + Clipboard(NSPasteboard* pasteboard, NSPasteboardItem* item) { - NSPasteboard *pasteBoard = [NSPasteboard generalPasteboard]; - [pasteBoard stringForType:NSPasteboardTypeString]; + if(pasteboard == nil && item == nil) + pasteboard = [NSPasteboard generalPasteboard]; + + _pb = pasteboard; + _item = item; } - virtual HRESULT GetText (IAvnString**ppv) override + NSPasteboardItem* TryGetItem() + { + return _item; + } + + virtual HRESULT GetText (char* type, IAvnString**ppv) override { @autoreleasepool { @@ -23,39 +31,124 @@ virtual HRESULT GetText (IAvnString**ppv) override { return E_POINTER; } + NSString* typeString = [NSString stringWithUTF8String:(const char*)type]; + NSString* string = _item == nil ? [_pb stringForType:typeString] : [_item stringForType:typeString]; - *ppv = CreateAvnString([[NSPasteboard generalPasteboard] stringForType:NSPasteboardTypeString]); + *ppv = CreateAvnString(string); return S_OK; } } - virtual HRESULT SetText (void* utf8String) override + virtual HRESULT GetStrings(char* type, IAvnStringArray**ppv) override { @autoreleasepool { - NSPasteboard *pasteBoard = [NSPasteboard generalPasteboard]; - [pasteBoard clearContents]; - [pasteBoard setString:[NSString stringWithUTF8String:(const char*)utf8String] forType:NSPasteboardTypeString]; + *ppv= nil; + NSString* typeString = [NSString stringWithUTF8String:(const char*)type]; + NSObject* data = _item == nil ? [_pb propertyListForType: typeString] : [_item propertyListForType: typeString]; + if(data == nil) + return S_OK; + + if([data isKindOfClass: [NSString class]]) + { + *ppv = CreateAvnStringArray((NSString*) data); + return S_OK; + } + + NSArray* arr = (NSArray*)data; + + for(int c = 0; c < [arr count]; c++) + if(![[arr objectAtIndex:c] isKindOfClass:[NSString class]]) + return E_INVALIDARG; + + *ppv = CreateAvnStringArray(arr); + return S_OK; + } + } + + virtual HRESULT SetText (char* type, char* utf8String) override + { + Clear(); + @autoreleasepool + { + auto string = [NSString stringWithUTF8String:(const char*)utf8String]; + auto typeString = [NSString stringWithUTF8String:(const char*)type]; + if(_item == nil) + [_pb setString: string forType: typeString]; + else + [_item setString: string forType:typeString]; } return S_OK; } + + virtual HRESULT SetBytes(char* type, void* bytes, int len) override + { + auto typeString = [NSString stringWithUTF8String:(const char*)type]; + auto data = [NSData dataWithBytes:bytes length:len]; + if(_item == nil) + [_pb setData:data forType:typeString]; + else + [_item setData:data forType:typeString]; + return S_OK; + } + + virtual HRESULT GetBytes(char* type, IAvnString**ppv) override + { + *ppv = nil; + auto typeString = [NSString stringWithUTF8String:(const char*)type]; + NSData*data; + @try + { + if(_item) + data = [_item dataForType:typeString]; + else + data = [_pb dataForType:typeString]; + if(data == nil) + return E_FAIL; + } + @catch(NSException* e) + { + return E_FAIL; + } + *ppv = CreateByteArray((void*)data.bytes, (int)data.length); + return S_OK; + } + virtual HRESULT Clear() override { @autoreleasepool { - NSPasteboard *pasteBoard = [NSPasteboard generalPasteboard]; - [pasteBoard clearContents]; - [pasteBoard setString:@"" forType:NSPasteboardTypeString]; + if(_item != nil) + _item = [NSPasteboardItem new]; + else + { + [_pb clearContents]; + [_pb setString:@"" forType:NSPasteboardTypeString]; + } } return S_OK; } + + virtual HRESULT ObtainFormats(IAvnStringArray** ppv) override + { + *ppv = CreateAvnStringArray(_item == nil ? [_pb types] : [_item types]); + return S_OK; + } }; -extern IAvnClipboard* CreateClipboard() +extern IAvnClipboard* CreateClipboard(NSPasteboard* pb, NSPasteboardItem* item) +{ + return new Clipboard(pb, item); +} + +extern NSPasteboardItem* TryGetPasteboardItem(IAvnClipboard*cb) { - return new Clipboard(); + auto clipboard = dynamic_cast(cb); + if(clipboard == nil) + return nil; + return clipboard->TryGetItem(); } diff --git a/src/OSX/common.h b/src/OSX/common.h index 10534de..ea48a19 100644 --- a/src/OSX/common.h +++ b/src/OSX/common.h @@ -1,6 +1,3 @@ -// Copyright (c) The Avalonia Project. All rights reserved. -// Licensed under the MIT license. See licence.md file in the project root for full license information. - #ifndef common_h #define common_h #include "comimpl.h" @@ -11,19 +8,25 @@ #include extern IAvnPlatformThreadingInterface* CreatePlatformThreading(); -extern IAvnWindow* CreateAvnWindow(IAvnWindowEvents*events); -extern IAvnPopup* CreateAvnPopup(IAvnWindowEvents*events); +extern void FreeAvnGCHandle(void* handle); +extern IAvnWindow* CreateAvnWindow(IAvnWindowEvents*events, IAvnGlContext* gl); +extern IAvnPopup* CreateAvnPopup(IAvnWindowEvents*events, IAvnGlContext* gl); extern IAvnSystemDialogs* CreateSystemDialogs(); extern IAvnScreens* CreateScreens(); -extern IAvnClipboard* CreateClipboard(); +extern IAvnClipboard* CreateClipboard(NSPasteboard*, NSPasteboardItem*); +extern NSPasteboardItem* TryGetPasteboardItem(IAvnClipboard*); +extern NSObject* CreateDraggingSource(NSDragOperation op, IAvnDndResultCallback* cb, void* handle); +extern void* GetAvnDataObjectHandleFromDraggingInfo(NSObject* info); +extern NSString* GetAvnCustomDataType(); +extern AvnDragDropEffects ConvertDragDropEffects(NSDragOperation nsop); extern IAvnCursorFactory* CreateCursorFactory(); -extern IAvnGlFeature* GetGlFeature(); -extern IAvnGlSurfaceRenderTarget* CreateGlRenderTarget(NSWindow* window, NSView* view); -extern IAvnAppMenu* CreateAppMenu(); -extern IAvnAppMenuItem* CreateAppMenuItem(); -extern IAvnAppMenuItem* CreateAppMenuItemSeperator(); -extern void SetAppMenu (NSString* appName, IAvnAppMenu* appMenu); -extern IAvnAppMenu* GetAppMenu (); +extern IAvnGlDisplay* GetGlDisplay(); +extern IAvnMenu* CreateAppMenu(IAvnMenuEvents* events); +extern IAvnMenuItem* CreateAppMenuItem(); +extern IAvnMenuItem* CreateAppMenuItemSeperator(); +extern IAvnNativeControlHost* CreateNativeControlHost(NSView* parent); +extern void SetAppMenu (NSString* appName, IAvnMenu* appMenu); +extern IAvnMenu* GetAppMenu (); extern NSMenuItem* GetAppMenuItem (); extern void InitializeAvnApp(); @@ -31,6 +34,7 @@ extern NSApplicationActivationPolicy AvnDesiredActivationPolicy; extern NSPoint ToNSPoint (AvnPoint p); extern AvnPoint ToAvnPoint (NSPoint p); extern AvnPoint ConvertPointY (AvnPoint p); +extern CGFloat PrimaryDisplayHeight(); extern NSSize ToNSSize (AvnSize s); #ifdef DEBUG #define NSDebugLog(...) NSLog(__VA_ARGS__) @@ -52,4 +56,12 @@ template inline T* objc_cast(id from) { - (void) action; @end +class AvnInsidePotentialDeadlock +{ +public: + static bool IsInside(); + AvnInsidePotentialDeadlock(); + ~AvnInsidePotentialDeadlock(); +}; + #endif diff --git a/src/OSX/controlhost.mm b/src/OSX/controlhost.mm new file mode 100644 index 0000000..5ee2344 --- /dev/null +++ b/src/OSX/controlhost.mm @@ -0,0 +1,160 @@ +#include "common.h" + + +IAvnNativeControlHostTopLevelAttachment* CreateAttachment(); + +class AvnNativeControlHost : + public ComSingleObject +{ +public: + FORWARD_IUNKNOWN(); + NSView* View; + AvnNativeControlHost(NSView* view) + { + View = view; + } + + virtual HRESULT CreateDefaultChild(void* parent, void** retOut) override + { + NSView* view = [NSView new]; + [view setWantsLayer: true]; + + *retOut = (__bridge_retained void*)view; + return S_OK; + }; + + virtual IAvnNativeControlHostTopLevelAttachment* CreateAttachment() override + { + return ::CreateAttachment(); + }; + + virtual void DestroyDefaultChild(void* child) override + { + // ARC will release the object for us + (__bridge_transfer NSView*) child; + } +}; + +class AvnNativeControlHostTopLevelAttachment : +public ComSingleObject +{ + NSView* _holder; + NSView* _child; +public: + FORWARD_IUNKNOWN(); + + AvnNativeControlHostTopLevelAttachment() + { + _holder = [NSView new]; + [_holder setWantsLayer:true]; + } + + virtual ~AvnNativeControlHostTopLevelAttachment() + { + if(_child != nil && [_child superview] == _holder) + { + [_child removeFromSuperview]; + } + + if([_holder superview] != nil) + { + [_holder removeFromSuperview]; + } + } + + virtual void* GetParentHandle() override + { + return (__bridge void*)_holder; + }; + + virtual HRESULT InitializeWithChildHandle(void* child) override + { + if(_child != nil) + return E_FAIL; + _child = (__bridge NSView*)child; + if(_child == nil) + return E_FAIL; + [_holder addSubview:_child]; + [_child setHidden: false]; + return S_OK; + }; + + virtual HRESULT AttachTo(IAvnNativeControlHost* host) override + { + if(host == nil) + { + [_holder removeFromSuperview]; + [_holder setHidden: true]; + } + else + { + AvnNativeControlHost* chost = dynamic_cast(host); + if(chost == nil || chost->View == nil) + return E_FAIL; + [_holder setHidden:true]; + [chost->View addSubview:_holder]; + } + return S_OK; + }; + + virtual void ShowInBounds(float x, float y, float width, float height) override + { + if(_child == nil) + return; + if(AvnInsidePotentialDeadlock::IsInside()) + { + IAvnNativeControlHostTopLevelAttachment* slf = this; + slf->AddRef(); + dispatch_async(dispatch_get_main_queue(), ^{ + slf->ShowInBounds(x, y, width, height); + slf->Release(); + }); + return; + } + + NSRect childFrame = {0, 0, width, height}; + NSRect holderFrame = {x, y, width, height}; + + [_child setFrame: childFrame]; + [_holder setFrame: holderFrame]; + [_holder setHidden: false]; + if([_holder superview] != nil) + [[_holder superview] setNeedsDisplay:true]; + } + + virtual void HideWithSize(float width, float height) override + { + if(_child == nil) + return; + if(AvnInsidePotentialDeadlock::IsInside()) + { + IAvnNativeControlHostTopLevelAttachment* slf = this; + slf->AddRef(); + dispatch_async(dispatch_get_main_queue(), ^{ + slf->HideWithSize(width, height); + slf->Release(); + }); + return; + } + + NSRect frame = {0, 0, width, height}; + [_holder setHidden: true]; + [_child setFrame: frame]; + } + + virtual void ReleaseChild() override + { + [_child removeFromSuperview]; + _child = nil; + } +}; + +IAvnNativeControlHostTopLevelAttachment* CreateAttachment() +{ + return new AvnNativeControlHostTopLevelAttachment(); +} + +extern IAvnNativeControlHost* CreateNativeControlHost(NSView* parent) +{ + return new AvnNativeControlHost(parent); +} diff --git a/src/OSX/cursor.h b/src/OSX/cursor.h index cfe9195..75a9c3d 100644 --- a/src/OSX/cursor.h +++ b/src/OSX/cursor.h @@ -1,6 +1,3 @@ -// Copyright (c) The Avalonia Project. All rights reserved. -// Licensed under the MIT license. See licence.md file in the project root for full license information. - #ifndef cursor_h #define cursor_h diff --git a/src/OSX/cursor.mm b/src/OSX/cursor.mm index 799fa9e..1732d6e 100644 --- a/src/OSX/cursor.mm +++ b/src/OSX/cursor.mm @@ -1,6 +1,3 @@ -// Copyright (c) The Avalonia Project. All rights reserved. -// Licensed under the MIT license. See licence.md file in the project root for full license information. - #include "common.h" #include "cursor.h" #include @@ -65,6 +62,28 @@ virtual HRESULT GetCursor (AvnStandardCursorType cursorType, IAvnCursor** retOut return S_OK; } + + virtual HRESULT CreateCustomCursor (void* bitmapData, size_t length, AvnPixelSize hotPixel, IAvnCursor** retOut) override + { + if(bitmapData == nullptr || retOut == nullptr) + { + return E_POINTER; + } + + NSData *imageData = [NSData dataWithBytes:bitmapData length:length]; + NSImage *image = [[NSImage alloc] initWithData:imageData]; + + + NSPoint hotSpot; + hotSpot.x = hotPixel.Width; + hotSpot.y = hotPixel.Height; + + *retOut = new Cursor([[NSCursor new] initWithImage: image hotSpot: hotSpot]); + + (*retOut)->AddRef(); + + return S_OK; + } }; extern IAvnCursorFactory* CreateCursorFactory() diff --git a/src/OSX/deadlock.mm b/src/OSX/deadlock.mm new file mode 100644 index 0000000..cb1767c --- /dev/null +++ b/src/OSX/deadlock.mm @@ -0,0 +1,17 @@ +#include "common.h" + +static int Counter = 0; +AvnInsidePotentialDeadlock::AvnInsidePotentialDeadlock() +{ + Counter++; +} + +AvnInsidePotentialDeadlock::~AvnInsidePotentialDeadlock() +{ + Counter--; +} + +bool AvnInsidePotentialDeadlock::IsInside() +{ + return Counter!=0; +} diff --git a/src/OSX/dnd.mm b/src/OSX/dnd.mm new file mode 100644 index 0000000..294b8ee --- /dev/null +++ b/src/OSX/dnd.mm @@ -0,0 +1,89 @@ +#include "common.h" + +extern AvnDragDropEffects ConvertDragDropEffects(NSDragOperation nsop) +{ + int effects = 0; + if((nsop & NSDragOperationCopy) != 0) + effects |= (int)AvnDragDropEffects::Copy; + if((nsop & NSDragOperationMove) != 0) + effects |= (int)AvnDragDropEffects::Move; + if((nsop & NSDragOperationLink) != 0) + effects |= (int)AvnDragDropEffects::Link; + return (AvnDragDropEffects)effects; +}; + +extern NSString* GetAvnCustomDataType() +{ + char buffer[256]; + sprintf(buffer, "net.avaloniaui.inproc.uti.n%in", getpid()); + return [NSString stringWithUTF8String:buffer]; +} + +@interface AvnDndSource : NSObject + +@end + +@implementation AvnDndSource +{ + NSDragOperation _operation; + ComPtr _cb; + void* _sourceHandle; +}; + +- (NSDragOperation)draggingSession:(nonnull NSDraggingSession *)session sourceOperationMaskForDraggingContext:(NSDraggingContext)context +{ + return NSDragOperationCopy; +} + +- (AvnDndSource*) initWithOperation: (NSDragOperation)operation + andCallback: (IAvnDndResultCallback*) cb + andSourceHandle: (void*) handle +{ + self = [super init]; + _operation = operation; + _cb = cb; + _sourceHandle = handle; + return self; +} + +- (void)draggingSession:(NSDraggingSession *)session + endedAtPoint:(NSPoint)screenPoint + operation:(NSDragOperation)operation +{ + if(_cb != nil) + { + auto cb = _cb; + _cb = nil; + cb->OnDragAndDropComplete(ConvertDragDropEffects(operation)); + } + if(_sourceHandle != nil) + { + FreeAvnGCHandle(_sourceHandle); + _sourceHandle = nil; + } +} + +- (void*) gcHandle +{ + return _sourceHandle; +} + +@end + +extern NSObject* CreateDraggingSource(NSDragOperation op, IAvnDndResultCallback* cb, void* handle) +{ + return [[AvnDndSource alloc] initWithOperation:op andCallback:cb andSourceHandle:handle]; +}; + +extern void* GetAvnDataObjectHandleFromDraggingInfo(NSObject* info) +{ + id obj = [info draggingSource]; + if(obj == nil) + return nil; + if([obj isKindOfClass: [AvnDndSource class]]) + { + auto src = (AvnDndSource*)obj; + return [src gcHandle]; + } + return nil; +} diff --git a/src/OSX/gl.mm b/src/OSX/gl.mm deleted file mode 100644 index 083adc9..0000000 --- a/src/OSX/gl.mm +++ /dev/null @@ -1,253 +0,0 @@ -#include "common.h" -#include -#include - -template char (&ArrayCounter(T (&a)[N]))[N]; -#define ARRAY_COUNT(a) (sizeof(ArrayCounter(a))) - -NSOpenGLPixelFormat* CreateFormat() -{ - NSOpenGLPixelFormatAttribute attribs[] = - { - NSOpenGLPFADoubleBuffer, - NSOpenGLPFAColorSize, 32, - NSOpenGLPFAStencilSize, 8, - NSOpenGLPFADepthSize, 8, - 0 - }; - return [[NSOpenGLPixelFormat alloc] initWithAttributes:attribs]; -} - -class AvnGlContext : public virtual ComSingleObject -{ -public: - FORWARD_IUNKNOWN() - NSOpenGLContext* GlContext; - GLuint Framebuffer, RenderBuffer, StencilBuffer; - AvnGlContext(NSOpenGLContext* gl, bool offscreen) - { - Framebuffer = 0; - RenderBuffer = 0; - StencilBuffer = 0; - GlContext = gl; - if(offscreen) - { - [GlContext makeCurrentContext]; - - glGenFramebuffersEXT(1, &Framebuffer); - glBindFramebufferEXT(GL_FRAMEBUFFER, Framebuffer); - glGenRenderbuffersEXT(1, &RenderBuffer); - glGenRenderbuffersEXT(1, &StencilBuffer); - - glBindRenderbufferEXT(GL_RENDERBUFFER, StencilBuffer); - glFramebufferRenderbufferEXT(GL_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, GL_RENDERBUFFER, StencilBuffer); - glBindRenderbufferEXT(GL_RENDERBUFFER, RenderBuffer); - glFramebufferRenderbufferEXT(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, RenderBuffer); - } - - } - - - virtual HRESULT MakeCurrent() override - { - [GlContext makeCurrentContext];/* - glBindFramebufferEXT(GL_FRAMEBUFFER, Framebuffer); - glBindRenderbufferEXT(GL_RENDERBUFFER, RenderBuffer); - glFramebufferRenderbufferEXT(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, RenderBuffer); - glBindRenderbufferEXT(GL_RENDERBUFFER, StencilBuffer); - glFramebufferRenderbufferEXT(GL_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, GL_RENDERBUFFER, StencilBuffer);*/ - return S_OK; - } -}; - -class AvnGlDisplay : public virtual ComSingleObject -{ - int _sampleCount, _stencilSize; - void* _libgl; - -public: - FORWARD_IUNKNOWN() - - AvnGlDisplay(int sampleCount, int stencilSize) - { - _sampleCount = sampleCount; - _stencilSize = stencilSize; - _libgl = dlopen("/System/Library/Frameworks/OpenGL.framework/Versions/A/Libraries/libGL.dylib", RTLD_LAZY); - } - - virtual HRESULT GetSampleCount(int* ret) override - { - *ret = _sampleCount; - return S_OK; - } - virtual HRESULT GetStencilSize(int* ret) override - { - *ret = _stencilSize; - return S_OK; - } - - virtual HRESULT ClearContext() override - { - [NSOpenGLContext clearCurrentContext]; - return S_OK; - } - - virtual void* GetProcAddress(char* proc) override - { - return dlsym(_libgl, proc); - } -}; - - -class GlFeature : public virtual ComSingleObject -{ - IAvnGlDisplay* _display; - AvnGlContext *_immediate; - NSOpenGLContext* _shared; -public: - FORWARD_IUNKNOWN() - NSOpenGLPixelFormat* _format; - GlFeature(IAvnGlDisplay* display, AvnGlContext* immediate, NSOpenGLPixelFormat* format) - { - _display = display; - _immediate = immediate; - _format = format; - _shared = [[NSOpenGLContext alloc] initWithFormat:_format shareContext:_immediate->GlContext]; - } - - NSOpenGLContext* CreateContext() - { - return _shared; - //return [[NSOpenGLContext alloc] initWithFormat:_format shareContext:nil]; - } - - virtual HRESULT ObtainDisplay(IAvnGlDisplay**retOut) override - { - *retOut = _display; - _display->AddRef(); - return S_OK; - } - - virtual HRESULT ObtainImmediateContext(IAvnGlContext**retOut) override - { - *retOut = _immediate; - _immediate->AddRef(); - return S_OK; - } -}; - -static GlFeature* Feature; - -GlFeature* CreateGlFeature() -{ - auto format = CreateFormat(); - if(format == nil) - { - NSLog(@"Unable to choose pixel format"); - return NULL; - } - - auto immediateContext = [[NSOpenGLContext alloc] initWithFormat:format shareContext:nil]; - if(immediateContext == nil) - { - NSLog(@"Unable to create NSOpenGLContext"); - return NULL; - } - - int stencilBits = 0, sampleCount = 0; - - auto fmt = CGLGetPixelFormat([immediateContext CGLContextObj]); - CGLDescribePixelFormat(fmt, 0, kCGLPFASamples, &sampleCount); - CGLDescribePixelFormat(fmt, 0, kCGLPFAStencilSize, &stencilBits); - - auto offscreen = new AvnGlContext(immediateContext, true); - auto display = new AvnGlDisplay(sampleCount, stencilBits); - - return new GlFeature(display, offscreen, format); -} - - -static GlFeature* GetFeature() -{ - if(Feature == nil) - Feature = CreateGlFeature(); - return Feature; -} - -extern IAvnGlFeature* GetGlFeature() -{ - return GetFeature(); -} - -class AvnGlRenderingSession : public ComSingleObject -{ - NSView* _view; - NSWindow* _window; - NSOpenGLContext* _context; -public: - FORWARD_IUNKNOWN() - AvnGlRenderingSession(NSWindow*window, NSView* view, NSOpenGLContext* context) - { - _context = context; - _window = window; - _view = view; - } - - virtual HRESULT GetPixelSize(AvnPixelSize* ret) override - { - auto fsize = [_view convertSizeToBacking: [_view frame].size]; - ret->Width = (int)fsize.width; - ret->Height = (int)fsize.height; - return S_OK; - } - virtual HRESULT GetScaling(double* ret) override - { - *ret = [_window backingScaleFactor]; - return S_OK; - } - - virtual ~AvnGlRenderingSession() - { - [_context flushBuffer]; - [NSOpenGLContext clearCurrentContext]; - CGLUnlockContext([_context CGLContextObj]); - [_view unlockFocus]; - } -}; - -class AvnGlRenderTarget : public ComSingleObject -{ - NSView* _view; - NSWindow* _window; - NSOpenGLContext* _context; -public: - FORWARD_IUNKNOWN() - AvnGlRenderTarget(NSWindow* window, NSView*view) - { - _window = window; - _view = view; - _context = GetFeature()->CreateContext(); - } - - virtual HRESULT BeginDrawing(IAvnGlSurfaceRenderingSession** ret) override - { - auto f = GetFeature(); - if(f == NULL) - return E_FAIL; - if(![_view lockFocusIfCanDraw]) - return E_ABORT; - - auto gl = _context; - CGLLockContext([_context CGLContextObj]); - [gl setView: _view]; - [gl update]; - [gl makeCurrentContext]; - *ret = new AvnGlRenderingSession(_window, _view, gl); - return S_OK; - } -}; - -extern IAvnGlSurfaceRenderTarget* CreateGlRenderTarget(NSWindow* window, NSView* view) -{ - return new AvnGlRenderTarget(window, view); -} diff --git a/src/OSX/main.mm b/src/OSX/main.mm index 9418782..72f5aa0 100644 --- a/src/OSX/main.mm +++ b/src/OSX/main.mm @@ -1,6 +1,3 @@ -// Copyright (c) The Avalonia Project. All rights reserved. -// Licensed under the MIT license. See licence.md file in the project root for full license information. - //This file will contain actual IID structures #define COM_GUIDS_MATERIALIZE #include "common.h" @@ -95,12 +92,11 @@ typedef OSStatus (*LSSetApplicationInformationItemType)(int, PrivateLSASN, PrivateLSASN asn = ls_get_current_application_asn_func(); // Constant used by WebKit; what exactly it means is unknown. const int magic_session_constant = -2; - OSErr err = + ls_set_application_information_item_func(magic_session_constant, asn, ls_display_name_key, process_name, NULL /* optional out param */); - //LOG_IF(ERROR, err) << "Call to set process name failed, err " << err; } class MacOptions : public ComSingleObject @@ -108,9 +104,9 @@ typedef OSStatus (*LSSetApplicationInformationItemType)(int, PrivateLSASN, public: FORWARD_IUNKNOWN() - virtual HRESULT SetApplicationTitle(void* utf8String) override + virtual HRESULT SetApplicationTitle(char* utf8String) override { - auto appTitle = [NSString stringWithUTF8String:(const char*)utf8String]; + auto appTitle = [NSString stringWithUTF8String: utf8String]; [[NSProcessInfo processInfo] setProcessName:appTitle]; @@ -154,14 +150,15 @@ - (void) do } @end - +static ComPtr _deallocator; class AvaloniaNative : public ComSingleObject { public: FORWARD_IUNKNOWN() - virtual HRESULT Initialize() override + virtual HRESULT Initialize(IAvnGCHandleDeallocatorCallback* deallocator) override { + _deallocator = deallocator; @autoreleasepool{ [[ThreadingInitializer new] do]; } @@ -174,20 +171,20 @@ virtual HRESULT Initialize() override return (IAvnMacOptions*)new MacOptions(); } - virtual HRESULT CreateWindow(IAvnWindowEvents* cb, IAvnWindow** ppv) override + virtual HRESULT CreateWindow(IAvnWindowEvents* cb, IAvnGlContext* gl, IAvnWindow** ppv) override { if(cb == nullptr || ppv == nullptr) return E_POINTER; - *ppv = CreateAvnWindow(cb); + *ppv = CreateAvnWindow(cb, gl); return S_OK; }; - virtual HRESULT CreatePopup(IAvnWindowEvents* cb, IAvnPopup** ppv) override + virtual HRESULT CreatePopup(IAvnWindowEvents* cb, IAvnGlContext* gl, IAvnPopup** ppv) override { if(cb == nullptr || ppv == nullptr) return E_POINTER; - *ppv = CreateAvnPopup(cb); + *ppv = CreateAvnPopup(cb, gl); return S_OK; } @@ -211,7 +208,13 @@ virtual HRESULT CreateScreens (IAvnScreens** ppv) override virtual HRESULT CreateClipboard(IAvnClipboard** ppv) override { - *ppv = ::CreateClipboard (); + *ppv = ::CreateClipboard (nil, nil); + return S_OK; + } + + virtual HRESULT CreateDndClipboard(IAvnClipboard** ppv) override + { + *ppv = ::CreateClipboard (nil, [NSPasteboardItem new]); return S_OK; } @@ -221,9 +224,9 @@ virtual HRESULT CreateCursorFactory(IAvnCursorFactory** ppv) override return S_OK; } - virtual HRESULT ObtainGlFeature(IAvnGlFeature** ppv) override + virtual HRESULT ObtainGlDisplay(IAvnGlDisplay** ppv) override { - auto rv = ::GetGlFeature(); + auto rv = ::GetGlDisplay(); if(rv == NULL) return E_FAIL; rv->AddRef(); @@ -231,41 +234,29 @@ virtual HRESULT ObtainGlFeature(IAvnGlFeature** ppv) override return S_OK; } - virtual HRESULT CreateMenu (IAvnAppMenu** ppv) override + virtual HRESULT CreateMenu (IAvnMenuEvents* cb, IAvnMenu** ppv) override { - *ppv = ::CreateAppMenu(); + *ppv = ::CreateAppMenu(cb); return S_OK; } - virtual HRESULT CreateMenuItem (IAvnAppMenuItem** ppv) override + virtual HRESULT CreateMenuItem (IAvnMenuItem** ppv) override { *ppv = ::CreateAppMenuItem(); return S_OK; } - virtual HRESULT CreateMenuItemSeperator (IAvnAppMenuItem** ppv) override + virtual HRESULT CreateMenuItemSeperator (IAvnMenuItem** ppv) override { *ppv = ::CreateAppMenuItemSeperator(); return S_OK; } - virtual HRESULT SetAppMenu (IAvnAppMenu* appMenu) override + virtual HRESULT SetAppMenu (IAvnMenu* appMenu) override { ::SetAppMenu(s_appTitle, appMenu); return S_OK; } - - virtual HRESULT ObtainAppMenu(IAvnAppMenu** retOut) override - { - if(retOut == nullptr) - { - return E_POINTER; - } - - *retOut = ::GetAppMenu(); - - return S_OK; - } }; extern "C" IAvaloniaNativeFactory* CreateAvaloniaNative() @@ -273,6 +264,12 @@ virtual HRESULT ObtainAppMenu(IAvnAppMenu** retOut) override return new AvaloniaNative(); }; +extern void FreeAvnGCHandle(void* handle) +{ + if(_deallocator != nil) + _deallocator->FreeGCHandle(handle); +} + NSSize ToNSSize (AvnSize s) { NSSize result; @@ -302,10 +299,14 @@ AvnPoint ToAvnPoint (NSPoint p) AvnPoint ConvertPointY (AvnPoint p) { - auto sw = [NSScreen.screens objectAtIndex:0].frame; + auto primaryDisplayHeight = NSMaxY([[[NSScreen screens] firstObject] frame]); - auto t = MAX(sw.origin.y, sw.origin.y + sw.size.height); - p.Y = t - p.Y; + p.Y = primaryDisplayHeight - p.Y; return p; } + +CGFloat PrimaryDisplayHeight() +{ + return NSMaxY([[[NSScreen screens] firstObject] frame]); +} diff --git a/src/OSX/menu.h b/src/OSX/menu.h index befbe6a..564fdc1 100644 --- a/src/OSX/menu.h +++ b/src/OSX/menu.h @@ -14,8 +14,10 @@ class AvnAppMenuItem; class AvnAppMenu; -@interface AvnMenu : NSMenu // for some reason it doesnt detect nsmenu here but compiler doesnt complain -- (void)setMenu:(NSMenu*) menu; +@interface AvnMenu : NSMenu +- (id) initWithDelegate: (NSObject*) del; +- (void) setHasGlobalMenuItem: (bool) value; +- (bool) hasGlobalMenuItem; @end @interface AvnMenuItem : NSMenuItem @@ -23,13 +25,14 @@ class AvnAppMenu; - (void)didSelectItem:(id)sender; @end -class AvnAppMenuItem : public ComSingleObject +class AvnAppMenuItem : public ComSingleObject { private: NSMenuItem* _native; // here we hold a pointer to an AvnMenuItem IAvnActionCallback* _callback; IAvnPredicateCallback* _predicate; bool _isSeperator; + bool _isCheckable; public: FORWARD_IUNKNOWN() @@ -38,43 +41,54 @@ class AvnAppMenuItem : public ComSingleObject +class AvnAppMenu : public ComSingleObject { private: AvnMenu* _native; + ComPtr _baseEvents; public: FORWARD_IUNKNOWN() - AvnAppMenu(); - - AvnAppMenu(AvnMenu* native); - + AvnAppMenu(IAvnMenuEvents* events); + AvnMenu* GetNative(); - virtual HRESULT AddItem (IAvnAppMenuItem* item) override; + void RaiseNeedsUpdate (); + + virtual HRESULT InsertItem (int index, IAvnMenuItem* item) override; - virtual HRESULT RemoveItem (IAvnAppMenuItem* item) override; + virtual HRESULT RemoveItem (IAvnMenuItem* item) override; - virtual HRESULT SetTitle (void* utf8String) override; + virtual HRESULT SetTitle (char* utf8String) override; virtual HRESULT Clear () override; }; +@interface AvnMenuDelegate : NSObject +- (id) initWithParent: (AvnAppMenu*) parent; +@end + #endif diff --git a/src/OSX/menu.mm b/src/OSX/menu.mm index 1d2f075..198b017 100644 --- a/src/OSX/menu.mm +++ b/src/OSX/menu.mm @@ -2,8 +2,35 @@ #include "common.h" #include "menu.h" #include "window.h" +#include "KeyTransform.h" +#include +#include /* For kVK_ constants, and TIS functions. */ @implementation AvnMenu +{ + bool _isReparented; + NSObject* _wtf; +} + +- (id) initWithDelegate: (NSObject*)del +{ + self = [super init]; + self.delegate = del; + _wtf = del; + _isReparented = false; + return self; +} + +- (bool)hasGlobalMenuItem +{ + return _isReparented; +} + +- (void)setHasGlobalMenuItem:(bool)value +{ + _isReparented = value; +} + @end @implementation AvnMenuItem @@ -46,6 +73,7 @@ - (void)didSelectItem:(nullable id)sender AvnAppMenuItem::AvnAppMenuItem(bool isSeperator) { + _isCheckable = false; _isSeperator = isSeperator; if(isSeperator) @@ -65,49 +93,168 @@ - (void)didSelectItem:(nullable id)sender return _native; } -HRESULT AvnAppMenuItem::SetSubMenu (IAvnAppMenu* menu) +HRESULT AvnAppMenuItem::SetSubMenu (IAvnMenu* menu) { - auto nsMenu = dynamic_cast(menu)->GetNative(); - - [_native setSubmenu: nsMenu]; - - return S_OK; + @autoreleasepool + { + if(menu != nullptr) + { + auto nsMenu = dynamic_cast(menu)->GetNative(); + + [_native setSubmenu: nsMenu]; + } + else + { + [_native setSubmenu: nullptr]; + } + + return S_OK; + } } -HRESULT AvnAppMenuItem::SetTitle (void* utf8String) +HRESULT AvnAppMenuItem::SetTitle (char* utf8String) { - if (utf8String != nullptr) + @autoreleasepool { - [_native setTitle:[NSString stringWithUTF8String:(const char*)utf8String]]; + if (utf8String != nullptr) + { + [_native setTitle:[NSString stringWithUTF8String:(const char*)utf8String]]; + } + + return S_OK; } - - return S_OK; } -HRESULT AvnAppMenuItem::SetGesture (void* key, AvnInputModifiers modifiers) + +HRESULT AvnAppMenuItem::SetGesture (AvnKey key, AvnInputModifiers modifiers) { - NSEventModifierFlags flags = 0; - - if (modifiers & Control) - flags |= NSEventModifierFlagControl; - if (modifiers & Shift) - flags |= NSEventModifierFlagShift; - if (modifiers & Alt) - flags |= NSEventModifierFlagOption; - if (modifiers & Windows) - flags |= NSEventModifierFlagCommand; - - [_native setKeyEquivalent:[NSString stringWithUTF8String:(const char*)key]]; - [_native setKeyEquivalentModifierMask:flags]; - - return S_OK; + @autoreleasepool + { + if(key != AvnKeyNone) + { + NSEventModifierFlags flags = 0; + + if (modifiers & Control) + flags |= NSEventModifierFlagControl; + if (modifiers & Shift) + flags |= NSEventModifierFlagShift; + if (modifiers & Alt) + flags |= NSEventModifierFlagOption; + if (modifiers & Windows) + flags |= NSEventModifierFlagCommand; + + auto it = s_UnicodeKeyMap.find(key); + + if(it != s_UnicodeKeyMap.end()) + { + auto keyString= [NSString stringWithFormat:@"%C", (unsigned short)it->second]; + + [_native setKeyEquivalent: keyString]; + [_native setKeyEquivalentModifierMask:flags]; + + return S_OK; + } + else + { + auto it = s_AvnKeyMap.find(key); // check if a virtual key is mapped. + + if(it != s_AvnKeyMap.end()) + { + auto it1 = s_QwertyKeyMap.find(it->second); // convert virtual key to qwerty string. + + if(it1 != s_QwertyKeyMap.end()) + { + [_native setKeyEquivalent: [NSString stringWithUTF8String: it1->second]]; + [_native setKeyEquivalentModifierMask:flags]; + + return S_OK; + } + } + } + } + + // Nothing matched... clear. + [_native setKeyEquivalent: @""]; + [_native setKeyEquivalentModifierMask: 0]; + + return S_OK; + } } HRESULT AvnAppMenuItem::SetAction (IAvnPredicateCallback* predicate, IAvnActionCallback* callback) { - _predicate = predicate; - _callback = callback; - return S_OK; + @autoreleasepool + { + _predicate = predicate; + _callback = callback; + return S_OK; + } +} + +HRESULT AvnAppMenuItem::SetIsChecked (bool isChecked) +{ + @autoreleasepool + { + [_native setState:(isChecked && _isCheckable ? NSOnState : NSOffState)]; + return S_OK; + } +} + +HRESULT AvnAppMenuItem::SetToggleType(AvnMenuItemToggleType toggleType) +{ + @autoreleasepool + { + switch(toggleType) + { + case AvnMenuItemToggleType::None: + [_native setOnStateImage: [NSImage imageNamed:@"NSMenuCheckmark"]]; + + _isCheckable = false; + break; + + case AvnMenuItemToggleType::CheckMark: + [_native setOnStateImage: [NSImage imageNamed:@"NSMenuCheckmark"]]; + + _isCheckable = true; + break; + + case AvnMenuItemToggleType::Radio: + [_native setOnStateImage: [NSImage imageNamed:@"NSMenuItemBullet"]]; + + _isCheckable = true; + break; + } + + return S_OK; + } +} + +HRESULT AvnAppMenuItem::SetIcon(void *data, size_t length) +{ + @autoreleasepool + { + if(data != nullptr) + { + NSData *imageData = [NSData dataWithBytes:data length:length]; + NSImage *image = [[NSImage alloc] initWithData:imageData]; + + NSSize originalSize = [image size]; + + NSSize size; + size.height = [[NSFont menuFontOfSize:0] pointSize] * 1.333333; + + auto scaleFactor = size.height / originalSize.height; + size.width = originalSize.width * scaleFactor; + + [image setSize: size]; + [_native setImage:image]; + } + else + { + [_native setImage:nullptr]; + } + return S_OK; + } } bool AvnAppMenuItem::EvaluateItemEnabled() @@ -130,71 +277,123 @@ - (void)didSelectItem:(nullable id)sender } } -AvnAppMenu::AvnAppMenu() +AvnAppMenu::AvnAppMenu(IAvnMenuEvents* events) { - _native = [AvnMenu new]; + _baseEvents = events; + id del = [[AvnMenuDelegate alloc] initWithParent: this]; + _native = [[AvnMenu alloc] initWithDelegate: del]; } -AvnAppMenu::AvnAppMenu(AvnMenu* native) -{ - _native = native; -} AvnMenu* AvnAppMenu::GetNative() { return _native; } -HRESULT AvnAppMenu::AddItem (IAvnAppMenuItem* item) +void AvnAppMenu::RaiseNeedsUpdate() { - auto avnMenuItem = dynamic_cast(item); - - if(avnMenuItem != nullptr) + if(_baseEvents != nullptr) { - [_native addItem: avnMenuItem->GetNative()]; + _baseEvents->NeedsUpdate(); } - - return S_OK; } -HRESULT AvnAppMenu::RemoveItem (IAvnAppMenuItem* item) +HRESULT AvnAppMenu::InsertItem(int index, IAvnMenuItem *item) { - auto avnMenuItem = dynamic_cast(item); - - if(avnMenuItem != nullptr) + @autoreleasepool { - [_native removeItem:avnMenuItem->GetNative()]; + if([_native hasGlobalMenuItem]) + { + index++; + } + + auto avnMenuItem = dynamic_cast(item); + + if(avnMenuItem != nullptr) + { + [_native insertItem: avnMenuItem->GetNative() atIndex:index]; + } + + return S_OK; } - - return S_OK; } -HRESULT AvnAppMenu::SetTitle (void* utf8String) +HRESULT AvnAppMenu::RemoveItem (IAvnMenuItem* item) { - if (utf8String != nullptr) + @autoreleasepool { - [_native setTitle:[NSString stringWithUTF8String:(const char*)utf8String]]; + auto avnMenuItem = dynamic_cast(item); + + if(avnMenuItem != nullptr) + { + [_native removeItem:avnMenuItem->GetNative()]; + } + + return S_OK; + } +} + +HRESULT AvnAppMenu::SetTitle (char* utf8String) +{ + @autoreleasepool + { + if (utf8String != nullptr) + { + [_native setTitle:[NSString stringWithUTF8String:(const char*)utf8String]]; + } + + return S_OK; } - - return S_OK; } HRESULT AvnAppMenu::Clear() { - [_native removeAllItems]; - return S_OK; + @autoreleasepool + { + [_native removeAllItems]; + return S_OK; + } +} + +@implementation AvnMenuDelegate +{ + ComPtr _parent; } +- (id) initWithParent:(AvnAppMenu *)parent +{ + self = [super init]; + _parent = parent; + return self; +} +- (BOOL)menu:(NSMenu *)menu updateItem:(NSMenuItem *)item atIndex:(NSInteger)index shouldCancel:(BOOL)shouldCancel +{ + if(shouldCancel) + return NO; + return YES; +} + +- (NSInteger)numberOfItemsInMenu:(NSMenu *)menu +{ + return [menu numberOfItems]; +} + +- (void)menuNeedsUpdate:(NSMenu *)menu +{ + _parent->RaiseNeedsUpdate(); +} + + +@end -extern IAvnAppMenu* CreateAppMenu() +extern IAvnMenu* CreateAppMenu(IAvnMenuEvents* cb) { @autoreleasepool { - id menuBar = [NSMenu new]; - return new AvnAppMenu(menuBar); + return new AvnAppMenu(cb); } } -extern IAvnAppMenuItem* CreateAppMenuItem() +extern IAvnMenuItem* CreateAppMenuItem() { @autoreleasepool { @@ -202,7 +401,7 @@ - (void)didSelectItem:(nullable id)sender } } -extern IAvnAppMenuItem* CreateAppMenuItemSeperator() +extern IAvnMenuItem* CreateAppMenuItemSeperator() { @autoreleasepool { @@ -210,10 +409,10 @@ - (void)didSelectItem:(nullable id)sender } } -static IAvnAppMenu* s_appMenu = nullptr; +static IAvnMenu* s_appMenu = nullptr; static NSMenuItem* s_appMenuItem = nullptr; -extern void SetAppMenu (NSString* appName, IAvnAppMenu* menu) +extern void SetAppMenu (NSString* appName, IAvnMenu* menu) { s_appMenu = menu; @@ -294,7 +493,7 @@ extern void SetAppMenu (NSString* appName, IAvnAppMenu* menu) } } -extern IAvnAppMenu* GetAppMenu () +extern IAvnMenu* GetAppMenu () { return s_appMenu; } diff --git a/src/OSX/platformthreading.mm b/src/OSX/platformthreading.mm index e7abeda..e83bf53 100644 --- a/src/OSX/platformthreading.mm +++ b/src/OSX/platformthreading.mm @@ -1,6 +1,3 @@ -// Copyright (c) The Avalonia Project. All rights reserved. -// Licensed under the MIT license. See licence.md file in the project root for full license information. - #include "common.h" class PlatformThreadingInterface; @@ -57,9 +54,11 @@ - (void) action { public: FORWARD_IUNKNOWN() + bool Running = false; bool Cancelled = false; - virtual void Cancel() + + virtual void Cancel() override { Cancelled = true; if(Running) @@ -102,7 +101,7 @@ virtual void Cancel() virtual bool GetCurrentThreadIsLoopThread() override { - return [[NSThread currentThread] isMainThread]; + return [NSThread isMainThread]; } virtual void SetSignaledCallback(IAvnSignaledCallback* cb) override { @@ -157,11 +156,14 @@ -(Signaler*) init -(void) perform { + ComPtr cb; @synchronized (self) { _signaled = false; - if(_parent != NULL && _parent->SignaledCallback != NULL) - _parent->SignaledCallback->Signaled(0, false); + if(_parent != NULL) + cb = _parent->SignaledCallback; } + if(cb != nullptr) + cb->Signaled(0, false); } -(void) setParent:(PlatformThreadingInterface *)parent diff --git a/src/OSX/rendertarget.mm b/src/OSX/rendertarget.mm new file mode 100644 index 0000000..b2d4341 --- /dev/null +++ b/src/OSX/rendertarget.mm @@ -0,0 +1,302 @@ +#include "common.h" +#include "rendertarget.h" +#import +#import +#import + +#include +#include +#include +#include +#include + +@interface IOSurfaceHolder : NSObject +@end + +@implementation IOSurfaceHolder +{ + @public IOSurfaceRef surface; + @public AvnPixelSize size; + @public float scale; + ComPtr _context; + GLuint _framebuffer, _texture, _renderbuffer; +} + +- (IOSurfaceHolder*) initWithSize: (AvnPixelSize) size + withScale: (float)scale + withOpenGlContext: (IAvnGlContext*) context +{ + long bytesPerRow = IOSurfaceAlignProperty(kIOSurfaceBytesPerRow, size.Width * 4); + long allocSize = IOSurfaceAlignProperty(kIOSurfaceAllocSize, size.Height * bytesPerRow); + NSDictionary* options = @{ + (id)kIOSurfaceWidth: @(size.Width), + (id)kIOSurfaceHeight: @(size.Height), + (id)kIOSurfacePixelFormat: @((uint)'BGRA'), + (id)kIOSurfaceBytesPerElement: @(4), + (id)kIOSurfaceBytesPerRow: @(bytesPerRow), + (id)kIOSurfaceAllocSize: @(allocSize), + + //(id)kIOSurfaceCacheMode: @(kIOMapWriteCombineCache), + (id)kIOSurfaceElementWidth: @(1), + (id)kIOSurfaceElementHeight: @(1) + }; + + surface = IOSurfaceCreate((CFDictionaryRef)options); + self->scale = scale; + self->size = size; + self->_context = context; + return self; +} + +-(HRESULT) prepareForGlRender +{ + if(_context == nil) + return E_FAIL; + if(CGLGetCurrentContext() != _context->GetNativeHandle()) + return E_FAIL; + if(_framebuffer == 0) + glGenFramebuffersEXT(1, &_framebuffer); + + + glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, _framebuffer); + if(_texture == 0) + { + glGenTextures(1, &_texture); + + glBindTexture(GL_TEXTURE_RECTANGLE_EXT, _texture); + CGLError res = CGLTexImageIOSurface2D((CGLContextObj)_context->GetNativeHandle(), + GL_TEXTURE_RECTANGLE_EXT, GL_RGBA8, + size.Width, size.Height, GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV, surface, 0); + glBindTexture(GL_TEXTURE_RECTANGLE_EXT, 0); + + if(res != 0) + { + glDeleteTextures(1, &_texture); + _texture = 0; + glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0); + return E_FAIL; + } + glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_RECTANGLE_EXT, _texture, 0); + } + + if(_renderbuffer == 0) + { + glGenRenderbuffers(1, &_renderbuffer); + glBindRenderbuffer(GL_RENDERBUFFER, _renderbuffer); + glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT, size.Width, size.Height); + glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, _renderbuffer); + } + + return S_OK; +} + +-(void) finishDraw +{ + ComPtr release; + _context->MakeCurrent(release.getPPV()); + glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0); + glFlush(); +} + +-(void) dealloc +{ + + if(_framebuffer != 0) + { + ComPtr release; + _context->MakeCurrent(release.getPPV()); + glDeleteFramebuffers(1, &_framebuffer); + if(_texture != 0) + glDeleteTextures(1, &_texture); + if(_renderbuffer != 0) + glDeleteRenderbuffers(1, &_renderbuffer); + } + + if(surface != nullptr) + { + CFRelease(surface); + } +} +@end + +static IAvnGlSurfaceRenderTarget* CreateGlRenderTarget(IOSurfaceRenderTarget* target); + +@implementation IOSurfaceRenderTarget +{ + CALayer* _layer; + @public IOSurfaceHolder* surface; + @public NSObject* lock; + ComPtr _glContext; +} + +- (IOSurfaceRenderTarget*) initWithOpenGlContext: (IAvnGlContext*) context; +{ + self = [super init]; + _glContext = context; + lock = [NSObject new]; + surface = nil; + [self resize:{1,1} withScale: 1]; + + return self; +} + +- (AvnPixelSize) pixelSize { + return {1, 1}; +} + +- (CALayer *)layer { + return _layer; +} + +- (void)resize:(AvnPixelSize)size withScale: (float) scale{ + + if(size.Height <= 0) + size.Height = 1; + if(size.Width <= 0) + size.Width = 1; + + @synchronized (lock) { + if(surface == nil + || surface->size.Width != size.Width + || surface->size.Height != size.Height + || surface->scale != scale) + { + surface = [[IOSurfaceHolder alloc] initWithSize:size withScale:scale withOpenGlContext:_glContext.getRaw()]; + + [self updateLayer]; + } + } +} + +- (void)updateLayer { + if ([NSThread isMainThread]) + { + @synchronized (lock) { + if(_layer == nil) + return; + [CATransaction begin]; + [_layer setContents: nil]; + if(surface != nil) + { + [_layer setContentsScale: surface->scale]; + [_layer setContents: (__bridge IOSurface*) surface->surface]; + } + [CATransaction commit]; + [CATransaction flush]; + } + } + else + dispatch_async(dispatch_get_main_queue(), ^{ + [self updateLayer]; + }); +} + +- (void) setNewLayer:(CALayer *)layer { + _layer = layer; + [self updateLayer]; +} + +- (HRESULT)setSwFrame:(AvnFramebuffer *)fb { + @synchronized (lock) { + if(fb->PixelFormat == AvnPixelFormat::kAvnRgb565) + return E_INVALIDARG; + if(surface == nil) + return E_FAIL; + IOSurfaceRef surf = surface->surface; + if(IOSurfaceLock(surf, 0, nil)) + return E_FAIL; + size_t w = MIN(fb->Width, IOSurfaceGetWidth(surf)); + size_t h = MIN(fb->Height, IOSurfaceGetHeight(surf)); + size_t wbytes = w*4; + size_t sstride = IOSurfaceGetBytesPerRow(surf); + size_t fstride = fb->Stride; + char*pSurface = (char*)IOSurfaceGetBaseAddress(surf); + char*pFb = (char*)fb->Data; + for(size_t y = 0; y < h; y++) + { + memcpy(pSurface + y*sstride, pFb + y*fstride, wbytes); + } + IOSurfaceUnlock(surf, 0, nil); + [self updateLayer]; + return S_OK; + } +} + +-(IAvnGlSurfaceRenderTarget*) createSurfaceRenderTarget +{ + return CreateGlRenderTarget(self); +} + +@end + +class AvnGlRenderingSession : public ComSingleObject +{ + ComPtr _releaseContext; + IOSurfaceRenderTarget* _target; + IOSurfaceHolder* _surface; +public: + FORWARD_IUNKNOWN() + AvnGlRenderingSession(IOSurfaceRenderTarget* target, ComPtr releaseContext) + { + _target = target; + // This happens in a synchronized block set up by AvnRenderTarget, so we take the current surface for this + // particular render session + _surface = _target->surface; + _releaseContext = releaseContext; + } + + virtual HRESULT GetPixelSize(AvnPixelSize* ret) override + { + if(!_surface) + return E_FAIL; + *ret = _surface->size; + return S_OK; + } + + virtual HRESULT GetScaling(double* ret) override + { + if(!_surface) + return E_FAIL; + *ret = _surface->scale; + return S_OK; + } + + virtual ~AvnGlRenderingSession() + { + [_surface finishDraw]; + [_target updateLayer]; + _releaseContext = nil; + } +}; + +class AvnGlRenderTarget : public ComSingleObject +{ + IOSurfaceRenderTarget* _target; +public: + FORWARD_IUNKNOWN() + AvnGlRenderTarget(IOSurfaceRenderTarget* target) + { + _target = target; + } + + virtual HRESULT BeginDrawing(IAvnGlSurfaceRenderingSession** ret) override + { + ComPtr releaseContext; + @synchronized (_target->lock) { + if(_target->surface == nil) + return E_FAIL; + _target->_glContext->MakeCurrent(releaseContext.getPPV()); + HRESULT res = [_target->surface prepareForGlRender]; + if(res) + return res; + *ret = new AvnGlRenderingSession(_target, releaseContext); + return S_OK; + } + } +}; + + +static IAvnGlSurfaceRenderTarget* CreateGlRenderTarget(IOSurfaceRenderTarget* target) +{ + return new AvnGlRenderTarget(target); +} diff --git a/src/OSX/window.h b/src/OSX/window.h index 932bc56..b1f64bc 100644 --- a/src/OSX/window.h +++ b/src/OSX/window.h @@ -1,17 +1,23 @@ -// Copyright (c) The Avalonia Project. All rights reserved. -// Licensed under the MIT license. See licence.md file in the project root for full license information. - #ifndef window_h #define window_h class WindowBaseImpl; -@interface AvnView : NSView +@interface AvnView : NSView -(AvnView* _Nonnull) initWithParent: (WindowBaseImpl* _Nonnull) parent; -(NSEvent* _Nonnull) lastMouseDownEvent; -(AvnPoint) translateLocalPoint:(AvnPoint)pt; -(void) setSwRenderedFrame: (AvnFramebuffer* _Nonnull) fb dispose: (IUnknown* _Nonnull) dispose; -(void) onClosed; +-(AvnPixelSize) getPixelSize; +@end + +@interface AutoFitContentView : NSView +-(AutoFitContentView* _Nonnull) initWithContent: (NSView* _Nonnull) content; +-(void) ShowTitleBar: (bool) show; +-(void) SetTitleBarHeightHint: (double) height; +-(void) SetContent: (NSView* _Nonnull) content; +-(void) ShowBlur: (bool) show; @end @interface AvnWindow : NSWindow @@ -21,7 +27,13 @@ class WindowBaseImpl; -(void) pollModalSession: (NSModalSession _Nonnull) session; -(void) restoreParentWindow; -(bool) shouldTryToHandleEvents; --(void) applyMenu:(NSMenu *)menu; +-(void) setEnabled: (bool) enable; +-(void) showAppMenuOnly; +-(void) showWindowMenuWithAppMenu; +-(void) applyMenu:(NSMenu* _Nullable)menu; +-(double) getScaling; +-(double) getExtendedTitleBarHeight; +-(void) setIsExtended:(bool)value; @end struct INSWindowHolder @@ -32,6 +44,10 @@ struct INSWindowHolder struct IWindowStateChanged { virtual void WindowStateChanged () = 0; + virtual void StartStateTransition () = 0; + virtual void EndStateTransition () = 0; + virtual SystemDecorations Decorations () = 0; + virtual AvnWindowState WindowState () = 0; }; #endif /* window_h */ diff --git a/src/OSX/window.mm b/src/OSX/window.mm index c54829d..9d49025 100644 --- a/src/OSX/window.mm +++ b/src/OSX/window.mm @@ -1,50 +1,10 @@ -// Copyright (c) The Avalonia Project. All rights reserved. -// Licensed under the MIT license. See licence.md file in the project root for full license information. - #include "common.h" #include "window.h" #include "KeyTransform.h" #include "cursor.h" #include "menu.h" #include - -class SoftwareDrawingOperation -{ -public: - void* Data = 0; - AvnFramebuffer Desc; - void Alloc(NSView* view) - { - auto logicalSize = [view frame].size; - auto pixelSize = [view convertSizeToBacking:logicalSize]; - int w = pixelSize.width; - int h = pixelSize.height; - int stride = w * 4; - Data = malloc(h * stride); - Desc = { - .Data = Data, - .Stride = stride, - .Width = w, - .Height = h, - .PixelFormat = kAvnRgba8888, - .Dpi = AvnVector { .X = w / logicalSize.width * 96, .Y = h / logicalSize.height * 96} - }; - } - - void Dealloc() - { - if(Data != NULL) - { - free(Data); - Data = NULL; - } - } - - ~SoftwareDrawingOperation() - { - Dealloc(); - } -}; +#include "rendertarget.h" class WindowBaseImpl : public virtual ComSingleObject, public INSWindowHolder { @@ -58,19 +18,27 @@ void Dealloc() View = NULL; Window = NULL; } + AutoFitContentView* StandardContainer; AvnView* View; AvnWindow* Window; ComPtr BaseEvents; - SoftwareDrawingOperation CurrentSwDrawingOperation; + ComPtr _glContext; + NSObject* renderTarget; AvnPoint lastPositionSet; NSString* _lastTitle; - IAvnAppMenu* _mainMenu; + IAvnMenu* _mainMenu; + + bool _shown; - WindowBaseImpl(IAvnWindowBaseEvents* events) + WindowBaseImpl(IAvnWindowBaseEvents* events, IAvnGlContext* gl) { + _shown = false; _mainMenu = nullptr; BaseEvents = events; + _glContext = gl; + renderTarget = [[IOSurfaceRenderTarget alloc] initWithOpenGlContext: gl]; View = [[AvnView alloc] initWithParent:this]; + StandardContainer = [[AutoFitContentView new] initWithContent:View]; Window = [[AvnWindow alloc] initWithParent:this]; @@ -80,7 +48,9 @@ void Dealloc() [Window setStyleMask:NSWindowStyleMaskBorderless]; [Window setBackingType:NSBackingStoreBuffered]; - [Window setContentView: View]; + + [Window setOpaque:false]; + [Window setContentView: StandardContainer]; } virtual HRESULT ObtainNSWindowHandle(void** ret) override @@ -136,23 +106,34 @@ virtual HRESULT ObtainNSViewHandleRetained(void** ret) override return Window; } - virtual HRESULT Show() override + virtual HRESULT Show(bool activate) override { @autoreleasepool { SetPosition(lastPositionSet); UpdateStyle(); - - [Window makeKeyAndOrderFront:Window]; - [NSApp activateIgnoringOtherApps:YES]; - + if(ShouldTakeFocusOnShow() && activate) + { + [Window makeKeyAndOrderFront:Window]; + [NSApp activateIgnoringOtherApps:YES]; + } + else + { + [Window orderFront: Window]; + } [Window setTitle:_lastTitle]; - [Window setTitleVisibility:NSWindowTitleVisible]; + + _shown = true; return S_OK; } } + virtual bool ShouldTakeFocusOnShow() + { + return true; + } + virtual HRESULT Hide () override { @autoreleasepool @@ -195,7 +176,11 @@ virtual HRESULT Close() override { @autoreleasepool { - [Window close]; + if (Window != nullptr) + { + [Window close]; + } + return S_OK; } } @@ -246,6 +231,29 @@ virtual HRESULT Resize(double x, double y) override { @autoreleasepool { + auto maxSize = [Window maxSize]; + auto minSize = [Window minSize]; + + if (x < minSize.width) + { + x = minSize.width; + } + + if (y < minSize.height) + { + y = minSize.height; + } + + if (x > maxSize.width) + { + x = maxSize.width; + } + + if (y > maxSize.height) + { + y = maxSize.height; + } + [Window setContentSize:NSSize{x, y}]; return S_OK; @@ -262,7 +270,7 @@ virtual HRESULT Invalidate (AvnRect rect) override } } - virtual HRESULT SetMainMenu(IAvnAppMenu* menu) override + virtual HRESULT SetMainMenu(IAvnMenu* menu) override { _mainMenu = menu; @@ -272,37 +280,14 @@ virtual HRESULT SetMainMenu(IAvnAppMenu* menu) override [Window applyMenu:nsmenu]; - return S_OK; - } - - virtual HRESULT ObtainMainMenu(IAvnAppMenu** ret) override - { - if(ret == nullptr) + if ([Window isKeyWindow]) { - return E_POINTER; + [Window showWindowMenuWithAppMenu]; } - *ret = _mainMenu; - return S_OK; } - virtual bool TryLock() override - { - @autoreleasepool - { - return [View lockFocusIfCanDraw] == YES; - } - } - - virtual void Unlock() override - { - @autoreleasepool - { - [View unlockFocus]; - } - } - virtual HRESULT BeginMoveDrag () override { @autoreleasepool @@ -397,16 +382,6 @@ virtual HRESULT ThreadSafeSetSwRenderedFrame(AvnFramebuffer* fb, IUnknown* dispo return S_OK; } - virtual HRESULT GetSoftwareFramebuffer(AvnFramebuffer*ret) override - { - if(![[NSThread currentThread] isMainThread]) - return E_FAIL; - if(CurrentSwDrawingOperation.Data == NULL) - CurrentSwDrawingOperation.Alloc(View); - *ret = CurrentSwDrawingOperation.Desc; - return S_OK; - } - virtual HRESULT SetCursor(IAvnCursor* cursor) override { @autoreleasepool @@ -440,7 +415,66 @@ virtual HRESULT CreateGlRenderTarget(IAvnGlSurfaceRenderTarget** ppv) override { if(View == NULL) return E_FAIL; - *ppv = ::CreateGlRenderTarget(Window, View); + *ppv = [renderTarget createSurfaceRenderTarget]; + return *ppv == nil ? E_FAIL : S_OK; + } + + virtual HRESULT CreateNativeControlHost(IAvnNativeControlHost** retOut) override + { + if(View == NULL) + return E_FAIL; + *retOut = ::CreateNativeControlHost(View); + return S_OK; + } + + virtual HRESULT SetBlurEnabled (bool enable) override + { + [StandardContainer ShowBlur:enable]; + + return S_OK; + } + + virtual HRESULT BeginDragAndDropOperation(AvnDragDropEffects effects, AvnPoint point, + IAvnClipboard* clipboard, IAvnDndResultCallback* cb, + void* sourceHandle) override + { + auto item = TryGetPasteboardItem(clipboard); + [item setString:@"" forType:GetAvnCustomDataType()]; + if(item == nil) + return E_INVALIDARG; + if(View == NULL) + return E_FAIL; + + auto nsevent = [NSApp currentEvent]; + auto nseventType = [nsevent type]; + + // If current event isn't a mouse one (probably due to malfunctioning user app) + // attempt to forge a new one + if(!((nseventType >= NSEventTypeLeftMouseDown && nseventType <= NSEventTypeMouseExited) + || (nseventType >= NSEventTypeOtherMouseDown && nseventType <= NSEventTypeOtherMouseDragged))) + { + auto nspoint = [Window convertBaseToScreen: ToNSPoint(point)]; + CGPoint cgpoint = NSPointToCGPoint(nspoint); + auto cgevent = CGEventCreateMouseEvent(NULL, kCGEventLeftMouseDown, cgpoint, kCGMouseButtonLeft); + nsevent = [NSEvent eventWithCGEvent: cgevent]; + CFRelease(cgevent); + } + + auto dragItem = [[NSDraggingItem alloc] initWithPasteboardWriter: item]; + + auto dragItemImage = [NSImage imageNamed:NSImageNameMultipleDocuments]; + NSRect dragItemRect = {(float)point.X, (float)point.Y, [dragItemImage size].width, [dragItemImage size].height}; + [dragItem setDraggingFrame: dragItemRect contents: dragItemImage]; + + int op = 0; int ieffects = (int)effects; + if((ieffects & (int)AvnDragDropEffects::Copy) != 0) + op |= NSDragOperationCopy; + if((ieffects & (int)AvnDragDropEffects::Link) != 0) + op |= NSDragOperationLink; + if((ieffects & (int)AvnDragDropEffects::Move) != 0) + op |= NSDragOperationMove; + [View beginDraggingSessionWithItems: @[dragItem] event: nsevent + source: CreateDraggingSource((NSDragOperation) op, cb, sourceHandle)]; return S_OK; } @@ -452,9 +486,10 @@ virtual NSWindowStyleMask GetStyle() void UpdateStyle() { - [Window setStyleMask:GetStyle()]; + [Window setStyleMask: GetStyle()]; } +public: virtual void OnResized () { @@ -464,10 +499,15 @@ virtual void OnResized () class WindowImpl : public virtual WindowBaseImpl, public virtual IAvnWindow, public IWindowStateChanged { private: - bool _canResize = true; - bool _hasDecorations = true; - CGRect _lastUndecoratedFrame; + bool _canResize; + bool _fullScreenActive; + SystemDecorations _decorations; AvnWindowState _lastWindowState; + bool _inSetWindowState; + NSRect _preZoomSize; + bool _transitioningWindowState; + bool _isClientAreaExtended; + AvnExtendClientAreaChromeHints _extendClientHints; FORWARD_IUNKNOWN() BEGIN_INTERFACE_MAP() @@ -479,26 +519,70 @@ virtual void OnResized () } ComPtr WindowEvents; - WindowImpl(IAvnWindowEvents* events) : WindowBaseImpl(events) - { + WindowImpl(IAvnWindowEvents* events, IAvnGlContext* gl) : WindowBaseImpl(events, gl) + { + _isClientAreaExtended = false; + _extendClientHints = AvnDefaultChrome; + _fullScreenActive = false; + _canResize = true; + _decorations = SystemDecorationsFull; + _transitioningWindowState = false; + _inSetWindowState = false; + _lastWindowState = Normal; WindowEvents = events; [Window setCanBecomeKeyAndMain]; [Window disableCursorRects]; + [Window setTabbingMode:NSWindowTabbingModeDisallowed]; + } + + void HideOrShowTrafficLights () + { + for (id subview in Window.contentView.superview.subviews) { + if ([subview isKindOfClass:NSClassFromString(@"NSTitlebarContainerView")]) { + NSView *titlebarView = [subview subviews][0]; + for (id button in titlebarView.subviews) { + if ([button isKindOfClass:[NSButton class]]) + { + if(_isClientAreaExtended) + { + auto wantsChrome = (_extendClientHints & AvnSystemChrome) || (_extendClientHints & AvnPreferSystemChrome); + + [button setHidden: !wantsChrome]; + } + else + { + [button setHidden: (_decorations != SystemDecorationsFull)]; + } + + [button setWantsLayer:true]; + } + } + } + } } - virtual HRESULT Show () override + virtual HRESULT Show (bool activate) override { @autoreleasepool - { - if([Window parentWindow] != nil) - [[Window parentWindow] removeChildWindow:Window]; - WindowBaseImpl::Show(); + { + WindowBaseImpl::Show(activate); - return SetWindowState(Normal); + HideOrShowTrafficLights(); + + return SetWindowState(_lastWindowState); + } + } + + virtual HRESULT SetEnabled (bool enable) override + { + @autoreleasepool + { + [Window setEnabled:enable]; + return S_OK; } } - virtual HRESULT ShowDialog (IAvnWindow* parent) override + virtual HRESULT SetParent (IAvnWindow* parent) override { @autoreleasepool { @@ -510,43 +594,102 @@ virtual HRESULT ShowDialog (IAvnWindow* parent) override return E_INVALIDARG; [cparent->Window addChildWindow:Window ordered:NSWindowAbove]; - WindowBaseImpl::Show(); + + UpdateStyle(); return S_OK; } } + void StartStateTransition () override + { + _transitioningWindowState = true; + } + + void EndStateTransition () override + { + _transitioningWindowState = false; + } + + SystemDecorations Decorations () override + { + return _decorations; + } + + AvnWindowState WindowState () override + { + return _lastWindowState; + } + void WindowStateChanged () override { - AvnWindowState state; - GetWindowState(&state); - WindowEvents->WindowStateChanged(state); + if(!_inSetWindowState && !_transitioningWindowState) + { + AvnWindowState state; + GetWindowState(&state); + + if(_lastWindowState != state) + { + if(_isClientAreaExtended) + { + if(_lastWindowState == FullScreen) + { + // we exited fs. + if(_extendClientHints & AvnOSXThickTitleBar) + { + Window.toolbar = [NSToolbar new]; + Window.toolbar.showsBaselineSeparator = false; + } + + [Window setTitlebarAppearsTransparent:true]; + + [StandardContainer setFrameSize: StandardContainer.frame.size]; + } + else if(state == FullScreen) + { + // we entered fs. + if(_extendClientHints & AvnOSXThickTitleBar) + { + Window.toolbar = nullptr; + } + + [Window setTitlebarAppearsTransparent:false]; + + [StandardContainer setFrameSize: StandardContainer.frame.size]; + } + } + + _lastWindowState = state; + WindowEvents->WindowStateChanged(state); + } + } } bool UndecoratedIsMaximized () { - return CGRectEqualToRect([Window frame], [Window screen].visibleFrame); + auto windowSize = [Window frame]; + auto available = [Window screen].visibleFrame; + return CGRectEqualToRect(windowSize, available); } bool IsZoomed () { - return _hasDecorations ? [Window isZoomed] : UndecoratedIsMaximized(); + return _decorations == SystemDecorationsFull ? [Window isZoomed] : UndecoratedIsMaximized(); } void DoZoom() { - if (_hasDecorations) - { - [Window performZoom:Window]; - } - else + switch (_decorations) { - if (!UndecoratedIsMaximized()) - { - _lastUndecoratedFrame = [Window frame]; - } + case SystemDecorationsNone: + case SystemDecorationsBorderOnly: + [Window setFrame:[Window screen].visibleFrame display:true]; + break; + - [Window zoom:Window]; + case SystemDecorationsFull: + [Window performZoom:Window]; + break; } } @@ -560,24 +703,77 @@ virtual HRESULT SetCanResize(bool value) override } } - virtual HRESULT SetHasDecorations(bool value) override + virtual HRESULT SetDecorations(SystemDecorations value) override { @autoreleasepool { - _hasDecorations = value; + auto currentWindowState = _lastWindowState; + _decorations = value; + + if(_fullScreenActive) + { + return S_OK; + } + UpdateStyle(); + HideOrShowTrafficLights(); + + switch (_decorations) + { + case SystemDecorationsNone: + [Window setHasShadow:NO]; + [Window setTitleVisibility:NSWindowTitleHidden]; + [Window setTitlebarAppearsTransparent:YES]; + + if(currentWindowState == Maximized) + { + if(!UndecoratedIsMaximized()) + { + DoZoom(); + } + } + break; + + case SystemDecorationsBorderOnly: + [Window setHasShadow:YES]; + [Window setTitleVisibility:NSWindowTitleHidden]; + [Window setTitlebarAppearsTransparent:YES]; + + if(currentWindowState == Maximized) + { + if(!UndecoratedIsMaximized()) + { + DoZoom(); + } + } + break; + + case SystemDecorationsFull: + [Window setHasShadow:YES]; + [Window setTitleVisibility:NSWindowTitleVisible]; + [Window setTitlebarAppearsTransparent:NO]; + [Window setTitle:_lastTitle]; + + if(currentWindowState == Maximized) + { + auto newFrame = [Window contentRectForFrameRect:[Window frame]].size; + + [View setFrameSize:newFrame]; + } + break; + } + return S_OK; } } - virtual HRESULT SetTitle (void* utf8title) override + virtual HRESULT SetTitle (char* utf8title) override { @autoreleasepool { _lastTitle = [NSString stringWithUTF8String:(const char*)utf8title]; [Window setTitle:_lastTitle]; - [Window setTitleVisibility:NSWindowTitleVisible]; return S_OK; } @@ -621,13 +817,19 @@ virtual HRESULT GetWindowState (AvnWindowState*ret) override return E_POINTER; } + if(([Window styleMask] & NSWindowStyleMaskFullScreen) == NSWindowStyleMaskFullScreen) + { + *ret = FullScreen; + return S_OK; + } + if([Window isMiniaturized]) { *ret = Minimized; return S_OK; } - if([Window isZoomed]) + if(IsZoomed()) { *ret = Maximized; return S_OK; @@ -639,112 +841,438 @@ virtual HRESULT GetWindowState (AvnWindowState*ret) override } } - virtual HRESULT SetWindowState (AvnWindowState state) override + virtual HRESULT TakeFocusFromChildren () override { - @autoreleasepool + if(Window == nil) + return S_OK; + if([Window isKeyWindow]) + [Window makeFirstResponder: View]; + + return S_OK; + } + + virtual HRESULT SetExtendClientArea (bool enable) override + { + _isClientAreaExtended = enable; + + if(enable) { - _lastWindowState = state; + Window.titleVisibility = NSWindowTitleHidden; - switch (state) { - case Maximized: - lastPositionSet.X = 0; - lastPositionSet.Y = 0; - - if([Window isMiniaturized]) - { - [Window deminiaturize:Window]; - } - - if(!IsZoomed()) - { - DoZoom(); - } - break; - - case Minimized: - [Window miniaturize:Window]; - break; - - default: - if([Window isMiniaturized]) - { - [Window deminiaturize:Window]; - } - - if(IsZoomed()) - { - DoZoom(); - } - break; + [Window setTitlebarAppearsTransparent:true]; + + auto wantsTitleBar = (_extendClientHints & AvnSystemChrome) || (_extendClientHints & AvnPreferSystemChrome); + + if (wantsTitleBar) + { + [StandardContainer ShowTitleBar:true]; + } + else + { + [StandardContainer ShowTitleBar:false]; } - return S_OK; + if(_extendClientHints & AvnOSXThickTitleBar) + { + Window.toolbar = [NSToolbar new]; + Window.toolbar.showsBaselineSeparator = false; + } + else + { + Window.toolbar = nullptr; + } + } + else + { + Window.titleVisibility = NSWindowTitleVisible; + Window.toolbar = nullptr; + [Window setTitlebarAppearsTransparent:false]; + View.layer.zPosition = 0; } + + [Window setIsExtended:enable]; + + HideOrShowTrafficLights(); + + UpdateStyle(); + + return S_OK; } -protected: - virtual void OnResized () override + virtual HRESULT SetExtendClientAreaHints (AvnExtendClientAreaChromeHints hints) override { - auto windowState = [Window isMiniaturized] ? Minimized - : (IsZoomed() ? Maximized : Normal); + _extendClientHints = hints; - if (windowState != _lastWindowState) + SetExtendClientArea(_isClientAreaExtended); + return S_OK; + } + + virtual HRESULT GetExtendTitleBarHeight (double*ret) override + { + if(ret == nullptr) { - _lastWindowState = windowState; - - WindowEvents->WindowStateChanged(windowState); + return E_POINTER; } + + *ret = [Window getExtendedTitleBarHeight]; + + return S_OK; } - virtual NSWindowStyleMask GetStyle() override + virtual HRESULT SetExtendTitleBarHeight (double value) override { - unsigned long s = NSWindowStyleMaskBorderless; - if(_hasDecorations) - s = s | NSWindowStyleMaskTitled | NSWindowStyleMaskClosable | NSWindowStyleMaskMiniaturizable; - if(_canResize) - s = s | NSWindowStyleMaskResizable; - return s; + [StandardContainer SetTitleBarHeightHint:value]; + return S_OK; } -}; - -NSArray* AllLoopModes = [NSArray arrayWithObjects: NSDefaultRunLoopMode, NSEventTrackingRunLoopMode, NSModalPanelRunLoopMode, NSRunLoopCommonModes, NSConnectionReplyMode, nil]; - -@implementation AvnView -{ - ComPtr _parent; - ComPtr _swRenderedFrame; - AvnFramebuffer _swRenderedFrameBuffer; - bool _queuedDisplayFromThread; - NSTrackingArea* _area; - bool _isLeftPressed, _isMiddlePressed, _isRightPressed, _isXButton1Pressed, _isXButton2Pressed, _isMouseOver; - NSEvent* _lastMouseDownEvent; - bool _lastKeyHandled; -} - -- (void)dealloc -{ -} - -- (void)onClosed -{ - _parent = NULL; -} - -- (NSEvent*) lastMouseDownEvent -{ - return _lastMouseDownEvent; -} - --(AvnView*) initWithParent: (WindowBaseImpl*) parent + + void EnterFullScreenMode () + { + _fullScreenActive = true; + + [Window setHasShadow:YES]; + [Window setTitleVisibility:NSWindowTitleVisible]; + [Window setTitlebarAppearsTransparent:NO]; + [Window setTitle:_lastTitle]; + + Window.styleMask = Window.styleMask | NSWindowStyleMaskTitled | NSWindowStyleMaskResizable; + Window.styleMask = Window.styleMask & ~NSWindowStyleMaskFullSizeContentView; + + [Window toggleFullScreen:nullptr]; + } + + void ExitFullScreenMode () + { + [Window toggleFullScreen:nullptr]; + + _fullScreenActive = false; + + SetDecorations(_decorations); + } + + virtual HRESULT SetWindowState (AvnWindowState state) override + { + @autoreleasepool + { + if(_lastWindowState == state) + { + return S_OK; + } + + _inSetWindowState = true; + + auto currentState = _lastWindowState; + _lastWindowState = state; + + if(currentState == Normal) + { + _preZoomSize = [Window frame]; + } + + if(_shown) + { + switch (state) { + case Maximized: + if(currentState == FullScreen) + { + ExitFullScreenMode(); + } + + lastPositionSet.X = 0; + lastPositionSet.Y = 0; + + if([Window isMiniaturized]) + { + [Window deminiaturize:Window]; + } + + if(!IsZoomed()) + { + DoZoom(); + } + break; + + case Minimized: + if(currentState == FullScreen) + { + ExitFullScreenMode(); + } + else + { + [Window miniaturize:Window]; + } + break; + + case FullScreen: + if([Window isMiniaturized]) + { + [Window deminiaturize:Window]; + } + + EnterFullScreenMode(); + break; + + case Normal: + if([Window isMiniaturized]) + { + [Window deminiaturize:Window]; + } + + if(currentState == FullScreen) + { + ExitFullScreenMode(); + } + + if(IsZoomed()) + { + if(_decorations == SystemDecorationsFull) + { + DoZoom(); + } + else + { + [Window setFrame:_preZoomSize display:true]; + auto newFrame = [Window contentRectForFrameRect:[Window frame]].size; + + [View setFrameSize:newFrame]; + } + + } + break; + } + } + + _inSetWindowState = false; + + return S_OK; + } + } + + virtual void OnResized () override + { + if(_shown && !_inSetWindowState && !_transitioningWindowState) + { + WindowStateChanged(); + } + } + +protected: + virtual NSWindowStyleMask GetStyle() override + { + unsigned long s = NSWindowStyleMaskBorderless; + + switch (_decorations) + { + case SystemDecorationsNone: + s = s | NSWindowStyleMaskFullSizeContentView; + break; + + case SystemDecorationsBorderOnly: + s = s | NSWindowStyleMaskTitled | NSWindowStyleMaskFullSizeContentView; + break; + + case SystemDecorationsFull: + s = s | NSWindowStyleMaskTitled | NSWindowStyleMaskClosable | NSWindowStyleMaskBorderless; + + if(_canResize) + { + s = s | NSWindowStyleMaskResizable; + } + break; + } + + if([Window parentWindow] == nullptr) + { + s |= NSWindowStyleMaskMiniaturizable; + } + + if(_isClientAreaExtended) + { + s |= NSWindowStyleMaskFullSizeContentView | NSWindowStyleMaskTexturedBackground; + } + return s; + } +}; + +NSArray* AllLoopModes = [NSArray arrayWithObjects: NSDefaultRunLoopMode, NSEventTrackingRunLoopMode, NSModalPanelRunLoopMode, NSRunLoopCommonModes, NSConnectionReplyMode, nil]; + +@implementation AutoFitContentView +{ + NSVisualEffectView* _titleBarMaterial; + NSBox* _titleBarUnderline; + NSView* _content; + NSVisualEffectView* _blurBehind; + double _titleBarHeightHint; + bool _settingSize; +} + +-(AutoFitContentView* _Nonnull) initWithContent:(NSView *)content +{ + _titleBarHeightHint = -1; + _content = content; + _settingSize = false; + + [self setAutoresizesSubviews:true]; + [self setWantsLayer:true]; + + _titleBarMaterial = [NSVisualEffectView new]; + [_titleBarMaterial setBlendingMode:NSVisualEffectBlendingModeWithinWindow]; + [_titleBarMaterial setMaterial:NSVisualEffectMaterialTitlebar]; + [_titleBarMaterial setWantsLayer:true]; + _titleBarMaterial.hidden = true; + + _titleBarUnderline = [NSBox new]; + _titleBarUnderline.boxType = NSBoxSeparator; + _titleBarUnderline.fillColor = [NSColor underPageBackgroundColor]; + _titleBarUnderline.hidden = true; + + [self addSubview:_titleBarMaterial]; + [self addSubview:_titleBarUnderline]; + + _blurBehind = [NSVisualEffectView new]; + [_blurBehind setBlendingMode:NSVisualEffectBlendingModeBehindWindow]; + [_blurBehind setMaterial:NSVisualEffectMaterialLight]; + [_blurBehind setWantsLayer:true]; + _blurBehind.hidden = true; + + [self addSubview:_blurBehind]; + [self addSubview:_content]; + + [self setWantsLayer:true]; + return self; +} + +-(void) ShowBlur:(bool)show +{ + _blurBehind.hidden = !show; +} + +-(void) ShowTitleBar: (bool) show +{ + _titleBarMaterial.hidden = !show; + _titleBarUnderline.hidden = !show; +} + +-(void) SetTitleBarHeightHint: (double) height +{ + _titleBarHeightHint = height; + + [self setFrameSize:self.frame.size]; +} + +-(void)setFrameSize:(NSSize)newSize +{ + if(_settingSize) + { + return; + } + + _settingSize = true; + [super setFrameSize:newSize]; + + [_blurBehind setFrameSize:newSize]; + [_content setFrameSize:newSize]; + + auto window = objc_cast([self window]); + + // TODO get actual titlebar size + + double height = _titleBarHeightHint == -1 ? [window getExtendedTitleBarHeight] : _titleBarHeightHint; + + NSRect tbar; + tbar.origin.x = 0; + tbar.origin.y = newSize.height - height; + tbar.size.width = newSize.width; + tbar.size.height = height; + + [_titleBarMaterial setFrame:tbar]; + tbar.size.height = height < 1 ? 0 : 1; + [_titleBarUnderline setFrame:tbar]; + _settingSize = false; +} + +-(void) SetContent: (NSView* _Nonnull) content +{ + if(content != nullptr) + { + [content removeFromSuperview]; + [self addSubview:content]; + _content = content; + } +} +@end + +@implementation AvnView +{ + ComPtr _parent; + ComPtr _swRenderedFrame; + AvnFramebuffer _swRenderedFrameBuffer; + bool _queuedDisplayFromThread; + NSTrackingArea* _area; + bool _isLeftPressed, _isMiddlePressed, _isRightPressed, _isXButton1Pressed, _isXButton2Pressed, _isMouseOver; + AvnInputModifiers _modifierState; + NSEvent* _lastMouseDownEvent; + bool _lastKeyHandled; + AvnPixelSize _lastPixelSize; + NSObject* _renderTarget; +} + +- (void)onClosed +{ + @synchronized (self) + { + _parent = nullptr; + } +} + +-(AvnPixelSize) getPixelSize +{ + return _lastPixelSize; +} + +- (NSEvent*) lastMouseDownEvent +{ + return _lastMouseDownEvent; +} + +- (void) updateRenderTarget +{ + [_renderTarget resize:_lastPixelSize withScale: [[self window] backingScaleFactor]]; + [self setNeedsDisplayInRect:[self frame]]; +} + +-(AvnView*) initWithParent: (WindowBaseImpl*) parent { self = [super init]; - [self setWantsBestResolutionOpenGLSurface:true]; + _renderTarget = parent->renderTarget; [self setWantsLayer:YES]; + [self setLayerContentsRedrawPolicy: NSViewLayerContentsRedrawDuringViewResize]; + _parent = parent; _area = nullptr; + _lastPixelSize.Height = 100; + _lastPixelSize.Width = 100; + [self registerForDraggedTypes: @[@"public.data", GetAvnCustomDataType()]]; + + _modifierState = AvnInputModifiersNone; return self; } +- (BOOL)isFlipped +{ + return YES; +} + +- (BOOL)wantsUpdateLayer +{ + return YES; +} + +- (void)setLayer:(CALayer *)layer +{ + [_renderTarget setNewLayer: layer]; + [super setLayer: layer]; +} + - (BOOL)isOpaque { return YES; @@ -774,7 +1302,12 @@ -(void)setFrameSize:(NSSize)newSize [self removeTrackingArea:_area]; _area = nullptr; } - + + if (_parent == nullptr) + { + return; + } + NSRect rect = NSZeroRect; rect.size = newSize; @@ -783,81 +1316,47 @@ -(void)setFrameSize:(NSSize)newSize [self addTrackingArea:_area]; _parent->UpdateCursor(); - - _parent->BaseEvents->Resized(AvnSize{newSize.width, newSize.height}); -} - -- (void) drawFb: (AvnFramebuffer*) fb -{ - auto colorSpace = CGColorSpaceCreateDeviceRGB(); - auto dataProvider = CGDataProviderCreateWithData(NULL, fb->Data, fb->Height*fb->Stride, NULL); - - - auto image = CGImageCreate(fb->Width, fb->Height, 8, 32, fb->Stride, colorSpace, kCGBitmapByteOrderDefault | kCGImageAlphaPremultipliedLast, - dataProvider, nullptr, false, kCGRenderingIntentDefault); - auto ctx = [NSGraphicsContext currentContext]; + auto fsize = [self convertSizeToBacking: [self frame].size]; - [ctx saveGraphicsState]; - auto cgc = [ctx CGContext]; - - CGContextDrawImage(cgc, CGRect{0,0, fb->Width/(fb->Dpi.X/96), fb->Height/(fb->Dpi.Y/96)}, image); - CGImageRelease(image); - CGColorSpaceRelease(colorSpace); - CGDataProviderRelease(dataProvider); + if(_lastPixelSize.Width != (int)fsize.width || _lastPixelSize.Height != (int)fsize.height) + { + _lastPixelSize.Width = (int)fsize.width; + _lastPixelSize.Height = (int)fsize.height; + [self updateRenderTarget]; - [ctx restoreGraphicsState]; - + _parent->BaseEvents->Resized(AvnSize{newSize.width, newSize.height}); + } } -- (void)drawRect:(NSRect)dirtyRect +- (void)updateLayer { - _parent->BaseEvents->RunRenderPriorityJobs(); - @synchronized (self) { - if(_swRenderedFrame != NULL) - { - [self drawFb: &_swRenderedFrameBuffer]; - return; - } + AvnInsidePotentialDeadlock deadlock; + if (_parent == nullptr) + { + return; } - auto swOp = &_parent->CurrentSwDrawingOperation; - _parent->BaseEvents->Paint(); - if(swOp->Data != NULL) - [self drawFb: &swOp->Desc]; + _parent->BaseEvents->RunRenderPriorityJobs(); - swOp->Dealloc(); - return; + if (_parent == nullptr) + { + return; + } + + _parent->BaseEvents->Paint(); } --(void) redrawSelf +- (void)drawRect:(NSRect)dirtyRect { - @autoreleasepool - { - @synchronized(self) - { - if(!_queuedDisplayFromThread) - return; - _queuedDisplayFromThread = false; - } - [self setNeedsDisplayInRect:[self frame]]; - [self display]; - - } + return; } -(void) setSwRenderedFrame: (AvnFramebuffer*) fb dispose: (IUnknown*) dispose { @autoreleasepool { - @synchronized (self) { - _swRenderedFrame = dispose; - _swRenderedFrameBuffer = *fb; - if(!_queuedDisplayFromThread) - { - _queuedDisplayFromThread = true; - [self performSelector:@selector(redrawSelf) onThread:[NSThread mainThread] withObject:NULL waitUntilDone:false modes: AllLoopModes]; - } - } + [_renderTarget setSwFrame:fb]; + dispose->Release(); } } @@ -879,24 +1378,50 @@ - (AvnPoint)toAvnPoint:(CGPoint)p - (void) viewDidChangeBackingProperties { - _parent->BaseEvents->ScalingChanged([_parent->Window backingScaleFactor]); + auto fsize = [self convertSizeToBacking: [self frame].size]; + _lastPixelSize.Width = (int)fsize.width; + _lastPixelSize.Height = (int)fsize.height; + [self updateRenderTarget]; + + if(_parent != nullptr) + { + _parent->BaseEvents->ScalingChanged([_parent->Window backingScaleFactor]); + } + [super viewDidChangeBackingProperties]; } -- (bool) ignoreUserInput +- (bool) ignoreUserInput:(bool)trigerInputWhenDisabled { auto parentWindow = objc_cast([self window]); + if(parentWindow == nil || ![parentWindow shouldTryToHandleEvents]) + { + if(trigerInputWhenDisabled) + { + auto window = dynamic_cast(_parent.getRaw()); + + if(window != nullptr) + { + window->WindowEvents->GotInputWhenDisabled(); + } + } + return TRUE; + } + return FALSE; } - (void)mouseEvent:(NSEvent *)event withType:(AvnRawMouseEventType) type { - if([self ignoreUserInput]) + bool triggerInputWhenDisabled = type != Move; + + if([self ignoreUserInput: triggerInputWhenDisabled]) + { return; + } - [self becomeFirstResponder]; auto localPoint = [self convertPoint:[event locationInWindow] toView:self]; auto avnPoint = [self toAvnPoint:localPoint]; auto point = [self translateLocalPoint:avnPoint]; @@ -923,11 +1448,31 @@ - (void)mouseEvent:(NSEvent *)event withType:(AvnRawMouseEventType) type auto timestamp = [event timestamp] * 1000; auto modifiers = [self getModifiers:[event modifierFlags]]; - [self becomeFirstResponder]; - _parent->BaseEvents->RawMouseEvent(type, timestamp, modifiers, point, delta); + if(type != AvnRawMouseEventType::Move || + ( + [self window] != nil && + ( + [[self window] firstResponder] == nil + || ![[[self window] firstResponder] isKindOfClass: [NSView class]] + ) + ) + ) + [self becomeFirstResponder]; + + if(_parent != nullptr) + { + _parent->BaseEvents->RawMouseEvent(type, timestamp, modifiers, point, delta); + } + [super mouseMoved:event]; } +- (BOOL) resignFirstResponder +{ + _parent->BaseEvents->LostFocus(); + return YES; +} + - (void)mouseMoved:(NSEvent *)event { [self mouseEvent:event withType:Move]; @@ -1038,14 +1583,20 @@ - (void)mouseExited:(NSEvent *)event - (void) keyboardEvent: (NSEvent *) event withType: (AvnRawKeyEventType)type { - if([self ignoreUserInput]) + if([self ignoreUserInput: false]) + { return; + } + auto key = s_KeyMap[[event keyCode]]; auto timestamp = [event timestamp] * 1000; auto modifiers = [self getModifiers:[event modifierFlags]]; - _lastKeyHandled = _parent->BaseEvents->RawKeyEvent(type, timestamp, modifiers, key); + if(_parent != nullptr) + { + _lastKeyHandled = _parent->BaseEvents->RawKeyEvent(type, timestamp, modifiers, key); + } } - (BOOL)performKeyEquivalent:(NSEvent *)event @@ -1057,6 +1608,63 @@ - (BOOL)performKeyEquivalent:(NSEvent *)event return result; } +- (void)flagsChanged:(NSEvent *)event +{ + auto newModifierState = [self getModifiers:[event modifierFlags]]; + + bool isAltCurrentlyPressed = (_modifierState & Alt) == Alt; + bool isControlCurrentlyPressed = (_modifierState & Control) == Control; + bool isShiftCurrentlyPressed = (_modifierState & Shift) == Shift; + bool isCommandCurrentlyPressed = (_modifierState & Windows) == Windows; + + bool isAltPressed = (newModifierState & Alt) == Alt; + bool isControlPressed = (newModifierState & Control) == Control; + bool isShiftPressed = (newModifierState & Shift) == Shift; + bool isCommandPressed = (newModifierState & Windows) == Windows; + + + if (isAltPressed && !isAltCurrentlyPressed) + { + [self keyboardEvent:event withType:KeyDown]; + } + else if (isAltCurrentlyPressed && !isAltPressed) + { + [self keyboardEvent:event withType:KeyUp]; + } + + if (isControlPressed && !isControlCurrentlyPressed) + { + [self keyboardEvent:event withType:KeyDown]; + } + else if (isControlCurrentlyPressed && !isControlPressed) + { + [self keyboardEvent:event withType:KeyUp]; + } + + if (isShiftPressed && !isShiftCurrentlyPressed) + { + [self keyboardEvent:event withType:KeyDown]; + } + else if(isShiftCurrentlyPressed && !isShiftPressed) + { + [self keyboardEvent:event withType:KeyUp]; + } + + if(isCommandPressed && !isCommandCurrentlyPressed) + { + [self keyboardEvent:event withType:KeyDown]; + } + else if(isCommandCurrentlyPressed && ! isCommandPressed) + { + [self keyboardEvent:event withType:KeyUp]; + } + + _modifierState = newModifierState; + + [[self inputContext] handleEvent:event]; + [super flagsChanged:event]; +} + - (void)keyDown:(NSEvent *)event { [self keyboardEvent:event withType:KeyDown]; @@ -1136,7 +1744,10 @@ - (void)insertText:(id)string replacementRange:(NSRange)replacementRange { if(!_lastKeyHandled) { - _lastKeyHandled = _parent->BaseEvents->RawTextInputEvent(0, [string UTF8String]); + if(_parent != nullptr) + { + _lastKeyHandled = _parent->BaseEvents->RawTextInputEvent(0, [string UTF8String]); + } } } @@ -1151,6 +1762,68 @@ - (NSRect)firstRectForCharacterRange:(NSRange)range actualRange:(NSRangePointer) return result; } + +- (NSDragOperation)triggerAvnDragEvent: (AvnDragEventType) type info: (id )info +{ + auto localPoint = [self convertPoint:[info draggingLocation] toView:self]; + auto avnPoint = [self toAvnPoint:localPoint]; + auto point = [self translateLocalPoint:avnPoint]; + auto modifiers = [self getModifiers:[[NSApp currentEvent] modifierFlags]]; + NSDragOperation nsop = [info draggingSourceOperationMask]; + + auto effects = ConvertDragDropEffects(nsop); + int reffects = (int)_parent->BaseEvents + ->DragEvent(type, point, modifiers, effects, + CreateClipboard([info draggingPasteboard], nil), + GetAvnDataObjectHandleFromDraggingInfo(info)); + + NSDragOperation ret = 0; + + // Ensure that the managed part didn't add any new effects + reffects = (int)effects & (int)reffects; + + // OSX requires exactly one operation + if((reffects & (int)AvnDragDropEffects::Copy) != 0) + ret = NSDragOperationCopy; + else if((reffects & (int)AvnDragDropEffects::Move) != 0) + ret = NSDragOperationMove; + else if((reffects & (int)AvnDragDropEffects::Link) != 0) + ret = NSDragOperationLink; + if(ret == 0) + ret = NSDragOperationNone; + return ret; +} + +- (NSDragOperation)draggingEntered:(id )sender +{ + return [self triggerAvnDragEvent: AvnDragEventType::Enter info:sender]; +} + +- (NSDragOperation)draggingUpdated:(id )sender +{ + return [self triggerAvnDragEvent: AvnDragEventType::Over info:sender]; +} + +- (void)draggingExited:(id )sender +{ + [self triggerAvnDragEvent: AvnDragEventType::Leave info:sender]; +} + +- (BOOL)prepareForDragOperation:(id )sender +{ + return [self triggerAvnDragEvent: AvnDragEventType::Over info:sender] != NSDragOperationNone; +} + +- (BOOL)performDragOperation:(id )sender +{ + return [self triggerAvnDragEvent: AvnDragEventType::Drop info:sender] != NSDragOperationNone; +} + +- (void)concludeDragOperation:(nullable id )sender +{ + +} + @end @@ -1159,8 +1832,42 @@ @implementation AvnWindow ComPtr _parent; bool _canBecomeKeyAndMain; bool _closed; - NSMenu* _menu; - bool _isAppMenuApplied; + bool _isEnabled; + bool _isExtended; + AvnMenu* _menu; + double _lastScaling; +} + +-(void) setIsExtended:(bool)value; +{ + _isExtended = value; +} + +-(double) getScaling +{ + return _lastScaling; +} + +-(double) getExtendedTitleBarHeight +{ + if(_isExtended) + { + for (id subview in self.contentView.superview.subviews) + { + if ([subview isKindOfClass:NSClassFromString(@"NSTitlebarContainerView")]) + { + NSView *titlebarView = [subview subviews][0]; + + return (double)titlebarView.frame.size.height; + } + } + + return -1; + } + else + { + return 0; + } } +(void)closeAll @@ -1174,8 +1881,18 @@ +(void)closeAll } } -- (void)dealloc +- (void)performClose:(id)sender { + if([[self delegate] respondsToSelector:@selector(windowShouldClose:)]) + { + if(![[self delegate] windowShouldClose:self]) return; + } + else if([self respondsToSelector:@selector(windowShouldClose:)]) + { + if(![self windowShouldClose:self]) return; + } + + [self close]; } - (void)pollModalSession:(nonnull NSModalSession)session @@ -1195,30 +1912,62 @@ - (void)pollModalSession:(nonnull NSModalSession)session } } --(void) applyMenu:(NSMenu *)menu +-(void) showWindowMenuWithAppMenu { - if(menu == nullptr) + if(_menu != nullptr) { - menu = [NSMenu new]; + auto appMenuItem = ::GetAppMenuItem(); + + if(appMenuItem != nullptr) + { + auto appMenu = [appMenuItem menu]; + + [appMenu removeItem:appMenuItem]; + + [_menu insertItem:appMenuItem atIndex:0]; + + [_menu setHasGlobalMenuItem:true]; + } + + [NSApp setMenu:_menu]; } +} + +-(void) showAppMenuOnly +{ + auto appMenuItem = ::GetAppMenuItem(); - _menu = menu; - - if ([self isKeyWindow]) + if(appMenuItem != nullptr) { - auto appMenu = ::GetAppMenuItem(); + auto appMenu = ::GetAppMenu(); - if(appMenu != nullptr) + auto nativeAppMenu = dynamic_cast(appMenu); + + [[appMenuItem menu] removeItem:appMenuItem]; + + if(_menu != nullptr) { - [[appMenu menu] removeItem:appMenu]; - - [_menu insertItem:appMenu atIndex:0]; - - _isAppMenuApplied = true; + [_menu setHasGlobalMenuItem:false]; } - [NSApp setMenu:menu]; + [nativeAppMenu->GetNative() addItem:appMenuItem]; + + [NSApp setMenu:nativeAppMenu->GetNative()]; } + else + { + [NSApp setMenu:nullptr]; + } +} + +-(void) applyMenu:(AvnMenu *)menu +{ + if(menu == nullptr) + { + menu = [AvnMenu new]; + } + + _menu = menu; } -(void) setCanBecomeKeyAndMain @@ -1232,6 +1981,14 @@ -(AvnWindow*) initWithParent: (WindowBaseImpl*) parent [self setReleasedWhenClosed:false]; _parent = parent; [self setDelegate:self]; + _closed = false; + _isEnabled = true; + + _lastScaling = [self backingScaleFactor]; + [self setOpaque:NO]; + [self setBackgroundColor: [NSColor clearColor]]; + [self invalidateShadow]; + _isExtended = false; return self; } @@ -1247,6 +2004,11 @@ - (BOOL)windowShouldClose:(NSWindow *)sender return true; } +- (void)windowDidChangeBackingProperties:(NSNotification *)notification +{ + _lastScaling = [self backingScaleFactor]; +} + - (void)windowWillClose:(NSNotification *)notification { _closed = true; @@ -1257,9 +2019,6 @@ - (void)windowWillClose:(NSNotification *)notification [self restoreParentWindow]; parent->BaseEvents->Closed(); [parent->View onClosed]; - dispatch_async(dispatch_get_main_queue(), ^{ - [self setContentView: nil]; - }); } } @@ -1291,14 +2050,12 @@ -(bool) activateAppropriateChild: (bool)activating -(bool)shouldTryToHandleEvents { - for(NSWindow* uch in [self childWindows]) - { - auto ch = objc_cast(uch); - if(ch == nil) - continue; - return FALSE; - } - return TRUE; + return _isEnabled; +} + +-(void) setEnabled:(bool)enable +{ + _isEnabled = enable; } -(void)makeKeyWindow @@ -1313,25 +2070,13 @@ -(void)becomeKeyWindow { if([self activateAppropriateChild: true]) { - if(_menu == nullptr) - { - _menu = [NSMenu new]; - } + [self showWindowMenuWithAppMenu]; - auto appMenu = ::GetAppMenuItem(); - - if(appMenu != nullptr) + if(_parent != nullptr) { - [[appMenu menu] removeItem:appMenu]; - - [_menu insertItem:appMenu atIndex:0]; - - _isAppMenuApplied = true; + _parent->BaseEvents->Activated(); } - [NSApp setMenu:_menu]; - - _parent->BaseEvents->Activated(); [super becomeKeyWindow]; } } @@ -1366,58 +2111,95 @@ - (void)windowDidDeminiaturize:(NSNotification *)notification } } -- (BOOL)windowShouldZoom:(NSWindow *)window toFrame:(NSRect)newFrame +- (void)windowDidResize:(NSNotification *)notification { - return true; + auto parent = dynamic_cast(_parent.operator->()); + + if(parent != nullptr) + { + parent->WindowStateChanged(); + } } --(void)resignKeyWindow +- (void)windowWillExitFullScreen:(NSNotification *)notification { - if(_parent) - _parent->BaseEvents->Deactivated(); + auto parent = dynamic_cast(_parent.operator->()); - auto appMenuItem = ::GetAppMenuItem(); + if(parent != nullptr) + { + parent->StartStateTransition(); + } +} + +- (void)windowDidExitFullScreen:(NSNotification *)notification +{ + auto parent = dynamic_cast(_parent.operator->()); - if(appMenuItem != nullptr) + if(parent != nullptr) { - auto appMenu = ::GetAppMenu(); + parent->EndStateTransition(); - auto nativeAppMenu = dynamic_cast(appMenu); - - [[appMenuItem menu] removeItem:appMenuItem]; + if(parent->Decorations() != SystemDecorationsFull && parent->WindowState() == Maximized) + { + NSRect screenRect = [[self screen] visibleFrame]; + [self setFrame:screenRect display:YES]; + } - [nativeAppMenu->GetNative() addItem:appMenuItem]; + if(parent->WindowState() == Minimized) + { + [self miniaturize:nullptr]; + } - [NSApp setMenu:nativeAppMenu->GetNative()]; + parent->WindowStateChanged(); } - else +} + +- (void)windowWillEnterFullScreen:(NSNotification *)notification +{ + auto parent = dynamic_cast(_parent.operator->()); + + if(parent != nullptr) { - [NSApp setMenu:nullptr]; + parent->StartStateTransition(); } +} + +- (void)windowDidEnterFullScreen:(NSNotification *)notification +{ + auto parent = dynamic_cast(_parent.operator->()); - // remove window menu items from appmenu? - - [super resignKeyWindow]; + if(parent != nullptr) + { + parent->EndStateTransition(); + parent->WindowStateChanged(); + } } -- (void)windowDidMove:(NSNotification *)notification +- (BOOL)windowShouldZoom:(NSWindow *)window toFrame:(NSRect)newFrame { - AvnPoint position; - _parent->GetPosition(&position); - _parent->BaseEvents->PositionChanged(position); + return true; } -// TODO this breaks resizing. -/*- (void)windowDidResize:(NSNotification *)notification +-(void)resignKeyWindow { + if(_parent) + _parent->BaseEvents->Deactivated(); - auto parent = dynamic_cast(_parent.operator->()); + [self showAppMenuOnly]; - if(parent != nullptr) + [super resignKeyWindow]; +} + +- (void)windowDidMove:(NSNotification *)notification +{ + AvnPoint position; + + if(_parent != nullptr) { - parent->WindowStateChanged(); + _parent->GetPosition(&position); + _parent->BaseEvents->PositionChanged(position); } -}*/ +} @end class PopupImpl : public virtual WindowBaseImpl, public IAvnPopup @@ -1429,12 +2211,11 @@ - (void)windowDidMove:(NSNotification *)notification END_INTERFACE_MAP() virtual ~PopupImpl(){} ComPtr WindowEvents; - PopupImpl(IAvnWindowEvents* events) : WindowBaseImpl(events) + PopupImpl(IAvnWindowEvents* events, IAvnGlContext* gl) : WindowBaseImpl(events, gl) { WindowEvents = events; [Window setLevel:NSPopUpMenuWindowLevel]; } - protected: virtual NSWindowStyleMask GetStyle() override { @@ -1448,25 +2229,31 @@ virtual HRESULT Resize(double x, double y) override [Window setContentSize:NSSize{x, y}]; [Window setFrameTopLeftPoint:ToNSPoint(ConvertPointY(lastPositionSet))]; + return S_OK; } } +public: + virtual bool ShouldTakeFocusOnShow() override + { + return false; + } }; -extern IAvnPopup* CreateAvnPopup(IAvnWindowEvents*events) +extern IAvnPopup* CreateAvnPopup(IAvnWindowEvents*events, IAvnGlContext* gl) { @autoreleasepool { - IAvnPopup* ptr = dynamic_cast(new PopupImpl(events)); + IAvnPopup* ptr = dynamic_cast(new PopupImpl(events, gl)); return ptr; } } -extern IAvnWindow* CreateAvnWindow(IAvnWindowEvents*events) +extern IAvnWindow* CreateAvnWindow(IAvnWindowEvents*events, IAvnGlContext* gl) { @autoreleasepool { - IAvnWindow* ptr = (IAvnWindow*)new WindowImpl(events); + IAvnWindow* ptr = (IAvnWindow*)new WindowImpl(events, gl); return ptr; } }