Branch libreoffice-5-0-4
[LibreOffice.git] / sfx2 / source / sidebar / ContextList.cxx
bloba64b43a8f5af87cf9ec38576521d9409a92f8321
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 "ContextList.hxx"
20 #include "Context.hxx"
22 using ::rtl::OUString;
24 namespace sfx2 { namespace sidebar {
26 ContextList::ContextList()
27 : maEntries()
31 ContextList::~ContextList()
35 const ContextList::Entry* ContextList::GetMatch (const Context& rContext) const
37 const ::std::vector<Entry>::const_iterator iEntry = FindBestMatch(rContext);
38 if (iEntry != maEntries.end())
39 return &*iEntry;
40 else
41 return NULL;
44 ContextList::Entry* ContextList::GetMatch (const Context& rContext)
46 const ::std::vector<Entry>::const_iterator iEntry = FindBestMatch(rContext);
47 if (iEntry != maEntries.end())
48 return const_cast<Entry*>(&*iEntry);
49 else
50 return NULL;
53 ::std::vector<ContextList::Entry>::const_iterator ContextList::FindBestMatch (const Context& rContext) const
55 sal_Int32 nBestMatch (Context::NoMatch);
56 ::std::vector<Entry>::const_iterator iBestMatch (maEntries.end());
58 for (::std::vector<Entry>::const_iterator
59 iEntry(maEntries.begin()),
60 iEnd(maEntries.end());
61 iEntry!=iEnd;
62 ++iEntry)
64 const sal_Int32 nMatch (rContext.EvaluateMatch(iEntry->maContext));
65 if (nMatch < nBestMatch)
67 nBestMatch = nMatch;
68 iBestMatch = iEntry;
70 if (nBestMatch == Context::OptimalMatch)
71 return iEntry;
74 return iBestMatch;
77 void ContextList::AddContextDescription (
78 const Context& rContext,
79 const bool bIsInitiallyVisible,
80 const OUString& rsMenuCommand)
82 maEntries.push_back(Entry());
83 maEntries.back().maContext = rContext;
84 maEntries.back().mbIsInitiallyVisible = bIsInitiallyVisible;
85 maEntries.back().msMenuCommand = rsMenuCommand;
88 } } // end of namespace sfx2::sidebar
90 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */