bump product version to 6.4.0.3
[LibreOffice.git] / vcl / source / window / accel.cxx
blob1795d04968299e84986efb00c4ca28c5bdd62bbf
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 <tools/solar.h>
23 #include <accel.h>
24 #include <vcl/accel.hxx>
25 #include <map>
26 #include <vector>
28 typedef ::std::map< sal_uLong, ImplAccelEntry* > ImplAccelMap;
29 typedef ::std::vector< std::unique_ptr<ImplAccelEntry> > ImplAccelList;
31 #define ACCELENTRY_NOTFOUND (sal_uInt16(0xFFFF))
33 class ImplAccelData
35 public:
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 )
43 size_t nLow;
44 size_t nHigh;
45 size_t nMid;
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) )
52 if ( pIndex )
53 *pIndex = 0;
54 return ACCELENTRY_NOTFOUND;
57 // Binary search
58 nLow = 0;
59 nHigh = nCount-1;
62 nMid = (nLow + nHigh) / 2;
63 nCompareId = (*pList)[ nMid ]->mnId;
64 if ( nId < nCompareId )
65 nHigh = nMid-1;
66 else
68 if ( nId > nCompareId )
69 nLow = nMid + 1;
70 else
71 return static_cast<sal_uInt16>(nMid);
74 while ( nLow <= nHigh );
76 if ( pIndex )
78 if ( nId > nCompareId )
79 *pIndex = static_cast<sal_uInt16>(nMid+1);
80 else
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 )
96 nIndex++;
97 ImplAccelEntry* pTempEntry = nullptr;
98 if ( nIndex < pList->size() )
99 pTempEntry = (*pList)[ nIndex ].get();
100 if ( !pTempEntry || (pTempEntry->mnId != pEntry->mnId) )
101 break;
103 while ( nIndex < pList->size() );
105 if ( nIndex < pList->size() ) {
106 pList->insert( pList->begin() + nIndex, std::move(pEntry) );
107 } else {
108 pList->push_back( std::move(pEntry) );
111 else {
112 if ( nInsIndex < pList->size() ) {
113 pList->insert( pList->begin() + nInsIndex, std::move(pEntry) );
114 } else {
115 pList->push_back( std::move(pEntry) );
120 void Accelerator::ImplInit()
122 mnCurId = 0;
123 mpDel = nullptr;
126 ImplAccelEntry* Accelerator::ImplGetAccelData( const vcl::KeyCode& rKeyCode ) const
128 ImplAccelMap::iterator it = mpData->maKeyMap.find( rKeyCode.GetFullCode() );
129 if( it != mpData->maKeyMap.end() )
130 return it->second;
131 else
132 return nullptr;
135 void Accelerator::ImplCopyData( ImplAccelData& rAccelData )
137 // copy table
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;
148 else
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() )
172 sal_uInt16 nCode1;
173 sal_uInt16 nCode2;
174 sal_uInt16 nCode3;
175 sal_uInt16 nCode4;
176 ImplGetKeyCode( rKeyCode.GetFunction(), nCode1, nCode2, nCode3, nCode4 );
177 if ( nCode1 )
178 ImplInsertAccel( nItemId, vcl::KeyCode( nCode1, nCode1 ), bEnable, pAutoAccel );
179 if ( nCode2 )
181 if ( pAutoAccel )
182 pAutoAccel = new Accelerator( *pAutoAccel );
183 ImplInsertAccel( nItemId, vcl::KeyCode( nCode2, nCode2 ), bEnable, pAutoAccel );
184 if ( nCode3 )
186 if ( pAutoAccel )
187 pAutoAccel = new Accelerator( *pAutoAccel );
188 ImplInsertAccel( nItemId, vcl::KeyCode( nCode3, nCode3 ), bEnable, pAutoAccel );
191 return;
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();
204 if ( !nCode )
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" );
212 else
213 ImplAccelEntryInsert( &(mpData->maIdList), std::move(pEntry) );
216 Accelerator::Accelerator()
218 ImplInit();
219 mpData.reset(new ImplAccelData);
222 Accelerator::Accelerator(const Accelerator& rAccel)
223 : maCurKeyCode( rAccel.maCurKeyCode )
225 ImplInit();
226 mpData.reset(new ImplAccelData);
227 ImplCopyData(*rAccel.mpData);
230 Accelerator::~Accelerator()
233 // inform AccelManager about deleting the Accelerator
234 if ( mpDel )
235 *mpDel = true;
237 ImplDeleteData();
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;
265 if ( pEntry )
266 return pEntry->mnId;
267 else
268 return 0;
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;
277 else
278 return nullptr;
281 Accelerator& Accelerator::operator=( const Accelerator& rAccel )
283 if(this == &rAccel)
284 return *this;
286 // assign new data
287 maCurKeyCode = vcl::KeyCode();
288 mnCurId = 0;
290 // delete and copy tables
291 ImplDeleteData();
292 mpData->maKeyMap.clear();
293 ImplCopyData(*rAccel.mpData);
295 return *this;
298 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */