tdf#154285 Check upper bound of arguments in SbRtl_Minute function
[LibreOffice.git] / sd / source / ui / table / tablefunction.cxx
blob943744da15dc9010e76b5d568aee86ab053bb302
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <sal/config.h>
21 #include <com/sun/star/uno/XComponentContext.hpp>
22 #include <com/sun/star/drawing/XSelectionFunction.hpp>
24 #include <comphelper/lok.hxx>
26 #include <svx/svdotable.hxx>
27 #include <svx/svxids.hrc>
28 #include <svx/svdpagv.hxx>
29 #include <svx/svxdlg.hxx>
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>
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>
48 #include <memory>
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::container;
56 namespace sd
59 static void apply_table_style( SdrTableObj* pObj, SdrModel const * pModel, const OUString& sTableStyle )
61 if( !(pModel && pObj) )
62 return;
64 Reference< XNameAccess > xPool(
65 static_cast< cppu::OWeakObject* >( pModel->GetStyleSheetPool() ), css::uno::UNO_QUERY );
66 if( !xPool.is() )
67 return;
69 try
71 Reference< XNameContainer > xTableFamily( xPool->getByName( u"table"_ustr ), UNO_QUERY_THROW );
72 OUString aStdName( u"default"_ustr );
73 if( !sTableStyle.isEmpty() )
74 aStdName = sTableStyle;
75 Reference< XIndexAccess > xStyle( xTableFamily->getByName( aStdName ), UNO_QUERY_THROW );
76 pObj->setTableStyle( xStyle );
78 catch( Exception& )
80 TOOLS_WARN_EXCEPTION( "sd", "sd::apply_default_table_style()");
84 static void InsertTableImpl(const DrawViewShell* pShell,
85 ::sd::View* pView,
86 sal_Int32 nColumns,
87 sal_Int32 nRows,
88 const OUString& sTableStyle)
90 ::tools::Rectangle aRect;
92 SdrObject* pPickObj = pView->GetEmptyPresentationObject( PresObjKind::Table );
93 if( pPickObj )
95 aRect = pPickObj->GetLogicRect();
96 aRect.setHeight( 200 );
98 else
100 Size aSize(14100, 200);
102 Point aPos;
103 ::tools::Rectangle aWinRect(aPos, pShell->GetActiveWindow()->GetOutputSizePixel());
104 aWinRect = pShell->GetActiveWindow()->PixelToLogic(aWinRect);
106 // make sure that the default size of the table fits on the paper and is inside the viewing area.
107 // if zoomed in close, don't make the table bigger than the viewing window.
108 Size aMaxSize = pShell->getCurrentPage()->GetSize();
110 if (comphelper::LibreOfficeKit::isActive())
112 // aWinRect is nonsensical in the LOK case
113 aWinRect = ::tools::Rectangle(aPos, aMaxSize);
115 else
117 if( aMaxSize.Height() > aWinRect.getOpenHeight() )
118 aMaxSize.setHeight( aWinRect.getOpenHeight() );
119 if( aMaxSize.Width() > aWinRect.getOpenWidth() )
120 aMaxSize.setWidth( aWinRect.getOpenWidth() );
123 if( aSize.Width() > aMaxSize.getWidth() )
124 aSize.setWidth( aMaxSize.getWidth() );
126 // adjust height based on # of rows.
127 if( nRows > 0 )
129 aSize.setHeight( aSize.Height() * nRows );
130 if( aSize.Height() > aMaxSize.getHeight() )
131 aSize.setHeight( aMaxSize.getHeight() );
134 aPos = aWinRect.Center();
135 aPos.AdjustX( -(aSize.Width() / 2) );
136 aPos.AdjustY( -(aSize.Height() / 2) );
137 aRect = ::tools::Rectangle(aPos, aSize);
140 rtl::Reference<sdr::table::SdrTableObj> pObj = new sdr::table::SdrTableObj(
141 *pShell->GetDoc(), // TTTT should be reference
142 aRect,
143 nColumns,
144 nRows);
145 pObj->NbcSetStyleSheet( pShell->GetDoc()->GetDefaultStyleSheet(), true );
146 apply_table_style( pObj.get(), pShell->GetDoc(), sTableStyle );
147 SdrPageView* pPV = pView->GetSdrPageView();
149 // #i123359# if an object is to be replaced/manipulated it may be that it is in text edit mode,
150 // so to be on the safe side call SdrEndTextEdit here
151 SdrTextObj* pCheckForTextEdit = DynCastSdrTextObj(pPickObj);
153 if(pCheckForTextEdit && pCheckForTextEdit->IsInEditMode())
155 pView->SdrEndTextEdit();
158 // if we have a pick obj we need to make this new ole a pres obj replacing the current pick obj
159 if( pPickObj )
161 SdPage* pPage = static_cast< SdPage* >(pPickObj->getSdrPageFromSdrObject());
162 if(pPage && pPage->IsPresObj(pPickObj))
164 pObj->SetUserCall( pPickObj->GetUserCall() );
165 pPage->InsertPresObj( pObj.get(), PresObjKind::Table );
169 pShell->GetParentWindow()->GrabFocus();
170 if( pPickObj )
171 pView->ReplaceObjectAtView(pPickObj, *pPV, pObj.get() );
172 else
173 pView->InsertObjectAtView(pObj.get(), *pPV, SdrInsertFlags::SETDEFLAYER);
176 void DrawViewShell::FuTable(SfxRequest& rReq)
178 switch( rReq.GetSlot() )
180 case SID_INSERT_TABLE:
182 sal_Int32 nColumns = 0;
183 sal_Int32 nRows = 0;
184 OUString sTableStyle;
185 DrawViewShell* pShell = this;
186 ::sd::View* pView = mpView;
188 const SfxUInt16Item* pCols = rReq.GetArg<SfxUInt16Item>(SID_ATTR_TABLE_COLUMN);
189 const SfxUInt16Item* pRows = rReq.GetArg<SfxUInt16Item>(SID_ATTR_TABLE_ROW);
190 const SfxStringItem* pStyle = rReq.GetArg<SfxStringItem>(SID_TABLE_STYLE);
192 if( pCols )
193 nColumns = pCols->GetValue();
195 if( pRows )
196 nRows = pRows->GetValue();
198 if( pStyle )
199 sTableStyle = pStyle->GetValue();
201 if( (nColumns == 0) || (nRows == 0) )
203 SvxAbstractDialogFactory* pFact = SvxAbstractDialogFactory::Create();
204 std::shared_ptr<SvxAbstractNewTableDialog> pDlg( pFact->CreateSvxNewTableDialog(rReq.GetFrameWeld()) );
206 weld::DialogController::runAsync(pDlg->getDialogController(),
207 [pDlg, pShell, pView, sTableStyle] (sal_Int32 nResult) {
208 if (nResult == RET_OK)
210 sal_Int32 nColumnsIn = pDlg->getColumns();
211 sal_Int32 nRowsIn = pDlg->getRows();
213 InsertTableImpl(pShell, pView, nColumnsIn, nRowsIn, sTableStyle);
217 else
219 InsertTableImpl(pShell, pView, nColumns, nRows, sTableStyle);
222 rReq.Ignore();
223 SfxViewShell* pViewShell = GetViewShell();
224 assert(pViewShell!=nullptr);
225 SfxBindings& rBindings = pViewShell->GetViewFrame().GetBindings();
226 rBindings.Invalidate( SID_INSERT_TABLE, true );
227 break;
229 case SID_TABLEDESIGN:
231 // First make sure that the sidebar is visible
232 if (SfxViewFrame* pViewFrame = GetViewFrame())
234 pViewFrame->ShowChildWindow(SID_SIDEBAR);
235 ::sfx2::sidebar::Sidebar::TogglePanel(
236 u"SdTableDesignPanel",
237 pViewFrame->GetFrame().GetFrameInterface());
239 Cancel();
240 rReq.Done ();
242 break;
244 default:
245 break;
249 void DrawViewShell::GetTableMenuState( SfxItemSet &rSet )
251 OUString aActiveLayer = mpDrawView->GetActiveLayer();
252 SdrPageView* pPV = mpDrawView->GetSdrPageView();
255 ( !aActiveLayer.isEmpty() && pPV && ( pPV->IsLayerLocked(aActiveLayer) ||
256 !pPV->IsLayerVisible(aActiveLayer) ) ) ||
257 SdModule::get()->GetWaterCan() )
259 rSet.DisableItem( SID_INSERT_TABLE );
263 void CreateTableFromRTF( SvStream& rStream, SdDrawDocument* pModel )
265 rStream.Seek( 0 );
267 if( !pModel )
268 return;
270 SdrPage* pPage = pModel->GetPage(0);
271 if( !pPage )
272 return;
274 Size aSize( 200, 200 );
275 ::tools::Rectangle aRect (Point(), aSize);
276 rtl::Reference<sdr::table::SdrTableObj> pObj = new sdr::table::SdrTableObj(
277 *pModel,
278 aRect,
281 pObj->NbcSetStyleSheet( pModel->GetDefaultStyleSheet(), true );
282 apply_table_style( pObj.get(), pModel, OUString() );
284 pPage->NbcInsertObject( pObj.get() );
286 sdr::table::ImportAsRTF( rStream, *pObj );
289 void CreateTableFromHTML(SvStream& rStream, SdDrawDocument* pModel)
291 rStream.Seek( 0 );
293 if( !pModel )
294 return;
296 SdrPage* pPage = pModel->GetPage(0);
297 if( !pPage )
298 return;
300 Size aSize( 200, 200 );
301 ::tools::Rectangle aRect (Point(), aSize);
302 rtl::Reference<sdr::table::SdrTableObj> pObj = new sdr::table::SdrTableObj(
303 *pModel,
304 aRect,
307 pObj->NbcSetStyleSheet( pModel->GetDefaultStyleSheet(), true );
308 apply_table_style( pObj.get(), pModel, OUString() );
310 pPage->NbcInsertObject( pObj.get() );
312 sdr::table::ImportAsHTML( rStream, *pObj );
317 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */