Simplify using designated initializers
[LibreOffice.git] / vcl / source / window / accel.cxx
bloba20c289f201ab25ab1262175ac9f3806330515b2
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 <sal/log.hxx>
21 #include <osl/diagnose.h>
22 #include <accel.hxx>
24 #define ACCELENTRY_NOTFOUND (sal_uInt16(0xFFFF))
26 static sal_uInt16 ImplAccelEntryGetIndex( const ImplAccelList* pList, sal_uInt16 nId,
27 sal_uInt16* pIndex = nullptr )
29 size_t nLow;
30 size_t nHigh;
31 size_t nMid;
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) )
38 if ( pIndex )
39 *pIndex = 0;
40 return ACCELENTRY_NOTFOUND;
43 // Binary search
44 nLow = 0;
45 nHigh = nCount-1;
48 nMid = (nLow + nHigh) / 2;
49 nCompareId = (*pList)[ nMid ]->mnId;
50 if ( nId < nCompareId )
51 nHigh = nMid-1;
52 else
54 if ( nId > nCompareId )
55 nLow = nMid + 1;
56 else
57 return static_cast<sal_uInt16>(nMid);
60 while ( nLow <= nHigh );
62 if ( pIndex )
64 if ( nId > nCompareId )
65 *pIndex = static_cast<sal_uInt16>(nMid+1);
66 else
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 )
82 nIndex++;
83 ImplAccelEntry* pTempEntry = nullptr;
84 if ( nIndex < pList->size() )
85 pTempEntry = (*pList)[ nIndex ].get();
86 if ( !pTempEntry || (pTempEntry->mnId != pEntry->mnId) )
87 break;
89 while ( nIndex < pList->size() );
91 if ( nIndex < pList->size() ) {
92 pList->insert( pList->begin() + nIndex, std::move(pEntry) );
93 } else {
94 pList->push_back( std::move(pEntry) );
97 else {
98 if ( nInsIndex < pList->size() ) {
99 pList->insert( pList->begin() + nInsIndex, std::move(pEntry) );
100 } else {
101 pList->push_back( std::move(pEntry) );
106 void Accelerator::ImplInit()
108 mnCurId = 0;
109 mpDel = nullptr;
112 ImplAccelEntry* Accelerator::ImplGetAccelData( const vcl::KeyCode& rKeyCode ) const
114 auto it = maKeyMap.find( rKeyCode.GetFullCode() );
115 if( it != maKeyMap.end() )
116 return it->second;
117 else
118 return nullptr;
121 void Accelerator::ImplCopyData( const Accelerator& rAccelData )
123 // copy table
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;
134 else
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;
148 maIdList.clear();
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() )
158 sal_uInt16 nCode1;
159 sal_uInt16 nCode2;
160 sal_uInt16 nCode3;
161 sal_uInt16 nCode4;
162 ImplGetKeyCode( rKeyCode.GetFunction(), nCode1, nCode2, nCode3, nCode4 );
163 if ( nCode1 )
164 ImplInsertAccel( nItemId, vcl::KeyCode( nCode1, nCode1 ), bEnable, pAutoAccel );
165 if ( nCode2 )
167 if ( pAutoAccel )
168 pAutoAccel = new Accelerator( *pAutoAccel );
169 ImplInsertAccel( nItemId, vcl::KeyCode( nCode2, nCode2 ), bEnable, pAutoAccel );
170 if ( nCode3 )
172 if ( pAutoAccel )
173 pAutoAccel = new Accelerator( *pAutoAccel );
174 ImplInsertAccel( nItemId, vcl::KeyCode( nCode3, nCode3 ), bEnable, pAutoAccel );
177 return;
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();
190 if ( !nCode )
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" );
198 else
199 ImplAccelEntryInsert( &maIdList, std::move(pEntry) );
202 Accelerator::Accelerator()
204 ImplInit();
207 Accelerator::Accelerator(const Accelerator& rAccel)
209 ImplInit();
210 ImplCopyData(rAccel);
213 Accelerator::~Accelerator()
216 // inform AccelManager about deleting the Accelerator
217 if ( mpDel )
218 *mpDel = true;
220 ImplDeleteData();
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;
247 if ( pEntry )
248 return pEntry->mnId;
249 else
250 return 0;
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;
259 else
260 return nullptr;
263 Accelerator& Accelerator::operator=( const Accelerator& rAccel )
265 if(this == &rAccel)
266 return *this;
268 // assign new data
269 mnCurId = 0;
271 // delete and copy tables
272 ImplDeleteData();
273 maKeyMap.clear();
274 ImplCopyData(rAccel);
276 return *this;
279 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */