1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 .
20 #include <osl/mutex.hxx>
21 #include <ucbhelper/content.hxx>
22 #include <vcl/msgbox.hxx>
24 #include <sfx2/dispatch.hxx>
25 #include <sfx2/docfile.hxx>
33 #include "utility.hxx"
36 #include "cfgitem.hxx"
38 #include "starmath.hrc"
41 using namespace ::com::sun::star
;
42 using namespace ::com::sun::star::ucb
;
43 using namespace ::com::sun::star::uno
;
46 /**************************************************************************/
49 m_aName(OUString("unknown")),
50 m_aSetName(OUString("unknown")),
55 m_aExportName
= m_aName
;
56 m_aFace
.SetTransparent(true);
57 m_aFace
.SetAlign(ALIGN_BASELINE
);
61 SmSym::SmSym(const SmSym
& rSymbol
)
67 SmSym::SmSym(const OUString
& rName
, const vcl::Font
& rFont
, sal_UCS4 cChar
,
68 const OUString
& rSet
, bool bIsPredefined
)
70 m_aName
= m_aExportName
= rName
;
73 m_aFace
.SetTransparent(true);
74 m_aFace
.SetAlign(ALIGN_BASELINE
);
78 m_bPredefined
= bIsPredefined
;
83 SmSym
& SmSym::operator = (const SmSym
& rSymbol
)
85 m_aName
= rSymbol
.m_aName
;
86 m_aExportName
= rSymbol
.m_aExportName
;
87 m_cChar
= rSymbol
.m_cChar
;
88 m_aFace
= rSymbol
.m_aFace
;
89 m_aSetName
= rSymbol
.m_aSetName
;
90 m_bPredefined
= rSymbol
.m_bPredefined
;
91 m_bDocSymbol
= rSymbol
.m_bDocSymbol
;
93 SmSymbolManager
* pSymSetManager
= &SM_MOD()->GetSymbolManager();
95 pSymSetManager
->SetModified(true);
101 bool SmSym::IsEqualInUI( const SmSym
& rSymbol
) const
103 return m_aName
== rSymbol
.m_aName
&&
104 m_aFace
== rSymbol
.m_aFace
&&
105 m_cChar
== rSymbol
.m_cChar
;
108 /**************************************************************************/
110 void SmSymbolManager::SFX_NOTIFY(SfxBroadcaster
& /*rBC*/, const TypeId
& rBCType
,
111 const SfxHint
& /*rHint*/, const TypeId
& rHintType
)
116 SmSymbolManager::SmSymbolManager()
122 SmSymbolManager::SmSymbolManager(const SmSymbolManager
& rSymbolSetManager
) :
125 m_aSymbols
= rSymbolSetManager
.m_aSymbols
;
130 SmSymbolManager::~SmSymbolManager()
135 SmSymbolManager
& SmSymbolManager::operator = (const SmSymbolManager
& rSymbolSetManager
)
137 m_aSymbols
= rSymbolSetManager
.m_aSymbols
;
143 SmSym
*SmSymbolManager::GetSymbolByName(const OUString
& rSymbolName
)
146 SymbolMap_t::iterator
aIt( m_aSymbols
.find( rSymbolName
) );
147 if (aIt
!= m_aSymbols
.end())
153 const SymbolPtrVec_t
SmSymbolManager::GetSymbols() const
156 SymbolMap_t::const_iterator
aIt( m_aSymbols
.begin() );
157 for ( ; aIt
!= m_aSymbols
.end(); ++aIt
)
158 aRes
.push_back( &aIt
->second
);
159 // OSL_ENSURE( sSymbols.size() == m_aSymbols.size(), "number of symbols mismatch " );
164 bool SmSymbolManager::AddOrReplaceSymbol( const SmSym
&rSymbol
, bool bForceChange
)
168 const OUString
aSymbolName( rSymbol
.GetName() );
169 if (aSymbolName
.getLength() > 0 && rSymbol
.GetSymbolSetName().getLength() > 0)
171 const SmSym
*pFound
= GetSymbolByName( aSymbolName
);
172 const bool bSymbolConflict
= pFound
&& !pFound
->IsEqualInUI( rSymbol
);
174 // avoid having the same symbol name twice but with different symbols in use
175 if (!pFound
|| bForceChange
)
177 m_aSymbols
[ aSymbolName
] = rSymbol
;
180 else if (pFound
&& !bForceChange
&& bSymbolConflict
)
182 // TODO: to solve this a document owned symbol manager would be required ...
183 SAL_WARN("starmath", "symbol conflict, different symbol with same name found!");
184 // symbols in all formulas. A copy of the global one would be needed here
185 // and then the new symbol has to be forcefully applied. This would keep
186 // the current formula intact but will leave the set of symbols in the
187 // global symbol manager somewhat to chance.
190 OSL_ENSURE( bAdded
, "failed to add symbol" );
193 OSL_ENSURE( bAdded
|| (pFound
&& !bSymbolConflict
), "AddOrReplaceSymbol: unresolved symbol conflict" );
200 void SmSymbolManager::RemoveSymbol( const OUString
& rSymbolName
)
202 if (rSymbolName
.getLength() > 0)
204 size_t nOldSize
= m_aSymbols
.size();
205 m_aSymbols
.erase( rSymbolName
);
206 m_bModified
= nOldSize
!= m_aSymbols
.size();
211 std::set
< OUString
> SmSymbolManager::GetSymbolSetNames() const
213 std::set
< OUString
> aRes
;
214 SymbolMap_t::const_iterator
aIt( m_aSymbols
.begin() );
215 for ( ; aIt
!= m_aSymbols
.end(); ++aIt
)
216 aRes
.insert( aIt
->second
.GetSymbolSetName() );
221 const SymbolPtrVec_t
SmSymbolManager::GetSymbolSet( const OUString
& rSymbolSetName
)
224 if (rSymbolSetName
.getLength() > 0)
226 SymbolMap_t::const_iterator
aIt( m_aSymbols
.begin() );
227 for ( ; aIt
!= m_aSymbols
.end(); ++aIt
)
229 if (aIt
->second
.GetSymbolSetName() == rSymbolSetName
)
230 aRes
.push_back( &aIt
->second
);
237 void SmSymbolManager::Load()
239 std::vector
< SmSym
> aSymbols
;
240 SmMathConfig
&rCfg
= *SM_MOD()->GetConfig();
241 rCfg
.GetSymbols( aSymbols
);
242 size_t nSymbolCount
= aSymbols
.size();
245 for (size_t i
= 0; i
< nSymbolCount
; ++i
)
247 const SmSym
&rSym
= aSymbols
[i
];
248 OSL_ENSURE( rSym
.GetName().getLength() > 0, "symbol without name!" );
249 if (rSym
.GetName().getLength() > 0)
250 AddOrReplaceSymbol( rSym
);
254 if (0 == nSymbolCount
)
256 SAL_WARN("starmath", "no symbol set found");
260 // now add a %i... symbol to the 'iGreek' set for every symbol found in the 'Greek' set.
261 const OUString
aGreekSymbolSetName(SmLocalizedSymbolData::GetUiSymbolSetName(OUString("Greek")));
262 const SymbolPtrVec_t
aGreekSymbols( GetSymbolSet( aGreekSymbolSetName
) );
263 OUString
aSymbolSetName('i');
264 aSymbolSetName
+= aGreekSymbolSetName
;
265 size_t nSymbols
= aGreekSymbols
.size();
266 for (size_t i
= 0; i
< nSymbols
; ++i
)
268 // make the new symbol a copy but with ITALIC_NORMAL, and add it to iGreek
269 const SmSym
&rSym
= *aGreekSymbols
[i
];
270 vcl::Font
aFont( rSym
.GetFace() );
271 OSL_ENSURE( aFont
.GetItalic() == ITALIC_NONE
, "expected Font with ITALIC_NONE, failed." );
272 aFont
.SetItalic( ITALIC_NORMAL
);
273 OUString
aSymbolName('i');
274 aSymbolName
+= rSym
.GetName();
275 SmSym
aSymbol( aSymbolName
, aFont
, rSym
.GetCharacter(),
276 aSymbolSetName
, true /*bIsPredefined*/ );
278 AddOrReplaceSymbol( aSymbol
);
282 void SmSymbolManager::Save()
286 SmMathConfig
&rCfg
= *SM_MOD()->GetConfig();
288 // prepare to skip symbols from iGreek on saving
289 OUString
aSymbolSetName('i');
290 aSymbolSetName
+= SmLocalizedSymbolData::GetUiSymbolSetName(OUString("Greek"));
292 SymbolPtrVec_t
aTmp( GetSymbols() );
293 std::vector
< SmSym
> aSymbols
;
294 for (size_t i
= 0; i
< aTmp
.size(); ++i
)
296 // skip symbols from iGreek set since those symbols always get added
297 // by computational means in SmSymbolManager::Load
298 if (aTmp
[i
]->GetSymbolSetName() != aSymbolSetName
)
299 aSymbols
.push_back( *aTmp
[i
] );
301 rCfg
.SetSymbols( aSymbols
);
308 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */