]> git.lyx.org Git - lyx.git/blob - src/LaTeXFeatures.C
use the new sstream return non-pods as const, use string instead of char * in a lot...
[lyx.git] / src / LaTeXFeatures.C
1 // -*- C++ -*-
2 /* This file is part of
3  * ====================================================== 
4  * 
5  *           LyX, The Document Processor
6  *        
7  *           Copyright 1995 Matthias Ettrich
8  *           Copyright 1995-2000 the LyX Team.
9  *
10  * ====================================================== */
11
12 #include <config.h>
13
14 #ifdef __GNUG__
15 #pragma implementation "LaTeXFeatures.h"
16 #endif
17
18 #include "LString.h"
19 #include "debug.h"
20 #include "lyx_sty.h"
21 #include "lyxrc.h"
22 #include "LaTeXFeatures.h"
23 #include "bufferparams.h"
24 #include "layout.h"
25
26 using std::endl;
27
28 LaTeXFeatures::LaTeXFeatures(BufferParams const & p, int n)
29         : layout(n, false), params(p)
30 {
31         // packages
32         array = false;
33         color = false;
34         graphics = false; // INSET_GRAPHICS: remove this when InsetFig is thrown.
35         graphicx = false;
36         setspace = false;
37         makeidx = false;
38         verbatim = false;
39         longtable = false;
40         algorithm = false;
41         rotating = false;
42         amssymb = false;
43         latexsym = false;
44         pifont = false;
45         subfigure = false;
46         floatflt = false;
47         url = false;
48         varioref = false;
49         prettyref = false;
50         chess = false;
51         
52         // commands
53         lyx = false;
54         lyxline = false;
55         noun = false;
56         lyxarrow = false;
57
58         // quotes
59         quotesinglbase = false;
60         quotedblbase = false;
61         guilsinglleft = false;
62         guilsinglright = false;
63         guillemotleft = false;
64         guillemotright = false;
65
66         // Math mode
67         amsstyle = false;
68         binom = false;
69         boldsymbol = false;
70     
71         // special features
72         LyXParagraphIndent = false;
73         NeedLyXFootnoteCode = false;
74         NeedLyXMinipageIndent = false;
75 }
76
77
78 void LaTeXFeatures::require(string const & name)
79 {
80         if (name == "array") {
81                 array = true;
82         } else if (name == "color") {
83                 color = true;
84         } else if (name == "graphics") {
85                 graphicx = true;
86                 graphics = true;// INSET_GRAPHICS: remove this when InsetFig is thrown.
87         } else if (name == "setspace") {
88                 setspace = true;
89         } else if (name == "makeidx") {
90                 makeidx = true;
91         } else if (name == "verbatim") {
92                 verbatim = true;
93         } else if (name == "longtable") {
94                 longtable = true;
95         } else if (name == "algorithm") {
96                 algorithm = true;
97         } else if (name == "rotating") {
98                 rotating = true;
99         } else if (name == "amssymb") {
100                 amssymb = true;
101         } else if (name == "latexsym") {
102                 latexsym = true;
103         } else if (name == "pifont") {
104                 pifont = true;
105         } else if (name == "subfigure") {
106                 subfigure = true;
107         } else if (name == "floatflt") {
108                 floatflt = true;
109         } else if (name == "url") {
110                 url = true;
111         } else if (name == "varioref") {
112                 varioref = true;
113         } else if (name == "prettyref") {
114                 prettyref = true;
115         } else if (name == "chess") {
116                 chess = true;
117         } else if (name == "amsstyle") {
118                 amsstyle = true;
119         } else if (name == "boldsymbol") {
120                 boldsymbol = true;
121         } else if (name == "binom") {
122                 binom = true;
123         }
124 }
125
126
127 string const LaTeXFeatures::getPackages()
128 {
129         string packages;
130         LyXTextClass const & tclass =
131                 textclasslist.TextClass(params.textclass);
132
133         // array-package
134         if (array)
135                 packages += "\\usepackage{array}\n";
136
137         // color.sty
138         if (color) {
139                 if (params.graphicsDriver == "default")
140                         packages += "\\usepackage{color}\n";
141                 else
142                         packages += "\\usepackage[" 
143                                 + params.graphicsDriver + "]{color}\n";
144         }
145                 
146         // makeidx.sty
147         if (makeidx) {
148                 if (! tclass.provides(LyXTextClass::makeidx)
149                     && params.language != "french") // french provides
150                                                     // \index !
151                         packages += "\\usepackage{makeidx}\n";
152                 packages += "\\makeindex\n";
153         }
154
155         // graphicx.sty
156         if (graphicx && params.graphicsDriver != "none") {
157                 if (params.graphicsDriver == "default")
158                         packages += "\\usepackage{graphicx}\n";
159                 else
160                         packages += "\\usepackage[" 
161                                 + params.graphicsDriver + "]{graphicx}\n";
162         }
163
164         // INSET_GRAPHICS: remove this when InsetFig is thrown.
165         // graphics.sty
166         if (graphics && params.graphicsDriver != "none") {
167                 if (params.graphicsDriver == "default")
168                         packages += "\\usepackage{graphics}\n";
169                 else
170                         packages += "\\usepackage[" 
171                                 + params.graphicsDriver + "]{graphics}\n";
172         }
173
174         // verbatim.sty
175         if (verbatim)
176                 packages += "\\usepackage{verbatim}\n";
177
178         if (algorithm) {
179                 packages += "\\usepackage{algorithm}\n";
180         }
181
182         // lyxchess.sty
183         if (chess) {
184                 packages += "\\usepackage{lyxchess}\n";
185         }
186
187         // setspace.sty
188         if ((params.spacing.getSpace() != Spacing::Single
189              && !params.spacing.isDefault())
190             || setspace) {
191                 packages += "\\usepackage{setspace}\n";
192         }
193         switch (params.spacing.getSpace()) {
194         case Spacing::Default:
195         case Spacing::Single:
196                 // we dont use setspace.sty so dont print anything
197                 //packages += "\\singlespacing\n";
198                 break;
199         case Spacing::Onehalf:
200                 packages += "\\onehalfspacing\n";
201                 break;
202         case Spacing::Double:
203                 packages += "\\doublespacing\n";
204                 break;
205         case Spacing::Other:
206 //#ifdef HAVE_SSTREAM
207                 std::ostringstream value;
208 //#else
209 //              char val[30];
210 //              ostrstream value(val, 30);
211 //              
212 //#endif
213                 value << params.spacing.getValue(); // setw?
214 //#ifdef HAVE_SSTREAM
215                 packages += string("\\setstretch{") 
216                           + value.str().c_str() + "}\n";
217 //#else
218 //              value << '\0';
219 //              packages += string("\\setstretch{") 
220 //                        + value.str() + "}\n";
221 //#endif
222                 break;
223         }
224
225         //longtable.sty
226         if (longtable)
227                 packages += "\\usepackage{longtable}\n";
228
229         //rotating.sty
230         if (rotating)
231                 packages += "\\usepackage{rotating}\n";
232
233         // amssymb.sty
234         if (amssymb || params.use_amsmath)
235                 packages += "\\usepackage{amssymb}\n";
236
237         // latexsym.sty
238         if (latexsym)
239                 packages += "\\usepackage{latexsym}\n";
240
241         // pifont.sty
242         if (pifont)
243                 packages += "\\usepackage{pifont}\n";
244
245         // subfigure.sty
246         if (subfigure)
247                 packages += "\\usepackage{subfigure}\n";
248
249         // floatflt.sty
250         if (floatflt)
251                 packages += "\\usepackage{floatflt}\n";
252
253         // url.sty
254         if (url && ! tclass.provides(LyXTextClass::url))
255                 packages += "\\IfFileExists{url.sty}{\\usepackage{url}}\n"
256                             "                      {\\newcommand{\\url}{\\texttt}}\n";
257
258         // varioref.sty
259         if (varioref)
260                 packages += "\\usepackage{varioref}\n";
261
262         // prettyref.sty
263         if (prettyref)
264                 packages += "\\usepackage{prettyref}\n";
265
266         // float.sty
267         // This is not correct and needs fixing.
268         // We don't need float.sty if we only use unchanged
269         // table and figure floats. (Lgb)
270         if (!usedFloats.empty())
271                 packages += "\\usepackage{float}\n";
272         
273         packages += externalPreambles;
274
275         return packages;
276 }
277
278
279 string const LaTeXFeatures::getMacros()
280 {
281         string macros;
282
283         // always include this
284         if (true || lyx) 
285                 macros += lyx_def + '\n';
286
287         if (lyxline) 
288                 macros += lyxline_def + '\n';
289
290         if (noun) {
291                 macros += noun_def + '\n';
292         }
293
294         if (lyxarrow) {
295                 macros += lyxarrow_def + '\n';
296         }
297
298         // quotes. 
299         if (quotesinglbase)
300                 macros += quotesinglbase_def + '\n';
301         if (quotedblbase)
302                 macros += quotedblbase_def + '\n';
303         if (guilsinglleft)
304                 macros += guilsinglleft_def + '\n';
305         if (guilsinglright)
306                 macros += guilsinglright_def + '\n';
307         if (guillemotleft)
308                 macros += guillemotleft_def + '\n';
309         if (guillemotright)
310                 macros += guillemotright_def + '\n';
311     
312         // Math mode    
313         if (boldsymbol && !amsstyle)
314                 macros += boldsymbol_def + '\n';
315         if (binom && !amsstyle)
316                 macros += binom_def + '\n';
317
318         // other
319         if (NeedLyXMinipageIndent) 
320                 macros += minipageindent_def;
321         if (LyXParagraphIndent) 
322                 macros += paragraphindent_def;
323         if (NeedLyXFootnoteCode) 
324                 macros += floatingfootnote_def;
325
326         // floats
327         // Here we will output the code to create the needed float styles.
328         // We will try to do this as minimal as possible.
329         // \floatstyle{ruled}
330         // \newfloat{algorithm}{htbp}{loa}
331         // \floatname{algorithm}{Algorithm}
332         return macros;
333 }
334
335
336 string const LaTeXFeatures::getTClassPreamble()
337 {
338         // the text class specific preamble 
339         LyXTextClass const & tclass =
340                 textclasslist.TextClass(params.textclass);
341         string tcpreamble = tclass.preamble();
342
343         for (unsigned int i = 0; i < tclass.numLayouts(); ++i) {
344                 if (layout[i]) {
345                         tcpreamble += tclass[i].preamble();
346                 }
347         }
348
349         return tcpreamble;
350 }       
351
352
353 string const LaTeXFeatures::getIncludedFiles()
354 {
355         string sgmlpreamble;
356         FileMap::const_iterator end = IncludedFiles.end();
357         for(FileMap::const_iterator fi=IncludedFiles.begin(); fi != end; ++fi)
358                 sgmlpreamble += "\n<!entity " + fi->first
359                         + " system \"" + fi->second + "\">";
360
361         return sgmlpreamble;
362 }
363
364
365 void LaTeXFeatures::showStruct() {
366         lyxerr << "LyX needs the following commands when LaTeXing:"
367                << "\n***** Packages:" << getPackages()
368                << "\n***** Macros:" << getMacros()
369                << "\n***** Textclass stuff:" << getTClassPreamble()
370                << "\n***** done." << endl;
371 }
372
373
374 BufferParams const & LaTeXFeatures::bufferParams() const
375 {
376         return params;
377 }