summaryrefslogtreecommitdiffstats
path: root/svx/source/tbxctrls/tbxcolorupdate.cxx
blob: 970fa40181af698ba99e884086140f73156128f6 (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
/* -*- 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/sfxbasemodel.hxx>
#include <sfx2/objsh.hxx>
#include <svx/drawitem.hxx>
#include <tbxcolorupdate.hxx>
#include <svx/svxids.hrc>
#include <svx/xdef.hxx>
#include <svx/xlineit0.hxx>
#include <svx/xlndsit.hxx>

#include <utility>
#include <vcl/commandinfoprovider.hxx>
#include <vcl/gdimtf.hxx>
#include <vcl/svapp.hxx>
#include <vcl/toolbox.hxx>
#include <vcl/virdev.hxx>
#include <vcl/weld.hxx>
#include <vcl/settings.hxx>

#include <svx/strings.hrc>
#include <svx/dialmgr.hxx>

namespace svx
{
    ToolboxButtonColorUpdaterBase::ToolboxButtonColorUpdaterBase(bool bWideButton, OUString aCommandLabel,
                                                                 OUString aCommandURL, sal_uInt16 nSlotId,
                                                                 css::uno::Reference<css::frame::XFrame> xFrame)
        : mbWideButton(bWideButton)
        , mbWasHiContrastMode(Application::GetSettings().GetStyleSettings().GetHighContrastMode())
        , mnSlotId(nSlotId)
        , maCurColor(COL_TRANSPARENT)
        , meImageType(vcl::ImageType::Size16)
        , maCommandLabel(std::move(aCommandLabel))
        , maCommandURL(std::move(aCommandURL))
        , mxFrame(std::move(xFrame))
    {
    }

    void ToolboxButtonColorUpdaterBase::Init(sal_uInt16 nSlotId)
    {
        if (mbWideButton)
        {
            Update(COL_TRANSPARENT, true);
            return;
        }

        if (rtl::Reference xModel = dynamic_cast<SfxBaseModel*>(mxFrame->getController()->getModel().get()))
        {
            auto pDocSh = xModel->GetObjectShell();
            StartListening(*pDocSh);
            if (auto oColor = pDocSh->GetRecentColor(nSlotId))
            {
                Update(*oColor);
                return;
            }
        }

        switch (nSlotId)
        {
            case SID_ATTR_CHAR_COLOR:
            case SID_ATTR_CHAR_COLOR2:
                Update(NamedColor(COL_DEFAULT_FONT, SvxResId(RID_SVXSTR_COLOR_DEFAULT_FONT)));
                break;
            case SID_FRAME_LINECOLOR:
                Update(NamedColor(COL_DEFAULT_FRAMELINE, SvxResId(RID_SVXSTR_COLOR_DEFAULT_FRAMELINE)));
                break;
            case SID_ATTR_CHAR_COLOR_BACKGROUND:
            case SID_ATTR_CHAR_BACK_COLOR:
            case SID_BACKGROUND_COLOR:
            case SID_TABLE_CELL_BACKGROUND_COLOR:
                Update(NamedColor(COL_DEFAULT_HIGHLIGHT, SvxResId(RID_SVXSTR_COLOR_DEFAULT_HIGHLIGHT)));
                break;
            case SID_ATTR_LINE_COLOR:
                Update(NamedColor(COL_DEFAULT_SHAPE_STROKE, SvxResId(RID_SVXSTR_COLOR_DEFAULT_SHAPE_STROKE)));
                break;
            case SID_ATTR_FILL_COLOR:
                Update(NamedColor(COL_DEFAULT_SHAPE_FILLING, SvxResId(RID_SVXSTR_COLOR_DEFAULT_SHAPE_FILLING)));
                break;
            default:
                Update(COL_TRANSPARENT);
        }
    }

    void ToolboxButtonColorUpdaterBase::Notify(SfxBroadcaster& rBC, const SfxHint& rHint)
    {
        if (rHint.GetId() == SfxHintId::Dying)
        {
            EndListeningAll();
        }
        else if (rHint.GetId() == SfxHintId::ColorsChanged)
        {
            if (auto oColor = static_cast<SfxObjectShell&>(rBC).GetRecentColor(mnSlotId))
                Update(*oColor);
        }
    }

    void ToolboxButtonColorUpdaterBase::SetRecentColor(const NamedColor &rNamedColor)
    {
        if (rtl::Reference xModel = dynamic_cast<SfxBaseModel*>(mxFrame->getController()->getModel().get()))
            xModel->GetObjectShell()->SetRecentColor(mnSlotId, rNamedColor);
        else if (!mbWideButton)
            Update(rNamedColor);
    }

    VclToolboxButtonColorUpdater::VclToolboxButtonColorUpdater(
            sal_uInt16 nSlotId, ToolBoxItemId nTbxBtnId, ToolBox* pToolBox, bool bWideButton,
            const OUString& rCommandLabel, const OUString& rCommandURL,
            const css::uno::Reference<css::frame::XFrame>& rFrame)
        : ToolboxButtonColorUpdaterBase(bWideButton, rCommandLabel, rCommandURL, nSlotId, rFrame)
        , mnBtnId(nTbxBtnId)
        , mpTbx(pToolBox)
    {
        Init(nSlotId);
    }

    void VclToolboxButtonColorUpdater::SetQuickHelpText(const OUString& rText)
    {
        mpTbx->SetQuickHelpText(mnBtnId, rText);
    }

    OUString VclToolboxButtonColorUpdater::GetQuickHelpText() const
    {
        return mpTbx->GetQuickHelpText(mnBtnId);
    }

    void VclToolboxButtonColorUpdater::SetImage(VirtualDevice* pVirDev)
    {
        GDIMetaFile* pMtf = pVirDev->GetConnectMetaFile();

        assert(pMtf && "should have been set in ToolboxButtonColorUpdaterBase::Update");

        pMtf->Stop();
        pMtf->WindStart();

        Graphic aGraphic(*pMtf);

        mpTbx->SetItemImage(mnBtnId, Image(aGraphic.GetXGraphic()));
    }

    VclPtr<VirtualDevice> VclToolboxButtonColorUpdater::CreateVirtualDevice() const
    {
        return VclPtr<VirtualDevice>::Create(*mpTbx->GetOutDev());
    }

    vcl::ImageType VclToolboxButtonColorUpdater::GetImageSize() const
    {
        return mpTbx->GetImageSize();
    }

    Size VclToolboxButtonColorUpdater::GetItemSize(const Size& rImageSize) const
    {
        if (mbWideButton)
            return mpTbx->GetItemContentSize(mnBtnId);
        return rImageSize;
    }

    ToolboxButtonColorUpdaterBase::~ToolboxButtonColorUpdaterBase()
    {}

    void ToolboxButtonColorUpdaterBase::Update(const NamedColor &rNamedColor)
    {
        Update(rNamedColor.m_aColor);

        // Also show the current color as QuickHelpText
        OUString colorSuffix = OUString(" (%1)").replaceFirst("%1", rNamedColor.m_aName);
        OUString colorHelpText = maCommandLabel + colorSuffix;
        SetQuickHelpText(colorHelpText);
    }

    void ToolboxButtonColorUpdaterBase::Update(const Color& rColor, bool bForceUpdate)
    {
        vcl::ImageType eImageType = GetImageSize();

#ifdef IOS // tdf#126966
        eImageType = vcl::ImageType::Size32;
#endif

        const bool bSizeChanged = (meImageType != eImageType);
        meImageType = eImageType;
        const bool bDisplayModeChanged = (mbWasHiContrastMode != Application::GetSettings().GetStyleSettings().GetHighContrastMode());
        Color aColor(rColor);

        // !!! #109290# Workaround for SetFillColor with COL_AUTO
        if (aColor == COL_AUTO)
            aColor = COL_TRANSPARENT;

        if ((maCurColor == aColor) && !bSizeChanged && !bDisplayModeChanged && !bForceUpdate)
            return;

        auto xImage = vcl::CommandInfoProvider::GetXGraphicForCommand(maCommandURL, mxFrame, meImageType);
        Image aImage(xImage);

        Size aItemSize = GetItemSize(aImage.GetSizePixel());
        if (!aItemSize.Width() || !aItemSize.Height())
            return;

        ScopedVclPtr<VirtualDevice> pVirDev(CreateVirtualDevice());
        pVirDev->SetOutputSizePixel(aItemSize, /*bErase*/true, /*bAlphaMaskTransparent*/true);
        maBmpSize = aItemSize;

        std::unique_ptr<GDIMetaFile> xMetaFile;
        if (RecordVirtualDevice())
        {
            xMetaFile.reset(new GDIMetaFile);
            xMetaFile->SetPrefSize(pVirDev->GetOutputSize());
            xMetaFile->SetPrefMapMode(pVirDev->GetMapMode());
            xMetaFile->Record(pVirDev.get());
            pVirDev->EnableOutput(false);
        }

        if (maBmpSize.Width() == maBmpSize.Height())
            // tdf#84985 align color bar with icon bottom edge; integer arithmetic e.g. 26 - 26/4 <> 26 * 3/4
            maUpdRect = tools::Rectangle(Point( 0, maBmpSize.Height() - maBmpSize.Height() / 4), Size(maBmpSize.Width(), maBmpSize.Height() / 4));
        else
            maUpdRect = tools::Rectangle(Point( maBmpSize.Height() + 2, 2), Point(maBmpSize.Width() - 3, maBmpSize.Height() - 3));

        pVirDev->Push(vcl::PushFlags::CLIPREGION);

        // tdf#135121 don't include the part of the image which we will
        // overwrite with the target color so that for the transparent color
        // case the original background of the device is shown
        vcl::Region aRegion(tools::Rectangle(Point(0, 0), maBmpSize));
        aRegion.Exclude(maUpdRect);
        pVirDev->SetClipRegion(aRegion);

        pVirDev->DrawImage(Point(0, 0), aImage);

        pVirDev->Pop();

        const StyleSettings& rStyleSettings = Application::GetSettings().GetStyleSettings();
        mbWasHiContrastMode = rStyleSettings.GetHighContrastMode();

        if ((COL_TRANSPARENT != aColor) && (maBmpSize.Width() == maBmpSize.Height()))
            pVirDev->SetLineColor(aColor);
        else
            pVirDev->SetLineColor(rStyleSettings.GetDisableColor());

        // use not only COL_TRANSPARENT for detection of transparence,
        // but the method/way which is designed to do that
        const bool bIsFullyTransparent(aColor.IsFullyTransparent());
        maCurColor = aColor;

        if (bIsFullyTransparent)
        {
            pVirDev->SetFillColor();
        }
        else
        {
            pVirDev->SetFillColor(maCurColor);
        }

        pVirDev->DrawRect(maUpdRect);

        SetImage(pVirDev.get());
    }

    OUString ToolboxButtonColorUpdaterBase::GetCurrentColorName() const
    {
        OUString sColorName = GetQuickHelpText();
        // The obtained string is of format: color context (color name)
        // Generate a substring which contains only the color name
        sal_Int32 nStart = sColorName.indexOf('(');
        sColorName = sColorName.copy( nStart + 1 );
        sal_Int32 nLength = sColorName.getLength();
        if(nLength > 0)
            sColorName = sColorName.copy( 0, nLength - 1);
        return sColorName;
    }

    ToolboxButtonColorUpdater::ToolboxButtonColorUpdater(sal_uInt16 nSlotId, const OUString& rTbxBtnId, weld::Toolbar* ptrTbx, bool bWideButton,
                                                         const OUString& rCommandLabel, const css::uno::Reference<css::frame::XFrame>& rFrame)
        : ToolboxButtonColorUpdaterBase(bWideButton, rCommandLabel, rTbxBtnId, nSlotId, rFrame)
        , msBtnId(rTbxBtnId)
        , mpTbx(ptrTbx)
    {
        Init(nSlotId);
    }

    void ToolboxButtonColorUpdater::SetQuickHelpText(const OUString& rText)
    {
        mpTbx->set_item_tooltip_text(msBtnId, rText);
    }

    OUString ToolboxButtonColorUpdater::GetQuickHelpText() const
    {
        return mpTbx->get_item_tooltip_text(msBtnId);
    }

    void ToolboxButtonColorUpdater::SetImage(VirtualDevice* pVirDev)
    {
        mpTbx->set_item_image(msBtnId, pVirDev);
    }

    VclPtr<VirtualDevice> ToolboxButtonColorUpdater::CreateVirtualDevice() const
    {
        return mpTbx->create_virtual_device();
    }

    vcl::ImageType ToolboxButtonColorUpdater::GetImageSize() const
    {
        return mpTbx->get_icon_size();
    }

    Size ToolboxButtonColorUpdater::GetItemSize(const Size& rImageSize) const
    {
        auto nWidth = rImageSize.Width();
        if (mbWideButton)
            nWidth = nWidth * 5;
        return Size(nWidth, rImageSize.Height());
    }

    ToolboxButtonLineStyleUpdater::ToolboxButtonLineStyleUpdater()
        : m_eXLS(css::drawing::LineStyle_NONE)
        , m_nDashStyleIndex(-1)
    {
    }

    void ToolboxButtonLineStyleUpdater::Update(const com::sun::star::frame::FeatureStateEvent& rEvent)
    {
        if (rEvent.FeatureURL.Complete == ".uno:LineDash")
        {
            m_nDashStyleIndex = -1;

            SfxObjectShell* pSh = SfxObjectShell::Current();
            if (!pSh)
                return;
            const SvxDashListItem* pItem = pSh->GetItem( SID_DASH_LIST );
            if (!pItem)
                return;

            XLineDashItem aDashItem;
            aDashItem.PutValue(rEvent.State, 0);
            const XDash& rDash = aDashItem.GetDashValue();

            XDashListRef xLineStyleList = pItem->GetDashList();
            for (tools::Long i = 0; i < xLineStyleList->Count(); ++i)
            {
                const XDashEntry* pEntry = xLineStyleList->GetDash(i);
                const XDash& rEntry = pEntry->GetDash();
                if (rDash == rEntry)
                {
                    m_nDashStyleIndex = i;
                    break;
                }
            }
        }
        else if (rEvent.FeatureURL.Complete == ".uno:XLineStyle")
        {
            XLineStyleItem aLineStyleItem;
            aLineStyleItem.PutValue(rEvent.State, 0);

            m_eXLS = aLineStyleItem.GetValue();
        }
    }

    int ToolboxButtonLineStyleUpdater::GetStyleIndex() const
    {
        int nRet;
        switch (m_eXLS)
        {
            case css::drawing::LineStyle_NONE:
                nRet = 0;
                break;
            case css::drawing::LineStyle_SOLID:
                nRet = 1;
                break;
            default:
                nRet = m_nDashStyleIndex + 2;
                break;
        }
        return nRet;
    }
}

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