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: chartins.cxx,v $
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_sw.hxx"
35 #include <sfx2/viewfrm.hxx>
36 #ifndef _MSGBOX_HXX //autogen
37 #include <vcl/msgbox.hxx>
39 #include <sfx2/dispatch.hxx>
40 #include <sfx2/basedlgs.hxx>
41 #include <IDocumentUndoRedo.hxx>
43 #include <sfx2/app.hxx>
44 #include <swtypes.hxx>
45 #include <swmodule.hxx>
51 #include <chartins.hxx>
52 #include <tablemgr.hxx>
54 #include <swtable.hxx>
57 #include <unochart.hxx>
58 #include <autoedit.hxx>
67 #include <chartins.hrc>
69 #include <anchoredobject.hxx>
71 #include <sot/clsids.hxx>
73 #include <cppuhelper/bootstrap.hxx>
74 #include <cppuhelper/component_context.hxx>
75 #include <comphelper/processfactory.hxx>
76 #include <com/sun/star/chart2/data/XDataProvider.hpp>
77 #include <com/sun/star/chart2/data/XDataReceiver.hpp>
78 #include <com/sun/star/chart/ChartDataRowSource.hpp>
79 #include <com/sun/star/frame/XComponentLoader.hpp>
80 #include <com/sun/star/lang/XInitialization.hpp>
81 #include <com/sun/star/ui/dialogs/XExecutableDialog.hpp>
82 #include <com/sun/star/ui/dialogs/ExecutableDialogResults.hpp>
83 #include <com/sun/star/util/XNumberFormatsSupplier.hpp>
85 using namespace ::com::sun::star
;
86 using namespace ::com::sun::star::uno
;
87 using ::rtl::OUString
;
91 Point
SwGetChartDialogPos( const Window
*pParentWin
, const Size
& rDialogSize
, const Rectangle
& rLogicChart
)
93 // !! positioning code according to spepc; similar to Calc fuins2.cxx
97 DBG_ASSERT( pParentWin
, "Window not found" );
100 Rectangle aObjPixel
= pParentWin
->LogicToPixel( rLogicChart
, pParentWin
->GetMapMode() );
101 Rectangle
aObjAbs( pParentWin
->OutputToAbsoluteScreenPixel( aObjPixel
.TopLeft() ),
102 pParentWin
->OutputToAbsoluteScreenPixel( aObjPixel
.BottomRight() ) );
104 Rectangle aDesktop
= pParentWin
->GetDesktopRectPixel();
105 Size aSpace
= pParentWin
->LogicToPixel( Size( 8, 12 ), MAP_APPFONT
);
107 BOOL bLayoutRTL
= ::GetActiveView()->GetWrtShell().IsTableRightToLeft();
108 bool bCenterHor
= false;
110 if ( aDesktop
.Bottom() - aObjAbs
.Bottom() >= rDialogSize
.Height() + aSpace
.Height() )
112 // first preference: below the chart
113 aRet
.Y() = aObjAbs
.Bottom() + aSpace
.Height();
116 else if ( aObjAbs
.Top() - aDesktop
.Top() >= rDialogSize
.Height() + aSpace
.Height() )
118 // second preference: above the chart
119 aRet
.Y() = aObjAbs
.Top() - rDialogSize
.Height() - aSpace
.Height();
124 bool bFitLeft
= ( aObjAbs
.Left() - aDesktop
.Left() >= rDialogSize
.Width() + aSpace
.Width() );
125 bool bFitRight
= ( aDesktop
.Right() - aObjAbs
.Right() >= rDialogSize
.Width() + aSpace
.Width() );
127 if ( bFitLeft
|| bFitRight
)
129 // if both fit, prefer right in RTL mode, left otherwise
130 bool bPutRight
= bFitRight
&& ( bLayoutRTL
|| !bFitLeft
);
132 aRet
.X() = aObjAbs
.Right() + aSpace
.Width();
134 aRet
.X() = aObjAbs
.Left() - rDialogSize
.Width() - aSpace
.Width();
137 aRet
.Y() = aObjAbs
.Top() + ( aObjAbs
.GetHeight() - rDialogSize
.Height() ) / 2;
141 // doesn't fit on any edge - put at the bottom of the screen
142 aRet
.Y() = aDesktop
.Bottom() - rDialogSize
.Height();
147 aRet
.X() = aObjAbs
.Left() + ( aObjAbs
.GetWidth() - rDialogSize
.Width() ) / 2;
149 // limit to screen (centering might lead to invalid positions)
150 if ( aRet
.X() + rDialogSize
.Width() - 1 > aDesktop
.Right() )
151 aRet
.X() = aDesktop
.Right() - rDialogSize
.Width() + 1;
152 if ( aRet
.X() < aDesktop
.Left() )
153 aRet
.X() = aDesktop
.Left();
154 if ( aRet
.Y() + rDialogSize
.Height() - 1 > aDesktop
.Bottom() )
155 aRet
.Y() = aDesktop
.Bottom() - rDialogSize
.Height() + 1;
156 if ( aRet
.Y() < aDesktop
.Top() )
157 aRet
.Y() = aDesktop
.Top();
163 /*------------------------------------------------------------------------
165 ------------------------------------------------------------------------*/
168 void SwInsertChart(Window
* pParent
, SfxBindings
* pBindings
)
172 SwView
*pView
= ::GetActiveView();
174 // get range string of marked data
175 SwWrtShell
&rWrtShell
= pView
->GetWrtShell();
176 uno::Reference
< chart2::data::XDataProvider
> xDataProvider
;
177 uno::Reference
< frame::XModel
> xChartModel
;
178 OUString aRangeString
;
180 if( rWrtShell
.IsCrsrInTbl())
182 if (!rWrtShell
.IsTableMode())
184 // select whole table
185 rWrtShell
.GetView().GetViewFrame()->GetDispatcher()->
186 Execute(FN_TABLE_SELECT_ALL
, SFX_CALLMODE_SYNCHRON
);
188 if( ! rWrtShell
.IsTblComplexForChart())
190 SwFrmFmt
* pTblFmt
= rWrtShell
.GetTableFmt();
191 String aCurrentTblName
= pTblFmt
->GetName();
192 // String aText( String::CreateFromAscii("<.>") ); // was used for UI
193 // aText.Insert( rWrtShell.GetBoxNms(), 2);
194 // aText.Insert( aCurrentTblName, 1 );
195 aRangeString
= aCurrentTblName
;
196 aRangeString
+= OUString::valueOf( sal_Unicode('.') );
197 aRangeString
+= rWrtShell
.GetBoxNms();
199 // get table data provider
200 xDataProvider
.set( pView
->GetDocShell()->getIDocumentChartDataProviderAccess()->GetChartDataProvider( true ) );
204 SwFlyFrmFmt
*pFlyFrmFmt
= 0;
205 xChartModel
.set( SwTableFUNC( &rWrtShell
, FALSE
).InsertChart( xDataProvider
, (sal_True
== xDataProvider
.is()), aRangeString
, &pFlyFrmFmt
));
208 //@todo get context from writer if that has one
209 uno::Reference
< uno::XComponentContext
> xContext(
210 ::cppu::defaultBootstrap_InitialComponentContext() );
211 if( xContext
.is() && xChartModel
.is() && xDataProvider
.is())
213 uno::Reference
< lang::XMultiComponentFactory
> xMCF( xContext
->getServiceManager() );
216 uno::Reference
< ui::dialogs::XExecutableDialog
> xDialog(
217 xMCF
->createInstanceWithContext(
218 C2U("com.sun.star.comp.chart2.WizardDialog")
219 , xContext
), uno::UNO_QUERY
);
220 uno::Reference
< lang::XInitialization
> xInit( xDialog
, uno::UNO_QUERY
);
223 uno::Reference
< awt::XWindow
> xDialogParentWindow(0);
225 uno::Sequence
<uno::Any
> aSeq(2);
226 uno::Any
* pArray
= aSeq
.getArray();
227 beans::PropertyValue aParam1
;
228 aParam1
.Name
= C2U("ParentWindow");
229 aParam1
.Value
<<= uno::makeAny(xDialogParentWindow
);
230 beans::PropertyValue aParam2
;
231 aParam2
.Name
= C2U("ChartModel");
232 aParam2
.Value
<<= uno::makeAny(xChartModel
);
233 pArray
[0] <<= uno::makeAny(aParam1
);
234 pArray
[1] <<= uno::makeAny(aParam2
);
235 xInit
->initialize( aSeq
);
237 // try to set the dialog's position so it doesn't hide the chart
238 uno::Reference
< beans::XPropertySet
> xDialogProps( xDialog
, uno::UNO_QUERY
);
239 if ( xDialogProps
.is() )
244 awt::Size aDialogAWTSize
;
245 if( xDialogProps
->getPropertyValue( ::rtl::OUString::createFromAscii("Size") )
248 Size
aDialogSize( aDialogAWTSize
.Width
, aDialogAWTSize
.Height
);
249 if ( aDialogSize
.Width() > 0 && aDialogSize
.Height() > 0 )
251 //calculate and set new position
254 aSwRect
= pFlyFrmFmt
->GetAnchoredObj()->GetObjRectWithSpaces();
255 Rectangle
aRect( aSwRect
.SVRect() );
256 Point aDialogPos
= SwGetChartDialogPos( &rWrtShell
.GetView().GetEditWin(), aDialogSize
, aRect
);
257 xDialogProps
->setPropertyValue( ::rtl::OUString::createFromAscii("Position"),
258 uno::makeAny( awt::Point(aDialogPos
.getX(),aDialogPos
.getY()) ) );
262 catch( uno::Exception
& )
264 DBG_ERROR( "Chart wizard couldn't be positioned automatically\n" );
268 sal_Int16 nDialogRet
= xDialog
->execute();
269 if( nDialogRet
== ui::dialogs::ExecutableDialogResults::CANCEL
)
272 rWrtShell
.getIDocumentUndoRedoAccess()->ClearRedo();
276 DBG_ASSERT( nDialogRet
== ui::dialogs::ExecutableDialogResults::OK
,
277 "dialog execution failed" );
280 uno::Reference
< lang::XComponent
> xComponent( xDialog
, uno::UNO_QUERY
);
282 xComponent
->dispose();
288 void __EXPORT
AutoEdit::KeyInput( const KeyEvent
& rEvt
)
290 USHORT nCode
= rEvt
.GetKeyCode().GetCode();
291 if( nCode
!= KEY_SPACE
)
292 Edit::KeyInput( rEvt
);