diff --git a/lcl/interfaces/cocoa/cocoaint.pas b/lcl/interfaces/cocoa/cocoaint.pas index 4637fa7341..f70985bf6e 100644 --- a/lcl/interfaces/cocoa/cocoaint.pas +++ b/lcl/interfaces/cocoa/cocoaint.pas @@ -85,6 +85,13 @@ type procedure sendEvent(theEvent: NSEvent); override; end; + TCocoaPasteboardsRef = record + pasteboard : NSPasteboard; + changeCount: NSInteger; + dataProc : TClipboardRequestEvent; + isOwned : Boolean; + end; + { TCocoaWidgetSet } TCocoaWidgetSet = class(TWidgetSet) @@ -118,9 +125,12 @@ type PrimarySelection: NSPasteboard; SecondarySelection: NSPasteboard; ClipboardFormats: TFPObjectList; // of TCocoaClipboardData + ClipboardChangeCount: NSInteger; + Pasteboards: array [TClipboardType] of TCocoaPasteboardsRef; procedure InitClipboard(); procedure FreeClipboard(); + procedure SyncClipboard(); function GetClipboardDataForFormat(AFormat: TClipboardFormat): TCocoaClipboardData; function PromptUser(const DialogCaption, DialogMessage: String; diff --git a/lcl/interfaces/cocoa/cocoalclintf.inc b/lcl/interfaces/cocoa/cocoalclintf.inc index 0f763c3a20..d6cc6ac759 100644 --- a/lcl/interfaces/cocoa/cocoalclintf.inc +++ b/lcl/interfaces/cocoa/cocoalclintf.inc @@ -163,6 +163,27 @@ begin ClipboardFormats.Free; end; +procedure TCocoaWidgetSet.SyncClipboard(); +var + ct : TClipboardType; + pb : NSPasteboard; +begin + for ct := low(TClipboardType) to high(TClipboardType) do begin + if not Pasteboards[ct].isOwned then Continue; + + pb := Pasteboards[ct].pasteboard; + if not Assigned(pb) then Continue; + + if (pb.changeCount <> Pasteboards[ct].changeCount) then + begin + Pasteboards[ct].isOwned:=false; + if Assigned(Pasteboards[ct].dataProc) then + // notifying about the loss of ownership + Pasteboards[ct].dataProc(0, nil); + end; + end; +end; + function TCocoaWidgetSet.GetClipboardDataForFormat(AFormat: TClipboardFormat): TCocoaClipboardData; begin Result := TCocoaClipboardData(AFormat); diff --git a/lcl/interfaces/cocoa/cocoaobject.inc b/lcl/interfaces/cocoa/cocoaobject.inc index 1f64ea7cdf..2e2b9cd3d4 100644 --- a/lcl/interfaces/cocoa/cocoaobject.inc +++ b/lcl/interfaces/cocoa/cocoaobject.inc @@ -106,6 +106,10 @@ begin NSApp.sendEvent(event); NSApp.updateWindows; end; + + SyncClipboard(); // NSPasteboard doesn't provide any notifications regarding the change + // Thus we have to check the clipboard on every loop + pool.release; until event = nil; end; @@ -130,6 +134,9 @@ begin NSApp.sendEvent(event); NSApp.updateWindows; end; + + SyncClipboard(); // NSPasteboard doesn't provide any notifications regarding the change + // Thus we have to check the clipboard on every loop pool.release; end; diff --git a/lcl/interfaces/cocoa/cocoawinapi.inc b/lcl/interfaces/cocoa/cocoawinapi.inc index b8c5d90a5a..a4f412a933 100644 --- a/lcl/interfaces/cocoa/cocoawinapi.inc +++ b/lcl/interfaces/cocoa/cocoawinapi.inc @@ -361,6 +361,16 @@ begin DataStream.Free; end; + Pasteboards[ClipboardType].pasteboard:=pasteboard; + Pasteboards[ClipboardType].dataProc:=OnRequestProc; + if Assigned(pasteboard) then + begin + Pasteboards[ClipboardType].changeCount:=pasteboard.changeCount; + Pasteboards[ClipboardType].isOwned:=true; + end else + Pasteboards[ClipboardType].isOwned:=false; + + Result := True; end;