Version 7.1.7.1, tag libreoffice-7.1.7.1
[LibreOffice.git] / sfx2 / source / sidebar / ContextList.cxx
blob36e9872d3d0ba4dcdbf392b81f2f1231c9f899eb
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 .
19 #include <sidebar/ContextList.hxx>
20 #include <sfx2/sidebar/Context.hxx>
22 namespace sfx2::sidebar {
24 ContextList::ContextList()
25 : maEntries()
29 const ContextList::Entry* ContextList::GetMatch (const Context& rContext) const
31 const ::std::vector<Entry>::const_iterator iEntry = FindBestMatch(rContext);
32 if (iEntry != maEntries.end())
33 return &*iEntry;
34 else
35 return nullptr;
38 ContextList::Entry* ContextList::GetMatch (const Context& rContext)
40 const ::std::vector<Entry>::const_iterator iEntry = FindBestMatch(rContext);
41 if (iEntry != maEntries.end())
42 return const_cast<Entry*>(&*iEntry);
43 else
44 return nullptr;
47 ::std::vector<ContextList::Entry>::const_iterator ContextList::FindBestMatch (const Context& rContext) const
49 sal_Int32 nBestMatch (Context::NoMatch);
50 ::std::vector<Entry>::const_iterator iBestMatch (maEntries.end());
52 for (::std::vector<Entry>::const_iterator
53 iEntry(maEntries.begin()),
54 iEnd(maEntries.end());
55 iEntry!=iEnd;
56 ++iEntry)
58 const sal_Int32 nMatch (rContext.EvaluateMatch(iEntry->maContext));
59 if (nMatch < nBestMatch)
61 nBestMatch = nMatch;
62 iBestMatch = iEntry;
64 if (nBestMatch == Context::OptimalMatch)
65 return iEntry;
68 return iBestMatch;
71 void ContextList::AddContextDescription (
72 const Context& rContext,
73 const bool bIsInitiallyVisible,
74 const OUString& rsMenuCommand)
76 maEntries.emplace_back();
77 maEntries.back().maContext = rContext;
78 maEntries.back().mbIsInitiallyVisible = bIsInitiallyVisible;
79 maEntries.back().msMenuCommand = rsMenuCommand;
82 void ContextList::ToggleVisibilityForContext( const Context &rContext, const bool bVisible)
84 ContextList::Entry *pEntry = GetMatch( rContext );
86 if ( !pEntry )
87 return;
89 const sal_Int32 nMatch( rContext.EvaluateMatch( pEntry->maContext ) );
91 if ( nMatch & Context::ApplicationWildcardMatch )
93 // Create a separate context list entry for this app if 'any'
94 // is the only context that matches. Toggling the visibility
95 // for 'any' would change it for all apps, not just this one
96 AddContextDescription( rContext, bVisible, OUString() );
98 else if ( nMatch == Context::OptimalMatch || nMatch == Context::ContextWildcardMatch )
99 pEntry->mbIsInitiallyVisible = bVisible;
102 } // end of namespace sfx2::sidebar
104 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */