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 --------------------------------------------------------------- 32 #include <tools/shl.hxx> 33 34 #define _SVX_CHECKLBX_CXX 35 36 #include <svx/checklbx.hxx> 37 #include <svx/dialmgr.hxx> 38 39 #include <svx/dialogs.hrc> 40 41 // class SvxCheckListBox ------------------------------------------------- 42 43 SvxCheckListBox::SvxCheckListBox( Window* pParent, WinBits nWinStyle ) : 44 45 SvTreeListBox( pParent, nWinStyle ) 46 47 { 48 Init_Impl(); 49 } 50 51 // ----------------------------------------------------------------------- 52 53 SvxCheckListBox::SvxCheckListBox( Window* pParent, const ResId& rResId ) : 54 55 SvTreeListBox( pParent, rResId ) 56 57 { 58 Init_Impl(); 59 } 60 61 // ----------------------------------------------------------------------- 62 63 SvxCheckListBox::SvxCheckListBox( Window* pParent, const ResId& rResId, 64 const Image& rNormalStaticImage, 65 const Image& /*TODO#i72485# rHighContrastStaticImage*/ ) : 66 67 SvTreeListBox( pParent, rResId ) 68 69 { 70 Init_Impl(); 71 pCheckButton->aBmps[SV_BMP_STATICIMAGE] = rNormalStaticImage; 72 } 73 74 // ----------------------------------------------------------------------- 75 76 SvxCheckListBox::~SvxCheckListBox() 77 { 78 delete pCheckButton; 79 } 80 81 // ----------------------------------------------------------------------- 82 83 void SvxCheckListBox::Init_Impl() 84 { 85 pCheckButton = new SvLBoxButtonData( this ); 86 EnableCheckButton( pCheckButton ); 87 } 88 89 // ----------------------------------------------------------------------- 90 91 void SvxCheckListBox::InsertEntry( const String& rStr, sal_uInt16 nPos, 92 void* pUserData, 93 SvLBoxButtonKind eButtonKind ) 94 { 95 SvTreeListBox::InsertEntry( rStr, NULL, sal_False, nPos, pUserData, 96 eButtonKind ); 97 } 98 99 // ----------------------------------------------------------------------- 100 101 void SvxCheckListBox::RemoveEntry( sal_uInt16 nPos ) 102 { 103 if ( nPos < GetEntryCount() ) 104 SvTreeListBox::GetModel()->Remove( GetEntry( nPos ) ); 105 } 106 107 // ----------------------------------------------------------------------- 108 109 void SvxCheckListBox::SelectEntryPos( sal_uInt16 nPos, sal_Bool bSelect ) 110 { 111 if ( nPos < GetEntryCount() ) 112 Select( GetEntry( nPos ), bSelect ); 113 } 114 115 // ----------------------------------------------------------------------- 116 117 sal_uInt16 SvxCheckListBox::GetSelectEntryPos() const 118 { 119 SvLBoxEntry* pEntry = GetCurEntry(); 120 121 if ( pEntry ) 122 return (sal_uInt16)GetModel()->GetAbsPos( pEntry ); 123 return LISTBOX_ENTRY_NOTFOUND; 124 } 125 126 // ----------------------------------------------------------------------- 127 128 String SvxCheckListBox::GetText( sal_uInt16 nPos ) const 129 { 130 SvLBoxEntry* pEntry = GetEntry( nPos ); 131 132 if ( pEntry ) 133 return GetEntryText( pEntry ); 134 return String(); 135 } 136 137 // ----------------------------------------------------------------------- 138 139 sal_uInt16 SvxCheckListBox::GetCheckedEntryCount() const 140 { 141 sal_uInt16 nCheckCount = 0; 142 sal_uInt16 nCount = (sal_uInt16)GetEntryCount(); 143 144 for ( sal_uInt16 i = 0; i < nCount; ++i ) 145 { 146 if ( IsChecked( i ) ) 147 nCheckCount++; 148 } 149 return nCheckCount; 150 } 151 152 // ----------------------------------------------------------------------- 153 154 void SvxCheckListBox::CheckEntryPos( sal_uInt16 nPos, sal_Bool bCheck ) 155 { 156 if ( nPos < GetEntryCount() ) 157 SetCheckButtonState( 158 GetEntry( nPos ), bCheck ? SvButtonState( SV_BUTTON_CHECKED ) : 159 SvButtonState( SV_BUTTON_UNCHECKED ) ); 160 } 161 162 // ----------------------------------------------------------------------- 163 164 sal_Bool SvxCheckListBox::IsChecked( sal_uInt16 nPos ) const 165 { 166 if ( nPos < GetEntryCount() ) 167 return (GetCheckButtonState( GetEntry( nPos ) ) == SV_BUTTON_CHECKED); 168 else 169 return sal_False; 170 } 171 172 // ----------------------------------------------------------------------- 173 174 void* SvxCheckListBox::SetEntryData ( sal_uInt16 nPos, void* pNewData ) 175 { 176 void* pOld = NULL; 177 178 if ( nPos < GetEntryCount() ) 179 { 180 pOld = GetEntry( nPos )->GetUserData(); 181 GetEntry( nPos )->SetUserData( pNewData ); 182 } 183 return pOld; 184 } 185 186 // ----------------------------------------------------------------------- 187 188 void* SvxCheckListBox::GetEntryData( sal_uInt16 nPos ) const 189 { 190 if ( nPos < GetEntryCount() ) 191 return GetEntry( nPos )->GetUserData(); 192 else 193 return NULL; 194 } 195 196 // ----------------------------------------------------------------------- 197 198 void SvxCheckListBox::ToggleCheckButton( SvLBoxEntry* pEntry ) 199 { 200 if ( pEntry ) 201 { 202 if ( !IsSelected( pEntry ) ) 203 Select( pEntry ); 204 else 205 CheckEntryPos( GetSelectEntryPos(), !IsChecked( GetSelectEntryPos() ) ); 206 } 207 } 208 209 // ----------------------------------------------------------------------- 210 211 void SvxCheckListBox::MouseButtonDown( const MouseEvent& rMEvt ) 212 { 213 if ( rMEvt.IsLeft() ) 214 { 215 const Point aPnt = rMEvt.GetPosPixel(); 216 SvLBoxEntry* pEntry = GetEntry( aPnt ); 217 218 if ( pEntry ) 219 { 220 sal_Bool bCheck = ( GetCheckButtonState( pEntry ) == SV_BUTTON_CHECKED ); 221 SvLBoxItem* pItem = GetItem( pEntry, aPnt.X() ); 222 223 if ( pItem && pItem->IsA() == SV_ITEM_ID_LBOXBUTTON ) 224 { 225 SvTreeListBox::MouseButtonDown( rMEvt ); 226 Select( pEntry, sal_True ); 227 return; 228 } 229 else 230 { 231 ToggleCheckButton( pEntry ); 232 SvTreeListBox::MouseButtonDown( rMEvt ); 233 234 // check if the entry below the mouse changed during the base method call. This is possible if, 235 // for instance, a handler invoked by the base class tampers with the list entries. 236 const SvLBoxEntry* pNewEntry = GetEntry( aPnt ); 237 if ( pNewEntry != pEntry ) 238 return; 239 240 if ( bCheck != ( GetCheckButtonState( pEntry ) == SV_BUTTON_CHECKED ) ) 241 CheckButtonHdl(); 242 return; 243 } 244 } 245 } 246 SvTreeListBox::MouseButtonDown( rMEvt ); 247 } 248 249 // ----------------------------------------------------------------------- 250 251 void SvxCheckListBox::KeyInput( const KeyEvent& rKEvt ) 252 { 253 const KeyCode& rKey = rKEvt.GetKeyCode(); 254 255 if ( rKey.GetCode() == KEY_RETURN || rKey.GetCode() == KEY_SPACE ) 256 { 257 SvLBoxEntry* pEntry = GetCurEntry(); 258 259 if ( pEntry ) 260 { 261 sal_Bool bCheck = ( GetCheckButtonState( pEntry ) == SV_BUTTON_CHECKED ); 262 ToggleCheckButton( pEntry ); 263 if ( bCheck != ( GetCheckButtonState( pEntry ) == SV_BUTTON_CHECKED ) ) 264 CheckButtonHdl(); 265 } 266 } 267 else if ( GetEntryCount() ) 268 SvTreeListBox::KeyInput( rKEvt ); 269 } 270 271 // ----------------------------------------------------------------------- 272 273 SvLBoxEntry* SvxCheckListBox::InsertEntry( const XubString& rText, SvLBoxEntry* pParent, sal_Bool bChildsOnDemand, sal_uIntPtr nPos, void* pUserData, SvLBoxButtonKind eButtonKind ) 274 { 275 return SvTreeListBox::InsertEntry( rText, pParent, bChildsOnDemand, nPos, pUserData, eButtonKind ); 276 } 277 278