Version 4.3.0.0.beta1, tag libreoffice-4.3.0.0.beta1
[LibreOffice.git] / svx / source / dialog / checklbx.cxx
bloba2fdfa52a0a80fde53b01e8a105ff646e41d4b5e
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <tools/shl.hxx>
22 #include <svtools/treelistentry.hxx>
24 #include <svx/checklbx.hxx>
25 #include <svx/dialmgr.hxx>
27 #include <svx/dialogs.hrc>
29 #include <vcl/builder.hxx>
31 // class SvxCheckListBox -------------------------------------------------
33 SvxCheckListBox::SvxCheckListBox( Window* pParent, WinBits nWinStyle ) :
35 SvTreeListBox( pParent, nWinStyle )
38 Init_Impl();
41 extern "C" SAL_DLLPUBLIC_EXPORT Window* SAL_CALL makeSvxCheckListBox(Window *pParent, VclBuilder::stringmap &rMap)
43 WinBits nWinStyle = WB_TABSTOP;
44 OString sBorder = VclBuilder::extractCustomProperty(rMap);
45 if (!sBorder.isEmpty())
46 nWinStyle |= WB_BORDER;
47 return new SvxCheckListBox(pParent, nWinStyle);
50 SvxCheckListBox::SvxCheckListBox( Window* pParent, const ResId& rResId,
51 const Image& rNormalStaticImage )
52 : SvTreeListBox( pParent, rResId )
54 Init_Impl();
55 SetNormalStaticImage(rNormalStaticImage);
58 void SvxCheckListBox::SetNormalStaticImage(const Image& rNormalStaticImage)
60 pCheckButton->aBmps[SV_BMP_STATICIMAGE] = rNormalStaticImage;
63 SvxCheckListBox::~SvxCheckListBox()
65 delete pCheckButton;
68 void SvxCheckListBox::Init_Impl()
70 pCheckButton = new SvLBoxButtonData( this );
71 EnableCheckButton( pCheckButton );
74 void SvxCheckListBox::InsertEntry( const OUString& rStr, sal_uLong nPos,
75 void* pUserData,
76 SvLBoxButtonKind eButtonKind )
78 SvTreeListBox::InsertEntry( rStr, NULL, false, nPos, pUserData,
79 eButtonKind );
84 void SvxCheckListBox::RemoveEntry( sal_uLong nPos )
86 if ( nPos < GetEntryCount() )
87 SvTreeListBox::GetModel()->Remove( GetEntry( nPos ) );
92 void SvxCheckListBox::SelectEntryPos( sal_uLong nPos, bool bSelect )
94 if ( nPos < GetEntryCount() )
95 Select( GetEntry( nPos ), bSelect );
100 sal_uLong SvxCheckListBox::GetSelectEntryPos() const
102 SvTreeListEntry* pEntry = GetCurEntry();
104 if ( pEntry )
105 return GetModel()->GetAbsPos( pEntry );
106 return TREELIST_ENTRY_NOTFOUND;
111 OUString SvxCheckListBox::GetText( sal_uLong nPos ) const
113 SvTreeListEntry* pEntry = GetEntry( nPos );
115 if ( pEntry )
116 return GetEntryText( pEntry );
117 return OUString();
122 sal_uLong SvxCheckListBox::GetCheckedEntryCount() const
124 sal_uLong nCheckCount = 0;
125 sal_uLong nCount = GetEntryCount();
127 for ( sal_uLong i = 0; i < nCount; ++i )
129 if ( IsChecked( i ) )
130 nCheckCount++;
132 return nCheckCount;
137 void SvxCheckListBox::CheckEntryPos( sal_uLong nPos, bool bCheck )
139 if ( nPos < GetEntryCount() )
140 SetCheckButtonState(
141 GetEntry( nPos ), bCheck ? SvButtonState( SV_BUTTON_CHECKED ) :
142 SvButtonState( SV_BUTTON_UNCHECKED ) );
147 bool SvxCheckListBox::IsChecked( sal_uLong nPos ) const
149 if ( nPos < GetEntryCount() )
150 return (GetCheckButtonState( GetEntry( nPos ) ) == SV_BUTTON_CHECKED);
151 else
152 return false;
157 void* SvxCheckListBox::SetEntryData ( sal_uLong nPos, void* pNewData )
159 void* pOld = NULL;
161 if ( nPos < GetEntryCount() )
163 pOld = GetEntry( nPos )->GetUserData();
164 GetEntry( nPos )->SetUserData( pNewData );
166 return pOld;
171 void* SvxCheckListBox::GetEntryData( sal_uLong nPos ) const
173 if ( nPos < GetEntryCount() )
174 return GetEntry( nPos )->GetUserData();
175 else
176 return NULL;
181 void SvxCheckListBox::ToggleCheckButton( SvTreeListEntry* pEntry )
183 if ( pEntry )
185 if ( !IsSelected( pEntry ) )
186 Select( pEntry );
187 else
188 CheckEntryPos( GetSelectEntryPos(), !IsChecked( GetSelectEntryPos() ) );
194 void SvxCheckListBox::MouseButtonDown( const MouseEvent& rMEvt )
196 if ( rMEvt.IsLeft() )
198 const Point aPnt = rMEvt.GetPosPixel();
199 SvTreeListEntry* pEntry = GetEntry( aPnt );
201 if ( pEntry )
203 bool bCheck = ( GetCheckButtonState( pEntry ) == SV_BUTTON_CHECKED );
204 SvLBoxItem* pItem = GetItem( pEntry, aPnt.X() );
206 if (pItem && pItem->GetType() == SV_ITEM_ID_LBOXBUTTON)
208 SvTreeListBox::MouseButtonDown( rMEvt );
209 Select( pEntry, true );
210 return;
212 else
214 ToggleCheckButton( pEntry );
215 SvTreeListBox::MouseButtonDown( rMEvt );
217 // check if the entry below the mouse changed during the base method call. This is possible if,
218 // for instance, a handler invoked by the base class tampers with the list entries.
219 const SvTreeListEntry* pNewEntry = GetEntry( aPnt );
220 if ( pNewEntry != pEntry )
221 return;
223 if ( bCheck != ( GetCheckButtonState( pEntry ) == SV_BUTTON_CHECKED ) )
224 CheckButtonHdl();
225 return;
229 SvTreeListBox::MouseButtonDown( rMEvt );
234 void SvxCheckListBox::KeyInput( const KeyEvent& rKEvt )
236 const KeyCode& rKey = rKEvt.GetKeyCode();
238 if ( rKey.GetCode() == KEY_RETURN || rKey.GetCode() == KEY_SPACE )
240 SvTreeListEntry* pEntry = GetCurEntry();
242 if ( pEntry )
244 bool bCheck = ( GetCheckButtonState( pEntry ) == SV_BUTTON_CHECKED );
245 ToggleCheckButton( pEntry );
246 if ( bCheck != ( GetCheckButtonState( pEntry ) == SV_BUTTON_CHECKED ) )
247 CheckButtonHdl();
250 else if ( GetEntryCount() )
251 SvTreeListBox::KeyInput( rKEvt );
256 SvTreeListEntry* SvxCheckListBox::InsertEntry( const OUString& rText, SvTreeListEntry* pParent, bool bChildrenOnDemand, sal_uIntPtr nPos, void* pUserData, SvLBoxButtonKind eButtonKind )
258 return SvTreeListBox::InsertEntry( rText, pParent, bChildrenOnDemand, nPos, pUserData, eButtonKind );
261 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */