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