1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 <sal/log.hxx>
21 #include <osl/diagnose.h>
24 #define ACCELENTRY_NOTFOUND (sal_uInt16(0xFFFF))
26 static sal_uInt16
ImplAccelEntryGetIndex( const ImplAccelList
* pList
, sal_uInt16 nId
,
27 sal_uInt16
* pIndex
= nullptr )
32 size_t nCount
= pList
->size();
33 sal_uInt16 nCompareId
;
35 // check if first key is larger then the key to compare
36 if ( !nCount
|| (nId
< (*pList
)[ 0 ]->mnId
) )
40 return ACCELENTRY_NOTFOUND
;
48 nMid
= (nLow
+ nHigh
) / 2;
49 nCompareId
= (*pList
)[ nMid
]->mnId
;
50 if ( nId
< nCompareId
)
54 if ( nId
> nCompareId
)
57 return static_cast<sal_uInt16
>(nMid
);
60 while ( nLow
<= nHigh
);
64 if ( nId
> nCompareId
)
65 *pIndex
= static_cast<sal_uInt16
>(nMid
+1);
67 *pIndex
= static_cast<sal_uInt16
>(nMid
);
70 return ACCELENTRY_NOTFOUND
;
73 static void ImplAccelEntryInsert( ImplAccelList
* pList
, std::unique_ptr
<ImplAccelEntry
> pEntry
)
75 sal_uInt16
nInsIndex(0);
76 std::vector
<ImplAccelEntry
*>::size_type nIndex
= ImplAccelEntryGetIndex( pList
, pEntry
->mnId
, &nInsIndex
);
78 if ( nIndex
!= ACCELENTRY_NOTFOUND
)
83 ImplAccelEntry
* pTempEntry
= nullptr;
84 if ( nIndex
< pList
->size() )
85 pTempEntry
= (*pList
)[ nIndex
].get();
86 if ( !pTempEntry
|| (pTempEntry
->mnId
!= pEntry
->mnId
) )
89 while ( nIndex
< pList
->size() );
91 if ( nIndex
< pList
->size() ) {
92 pList
->insert( pList
->begin() + nIndex
, std::move(pEntry
) );
94 pList
->push_back( std::move(pEntry
) );
98 if ( nInsIndex
< pList
->size() ) {
99 pList
->insert( pList
->begin() + nInsIndex
, std::move(pEntry
) );
101 pList
->push_back( std::move(pEntry
) );
106 void Accelerator::ImplInit()
112 ImplAccelEntry
* Accelerator::ImplGetAccelData( const vcl::KeyCode
& rKeyCode
) const
114 auto it
= maKeyMap
.find( rKeyCode
.GetFullCode() );
115 if( it
!= maKeyMap
.end() )
121 void Accelerator::ImplCopyData( const Accelerator
& rAccelData
)
124 for (const std::unique_ptr
<ImplAccelEntry
>& i
: rAccelData
.maIdList
)
126 std::unique_ptr
<ImplAccelEntry
> pEntry(new ImplAccelEntry( *i
));
128 // sequence accelerator, then copy also
129 if ( pEntry
->mpAccel
)
131 pEntry
->mpAccel
= new Accelerator( *(pEntry
->mpAccel
) );
132 pEntry
->mpAutoAccel
= pEntry
->mpAccel
;
135 pEntry
->mpAutoAccel
= nullptr;
137 maKeyMap
.insert( std::make_pair( pEntry
->maKeyCode
.GetFullCode(), pEntry
.get() ) );
138 maIdList
.push_back( std::move(pEntry
) );
142 void Accelerator::ImplDeleteData()
144 // delete accelerator-entries using the id-table
145 for (const std::unique_ptr
<ImplAccelEntry
>& pEntry
: maIdList
) {
146 delete pEntry
->mpAutoAccel
;
151 void Accelerator::ImplInsertAccel( sal_uInt16 nItemId
, const vcl::KeyCode
& rKeyCode
,
152 bool bEnable
, Accelerator
* pAutoAccel
)
154 SAL_WARN_IF( !nItemId
, "vcl", "Accelerator::InsertItem(): ItemId == 0" );
156 if ( rKeyCode
.IsFunction() )
162 ImplGetKeyCode( rKeyCode
.GetFunction(), nCode1
, nCode2
, nCode3
, nCode4
);
164 ImplInsertAccel( nItemId
, vcl::KeyCode( nCode1
, nCode1
), bEnable
, pAutoAccel
);
168 pAutoAccel
= new Accelerator( *pAutoAccel
);
169 ImplInsertAccel( nItemId
, vcl::KeyCode( nCode2
, nCode2
), bEnable
, pAutoAccel
);
173 pAutoAccel
= new Accelerator( *pAutoAccel
);
174 ImplInsertAccel( nItemId
, vcl::KeyCode( nCode3
, nCode3
), bEnable
, pAutoAccel
);
180 // fetch and fill new entries
181 std::unique_ptr
<ImplAccelEntry
> pEntry(new ImplAccelEntry
);
182 pEntry
->mnId
= nItemId
;
183 pEntry
->maKeyCode
= rKeyCode
;
184 pEntry
->mpAccel
= pAutoAccel
;
185 pEntry
->mpAutoAccel
= pAutoAccel
;
186 pEntry
->mbEnabled
= bEnable
;
188 // now into the tables
189 sal_uLong nCode
= rKeyCode
.GetFullCode();
192 OSL_FAIL( "Accelerator::InsertItem(): KeyCode with KeyCode 0 not allowed" );
194 else if ( !maKeyMap
.insert( std::make_pair( nCode
, pEntry
.get() ) ).second
)
196 SAL_WARN( "vcl", "Accelerator::InsertItem(): KeyCode (Key: " << nCode
<< ") already exists" );
199 ImplAccelEntryInsert( &maIdList
, std::move(pEntry
) );
202 Accelerator::Accelerator()
207 Accelerator::Accelerator(const Accelerator
& rAccel
)
210 ImplCopyData(rAccel
);
213 Accelerator::~Accelerator()
216 // inform AccelManager about deleting the Accelerator
223 void Accelerator::Activate()
225 maActivateHdl
.Call( *this );
228 void Accelerator::Select()
230 maSelectHdl
.Call( *this );
233 void Accelerator::InsertItem( sal_uInt16 nItemId
, const vcl::KeyCode
& rKeyCode
)
235 ImplInsertAccel( nItemId
, rKeyCode
, true, nullptr );
238 sal_uInt16
Accelerator::GetItemCount() const
240 return static_cast<sal_uInt16
>(maIdList
.size());
243 sal_uInt16
Accelerator::GetItemId( sal_uInt16 nPos
) const
246 ImplAccelEntry
* pEntry
= ( nPos
< maIdList
.size() ) ? maIdList
[ nPos
].get() : nullptr;
253 Accelerator
* Accelerator::GetAccel( sal_uInt16 nItemId
) const
256 sal_uInt16 nIndex
= ImplAccelEntryGetIndex( &maIdList
, nItemId
);
257 if ( nIndex
!= ACCELENTRY_NOTFOUND
)
258 return maIdList
[ nIndex
]->mpAccel
;
263 Accelerator
& Accelerator::operator=( const Accelerator
& rAccel
)
271 // delete and copy tables
274 ImplCopyData(rAccel
);
279 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */