xref: /aoo41x/main/sw/source/core/unocore/unoevent.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_sw.hxx"
30 
31 
32 // HINTIDs must be on top; it is required for the macitem.hxx header
33 #include "hintids.hxx"
34 #include "unoevent.hxx"
35 #include "unoframe.hxx"
36 #include "unostyle.hxx"
37 #include "swevent.hxx"
38 #include "docstyle.hxx"
39 #ifndef _SVX_SVXIDS_HRC
40 #include <svx/svxids.hrc>
41 #endif
42 #include "fmtinfmt.hxx"
43 #include <svl/macitem.hxx>
44 #include <rtl/ustrbuf.hxx>
45 #ifndef _COM_SUN_STAR_BEANS_PROPERTYVALUE_HPP
46 #include <com/sun/star/beans/PropertyValue.hpp>
47 #endif
48 
49 using namespace ::com::sun::star;
50 using namespace ::com::sun::star::uno;
51 
52 using ::com::sun::star::container::NoSuchElementException;
53 using ::com::sun::star::container::XNameReplace;
54 using ::com::sun::star::lang::IllegalArgumentException;
55 using ::com::sun::star::lang::WrappedTargetException;
56 using ::com::sun::star::lang::XServiceInfo;
57 using ::com::sun::star::beans::PropertyValue;
58 using ::cppu::WeakImplHelper2;
59 using ::rtl::OUString;
60 using ::rtl::OUStringBuffer;
61 
62 
63 const sal_Char sAPI_ServiceName[] = "com.sun.star.container.XNameReplace";
64 const sal_Char sAPI_SwFrameEventDescriptor[] = "SwFrameEventDescriptor";
65 const sal_Char sAPI_SwFrameStyleEventDescriptor[] =
66 									"SwFrameStyleEventDescriptor";
67 const sal_Char sAPI_SwDetachedEventDescriptor[] = "SwDetachedEventDescriptor";
68 const sal_Char sAPI_SwHyperlinkEventDescriptor[] =
69 									"SwHyperlinkEventDescriptor";
70 const sal_Char sAPI_SwAutoTextEventDescriptor[] =
71 									"SwAutoTextEventDescriptor";
72 
73 //
74 // tables of allowed events for specific objects
75 //
76 
77 const struct SvEventDescription aGraphicEvents[] =
78 {
79 	{ SW_EVENT_OBJECT_SELECT,		"OnSelect" },
80 	{ SFX_EVENT_MOUSEOVER_OBJECT,	"OnMouseOver" },
81 	{ SFX_EVENT_MOUSECLICK_OBJECT,	"OnClick" },
82 	{ SFX_EVENT_MOUSEOUT_OBJECT,	"OnMouseOut" },
83 	{ SVX_EVENT_IMAGE_LOAD,			"OnLoadDone" },
84 	{ SVX_EVENT_IMAGE_ABORT,		"OnLoadCancel" },
85 	{ SVX_EVENT_IMAGE_ERROR,		"OnLoadError" },
86 	{ 0, NULL }
87 };
88 
89 const struct SvEventDescription aFrameEvents[] =
90 {
91 	{ SW_EVENT_OBJECT_SELECT,		"OnSelect" },
92 	{ SW_EVENT_FRM_KEYINPUT_ALPHA,	"OnAlphaCharInput" },
93 	{ SW_EVENT_FRM_KEYINPUT_NOALPHA,	"OnNonAlphaCharInput" },
94 	{ SW_EVENT_FRM_RESIZE,			"OnResize" },
95 	{ SW_EVENT_FRM_MOVE,			"OnMove" },
96 	{ SFX_EVENT_MOUSEOVER_OBJECT,	"OnMouseOver" },
97 	{ SFX_EVENT_MOUSECLICK_OBJECT,	"OnClick" },
98 	{ SFX_EVENT_MOUSEOUT_OBJECT,	"OnMouseOut" },
99 	{ 0, NULL }
100 };
101 
102 const struct SvEventDescription aOLEEvents[] =
103 {
104 	{ SW_EVENT_OBJECT_SELECT,		"OnSelect" },
105 	{ SFX_EVENT_MOUSEOVER_OBJECT,	"OnMouseOver" },
106 	{ SFX_EVENT_MOUSECLICK_OBJECT,	"OnClick" },
107 	{ SFX_EVENT_MOUSEOUT_OBJECT,	"OnMouseOut" },
108 	{ 0, NULL }
109 };
110 
111 const struct SvEventDescription aHyperlinkEvents[] =
112 {
113 	{ SFX_EVENT_MOUSEOVER_OBJECT,	"OnMouseOver" },
114 	{ SFX_EVENT_MOUSECLICK_OBJECT,	"OnClick" },
115 	{ SFX_EVENT_MOUSEOUT_OBJECT,	"OnMouseOut" },
116 	{ 0, NULL }
117 };
118 
119 const struct SvEventDescription aAutotextEvents[] =
120 {
121 	{ SW_EVENT_START_INS_GLOSSARY,	"OnInsertStart" },
122 	{ SW_EVENT_END_INS_GLOSSARY,	"OnInsertDone" },
123 	{ 0, NULL }
124 };
125 
126 const struct SvEventDescription aFrameStyleEvents[] =
127 {
128 	{ SW_EVENT_OBJECT_SELECT,		"OnSelect" },
129 	{ SW_EVENT_FRM_KEYINPUT_ALPHA,	"OnAlphaCharInput" },
130 	{ SW_EVENT_FRM_KEYINPUT_NOALPHA,	"OnNonAlphaCharInput" },
131 	{ SW_EVENT_FRM_RESIZE,			"OnResize" },
132 	{ SW_EVENT_FRM_MOVE,			"OnMove" },
133 	{ SFX_EVENT_MOUSEOVER_OBJECT,	"OnMouseOver" },
134 	{ SFX_EVENT_MOUSECLICK_OBJECT,	"OnClick" },
135 	{ SFX_EVENT_MOUSEOUT_OBJECT,	"OnMouseOut" },
136 	{ SVX_EVENT_IMAGE_LOAD,			"OnLoadDone" },
137 	{ SVX_EVENT_IMAGE_ABORT,		"OnLoadCancel" },
138 	{ SVX_EVENT_IMAGE_ERROR,		"OnLoadError" },
139 	{ 0, NULL }
140 };
141 
142 //
143 // SwHyperlinkEventDescriptor
144 //
145 
146 SwHyperlinkEventDescriptor::SwHyperlinkEventDescriptor() :
147 	SvDetachedEventDescriptor(aHyperlinkEvents),
148 	sImplName(RTL_CONSTASCII_USTRINGPARAM(sAPI_SwHyperlinkEventDescriptor))
149 {
150 }
151 
152 SwHyperlinkEventDescriptor::~SwHyperlinkEventDescriptor()
153 {
154 }
155 
156 OUString SwHyperlinkEventDescriptor::getImplementationName(void)
157 	throw( RuntimeException )
158 {
159 	return sImplName;
160 }
161 
162 void SwHyperlinkEventDescriptor::copyMacrosFromINetFmt(
163 	const SwFmtINetFmt& aFmt)
164 {
165 	for(sal_Int16 i = 0; mpSupportedMacroItems[i].mnEvent != 0; i++)
166 	{
167 		sal_uInt16 nEvent = mpSupportedMacroItems[i].mnEvent;
168 		const SvxMacro* aMacro = aFmt.GetMacro(nEvent);
169 		if (NULL != aMacro)
170 			replaceByName(nEvent, *aMacro);
171 	}
172 }
173 
174 void SwHyperlinkEventDescriptor::copyMacrosIntoINetFmt(
175 	SwFmtINetFmt& aFmt)
176 {
177 	for(sal_Int16 i = 0; mpSupportedMacroItems[i].mnEvent != 0; i++)
178 	{
179 		sal_uInt16 nEvent = mpSupportedMacroItems[i].mnEvent;
180 		if (hasByName(nEvent))
181 		{
182 			SvxMacro aMacro(sEmpty, sEmpty);
183 			getByName(aMacro, nEvent);
184 			aFmt.SetMacro(nEvent, aMacro);
185 		}
186 	}
187 }
188 
189 
190 void SwHyperlinkEventDescriptor::copyMacrosFromNameReplace(
191     uno::Reference<
192         container::XNameReplace> & xReplace)
193 {
194 	// iterate over all names (all names that *we* support)
195 	Sequence<OUString> aNames = getElementNames();
196 	sal_Int32 nCount = aNames.getLength();
197 	for(sal_Int32 i = 0; i < nCount; i++)
198 	{
199 		// copy element for that name
200 		const OUString& rName = aNames[i];
201 		if (xReplace->hasByName(rName))
202 		{
203 			SvBaseEventDescriptor::replaceByName(rName,
204 												 xReplace->getByName(rName));
205 		}
206 	}
207 }
208 
209 
210 //
211 // SwFrameEventDescriptor
212 //
213 
214 // use double cast in superclass constructor to avoid ambigous cast
215 SwFrameEventDescriptor::SwFrameEventDescriptor(
216 	SwXTextFrame& rFrameRef ) :
217 		SvEventDescriptor((text::XTextFrame&)rFrameRef, aFrameEvents),
218 		sSwFrameEventDescriptor(RTL_CONSTASCII_USTRINGPARAM(
219 			sAPI_SwFrameEventDescriptor)),
220 		rFrame(rFrameRef)
221 {
222 }
223 
224 SwFrameEventDescriptor::SwFrameEventDescriptor(
225 	SwXTextGraphicObject& rGraphicRef ) :
226 		SvEventDescriptor((text::XTextContent&)rGraphicRef, aGraphicEvents),
227 		rFrame((SwXFrame&)rGraphicRef)
228 {
229 }
230 
231 SwFrameEventDescriptor::SwFrameEventDescriptor(
232 	SwXTextEmbeddedObject& rObjectRef ) :
233 		SvEventDescriptor((text::XTextContent&)rObjectRef, aOLEEvents),
234 		rFrame((SwXFrame&)rObjectRef)
235 {
236 }
237 
238 SwFrameEventDescriptor::~SwFrameEventDescriptor()
239 {
240 }
241 
242 void SwFrameEventDescriptor::setMacroItem(const SvxMacroItem& rItem)
243 {
244     rFrame.GetFrmFmt()->SetFmtAttr(rItem);
245 }
246 
247 const SvxMacroItem& SwFrameEventDescriptor::getMacroItem()
248 {
249     return (const SvxMacroItem&)rFrame.GetFrmFmt()->GetFmtAttr(RES_FRMMACRO);
250 }
251 
252 sal_uInt16 SwFrameEventDescriptor::getMacroItemWhich() const
253 {
254 	return RES_FRMMACRO;
255 }
256 
257 OUString SwFrameEventDescriptor::getImplementationName()
258 	throw( RuntimeException )
259 {
260 	return sSwFrameEventDescriptor;
261 }
262 
263 
264 //
265 // SwFrameStyleEventDescriptor
266 //
267 
268 SwFrameStyleEventDescriptor::SwFrameStyleEventDescriptor(
269 	SwXFrameStyle& rStyleRef ) :
270 		SvEventDescriptor((document::XEventsSupplier&)rStyleRef,
271 						  aFrameStyleEvents),
272 		sSwFrameStyleEventDescriptor(RTL_CONSTASCII_USTRINGPARAM(
273 			sAPI_SwFrameStyleEventDescriptor)),
274 		rStyle(rStyleRef)
275 {
276 }
277 
278 SwFrameStyleEventDescriptor::~SwFrameStyleEventDescriptor()
279 {
280 }
281 
282 void SwFrameStyleEventDescriptor::setMacroItem(const SvxMacroItem& rItem)
283 {
284 	// As I was told, for some entirely unobvious reason getting an
285 	// item from a style has to look as follows:
286 	SfxStyleSheetBasePool* pBasePool = rStyle.GetBasePool();
287 	if (pBasePool)
288 	{
289 		SfxStyleSheetBase* pBase = pBasePool->Find(rStyle.GetStyleName());
290 		if (pBase)
291 		{
292 			rtl::Reference< SwDocStyleSheet > xStyle( new SwDocStyleSheet( *(SwDocStyleSheet*)pBase ) );
293 			SfxItemSet& rStyleSet = xStyle->GetItemSet();
294 			SfxItemSet aSet(*rStyleSet.GetPool(), RES_FRMMACRO, RES_FRMMACRO);
295 			aSet.Put(rItem);
296 			xStyle->SetItemSet(aSet);
297 		}
298 	}
299 }
300 
301 static const SvxMacroItem aEmptyMacroItem(RES_FRMMACRO);
302 
303 const SvxMacroItem& SwFrameStyleEventDescriptor::getMacroItem()
304 {
305 	// As I was told, for some entirely unobvious reason getting an
306 	// item from a style has to look as follows:
307 	SfxStyleSheetBasePool* pBasePool = rStyle.GetBasePool();
308 	if (pBasePool)
309 	{
310 		SfxStyleSheetBase* pBase = pBasePool->Find(rStyle.GetStyleName());
311 		if (pBase)
312 		{
313 			rtl::Reference< SwDocStyleSheet > xStyle( new SwDocStyleSheet( *(SwDocStyleSheet*)pBase) );
314 			return (const SvxMacroItem&)xStyle->GetItemSet().Get(RES_FRMMACRO);
315 		}
316 		else
317 			return aEmptyMacroItem;
318 	}
319 	else
320 		return aEmptyMacroItem;
321 }
322 
323 OUString SwFrameStyleEventDescriptor::getImplementationName()
324 	throw( RuntimeException )
325 {
326 	return sSwFrameStyleEventDescriptor;
327 }
328 
329 sal_uInt16 SwFrameStyleEventDescriptor::getMacroItemWhich() const
330 {
331 	return RES_FRMMACRO;
332 }
333 
334