]> git.lyx.org Git - lyx.git/blob - src/insets/insetquotes.C
change to use ostreams instead of string when writing files. fiddling with insettext...
[lyx.git] / src / insets / insetquotes.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
13 #ifdef __GNUG__
14 #pragma implementation
15 #endif
16
17 #include "insetquotes.h"
18 #include "support/lyxlib.h"
19 #include "debug.h"
20 #include "lyxfont.h"
21 #include "lyxrc.h"
22 #include "buffer.h"
23 #include "LaTeXFeatures.h"
24 #include "support/lstrings.h"
25 #include "Painter.h"
26
27 // Quotes. Used for the various quotes. German, English, French,
28 // Danish, Polish, all either double or single.
29
30 extern LyXRC * lyxrc;
31 extern BufferView * current_view;
32
33 // codes used to read/write quotes to LyX files
34 static char const * const language_char = "esgpfa";
35 static char const * const side_char = "lr" ;
36 static char const * const times_char = "sd";
37
38 // List of known quote chars
39 static char const * const quote_char = ",'`<>";
40
41 // Index of chars used for the quote. Index is [side, language]
42 int quote_index[2][6] = {
43         { 2, 1, 0, 0, 3, 4 },    // "'',,<>" 
44         { 1, 1, 2, 1, 4, 3 } };  // "`'`'><"
45
46 // Corresponding LaTeX code, for double and single quotes.
47 static char const * const latex_quote_t1[2][5] = 
48 { { "\\quotesinglbase{}",  "'", "`", 
49     "\\guilsinglleft{}", "\\guilsinglright{}" }, 
50   { ",,", "''", "``", "<<", ">>" } };
51
52 static char const * const latex_quote_ot1[2][5] = 
53 { { "\\quotesinglbase{}",  "'", "`", 
54     "\\guilsinglleft{}", "\\guilsinglright{}" }, 
55   { "\\quotedblbase{}", "''", "``",
56     "\\guillemotleft{}", "\\guillemotright{}" } };
57
58 static char const * const latex_quote_babel[2][5] = 
59 { { "\\glq{}",  "'", "`", "\\flq{}", "\\frq{}" },
60   { "\\glqq{}", "''", "``", "\\flqq{}", "\\frqq{}" } };
61
62
63 InsetQuotes::InsetQuotes(string const & str)
64 {
65         ParseString(str);
66 }
67
68
69 InsetQuotes::InsetQuotes(InsetQuotes::quote_language l,
70                          InsetQuotes::quote_side s,
71                          InsetQuotes::quote_times t)
72         : language(l), side(s), times(t)
73 {
74 }
75
76
77 InsetQuotes::InsetQuotes(char c, BufferParams const & params)
78         : language(params.quotes_language), times(params.quotes_times)
79 {
80         // Decide whether left or right 
81         switch(c) {
82         case ' ': case '(': case '{': case '[': case '-': case ':':
83         case LyXParagraph::META_HFILL:
84 #warning think about this
85 #if 0
86         case LyXParagraph::META_PROTECTED_SEPARATOR:
87 #endif
88         case LyXParagraph::META_NEWLINE: 
89                 side = InsetQuotes::LeftQ;   // left quote 
90                 break;
91         default:
92                 side = InsetQuotes::RightQ;  // right quote
93         }
94 }
95
96
97 void InsetQuotes::ParseString(string const & s)
98 {
99         int i;
100         string str(s);
101         if (str.length() != 3) {
102                 lyxerr << "ERROR (InsetQuotes::InsetQuotes):"
103                         " bad string length." << endl;
104                 str = "eld";
105         }
106
107         for (i = 0; i < 6; ++i) {
108                 if (str[0] == language_char[i]) {
109                         language = InsetQuotes::quote_language(i);
110                         break;
111                 }
112         }
113         if (i >= 6) {
114                 lyxerr << "ERROR (InsetQuotes::InsetQuotes):"
115                         " bad language specification." << endl;
116                 language = InsetQuotes::EnglishQ; 
117         }
118
119         for (i = 0; i < 2; ++i) {
120                 if (str[1] == side_char[i]) {
121                         side = InsetQuotes::quote_side(i);
122                         break;
123                 }
124         }
125         if (i >= 2) {
126                 lyxerr << "ERROR (InsetQuotes::InsetQuotes):"
127                         " bad side specification." << endl;
128                 side = InsetQuotes::LeftQ; 
129         }
130
131         for (i = 0; i < 2; ++i) {
132                 if (str[2] == times_char[i]) {
133                         times = InsetQuotes::quote_times(i);
134                         break;
135                 }
136         }
137         if (i >= 2) {
138                 lyxerr << "ERROR (InsetQuotes::InsetQuotes):"
139                         " bad times specification." << endl;
140                 times = InsetQuotes::DoubleQ; 
141         }
142 }
143
144
145 string InsetQuotes::DispString() const
146 {
147         string disp;
148         disp += quote_char[quote_index[side][language]];
149         if (times == InsetQuotes::DoubleQ)
150                 disp += disp;
151
152         if (lyxrc->font_norm == "iso8859-1") 
153                 if (disp == "<<")
154                         disp = '«';
155                 else if (disp == ">>")
156                         disp = '»';
157         
158         return disp;
159 }
160
161
162 int InsetQuotes::ascent(Painter &, LyXFont const & font) const
163 {
164         return font.maxAscent();
165 }
166
167
168 int InsetQuotes::descent(Painter &, LyXFont const & font) const
169 {
170         return font.maxDescent();
171 }
172
173
174 int InsetQuotes::width(Painter &, LyXFont const & font) const
175 {
176         string text = DispString();
177         int w = 0;
178
179         for (string::size_type i = 0; i < text.length(); ++i) {
180                 if (text[i] == ' ')
181                         w += font.width('i');
182                 else if (i == 0 || text[i] != text[i-1])
183                         w += font.width(text[i]);
184                 else
185                         w += font.width(',');
186         }
187
188         return w;
189 }
190
191
192 LyXFont InsetQuotes::ConvertFont(LyXFont font)
193 {
194         /* quotes-insets cannot be latex of any kind */
195         font.setLatex(LyXFont::OFF);
196         return font;
197 }
198
199
200 void InsetQuotes::draw(Painter & pain, LyXFont const & font,
201                        int baseline, float & x) const
202 {
203         string text = DispString();
204
205         pain.text(int(x), baseline, text, font);
206         x += width(pain, font);
207 }
208
209
210 void InsetQuotes::Write(ostream & os) const
211 {
212         string text;
213         text += language_char[language];
214         text += side_char[side];
215         text += times_char[times]; 
216         os << "Quotes " << text;
217 }
218
219
220 void InsetQuotes::Read(LyXLex & lex)
221 {
222         lex.nextToken();
223         ParseString(lex.GetString());
224 }
225
226
227 int InsetQuotes::Latex(ostream & os, signed char /*fragile*/) const
228 {
229 #ifdef USE_OSTREAM_ONLY
230         string doclang = 
231                 current_view->buffer()->GetLanguage();
232         int quoteind = quote_index[side][language];
233         string qstr;
234         
235         if (lyxrc->fontenc == "T1") {
236                 qstr = latex_quote_t1[times][quoteind];
237         }
238         else if (doclang == "default") {
239                 qstr = latex_quote_ot1[times][quoteind];
240         } 
241         else if (language == InsetQuotes::FrenchQ 
242                  && times == InsetQuotes::DoubleQ
243                  && doclang == "frenchb") {
244                 if (side == InsetQuotes::LeftQ) 
245                         qstr = "\\og{}";
246                 else 
247                         qstr = " \\fg{}";
248         } 
249         else 
250                 qstr = latex_quote_babel[times][quoteind];
251
252         // protect against !` and ?` ligatures.
253         // Is it very bad of us to always protect against those ligatures?
254 #if 0
255         if ((suffixIs(file, '?') || suffixIs(file, '!'))
256             && qstr[0] == '`')
257                 qstr = "{}" + qstr;
258 #else
259         // Always guard against unfortunate ligatures (!` ?`)
260         qstr.insert(0, "{}");
261 #endif
262
263         os << qstr;
264         return 0;
265 #else
266         string quote;
267         int res = Latex(quote, 0);
268         os << quote;
269         return res;
270 #endif
271 }
272
273
274 #ifndef USE_OSTREAM_ONLY
275 int InsetQuotes::Latex(string & file, signed char /*fragile*/) const
276 {
277         string doclang = 
278                 current_view->buffer()->GetLanguage();
279         int quoteind = quote_index[side][language];
280         string qstr;
281         
282         if (lyxrc->fontenc == "T1") {
283                 qstr = latex_quote_t1[times][quoteind];
284         }
285         else if (doclang == "default") {
286                 qstr = latex_quote_ot1[times][quoteind];
287         } 
288         else if (language == InsetQuotes::FrenchQ 
289                  && times == InsetQuotes::DoubleQ
290                  && doclang == "frenchb") {
291                 if (side == InsetQuotes::LeftQ) 
292                         qstr = "\\og{}";
293                 else 
294                         qstr = " \\fg{}";
295         } 
296         else 
297                 qstr = latex_quote_babel[times][quoteind];
298
299         // protect against !` and ?` ligatures.
300         if ((suffixIs(file, '?') || suffixIs(file, '!'))
301             && qstr[0] == '`')
302                 qstr = "{}" + qstr;
303
304         file += qstr;
305         return 0;
306 }
307
308
309 int InsetQuotes::Linuxdoc(string & file) const
310 {
311         file += "\"";
312
313         return 0;
314 }
315
316
317 int InsetQuotes::DocBook(string & file) const
318 {
319         if(times == InsetQuotes::DoubleQ) {
320                 if (side == InsetQuotes::LeftQ)
321                         file += "&ldquo;";
322                 else
323                         file += "&rdquo;";
324         } else {
325                 if (side == InsetQuotes::LeftQ)
326                         file += "&lsquo;";
327                 else
328                         file += "&rsquo;";
329         }
330         return 0;
331 }
332
333 #else
334
335 int InsetQuotes::Linuxdoc(ostream & os) const
336 {
337         os << "\"";
338         return 0;
339 }
340
341
342 int InsetQuotes::DocBook(ostream & os) const
343 {
344         if(times == InsetQuotes::DoubleQ) {
345                 if (side == InsetQuotes::LeftQ)
346                         os << "&ldquo;";
347                 else
348                         os << "&rdquo;";
349         } else {
350                 if (side == InsetQuotes::LeftQ)
351                         os << "&lsquo;";
352                 else
353                         os << "&rsquo;";
354         }
355         return 0;
356 }
357 #endif
358
359
360 void InsetQuotes::Validate(LaTeXFeatures & features) const 
361 {
362         char type = quote_char[quote_index[side][language]];
363
364         if (current_view->buffer()->GetLanguage() == "default" 
365             && lyxrc->fontenc != "T1") {
366                 if (times == InsetQuotes::SingleQ) 
367                         switch (type) {
368                         case ',': features.quotesinglbase = true; break;
369                         case '<': features.guilsinglleft = true; break;
370                         case '>': features.guilsinglright = true; break;
371                         default: break;
372                         }
373                 else 
374                         switch (type) {
375                         case ',': features.quotedblbase = true; break;
376                         case '<': features.guillemotleft = true; break;
377                         case '>': features.guillemotright = true; break;
378                         default: break;
379                         }
380         }
381 }
382
383
384 Inset * InsetQuotes::Clone() const
385 {
386   return new InsetQuotes(language, side, times);
387 }
388
389
390 Inset::Code InsetQuotes::LyxCode() const
391 {
392   return Inset::QUOTE_CODE;
393 }