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