Update ooo320-m1
[ooovba.git] / svtools / source / contnr / svlbitm.cxx
blobcbc0b477a80601063824173e49254144393ad574
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: svlbitm.cxx,v $
10 * $Revision: 1.16 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_svtools.hxx"
35 #include <svtools/svlbox.hxx>
36 #include <svtools/svlbitm.hxx>
37 #include <vcl/svapp.hxx>
38 #ifndef _SV_BUTTON_HXX
39 #include <vcl/button.hxx>
40 #endif
41 #include <vcl/decoview.hxx>
42 #include <vcl/sound.hxx>
43 #include <vcl/salnativewidgets.hxx>
45 #define TABOFFS_NOT_VALID -2000000
47 struct SvLBoxButtonData_Impl
49 SvLBoxEntry* pEntry;
50 BOOL bDefaultImages;
51 BOOL bShowRadioButton;
53 SvLBoxButtonData_Impl() : pEntry( NULL ), bDefaultImages( FALSE ), bShowRadioButton( FALSE ) {}
57 DBG_NAME(SvLBoxButtonData)
59 void SvLBoxButtonData::InitData( BOOL bImagesFromDefault, bool _bRadioBtn, const Control* pCtrl )
61 pImpl = new SvLBoxButtonData_Impl;
63 bDataOk = FALSE;
64 eState = SV_BUTTON_UNCHECKED;
65 pImpl->bDefaultImages = bImagesFromDefault;
66 pImpl->bShowRadioButton = ( _bRadioBtn != false );
68 if ( bImagesFromDefault )
69 SetDefaultImages( pCtrl );
72 SvLBoxButtonData::SvLBoxButtonData( const Control* pControlForSettings )
74 DBG_CTOR(SvLBoxButtonData,0);
76 InitData( TRUE, false, pControlForSettings );
79 SvLBoxButtonData::SvLBoxButtonData( const Control* pControlForSettings, bool _bRadioBtn )
81 DBG_CTOR(SvLBoxButtonData,0);
83 InitData( TRUE, _bRadioBtn, pControlForSettings );
86 SvLBoxButtonData::SvLBoxButtonData()
88 DBG_CTOR(SvLBoxButtonData,0);
90 InitData( FALSE, false );
93 SvLBoxButtonData::~SvLBoxButtonData()
95 DBG_DTOR(SvLBoxButtonData,0);
97 delete pImpl;
98 #ifdef DBG_UTIL
99 pImpl = NULL;
100 #endif
103 void SvLBoxButtonData::CallLink()
105 DBG_CHKTHIS(SvLBoxButtonData,0);
106 aLink.Call( this );
109 USHORT SvLBoxButtonData::GetIndex( USHORT nItemState )
111 DBG_CHKTHIS(SvLBoxButtonData,0);
112 nItemState &= 0x000F;
113 USHORT nIdx;
114 switch( nItemState )
116 case SV_ITEMSTATE_UNCHECKED:
117 nIdx = SV_BMP_UNCHECKED; break;
118 case SV_ITEMSTATE_CHECKED:
119 nIdx = SV_BMP_CHECKED; break;
120 case SV_ITEMSTATE_TRISTATE:
121 nIdx = SV_BMP_TRISTATE; break;
122 case SV_ITEMSTATE_UNCHECKED | SV_ITEMSTATE_HILIGHTED:
123 nIdx = SV_BMP_HIUNCHECKED; break;
124 case SV_ITEMSTATE_CHECKED | SV_ITEMSTATE_HILIGHTED:
125 nIdx = SV_BMP_HICHECKED; break;
126 case SV_ITEMSTATE_TRISTATE | SV_ITEMSTATE_HILIGHTED:
127 nIdx = SV_BMP_HITRISTATE; break;
128 default:
129 nIdx = SV_BMP_UNCHECKED;
131 return nIdx;
134 void SvLBoxButtonData::SetWidthAndHeight()
136 DBG_CHKTHIS(SvLBoxButtonData,0);
137 Size aSize = aBmps[0].GetSizePixel();
138 nWidth = aSize.Width();
139 nHeight = aSize.Height();
140 bDataOk = TRUE;
144 void SvLBoxButtonData::StoreButtonState( SvLBoxEntry* pActEntry, USHORT nItemFlags )
146 DBG_CHKTHIS(SvLBoxButtonData,0);
147 pImpl->pEntry = pActEntry;
148 eState = ConvertToButtonState( nItemFlags );
151 SvButtonState SvLBoxButtonData::ConvertToButtonState( USHORT nItemFlags ) const
153 DBG_CHKTHIS(SvLBoxButtonData,0);
154 nItemFlags &= (SV_ITEMSTATE_UNCHECKED |
155 SV_ITEMSTATE_CHECKED |
156 SV_ITEMSTATE_TRISTATE);
157 switch( nItemFlags )
159 case SV_ITEMSTATE_UNCHECKED:
160 return SV_BUTTON_UNCHECKED;
162 case SV_ITEMSTATE_CHECKED:
163 return SV_BUTTON_CHECKED;
165 case SV_ITEMSTATE_TRISTATE:
166 return SV_BUTTON_TRISTATE;
167 default:
168 return SV_BUTTON_UNCHECKED;
172 SvLBoxEntry* SvLBoxButtonData::GetActEntry() const
174 DBG_ASSERT( pImpl, "-SvLBoxButtonData::GetActEntry(): don't use me that way!" );
175 return pImpl->pEntry;
178 void SvLBoxButtonData::SetDefaultImages( const Control* pCtrl )
180 const AllSettings& rSettings = pCtrl? pCtrl->GetSettings() : Application::GetSettings();
182 if ( pImpl->bShowRadioButton )
184 aBmps[ SV_BMP_UNCHECKED ] = RadioButton::GetRadioImage( rSettings, BUTTON_DRAW_DEFAULT );
185 aBmps[ SV_BMP_CHECKED ] = RadioButton::GetRadioImage( rSettings, BUTTON_DRAW_CHECKED );
186 aBmps[ SV_BMP_HICHECKED ] = RadioButton::GetRadioImage( rSettings, BUTTON_DRAW_CHECKED | BUTTON_DRAW_PRESSED );
187 aBmps[ SV_BMP_HIUNCHECKED ] = RadioButton::GetRadioImage( rSettings, BUTTON_DRAW_DEFAULT | BUTTON_DRAW_PRESSED );
188 aBmps[ SV_BMP_TRISTATE ] = RadioButton::GetRadioImage( rSettings, BUTTON_DRAW_DONTKNOW );
189 aBmps[ SV_BMP_HITRISTATE ] = RadioButton::GetRadioImage( rSettings, BUTTON_DRAW_DONTKNOW | BUTTON_DRAW_PRESSED );
191 else
193 aBmps[ SV_BMP_UNCHECKED ] = CheckBox::GetCheckImage( rSettings, BUTTON_DRAW_DEFAULT );
194 aBmps[ SV_BMP_CHECKED ] = CheckBox::GetCheckImage( rSettings, BUTTON_DRAW_CHECKED );
195 aBmps[ SV_BMP_HICHECKED ] = CheckBox::GetCheckImage( rSettings, BUTTON_DRAW_CHECKED | BUTTON_DRAW_PRESSED );
196 aBmps[ SV_BMP_HIUNCHECKED ] = CheckBox::GetCheckImage( rSettings, BUTTON_DRAW_DEFAULT | BUTTON_DRAW_PRESSED );
197 aBmps[ SV_BMP_TRISTATE ] = CheckBox::GetCheckImage( rSettings, BUTTON_DRAW_DONTKNOW );
198 aBmps[ SV_BMP_HITRISTATE ] = CheckBox::GetCheckImage( rSettings, BUTTON_DRAW_DONTKNOW | BUTTON_DRAW_PRESSED );
202 BOOL SvLBoxButtonData::HasDefaultImages( void ) const
204 return pImpl->bDefaultImages;
207 BOOL SvLBoxButtonData::IsRadio() {
208 return pImpl->bShowRadioButton;
211 // ***************************************************************
212 // class SvLBoxString
213 // ***************************************************************
215 DBG_NAME(SvLBoxString);
217 SvLBoxString::SvLBoxString( SvLBoxEntry* pEntry,USHORT nFlags,const XubString& rStr) :
218 SvLBoxItem( pEntry, nFlags )
220 DBG_CTOR(SvLBoxString,0);
221 SetText( pEntry, rStr );
224 SvLBoxString::SvLBoxString() : SvLBoxItem()
226 DBG_CTOR(SvLBoxString,0);
229 SvLBoxString::~SvLBoxString()
231 DBG_DTOR(SvLBoxString,0);
234 USHORT SvLBoxString::IsA()
236 DBG_CHKTHIS(SvLBoxString,0);
237 return SV_ITEM_ID_LBOXSTRING;
240 void SvLBoxString::Paint( const Point& rPos, SvLBox& rDev, USHORT /* nFlags */,
241 SvLBoxEntry* _pEntry)
243 DBG_CHKTHIS(SvLBoxString,0);
244 if ( _pEntry )
246 USHORT nStyle = rDev.IsEnabled() ? 0 : TEXT_DRAW_DISABLE;
247 if ( rDev.IsEntryMnemonicsEnabled() )
248 nStyle |= TEXT_DRAW_MNEMONIC;
249 rDev.DrawText( Rectangle(rPos,GetSize(&rDev,_pEntry)),aStr,nStyle);
251 else
252 rDev.DrawText( rPos, aStr);
256 SvLBoxItem* SvLBoxString::Create() const
258 DBG_CHKTHIS(SvLBoxString,0);
259 return new SvLBoxString;
262 void SvLBoxString::Clone( SvLBoxItem* pSource )
264 DBG_CHKTHIS(SvLBoxString,0);
265 aStr = ((SvLBoxString*)pSource)->aStr;
268 void SvLBoxString::SetText( SvLBoxEntry*, const XubString& rStr )
270 DBG_CHKTHIS(SvLBoxString,0);
271 aStr = rStr;
274 void SvLBoxString::InitViewData( SvLBox* pView,SvLBoxEntry* pEntry,
275 SvViewDataItem* pViewData)
277 DBG_CHKTHIS(SvLBoxString,0);
278 if( !pViewData )
279 pViewData = pView->GetViewDataItem( pEntry, this );
280 pViewData->aSize = Size(pView->GetTextWidth( aStr ), pView->GetTextHeight());
283 // ***************************************************************
284 // class SvLBoxBmp
285 // ***************************************************************
287 DBG_NAME(SvLBoxBmp);
289 SvLBoxBmp::SvLBoxBmp( SvLBoxEntry* pEntry, USHORT nFlags, Image aBitmap ) :
290 SvLBoxItem( pEntry, nFlags )
292 DBG_CTOR(SvLBoxBmp,0);
293 SetBitmap( pEntry, aBitmap);
296 SvLBoxBmp::SvLBoxBmp() : SvLBoxItem()
298 DBG_CTOR(SvLBoxBmp,0);
301 SvLBoxBmp::~SvLBoxBmp()
303 DBG_DTOR(SvLBoxBmp,0);
306 USHORT SvLBoxBmp::IsA()
308 DBG_CHKTHIS(SvLBoxBmp,0);
309 return SV_ITEM_ID_LBOXBMP;
312 void SvLBoxBmp::SetBitmap( SvLBoxEntry*, Image aBitmap)
314 DBG_CHKTHIS(SvLBoxBmp,0);
315 aBmp = aBitmap;
318 void SvLBoxBmp::InitViewData( SvLBox* pView,SvLBoxEntry* pEntry,
319 SvViewDataItem* pViewData)
321 DBG_CHKTHIS(SvLBoxBmp,0);
322 if( !pViewData )
323 pViewData = pView->GetViewDataItem( pEntry, this );
324 pViewData->aSize = aBmp.GetSizePixel();
327 void SvLBoxBmp::Paint( const Point& rPos, SvLBox& rDev, USHORT /* nFlags */,
328 SvLBoxEntry* )
330 DBG_CHKTHIS(SvLBoxBmp,0);
331 USHORT nStyle = rDev.IsEnabled() ? 0 : IMAGE_DRAW_DISABLE;
332 rDev.DrawImage( rPos, aBmp ,nStyle);
335 SvLBoxItem* SvLBoxBmp::Create() const
337 DBG_CHKTHIS(SvLBoxBmp,0);
338 return new SvLBoxBmp;
341 void SvLBoxBmp::Clone( SvLBoxItem* pSource )
343 DBG_CHKTHIS(SvLBoxBmp,0);
344 aBmp = ((SvLBoxBmp*)pSource)->aBmp;
347 // ***************************************************************
348 // class SvLBoxButton
349 // ***************************************************************
351 DBG_NAME(SvLBoxButton);
353 SvLBoxButton::SvLBoxButton( SvLBoxEntry* pEntry, SvLBoxButtonKind eTheKind,
354 USHORT nFlags, SvLBoxButtonData* pBData )
355 : SvLBoxItem( pEntry, nFlags )
357 DBG_CTOR(SvLBoxButton,0);
358 eKind = eTheKind;
359 nBaseOffs = 0;
360 nItemFlags = 0;
361 SetStateUnchecked();
362 pData = pBData;
365 SvLBoxButton::SvLBoxButton() : SvLBoxItem()
367 DBG_CTOR(SvLBoxButton,0);
368 eKind = SvLBoxButtonKind_enabledCheckbox;
369 nItemFlags = 0;
370 SetStateUnchecked();
373 SvLBoxButton::~SvLBoxButton()
375 DBG_DTOR(SvLBoxButton,0);
378 USHORT SvLBoxButton::IsA()
380 DBG_CHKTHIS(SvLBoxButton,0);
381 return SV_ITEM_ID_LBOXBUTTON;
384 void SvLBoxButton::Check(SvLBox*, SvLBoxEntry*, BOOL bOn)
386 DBG_CHKTHIS(SvLBoxButton,0);
387 if ( bOn != IsStateChecked() )
389 if ( bOn )
390 SetStateChecked();
391 else
392 SetStateUnchecked();
396 BOOL SvLBoxButton::ClickHdl( SvLBox*, SvLBoxEntry* pEntry )
398 DBG_CHKTHIS(SvLBoxButton,0);
399 if ( CheckModification() )
401 if ( IsStateChecked() )
402 SetStateUnchecked();
403 else
404 SetStateChecked();
405 pData->StoreButtonState( pEntry, nItemFlags );
406 pData->CallLink();
408 return FALSE;
411 void SvLBoxButton::Paint( const Point& rPos, SvLBox& rDev, USHORT /* nFlags */,
412 SvLBoxEntry* )
414 DBG_CHKTHIS(SvLBoxButton,0);
415 USHORT nIndex = eKind == SvLBoxButtonKind_staticImage
416 ? SV_BMP_STATICIMAGE : pData->GetIndex( nItemFlags );
417 USHORT nStyle = eKind != SvLBoxButtonKind_disabledCheckbox &&
418 rDev.IsEnabled() ? 0 : IMAGE_DRAW_DISABLE;
421 //Native drawing
423 BOOL bNativeOK = FALSE;
424 Window *pWin = NULL;
425 if( rDev.GetOutDevType() == OUTDEV_WINDOW )
426 pWin = (Window*) &rDev;
428 if ( nIndex != SV_BMP_STATICIMAGE && pWin && pWin->IsNativeControlSupported( (pData->IsRadio())? CTRL_RADIOBUTTON : CTRL_CHECKBOX, PART_ENTIRE_CONTROL) )
430 ImplControlValue aControlValue;
431 Region aCtrlRegion( Rectangle(rPos, Size(pData->Width(), pData->Height())) );
432 ControlState nState = 0;
434 //states CTRL_STATE_DEFAULT, CTRL_STATE_PRESSED and CTRL_STATE_ROLLOVER are not implemented
435 if ( IsStateHilighted() ) nState |= CTRL_STATE_FOCUSED;
436 if ( nStyle != IMAGE_DRAW_DISABLE ) nState |= CTRL_STATE_ENABLED;
438 if ( IsStateChecked() )
439 aControlValue.setTristateVal( BUTTONVALUE_ON );
440 else if ( IsStateUnchecked() )
441 aControlValue.setTristateVal( BUTTONVALUE_OFF );
442 else if ( IsStateTristate() )
443 aControlValue.setTristateVal( BUTTONVALUE_MIXED );
445 bNativeOK = pWin->DrawNativeControl( (pData->IsRadio())? CTRL_RADIOBUTTON : CTRL_CHECKBOX, PART_ENTIRE_CONTROL,
446 aCtrlRegion, nState, aControlValue, rtl::OUString() );
449 if( !bNativeOK)
450 rDev.DrawImage( rPos, pData->aBmps[nIndex + nBaseOffs] ,nStyle);
453 SvLBoxItem* SvLBoxButton::Create() const
455 DBG_CHKTHIS(SvLBoxButton,0);
456 return new SvLBoxButton;
459 void SvLBoxButton::Clone( SvLBoxItem* pSource )
461 DBG_CHKTHIS(SvLBoxButton,0);
462 pData = ((SvLBoxButton*)pSource)->pData;
465 void SvLBoxButton::InitViewData( SvLBox* pView,SvLBoxEntry* pEntry,
466 SvViewDataItem* pViewData )
468 DBG_CHKTHIS(SvLBoxButton,0);
469 if( !pViewData )
470 pViewData = pView->GetViewDataItem( pEntry, this );
471 pViewData->aSize = Size( pData->Width(), pData->Height() );
474 bool SvLBoxButton::CheckModification() const
476 if( eKind == SvLBoxButtonKind_disabledCheckbox )
477 Sound::Beep();
478 return eKind == SvLBoxButtonKind_enabledCheckbox;
481 // ***************************************************************
482 // class SvLBoxContextBmp
483 // ***************************************************************
485 struct SvLBoxContextBmp_Impl
487 Image m_aImage1;
488 Image m_aImage2;
490 Image m_aImage1_hc;
491 Image m_aImage2_hc;
493 USHORT m_nB2IndicatorFlags;
496 // ***************************************************************
497 DBG_NAME(SvLBoxContextBmp)
499 SvLBoxContextBmp::SvLBoxContextBmp( SvLBoxEntry* pEntry, USHORT nItemFlags,
500 Image aBmp1, Image aBmp2, USHORT nEntryFlags )
501 :SvLBoxItem( pEntry, nItemFlags )
502 ,m_pImpl( new SvLBoxContextBmp_Impl )
504 DBG_CTOR(SvLBoxContextBmp,0);
506 m_pImpl->m_nB2IndicatorFlags = nEntryFlags;
507 SetModeImages( aBmp1, aBmp2 );
510 SvLBoxContextBmp::SvLBoxContextBmp()
511 :SvLBoxItem( )
512 ,m_pImpl( new SvLBoxContextBmp_Impl )
514 m_pImpl->m_nB2IndicatorFlags = 0;
515 DBG_CTOR(SvLBoxContextBmp,0);
518 SvLBoxContextBmp::~SvLBoxContextBmp()
520 delete m_pImpl;
521 DBG_DTOR(SvLBoxContextBmp,0);
524 USHORT SvLBoxContextBmp::IsA()
526 DBG_CHKTHIS(SvLBoxContextBmp,0);
527 return SV_ITEM_ID_LBOXCONTEXTBMP;
530 BOOL SvLBoxContextBmp::SetModeImages( const Image& _rBitmap1, const Image& _rBitmap2, BmpColorMode _eMode )
532 DBG_CHKTHIS(SvLBoxContextBmp,0);
534 sal_Bool bSuccess = sal_True;
535 switch ( _eMode )
537 case BMP_COLOR_NORMAL:
538 m_pImpl->m_aImage1 = _rBitmap1;
539 m_pImpl->m_aImage2 = _rBitmap2;
540 break;
542 case BMP_COLOR_HIGHCONTRAST:
543 m_pImpl->m_aImage1_hc = _rBitmap1;
544 m_pImpl->m_aImage2_hc = _rBitmap2;
545 break;
547 default:
548 DBG_ERROR( "SvLBoxContextBmp::SetModeImages: unexpected mode!");
549 bSuccess = sal_False;
550 break;
552 return bSuccess;
555 Image& SvLBoxContextBmp::implGetImageStore( sal_Bool _bFirst, BmpColorMode _eMode )
557 DBG_CHKTHIS(SvLBoxContextBmp,0);
559 switch ( _eMode )
561 case BMP_COLOR_NORMAL:
562 return _bFirst ? m_pImpl->m_aImage1 : m_pImpl->m_aImage2;
564 case BMP_COLOR_HIGHCONTRAST:
565 return _bFirst ? m_pImpl->m_aImage1_hc : m_pImpl->m_aImage2_hc;
567 default:
568 DBG_ERROR( "SvLBoxContextBmp::implGetImageStore: unexpected mode!");
571 // OJ: #i27071# wrong mode so we just return the normal images
572 return _bFirst ? m_pImpl->m_aImage1 : m_pImpl->m_aImage2;
575 void SvLBoxContextBmp::InitViewData( SvLBox* pView,SvLBoxEntry* pEntry,
576 SvViewDataItem* pViewData)
578 DBG_CHKTHIS(SvLBoxContextBmp,0);
579 if( !pViewData )
580 pViewData = pView->GetViewDataItem( pEntry, this );
581 pViewData->aSize = m_pImpl->m_aImage1.GetSizePixel();
584 void SvLBoxContextBmp::Paint( const Point& _rPos, SvLBox& _rDev,
585 USHORT _nViewDataEntryFlags, SvLBoxEntry* _pEntry )
587 DBG_CHKTHIS(SvLBoxContextBmp,0);
589 // determine the image set
590 BmpColorMode eMode( BMP_COLOR_NORMAL );
591 if ( !!m_pImpl->m_aImage1_hc )
592 { // we really have HC images
593 const Wallpaper& rDeviceBackground = _rDev.GetDisplayBackground();
594 if ( rDeviceBackground.GetColor().IsDark() )
595 eMode = BMP_COLOR_HIGHCONTRAST;
598 // get the image
599 const Image& rImage = implGetImageStore( 0 == ( _nViewDataEntryFlags & m_pImpl->m_nB2IndicatorFlags ), eMode );
601 sal_Bool _bSemiTransparent = _pEntry && ( 0 != ( SV_ENTRYFLAG_SEMITRANSPARENT & _pEntry->GetFlags( ) ) );
602 // draw
603 USHORT nStyle = _rDev.IsEnabled() ? 0 : IMAGE_DRAW_DISABLE;
604 if ( _bSemiTransparent )
605 nStyle |= IMAGE_DRAW_SEMITRANSPARENT;
606 _rDev.DrawImage( _rPos, rImage, nStyle);
609 SvLBoxItem* SvLBoxContextBmp::Create() const
611 DBG_CHKTHIS(SvLBoxContextBmp,0);
612 return new SvLBoxContextBmp;
615 void SvLBoxContextBmp::Clone( SvLBoxItem* pSource )
617 DBG_CHKTHIS(SvLBoxContextBmp,0);
618 m_pImpl->m_aImage1 = static_cast< SvLBoxContextBmp* >( pSource )->m_pImpl->m_aImage1;
619 m_pImpl->m_aImage2 = static_cast< SvLBoxContextBmp* >( pSource )->m_pImpl->m_aImage2;
620 m_pImpl->m_nB2IndicatorFlags = static_cast< SvLBoxContextBmp* >( pSource )->m_pImpl->m_nB2IndicatorFlags;