]> git.lyx.org Git - lyx.git/blob - src/insets/insetquotes.C
the fstream/iostream changes and some small other things
[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
26 // Quotes. Used for the various quotes. German, English, French,
27 // Danish, Polish, all either double or single.
28
29 extern LyXRC * lyxrc;
30 extern BufferView * current_view;
31
32 // codes used to read/write quotes to LyX files
33 static char const * const language_char = "esgpfa";
34 static char const * const side_char = "lr" ;
35 static char const * const times_char = "sd";
36
37 // List of known quote chars
38 static char const * const quote_char = ",'`<>";
39
40 // Index of chars used for the quote. Index is [side, language]
41 int quote_index[2][6] = {
42         { 2, 1, 0, 0, 3, 4 },    // "'',,<>" 
43         { 1, 1, 2, 1, 4, 3 } };  // "`'`'><"
44
45 // Corresponding LaTeX code, for double and single quotes.
46 static char const * const latex_quote_t1[2][5] = 
47 { { "\\quotesinglbase{}",  "'", "`", 
48     "\\guilsinglleft{}", "\\guilsinglright{}" }, 
49   { ",,", "''", "``", "<<", ">>" } };
50
51 static char const * const latex_quote_ot1[2][5] = 
52 { { "\\quotesinglbase{}",  "'", "`", 
53     "\\guilsinglleft{}", "\\guilsinglright{}" }, 
54   { "\\quotedblbase{}", "''", "``",
55     "\\guillemotleft{}", "\\guillemotright{}" } };
56
57 static char const * const latex_quote_babel[2][5] = 
58 { { "\\glq{}",  "'", "`", "\\flq{}", "\\frq{}" },
59   { "\\glqq{}", "''", "``", "\\flqq{}", "\\frqq{}" } };
60
61
62 InsetQuotes::InsetQuotes(string const & str)
63 {
64         ParseString(str);
65 }
66
67
68 InsetQuotes::InsetQuotes(InsetQuotes::quote_language l,
69                          InsetQuotes::quote_side s,
70                          InsetQuotes::quote_times t)
71         : language(l), side(s), times(t)
72 {
73 }
74
75
76 InsetQuotes::InsetQuotes(char c, BufferParams const & params)
77         : language(params.quotes_language), times(params.quotes_times)
78 {
79         // Decide whether left or right 
80         switch(c) {
81         case ' ': case '(': case '{': case '[': case '-': case ':':
82         case LyXParagraph::META_HFILL:
83         case LyXParagraph::META_PROTECTED_SEPARATOR:
84         case LyXParagraph::META_NEWLINE: 
85                 side = InsetQuotes::LeftQ;   // left quote 
86                 break;
87         default:
88                 side = InsetQuotes::RightQ;  // right quote
89         }
90 }
91
92
93 void InsetQuotes::ParseString(string const & s)
94 {
95         int i;
96         string str(s);
97         if (str.length() != 3) {
98                 lyxerr << "ERROR (InsetQuotes::InsetQuotes):"
99                         " bad string length." << endl;
100                 str = "eld";
101         }
102
103         for (i = 0; i < 6; ++i) {
104                 if (str[0] == language_char[i]) {
105                         language = (InsetQuotes::quote_language)i;
106                         break;
107                 }
108         }
109         if (i >= 6) {
110                 lyxerr << "ERROR (InsetQuotes::InsetQuotes):"
111                         " bad language specification." << endl;
112                 language = InsetQuotes::EnglishQ; 
113         }
114
115         for (i = 0; i < 2; ++i) {
116                 if (str[1] == side_char[i]) {
117                         side = (InsetQuotes::quote_side)i;
118                         break;
119                 }
120         }
121         if (i >= 2) {
122                 lyxerr << "ERROR (InsetQuotes::InsetQuotes):"
123                         " bad side specification." << endl;
124                 side = InsetQuotes::LeftQ; 
125         }
126
127         for (i = 0; i < 2; ++i) {
128                 if (str[2] == times_char[i]) {
129                         times = (InsetQuotes::quote_times)i;
130                         break;
131                 }
132         }
133         if (i >= 2) {
134                 lyxerr << "ERROR (InsetQuotes::InsetQuotes):"
135                         " bad times specification." << endl;
136                 times = InsetQuotes::DoubleQ; 
137         }
138 }
139
140
141 string InsetQuotes::DispString() const
142 {
143         string disp;
144         disp += quote_char[quote_index[side][language]];
145         if (times == InsetQuotes::DoubleQ)
146                 disp += disp;
147
148         if (lyxrc->font_norm == "iso8859-1") 
149                 if (disp == "<<")
150                         disp = '«';
151                 else if (disp == ">>")
152                         disp = '»';
153         
154         return disp;
155 }
156
157
158 int InsetQuotes::Ascent(LyXFont const & font) const
159 {
160         return font.maxAscent();
161 }
162
163
164 int InsetQuotes::Descent(LyXFont const & font) const
165 {
166         return font.maxDescent();
167 }
168
169
170 int InsetQuotes::Width(LyXFont const & font) const
171 {
172         string text = DispString();
173         int w = 0;
174
175         for (string::size_type i = 0; i < text.length(); ++i) {
176                 if (text[i] == ' ')
177                         w += font.width('i');
178                 else if (i == 0 || text[i] != text[i-1])
179                         w += font.width(text[i]);
180                 else
181                         w += font.width(',');
182         }
183
184         return w;
185 }
186
187
188 LyXFont InsetQuotes::ConvertFont(LyXFont font)
189 {
190         /* quotes-insets cannot be latex of any kind */
191         font.setLatex(LyXFont::OFF);
192         return font;
193 }
194
195
196 void InsetQuotes::Draw(LyXFont font, LyXScreen & scr,
197                        int baseline, float & x)
198 {
199         string text = DispString();
200
201         scr.drawString(font, text, baseline, int(x));
202         x += Width(font);
203 }
204
205
206 void InsetQuotes::Write(ostream & os)
207 {
208         string text;
209         text += language_char[language];
210         text += side_char[side];
211         text += times_char[times]; 
212         os << "Quotes " << text;
213 }
214
215
216 void InsetQuotes::Read(LyXLex & lex)
217 {
218         lex.nextToken();
219         ParseString(lex.GetString());
220 }
221
222
223 int InsetQuotes::Latex(ostream & os, signed char /*fragile*/)
224 {
225         string quote;
226         int res = Latex(quote, 0);
227         os << quote;
228         return res;
229 }
230
231
232 int InsetQuotes::Latex(string & file, signed char /*fragile*/)
233 {
234         string doclang = 
235                 current_view->buffer()->GetLanguage();
236         int quoteind = quote_index[side][language];
237         string qstr;
238         
239         if (lyxrc->fontenc == "T1") {
240                 qstr = latex_quote_t1[times][quoteind];
241         }
242         else if (doclang == "default") {
243                 qstr = latex_quote_ot1[times][quoteind];
244         } 
245         else if (language == InsetQuotes::FrenchQ 
246                  && times == InsetQuotes::DoubleQ
247                  && doclang == "frenchb") {
248                 if (side == InsetQuotes::LeftQ) 
249                         qstr = "\\og{}";
250                 else 
251                         qstr = " \\fg{}";
252         } 
253         else 
254                 qstr = latex_quote_babel[times][quoteind];
255
256         // protect against !` and ?` ligatures.
257         if ((suffixIs(file, '?') || suffixIs(file, '!'))
258             && qstr[0] == '`')
259                 qstr = "{}" + qstr;
260
261         file += qstr;
262         return 0;
263 }
264
265
266 int InsetQuotes::Linuxdoc(string & file)
267 {
268         file += "\"";
269
270         return 0;
271 }
272
273
274 int InsetQuotes::DocBook(string & file)
275 {
276         if(times == InsetQuotes::DoubleQ) {
277                 if (side == InsetQuotes::LeftQ)
278                         file += "&ldquo;";
279                 else
280                         file += "&rdquo;";
281         } else {
282                 if (side == InsetQuotes::LeftQ)
283                         file += "&lsquo;";
284                 else
285                         file += "&rsquo;";
286         }
287         return 0;
288 }
289
290
291 void InsetQuotes::Validate(LaTeXFeatures & features) const 
292 {
293         char type = quote_char[quote_index[side][language]];
294
295         if (current_view->buffer()->GetLanguage() == "default" 
296             && lyxrc->fontenc != "T1") {
297                 if (times == InsetQuotes::SingleQ) 
298                         switch (type) {
299                         case ',': features.quotesinglbase = true; break;
300                         case '<': features.guilsinglleft = true; break;
301                         case '>': features.guilsinglright = true; break;
302                         default: break;
303                         }
304                 else 
305                         switch (type) {
306                         case ',': features.quotedblbase = true; break;
307                         case '<': features.guillemotleft = true; break;
308                         case '>': features.guillemotright = true; break;
309                         default: break;
310                         }
311         }
312 }
313
314
315 Inset * InsetQuotes::Clone() const
316 {
317   return new InsetQuotes(language, side, times);
318 }
319
320
321 Inset::Code InsetQuotes::LyxCode() const
322 {
323   return Inset::QUOTE_CODE;
324 }