summaryrefslogtreecommitdiff
path: root/sd/source/ui/table/tablefunction.cxx
blob: 818cde2f7c57d44f7e4e9ba2243444b1efdc5190 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * This file is part of the LibreOffice project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 * This file incorporates work covered by the following license notice:
 *
 *   Licensed to the Apache Software Foundation (ASF) under one or more
 *   contributor license agreements. See the NOTICE file distributed
 *   with this work for additional information regarding copyright
 *   ownership. The ASF licenses this file to you under the Apache
 *   License, Version 2.0 (the "License"); you may not use this file
 *   except in compliance with the License. You may obtain a copy of
 *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
 */

#include <sal/config.h>
#include <com/sun/star/uno/XComponentContext.hpp>
#include <com/sun/star/drawing/XSelectionFunction.hpp>
#include <com/sun/star/awt/KeyModifier.hpp>
#include <com/sun/star/lang/XInitialization.hpp>

#include <comphelper/lok.hxx>
#include <cppuhelper/basemutex.hxx>

#include <vcl/svapp.hxx>

#include <svx/svdotable.hxx>
#include <svx/sdr/overlay/overlayobjectcell.hxx>
#include <svx/sdr/overlay/overlaymanager.hxx>
#include <svx/svxids.hrc>
#include <editeng/outlobj.hxx>
#include <svx/svdoutl.hxx>
#include <svx/svdpagv.hxx>
#include <svx/svdetc.hxx>
#include <editeng/editstat.hxx>
#include <editeng/unolingu.hxx>
#include <svx/sdrpagewindow.hxx>
#include <svx/sdr/table/tabledesign.hxx>
#include <svx/svxdlg.hxx>

#include <svl/itempool.hxx>
#include <sfx2/viewfrm.hxx>
#include <sfx2/dispatch.hxx>
#include <sfx2/bindings.hxx>
#include <sfx2/request.hxx>
#include <sfx2/sidebar/Sidebar.hxx>
#include <svl/style.hxx>

#include <framework/FrameworkHelper.hxx>
#include "TableDesignPane.hxx"
#include <app.hrc>
#include <tablefunction.hxx>
#include <DrawViewShell.hxx>
#include <drawdoc.hxx>
#include <DrawDocShell.hxx>
#include <Window.hxx>
#include <drawview.hxx>
#include <undo/undoobjects.hxx>

#include <memory>

using namespace ::sd;
using namespace sdr::table;
using namespace ::com::sun::star;
using namespace ::com::sun::star::uno;
using namespace ::com::sun::star::beans;
using namespace ::com::sun::star::util;
using namespace ::com::sun::star::container;
using namespace ::com::sun::star::lang;
using namespace ::com::sun::star::drawing;
using namespace ::com::sun::star::linguistic2;

namespace sd
{

static void apply_table_style( SdrTableObj* pObj, SdrModel const * pModel, const OUString& sTableStyle )
{
    if( pModel && pObj )
    {
        Reference< XNameAccess > xPool( dynamic_cast< XNameAccess* >( pModel->GetStyleSheetPool() ) );
        if( xPool.is() ) try
        {
            Reference< XNameContainer > xTableFamily( xPool->getByName( "table" ), UNO_QUERY_THROW );
            OUString aStdName( "default" );
            if( !sTableStyle.isEmpty() )
                aStdName = sTableStyle;
            Reference< XIndexAccess > xStyle( xTableFamily->getByName( aStdName ), UNO_QUERY_THROW );
            pObj->setTableStyle( xStyle );
        }
        catch( Exception& )
        {
            OSL_FAIL("sd::apply_default_table_style(), exception caught!");
        }
    }
}

void DrawViewShell::FuTable(SfxRequest& rReq)
{
    switch( rReq.GetSlot() )
    {
    case SID_INSERT_TABLE:
    {
        sal_Int32 nColumns = 0;
        sal_Int32 nRows = 0;
        OUString sTableStyle;

        const SfxUInt16Item* pCols = rReq.GetArg<SfxUInt16Item>(SID_ATTR_TABLE_COLUMN);
        const SfxUInt16Item* pRows = rReq.GetArg<SfxUInt16Item>(SID_ATTR_TABLE_ROW);
        const SfxStringItem* pStyle = rReq.GetArg<SfxStringItem>(SID_TABLE_STYLE);

        if( pCols )
            nColumns = pCols->GetValue();

        if( pRows )
            nRows = pRows->GetValue();

        if( pStyle )
            sTableStyle = pStyle->GetValue();

        if( (nColumns == 0) || (nRows == 0) )
        {
            SvxAbstractDialogFactory* pFact = SvxAbstractDialogFactory::Create();
            ScopedVclPtr<SvxAbstractNewTableDialog> pDlg( pFact ? pFact->CreateSvxNewTableDialog(rReq.GetFrameWeld()) : nullptr);

            if( !pDlg.get() || (pDlg->Execute() != RET_OK) )
                break;

            nColumns = pDlg->getColumns();
            nRows = pDlg->getRows();
        }

        ::tools::Rectangle aRect;

        SdrObject* pPickObj = mpView->GetEmptyPresentationObject( PRESOBJ_TABLE );
        if( pPickObj )
        {
            aRect = pPickObj->GetLogicRect();
            aRect.setHeight( 200 );
        }
        else
        {
            Size aSize( 14100, 2000 );

            Point aPos;
            ::tools::Rectangle aWinRect(aPos, GetActiveWindow()->GetOutputSizePixel());
            aWinRect = GetActiveWindow()->PixelToLogic(aWinRect);

            // make sure that the default size of the table fits on the paper and is inside the viewing area.
            // if zoomed in close, don't make the table bigger than the viewing window.
            Size aMaxSize = getCurrentPage()->GetSize();

            if (comphelper::LibreOfficeKit::isActive())
            {
                // aWinRect is nonsensical in the LOK case
                aWinRect = ::tools::Rectangle(aPos, aMaxSize);
            }
            else
            {
                if( aMaxSize.Height() > aWinRect.getHeight() )
                    aMaxSize.setHeight( aWinRect.getHeight() );
                if( aMaxSize.Width() > aWinRect.getWidth() )
                    aMaxSize.setWidth( aWinRect.getWidth() );
            }

            if( aSize.Width() > aMaxSize.getWidth() )
                aSize.setWidth( aMaxSize.getWidth() );

            // adjust height based on # of rows.
            if( nRows > 0 )
            {
                aSize.setHeight( aSize.Height() * nRows );
                if( aSize.Height() > aMaxSize.getHeight() )
                    aSize.setHeight( aMaxSize.getHeight() );
            }

            aPos = aWinRect.Center();
            aPos.AdjustX( -(aSize.Width() / 2) );
            aPos.AdjustY( -(aSize.Height() / 2) );
            aRect = ::tools::Rectangle(aPos, aSize);
        }

        sdr::table::SdrTableObj* pObj = new sdr::table::SdrTableObj(
            *GetDoc(), // TTTT should be reference
            aRect,
            nColumns,
            nRows);
        pObj->NbcSetStyleSheet( GetDoc()->GetDefaultStyleSheet(), true );
        apply_table_style( pObj, GetDoc(), sTableStyle );
        SdrPageView* pPV = mpView->GetSdrPageView();

        // #i123359# if an object is to be replaced/manipulated it may be that it is in text edit mode,
        // so to be on the safe side call SdrEndTextEdit here
        SdrTextObj* pCheckForTextEdit = dynamic_cast< SdrTextObj* >(pPickObj);

        if(pCheckForTextEdit && pCheckForTextEdit->IsInEditMode())
        {
            mpView->SdrEndTextEdit();
        }

        // if we have a pick obj we need to make this new ole a pres obj replacing the current pick obj
        if( pPickObj )
        {
            SdPage* pPage = static_cast< SdPage* >(pPickObj->GetPage());
            if(pPage && pPage->IsPresObj(pPickObj))
            {
                pObj->SetUserCall( pPickObj->GetUserCall() );
                pPage->InsertPresObj( pObj, PRESOBJ_TABLE );
            }
        }

        GetParentWindow()->GrabFocus();
        if( pPickObj )
            mpView->ReplaceObjectAtView(pPickObj, *pPV, pObj );
        else
            mpView->InsertObjectAtView(pObj, *pPV, SdrInsertFlags::SETDEFLAYER);

        rReq.Ignore();
        SfxViewShell* pViewShell = GetViewShell();
        OSL_ASSERT (pViewShell!=nullptr);
        SfxBindings& rBindings = pViewShell->GetViewFrame()->GetBindings();
        rBindings.Invalidate( SID_INSERT_TABLE, true );
        break;
    }
    case SID_TABLEDESIGN:
    {
        // First make sure that the sidebar is visible
        GetViewFrame()->ShowChildWindow(SID_SIDEBAR);
        ::sfx2::sidebar::Sidebar::ShowPanel(
            "SdTableDesignPanel",
            GetViewFrame()->GetFrame().GetFrameInterface());

        Cancel();
        rReq.Done ();
        break;
    }
    default:
        break;
    }
}

void DrawViewShell::GetTableMenuState( SfxItemSet &rSet )
{
    OUString aActiveLayer = mpDrawView->GetActiveLayer();
    SdrPageView* pPV = mpDrawView->GetSdrPageView();

    if(
        ( !aActiveLayer.isEmpty() && pPV && ( pPV->IsLayerLocked(aActiveLayer) ||
        !pPV->IsLayerVisible(aActiveLayer) ) ) ||
        SD_MOD()->GetWaterCan() )
    {
        rSet.DisableItem( SID_INSERT_TABLE );
    }
}

void CreateTableFromRTF( SvStream& rStream, SdDrawDocument* pModel )
{
    rStream.Seek( 0 );

    if( pModel )
    {
        SdrPage* pPage = pModel->GetPage(0);
        if( pPage )
        {
            Size aSize( 200, 200 );
            ::tools::Rectangle aRect (Point(), aSize);
            sdr::table::SdrTableObj* pObj = new sdr::table::SdrTableObj(
                *pModel,
                aRect,
                1,
                1);
            pObj->NbcSetStyleSheet( pModel->GetDefaultStyleSheet(), true );
            apply_table_style( pObj, pModel, OUString() );

            pPage->NbcInsertObject( pObj );

            sdr::table::SdrTableObj::ImportAsRTF( rStream, *pObj );
        }
    }
}

}

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */