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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
* This file is part of the LibreOffice project.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
#include "../gtk/gtkinst.cxx"
#include <com/sun/star/lang/IllegalArgumentException.hpp>
#include <com/sun/star/lang/XMultiServiceFactory.hpp>
#include "com/sun/star/lang/XServiceInfo.hpp"
#include "com/sun/star/lang/XSingleServiceFactory.hpp"
#include "com/sun/star/lang/XInitialization.hpp"
#include "com/sun/star/datatransfer/XTransferable.hpp"
#include "com/sun/star/datatransfer/clipboard/XClipboard.hpp"
#include "com/sun/star/datatransfer/clipboard/XClipboardEx.hpp"
#include "com/sun/star/datatransfer/clipboard/XClipboardNotifier.hpp"
#include "com/sun/star/datatransfer/clipboard/XClipboardListener.hpp"
#include "com/sun/star/datatransfer/clipboard/XFlushableClipboard.hpp"
#include "com/sun/star/datatransfer/clipboard/XSystemClipboard.hpp"
#include "com/sun/star/datatransfer/dnd/DNDConstants.hpp"
#include <comphelper/processfactory.hxx>
#include <comphelper/sequence.hxx>
#include "cppuhelper/compbase.hxx"
#include <cppuhelper/implbase.hxx>
#include <cppuhelper/supportsservice.hxx>
using namespace com::sun::star;
using namespace com::sun::star::uno;
using namespace com::sun::star::lang;
namespace
{
struct TypeEntry
{
const char* pNativeType; // string corresponding to nAtom for the case of nAtom being uninitialized
const char* pType; // Mime encoding on our side
};
static const TypeEntry aConversionTab[] =
{
{ "ISO10646-1", "text/plain;charset=utf-16" },
{ "UTF8_STRING", "text/plain;charset=utf-8" },
{ "UTF-8", "text/plain;charset=utf-8" },
{ "text/plain;charset=UTF-8", "text/plain;charset=utf-8" },
// ISO encodings
{ "ISO8859-2", "text/plain;charset=iso8859-2" },
{ "ISO8859-3", "text/plain;charset=iso8859-3" },
{ "ISO8859-4", "text/plain;charset=iso8859-4" },
{ "ISO8859-5", "text/plain;charset=iso8859-5" },
{ "ISO8859-6", "text/plain;charset=iso8859-6" },
{ "ISO8859-7", "text/plain;charset=iso8859-7" },
{ "ISO8859-8", "text/plain;charset=iso8859-8" },
{ "ISO8859-9", "text/plain;charset=iso8859-9" },
{ "ISO8859-10", "text/plain;charset=iso8859-10" },
{ "ISO8859-13", "text/plain;charset=iso8859-13" },
{ "ISO8859-14", "text/plain;charset=iso8859-14" },
{ "ISO8859-15", "text/plain;charset=iso8859-15" },
// asian encodings
{ "JISX0201.1976-0", "text/plain;charset=jisx0201.1976-0" },
{ "JISX0208.1983-0", "text/plain;charset=jisx0208.1983-0" },
{ "JISX0208.1990-0", "text/plain;charset=jisx0208.1990-0" },
{ "JISX0212.1990-0", "text/plain;charset=jisx0212.1990-0" },
{ "GB2312.1980-0", "text/plain;charset=gb2312.1980-0" },
{ "KSC5601.1992-0", "text/plain;charset=ksc5601.1992-0" },
// eastern european encodings
{ "KOI8-R", "text/plain;charset=koi8-r" },
{ "KOI8-U", "text/plain;charset=koi8-u" },
// String (== iso8859-1)
{ "STRING", "text/plain;charset=iso8859-1" },
// special for compound text
{ "COMPOUND_TEXT", "text/plain;charset=compound_text" },
// PIXMAP
{ "PIXMAP", "image/bmp" }
};
class DataFlavorEq : public std::unary_function<const css::datatransfer::DataFlavor&, bool>
{
private:
const css::datatransfer::DataFlavor& m_rData;
public:
explicit DataFlavorEq(const css::datatransfer::DataFlavor& rData) : m_rData(rData) {}
bool operator() (const css::datatransfer::DataFlavor& rData) const
{
return rData.MimeType == m_rData.MimeType &&
rData.DataType == m_rData.DataType;
}
};
}
std::vector<css::datatransfer::DataFlavor> GtkTransferable::getTransferDataFlavorsAsVector(GdkAtom *targets, gint n_targets)
{
std::vector<css::datatransfer::DataFlavor> aVector;
bool bHaveText = false, bHaveUTF16 = false;
for (gint i = 0; i < n_targets; ++i)
{
gchar* pName = gdk_atom_name(targets[i]);
const char* pFinalName = pName;
css::datatransfer::DataFlavor aFlavor;
// omit text/plain;charset=unicode since it is not well defined
if (rtl_str_compare(pName, "text/plain;charset=unicode") == 0)
continue;
for (size_t j = 0; j < SAL_N_ELEMENTS(aConversionTab); ++j)
{
if (rtl_str_compare(pName, aConversionTab[j].pNativeType) == 0)
{
pFinalName = aConversionTab[j].pType;
break;
}
}
// There are more non-MIME-types reported that are not translated by
// aConversionTab, like "SAVE_TARGETS", "INTEGER", "ATOM"; just filter
// them out for now before they confuse this code's clients:
if (rtl_str_indexOfChar(pFinalName, '/') == -1)
{
continue;
}
aFlavor.MimeType = OUString(pFinalName,
rtl_str_getLength(pFinalName),
RTL_TEXTENCODING_UTF8);
m_aMimeTypeToAtom[aFlavor.MimeType] = targets[i];
aFlavor.DataType = cppu::UnoType<Sequence< sal_Int8 >>::get();
sal_Int32 nIndex(0);
if (aFlavor.MimeType.getToken(0, ';', nIndex) == "text/plain")
{
bHaveText = true;
OUString aToken(aFlavor.MimeType.getToken(0, ';', nIndex));
if (aToken == "charset=utf-16")
{
bHaveUTF16 = true;
aFlavor.DataType = cppu::UnoType<OUString>::get();
}
}
aVector.push_back(aFlavor);
g_free(pName);
}
//If we have text, but no UTF-16 format which is basically the only
//text-format LibreOffice supports for cnp then claim we do and we
//will convert on demand
if (bHaveText && !bHaveUTF16)
{
css::datatransfer::DataFlavor aFlavor;
aFlavor.MimeType = "text/plain;charset=utf-16";
aFlavor.DataType = cppu::UnoType<OUString>::get();
aVector.push_back(aFlavor);
}
return aVector;
}
css::uno::Sequence<css::datatransfer::DataFlavor> SAL_CALL GtkTransferable::getTransferDataFlavors()
{
return comphelper::containerToSequence(getTransferDataFlavorsAsVector());
}
sal_Bool SAL_CALL GtkTransferable::isDataFlavorSupported(const css::datatransfer::DataFlavor& rFlavor)
{
const std::vector<css::datatransfer::DataFlavor> aAll =
getTransferDataFlavorsAsVector();
return std::find_if(aAll.begin(), aAll.end(), DataFlavorEq(rFlavor)) != aAll.end();
}
class GtkClipboardTransferable : public GtkTransferable
{
private:
GdkAtom m_nSelection;
public:
explicit GtkClipboardTransferable(GdkAtom nSelection)
: m_nSelection(nSelection)
{
}
/*
* XTransferable
*/
virtual css::uno::Any SAL_CALL getTransferData(const css::datatransfer::DataFlavor& rFlavor) override
{
GtkClipboard* clipboard = gtk_clipboard_get(m_nSelection);
if (rFlavor.MimeType == "text/plain;charset=utf-16")
{
OUString aStr;
gchar *pText = gtk_clipboard_wait_for_text(clipboard);
if (pText)
aStr = OUString(pText, rtl_str_getLength(pText), RTL_TEXTENCODING_UTF8);
g_free(pText);
css::uno::Any aRet;
aRet <<= aStr.replaceAll("\r\n", "\n");
return aRet;
}
auto it = m_aMimeTypeToAtom.find(rFlavor.MimeType);
if (it == m_aMimeTypeToAtom.end())
return css::uno::Any();
css::uno::Any aRet;
GtkSelectionData* data = gtk_clipboard_wait_for_contents(clipboard,
it->second);
if (!data)
{
return css::uno::Any();
}
gint length;
const guchar *rawdata = gtk_selection_data_get_data_with_length(data,
&length);
Sequence<sal_Int8> aSeq(reinterpret_cast<const sal_Int8*>(rawdata), length);
gtk_selection_data_free(data);
aRet <<= aSeq;
return aRet;
}
std::vector<css::datatransfer::DataFlavor> getTransferDataFlavorsAsVector()
override
{
std::vector<css::datatransfer::DataFlavor> aVector;
GtkClipboard* clipboard = gtk_clipboard_get(m_nSelection);
GdkAtom *targets;
gint n_targets;
if (gtk_clipboard_wait_for_targets(clipboard, &targets, &n_targets))
{
aVector = GtkTransferable::getTransferDataFlavorsAsVector(targets, n_targets);
g_free(targets);
}
return aVector;
}
};
class VclGtkClipboard :
public cppu::WeakComponentImplHelper<
datatransfer::clipboard::XSystemClipboard,
datatransfer::clipboard::XFlushableClipboard,
XServiceInfo>
{
GdkAtom m_nSelection;
osl::Mutex m_aMutex;
gulong m_nOwnerChangedSignalId;
Reference<css::datatransfer::XTransferable> m_aContents;
Reference<css::datatransfer::clipboard::XClipboardOwner> m_aOwner;
std::list< Reference<css::datatransfer::clipboard::XClipboardListener> > m_aListeners;
std::vector<GtkTargetEntry> m_aGtkTargets;
VclToGtkHelper m_aConversionHelper;
public:
explicit VclGtkClipboard(GdkAtom nSelection);
virtual ~VclGtkClipboard() override;
/*
* XServiceInfo
*/
virtual OUString SAL_CALL getImplementationName() override;
virtual sal_Bool SAL_CALL supportsService( const OUString& ServiceName ) override;
virtual Sequence< OUString > SAL_CALL getSupportedServiceNames() override;
/*
* XClipboard
*/
virtual Reference< css::datatransfer::XTransferable > SAL_CALL getContents() override;
virtual void SAL_CALL setContents(
const Reference< css::datatransfer::XTransferable >& xTrans,
const Reference< css::datatransfer::clipboard::XClipboardOwner >& xClipboardOwner ) override;
virtual OUString SAL_CALL getName() override;
/*
* XClipboardEx
*/
virtual sal_Int8 SAL_CALL getRenderingCapabilities() override;
/*
* XFlushableClipboard
*/
virtual void SAL_CALL flushClipboard() override;
/*
* XClipboardNotifier
*/
virtual void SAL_CALL addClipboardListener(
const Reference< css::datatransfer::clipboard::XClipboardListener >& listener ) override;
virtual void SAL_CALL removeClipboardListener(
const Reference< css::datatransfer::clipboard::XClipboardListener >& listener ) override;
void ClipboardGet(GtkSelectionData *selection_data, guint info);
void ClipboardClear();
void OwnerPossiblyChanged(GtkClipboard *clipboard);
};
OUString VclGtkClipboard::getImplementationName()
{
return OUString("com.sun.star.datatransfer.VclGtkClipboard");
}
Sequence< OUString > VclGtkClipboard::getSupportedServiceNames()
{
Sequence<OUString> aRet { "com.sun.star.datatransfer.clipboard.SystemClipboard" };
return aRet;
}
sal_Bool VclGtkClipboard::supportsService( const OUString& ServiceName )
{
return cppu::supportsService(this, ServiceName);
}
Reference< css::datatransfer::XTransferable > VclGtkClipboard::getContents()
{
if (!m_aContents.is())
{
//tdf#93887 This is the system clipboard/selection. We fetch it when we are not
//the owner of the clipboard and have not already fetched it.
m_aContents = new GtkClipboardTransferable(m_nSelection);
}
return m_aContents;
}
void VclGtkClipboard::ClipboardGet(GtkSelectionData *selection_data, guint info)
{
if (!m_aContents.is())
return;
m_aConversionHelper.setSelectionData(m_aContents, selection_data, info);
}
namespace
{
const OString& getPID()
{
static OString sPID;
if (!sPID.getLength())
{
oslProcessIdentifier aProcessId = 0;
oslProcessInfo info;
info.Size = sizeof (oslProcessInfo);
if (osl_getProcessInfo(nullptr, osl_Process_IDENTIFIER, &info) == osl_Process_E_None)
aProcessId = info.Ident;
sPID = OString::number(aProcessId);
}
return sPID;
}
}
namespace
{
void ClipboardGetFunc(GtkClipboard */*clipboard*/, GtkSelectionData *selection_data,
guint info,
gpointer user_data_or_owner)
{
VclGtkClipboard* pThis = static_cast<VclGtkClipboard*>(user_data_or_owner);
pThis->ClipboardGet(selection_data, info);
}
void ClipboardClearFunc(GtkClipboard */*clipboard*/, gpointer user_data_or_owner)
{
VclGtkClipboard* pThis = static_cast<VclGtkClipboard*>(user_data_or_owner);
pThis->ClipboardClear();
}
void handle_owner_change(GtkClipboard *clipboard, GdkEvent */*event*/, gpointer user_data)
{
VclGtkClipboard* pThis = static_cast<VclGtkClipboard*>(user_data);
pThis->OwnerPossiblyChanged(clipboard);
}
}
void VclGtkClipboard::OwnerPossiblyChanged(GtkClipboard* clipboard)
{
if (!m_aContents.is())
return;
//if gdk_display_supports_selection_notification is not supported, e.g. like
//right now under wayland, then you only get owner-changed notifications at
//opportune times when the selection might have changed. So here
//we see if the selection supports a dummy selection type identifying
//our pid, in which case it's us.
bool bSelf = false;
//disconnect and reconnect after gtk_clipboard_wait_for_targets to
//avoid possible recursion
g_signal_handler_disconnect(clipboard, m_nOwnerChangedSignalId);
OString sTunnel = "application/x-libreoffice-internal-id-" + getPID();
GdkAtom *targets;
gint n_targets;
if (gtk_clipboard_wait_for_targets(clipboard, &targets, &n_targets))
{
for (gint i = 0; i < n_targets && !bSelf; ++i)
{
gchar* pName = gdk_atom_name(targets[i]);
if (strcmp(pName, sTunnel.getStr()) == 0)
{
bSelf = true;
}
g_free(pName);
}
g_free(targets);
}
m_nOwnerChangedSignalId = g_signal_connect(clipboard, "owner-change",
G_CALLBACK(handle_owner_change), this);
if (!bSelf)
{
//null out m_aContents to return control to the system-one which
//will be retrieved if getContents is called again
setContents(Reference<css::datatransfer::XTransferable>(),
Reference<css::datatransfer::clipboard::XClipboardOwner>());
}
}
void VclGtkClipboard::ClipboardClear()
{
for (auto &a : m_aGtkTargets)
g_free(a.target);
m_aGtkTargets.clear();
}
GtkTargetEntry VclToGtkHelper::makeGtkTargetEntry(const css::datatransfer::DataFlavor& rFlavor)
{
GtkTargetEntry aEntry;
aEntry.target =
g_strdup(OUStringToOString(rFlavor.MimeType, RTL_TEXTENCODING_UTF8).getStr());
aEntry.flags = 0;
auto it = std::find_if(aInfoToFlavor.begin(), aInfoToFlavor.end(),
DataFlavorEq(rFlavor));
if (it != aInfoToFlavor.end())
aEntry.info = std::distance(aInfoToFlavor.begin(), it);
else
{
aEntry.info = aInfoToFlavor.size();
aInfoToFlavor.push_back(rFlavor);
}
return aEntry;
}
void VclToGtkHelper::setSelectionData(const Reference<css::datatransfer::XTransferable> &rTrans,
GtkSelectionData *selection_data, guint info)
{
GdkAtom type(gdk_atom_intern(OUStringToOString(aInfoToFlavor[info].MimeType,
RTL_TEXTENCODING_UTF8).getStr(),
false));
css::datatransfer::DataFlavor aFlavor(aInfoToFlavor[info]);
if (aFlavor.MimeType == "UTF8_STRING" || aFlavor.MimeType == "STRING")
aFlavor.MimeType = "text/plain;charset=utf-8";
Sequence<sal_Int8> aData;
Any aValue;
try
{
aValue = rTrans->getTransferData(aFlavor);
}
catch (...)
{
}
if (aValue.getValueTypeClass() == TypeClass_STRING)
{
OUString aString;
aValue >>= aString;
aData = Sequence< sal_Int8 >( reinterpret_cast<sal_Int8 const *>(aString.getStr()), aString.getLength() * sizeof( sal_Unicode ) );
}
else if (aValue.getValueType() == cppu::UnoType<Sequence< sal_Int8 >>::get())
{
aValue >>= aData;
}
else if (aFlavor.MimeType == "text/plain;charset=utf-8")
{
//didn't have utf-8, try utf-16 and convert
aFlavor.MimeType = "text/plain;charset=utf-16";
aFlavor.DataType = cppu::UnoType<OUString>::get();
try
{
aValue = rTrans->getTransferData(aFlavor);
}
catch (...)
{
}
OUString aString;
aValue >>= aString;
OString aUTF8String(OUStringToOString(aString, RTL_TEXTENCODING_UTF8));
gtk_selection_data_set(selection_data, type, 8,
reinterpret_cast<const guchar *>(aUTF8String.getStr()),
aUTF8String.getLength());
return;
}
gtk_selection_data_set(selection_data, type, 8,
reinterpret_cast<const guchar *>(aData.getArray()),
aData.getLength());
}
VclGtkClipboard::VclGtkClipboard(GdkAtom nSelection)
: cppu::WeakComponentImplHelper<datatransfer::clipboard::XSystemClipboard,
datatransfer::clipboard::XFlushableClipboard, XServiceInfo>
(m_aMutex)
, m_nSelection(nSelection)
{
GtkClipboard* clipboard = gtk_clipboard_get(m_nSelection);
m_nOwnerChangedSignalId = g_signal_connect(clipboard, "owner-change",
G_CALLBACK(handle_owner_change), this);
}
void VclGtkClipboard::flushClipboard()
{
SolarMutexGuard aGuard;
if (GDK_SELECTION_CLIPBOARD != m_nSelection)
return;
GtkClipboard* clipboard = gtk_clipboard_get(m_nSelection);
gtk_clipboard_store(clipboard);
}
VclGtkClipboard::~VclGtkClipboard()
{
GtkClipboard* clipboard = gtk_clipboard_get(m_nSelection);
g_signal_handler_disconnect(clipboard, m_nOwnerChangedSignalId);
if (!m_aGtkTargets.empty())
{
gtk_clipboard_clear(clipboard);
ClipboardClear();
}
assert(m_aGtkTargets.empty());
}
std::vector<GtkTargetEntry> VclToGtkHelper::FormatsToGtk(const css::uno::Sequence<css::datatransfer::DataFlavor> &rFormats)
{
std::vector<GtkTargetEntry> aGtkTargets;
bool bHaveText(false), bHaveUTF8(false);
for (int i = 0; i < rFormats.getLength(); ++i)
{
const css::datatransfer::DataFlavor& rFlavor = rFormats[i];
sal_Int32 nIndex(0);
if (rFlavor.MimeType.getToken(0, ';', nIndex) == "text/plain")
{
bHaveText = true;
OUString aToken(rFlavor.MimeType.getToken(0, ';', nIndex));
if (aToken == "charset=utf-8")
{
bHaveUTF8 = true;
}
}
GtkTargetEntry aEntry(makeGtkTargetEntry(rFlavor));
aGtkTargets.push_back(aEntry);
}
if (bHaveText)
{
css::datatransfer::DataFlavor aFlavor;
aFlavor.DataType = cppu::UnoType<Sequence< sal_Int8 >>::get();
if (!bHaveUTF8)
{
aFlavor.MimeType = "text/plain;charset=utf-8";
aGtkTargets.push_back(makeGtkTargetEntry(aFlavor));
}
aFlavor.MimeType = "UTF8_STRING";
aGtkTargets.push_back(makeGtkTargetEntry(aFlavor));
aFlavor.MimeType = "STRING";
aGtkTargets.push_back(makeGtkTargetEntry(aFlavor));
}
return aGtkTargets;
}
void VclGtkClipboard::setContents(
const Reference< css::datatransfer::XTransferable >& xTrans,
const Reference< css::datatransfer::clipboard::XClipboardOwner >& xClipboardOwner )
{
osl::ClearableMutexGuard aGuard( m_aMutex );
Reference< datatransfer::clipboard::XClipboardOwner > xOldOwner( m_aOwner );
Reference< datatransfer::XTransferable > xOldContents( m_aContents );
m_aContents = xTrans;
m_aOwner = xClipboardOwner;
std::list< Reference< datatransfer::clipboard::XClipboardListener > > aListeners( m_aListeners );
datatransfer::clipboard::ClipboardEvent aEv;
GtkClipboard* clipboard = gtk_clipboard_get(m_nSelection);
if (!m_aGtkTargets.empty())
{
gtk_clipboard_clear(clipboard);
ClipboardClear();
}
assert(m_aGtkTargets.empty());
if (m_aContents.is())
{
css::uno::Sequence<css::datatransfer::DataFlavor> aFormats = xTrans->getTransferDataFlavors();
std::vector<GtkTargetEntry> aGtkTargets(m_aConversionHelper.FormatsToGtk(aFormats));
if (!aGtkTargets.empty())
{
GtkTargetEntry aEntry;
OString sTunnel = "application/x-libreoffice-internal-id-" + getPID();
aEntry.target = g_strdup(sTunnel.getStr());
aEntry.flags = 0;
aEntry.info = 0;
aGtkTargets.push_back(aEntry);
gtk_clipboard_set_with_data(clipboard, aGtkTargets.data(), aGtkTargets.size(),
ClipboardGetFunc, ClipboardClearFunc, this);
gtk_clipboard_set_can_store(clipboard, aGtkTargets.data(), aGtkTargets.size());
}
m_aGtkTargets = aGtkTargets;
}
aEv.Contents = getContents();
aGuard.clear();
if (xOldOwner.is() && xOldOwner != xClipboardOwner)
xOldOwner->lostOwnership( this, xOldContents );
for( std::list< Reference< datatransfer::clipboard::XClipboardListener > >::iterator it =
aListeners.begin(); it != aListeners.end() ; ++it )
{
(*it)->changedContents( aEv );
}
}
OUString VclGtkClipboard::getName()
{
return (m_nSelection == GDK_SELECTION_CLIPBOARD) ? OUString("CLIPBOARD") : OUString("PRIMARY");
}
sal_Int8 VclGtkClipboard::getRenderingCapabilities()
{
return 0;
}
void VclGtkClipboard::addClipboardListener( const Reference< datatransfer::clipboard::XClipboardListener >& listener )
{
osl::ClearableMutexGuard aGuard( m_aMutex );
m_aListeners.push_back( listener );
}
void VclGtkClipboard::removeClipboardListener( const Reference< datatransfer::clipboard::XClipboardListener >& listener )
{
osl::ClearableMutexGuard aGuard( m_aMutex );
m_aListeners.remove( listener );
}
Reference< XInterface > GtkInstance::CreateClipboard(const Sequence< Any >& arguments)
{
OUString sel;
if (arguments.getLength() == 0) {
sel = "CLIPBOARD";
} else if (arguments.getLength() != 1 || !(arguments[0] >>= sel)) {
throw css::lang::IllegalArgumentException(
"bad GtkInstance::CreateClipboard arguments",
css::uno::Reference<css::uno::XInterface>(), -1);
}
GdkAtom nSelection = (sel == "CLIPBOARD") ? GDK_SELECTION_CLIPBOARD : GDK_SELECTION_PRIMARY;
auto it = m_aClipboards.find(nSelection);
if (it != m_aClipboards.end())
return it->second;
Reference<XInterface> xClipboard(static_cast<cppu::OWeakObject *>(new VclGtkClipboard(nSelection)));
m_aClipboards[nSelection] = xClipboard;
return xClipboard;
}
GtkDropTarget::GtkDropTarget()
: WeakComponentImplHelper(m_aMutex)
, m_pFrame(nullptr)
, m_bActive(false)
, m_nDefaultActions(0)
{
}
OUString SAL_CALL GtkDropTarget::getImplementationName()
{
return OUString("com.sun.star.datatransfer.dnd.VclGtkDropTarget");
}
sal_Bool SAL_CALL GtkDropTarget::supportsService(OUString const & ServiceName)
{
return cppu::supportsService(this, ServiceName);
}
css::uno::Sequence<OUString> SAL_CALL GtkDropTarget::getSupportedServiceNames()
{
Sequence<OUString> aRet { "com.sun.star.datatransfer.dnd.GtkDropTarget" };
return aRet;
}
GtkDropTarget::~GtkDropTarget()
{
if (m_pFrame)
m_pFrame->deregisterDropTarget(this);
}
void GtkDropTarget::deinitialize()
{
m_pFrame = nullptr;
m_bActive = false;
}
void GtkDropTarget::initialize(const Sequence<Any>& rArguments)
{
if (rArguments.getLength() < 2)
{
throw RuntimeException("DropTarget::initialize: Cannot install window event handler",
static_cast<OWeakObject*>(this));
}
sal_IntPtr nFrame = 0;
rArguments.getConstArray()[1] >>= nFrame;
if (!nFrame)
{
throw RuntimeException("DropTarget::initialize: missing SalFrame",
static_cast<OWeakObject*>(this));
}
m_pFrame = reinterpret_cast<GtkSalFrame*>(nFrame);
m_pFrame->registerDropTarget(this);
m_bActive = true;
}
void GtkDropTarget::addDropTargetListener( const Reference< css::datatransfer::dnd::XDropTargetListener >& xListener)
{
::osl::Guard< ::osl::Mutex > aGuard( m_aMutex );
m_aListeners.push_back( xListener );
}
void GtkDropTarget::removeDropTargetListener( const Reference< css::datatransfer::dnd::XDropTargetListener >& xListener)
{
::osl::Guard< ::osl::Mutex > aGuard( m_aMutex );
m_aListeners.remove( xListener );
}
void GtkDropTarget::fire_drop(const css::datatransfer::dnd::DropTargetDropEvent& dtde)
{
osl::ClearableGuard<osl::Mutex> aGuard( m_aMutex );
std::list<css::uno::Reference<css::datatransfer::dnd::XDropTargetListener>> aListeners(m_aListeners);
aGuard.clear();
for (auto it = aListeners.begin(); it != aListeners.end(); ++it)
{
(*it)->drop( dtde );
}
}
void GtkDropTarget::fire_dragEnter(const css::datatransfer::dnd::DropTargetDragEnterEvent& dtde)
{
osl::ClearableGuard< ::osl::Mutex > aGuard( m_aMutex );
std::list<css::uno::Reference<css::datatransfer::dnd::XDropTargetListener>> aListeners(m_aListeners);
aGuard.clear();
for (auto it = aListeners.begin(); it != aListeners.end(); ++it)
{
(*it)->dragEnter( dtde );
}
}
void GtkDropTarget::fire_dragOver(const css::datatransfer::dnd::DropTargetDragEvent& dtde)
{
osl::ClearableGuard< ::osl::Mutex > aGuard( m_aMutex );
std::list<css::uno::Reference<css::datatransfer::dnd::XDropTargetListener>> aListeners(m_aListeners);
aGuard.clear();
for (auto it = aListeners.begin(); it != aListeners.end(); ++it)
{
(*it)->dragOver( dtde );
}
}
void GtkDropTarget::fire_dragExit(const css::datatransfer::dnd::DropTargetEvent& dte)
{
osl::ClearableGuard< ::osl::Mutex > aGuard( m_aMutex );
std::list<css::uno::Reference<css::datatransfer::dnd::XDropTargetListener>> aListeners(m_aListeners);
aGuard.clear();
for (auto it = aListeners.begin(); it != aListeners.end(); ++it)
{
(*it)->dragExit( dte );
}
}
sal_Bool GtkDropTarget::isActive()
{
return m_bActive;
}
void GtkDropTarget::setActive(sal_Bool bActive)
{
m_bActive = bActive;
}
sal_Int8 GtkDropTarget::getDefaultActions()
{
return m_nDefaultActions;
}
void GtkDropTarget::setDefaultActions(sal_Int8 nDefaultActions)
{
m_nDefaultActions = nDefaultActions;
}
Reference< XInterface > GtkInstance::CreateDropTarget()
{
return Reference< XInterface >( static_cast<cppu::OWeakObject *>(new GtkDropTarget()) );
}
GtkDragSource::~GtkDragSource()
{
if (m_pFrame)
m_pFrame->deregisterDragSource(this);
if (GtkDragSource::g_ActiveDragSource == this)
{
SAL_WARN( "vcl.gtk", "dragEnd should have been called on GtkDragSource before dtor");
GtkDragSource::g_ActiveDragSource = nullptr;
}
}
void GtkDragSource::deinitialize()
{
m_pFrame = nullptr;
}
sal_Bool GtkDragSource::isDragImageSupported()
{
return true;
}
sal_Int32 GtkDragSource::getDefaultCursor( sal_Int8 )
{
return 0;
}
void GtkDragSource::initialize(const css::uno::Sequence<css::uno::Any >& rArguments)
{
if (rArguments.getLength() < 2)
{
throw RuntimeException("DragSource::initialize: Cannot install window event handler",
static_cast<OWeakObject*>(this));
}
sal_IntPtr nFrame = 0;
rArguments.getConstArray()[1] >>= nFrame;
if (!nFrame)
{
throw RuntimeException("DragSource::initialize: missing SalFrame",
static_cast<OWeakObject*>(this));
}
m_pFrame = reinterpret_cast<GtkSalFrame*>(nFrame);
m_pFrame->registerDragSource(this);
}
OUString SAL_CALL GtkDragSource::getImplementationName()
{
return OUString("com.sun.star.datatransfer.dnd.VclGtkDragSource");
}
sal_Bool SAL_CALL GtkDragSource::supportsService(OUString const & ServiceName)
{
return cppu::supportsService(this, ServiceName);
}
css::uno::Sequence<OUString> SAL_CALL GtkDragSource::getSupportedServiceNames()
{
Sequence<OUString> aRet { "com.sun.star.datatransfer.dnd.GtkDragSource" };
return aRet;
}
Reference< XInterface > GtkInstance::CreateDragSource()
{
return Reference< XInterface >( static_cast<cppu::OWeakObject *>(new GtkDragSource()) );
}
class GtkOpenGLContext : public OpenGLContext
{
GLWindow m_aGLWin;
#if GTK_CHECK_VERSION(3,16,0)
GtkWidget *m_pGLArea;
#endif
public:
GtkOpenGLContext()
: OpenGLContext()
#if GTK_CHECK_VERSION(3,16,0)
, m_pGLArea(nullptr)
#endif
{
}
virtual bool initWindow() override
{
if( !m_pChildWindow )
{
SystemWindowData winData = generateWinData(mpWindow, mbRequestLegacyContext);
m_pChildWindow = VclPtr<SystemChildWindow>::Create(mpWindow, 0, &winData, false);
}
if (m_pChildWindow)
{
InitChildWindow(m_pChildWindow.get());
}
return true;
}
private:
virtual const GLWindow& getOpenGLWindow() const override { return m_aGLWin; }
virtual GLWindow& getModifiableOpenGLWindow() override { return m_aGLWin; }
#if GTK_CHECK_VERSION(3,16,0)
static void signalDestroy(GtkWidget*, gpointer context)
{
GtkOpenGLContext* pThis = static_cast<GtkOpenGLContext*>(context);
pThis->m_pGLArea = nullptr;
}
#endif
virtual bool ImplInit() override
{
#if GTK_CHECK_VERSION(3,16,0)
const SystemEnvData* pEnvData = m_pChildWindow->GetSystemData();
GtkWidget *pParent = static_cast<GtkWidget*>(pEnvData->pWidget);
m_pGLArea = gtk_gl_area_new();
g_signal_connect(G_OBJECT(m_pGLArea), "destroy", G_CALLBACK(signalDestroy), this);
gtk_gl_area_set_has_depth_buffer(GTK_GL_AREA(m_pGLArea), true);
gtk_gl_area_set_auto_render(GTK_GL_AREA(m_pGLArea), false);
gtk_widget_set_hexpand(m_pGLArea, true);
gtk_widget_set_vexpand(m_pGLArea, true);
gtk_container_add(GTK_CONTAINER(pParent), m_pGLArea);
gtk_widget_show_all(pParent);
gtk_gl_area_make_current(GTK_GL_AREA(m_pGLArea));
gtk_gl_area_attach_buffers(GTK_GL_AREA(m_pGLArea));
#endif
bool bRet = InitGL();
InitGLDebugging();
return bRet;
}
#if GTK_CHECK_VERSION(3,16,0)
virtual void restoreDefaultFramebuffer() override
{
OpenGLContext::restoreDefaultFramebuffer();
gtk_gl_area_attach_buffers(GTK_GL_AREA(m_pGLArea));
}
#endif
virtual void makeCurrent() override
{
if (isCurrent())
return;
clearCurrent();
#if GTK_CHECK_VERSION(3,16,0)
if (m_pGLArea)
gtk_gl_area_make_current(GTK_GL_AREA(m_pGLArea));
#endif
registerAsCurrent();
}
virtual void destroyCurrentContext() override
{
#if GTK_CHECK_VERSION(3,16,0)
gdk_gl_context_clear_current();
#endif
}
virtual bool isCurrent() override
{
#if GTK_CHECK_VERSION(3,16,0)
return m_pGLArea && gdk_gl_context_get_current() == gtk_gl_area_get_context(GTK_GL_AREA(m_pGLArea));
#else
return false;
#endif
}
virtual void sync() override
{
#if GTK_CHECK_VERSION(3,16,0)
gtk_gl_area_queue_render(GTK_GL_AREA(m_pGLArea));
#endif
}
virtual void resetCurrent() override
{
clearCurrent();
#if GTK_CHECK_VERSION(3,16,0)
gdk_gl_context_clear_current();
#endif
}
virtual void swapBuffers() override
{
#if GTK_CHECK_VERSION(3,16,0)
gtk_gl_area_queue_render(GTK_GL_AREA(m_pGLArea));
#endif
BuffersSwapped();
}
};
OpenGLContext* GtkInstance::CreateOpenGLContext()
{
return new GtkOpenGLContext;
}
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|