]> git.lyx.org Git - lyx.git/blob - src/insets/insetcharstyle.C
Change editMessage to return a docstring, change functions to not use 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 docstring const InsetCharStyle::editMessage() const
118 {
119         return _("Opened CharStyle Inset");
120 }
121
122
123 void InsetCharStyle::write(Buffer const & buf, ostream & os) const
124 {
125         params_.write(os);
126         InsetCollapsable::write(buf, os);
127 }
128
129
130 void InsetCharStyle::read(Buffer const & buf, LyXLex & lex)
131 {
132         InsetCollapsable::read(buf, lex);
133         setInlined();
134 }
135
136
137 void InsetCharStyle::metrics(MetricsInfo & mi, Dimension & dim) const
138 {
139         LyXFont tmpfont = mi.base.font;
140         getDrawFont(mi.base.font);
141         mi.base.font.reduce(LyXFont(LyXFont::ALL_SANE));
142         mi.base.font.realize(tmpfont);
143         mi.base.textwidth -= 2 * TEXT_TO_INSET_OFFSET;
144         InsetText::metrics(mi, dim);
145         mi.base.font = tmpfont;
146         if (has_label_) {
147                 // consider width of the inset label
148                 LyXFont font(params_.labelfont);
149                 font.realize(LyXFont(LyXFont::ALL_SANE));
150                 font.decSize();
151                 font.decSize();
152                 int w = 0;
153                 int a = 0;
154                 int d = 0;
155                 string s(params_.type);
156                 if (undefined())
157                         // FIXME UNICODE
158                         s = lyx::to_utf8(_("Undef: ")) + s;
159                 docstring ds(s.begin(), s.end());
160                 font_metrics::rectText(ds, font, w, a, d);
161                 dim.wid = max(dim.wid, w);
162         }
163         dim.asc += TEXT_TO_INSET_OFFSET;
164         dim.des += TEXT_TO_INSET_OFFSET;
165         dim.wid += 2 * TEXT_TO_INSET_OFFSET;
166         mi.base.textwidth += 2 * TEXT_TO_INSET_OFFSET;
167         dim_ = dim;
168         if (has_label_)
169                 dim_.des += ascent();
170 }
171
172
173 void InsetCharStyle::draw(PainterInfo & pi, int x, int y) const
174 {
175         setPosCache(pi, x, y);
176
177         LyXFont tmpfont = pi.base.font;
178         getDrawFont(pi.base.font);
179         // I don't understand why the above .reduce and .realize aren't
180         //needed, or even wanted, here. It just works. -- MV 10.04.2005
181         InsetText::draw(pi, x, y);
182         pi.base.font = tmpfont;
183
184         int desc = InsetText::descent();
185         if (has_label_)
186                 desc -= ascent();
187
188         pi.pain.line(x, y + desc - 4, x, y + desc, params_.labelfont.color());
189         pi.pain.line(x, y + desc, x + dim_.wid - 3, y + desc,
190                 params_.labelfont.color());
191         pi.pain.line(x + dim_.wid - 3, y + desc, x + dim_.wid - 3, y + desc - 4,
192                 params_.labelfont.color());
193
194         // the name of the charstyle. Can be toggled.
195         if (has_label_) {
196                 LyXFont font(params_.labelfont);
197                 font.realize(LyXFont(LyXFont::ALL_SANE));
198                 font.decSize();
199                 font.decSize();
200                 int w = 0;
201                 int a = 0;
202                 int d = 0;
203                 string s(params_.type);
204                 if (undefined())
205                         // FIXME UNICODE
206                         s = lyx::to_utf8(_("Undef: ")) + s;
207                 docstring ds(s.begin(), s.end());
208                 font_metrics::rectText(ds, font, w, a, d);
209                 pi.pain.rectText(x + (dim_.wid - w) / 2, y + desc + a,
210                         ds, font, LColor::none, LColor::none);
211         }
212
213         // a visual clue when the cursor is inside the inset
214         LCursor & cur = pi.base.bv->cursor();
215         if (cur.isInside(this)) {
216                 y -= ascent();
217                 pi.pain.line(x, y + 4, x, y, params_.labelfont.color());
218                 pi.pain.line(x + 4, y, x, y, params_.labelfont.color());
219                 pi.pain.line(x + dim_.wid - 3, y + 4, x + dim_.wid - 3, y,
220                         params_.labelfont.color());
221                 pi.pain.line(x + dim_.wid - 7, y, x + dim_.wid - 3, y,
222                         params_.labelfont.color());
223         }
224 }
225
226
227 void InsetCharStyle::getDrawFont(LyXFont & font) const
228 {
229         font = params_.font;
230 }
231
232
233 void InsetCharStyle::doDispatch(LCursor & cur, FuncRequest & cmd)
234 {
235         setInlined();
236         switch (cmd.action) {
237                 case LFUN_MOUSE_PRESS:
238                         if (cmd.button() == mouse_button::button3)
239                                 has_label_ = !has_label_;
240                         else
241                                 InsetText::doDispatch(cur, cmd);
242                         break;
243         case LFUN_PASTE:
244         case LFUN_CLIPBOARD_PASTE:
245         case LFUN_PRIMARY_SELECTION_PASTE: {
246                 InsetCollapsable::doDispatch(cur, cmd);
247                 forceParagraphsToDefault(cur);
248                 break;
249                 }
250                 default:
251                         InsetCollapsable::doDispatch(cur, cmd);
252                         break;
253         }
254 }
255
256
257 bool InsetCharStyle::getStatus(LCursor & cur, FuncRequest const & cmd,
258         FuncStatus & status) const
259 {
260         switch (cmd.action) {
261                 // paragraph breaks not allowed in charstyle insets
262                 case LFUN_BREAK_PARAGRAPH:
263                 case LFUN_BREAK_PARAGRAPH_KEEP_LAYOUT:
264                 case LFUN_BREAK_PARAGRAPH_SKIP:
265                         status.enabled(false);
266                         return true;
267
268                 default:
269                         return InsetCollapsable::getStatus(cur, cmd, status);
270                 }
271 }
272
273
274 int InsetCharStyle::latex(Buffer const & buf, ostream & os,
275                      OutputParams const & runparams) const
276 {
277         if (!undefined()) {
278                 os << "\\" << params_.latexname;
279                 if (!params_.latexparam.empty())
280                         os << params_.latexparam;
281                 os << "{";
282         }
283         int i = InsetText::latex(buf, os, runparams);
284         if (!undefined())
285                 os << "}";
286         return i;
287 }
288
289
290 int InsetCharStyle::docbook(Buffer const & buf, ostream & os,
291                             OutputParams const & runparams) const
292 {
293         ParagraphList::const_iterator beg = paragraphs().begin();
294         ParagraphList::const_iterator par = paragraphs().begin();
295         ParagraphList::const_iterator end = paragraphs().end();
296
297         if (!undefined())
298                 sgml::openTag(os, params_.latexname,
299                         par->getID(buf, runparams) + params_.latexparam);
300
301         for (; par != end; ++par) {
302                 par->simpleDocBookOnePar(buf, os, runparams,
303                                          outerFont(std::distance(beg, par),
304                                                    paragraphs()));
305         }
306
307         if (!undefined())
308                 sgml::closeTag(os, params_.latexname);
309
310         return 0;
311 }
312
313
314 int InsetCharStyle::plaintext(Buffer const & buf, ostream & os,
315                               OutputParams const & runparams) const
316 {
317         return InsetText::plaintext(buf, os, runparams);
318 }
319
320
321 int InsetCharStyle::textString(Buffer const & buf, ostream & os,
322                        OutputParams const & op) const
323 {
324         return plaintext(buf, os, op);
325 }
326
327
328 void InsetCharStyle::validate(LaTeXFeatures & features) const
329 {
330         // Force inclusion of preamble snippet in layout file
331         features.require(params_.type);
332         InsetText::validate(features);
333 }
334
335
336 void InsetCharStyleParams::write(ostream & os) const
337 {
338         os << "CharStyle " << type << "\n";
339 }
340
341
342 void InsetCharStyleParams::read(LyXLex & lex)
343 {
344         if (lex.isOK()) {
345                 lex.next();
346                 string token = lex.getString();
347         }
348
349         if (lex.isOK()) {
350                 lex.next();
351                 type = lex.getString();
352         }
353 }