xref: /aoo42x/main/sw/source/ui/table/chartins.cxx (revision cdf0e10c)
1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
29*cdf0e10cSrcweir #include "precompiled_sw.hxx"
30*cdf0e10cSrcweir 
31*cdf0e10cSrcweir 
32*cdf0e10cSrcweir #include <sfx2/viewfrm.hxx>
33*cdf0e10cSrcweir #include <vcl/msgbox.hxx>
34*cdf0e10cSrcweir #include <sfx2/dispatch.hxx>
35*cdf0e10cSrcweir #include <sfx2/basedlgs.hxx>
36*cdf0e10cSrcweir #include <IDocumentUndoRedo.hxx>
37*cdf0e10cSrcweir 
38*cdf0e10cSrcweir #include <sfx2/app.hxx>
39*cdf0e10cSrcweir #include <swtypes.hxx>
40*cdf0e10cSrcweir #include <swmodule.hxx>
41*cdf0e10cSrcweir #include <wrtsh.hxx>
42*cdf0e10cSrcweir #include <docsh.hxx>
43*cdf0e10cSrcweir #include <view.hxx>
44*cdf0e10cSrcweir #include <chartins.hxx>
45*cdf0e10cSrcweir #include <tablemgr.hxx>
46*cdf0e10cSrcweir #include <frmfmt.hxx>
47*cdf0e10cSrcweir #include <swtable.hxx>
48*cdf0e10cSrcweir #include <tblsel.hxx>
49*cdf0e10cSrcweir #include <unochart.hxx>
50*cdf0e10cSrcweir #include <autoedit.hxx>
51*cdf0e10cSrcweir #include <doc.hxx>
52*cdf0e10cSrcweir 
53*cdf0e10cSrcweir #include <edtwin.hxx>
54*cdf0e10cSrcweir 
55*cdf0e10cSrcweir #include <cmdid.h>
56*cdf0e10cSrcweir #include <chartins.hrc>
57*cdf0e10cSrcweir #include <anchoredobject.hxx>
58*cdf0e10cSrcweir 
59*cdf0e10cSrcweir #include <sot/clsids.hxx>
60*cdf0e10cSrcweir 
61*cdf0e10cSrcweir #include <cppuhelper/bootstrap.hxx>
62*cdf0e10cSrcweir #include <cppuhelper/component_context.hxx>
63*cdf0e10cSrcweir #include <comphelper/processfactory.hxx>
64*cdf0e10cSrcweir #include <com/sun/star/chart2/data/XDataProvider.hpp>
65*cdf0e10cSrcweir #include <com/sun/star/chart2/data/XDataReceiver.hpp>
66*cdf0e10cSrcweir #include <com/sun/star/chart/ChartDataRowSource.hpp>
67*cdf0e10cSrcweir #include <com/sun/star/frame/XComponentLoader.hpp>
68*cdf0e10cSrcweir #include <com/sun/star/lang/XInitialization.hpp>
69*cdf0e10cSrcweir #include <com/sun/star/ui/dialogs/XExecutableDialog.hpp>
70*cdf0e10cSrcweir #include <com/sun/star/ui/dialogs/ExecutableDialogResults.hpp>
71*cdf0e10cSrcweir #include <com/sun/star/util/XNumberFormatsSupplier.hpp>
72*cdf0e10cSrcweir 
73*cdf0e10cSrcweir using namespace ::com::sun::star;
74*cdf0e10cSrcweir using namespace ::com::sun::star::uno;
75*cdf0e10cSrcweir using ::rtl::OUString;
76*cdf0e10cSrcweir 
77*cdf0e10cSrcweir 
78*cdf0e10cSrcweir 
79*cdf0e10cSrcweir Point SwGetChartDialogPos( const Window *pParentWin, const Size& rDialogSize, const Rectangle& rLogicChart )
80*cdf0e10cSrcweir {
81*cdf0e10cSrcweir     // !! positioning code according to spepc; similar to Calc fuins2.cxx
82*cdf0e10cSrcweir 
83*cdf0e10cSrcweir     Point aRet;
84*cdf0e10cSrcweir 
85*cdf0e10cSrcweir     DBG_ASSERT( pParentWin, "Window not found" );
86*cdf0e10cSrcweir     if (pParentWin)
87*cdf0e10cSrcweir     {
88*cdf0e10cSrcweir         Rectangle aObjPixel = pParentWin->LogicToPixel( rLogicChart, pParentWin->GetMapMode() );
89*cdf0e10cSrcweir         Rectangle aObjAbs( pParentWin->OutputToAbsoluteScreenPixel( aObjPixel.TopLeft() ),
90*cdf0e10cSrcweir                            pParentWin->OutputToAbsoluteScreenPixel( aObjPixel.BottomRight() ) );
91*cdf0e10cSrcweir 
92*cdf0e10cSrcweir         Rectangle aDesktop = pParentWin->GetDesktopRectPixel();
93*cdf0e10cSrcweir         Size aSpace = pParentWin->LogicToPixel( Size( 8, 12 ), MAP_APPFONT );
94*cdf0e10cSrcweir 
95*cdf0e10cSrcweir         sal_Bool bLayoutRTL = ::GetActiveView()->GetWrtShell().IsTableRightToLeft();
96*cdf0e10cSrcweir         bool bCenterHor = false;
97*cdf0e10cSrcweir 
98*cdf0e10cSrcweir         if ( aDesktop.Bottom() - aObjAbs.Bottom() >= rDialogSize.Height() + aSpace.Height() )
99*cdf0e10cSrcweir         {
100*cdf0e10cSrcweir             // first preference: below the chart
101*cdf0e10cSrcweir             aRet.Y() = aObjAbs.Bottom() + aSpace.Height();
102*cdf0e10cSrcweir             bCenterHor = true;
103*cdf0e10cSrcweir         }
104*cdf0e10cSrcweir         else if ( aObjAbs.Top() - aDesktop.Top() >= rDialogSize.Height() + aSpace.Height() )
105*cdf0e10cSrcweir         {
106*cdf0e10cSrcweir             // second preference: above the chart
107*cdf0e10cSrcweir             aRet.Y() = aObjAbs.Top() - rDialogSize.Height() - aSpace.Height();
108*cdf0e10cSrcweir             bCenterHor = true;
109*cdf0e10cSrcweir         }
110*cdf0e10cSrcweir         else
111*cdf0e10cSrcweir         {
112*cdf0e10cSrcweir             bool bFitLeft = ( aObjAbs.Left() - aDesktop.Left() >= rDialogSize.Width() + aSpace.Width() );
113*cdf0e10cSrcweir             bool bFitRight = ( aDesktop.Right() - aObjAbs.Right() >= rDialogSize.Width() + aSpace.Width() );
114*cdf0e10cSrcweir 
115*cdf0e10cSrcweir             if ( bFitLeft || bFitRight )
116*cdf0e10cSrcweir             {
117*cdf0e10cSrcweir                 // if both fit, prefer right in RTL mode, left otherwise
118*cdf0e10cSrcweir                 bool bPutRight = bFitRight && ( bLayoutRTL || !bFitLeft );
119*cdf0e10cSrcweir                 if ( bPutRight )
120*cdf0e10cSrcweir                     aRet.X() = aObjAbs.Right() + aSpace.Width();
121*cdf0e10cSrcweir                 else
122*cdf0e10cSrcweir                     aRet.X() = aObjAbs.Left() - rDialogSize.Width() - aSpace.Width();
123*cdf0e10cSrcweir 
124*cdf0e10cSrcweir                 // center vertically
125*cdf0e10cSrcweir                 aRet.Y() = aObjAbs.Top() + ( aObjAbs.GetHeight() - rDialogSize.Height() ) / 2;
126*cdf0e10cSrcweir             }
127*cdf0e10cSrcweir             else
128*cdf0e10cSrcweir             {
129*cdf0e10cSrcweir                 // doesn't fit on any edge - put at the bottom of the screen
130*cdf0e10cSrcweir                 aRet.Y() = aDesktop.Bottom() - rDialogSize.Height();
131*cdf0e10cSrcweir                 bCenterHor = true;
132*cdf0e10cSrcweir             }
133*cdf0e10cSrcweir         }
134*cdf0e10cSrcweir         if ( bCenterHor )
135*cdf0e10cSrcweir             aRet.X() = aObjAbs.Left() + ( aObjAbs.GetWidth() - rDialogSize.Width() ) / 2;
136*cdf0e10cSrcweir 
137*cdf0e10cSrcweir         // limit to screen (centering might lead to invalid positions)
138*cdf0e10cSrcweir         if ( aRet.X() + rDialogSize.Width() - 1 > aDesktop.Right() )
139*cdf0e10cSrcweir             aRet.X() = aDesktop.Right() - rDialogSize.Width() + 1;
140*cdf0e10cSrcweir         if ( aRet.X() < aDesktop.Left() )
141*cdf0e10cSrcweir             aRet.X() = aDesktop.Left();
142*cdf0e10cSrcweir         if ( aRet.Y() + rDialogSize.Height() - 1 > aDesktop.Bottom() )
143*cdf0e10cSrcweir             aRet.Y() = aDesktop.Bottom() - rDialogSize.Height() + 1;
144*cdf0e10cSrcweir         if ( aRet.Y() < aDesktop.Top() )
145*cdf0e10cSrcweir             aRet.Y() = aDesktop.Top();
146*cdf0e10cSrcweir     }
147*cdf0e10cSrcweir 
148*cdf0e10cSrcweir     return aRet;
149*cdf0e10cSrcweir }
150*cdf0e10cSrcweir 
151*cdf0e10cSrcweir /*------------------------------------------------------------------------
152*cdf0e10cSrcweir 	Beschreibung:
153*cdf0e10cSrcweir ------------------------------------------------------------------------*/
154*cdf0e10cSrcweir 
155*cdf0e10cSrcweir 
156*cdf0e10cSrcweir void SwInsertChart(Window* pParent, SfxBindings* pBindings )
157*cdf0e10cSrcweir {
158*cdf0e10cSrcweir 	(void) pParent;
159*cdf0e10cSrcweir 	(void) pBindings;
160*cdf0e10cSrcweir     SwView *pView = ::GetActiveView();
161*cdf0e10cSrcweir 
162*cdf0e10cSrcweir     // get range string of marked data
163*cdf0e10cSrcweir     SwWrtShell &rWrtShell = pView->GetWrtShell();
164*cdf0e10cSrcweir     uno::Reference< chart2::data::XDataProvider > xDataProvider;
165*cdf0e10cSrcweir     uno::Reference< frame::XModel > xChartModel;
166*cdf0e10cSrcweir     OUString aRangeString;
167*cdf0e10cSrcweir 
168*cdf0e10cSrcweir 	if( rWrtShell.IsCrsrInTbl())
169*cdf0e10cSrcweir     {
170*cdf0e10cSrcweir         if (!rWrtShell.IsTableMode())
171*cdf0e10cSrcweir         {
172*cdf0e10cSrcweir             // select whole table
173*cdf0e10cSrcweir             rWrtShell.GetView().GetViewFrame()->GetDispatcher()->
174*cdf0e10cSrcweir                 Execute(FN_TABLE_SELECT_ALL, SFX_CALLMODE_SYNCHRON);
175*cdf0e10cSrcweir         }
176*cdf0e10cSrcweir         if( ! rWrtShell.IsTblComplexForChart())
177*cdf0e10cSrcweir         {
178*cdf0e10cSrcweir             SwFrmFmt* pTblFmt = rWrtShell.GetTableFmt();
179*cdf0e10cSrcweir             String aCurrentTblName = pTblFmt->GetName();
180*cdf0e10cSrcweir //             String aText( String::CreateFromAscii("<.>") );   // was used for UI
181*cdf0e10cSrcweir //             aText.Insert( rWrtShell.GetBoxNms(), 2);
182*cdf0e10cSrcweir //             aText.Insert( aCurrentTblName, 1 );
183*cdf0e10cSrcweir             aRangeString = aCurrentTblName;
184*cdf0e10cSrcweir             aRangeString += OUString::valueOf( sal_Unicode('.') );
185*cdf0e10cSrcweir             aRangeString += rWrtShell.GetBoxNms();
186*cdf0e10cSrcweir 
187*cdf0e10cSrcweir             // get table data provider
188*cdf0e10cSrcweir             xDataProvider.set( pView->GetDocShell()->getIDocumentChartDataProviderAccess()->GetChartDataProvider( true ) );
189*cdf0e10cSrcweir         }
190*cdf0e10cSrcweir     }
191*cdf0e10cSrcweir 
192*cdf0e10cSrcweir     SwFlyFrmFmt *pFlyFrmFmt = 0;
193*cdf0e10cSrcweir     xChartModel.set( SwTableFUNC( &rWrtShell, sal_False ).InsertChart( xDataProvider, (sal_True == xDataProvider.is()), aRangeString, &pFlyFrmFmt ));
194*cdf0e10cSrcweir 
195*cdf0e10cSrcweir     //open wizard
196*cdf0e10cSrcweir     //@todo get context from writer if that has one
197*cdf0e10cSrcweir     uno::Reference< uno::XComponentContext > xContext(
198*cdf0e10cSrcweir         ::cppu::defaultBootstrap_InitialComponentContext() );
199*cdf0e10cSrcweir     if( xContext.is() && xChartModel.is() && xDataProvider.is())
200*cdf0e10cSrcweir     {
201*cdf0e10cSrcweir         uno::Reference< lang::XMultiComponentFactory > xMCF( xContext->getServiceManager() );
202*cdf0e10cSrcweir         if(xMCF.is())
203*cdf0e10cSrcweir         {
204*cdf0e10cSrcweir             uno::Reference< ui::dialogs::XExecutableDialog > xDialog(
205*cdf0e10cSrcweir                 xMCF->createInstanceWithContext(
206*cdf0e10cSrcweir                     C2U("com.sun.star.comp.chart2.WizardDialog")
207*cdf0e10cSrcweir                     , xContext), uno::UNO_QUERY);
208*cdf0e10cSrcweir             uno::Reference< lang::XInitialization > xInit( xDialog, uno::UNO_QUERY );
209*cdf0e10cSrcweir             if( xInit.is() )
210*cdf0e10cSrcweir             {
211*cdf0e10cSrcweir                 uno::Reference< awt::XWindow > xDialogParentWindow(0);
212*cdf0e10cSrcweir                 //	initialize dialog
213*cdf0e10cSrcweir                 uno::Sequence<uno::Any> aSeq(2);
214*cdf0e10cSrcweir                 uno::Any* pArray = aSeq.getArray();
215*cdf0e10cSrcweir                 beans::PropertyValue aParam1;
216*cdf0e10cSrcweir                 aParam1.Name = C2U("ParentWindow");
217*cdf0e10cSrcweir                 aParam1.Value <<= uno::makeAny(xDialogParentWindow);
218*cdf0e10cSrcweir                 beans::PropertyValue aParam2;
219*cdf0e10cSrcweir                 aParam2.Name = C2U("ChartModel");
220*cdf0e10cSrcweir                 aParam2.Value <<= uno::makeAny(xChartModel);
221*cdf0e10cSrcweir                 pArray[0] <<= uno::makeAny(aParam1);
222*cdf0e10cSrcweir                 pArray[1] <<= uno::makeAny(aParam2);
223*cdf0e10cSrcweir                 xInit->initialize( aSeq );
224*cdf0e10cSrcweir 
225*cdf0e10cSrcweir                 // try to set the dialog's position so it doesn't hide the chart
226*cdf0e10cSrcweir                 uno::Reference < beans::XPropertySet > xDialogProps( xDialog, uno::UNO_QUERY );
227*cdf0e10cSrcweir                 if ( xDialogProps.is() )
228*cdf0e10cSrcweir                 {
229*cdf0e10cSrcweir                     try
230*cdf0e10cSrcweir                     {
231*cdf0e10cSrcweir                         //get dialog size:
232*cdf0e10cSrcweir                         awt::Size aDialogAWTSize;
233*cdf0e10cSrcweir                         if( xDialogProps->getPropertyValue( ::rtl::OUString::createFromAscii("Size") )
234*cdf0e10cSrcweir                             >>= aDialogAWTSize )
235*cdf0e10cSrcweir                         {
236*cdf0e10cSrcweir                             Size aDialogSize( aDialogAWTSize.Width, aDialogAWTSize.Height );
237*cdf0e10cSrcweir                             if ( aDialogSize.Width() > 0 && aDialogSize.Height() > 0 )
238*cdf0e10cSrcweir                             {
239*cdf0e10cSrcweir                                 //calculate and set new position
240*cdf0e10cSrcweir                                 SwRect aSwRect;
241*cdf0e10cSrcweir                                 if (pFlyFrmFmt)
242*cdf0e10cSrcweir                                     aSwRect = pFlyFrmFmt->GetAnchoredObj()->GetObjRectWithSpaces();
243*cdf0e10cSrcweir                                 Rectangle aRect( aSwRect.SVRect() );
244*cdf0e10cSrcweir                                 Point aDialogPos = SwGetChartDialogPos( &rWrtShell.GetView().GetEditWin(), aDialogSize, aRect );
245*cdf0e10cSrcweir                                 xDialogProps->setPropertyValue( ::rtl::OUString::createFromAscii("Position"),
246*cdf0e10cSrcweir                                     uno::makeAny( awt::Point(aDialogPos.getX(),aDialogPos.getY()) ) );
247*cdf0e10cSrcweir                             }
248*cdf0e10cSrcweir                         }
249*cdf0e10cSrcweir                     }
250*cdf0e10cSrcweir                     catch( uno::Exception& )
251*cdf0e10cSrcweir                     {
252*cdf0e10cSrcweir                         DBG_ERROR( "Chart wizard couldn't be positioned automatically\n" );
253*cdf0e10cSrcweir                     }
254*cdf0e10cSrcweir                 }
255*cdf0e10cSrcweir 
256*cdf0e10cSrcweir                 sal_Int16 nDialogRet = xDialog->execute();
257*cdf0e10cSrcweir                 if( nDialogRet == ui::dialogs::ExecutableDialogResults::CANCEL )
258*cdf0e10cSrcweir                 {
259*cdf0e10cSrcweir 					rWrtShell.Undo();
260*cdf0e10cSrcweir                     rWrtShell.GetIDocumentUndoRedo().ClearRedo();
261*cdf0e10cSrcweir                 }
262*cdf0e10cSrcweir                 else
263*cdf0e10cSrcweir                 {
264*cdf0e10cSrcweir                     DBG_ASSERT( nDialogRet == ui::dialogs::ExecutableDialogResults::OK,
265*cdf0e10cSrcweir                         "dialog execution failed" );
266*cdf0e10cSrcweir                 }
267*cdf0e10cSrcweir             }
268*cdf0e10cSrcweir             uno::Reference< lang::XComponent > xComponent( xDialog, uno::UNO_QUERY );
269*cdf0e10cSrcweir             if( xComponent.is())
270*cdf0e10cSrcweir                 xComponent->dispose();
271*cdf0e10cSrcweir         }
272*cdf0e10cSrcweir     }
273*cdf0e10cSrcweir }
274*cdf0e10cSrcweir 
275*cdf0e10cSrcweir 
276*cdf0e10cSrcweir void __EXPORT AutoEdit::KeyInput( const KeyEvent& rEvt )
277*cdf0e10cSrcweir {
278*cdf0e10cSrcweir 	sal_uInt16 nCode = rEvt.GetKeyCode().GetCode();
279*cdf0e10cSrcweir 	if( nCode != KEY_SPACE )
280*cdf0e10cSrcweir 		Edit::KeyInput( rEvt );
281*cdf0e10cSrcweir }
282*cdf0e10cSrcweir 
283*cdf0e10cSrcweir 
284*cdf0e10cSrcweir 
285*cdf0e10cSrcweir 
286