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 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 if(76 == maBmpSize.Width() && 12 == maBmpSize.Height()) 121 { 122 maUpdRect.Left() = 22; 123 maUpdRect.Top() = 2; 124 maUpdRect.Right() = 73; 125 maUpdRect.Bottom() = 9; 126 } 127 else if(30 == maBmpSize.Width() && 16 == maBmpSize.Height()) 128 { 129 maUpdRect.Left() = 17; 130 maUpdRect.Top() = 2; 131 maUpdRect.Right() = 27; 132 maUpdRect.Bottom() = 13; 133 } 134 else 135 maUpdRect = Rectangle( Point( 1, maBmpSize.Height() - 7 ), Size( maBmpSize.Width() - 2 ,6 ) ); 136 } 137 else 138 { 139 if( maBmpSize.Width() <= 16 ) 140 maUpdRect = Rectangle( Point( 7, 7 ), Size( 8, 8 ) ); 141 else 142 maUpdRect = Rectangle( Point( maBmpSize.Width() - 12, maBmpSize.Height() - 12 ), Size( 11, 11 ) ); 143 } 144 145 pBmpAcc->DrawRect( maUpdRect ); 146 147 if( pMskAcc ) 148 { 149 if( COL_TRANSPARENT == aColor.GetColor() ) 150 { 151 pMskAcc->SetLineColor( COL_BLACK ); 152 pMskAcc->SetFillColor( COL_WHITE ); 153 } 154 else 155 pMskAcc->SetFillColor( COL_BLACK ); 156 157 pMskAcc->DrawRect( maUpdRect ); 158 } 159 } 160 else 161 { 162 DBG_ERROR( "ToolboxButtonColorUpdater::Update: TBX_UPDATER_MODE_CHAR_COLOR / TBX_UPDATER_MODE_CHAR_BACKGROUND" ); 163 // !!! DrawChar( aVirDev, aColor ); 164 } 165 166 aBmp.ReleaseAccess( pBmpAcc ); 167 168 if( pMskAcc ) 169 aMsk.ReleaseAccess( pMskAcc ); 170 171 if( aBmpEx.IsAlpha() ) 172 aBmpEx = BitmapEx( aBmp, AlphaMask( aMsk ) ); 173 else if( aBmpEx.IsTransparent() ) 174 aBmpEx = BitmapEx( aBmp, aMsk ); 175 else 176 aBmpEx = aBmp; 177 178 mpTbx->SetItemImage( mnBtnId, Image( aBmpEx ) ); 179 } 180 } 181 } 182 183 // ----------------------------------------------------------------------- 184 DrawChar(VirtualDevice & rVirDev,const Color & rCol)185 void ToolboxButtonColorUpdater::DrawChar( VirtualDevice& rVirDev, const Color& rCol ) 186 { 187 Font aOldFont = rVirDev.GetFont(); 188 Font aFont = aOldFont; 189 Size aSz = aFont.GetSize(); 190 aSz.Height() = maBmpSize.Height(); 191 aFont.SetSize( aSz ); 192 aFont.SetWeight( WEIGHT_BOLD ); 193 194 if ( mnDrawMode == TBX_UPDATER_MODE_CHAR_COLOR ) 195 { 196 aFont.SetColor( rCol ); 197 aFont.SetFillColor( Color( IMAGE_COL_TRANSPARENT ) ); 198 } 199 else 200 { 201 rVirDev.SetLineColor(); 202 rVirDev.SetFillColor( rCol ); 203 Rectangle aRect( Point(0,0), maBmpSize ); 204 rVirDev.DrawRect( aRect ); 205 aFont.SetFillColor( rCol ); 206 } 207 rVirDev.SetFont( aFont ); 208 Size aTxtSize(rVirDev.GetTextWidth( 'A' ), rVirDev.GetTextHeight()); 209 Point aPos( ( maBmpSize.Width() - aTxtSize.Width() ) / 2, 210 ( maBmpSize.Height() - aTxtSize.Height() ) / 2 ); 211 212 rVirDev.DrawText( aPos, 'A' ); 213 rVirDev.SetFont( aOldFont ); 214 } 215 216 //........................................................................ 217 } // namespace svx 218 //........................................................................ 219