]> git.lyx.org Git - lyx.git/blob - src/insets/insetquotes.C
fix the resize bug, make some more inset funcs const, cleanup in lyxscreen, painter...
[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         case LyXParagraph::META_PROTECTED_SEPARATOR:
85         case LyXParagraph::META_NEWLINE: 
86                 side = InsetQuotes::LeftQ;   // left quote 
87                 break;
88         default:
89                 side = InsetQuotes::RightQ;  // right quote
90         }
91 }
92
93
94 void InsetQuotes::ParseString(string const & s)
95 {
96         int i;
97         string str(s);
98         if (str.length() != 3) {
99                 lyxerr << "ERROR (InsetQuotes::InsetQuotes):"
100                         " bad string length." << endl;
101                 str = "eld";
102         }
103
104         for (i = 0; i < 6; ++i) {
105                 if (str[0] == language_char[i]) {
106                         language = InsetQuotes::quote_language(i);
107                         break;
108                 }
109         }
110         if (i >= 6) {
111                 lyxerr << "ERROR (InsetQuotes::InsetQuotes):"
112                         " bad language specification." << endl;
113                 language = InsetQuotes::EnglishQ; 
114         }
115
116         for (i = 0; i < 2; ++i) {
117                 if (str[1] == side_char[i]) {
118                         side = InsetQuotes::quote_side(i);
119                         break;
120                 }
121         }
122         if (i >= 2) {
123                 lyxerr << "ERROR (InsetQuotes::InsetQuotes):"
124                         " bad side specification." << endl;
125                 side = InsetQuotes::LeftQ; 
126         }
127
128         for (i = 0; i < 2; ++i) {
129                 if (str[2] == times_char[i]) {
130                         times = InsetQuotes::quote_times(i);
131                         break;
132                 }
133         }
134         if (i >= 2) {
135                 lyxerr << "ERROR (InsetQuotes::InsetQuotes):"
136                         " bad times specification." << endl;
137                 times = InsetQuotes::DoubleQ; 
138         }
139 }
140
141
142 string InsetQuotes::DispString() const
143 {
144         string disp;
145         disp += quote_char[quote_index[side][language]];
146         if (times == InsetQuotes::DoubleQ)
147                 disp += disp;
148
149         if (lyxrc->font_norm == "iso8859-1") 
150                 if (disp == "<<")
151                         disp = '«';
152                 else if (disp == ">>")
153                         disp = '»';
154         
155         return disp;
156 }
157
158
159 int InsetQuotes::ascent(Painter &, LyXFont const & font) const
160 {
161         return font.maxAscent();
162 }
163
164
165 int InsetQuotes::descent(Painter &, LyXFont const & font) const
166 {
167         return font.maxDescent();
168 }
169
170
171 int InsetQuotes::width(Painter &, LyXFont const & font) const
172 {
173         string text = DispString();
174         int w = 0;
175
176         for (string::size_type i = 0; i < text.length(); ++i) {
177                 if (text[i] == ' ')
178                         w += font.width('i');
179                 else if (i == 0 || text[i] != text[i-1])
180                         w += font.width(text[i]);
181                 else
182                         w += font.width(',');
183         }
184
185         return w;
186 }
187
188
189 LyXFont InsetQuotes::ConvertFont(LyXFont font)
190 {
191         /* quotes-insets cannot be latex of any kind */
192         font.setLatex(LyXFont::OFF);
193         return font;
194 }
195
196
197 void InsetQuotes::draw(Painter & pain, LyXFont const & font,
198                        int baseline, float & x) const
199 {
200         string text = DispString();
201
202         pain.text(int(x), baseline, text, font);
203         x += width(pain, font);
204 }
205
206
207 void InsetQuotes::Write(ostream & os) const
208 {
209         string text;
210         text += language_char[language];
211         text += side_char[side];
212         text += times_char[times]; 
213         os << "Quotes " << text;
214 }
215
216
217 void InsetQuotes::Read(LyXLex & lex)
218 {
219         lex.nextToken();
220         ParseString(lex.GetString());
221 }
222
223
224 int InsetQuotes::Latex(ostream & os, signed char /*fragile*/) const
225 {
226         string quote;
227         int res = Latex(quote, 0);
228         os << quote;
229         return res;
230 }
231
232
233 int InsetQuotes::Latex(string & file, signed char /*fragile*/) const
234 {
235         string doclang = 
236                 current_view->buffer()->GetLanguage();
237         int quoteind = quote_index[side][language];
238         string qstr;
239         
240         if (lyxrc->fontenc == "T1") {
241                 qstr = latex_quote_t1[times][quoteind];
242         }
243         else if (doclang == "default") {
244                 qstr = latex_quote_ot1[times][quoteind];
245         } 
246         else if (language == InsetQuotes::FrenchQ 
247                  && times == InsetQuotes::DoubleQ
248                  && doclang == "frenchb") {
249                 if (side == InsetQuotes::LeftQ) 
250                         qstr = "\\og{}";
251                 else 
252                         qstr = " \\fg{}";
253         } 
254         else 
255                 qstr = latex_quote_babel[times][quoteind];
256
257         // protect against !` and ?` ligatures.
258         if ((suffixIs(file, '?') || suffixIs(file, '!'))
259             && qstr[0] == '`')
260                 qstr = "{}" + qstr;
261
262         file += qstr;
263         return 0;
264 }
265
266
267 int InsetQuotes::Linuxdoc(string & file) const
268 {
269         file += "\"";
270
271         return 0;
272 }
273
274
275 int InsetQuotes::DocBook(string & file) const
276 {
277         if(times == InsetQuotes::DoubleQ) {
278                 if (side == InsetQuotes::LeftQ)
279                         file += "&ldquo;";
280                 else
281                         file += "&rdquo;";
282         } else {
283                 if (side == InsetQuotes::LeftQ)
284                         file += "&lsquo;";
285                 else
286                         file += "&rsquo;";
287         }
288         return 0;
289 }
290
291
292 void InsetQuotes::Validate(LaTeXFeatures & features) const 
293 {
294         char type = quote_char[quote_index[side][language]];
295
296         if (current_view->buffer()->GetLanguage() == "default" 
297             && lyxrc->fontenc != "T1") {
298                 if (times == InsetQuotes::SingleQ) 
299                         switch (type) {
300                         case ',': features.quotesinglbase = true; break;
301                         case '<': features.guilsinglleft = true; break;
302                         case '>': features.guilsinglright = true; break;
303                         default: break;
304                         }
305                 else 
306                         switch (type) {
307                         case ',': features.quotedblbase = true; break;
308                         case '<': features.guillemotleft = true; break;
309                         case '>': features.guillemotright = true; break;
310                         default: break;
311                         }
312         }
313 }
314
315
316 Inset * InsetQuotes::Clone() const
317 {
318   return new InsetQuotes(language, side, times);
319 }
320
321
322 Inset::Code InsetQuotes::LyxCode() const
323 {
324   return Inset::QUOTE_CODE;
325 }