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