xref: /trunk/main/sd/source/ui/table/tablefunction.cxx (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
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_sd.hxx"
30 
31 #include <sal/config.h>
32 #include <com/sun/star/uno/XComponentContext.hpp>
33 #include <com/sun/star/drawing/XSelectionFunction.hpp>
34 #include <com/sun/star/awt/KeyModifier.hpp>
35 #include <com/sun/star/lang/XInitialization.hpp>
36 
37 #include <cppuhelper/compbase2.hxx>
38 #include <cppuhelper/basemutex.hxx>
39 
40 #include <vcl/svapp.hxx>
41 
42 #include <svx/svdotable.hxx>
43 #include <svx/sdr/overlay/overlayobjectcell.hxx>
44 #include <svx/sdr/overlay/overlaymanager.hxx>
45 #include <svx/svxids.hrc>
46 #include <editeng/outlobj.hxx>
47 #include <svx/svdoutl.hxx>
48 #include <svx/svdpagv.hxx>
49 #include <svx/svdetc.hxx>
50 #include <editeng/editstat.hxx>
51 #include <editeng/unolingu.hxx>
52 #include <svx/sdrpagewindow.hxx>
53 #include <svx/sdr/table/tabledesign.hxx>
54 #include <svx/svxdlg.hxx>
55 #include <vcl/msgbox.hxx>
56 
57 #include <svl/itempool.hxx>
58 #include <sfx2/viewfrm.hxx>
59 #include <sfx2/dispatch.hxx>
60 #include <sfx2/bindings.hxx>
61 #include <sfx2/request.hxx>
62 #include <svl/style.hxx>
63 
64 #include "framework/FrameworkHelper.hxx"
65 #include "app.hrc"
66 #include "glob.hrc"
67 #include "DrawViewShell.hxx"
68 #include "drawdoc.hxx"
69 #include "DrawDocShell.hxx"
70 #include "Window.hxx"
71 #include "drawview.hxx"
72 #include "sdresid.hxx"
73 #include "undo/undoobjects.hxx"
74 
75 using ::rtl::OUString;
76 using namespace ::sd;
77 using namespace ::sdr::table;
78 using namespace ::com::sun::star;
79 using namespace ::com::sun::star::uno;
80 using namespace ::com::sun::star::beans;
81 using namespace ::com::sun::star::util;
82 using namespace ::com::sun::star::frame;
83 using namespace ::com::sun::star::container;
84 using namespace ::com::sun::star::lang;
85 using namespace ::com::sun::star::drawing;
86 using namespace ::com::sun::star::linguistic2;
87 
88 namespace css = ::com::sun::star;
89 
90 namespace sd
91 {
92 extern void showTableDesignDialog( ::Window*, ViewShellBase& );
93 
94 static void apply_table_style( SdrTableObj* pObj, SdrModel* pModel, const OUString& sTableStyle )
95 {
96     if( pModel && pObj )
97     {
98         Reference< XNameAccess > xPool( dynamic_cast< XNameAccess* >( pModel->GetStyleSheetPool() ) );
99         if( xPool.is() ) try
100         {
101             const OUString sFamilyName( RTL_CONSTASCII_USTRINGPARAM( "table" ) );
102             Reference< XNameContainer > xTableFamily( xPool->getByName( sFamilyName ), UNO_QUERY_THROW );
103             OUString aStdName( RTL_CONSTASCII_USTRINGPARAM("default") );
104             if( sTableStyle.getLength() )
105                 aStdName = sTableStyle;
106             Reference< XIndexAccess > xStyle( xTableFamily->getByName( aStdName ), UNO_QUERY_THROW );
107             pObj->setTableStyle( xStyle );
108         }
109         catch( Exception& )
110         {
111             DBG_ERROR("sd::apply_default_table_style(), exception caught!");
112         }
113     }
114 }
115 
116 void DrawViewShell::FuTable(SfxRequest& rReq)
117 {
118     switch( rReq.GetSlot() )
119     {
120     case SID_INSERT_TABLE:
121     {
122         sal_Int32 nColumns = 0;
123         sal_Int32 nRows = 0;
124         OUString sTableStyle;
125 
126         SFX_REQUEST_ARG( rReq, pCols, SfxUInt16Item, SID_ATTR_TABLE_COLUMN, sal_False );
127         SFX_REQUEST_ARG( rReq, pRows, SfxUInt16Item, SID_ATTR_TABLE_ROW, sal_False );
128         SFX_REQUEST_ARG( rReq, pStyle, SfxStringItem, SID_TABLE_STYLE, sal_False );
129 
130         if( pCols )
131             nColumns = pCols->GetValue();
132 
133         if( pRows )
134             nRows = pRows->GetValue();
135 
136         if( pStyle )
137             sTableStyle = pStyle->GetValue();
138 
139         if( (nColumns == 0) || (nRows == 0) )
140         {
141             SvxAbstractDialogFactory* pFact = SvxAbstractDialogFactory::Create();
142             ::std::auto_ptr<SvxAbstractNewTableDialog> pDlg( pFact ? pFact->CreateSvxNewTableDialog( NULL ) : 0);
143 
144             if( !pDlg.get() || (pDlg->Execute() != RET_OK) )
145                 break;
146 
147             nColumns = pDlg->getColumns();
148             nRows = pDlg->getRows();
149         }
150 
151         Rectangle aRect;
152 
153         SdrObject* pPickObj = mpView->GetEmptyPresentationObject( PRESOBJ_TABLE );
154         if( pPickObj )
155         {
156             aRect = pPickObj->GetLogicRect();
157             aRect.setHeight( 200 );
158         }
159         else
160         {
161             Size aSize( 14100, 200 );
162 
163             Point aPos;
164             Rectangle aWinRect(aPos, GetActiveWindow()->GetOutputSizePixel() );
165             aPos = aWinRect.Center();
166             aPos = GetActiveWindow()->PixelToLogic(aPos);
167             aPos.X() -= aSize.Width() / 2;
168             aPos.Y() -= aSize.Height() / 2;
169             aRect = Rectangle(aPos, aSize);
170         }
171 
172         ::sdr::table::SdrTableObj* pObj = new ::sdr::table::SdrTableObj( GetDoc(), aRect, nColumns, nRows );
173         pObj->NbcSetStyleSheet( GetDoc()->GetDefaultStyleSheet(), sal_True );
174         apply_table_style( pObj, GetDoc(), sTableStyle );
175         SdrPageView* pPV = mpView->GetSdrPageView();
176 
177         // if we have a pick obj we need to make this new ole a pres obj replacing the current pick obj
178         if( pPickObj )
179         {
180             SdPage* pPage = static_cast< SdPage* >(pPickObj->GetPage());
181             if(pPage && pPage->IsPresObj(pPickObj))
182             {
183                 pObj->SetUserCall( pPickObj->GetUserCall() );
184                 pPage->InsertPresObj( pObj, PRESOBJ_TABLE );
185             }
186         }
187 
188         GetParentWindow()->GrabFocus();
189         if( pPickObj )
190             mpView->ReplaceObjectAtView(pPickObj, *pPV, pObj, sal_True );
191         else
192             mpView->InsertObjectAtView(pObj, *pPV, SDRINSERT_SETDEFLAYER);
193 
194         Invalidate(SID_DRAWTBX_INSERT);
195         rReq.Ignore();
196 SfxViewShell* pViewShell = GetViewShell();
197         OSL_ASSERT (pViewShell!=NULL);
198         SfxBindings& rBindings = pViewShell->GetViewFrame()->GetBindings();
199         rBindings.Invalidate( SID_INSERT_TABLE, sal_True, sal_False );
200         break;
201     }
202     case SID_TABLEDESIGN:
203     {
204         if( GetDoc() && (GetDoc()->GetDocumentType() == DOCUMENT_TYPE_DRAW) )
205         {
206             // in draw open a modal dialog since we have no tool pane yet
207             showTableDesignDialog( GetActiveWindow(), GetViewShellBase() );
208         }
209         else
210         {
211             // Make the slide transition panel visible (expand it) in the
212             // tool pane.
213             framework::FrameworkHelper::Instance(GetViewShellBase())->RequestTaskPanel(
214                 framework::FrameworkHelper::msTableDesignPanelURL);
215         }
216 
217         Cancel();
218         rReq.Done ();
219     }
220     default:
221         break;
222     }
223 }
224 
225 // --------------------------------------------------------------------
226 
227 void DrawViewShell::GetTableMenuState( SfxItemSet &rSet )
228 {
229     bool bIsUIActive = GetDocSh()->IsUIActive();
230     if( bIsUIActive )
231     {
232         rSet.DisableItem( SID_INSERT_TABLE );
233     }
234     else
235     {
236         String aActiveLayer = mpDrawView->GetActiveLayer();
237         SdrPageView* pPV = mpDrawView->GetSdrPageView();
238 
239         if( bIsUIActive ||
240             ( aActiveLayer.Len() != 0 && pPV && ( pPV->IsLayerLocked(aActiveLayer) ||
241             !pPV->IsLayerVisible(aActiveLayer) ) ) ||
242             SD_MOD()->GetWaterCan() )
243         {
244             rSet.DisableItem( SID_INSERT_TABLE );
245         }
246     }
247 }
248 
249 // --------------------------------------------------------------------
250 
251 void CreateTableFromRTF( SvStream& rStream, SdDrawDocument* pModel )
252 {
253     rStream.Seek( 0 );
254 
255     if( pModel )
256     {
257         SdrPage* pPage = pModel->GetPage(0);
258         if( pPage )
259         {
260             Size aSize( 200, 200 );
261             Point aPos;
262             Rectangle aRect (aPos, aSize);
263             ::sdr::table::SdrTableObj* pObj = new ::sdr::table::SdrTableObj( pModel, aRect, 1, 1 );
264             pObj->NbcSetStyleSheet( pModel->GetDefaultStyleSheet(), sal_True );
265             OUString sTableStyle;
266             apply_table_style( pObj, pModel, sTableStyle );
267 
268             pPage->NbcInsertObject( pObj );
269 
270             sdr::table::SdrTableObj::ImportAsRTF( rStream, *pObj );
271         }
272     }
273 }
274 
275 }
276