1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
22 #include <pastedlg.hxx>
23 #include <svtools/insdlg.hxx>
24 #include <sot/exchange.hxx>
25 #include <sot/formats.hxx>
26 #include <svtools/strings.hrc>
27 #include <svtools/svtresid.hxx>
28 #include <tools/lineend.hxx>
29 #include <comphelper/dispatchcommand.hxx>
30 #include <com/sun/star/beans/PropertyValue.hpp>
32 SvPasteObjectDialog::SvPasteObjectDialog(weld::Window
* pParent
)
33 : GenericDialogController(pParent
, u
"cui/ui/pastespecial.ui"_ustr
, u
"PasteSpecialDialog"_ustr
)
34 , m_xFtObjectSource(m_xBuilder
->weld_label(u
"source"_ustr
))
35 , m_xLbInsertList(m_xBuilder
->weld_tree_view(u
"list"_ustr
))
36 , m_xOKButton(m_xBuilder
->weld_button(u
"ok"_ustr
))
38 m_xLbInsertList
->set_size_request(m_xLbInsertList
->get_approximate_digit_width() * 40,
39 m_xLbInsertList
->get_height_rows(6));
40 m_xOKButton
->set_sensitive(false);
42 ObjectLB().connect_selection_changed(LINK(this, SvPasteObjectDialog
, SelectHdl
));
43 ObjectLB().connect_row_activated(LINK( this, SvPasteObjectDialog
, DoubleClickHdl
));
46 void SvPasteObjectDialog::SelectObject()
48 if (m_xLbInsertList
->n_children())
50 m_xLbInsertList
->select(0);
51 SelectHdl(*m_xLbInsertList
);
55 IMPL_LINK_NOARG(SvPasteObjectDialog
, SelectHdl
, weld::TreeView
&, void)
57 if (!m_xOKButton
->get_sensitive())
58 m_xOKButton
->set_sensitive(true);
61 IMPL_LINK_NOARG(SvPasteObjectDialog
, DoubleClickHdl
, weld::TreeView
&, bool)
63 m_xDialog
->response(RET_OK
);
67 /*************************************************************************
68 |* SvPasteObjectDialog::Insert()
69 *************************************************************************/
70 void SvPasteObjectDialog::Insert( SotClipboardFormatId nFormat
, const OUString
& rFormatName
)
72 aSupplementMap
.insert( std::make_pair( nFormat
, rFormatName
) );
75 void SvPasteObjectDialog::InsertUno(const OUString
& sCmd
, const OUString
& sLabel
)
77 aExtraCommand
.first
= sCmd
;
78 aExtraCommand
.second
= sLabel
;
82 void SvPasteObjectDialog::PreGetFormat( const TransferableDataHelper
&rHelper
)
84 //TODO/LATER: why is the Descriptor never used?!
85 TransferableObjectDescriptor aDesc
;
86 if (rHelper
.HasFormat(SotClipboardFormatId::OBJECTDESCRIPTOR
))
88 (void)rHelper
.GetTransferableObjectDescriptor(
89 SotClipboardFormatId::OBJECTDESCRIPTOR
, aDesc
);
91 const DataFlavorExVector
* pFormats
= &rHelper
.GetDataFlavorExVector();
93 // create and fill dialog box
94 OUString aSourceName
, aTypeName
;
95 SvGlobalName aEmptyNm
;
97 //ObjectLB().SetUpdateMode( false );
100 DataFlavorExVector::iterator
aIter( const_cast<DataFlavorExVector
&>(*pFormats
).begin() ),
101 aEnd( const_cast<DataFlavorExVector
&>(*pFormats
).end() );
102 while( aIter
!= aEnd
)
104 SotClipboardFormatId nFormat
= (*aIter
++).mnSotId
;
106 std::map
< SotClipboardFormatId
, OUString
>::iterator itName
=
107 aSupplementMap
.find( nFormat
);
109 // if there is an "Embed Source" or and "Embedded Object" on the
110 // Clipboard we read the Description and the Source of this object
111 // from an accompanied "Object Descriptor" format on the clipboard
112 // Remember: these formats mostly appear together on the clipboard
114 const OUString
* pName
= nullptr;
115 if ( itName
== aSupplementMap
.end() )
117 SvPasteObjectHelper::GetEmbeddedName(rHelper
,aName
,aSourceName
,nFormat
);
118 if ( !aName
.isEmpty() )
123 pName
= &(itName
->second
);
130 if( SotClipboardFormatId::EMBED_SOURCE
== nFormat
)
132 if( aDesc
.maClassName
!= aEmptyNm
)
134 aSourceName
= aDesc
.maDisplayName
;
136 if( aDesc
.maClassName
== aObjClassName
)
139 aName
= aTypeName
= aDesc
.maTypeName
;
142 else if( SotClipboardFormatId::LINK_SOURCE
== nFormat
)
146 else if( aName
.isEmpty() )
147 aName
= SvPasteObjectHelper::GetSotFormatUIName( nFormat
);
149 // Show RICHTEXT only in case RTF is not present.
150 if (nFormat
== SotClipboardFormatId::RICHTEXT
&&
151 std::any_of(pFormats
->begin(), pFormats
->end(),
152 [](const DataFlavorEx
& rFlavor
) {
153 return rFlavor
.mnSotId
== SotClipboardFormatId::RTF
;
159 if (ObjectLB().find_text(aName
) == -1)
161 ObjectLB().append(OUString::number(static_cast<sal_uInt32
>(nFormat
)), aName
);
166 if( aTypeName
.isEmpty() && aSourceName
.isEmpty() )
168 if( aDesc
.maClassName
!= aEmptyNm
)
170 aSourceName
= aDesc
.maDisplayName
;
171 aTypeName
= aDesc
.maTypeName
;
174 if( aTypeName
.isEmpty() && aSourceName
.isEmpty() )
176 // global resource from svtools (former so3 resource)
177 aSourceName
= SvtResId(STR_UNKNOWN_SOURCE
);
184 if( !aSourceName
.isEmpty() )
186 if( !aTypeName
.isEmpty() )
189 aTypeName
+= aSourceName
;
190 aTypeName
= convertLineEnd(aTypeName
, GetSystemLineEnd());
193 m_xFtObjectSource
->set_label(aTypeName
);
196 SotClipboardFormatId
SvPasteObjectDialog::GetFormatOnly()
198 return static_cast<SotClipboardFormatId
>(ObjectLB().get_selected_id().toUInt32());
201 SotClipboardFormatId
SvPasteObjectDialog::GetFormat( const TransferableDataHelper
& rHelper
)
203 //TODO/LATER: why is the Descriptor never used?!
204 TransferableObjectDescriptor aDesc
;
205 if (rHelper
.HasFormat(SotClipboardFormatId::OBJECTDESCRIPTOR
))
207 (void)rHelper
.GetTransferableObjectDescriptor(
208 SotClipboardFormatId::OBJECTDESCRIPTOR
, aDesc
);
210 const DataFlavorExVector
* pFormats
= &rHelper
.GetDataFlavorExVector();
212 // create and fill dialog box
213 OUString aSourceName
, aTypeName
;
214 SotClipboardFormatId nSelFormat
= SotClipboardFormatId::NONE
;
215 SvGlobalName aEmptyNm
;
219 for (auto const& format
: *pFormats
)
221 SotClipboardFormatId nFormat
= format
.mnSotId
;
223 std::map
< SotClipboardFormatId
, OUString
>::iterator itName
=
224 aSupplementMap
.find( nFormat
);
226 // if there is an "Embed Source" or and "Embedded Object" on the
227 // Clipboard we read the Description and the Source of this object
228 // from an accompanied "Object Descriptor" format on the clipboard
229 // Remember: these formats mostly appear together on the clipboard
231 const OUString
* pName
= nullptr;
232 if ( itName
== aSupplementMap
.end() )
234 SvPasteObjectHelper::GetEmbeddedName(rHelper
,aName
,aSourceName
,nFormat
);
235 if ( !aName
.isEmpty() )
240 pName
= &(itName
->second
);
247 if( SotClipboardFormatId::EMBED_SOURCE
== nFormat
)
249 if( aDesc
.maClassName
!= aEmptyNm
)
251 aSourceName
= aDesc
.maDisplayName
;
253 if( aDesc
.maClassName
== aObjClassName
)
256 aName
= aTypeName
= aDesc
.maTypeName
;
259 else if( SotClipboardFormatId::LINK_SOURCE
== nFormat
)
263 else if( aName
.isEmpty() )
264 aName
= SvPasteObjectHelper::GetSotFormatUIName( nFormat
);
266 // Show RICHTEXT only in case RTF is not present.
267 if (nFormat
== SotClipboardFormatId::RICHTEXT
&&
268 std::any_of(pFormats
->begin(), pFormats
->end(),
269 [](const DataFlavorEx
& rFlavor
) {
270 return rFlavor
.mnSotId
== SotClipboardFormatId::RTF
;
276 if (ObjectLB().find_text(aName
) == -1)
278 ObjectLB().append(OUString::number(static_cast<sal_uInt32
>(nFormat
)), aName
);
283 if( aTypeName
.isEmpty() && aSourceName
.isEmpty() )
285 if( aDesc
.maClassName
!= aEmptyNm
)
287 aSourceName
= aDesc
.maDisplayName
;
288 aTypeName
= aDesc
.maTypeName
;
291 if( aTypeName
.isEmpty() && aSourceName
.isEmpty() )
293 // global resource from svtools (former so3 resource)
294 aSourceName
= SvtResId(STR_UNKNOWN_SOURCE
);
298 if (!aExtraCommand
.first
.isEmpty())
300 ObjectLB().append(aExtraCommand
.first
, aExtraCommand
.second
);
306 if( !aSourceName
.isEmpty() )
308 if( !aTypeName
.isEmpty() )
311 aTypeName
+= aSourceName
;
312 aTypeName
= convertLineEnd(aTypeName
, GetSystemLineEnd());
315 m_xFtObjectSource
->set_label(aTypeName
);
319 if (ObjectLB().get_selected_id().startsWithIgnoreAsciiCase(".uno"))
321 comphelper::dispatchCommand(aExtraCommand
.first
, {});
322 nSelFormat
= SotClipboardFormatId::NONE
;
326 nSelFormat
= static_cast<SotClipboardFormatId
>(ObjectLB().get_selected_id().toUInt32());
333 void SvPasteObjectDialog::SetObjName( const SvGlobalName
& rClass
, const OUString
& rObjName
)
335 aObjClassName
= rClass
;
339 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */