File: cocoawsclipboard.pas

package info (click to toggle)
lazarus 4.0%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 275,760 kB
  • sloc: pascal: 2,341,904; xml: 509,420; makefile: 348,726; cpp: 93,608; sh: 3,387; java: 609; perl: 297; sql: 222; ansic: 137
file content (476 lines) | stat: -rw-r--r-- 13,305 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
{ $Id: $}
{                  --------------------------------------------
                  cocoawsclipboard.pas  -  Cocoa internal classes
                  --------------------------------------------

 This unit contains the private classhierarchy for the Cocoa implemetations

 *****************************************************************************
  This file is part of the Lazarus Component Library (LCL)

  See the file COPYING.modifiedLGPL.txt, included in this distribution,
  for details about the license.
 *****************************************************************************
}
unit CocoaWSClipboard;

interface

{$mode objfpc}{$H+}
{$modeswitch objectivec2}

uses
  CocoaAll, Classes, SysUtils, contnrs
  // fcl-image
  ,fpreadpng, fpwritepng, fpimage, fpreadbmp, fpwritebmp
  ,LCLType
  ,CocoaUtils, Cocoa_Extra;

type
  TCocoaClipboardDataType = (ccdtText,
    ccdtCocoaStandard, // Formats supported natively by Mac OS X
    ccdtBitmap,     // BMPs need conversion to PNG to work with other Mac OS X apps
    ccdtNonStandard { Formats that will only work in LCL apps } );

  TCocoaClipboardData = class(TObject) // TClipboardFormat is a reference to a TClipboardData
  public
    MimeType: string;
    CocoaFormat: NSString;  // utilized for ccdtCocoaStandard and ccdtNonStandard
    DataType: TCocoaClipboardDataType;
    constructor Create(AMimeType: string; ACocoaFormat: NSString; ADataType: TCocoaClipboardDataType);
    destructor Destroy; override;
  end;

  TCocoaPasteboardsRef = record
    pasteboard : NSPasteboard;
    changeCount: NSInteger;
    dataProc   : TClipboardRequestEvent;
    isOwned    : Boolean;
  end;


  TDynClipboardFormatArray = array of TClipboardFormat;

  { TCocoaWSClipboard }

  TCocoaWSClipboard = class(TObject)
  public
    PrimarySelection: NSPasteboard;
    SecondarySelection: NSPasteboard;
    ClipboardFormats: TFPObjectList; // of TCocoaClipboardData
    Pasteboards: array [TClipboardType] of TCocoaPasteboardsRef;

    constructor Create;
    destructor Destroy; override;

    procedure Sync;

    function FormatToMimeType(FormatID: TClipboardFormat): string;
    function GetData(ClipboardType: TClipboardType;
      FormatID: TClipboardFormat; Stream: TStream): boolean;
    function GetFormats(ClipboardType: TClipboardType;
      var Count: integer; var List: TDynClipboardFormatArray): boolean;
    function GetOwnership(ClipboardType: TClipboardType;
      OnRequestProc: TClipboardRequestEvent; FormatCount: integer;
      Formats: PClipboardFormat): boolean;
    function RegisterFormat(const AMimeType: string): TClipboardFormat;

    function GetClipboardDataForFormat(AFormat: TClipboardFormat): TCocoaClipboardData;
    function RegisterCocoaType(AType: NSString): TClipboardFormat;
    function CocoaTypeToMimeType(AType: NSString): string;
  end;


implementation

{ TCocoaWSClipboard }

constructor TCocoaWSClipboard.Create;
var
  t : TClipboardType;
begin
  inherited Create;
  PrimarySelection := NSPasteboard.pasteboardWithUniqueName();
  SecondarySelection := NSPasteboard.pasteboardWithUniqueName();
  ClipboardFormats := TFPObjectList.Create(True);

  Pasteboards[ctPrimarySelection].pasteboard := PrimarySelection;
  Pasteboards[ctSecondarySelection].pasteboard := SecondarySelection;
  Pasteboards[ctClipboard].pasteboard := NSPasteboard.generalPasteboard;

  for t := Low(TClipboardType) to High(TClipboardType) do
    if Assigned(Pasteboards[t].pasteboard) then
      Pasteboards[t].changeCount := Pasteboards[t].pasteboard.changeCount;
end;

destructor TCocoaWSClipboard.Destroy;
begin
  PrimarySelection.releaseGlobally();
  SecondarySelection.releaseGlobally();
  ClipboardFormats.Free;
  inherited;
end;

procedure TCocoaWSClipboard.Sync;
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 TCocoaWSClipboard.FormatToMimeType(FormatID: TClipboardFormat
  ): string;
var
  lFormat: TCocoaClipboardData;
begin
  lFormat := GetClipboardDataForFormat(FormatID);
  if lFormat = nil then Result := ''
  else Result := lFormat.MimeType;
end;

function TCocoaWSClipboard.GetClipboardDataForFormat(AFormat: TClipboardFormat
  ): TCocoaClipboardData;
begin
  Result := TCocoaClipboardData(AFormat);
end;

function PasteboardToPngData(pb: NSPasteboard): NSData;
var
  rep  : NSBitmapImageRep;
begin
  rep := NSBitmapImageRep.imageRepWithPasteboard(pb);
  if Assigned(rep) then
    Result := rep.representationUsingType_properties(NSPNGFileType, nil)
  else
    Result := nil;
end;

function TCocoaWSClipboard.GetData(ClipboardType: TClipboardType;
  FormatID: TClipboardFormat; Stream: TStream): boolean;
var
  pasteboard: NSPasteboard;
  lFormat: TCocoaClipboardData;
  lNSStr: NSString;
  // for text
  lStr: String;
  // for standard
  lNSData: NSData;
  lNSbytes: PByte;
  i: Integer;
  // for bitmap
  image: TFPCustomImage;
  lTmpStream: TMemoryStream;
  reader: TFPCustomImageReader;
  writer: TFPCustomImageWriter;
begin
  Result := False;
  pasteboard := Pasteboards[ClipboardType].pasteboard;

  lFormat := GetClipboardDataForFormat(FormatID);
  if lFormat = nil then Exit;

  case lFormat.DataType of
  ccdtText:
  begin
    lNSStr := pasteboard.stringForType(lFormat.CocoaFormat);
    if lNSStr = nil then Exit;
    lStr := NSStringToString(lNSStr);
    Stream.Write(lStr[1], Length(lStr));
    {$IFDEF VerboseClipboard}
      DebugLn('TCocoaWidgetSet.ClipboardGetData IsText Result=' + lStr);
    {$ENDIF}
  end;
  ccdtCocoaStandard, ccdtNonStandard:
  begin
    lNSData := pasteboard.dataForType(lFormat.CocoaFormat);
    if lNSData = nil then Exit;
    lNSbytes := lNSData.bytes;
    for i := 0 to lNSData.length-1 do
      Stream.WriteByte(lNSbytes[i]);
  end;
  // In Cocoa images are stored as PNG, convert to BMP for LCL app usage
  ccdtBitmap:
  begin
    lNSData := pasteboard.dataForType(lFormat.CocoaFormat);
    if lNSData = nil then begin
      lNSData := PasteboardToPngData(pasteboard);
      if not Assigned(lNSData) then Exit;
    end;
    lNSbytes := lNSData.bytes;

    Image := TFPMemoryImage.Create(10, 10);
    Reader := TFPReaderPNG.Create;
    Writer := TFPWriterBMP.Create;
    lTmpStream := TMemoryStream.Create;
    try
      for i := 0 to lNSData.length-1 do
        lTmpStream.WriteByte(lNSbytes[i]);
      lTmpStream.Position := 0;

      Image.LoadFromStream(lTmpStream, Reader);
      Image.SaveToStream(Stream, Writer);
    finally
      Image.Free;
      Reader.Free;
      Writer.Free;
      lTmpStream.Free;
    end;
  end;
  end;

  Result := True;
end;

function TCocoaWSClipboard.GetFormats(ClipboardType: TClipboardType;
  var Count: integer; var List: TDynClipboardFormatArray): boolean;
var
  i: Integer;
  pb : NSPasteboard;
  tp : NSString;
  hasBmp: Boolean;
  hasImage: Boolean; // this a default macOS sharing format
  imgArr: NSArray;
const
  ImgBmpFmt : string = 'image/bmp';
begin
  pb := Pasteboards[ClipboardType].pasteboard;
  Result := Assigned(pb);
  if not Result then Exit;

  i := 0;
  SetLength(List, pb.types.count);
  hasImage := false;
  hasBmp := false;
  imgArr := NSBitmapImageRep.imagePasteboardTypes;

  for tp in pb.types do
  begin
    List[i]:=RegisterCocoaType(tp);
    inc(i);
    if not hasImage and Assigned(imgArr) then
    begin
      hasImage := (imgArr.indexOfObject(tp) <> NSNotFound);
    end;
    if (tp.UTF8String = ImgBmpFmt) then
      hasBmp := true;
  end;

  if hasImage and not hasBmp then
  begin
    inc(i);
    SetLength(List, i);
    List[i-1] := RegisterFormat(ImgBmpFmt);
  end;

  Count := i;
end;

function TCocoaWSClipboard.GetOwnership(ClipboardType: TClipboardType;
  OnRequestProc: TClipboardRequestEvent; FormatCount: integer;
  Formats: PClipboardFormat): boolean;
var
  pasteboard: NSPasteboard;
  i: Integer;
  lCurFormat: TCocoaClipboardData;
  DataStream: TMemoryStream;
  FormatToOwn: NSString;
  FormatToOwnArray: NSArray;
  // text format
  lText: string;
  lNSText: NSString;
  // non-text
  lNSData: NSData;
  // for bitmap
  image: TFPCustomImage;
  lTmpStream: TMemoryStream;
  reader: TFPCustomImageReader;
  writer: TFPCustomImageWriter;
begin
  pasteboard := Pasteboards[ClipboardType].pasteboard;

  DataStream := TMemoryStream.Create;
  try
    FormatToOwnArray := nil;
    if FormatCount>0 then
    begin
      FormatToOwnArray := NSArray(NSMutableArray.array_);
      for i := 0 to FormatCount-1 do
      begin
        lCurFormat := TCocoaClipboardData(Formats[i]);
        if lCurFormat = nil then Continue;
        FormatToOwn := lCurFormat.CocoaFormat;
        NSMutableArray(FormatToOwnArray).addObject(FormatToOwn);
      end;
    end;

    if Assigned(FormatToOwnArray) and (FormatToOwnArray.count>0) then
      pasteboard.declareTypes_owner(FormatToOwnArray, nil);

    for i := 0 to FormatCount-1 do
    begin
      lCurFormat := TCocoaClipboardData(Formats[i]);
      if lCurFormat = nil then Continue;
      DataStream.Position := 0;
      DataStream.Size := 0;
      OnRequestProc(Formats[i], DataStream);

      case lCurFormat.DataType of
      ccdtText:
      begin
        DataStream.Position := 0;
        SetLength(lText, DataStream.Size);
        if DataStream.Size > 0 then
          DataStream.Read(lText[1], DataStream.Size);
        lNSText := NSStringUtf8(lText);

        pasteboard.setString_forType(lNSText, lCurFormat.CocoaFormat);
        lNsText.release;
      end;
      ccdtCocoaStandard, ccdtNonStandard:
      begin
        DataStream.Position := 0;
        lNSData := NSData.dataWithBytes_length(DataStream.Memory, DataStream.Size);

        pasteboard.setData_forType(lNSData, lCurFormat.CocoaFormat);
      end;
      ccdtBitmap:
      begin
        Image := TFPMemoryImage.Create(10, 10);
        Reader := TFPReaderBMP.Create;
        Writer := TFPWriterPNG.Create;
        lTmpStream := TMemoryStream.Create;
        try
          DataStream.Position := 0;
          Image.LoadFromStream(DataStream, Reader);
          Image.SaveToStream(lTmpStream, Writer);
          lTmpStream.Position := 0;
          lNSData := NSData.dataWithBytes_length(lTmpStream.Memory, lTmpStream.Size);
          pasteboard.setData_forType(lNSData, lCurFormat.CocoaFormat);
        finally
          Image.Free;
          Reader.Free;
          Writer.Free;
          lTmpStream.Free;
        end;
      end;
      end;
    end;
  finally
    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;

function TCocoaWSClipboard.RegisterFormat(const AMimeType: string
  ): TClipboardFormat;
var
  i: Integer;
  lCurData: TCocoaClipboardData;
  lNSStr: NSString = nil;
  lDataType: TCocoaClipboardDataType;
begin
  Result := 0;

  // Check first if it was already registered
  for i := 0 to ClipboardFormats.Count-1 do
  begin
    lCurData := TCocoaClipboardData(ClipboardFormats.Items[i]);
    if lCurData.MimeType = AMimeType then
    begin
      Result := TClipboardFormat(lCurData);
      {$IFDEF VerboseClipboard}
        DebugLn('TCocoaWidgetSet.ClipboardRegisterFormat AMimeType=' + AMimeType
          + ' Result='+DbgS(Result));
      {$ENDIF}
      Exit;
    end;
  end;

  // if none was found, we need to register it

  lDataType := ccdtNonStandard;
  // See PredefinedClipboardMimeTypes for the most common mime-types
  case AMimeType of
  'text/plain':
  begin
    lNSStr := NSPasteboardTypeString;
    lDataType := ccdtText;
  end;
  'image/png':
  begin
    lNSStr := NSPasteboardTypePNG;
    lDataType := ccdtCocoaStandard;
  end;
  'image/bmp':
  begin
    lNSStr := NSPasteboardTypePNG;
    lDataType := ccdtBitmap;
  end;
  else
    lNSStr := NSStringUtf8(AMimeType);
    lDataType := ccdtNonStandard;
  end;

  if lNSStr <> nil then
  begin
    lCurData := TCocoaClipboardData.Create(AMimeType, lNSStr, lDataType);
    ClipboardFormats.Add(lCurData);
    Result := TClipboardFormat(lCurData);
  end;
end;

function TCocoaWSClipboard.RegisterCocoaType(AType: NSString): TClipboardFormat;
begin
  Result := RegisterFormat( CocoaTypeToMimeType(AType) );
end;

function TCocoaWSClipboard.CocoaTypeToMimeType(AType: NSString): string;
begin
  // "default" types must be mapped to a default LCL mime-type
  if AType.isEqualToString(NSPasteboardTypeString) then
    Result := 'text/plain'
  else
    Result := NSStringToString(AType);
end;

{ TCocoaClipboardData }

constructor TCocoaClipboardData.Create(AMimeType: string; ACocoaFormat: NSString; ADataType: TCocoaClipboardDataType);
begin
  MimeType := AMimeType;
  CocoaFormat := ACocoaFormat;
  DataType := ADataType;
end;

destructor TCocoaClipboardData.Destroy;
begin
  CocoaFormat.release;
end;

end.