Coverage Report

Created: 2026-04-09 11:41

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libreoffice/sc/source/ui/view/tabvwshf.cxx
Line
Count
Source
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 <config_features.h>
21
22
#include <memory>
23
24
#include <sfx2/dialogrequesthelper.hxx>
25
#include <sfx2/request.hxx>
26
#include <sfx2/bindings.hxx>
27
#include <sfx2/viewfrm.hxx>
28
#include <basic/sbstar.hxx>
29
#include <basic/sberrors.hxx>
30
#include <svl/ctloptions.hxx>
31
#include <svl/stritem.hxx>
32
#include <svl/whiter.hxx>
33
#include <vcl/svapp.hxx>
34
#include <vcl/weld/MessageDialog.hxx>
35
#include <vcl/weld/weld.hxx>
36
#include <sfx2/objface.hxx>
37
#include <svx/svxdlg.hxx>
38
#include <editeng/colritem.hxx>
39
40
#include <tabvwsh.hxx>
41
#include <sc.hrc>
42
#include <helpids.h>
43
#include <docsh.hxx>
44
#include <document.hxx>
45
#include <scresid.hxx>
46
#include <globstr.hrc>
47
#include <strings.hrc>
48
#include <docfunc.hxx>
49
#include <eventuno.hxx>
50
#include <dpobject.hxx>
51
#include <dpshttab.hxx>
52
53
#include <scabstdlg.hxx>
54
55
#include <tabbgcolor.hxx>
56
#include <markdata.hxx>
57
58
#include <vector>
59
60
using namespace com::sun::star;
61
62
void ScTabViewShell::ExecuteTable( SfxRequest& rReq )
63
0
{
64
0
    ScViewData& rViewData   = GetViewData();
65
0
    ScDocument& rDoc        = rViewData.GetDocument();
66
67
0
    SCTAB       nCurrentTab = rViewData.CurrentTabForData();
68
0
    SCTAB       nTabCount   = rDoc.GetTableCount();
69
0
    sal_uInt16  nSlot       = rReq.GetSlot();
70
0
    const SfxItemSet* pReqArgs = rReq.GetArgs();
71
72
0
    HideListBox();                  // Autofilter-DropDown-Listbox
73
74
0
    switch ( nSlot )
75
0
    {
76
0
        case FID_TABLE_VISIBLE:
77
0
            {
78
0
                OUString aName;
79
0
                rDoc.GetName( nCurrentTab, aName );
80
81
0
                bool bVisible=true;
82
0
                if( pReqArgs != nullptr )
83
0
                {
84
0
                    const SfxPoolItem* pItem;
85
0
                    if( pReqArgs->HasItem( FID_TABLE_VISIBLE, &pItem ) )
86
0
                        bVisible = static_cast<const SfxBoolItem*>(pItem)->GetValue();
87
0
                }
88
89
0
                if( ! bVisible )            // fade out
90
0
                {
91
0
                    if ( rDoc.IsDocEditable() )
92
0
                    {
93
0
                        ScMarkData& rMark = rViewData.GetMarkData();
94
0
                        HideTable( rMark );
95
0
                    }
96
0
                }
97
0
                else                        // fade in
98
0
                {
99
0
                    std::vector<OUString> rNames { aName };
100
0
                    ShowTable( rNames );
101
0
                }
102
0
            }
103
0
            break;
104
105
0
        case FID_TABLE_HIDE:
106
0
            {
107
0
                if ( rDoc.IsDocEditable() )
108
0
                {
109
0
                    ScMarkData& rMark = rViewData.GetMarkData();
110
0
                    SCTAB nActiveTab = -1;
111
                    // For the cases when user right clicks on a non-active tab and hides it. This case is possible for Online.
112
0
                    if (pReqArgs)
113
0
                    {
114
0
                        const SfxPoolItem *pItem;
115
0
                        if( pReqArgs->HasItem( FID_TABLE_HIDE, &pItem ) )
116
0
                        {
117
0
                            SCTAB nTabNumber = static_cast<const SfxInt16Item*>(pItem)->GetValue();
118
                            // Does selected sheets (tabs) list include the sheet to be hidden?
119
0
                            std::set<SCTAB>::iterator it = rMark.GetSelectedTabs().find(nTabNumber);
120
0
                            if (it == rMark.GetSelectedTabs().end())
121
0
                            {
122
                                // No it doesn't, so we won't shift the selected tab. Let's remember its position.
123
0
                                nActiveTab = GetViewData().CurrentTabForData();
124
0
                            }
125
0
                            rMark.SelectOneTable(nTabNumber);
126
0
                        }
127
0
                    }
128
0
                    HideTable( rMark, nActiveTab );
129
0
                }
130
0
            }
131
0
            break;
132
133
0
        case FID_TABLE_SHOW:
134
0
            {
135
0
                std::vector<OUString> rNames;
136
0
                if ( pReqArgs )
137
0
                {
138
0
                    const SfxPoolItem* pItem;
139
0
                    if( pReqArgs->HasItem( FID_TABLE_SHOW, &pItem ) )
140
0
                    {
141
0
                        OUString aName = static_cast<const SfxStringItem*>(pItem)->GetValue();
142
0
                        rNames.push_back(aName);
143
0
                        ShowTable( rNames );
144
145
0
                        if( ! rReq.IsAPI() )
146
0
                            rReq.Done();
147
0
                    }
148
0
                }
149
0
                else
150
0
                {
151
0
                    ScAbstractDialogFactory* pFact = ScAbstractDialogFactory::Create();
152
153
0
                    VclPtr<AbstractScShowTabDlg> pDlg(pFact->CreateScShowTabDlg(GetFrameWeld()));
154
155
0
                    OUString aTabName;
156
0
                    bool bFirst = true;
157
0
                    for ( SCTAB i=0; i != nTabCount; i++ )
158
0
                    {
159
0
                        if (!rDoc.IsVisible(i))
160
0
                        {
161
0
                            rDoc.GetName( i, aTabName );
162
0
                            pDlg->Insert( aTabName, bFirst );
163
0
                            bFirst = false;
164
0
                        }
165
0
                    }
166
167
0
                    std::shared_ptr<SfxRequest> xReq = std::make_shared<SfxRequest>(rReq);
168
0
                    pDlg->StartExecuteAsync([this, pDlg, xReq = std::move(xReq)](sal_Int32 nResult){
169
0
                        std::vector<OUString> sTables;
170
0
                        if (RET_OK == nResult)
171
0
                        {
172
0
                            std::vector<sal_Int32> aSelectedRows = pDlg->GetSelectedRows();
173
0
                            for (auto a : aSelectedRows)
174
0
                            {
175
0
                                OUString sTable = pDlg->GetEntry(a);
176
0
                                xReq->AppendItem( SfxStringItem( FID_TABLE_SHOW, sTable ) );
177
0
                                sTables.push_back(sTable);
178
0
                            }
179
0
                            ShowTable( sTables );
180
0
                            xReq->Done();
181
0
                        }
182
0
                        pDlg->disposeOnce();
183
0
                    });
184
0
                    rReq.Ignore();
185
0
                }
186
0
            }
187
0
            break;
188
189
0
        case FID_INS_TABLE:
190
0
        case FID_INS_TABLE_EXT:
191
0
            ExecuteInsertTable(rReq);
192
0
            break;
193
194
0
        case FID_TAB_APPEND:
195
0
        case FID_TAB_RENAME:
196
0
        case FID_TAB_MENU_RENAME:
197
0
            {
198
                //  FID_TAB_MENU_RENAME - "rename" in menu
199
                //  FID_TAB_RENAME      - "name"-property for basic
200
                //  equal execute, but MENU_RENAME may be disabled inside GetState
201
202
0
                ExecuteAppendOrRenameTable(rReq);
203
0
            }
204
0
            break;
205
206
0
        case FID_TAB_MOVE:
207
0
            {
208
0
                ExecuteMoveTable(rReq);
209
0
            }
210
0
            break;
211
212
0
        case FID_TAB_DUPLICATE:
213
0
            {
214
                // Get info about current document and selected tab
215
0
                SCTAB nTab = rViewData.CurrentTabForData();
216
0
                OUString aDocName = GetViewData().GetDocShell()->GetTitle(SFX_TITLE_FULLNAME);
217
0
                sal_uInt16 nDoc = 0;
218
219
0
                SfxObjectShell* pSh = SfxObjectShell::GetFirst();
220
0
                ScDocShell* pScSh = nullptr;
221
0
                sal_uInt16 i = 0;
222
223
                // Determine the index of the current document
224
0
                while ( pSh )
225
0
                {
226
0
                    pScSh = dynamic_cast<ScDocShell*>( pSh );
227
228
0
                    if( pScSh )
229
0
                    {
230
0
                        if (aDocName == pScSh->GetTitle(SFX_TITLE_FULLNAME))
231
0
                        {
232
0
                            nDoc = i;
233
0
                            break;
234
0
                        }
235
                        // Only count ScDocShell
236
0
                        i++;
237
0
                    }
238
0
                    pSh = SfxObjectShell::GetNext( *pSh );
239
0
                }
240
241
0
                MoveTable(nDoc, nTab + 1, true);
242
0
            }
243
0
            break;
244
245
0
        case FID_DELETE_TABLE:
246
0
            {
247
0
                bool bHasIndex = (pReqArgs != nullptr);
248
249
                // allow removing via the Index/FID_DELETE_TABLE parameter
250
0
                SCTAB nTabNr = nCurrentTab;
251
0
                if (bHasIndex)
252
0
                {
253
0
                    const SfxPoolItem* pItem;
254
0
                    if (pReqArgs->HasItem(FID_DELETE_TABLE, &pItem))
255
0
                    {
256
0
                        nTabNr = static_cast<const SfxUInt16Item*>(pItem)->GetValue();
257
258
                        // inserting is 1-based, let's be consistent
259
0
                        if (nTabNr > 0)
260
0
                            --nTabNr;
261
0
                    }
262
0
                }
263
264
0
                bool bDoIt = bHasIndex;
265
0
                if (!bDoIt)
266
0
                {
267
0
                    bool bTabWithPivotTable = false;
268
0
                    if (rDoc.HasPivotTable())
269
0
                    {
270
0
                        const ScDPCollection* pDPs = rDoc.GetDPCollection();
271
0
                        if (pDPs)
272
0
                        {
273
0
                            const ScMarkData::MarkedTabsType& rSelectedTabs = rViewData.GetMarkData().GetSelectedTabs();
274
0
                            const size_t nCount = pDPs->GetCount();
275
0
                            for (size_t i = 0; i < nCount; ++i)
276
0
                            {
277
0
                                const ScDPObject& rDPObj = (*pDPs)[i];
278
0
                                const ScSheetSourceDesc* pSheetSourceDesc = rDPObj.GetSheetDesc();
279
0
                                if (pSheetSourceDesc)
280
0
                                {
281
0
                                    SCTAB nTabOut = rDPObj.GetOutRange().aStart.Tab();
282
0
                                    SCTAB nTabSource = pSheetSourceDesc->GetSourceRange().aStart.Tab();
283
0
                                    bool bTabOutSel = false;
284
0
                                    for (const SCTAB nSelTab : rSelectedTabs)
285
0
                                    {
286
0
                                        if (nSelTab == nTabSource)
287
0
                                            bTabWithPivotTable = true;
288
0
                                        if (nSelTab == nTabOut)
289
0
                                            bTabOutSel = true;
290
0
                                        if (bTabWithPivotTable && bTabOutSel)
291
0
                                            break;
292
0
                                    }
293
                                    // if both pivot table and data are selected
294
                                    // no need to warn for source data losing
295
0
                                    if (bTabWithPivotTable && bTabOutSel)
296
0
                                        bTabWithPivotTable = false;
297
0
                                    if (bTabWithPivotTable)
298
0
                                        break;
299
0
                                }
300
0
                            }
301
0
                        }
302
0
                    }
303
304
0
                    SCTAB nTabSelCnt = rViewData.GetMarkData().GetSelectCount();
305
0
                    OUString aTabSelCnt = Application::GetSettings().GetUILocaleDataWrapper().getNum( nTabSelCnt, 0 );
306
0
                    OUString aQueryDeleteTab = ScResId( STR_QUERY_DELTAB, nTabSelCnt )
307
0
                                                        .replaceAll( "%d", aTabSelCnt );
308
0
                    if (bTabWithPivotTable)
309
0
                    {
310
0
                        OUString aStr = ScResId( STR_QUERY_PIVOTTABLE_DELTAB, nTabSelCnt )
311
0
                                                        .replaceAll( "%d", aTabSelCnt )
312
0
                                                        + " " + aQueryDeleteTab;
313
314
0
                        std::unique_ptr<weld::MessageDialog> xQueryBox(Application::CreateMessageDialog(GetFrameWeld(),
315
0
                                                                       VclMessageType::Question, VclButtonsType::YesNo,
316
0
                                                                       aStr));
317
0
                        xQueryBox->set_default_response(RET_NO);
318
319
                        // Hard warning as there is potential of data loss on deletion
320
0
                        bDoIt = (RET_YES == xQueryBox->run());
321
0
                    }
322
0
                    else
323
0
                    {
324
0
                        bool bHasData = false;
325
0
                        ScMarkData& rMark = rViewData.GetMarkData();
326
0
                        for ( SCTAB i = 0; i < nTabCount && !bHasData; i++ )
327
0
                        {
328
0
                            if ( rMark.GetTableSelect(i) && !rDoc.IsTabProtected(i) )
329
0
                            {
330
0
                                SCCOL nStartCol;
331
0
                                SCROW nStartRow;
332
0
                                bHasData = rDoc.GetDataStart( i, nStartCol, nStartRow );
333
0
                            }
334
0
                        }
335
                        // Do not ask for confirmation if all selected tabs are empty
336
0
                        if (bHasData)
337
0
                        {
338
0
                            std::unique_ptr<weld::MessageDialog> xQueryBox(Application::CreateMessageDialog(GetFrameWeld(),
339
0
                                                                           VclMessageType::Question, VclButtonsType::YesNo,
340
0
                                                                           aQueryDeleteTab));
341
0
                            xQueryBox->set_default_response(RET_YES);
342
343
                            // no parameter given, ask for confirmation
344
0
                            bDoIt = (RET_YES == xQueryBox->run());
345
0
                        }
346
0
                        else
347
0
                            bDoIt = true;
348
0
                    }
349
0
                }
350
351
0
                if (bDoIt)
352
0
                {
353
0
                    SCTAB nNewTab = nCurrentTab;
354
0
                    std::vector<SCTAB> TheTabs;
355
356
0
                    if (bHasIndex)
357
0
                    {
358
                        // sheet no. provided by the parameter
359
0
                        TheTabs.push_back(nTabNr);
360
0
                        if (nNewTab > nTabNr && nNewTab > 0)
361
0
                            --nNewTab;
362
0
                    }
363
0
                    else
364
0
                    {
365
0
                        SCTAB nFirstTab = 0;
366
0
                        bool bTabFlag = false;
367
0
                        ScMarkData& rMark = rViewData.GetMarkData();
368
0
                        for (SCTAB i = 0; i < nTabCount; i++)
369
0
                        {
370
0
                            if (rMark.GetTableSelect(i) && !rDoc.IsTabProtected(i))
371
0
                            {
372
0
                                TheTabs.push_back(i);
373
0
                                bTabFlag = true;
374
0
                                if (nNewTab == i && i+1 < nTabCount)
375
0
                                    nNewTab++;
376
0
                            }
377
0
                            if (!bTabFlag)
378
0
                                nFirstTab = i;
379
0
                        }
380
0
                        if (nNewTab >= nTabCount - static_cast<SCTAB>(TheTabs.size()))
381
0
                            nNewTab = nFirstTab;
382
0
                    }
383
384
0
                    rViewData.SetTabNo(nNewTab);
385
0
                    DeleteTables(TheTabs);
386
0
                    TheTabs.clear();
387
0
                    rReq.Done();
388
0
                }
389
0
            }
390
0
            break;
391
392
0
        case FID_TAB_RTL:
393
0
            {
394
0
                ScDocShell* pDocSh = rViewData.GetDocShell();
395
0
                ScDocFunc &rFunc = pDocSh->GetDocFunc();
396
0
                bool bSet = !rDoc.IsLayoutRTL( nCurrentTab );
397
398
0
                const ScMarkData& rMark = rViewData.GetMarkData();
399
0
                if ( rMark.GetSelectCount() != 0 )
400
0
                {
401
                    //  handle several sheets
402
403
0
                    SfxUndoManager* pUndoManager = pDocSh->GetUndoManager();
404
0
                    OUString aUndo = ScResId( STR_UNDO_TAB_RTL );
405
0
                    pUndoManager->EnterListAction( aUndo, aUndo, 0, rViewData.GetViewShell()->GetViewShellId() );
406
407
0
                    for (const auto& rTab : rMark)
408
0
                        rFunc.SetLayoutRTL( rTab, bSet );
409
410
0
                    pUndoManager->LeaveListAction();
411
0
                }
412
0
                else
413
0
                    rFunc.SetLayoutRTL( nCurrentTab, bSet );
414
0
            }
415
0
            break;
416
417
0
        case FID_TAB_TOGGLE_GRID:
418
0
            {
419
0
                bool bShowGrid = rViewData.GetShowGrid();
420
0
                rViewData.SetShowGrid(!bShowGrid);
421
0
                SfxBindings& rBindings = GetViewFrame().GetBindings();
422
0
                rBindings.Invalidate( FID_TAB_TOGGLE_GRID );
423
0
                ScDocShellModificator aModificator(*rViewData.GetDocShell());
424
0
                aModificator.SetDocumentModified();
425
0
                PaintGrid();
426
0
                rReq.Done();
427
0
            }
428
0
            break;
429
430
0
        case FID_TAB_SET_TAB_BG_COLOR:
431
0
        case FID_TAB_MENU_SET_TAB_BG_COLOR:
432
0
            ExecuteSetTableBackgroundCol(rReq);
433
0
            break;
434
435
0
        case FID_TAB_EVENTS:
436
0
                {
437
0
                    ScDocShell* pDocSh = rViewData.GetDocShell();
438
0
                    uno::Reference<container::XNameReplace> xEvents( new ScSheetEventsObj( pDocSh, nCurrentTab ) );
439
0
                    uno::Reference<frame::XFrame> xFrame = GetViewFrame().GetFrame().GetFrameInterface();
440
0
                    SvxAbstractDialogFactory* pDlgFactory = SvxAbstractDialogFactory::Create();
441
0
                    VclPtr<VclAbstractDialog> pDialog( pDlgFactory->CreateSvxMacroAssignDlg(
442
0
                        GetFrameWeld(), xFrame, false, xEvents, 0 ) );
443
                    // the dialog modifies the settings directly
444
0
                    pDialog->StartExecuteAsync(
445
0
                        [pDialog] (sal_Int32 /*nResult*/)->void
446
0
                        {
447
0
                            pDialog->disposeOnce();
448
0
                        }
449
0
                    );
450
0
                }
451
0
                break;
452
0
        case FID_TOGGLEHIDDENCOLROW:
453
0
                {
454
0
                    svtools::EditableColorConfig aEditableConfig;
455
0
                    svtools::ColorConfigValue aValue = aEditableConfig.GetColorValue(svtools::CALCHIDDENROWCOL);
456
0
                    aValue.bIsVisible = !aValue.bIsVisible;
457
0
                    aEditableConfig.SetColorValue(svtools::CALCHIDDENROWCOL, aValue);
458
0
                }
459
0
                break;
460
0
        default:
461
0
                OSL_FAIL("unknown message for ViewShell");
462
0
                break;
463
0
    }
464
0
}
465
466
void ScTabViewShell::GetStateTable( SfxItemSet& rSet )
467
0
{
468
0
    ScViewData& rViewData   = GetViewData();
469
0
    ScDocument& rDoc        = rViewData.GetDocument();
470
0
    ScDocShell* pDocShell   = rViewData.GetDocShell();
471
0
    ScMarkData& rMark       = GetViewData().GetMarkData();
472
0
    SCTAB       nTab        = rViewData.CurrentTabForData();
473
474
0
    SCTAB nTabCount = rDoc.GetTableCount();
475
0
    SCTAB nTabSelCount = rMark.GetSelectCount();
476
477
0
    SfxWhichIter    aIter(rSet);
478
0
    sal_uInt16          nWhich = aIter.FirstWhich();
479
480
0
    while ( nWhich )
481
0
    {
482
0
        switch ( nWhich )
483
0
        {
484
485
0
            case FID_TABLE_VISIBLE:
486
0
                rSet.Put( SfxBoolItem( nWhich, rDoc.IsVisible(nTab) ));
487
0
                break;
488
489
0
            case FID_TABLE_HIDE:
490
0
                {
491
0
                    sal_uInt16 nVis = 0;
492
                    // enable menu : check to make sure we won't hide all sheets. we need at least one visible at all times.
493
0
                    for ( SCTAB i=0; i < nTabCount && nVis<nTabSelCount + 1; i++ )
494
0
                        if (rDoc.IsVisible(i))
495
0
                            ++nVis;
496
0
                    if ( nVis<=nTabSelCount || !rDoc.IsDocEditable() )
497
0
                        rSet.DisableItem( nWhich );
498
0
                }
499
0
                break;
500
501
0
            case FID_TABLE_SHOW:
502
0
                {
503
0
                    bool bHasHidden = false;
504
0
                    for ( SCTAB i=0; i < nTabCount && !bHasHidden; i++ )
505
0
                        if (!rDoc.IsVisible(i))
506
0
                            bHasHidden = true;
507
0
                    if ( !bHasHidden || rDoc.IsDocProtected() || nTabSelCount > 1 )
508
0
                        rSet.DisableItem( nWhich );
509
0
                }
510
0
                break;
511
512
0
            case FID_DELETE_TABLE:
513
0
                {
514
0
                    if ( rDoc.GetChangeTrack() )
515
0
                        rSet.DisableItem( nWhich );
516
0
                    else
517
0
                    {
518
0
                        sal_uInt16 nVis = 0;
519
0
                        for ( SCTAB i=0; i < nTabCount && nVis<2; i++ )
520
0
                            if (rDoc.IsVisible(i))
521
0
                                ++nVis;
522
0
                        if (   rDoc.IsTabProtected(nTab)
523
0
                            || !rDoc.IsDocEditable()
524
0
                            || nVis < 2
525
0
                            || nTabSelCount == nTabCount)
526
0
                            rSet.DisableItem( nWhich );
527
0
                    }
528
0
                }
529
0
                break;
530
531
0
            case FID_INS_TABLE:
532
0
            case FID_INS_TABLE_EXT:
533
0
            case FID_TAB_APPEND:
534
0
                if ( !rDoc.IsDocEditable() ||
535
0
                     nTabCount > MAXTAB ||
536
0
                     ( nWhich == FID_INS_TABLE_EXT && pDocShell && pDocShell->IsDocShared() ) )
537
0
                    rSet.DisableItem( nWhich );
538
0
                break;
539
540
0
            case FID_TAB_MOVE:
541
0
            case FID_TAB_DUPLICATE:
542
0
                if (   !rDoc.IsDocEditable()
543
0
                    || rDoc.GetChangeTrack() != nullptr
544
0
                    || nTabCount > MAXTAB)
545
0
                    rSet.DisableItem( nWhich );
546
0
                break;
547
548
            //  FID_TAB_MENU_RENAME - "rename" from Menu
549
            //  FID_TAB_RENAME      - "name"-property for Basic
550
551
0
            case FID_TAB_MENU_RENAME:
552
0
                if ( !rDoc.IsDocEditable() ||
553
0
                     rDoc.IsTabProtected(nTab) ||nTabSelCount > 1 ||
554
0
                     ( pDocShell && pDocShell->IsDocShared() ) )
555
0
                    rSet.DisableItem( nWhich );
556
0
                break;
557
558
0
            case FID_TAB_RENAME:
559
0
                {
560
0
                    OUString aTabName;
561
0
                    rDoc.GetName( nTab, aTabName );
562
563
0
                    rSet.Put( SfxStringItem( nWhich, aTabName ));
564
565
0
                }
566
0
                break;
567
568
0
            case FID_TAB_RTL:
569
0
                {
570
0
                    if ( !SvtCTLOptions::IsCTLFontEnabled() )
571
0
                        rSet.DisableItem( nWhich );
572
0
                    else
573
0
                        rSet.Put( SfxBoolItem( nWhich, rDoc.IsLayoutRTL( nTab ) ) );
574
0
                }
575
0
                break;
576
577
0
            case FID_TAB_MENU_SET_TAB_BG_COLOR:
578
0
                {
579
0
                    if ( !rDoc.IsDocEditable()
580
0
                        || ( pDocShell && pDocShell->IsDocShared() )
581
0
                        || rDoc.IsTabProtected(nTab) )
582
0
                        rSet.DisableItem( nWhich );
583
0
                }
584
0
                break;
585
586
0
            case FID_TAB_SET_TAB_BG_COLOR:
587
0
                {
588
0
                    Color aColor = rDoc.GetTabBgColor( nTab );
589
0
                    rSet.Put( SvxColorItem( aColor, nWhich ) );
590
0
                }
591
0
                break;
592
593
0
            case FID_TAB_TOGGLE_GRID:
594
0
                rSet.Put( SfxBoolItem(nWhich, rViewData.GetShowGrid()) );
595
0
                break;
596
0
        }
597
0
        nWhich = aIter.NextWhich();
598
0
    }
599
0
}
600
601
void ScTabViewShell::ExecuteMoveTable( SfxRequest& rReq )
602
0
{
603
0
    ScViewData& rViewData   = GetViewData();
604
0
    ScDocument& rDoc        = rViewData.GetDocument();
605
0
    const SfxItemSet* pReqArgs = rReq.GetArgs();
606
607
0
    if ( rDoc.GetChangeTrack() != nullptr )
608
0
        return;    // if ChangeTracking is active, then no TabMove
609
610
0
    bool   bDoIt = false;
611
0
    sal_uInt16 nDoc = 0;
612
0
    SCTAB nTab = rViewData.CurrentTabForData();
613
0
    SCTAB nContextMenuTab = -1;
614
0
    bool bFromContextMenu = false;
615
0
    bool bFromMoveOrCopySheetDialog = false; // FN_PARAM_6
616
0
    bool bCpy = false, bUseCurrentDocument = false;
617
0
    OUString aDocName;
618
0
    OUString aTabName;
619
620
    // if FID_TAB_MOVE has parameters
621
0
    if( pReqArgs != nullptr )
622
0
    {
623
0
        SCTAB nTableCount = rDoc.GetTableCount();
624
0
        const SfxPoolItem* pItem;
625
626
        // if UseCurrentDocument(FN_PARAM_3) is true ignore the document name provided and use current document
627
0
        if( pReqArgs->HasItem( FN_PARAM_3, &pItem ) )
628
0
            bUseCurrentDocument = static_cast<const SfxBoolItem*>(pItem)->GetValue();
629
630
0
        if (bUseCurrentDocument)
631
0
            aDocName = GetViewData().GetDocShell()->GetTitle();
632
0
        else if(pReqArgs->HasItem( FID_TAB_MOVE, &pItem ))
633
0
            aDocName = static_cast<const SfxStringItem*>(pItem)->GetValue();
634
635
0
        if( pReqArgs->HasItem( FN_PARAM_1, &pItem ) )
636
0
        {
637
            // nTab is the target tab.
638
            // source tab is either the active tab or the tab that context menu opened on.
639
            //  table is 1-based
640
0
            nTab = static_cast<const SfxUInt16Item*>(pItem)->GetValue() - 1;
641
0
            if ( nTab >= nTableCount )
642
0
                nTab = SC_TAB_APPEND;
643
0
        }
644
0
        if( pReqArgs->HasItem( FN_PARAM_2, &pItem ) )
645
0
            bCpy = static_cast<const SfxBoolItem*>(pItem)->GetValue();
646
647
0
        if (pReqArgs->HasItem(FN_PARAM_4, &pItem))
648
0
        {
649
0
            bFromContextMenu = static_cast<const SfxBoolItem*>(pItem)->GetValue();
650
651
0
            if (bFromContextMenu)
652
0
            {
653
                // source tab: the tab that context menu opened on
654
0
                if (pReqArgs->HasItem(FN_PARAM_5, &pItem))
655
0
                    nContextMenuTab
656
0
                        = static_cast<const SfxUInt16Item*>(pItem)->GetValue();
657
658
0
                if (pReqArgs->HasItem(FN_PARAM_6, &pItem))
659
0
                    bFromMoveOrCopySheetDialog
660
0
                        = static_cast<const SfxBoolItem*>(pItem)->GetValue();
661
0
            }
662
0
        }
663
664
0
        if (bFromMoveOrCopySheetDialog)
665
0
        {
666
0
            OUString aDefaultName;
667
0
            rDoc.GetName(nContextMenuTab, aDefaultName);
668
669
0
            ScAbstractDialogFactory* pFact = ScAbstractDialogFactory::Create();
670
671
0
            VclPtr<AbstractScMoveTableDlg> pDlg(
672
0
                pFact->CreateScMoveTableDlg(GetFrameWeld(), aDefaultName));
673
674
0
            ScMarkData& rMark = GetViewData().GetMarkData();
675
0
            SCTAB nTabSelCount = rMark.GetSelectCount();
676
677
0
            if (nTableCount == nTabSelCount)
678
0
            {
679
0
                pDlg->SetForceCopyTable();
680
0
            }
681
682
            // We support direct renaming of sheet only when one sheet
683
            // is selected.
684
0
            pDlg->EnableRenameTable(nTabSelCount == 1);
685
686
0
            std::shared_ptr<SfxRequest> xReq = std::make_shared<SfxRequest>(rReq);
687
0
            pDlg->StartExecuteAsync([this, pDlg, xReq=std::move(xReq),
688
0
                                     nContextMenuTab](sal_Int32 nResult) {
689
690
0
                OUString aTableName;
691
0
                sal_uInt16 nDocument = 0;
692
0
                SCTAB nTargetIndex = -1;
693
0
                bool bCopy = false;
694
0
                bool bDoItAsync = false;
695
696
0
                if (RET_OK == nResult)
697
0
                {
698
0
                    nDocument = pDlg->GetSelectedDocument();
699
0
                    nTargetIndex = pDlg->GetSelectedTable();
700
0
                    bCopy = pDlg->GetCopyTable();
701
0
                    bool bRna = pDlg->GetRenameTable();
702
                    // Leave aTabName string empty, when Rename is FALSE.
703
0
                    if (bRna)
704
0
                        pDlg->GetTabNameString(aTableName);
705
706
0
                    bDoItAsync = true;
707
708
0
                    OUString aFoundDocName;
709
0
                    if (nDocument != SC_DOC_NEW)
710
0
                    {
711
0
                        ScDocShell* pSh = ScDocShell::GetShellByNum(nDocument);
712
0
                        if (pSh)
713
0
                        {
714
0
                            aFoundDocName = pSh->GetTitle();
715
0
                            if (!pSh->GetDocument().IsDocEditable())
716
0
                            {
717
0
                                ErrorMessage(STR_READONLYERR);
718
0
                                bDoItAsync = false;
719
0
                            }
720
0
                        }
721
0
                    }
722
0
                    xReq->AppendItem(SfxStringItem(FID_TAB_MOVE, aFoundDocName));
723
                    // 1-based table, if not APPEND
724
0
                    SCTAB nBasicTab = (nContextMenuTab <= MAXTAB)
725
0
                                          ? (nContextMenuTab + 1)
726
0
                                          : nContextMenuTab;
727
0
                    xReq->AppendItem(
728
0
                        SfxUInt16Item(FN_PARAM_1, static_cast<sal_uInt16>(nBasicTab)));
729
0
                    xReq->AppendItem(SfxBoolItem(FN_PARAM_2, bCopy));
730
731
0
                    if (bDoItAsync)
732
0
                    {
733
0
                        xReq->Done();
734
735
                        // send move or copy request
736
0
                        MoveTable(nDocument, nTargetIndex, bCopy, &aTableName, true,
737
0
                                  nContextMenuTab);
738
0
                    }
739
0
                }
740
0
                pDlg->disposeOnce();
741
0
            });
742
0
            rReq.Ignore();
743
0
        }
744
0
        else
745
0
        {
746
0
            if (!aDocName.isEmpty())
747
0
            {
748
0
                SfxObjectShell* pSh     = SfxObjectShell::GetFirst();
749
0
                ScDocShell*     pScSh   = nullptr;
750
0
                sal_uInt16          i=0;
751
752
0
                while ( pSh )
753
0
                {
754
0
                    pScSh = dynamic_cast<ScDocShell*>( pSh  );
755
756
0
                    if( pScSh )
757
0
                    {
758
0
                        if (aDocName == pScSh->GetTitle())
759
0
                        {
760
0
                            nDoc = i;
761
0
                            ScDocument& rDestDoc = pScSh->GetDocument();
762
0
                            nTableCount = rDestDoc.GetTableCount();
763
0
                            bDoIt = rDestDoc.IsDocEditable();
764
0
                            break;
765
0
                        }
766
767
0
                        i++;        // only count ScDocShell
768
0
                    }
769
0
                    pSh = SfxObjectShell::GetNext( *pSh );
770
0
                }
771
0
            }
772
0
            else // no doc-name -> new doc
773
0
            {
774
0
                nDoc = SC_DOC_NEW;
775
0
                bDoIt = true;
776
0
            }
777
778
0
            if ( bDoIt && nTab >= nTableCount )     // if necessary append
779
0
                nTab = SC_TAB_APPEND;
780
0
        }
781
0
    }
782
0
    else
783
0
    {
784
0
        OUString aDefaultName;
785
0
        rDoc.GetName( rViewData.CurrentTabForData(), aDefaultName );
786
787
0
        ScAbstractDialogFactory* pFact = ScAbstractDialogFactory::Create();
788
789
0
        VclPtr<AbstractScMoveTableDlg> pDlg(pFact->CreateScMoveTableDlg(GetFrameWeld(),
790
0
            aDefaultName));
791
792
0
        SCTAB nTableCount = rDoc.GetTableCount();
793
0
        ScMarkData& rMark       = GetViewData().GetMarkData();
794
0
        SCTAB       nTabSelCount = rMark.GetSelectCount();
795
796
0
        if(nTableCount==nTabSelCount)
797
0
        {
798
0
            pDlg->SetForceCopyTable();
799
0
        }
800
801
        // We support direct renaming of sheet only when one sheet
802
        // is selected.
803
0
        pDlg->EnableRenameTable(nTabSelCount == 1);
804
805
0
        auto xRequest = std::make_shared<SfxRequest>(rReq);
806
0
        rReq.Ignore(); // the 'old' request is not relevant any more
807
0
        pDlg->StartExecuteAsync(
808
0
            [this, pDlg, xRequest=std::move(xRequest)] (sal_Int32 nResult)->void
809
0
            {
810
0
                if (nResult == RET_OK)
811
0
                {
812
0
                    DoMoveTableFromDialog(*xRequest, pDlg);
813
0
                }
814
0
                pDlg->disposeOnce();
815
0
            }
816
0
        );
817
0
    }
818
819
0
    if( bDoIt )
820
0
    {
821
0
        rReq.Done();        // record, while doc is active
822
823
0
        if (bFromContextMenu)
824
0
            MoveTable(nDoc, nTab, bCpy, &aTabName, true,
825
0
                      nContextMenuTab);
826
0
        else
827
0
            MoveTable( nDoc, nTab, bCpy, &aTabName );
828
0
    }
829
0
}
830
831
void ScTabViewShell::ExecuteInsertTable(SfxRequest& rReq)
832
0
{
833
0
    ScViewData& rViewData   = GetViewData();
834
0
    ScDocument& rDoc        = rViewData.GetDocument();
835
0
    const SfxItemSet* pReqArgs = rReq.GetArgs();
836
0
    sal_uInt16  nSlot       = rReq.GetSlot();
837
0
    SCTAB       nCurrentTab = rViewData.CurrentTabForData();
838
0
    SCTAB       nTabCount   = rDoc.GetTableCount();
839
0
    ScMarkData& rMark    = rViewData.GetMarkData();
840
0
    SCTAB   nTabSelCount = rMark.GetSelectCount();
841
0
    SCTAB   nTabNr       = nCurrentTab;
842
843
0
    if ( !rDoc.IsDocEditable() )
844
0
        return;                          // locked
845
846
0
    if ( pReqArgs != nullptr )             // from basic
847
0
    {
848
0
        bool bOk = false;
849
0
        const SfxPoolItem*  pTabItem;
850
0
        const SfxPoolItem*  pNameItem;
851
852
0
        if ( pReqArgs->HasItem( FN_PARAM_1, &pTabItem ) &&
853
0
             pReqArgs->HasItem( nSlot, &pNameItem ) )
854
0
        {
855
0
            OUString aName = static_cast<const SfxStringItem*>(pNameItem)->GetValue();
856
0
            rDoc.CreateValidTabName(aName);
857
858
            // sheet number from basic: 1-based
859
            // 0 is special, means adding at the end
860
0
            nTabNr = static_cast<const SfxUInt16Item*>(pTabItem)->GetValue();
861
0
            if (nTabNr == 0)
862
0
                nTabNr = nTabCount;
863
0
            else
864
0
                --nTabNr;
865
866
0
            if (nTabNr > nTabCount)
867
0
                nTabNr = nTabCount;
868
869
0
            bOk = InsertTable(aName, nTabNr);
870
0
        }
871
872
0
        if (bOk)
873
0
        {
874
0
            rViewData.GetViewShell()->SetActive();
875
0
            rReq.Done( *pReqArgs );
876
0
        }
877
        //! else set error
878
0
    }
879
0
    else                                // dialog
880
0
    {
881
0
        ScAbstractDialogFactory* pFact = ScAbstractDialogFactory::Create();
882
0
        VclPtr<AbstractScInsertTableDlg> pDlg(pFact->CreateScInsertTableDlg(GetFrameWeld(), rViewData,
883
0
            nTabSelCount, nSlot == FID_INS_TABLE_EXT));
884
0
        sfx2::ExecDialogPerRequestAndDispose(pDlg, rReq,
885
0
                                             [this, pDlg](sal_Int32 nResult, SfxRequest& req)
886
0
                                             {
887
0
                                                 if (nResult == RET_OK)
888
0
                                                     DoInsertTableFromDialog(req, pDlg);
889
0
                                             });
890
0
    }
891
0
}
892
893
void ScTabViewShell::DoInsertTableFromDialog(SfxRequest& rReq, const VclPtr<AbstractScInsertTableDlg>& pDlg)
894
0
{
895
0
    ScViewData& rViewData   = GetViewData();
896
0
    ScDocument& rDoc        = rViewData.GetDocument();
897
0
    SCTAB       nCurrentTab = rViewData.CurrentTabForData();
898
0
    SCTAB       nTabNr      = nCurrentTab;
899
0
    SCTAB       nTabCount   = rDoc.GetTableCount();
900
0
    ScMarkData& rMark    = rViewData.GetMarkData();
901
902
0
    if (pDlg->GetTablesFromFile())
903
0
    {
904
0
        std::vector<SCTAB> nTabs;
905
0
        sal_uInt16 n = 0;
906
0
        const OUString* pStr = pDlg->GetFirstTable( &n );
907
0
        while ( pStr )
908
0
        {
909
0
            nTabs.push_back( static_cast<SCTAB>(n) );
910
0
            pStr = pDlg->GetNextTable( &n );
911
0
        }
912
0
        bool bLink = pDlg->GetTablesAsLink();
913
0
        if (!nTabs.empty())
914
0
        {
915
0
            if(pDlg->IsTableBefore())
916
0
            {
917
0
                ImportTables( pDlg->GetDocShellTables(), nTabs.size(), nTabs.data(),
918
0
                            bLink,nTabNr );
919
0
            }
920
0
            else
921
0
            {
922
0
                SCTAB   nTabAfter    = nTabNr+1;
923
924
0
                for(SCTAB j=nCurrentTab+1;j<nTabCount;j++)
925
0
                {
926
0
                    if(!rDoc.IsScenario(j))
927
0
                    {
928
0
                        nTabAfter=j;
929
0
                        break;
930
0
                    }
931
0
                }
932
933
0
                ImportTables( pDlg->GetDocShellTables(), nTabs.size(), nTabs.data(),
934
0
                            bLink,nTabAfter );
935
0
            }
936
0
        }
937
0
    }
938
0
    else
939
0
    {
940
0
        SCTAB nCount=pDlg->GetTableCount();
941
0
        if(pDlg->IsTableBefore())
942
0
        {
943
0
            if(nCount==1 && !pDlg->GetFirstTable()->isEmpty())
944
0
            {
945
0
                rReq.AppendItem( SfxStringItem( FID_INS_TABLE, *pDlg->GetFirstTable() ) );
946
0
                rReq.AppendItem( SfxUInt16Item( FN_PARAM_1, static_cast<sal_uInt16>(nTabNr) + 1 ) );        // 1-based
947
0
                rReq.Done();
948
949
0
                InsertTable( *pDlg->GetFirstTable(), nTabNr );
950
0
            }
951
0
            else
952
0
            {
953
0
                std::vector<OUString> aNames(0);
954
0
                InsertTables( aNames, nTabNr,nCount );
955
0
            }
956
0
        }
957
0
        else
958
0
        {
959
0
            SCTAB   nTabAfter    = nTabNr+1;
960
0
            SCTAB nSelHigh = rMark.GetLastSelected();
961
962
0
            for(SCTAB j=nSelHigh+1;j<nTabCount;j++)
963
0
            {
964
0
                if(!rDoc.IsScenario(j))
965
0
                {
966
0
                    nTabAfter=j;
967
0
                    break;
968
0
                }
969
0
                else // #101672#; increase nTabAfter, because it is possible that the scenario tables are the last
970
0
                    nTabAfter = j + 1;
971
0
            }
972
973
0
            if(nCount==1 && !pDlg->GetFirstTable()->isEmpty())
974
0
            {
975
0
                rReq.AppendItem( SfxStringItem( FID_INS_TABLE, *pDlg->GetFirstTable() ) );
976
0
                rReq.AppendItem( SfxUInt16Item( FN_PARAM_1, static_cast<sal_uInt16>(nTabAfter) + 1 ) );     // 1-based
977
0
                rReq.Done();
978
979
0
                InsertTable( *pDlg->GetFirstTable(), nTabAfter);
980
0
            }
981
0
            else
982
0
            {
983
0
                std::vector<OUString> aNames(0);
984
0
                InsertTables( aNames, nTabAfter,nCount);
985
0
            }
986
0
        }
987
0
    }
988
0
    rViewData.GetViewShell()->SetActive();
989
0
}
990
991
void ScTabViewShell::DoMoveTableFromDialog( SfxRequest& rReq, const VclPtr<AbstractScMoveTableDlg>& pDlg )
992
0
{
993
0
    sal_uInt16 nDoc = pDlg->GetSelectedDocument();
994
0
    SCTAB nTab = pDlg->GetSelectedTable();
995
0
    bool bCpy = pDlg->GetCopyTable();
996
0
    if (!bCpy)
997
0
    {
998
0
        SCTAB nOldIndex = GetViewData().GetMarkData().GetFirstSelected();
999
0
        if (nTab > nOldIndex)
1000
0
        {
1001
            // move right: target index will be one less after source tab is removed
1002
0
            --nTab;
1003
0
        }
1004
0
    }
1005
0
    bool bRna = pDlg->GetRenameTable();
1006
0
    OUString aTabName;
1007
    // Leave aTabName string empty, when Rename is FALSE.
1008
0
    if( bRna )
1009
0
    {
1010
0
       pDlg->GetTabNameString( aTabName );
1011
0
    }
1012
0
    bool bDoIt = true;
1013
1014
0
    OUString aFoundDocName;
1015
0
    if ( nDoc != SC_DOC_NEW )
1016
0
    {
1017
0
        ScDocShell* pSh = ScDocShell::GetShellByNum( nDoc );
1018
0
        if (pSh)
1019
0
        {
1020
0
            aFoundDocName = pSh->GetTitle();
1021
0
            if ( !pSh->GetDocument().IsDocEditable() )
1022
0
            {
1023
0
                ErrorMessage(STR_READONLYERR);
1024
0
                bDoIt = false;
1025
0
            }
1026
0
        }
1027
0
    }
1028
0
    rReq.AppendItem( SfxStringItem( FID_TAB_MOVE, aFoundDocName ) );
1029
    // 1-based table, if not APPEND
1030
0
    SCTAB nBasicTab = ( nTab <= MAXTAB ) ? (nTab+1) : nTab;
1031
0
    rReq.AppendItem( SfxUInt16Item( FN_PARAM_1, static_cast<sal_uInt16>(nBasicTab) ) );
1032
0
    rReq.AppendItem( SfxBoolItem( FN_PARAM_2, bCpy ) );
1033
0
    if( bDoIt )
1034
0
    {
1035
0
        rReq.Done();        // record, while doc is active
1036
0
        MoveTable( nDoc, nTab, bCpy, &aTabName );
1037
0
    }
1038
0
}
1039
1040
void ScTabViewShell::ExecuteAppendOrRenameTable(SfxRequest& rReq)
1041
0
{
1042
0
    ScViewData& rViewData   = GetViewData();
1043
0
    ScDocument& rDoc        = rViewData.GetDocument();
1044
0
    sal_uInt16  nSlot       = rReq.GetSlot();
1045
0
    const SfxItemSet* pReqArgs = rReq.GetArgs();
1046
1047
0
    if ( nSlot == FID_TAB_MENU_RENAME )
1048
0
        nSlot = FID_TAB_RENAME;             // equal execute
1049
1050
0
    SCTAB nTabNr = rViewData.CurrentTabForData();
1051
0
    ScMarkData& rMark = rViewData.GetMarkData();
1052
0
    SCTAB nTabSelCount = rMark.GetSelectCount();
1053
1054
0
    if ( !rDoc.IsDocEditable() )
1055
0
        return; // everything locked
1056
1057
0
    if ( nSlot != FID_TAB_APPEND &&
1058
0
            ( rDoc.IsTabProtected( nTabNr ) || nTabSelCount > 1 ) )
1059
0
        return; // no rename
1060
1061
0
    if( pReqArgs != nullptr )
1062
0
    {
1063
0
        bool        bDone   = false;
1064
0
        const SfxPoolItem* pItem;
1065
0
        OUString      aName;
1066
1067
0
        if( pReqArgs->HasItem( FN_PARAM_1, &pItem ) )
1068
0
        {
1069
0
            nTabNr = static_cast<const SfxUInt16Item*>(pItem)->GetValue();
1070
1071
            // inserting is 1-based, let's be consistent
1072
0
            if (nTabNr > 0)
1073
0
                --nTabNr;
1074
0
        }
1075
1076
0
        if( pReqArgs->HasItem( nSlot, &pItem ) )
1077
0
            aName = static_cast<const SfxStringItem*>(pItem)->GetValue();
1078
1079
0
        switch ( nSlot )
1080
0
        {
1081
0
            case FID_TAB_APPEND:
1082
0
                bDone = AppendTable( aName );
1083
0
                break;
1084
0
            case FID_TAB_RENAME:
1085
0
                bDone = RenameTable( aName, nTabNr );
1086
0
                break;
1087
0
        }
1088
1089
0
        if( bDone )
1090
0
        {
1091
0
            rReq.Done( *pReqArgs );
1092
0
        }
1093
0
    }
1094
0
    else
1095
0
    {
1096
0
        OUString aName;
1097
0
        OUString aDlgTitle;
1098
0
        OUString sHelpId;
1099
1100
0
        switch ( nSlot )
1101
0
        {
1102
0
            case FID_TAB_APPEND:
1103
0
                aDlgTitle = ScResId(SCSTR_APDTABLE);
1104
0
                rDoc.CreateValidTabName( aName );
1105
0
                sHelpId = HID_SC_APPEND_NAME;
1106
0
                break;
1107
1108
0
            case FID_TAB_RENAME:
1109
0
                aDlgTitle = ScResId(SCSTR_RENAMETAB);
1110
0
                rDoc.GetName( rViewData.CurrentTabForData(), aName );
1111
0
                sHelpId = HID_SC_RENAME_NAME;
1112
0
                break;
1113
0
        }
1114
1115
0
        ScAbstractDialogFactory* pFact = ScAbstractDialogFactory::Create();
1116
1117
0
        VclPtr<AbstractScStringInputDlg> pDlg(pFact->CreateScStringInputDlg(
1118
0
            GetFrameWeld(), aDlgTitle, ScResId(SCSTR_NAME),
1119
0
            aName, GetStaticInterface()->GetSlot(nSlot)->GetCommand(),
1120
0
            sHelpId));
1121
1122
0
        auto xRequest = std::make_shared<SfxRequest>(rReq);
1123
0
        rReq.Ignore(); // the 'old' request is not relevant any more
1124
0
        ExecuteAppendOrRenameTableDialog(pDlg, xRequest, nSlot);
1125
0
    }
1126
0
}
1127
1128
void ScTabViewShell::ExecuteAppendOrRenameTableDialog(const VclPtr<AbstractScStringInputDlg>& pDlg,
1129
                        const std::shared_ptr<SfxRequest>& xReq,
1130
                        sal_uInt16 nSlot)
1131
0
{
1132
0
    pDlg->StartExecuteAsync(
1133
0
        [this, pDlg, xReq, nSlot] (sal_Int32 nResult)->void
1134
0
        {
1135
0
            if (DoAppendOrRenameTableDialog(nResult, pDlg, xReq, nSlot))
1136
0
                ExecuteAppendOrRenameTableDialog(pDlg, xReq, nSlot);
1137
0
            else
1138
0
                pDlg->disposeOnce();
1139
0
        }
1140
0
    );
1141
0
}
1142
1143
bool ScTabViewShell::DoAppendOrRenameTableDialog(sal_Int32 nResult, const VclPtr<AbstractScStringInputDlg>& pDlg,
1144
                        const std::shared_ptr<SfxRequest>& xReq,
1145
                        sal_uInt16 nSlot)
1146
0
{
1147
0
    if (nResult != RET_OK)
1148
0
        return false;
1149
1150
0
    ScViewData& rViewData   = GetViewData();
1151
0
    SCTAB nTabNr = rViewData.CurrentTabForData();
1152
0
    bool     bDone   = false;
1153
1154
0
    OUString aName = pDlg->GetInputString();
1155
1156
0
    switch ( nSlot )
1157
0
    {
1158
0
        case FID_TAB_APPEND:
1159
0
            bDone = AppendTable( aName );
1160
0
            break;
1161
0
        case FID_TAB_RENAME:
1162
0
            bDone = RenameTable( aName, nTabNr );
1163
0
            break;
1164
0
    }
1165
1166
0
    if ( bDone )
1167
0
    {
1168
0
        if (nSlot == FID_TAB_APPEND)
1169
0
            rViewData.GetViewShell()->SetActive();
1170
0
        xReq->AppendItem( SfxStringItem( nSlot, aName ) );
1171
0
        xReq->Done();
1172
0
    }
1173
0
    else
1174
0
    {
1175
0
        if( xReq->IsAPI() )
1176
0
        {
1177
#if HAVE_FEATURE_SCRIPTING
1178
            StarBASIC::Error( ERRCODE_BASIC_SETPROP_FAILED ); // XXX error handling???
1179
#endif
1180
0
        }
1181
0
        else
1182
0
        {
1183
0
            OUString aErrMsg ( ScResId( STR_INVALIDTABNAME ) );
1184
0
            std::unique_ptr<weld::MessageDialog> xBox(Application::CreateMessageDialog(GetFrameWeld(),
1185
0
                                                      VclMessageType::Warning, VclButtonsType::Ok, aErrMsg));
1186
0
            xBox->run();
1187
0
        }
1188
0
    }
1189
1190
0
    return !bDone;
1191
0
}
1192
1193
void ScTabViewShell::ExecuteSetTableBackgroundCol(SfxRequest& rReq)
1194
0
{
1195
0
    ScViewData& rViewData   = GetViewData();
1196
0
    ScDocument& rDoc        = rViewData.GetDocument();
1197
0
    sal_uInt16  nSlot       = rReq.GetSlot();
1198
0
    const SfxItemSet* pReqArgs = rReq.GetArgs();
1199
0
    if ( nSlot == FID_TAB_MENU_SET_TAB_BG_COLOR )
1200
0
        nSlot = FID_TAB_SET_TAB_BG_COLOR;
1201
0
    SCTAB nTabNr = rViewData.CurrentTabForData();
1202
0
    ScMarkData& rMark = rViewData.GetMarkData();
1203
0
    SCTAB nTabSelCount = rMark.GetSelectCount();
1204
0
    SCTAB nCurrentTab = rViewData.CurrentTabForData();
1205
1206
0
    if ( !rDoc.IsDocEditable() )
1207
0
        return;
1208
1209
0
    if ( rDoc.IsTabProtected( nTabNr ) ) // ||nTabSelCount > 1
1210
0
        return;
1211
1212
0
    if( pReqArgs != nullptr )
1213
0
    {
1214
0
        bool                bDone = false;
1215
0
        const SfxPoolItem*  pItem;
1216
0
        Color               aColor;
1217
1218
0
        if( pReqArgs->HasItem( nSlot, &pItem ) )
1219
0
            aColor = static_cast<const SvxColorItem*>(pItem)->GetValue();
1220
1221
0
        if ( nTabSelCount > 1 )
1222
0
        {
1223
0
            std::unique_ptr<ScUndoTabColorInfo::List>
1224
0
                pTabColorList(new ScUndoTabColorInfo::List);
1225
0
            for (const auto& rTab : rMark)
1226
0
            {
1227
0
                if ( !rDoc.IsTabProtected(rTab) )
1228
0
                {
1229
0
                    ScUndoTabColorInfo aTabColorInfo(rTab);
1230
0
                    aTabColorInfo.maNewTabBgColor = aColor;
1231
0
                    pTabColorList->push_back(aTabColorInfo);
1232
0
                }
1233
0
            }
1234
0
            bDone = SetTabBgColor( *pTabColorList );
1235
0
        }
1236
0
        else
1237
0
        {
1238
0
            bDone = SetTabBgColor( aColor, nCurrentTab ); //ScViewFunc.SetTabBgColor
1239
0
        }
1240
0
        if( bDone )
1241
0
        {
1242
0
            rReq.Done( *pReqArgs );
1243
0
        }
1244
0
    }
1245
0
    else
1246
0
    {
1247
0
        Color aTabBgColor = rDoc.GetTabBgColor( nCurrentTab );
1248
0
        ScAbstractDialogFactory* pFact = ScAbstractDialogFactory::Create();
1249
0
        VclPtr<AbstractScTabBgColorDlg> pDlg(pFact->CreateScTabBgColorDlg(
1250
0
                                                    GetFrameWeld(),
1251
0
                                                    ScResId(SCSTR_SET_TAB_BG_COLOR),
1252
0
                                                    ScResId(SCSTR_NO_TAB_BG_COLOR),
1253
0
                                                    aTabBgColor));
1254
1255
0
        auto xRequest = std::make_shared<SfxRequest>(rReq);
1256
0
        rReq.Ignore(); // the 'old' request is not relevant any more
1257
0
        ExecuteTableBackgroundDialog(pDlg, xRequest, aTabBgColor, nSlot);
1258
0
    }
1259
0
}
1260
1261
void ScTabViewShell::ExecuteTableBackgroundDialog(const VclPtr<AbstractScTabBgColorDlg>& pDlg,
1262
                        const std::shared_ptr<SfxRequest>& xReq,
1263
                        Color aOldTabBgColor, sal_uInt16 nSlot)
1264
0
{
1265
0
    pDlg->StartExecuteAsync(
1266
0
        [this, pDlg, xReq, aOldTabBgColor, nSlot] (sal_Int32 nResult)->void
1267
0
        {
1268
0
            if (DoTableBackgroundDialog(nResult, pDlg, xReq, aOldTabBgColor, nSlot))
1269
0
                ExecuteTableBackgroundDialog(pDlg, xReq, aOldTabBgColor, nSlot);
1270
0
            else
1271
0
                pDlg->disposeOnce();
1272
0
        }
1273
0
    );
1274
0
}
1275
1276
bool ScTabViewShell::DoTableBackgroundDialog(sal_Int32 nResult, const VclPtr<AbstractScTabBgColorDlg>& pDlg,
1277
                        const std::shared_ptr<SfxRequest>& xReq,
1278
                        Color aOldTabBgColor, sal_uInt16 nSlot)
1279
0
{
1280
0
    if (nResult != RET_OK)
1281
0
        return false;
1282
1283
0
    ScViewData& rViewData   = GetViewData();
1284
0
    ScDocument& rDoc        = rViewData.GetDocument();
1285
0
    ScMarkData& rMark = rViewData.GetMarkData();
1286
0
    SCTAB nCurrentTab = rViewData.CurrentTabForData();
1287
0
    SCTAB nTabSelCount = rMark.GetSelectCount();
1288
0
    bool bDone   = false; /// temp
1289
0
    Color aSelectedColor = pDlg->GetSelectedColor();
1290
0
    std::unique_ptr<ScUndoTabColorInfo::List>
1291
0
        pTabColorList(new ScUndoTabColorInfo::List);
1292
0
    if ( nTabSelCount > 1 )
1293
0
    {
1294
0
        for (const auto& rTab : rMark)
1295
0
        {
1296
0
            if ( !rDoc.IsTabProtected(rTab) )
1297
0
            {
1298
0
                ScUndoTabColorInfo aTabColorInfo(rTab);
1299
0
                aTabColorInfo.maNewTabBgColor = aSelectedColor;
1300
0
                pTabColorList->push_back(aTabColorInfo);
1301
0
            }
1302
0
        }
1303
0
        bDone = SetTabBgColor( *pTabColorList );
1304
0
    }
1305
0
    else
1306
0
    {
1307
0
        bDone = SetTabBgColor( aSelectedColor, nCurrentTab ); //ScViewFunc.SetTabBgColor
1308
0
    }
1309
1310
0
    if ( bDone )
1311
0
    {
1312
0
        xReq->AppendItem( SvxColorItem( aOldTabBgColor, nSlot ) );
1313
0
        xReq->Done();
1314
0
    }
1315
0
    else
1316
0
    {
1317
0
        if( xReq->IsAPI() )
1318
0
        {
1319
#if HAVE_FEATURE_SCRIPTING
1320
            StarBASIC::Error( ERRCODE_BASIC_SETPROP_FAILED );
1321
#endif
1322
0
        }
1323
0
    }
1324
1325
0
    return !bDone;
1326
0
}
1327
1328
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */