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