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