bump product version to 6.4.0.3
[LibreOffice.git] / sd / source / ui / table / tablefunction.cxx
blob6fb9be6973a3deaa8bd1cfae56291e99a4120d74
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>
39 #include <tablefunction.hxx>
40 #include <DrawViewShell.hxx>
41 #include <drawdoc.hxx>
42 #include <sdpage.hxx>
43 #include <Window.hxx>
44 #include <drawview.hxx>
45 #include <sdmod.hxx>
47 #include <memory>
49 using namespace ::sd;
50 using namespace sdr::table;
51 using namespace ::com::sun::star;
52 using namespace ::com::sun::star::uno;
53 using namespace ::com::sun::star::beans;
54 using namespace ::com::sun::star::util;
55 using namespace ::com::sun::star::container;
56 using namespace ::com::sun::star::lang;
57 using namespace ::com::sun::star::drawing;
58 using namespace ::com::sun::star::linguistic2;
60 namespace sd
63 static void apply_table_style( SdrTableObj* pObj, SdrModel const * pModel, const OUString& sTableStyle )
65 if( !(pModel && pObj) )
66 return;
68 Reference< XNameAccess > xPool( dynamic_cast< XNameAccess* >( pModel->GetStyleSheetPool() ) );
69 if( !xPool.is() )
70 return;
72 try
74 Reference< XNameContainer > xTableFamily( xPool->getByName( "table" ), UNO_QUERY_THROW );
75 OUString aStdName( "default" );
76 if( !sTableStyle.isEmpty() )
77 aStdName = sTableStyle;
78 Reference< XIndexAccess > xStyle( xTableFamily->getByName( aStdName ), UNO_QUERY_THROW );
79 pObj->setTableStyle( xStyle );
81 catch( Exception& )
83 OSL_FAIL("sd::apply_default_table_style(), exception caught!");
87 void DrawViewShell::FuTable(SfxRequest& rReq)
89 switch( rReq.GetSlot() )
91 case SID_INSERT_TABLE:
93 sal_Int32 nColumns = 0;
94 sal_Int32 nRows = 0;
95 OUString sTableStyle;
97 const SfxUInt16Item* pCols = rReq.GetArg<SfxUInt16Item>(SID_ATTR_TABLE_COLUMN);
98 const SfxUInt16Item* pRows = rReq.GetArg<SfxUInt16Item>(SID_ATTR_TABLE_ROW);
99 const SfxStringItem* pStyle = rReq.GetArg<SfxStringItem>(SID_TABLE_STYLE);
101 if( pCols )
102 nColumns = pCols->GetValue();
104 if( pRows )
105 nRows = pRows->GetValue();
107 if( pStyle )
108 sTableStyle = pStyle->GetValue();
110 if( (nColumns == 0) || (nRows == 0) )
112 SvxAbstractDialogFactory* pFact = SvxAbstractDialogFactory::Create();
113 ScopedVclPtr<SvxAbstractNewTableDialog> pDlg( pFact->CreateSvxNewTableDialog(rReq.GetFrameWeld()) );
115 if( pDlg->Execute() != RET_OK )
116 break;
118 nColumns = pDlg->getColumns();
119 nRows = pDlg->getRows();
122 ::tools::Rectangle aRect;
124 SdrObject* pPickObj = mpView->GetEmptyPresentationObject( PRESOBJ_TABLE );
125 if( pPickObj )
127 aRect = pPickObj->GetLogicRect();
128 aRect.setHeight( 200 );
130 else
132 Size aSize( 14100, 2000 );
134 Point aPos;
135 ::tools::Rectangle aWinRect(aPos, GetActiveWindow()->GetOutputSizePixel());
136 aWinRect = GetActiveWindow()->PixelToLogic(aWinRect);
138 // make sure that the default size of the table fits on the paper and is inside the viewing area.
139 // if zoomed in close, don't make the table bigger than the viewing window.
140 Size aMaxSize = getCurrentPage()->GetSize();
142 if (comphelper::LibreOfficeKit::isActive())
144 // aWinRect is nonsensical in the LOK case
145 aWinRect = ::tools::Rectangle(aPos, aMaxSize);
147 else
149 if( aMaxSize.Height() > aWinRect.getHeight() )
150 aMaxSize.setHeight( aWinRect.getHeight() );
151 if( aMaxSize.Width() > aWinRect.getWidth() )
152 aMaxSize.setWidth( aWinRect.getWidth() );
155 if( aSize.Width() > aMaxSize.getWidth() )
156 aSize.setWidth( aMaxSize.getWidth() );
158 // adjust height based on # of rows.
159 if( nRows > 0 )
161 aSize.setHeight( aSize.Height() * nRows );
162 if( aSize.Height() > aMaxSize.getHeight() )
163 aSize.setHeight( aMaxSize.getHeight() );
166 aPos = aWinRect.Center();
167 aPos.AdjustX( -(aSize.Width() / 2) );
168 aPos.AdjustY( -(aSize.Height() / 2) );
169 aRect = ::tools::Rectangle(aPos, aSize);
172 sdr::table::SdrTableObj* pObj = new sdr::table::SdrTableObj(
173 *GetDoc(), // TTTT should be reference
174 aRect,
175 nColumns,
176 nRows);
177 pObj->NbcSetStyleSheet( GetDoc()->GetDefaultStyleSheet(), true );
178 apply_table_style( pObj, GetDoc(), sTableStyle );
179 SdrPageView* pPV = mpView->GetSdrPageView();
181 // #i123359# if an object is to be replaced/manipulated it may be that it is in text edit mode,
182 // so to be on the safe side call SdrEndTextEdit here
183 SdrTextObj* pCheckForTextEdit = dynamic_cast< SdrTextObj* >(pPickObj);
185 if(pCheckForTextEdit && pCheckForTextEdit->IsInEditMode())
187 mpView->SdrEndTextEdit();
190 // if we have a pick obj we need to make this new ole a pres obj replacing the current pick obj
191 if( pPickObj )
193 SdPage* pPage = static_cast< SdPage* >(pPickObj->getSdrPageFromSdrObject());
194 if(pPage && pPage->IsPresObj(pPickObj))
196 pObj->SetUserCall( pPickObj->GetUserCall() );
197 pPage->InsertPresObj( pObj, PRESOBJ_TABLE );
201 GetParentWindow()->GrabFocus();
202 if( pPickObj )
203 mpView->ReplaceObjectAtView(pPickObj, *pPV, pObj );
204 else
205 mpView->InsertObjectAtView(pObj, *pPV, SdrInsertFlags::SETDEFLAYER);
207 rReq.Ignore();
208 SfxViewShell* pViewShell = GetViewShell();
209 OSL_ASSERT (pViewShell!=nullptr);
210 SfxBindings& rBindings = pViewShell->GetViewFrame()->GetBindings();
211 rBindings.Invalidate( SID_INSERT_TABLE, true );
212 break;
214 case SID_TABLEDESIGN:
216 // First make sure that the sidebar is visible
217 GetViewFrame()->ShowChildWindow(SID_SIDEBAR);
218 ::sfx2::sidebar::Sidebar::ShowPanel(
219 "SdTableDesignPanel",
220 GetViewFrame()->GetFrame().GetFrameInterface());
222 Cancel();
223 rReq.Done ();
224 break;
226 default:
227 break;
231 void DrawViewShell::GetTableMenuState( SfxItemSet &rSet )
233 OUString aActiveLayer = mpDrawView->GetActiveLayer();
234 SdrPageView* pPV = mpDrawView->GetSdrPageView();
237 ( !aActiveLayer.isEmpty() && pPV && ( pPV->IsLayerLocked(aActiveLayer) ||
238 !pPV->IsLayerVisible(aActiveLayer) ) ) ||
239 SD_MOD()->GetWaterCan() )
241 rSet.DisableItem( SID_INSERT_TABLE );
245 void CreateTableFromRTF( SvStream& rStream, SdDrawDocument* pModel )
247 rStream.Seek( 0 );
249 if( !pModel )
250 return;
252 SdrPage* pPage = pModel->GetPage(0);
253 if( !pPage )
254 return;
256 Size aSize( 200, 200 );
257 ::tools::Rectangle aRect (Point(), aSize);
258 sdr::table::SdrTableObj* pObj = new sdr::table::SdrTableObj(
259 *pModel,
260 aRect,
263 pObj->NbcSetStyleSheet( pModel->GetDefaultStyleSheet(), true );
264 apply_table_style( pObj, pModel, OUString() );
266 pPage->NbcInsertObject( pObj );
268 sdr::table::SdrTableObj::ImportAsRTF( rStream, *pObj );
273 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */