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