1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_svx.hxx"
26 
27 #include <svx/tbxcolorupdate.hxx>
28 #include <svx/svxids.hrc>
29 
30 #include <vcl/toolbox.hxx>
31 #include <vcl/bmpacc.hxx>
32 #include <tools/debug.hxx>
33 
34 #define IMAGE_COL_TRANSPARENT		COL_LIGHTMAGENTA
35 
36 //........................................................................
37 namespace svx
38 {
39 //........................................................................
40 
41     //====================================================================
42     //= ToolboxButtonColorUpdater
43     //====================================================================
44 
ToolboxButtonColorUpdater(sal_uInt16 nId,sal_uInt16 nTbxBtnId,ToolBox * ptrTbx,sal_uInt16 nMode)45     ToolboxButtonColorUpdater::ToolboxButtonColorUpdater(
46         sal_uInt16 nId,
47         sal_uInt16 nTbxBtnId,
48         ToolBox* ptrTbx,
49         sal_uInt16 nMode ) :
50         mnDrawMode        ( nMode ),
51         mnBtnId           ( nTbxBtnId ),
52         mnSlotId           ( nId ),
53         mpTbx             ( ptrTbx ),
54         maCurColor        ( COL_TRANSPARENT )
55     {
56         if (mnSlotId == SID_BACKGROUND_COLOR)
57             mnDrawMode = TBX_UPDATER_MODE_CHAR_COLOR_NEW;
58         DBG_ASSERT( ptrTbx, "ToolBox not found :-(" );
59         mbWasHiContrastMode = ptrTbx ? ( ptrTbx->GetSettings().GetStyleSettings().GetHighContrastMode() ) : sal_False;
60         Update(mnSlotId == SID_ATTR_CHAR_COLOR2 ? COL_BLACK : COL_GRAY);
61     }
62 
63     // -----------------------------------------------------------------------
64 
~ToolboxButtonColorUpdater()65     ToolboxButtonColorUpdater::~ToolboxButtonColorUpdater()
66     {
67     }
68 
69     // -----------------------------------------------------------------------
70 
Update(const Color & rColor)71     void ToolboxButtonColorUpdater::Update( const Color& rColor )
72     {
73         Image       aImage( mpTbx->GetItemImage( mnBtnId ) );
74         const bool  bSizeChanged = ( maBmpSize != aImage.GetSizePixel() );
75         const bool  bDisplayModeChanged = ( mbWasHiContrastMode != mpTbx->GetSettings().GetStyleSettings().GetHighContrastMode() );
76         Color       aColor( rColor );
77 
78         // !!! #109290# Workaround for SetFillColor with COL_AUTO
79         if( aColor.GetColor() == COL_AUTO )
80             aColor = Color( COL_TRANSPARENT );
81 
82         if( ( maCurColor != aColor ) || bSizeChanged || bDisplayModeChanged )
83         {
84             BitmapEx            aBmpEx( aImage.GetBitmapEx() );
85             Bitmap              aBmp( aBmpEx.GetBitmap() );
86             BitmapWriteAccess*  pBmpAcc = aBmp.IsEmpty() ? NULL : aBmp.AcquireWriteAccess();
87 
88             maBmpSize = aBmp.GetSizePixel();
89 
90             if( pBmpAcc )
91             {
92                 Bitmap              aMsk;
93                 BitmapWriteAccess*  pMskAcc;
94                 const Point         aNullPnt;
95 
96                 if( aBmpEx.IsAlpha() )
97                     pMskAcc = ( aMsk = aBmpEx.GetAlpha().GetBitmap() ).AcquireWriteAccess();
98                 else if( aBmpEx.IsTransparent() )
99                     pMskAcc = ( aMsk = aBmpEx.GetMask() ).AcquireWriteAccess();
100                 else
101                     pMskAcc = NULL;
102 
103                 mbWasHiContrastMode = mpTbx->GetSettings().GetStyleSettings().GetHighContrastMode();
104 
105                 if( mnDrawMode == TBX_UPDATER_MODE_CHAR_COLOR_NEW && ( COL_TRANSPARENT != aColor.GetColor() ) )
106                     pBmpAcc->SetLineColor( aColor );
107                 else if( mpTbx->GetBackground().GetColor().IsDark() )
108                     pBmpAcc->SetLineColor( Color( COL_WHITE ) );
109                 else
110                     pBmpAcc->SetLineColor( Color( COL_BLACK ) );
111 
112                 // use not only COL_TRANSPARENT for detection of transparence,
113                 // but the method/way which is designed to do that
114                 const bool bIsTransparent(0xff == aColor.GetTransparency());
115                 maCurColor = aColor;
116 
117                 if(bIsTransparent)
118                 {
119                     pBmpAcc->SetFillColor();
120                 }
121                 else
122                 {
123                     pBmpAcc->SetFillColor(maCurColor);
124                 }
125 
126                 if( TBX_UPDATER_MODE_CHAR_COLOR_NEW == mnDrawMode || TBX_UPDATER_MODE_NONE == mnDrawMode )
127                 {
128                     if( TBX_UPDATER_MODE_CHAR_COLOR_NEW == mnDrawMode )
129                     {
130                         if( maBmpSize.Width() <= 16 )
131                             maUpdRect = Rectangle( Point( 0,12 ), Size( maBmpSize.Width(), 4 ) );
132                         else if(76 == maBmpSize.Width() && 12 == maBmpSize.Height())
133                         {
134                             maUpdRect.Left() = 22;
135                             maUpdRect.Top() = 2;
136                             maUpdRect.Right() = 73;
137                             maUpdRect.Bottom() = 9;
138                         }
139                         else if(30 == maBmpSize.Width() && 16 == maBmpSize.Height())
140                         {
141                             maUpdRect.Left() = 17;
142                             maUpdRect.Top() = 2;
143                             maUpdRect.Right() = 27;
144                             maUpdRect.Bottom() = 13;
145                         }
146                         else
147                             maUpdRect = Rectangle( Point( 1, maBmpSize.Height() - 7 ), Size( maBmpSize.Width() - 2 ,6 ) );
148                     }
149                     else
150                     {
151                         if( maBmpSize.Width() <= 16 )
152                             maUpdRect = Rectangle( Point( 7, 7 ), Size( 8, 8 ) );
153                         else
154                             maUpdRect = Rectangle( Point( maBmpSize.Width() - 12, maBmpSize.Height() - 12 ), Size( 11, 11 ) );
155                     }
156 
157                     pBmpAcc->DrawRect( maUpdRect );
158 
159                     if( pMskAcc )
160                     {
161                         if( bIsTransparent )
162                         {
163                             pMskAcc->SetLineColor( COL_BLACK );
164                             pMskAcc->SetFillColor( COL_WHITE );
165                         }
166                         else
167                             pMskAcc->SetFillColor( COL_BLACK );
168 
169                         pMskAcc->DrawRect( maUpdRect );
170                     }
171                 }
172                 else
173                 {
174                     DBG_ERROR( "ToolboxButtonColorUpdater::Update: TBX_UPDATER_MODE_CHAR_COLOR / TBX_UPDATER_MODE_CHAR_BACKGROUND" );
175                     // !!! DrawChar( aVirDev, aColor );
176                 }
177 
178                 aBmp.ReleaseAccess( pBmpAcc );
179 
180                 if( pMskAcc )
181                     aMsk.ReleaseAccess( pMskAcc );
182 
183                 if( aBmpEx.IsAlpha() )
184                     aBmpEx = BitmapEx( aBmp, AlphaMask( aMsk ) );
185                 else if( aBmpEx.IsTransparent() )
186                     aBmpEx = BitmapEx( aBmp, aMsk );
187                 else
188                     aBmpEx = aBmp;
189 
190                 mpTbx->SetItemImage( mnBtnId, Image( aBmpEx ) );
191             }
192         }
193     }
194 
195     // -----------------------------------------------------------------------
196 
DrawChar(VirtualDevice & rVirDev,const Color & rCol)197     void ToolboxButtonColorUpdater::DrawChar( VirtualDevice& rVirDev, const Color& rCol )
198     {
199         Font aOldFont = rVirDev.GetFont();
200         Font aFont = aOldFont;
201         Size aSz = aFont.GetSize();
202         aSz.Height() = maBmpSize.Height();
203         aFont.SetSize( aSz );
204         aFont.SetWeight( WEIGHT_BOLD );
205 
206         if ( mnDrawMode == TBX_UPDATER_MODE_CHAR_COLOR )
207         {
208             aFont.SetColor( rCol );
209             aFont.SetFillColor( Color( IMAGE_COL_TRANSPARENT ) );
210         }
211         else
212         {
213             rVirDev.SetLineColor();
214             rVirDev.SetFillColor( rCol );
215             Rectangle aRect( Point(0,0), maBmpSize );
216             rVirDev.DrawRect( aRect );
217             aFont.SetFillColor( rCol );
218         }
219         rVirDev.SetFont( aFont );
220         Size aTxtSize(rVirDev.GetTextWidth( 'A' ), rVirDev.GetTextHeight());
221         Point aPos( ( maBmpSize.Width() - aTxtSize.Width() ) / 2,
222                     ( maBmpSize.Height() - aTxtSize.Height() ) / 2 );
223 
224         rVirDev.DrawText( aPos, 'A' );
225         rVirDev.SetFont( aOldFont );
226     }
227 
228 //........................................................................
229 } // namespace svx
230 //........................................................................
231