xref: /AOO41X/main/svx/source/tbxctrls/tbxcolorupdate.cxx (revision 6da5f31158a7dd09f46f041b4f15bb7ae3eb92a4)
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 
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 
65     ToolboxButtonColorUpdater::~ToolboxButtonColorUpdater()
66     {
67     }
68 
69     // -----------------------------------------------------------------------
70 
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.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                 pBmpAcc->SetFillColor( maCurColor = aColor );
113 
114                 if( TBX_UPDATER_MODE_CHAR_COLOR_NEW == mnDrawMode || TBX_UPDATER_MODE_NONE == mnDrawMode )
115                 {
116                     if( TBX_UPDATER_MODE_CHAR_COLOR_NEW == mnDrawMode )
117                     {
118                         if( maBmpSize.Width() <= 16 )
119                             maUpdRect = Rectangle( Point( 0,12 ), Size( maBmpSize.Width(), 4 ) );
120                         else
121                             maUpdRect = Rectangle( Point( 1, maBmpSize.Height() - 7 ), Size( maBmpSize.Width() - 2 ,6 ) );
122                     }
123                     else
124                     {
125                         if( maBmpSize.Width() <= 16 )
126                             maUpdRect = Rectangle( Point( 7, 7 ), Size( 8, 8 ) );
127                         else
128                             maUpdRect = Rectangle( Point( maBmpSize.Width() - 12, maBmpSize.Height() - 12 ), Size( 11, 11 ) );
129                     }
130 
131                     pBmpAcc->DrawRect( maUpdRect );
132 
133                     if( pMskAcc )
134                     {
135                         if( COL_TRANSPARENT == aColor.GetColor() )
136                         {
137                             pMskAcc->SetLineColor( COL_BLACK );
138                             pMskAcc->SetFillColor( COL_WHITE );
139                         }
140                         else
141                             pMskAcc->SetFillColor( COL_BLACK );
142 
143                         pMskAcc->DrawRect( maUpdRect );
144                     }
145                 }
146                 else
147                 {
148                     DBG_ERROR( "ToolboxButtonColorUpdater::Update: TBX_UPDATER_MODE_CHAR_COLOR / TBX_UPDATER_MODE_CHAR_BACKGROUND" );
149                     // !!! DrawChar( aVirDev, aColor );
150                 }
151 
152                 aBmp.ReleaseAccess( pBmpAcc );
153 
154                 if( pMskAcc )
155                     aMsk.ReleaseAccess( pMskAcc );
156 
157                 if( aBmpEx.IsAlpha() )
158                     aBmpEx = BitmapEx( aBmp, AlphaMask( aMsk ) );
159                 else if( aBmpEx.IsTransparent() )
160                     aBmpEx = BitmapEx( aBmp, aMsk );
161                 else
162                     aBmpEx = aBmp;
163 
164                 mpTbx->SetItemImage( mnBtnId, Image( aBmpEx ) );
165             }
166         }
167     }
168 
169     // -----------------------------------------------------------------------
170 
171     void ToolboxButtonColorUpdater::DrawChar( VirtualDevice& rVirDev, const Color& rCol )
172     {
173         Font aOldFont = rVirDev.GetFont();
174         Font aFont = aOldFont;
175         Size aSz = aFont.GetSize();
176         aSz.Height() = maBmpSize.Height();
177         aFont.SetSize( aSz );
178         aFont.SetWeight( WEIGHT_BOLD );
179 
180         if ( mnDrawMode == TBX_UPDATER_MODE_CHAR_COLOR )
181         {
182             aFont.SetColor( rCol );
183             aFont.SetFillColor( Color( IMAGE_COL_TRANSPARENT ) );
184         }
185         else
186         {
187             rVirDev.SetLineColor();
188             rVirDev.SetFillColor( rCol );
189             Rectangle aRect( Point(0,0), maBmpSize );
190             rVirDev.DrawRect( aRect );
191             aFont.SetFillColor( rCol );
192         }
193         rVirDev.SetFont( aFont );
194         Size aTxtSize(rVirDev.GetTextWidth( 'A' ), rVirDev.GetTextHeight());
195         Point aPos( ( maBmpSize.Width() - aTxtSize.Width() ) / 2,
196                     ( maBmpSize.Height() - aTxtSize.Height() ) / 2 );
197 
198         rVirDev.DrawText( aPos, 'A' );
199         rVirDev.SetFont( aOldFont );
200     }
201 
202 //........................................................................
203 } // namespace svx
204 //........................................................................
205