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