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