summaryrefslogtreecommitdiff
path: root/sw/source/uibase/shells/drawdlg.cxx
blob: b18ce9327b68c8116c747e3c3397db06ca9da6b3 (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
/* -*- 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 <sfx2/request.hxx>
#include <sfx2/dispatch.hxx>
#include <sfx2/viewfrm.hxx>
#include <svx/svdview.hxx>

#include <view.hxx>
#include <wrtsh.hxx>
#include <cmdid.h>

#include <drawsh.hxx>
#include <svx/svxdlg.hxx>
#include <svx/dialogs.hrc>
#include <memory>
#include <svl/stritem.hxx>
#include <svx/xlnclit.hxx>
#include <svx/xflclit.hxx>
#include <svx/chrtitem.hxx>
#include <svx/xlnwtit.hxx>
#include <svx/xflgrit.hxx>
#include <svx/xflftrit.hxx>
#include <svx/xfltrit.hxx>
#include <comphelper/lok.hxx>
#include <textboxhelper.hxx>

using namespace com::sun::star::drawing;

void SwDrawShell::ExecDrawDlg(SfxRequest& rReq)
{
    SwWrtShell*     pSh     = &GetShell();
    SdrView*        pView   = pSh->GetDrawView();
    SdrModel*       pDoc    = pView->GetModel();
    bool            bChanged = pDoc->IsChanged();
    pDoc->SetChanged(false);

    SfxItemSet aNewAttr( pDoc->GetItemPool() );
    pView->GetAttributes( aNewAttr );

    GetView().NoRotate();

    switch (rReq.GetSlot())
    {
        case FN_DRAWTEXT_ATTR_DLG:
        {
            SvxAbstractDialogFactory* pFact = SvxAbstractDialogFactory::Create();
            ScopedVclPtr<SfxAbstractTabDialog> pDlg(pFact->CreateTextTabDialog(rReq.GetFrameWeld(), &aNewAttr, pView));
            sal_uInt16 nResult = pDlg->Execute();

            if (nResult == RET_OK)
            {
                if (pView->AreObjectsMarked())
                {
                    pSh->StartAction();
                    pView->SetAttributes(*pDlg->GetOutputItemSet());
                    auto vMarkedObjs = pView->GetMarkedObjects();
                    for (auto pObj : vMarkedObjs)
                    {
                        // If the shape has textframe, set its params as well.
                        if (SwTextBoxHelper::hasTextFrame(pObj))
                            SwTextBoxHelper::updateTextBoxMargin(pObj);
                    }
                    rReq.Done(*(pDlg->GetOutputItemSet()));
                    pSh->EndAction();
                }
            }
        }
        break;

        case SID_MEASURE_DLG:
        {
            bool bHasMarked = pView->AreObjectsMarked();

            SvxAbstractDialogFactory* pFact = SvxAbstractDialogFactory::Create();
            ScopedVclPtr<SfxAbstractDialog> pDlg(pFact->CreateSfxDialog(rReq.GetFrameWeld(),
                                                 aNewAttr, pView, RID_SVXPAGE_MEASURE));
            if (pDlg->Execute() == RET_OK)
            {
                pSh->StartAction();
                if (bHasMarked)
                    pView->SetAttrToMarked(*pDlg->GetOutputItemSet(), false);
                else
                    pView->SetDefaultAttr(*pDlg->GetOutputItemSet(), false);
                pSh->EndAction();
            }
        }
        break;

        case SID_ATTRIBUTES_AREA:
        {
            bool bHasMarked = pView->AreObjectsMarked();

            SvxAbstractDialogFactory* pFact = SvxAbstractDialogFactory::Create();
            VclPtr<AbstractSvxAreaTabDialog> pDlg(pFact->CreateSvxAreaTabDialog(rReq.GetFrameWeld(),
                                                                            &aNewAttr,
                                                                            pDoc,
                                                                            true));

            pDlg->StartExecuteAsync([bChanged, bHasMarked, pDoc, pDlg, pSh, pView, this](
                                        sal_Int32 nResult){
                if (nResult == RET_OK)
                {
                    pSh->StartAction();
                    if (bHasMarked)
                        pView->SetAttributes(*pDlg->GetOutputItemSet());
                    else
                        pView->SetDefaultAttr(*pDlg->GetOutputItemSet(), false);
                    pSh->EndAction();

                    static sal_uInt16 aInval[] =
                    {
                        SID_ATTR_FILL_STYLE,
                        SID_ATTR_FILL_COLOR,
                        SID_ATTR_FILL_TRANSPARENCE,
                        SID_ATTR_FILL_FLOATTRANSPARENCE,
                        0
                    };
                    SfxBindings &rBnd = GetView().GetViewFrame()->GetBindings();
                    rBnd.Invalidate(aInval);
                    rBnd.Update(SID_ATTR_FILL_STYLE);
                    rBnd.Update(SID_ATTR_FILL_COLOR);
                    rBnd.Update(SID_ATTR_FILL_TRANSPARENCE);
                    rBnd.Update(SID_ATTR_FILL_FLOATTRANSPARENCE);
                }

                if (pDoc->IsChanged())
                    GetShell().SetModified();
                else if (bChanged)
                    pDoc->SetChanged();

                pDlg->disposeOnce();
            });
        }
        break;

        case SID_ATTRIBUTES_LINE:
        {
            bool bHasMarked = pView->AreObjectsMarked();

            const SdrObject* pObj = nullptr;
            const SdrMarkList& rMarkList = pView->GetMarkedObjectList();
            if( rMarkList.GetMarkCount() == 1 )
                pObj = rMarkList.GetMark(0)->GetMarkedSdrObj();

            SvxAbstractDialogFactory* pFact = SvxAbstractDialogFactory::Create();
            VclPtr<SfxAbstractTabDialog> pDlg(pFact->CreateSvxLineTabDialog(rReq.GetFrameWeld(),
                    &aNewAttr,
                pDoc,
                pObj,
                bHasMarked));

            pDlg->StartExecuteAsync([bChanged, bHasMarked, pDoc, pDlg, pSh, pView, this](
                                        sal_Int32 nResult){
                if (nResult == RET_OK)
                {
                    pSh->StartAction();
                    if(bHasMarked)
                        pView->SetAttrToMarked(*pDlg->GetOutputItemSet(), false);
                    else
                        pView->SetDefaultAttr(*pDlg->GetOutputItemSet(), false);
                    pSh->EndAction();

                    static sal_uInt16 aInval[] =
                    {
                        SID_ATTR_LINE_STYLE,                // ( SID_SVX_START + 169 )
                        SID_ATTR_LINE_DASH,                 // ( SID_SVX_START + 170 )
                        SID_ATTR_LINE_WIDTH,                // ( SID_SVX_START + 171 )
                        SID_ATTR_LINE_COLOR,                // ( SID_SVX_START + 172 )
                        SID_ATTR_LINE_START,                // ( SID_SVX_START + 173 )
                        SID_ATTR_LINE_END,                  // ( SID_SVX_START + 174 )
                        SID_ATTR_LINE_TRANSPARENCE,         // (SID_SVX_START+1107)
                        SID_ATTR_LINE_JOINT,                // (SID_SVX_START+1110)
                        SID_ATTR_LINE_CAP,                  // (SID_SVX_START+1111)
                        0
                    };

                    GetView().GetViewFrame()->GetBindings().Invalidate(aInval);
                }

                if (pDoc->IsChanged())
                    GetShell().SetModified();
                else if (bChanged)
                    pDoc->SetChanged();

                pDlg->disposeOnce();
            });
        }
        break;

        default:
            break;
    }

    if (pDoc->IsChanged())
        GetShell().SetModified();
    else
        if (bChanged)
            pDoc->SetChanged();
}

namespace
{
    void lcl_convertStringArguments(sal_uInt16 nSlot, const std::unique_ptr<SfxItemSet>& pArgs)
    {
        Color aColor;
        const SfxPoolItem* pItem = nullptr;

        if (SfxItemState::SET == pArgs->GetItemState(SID_ATTR_COLOR_STR, false, &pItem))
        {
            OUString sColor = static_cast<const SfxStringItem*>(pItem)->GetValue();

            if (sColor == "transparent")
                aColor = COL_TRANSPARENT;
            else
                aColor = Color(ColorTransparency, sColor.toInt32(16));

            switch (nSlot)
            {
                case SID_ATTR_LINE_COLOR:
                {
                    XLineColorItem aLineColorItem(OUString(), aColor);
                    pArgs->Put(aLineColorItem);
                    break;
                }

                case SID_ATTR_FILL_COLOR:
                {
                    XFillColorItem aFillColorItem(OUString(), aColor);
                    pArgs->Put(aFillColorItem);
                    break;
                }
            }
        }
        else if (SfxItemState::SET == pArgs->GetItemState(SID_ATTR_LINE_WIDTH_ARG, false, &pItem))
        {
            double fValue = static_cast<const SvxDoubleItem*>(pItem)->GetValue();
            // FIXME: different units...
            int nPow = 100;
            int nValue = fValue * nPow;

            XLineWidthItem aItem(nValue);
            pArgs->Put(aItem);
        }
        if (SfxItemState::SET == pArgs->GetItemState(SID_FILL_GRADIENT_JSON, false, &pItem))
        {
            const SfxStringItem* pJSON = static_cast<const SfxStringItem*>(pItem);
            if (pJSON)
            {
                XGradient aGradient = XGradient::fromJSON(pJSON->GetValue());
                XFillGradientItem aItem(aGradient);
                pArgs->Put(aItem);
            }
        }
    }
}

void SwDrawShell::ExecDrawAttrArgs(SfxRequest const & rReq)
{
    SwWrtShell* pSh   = &GetShell();
    SdrView*    pView = pSh->GetDrawView();
    const SfxItemSet* pArgs = rReq.GetArgs();
    bool        bChanged = pView->GetModel()->IsChanged();
    pView->GetModel()->SetChanged(false);

    GetView().NoRotate();

    if (pArgs)
    {
        if(pView->AreObjectsMarked())
        {
            std::unique_ptr<SfxItemSet> pNewArgs = pArgs->Clone();
            lcl_convertStringArguments(rReq.GetSlot(), pNewArgs);
            pView->SetAttrToMarked(*pNewArgs, false);
        }
        else
            pView->SetDefaultAttr(*rReq.GetArgs(), false);
    }
    else
    {
        SfxDispatcher* pDis = pSh->GetView().GetViewFrame()->GetDispatcher();
        switch (rReq.GetSlot())
        {
            case SID_ATTR_FILL_STYLE:
            case SID_ATTR_FILL_COLOR:
            case SID_ATTR_FILL_GRADIENT:
            case SID_ATTR_FILL_HATCH:
            case SID_ATTR_FILL_BITMAP:
            case SID_ATTR_FILL_TRANSPARENCE:
            case SID_ATTR_FILL_FLOATTRANSPARENCE:
                pDis->Execute(SID_ATTRIBUTES_AREA);
                break;
            case SID_ATTR_LINE_STYLE:
            case SID_ATTR_LINE_DASH:
            case SID_ATTR_LINE_WIDTH:
            case SID_ATTR_LINE_COLOR:
            case SID_ATTR_LINE_TRANSPARENCE:
            case SID_ATTR_LINE_JOINT:
            case SID_ATTR_LINE_CAP:
                pDis->Execute(SID_ATTRIBUTES_LINE);
                break;
        }
    }
    if (pView->GetModel()->IsChanged())
        GetShell().SetModified();
    else
        if (bChanged)
            pView->GetModel()->SetChanged();
}

static void lcl_unifyFillTransparencyItems(const SfxItemSet& rSet)
{
    // Transparent fill options are None, Solid, Linear, Axial, Radial, Elliptical, Quadratic, Square.
    // But this is represented across two items namely XFillTransparenceItem (for None and Solid)
    // and XFillFloatTransparenceItem (for the rest). To simplify the representation in LOKit case let's
    // use XFillFloatTransparenceItem to carry the information of XFillTransparenceItem when gradients
    // are disabled. When gradient transparency is disabled, all fields of XFillFloatTransparenceItem are invalid
    // and not used. So convert XFillTransparenceItem's constant transparency percentage as an intensity
    // and assign this to the XFillFloatTransparenceItem's start-intensity and end-intensity fields.
    // Now the LOK clients need only listen to statechange messages of XFillFloatTransparenceItem
    // to get fill-transparency settings instead of listening to two separate items.

    XFillFloatTransparenceItem* pFillFloatTranspItem =
        const_cast<XFillFloatTransparenceItem*>
        (rSet.GetItem<XFillFloatTransparenceItem>(XATTR_FILLFLOATTRANSPARENCE));
    if (!pFillFloatTranspItem || pFillFloatTranspItem->IsEnabled())
        return;

    const XFillTransparenceItem* pFillTranspItem =
        rSet.GetItem<XFillTransparenceItem>(XATTR_FILLTRANSPARENCE);

    if (!pFillTranspItem)
        return;

    XGradient aTmpGradient = pFillFloatTranspItem->GetGradientValue();
    sal_uInt16 nTranspPercent = pFillTranspItem->GetValue();
    // Encode transparency percentage as intensity
    sal_uInt16 nIntensity = 100 - std::min<sal_uInt16>
        (std::max<sal_uInt16>(nTranspPercent, 0), 100);
    aTmpGradient.SetStartIntens(nIntensity);
    aTmpGradient.SetEndIntens(nIntensity);
    pFillFloatTranspItem->SetGradientValue(aTmpGradient);
}

void SwDrawShell::GetDrawAttrState(SfxItemSet& rSet)
{
    SdrView* pSdrView = GetShell().GetDrawView();

    if (pSdrView->AreObjectsMarked())
    {
        bool bDisable = Disable( rSet );

        if( !bDisable )
        {
            SfxItemSet aSet(rSet);
            aSet.MergeRange(SDRATTR_TEXTCOLUMNS_NUMBER, SDRATTR_TEXTCOLUMNS_SPACING);
            pSdrView->GetAttributes(aSet);
            if (const SfxPoolItem * pItem;
                aSet.GetItemState(SDRATTR_TEXTCOLUMNS_NUMBER, false, &pItem)
                    >= SfxItemState::DEFAULT
                && pItem)
            {
                aSet.Put(pItem->CloneSetWhich(SID_ATTR_TEXTCOLUMNS_NUMBER));
            }
            if (const SfxPoolItem * pItem;
                aSet.GetItemState(SDRATTR_TEXTCOLUMNS_SPACING, false, &pItem)
                    >= SfxItemState::DEFAULT
                && pItem)
            {
                aSet.Put(pItem->CloneSetWhich(SID_ATTR_TEXTCOLUMNS_SPACING));
            }
            rSet.Put(aSet, false);
            if (comphelper::LibreOfficeKit::isActive())
                lcl_unifyFillTransparencyItems(rSet);
        }
    }
    else
        rSet.Put(pSdrView->GetDefaultAttr());
}

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