Coverage Report

Created: 2025-07-07 10:01

/src/libreoffice/sc/source/ui/view/tabvwsh2.cxx
Line
Count
Source (jump to first uncovered line)
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 <comphelper/lok.hxx>
21
#include <sfx2/bindings.hxx>
22
#include <sfx2/viewfrm.hxx>
23
#include <svl/whiter.hxx>
24
#include <unotools/moduleoptions.hxx>
25
#include <svl/cjkoptions.hxx>
26
#include <sfx2/dispatch.hxx>
27
#include <tools/UnitConversion.hxx>
28
29
#include <tabvwsh.hxx>
30
#include <drawview.hxx>
31
#include <fupoor.hxx>
32
#include <fuconrec.hxx>
33
#include <fuconpol.hxx>
34
#include <fuconarc.hxx>
35
#include <fuconuno.hxx>
36
#include <fusel.hxx>
37
#include <futext.hxx>
38
#include <fuinsert.hxx>
39
#include <sc.hrc>
40
#include <scmod.hxx>
41
#include <appoptio.hxx>
42
#include <gridwin.hxx>
43
44
// Create default drawing objects via keyboard
45
#include <svx/svdpagv.hxx>
46
#include <svl/stritem.hxx>
47
#include <fuconcustomshape.hxx>
48
49
SdrView* ScTabViewShell::GetDrawView() const
50
0
{
51
0
    return const_cast<ScTabViewShell*>(this)->GetScDrawView();    // GetScDrawView is non-const
52
0
}
53
54
void ScTabViewShell::WindowChanged()
55
0
{
56
0
    vcl::Window* pWin = GetActiveWin();
57
58
0
    ScDrawView* pDrView = GetScDrawView();
59
0
    if (pDrView)
60
0
        pDrView->SetActualWin(pWin->GetOutDev());
61
62
0
    FuPoor* pFunc = GetDrawFuncPtr();
63
0
    if (pFunc)
64
0
        pFunc->SetWindow(pWin);
65
66
    //  when font from InputContext is used,
67
    //  this must be moved to change of cursor position:
68
0
    UpdateInputContext();
69
0
}
70
71
void ScTabViewShell::ExecDraw(SfxRequest& rReq)
72
0
{
73
0
    ScModule* mod = ScModule::get();
74
0
    mod->InputEnterHandler();
75
0
    UpdateInputHandler();
76
77
0
    MakeDrawLayer();
78
79
0
    ScTabView* pTabView = GetViewData().GetView();
80
0
    SfxBindings& rBindings = GetViewFrame().GetBindings();
81
82
0
    vcl::Window*     pWin    = pTabView->GetActiveWin();
83
0
    ScDrawView* pView   = pTabView->GetScDrawView();
84
0
    SdrModel& rModel = pView->GetModel();
85
86
0
    const SfxItemSet *pArgs = rReq.GetArgs();
87
0
    sal_uInt16 nNewId = rReq.GetSlot();
88
89
0
    if ( nNewId == SID_DRAW_CHART )
90
0
    {
91
        // #i71254# directly insert a chart instead of drawing its output rectangle
92
0
        FuInsertChart(*this, pWin, pView, rModel, rReq, LINK( this, ScTabViewShell, DialogClosedHdl ));
93
0
        return;
94
0
    }
95
96
0
    if ( nNewId == SID_DRAW_SELECT )
97
0
        nNewId = SID_OBJECT_SELECT;
98
99
0
    SdrObjKind eNewFormObjKind = SdrObjKind::NONE;
100
0
    if (nNewId == SID_FM_CREATE_CONTROL)
101
0
    {
102
0
        const SfxUInt16Item* pIdentifierItem = rReq.GetArg<SfxUInt16Item>(SID_FM_CONTROL_IDENTIFIER);
103
0
        if (pIdentifierItem)
104
0
            eNewFormObjKind = static_cast<SdrObjKind>(pIdentifierItem->GetValue());
105
0
    }
106
107
0
    OUString sStringItemValue;
108
0
    if ( pArgs )
109
0
    {
110
0
        const SfxPoolItem* pItem;
111
0
        if ( pArgs->GetItemState( nNewId, true, &pItem ) == SfxItemState::SET )
112
0
            if (auto pStringItem = dynamic_cast<const SfxStringItem*>(pItem) )
113
0
                sStringItemValue = pStringItem->GetValue();
114
0
    }
115
0
    bool bSwitchCustom = ( !sStringItemValue.isEmpty() && !sDrawCustom.isEmpty() && sStringItemValue != sDrawCustom );
116
117
0
    if (nNewId == SID_INSERT_FRAME)                     // from Tbx button
118
0
        nNewId = SID_DRAW_TEXT;
119
120
    //  CTRL-SID_OBJECT_SELECT is used to select the first object,
121
    //  but not if SID_OBJECT_SELECT is the result of clicking a create function again,
122
    //  so this must be tested before changing nNewId below.
123
0
    bool bSelectFirst = ( nNewId == SID_OBJECT_SELECT && (rReq.GetModifier() & KEY_MOD1) );
124
125
0
    bool bEx = IsDrawSelMode();
126
0
    if ( rReq.GetModifier() & KEY_MOD1 )
127
0
    {
128
        //  always allow keyboard selection also on background layer
129
        //  also allow creation of default objects if the same object type
130
        //  was already active
131
0
        bEx = true;
132
0
    }
133
0
    else if ( nNewId == nDrawSfxId && ( nNewId != SID_FM_CREATE_CONTROL ||
134
0
                                    eNewFormObjKind == eFormObjKind || eNewFormObjKind == SdrObjKind::NONE ) && !bSwitchCustom )
135
0
    {
136
        // #i52871# if a different custom shape is selected, the slot id can be the same,
137
        // so the custom shape type string has to be compared, too.
138
139
        // SID_FM_CREATE_CONTROL with eNewFormObjKind==OBJ_NONE (without parameter) comes
140
        // from FuConstruct::SimpleMouseButtonUp when deactivating
141
        // Execute for the form shell, to deselect the controller
142
0
        if ( nNewId == SID_FM_CREATE_CONTROL )
143
0
        {
144
0
            GetViewData().GetDispatcher().Execute(SID_FM_LEAVE_CREATE);
145
0
            GetViewFrame().GetBindings().InvalidateAll(false);
146
            //! what kind of slot does the weird controller really need to display this????
147
0
        }
148
149
0
        bEx = !bEx;
150
0
        nNewId = SID_OBJECT_SELECT;
151
0
    }
152
0
    else
153
0
        bEx = true;
154
155
0
    if ( nDrawSfxId == SID_FM_CREATE_CONTROL && nNewId != nDrawSfxId )
156
0
    {
157
        // switching from control- to paint function -> deselect in control-controller
158
0
        GetViewData().GetDispatcher().Execute(SID_FM_LEAVE_CREATE);
159
0
        GetViewFrame().GetBindings().InvalidateAll(false);
160
        //! what kind of slot does the weird controller really need to display this????
161
0
    }
162
163
0
    SetDrawSelMode(bEx);
164
165
0
    pView->LockBackgroundLayer( !bEx );
166
167
0
    const SdrMarkList& rMarkList = pView->GetMarkedObjectList();
168
0
    if ( bSelectFirst )
169
0
    {
170
        // select first draw object if none is selected yet
171
0
        if(rMarkList.GetMarkCount() == 0)
172
0
        {
173
            // select first object
174
0
            pView->UnmarkAllObj();
175
0
            pView->MarkNextObj(true);
176
177
            // ...and make it visible
178
0
            if(rMarkList.GetMarkCount() != 0)
179
0
                pView->MakeVisible(pView->GetAllMarkedRect(), *pWin);
180
0
        }
181
0
    }
182
183
0
    nDrawSfxId = nNewId;
184
0
    sDrawCustom.clear();    // value is set below for custom shapes
185
186
0
    if (nNewId == SID_DRAW_TEXT || nNewId == SID_DRAW_TEXT_VERTICAL
187
0
        || nNewId == SID_DRAW_TEXT_MARQUEE || nNewId == SID_DRAW_NOTEEDIT)
188
0
        SetDrawTextShell(true);
189
0
    else
190
0
    {
191
0
        if (bEx || rMarkList.GetMarkCount() != 0)
192
0
            SetDrawShellOrSub();
193
0
        else
194
0
            SetDrawShell(false);
195
0
    }
196
197
0
    if (pTabView->GetDrawFuncPtr())
198
0
    {
199
0
        if (pTabView->GetDrawFuncOldPtr() != pTabView->GetDrawFuncPtr())
200
0
            delete pTabView->GetDrawFuncOldPtr();
201
202
0
        pTabView->GetDrawFuncPtr()->Deactivate();
203
0
        pTabView->SetDrawFuncOldPtr(pTabView->GetDrawFuncPtr());
204
0
        pTabView->SetDrawFuncPtr(nullptr);
205
0
    }
206
207
0
    SfxRequest aNewReq(rReq);
208
0
    aNewReq.SetSlot(nDrawSfxId);
209
210
0
    assert(nNewId != SID_DRAW_CHART); //#i71254# handled already above
211
212
    // for LibreOfficeKit - choosing a shape should construct it directly
213
0
    bool bCreateDirectly = false;
214
215
0
    switch (nNewId)
216
0
    {
217
0
        case SID_OBJECT_SELECT:
218
            // not always switch back
219
0
            if(rMarkList.GetMarkCount() == 0) SetDrawShell(bEx);
220
0
            pTabView->SetDrawFuncPtr(new FuSelection(*this, pWin, pView, rModel, aNewReq));
221
0
            break;
222
223
0
        case SID_DRAW_LINE:
224
0
        case SID_DRAW_XLINE:
225
0
        case SID_LINE_ARROW_END:
226
0
        case SID_LINE_ARROW_CIRCLE:
227
0
        case SID_LINE_ARROW_SQUARE:
228
0
        case SID_LINE_ARROW_START:
229
0
        case SID_LINE_CIRCLE_ARROW:
230
0
        case SID_LINE_SQUARE_ARROW:
231
0
        case SID_LINE_ARROWS:
232
0
        case SID_DRAW_RECT:
233
0
        case SID_DRAW_ELLIPSE:
234
0
        case SID_DRAW_MEASURELINE:
235
0
            pTabView->SetDrawFuncPtr(new FuConstRectangle(*this, pWin, pView, rModel, aNewReq));
236
0
            bCreateDirectly = comphelper::LibreOfficeKit::isActive();
237
0
            break;
238
239
0
        case SID_DRAW_CAPTION:
240
0
        case SID_DRAW_CAPTION_VERTICAL:
241
0
            pTabView->SetDrawFuncPtr(new FuConstRectangle(*this, pWin, pView, rModel, aNewReq));
242
0
            pView->SetFrameDragSingles( false );
243
0
            rBindings.Invalidate( SID_BEZIER_EDIT );
244
0
            break;
245
246
0
        case SID_DRAW_XPOLYGON:
247
0
        case SID_DRAW_XPOLYGON_NOFILL:
248
0
        case SID_DRAW_POLYGON:
249
0
        case SID_DRAW_POLYGON_NOFILL:
250
0
        case SID_DRAW_BEZIER_NOFILL:
251
0
        case SID_DRAW_BEZIER_FILL:
252
0
        case SID_DRAW_FREELINE:
253
0
        case SID_DRAW_FREELINE_NOFILL:
254
0
            pTabView->SetDrawFuncPtr(new FuConstPolygon(*this, pWin, pView, rModel, aNewReq));
255
0
            break;
256
257
0
        case SID_DRAW_ARC:
258
0
        case SID_DRAW_PIE:
259
0
        case SID_DRAW_CIRCLECUT:
260
0
            pTabView->SetDrawFuncPtr(new FuConstArc(*this, pWin, pView, rModel, aNewReq));
261
0
            break;
262
263
0
        case SID_DRAW_TEXT:
264
0
        case SID_DRAW_TEXT_VERTICAL:
265
0
        case SID_DRAW_TEXT_MARQUEE:
266
0
        case SID_DRAW_NOTEEDIT:
267
0
            pTabView->SetDrawFuncPtr(new FuText(*this, pWin, pView, rModel, aNewReq));
268
0
            bCreateDirectly = comphelper::LibreOfficeKit::isActive();
269
0
            break;
270
271
0
        case SID_FM_CREATE_CONTROL:
272
0
            SetDrawFormShell(true);
273
0
            pTabView->SetDrawFuncPtr(new FuConstUnoControl(*this, pWin, pView, rModel, aNewReq));
274
0
            eFormObjKind = eNewFormObjKind;
275
0
            break;
276
277
0
        case SID_DRAWTBX_CS_BASIC :
278
0
        case SID_DRAWTBX_CS_SYMBOL :
279
0
        case SID_DRAWTBX_CS_ARROW :
280
0
        case SID_DRAWTBX_CS_FLOWCHART :
281
0
        case SID_DRAWTBX_CS_CALLOUT :
282
0
        case SID_DRAWTBX_CS_STAR :
283
0
        case SID_DRAW_CS_ID :
284
0
        {
285
0
            pTabView->SetDrawFuncPtr(new FuConstCustomShape(*this, pWin, pView, rModel, aNewReq));
286
287
0
            bCreateDirectly = comphelper::LibreOfficeKit::isActive();
288
289
0
            if ( nNewId != SID_DRAW_CS_ID )
290
0
            {
291
0
                const SfxStringItem* pEnumCommand = rReq.GetArg<SfxStringItem>(nNewId);
292
0
                if ( pEnumCommand )
293
0
                {
294
0
                    SfxBindings& rBind = GetViewFrame().GetBindings();
295
0
                    rBind.Invalidate( nNewId );
296
0
                    rBind.Update( nNewId );
297
298
0
                    sDrawCustom = pEnumCommand->GetValue();  // to detect when a different shape type is selected
299
0
                }
300
0
            }
301
0
        }
302
0
        break;
303
304
0
        default:
305
0
            break;
306
0
    }
307
308
0
    if (pTabView->GetDrawFuncPtr())
309
0
        pTabView->GetDrawFuncPtr()->Activate();
310
311
0
    rReq.Done();
312
313
0
    Invalidate();
314
315
    // Create default drawing objects via keyboard
316
    // with qualifier construct directly
317
0
    FuPoor* pFuActual = GetDrawFuncPtr();
318
319
0
    if(!(pFuActual && ((rReq.GetModifier() & KEY_MOD1) || bCreateDirectly)))
320
0
        return;
321
322
    // Create default drawing objects via keyboard
323
0
    const ScAppOptions& rAppOpt = mod->GetAppOptions();
324
0
    sal_uInt32 nDefaultObjectSizeWidth = rAppOpt.GetDefaultObjectSizeWidth();
325
0
    sal_uInt32 nDefaultObjectSizeHeight = rAppOpt.GetDefaultObjectSizeHeight();
326
327
    // calc position and size
328
0
    bool bLOKIsActive = comphelper::LibreOfficeKit::isActive();
329
0
    Point aInsertPos;
330
0
    if(!bLOKIsActive)
331
0
    {
332
0
        tools::Rectangle aVisArea = pWin->PixelToLogic(tools::Rectangle(Point(0,0), pWin->GetOutputSizePixel()));
333
0
        aInsertPos = aVisArea.Center();
334
0
        aInsertPos.AdjustX( -sal_Int32(nDefaultObjectSizeWidth / 2) );
335
0
        aInsertPos.AdjustY( -sal_Int32(nDefaultObjectSizeHeight / 2) );
336
0
    }
337
0
    else
338
0
    {
339
0
        ScViewData& rViewData = GetViewData();
340
0
        tools::Long nLayoutSign = rViewData.GetDocument().IsLayoutRTL(rViewData.GetTabNo()) ? -1 : 1;
341
0
        aInsertPos = rViewData.getLOKVisibleArea().Center();
342
0
        if (comphelper::LibreOfficeKit::isCompatFlagSet(
343
0
                comphelper::LibreOfficeKit::Compat::scPrintTwipsMsgs))
344
0
            aInsertPos = rViewData.GetPrintTwipsPosFromTileTwips(aInsertPos);
345
346
0
        aInsertPos.setX(nLayoutSign * convertTwipToMm100(aInsertPos.X()));
347
0
        aInsertPos.setY(convertTwipToMm100(aInsertPos.Y()));
348
349
0
        aInsertPos.AdjustX( -sal_Int32(nDefaultObjectSizeWidth / 2) );
350
0
        aInsertPos.AdjustY( -sal_Int32(nDefaultObjectSizeHeight / 2) );
351
0
    }
352
353
0
    tools::Rectangle aNewObjectRectangle(aInsertPos, Size(nDefaultObjectSizeWidth, nDefaultObjectSizeHeight));
354
355
0
    ScDrawView* pDrView = GetScDrawView();
356
357
0
    if(!pDrView)
358
0
        return;
359
360
0
    SdrPageView* pPageView = pDrView->GetSdrPageView();
361
362
0
    if(!pPageView)
363
0
        return;
364
365
    // create the default object
366
0
    rtl::Reference<SdrObject> pObj = pFuActual->CreateDefaultObject(nNewId, aNewObjectRectangle);
367
368
0
    if(!pObj)
369
0
        return;
370
371
    // insert into page
372
0
    pView->InsertObjectAtView(pObj.get(), *pPageView);
373
374
0
    switch ( nNewId )
375
0
    {
376
0
    case SID_DRAW_CAPTION:
377
0
    case SID_DRAW_CAPTION_VERTICAL:
378
0
    case SID_DRAW_TEXT:
379
0
    case SID_DRAW_TEXT_VERTICAL:
380
        //  use KeyInput to start edit mode (FuText is created).
381
        //  For FuText objects, edit mode is handled within CreateDefaultObject.
382
        //  KEY_F2 is handled in FuDraw::KeyInput.
383
384
0
        pFuActual->KeyInput( KeyEvent( 0, vcl::KeyCode( KEY_F2 ) ) );
385
0
        break;
386
0
    default:
387
0
        break;
388
0
    }
389
0
}
390
391
void ScTabViewShell::GetDrawState(SfxItemSet &rSet)
392
0
{
393
0
    SfxWhichIter    aIter(rSet);
394
0
    sal_uInt16          nWhich = aIter.FirstWhich();
395
396
0
    while ( nWhich )
397
0
    {
398
0
        switch ( nWhich )
399
0
        {
400
0
            case SID_DRAW_CHART:
401
0
                {
402
0
                    bool bOle = GetViewFrame().GetFrame().IsInPlace();
403
0
                    if ( bOle || !SvtModuleOptions().IsChartInstalled() )
404
0
                        rSet.DisableItem( nWhich );
405
0
                }
406
0
                break;
407
408
0
            case SID_DRAW_LINE:
409
0
            case SID_DRAW_XLINE:
410
0
            case SID_LINE_ARROW_END:
411
0
            case SID_LINE_ARROW_CIRCLE:
412
0
            case SID_LINE_ARROW_SQUARE:
413
0
            case SID_LINE_ARROW_START:
414
0
            case SID_LINE_CIRCLE_ARROW:
415
0
            case SID_LINE_SQUARE_ARROW:
416
0
            case SID_LINE_ARROWS:
417
0
            case SID_DRAW_MEASURELINE:
418
0
            case SID_DRAW_RECT:
419
0
            case SID_DRAW_ELLIPSE:
420
0
            case SID_DRAW_POLYGON:
421
0
            case SID_DRAW_POLYGON_NOFILL:
422
0
            case SID_DRAW_XPOLYGON:
423
0
            case SID_DRAW_XPOLYGON_NOFILL:
424
0
            case SID_DRAW_BEZIER_FILL:
425
0
            case SID_DRAW_BEZIER_NOFILL:
426
0
            case SID_DRAW_FREELINE:
427
0
            case SID_DRAW_FREELINE_NOFILL:
428
0
            case SID_DRAW_ARC:
429
0
            case SID_DRAW_PIE:
430
0
            case SID_DRAW_CIRCLECUT:
431
0
            case SID_DRAW_TEXT:
432
0
            case SID_DRAW_TEXT_MARQUEE:
433
0
            case SID_DRAW_CAPTION:
434
0
                rSet.Put( SfxBoolItem( nWhich, nDrawSfxId == nWhich ) );
435
0
                break;
436
437
0
            case SID_DRAW_TEXT_VERTICAL:
438
0
            case SID_DRAW_CAPTION_VERTICAL:
439
0
                if ( !SvtCJKOptions::IsVerticalTextEnabled() )
440
0
                    rSet.DisableItem( nWhich );
441
0
                else
442
0
                    rSet.Put( SfxBoolItem( nWhich, nDrawSfxId == nWhich ) );
443
0
                break;
444
445
0
            case SID_OBJECT_SELECT:     // important for the old control-controller
446
0
                rSet.Put( SfxBoolItem( nWhich, nDrawSfxId == SID_OBJECT_SELECT && IsDrawSelMode() ) );
447
0
                break;
448
449
0
            case SID_DRAWTBX_CS_BASIC:
450
0
            case SID_DRAWTBX_CS_SYMBOL:
451
0
            case SID_DRAWTBX_CS_ARROW:
452
0
            case SID_DRAWTBX_CS_FLOWCHART:
453
0
            case SID_DRAWTBX_CS_CALLOUT:
454
0
            case SID_DRAWTBX_CS_STAR:
455
0
                rSet.Put( SfxStringItem( nWhich, nDrawSfxId == nWhich ? sDrawCustom : OUString() ) );
456
0
            break;
457
0
        }
458
0
        nWhich = aIter.NextWhich();
459
0
    }
460
0
}
461
462
bool ScTabViewShell::SelectObject( std::u16string_view rName )
463
0
{
464
0
    ScDrawView* pView = GetViewData().GetScDrawView();
465
0
    if (!pView)
466
0
        return false;
467
468
0
    bool bFound = pView->SelectObject( rName );
469
    // DrawShell etc. is handled in MarkListHasChanged
470
471
0
    return bFound;
472
0
}
473
474
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */