summaryrefslogtreecommitdiffstats
path: root/sd/source/ui/docshell/docshel2.cxx
blob: 160c64a662eb2176a02d277b5a61a2d455d122c6 (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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
/* -*- 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 <memory>
#include <DrawDocShell.hxx>
#include <svx/svdpagv.hxx>
#include <svx/svxdlg.hxx>
#include <o3tl/string_view.hxx>

#include <helpids.h>
#include <ViewShell.hxx>
#include <FrameView.hxx>
#include <drawdoc.hxx>
#include <sdpage.hxx>
#include <ClientView.hxx>
#include <Window.hxx>
#include <strings.hrc>

#include <sdresid.hxx>
#include <fupoor.hxx>
#include <vcl/svapp.hxx>
#include <vcl/virdev.hxx>
#include <rtl/character.hxx>
#include <tools/debug.hxx>

namespace sd {

/**
 * Drawing of DocShell (with the helper class SdDrawViewShell)
 */
void DrawDocShell::Draw(OutputDevice* pOut, const JobSetup&, sal_uInt16 nAspect)
{
    if (nAspect == ASPECT_THUMBNAIL)
    {
      // THUMBNAIL: here we may can set the draft mode
    }

    std::optional<ClientView> pView( std::in_place, this, pOut );

    pView->SetHlplVisible(false);
    pView->SetGridVisible(false);
    pView->SetBordVisible(false);
    pView->SetPageVisible(false);
    pView->SetGlueVisible(false);

    SdPage* pSelectedPage = nullptr;

    const std::vector<std::unique_ptr<sd::FrameView>> &rViews = mpDoc->GetFrameViewList();
    if( !rViews.empty() )
    {
        sd::FrameView* pFrameView = rViews[0].get();
        if( pFrameView->GetPageKind() == PageKind::Standard )
        {
            sal_uInt16 nSelectedPage = pFrameView->GetSelectedPage();
            pSelectedPage = mpDoc->GetSdPage(nSelectedPage, PageKind::Standard);
        }
    }

    if( nullptr == pSelectedPage )
    {
        SdPage* pPage = nullptr;
        sal_uInt16 nPageCnt = mpDoc->GetSdPageCount(PageKind::Standard);

        for (sal_uInt16 i = 0; i < nPageCnt; i++)
        {
            pPage = mpDoc->GetSdPage(i, PageKind::Standard);

            if ( pPage->IsSelected() )
                pSelectedPage = pPage;
        }

        if( nullptr == pSelectedPage )
            pSelectedPage = mpDoc->GetSdPage(0, PageKind::Standard);
    }

    ::tools::Rectangle aVisArea = GetVisArea(nAspect);
    pOut->IntersectClipRegion(aVisArea);
    pView->ShowSdrPage(pSelectedPage);

    if (pOut->GetOutDevType() == OUTDEV_WINDOW)
        return;

    MapMode aOldMapMode = pOut->GetMapMode();

    if (pOut->GetOutDevType() == OUTDEV_PRINTER)
    {
        MapMode aMapMode = aOldMapMode;
        Point aOrigin = aMapMode.GetOrigin();
        aOrigin.AdjustX(1 );
        aOrigin.AdjustY(1 );
        aMapMode.SetOrigin(aOrigin);
        pOut->SetMapMode(aMapMode);
    }

    vcl::Region aRegion(aVisArea);
    pView->CompleteRedraw(pOut, aRegion);

    if (pOut->GetOutDevType() == OUTDEV_PRINTER)
    {
        pOut->SetMapMode(aOldMapMode);
    }
}

::tools::Rectangle DrawDocShell::GetVisArea(sal_uInt16 nAspect) const
{
    ::tools::Rectangle aVisArea;

    if( ( ASPECT_THUMBNAIL == nAspect ) || ( ASPECT_DOCPRINT == nAspect ) )
    {
        // provide size of first page
        aVisArea.SetSize(mpDoc->GetSdPage(0, PageKind::Standard)->GetSize());
    }
    else
    {
        aVisArea = SfxObjectShell::GetVisArea(nAspect);
    }

    if (aVisArea.IsEmpty() && mpViewShell)
    {
        vcl::Window* pWin = mpViewShell->GetActiveWindow();

        if (pWin)
        {
            aVisArea = pWin->PixelToLogic(::tools::Rectangle(Point(0,0), pWin->GetOutputSizePixel()));
        }
    }

    return aVisArea;
}

void DrawDocShell::Connect(ViewShell* pViewSh)
{
    mpViewShell = pViewSh;
}

void DrawDocShell::Disconnect(ViewShell const * pViewSh)
{
    if (mpViewShell == pViewSh)
    {
        mpViewShell = nullptr;
    }
}

FrameView* DrawDocShell::GetFrameView()
{
    FrameView* pFrameView = nullptr;

    if (mpViewShell)
    {
        pFrameView = mpViewShell->GetFrameView();
    }

    return pFrameView;
}

/**
 * Creates a bitmap of an arbitrary page
 */
BitmapEx DrawDocShell::GetPagePreviewBitmap(SdPage* pPage)
{
    const sal_uInt16 nMaxEdgePixel = 90;
    MapMode         aMapMode( MapUnit::Map100thMM );
    const Size      aSize( pPage->GetSize() );
    const Point     aNullPt;
    ScopedVclPtrInstance< VirtualDevice > pVDev( *Application::GetDefaultDevice() );

    pVDev->SetMapMode( aMapMode );

    const Size  aPixSize( pVDev->LogicToPixel( aSize ) );
    const sal_uLong nMaxEdgePix = std::max( aPixSize.Width(), aPixSize.Height() );
    Fraction    aFrac( nMaxEdgePixel, nMaxEdgePix );

    aMapMode.SetScaleX( aFrac );
    aMapMode.SetScaleY( aFrac );
    pVDev->SetMapMode( aMapMode );
    pVDev->SetOutputSize( aSize );

    // that we also get the dark lines at the right and bottom page margin
    aFrac = Fraction( nMaxEdgePixel - 1, nMaxEdgePix );
    aMapMode.SetScaleX( aFrac );
    aMapMode.SetScaleY( aFrac );
    pVDev->SetMapMode( aMapMode );

    std::optional<ClientView> pView( std::in_place, this, pVDev );
    FrameView*      pFrameView = GetFrameView();
    pView->ShowSdrPage( pPage );

    if ( GetFrameView() )
    {
        // initialize the drawing-(screen) attributes
        pView->SetGridCoarse( pFrameView->GetGridCoarse() );
        pView->SetGridFine( pFrameView->GetGridFine() );
        pView->SetSnapGridWidth(pFrameView->GetSnapGridWidthX(), pFrameView->GetSnapGridWidthY());
        pView->SetGridVisible( pFrameView->IsGridVisible() );
        pView->SetGridFront( pFrameView->IsGridFront() );
        pView->SetSnapAngle( pFrameView->GetSnapAngle() );
        pView->SetGridSnap( pFrameView->IsGridSnap() );
        pView->SetBordSnap( pFrameView->IsBordSnap() );
        pView->SetHlplSnap( pFrameView->IsHlplSnap() );
        pView->SetOFrmSnap( pFrameView->IsOFrmSnap() );
        pView->SetOPntSnap( pFrameView->IsOPntSnap() );
        pView->SetOConSnap( pFrameView->IsOConSnap() );
        pView->SetDragStripes( pFrameView->IsDragStripes() );
        pView->SetFrameDragSingles( pFrameView->IsFrameDragSingles() );
        pView->SetSnapMagneticPixel( pFrameView->GetSnapMagneticPixel() );
        pView->SetMarkedHitMovesAlways( pFrameView->IsMarkedHitMovesAlways() );
        pView->SetMoveOnlyDragging( pFrameView->IsMoveOnlyDragging() );
        pView->SetSlantButShear( pFrameView->IsSlantButShear() );
        pView->SetNoDragXorPolys( pFrameView->IsNoDragXorPolys() );
        pView->SetCrookNoContortion( pFrameView->IsCrookNoContortion() );
        pView->SetAngleSnapEnabled( pFrameView->IsAngleSnapEnabled() );
        pView->SetBigOrtho( pFrameView->IsBigOrtho() );
        pView->SetOrtho( pFrameView->IsOrtho() );

        SdrPageView* pPageView = pView->GetSdrPageView();

        if (pPageView)
        {
            if ( pPageView->GetVisibleLayers() != pFrameView->GetVisibleLayers() )
                pPageView->SetVisibleLayers( pFrameView->GetVisibleLayers() );

            if ( pPageView->GetPrintableLayers() != pFrameView->GetPrintableLayers() )
                pPageView->SetPrintableLayers( pFrameView->GetPrintableLayers() );

            if ( pPageView->GetLockedLayers() != pFrameView->GetLockedLayers() )
                pPageView->SetLockedLayers( pFrameView->GetLockedLayers() );

            pPageView->SetHelpLines( pFrameView->GetStandardHelpLines() );
        }

        if ( pView->GetActiveLayer() != pFrameView->GetActiveLayer() )
            pView->SetActiveLayer( pFrameView->GetActiveLayer() );
    }

    pView->CompleteRedraw( pVDev, vcl::Region(::tools::Rectangle(aNullPt, aSize)) );

    // IsRedrawReady() always gives sal_True while ( !pView->IsRedrawReady() ) {}
    pView.reset();

    pVDev->SetMapMode( MapMode() );

    BitmapEx aPreview( pVDev->GetBitmapEx( aNullPt, pVDev->GetOutputSizePixel() ) );

    DBG_ASSERT(!aPreview.IsEmpty(), "Preview-Bitmap could not be generated");

    return aPreview;
}

/**
 * Checks if the page exists. If so, we force the user to enter a not yet used
 * name.
 * @return sal_False if the user cancels the action.
 */
bool DrawDocShell::CheckPageName(weld::Window* pWin, OUString& rName)
{
    const OUString aStrForDlg( rName );
    bool bIsNameValid = IsNewPageNameValid( rName, true );

    if( ! bIsNameValid )
    {
        OUString aDesc;
        SvxAbstractDialogFactory* pFact = SvxAbstractDialogFactory::Create();

        if (GetDocumentType() == DocumentType::Draw)
            aDesc = SdResId( STR_WARN_PAGE_EXISTS_DRAW );
        else
            aDesc = SdResId( STR_WARN_PAGE_EXISTS );

        ScopedVclPtr<AbstractSvxNameDialog> aNameDlg(pFact->CreateSvxNameDialog(pWin, aStrForDlg, aDesc));
        aNameDlg->SetEditHelpId( HID_SD_NAMEDIALOG_PAGE );

        aNameDlg->SetCheckNameHdl( LINK( this, DrawDocShell, RenameSlideHdl ) );

        rtl::Reference<FuPoor> xFunc( mpViewShell->GetCurrentFunction() );
        if( xFunc.is() )
            xFunc->cancel();

        if( aNameDlg->Execute() == RET_OK )
        {
            aNameDlg->GetName( rName );
            bIsNameValid = IsNewPageNameValid( rName );
        }
    }

    return bIsNameValid;
}

bool DrawDocShell::IsNewPageNameValid( OUString & rInOutPageName, bool bResetStringIfStandardName /* = false */ )
{
    bool bCanUseNewName = false;

    // check if name is something like 'Slide n'
    OUString aStrPage(SdResId(STR_SD_PAGE) + " ");

    bool bIsStandardName = false;

    // prevent also _future_ slide names of the form "'STR_SD_PAGE' + ' ' + '[0-9]+|[a-z]|[A-Z]|[CDILMVX]+|[cdilmvx]+'"
    // (arabic, lower- and upper case single letter, lower- and upper case roman numbers)
    if (rInOutPageName.startsWith(aStrPage) &&
        rInOutPageName.getLength() > aStrPage.getLength())
    {
        sal_Int32 nIdx{ aStrPage.getLength() };
        std::u16string_view sRemainder = o3tl::getToken(rInOutPageName, 0, ' ', nIdx);
        if (!sRemainder.empty() && sRemainder[0] >= '0' && sRemainder[0] <= '9')
        {
            // check for arabic numbering

            size_t nIndex = 1;
            // skip all following numbers
            while (nIndex < sRemainder.size() &&
                   sRemainder[nIndex] >= '0' && sRemainder[nIndex] <= '9')
            {
                nIndex++;
            }

            // EOL? Reserved name!
            if (nIndex >= sRemainder.size())
            {
                bIsStandardName = true;
            }
        }
        else if (sRemainder.size() == 1 &&
                 rtl::isAsciiLowerCase(sRemainder[0]))
        {
            // lower case, single character: reserved
            bIsStandardName = true;
        }
        else if (sRemainder.size() == 1 &&
                 rtl::isAsciiUpperCase(sRemainder[0]))
        {
            // upper case, single character: reserved
            bIsStandardName = true;
        }
        else
        {
            // check for upper/lower case roman numbering
            OUString sReserved("cdilmvx");

            // skip all following characters contained in one reserved class
            if (sReserved.indexOf(sRemainder[0]) == -1)
                sReserved = sReserved.toAsciiUpperCase();

            size_t nIndex = 0;
            while (nIndex < sRemainder.size() &&
                   sReserved.indexOf(sRemainder[nIndex]) != -1)
            {
                nIndex++;
            }

            // EOL? Reserved name!
            if (nIndex >= sRemainder.size())
            {
                bIsStandardName = true;
            }
        }
    }

    if( bIsStandardName )
    {
        if( bResetStringIfStandardName )
        {
            // this is for insertion of slides from other files with standard
            // name.  They get a new standard name, if the string is set to an
            // empty one.
            rInOutPageName.clear();
            bCanUseNewName = true;
        }
        else
            bCanUseNewName = false;
    }
    else
    {
        if (!rInOutPageName.isEmpty())
        {
            bool   bOutDummy;
            sal_uInt16 nExistingPageNum = mpDoc->GetPageByName( rInOutPageName, bOutDummy );
            bCanUseNewName = ( nExistingPageNum == SDRPAGE_NOTFOUND );
        }
        else
            bCanUseNewName = false;
    }

    return bCanUseNewName;
}

bool DrawDocShell::IsPageNameUnique( std::u16string_view rPageName ) const
{
    return mpDoc->IsPageNameUnique(rPageName);
}

IMPL_LINK( DrawDocShell, RenameSlideHdl, AbstractSvxNameDialog&, rDialog, bool )
{
    OUString aNewName;
    rDialog.GetName( aNewName );
    return IsNewPageNameValid( aNewName );
}

} // end of namespace sd

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