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