]> git.lyx.org Git - lyx.git/blob - src/mathed/InsetMathString.cpp
Do not warn about changed modules when changing modules.
[lyx.git] / src / mathed / InsetMathString.cpp
1 /**
2  * \file InsetMathString.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author André Pönitz
7  *
8  * Full author contact details are available in file CREDITS.
9  */
10
11 #include <config.h>
12
13 #include "InsetMathString.h"
14 #include "MathFactory.h"
15 #include "MathStream.h"
16 #include "MathSupport.h"
17
18 #include "Encoding.h"
19
20 #include "support/debug.h"
21 #include "support/gettext.h"
22 #include "support/lassert.h"
23 #include "support/lstrings.h"
24 #include "support/textutils.h"
25
26 using lyx::support::escape;
27
28
29 namespace lyx {
30
31 InsetMathString::InsetMathString(docstring const & s)
32         : str_(s)
33 {}
34
35
36 Inset * InsetMathString::clone() const
37 {
38         return new InsetMathString(*this);
39 }
40
41
42 void InsetMathString::metrics(MetricsInfo & mi, Dimension & dim) const
43 {
44         mathed_string_dim(mi.base.font, str_, dim);
45 }
46
47
48 void InsetMathString::draw(PainterInfo & pi, int x, int y) const
49 {
50         pi.draw(x, y, str_);
51 }
52
53
54 void InsetMathString::normalize(NormalStream & os) const
55 {
56         os << "[string " << str_ << ' ' << "mathalpha" << ']';
57 }
58
59
60 void InsetMathString::maple(MapleStream & os) const
61 {
62         if (/*code_ != LM_TC_VAR ||*/ str_.size() <= 1) {
63                 os << ' ' << str_ << ' ';
64                 return;
65         }
66
67         // insert '*' between adjacent chars if type is LM_TC_VAR
68         os << str_[0];
69         for (size_t i = 1; i < str_.size(); ++i)
70                 os << str_[i];
71 }
72
73
74 void InsetMathString::mathematica(MathematicaStream & os) const
75 {
76         os << ' ' << str_ << ' ';
77 }
78
79
80 void InsetMathString::octave(OctaveStream & os) const
81 {
82         if (/*code_ != LM_TC_VAR ||*/ str_.size() <= 1) {
83                 os << ' ' << str_ << ' ';
84                 return;
85         }
86
87         // insert '*' between adjacent chars if type is LM_TC_VAR
88         os << str_[0];
89         for (size_t i = 1; i < str_.size(); ++i)
90                 os << str_[i];
91 }
92
93
94 void InsetMathString::mathmlize(MathStream &) const
95 {
96         // useless, no doubt, but we should not be here
97         LATTEST(false);
98 }
99
100
101 void InsetMathString::write(WriteStream & os) const
102 {
103         if (!os.latex() || os.lockedMode()) {
104                 os << (os.asciiOnly() ? escape(str_) : str_);
105                 return;
106         }
107
108         docstring::const_iterator cit = str_.begin();
109         docstring::const_iterator end = str_.end();
110
111         // We may already be inside an \ensuremath command.
112         bool in_forced_mode = os.pendingBrace();
113
114         // We will take care of matching braces.
115         os.pendingBrace(false);
116
117         while (cit != end) {
118                 bool mathmode = in_forced_mode ? os.textMode() : !os.textMode();
119                 char_type const c = *cit;
120                 docstring command(1, c);
121                 try {
122                         bool termination = false;
123                         if (isASCII(c) ||
124                             Encodings::latexMathChar(c, mathmode, os.encoding(), command, termination)) {
125                                 if (os.textMode()) {
126                                         if (in_forced_mode) {
127                                                 // we were inside \lyxmathsym
128                                                 os << '}';
129                                                 os.textMode(false);
130                                                 in_forced_mode = false;
131                                         }
132                                         if (!isASCII(c) && os.textMode()) {
133                                                 os << "\\ensuremath{";
134                                                 os.textMode(false);
135                                                 in_forced_mode = true;
136                                         }
137                                 } else if (isASCII(c) && in_forced_mode) {
138                                         // we were inside \ensuremath
139                                         os << '}';
140                                         os.textMode(true);
141                                         in_forced_mode = false;
142                                 }
143                         } else if (!os.textMode()) {
144                                         if (in_forced_mode) {
145                                                 // we were inside \ensuremath
146                                                 os << '}';
147                                                 in_forced_mode = false;
148                                         } else {
149                                                 os << "\\lyxmathsym{";
150                                                 in_forced_mode = true;
151                                         }
152                                         os.textMode(true);
153                         }
154                         os << command;
155                         // We may need a space if the command contains a macro
156                         // and the last char is ASCII.
157                         if (termination)
158                                 os.pendingSpace(true);
159                 } catch (EncodingException const & e) {
160                         switch (os.output()) {
161                         case WriteStream::wsDryrun: {
162                                 os << "<" << _("LyX Warning: ")
163                                    << _("uncodable character") << " '";
164                                 os << docstring(1, e.failed_char);
165                                 os << "'>";
166                                 break;
167                         }
168                         case WriteStream::wsPreview: {
169                                 // indicate the encoding error by a boxed '?'
170                                 os << "{\\fboxsep=1pt\\fbox{?}}";
171                                 LYXERR0("Uncodable character" << " '"
172                                         << docstring(1, e.failed_char)
173                                         << "'");
174                                 break;
175                         }
176                         case WriteStream::wsDefault:
177                         default:
178                                 // throw again
179                                 throw(e);
180                         }
181                 }
182                 ++cit;
183         }
184
185         if (in_forced_mode && os.textMode()) {
186                 // We have to care for closing \lyxmathsym
187                 os << '}';
188                 os.textMode(false);
189         } else {
190                 os.pendingBrace(in_forced_mode);
191         }
192 }
193
194
195 } // namespace lyx