summaryrefslogtreecommitdiffstats
path: root/sd/source/ui/docshell/docshell.cxx
blob: 86fa9198dc052d08c299348c8fd44e688161e49c (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
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
/* -*- 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 <DrawDocShell.hxx>

#include <officecfg/Office/Common.hxx>
#include <unotools/configmgr.hxx>

#include <sfx2/docfac.hxx>
#include <sfx2/objface.hxx>
#include <sfx2/viewfrm.hxx>
#include <svx/svxids.hrc>
#include <svl/srchitem.hxx>
#include <svx/srchdlg.hxx>
#include <svx/svdoutl.hxx>
#include <svx/svditer.hxx>
#include <editeng/flstitem.hxx>
#include <editeng/eeitem.hxx>
#include <svl/eitem.hxx>
#include <svl/intitem.hxx>
#include <sfx2/printer.hxx>
#include <svx/drawitem.hxx>
#include <sfx2/dispatch.hxx>
#include <svl/whiter.hxx>
#include <svl/itempool.hxx>
#include <svl/stritem.hxx>
#include <svtools/ctrltool.hxx>
#include <svtools/langtab.hxx>
#include <comphelper/classids.hxx>
#include <svl/cjkoptions.hxx>
#include <svl/visitem.hxx>

#include <app.hrc>
#include <sdmod.hxx>
#include <View.hxx>
#include <drawdoc.hxx>

#include <fupoor.hxx>
#include <fusearch.hxx>
#include <ViewShell.hxx>
#include <unomodel.hxx>
#include <undo/undomanager.hxx>
#include <undo/undofactory.hxx>
#include <OutlineView.hxx>
#include <ViewShellBase.hxx>
#include <sfx2/notebookbar/SfxNotebookBar.hxx>
#include <comphelper/lok.hxx>

using namespace sd;
#define ShellClass_DrawDocShell
#include <sdslots.hxx>

SFX_IMPL_SUPERCLASS_INTERFACE(DrawDocShell, SfxObjectShell);

void DrawDocShell::InitInterface_Impl()
{
    GetStaticInterface()->RegisterChildWindow(SvxSearchDialogWrapper::GetChildWindowId());
}

namespace sd {

/**
 * slotmaps and definitions of SFX
 */

SFX_IMPL_OBJECTFACTORY(
    DrawDocShell,
    SvGlobalName(SO3_SIMPRESS_CLASSID),
    "simpress" )

void DrawDocShell::Construct( bool bClipboard )
{
    mbInDestruction = false;
    SetSlotFilter();     // resets the filter

    mbOwnDocument = mpDoc == nullptr;
    if( mbOwnDocument )
        mpDoc = new SdDrawDocument(meDocType, this);

    // The document has been created so we can call UpdateRefDevice() to set
    // the document's ref device.
    UpdateRefDevice();

    SetBaseModel( new SdXImpressDocument( this, bClipboard ) );
    SetPool( &mpDoc->GetItemPool() );
    std::unique_ptr<sd::UndoManager> pUndoManager(new sd::UndoManager);
    pUndoManager->SetDocShell(this);
    mpUndoManager = std::move(pUndoManager);

    if (!utl::ConfigManager::IsFuzzing()
        && officecfg::Office::Common::Undo::Steps::get() < 1)
    {
        mpUndoManager->EnableUndo(false); // tdf#108863 disable if 0 steps
    }
    mpDoc->SetSdrUndoManager( mpUndoManager.get() );
    mpDoc->SetSdrUndoFactory( new sd::UndoFactory );
    UpdateTablePointers();
    SetStyleFamily(SfxStyleFamily::Pseudo);
}

DrawDocShell::DrawDocShell(SfxObjectCreateMode eMode,
                               bool bDataObject,
                               DocumentType eDocumentType) :
    SfxObjectShell( eMode == SfxObjectCreateMode::INTERNAL ?  SfxObjectCreateMode::EMBEDDED : eMode),
    mpDoc(nullptr),
    mpPrinter(nullptr),
    mpViewShell(nullptr),
    meDocType(eDocumentType),
    mbSdDataObj(bDataObject),
    mbOwnPrinter(false)
{
    Construct( eMode == SfxObjectCreateMode::INTERNAL );
}

DrawDocShell::DrawDocShell( SfxModelFlags nModelCreationFlags, bool bDataObject, DocumentType eDocumentType ) :
    SfxObjectShell( nModelCreationFlags ),
    mpDoc(nullptr),
    mpPrinter(nullptr),
    mpViewShell(nullptr),
    meDocType(eDocumentType),
    mbSdDataObj(bDataObject),
    mbOwnPrinter(false)
{
    Construct( false );
}

DrawDocShell::DrawDocShell(SdDrawDocument* pDoc, SfxObjectCreateMode eMode,
                               bool bDataObject,
                               DocumentType eDocumentType) :
    SfxObjectShell(eMode == SfxObjectCreateMode::INTERNAL ?  SfxObjectCreateMode::EMBEDDED : eMode),
    mpDoc(pDoc),
    mpPrinter(nullptr),
    mpViewShell(nullptr),
    meDocType(eDocumentType),
    mbSdDataObj(bDataObject),
    mbOwnPrinter(false)
{
    Construct( eMode == SfxObjectCreateMode::INTERNAL );
}

DrawDocShell::~DrawDocShell()
{
    // Tell all listeners that the doc shell is about to be
    // destroyed.  This has been introduced for the PreviewRenderer to
    // free its view (that uses the item poll of the doc shell) but
    // may be useful in other places as well.
    Broadcast(SfxHint(SfxHintId::Dying));

    mbInDestruction = true;

    SetDocShellFunction(nullptr);

    mpFontList.reset();

    if( mpDoc )
        mpDoc->SetSdrUndoManager( nullptr );
    mpUndoManager.reset();

    if (mbOwnPrinter)
        mpPrinter.disposeAndClear();

    if( mbOwnDocument )
        delete mpDoc;

    // that the navigator get informed about the disappearance of the document
    SfxBoolItem     aItem(SID_NAVIGATOR_INIT, true);
    SfxViewFrame*   pFrame = mpViewShell ? mpViewShell->GetFrame() : GetFrame();

    if( !pFrame )
        pFrame = SfxViewFrame::GetFirst( this );

    if( pFrame )
    {
        pFrame->GetDispatcher()->ExecuteList(
            SID_NAVIGATOR_INIT, SfxCallMode::ASYNCHRON | SfxCallMode::RECORD,
            { &aItem });
    }
}

void DrawDocShell::GetState(SfxItemSet &rSet)
{

    SfxWhichIter aIter( rSet );
    sal_uInt16 nWhich = aIter.FirstWhich();

    while ( nWhich )
    {
        sal_uInt16 nSlotId = SfxItemPool::IsWhich(nWhich)
            ? GetPool().GetSlotId(nWhich)
            : nWhich;

        switch ( nSlotId )
        {
            case SID_ATTR_CHAR_FONTLIST:
                rSet.Put( SvxFontListItem( mpFontList.get(), nSlotId ) );
            break;

            case SID_SEARCH_ITEM:
            {
                rSet.Put( *SD_MOD()->GetSearchItem() );
            }
            break;

            case SID_CLOSEDOC:
                GetSlotState(SID_CLOSEDOC, SfxObjectShell::GetInterface(), &rSet);
            break;

            case SID_SEARCH_OPTIONS:
            {
                SearchOptionFlags nOpt = SearchOptionFlags::SEARCH |
                              SearchOptionFlags::WHOLE_WORDS |
                              SearchOptionFlags::BACKWARDS   |
                              SearchOptionFlags::REG_EXP     |
                              SearchOptionFlags::EXACT       |
                              SearchOptionFlags::SIMILARITY  |
                              SearchOptionFlags::SELECTION;

                if (!IsReadOnly())
                {
                    nOpt |= SearchOptionFlags::REPLACE;
                    nOpt |= SearchOptionFlags::REPLACE_ALL;
                }

                rSet.Put(SfxUInt16Item(nWhich, static_cast<sal_uInt16>(nOpt)));
            }
            break;

            case SID_VERSION:
            {
                GetSlotState( SID_VERSION, SfxObjectShell::GetInterface(), &rSet );
            }
            break;

            case SID_CHINESE_CONVERSION:
            case SID_HANGUL_HANJA_CONVERSION:
            {
                rSet.Put(SfxVisibilityItem(nWhich, SvtCJKOptions().IsAnyEnabled()));
            }
            break;
            case SID_LANGUAGE_STATUS:
            {
                SdrObject* pObj = nullptr;
                bool bLanguageFound = false;
                OutlinerParaObject* pParaObj = nullptr;
                LanguageType eLanguage( LANGUAGE_DONTKNOW );
                sal_uInt16 nCount = mpDoc->GetPageCount();
                for ( sal_uInt16 itPage = 0; itPage < nCount && !bLanguageFound; itPage++ )
                {
                    SdrObjListIter aListIter(mpDoc->GetPage(itPage), SdrIterMode::DeepWithGroups);
                    while ( aListIter.IsMore() && !bLanguageFound )
                    {
                        pObj = aListIter.Next();
                        if ( pObj )
                        {
                            pParaObj = pObj->GetOutlinerParaObject();
                            if ( pParaObj )
                            {
                                SdrOutliner aOutliner(&mpDoc->GetPool(), OutlinerMode::TextObject);
                                aOutliner.SetText(*pParaObj);
                                eLanguage = aOutliner.GetLanguage(0, 0);
                                bLanguageFound = eLanguage != LANGUAGE_DONTKNOW;
                            }
                        }
                    }
                }

                if ( eLanguage == LANGUAGE_DONTKNOW )
                {
                    eLanguage = mpDoc->GetLanguage( EE_CHAR_LANGUAGE );
                }

                OUString aLanguage = SvtLanguageTable::GetLanguageString(eLanguage);
                if (comphelper::LibreOfficeKit::isActive())
                {
                    if (eLanguage == LANGUAGE_DONTKNOW)
                    {
                        aLanguage += ";-";
                    }
                    else
                    {
                        aLanguage += ";" + LanguageTag(eLanguage).getBcp47(false);
                    }
                }
                rSet.Put(SfxStringItem(nWhich, aLanguage));
            }
            break;

            case SID_NOTEBOOKBAR:
            {
                if (mpViewShell)
                {
                    bool bImpress = mpDoc->GetDocumentType() == DocumentType::Impress;
                    bool bVisible = false;
                    if(bImpress)
                    {
                        bVisible = sfx2::SfxNotebookBar::StateMethod(mpViewShell->GetFrame()->GetBindings(),
                                                                      "modules/simpress/ui/");
                    }
                    else
                    {
                        bVisible = sfx2::SfxNotebookBar::StateMethod(mpViewShell->GetFrame()->GetBindings(),
                                                                      "modules/sdraw/ui/");
                    }
                    rSet.Put( SfxBoolItem( SID_NOTEBOOKBAR, bVisible ) );
                }
            }
            break;

            default:
            break;
        }
        nWhich = aIter.NextWhich();
    }

    SfxViewFrame* pFrame = SfxViewFrame::Current();

    if (pFrame)
    {
        if (rSet.GetItemState(SID_RELOAD) != SfxItemState::UNKNOWN)
        {
            pFrame->GetSlotState(SID_RELOAD,
                                 pFrame->GetInterface(), &rSet);
        }
    }
}

void DrawDocShell::Activate( bool bMDI)
{
    if (bMDI)
    {
        ApplySlotFilter();
        mpDoc->StartOnlineSpelling();
    }
}

void DrawDocShell::Deactivate( bool )
{
}

SfxUndoManager* DrawDocShell::GetUndoManager()
{
    return mpUndoManager.get();
}

void DrawDocShell::UpdateTablePointers()
{
    PutItem( SvxColorListItem( mpDoc->GetColorList(), SID_COLOR_TABLE ) );
    PutItem( SvxGradientListItem( mpDoc->GetGradientList(), SID_GRADIENT_LIST ) );
    PutItem( SvxHatchListItem( mpDoc->GetHatchList(), SID_HATCH_LIST ) );
    PutItem( SvxBitmapListItem( mpDoc->GetBitmapList(), SID_BITMAP_LIST ) );
    PutItem( SvxPatternListItem( mpDoc->GetPatternList(), SID_PATTERN_LIST ) );
    PutItem( SvxDashListItem( mpDoc->GetDashList(), SID_DASH_LIST ) );
    PutItem( SvxLineEndListItem( mpDoc->GetLineEndList(), SID_LINEEND_LIST ) );

    UpdateFontList();
}

void DrawDocShell::CancelSearching()
{
    if( dynamic_cast<FuSearch*>( mxDocShellFunction.get() ) )
    {
        SetDocShellFunction(nullptr);
    }
}

/**
 * apply configured slot filters
 */
void DrawDocShell::ApplySlotFilter() const
{
    SfxViewShell* pTestViewShell = SfxViewShell::GetFirst();

    while( pTestViewShell )
    {
        if( pTestViewShell->GetObjectShell()
            == this
            && pTestViewShell->GetViewFrame()
            && pTestViewShell->GetViewFrame()->GetDispatcher() )
        {
            SfxDispatcher* pDispatcher = pTestViewShell->GetViewFrame()->GetDispatcher();

            if( !mpFilterSIDs.empty() )
                pDispatcher->SetSlotFilter( mbFilterEnable ? SfxSlotFilterState::ENABLED : SfxSlotFilterState::DISABLED, mpFilterSIDs );
            else
                pDispatcher->SetSlotFilter();

            if( pDispatcher->GetBindings() )
                pDispatcher->GetBindings()->InvalidateAll( true );
        }

        pTestViewShell = SfxViewShell::GetNext( *pTestViewShell );
    }
}

void DrawDocShell::SetModified( bool bSet /* = true */ )
{
    SfxObjectShell::SetModified( bSet );

    // change model state, too
    // only set the changed state if modification is enabled
    if( IsEnableSetModified() )
    {
        if ( mpDoc )
            mpDoc->NbcSetChanged( bSet );

        Broadcast( SfxHint( SfxHintId::DocChanged ) );
    }
}

/**
 * Callback for ExecuteSpellPopup()
 */
// ExecuteSpellPopup now handled by DrawDocShell. This is necessary
// to get hands on the outliner and the text object.
IMPL_LINK(DrawDocShell, OnlineSpellCallback, SpellCallbackInfo&, rInfo, void)
{
    SdrObject* pObj = nullptr;
    SdrOutliner* pOutl = nullptr;

    if(GetViewShell())
    {
        pOutl = GetViewShell()->GetView()->GetTextEditOutliner();
        pObj = GetViewShell()->GetView()->GetTextEditObject();
    }

    mpDoc->ImpOnlineSpellCallback(&rInfo, pObj, pOutl);
}

void DrawDocShell::ClearUndoBuffer()
{
    // clear possible undo buffers of outliners
    SfxViewFrame* pSfxViewFrame = SfxViewFrame::GetFirst(this, false);
    while(pSfxViewFrame)
    {
        ViewShellBase* pViewShellBase = dynamic_cast< ViewShellBase* >( pSfxViewFrame->GetViewShell() );
        if( pViewShellBase )
        {
            std::shared_ptr<ViewShell> pViewSh( pViewShellBase->GetMainViewShell() );
            if( pViewSh )
            {
                ::sd::View* pView = pViewSh->GetView();
                if( pView )
                {
                    pView->SdrEndTextEdit();
                    sd::OutlineView* pOutlView = dynamic_cast< sd::OutlineView* >( pView );
                    if( pOutlView )
                    {
                        pOutlView->GetOutliner().GetUndoManager().Clear();
                    }
                }
            }
        }
        pSfxViewFrame = SfxViewFrame::GetNext(*pSfxViewFrame, this, false);
    }

    SfxUndoManager* pUndoManager = GetUndoManager();
    if(pUndoManager && pUndoManager->GetUndoActionCount())
        pUndoManager->Clear();
}

} // end of namespace sd

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