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