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