Line data Source code
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 :
21 : #include <svx/tbxcolorupdate.hxx>
22 : #include <svx/svxids.hrc>
23 : #include <svx/xdef.hxx>
24 :
25 : #include <vcl/toolbox.hxx>
26 : #include <vcl/bmpacc.hxx>
27 : #include <vcl/settings.hxx>
28 : #include <tools/debug.hxx>
29 :
30 : namespace svx
31 : {
32 : //= ToolboxButtonColorUpdater
33 :
34 : /* Note:
35 : The initial color shown on the button is set in /core/svx/source/tbxctrls/tbxcolorupdate.cxx
36 : (ToolboxButtonColorUpdater::ToolboxButtonColorUpdater()) .
37 : The initial color used by the button is set in /core/svx/source/tbxctrls/tbcontrl.cxx
38 : (SvxColorToolBoxControl::SvxColorToolBoxControl())
39 : and in case of writer for text(background)color also in /core/sw/source/uibase/docvw/edtwin.cxx
40 : (SwEditWin::m_aTextBackColor and SwEditWin::m_aTextColor)
41 : */
42 :
43 3448 : ToolboxButtonColorUpdater::ToolboxButtonColorUpdater(
44 : sal_uInt16 nId, sal_uInt16 nTbxBtnId, ToolBox* pToolBox)
45 : : mnBtnId(nTbxBtnId)
46 : , mnSlotId(nId)
47 : , mpTbx(pToolBox)
48 3448 : , maCurColor(COL_TRANSPARENT)
49 : {
50 : DBG_ASSERT(pToolBox, "ToolBox not found :-(");
51 3448 : mbWasHiContrastMode = pToolBox && pToolBox->GetSettings().GetStyleSettings().GetHighContrastMode();
52 3448 : switch (mnSlotId)
53 : {
54 : case SID_ATTR_CHAR_COLOR:
55 : case SID_ATTR_CHAR_COLOR2:
56 1323 : Update(COL_RED);
57 1323 : break;
58 : case SID_FRAME_LINECOLOR:
59 306 : Update(COL_BLUE);
60 306 : break;
61 : case SID_ATTR_CHAR_COLOR_BACKGROUND:
62 : case SID_BACKGROUND_COLOR:
63 1666 : Update(COL_YELLOW);
64 1666 : break;
65 : case SID_ATTR_LINE_COLOR:
66 51 : Update(COL_BLACK);
67 51 : break;
68 : case SID_ATTR_FILL_COLOR:
69 101 : Update(COL_DEFAULT_SHAPE_FILLING);
70 101 : break;
71 : default:
72 1 : Update(COL_TRANSPARENT);
73 : }
74 3448 : }
75 :
76 3448 : ToolboxButtonColorUpdater::~ToolboxButtonColorUpdater()
77 3448 : {}
78 :
79 5098 : void ToolboxButtonColorUpdater::Update(const Color& rColor)
80 : {
81 5098 : Image aImage(mpTbx->GetItemImageOriginal(mnBtnId));
82 5098 : Size aItemSize(mpTbx->GetItemContentSize(mnBtnId));
83 :
84 5098 : const bool bSizeChanged = (maBmpSize != aItemSize);
85 5098 : const bool bDisplayModeChanged = (mbWasHiContrastMode != mpTbx->GetSettings().GetStyleSettings().GetHighContrastMode());
86 5098 : Color aColor(rColor);
87 :
88 : // !!! #109290# Workaround for SetFillColor with COL_AUTO
89 5098 : if (aColor.GetColor() == COL_AUTO)
90 1560 : aColor = Color(COL_TRANSPARENT);
91 :
92 : // For a shape selected in 'Draw', when color selected in Sidebar > Line > Color
93 : // is COL_BLACK, then (maCurColor != aColor) becomes 'false', therefore we take
94 : // explicit care of COL_BLACK from the last argument in the condition so that the
95 : // Update() does its routine job appropriately !
96 5098 : if ((maCurColor != aColor) || bSizeChanged || bDisplayModeChanged || (aColor == COL_BLACK))
97 : {
98 : // create an empty bitmap, and copy the original bitmap inside
99 : // (so that it grows in case the original bitmap was smaller)
100 4090 : sal_uInt8 nAlpha = 255;
101 4090 : BitmapEx aBmpEx(Bitmap(aItemSize, 24), AlphaMask(aItemSize, &nAlpha));
102 :
103 8180 : BitmapEx aSource(aImage.GetBitmapEx());
104 4090 : long nWidth = std::min(aItemSize.Width(), aSource.GetSizePixel().Width());
105 4090 : long nHeight = std::min(aItemSize.Height(), aSource.GetSizePixel().Height());
106 :
107 4090 : Rectangle aRect(Point(0, 0), Size(nWidth, nHeight));
108 :
109 4090 : aBmpEx.CopyPixel( aRect, aRect, &aSource );
110 :
111 8180 : Bitmap aBmp( aBmpEx.GetBitmap() );
112 4090 : BitmapWriteAccess* pBmpAcc = aBmp.IsEmpty() ? NULL : aBmp.AcquireWriteAccess();
113 :
114 4090 : maBmpSize = aBmp.GetSizePixel();
115 :
116 4090 : if (pBmpAcc)
117 : {
118 4090 : Bitmap aMsk;
119 : BitmapWriteAccess* pMskAcc;
120 :
121 4090 : if (aBmpEx.IsAlpha())
122 : {
123 4090 : aMsk = aBmpEx.GetAlpha().GetBitmap();
124 4090 : pMskAcc = aMsk.AcquireWriteAccess();
125 : }
126 0 : else if (aBmpEx.IsTransparent())
127 : {
128 0 : aMsk = aBmpEx.GetMask();
129 0 : pMskAcc = aMsk.AcquireWriteAccess();
130 : }
131 : else
132 : {
133 0 : pMskAcc = NULL;
134 : }
135 :
136 4090 : mbWasHiContrastMode = mpTbx->GetSettings().GetStyleSettings().GetHighContrastMode();
137 :
138 4090 : if ((COL_TRANSPARENT != aColor.GetColor()) && (maBmpSize.Width() == maBmpSize.Height()))
139 2756 : pBmpAcc->SetLineColor(aColor);
140 1334 : else if( mpTbx->GetBackground().GetColor().IsDark() )
141 0 : pBmpAcc->SetLineColor(Color(COL_WHITE));
142 : else
143 1334 : pBmpAcc->SetLineColor(Color(COL_BLACK));
144 :
145 : // use not only COL_TRANSPARENT for detection of transparence,
146 : // but the method/way which is designed to do that
147 4090 : const bool bIsTransparent(0xff == aColor.GetTransparency());
148 4090 : maCurColor = aColor;
149 :
150 4090 : if (bIsTransparent)
151 : {
152 640 : pBmpAcc->SetFillColor();
153 : }
154 : else
155 : {
156 3450 : pBmpAcc->SetFillColor(maCurColor);
157 : }
158 :
159 4090 : if (maBmpSize.Width() == maBmpSize.Height())
160 2757 : maUpdRect = Rectangle(Point( 0, maBmpSize.Height() * 3 / 4), Size(maBmpSize.Width(), maBmpSize.Height() / 4));
161 : else
162 1333 : maUpdRect = Rectangle(Point( maBmpSize.Height() + 2, 2), Point(maBmpSize.Width() - 3, maBmpSize.Height() - 3));
163 :
164 4090 : pBmpAcc->DrawRect(maUpdRect);
165 :
166 4090 : if (pMskAcc)
167 : {
168 4090 : if (bIsTransparent)
169 : {
170 640 : pMskAcc->SetLineColor(COL_BLACK);
171 640 : pMskAcc->SetFillColor(COL_WHITE);
172 : }
173 : else
174 3450 : pMskAcc->SetFillColor(COL_BLACK);
175 :
176 4090 : pMskAcc->DrawRect(maUpdRect);
177 : }
178 :
179 4090 : Bitmap::ReleaseAccess(pBmpAcc);
180 :
181 4090 : if (pMskAcc)
182 4090 : Bitmap::ReleaseAccess(pMskAcc);
183 :
184 4090 : if (aBmpEx.IsAlpha())
185 4090 : aBmpEx = BitmapEx(aBmp, AlphaMask(aMsk));
186 0 : else if (aBmpEx.IsTransparent())
187 0 : aBmpEx = BitmapEx(aBmp, aMsk);
188 : else
189 0 : aBmpEx = aBmp;
190 :
191 4090 : mpTbx->SetItemImage(mnBtnId, Image(aBmpEx));
192 4090 : }
193 5098 : }
194 5098 : }
195 : }
196 :
197 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|