1 /************************************************************************* 2 * 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * Copyright 2000, 2010 Oracle and/or its affiliates. 6 * 7 * OpenOffice.org - a multi-platform office productivity suite 8 * 9 * This file is part of OpenOffice.org. 10 * 11 * OpenOffice.org is free software: you can redistribute it and/or modify 12 * it under the terms of the GNU Lesser General Public License version 3 13 * only, as published by the Free Software Foundation. 14 * 15 * OpenOffice.org is distributed in the hope that it will be useful, 16 * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 * GNU Lesser General Public License version 3 for more details 19 * (a copy is included in the LICENSE file that accompanied this code). 20 * 21 * You should have received a copy of the GNU Lesser General Public License 22 * version 3 along with OpenOffice.org. If not, see 23 * <http://www.openoffice.org/license.html> 24 * for a copy of the LGPLv3 License. 25 * 26 ************************************************************************/ 27 28 // MARKER(update_precomp.py): autogen include statement, do not remove 29 #include "precompiled_svx.hxx" 30 31 #include <svx/tbxcolorupdate.hxx> 32 #include <svx/svxids.hrc> 33 34 #include <vcl/toolbox.hxx> 35 #include <vcl/bmpacc.hxx> 36 #include <tools/debug.hxx> 37 38 #define IMAGE_COL_TRANSPARENT COL_LIGHTMAGENTA 39 40 //........................................................................ 41 namespace svx 42 { 43 //........................................................................ 44 45 //==================================================================== 46 //= ToolboxButtonColorUpdater 47 //==================================================================== 48 49 ToolboxButtonColorUpdater::ToolboxButtonColorUpdater( 50 sal_uInt16 nId, 51 sal_uInt16 nTbxBtnId, 52 ToolBox* ptrTbx, 53 sal_uInt16 nMode ) : 54 mnDrawMode ( nMode ), 55 mnBtnId ( nTbxBtnId ), 56 mnSlotId ( nId ), 57 mpTbx ( ptrTbx ), 58 maCurColor ( COL_TRANSPARENT ) 59 { 60 if (mnSlotId == SID_BACKGROUND_COLOR) 61 mnDrawMode = TBX_UPDATER_MODE_CHAR_COLOR_NEW; 62 DBG_ASSERT( ptrTbx, "ToolBox not found :-(" ); 63 mbWasHiContrastMode = ptrTbx ? ( ptrTbx->GetSettings().GetStyleSettings().GetHighContrastMode() ) : sal_False; 64 Update(mnSlotId == SID_ATTR_CHAR_COLOR2 ? COL_BLACK : COL_GRAY); 65 } 66 67 // ----------------------------------------------------------------------- 68 69 ToolboxButtonColorUpdater::~ToolboxButtonColorUpdater() 70 { 71 } 72 73 // ----------------------------------------------------------------------- 74 75 void ToolboxButtonColorUpdater::Update( const Color& rColor ) 76 { 77 Image aImage( mpTbx->GetItemImage( mnBtnId ) ); 78 const bool bSizeChanged = ( maBmpSize != aImage.GetSizePixel() ); 79 const bool bDisplayModeChanged = ( mbWasHiContrastMode != mpTbx->GetSettings().GetStyleSettings().GetHighContrastMode() ); 80 Color aColor( rColor ); 81 82 // !!! #109290# Workaround for SetFillColor with COL_AUTO 83 if( aColor.GetColor() == COL_AUTO ) 84 aColor = Color( COL_TRANSPARENT ); 85 86 if( ( maCurColor != aColor ) || bSizeChanged || bDisplayModeChanged ) 87 { 88 BitmapEx aBmpEx( aImage.GetBitmapEx() ); 89 Bitmap aBmp( aBmpEx.GetBitmap() ); 90 BitmapWriteAccess* pBmpAcc = aBmp.AcquireWriteAccess(); 91 92 maBmpSize = aBmp.GetSizePixel(); 93 94 if( pBmpAcc ) 95 { 96 Bitmap aMsk; 97 BitmapWriteAccess* pMskAcc; 98 const Point aNullPnt; 99 100 if( aBmpEx.IsAlpha() ) 101 pMskAcc = ( aMsk = aBmpEx.GetAlpha().GetBitmap() ).AcquireWriteAccess(); 102 else if( aBmpEx.IsTransparent() ) 103 pMskAcc = ( aMsk = aBmpEx.GetMask() ).AcquireWriteAccess(); 104 else 105 pMskAcc = NULL; 106 107 mbWasHiContrastMode = mpTbx->GetSettings().GetStyleSettings().GetHighContrastMode(); 108 109 if( mnDrawMode == TBX_UPDATER_MODE_CHAR_COLOR_NEW && ( COL_TRANSPARENT != aColor.GetColor() ) ) 110 pBmpAcc->SetLineColor( aColor ); 111 else if( mpTbx->GetBackground().GetColor().IsDark() ) 112 pBmpAcc->SetLineColor( Color( COL_WHITE ) ); 113 else 114 pBmpAcc->SetLineColor( Color( COL_BLACK ) ); 115 116 pBmpAcc->SetFillColor( maCurColor = aColor ); 117 118 if( TBX_UPDATER_MODE_CHAR_COLOR_NEW == mnDrawMode || TBX_UPDATER_MODE_NONE == mnDrawMode ) 119 { 120 if( TBX_UPDATER_MODE_CHAR_COLOR_NEW == mnDrawMode ) 121 { 122 if( maBmpSize.Width() <= 16 ) 123 maUpdRect = Rectangle( Point( 0,12 ), Size( maBmpSize.Width(), 4 ) ); 124 else 125 maUpdRect = Rectangle( Point( 1, maBmpSize.Height() - 7 ), Size( maBmpSize.Width() - 2 ,6 ) ); 126 } 127 else 128 { 129 if( maBmpSize.Width() <= 16 ) 130 maUpdRect = Rectangle( Point( 7, 7 ), Size( 8, 8 ) ); 131 else 132 maUpdRect = Rectangle( Point( maBmpSize.Width() - 12, maBmpSize.Height() - 12 ), Size( 11, 11 ) ); 133 } 134 135 pBmpAcc->DrawRect( maUpdRect ); 136 137 if( pMskAcc ) 138 { 139 if( COL_TRANSPARENT == aColor.GetColor() ) 140 { 141 pMskAcc->SetLineColor( COL_BLACK ); 142 pMskAcc->SetFillColor( COL_WHITE ); 143 } 144 else 145 pMskAcc->SetFillColor( COL_BLACK ); 146 147 pMskAcc->DrawRect( maUpdRect ); 148 } 149 } 150 else 151 { 152 DBG_ERROR( "ToolboxButtonColorUpdater::Update: TBX_UPDATER_MODE_CHAR_COLOR / TBX_UPDATER_MODE_CHAR_BACKGROUND" ); 153 // !!! DrawChar( aVirDev, aColor ); 154 } 155 156 aBmp.ReleaseAccess( pBmpAcc ); 157 158 if( pMskAcc ) 159 aMsk.ReleaseAccess( pMskAcc ); 160 161 if( aBmpEx.IsAlpha() ) 162 aBmpEx = BitmapEx( aBmp, AlphaMask( aMsk ) ); 163 else if( aBmpEx.IsTransparent() ) 164 aBmpEx = BitmapEx( aBmp, aMsk ); 165 else 166 aBmpEx = aBmp; 167 168 mpTbx->SetItemImage( mnBtnId, Image( aBmpEx ) ); 169 } 170 } 171 } 172 173 // ----------------------------------------------------------------------- 174 175 void ToolboxButtonColorUpdater::DrawChar( VirtualDevice& rVirDev, const Color& rCol ) 176 { 177 Font aOldFont = rVirDev.GetFont(); 178 Font aFont = aOldFont; 179 Size aSz = aFont.GetSize(); 180 aSz.Height() = maBmpSize.Height(); 181 aFont.SetSize( aSz ); 182 aFont.SetWeight( WEIGHT_BOLD ); 183 184 if ( mnDrawMode == TBX_UPDATER_MODE_CHAR_COLOR ) 185 { 186 aFont.SetColor( rCol ); 187 aFont.SetFillColor( Color( IMAGE_COL_TRANSPARENT ) ); 188 } 189 else 190 { 191 rVirDev.SetLineColor(); 192 rVirDev.SetFillColor( rCol ); 193 Rectangle aRect( Point(0,0), maBmpSize ); 194 rVirDev.DrawRect( aRect ); 195 aFont.SetFillColor( rCol ); 196 } 197 rVirDev.SetFont( aFont ); 198 Size aTxtSize(rVirDev.GetTextWidth( 'A' ), rVirDev.GetTextHeight()); 199 Point aPos( ( maBmpSize.Width() - aTxtSize.Width() ) / 2, 200 ( maBmpSize.Height() - aTxtSize.Height() ) / 2 ); 201 202 rVirDev.DrawText( aPos, 'A' ); 203 rVirDev.SetFont( aOldFont ); 204 } 205 206 //........................................................................ 207 } // namespace svx 208 //........................................................................ 209