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