Update to m13
[ooovba.git] / sc / source / filter / xlsx / xlsx-xlescher.cxx
blob841eeb7859fc086bea90a211933ac8db3b84ac58
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>
49 using ::rtl::OUString;
50 using ::com::sun::star::uno::Reference;
51 using ::com::sun::star::uno::UNO_QUERY;
52 using ::com::sun::star::drawing::XShape;
53 using ::com::sun::star::drawing::XControlShape;
54 using ::com::sun::star::awt::XControlModel;
55 using ::com::sun::star::script::ScriptEventDescriptor;
57 // Structs and classes ========================================================
59 XclObjId::XclObjId() :
60 mnScTab( SCTAB_INVALID ),
61 mnObjId( EXC_OBJ_INVALID_ID )
65 XclObjId::XclObjId( SCTAB nScTab, sal_uInt16 nObjId ) :
66 mnScTab( nScTab ),
67 mnObjId( nObjId )
71 bool operator==( const XclObjId& rL, const XclObjId& rR )
73 return (rL.mnScTab == rR.mnScTab) && (rL.mnObjId == rR.mnObjId);
76 bool operator<( const XclObjId& rL, const XclObjId& rR )
78 return (rL.mnScTab < rR.mnScTab) || ((rL.mnScTab == rR.mnScTab) && (rL.mnObjId < rR.mnObjId));
81 // ----------------------------------------------------------------------------
83 namespace {
85 /** Returns the scaling factor to calculate coordinates from twips. */
86 double lclGetTwipsScale( MapUnit eMapUnit )
88 /* #111027# We cannot use OutputDevice::LogicToLogic() or the XclTools
89 conversion functions to calculate drawing layer coordinates due to
90 Calc's strange definition of a point (1 inch == 72.27 points, instead
91 of 72 points). */
92 double fScale = 1.0;
93 switch( eMapUnit )
95 case MAP_TWIP: fScale = 72 / POINTS_PER_INCH; break; // Calc twips <-> real twips
96 case MAP_100TH_MM: fScale = HMM_PER_TWIPS; break; // Calc twips <-> 1/100mm
97 default: DBG_ERRORFILE( "lclGetTwipsScale - map unit not implemented" );
99 return fScale;
102 /** Calculates a drawing layer X position (in twips) from an object column position. */
103 long lclGetXFromCol( ScDocument& rDoc, SCTAB nScTab, sal_uInt16 nXclCol, sal_uInt16 nOffset, double fScale )
105 SCCOL nScCol = static_cast< SCCOL >( nXclCol );
106 return static_cast< long >( fScale * (rDoc.GetColOffset( nScCol, nScTab ) +
107 ::std::min( nOffset / 1024.0, 1.0 ) * rDoc.GetColWidth( nScCol, nScTab )) + 0.5 );
110 /** Calculates a drawing layer Y position (in twips) from an object row position. */
111 long lclGetYFromRow( ScDocument& rDoc, SCTAB nScTab, sal_uInt16 nXclRow, sal_uInt16 nOffset, double fScale )
113 SCROW nScRow = static_cast< SCROW >( nXclRow );
114 return static_cast< long >( fScale * (rDoc.GetRowOffset( nScRow, nScTab ) +
115 ::std::min( nOffset / 256.0, 1.0 ) * rDoc.GetRowHeight( nScRow, nScTab )) + 0.5 );
118 /** Calculates an object column position from a drawing layer X position (in twips). */
119 void lclGetColFromX(
120 ScDocument& rDoc, SCTAB nScTab, sal_uInt16& rnXclCol,
121 sal_uInt16& rnOffset, sal_uInt16 nXclStartCol,
122 long& rnStartW, long nX, double fScale )
124 // rnStartW in conjunction with nXclStartCol is used as buffer for previously calculated width
125 long nTwipsX = static_cast< long >( nX / fScale + 0.5 );
126 long nColW = 0;
127 for( rnXclCol = nXclStartCol; rnXclCol <= MAXCOL; ++rnXclCol )
129 nColW = rDoc.GetColWidth( static_cast<SCCOL>(rnXclCol), nScTab );
130 if( rnStartW + nColW > nTwipsX )
131 break;
132 rnStartW += nColW;
134 rnOffset = nColW ? static_cast< sal_uInt16 >( (nTwipsX - rnStartW) * 1024.0 / nColW + 0.5 ) : 0;
137 /** Calculates an object row position from a drawing layer Y position (in twips). */
138 void lclGetRowFromY(
139 ScDocument& rDoc, SCTAB nScTab,
140 sal_uInt32& rnXclRow, sal_uInt16& rnOffset, sal_uInt32 nXclStartRow,
141 long& rnStartH, long nY, double fScale )
143 // rnStartH in conjunction with nXclStartRow is used as buffer for previously calculated height
144 long nTwipsY = static_cast< long >( nY / fScale + 0.5 );
145 long nRowH = 0;
146 bool bFound = false;
147 for (SCROW nRow = static_cast<SCROW>(nXclStartRow); nRow <= MAXROW; ++nRow)
149 nRowH = rDoc.GetRowHeight(nRow, nScTab);
150 if( rnStartH + nRowH > nTwipsY )
152 rnXclRow = static_cast< sal_uInt16 >(nRow);
153 bFound = true;
154 break;
156 rnStartH += nRowH;
158 if (!bFound)
159 rnXclRow = static_cast<sal_uInt16>(MAXROW);
160 rnOffset = static_cast< sal_uInt16 >( nRowH ? ((nTwipsY - rnStartH) * 256.0 / nRowH + 0.5) : 0 );
163 /** Mirrors a rectangle (from LTR to RTL layout or vice versa). */
164 void lclMirrorRectangle( Rectangle& rRect )
166 long nLeft = rRect.Left();
167 rRect.Left() = -rRect.Right();
168 rRect.Right() = -nLeft;
171 } // namespace
173 // ----------------------------------------------------------------------------
175 XclObjAnchor::XclObjAnchor( SCTAB nScTab ) :
176 mnScTab( nScTab ),
177 mnLX( 0 ),
178 mnTY( 0 ),
179 mnRX( 0 ),
180 mnBY( 0 )
184 Rectangle XclObjAnchor::GetRect( ScDocument& rDoc, MapUnit eMapUnit ) const
186 double fScale = lclGetTwipsScale( eMapUnit );
187 Rectangle aRect(
188 lclGetXFromCol( rDoc, mnScTab, maFirst.mnCol, mnLX, fScale ),
189 lclGetYFromRow( rDoc, mnScTab, maFirst.mnRow, mnTY, fScale ),
190 lclGetXFromCol( rDoc, mnScTab, maLast.mnCol, mnRX + 1, fScale ),
191 lclGetYFromRow( rDoc, mnScTab, maLast.mnRow, mnBY, fScale ) );
193 // #106948# adjust coordinates in mirrored sheets
194 if( rDoc.IsLayoutRTL( mnScTab ) )
195 lclMirrorRectangle( aRect );
196 return aRect;
199 void XclObjAnchor::SetRect( ScDocument& rDoc, const Rectangle& rRect, MapUnit eMapUnit )
201 Rectangle aRect( rRect );
202 // #106948# adjust coordinates in mirrored sheets
203 if( rDoc.IsLayoutRTL( mnScTab ) )
204 lclMirrorRectangle( aRect );
206 double fScale = lclGetTwipsScale( eMapUnit );
207 long nDummy = 0;
208 lclGetColFromX( rDoc, mnScTab, maFirst.mnCol, mnLX, 0, nDummy, aRect.Left(), fScale );
209 lclGetColFromX( rDoc, mnScTab, maLast.mnCol, mnRX, maFirst.mnCol, nDummy, aRect.Right(), fScale );
210 nDummy = 0;
211 lclGetRowFromY( rDoc, mnScTab, maFirst.mnRow, mnTY, 0, nDummy, aRect.Top(), fScale );
212 lclGetRowFromY( rDoc, mnScTab, maLast.mnRow, mnBY, maFirst.mnRow, nDummy, aRect.Bottom(), fScale );
215 // ----------------------------------------------------------------------------
217 XclObjLineData::XclObjLineData() :
218 mnColorIdx( EXC_OBJ_LINE_AUTOCOLOR ),
219 mnStyle( EXC_OBJ_LINE_SOLID ),
220 mnWidth( EXC_OBJ_LINE_HAIR ),
221 mnAuto( EXC_OBJ_LINE_AUTO )
225 XclImpStream& operator>>( XclImpStream& rStrm, XclObjLineData& rLineData )
227 return rStrm
228 >> rLineData.mnColorIdx
229 >> rLineData.mnStyle
230 >> rLineData.mnWidth
231 >> rLineData.mnAuto;
234 // ----------------------------------------------------------------------------
236 XclObjFillData::XclObjFillData() :
237 mnBackColorIdx( EXC_OBJ_LINE_AUTOCOLOR ),
238 mnPattColorIdx( EXC_OBJ_FILL_AUTOCOLOR ),
239 mnPattern( EXC_PATT_SOLID ),
240 mnAuto( EXC_OBJ_FILL_AUTO )
244 XclImpStream& operator>>( XclImpStream& rStrm, XclObjFillData& rFillData )
246 return rStrm
247 >> rFillData.mnBackColorIdx
248 >> rFillData.mnPattColorIdx
249 >> rFillData.mnPattern
250 >> rFillData.mnAuto;
253 // ----------------------------------------------------------------------------
255 XclObjTextData::XclObjTextData() :
256 mnTextLen( 0 ),
257 mnFormatSize( 0 ),
258 mnLinkSize( 0 ),
259 mnDefFontIdx( EXC_FONT_APP ),
260 mnFlags( 0 ),
261 mnOrient( EXC_OBJ_ORIENT_NONE ),
262 mnButtonFlags( 0 ),
263 mnShortcut( 0 ),
264 mnShortcutEA( 0 )
268 void XclObjTextData::ReadObj3( XclImpStream& rStrm )
270 rStrm >> mnTextLen;
271 rStrm.Ignore( 2 );
272 rStrm >> mnFormatSize >> mnDefFontIdx;
273 rStrm.Ignore( 2 );
274 rStrm >> mnFlags >> mnOrient;
275 rStrm.Ignore( 8 );
278 void XclObjTextData::ReadObj5( XclImpStream& rStrm )
280 rStrm >> mnTextLen;
281 rStrm.Ignore( 2 );
282 rStrm >> mnFormatSize >> mnDefFontIdx;
283 rStrm.Ignore( 2 );
284 rStrm >> mnFlags >> mnOrient;
285 rStrm.Ignore( 2 );
286 rStrm >> mnLinkSize;
287 rStrm.Ignore( 2 );
288 rStrm >> mnButtonFlags >> mnShortcut >> mnShortcutEA;
291 void XclObjTextData::ReadTxo8( XclImpStream& rStrm )
293 rStrm >> mnFlags >> mnOrient >> mnButtonFlags >> mnShortcut >> mnShortcutEA >> mnTextLen >> mnFormatSize;
296 // ============================================================================
298 Reference< XControlModel > XclControlHelper::GetControlModel( Reference< XShape > xShape )
300 Reference< XControlModel > xCtrlModel;
301 Reference< XControlShape > xCtrlShape( xShape, UNO_QUERY );
302 if( xCtrlShape.is() )
303 xCtrlModel = xCtrlShape->getControl();
304 return xCtrlModel;
307 #define EXC_MACRONAME_PRE "vnd.sun.star.script:Standard."
308 #define EXC_MACRO_SCHEME "vnd.sun.star.script:"
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 String sProjectName( RTL_CONSTASCII_USTRINGPARAM("Standard") );
318 if ( pDocShell && pDocShell->GetBasicManager()->GetName().Len() > 0 )
319 sProjectName = pDocShell->GetBasicManager()->GetName();
321 if ( ( sTmp.Search( '.' ) == STRING_NOTFOUND) && pDocShell )
323 if( StarBASIC* pBasic = pDocShell->GetBasicManager()->GetLib( sProjectName ) )
325 if( SbMethod* pMethod = dynamic_cast< SbMethod* >( pBasic->Find( sTmp, SbxCLASS_METHOD ) ) )
327 if( SbModule* pModule = pMethod->GetModule() )
329 sTmp.Insert( '.', 0 ).Insert( pModule->GetName(), 0 );
334 sProjectName.Append( '.' );
335 sTmp.Insert( sProjectName, 0 );
336 return CREATE_OUSTRING( EXC_MACRO_SCHEME ) + sTmp + CREATE_OUSTRING( EXC_MACRONAME_SUF );
338 return OUString();
341 String XclControlHelper::GetXclMacroName( const OUString& rScMacroName )
343 const OUString saMacroNamePre = CREATE_OUSTRING( EXC_MACRONAME_PRE );
344 const OUString saMacroNameSuf = CREATE_OUSTRING( EXC_MACRONAME_SUF );
345 sal_Int32 snScMacroNameLen = rScMacroName.getLength();
346 sal_Int32 snXclMacroNameLen = snScMacroNameLen - saMacroNamePre.getLength() - saMacroNameSuf.getLength();
347 if( (snXclMacroNameLen > 0) && rScMacroName.matchIgnoreAsciiCase( saMacroNamePre, 0 ) &&
348 rScMacroName.matchIgnoreAsciiCase( saMacroNameSuf, snScMacroNameLen - saMacroNameSuf.getLength() ) )
349 return rScMacroName.copy( saMacroNamePre.getLength(), snXclMacroNameLen );
350 return String::EmptyString();
353 static const struct
355 const sal_Char* mpcListenerType;
356 const sal_Char* mpcEventMethod;
358 spTbxListenerData[] =
360 // Attention: MUST be in order of the XclTbxEventType enum!
361 /*EXC_TBX_EVENT_ACTION*/ { "XActionListener", "actionPerformed" },
362 /*EXC_TBX_EVENT_MOUSE*/ { "XMouseListener", "mouseReleased" },
363 /*EXC_TBX_EVENT_TEXT*/ { "XTextListener", "textChanged" },
364 /*EXC_TBX_EVENT_VALUE*/ { "XAdjustmentListener", "adjustmentValueChanged" },
365 /*EXC_TBX_EVENT_CHANGE*/ { "XChangeListener", "changed" }
368 #define EXC_MACROSCRIPT "Script"
370 bool XclControlHelper::FillMacroDescriptor( ScriptEventDescriptor& rDescriptor,
371 XclTbxEventType eEventType, const String& rXclMacroName, SfxObjectShell* pShell )
373 if( rXclMacroName.Len() > 0 )
375 rDescriptor.ListenerType = OUString::createFromAscii( spTbxListenerData[ eEventType ].mpcListenerType );
376 rDescriptor.EventMethod = OUString::createFromAscii( spTbxListenerData[ eEventType ].mpcEventMethod );
377 rDescriptor.ScriptType = CREATE_OUSTRING( EXC_MACROSCRIPT );
378 rDescriptor.ScriptCode = GetScMacroName( rXclMacroName, pShell );
379 return true;
381 return false;
384 String XclControlHelper::ExtractFromMacroDescriptor(
385 const ScriptEventDescriptor& rDescriptor, XclTbxEventType eEventType )
387 if( (rDescriptor.ScriptCode.getLength() > 0) &&
388 rDescriptor.ScriptType.equalsIgnoreAsciiCaseAscii( EXC_MACROSCRIPT ) &&
389 rDescriptor.ListenerType.equalsAscii( spTbxListenerData[ eEventType ].mpcListenerType ) &&
390 rDescriptor.EventMethod.equalsAscii( spTbxListenerData[ eEventType ].mpcEventMethod ) )
391 return GetXclMacroName( rDescriptor.ScriptCode );
392 return String::EmptyString();
395 // ============================================================================