xref: /AOO41X/main/sfx2/source/sidebar/ContextList.cxx (revision 3ce09a58b0d6873449cda31e55c66dba2dbc8f7f)
15d39f272SZheng Fan /**************************************************************
25d39f272SZheng Fan  *
35d39f272SZheng Fan  * Licensed to the Apache Software Foundation (ASF) under one
45d39f272SZheng Fan  * or more contributor license agreements.  See the NOTICE file
55d39f272SZheng Fan  * distributed with this work for additional information
65d39f272SZheng Fan  * regarding copyright ownership.  The ASF licenses this file
75d39f272SZheng Fan  * to you under the Apache License, Version 2.0 (the
85d39f272SZheng Fan  * "License"); you may not use this file except in compliance
95d39f272SZheng Fan  * with the License.  You may obtain a copy of the License at
105d39f272SZheng Fan  *
115d39f272SZheng Fan  *   http://www.apache.org/licenses/LICENSE-2.0
125d39f272SZheng Fan  *
135d39f272SZheng Fan  * Unless required by applicable law or agreed to in writing,
145d39f272SZheng Fan  * software distributed under the License is distributed on an
155d39f272SZheng Fan  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
165d39f272SZheng Fan  * KIND, either express or implied.  See the License for the
175d39f272SZheng Fan  * specific language governing permissions and limitations
185d39f272SZheng Fan  * under the License.
195d39f272SZheng Fan  *
205d39f272SZheng Fan  *************************************************************/
215d39f272SZheng Fan 
225d39f272SZheng Fan #include "precompiled_sfx2.hxx"
235d39f272SZheng Fan 
245d39f272SZheng Fan #include "ContextList.hxx"
255d39f272SZheng Fan #include "Context.hxx"
265d39f272SZheng Fan 
275d39f272SZheng Fan using ::rtl::OUString;
285d39f272SZheng Fan 
295d39f272SZheng Fan namespace sfx2 { namespace sidebar {
305d39f272SZheng Fan 
315d39f272SZheng Fan 
ContextList(void)325d39f272SZheng Fan ContextList::ContextList (void)
335d39f272SZheng Fan     : maEntries()
345d39f272SZheng Fan {
355d39f272SZheng Fan }
365d39f272SZheng Fan 
375d39f272SZheng Fan 
385d39f272SZheng Fan 
395d39f272SZheng Fan 
~ContextList(void)405d39f272SZheng Fan ContextList::~ContextList (void)
415d39f272SZheng Fan {
425d39f272SZheng Fan }
435d39f272SZheng Fan 
445d39f272SZheng Fan 
455d39f272SZheng Fan 
465d39f272SZheng Fan 
GetMatch(const Context & rContext) const475d39f272SZheng Fan const ContextList::Entry* ContextList::GetMatch (const Context& rContext) const
485d39f272SZheng Fan {
495d39f272SZheng Fan     const ::std::vector<Entry>::const_iterator iEntry = FindBestMatch(rContext);
505d39f272SZheng Fan     if (iEntry != maEntries.end())
515d39f272SZheng Fan         return &*iEntry;
525d39f272SZheng Fan     else
535d39f272SZheng Fan         return NULL;
545d39f272SZheng Fan }
555d39f272SZheng Fan 
565d39f272SZheng Fan 
575d39f272SZheng Fan 
585d39f272SZheng Fan 
GetMatch(const Context & rContext)59*7e429a12SAndre Fischer ContextList::Entry* ContextList::GetMatch (const Context& rContext)
60*7e429a12SAndre Fischer {
61*7e429a12SAndre Fischer     const ::std::vector<Entry>::const_iterator iEntry = FindBestMatch(rContext);
62*7e429a12SAndre Fischer     if (iEntry != maEntries.end())
63*7e429a12SAndre Fischer         return const_cast<Entry*>(&*iEntry);
64*7e429a12SAndre Fischer     else
65*7e429a12SAndre Fischer         return NULL;
66*7e429a12SAndre Fischer }
67*7e429a12SAndre Fischer 
68*7e429a12SAndre Fischer 
69*7e429a12SAndre Fischer 
70*7e429a12SAndre Fischer 
FindBestMatch(const Context & rContext) const715d39f272SZheng Fan ::std::vector<ContextList::Entry>::const_iterator ContextList::FindBestMatch (const Context& rContext) const
725d39f272SZheng Fan {
735d39f272SZheng Fan     sal_Int32 nBestMatch (Context::NoMatch);
745d39f272SZheng Fan     ::std::vector<Entry>::const_iterator iBestMatch (maEntries.end());
755d39f272SZheng Fan 
765d39f272SZheng Fan     for (::std::vector<Entry>::const_iterator
775d39f272SZheng Fan              iEntry(maEntries.begin()),
785d39f272SZheng Fan              iEnd(maEntries.end());
795d39f272SZheng Fan          iEntry!=iEnd;
805d39f272SZheng Fan          ++iEntry)
815d39f272SZheng Fan     {
825d39f272SZheng Fan         const sal_Int32 nMatch (rContext.EvaluateMatch(iEntry->maContext));
835d39f272SZheng Fan         if (nMatch < nBestMatch)
845d39f272SZheng Fan         {
855d39f272SZheng Fan             nBestMatch = nMatch;
865d39f272SZheng Fan             iBestMatch = iEntry;
875d39f272SZheng Fan         }
885d39f272SZheng Fan         if (nBestMatch == Context::OptimalMatch)
895d39f272SZheng Fan             return iEntry;
905d39f272SZheng Fan     }
915d39f272SZheng Fan 
925d39f272SZheng Fan     return iBestMatch;
935d39f272SZheng Fan }
945d39f272SZheng Fan 
955d39f272SZheng Fan 
965d39f272SZheng Fan 
975d39f272SZheng Fan 
AddContextDescription(const Context & rContext,const bool bIsInitiallyVisible,const OUString & rsMenuCommand)985d39f272SZheng Fan void ContextList::AddContextDescription (
995d39f272SZheng Fan     const Context& rContext,
1005d39f272SZheng Fan     const bool bIsInitiallyVisible,
1015d39f272SZheng Fan     const OUString& rsMenuCommand)
1025d39f272SZheng Fan {
1035d39f272SZheng Fan     maEntries.push_back(Entry());
1045d39f272SZheng Fan     maEntries.back().maContext = rContext;
1055d39f272SZheng Fan     maEntries.back().mbIsInitiallyVisible = bIsInitiallyVisible;
1065d39f272SZheng Fan     maEntries.back().msMenuCommand = rsMenuCommand;
1075d39f272SZheng Fan }
1085d39f272SZheng Fan 
1095d39f272SZheng Fan 
1105d39f272SZheng Fan 
1115d39f272SZheng Fan 
IsEmpty(void)1125d39f272SZheng Fan bool ContextList::IsEmpty (void)
1135d39f272SZheng Fan {
1145d39f272SZheng Fan     return maEntries.empty();
1155d39f272SZheng Fan }
1165d39f272SZheng Fan 
1175d39f272SZheng Fan 
1185d39f272SZheng Fan } } // end of namespace sfx2::sidebar
119