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 
apply_table_style(SdrTableObj * pObj,SdrModel * pModel,const OUString & sTableStyle)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 
FuTable(SfxRequest & rReq)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         // #123359# if an object is to be replaced/manipulated it may be that it is in text edit mode,
175         // so to be on the safe side call SdrEndTextEdit here
176         SdrTextObj* pCheckForTextEdit = dynamic_cast< SdrTextObj* >(pPickObj);
177 
178         if(pCheckForTextEdit && pCheckForTextEdit->IsInEditMode())
179         {
180             mpView->SdrEndTextEdit();
181         }
182 
183 		// if we have a pick obj we need to make this new ole a pres obj replacing the current pick obj
184 		if( pPickObj )
185 		{
186 			SdPage* pPage = static_cast< SdPage* >(pPickObj->GetPage());
187 			if(pPage && pPage->IsPresObj(pPickObj))
188 			{
189 				pObj->SetUserCall( pPickObj->GetUserCall() );
190 				pPage->InsertPresObj( pObj, PRESOBJ_TABLE );
191 			}
192 		}
193 
194 		GetParentWindow()->GrabFocus();
195 		if( pPickObj )
196 			mpView->ReplaceObjectAtView(pPickObj, *pPV, pObj, sal_True );
197 		else
198 			mpView->InsertObjectAtView(pObj, *pPV, SDRINSERT_SETDEFLAYER);
199 
200 		Invalidate(SID_DRAWTBX_INSERT);
201 		rReq.Ignore();
202 SfxViewShell* pViewShell = GetViewShell();
203         OSL_ASSERT (pViewShell!=NULL);
204 		SfxBindings& rBindings = pViewShell->GetViewFrame()->GetBindings();
205 		rBindings.Invalidate( SID_INSERT_TABLE, sal_True, sal_False );
206 		break;
207 	}
208 	case SID_TABLEDESIGN:
209 	{
210 		if( GetDoc() && (GetDoc()->GetDocumentType() == DOCUMENT_TYPE_DRAW) )
211 		{
212 			// in draw open a modal dialog since we have no tool pane yet
213 			showTableDesignDialog( GetActiveWindow(), GetViewShellBase() );
214 		}
215 		else
216 		{
217 			// Make the table design panel visible (expand it) in the
218 	        // sidebar.
219             ::sfx2::sidebar::Sidebar::ShowPanel(
220                 ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("ImpressTableDesignPanel")),
221                 GetViewFrame()->GetFrame().GetFrameInterface());
222 		}
223 
224 		Cancel();
225 		rReq.Done ();
226 	}
227 	default:
228 		break;
229 	}
230 }
231 
232 // --------------------------------------------------------------------
233 
GetTableMenuState(SfxItemSet & rSet)234 void DrawViewShell::GetTableMenuState( SfxItemSet &rSet )
235 {
236 	bool bIsUIActive = GetDocSh()->IsUIActive();
237 	if( bIsUIActive )
238 	{
239 		rSet.DisableItem( SID_INSERT_TABLE );
240 	}
241 	else
242 	{
243 		String aActiveLayer = mpDrawView->GetActiveLayer();
244 		SdrPageView* pPV = mpDrawView->GetSdrPageView();
245 
246 		if( bIsUIActive ||
247 			( aActiveLayer.Len() != 0 && pPV && ( pPV->IsLayerLocked(aActiveLayer) ||
248 			!pPV->IsLayerVisible(aActiveLayer) ) ) ||
249 			SD_MOD()->GetWaterCan() )
250 		{
251 			rSet.DisableItem( SID_INSERT_TABLE );
252 		}
253 	}
254 }
255 
256 // --------------------------------------------------------------------
257 
CreateTableFromRTF(SvStream & rStream,SdDrawDocument * pModel)258 void CreateTableFromRTF( SvStream& rStream, SdDrawDocument* pModel )
259 {
260 	rStream.Seek( 0 );
261 
262 	if( pModel )
263 	{
264 		SdrPage* pPage = pModel->GetPage(0);
265 		if( pPage )
266 		{
267 			Size aSize( 200, 200 );
268 			Point aPos;
269 			Rectangle aRect (aPos, aSize);
270 			::sdr::table::SdrTableObj* pObj = new ::sdr::table::SdrTableObj( pModel, aRect, 1, 1 );
271 			pObj->NbcSetStyleSheet( pModel->GetDefaultStyleSheet(), sal_True );
272 			OUString sTableStyle;
273 			apply_table_style( pObj, pModel, sTableStyle );
274 
275 			pPage->NbcInsertObject( pObj );
276 
277 			sdr::table::SdrTableObj::ImportAsRTF( rStream, *pObj );
278 		}
279 	}
280 }
281 
282 }
283