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
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
|
/**
* @file
* System-wide clipboard management - implementation.
*/
/* Authors:
* Krzysztof Kosiński <tweenk@o2.pl>
* Jon A. Cruz <jon@joncruz.org>
* Incorporates some code from selection-chemistry.cpp, see that file for more credits.
* Abhishek Sharma
*
* Copyright (C) 2008 authors
* Copyright (C) 2010 Jon A. Cruz
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* See the file COPYING for details.
*/
#include "ui/clipboard.h"
// TODO: reduce header bloat if possible
#include "file.h" // for file_import, used in _pasteImage
#include <list>
#include <algorithm>
#include <gtkmm/clipboard.h>
#include <glibmm/ustring.h>
#include <glibmm/i18n.h>
#include <glib/gstdio.h> // for g_file_set_contents etc., used in _onGet and paste
#include "gc-core.h"
#include "xml/repr.h"
#include "inkscape.h"
#include "io/stringstream.h"
#include "desktop.h"
#include "desktop-handles.h"
#include "desktop-style.h" // for sp_desktop_set_style, used in _pasteStyle
#include "document.h"
#include "document-private.h"
#include "selection.h"
#include "message-stack.h"
#include "context-fns.h"
#include "dropper-context.h" // used in copy()
#include "style.h"
#include "extension/db.h" // extension database
#include "extension/input.h"
#include "extension/output.h"
#include "selection-chemistry.h"
#include <2geom/rect.h>
#include <2geom/transforms.h>
#include "box3d.h"
#include "gradient-drag.h"
#include "sp-item.h"
#include "sp-item-transform.h" // for sp_item_scale_rel, used in _pasteSize
#include "sp-path.h"
#include "sp-pattern.h"
#include "sp-shape.h"
#include "sp-gradient.h"
#include "sp-gradient-reference.h"
#include "sp-gradient-fns.h"
#include "sp-linear-gradient-fns.h"
#include "sp-radial-gradient-fns.h"
#include "sp-clippath.h"
#include "sp-mask.h"
#include "sp-textpath.h"
#include "sp-rect.h"
#include "live_effects/lpeobject.h"
#include "live_effects/lpeobject-reference.h"
#include "live_effects/parameter/path.h"
#include "svg/svg.h" // for sp_svg_transform_write, used in _copySelection
#include "svg/css-ostringstream.h" // used in copy
#include "text-context.h"
#include "text-editing.h"
#include "tools-switch.h"
#include "path-chemistry.h"
#include "unit-constants.h"
#include "helper/png-write.h"
#include "svg/svg-color.h"
#include "sp-namedview.h"
#include "snap.h"
#include "persp3d.h"
#include "preferences.h"
/// Made up mimetype to represent Gdk::Pixbuf clipboard contents.
#define CLIPBOARD_GDK_PIXBUF_TARGET "image/x-gdk-pixbuf"
#define CLIPBOARD_TEXT_TARGET "text/plain"
#ifdef WIN32
#include <windows.h>
// Clipboard Formats: http://msdn.microsoft.com/en-us/library/ms649013(VS.85).aspx
// On Windows, most graphical applications can handle CF_DIB/CF_BITMAP and/or CF_ENHMETAFILE
// GTK automatically presents an "image/bmp" target as CF_DIB/CF_BITMAP
// Presenting "image/x-emf" as CF_ENHMETAFILE must be done by Inkscape ?
#define CLIPBOARD_WIN32_EMF_TARGET "CF_ENHMETAFILE"
#define CLIPBOARD_WIN32_EMF_MIME "image/x-emf"
#endif
namespace Inkscape {
namespace UI {
/**
* Default implementation of the clipboard manager.
*/
class ClipboardManagerImpl : public ClipboardManager {
public:
virtual void copy(SPDesktop *desktop);
virtual void copyPathParameter(Inkscape::LivePathEffect::PathParam *);
virtual bool paste(SPDesktop *desktop, bool in_place);
virtual bool pasteStyle(SPDesktop *desktop);
virtual bool pasteSize(SPDesktop *desktop, bool separately, bool apply_x, bool apply_y);
virtual bool pastePathEffect(SPDesktop *desktop);
virtual Glib::ustring getPathParameter(SPDesktop* desktop);
virtual Glib::ustring getShapeOrTextObjectId(SPDesktop *desktop);
virtual const gchar *getFirstObjectID();
ClipboardManagerImpl();
~ClipboardManagerImpl();
private:
void _copySelection(Inkscape::Selection *);
void _copyUsedDefs(SPItem *);
void _copyGradient(SPGradient *);
void _copyPattern(SPPattern *);
void _copyTextPath(SPTextPath *);
Inkscape::XML::Node *_copyNode(Inkscape::XML::Node *, Inkscape::XML::Document *, Inkscape::XML::Node *);
bool _pasteImage(SPDocument *doc);
bool _pasteText(SPDesktop *desktop);
void _applyPathEffect(SPItem *, gchar const *);
SPDocument *_retrieveClipboard(Glib::ustring = "");
// clipboard callbacks
void _onGet(Gtk::SelectionData &, guint);
void _onClear();
// various helpers
void _createInternalClipboard();
void _discardInternalClipboard();
Inkscape::XML::Node *_createClipNode();
Geom::Scale _getScale(SPDesktop *desktop, Geom::Point const &min, Geom::Point const &max, Geom::Rect const &obj_rect, bool apply_x, bool apply_y);
Glib::ustring _getBestTarget();
void _setClipboardTargets();
void _setClipboardColor(guint32);
void _userWarn(SPDesktop *, char const *);
void _inkscape_wait_for_targets(std::list<Glib::ustring> &);
// private properites
SPDocument *_clipboardSPDoc; ///< Document that stores the clipboard until someone requests it
Inkscape::XML::Node *_defs; ///< Reference to the clipboard document's defs node
Inkscape::XML::Node *_root; ///< Reference to the clipboard's root node
Inkscape::XML::Node *_clipnode; ///< The node that holds extra information
Inkscape::XML::Document *_doc; ///< Reference to the clipboard's Inkscape::XML::Document
// we need a way to copy plain text AND remember its style;
// the standard _clipnode is only available in an SVG tree, hence this special storage
SPCSSAttr *_text_style; ///< Style copied along with plain text fragment
Glib::RefPtr<Gtk::Clipboard> _clipboard; ///< Handle to the system wide clipboard - for convenience
std::list<Glib::ustring> _preferred_targets; ///< List of supported clipboard targets
};
ClipboardManagerImpl::ClipboardManagerImpl()
: _clipboardSPDoc(NULL),
_defs(NULL),
_root(NULL),
_clipnode(NULL),
_doc(NULL),
_text_style(NULL),
_clipboard( Gtk::Clipboard::get() )
{
// push supported clipboard targets, in order of preference
_preferred_targets.push_back("image/x-inkscape-svg");
_preferred_targets.push_back("image/svg+xml");
_preferred_targets.push_back("image/svg+xml-compressed");
#ifdef WIN32
_preferred_targets.push_back(CLIPBOARD_WIN32_EMF_MIME);
#endif
_preferred_targets.push_back("application/pdf");
_preferred_targets.push_back("image/x-adobe-illustrator");
}
ClipboardManagerImpl::~ClipboardManagerImpl() {}
/**
* Copy selection contents to the clipboard.
*/
void ClipboardManagerImpl::copy(SPDesktop *desktop)
{
if ( desktop == NULL ) {
return;
}
Inkscape::Selection *selection = sp_desktop_selection(desktop);
// Special case for when the gradient dragger is active - copies gradient color
if (desktop->event_context->get_drag()) {
GrDrag *drag = desktop->event_context->get_drag();
if (drag->hasSelection()) {
guint32 col = drag->getColor();
// set the color as clipboard content (text in RRGGBBAA format)
_setClipboardColor(col);
// create a style with this color on fill and opacity in master opacity, so it can be
// pasted on other stops or objects
if (_text_style) {
sp_repr_css_attr_unref(_text_style);
_text_style = NULL;
}
_text_style = sp_repr_css_attr_new();
// print and set properties
gchar color_str[16];
g_snprintf(color_str, 16, "#%06x", col >> 8);
sp_repr_css_set_property(_text_style, "fill", color_str);
float opacity = SP_RGBA32_A_F(col);
if (opacity > 1.0) {
opacity = 1.0; // safeguard
}
Inkscape::CSSOStringStream opcss;
opcss << opacity;
sp_repr_css_set_property(_text_style, "opacity", opcss.str().data());
_discardInternalClipboard();
return;
}
}
// Special case for when the color picker ("dropper") is active - copies color under cursor
if (tools_isactive(desktop, TOOLS_DROPPER)) {
_setClipboardColor(sp_dropper_context_get_color(desktop->event_context));
_discardInternalClipboard();
return;
}
// Special case for when the text tool is active - if some text is selected, copy plain text,
// not the object that holds it; also copy the style at cursor into
if (tools_isactive(desktop, TOOLS_TEXT)) {
_discardInternalClipboard();
Glib::ustring selected_text = sp_text_get_selected_text(desktop->event_context);
_clipboard->set_text(selected_text);
if (_text_style) {
sp_repr_css_attr_unref(_text_style);
_text_style = NULL;
}
_text_style = sp_text_get_style_at_cursor(desktop->event_context);
return;
}
if (selection->isEmpty()) { // check whether something is selected
_userWarn(desktop, _("Nothing was copied."));
return;
}
_discardInternalClipboard();
_createInternalClipboard(); // construct a new clipboard document
_copySelection(selection); // copy all items in the selection to the internal clipboard
fit_canvas_to_drawing(_clipboardSPDoc);
_setClipboardTargets();
}
/**
* Copy a Live Path Effect path parameter to the clipboard.
* @param pp The path parameter to store in the clipboard.
*/
void ClipboardManagerImpl::copyPathParameter(Inkscape::LivePathEffect::PathParam *pp)
{
if ( pp == NULL ) {
return;
}
gchar *svgd = sp_svg_write_path( pp->get_pathvector() );
if ( svgd == NULL || *svgd == '\0' ) {
return;
}
_discardInternalClipboard();
_createInternalClipboard();
Inkscape::XML::Node *pathnode = _doc->createElement("svg:path");
pathnode->setAttribute("d", svgd);
g_free(svgd);
_root->appendChild(pathnode);
Inkscape::GC::release(pathnode);
fit_canvas_to_drawing(_clipboardSPDoc);
_setClipboardTargets();
}
/**
* Paste from the system clipboard into the active desktop.
* @param in_place Whether to put the contents where they were when copied.
*/
bool ClipboardManagerImpl::paste(SPDesktop *desktop, bool in_place)
{
// do any checking whether we really are able to paste before requesting the contents
if ( desktop == NULL ) {
return false;
}
if ( Inkscape::have_viable_layer(desktop, desktop->messageStack()) == false ) {
return false;
}
Glib::ustring target = _getBestTarget();
// Special cases of clipboard content handling go here
// Note that target priority is determined in _getBestTarget.
// TODO: Handle x-special/gnome-copied-files and text/uri-list to support pasting files
// if there is an image on the clipboard, paste it
if ( target == CLIPBOARD_GDK_PIXBUF_TARGET ) {
return _pasteImage(desktop->doc());
}
// if there's only text, paste it into a selected text object or create a new one
if ( target == CLIPBOARD_TEXT_TARGET ) {
return _pasteText(desktop);
}
// otherwise, use the import extensions
SPDocument *tempdoc = _retrieveClipboard(target);
if ( tempdoc == NULL ) {
_userWarn(desktop, _("Nothing on the clipboard."));
return false;
}
sp_import_document(desktop, tempdoc, in_place);
tempdoc->doUnref();
return true;
}
/**
* Returns the id of the first visible copied object.
*/
const gchar *ClipboardManagerImpl::getFirstObjectID()
{
SPDocument *tempdoc = _retrieveClipboard("image/x-inkscape-svg");
if ( tempdoc == NULL ) {
return NULL;
}
Inkscape::XML::Node *root = tempdoc->getReprRoot();
if (!root) {
return NULL;
}
Inkscape::XML::Node *ch = root->firstChild();
while (ch != NULL &&
strcmp(ch->name(), "svg:g") &&
strcmp(ch->name(), "svg:path") &&
strcmp(ch->name(), "svg:use") &&
strcmp(ch->name(), "svg:text") &&
strcmp(ch->name(), "svg:image") &&
strcmp(ch->name(), "svg:rect")
) {
ch = ch->next();
}
if (ch) {
return ch->attribute("id");
}
return NULL;
}
/**
* Implements the Paste Style action.
*/
bool ClipboardManagerImpl::pasteStyle(SPDesktop *desktop)
{
if (desktop == NULL) {
return false;
}
// check whether something is selected
Inkscape::Selection *selection = sp_desktop_selection(desktop);
if (selection->isEmpty()) {
_userWarn(desktop, _("Select <b>object(s)</b> to paste style to."));
return false;
}
SPDocument *tempdoc = _retrieveClipboard("image/x-inkscape-svg");
if ( tempdoc == NULL ) {
// no document, but we can try _text_style
if (_text_style) {
sp_desktop_set_style(desktop, _text_style);
return true;
} else {
_userWarn(desktop, _("No style on the clipboard."));
return false;
}
}
Inkscape::XML::Node *root = tempdoc->getReprRoot();
Inkscape::XML::Node *clipnode = sp_repr_lookup_name(root, "inkscape:clipboard", 1);
bool pasted = false;
if (clipnode) {
desktop->doc()->importDefs(tempdoc);
SPCSSAttr *style = sp_repr_css_attr(clipnode, "style");
sp_desktop_set_style(desktop, style);
pasted = true;
}
else {
_userWarn(desktop, _("No style on the clipboard."));
}
tempdoc->doUnref();
return pasted;
}
/**
* Resize the selection or each object in the selection to match the clipboard's size.
* @param separately Whether to scale each object in the selection separately
* @param apply_x Whether to scale the width of objects / selection
* @param apply_y Whether to scale the height of objects / selection
*/
bool ClipboardManagerImpl::pasteSize(SPDesktop *desktop, bool separately, bool apply_x, bool apply_y)
{
if (!apply_x && !apply_y) {
return false; // pointless parameters
}
if ( desktop == NULL ) {
return false;
}
Inkscape::Selection *selection = sp_desktop_selection(desktop);
if (selection->isEmpty()) {
_userWarn(desktop, _("Select <b>object(s)</b> to paste size to."));
return false;
}
// FIXME: actually, this should accept arbitrary documents
SPDocument *tempdoc = _retrieveClipboard("image/x-inkscape-svg");
if ( tempdoc == NULL ) {
_userWarn(desktop, _("No size on the clipboard."));
return false;
}
// retrieve size ifomration from the clipboard
Inkscape::XML::Node *root = tempdoc->getReprRoot();
Inkscape::XML::Node *clipnode = sp_repr_lookup_name(root, "inkscape:clipboard", 1);
bool pasted = false;
if (clipnode) {
Geom::Point min, max;
sp_repr_get_point(clipnode, "min", &min);
sp_repr_get_point(clipnode, "max", &max);
// resize each object in the selection
if (separately) {
for (GSList *i = const_cast<GSList*>(selection->itemList()) ; i ; i = i->next) {
SPItem *item = SP_ITEM(i->data);
Geom::OptRect obj_size = item->desktopVisualBounds();
if ( !obj_size ) {
continue;
}
sp_item_scale_rel(item, _getScale(desktop, min, max, *obj_size, apply_x, apply_y));
}
}
// resize the selection as a whole
else {
Geom::OptRect sel_size = selection->visualBounds();
if ( sel_size ) {
sp_selection_scale_relative(selection, sel_size->midpoint(),
_getScale(desktop, min, max, *sel_size, apply_x, apply_y));
}
}
pasted = true;
}
tempdoc->doUnref();
return pasted;
}
/**
* Applies a path effect from the clipboard to the selected path.
*/
bool ClipboardManagerImpl::pastePathEffect(SPDesktop *desktop)
{
/** @todo FIXME: pastePathEffect crashes when moving the path with the applied effect,
segfaulting in fork_private_if_necessary(). */
if ( desktop == NULL ) {
return false;
}
Inkscape::Selection *selection = sp_desktop_selection(desktop);
if (selection && selection->isEmpty()) {
_userWarn(desktop, _("Select <b>object(s)</b> to paste live path effect to."));
return false;
}
SPDocument *tempdoc = _retrieveClipboard("image/x-inkscape-svg");
if ( tempdoc ) {
Inkscape::XML::Node *root = tempdoc->getReprRoot();
Inkscape::XML::Node *clipnode = sp_repr_lookup_name(root, "inkscape:clipboard", 1);
if ( clipnode ) {
gchar const *effectstack = clipnode->attribute("inkscape:path-effect");
if ( effectstack ) {
desktop->doc()->importDefs(tempdoc);
// make sure all selected items are converted to paths first (i.e. rectangles)
sp_selected_to_lpeitems(desktop);
for (GSList *itemptr = const_cast<GSList *>(selection->itemList()) ; itemptr ; itemptr = itemptr->next) {
SPItem *item = reinterpret_cast<SPItem*>(itemptr->data);
_applyPathEffect(item, effectstack);
}
return true;
}
}
}
// no_effect:
_userWarn(desktop, _("No effect on the clipboard."));
return false;
}
/**
* Get LPE path data from the clipboard.
* @return The retrieved path data (contents of the d attribute), or "" if no path was found
*/
Glib::ustring ClipboardManagerImpl::getPathParameter(SPDesktop* desktop)
{
SPDocument *tempdoc = _retrieveClipboard(); // any target will do here
if ( tempdoc == NULL ) {
_userWarn(desktop, _("Nothing on the clipboard."));
return "";
}
Inkscape::XML::Node *root = tempdoc->getReprRoot();
Inkscape::XML::Node *path = sp_repr_lookup_name(root, "svg:path", -1); // unlimited search depth
if ( path == NULL ) {
_userWarn(desktop, _("Clipboard does not contain a path."));
tempdoc->doUnref();
return "";
}
gchar const *svgd = path->attribute("d");
return svgd;
}
/**
* Get object id of a shape or text item from the clipboard.
* @return The retrieved id string (contents of the id attribute), or "" if no shape or text item was found.
*/
Glib::ustring ClipboardManagerImpl::getShapeOrTextObjectId(SPDesktop *desktop)
{
SPDocument *tempdoc = _retrieveClipboard(); // any target will do here
if ( tempdoc == NULL ) {
_userWarn(desktop, _("Nothing on the clipboard."));
return "";
}
Inkscape::XML::Node *root = tempdoc->getReprRoot();
Inkscape::XML::Node *repr = sp_repr_lookup_name(root, "svg:path", -1); // unlimited search depth
if ( repr == NULL ) {
repr = sp_repr_lookup_name(root, "svg:text", -1);
}
if ( repr == NULL ) {
_userWarn(desktop, _("Clipboard does not contain a path."));
tempdoc->doUnref();
return "";
}
gchar const *svgd = repr->attribute("id");
return svgd;
}
/**
* Iterate over a list of items and copy them to the clipboard.
*/
void ClipboardManagerImpl::_copySelection(Inkscape::Selection *selection)
{
GSList const *items = selection->itemList();
// copy the defs used by all items
for (GSList *i = const_cast<GSList *>(items) ; i != NULL ; i = i->next) {
_copyUsedDefs(SP_ITEM (i->data));
}
// copy the representation of the items
GSList *sorted_items = g_slist_copy(const_cast<GSList *>(items));
sorted_items = g_slist_sort(sorted_items, (GCompareFunc) sp_object_compare_position);
for (GSList *i = sorted_items ; i ; i = i->next) {
if (!SP_IS_ITEM(i->data)) {
continue;
}
Inkscape::XML::Node *obj = reinterpret_cast<SPObject *>(i->data)->getRepr();
Inkscape::XML::Node *obj_copy = _copyNode(obj, _doc, _root);
// copy complete inherited style
SPCSSAttr *css = sp_repr_css_attr_inherited(obj, "style");
sp_repr_css_set(obj_copy, css, "style");
sp_repr_css_attr_unref(css);
// write the complete accumulated transform passed to us
// (we're dealing with unattached representations, so we write to their attributes
// instead of using sp_item_set_transform)
gchar *transform_str = sp_svg_transform_write(SP_ITEM(i->data)->i2doc_affine());
obj_copy->setAttribute("transform", transform_str);
g_free(transform_str);
}
// copy style for Paste Style action
if (sorted_items) {
if (SP_IS_ITEM(sorted_items->data)) {
SPCSSAttr *style = take_style_from_item((SPItem *) sorted_items->data);
sp_repr_css_set(_clipnode, style, "style");
sp_repr_css_attr_unref(style);
}
// copy path effect from the first path
if (SP_IS_OBJECT(sorted_items->data)) {
gchar const *effect = reinterpret_cast<SPObject *>(sorted_items->data)->getRepr()->attribute("inkscape:path-effect");
if (effect) {
_clipnode->setAttribute("inkscape:path-effect", effect);
}
}
}
Geom::OptRect size = selection->visualBounds();
if (size) {
sp_repr_set_point(_clipnode, "min", size->min());
sp_repr_set_point(_clipnode, "max", size->max());
}
g_slist_free(sorted_items);
}
/**
* Recursively copy all the definitions used by a given item to the clipboard defs.
*/
void ClipboardManagerImpl::_copyUsedDefs(SPItem *item)
{
// copy fill and stroke styles (patterns and gradients)
SPStyle *style = item->style;
if (style && (style->fill.isPaintserver())) {
SPPaintServer *server = item->style->getFillPaintServer();
if ( SP_IS_LINEARGRADIENT(server) || SP_IS_RADIALGRADIENT(server) ) {
_copyGradient(SP_GRADIENT(server));
}
if ( SP_IS_PATTERN(server) ) {
_copyPattern(SP_PATTERN(server));
}
}
if (style && (style->stroke.isPaintserver())) {
SPPaintServer *server = item->style->getStrokePaintServer();
if ( SP_IS_LINEARGRADIENT(server) || SP_IS_RADIALGRADIENT(server) ) {
_copyGradient(SP_GRADIENT(server));
}
if ( SP_IS_PATTERN(server) ) {
_copyPattern(SP_PATTERN(server));
}
}
// For shapes, copy all of the shape's markers
if (SP_IS_SHAPE(item)) {
SPShape *shape = SP_SHAPE (item);
for (int i = 0 ; i < SP_MARKER_LOC_QTY ; i++) {
if (shape->_marker[i]) {
_copyNode(shape->_marker[i]->getRepr(), _doc, _defs);
}
}
}
// For lpe items, copy lpe stack if applicable
if (SP_IS_LPE_ITEM(item)) {
SPLPEItem *lpeitem = SP_LPE_ITEM (item);
if (sp_lpe_item_has_path_effect(lpeitem)) {
for (PathEffectList::iterator it = lpeitem->path_effect_list->begin(); it != lpeitem->path_effect_list->end(); ++it)
{
LivePathEffectObject *lpeobj = (*it)->lpeobject;
if (lpeobj) {
_copyNode(lpeobj->getRepr(), _doc, _defs);
}
}
}
}
// For 3D boxes, copy perspectives
if (SP_IS_BOX3D(item)) {
_copyNode(box3d_get_perspective(SP_BOX3D(item))->getRepr(), _doc, _defs);
}
// Copy text paths
if (SP_IS_TEXT_TEXTPATH(item)) {
_copyTextPath(SP_TEXTPATH(item->firstChild()));
}
// Copy clipping objects
if (item->clip_ref){
if (item->clip_ref->getObject()) {
_copyNode(item->clip_ref->getObject()->getRepr(), _doc, _defs);
}
}
// Copy mask objects
if (item->mask_ref){
if (item->mask_ref->getObject()) {
SPObject *mask = item->mask_ref->getObject();
_copyNode(mask->getRepr(), _doc, _defs);
// recurse into the mask for its gradients etc.
for (SPObject *o = mask->children ; o != NULL ; o = o->next) {
if (SP_IS_ITEM(o)) {
_copyUsedDefs(SP_ITEM(o));
}
}
}
}
// Copy filters
if (style->getFilter()) {
SPObject *filter = style->getFilter();
if (SP_IS_FILTER(filter)) {
_copyNode(filter->getRepr(), _doc, _defs);
}
}
// recurse
for (SPObject *o = item->children ; o != NULL ; o = o->next) {
if (SP_IS_ITEM(o)) {
_copyUsedDefs(SP_ITEM(o));
}
}
}
/**
* Copy a single gradient to the clipboard's defs element.
*/
void ClipboardManagerImpl::_copyGradient(SPGradient *gradient)
{
while (gradient) {
// climb up the refs, copying each one in the chain
_copyNode(gradient->getRepr(), _doc, _defs);
if (gradient->ref){
gradient = gradient->ref->getObject();
}
else {
gradient = NULL;
}
}
}
/**
* Copy a single pattern to the clipboard document's defs element.
*/
void ClipboardManagerImpl::_copyPattern(SPPattern *pattern)
{
// climb up the references, copying each one in the chain
while (pattern) {
_copyNode(pattern->getRepr(), _doc, _defs);
// items in the pattern may also use gradients and other patterns, so recurse
for ( SPObject *child = pattern->firstChild() ; child ; child = child->getNext() ) {
if (!SP_IS_ITEM (child)) {
continue;
}
_copyUsedDefs(SP_ITEM(child));
}
if (pattern->ref){
pattern = pattern->ref->getObject();
}
else{
pattern = NULL;
}
}
}
/**
* Copy a text path to the clipboard's defs element.
*/
void ClipboardManagerImpl::_copyTextPath(SPTextPath *tp)
{
SPItem *path = sp_textpath_get_path_item(tp);
if (!path) {
return;
}
Inkscape::XML::Node *path_node = path->getRepr();
// Do not copy the text path to defs if it's already copied
if (sp_repr_lookup_child(_root, "id", path_node->attribute("id"))) {
return;
}
_copyNode(path_node, _doc, _defs);
}
/**
* Copy a single XML node from one document to another.
* @param node The node to be copied
* @param target_doc The document to which the node is to be copied
* @param parent The node in the target document which will become the parent of the copied node
* @return Pointer to the copied node
*/
Inkscape::XML::Node *ClipboardManagerImpl::_copyNode(Inkscape::XML::Node *node, Inkscape::XML::Document *target_doc, Inkscape::XML::Node *parent)
{
Inkscape::XML::Node *dup = node->duplicate(target_doc);
parent->appendChild(dup);
Inkscape::GC::release(dup);
return dup;
}
/**
* Retrieve a bitmap image from the clipboard and paste it into the active document.
*/
bool ClipboardManagerImpl::_pasteImage(SPDocument *doc)
{
if ( doc == NULL ) {
return false;
}
// retrieve image data
Glib::RefPtr<Gdk::Pixbuf> img = _clipboard->wait_for_image();
if (!img) {
return false;
}
// TODO unify with interface.cpp's sp_ui_drag_data_received()
// AARGH stupid
Inkscape::Extension::DB::InputList o;
Inkscape::Extension::db.get_input_list(o);
Inkscape::Extension::DB::InputList::const_iterator i = o.begin();
while (i != o.end() && strcmp( (*i)->get_mimetype(), "image/png" ) != 0) {
++i;
}
Inkscape::Extension::Extension *png = *i;
Inkscape::Preferences *prefs = Inkscape::Preferences::get();
Glib::ustring attr = prefs->getString("/dialogs/import/link");
prefs->setString("/dialogs/import/link", "embed");
png->set_gui(false);
gchar *filename = g_build_filename( g_get_tmp_dir(), "inkscape-clipboard-import", NULL );
img->save(filename, "png");
file_import(doc, filename, png);
g_free(filename);
prefs->setString("/dialogs/import/link", attr);
png->set_gui(true);
return true;
}
/**
* Paste text into the selected text object or create a new one to hold it.
*/
bool ClipboardManagerImpl::_pasteText(SPDesktop *desktop)
{
if ( desktop == NULL ) {
return false;
}
// if the text editing tool is active, paste the text into the active text object
if (tools_isactive(desktop, TOOLS_TEXT)) {
return sp_text_paste_inline(desktop->event_context);
}
// try to parse the text as a color and, if successful, apply it as the current style
SPCSSAttr *css = sp_repr_css_attr_parse_color_to_fill(_clipboard->wait_for_text());
if (css) {
sp_desktop_set_style(desktop, css);
return true;
}
return false;
}
/**
* Applies a pasted path effect to a given item.
*/
void ClipboardManagerImpl::_applyPathEffect(SPItem *item, gchar const *effectstack)
{
if ( item == NULL ) {
return;
}
if ( SP_IS_RECT(item) ) {
return;
}
if (SP_IS_LPE_ITEM(item))
{
SPLPEItem *lpeitem = SP_LPE_ITEM(item);
// for each effect in the stack, check if we need to fork it before adding it to the item
sp_lpe_item_fork_path_effects_if_necessary(lpeitem, 1);
std::istringstream iss(effectstack);
std::string href;
while (std::getline(iss, href, ';'))
{
SPObject *obj = sp_uri_reference_resolve(_clipboardSPDoc, href.c_str());
if (!obj) {
return;
}
LivePathEffectObject *lpeobj = LIVEPATHEFFECT(obj);
sp_lpe_item_add_path_effect(lpeitem, lpeobj);
}
}
}
/**
* Retrieve the clipboard contents as a document.
* @return Clipboard contents converted to SPDocument, or NULL if no suitable content was present
*/
SPDocument *ClipboardManagerImpl::_retrieveClipboard(Glib::ustring required_target)
{
Glib::ustring best_target;
if ( required_target == "" ) {
best_target = _getBestTarget();
} else {
best_target = required_target;
}
if ( best_target == "" ) {
return NULL;
}
// FIXME: Temporary hack until we add memory input.
// Save the clipboard contents to some file, then read it
gchar *filename = g_build_filename( g_get_tmp_dir(), "inkscape-clipboard-import", NULL );
bool file_saved = false;
Glib::ustring target = best_target;
#ifdef WIN32
if (best_target == CLIPBOARD_WIN32_EMF_TARGET)
{ // Try to save clipboard data as en emf file (using win32 api)
if (OpenClipboard(NULL)) {
HGLOBAL hglb = GetClipboardData(CF_ENHMETAFILE);
if (hglb) {
HENHMETAFILE hemf = CopyEnhMetaFile((HENHMETAFILE) hglb, filename);
if (hemf) {
file_saved = true;
target = CLIPBOARD_WIN32_EMF_MIME;
DeleteEnhMetaFile(hemf);
}
}
CloseClipboard();
}
}
#endif
if (!file_saved) {
if ( !_clipboard->wait_is_target_available(best_target) ) {
return NULL;
}
// doing this synchronously makes better sense
// TODO: use another method because this one is badly broken imo.
// from documentation: "Returns: A SelectionData object, which will be invalid if retrieving the given target failed."
// I don't know how to check whether an object is 'valid' or not, unusable if that's not possible...
Gtk::SelectionData sel = _clipboard->wait_for_contents(best_target);
target = sel.get_target(); // this can crash if the result was invalid of last function. No way to check for this :(
// FIXME: Temporary hack until we add memory input.
// Save the clipboard contents to some file, then read it
g_file_set_contents(filename, (const gchar *) sel.get_data(), sel.get_length(), NULL);
}
// there is no specific plain SVG input extension, so if we can paste the Inkscape SVG format,
// we use the image/svg+xml mimetype to look up the input extension
if (target == "image/x-inkscape-svg") {
target = "image/svg+xml";
}
Inkscape::Extension::DB::InputList inlist;
Inkscape::Extension::db.get_input_list(inlist);
Inkscape::Extension::DB::InputList::const_iterator in = inlist.begin();
for (; in != inlist.end() && target != (*in)->get_mimetype() ; ++in) {
};
if ( in == inlist.end() ) {
return NULL; // this shouldn't happen unless _getBestTarget returns something bogus
}
SPDocument *tempdoc = NULL;
try {
tempdoc = (*in)->open(filename);
} catch (...) {
}
g_unlink(filename);
g_free(filename);
return tempdoc;
}
/**
* Callback called when some other application requests data from Inkscape.
*
* Finds a suitable output extension to save the internal clipboard document,
* then saves it to memory and sets the clipboard contents.
*/
void ClipboardManagerImpl::_onGet(Gtk::SelectionData &sel, guint /*info*/)
{
g_assert( _clipboardSPDoc != NULL );
Glib::ustring target = sel.get_target();
if (target == "") {
return; // this shouldn't happen
}
if (target == CLIPBOARD_TEXT_TARGET) {
target = "image/x-inkscape-svg";
}
Inkscape::Extension::DB::OutputList outlist;
Inkscape::Extension::db.get_output_list(outlist);
Inkscape::Extension::DB::OutputList::const_iterator out = outlist.begin();
for ( ; out != outlist.end() && target != (*out)->get_mimetype() ; ++out) {
};
if ( out == outlist.end() && target != "image/png") {
return; // this also shouldn't happen
}
// FIXME: Temporary hack until we add support for memory output.
// Save to a temporary file, read it back and then set the clipboard contents
gchar *filename = g_build_filename( g_get_tmp_dir(), "inkscape-clipboard-export", NULL );
gsize len; gchar *data;
try {
if (out == outlist.end() && target == "image/png")
{
gdouble dpi = PX_PER_IN;
guint32 bgcolor = 0x00000000;
Geom::Point origin (_clipboardSPDoc->getRoot()->x.computed, _clipboardSPDoc->getRoot()->y.computed);
Geom::Rect area = Geom::Rect(origin, origin + _clipboardSPDoc->getDimensions());
unsigned long int width = (unsigned long int) (area.width() * dpi / PX_PER_IN + 0.5);
unsigned long int height = (unsigned long int) (area.height() * dpi / PX_PER_IN + 0.5);
// read from namedview
Inkscape::XML::Node *nv = sp_repr_lookup_name (_clipboardSPDoc->rroot, "sodipodi:namedview");
if (nv && nv->attribute("pagecolor")) {
bgcolor = sp_svg_read_color(nv->attribute("pagecolor"), 0xffffff00);
}
if (nv && nv->attribute("inkscape:pageopacity")) {
double opacity = 1.0;
sp_repr_get_double(nv, "inkscape:pageopacity", &opacity);
bgcolor |= SP_COLOR_F_TO_U(opacity);
}
sp_export_png_file(_clipboardSPDoc, filename, area, width, height, dpi, dpi, bgcolor, NULL, NULL, true, NULL);
}
else
{
if (!(*out)->loaded()) {
// Need to load the extension.
(*out)->set_state(Inkscape::Extension::Extension::STATE_LOADED);
}
(*out)->save(_clipboardSPDoc, filename);
}
g_file_get_contents(filename, &data, &len, NULL);
sel.set(8, (guint8 const *) data, len);
} catch (...) {
}
g_unlink(filename); // delete the temporary file
g_free(filename);
}
/**
* Callback when someone else takes the clipboard.
*
* When the clipboard owner changes, this callback clears the internal clipboard document
* to reduce memory usage.
*/
void ClipboardManagerImpl::_onClear()
{
// why is this called before _onGet???
//_discardInternalClipboard();
}
/**
* Creates an internal clipboard document from scratch.
*/
void ClipboardManagerImpl::_createInternalClipboard()
{
if ( _clipboardSPDoc == NULL ) {
_clipboardSPDoc = SPDocument::createNewDoc(NULL, false, true);
//g_assert( _clipboardSPDoc != NULL );
_defs = _clipboardSPDoc->getDefs()->getRepr();
_doc = _clipboardSPDoc->getReprDoc();
_root = _clipboardSPDoc->getReprRoot();
_clipnode = _doc->createElement("inkscape:clipboard");
_root->appendChild(_clipnode);
Inkscape::GC::release(_clipnode);
// once we create a SVG document, style will be stored in it, so flush _text_style
if (_text_style) {
sp_repr_css_attr_unref(_text_style);
_text_style = NULL;
}
}
}
/**
* Deletes the internal clipboard document.
*/
void ClipboardManagerImpl::_discardInternalClipboard()
{
if ( _clipboardSPDoc != NULL ) {
_clipboardSPDoc->doUnref();
_clipboardSPDoc = NULL;
_defs = NULL;
_doc = NULL;
_root = NULL;
_clipnode = NULL;
}
}
/**
* Get the scale to resize an item, based on the command and desktop state.
*/
Geom::Scale ClipboardManagerImpl::_getScale(SPDesktop *desktop, Geom::Point const &min, Geom::Point const &max, Geom::Rect const &obj_rect, bool apply_x, bool apply_y)
{
double scale_x = 1.0;
double scale_y = 1.0;
if (apply_x) {
scale_x = (max[Geom::X] - min[Geom::X]) / obj_rect[Geom::X].extent();
}
if (apply_y) {
scale_y = (max[Geom::Y] - min[Geom::Y]) / obj_rect[Geom::Y].extent();
}
// If the "lock aspect ratio" button is pressed and we paste only a single coordinate,
// resize the second one by the same ratio too
if (desktop->isToolboxButtonActive("lock")) {
if (apply_x && !apply_y) {
scale_y = scale_x;
}
if (apply_y && !apply_x) {
scale_x = scale_y;
}
}
return Geom::Scale(scale_x, scale_y);
}
/**
* Find the most suitable clipboard target.
*/
Glib::ustring ClipboardManagerImpl::_getBestTarget()
{
// GTKmm's wait_for_targets() is broken, see the comment in _inkscape_wait_for_targets()
std::list<Glib::ustring> targets; // = _clipboard->wait_for_targets();
_inkscape_wait_for_targets(targets);
// clipboard target debugging snippet
/*
g_debug("Begin clipboard targets");
for ( std::list<Glib::ustring>::iterator x = targets.begin() ; x != targets.end(); ++x )
g_debug("Clipboard target: %s", (*x).data());
g_debug("End clipboard targets\n");
//*/
for (std::list<Glib::ustring>::iterator i = _preferred_targets.begin() ;
i != _preferred_targets.end() ; ++i)
{
if ( std::find(targets.begin(), targets.end(), *i) != targets.end() ) {
return *i;
}
}
#ifdef WIN32
if (OpenClipboard(NULL))
{ // If both bitmap and metafile are present, pick the one that was exported first.
UINT format = EnumClipboardFormats(0);
while (format) {
if (format == CF_ENHMETAFILE || format == CF_DIB || format == CF_BITMAP) {
break;
}
format = EnumClipboardFormats(format);
}
CloseClipboard();
if (format == CF_ENHMETAFILE) {
return CLIPBOARD_WIN32_EMF_TARGET;
}
if (format == CF_DIB || format == CF_BITMAP) {
return CLIPBOARD_GDK_PIXBUF_TARGET;
}
}
if (IsClipboardFormatAvailable(CF_ENHMETAFILE)) {
return CLIPBOARD_WIN32_EMF_TARGET;
}
#endif
if (_clipboard->wait_is_image_available()) {
return CLIPBOARD_GDK_PIXBUF_TARGET;
}
if (_clipboard->wait_is_text_available()) {
return CLIPBOARD_TEXT_TARGET;
}
return "";
}
/**
* Set the clipboard targets to reflect the mimetypes Inkscape can output.
*/
void ClipboardManagerImpl::_setClipboardTargets()
{
Inkscape::Extension::DB::OutputList outlist;
Inkscape::Extension::db.get_output_list(outlist);
std::list<Gtk::TargetEntry> target_list;
bool plaintextSet = false;
for (Inkscape::Extension::DB::OutputList::const_iterator out = outlist.begin() ; out != outlist.end() ; ++out) {
if ( !(*out)->deactivated() ) {
Glib::ustring mime = (*out)->get_mimetype();
if (mime != CLIPBOARD_TEXT_TARGET) {
if ( !plaintextSet && (mime.find("svg") == Glib::ustring::npos) ) {
target_list.push_back(Gtk::TargetEntry(CLIPBOARD_TEXT_TARGET));
plaintextSet = true;
}
target_list.push_back(Gtk::TargetEntry(mime));
}
}
}
// Add PNG export explicitly since there is no extension for this...
// On Windows, GTK will also present this as a CF_DIB/CF_BITMAP
target_list.push_back(Gtk::TargetEntry( "image/png" ));
_clipboard->set(target_list,
sigc::mem_fun(*this, &ClipboardManagerImpl::_onGet),
sigc::mem_fun(*this, &ClipboardManagerImpl::_onClear));
#ifdef WIN32
// If the "image/x-emf" target handled by the emf extension would be
// presented as a CF_ENHMETAFILE automatically (just like an "image/bmp"
// is presented as a CF_BITMAP) this code would not be needed.. ???
// Or maybe there is some other way to achieve the same?
// Note: Metafile is the only format that is rendered and stored in clipboard
// on Copy, all other formats are rendered only when needed by a Paste command.
// FIXME: This should at least be rewritten to use "delayed rendering".
// If possible make it delayed rendering by using GTK API only.
if (OpenClipboard(NULL)) {
if ( _clipboardSPDoc != NULL ) {
const Glib::ustring target = CLIPBOARD_WIN32_EMF_MIME;
Inkscape::Extension::DB::OutputList outlist;
Inkscape::Extension::db.get_output_list(outlist);
Inkscape::Extension::DB::OutputList::const_iterator out = outlist.begin();
for ( ; out != outlist.end() && target != (*out)->get_mimetype() ; ++out) {
}
if ( out != outlist.end() ) {
// FIXME: Temporary hack until we add support for memory output.
// Save to a temporary file, read it back and then set the clipboard contents
gchar *filename = g_build_filename( g_get_tmp_dir(), "inkscape-clipboard-export.emf", NULL );
try {
(*out)->save(_clipboardSPDoc, filename);
HENHMETAFILE hemf = GetEnhMetaFileA(filename);
if (hemf) {
SetClipboardData(CF_ENHMETAFILE, hemf);
DeleteEnhMetaFile(hemf);
}
} catch (...) {
}
g_unlink(filename); // delete the temporary file
g_free(filename);
}
}
CloseClipboard();
}
#endif
}
/**
* Set the string representation of a 32-bit RGBA color as the clipboard contents.
*/
void ClipboardManagerImpl::_setClipboardColor(guint32 color)
{
gchar colorstr[16];
g_snprintf(colorstr, 16, "%08x", color);
_clipboard->set_text(colorstr);
}
/**
* Put a notification on the mesage stack.
*/
void ClipboardManagerImpl::_userWarn(SPDesktop *desktop, char const *msg)
{
desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, msg);
}
// GTKMM's clipboard::wait_for_targets is buggy and might return bogus, see
//
// https://bugs.launchpad.net/inkscape/+bug/296778
// http://mail.gnome.org/archives/gtk-devel-list/2009-June/msg00062.html
//
// for details. Until this has been fixed upstream we will use our own implementation
// of this method, as copied from /gtkmm-2.16.0/gtk/gtkmm/clipboard.cc.
void ClipboardManagerImpl::_inkscape_wait_for_targets(std::list<Glib::ustring> &listTargets)
{
//Get a newly-allocated array of atoms:
GdkAtom* targets = NULL;
gint n_targets = 0;
gboolean test = gtk_clipboard_wait_for_targets( gtk_clipboard_get(GDK_SELECTION_CLIPBOARD), &targets, &n_targets );
if (!test || (targets == NULL)) {
return;
}
//Add the targets to the C++ container:
for (int i = 0; i < n_targets; i++)
{
//Convert the atom to a string:
gchar* const atom_name = gdk_atom_name(targets[i]);
Glib::ustring target;
if (atom_name) {
target = Glib::ScopedPtr<char>(atom_name).get(); //This frees the gchar*.
}
listTargets.push_back(target);
}
}
/* #######################################
ClipboardManager class
####################################### */
ClipboardManager *ClipboardManager::_instance = NULL;
ClipboardManager::ClipboardManager() {}
ClipboardManager::~ClipboardManager() {}
ClipboardManager *ClipboardManager::get()
{
if ( _instance == NULL ) {
_instance = new ClipboardManagerImpl;
}
return _instance;
}
} // namespace Inkscape
} // namespace IO
/*
Local Variables:
mode:c++
c-file-style:"stroustrup"
c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
indent-tabs-mode:nil
fill-column:99
End:
*/
// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :
|