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