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