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>
22 #include <tools/solar.h>
24 #include <vcl/accel.hxx>
28 typedef ::std::map
< sal_uLong
, ImplAccelEntry
* > ImplAccelMap
;
29 typedef ::std::vector
< std::unique_ptr
<ImplAccelEntry
> > ImplAccelList
;
31 #define ACCELENTRY_NOTFOUND (sal_uInt16(0xFFFF))
36 ImplAccelMap maKeyMap
; // for keycodes, generated with a code
37 ImplAccelList maIdList
; // Id-List
40 static sal_uInt16
ImplAccelEntryGetIndex( ImplAccelList
* pList
, sal_uInt16 nId
,
41 sal_uInt16
* pIndex
= nullptr )
46 size_t nCount
= pList
->size();
47 sal_uInt16 nCompareId
;
49 // check if first key is larger then the key to compare
50 if ( !nCount
|| (nId
< (*pList
)[ 0 ]->mnId
) )
54 return ACCELENTRY_NOTFOUND
;
62 nMid
= (nLow
+ nHigh
) / 2;
63 nCompareId
= (*pList
)[ nMid
]->mnId
;
64 if ( nId
< nCompareId
)
68 if ( nId
> nCompareId
)
71 return static_cast<sal_uInt16
>(nMid
);
74 while ( nLow
<= nHigh
);
78 if ( nId
> nCompareId
)
79 *pIndex
= static_cast<sal_uInt16
>(nMid
+1);
81 *pIndex
= static_cast<sal_uInt16
>(nMid
);
84 return ACCELENTRY_NOTFOUND
;
87 static void ImplAccelEntryInsert( ImplAccelList
* pList
, std::unique_ptr
<ImplAccelEntry
> pEntry
)
89 sal_uInt16
nInsIndex(0);
90 std::vector
<ImplAccelEntry
*>::size_type nIndex
= ImplAccelEntryGetIndex( pList
, pEntry
->mnId
, &nInsIndex
);
92 if ( nIndex
!= ACCELENTRY_NOTFOUND
)
97 ImplAccelEntry
* pTempEntry
= nullptr;
98 if ( nIndex
< pList
->size() )
99 pTempEntry
= (*pList
)[ nIndex
].get();
100 if ( !pTempEntry
|| (pTempEntry
->mnId
!= pEntry
->mnId
) )
103 while ( nIndex
< pList
->size() );
105 if ( nIndex
< pList
->size() ) {
106 pList
->insert( pList
->begin() + nIndex
, std::move(pEntry
) );
108 pList
->push_back( std::move(pEntry
) );
112 if ( nInsIndex
< pList
->size() ) {
113 pList
->insert( pList
->begin() + nInsIndex
, std::move(pEntry
) );
115 pList
->push_back( std::move(pEntry
) );
120 void Accelerator::ImplInit()
126 ImplAccelEntry
* Accelerator::ImplGetAccelData( const vcl::KeyCode
& rKeyCode
) const
128 ImplAccelMap::iterator it
= mpData
->maKeyMap
.find( rKeyCode
.GetFullCode() );
129 if( it
!= mpData
->maKeyMap
.end() )
135 void Accelerator::ImplCopyData( ImplAccelData
& rAccelData
)
138 for (std::unique_ptr
<ImplAccelEntry
>& i
: rAccelData
.maIdList
)
140 std::unique_ptr
<ImplAccelEntry
> pEntry(new ImplAccelEntry( *i
));
142 // sequence accelerator, then copy also
143 if ( pEntry
->mpAccel
)
145 pEntry
->mpAccel
= new Accelerator( *(pEntry
->mpAccel
) );
146 pEntry
->mpAutoAccel
= pEntry
->mpAccel
;
149 pEntry
->mpAutoAccel
= nullptr;
151 mpData
->maKeyMap
.insert( std::make_pair( pEntry
->maKeyCode
.GetFullCode(), pEntry
.get() ) );
152 mpData
->maIdList
.push_back( std::move(pEntry
) );
156 void Accelerator::ImplDeleteData()
158 // delete accelerator-entries using the id-table
159 for (const std::unique_ptr
<ImplAccelEntry
>& pEntry
: mpData
->maIdList
) {
160 delete pEntry
->mpAutoAccel
;
162 mpData
->maIdList
.clear();
165 void Accelerator::ImplInsertAccel( sal_uInt16 nItemId
, const vcl::KeyCode
& rKeyCode
,
166 bool bEnable
, Accelerator
* pAutoAccel
)
168 SAL_WARN_IF( !nItemId
, "vcl", "Accelerator::InsertItem(): ItemId == 0" );
170 if ( rKeyCode
.IsFunction() )
176 ImplGetKeyCode( rKeyCode
.GetFunction(), nCode1
, nCode2
, nCode3
, nCode4
);
178 ImplInsertAccel( nItemId
, vcl::KeyCode( nCode1
, nCode1
), bEnable
, pAutoAccel
);
182 pAutoAccel
= new Accelerator( *pAutoAccel
);
183 ImplInsertAccel( nItemId
, vcl::KeyCode( nCode2
, nCode2
), bEnable
, pAutoAccel
);
187 pAutoAccel
= new Accelerator( *pAutoAccel
);
188 ImplInsertAccel( nItemId
, vcl::KeyCode( nCode3
, nCode3
), bEnable
, pAutoAccel
);
194 // fetch and fill new entries
195 std::unique_ptr
<ImplAccelEntry
> pEntry(new ImplAccelEntry
);
196 pEntry
->mnId
= nItemId
;
197 pEntry
->maKeyCode
= rKeyCode
;
198 pEntry
->mpAccel
= pAutoAccel
;
199 pEntry
->mpAutoAccel
= pAutoAccel
;
200 pEntry
->mbEnabled
= bEnable
;
202 // now into the tables
203 sal_uLong nCode
= rKeyCode
.GetFullCode();
206 OSL_FAIL( "Accelerator::InsertItem(): KeyCode with KeyCode 0 not allowed" );
208 else if ( !mpData
->maKeyMap
.insert( std::make_pair( nCode
, pEntry
.get() ) ).second
)
210 SAL_WARN( "vcl", "Accelerator::InsertItem(): KeyCode (Key: " << nCode
<< ") already exists" );
213 ImplAccelEntryInsert( &(mpData
->maIdList
), std::move(pEntry
) );
216 Accelerator::Accelerator()
219 mpData
.reset(new ImplAccelData
);
222 Accelerator::Accelerator(const Accelerator
& rAccel
)
223 : maCurKeyCode( rAccel
.maCurKeyCode
)
226 mpData
.reset(new ImplAccelData
);
227 ImplCopyData(*rAccel
.mpData
);
230 Accelerator::~Accelerator()
233 // inform AccelManager about deleting the Accelerator
240 void Accelerator::Activate()
242 maActivateHdl
.Call( *this );
245 void Accelerator::Select()
247 maSelectHdl
.Call( *this );
250 void Accelerator::InsertItem( sal_uInt16 nItemId
, const vcl::KeyCode
& rKeyCode
)
252 ImplInsertAccel( nItemId
, rKeyCode
, true, nullptr );
255 sal_uInt16
Accelerator::GetItemCount() const
258 return static_cast<sal_uInt16
>(mpData
->maIdList
.size());
261 sal_uInt16
Accelerator::GetItemId( sal_uInt16 nPos
) const
264 ImplAccelEntry
* pEntry
= ( nPos
< mpData
->maIdList
.size() ) ? mpData
->maIdList
[ nPos
].get() : nullptr;
271 Accelerator
* Accelerator::GetAccel( sal_uInt16 nItemId
) const
274 sal_uInt16 nIndex
= ImplAccelEntryGetIndex( &(mpData
->maIdList
), nItemId
);
275 if ( nIndex
!= ACCELENTRY_NOTFOUND
)
276 return mpData
->maIdList
[ nIndex
]->mpAccel
;
281 Accelerator
& Accelerator::operator=( const Accelerator
& rAccel
)
287 maCurKeyCode
= vcl::KeyCode();
290 // delete and copy tables
292 mpData
->maKeyMap
.clear();
293 ImplCopyData(*rAccel
.mpData
);
298 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */