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