merge the formfield patch from ooo-build
[ooovba.git] / sc / source / filter / excel / xlescher.cxx
blob066eb57a6a9a5a619a99ef043c86c05bc4811f52
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: xlescher.cxx,v $
10 * $Revision: 1.14.90.8 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_sc.hxx"
34 #include <com/sun/star/drawing/XControlShape.hpp>
35 #include <com/sun/star/script/ScriptEventDescriptor.hpp>
36 #include <svx/unoapi.hxx>
37 #include "xestream.hxx"
38 #include "document.hxx"
39 #include "xistream.hxx"
40 #include "xlescher.hxx"
41 #include "globstr.hrc"
43 #include <sfx2/objsh.hxx>
44 #include <basic/sbstar.hxx>
45 #include <basic/sbmod.hxx>
46 #include <basic/sbmeth.hxx>
47 #include <basic/basmgr.hxx>
48 #include <svx/msvbahelper.hxx>
50 using ::rtl::OUString;
51 using ::com::sun::star::uno::Reference;
52 using ::com::sun::star::uno::UNO_QUERY;
53 using ::com::sun::star::drawing::XShape;
54 using ::com::sun::star::drawing::XControlShape;
55 using ::com::sun::star::awt::XControlModel;
56 using ::com::sun::star::script::ScriptEventDescriptor;
58 // Structs and classes ========================================================
60 XclObjId::XclObjId() :
61 mnScTab( SCTAB_INVALID ),
62 mnObjId( EXC_OBJ_INVALID_ID )
66 XclObjId::XclObjId( SCTAB nScTab, sal_uInt16 nObjId ) :
67 mnScTab( nScTab ),
68 mnObjId( nObjId )
72 bool operator==( const XclObjId& rL, const XclObjId& rR )
74 return (rL.mnScTab == rR.mnScTab) && (rL.mnObjId == rR.mnObjId);
77 bool operator<( const XclObjId& rL, const XclObjId& rR )
79 return (rL.mnScTab < rR.mnScTab) || ((rL.mnScTab == rR.mnScTab) && (rL.mnObjId < rR.mnObjId));
82 // ----------------------------------------------------------------------------
84 namespace {
86 /** Returns the scaling factor to calculate coordinates from twips. */
87 double lclGetTwipsScale( MapUnit eMapUnit )
89 /* #111027# We cannot use OutputDevice::LogicToLogic() or the XclTools
90 conversion functions to calculate drawing layer coordinates due to
91 Calc's strange definition of a point (1 inch == 72.27 points, instead
92 of 72 points). */
93 double fScale = 1.0;
94 switch( eMapUnit )
96 case MAP_TWIP: fScale = 72 / POINTS_PER_INCH; break; // Calc twips <-> real twips
97 case MAP_100TH_MM: fScale = HMM_PER_TWIPS; break; // Calc twips <-> 1/100mm
98 default: DBG_ERRORFILE( "lclGetTwipsScale - map unit not implemented" );
100 return fScale;
103 /** Calculates a drawing layer X position (in twips) from an object column position. */
104 long lclGetXFromCol( ScDocument& rDoc, SCTAB nScTab, sal_uInt16 nXclCol, sal_uInt16 nOffset, double fScale )
106 SCCOL nScCol = static_cast< SCCOL >( nXclCol );
107 return static_cast< long >( fScale * (rDoc.GetColOffset( nScCol, nScTab ) +
108 ::std::min( nOffset / 1024.0, 1.0 ) * rDoc.GetColWidth( nScCol, nScTab )) + 0.5 );
111 /** Calculates a drawing layer Y position (in twips) from an object row position. */
112 long lclGetYFromRow( ScDocument& rDoc, SCTAB nScTab, sal_uInt16 nXclRow, sal_uInt16 nOffset, double fScale )
114 SCROW nScRow = static_cast< SCROW >( nXclRow );
115 return static_cast< long >( fScale * (rDoc.GetRowOffset( nScRow, nScTab ) +
116 ::std::min( nOffset / 256.0, 1.0 ) * rDoc.GetRowHeight( nScRow, nScTab )) + 0.5 );
119 /** Calculates an object column position from a drawing layer X position (in twips). */
120 void lclGetColFromX(
121 ScDocument& rDoc, SCTAB nScTab, sal_uInt16& rnXclCol,
122 sal_uInt16& rnOffset, sal_uInt16 nXclStartCol,
123 long& rnStartW, long nX, double fScale )
125 // rnStartW in conjunction with nXclStartCol is used as buffer for previously calculated width
126 long nTwipsX = static_cast< long >( nX / fScale + 0.5 );
127 long nColW = 0;
128 for( rnXclCol = nXclStartCol; rnXclCol <= MAXCOL; ++rnXclCol )
130 nColW = rDoc.GetColWidth( static_cast<SCCOL>(rnXclCol), nScTab );
131 if( rnStartW + nColW > nTwipsX )
132 break;
133 rnStartW += nColW;
135 rnOffset = nColW ? static_cast< sal_uInt16 >( (nTwipsX - rnStartW) * 1024.0 / nColW + 0.5 ) : 0;
138 /** Calculates an object row position from a drawing layer Y position (in twips). */
139 void lclGetRowFromY(
140 ScDocument& rDoc, SCTAB nScTab,
141 sal_uInt16& rnXclRow, sal_uInt16& rnOffset, sal_uInt16 nXclStartRow,
142 long& rnStartH, long nY, double fScale )
144 // rnStartH in conjunction with nXclStartRow is used as buffer for previously calculated height
145 long nTwipsY = static_cast< long >( nY / fScale + 0.5 );
146 long nRowH = 0;
147 bool bFound = false;
148 for (SCROW nRow = static_cast<SCROW>(nXclStartRow); nRow <= MAXROW; ++nRow)
150 nRowH = rDoc.GetRowHeight(nRow, nScTab);
151 if( rnStartH + nRowH > nTwipsY )
153 rnXclRow = static_cast< sal_uInt16 >(nRow);
154 bFound = true;
155 break;
157 rnStartH += nRowH;
159 if (!bFound)
160 rnXclRow = static_cast<sal_uInt16>(MAXROW);
161 rnOffset = static_cast< sal_uInt16 >( nRowH ? ((nTwipsY - rnStartH) * 256.0 / nRowH + 0.5) : 0 );
164 /** Mirrors a rectangle (from LTR to RTL layout or vice versa). */
165 void lclMirrorRectangle( Rectangle& rRect )
167 long nLeft = rRect.Left();
168 rRect.Left() = -rRect.Right();
169 rRect.Right() = -nLeft;
172 } // namespace
174 // ----------------------------------------------------------------------------
176 XclObjAnchor::XclObjAnchor( SCTAB nScTab ) :
177 mnScTab( nScTab ),
178 mnLX( 0 ),
179 mnTY( 0 ),
180 mnRX( 0 ),
181 mnBY( 0 )
185 Rectangle XclObjAnchor::GetRect( ScDocument& rDoc, MapUnit eMapUnit ) const
187 double fScale = lclGetTwipsScale( eMapUnit );
188 Rectangle aRect(
189 lclGetXFromCol( rDoc, mnScTab, maFirst.mnCol, mnLX, fScale ),
190 lclGetYFromRow( rDoc, mnScTab, maFirst.mnRow, mnTY, fScale ),
191 lclGetXFromCol( rDoc, mnScTab, maLast.mnCol, mnRX + 1, fScale ),
192 lclGetYFromRow( rDoc, mnScTab, maLast.mnRow, mnBY, fScale ) );
194 // #106948# adjust coordinates in mirrored sheets
195 if( rDoc.IsLayoutRTL( mnScTab ) )
196 lclMirrorRectangle( aRect );
197 return aRect;
200 void XclObjAnchor::SetRect( ScDocument& rDoc, const Rectangle& rRect, MapUnit eMapUnit )
202 Rectangle aRect( rRect );
203 // #106948# adjust coordinates in mirrored sheets
204 if( rDoc.IsLayoutRTL( mnScTab ) )
205 lclMirrorRectangle( aRect );
207 double fScale = lclGetTwipsScale( eMapUnit );
208 long nDummy = 0;
209 lclGetColFromX( rDoc, mnScTab, maFirst.mnCol, mnLX, 0, nDummy, aRect.Left(), fScale );
210 lclGetColFromX( rDoc, mnScTab, maLast.mnCol, mnRX, maFirst.mnCol, nDummy, aRect.Right(), fScale );
211 nDummy = 0;
212 lclGetRowFromY( rDoc, mnScTab, maFirst.mnRow, mnTY, 0, nDummy, aRect.Top(), fScale );
213 lclGetRowFromY( rDoc, mnScTab, maLast.mnRow, mnBY, maFirst.mnRow, nDummy, aRect.Bottom(), fScale );
216 // ----------------------------------------------------------------------------
218 XclObjLineData::XclObjLineData() :
219 mnColorIdx( EXC_OBJ_LINE_AUTOCOLOR ),
220 mnStyle( EXC_OBJ_LINE_SOLID ),
221 mnWidth( EXC_OBJ_LINE_HAIR ),
222 mnAuto( EXC_OBJ_LINE_AUTO )
226 XclImpStream& operator>>( XclImpStream& rStrm, XclObjLineData& rLineData )
228 return rStrm
229 >> rLineData.mnColorIdx
230 >> rLineData.mnStyle
231 >> rLineData.mnWidth
232 >> rLineData.mnAuto;
235 // ----------------------------------------------------------------------------
237 XclObjFillData::XclObjFillData() :
238 mnBackColorIdx( EXC_OBJ_LINE_AUTOCOLOR ),
239 mnPattColorIdx( EXC_OBJ_FILL_AUTOCOLOR ),
240 mnPattern( EXC_PATT_SOLID ),
241 mnAuto( EXC_OBJ_FILL_AUTO )
245 XclImpStream& operator>>( XclImpStream& rStrm, XclObjFillData& rFillData )
247 return rStrm
248 >> rFillData.mnBackColorIdx
249 >> rFillData.mnPattColorIdx
250 >> rFillData.mnPattern
251 >> rFillData.mnAuto;
254 // ----------------------------------------------------------------------------
256 XclObjTextData::XclObjTextData() :
257 mnTextLen( 0 ),
258 mnFormatSize( 0 ),
259 mnLinkSize( 0 ),
260 mnDefFontIdx( EXC_FONT_APP ),
261 mnFlags( 0 ),
262 mnOrient( EXC_OBJ_ORIENT_NONE ),
263 mnButtonFlags( 0 ),
264 mnShortcut( 0 ),
265 mnShortcutEA( 0 )
269 void XclObjTextData::ReadObj3( XclImpStream& rStrm )
271 rStrm >> mnTextLen;
272 rStrm.Ignore( 2 );
273 rStrm >> mnFormatSize >> mnDefFontIdx;
274 rStrm.Ignore( 2 );
275 rStrm >> mnFlags >> mnOrient;
276 rStrm.Ignore( 8 );
279 void XclObjTextData::ReadObj5( XclImpStream& rStrm )
281 rStrm >> mnTextLen;
282 rStrm.Ignore( 2 );
283 rStrm >> mnFormatSize >> mnDefFontIdx;
284 rStrm.Ignore( 2 );
285 rStrm >> mnFlags >> mnOrient;
286 rStrm.Ignore( 2 );
287 rStrm >> mnLinkSize;
288 rStrm.Ignore( 2 );
289 rStrm >> mnButtonFlags >> mnShortcut >> mnShortcutEA;
292 void XclObjTextData::ReadTxo8( XclImpStream& rStrm )
294 rStrm >> mnFlags >> mnOrient >> mnButtonFlags >> mnShortcut >> mnShortcutEA >> mnTextLen >> mnFormatSize;
297 // ============================================================================
299 Reference< XControlModel > XclControlHelper::GetControlModel( Reference< XShape > xShape )
301 Reference< XControlModel > xCtrlModel;
302 Reference< XControlShape > xCtrlShape( xShape, UNO_QUERY );
303 if( xCtrlShape.is() )
304 xCtrlModel = xCtrlShape->getControl();
305 return xCtrlModel;
308 #define EXC_MACRONAME_PRE "vnd.sun.star.script:Standard."
309 #define EXC_MACRONAME_SUF "?language=Basic&location=document"
311 OUString XclControlHelper::GetScMacroName( const String& rXclMacroName, SfxObjectShell* pDocShell )
313 String sTmp( rXclMacroName );
314 if( rXclMacroName.Len() > 0 )
316 ooo::vba::VBAMacroResolvedInfo aMacro = ooo::vba::resolveVBAMacro( pDocShell, rXclMacroName, false );
317 if ( aMacro.IsResolved() )
318 return ooo::vba::makeMacroURL( aMacro.ResolvedMacro() );
321 return OUString();
324 String XclControlHelper::GetXclMacroName( const OUString& rScMacroName )
326 const OUString saMacroNamePre = CREATE_OUSTRING( EXC_MACRONAME_PRE );
327 const OUString saMacroNameSuf = CREATE_OUSTRING( EXC_MACRONAME_SUF );
328 sal_Int32 snScMacroNameLen = rScMacroName.getLength();
329 sal_Int32 snXclMacroNameLen = snScMacroNameLen - saMacroNamePre.getLength() - saMacroNameSuf.getLength();
330 if( (snXclMacroNameLen > 0) && rScMacroName.matchIgnoreAsciiCase( saMacroNamePre, 0 ) &&
331 rScMacroName.matchIgnoreAsciiCase( saMacroNameSuf, snScMacroNameLen - saMacroNameSuf.getLength() ) )
332 return rScMacroName.copy( saMacroNamePre.getLength(), snXclMacroNameLen );
333 return String::EmptyString();
336 static const struct
338 const sal_Char* mpcListenerType;
339 const sal_Char* mpcEventMethod;
341 spTbxListenerData[] =
343 // Attention: MUST be in order of the XclTbxEventType enum!
344 /*EXC_TBX_EVENT_ACTION*/ { "XActionListener", "actionPerformed" },
345 /*EXC_TBX_EVENT_MOUSE*/ { "XMouseListener", "mouseReleased" },
346 /*EXC_TBX_EVENT_TEXT*/ { "XTextListener", "textChanged" },
347 /*EXC_TBX_EVENT_VALUE*/ { "XAdjustmentListener", "adjustmentValueChanged" },
348 /*EXC_TBX_EVENT_CHANGE*/ { "XChangeListener", "changed" }
351 #define EXC_MACROSCRIPT "Script"
353 bool XclControlHelper::FillMacroDescriptor( ScriptEventDescriptor& rDescriptor,
354 XclTbxEventType eEventType, const String& rXclMacroName, SfxObjectShell* pShell )
356 if( rXclMacroName.Len() > 0 )
358 rDescriptor.ListenerType = OUString::createFromAscii( spTbxListenerData[ eEventType ].mpcListenerType );
359 rDescriptor.EventMethod = OUString::createFromAscii( spTbxListenerData[ eEventType ].mpcEventMethod );
360 rDescriptor.ScriptType = CREATE_OUSTRING( EXC_MACROSCRIPT );
361 rDescriptor.ScriptCode = GetScMacroName( rXclMacroName, pShell );
362 return true;
364 return false;
367 String XclControlHelper::ExtractFromMacroDescriptor(
368 const ScriptEventDescriptor& rDescriptor, XclTbxEventType eEventType )
370 if( (rDescriptor.ScriptCode.getLength() > 0) &&
371 rDescriptor.ScriptType.equalsIgnoreAsciiCaseAscii( EXC_MACROSCRIPT ) &&
372 rDescriptor.ListenerType.equalsAscii( spTbxListenerData[ eEventType ].mpcListenerType ) &&
373 rDescriptor.EventMethod.equalsAscii( spTbxListenerData[ eEventType ].mpcEventMethod ) )
374 return GetXclMacroName( rDescriptor.ScriptCode );
375 return String::EmptyString();
378 // ============================================================================