xref: /core/sd/source/ui/table/tablefunction.cxx (revision 8611f6e2)
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3  * This file is part of the LibreOffice project.
4  *
5  * This Source Code Form is subject to the terms of the Mozilla Public
6  * License, v. 2.0. If a copy of the MPL was not distributed with this
7  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8  *
9  * This file incorporates work covered by the following license notice:
10  *
11  *   Licensed to the Apache Software Foundation (ASF) under one or more
12  *   contributor license agreements. See the NOTICE file distributed
13  *   with this work for additional information regarding copyright
14  *   ownership. The ASF licenses this file to you under the Apache
15  *   License, Version 2.0 (the "License"); you may not use this file
16  *   except in compliance with the License. You may obtain a copy of
17  *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
18  */
19 
20 #include <sal/config.h>
21 #include <com/sun/star/uno/XComponentContext.hpp>
22 #include <com/sun/star/drawing/XSelectionFunction.hpp>
23 
24 #include <comphelper/lok.hxx>
25 
26 #include <svx/svdotable.hxx>
27 #include <svx/svxids.hrc>
28 #include <svx/svdpagv.hxx>
29 #include <svx/svxdlg.hxx>
30 
31 #include <svl/intitem.hxx>
32 #include <svl/stritem.hxx>
33 #include <sfx2/viewfrm.hxx>
34 #include <sfx2/bindings.hxx>
35 #include <sfx2/request.hxx>
36 #include <sfx2/sidebar/Sidebar.hxx>
37 #include <svl/style.hxx>
38 #include <comphelper/diagnose_ex.hxx>
39 
40 #include <tablefunction.hxx>
41 #include <DrawViewShell.hxx>
42 #include <drawdoc.hxx>
43 #include <sdpage.hxx>
44 #include <Window.hxx>
45 #include <drawview.hxx>
46 #include <sdmod.hxx>
47 
48 #include <memory>
49 
50 using namespace ::sd;
51 using namespace sdr::table;
52 using namespace ::com::sun::star;
53 using namespace ::com::sun::star::uno;
54 using namespace ::com::sun::star::beans;
55 using namespace ::com::sun::star::util;
56 using namespace ::com::sun::star::container;
57 using namespace ::com::sun::star::lang;
58 using namespace ::com::sun::star::drawing;
59 using namespace ::com::sun::star::linguistic2;
60 
61 namespace sd
62 {
63 
64 static void apply_table_style( SdrTableObj* pObj, SdrModel const * pModel, const OUString& sTableStyle )
65 {
66     if( !(pModel && pObj) )
67         return;
68 
69     Reference< XNameAccess > xPool( dynamic_cast< XNameAccess* >( pModel->GetStyleSheetPool() ) );
70     if( !xPool.is() )
71         return;
72 
73     try
74     {
75         Reference< XNameContainer > xTableFamily( xPool->getByName( "table" ), UNO_QUERY_THROW );
76         OUString aStdName( "default" );
77         if( !sTableStyle.isEmpty() )
78             aStdName = sTableStyle;
79         Reference< XIndexAccess > xStyle( xTableFamily->getByName( aStdName ), UNO_QUERY_THROW );
80         pObj->setTableStyle( xStyle );
81     }
82     catch( Exception& )
83     {
84         TOOLS_WARN_EXCEPTION( "sd", "sd::apply_default_table_style()");
85     }
86 }
87 
88 static void InsertTableImpl(const DrawViewShell* pShell,
89                             ::sd::View* pView,
90                             sal_Int32 nColumns,
91                             sal_Int32 nRows,
92                             const OUString& sTableStyle)
93 {
94     ::tools::Rectangle aRect;
95 
96     SdrObject* pPickObj = pView->GetEmptyPresentationObject( PresObjKind::Table );
97     if( pPickObj )
98     {
99         aRect = pPickObj->GetLogicRect();
100         aRect.setHeight( 200 );
101     }
102     else
103     {
104         Size aSize( 14100, 2000 );
105 
106         Point aPos;
107         ::tools::Rectangle aWinRect(aPos, pShell->GetActiveWindow()->GetOutputSizePixel());
108         aWinRect = pShell->GetActiveWindow()->PixelToLogic(aWinRect);
109 
110         // make sure that the default size of the table fits on the paper and is inside the viewing area.
111         // if zoomed in close, don't make the table bigger than the viewing window.
112         Size aMaxSize = pShell->getCurrentPage()->GetSize();
113 
114         if (comphelper::LibreOfficeKit::isActive())
115         {
116             // aWinRect is nonsensical in the LOK case
117             aWinRect = ::tools::Rectangle(aPos, aMaxSize);
118         }
119         else
120         {
121             if( aMaxSize.Height() > aWinRect.getOpenHeight() )
122                 aMaxSize.setHeight( aWinRect.getOpenHeight() );
123             if( aMaxSize.Width() > aWinRect.getOpenWidth() )
124                 aMaxSize.setWidth( aWinRect.getOpenWidth() );
125         }
126 
127         if( aSize.Width() > aMaxSize.getWidth() )
128             aSize.setWidth( aMaxSize.getWidth() );
129 
130         // adjust height based on # of rows.
131         if( nRows > 0 )
132         {
133             aSize.setHeight( aSize.Height() * nRows );
134             if( aSize.Height() > aMaxSize.getHeight() )
135                 aSize.setHeight( aMaxSize.getHeight() );
136         }
137 
138         aPos = aWinRect.Center();
139         aPos.AdjustX( -(aSize.Width() / 2) );
140         aPos.AdjustY( -(aSize.Height() / 2) );
141         aRect = ::tools::Rectangle(aPos, aSize);
142     }
143 
144     rtl::Reference<sdr::table::SdrTableObj> pObj = new sdr::table::SdrTableObj(
145         *pShell->GetDoc(), // TTTT should be reference
146         aRect,
147         nColumns,
148         nRows);
149     pObj->NbcSetStyleSheet( pShell->GetDoc()->GetDefaultStyleSheet(), true );
150     apply_table_style( pObj.get(), pShell->GetDoc(), sTableStyle );
151     SdrPageView* pPV = pView->GetSdrPageView();
152 
153     // #i123359# if an object is to be replaced/manipulated it may be that it is in text edit mode,
154     // so to be on the safe side call SdrEndTextEdit here
155     SdrTextObj* pCheckForTextEdit = dynamic_cast< SdrTextObj* >(pPickObj);
156 
157     if(pCheckForTextEdit && pCheckForTextEdit->IsInEditMode())
158     {
159         pView->SdrEndTextEdit();
160     }
161 
162     // if we have a pick obj we need to make this new ole a pres obj replacing the current pick obj
163     if( pPickObj )
164     {
165         SdPage* pPage = static_cast< SdPage* >(pPickObj->getSdrPageFromSdrObject());
166         if(pPage && pPage->IsPresObj(pPickObj))
167         {
168             pObj->SetUserCall( pPickObj->GetUserCall() );
169             pPage->InsertPresObj( pObj.get(), PresObjKind::Table );
170         }
171     }
172 
173     pShell->GetParentWindow()->GrabFocus();
174     if( pPickObj )
175         pView->ReplaceObjectAtView(pPickObj, *pPV, pObj.get() );
176     else
177         pView->InsertObjectAtView(pObj.get(), *pPV, SdrInsertFlags::SETDEFLAYER);
178 }
179 
180 void DrawViewShell::FuTable(SfxRequest& rReq)
181 {
182     switch( rReq.GetSlot() )
183     {
184     case SID_INSERT_TABLE:
185     {
186         sal_Int32 nColumns = 0;
187         sal_Int32 nRows = 0;
188         OUString sTableStyle;
189         DrawViewShell* pShell = this;
190         ::sd::View* pView = mpView;
191 
192         const SfxUInt16Item* pCols = rReq.GetArg<SfxUInt16Item>(SID_ATTR_TABLE_COLUMN);
193         const SfxUInt16Item* pRows = rReq.GetArg<SfxUInt16Item>(SID_ATTR_TABLE_ROW);
194         const SfxStringItem* pStyle = rReq.GetArg<SfxStringItem>(SID_TABLE_STYLE);
195 
196         if( pCols )
197             nColumns = pCols->GetValue();
198 
199         if( pRows )
200             nRows = pRows->GetValue();
201 
202         if( pStyle )
203             sTableStyle = pStyle->GetValue();
204 
205         if( (nColumns == 0) || (nRows == 0) )
206         {
207             SvxAbstractDialogFactory* pFact = SvxAbstractDialogFactory::Create();
208             std::shared_ptr<SvxAbstractNewTableDialog> pDlg( pFact->CreateSvxNewTableDialog(rReq.GetFrameWeld()) );
209 
210             weld::DialogController::runAsync(pDlg->getDialogController(),
211                 [pDlg, pShell, pView, sTableStyle] (sal_Int32 nResult) {
212                     if (nResult == RET_OK)
213                     {
214                         sal_Int32 nColumnsIn = pDlg->getColumns();
215                         sal_Int32 nRowsIn = pDlg->getRows();
216 
217                         InsertTableImpl(pShell, pView, nColumnsIn, nRowsIn, sTableStyle);
218                     }
219                 });
220         }
221         else
222         {
223             InsertTableImpl(pShell, pView, nColumns, nRows, sTableStyle);
224         }
225 
226         rReq.Ignore();
227         SfxViewShell* pViewShell = GetViewShell();
228         OSL_ASSERT (pViewShell!=nullptr);
229         SfxBindings& rBindings = pViewShell->GetViewFrame()->GetBindings();
230         rBindings.Invalidate( SID_INSERT_TABLE, true );
231         break;
232     }
233     case SID_TABLEDESIGN:
234     {
235         // First make sure that the sidebar is visible
236         GetViewFrame()->ShowChildWindow(SID_SIDEBAR);
237         ::sfx2::sidebar::Sidebar::ShowPanel(
238             u"SdTableDesignPanel",
239             GetViewFrame()->GetFrame().GetFrameInterface());
240 
241         Cancel();
242         rReq.Done ();
243         break;
244     }
245     default:
246         break;
247     }
248 }
249 
250 void DrawViewShell::GetTableMenuState( SfxItemSet &rSet )
251 {
252     OUString aActiveLayer = mpDrawView->GetActiveLayer();
253     SdrPageView* pPV = mpDrawView->GetSdrPageView();
254 
255     if(
256         ( !aActiveLayer.isEmpty() && pPV && ( pPV->IsLayerLocked(aActiveLayer) ||
257         !pPV->IsLayerVisible(aActiveLayer) ) ) ||
258         SD_MOD()->GetWaterCan() )
259     {
260         rSet.DisableItem( SID_INSERT_TABLE );
261     }
262 }
263 
264 void CreateTableFromRTF( SvStream& rStream, SdDrawDocument* pModel )
265 {
266     rStream.Seek( 0 );
267 
268     if( !pModel )
269         return;
270 
271     SdrPage* pPage = pModel->GetPage(0);
272     if( !pPage )
273         return;
274 
275     Size aSize( 200, 200 );
276     ::tools::Rectangle aRect (Point(), aSize);
277     rtl::Reference<sdr::table::SdrTableObj> pObj = new sdr::table::SdrTableObj(
278         *pModel,
279         aRect,
280         1,
281         1);
282     pObj->NbcSetStyleSheet( pModel->GetDefaultStyleSheet(), true );
283     apply_table_style( pObj.get(), pModel, OUString() );
284 
285     pPage->NbcInsertObject( pObj.get() );
286 
287     sdr::table::ImportAsRTF( rStream, *pObj );
288 }
289 
290 }
291 
292 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
293