This repository was archived by the owner on Aug 31, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 227
Expand file tree
/
Copy pathclipboard.cpp
More file actions
1579 lines (1286 loc) · 49.1 KB
/
clipboard.cpp
File metadata and controls
1579 lines (1286 loc) · 49.1 KB
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
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* Copyright (C) 2015 LiveCode Ltd.
This file is part of LiveCode.
LiveCode is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License v3 as published by the Free
Software Foundation.
LiveCode is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
You should have received a copy of the GNU General Public License
along with LiveCode. If not see <http://www.gnu.org/licenses/>. */
#include "clipboard.h"
#include "foundation-auto.h"
#include "imagebitmap.h"
#include "paragraf.h"
#include "object.h"
#include "stack.h"
#include "styledtext.h"
#include "globals.h"
class MCClipboard::AutoLock
{
public:
inline AutoLock(const MCClipboard* p_clipboard) :
m_clipboard(p_clipboard)
{
m_clipboard->Lock();
}
inline ~AutoLock()
{
m_clipboard->Unlock();
}
private:
const MCClipboard* m_clipboard;
};
MCClipboard::MCClipboard(MCRawClipboard* p_clipboard) :
m_clipboard(p_clipboard),
m_lock_count(0),
m_dirty(false)
{
;
}
bool MCClipboard::IsOwned() const
{
return m_clipboard->IsOwned();
}
bool MCClipboard::Lock(bool p_skip_pull) const
{
// Increase the lock count. If it moves from zero to one, update the
// contents of this clipboard.
//
// TODO: atomic operations
if (m_lock_count++ == 0 && !p_skip_pull)
{
return PullUpdates();
}
return true;
}
bool MCClipboard::Unlock() const
{
MCAssert(m_lock_count > 0);
// Decrement the lock count. If it moves from one to zero, push any changes
// out to the underlying OS clipboard.
//
// TODO: atomic operations
if (--m_lock_count == 0)
{
return PushUpdates();
}
return true;
}
bool MCClipboard::IsLocked() const
{
return m_lock_count != 0;
}
void MCClipboard::Clear()
{
// Clear the private data
ClearPrivateData();
// Pass on the request
Lock();
m_clipboard->Clear();
m_dirty = true;
Unlock();
}
void MCClipboard::ReleaseData()
{
// Clear any external data on the clipboard
if (m_clipboard->IsExternalData())
m_clipboard->Clear();
}
bool MCClipboard::PullUpdates() const
{
// If ownership has changed, the private clipboard data needs to be cleared
if (!this->IsDragboard() && !m_clipboard->IsOwned())
const_cast<MCClipboard*>(this)->ClearPrivateData();
// Pass on the request
return m_clipboard->PullUpdates();
}
bool MCClipboard::PushUpdates(bool p_force) const
{
// Pass on the request
if (m_dirty || p_force)
{
const_cast<MCClipboard*>(this)->m_dirty = false;
return m_clipboard->PushUpdates();
}
return true;
}
void MCClipboard::FlushData()
{
// Pass on the request
m_clipboard->FlushData();
}
MCRawClipboard* MCClipboard::GetRawClipboard()
{
return m_clipboard;
}
const MCRawClipboard* MCClipboard::GetRawClipboard() const
{
return m_clipboard;
}
bool MCClipboard::Rebind(MCRawClipboard* p_clipboard)
{
// Change the underlying clipboard
m_clipboard = p_clipboard->Retain();
return true;
}
bool MCClipboard::IsEmpty() const
{
return m_clipboard->GetItemCount() == 0 && !HasPrivateData();
}
bool MCClipboard::AddFileList(MCStringRef p_file_names)
{
AutoLock t_lock(this);
// Clear contents if the clipboard contains external data
if (m_clipboard->IsExternalData())
Clear();
// Does the clipboard support multiple items?
if (m_clipboard->SupportsMultipleItems())
{
// Split the file name list into individual paths
MCAutoArrayRef t_paths;
if (!MCStringSplit(p_file_names, MCSTR("\n"), NULL, kMCStringOptionCompareExact, &t_paths))
return false;
// For each path, add a new item to the clipboard
for (uindex_t i = 0; i < MCArrayGetCount(*t_paths); i++)
{
// Get the existing item for this index or create a new one
MCAutoRefcounted<MCRawClipboardItem> t_item;
if ((t_item = m_clipboard->GetItemAtIndex(i)) == NULL)
{
// No item yet; create a new one and add it to the clipboard
t_item = m_clipboard->CreateNewItem();
if (t_item == NULL)
return false;
if (!m_clipboard->AddItem(t_item))
return false;
}
// Get this path
MCValueRef t_path;
if (!MCArrayFetchValueAtIndex(*t_paths, index_t(i+1), t_path))
return false;
// Sanity check - should always be a string!
if (MCValueGetTypeCode(t_path) != kMCValueTypeCodeString)
return false;
// Encode it appropriately for this clipboard
MCAutoDataRef t_encoded_path;
t_encoded_path.Give(m_clipboard->EncodeFileListForTransfer((MCStringRef)t_path));
if (*t_encoded_path == NULL)
return false;
// Add a representation to this item containing the path
if (!t_item->AddRepresentation(m_clipboard->GetKnownTypeString(kMCRawClipboardKnownTypeFileURL), *t_encoded_path))
return false;
// The first item on the list also receives a text representation
// containing the paths that were added (unless the user has already
// added a text representation).
if (i == 0 && !HasText())
if (!AddTextToItem(t_item, p_file_names))
return false;
}
}
else
{
// Convert the file list into the correct format
MCAutoDataRef t_encoded_list;
t_encoded_list.Give(m_clipboard->EncodeFileListForTransfer(p_file_names));
if (*t_encoded_list == NULL)
return false;
// Get the first item on the clipboard
MCAutoRefcounted<MCRawClipboardItem> t_item = GetItem();
if (t_item == NULL)
return false;
// And add the representation to it
if (!t_item->AddRepresentation(m_clipboard->GetKnownTypeString(kMCRawClipboardKnownTypeFileURLList), *t_encoded_list))
return false;
// Add the original list as a text representation as well (unless one
// already exists)
if (!HasText() && !AddTextToItem(t_item, p_file_names))
return false;
}
// Done!
return true;
}
bool MCClipboard::AddText(MCStringRef p_string)
{
// [Bug 19206] Converting text to styled text was causing
// presentation issues when attempting to paste into other
// applications due to the HTML format being included.
// Only add the plain text representations to the clipboard.
AutoLock t_lock(this);
// Clear contents if the clipboard contains external data
if (m_clipboard->IsExternalData())
Clear();
// Clear contents to ensure that the clipboard does not end up
// containing data from different copy events.
// i.e. the HTML/RTF representation would be left untouched
if (HasLiveCodeStyledTextOrCompatible())
{
m_clipboard->Clear();
}
// Get the first item on the clipboard
MCAutoRefcounted<MCRawClipboardItem> t_item = GetItem();
if (t_item == NULL)
return false;
return AddTextToItem(t_item, p_string);
}
bool MCClipboard::AddTextToItem(MCRawClipboardItem* p_item, MCStringRef p_string)
{
// For each text encoding that the underlying clipboard supports, encode the
// text and add it.
bool t_success = true;
MCStringRef t_type_string;
if (t_success && (t_type_string = m_clipboard->GetKnownTypeString(kMCRawClipboardKnownTypeUTF8)) != NULL)
{
// UTF-8
MCAutoDataRef t_utf8;
t_success = MCStringEncode(p_string, kMCStringEncodingUTF8, false, &t_utf8);
if (t_success)
t_success = p_item->AddRepresentation(t_type_string, *t_utf8);
}
if (t_success && (t_type_string = m_clipboard->GetKnownTypeString(kMCRawClipboardKnownTypeUTF16)) != NULL)
{
// UTF-16
MCAutoDataRef t_utf16;
t_success = MCStringEncode(p_string, kMCStringEncodingUTF16, false, &t_utf16);
if (t_success)
t_success = p_item->AddRepresentation(t_type_string, *t_utf16);
}
if (t_success && (t_type_string = m_clipboard->GetKnownTypeString(kMCRawClipboardKnownTypeISO8859_1)) != NULL)
{
// ISO-8859-1 (aka Latin1)
MCAutoDataRef t_latin1;
t_success = MCStringEncode(p_string, kMCStringEncodingISO8859_1, false, &t_latin1);
if (t_success)
t_success = p_item->AddRepresentation(t_type_string, *t_latin1);
}
if (t_success && (t_type_string = m_clipboard->GetKnownTypeString(kMCRawClipboardKnownTypeMacRoman)) != NULL)
{
// MacRoman
MCAutoDataRef t_macroman;
t_success = MCStringEncode(p_string, kMCStringEncodingMacRoman, false, &t_macroman);
if (t_success)
t_success = p_item->AddRepresentation(t_type_string, *t_macroman);
}
if (t_success && (t_type_string = m_clipboard->GetKnownTypeString(kMCRawClipboardKnownTypeCP1252)) != NULL)
{
// Windows codepage 1252 (Western Europe)
MCAutoDataRef t_1252;
t_success = MCStringEncode(p_string, kMCStringEncodingWindows1252, false, &t_1252);
if (t_success)
t_success = p_item->AddRepresentation(t_type_string, *t_1252);
}
return t_success;
}
bool MCClipboard::AddLiveCodeObjects(MCDataRef p_pickled_objects)
{
AutoLock t_lock(this);
// Clear contents if the clipboard contains external data
if (m_clipboard->IsExternalData())
Clear();
// Get the first item on the clipboard
MCAutoRefcounted<MCRawClipboardItem> t_item = GetItem();
if (t_item == NULL)
return false;
// Add the objects to the clipboard under the correct type
MCStringRef t_type_string = m_clipboard->GetKnownTypeString(kMCRawClipboardKnownTypeLiveCodeObjects);
if (t_type_string == NULL)
return false;
return t_item->AddRepresentation(t_type_string, p_pickled_objects);
}
bool MCClipboard::AddLiveCodeStyledText(MCDataRef p_pickled_text)
{
AutoLock t_lock(this);
// Clear contents if the clipboard contains external data
if (m_clipboard->IsExternalData())
Clear();
// Get the first item on the clipboard
MCAutoRefcounted<MCRawClipboardItem> t_item = GetItem();
if (t_item == NULL)
return false;
// Styled text can be presented in various forms; try each of these.
bool t_success = true;
MCStringRef t_type_string;
if (t_success && (t_type_string = m_clipboard->GetKnownTypeString(kMCRawClipboardKnownTypeLiveCodeStyledText)) != NULL)
{
t_success = t_item->AddRepresentation(t_type_string, p_pickled_text);
}
if (t_success && (t_type_string = m_clipboard->GetKnownTypeString(kMCRawClipboardKnownTypeRTF)) != NULL)
{
// This type is optional as it may not be a faithful representation
MCAutoDataRef t_rtf;
t_rtf.Give(ConvertStyledTextToRTF(p_pickled_text));
if (*t_rtf != NULL)
t_success = t_item->AddRepresentation(t_type_string, *t_rtf);
}
if (t_success && (t_type_string = m_clipboard->GetKnownTypeString(kMCRawClipboardKnownTypeHTML)) != NULL)
{
// This type is optional as it may not be a faithful representation
MCAutoStringRef t_html;
t_html.Give(ConvertStyledTextToHTML(p_pickled_text));
if (*t_html != NULL)
{
// All platforms accept UTF-8 as an encoding for HTML on the clipboard
MCAutoDataRef t_html_utf8;
t_success = MCStringEncode(*t_html, kMCStringEncodingUTF8, false, &t_html_utf8);
if (t_success)
{
MCAutoDataRef t_encoded;
t_encoded.Give(m_clipboard->EncodeHTMLFragmentForTransfer(*t_html_utf8));
t_success = *t_encoded != nil;
if (t_success)
t_success = t_item->AddRepresentation(t_type_string, *t_encoded);
}
}
}
// Also attempt to add as plain text, so we have a fall-back
if (t_success)
{
// This type is optional as it may not be a faithful representation
MCAutoStringRef t_text;
t_text.Give(ConvertStyledTextToText(p_pickled_text));
if (*t_text != NULL)
t_success = AddTextToItem(t_item, *t_text);
}
return t_success;
}
bool MCClipboard::AddLiveCodeStyledTextArray(MCArrayRef p_styled_text)
{
// Convert the styled text array to serialised styled text and add that way.
MCAutoDataRef t_pickled_text;
t_pickled_text.Give(ConvertStyledTextArrayToStyledText(p_styled_text));
if (*t_pickled_text == NULL)
return false;
return AddLiveCodeStyledText(*t_pickled_text);
}
bool MCClipboard::AddHTMLText(MCStringRef p_html_string)
{
// Adding HTML to the clipboard is done by converting the HTML to LiveCode's
// internal styled-text format and then adding it to the clipboard in that
// format.
//
// This is a lossy process but preserves legacy behaviour.
MCAutoDataRef t_pickled_text;
t_pickled_text.Give(ConvertHTMLToStyledText(p_html_string));
if (*t_pickled_text == NULL)
return false;
return AddLiveCodeStyledText(*t_pickled_text);
}
bool MCClipboard::AddRTFText(MCDataRef p_rtf_data)
{
// Adding RTF to the clipboard is done by converting the RTF to LiveCode's
// internal styled-text format and then adding it to the clipboard in that
// format.
//
// This is a lossy process but preserves legacy behaviour.
MCAutoDataRef t_pickled_text;
t_pickled_text.Give(ConvertRTFToStyledText(p_rtf_data));
if (*t_pickled_text == NULL)
return false;
return AddLiveCodeStyledText(*t_pickled_text);
}
bool MCClipboard::AddRTF(MCDataRef p_rtf)
{
AutoLock t_lock(this);
// Clear the contents if the clipboard contains external data
if (m_clipboard->IsExternalData())
Clear();
// Get the first item on the clipboard
MCAutoRefcounted<MCRawClipboardItem> t_item = GetItem();
if (t_item == NULL)
return false;
// Add the data to the clipboard with the correct type
MCStringRef t_type_string = m_clipboard->GetKnownTypeString(kMCRawClipboardKnownTypeRTF);
if (t_type_string == NULL)
return false;
return t_item->AddRepresentation(t_type_string, p_rtf);
}
bool MCClipboard::AddHTML(MCStringRef p_html)
{
AutoLock t_lock(this);
// All platforms support UTF-8 as a text encoding for HTML on the clipboard
MCAutoDataRef t_html_utf8;
if (!MCStringEncode(p_html, kMCStringEncodingUTF8, false, &t_html_utf8))
return false;
// Clear the contents if the clipboard contains external data
if (m_clipboard->IsExternalData())
Clear();
// Get the first item on the clipboard
MCAutoRefcounted<MCRawClipboardItem> t_item = GetItem();
if (t_item == NULL)
return false;
// Encode the HTML in the required format for the clipboard
MCAutoDataRef t_encoded;
t_encoded.Give(m_clipboard->EncodeHTMLFragmentForTransfer(*t_html_utf8));
if (*t_encoded == nil)
return false;
// Add the data to the clipboard with the correct type
MCStringRef t_type_string = m_clipboard->GetKnownTypeString(kMCRawClipboardKnownTypeHTML);
if (t_type_string == NULL)
return false;
return t_item->AddRepresentation(t_type_string, *t_encoded);
}
bool MCClipboard::AddPNG(MCDataRef p_png)
{
AutoLock t_lock(this);
// Clear contents if the clipboard contains external data
if (m_clipboard->IsExternalData())
Clear();
// Get the first item on the clipboard
MCAutoRefcounted<MCRawClipboardItem> t_item = GetItem();
if (t_item == NULL)
return false;
// Add the objects to the clipboard under the correct type
MCStringRef t_type_string = m_clipboard->GetKnownTypeString(kMCRawClipboardKnownTypePNG);
if (t_type_string == NULL)
return false;
return t_item->AddRepresentation(t_type_string, p_png);
}
bool MCClipboard::AddGIF(MCDataRef p_gif)
{
AutoLock t_lock(this);
// Clear contents if the clipboard contains external data
if (m_clipboard->IsExternalData())
Clear();
// Get the first item on the clipboard
MCAutoRefcounted<MCRawClipboardItem> t_item = GetItem();
if (t_item == NULL)
return false;
// Add the objects to the clipboard under the correct type
MCStringRef t_type_string = m_clipboard->GetKnownTypeString(kMCRawClipboardKnownTypeGIF);
if (t_type_string == NULL)
return false;
return t_item->AddRepresentation(t_type_string, p_gif);
}
bool MCClipboard::AddJPEG(MCDataRef p_jpeg)
{
AutoLock t_lock(this);
// Clear contents if the clipboard contains external data
if (m_clipboard->IsExternalData())
Clear();
// Get the first item on the clipboard
MCAutoRefcounted<MCRawClipboardItem> t_item = GetItem();
if (t_item == NULL)
return false;
// Add the objects to the clipboard under the correct type
MCStringRef t_type_string = m_clipboard->GetKnownTypeString(kMCRawClipboardKnownTypeJPEG);
if (t_type_string == NULL)
return false;
return t_item->AddRepresentation(t_type_string, p_jpeg);
}
bool MCClipboard::AddBMP(MCDataRef p_bmp)
{
AutoLock t_lock(this);
// Clear contents if the clipboard contains external data
if (m_clipboard->IsExternalData())
Clear();
// Get the first item on the clipboard
MCAutoRefcounted<MCRawClipboardItem> t_item = GetItem();
if (t_item == NULL)
return false;
// Add the objects to the clipboard under the correct type
MCStringRef t_type_string = m_clipboard->GetKnownTypeString(kMCRawClipboardKnownTypeWinDIBv5);
if (t_type_string == NULL)
return false;
// Encode the BMP for transfer
MCAutoDataRef t_bmp(m_clipboard->EncodeBMPForTransfer(p_bmp));
return t_item->AddRepresentation(t_type_string, *t_bmp);
}
bool MCClipboard::AddWinMetafile(MCDataRef p_wmf)
{
AutoLock t_lock(this);
// Clear contents if the clipboard contains external data
if (m_clipboard->IsExternalData())
Clear();
// Get the first item on the clipboard
MCAutoRefcounted<MCRawClipboardItem> t_item = GetItem();
if (t_item == NULL)
return false;
// Add the objects to the clipboard under the correct type
MCStringRef t_type_string = m_clipboard->GetKnownTypeString(kMCRawClipboardKnownTypeWinMF);
if (t_type_string == NULL)
return false;
return t_item->AddRepresentation(t_type_string, p_wmf);
}
bool MCClipboard::AddWinEnhMetafile(MCDataRef p_emf)
{
AutoLock t_lock(this);
// Clear contents if the clipboard contains external data
if (m_clipboard->IsExternalData())
Clear();
// Get the first item on the clipboard
MCAutoRefcounted<MCRawClipboardItem> t_item = GetItem();
if (t_item == NULL)
return false;
// Add the objects to the clipboard under the correct type
MCStringRef t_type_string = m_clipboard->GetKnownTypeString(kMCRawClipboardKnownTypeWinEMF);
if (t_type_string == NULL)
return false;
return t_item->AddRepresentation(t_type_string, p_emf);
}
bool MCClipboard::AddImage(MCDataRef p_image_data)
{
// Examine the data to see if it matches any of the formats that we handle
if (MCImageDataIsPNG(p_image_data))
return AddPNG(p_image_data);
if (MCImageDataIsGIF(p_image_data))
return AddGIF(p_image_data);
if (MCImageDataIsJPEG(p_image_data))
return AddJPEG(p_image_data);
if (MCImageDataIsBMP(p_image_data))
return AddBMP(p_image_data);
return false;
}
bool MCClipboard::AddPrivateData(MCDataRef p_private_data)
{
// Clear contents if the clipboard contains external data
if (m_clipboard->IsExternalData())
Clear();
// Update the stored private data
m_private_data.Reset(p_private_data);
// Ensure that an item is always on the clipboard, even if it is empty
if (m_clipboard->GetItemCount() == 0)
{
MCAutoRefcounted<MCRawClipboardItem> t_item = m_clipboard->CreateNewItem();
m_clipboard->AddItem(t_item);
}
return true;
}
bool MCClipboard::HasFileList() const
{
// A list of files can be returned as either multiple file URL items or a
// single file URL list item.
MCAutoRefcounted<const MCRawClipboardItem> t_item = GetItem();
if (t_item == NULL)
return false;
if (t_item->HasRepresentation(m_clipboard->GetKnownTypeString(kMCRawClipboardKnownTypeFileURL)))
return true;
if (t_item->HasRepresentation(m_clipboard->GetKnownTypeString(kMCRawClipboardKnownTypeFileURLList)))
return true;
return false;
}
bool MCClipboard::HasText() const
{
AutoLock t_lock(this);
// The clipboard has text if any of the known formats are present
MCAutoRefcounted<const MCRawClipboardItem> t_item = GetItem();
if (t_item == NULL)
return false;
if (t_item->HasRepresentation(m_clipboard->GetKnownTypeString(kMCRawClipboardKnownTypeUTF8)))
return true;
if (t_item->HasRepresentation(m_clipboard->GetKnownTypeString(kMCRawClipboardKnownTypeUTF16)))
return true;
if (t_item->HasRepresentation(m_clipboard->GetKnownTypeString(kMCRawClipboardKnownTypeISO8859_1)))
return true;
if (t_item->HasRepresentation(m_clipboard->GetKnownTypeString(kMCRawClipboardKnownTypeMacRoman)))
return true;
if (t_item->HasRepresentation(m_clipboard->GetKnownTypeString(kMCRawClipboardKnownTypeCP1252)))
return true;
return false;
}
bool MCClipboard::HasLiveCodeObjects() const
{
AutoLock t_lock(this);
// Check for the corresponding type
MCAutoRefcounted<const MCRawClipboardItem> t_item = GetItem();
if (t_item == NULL)
return false;
return t_item->HasRepresentation(m_clipboard->GetKnownTypeString(kMCRawClipboardKnownTypeLiveCodeObjects));
}
bool MCClipboard::HasLiveCodeStyledText() const
{
AutoLock t_lock(this);
// Check for the corresponding type
MCAutoRefcounted<const MCRawClipboardItem> t_item = GetItem();
if (t_item == NULL)
return false;
return t_item->HasRepresentation(m_clipboard->GetKnownTypeString(kMCRawClipboardKnownTypeLiveCodeStyledText));
}
bool MCClipboard::HasRTF() const
{
AutoLock t_lock(this);
// Check for the corresponding type
MCAutoRefcounted<const MCRawClipboardItem> t_item = GetItem();
if (t_item == NULL)
return false;
return t_item->HasRepresentation(m_clipboard->GetKnownTypeString(kMCRawClipboardKnownTypeRTF));
}
bool MCClipboard::HasHTML() const
{
AutoLock t_lock(this);
// Check for the corresponding type
MCAutoRefcounted<const MCRawClipboardItem> t_item = GetItem();
if (t_item == NULL)
return false;
return t_item->HasRepresentation(m_clipboard->GetKnownTypeString(kMCRawClipboardKnownTypeHTML));
}
bool MCClipboard::HasPNG() const
{
AutoLock t_lock(this);
// Check for the corresponding type
MCAutoRefcounted<const MCRawClipboardItem> t_item = GetItem();
if (t_item == NULL)
return false;
#if defined(__MAC__) && !defined(MODE_SERVER)
// Many Mac apps (like Preview) present things as TIFF on the clipboard. Since
// the engine doesn't currently understand TIFF generally we make TIFF data
// masquerade as PNG.
if (t_item -> HasRepresentation(m_clipboard->GetKnownTypeString(kMCRawClipboardKnownTypeTIFF)))
return true;
#endif
return t_item->HasRepresentation(m_clipboard->GetKnownTypeString(kMCRawClipboardKnownTypePNG));
}
bool MCClipboard::HasGIF() const
{
AutoLock t_lock(this);
// Check for the corresponding type
MCAutoRefcounted<const MCRawClipboardItem> t_item = GetItem();
if (t_item == NULL)
return false;
return t_item->HasRepresentation(m_clipboard->GetKnownTypeString(kMCRawClipboardKnownTypeGIF));
}
bool MCClipboard::HasJPEG() const
{
AutoLock t_lock(this);
// Check for the corresponding type
MCAutoRefcounted<const MCRawClipboardItem> t_item = GetItem();
if (t_item == NULL)
return false;
return t_item->HasRepresentation(m_clipboard->GetKnownTypeString(kMCRawClipboardKnownTypeJPEG));
}
bool MCClipboard::HasBMP() const
{
AutoLock t_lock(this);
// Check for the corresponding type
MCAutoRefcounted<const MCRawClipboardItem> t_item = GetItem();
if (t_item == NULL)
return false;
return t_item->HasRepresentation(m_clipboard->GetKnownTypeString(kMCRawClipboardKnownTypeWinDIB))
|| t_item->HasRepresentation(m_clipboard->GetKnownTypeString(kMCRawClipboardKnownTypeWinDIBv5));
}
bool MCClipboard::HasWinMetafile() const
{
AutoLock t_lock(this);
// Check for the corresponding type
MCAutoRefcounted<const MCRawClipboardItem> t_item = GetItem();
if (t_item == NULL)
return false;
return t_item->HasRepresentation(m_clipboard->GetKnownTypeString(kMCRawClipboardKnownTypeWinMF));
}
bool MCClipboard::HasWinEnhMetafile() const
{
AutoLock t_lock(this);
// Check for the corresponding type
MCAutoRefcounted<const MCRawClipboardItem> t_item = GetItem();
if (t_item == NULL)
return false;
return t_item->HasRepresentation(m_clipboard->GetKnownTypeString(kMCRawClipboardKnownTypeWinEMF));
}
bool MCClipboard::HasImage() const
{
// Images are any of PNG, GIF or JPEG
AutoLock t_lock(this);
return HasPNG() || HasGIF() || HasJPEG() || HasBMP();
}
bool MCClipboard::HasTextOrCompatible() const
{
// Styled text is compatible with plain text. A list of file paths can also
// be auto-converted into text.
AutoLock t_lock(this);
return HasText() || HasLiveCodeStyledTextOrCompatible() || HasFileList();
}
bool MCClipboard::HasLiveCodeStyledTextOrCompatible() const
{
// Note that plain text is *not* up-converted to styled text
AutoLock t_lock(this);
return HasLiveCodeStyledText() || HasRTF() || HasHTML();
}
bool MCClipboard::HasPrivateData() const
{
return m_private_data.IsSet();
}
bool MCClipboard::CopyAsFileList(MCStringRef& r_file_list) const
{
AutoLock t_lock(this);
// String containing a newline-separated list of file paths
MCStringRef t_output = NULL;
// If the clipboard supports multiple items, look for multiple "file URL"
// representations.
if (m_clipboard->SupportsMultipleItems())
{
// Accumulate the items into a list, separated by newlines
MCAutoListRef t_list;
if (!MCListCreateMutable('\n', &t_list))
return false;
for (uindex_t i = 0; i < m_clipboard->GetItemCount(); i++)
{
// Get this item and extract the file URL representation from it
const MCRawClipboardItemRep* t_rep = NULL;
MCAutoRefcounted<const MCRawClipboardItem> t_item = m_clipboard->GetItemAtIndex(i);
if (t_item == NULL)
return false;
t_rep = t_item->FetchRepresentationByType(m_clipboard->GetKnownTypeString(kMCRawClipboardKnownTypeFileURL));
if (t_rep == NULL)
return false;
// Get the data for this representation and decode it
MCAutoStringRef t_url;
MCAutoDataRef t_encoded_url;
t_encoded_url.Give(t_rep->CopyData());
if (*t_encoded_url == NULL)
return false;
t_url.Give(m_clipboard->DecodeTransferredFileList(*t_encoded_url));
if (*t_url == NULL)
return false;
// Append it to the list
if (!MCListAppend(*t_list, *t_url))
return false;
}
// Convert the list to a string
if (!MCListCopyAsString(*t_list, t_output))
return false;
}
else
{
// Clipboard only supports a single item. Grab the contents of the file
// URL list representation and massage slightly.
MCAutoRefcounted<const MCRawClipboardItem> t_item = GetItem();
if (t_item == NULL)
return false;
const MCRawClipboardItemRep* t_rep = t_item->FetchRepresentationByType(m_clipboard->GetKnownTypeString(kMCRawClipboardKnownTypeFileURLList));
if (t_rep == NULL)
return false;
// Decode the list of files
MCAutoDataRef t_data;
t_data.Give(t_rep->CopyData());
if (*t_data == NULL)
return false;
t_output = m_clipboard->DecodeTransferredFileList(*t_data);
}
// All done
if (t_output != NULL)
r_file_list = t_output;
return t_output != NULL;
}
bool MCClipboard::CopyAsText(MCStringRef& r_text) const
{
AutoLock t_lock(this);
// Try to fetch the data using any of the supported text types
MCAutoRefcounted<const MCRawClipboardItem> t_item = GetItem();
if (t_item == NULL)
return false;
// IM-2016-11-21: [[ Bug 18652 ]] If we should be able to fetch using a text encoding then return
// false if conversion fails instead of falling through to other encodings.
if (t_item->HasRepresentation(m_clipboard->GetKnownTypeString(kMCRawClipboardKnownTypeUTF8)))
return CopyAsEncodedText(t_item, kMCRawClipboardKnownTypeUTF8, kMCStringEncodingUTF8, r_text);
if (t_item->HasRepresentation(m_clipboard->GetKnownTypeString(kMCRawClipboardKnownTypeUTF16)))
return CopyAsEncodedText(t_item, kMCRawClipboardKnownTypeUTF16, kMCStringEncodingUTF16, r_text);
if (t_item->HasRepresentation(m_clipboard->GetKnownTypeString(kMCRawClipboardKnownTypeISO8859_1)))
return CopyAsEncodedText(t_item, kMCRawClipboardKnownTypeISO8859_1, kMCStringEncodingISO8859_1, r_text);
if (t_item->HasRepresentation(m_clipboard->GetKnownTypeString(kMCRawClipboardKnownTypeMacRoman)))
return CopyAsEncodedText(t_item, kMCRawClipboardKnownTypeMacRoman, kMCStringEncodingMacRoman, r_text);
if (t_item->HasRepresentation(m_clipboard->GetKnownTypeString(kMCRawClipboardKnownTypeCP1252)))
return CopyAsEncodedText(t_item, kMCRawClipboardKnownTypeCP1252, kMCStringEncodingWindows1252, r_text);
// As a fallback, try to convert a list of file paths into text
if (CopyAsFileList(r_text))
return true;
// None of the text representations existed
return false;
}
bool MCClipboard::CopyAsLiveCodeObjects(MCDataRef& r_objects) const
{
return CopyAsData(kMCRawClipboardKnownTypeLiveCodeObjects, r_objects);
}
bool MCClipboard::CopyAsLiveCodeStyledText(MCDataRef& r_pickled_text) const
{
AutoLock t_lock(this);
// First, try to get the data directly as styled text
if (CopyAsData(kMCRawClipboardKnownTypeLiveCodeStyledText, r_pickled_text))
return true;
// No LiveCode styled text on the clipboard; try RTF
MCAutoDataRef t_rtf;
if (CopyAsData(kMCRawClipboardKnownTypeRTF, &t_rtf))
{
// Convert to LiveCode styled text
MCDataRef t_pickled_text = ConvertRTFToStyledText(*t_rtf);
if (t_pickled_text != NULL)
{
r_pickled_text = t_pickled_text;
return true;
}
}
// Could not grab as either styled text or RTF. Try with HTML.
MCAutoStringRef t_html;
if (CopyAsHTML(&t_html))