LibreOffice Module sd (master) 1
tablefunction.cxx
Go to the documentation of this file.
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>
37#include <svl/style.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
50using namespace ::sd;
51using namespace sdr::table;
52using namespace ::com::sun::star;
53using namespace ::com::sun::star::uno;
54using namespace ::com::sun::star::beans;
55using namespace ::com::sun::star::util;
56using namespace ::com::sun::star::container;
57using namespace ::com::sun::star::lang;
58using namespace ::com::sun::star::drawing;
59using namespace ::com::sun::star::linguistic2;
60
61namespace sd
62{
63
64static void apply_table_style( SdrTableObj* pObj, SdrModel const * pModel, const OUString& sTableStyle )
65{
66 if( !(pModel && pObj) )
67 return;
68
69 Reference< XNameAccess > xPool(
70 static_cast< cppu::OWeakObject* >( pModel->GetStyleSheetPool() ), css::uno::UNO_QUERY );
71 if( !xPool.is() )
72 return;
73
74 try
75 {
76 Reference< XNameContainer > xTableFamily( xPool->getByName( "table" ), UNO_QUERY_THROW );
77 OUString aStdName( "default" );
78 if( !sTableStyle.isEmpty() )
79 aStdName = sTableStyle;
80 Reference< XIndexAccess > xStyle( xTableFamily->getByName( aStdName ), UNO_QUERY_THROW );
81 pObj->setTableStyle( xStyle );
82 }
83 catch( Exception& )
84 {
85 TOOLS_WARN_EXCEPTION( "sd", "sd::apply_default_table_style()");
86 }
87}
88
89static void InsertTableImpl(const DrawViewShell* pShell,
90 ::sd::View* pView,
91 sal_Int32 nColumns,
92 sal_Int32 nRows,
93 const OUString& sTableStyle)
94{
96
98 if( pPickObj )
99 {
100 aRect = pPickObj->GetLogicRect();
101 aRect.setHeight( 200 );
102 }
103 else
104 {
105 Size aSize(14100, 200);
106
107 Point aPos;
108 ::tools::Rectangle aWinRect(aPos, pShell->GetActiveWindow()->GetOutputSizePixel());
109 aWinRect = pShell->GetActiveWindow()->PixelToLogic(aWinRect);
110
111 // make sure that the default size of the table fits on the paper and is inside the viewing area.
112 // if zoomed in close, don't make the table bigger than the viewing window.
113 Size aMaxSize = pShell->getCurrentPage()->GetSize();
114
116 {
117 // aWinRect is nonsensical in the LOK case
118 aWinRect = ::tools::Rectangle(aPos, aMaxSize);
119 }
120 else
121 {
122 if( aMaxSize.Height() > aWinRect.getOpenHeight() )
123 aMaxSize.setHeight( aWinRect.getOpenHeight() );
124 if( aMaxSize.Width() > aWinRect.getOpenWidth() )
125 aMaxSize.setWidth( aWinRect.getOpenWidth() );
126 }
127
128 if( aSize.Width() > aMaxSize.getWidth() )
129 aSize.setWidth( aMaxSize.getWidth() );
130
131 // adjust height based on # of rows.
132 if( nRows > 0 )
133 {
134 aSize.setHeight( aSize.Height() * nRows );
135 if( aSize.Height() > aMaxSize.getHeight() )
136 aSize.setHeight( aMaxSize.getHeight() );
137 }
138
139 aPos = aWinRect.Center();
140 aPos.AdjustX( -(aSize.Width() / 2) );
141 aPos.AdjustY( -(aSize.Height() / 2) );
142 aRect = ::tools::Rectangle(aPos, aSize);
143 }
144
146 *pShell->GetDoc(), // TTTT should be reference
147 aRect,
148 nColumns,
149 nRows);
150 pObj->NbcSetStyleSheet( pShell->GetDoc()->GetDefaultStyleSheet(), true );
151 apply_table_style( pObj.get(), pShell->GetDoc(), sTableStyle );
152 SdrPageView* pPV = pView->GetSdrPageView();
153
154 // #i123359# if an object is to be replaced/manipulated it may be that it is in text edit mode,
155 // so to be on the safe side call SdrEndTextEdit here
156 SdrTextObj* pCheckForTextEdit = DynCastSdrTextObj(pPickObj);
157
158 if(pCheckForTextEdit && pCheckForTextEdit->IsInEditMode())
159 {
160 pView->SdrEndTextEdit();
161 }
162
163 // if we have a pick obj we need to make this new ole a pres obj replacing the current pick obj
164 if( pPickObj )
165 {
166 SdPage* pPage = static_cast< SdPage* >(pPickObj->getSdrPageFromSdrObject());
167 if(pPage && pPage->IsPresObj(pPickObj))
168 {
169 pObj->SetUserCall( pPickObj->GetUserCall() );
170 pPage->InsertPresObj( pObj.get(), PresObjKind::Table );
171 }
172 }
173
174 pShell->GetParentWindow()->GrabFocus();
175 if( pPickObj )
176 pView->ReplaceObjectAtView(pPickObj, *pPV, pObj.get() );
177 else
178 pView->InsertObjectAtView(pObj.get(), *pPV, SdrInsertFlags::SETDEFLAYER);
179}
180
182{
183 switch( rReq.GetSlot() )
184 {
185 case SID_INSERT_TABLE:
186 {
187 sal_Int32 nColumns = 0;
188 sal_Int32 nRows = 0;
189 OUString sTableStyle;
190 DrawViewShell* pShell = this;
191 ::sd::View* pView = mpView;
192
193 const SfxUInt16Item* pCols = rReq.GetArg<SfxUInt16Item>(SID_ATTR_TABLE_COLUMN);
194 const SfxUInt16Item* pRows = rReq.GetArg<SfxUInt16Item>(SID_ATTR_TABLE_ROW);
195 const SfxStringItem* pStyle = rReq.GetArg<SfxStringItem>(SID_TABLE_STYLE);
196
197 if( pCols )
198 nColumns = pCols->GetValue();
199
200 if( pRows )
201 nRows = pRows->GetValue();
202
203 if( pStyle )
204 sTableStyle = pStyle->GetValue();
205
206 if( (nColumns == 0) || (nRows == 0) )
207 {
209 std::shared_ptr<SvxAbstractNewTableDialog> pDlg( pFact->CreateSvxNewTableDialog(rReq.GetFrameWeld()) );
210
211 weld::DialogController::runAsync(pDlg->getDialogController(),
212 [pDlg, pShell, pView, sTableStyle] (sal_Int32 nResult) {
213 if (nResult == RET_OK)
214 {
215 sal_Int32 nColumnsIn = pDlg->getColumns();
216 sal_Int32 nRowsIn = pDlg->getRows();
217
218 InsertTableImpl(pShell, pView, nColumnsIn, nRowsIn, sTableStyle);
219 }
220 });
221 }
222 else
223 {
224 InsertTableImpl(pShell, pView, nColumns, nRows, sTableStyle);
225 }
226
227 rReq.Ignore();
228 SfxViewShell* pViewShell = GetViewShell();
229 OSL_ASSERT (pViewShell!=nullptr);
230 SfxBindings& rBindings = pViewShell->GetViewFrame().GetBindings();
231 rBindings.Invalidate( SID_INSERT_TABLE, true );
232 break;
233 }
234 case SID_TABLEDESIGN:
235 {
236 // First make sure that the sidebar is visible
237 GetViewFrame()->ShowChildWindow(SID_SIDEBAR);
239 u"SdTableDesignPanel",
240 GetViewFrame()->GetFrame().GetFrameInterface());
241
242 Cancel();
243 rReq.Done ();
244 break;
245 }
246 default:
247 break;
248 }
249}
250
251void DrawViewShell::GetTableMenuState( SfxItemSet &rSet )
252{
253 OUString aActiveLayer = mpDrawView->GetActiveLayer();
254 SdrPageView* pPV = mpDrawView->GetSdrPageView();
255
256 if(
257 ( !aActiveLayer.isEmpty() && pPV && ( pPV->IsLayerLocked(aActiveLayer) ||
258 !pPV->IsLayerVisible(aActiveLayer) ) ) ||
259 SD_MOD()->GetWaterCan() )
260 {
261 rSet.DisableItem( SID_INSERT_TABLE );
262 }
263}
264
266{
267 rStream.Seek( 0 );
268
269 if( !pModel )
270 return;
271
272 SdrPage* pPage = pModel->GetPage(0);
273 if( !pPage )
274 return;
275
276 Size aSize( 200, 200 );
277 ::tools::Rectangle aRect (Point(), aSize);
279 *pModel,
280 aRect,
281 1,
282 1);
283 pObj->NbcSetStyleSheet( pModel->GetDefaultStyleSheet(), true );
284 apply_table_style( pObj.get(), pModel, OUString() );
285
286 pPage->NbcInsertObject( pObj.get() );
287
288 sdr::table::ImportAsRTF( rStream, *pObj );
289}
290
291}
292
293/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
sal_uInt16 GetValue() const
tools::Long AdjustY(tools::Long nVertMove)
tools::Long AdjustX(tools::Long nHorzMove)
bool IsPresObj(const SdrObject *pObj)
Definition: sdpage.cxx:2322
void InsertPresObj(SdrObject *pObj, PresObjKind eKind)
inserts the given SdrObject into the presentation object list
Definition: sdpage.cxx:2338
void ReplaceObjectAtView(SdrObject *pOldObj, SdrPageView &rPV, SdrObject *pNewObj, bool bMark=true)
bool InsertObjectAtView(SdrObject *pObj, SdrPageView &rPV, SdrInsertFlags nOptions=SdrInsertFlags::NONE)
SfxStyleSheet * GetDefaultStyleSheet() const
SfxStyleSheetBasePool * GetStyleSheetPool() const
const SdrPage * GetPage(sal_uInt16 nPgNum) const
virtual void NbcInsertObject(SdrObject *pObj, size_t nPos=SAL_MAX_SIZE)
SdrObjUserCall * GetUserCall() const
SdrPage * getSdrPageFromSdrObject() const
virtual const tools::Rectangle & GetLogicRect() const
bool IsLayerVisible(const OUString &rName) const
bool IsLayerLocked(const OUString &rName) const
Size GetSize() const
SdrPageView * GetSdrPageView() const
bool IsInEditMode() const
void Invalidate(sal_uInt16 nId)
void DisableItem(sal_uInt16 nWhich)
sal_uInt16 GetSlot() const
void Ignore()
const T * GetArg(sal_uInt16 nSlotId) const
weld::Window * GetFrameWeld() const
void Done(bool bRemove=false)
SfxViewFrame * GetFrame() const
SfxViewShell * GetViewShell() const
SfxBindings & GetBindings()
void ShowChildWindow(sal_uInt16, bool bVisible=true)
SfxViewFrame & GetViewFrame() const
constexpr tools::Long getHeight() const
constexpr tools::Long Height() const
constexpr tools::Long getWidth() const
void setWidth(tools::Long nWidth)
void setHeight(tools::Long nHeight)
constexpr tools::Long Width() const
sal_uInt64 Seek(sal_uInt64 nPos)
virtual std::shared_ptr< SvxAbstractNewTableDialog > CreateSvxNewTableDialog(weld::Window *pParent)=0
static SvxAbstractDialogFactory * Create()
Base class of the stacked shells that provide graphical views to Draw and Impress documents and editi...
virtual SdPage * getCurrentPage() const override
inherited from sd::ViewShell
Definition: drviews1.cxx:636
void FuTable(SfxRequest &rReq)
SdDrawDocument * GetDoc() const
Definition: viewshel.cxx:1412
::sd::Window * GetActiveWindow() const
The active window is usually the mpContentWindow.
Definition: ViewShell.hxx:155
vcl::Window * GetParentWindow() const
Return the window that is the parent of all controls of this view shell.
Definition: ViewShell.hxx:140
::sd::View * mpView
Definition: ViewShell.hxx:452
void Cancel()
this method deactivates the current function.
Definition: viewshel.cxx:1442
SD_DLLPUBLIC SfxViewFrame * GetViewFrame() const
Definition: viewshel.cxx:118
virtual SdrEndTextEditKind SdrEndTextEdit(bool bDontDeleteReally=false) override
ends current text editing
Definition: sdview.cxx:772
SdrObject * GetEmptyPresentationObject(PresObjKind eKind)
Definition: sdview5.cxx:79
void setTableStyle(const css::uno::Reference< css::container::XIndexAccess > &xAutoFormatStyle)
static void TogglePanel(std::u16string_view rsPanelId, const css::uno::Reference< css::frame::XFrame > &rxFrame)
constexpr Point Center() const
tools::Long getOpenHeight() const
void setHeight(tools::Long n)
tools::Long getOpenWidth() const
void GrabFocus()
Point PixelToLogic(const Point &rDevicePt) const
Size GetOutputSizePixel() const
static bool runAsync(const std::shared_ptr< DialogController > &rController, const std::function< void(sal_Int32)> &)
#define TOOLS_WARN_EXCEPTION(area, stream)
float u
@ Exception
static void InsertTableImpl(const DrawViewShell *pShell, ::sd::View *pView, sal_Int32 nColumns, sal_Int32 nRows, const OUString &sTableStyle)
static void apply_table_style(SdrTableObj *pObj, SdrModel const *pModel, const OUString &sTableStyle)
void CreateTableFromRTF(SvStream &rStream, SdDrawDocument *pModel)
void ImportAsRTF(SvStream &rStream, SdrTableObj &rObj)
#define SD_MOD()
Definition: sdmod.hxx:184
static SfxItemSet & rSet
SVXCORE_DLLPUBLIC SdrTextObj * DynCastSdrTextObj(SdrObject *)