]> git.lyx.org Git - lyx.git/blob - src/bufferparams.C
layout as string
[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
26 #include "support/lyxlib.h"
27 #include "support/lstrings.h"
28
29 #include <cstdlib>
30
31 using std::ostream;
32 using std::endl;
33
34 BufferParams::BufferParams()
35         // Initialize textclass to point to article. if `first' is
36         // true in the returned pair, then `second' is the textclass
37         // number; if it is false, second is 0. In both cases, second
38         // is what we want.
39         : textclass(textclasslist.NumberOfClass("article").second)
40 {
41         paragraph_separation = PARSEP_INDENT;
42         defskip = VSpace(VSpace::MEDSKIP); 
43         quotes_language = InsetQuotes::EnglishQ;
44         quotes_times = InsetQuotes::DoubleQ;
45         fontsize = "default";
46
47         /*  PaperLayout */
48         papersize = PAPER_DEFAULT;
49         papersize2 = VM_PAPER_DEFAULT; /* DEFAULT */
50         paperpackage = PACKAGE_NONE;
51         orientation = ORIENTATION_PORTRAIT;
52         use_geometry = false;
53         use_amsmath = false;
54         use_natbib = false;
55         use_numerical_citations = false;
56         secnumdepth = 3;
57         tocdepth = 3;
58         language = default_language;
59         fonts = "default";
60         inputenc = "auto";
61         graphicsDriver = "default";
62         sides = LyXTextClass::OneSide;
63         columns = 1;
64         pagestyle = "default";
65         for (int iter = 0; iter < 4; ++iter) {
66                 user_defined_bullets[iter] = ITEMIZE_DEFAULTS[iter];
67                 temp_bullets[iter] = ITEMIZE_DEFAULTS[iter];
68         }
69 }
70
71
72 void BufferParams::writeFile(ostream & os) const
73 {
74         // The top of the file is written by the buffer.
75         // Prints out the buffer info into the .lyx file given by file
76
77         // the textclass
78         os << "\\textclass " << textclasslist[textclass].name() << '\n';
79         
80         // then the the preamble
81         if (!preamble.empty()) {
82                 // remove '\n' from the end of preamble
83                 string const tmppreamble = strip(preamble, '\n');
84                 os << "\\begin_preamble\n"
85                    << tmppreamble
86                    << "\n\\end_preamble\n";
87         }
88       
89         /* the options */ 
90         if (!options.empty()) {
91                 os << "\\options " << options << '\n';
92         }
93    
94         /* then the text parameters */
95         if (language != ignore_language)
96                 os << "\\language " << language->lang() << '\n';
97         os << "\\inputencoding " << inputenc
98            << "\n\\fontscheme " << fonts
99            << "\n\\graphics " << graphicsDriver << '\n';
100
101         if (!float_placement.empty()) {
102                 os << "\\float_placement " << float_placement << '\n';
103         }
104         os << "\\paperfontsize " << fontsize << '\n';
105
106         spacing.writeFile(os);
107
108         os << "\\papersize " << string_papersize[papersize2]
109            << "\n\\paperpackage " << string_paperpackages[paperpackage]
110            << "\n\\use_geometry " << use_geometry
111            << "\n\\use_amsmath " << use_amsmath
112            << "\n\\use_natbib " << use_natbib
113            << "\n\\use_numerical_citations " << use_numerical_citations
114            << "\n\\paperorientation " << string_orientation[orientation]
115            << '\n';
116         if (!paperwidth.empty())
117                 os << "\\paperwidth "
118                    << VSpace(paperwidth).asLyXCommand() << '\n';
119         if (!paperheight.empty())
120                 os << "\\paperheight "
121                    << VSpace(paperheight).asLyXCommand() << '\n';
122         if (!leftmargin.empty())
123                 os << "\\leftmargin "
124                    << VSpace(leftmargin).asLyXCommand() << '\n';
125         if (!topmargin.empty())
126                 os << "\\topmargin "
127                    << VSpace(topmargin).asLyXCommand() << '\n';
128         if (!rightmargin.empty())
129                 os << "\\rightmargin "
130                    << VSpace(rightmargin).asLyXCommand() << '\n';
131         if (!bottommargin.empty())
132                 os << "\\bottommargin "
133                    << VSpace(bottommargin).asLyXCommand() << '\n';
134         if (!headheight.empty())
135                 os << "\\headheight "
136                    << VSpace(headheight).asLyXCommand() << '\n';
137         if (!headsep.empty())
138                 os << "\\headsep "
139                    << VSpace(headsep).asLyXCommand() << '\n';
140         if (!footskip.empty())
141                 os << "\\footskip "
142                    << VSpace(footskip).asLyXCommand() << '\n';
143         os << "\\secnumdepth " << secnumdepth
144            << "\n\\tocdepth " << tocdepth
145            << "\n\\paragraph_separation "
146            << string_paragraph_separation[paragraph_separation]
147            << "\n\\defskip " << defskip.asLyXCommand()
148            << "\n\\quotes_language "
149            << string_quotes_language[quotes_language] << '\n';
150         switch (quotes_times) {
151                 // An output operator for insetquotes would be nice
152         case InsetQuotes::SingleQ:
153                 os << "\\quotes_times 1\n"; break;
154         case InsetQuotes::DoubleQ: 
155                 os << "\\quotes_times 2\n"; break;
156         }
157         os << "\\papercolumns " << columns
158            << "\n\\papersides " << sides
159            << "\n\\paperpagestyle " << pagestyle << '\n';
160         for (int i = 0; i < 4; ++i) {
161                 if (user_defined_bullets[i] != ITEMIZE_DEFAULTS[i]) {
162                         if (user_defined_bullets[i].getFont() != -1) {
163                                 os << "\\bullet " << i
164                                    << "\n\t"
165                                    << user_defined_bullets[i].getFont()
166                                    << "\n\t"
167                                    << user_defined_bullets[i].getCharacter()
168                                    << "\n\t"
169                                    << user_defined_bullets[i].getSize()
170                                    << "\n\\end_bullet\n";
171                         }
172                         else {
173                                 os << "\\bulletLaTeX " << i
174                                    << "\n\t\""
175                                    << user_defined_bullets[i].getText()
176                                    << "\"\n\\end_bullet\n";
177                         }
178                 }
179         }
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 void BufferParams::readPreamble(LyXLex & lex)
237 {
238         if (lex.getString() != "\\begin_preamble")
239                 lyxerr << "Error (BufferParams::readPreamble):"
240                         "consistency check failed." << endl;
241
242         preamble = lex.getLongString("\\end_preamble");
243 }
244
245
246 void BufferParams::readLanguage(LyXLex & lex)
247 {
248         if (!lex.next()) return;
249         
250         string const tmptok = lex.getString();
251
252         // check if tmptok is part of tex_babel in tex-defs.h
253         language = languages.getLanguage(tmptok);
254         if (!language) {
255                 // Language tmptok was not found
256                 language = default_language;
257                 lyxerr << "Warning: Setting language `"
258                        << tmptok << "' to `" << language->lang()
259                        << "'." << endl;
260         }
261 }
262
263
264 void BufferParams::readGraphicsDriver(LyXLex & lex)
265 {
266         if (!lex.next()) return;
267         
268         string const tmptok = lex.getString();
269         // check if tmptok is part of tex_graphics in tex_defs.h
270         int n = 0;
271         while (true) {
272                 string const test = tex_graphics[n++];
273                 
274                 if (test == tmptok) {    
275                         graphicsDriver = tmptok;
276                         break;
277                 } else if (test == "last_item") {
278                         lex.printError(
279                                 "Warning: graphics driver `$$Token' not recognized!\n"
280                                 "         Setting graphics driver to `default'.\n");
281                         graphicsDriver = "default";
282                         break;
283                 }      
284         }
285 }