xref: /trunk/main/sfx2/source/sidebar/ContextList.cxx (revision f120fe41)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 #include "precompiled_sfx2.hxx"
23 
24 #include "ContextList.hxx"
25 #include "Context.hxx"
26 
27 using ::rtl::OUString;
28 
29 namespace sfx2 { namespace sidebar {
30 
31 namespace {
32     static const sal_Char* gsAny = "any";
33 }
34 
35 
36 
37 ContextList::ContextList (void)
38     : maEntries()
39 {
40 }
41 
42 
43 
44 
45 ContextList::~ContextList (void)
46 {
47 }
48 
49 
50 
51 
52 const ContextList::Entry* ContextList::GetMatch (const Context& rContext) const
53 {
54     const ::std::vector<Entry>::const_iterator iEntry = FindBestMatch(rContext);
55     if (iEntry != maEntries.end())
56         return &*iEntry;
57     else
58         return NULL;
59 }
60 
61 
62 
63 
64 ::std::vector<ContextList::Entry>::const_iterator ContextList::FindBestMatch (const Context& rContext) const
65 {
66     sal_Int32 nBestMatch (Context::NoMatch);
67     ::std::vector<Entry>::const_iterator iBestMatch (maEntries.end());
68 
69     for (::std::vector<Entry>::const_iterator
70              iEntry(maEntries.begin()),
71              iEnd(maEntries.end());
72          iEntry!=iEnd;
73          ++iEntry)
74     {
75         const sal_Int32 nMatch (rContext.EvaluateMatch(iEntry->maContext));
76         if (nMatch < nBestMatch)
77         {
78             nBestMatch = nMatch;
79             iBestMatch = iEntry;
80         }
81         if (nBestMatch == Context::OptimalMatch)
82             return iEntry;
83     }
84 
85     return iBestMatch;
86 }
87 
88 
89 
90 
91 void ContextList::AddContextDescription (
92     const Context& rContext,
93     const bool bIsInitiallyVisible,
94     const OUString& rsMenuCommand)
95 {
96     maEntries.push_back(Entry());
97     maEntries.back().maContext = rContext;
98     maEntries.back().mbIsInitiallyVisible = bIsInitiallyVisible;
99     maEntries.back().msMenuCommand = rsMenuCommand;
100 }
101 
102 
103 
104 
105 bool ContextList::IsEmpty (void)
106 {
107     return maEntries.empty();
108 }
109 
110 
111 } } // end of namespace sfx2::sidebar
112