1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
4 *
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/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
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 .
18 */
19
20
21 // ATTENTION: We assume StarView Clipboard format numbers and Windows
22 // Format numbers to be the same! If that's not the case, we need to
23 // adapt the code here. The implementation uses the conversions here.
24
25 #include <string.h>
26 #include "ddeimp.hxx"
27 #include <svl/svdde.hxx>
28 #include <o3tl/char16_t2wchar_t.hxx>
29
30 #include <osl/thread.h>
31 #include <sot/exchange.hxx>
32
DdeData()33 DdeData::DdeData()
34 {
35 xImp.reset(new DdeDataImp);
36 xImp->hData = nullptr;
37 xImp->nData = 0;
38 xImp->pData = nullptr;
39 xImp->nFmt = SotClipboardFormatId::STRING;
40 }
41
DdeData(const void * p,tools::Long n,SotClipboardFormatId f)42 DdeData::DdeData(const void* p, tools::Long n, SotClipboardFormatId f)
43 {
44 xImp.reset(new DdeDataImp);
45 xImp->hData = nullptr;
46 xImp->pData = p;
47 xImp->nData = n;
48 xImp->nFmt = f;
49 }
50
DdeData(const OUString & s)51 DdeData::DdeData( const OUString& s )
52 {
53 xImp.reset(new DdeDataImp);
54 xImp->hData = nullptr;
55 xImp->pData = s.getStr();
56 xImp->nData = s.getLength()+1;
57 xImp->nFmt = SotClipboardFormatId::STRING;
58 }
59
DdeData(const DdeData & rData)60 DdeData::DdeData(const DdeData& rData)
61 {
62 xImp.reset(new DdeDataImp);
63 xImp->hData = rData.xImp->hData;
64 xImp->nData = rData.xImp->nData;
65 xImp->pData = rData.xImp->pData;
66 xImp->nFmt = rData.xImp->nFmt;
67 Lock();
68 }
69
DdeData(DdeData && rData)70 DdeData::DdeData(DdeData&& rData) noexcept
71 : xImp(std::move(rData.xImp))
72 {
73 }
74
~DdeData()75 DdeData::~DdeData()
76 {
77 if (xImp && xImp->hData)
78 DdeUnaccessData(xImp->hData);
79 }
80
Lock()81 void DdeData::Lock()
82 {
83 if (xImp->hData)
84 xImp->pData = DdeAccessData(xImp->hData, &xImp->nData);
85 }
86
GetFormat() const87 SotClipboardFormatId DdeData::GetFormat() const
88 {
89 return xImp->nFmt;
90 }
91
SetFormat(SotClipboardFormatId nFmt)92 void DdeData::SetFormat(SotClipboardFormatId nFmt)
93 {
94 xImp->nFmt = nFmt;
95 }
96
getData() const97 void const * DdeData::getData() const
98 {
99 return xImp->pData;
100 }
101
getSize() const102 tools::Long DdeData::getSize() const
103 {
104 return xImp->nData;
105 }
106
operator =(const DdeData & rData)107 DdeData& DdeData::operator=(const DdeData& rData)
108 {
109 if ( &rData != this )
110 {
111 DdeData tmp(rData);
112 xImp = std::move(tmp.xImp);
113 }
114
115 return *this;
116 }
117
operator =(DdeData && rData)118 DdeData& DdeData::operator=(DdeData&& rData) noexcept
119 {
120 xImp = std::move(rData.xImp);
121 return *this;
122 }
123
GetExternalFormat(SotClipboardFormatId nFmt)124 sal_uInt32 DdeData::GetExternalFormat(SotClipboardFormatId nFmt)
125 {
126 switch( nFmt )
127 {
128 case SotClipboardFormatId::STRING:
129 return CF_TEXT;
130 case SotClipboardFormatId::BITMAP:
131 return CF_BITMAP;
132 case SotClipboardFormatId::GDIMETAFILE:
133 return CF_METAFILEPICT;
134 default:
135 {
136 OUString aName( SotExchange::GetFormatName( nFmt ) );
137 if( !aName.isEmpty() )
138 return RegisterClipboardFormatW( o3tl::toW(aName.getStr()) );
139 }
140 }
141 return static_cast<sal_uInt32>(nFmt);
142 }
143
GetInternalFormat(sal_uLong nFmt)144 SotClipboardFormatId DdeData::GetInternalFormat(sal_uLong nFmt)
145 {
146 switch( nFmt )
147 {
148 case CF_TEXT:
149 return SotClipboardFormatId::STRING;
150 case CF_BITMAP:
151 return SotClipboardFormatId::BITMAP;
152 case CF_METAFILEPICT:
153 return SotClipboardFormatId::GDIMETAFILE;
154 default:
155 if( nFmt >= CF_MAX )
156 {
157 WCHAR szName[ 256 ];
158
159 if(GetClipboardFormatNameW( nFmt, szName, SAL_N_ELEMENTS(szName) ))
160 return SotExchange::RegisterFormatName( OUString(o3tl::toU(szName)) );
161 }
162 break;
163 }
164 return static_cast<SotClipboardFormatId>(nFmt);
165 }
166
167 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
168