]> git.lyx.org Git - lyx.git/blob - src/bufferparams.C
another fix
[lyx.git] / src / bufferparams.C
1 /* This file is part of
2  * ======================================================
3  *
4  *           LyX, The Document Processor
5  *
6  *           Copyright 1995 Matthias Ettrich
7  *           Copyright 1995-2001 The LyX Team.
8  *
9  * ====================================================== */
10
11 #include <config.h>
12
13 #ifdef __GNUG__
14 #pragma implementation
15 #endif
16
17 #include "bufferparams.h"
18 #include "tex-strings.h"
19 #include "layout.h"
20 #include "vspace.h"
21 #include "debug.h"
22 #include "lyxrc.h"
23 #include "language.h"
24 #include "lyxtextclasslist.h"
25 #include "lyxlex.h"
26
27 #include "support/lyxlib.h"
28 #include "support/lstrings.h"
29
30 #include <cstdlib>
31
32 using std::ostream;
33 using std::endl;
34
35 BufferParams::BufferParams()
36         // Initialize textclass to point to article. if `first' is
37         // true in the returned pair, then `second' is the textclass
38         // number; if it is false, second is 0. In both cases, second
39         // is what we want.
40         : textclass(textclasslist.NumberOfClass("article").second)
41 {
42         paragraph_separation = PARSEP_INDENT;
43         defskip = VSpace(VSpace::MEDSKIP);
44         quotes_language = InsetQuotes::EnglishQ;
45         quotes_times = InsetQuotes::DoubleQ;
46         fontsize = "default";
47
48         /*  PaperLayout */
49         papersize = PAPER_DEFAULT;
50         papersize2 = VM_PAPER_DEFAULT; /* DEFAULT */
51         paperpackage = PACKAGE_NONE;
52         orientation = ORIENTATION_PORTRAIT;
53         use_geometry = false;
54         use_amsmath = false;
55         use_natbib = false;
56         use_numerical_citations = false;
57         secnumdepth = 3;
58         tocdepth = 3;
59         language = default_language;
60         fonts = "default";
61         inputenc = "auto";
62         graphicsDriver = "default";
63         sides = LyXTextClass::OneSide;
64         columns = 1;
65         pagestyle = "default";
66         for (int iter = 0; iter < 4; ++iter) {
67                 user_defined_bullets[iter] = ITEMIZE_DEFAULTS[iter];
68                 temp_bullets[iter] = ITEMIZE_DEFAULTS[iter];
69         }
70 }
71
72
73 void BufferParams::writeFile(ostream & os) const
74 {
75         // The top of the file is written by the buffer.
76         // Prints out the buffer info into the .lyx file given by file
77
78         // the textclass
79         os << "\\textclass " << textclasslist[textclass].name() << '\n';
80
81         // then the the preamble
82         if (!preamble.empty()) {
83                 // remove '\n' from the end of preamble
84                 string const tmppreamble = rtrim(preamble, "\n");
85                 os << "\\begin_preamble\n"
86                    << tmppreamble
87                    << "\n\\end_preamble\n";
88         }
89
90         /* the options */
91         if (!options.empty()) {
92                 os << "\\options " << options << '\n';
93         }
94
95         /* then the text parameters */
96         if (language != ignore_language)
97                 os << "\\language " << language->lang() << '\n';
98         os << "\\inputencoding " << inputenc
99            << "\n\\fontscheme " << fonts
100            << "\n\\graphics " << graphicsDriver << '\n';
101
102         if (!float_placement.empty()) {
103                 os << "\\float_placement " << float_placement << '\n';
104         }
105         os << "\\paperfontsize " << fontsize << '\n';
106
107         spacing.writeFile(os);
108
109         os << "\\papersize " << string_papersize[papersize2]
110            << "\n\\paperpackage " << string_paperpackages[paperpackage]
111            << "\n\\use_geometry " << use_geometry
112            << "\n\\use_amsmath " << use_amsmath
113            << "\n\\use_natbib " << use_natbib
114            << "\n\\use_numerical_citations " << use_numerical_citations
115            << "\n\\paperorientation " << string_orientation[orientation]
116            << '\n';
117         if (!paperwidth.empty())
118                 os << "\\paperwidth "
119                    << VSpace(paperwidth).asLyXCommand() << '\n';
120         if (!paperheight.empty())
121                 os << "\\paperheight "
122                    << VSpace(paperheight).asLyXCommand() << '\n';
123         if (!leftmargin.empty())
124                 os << "\\leftmargin "
125                    << VSpace(leftmargin).asLyXCommand() << '\n';
126         if (!topmargin.empty())
127                 os << "\\topmargin "
128                    << VSpace(topmargin).asLyXCommand() << '\n';
129         if (!rightmargin.empty())
130                 os << "\\rightmargin "
131                    << VSpace(rightmargin).asLyXCommand() << '\n';
132         if (!bottommargin.empty())
133                 os << "\\bottommargin "
134                    << VSpace(bottommargin).asLyXCommand() << '\n';
135         if (!headheight.empty())
136                 os << "\\headheight "
137                    << VSpace(headheight).asLyXCommand() << '\n';
138         if (!headsep.empty())
139                 os << "\\headsep "
140                    << VSpace(headsep).asLyXCommand() << '\n';
141         if (!footskip.empty())
142                 os << "\\footskip "
143                    << VSpace(footskip).asLyXCommand() << '\n';
144         os << "\\secnumdepth " << secnumdepth
145            << "\n\\tocdepth " << tocdepth
146            << "\n\\paragraph_separation "
147            << string_paragraph_separation[paragraph_separation]
148            << "\n\\defskip " << defskip.asLyXCommand()
149            << "\n\\quotes_language "
150            << string_quotes_language[quotes_language] << '\n';
151         switch (quotes_times) {
152                 // An output operator for insetquotes would be nice
153         case InsetQuotes::SingleQ:
154                 os << "\\quotes_times 1\n"; break;
155         case InsetQuotes::DoubleQ:
156                 os << "\\quotes_times 2\n"; break;
157         }
158         os << "\\papercolumns " << columns
159            << "\n\\papersides " << sides
160            << "\n\\paperpagestyle " << pagestyle << '\n';
161         for (int i = 0; i < 4; ++i) {
162                 if (user_defined_bullets[i] != ITEMIZE_DEFAULTS[i]) {
163                         if (user_defined_bullets[i].getFont() != -1) {
164                                 os << "\\bullet " << i
165                                    << "\n\t"
166                                    << user_defined_bullets[i].getFont()
167                                    << "\n\t"
168                                    << user_defined_bullets[i].getCharacter()
169                                    << "\n\t"
170                                    << user_defined_bullets[i].getSize()
171                                    << "\n\\end_bullet\n";
172                         }
173                         else {
174                                 os << "\\bulletLaTeX " << i
175                                    << "\n\t\""
176                                    << user_defined_bullets[i].getText()
177                                    << "\"\n\\end_bullet\n";
178                         }
179                 }
180         }
181 }
182
183
184 void BufferParams::setPaperStuff()
185 {
186         papersize = PAPER_DEFAULT;
187         char const c1 = paperpackage;
188         if (c1 == PACKAGE_NONE) {
189                 char const c2 = papersize2;
190                 if (c2 == VM_PAPER_USLETTER)
191                         papersize = PAPER_USLETTER;
192                 else if (c2 == VM_PAPER_USLEGAL)
193                         papersize = PAPER_LEGALPAPER;
194                 else if (c2 == VM_PAPER_USEXECUTIVE)
195                         papersize = PAPER_EXECUTIVEPAPER;
196                 else if (c2 == VM_PAPER_A3)
197                         papersize = PAPER_A3PAPER;
198                 else if (c2 == VM_PAPER_A4)
199                         papersize = PAPER_A4PAPER;
200                 else if (c2 == VM_PAPER_A5)
201                         papersize = PAPER_A5PAPER;
202                 else if ((c2 == VM_PAPER_B3) || (c2 == VM_PAPER_B4) ||
203                          (c2 == VM_PAPER_B5))
204                         papersize = PAPER_B5PAPER;
205         } else if ((c1 == PACKAGE_A4) || (c1 == PACKAGE_A4WIDE) ||
206                    (c1 == PACKAGE_WIDEMARGINSA4))
207                 papersize = PAPER_A4PAPER;
208 }
209
210
211 void BufferParams::useClassDefaults()
212 {
213         LyXTextClass const & tclass = textclasslist[textclass];
214
215         sides = tclass.sides();
216         columns = tclass.columns();
217         pagestyle = tclass.pagestyle();
218         options = tclass.options();
219         secnumdepth = tclass.secnumdepth();
220         tocdepth = tclass.tocdepth();
221 }
222
223
224 bool BufferParams::hasClassDefaults() const
225 {
226         LyXTextClass const & tclass = textclasslist[textclass];
227
228         return (sides == tclass.sides()
229                 && columns == tclass.columns()
230                 && pagestyle == tclass.pagestyle()
231                 && options == tclass.options()
232                 && secnumdepth == tclass.secnumdepth()
233                 && tocdepth == tclass.tocdepth());
234 }
235
236
237 LyXTextClass const & BufferParams::getLyXTextClass() const
238 {
239         return textclasslist[textclass];
240 }
241
242
243 void BufferParams::readPreamble(LyXLex & lex)
244 {
245         if (lex.getString() != "\\begin_preamble")
246                 lyxerr << "Error (BufferParams::readPreamble):"
247                         "consistency check failed." << endl;
248
249         preamble = lex.getLongString("\\end_preamble");
250 }
251
252
253 void BufferParams::readLanguage(LyXLex & lex)
254 {
255         if (!lex.next()) return;
256
257         string const tmptok = lex.getString();
258
259         // check if tmptok is part of tex_babel in tex-defs.h
260         language = languages.getLanguage(tmptok);
261         if (!language) {
262                 // Language tmptok was not found
263                 language = default_language;
264                 lyxerr << "Warning: Setting language `"
265                        << tmptok << "' to `" << language->lang()
266                        << "'." << endl;
267         }
268 }
269
270
271 void BufferParams::readGraphicsDriver(LyXLex & lex)
272 {
273         if (!lex.next()) return;
274
275         string const tmptok = lex.getString();
276         // check if tmptok is part of tex_graphics in tex_defs.h
277         int n = 0;
278         while (true) {
279                 string const test = tex_graphics[n++];
280
281                 if (test == tmptok) {
282                         graphicsDriver = tmptok;
283                         break;
284                 } else if (test == "last_item") {
285                         lex.printError(
286                                 "Warning: graphics driver `$$Token' not recognized!\n"
287                                 "         Setting graphics driver to `default'.\n");
288                         graphicsDriver = "default";
289                         break;
290                 }
291         }
292 }