xref: /aoo41x/main/svx/source/items/hlnkitem.cxx (revision cdf0e10c)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_svx.hxx"
30 
31 // include ---------------------------------------------------------------
32 #define _SVX_HLNKITEM_CXX
33 
34 #ifndef _SVX_SVXIDS_HRC
35 #include <svx/svxids.hrc>
36 #endif
37 #include <tools/stream.hxx>
38 
39 #ifndef _MEMBERID_HRC
40 #include <svx/memberid.hrc>
41 #endif
42 
43 #ifndef __SBX_SBXVARIABLE_HXX
44 #include <basic/sbxvar.hxx>
45 #endif
46 
47 #include "svx/hlnkitem.hxx"
48 
49 // -----------------------------------------------------------------------
50 
51 TYPEINIT1_FACTORY(SvxHyperlinkItem, SfxPoolItem, new SvxHyperlinkItem(0));
52 
53 // class SvxHyperlinkItem ------------------------------------------------
54 
55 /*--------------------------------------------------------------------
56 	Beschreibung:
57  --------------------------------------------------------------------*/
58 
59 #define HYPERLINKFF_MARKER	0x599401FE
60 
61 SvStream& SvxHyperlinkItem::Store( SvStream& rStrm, sal_uInt16 /*nItemVersion*/ ) const
62 {
63 	// store 'simple' data
64 	// UNICODE: rStrm << sName;
65 	rStrm.WriteByteString(sName);
66 
67 	// UNICODE: rStrm << sURL;
68 	rStrm.WriteByteString(sURL);
69 
70 	// UNICODE: rStrm << sTarget;
71 	rStrm.WriteByteString(sTarget);
72 
73 	rStrm << (sal_uInt32) eType;
74 
75 	// marker for versioninfo
76 	rStrm << (sal_uInt32) HYPERLINKFF_MARKER;
77 
78 	// new data
79 	// UNICODE: rStrm << sIntName;
80 	rStrm.WriteByteString(sIntName);
81 
82 	// macro-events
83 	rStrm << nMacroEvents;
84 
85 	// store macros
86 	sal_uInt16 nCnt = pMacroTable ? (sal_uInt16)pMacroTable->Count() : 0;
87 	sal_uInt16 nMax = nCnt;
88 	if( nCnt )
89 	{
90 		for( SvxMacro* pMac = pMacroTable->First(); pMac; pMac = pMacroTable->Next() )
91 			if( STARBASIC != pMac->GetScriptType() )
92 				--nCnt;
93 	}
94 
95 	rStrm << nCnt;
96 
97 	if( nCnt )
98 	{
99 		// 1. StarBasic-Macros
100 		for( SvxMacro* pMac = pMacroTable->First(); pMac; pMac = pMacroTable->Next() )
101 		{
102 			if( STARBASIC == pMac->GetScriptType() )
103 			{
104 				rStrm << (sal_uInt16)pMacroTable->GetCurKey();
105 
106 				// UNICODE: rStrm << pMac->GetLibName();
107 				rStrm.WriteByteString(pMac->GetLibName());
108 
109 				// UNICODE: rStrm << pMac->GetMacName();
110 				rStrm.WriteByteString(pMac->GetMacName());
111 			}
112 		}
113 	}
114 
115 	nCnt = nMax - nCnt;
116 	rStrm << nCnt;
117 	if( nCnt )
118 	{
119 		// 2. ::com::sun::star::script::JavaScript-Macros
120 		for( SvxMacro* pMac = pMacroTable->First(); pMac; pMac = pMacroTable->Next() )
121 		{
122 			if( STARBASIC != pMac->GetScriptType() )
123 			{
124 				rStrm << (sal_uInt16)pMacroTable->GetCurKey();
125 
126 				// UNICODE: rStrm << pMac->GetLibName();
127 				rStrm.WriteByteString(pMac->GetLibName());
128 
129 				// UNICODE: rStrm << pMac->GetMacName();
130 				rStrm.WriteByteString(pMac->GetMacName());
131 
132 				rStrm << (sal_uInt16)pMac->GetScriptType();
133 			}
134 		}
135 	}
136 
137 	return rStrm;
138 }
139 
140 /*--------------------------------------------------------------------
141 	Beschreibung:
142  --------------------------------------------------------------------*/
143 
144 SfxPoolItem*    SvxHyperlinkItem::Create( SvStream &rStrm, sal_uInt16 /*nItemVersion*/ ) const
145 {
146 	SvxHyperlinkItem* pNew = new SvxHyperlinkItem( Which() );
147 	sal_uInt32 nType;
148 
149 	// simple data-types
150 	// UNICODE: rStrm >> pNew->sName;
151 	rStrm.ReadByteString(pNew->sName);
152 
153 	// UNICODE: rStrm >> pNew->sURL;
154 	rStrm.ReadByteString(pNew->sURL);
155 
156 	// UNICODE: rStrm >> pNew->sTarget;
157 	rStrm.ReadByteString(pNew->sTarget);
158 
159 	rStrm >> nType;
160 	pNew->eType = (SvxLinkInsertMode) nType;
161 
162 	sal_uInt32 nPos = rStrm.Tell();
163 	sal_uInt32 nMarker;
164 	rStrm >> nMarker;
165 	if ( nMarker == HYPERLINKFF_MARKER )
166 	{
167 		// new data
168 		// UNICODE: rStrm >> pNew->sIntName;
169 		rStrm.ReadByteString(pNew->sIntName);
170 
171 		// macro-events
172 		rStrm >> pNew->nMacroEvents;
173 
174 		// macros
175 		sal_uInt16 nCnt;
176 		rStrm >> nCnt;
177 		while( nCnt-- )
178 		{
179 			sal_uInt16 nCurKey;
180 			String aLibName, aMacName;
181 
182 			rStrm >> nCurKey;
183 			// UNICODE: rStrm >> aLibName;
184 			rStrm.ReadByteString(aLibName);
185 
186 			// UNICODE: rStrm >> aMacName;
187 			rStrm.ReadByteString(aMacName);
188 
189 			pNew->SetMacro( nCurKey, SvxMacro( aMacName, aLibName, STARBASIC ) );
190 		}
191 
192 		rStrm >> nCnt;
193 		while( nCnt-- )
194 		{
195 			sal_uInt16 nCurKey, nScriptType;
196 			String aLibName, aMacName;
197 
198 			rStrm >> nCurKey;
199 
200 			// UNICODE: rStrm >> aLibName;
201 			rStrm.ReadByteString(aLibName);
202 
203 			// UNICODE: rStrm >> aMacName;
204 			rStrm.ReadByteString(aMacName);
205 
206 			rStrm >> nScriptType;
207 
208 			pNew->SetMacro( nCurKey, SvxMacro( aMacName, aLibName,
209 										(ScriptType)nScriptType ) );
210 		}
211 	}
212 	else
213 		rStrm.Seek( nPos );
214 
215 	return pNew;
216 }
217 
218 /*--------------------------------------------------------------------
219 	Beschreibung:
220  --------------------------------------------------------------------*/
221 
222 SvxHyperlinkItem::SvxHyperlinkItem( const SvxHyperlinkItem& rHyperlinkItem ):
223 			SfxPoolItem(rHyperlinkItem)
224 {
225 	sName   = rHyperlinkItem.sName;
226 	sURL    = rHyperlinkItem.sURL;
227 	sTarget = rHyperlinkItem.sTarget;
228 	eType   = rHyperlinkItem.eType;
229 	sIntName = rHyperlinkItem.sIntName;
230 	nMacroEvents = rHyperlinkItem.nMacroEvents;
231 
232 	if( rHyperlinkItem.GetMacroTbl() )
233 		pMacroTable = new SvxMacroTableDtor( *rHyperlinkItem.GetMacroTbl() );
234 	else
235 		pMacroTable=NULL;
236 
237 };
238 
239 /*--------------------------------------------------------------------
240 	Beschreibung:
241  --------------------------------------------------------------------*/
242 
243 SvxHyperlinkItem::SvxHyperlinkItem( sal_uInt16 _nWhich, String& rName, String& rURL,
244 								    String& rTarget, String& rIntName, SvxLinkInsertMode eTyp,
245 									sal_uInt16 nEvents, SvxMacroTableDtor *pMacroTbl ):
246     SfxPoolItem (_nWhich),
247 	sName		(rName),
248 	sURL    	(rURL),
249 	sTarget 	(rTarget),
250 	eType   	(eTyp),
251 	sIntName (rIntName),
252 	nMacroEvents (nEvents)
253 {
254 	if (pMacroTbl)
255 		pMacroTable = new SvxMacroTableDtor ( *pMacroTbl );
256 	else
257 		pMacroTable=NULL;
258 }
259 
260 /*--------------------------------------------------------------------
261 	Beschreibung:
262  --------------------------------------------------------------------*/
263 
264 SfxPoolItem* SvxHyperlinkItem::Clone( SfxItemPool* ) const
265 {
266 	return new SvxHyperlinkItem( *this );
267 }
268 
269 /*--------------------------------------------------------------------
270 	Beschreibung:
271  --------------------------------------------------------------------*/
272 
273 int SvxHyperlinkItem::operator==( const SfxPoolItem& rAttr ) const
274 {
275 	DBG_ASSERT( SfxPoolItem::operator==(rAttr), "unterschiedliche Typen" );
276 
277 	const SvxHyperlinkItem& rItem = (const SvxHyperlinkItem&) rAttr;
278 
279 	sal_Bool bRet = ( sName   == rItem.sName   &&
280 				  sURL    == rItem.sURL    &&
281 				  sTarget == rItem.sTarget &&
282 				  eType   == rItem.eType   &&
283 				  sIntName == rItem.sIntName &&
284 				  nMacroEvents == rItem.nMacroEvents);
285 	if (!bRet)
286 		return sal_False;
287 
288 	const SvxMacroTableDtor* pOther = ((SvxHyperlinkItem&)rAttr).pMacroTable;
289 	if( !pMacroTable )
290 		return ( !pOther || !pOther->Count() );
291 	if( !pOther )
292 		return 0 == pMacroTable->Count();
293 
294 	const SvxMacroTableDtor& rOwn = *pMacroTable;
295 	const SvxMacroTableDtor& rOther = *pOther;
296 
297 	// Anzahl unterschiedlich => auf jeden Fall ungleich
298 	if( rOwn.Count() != rOther.Count() )
299 		return sal_False;
300 
301 	// einzeln vergleichen; wegen Performance ist die Reihenfolge wichtig
302 	for( sal_uInt16 nNo = 0; nNo < rOwn.Count(); ++nNo )
303 	{
304 		const SvxMacro *pOwnMac = rOwn.GetObject(nNo);
305 		const SvxMacro *pOtherMac = rOther.GetObject(nNo);
306 		if ( 	rOwn.GetKey(pOwnMac) != rOther.GetKey(pOtherMac)  ||
307 				pOwnMac->GetLibName() != pOtherMac->GetLibName() ||
308 				pOwnMac->GetMacName() != pOtherMac->GetMacName() )
309 			return sal_False;
310 	}
311 
312 	return sal_True;
313 }
314 
315 
316 /*--------------------------------------------------------------------
317 	Beschreibung:
318  --------------------------------------------------------------------*/
319 
320 void SvxHyperlinkItem::SetMacro( sal_uInt16 nEvent, const SvxMacro& rMacro )
321 {
322 	if( nEvent < EVENT_SFX_START )
323 	{
324 		switch( nEvent )
325 		{
326 			case HYPERDLG_EVENT_MOUSEOVER_OBJECT:
327 				nEvent = SFX_EVENT_MOUSEOVER_OBJECT;
328 				break;
329 			case HYPERDLG_EVENT_MOUSECLICK_OBJECT:
330 				nEvent = SFX_EVENT_MOUSECLICK_OBJECT;
331 				break;
332 			case HYPERDLG_EVENT_MOUSEOUT_OBJECT:
333 				nEvent = SFX_EVENT_MOUSEOUT_OBJECT;
334 				break;
335 		}
336 	}
337 
338 	if( !pMacroTable )
339 		pMacroTable = new SvxMacroTableDtor;
340 
341 	SvxMacro *pOldMacro;
342 	if( 0 != ( pOldMacro = pMacroTable->Get( nEvent )) )
343 	{
344 		delete pOldMacro;
345 		pMacroTable->Replace( nEvent, new SvxMacro( rMacro ) );
346 	}
347 	else
348 		pMacroTable->Insert( nEvent, new SvxMacro( rMacro ) );
349 }
350 
351 /*--------------------------------------------------------------------
352 	Beschreibung:
353  --------------------------------------------------------------------*/
354 
355 void SvxHyperlinkItem::SetMacroTable( const SvxMacroTableDtor& rTbl )
356 {
357 	if ( pMacroTable )
358 		delete pMacroTable;
359 
360 	pMacroTable = new SvxMacroTableDtor ( rTbl );
361 }
362 
363 sal_Bool SvxHyperlinkItem::QueryValue( com::sun::star::uno::Any& rVal, sal_uInt8 nMemberId ) const
364 {
365 //    sal_Bool bConvert = 0!=(nMemberId&CONVERT_TWIPS);
366     nMemberId &= ~CONVERT_TWIPS;
367 	switch(nMemberId)
368 	{
369         case MID_HLINK_NAME   :
370             rVal <<= ::rtl::OUString(sIntName.GetBuffer());
371 		break;
372         case MID_HLINK_TEXT   :
373             rVal <<= ::rtl::OUString(sName.GetBuffer());
374 		break;
375         case MID_HLINK_URL:
376             rVal <<= ::rtl::OUString(sURL.GetBuffer());
377 		break;
378         case MID_HLINK_TARGET:
379             rVal <<= ::rtl::OUString(sTarget.GetBuffer());
380 		break;
381         case MID_HLINK_TYPE:
382             rVal <<= (sal_Int32) eType;
383 		break;
384         default:
385             return sal_False;
386 	}
387 
388     return sal_True;
389 }
390 
391 sal_Bool SvxHyperlinkItem::PutValue( const com::sun::star::uno::Any& rVal, sal_uInt8 nMemberId )
392 {
393 //    sal_Bool bConvert = 0!=(nMemberId&CONVERT_TWIPS);
394     nMemberId &= ~CONVERT_TWIPS;
395     ::rtl::OUString aStr;
396     sal_Int32 nVal = 0;
397 	switch(nMemberId)
398 	{
399         case MID_HLINK_NAME   :
400 			if(!(rVal >>= aStr))
401 				return sal_False;
402             sIntName = aStr.getStr();
403 		break;
404         case MID_HLINK_TEXT   :
405 			if(!(rVal >>= aStr))
406 				return sal_False;
407             sName = aStr.getStr();
408 		break;
409         case MID_HLINK_URL:
410 			if(!(rVal >>= aStr))
411 				return sal_False;
412             sURL = aStr.getStr();
413 		break;
414         case MID_HLINK_TARGET:
415 			if(!(rVal >>= aStr))
416 				return sal_False;
417             sTarget = aStr.getStr();
418 		break;
419         case MID_HLINK_TYPE:
420             if(!(rVal >>= nVal))
421 				return sal_False;
422             eType = (SvxLinkInsertMode) (sal_uInt16) nVal;
423 		break;
424         default:
425             return sal_False;
426 	}
427 
428     return sal_True;
429 }
430 
431