]> git.lyx.org Git - lyx.git/blob - src/mathed/MathStream.h
* GuiView.cpp:
[lyx.git] / src / mathed / MathStream.h
1 // -*- C++ -*-
2 /**
3  * \file MathStream.h
4  * This file is part of LyX, the document processor.
5  * Licence details can be found in the file COPYING.
6  *
7  * \author André Pönitz
8  *
9  * Full author contact details are available in file CREDITS.
10  */
11
12 #ifndef MATH_MATHMLSTREAM_H
13 #define MATH_MATHMLSTREAM_H
14
15 #include "support/strfwd.h"
16
17 #include "InsetMath.h"
18 // FIXME: Move to individual insets
19 #include "MetricsInfo.h"
20
21
22 namespace lyx {
23
24 class MathData;
25 class InsetMath;
26 class MathAtom;
27
28 //
29 // LaTeX/LyX
30 //
31
32 class WriteStream {
33 public:
34         ///
35         WriteStream(odocstream & os, bool fragile, bool latex, bool dryrun);
36         ///
37         explicit WriteStream(odocstream & os);
38         ///
39         ~WriteStream();
40         ///
41         int line() const { return line_; }
42         ///
43         bool fragile() const { return fragile_; }
44         ///
45         bool latex() const { return latex_; }
46         ///
47         bool dryrun() const { return dryrun_; }
48         ///
49         odocstream & os() { return os_; }
50         ///
51         bool & firstitem() { return firstitem_; }
52         ///
53         void addlines(unsigned int);
54         /// writes space if next thing is isalpha()
55         void pendingSpace(bool how);
56         /// writes space if next thing is isalpha()
57         bool pendingSpace() const { return pendingspace_; }
58         /// tell whether to write the closing brace of \ensuremath
59         void pendingBrace(bool brace);
60         /// tell whether to write the closing brace of \ensuremath
61         bool pendingBrace() const { return pendingbrace_; }
62         /// tell whether we are in text mode or not when producing latex code
63         void textMode(bool textmode);
64         /// tell whether we are in text mode or not when producing latex code
65         bool textMode() const { return textmode_; }
66 private:
67         ///
68         odocstream & os_;
69         /// do we have to write \\protect sometimes
70         bool fragile_;
71         /// are we at the beginning of an MathData?
72         bool firstitem_;
73         /// are we writing to .tex?
74         int latex_;
75         /// is it for preview?
76         bool dryrun_;
77         /// do we have a space pending?
78         bool pendingspace_;
79         /// do we have a brace pending?
80         bool pendingbrace_;
81         /// are we in text mode when producing latex code?
82         bool textmode_;
83         ///
84         int line_;
85 };
86
87 ///
88 WriteStream & operator<<(WriteStream &, MathAtom const &);
89 ///
90 WriteStream & operator<<(WriteStream &, MathData const &);
91 ///
92 WriteStream & operator<<(WriteStream &, docstring const &);
93 ///
94 WriteStream & operator<<(WriteStream &, char const * const);
95 ///
96 WriteStream & operator<<(WriteStream &, char);
97 ///
98 WriteStream & operator<<(WriteStream &, int);
99 ///
100 WriteStream & operator<<(WriteStream &, unsigned int);
101
102 /// ensure math mode, possibly by opening \ensuremath
103 bool ensureMath(WriteStream & os, bool needs_math_mode = true, bool macro = false);
104
105 /// ensure the requested mode, possibly by closing \ensuremath
106 bool ensureMode(WriteStream & os, InsetMath::mode_type mode);
107
108
109 /**
110  * MathEnsurer - utility class for ensuring math mode
111  *
112  * A local variable of this type can be used to either ensure math mode
113  * or delay the writing of a pending brace when outputting LaTeX.
114  *
115  * Example 1:
116  *
117  *      MathEnsurer ensurer(os);
118  *
119  * If not already in math mode, inserts an \ensuremath command followed
120  * by an open brace. This brace will be automatically closed when exiting
121  * math mode. Math mode is automatically exited when writing something
122  * that doesn't explicitly require math mode.
123  *
124  * Example 2:
125  *
126  *      MathEnsurer ensurer(os, false);
127  *
128  * Simply suspend writing a closing brace until the end of ensurer's scope.
129  *
130  * Example 3:
131  *
132  *      MathEnsurer ensurer(os, needs_math_mode, true);
133  *
134  * The third parameter is set to true only for a user defined macro, which
135  * needs special handling. When it is a MathMacro, the needs_math_mode
136  * parameter is true and the behavior is as in Example 1. When the
137  * needs_math_mode parameter is false (not a MathMacro) and the macro
138  * was entered in a text box and we are in math mode, the mode is reset
139  * to text. This is because the macro was probably designed for text mode
140  * (given that it was entered in text mode and we have no way to tell the
141  * contrary).
142  */
143 class MathEnsurer
144 {
145 public:
146         ///
147         explicit MathEnsurer(WriteStream & os, bool needs_math_mode = true, bool macro = false)
148                 : os_(os), brace_(ensureMath(os, needs_math_mode, macro)) {}
149         ///
150         ~MathEnsurer() { os_.pendingBrace(brace_); }
151 private:
152         ///
153         WriteStream & os_;
154         ///
155         bool brace_;
156 };
157
158
159 /**
160  * ModeSpecifier - utility class for specifying a given mode (math or text)
161  *
162  * A local variable of this type can be used to specify that a command or
163  * environment works in a given mode. For example, \mbox works in text
164  * mode, but \boxed works in math mode. Note that no mode changing commands
165  * are needed, but we have to track the current mode, hence this class.
166  *
167  * Example:
168  *
169  *      ModeSpecifier specifier(os, TEXT_MODE);
170  *
171  * Sets the current mode to text mode.
172  *
173  * At the end of specifier's scope the mode is reset to its previous value.
174  */
175 class ModeSpecifier
176 {
177 public:
178         ///
179         explicit ModeSpecifier(WriteStream & os, InsetMath::mode_type mode)
180                 : os_(os), textmode_(ensureMode(os, mode)) {}
181         ///
182         ~ModeSpecifier() { os_.textMode(textmode_); }
183 private:
184         ///
185         WriteStream & os_;
186         ///
187         bool textmode_;
188 };
189
190
191
192 //
193 //  MathML
194 //
195
196 class MTag {
197 public:
198         ///
199         MTag(char const * const tag) : tag_(tag) {}
200         ///
201         char const * const tag_;
202 };
203
204 class ETag {
205 public:
206         ///
207         ETag(char const * const tag) : tag_(tag) {}
208         ///
209         char const * const tag_;
210 };
211
212 class MathStream {
213 public:
214         ///
215         explicit MathStream(odocstream & os);
216         ///
217         void cr();
218         ///
219         odocstream & os() { return os_; }
220         ///
221         int line() const { return line_; }
222         ///
223         int & tab() { return tab_; }
224         ///
225         friend MathStream & operator<<(MathStream &, char const *);
226 private:
227         ///
228         odocstream & os_;
229         ///
230         int tab_;
231         ///
232         int line_;
233         ///
234         char lastchar_;
235 };
236
237 ///
238 MathStream & operator<<(MathStream &, MathAtom const &);
239 ///
240 MathStream & operator<<(MathStream &, MathData const &);
241 ///
242 MathStream & operator<<(MathStream &, docstring const &);
243 ///
244 MathStream & operator<<(MathStream &, char const *);
245 ///
246 MathStream & operator<<(MathStream &, char);
247 ///
248 MathStream & operator<<(MathStream &, MTag const &);
249 ///
250 MathStream & operator<<(MathStream &, ETag const &);
251
252
253
254 //
255 // Debugging
256 //
257
258 class NormalStream {
259 public:
260         ///
261         explicit NormalStream(odocstream & os) : os_(os) {}
262         ///
263         odocstream & os() { return os_; }
264 private:
265         ///
266         odocstream & os_;
267 };
268
269 ///
270 NormalStream & operator<<(NormalStream &, MathAtom const &);
271 ///
272 NormalStream & operator<<(NormalStream &, MathData const &);
273 ///
274 NormalStream & operator<<(NormalStream &, docstring const &);
275 ///
276 NormalStream & operator<<(NormalStream &, char const *);
277 ///
278 NormalStream & operator<<(NormalStream &, char);
279 ///
280 NormalStream & operator<<(NormalStream &, int);
281
282
283 //
284 // Maple
285 //
286
287
288 class MapleStream {
289 public:
290         ///
291         explicit MapleStream(odocstream & os) : os_(os) {}
292         ///
293         odocstream & os() { return os_; }
294 private:
295         ///
296         odocstream & os_;
297 };
298
299
300 ///
301 MapleStream & operator<<(MapleStream &, MathAtom const &);
302 ///
303 MapleStream & operator<<(MapleStream &, MathData const &);
304 ///
305 MapleStream & operator<<(MapleStream &, docstring const &);
306 ///
307 MapleStream & operator<<(MapleStream &, char_type);
308 ///
309 MapleStream & operator<<(MapleStream &, char const *);
310 ///
311 MapleStream & operator<<(MapleStream &, char);
312 ///
313 MapleStream & operator<<(MapleStream &, int);
314
315
316 //
317 // Maxima
318 //
319
320
321 class MaximaStream {
322 public:
323         ///
324         explicit MaximaStream(odocstream & os) : os_(os) {}
325         ///
326         odocstream & os() { return os_; }
327 private:
328         ///
329         odocstream & os_;
330 };
331
332
333 ///
334 MaximaStream & operator<<(MaximaStream &, MathAtom const &);
335 ///
336 MaximaStream & operator<<(MaximaStream &, MathData const &);
337 ///
338 MaximaStream & operator<<(MaximaStream &, docstring const &);
339 ///
340 MaximaStream & operator<<(MaximaStream &, char_type);
341 ///
342 MaximaStream & operator<<(MaximaStream &, char const *);
343 ///
344 MaximaStream & operator<<(MaximaStream &, char);
345 ///
346 MaximaStream & operator<<(MaximaStream &, int);
347
348
349 //
350 // Mathematica
351 //
352
353
354 class MathematicaStream {
355 public:
356         ///
357         explicit MathematicaStream(odocstream & os) : os_(os) {}
358         ///
359         odocstream & os() { return os_; }
360 private:
361         ///
362         odocstream & os_;
363 };
364
365
366 ///
367 MathematicaStream & operator<<(MathematicaStream &, MathAtom const &);
368 ///
369 MathematicaStream & operator<<(MathematicaStream &, MathData const &);
370 ///
371 MathematicaStream & operator<<(MathematicaStream &, docstring const &);
372 ///
373 MathematicaStream & operator<<(MathematicaStream &, char const *);
374 ///
375 MathematicaStream & operator<<(MathematicaStream &, char);
376 ///
377 MathematicaStream & operator<<(MathematicaStream &, int);
378
379
380 //
381 // Octave
382 //
383
384
385 class OctaveStream {
386 public:
387         ///
388         explicit OctaveStream(odocstream & os) : os_(os) {}
389         ///
390         odocstream & os() { return os_; }
391 private:
392         ///
393         odocstream & os_;
394 };
395
396 ///
397 OctaveStream & operator<<(OctaveStream &, MathAtom const &);
398 ///
399 OctaveStream & operator<<(OctaveStream &, MathData const &);
400 ///
401 OctaveStream & operator<<(OctaveStream &, docstring const &);
402 ///
403 OctaveStream & operator<<(OctaveStream &, char_type);
404 ///
405 OctaveStream & operator<<(OctaveStream &, char const *);
406 ///
407 OctaveStream & operator<<(OctaveStream &, char);
408 ///
409 OctaveStream & operator<<(OctaveStream &, int);
410
411 } // namespace lyx
412
413 #endif