Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
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 .
18 : */
19 :
20 : #include <osl/mutex.hxx>
21 : #include <ucbhelper/content.hxx>
22 : #include <vcl/msgbox.hxx>
23 :
24 : #include <sfx2/dispatch.hxx>
25 : #include <sfx2/docfile.hxx>
26 :
27 : #include <map>
28 : #include <vector>
29 : #include <iterator>
30 :
31 : #include "symbol.hxx"
32 : #include "view.hxx"
33 : #include "utility.hxx"
34 : #include "dialog.hxx"
35 : #include "config.hxx"
36 : #include "cfgitem.hxx"
37 : #include "smmod.hxx"
38 : #include "starmath.hrc"
39 :
40 :
41 : using namespace ::com::sun::star;
42 : using namespace ::com::sun::star::ucb;
43 : using namespace ::com::sun::star::uno;
44 : using namespace ::rtl;
45 :
46 :
47 : /**************************************************************************/
48 :
49 0 : SmSym::SmSym() :
50 : m_aName(OUString("unknown")),
51 : m_aSetName(OUString("unknown")),
52 : m_cChar('\0'),
53 : m_bPredefined(false),
54 0 : m_bDocSymbol(false)
55 : {
56 0 : m_aExportName = m_aName;
57 0 : m_aFace.SetTransparent(true);
58 0 : m_aFace.SetAlign(ALIGN_BASELINE);
59 0 : }
60 :
61 :
62 0 : SmSym::SmSym(const SmSym& rSymbol)
63 : {
64 0 : *this = rSymbol;
65 0 : }
66 :
67 :
68 0 : SmSym::SmSym(const OUString& rName, const Font& rFont, sal_UCS4 cChar,
69 0 : const OUString& rSet, bool bIsPredefined)
70 : {
71 0 : m_aName = m_aExportName = rName;
72 :
73 0 : m_aFace = rFont;
74 0 : m_aFace.SetTransparent(true);
75 0 : m_aFace.SetAlign(ALIGN_BASELINE);
76 :
77 0 : m_cChar = cChar;
78 0 : m_aSetName = rSet;
79 0 : m_bPredefined = bIsPredefined;
80 0 : m_bDocSymbol = false;
81 0 : }
82 :
83 :
84 0 : SmSym& SmSym::operator = (const SmSym& rSymbol)
85 : {
86 0 : m_aName = rSymbol.m_aName;
87 0 : m_aExportName = rSymbol.m_aExportName;
88 0 : m_cChar = rSymbol.m_cChar;
89 0 : m_aFace = rSymbol.m_aFace;
90 0 : m_aSetName = rSymbol.m_aSetName;
91 0 : m_bPredefined = rSymbol.m_bPredefined;
92 0 : m_bDocSymbol = rSymbol.m_bDocSymbol;
93 :
94 0 : SmSymbolManager * pSymSetManager = &SM_MOD()->GetSymbolManager();
95 0 : if (pSymSetManager)
96 0 : pSymSetManager->SetModified(true);
97 :
98 0 : return *this;
99 : }
100 :
101 :
102 0 : bool SmSym::IsEqualInUI( const SmSym& rSymbol ) const
103 : {
104 0 : return m_aName == rSymbol.m_aName &&
105 0 : m_aFace == rSymbol.m_aFace &&
106 0 : m_cChar == rSymbol.m_cChar;
107 : }
108 :
109 : /**************************************************************************/
110 :
111 0 : void SmSymbolManager::SFX_NOTIFY(SfxBroadcaster& /*rBC*/, const TypeId& rBCType,
112 : const SfxHint& /*rHint*/, const TypeId& rHintType)
113 : {
114 0 : }
115 :
116 :
117 0 : SmSymbolManager::SmSymbolManager()
118 : {
119 0 : m_bModified = false;
120 0 : }
121 :
122 :
123 0 : SmSymbolManager::SmSymbolManager(const SmSymbolManager& rSymbolSetManager) :
124 0 : SfxListener()
125 : {
126 0 : m_aSymbols = rSymbolSetManager.m_aSymbols;
127 0 : m_bModified = true;
128 0 : }
129 :
130 :
131 0 : SmSymbolManager::~SmSymbolManager()
132 : {
133 0 : }
134 :
135 :
136 0 : SmSymbolManager& SmSymbolManager::operator = (const SmSymbolManager& rSymbolSetManager)
137 : {
138 0 : m_aSymbols = rSymbolSetManager.m_aSymbols;
139 0 : m_bModified = true;
140 0 : return *this;
141 : }
142 :
143 :
144 0 : SmSym *SmSymbolManager::GetSymbolByName(const OUString& rSymbolName)
145 : {
146 0 : SmSym *pRes = NULL;
147 0 : SymbolMap_t::iterator aIt( m_aSymbols.find( rSymbolName ) );
148 0 : if (aIt != m_aSymbols.end())
149 0 : pRes = &aIt->second;
150 0 : return pRes;
151 : }
152 :
153 :
154 0 : const SymbolPtrVec_t SmSymbolManager::GetSymbols() const
155 : {
156 0 : SymbolPtrVec_t aRes;
157 0 : SymbolMap_t::const_iterator aIt( m_aSymbols.begin() );
158 0 : for ( ; aIt != m_aSymbols.end(); ++aIt)
159 0 : aRes.push_back( &aIt->second );
160 : // OSL_ENSURE( sSymbols.size() == m_aSymbols.size(), "number of symbols mismatch " );
161 0 : return aRes;
162 : }
163 :
164 :
165 0 : bool SmSymbolManager::AddOrReplaceSymbol( const SmSym &rSymbol, bool bForceChange )
166 : {
167 0 : bool bAdded = false;
168 :
169 0 : const OUString aSymbolName( rSymbol.GetName() );
170 0 : if (aSymbolName.getLength() > 0 && rSymbol.GetSymbolSetName().getLength() > 0)
171 : {
172 0 : const SmSym *pFound = GetSymbolByName( aSymbolName );
173 0 : const bool bSymbolConflict = pFound && !pFound->IsEqualInUI( rSymbol );
174 :
175 : // avoid having the same symbol name twice but with different symbols in use
176 0 : if (!pFound || bForceChange)
177 : {
178 0 : m_aSymbols[ aSymbolName ] = rSymbol;
179 0 : bAdded = true;
180 : }
181 0 : else if (pFound && !bForceChange && bSymbolConflict)
182 : {
183 : // TODO: to solve this a document owned symbol manager would be required ...
184 : SAL_WARN("starmath", "symbol conflict, different symbol with same name found!");
185 : // symbols in all formulas. A copy of the global one would be needed here
186 : // and then the new symbol has to be forcefully applied. This would keep
187 : // the current formula intact but will leave the set of symbols in the
188 : // global symbol manager somewhat to chance.
189 : }
190 :
191 : OSL_ENSURE( bAdded, "failed to add symbol" );
192 0 : if (bAdded)
193 0 : m_bModified = true;
194 : OSL_ENSURE( bAdded || (pFound && !bSymbolConflict), "AddOrReplaceSymbol: unresolved symbol conflict" );
195 : }
196 :
197 0 : return bAdded;
198 : }
199 :
200 :
201 0 : void SmSymbolManager::RemoveSymbol( const OUString & rSymbolName )
202 : {
203 0 : if (rSymbolName.getLength() > 0)
204 : {
205 0 : size_t nOldSize = m_aSymbols.size();
206 0 : m_aSymbols.erase( rSymbolName );
207 0 : m_bModified = nOldSize != m_aSymbols.size();
208 : }
209 0 : }
210 :
211 :
212 0 : std::set< OUString > SmSymbolManager::GetSymbolSetNames() const
213 : {
214 0 : std::set< OUString > aRes;
215 0 : SymbolMap_t::const_iterator aIt( m_aSymbols.begin() );
216 0 : for ( ; aIt != m_aSymbols.end(); ++aIt )
217 0 : aRes.insert( aIt->second.GetSymbolSetName() );
218 0 : return aRes;
219 : }
220 :
221 :
222 0 : const SymbolPtrVec_t SmSymbolManager::GetSymbolSet( const OUString& rSymbolSetName )
223 : {
224 0 : SymbolPtrVec_t aRes;
225 0 : if (rSymbolSetName.getLength() > 0)
226 : {
227 0 : SymbolMap_t::const_iterator aIt( m_aSymbols.begin() );
228 0 : for ( ; aIt != m_aSymbols.end(); ++aIt )
229 : {
230 0 : if (aIt->second.GetSymbolSetName() == rSymbolSetName)
231 0 : aRes.push_back( &aIt->second );
232 : }
233 : }
234 0 : return aRes;
235 : }
236 :
237 :
238 0 : void SmSymbolManager::Load()
239 : {
240 0 : std::vector< SmSym > aSymbols;
241 0 : SmMathConfig &rCfg = *SM_MOD()->GetConfig();
242 0 : rCfg.GetSymbols( aSymbols );
243 0 : size_t nSymbolCount = aSymbols.size();
244 :
245 0 : m_aSymbols.clear();
246 0 : for (size_t i = 0; i < nSymbolCount; ++i)
247 : {
248 0 : const SmSym &rSym = aSymbols[i];
249 : OSL_ENSURE( rSym.GetName().getLength() > 0, "symbol without name!" );
250 0 : if (rSym.GetName().getLength() > 0)
251 0 : AddOrReplaceSymbol( rSym );
252 : }
253 0 : m_bModified = true;
254 :
255 0 : if (0 == nSymbolCount)
256 : {
257 : SAL_WARN("starmath", "no symbol set found");
258 0 : m_bModified = false;
259 : }
260 :
261 : // now add a %i... symbol to the 'iGreek' set for every symbol found in the 'Greek' set.
262 0 : SmLocalizedSymbolData aLocalizedData;
263 0 : const OUString aGreekSymbolSetName(aLocalizedData.GetUiSymbolSetName(OUString("Greek")));
264 0 : const SymbolPtrVec_t aGreekSymbols( GetSymbolSet( aGreekSymbolSetName ) );
265 0 : OUString aSymbolSetName('i');
266 0 : aSymbolSetName += aGreekSymbolSetName;
267 0 : size_t nSymbols = aGreekSymbols.size();
268 0 : for (size_t i = 0; i < nSymbols; ++i)
269 : {
270 : // make the new symbol a copy but with ITALIC_NORMAL, and add it to iGreek
271 0 : const SmSym &rSym = *aGreekSymbols[i];
272 0 : Font aFont( rSym.GetFace() );
273 : OSL_ENSURE( aFont.GetItalic() == ITALIC_NONE, "expected Font with ITALIC_NONE, failed." );
274 0 : aFont.SetItalic( ITALIC_NORMAL );
275 0 : OUString aSymbolName('i');
276 0 : aSymbolName += rSym.GetName();
277 : SmSym aSymbol( aSymbolName, aFont, rSym.GetCharacter(),
278 0 : aSymbolSetName, true /*bIsPredefined*/ );
279 :
280 0 : AddOrReplaceSymbol( aSymbol );
281 0 : }
282 0 : }
283 :
284 0 : void SmSymbolManager::Save()
285 : {
286 0 : if (m_bModified)
287 : {
288 0 : SmMathConfig &rCfg = *SM_MOD()->GetConfig();
289 :
290 : // prepare to skip symbols from iGreek on saving
291 0 : SmLocalizedSymbolData aLocalizedData;
292 0 : OUString aSymbolSetName('i');
293 0 : aSymbolSetName += aLocalizedData.GetUiSymbolSetName(OUString("Greek"));
294 :
295 0 : SymbolPtrVec_t aTmp( GetSymbols() );
296 0 : std::vector< SmSym > aSymbols;
297 0 : for (size_t i = 0; i < aTmp.size(); ++i)
298 : {
299 : // skip symbols from iGreek set since those symbols always get added
300 : // by computational means in SmSymbolManager::Load
301 0 : if (aTmp[i]->GetSymbolSetName() != aSymbolSetName)
302 0 : aSymbols.push_back( *aTmp[i] );
303 : }
304 0 : rCfg.SetSymbols( aSymbols );
305 :
306 0 : m_bModified = false;
307 : }
308 0 : }
309 :
310 :
311 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|