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