]> git.lyx.org Git - lyx.git/blob - src/insets/insetcharstyle.C
Change _() to return a docstring. Fixup callers with the help of lyx::to_utf8.
[lyx.git] / src / insets / insetcharstyle.C
1 /**
2  * \file insetcharstyle.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Angus Leeming
7  * \author Martin Vermeer
8  * \author Jürgen Spitzmüller
9  *
10  * Full author contact details are available in file CREDITS.
11  */
12
13 #include <config.h>
14
15 #include "insetcharstyle.h"
16
17 #include "buffer.h"
18 #include "BufferView.h"
19 #include "bufferparams.h"
20 #include "dispatchresult.h"
21 #include "funcrequest.h"
22 #include "FuncStatus.h"
23 #include "cursor.h"
24 #include "gettext.h"
25 #include "LaTeXFeatures.h"
26 #include "LColor.h"
27 #include "lyxlex.h"
28 #include "lyxtext.h"
29 #include "metricsinfo.h"
30 #include "paragraph.h"
31 #include "paragraph_funcs.h"
32 #include "sgml.h"
33
34 #include "frontends/font_metrics.h"
35 #include "frontends/Painter.h"
36
37 #include <sstream>
38
39 using lyx::docstring;
40
41 using std::string;
42 using std::auto_ptr;
43 using std::istringstream;
44 using std::max;
45 using std::ostream;
46 using std::ostringstream;
47
48
49 void InsetCharStyle::init()
50 {
51         setInsetName("CharStyle");
52         setInlined();
53         setDrawFrame(false);
54         has_label_ = true;
55 }
56
57
58 InsetCharStyle::InsetCharStyle(BufferParams const & bp, string const s)
59         : InsetCollapsable(bp)
60 {
61         params_.type = s;
62         setUndefined();
63         init();
64 }
65
66
67 InsetCharStyle::InsetCharStyle(BufferParams const & bp,
68                                 CharStyles::iterator cs)
69         : InsetCollapsable(bp)
70 {
71         params_.type = cs->name;
72         setDefined(cs);
73         init();
74 }
75
76
77 InsetCharStyle::InsetCharStyle(InsetCharStyle const & in)
78         : InsetCollapsable(in), params_(in.params_)
79 {
80         init();
81 }
82
83
84 auto_ptr<InsetBase> InsetCharStyle::doClone() const
85 {
86         return auto_ptr<InsetBase>(new InsetCharStyle(*this));
87 }
88
89
90 bool InsetCharStyle::undefined() const
91 {
92         return params_.latexname.empty();
93 }
94
95
96 void InsetCharStyle::setUndefined()
97 {
98         params_.latextype.clear();
99         params_.latexname.clear();
100         params_.latexparam.clear();
101         params_.font = LyXFont(LyXFont::ALL_INHERIT);
102         params_.labelfont = LyXFont(LyXFont::ALL_INHERIT);
103         params_.labelfont.setColor(LColor::error);
104 }
105
106
107 void InsetCharStyle::setDefined(CharStyles::iterator cs)
108 {
109         params_.latextype = cs->latextype;
110         params_.latexname = cs->latexname;
111         params_.latexparam = cs->latexparam;
112         params_.font = cs->font;
113         params_.labelfont = cs->labelfont;
114 }
115
116
117 string const InsetCharStyle::editMessage() const
118 {
119         // FIXME UNICODE
120         return lyx::to_utf8(_("Opened CharStyle Inset"));
121 }
122
123
124 void InsetCharStyle::write(Buffer const & buf, ostream & os) const
125 {
126         params_.write(os);
127         InsetCollapsable::write(buf, os);
128 }
129
130
131 void InsetCharStyle::read(Buffer const & buf, LyXLex & lex)
132 {
133         InsetCollapsable::read(buf, lex);
134         setInlined();
135 }
136
137
138 void InsetCharStyle::metrics(MetricsInfo & mi, Dimension & dim) const
139 {
140         LyXFont tmpfont = mi.base.font;
141         getDrawFont(mi.base.font);
142         mi.base.font.reduce(LyXFont(LyXFont::ALL_SANE));
143         mi.base.font.realize(tmpfont);
144         mi.base.textwidth -= 2 * TEXT_TO_INSET_OFFSET;
145         InsetText::metrics(mi, dim);
146         mi.base.font = tmpfont;
147         if (has_label_) {
148                 // consider width of the inset label
149                 LyXFont font(params_.labelfont);
150                 font.realize(LyXFont(LyXFont::ALL_SANE));
151                 font.decSize();
152                 font.decSize();
153                 int w = 0;
154                 int a = 0;
155                 int d = 0;
156                 string s(params_.type);
157                 if (undefined())
158                         // FIXME UNICODE
159                         s = lyx::to_utf8(_("Undef: ")) + s;
160                 docstring ds(s.begin(), s.end());
161                 font_metrics::rectText(ds, font, w, a, d);
162                 dim.wid = max(dim.wid, w);
163         }
164         dim.asc += TEXT_TO_INSET_OFFSET;
165         dim.des += TEXT_TO_INSET_OFFSET;
166         dim.wid += 2 * TEXT_TO_INSET_OFFSET;
167         mi.base.textwidth += 2 * TEXT_TO_INSET_OFFSET;
168         dim_ = dim;
169         if (has_label_)
170                 dim_.des += ascent();
171 }
172
173
174 void InsetCharStyle::draw(PainterInfo & pi, int x, int y) const
175 {
176         setPosCache(pi, x, y);
177
178         LyXFont tmpfont = pi.base.font;
179         getDrawFont(pi.base.font);
180         // I don't understand why the above .reduce and .realize aren't
181         //needed, or even wanted, here. It just works. -- MV 10.04.2005
182         InsetText::draw(pi, x, y);
183         pi.base.font = tmpfont;
184
185         int desc = InsetText::descent();
186         if (has_label_)
187                 desc -= ascent();
188
189         pi.pain.line(x, y + desc - 4, x, y + desc, params_.labelfont.color());
190         pi.pain.line(x, y + desc, x + dim_.wid - 3, y + desc,
191                 params_.labelfont.color());
192         pi.pain.line(x + dim_.wid - 3, y + desc, x + dim_.wid - 3, y + desc - 4,
193                 params_.labelfont.color());
194
195         // the name of the charstyle. Can be toggled.
196         if (has_label_) {
197                 LyXFont font(params_.labelfont);
198                 font.realize(LyXFont(LyXFont::ALL_SANE));
199                 font.decSize();
200                 font.decSize();
201                 int w = 0;
202                 int a = 0;
203                 int d = 0;
204                 string s(params_.type);
205                 if (undefined())
206                         // FIXME UNICODE
207                         s = lyx::to_utf8(_("Undef: ")) + s;
208                 docstring ds(s.begin(), s.end());
209                 font_metrics::rectText(ds, font, w, a, d);
210                 pi.pain.rectText(x + (dim_.wid - w) / 2, y + desc + a,
211                         ds, font, LColor::none, LColor::none);
212         }
213
214         // a visual clue when the cursor is inside the inset
215         LCursor & cur = pi.base.bv->cursor();
216         if (cur.isInside(this)) {
217                 y -= ascent();
218                 pi.pain.line(x, y + 4, x, y, params_.labelfont.color());
219                 pi.pain.line(x + 4, y, x, y, params_.labelfont.color());
220                 pi.pain.line(x + dim_.wid - 3, y + 4, x + dim_.wid - 3, y,
221                         params_.labelfont.color());
222                 pi.pain.line(x + dim_.wid - 7, y, x + dim_.wid - 3, y,
223                         params_.labelfont.color());
224         }
225 }
226
227
228 void InsetCharStyle::getDrawFont(LyXFont & font) const
229 {
230         font = params_.font;
231 }
232
233
234 void InsetCharStyle::doDispatch(LCursor & cur, FuncRequest & cmd)
235 {
236         setInlined();
237         switch (cmd.action) {
238                 case LFUN_MOUSE_PRESS:
239                         if (cmd.button() == mouse_button::button3)
240                                 has_label_ = !has_label_;
241                         else
242                                 InsetText::doDispatch(cur, cmd);
243                         break;
244         case LFUN_PASTE:
245         case LFUN_CLIPBOARD_PASTE:
246         case LFUN_PRIMARY_SELECTION_PASTE: {
247                 InsetCollapsable::doDispatch(cur, cmd);
248                 forceParagraphsToDefault(cur);
249                 break;
250                 }
251                 default:
252                         InsetCollapsable::doDispatch(cur, cmd);
253                         break;
254         }
255 }
256
257
258 bool InsetCharStyle::getStatus(LCursor & cur, FuncRequest const & cmd,
259         FuncStatus & status) const
260 {
261         switch (cmd.action) {
262                 // paragraph breaks not allowed in charstyle insets
263                 case LFUN_BREAK_PARAGRAPH:
264                 case LFUN_BREAK_PARAGRAPH_KEEP_LAYOUT:
265                 case LFUN_BREAK_PARAGRAPH_SKIP:
266                         status.enabled(false);
267                         return true;
268
269                 default:
270                         return InsetCollapsable::getStatus(cur, cmd, status);
271                 }
272 }
273
274
275 int InsetCharStyle::latex(Buffer const & buf, ostream & os,
276                      OutputParams const & runparams) const
277 {
278         if (!undefined()) {
279                 os << "\\" << params_.latexname;
280                 if (!params_.latexparam.empty())
281                         os << params_.latexparam;
282                 os << "{";
283         }
284         int i = InsetText::latex(buf, os, runparams);
285         if (!undefined())
286                 os << "}";
287         return i;
288 }
289
290
291 int InsetCharStyle::docbook(Buffer const & buf, ostream & os,
292                             OutputParams const & runparams) const
293 {
294         ParagraphList::const_iterator beg = paragraphs().begin();
295         ParagraphList::const_iterator par = paragraphs().begin();
296         ParagraphList::const_iterator end = paragraphs().end();
297
298         if (!undefined())
299                 sgml::openTag(os, params_.latexname,
300                         par->getID(buf, runparams) + params_.latexparam);
301
302         for (; par != end; ++par) {
303                 par->simpleDocBookOnePar(buf, os, runparams,
304                                          outerFont(std::distance(beg, par),
305                                                    paragraphs()));
306         }
307
308         if (!undefined())
309                 sgml::closeTag(os, params_.latexname);
310
311         return 0;
312 }
313
314
315 int InsetCharStyle::plaintext(Buffer const & buf, ostream & os,
316                               OutputParams const & runparams) const
317 {
318         return InsetText::plaintext(buf, os, runparams);
319 }
320
321
322 int InsetCharStyle::textString(Buffer const & buf, ostream & os,
323                        OutputParams const & op) const
324 {
325         return plaintext(buf, os, op);
326 }
327
328
329 void InsetCharStyle::validate(LaTeXFeatures & features) const
330 {
331         // Force inclusion of preamble snippet in layout file
332         features.require(params_.type);
333         InsetText::validate(features);
334 }
335
336
337 void InsetCharStyleParams::write(ostream & os) const
338 {
339         os << "CharStyle " << type << "\n";
340 }
341
342
343 void InsetCharStyleParams::read(LyXLex & lex)
344 {
345         if (lex.isOK()) {
346                 lex.next();
347                 string token = lex.getString();
348         }
349
350         if (lex.isOK()) {
351                 lex.next();
352                 type = lex.getString();
353         }
354 }