This commit was manufactured by cvs2svn to create tag
[lyx.git] / src / insets / insetquotes.C
blob3fba432722d2a7fb9f9bf3c7378a0abd80c99910
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  * ======================================================*/
11 #include <config.h>
13 #ifdef __GNUG__
14 #pragma implementation
15 #endif
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"
26 // Quotes. Used for the various quotes. German, English, French,
27 // Danish, Polish, all either double or single.
29 extern LyXRC * lyxrc;
30 extern BufferView * current_view;
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";
37 // List of known quote chars
38 static char const * const quote_char = ",'`<>";
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 } };  // "`'`'><"
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   { ",,", "''", "``", "<<", ">>" } };
51 static char const * const latex_quote_ot1[2][5] =
52 { { "\\quotesinglbase{}",  "'", "`", 
53     "\\guilsinglleft{}", "\\guilsinglright{}" }, 
54   { "\\quotedblbase{}", "''", "``",
55     "\\guillemotleft{}", "\\guillemotright{}" } };
57 static char const * const latex_quote_babel[2][5] =
58 { { "\\glq{}",  "'", "`", "\\flq{}", "\\frq{}" },
59   { "\\glqq{}", "''", "``", "\\flqq{}", "\\frqq{}" } };
62 InsetQuotes::InsetQuotes(string const & str)
64         ParseString(str);
67 InsetQuotes::InsetQuotes(InsetQuotes::quote_language l,
68                          InsetQuotes::quote_side s,
69                          InsetQuotes::quote_times t)
70         : language(l), side(s), times(t)
75 InsetQuotes::InsetQuotes(char c, BufferParams const & params)
76         : language(params.quotes_language), times(params.quotes_times)
78         // Decide whether left or right 
79         switch(c) {
80         case ' ': case '(': case '{': case '[': case '-': case ':':
81         case LYX_META_HFILL: case LYX_META_PROTECTED_SEPARATOR:
82         case LYX_META_NEWLINE: 
83                 side = InsetQuotes::LeftQ;   // left quote 
84                 break;
85         default:
86                 side = InsetQuotes::RightQ;  // right quote
87         }
91 void InsetQuotes::ParseString(string const & s)
93         int i;
94         string str(s);
95         if (str.length() != 3) {
96                 lyxerr << "ERROR (InsetQuotes::InsetQuotes):"
97                         " bad string length." << endl;
98                 str = "eld";
99         }
101         for (i=0;i<6;i++) {
102                 if (str[0] == language_char[i]) {
103                         language = (InsetQuotes::quote_language)i;
104                         break;
105                 }
106         }
107         if (i>=6) {
108                 lyxerr << "ERROR (InsetQuotes::InsetQuotes):"
109                         " bad language specification." << endl;
110                 language = InsetQuotes::EnglishQ; 
111         }
113         for (i=0;i<2;i++) {
114                 if (str[1] == side_char[i]) {
115                         side = (InsetQuotes::quote_side)i;
116                         break;
117                 }
118         }
119         if (i>=2) {
120                 lyxerr << "ERROR (InsetQuotes::InsetQuotes):"
121                         " bad side specification." << endl;
122                 side = InsetQuotes::LeftQ; 
123         }
125         for (i=0;i<2;i++) {
126                 if (str[2] == times_char[i]) {
127                         times = (InsetQuotes::quote_times)i;
128                         break;
129                 }
130         }
131         if (i>=2) {
132                 lyxerr << "ERROR (InsetQuotes::InsetQuotes):"
133                         " bad times specification." << endl;
134                 times = InsetQuotes::DoubleQ; 
135         }
138 string InsetQuotes::DispString() const
140         string disp;
141         disp += quote_char[quote_index[side][language]];
142         if (times == InsetQuotes::DoubleQ)
143                 disp += disp;
145         if (lyxrc->font_norm == "iso8859-1") 
146                 if (disp == "<<")
147                         disp = '«';
148                 else if (disp == ">>")
149                         disp = '»';
150         
151         return disp;
155 int InsetQuotes::Ascent(LyXFont const & font) const
157         return font.maxAscent();
161 int InsetQuotes::Descent(LyXFont const & font) const
163         return font.maxDescent();
167 int InsetQuotes::Width(LyXFont const & font) const
169         string text = DispString();
170         int w = 0;
172         for (string::size_type i = 0; i < text.length(); ++i) {
173                 if (text[i] == ' ')
174                         w += font.width('i');
175                 else if (i == 0 || text[i] != text[i-1])
176                         w += font.width(text[i]);
177                 else
178                         w += font.width(',');
179         }
181         return w;
185 LyXFont InsetQuotes::ConvertFont(LyXFont font)
187         /* quotes-insets cannot be latex of any kind */
188         font.setLatex(LyXFont::OFF);
189         return font;
193 void InsetQuotes::Draw(LyXFont font, LyXScreen & scr,
194                        int baseline, float & x)
196         string text = DispString();
198         scr.drawString(font, text, baseline, int(x));
199         x += Width(font);
203 void InsetQuotes::Write(FILE * file)
205         string text;
206         text += language_char[language];
207         text += side_char[side];
208         text += times_char[times]; 
209         fprintf(file, "Quotes %s", text.c_str());
213 void InsetQuotes::Read(LyXLex & lex)
215         lex.nextToken();
216         ParseString(lex.GetString());
220 int InsetQuotes::Latex(FILE * file, signed char /*fragile*/)
222         string quote;
223         int res = Latex(quote, 0);
224         fprintf(file, "%s", quote.c_str()); 
225         return res;
228 int InsetQuotes::Latex(string & file, signed char /*fragile*/)
230         string doclang =
231                 current_view->currentBuffer()->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];
252         // protect against !` and ?` ligatures.
253         if ((suffixIs(file, '?') || suffixIs(file, '!'))
254             && qstr[0] == '`')
255                 qstr = "{}" + qstr;
257         file += qstr;
258         return 0;
262 int InsetQuotes::Linuxdoc(string & file)
264         file += "\"";
266         return 0;
270 int InsetQuotes::DocBook(string & file)
272         if(times == InsetQuotes::DoubleQ) {
273                 if (side == InsetQuotes::LeftQ)
274                         file += "&ldquo;";
275                 else
276                         file += "&rdquo;";
277         } else {
278                 if (side == InsetQuotes::LeftQ)
279                         file += "&lsquo;";
280                 else
281                         file += "&rsquo;";
282         }
283         return 0;
287 void InsetQuotes::Validate(LaTeXFeatures & features) const 
289         char type = quote_char[quote_index[side][language]];
291         if (current_view->currentBuffer()->GetLanguage() == "default" 
292             && lyxrc->fontenc != "T1") {
293                 if (times == InsetQuotes::SingleQ) 
294                         switch (type) {
295                         case ',': features.quotesinglbase = true; break;
296                         case '<': features.guilsinglleft = true; break;
297                         case '>': features.guilsinglright = true; break;
298                         default: break;
299                         }
300                 else 
301                         switch (type) {
302                         case ',': features.quotedblbase = true; break;
303                         case '<': features.guillemotleft = true; break;
304                         case '>': features.guillemotright = true; break;
305                         default: break;
306                         }
307         }
310 Inset * InsetQuotes::Clone()
312   return new InsetQuotes(language, side, times);
316 Inset::Code InsetQuotes::LyxCode() const
318   return Inset::QUOTE_CODE;