]> git.lyx.org Git - lyx.git/blob - src/insets/insetcharstyle.C
beb2b2135783a6b40bef08838ede874235af1b15
[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_CLIPBOARD_PASTE:
240         case LFUN_PRIMARY_SELECTION_PASTE: {
241                 InsetCollapsable::doDispatch(cur, cmd);
242                 forceParagraphsToDefault(cur);
243                 break;
244                 }
245                 default:
246                         InsetCollapsable::doDispatch(cur, cmd);
247                         break;
248         }
249 }
250
251
252 bool InsetCharStyle::getStatus(LCursor & cur, FuncRequest const & cmd,
253         FuncStatus & status) const
254 {
255         switch (cmd.action) {
256                 // paragraph breaks not allowed in charstyle insets
257                 case LFUN_BREAK_PARAGRAPH:
258                 case LFUN_BREAK_PARAGRAPH_KEEP_LAYOUT:
259                 case LFUN_BREAK_PARAGRAPH_SKIP:
260                         status.enabled(false);
261                         return true;
262
263                 default:
264                         return InsetCollapsable::getStatus(cur, cmd, status);
265                 }
266 }
267
268
269 int InsetCharStyle::latex(Buffer const & buf, ostream & os,
270                      OutputParams const & runparams) const
271 {
272         if (!undefined()) {
273                 os << "\\" << params_.latexname;
274                 if (!params_.latexparam.empty())
275                         os << params_.latexparam;
276                 os << "{";
277         }
278         int i = InsetText::latex(buf, os, runparams);
279         if (!undefined())
280                 os << "}";
281         return i;
282 }
283
284
285 int InsetCharStyle::linuxdoc(Buffer const & buf, ostream & os,
286                              OutputParams const & runparams) const
287 {
288         if (!undefined())
289                 sgml::openTag(os, params_.latexname, params_.latexparam);
290         int i = InsetText::linuxdoc(buf, os, runparams);
291         if (!undefined())
292                 sgml::closeTag(os, params_.latexname);
293         return i;
294 }
295
296
297 int InsetCharStyle::docbook(Buffer const & buf, ostream & os,
298                             OutputParams const & runparams) const
299 {
300         ParagraphList::const_iterator beg = paragraphs().begin();
301         ParagraphList::const_iterator par = paragraphs().begin();
302         ParagraphList::const_iterator end = paragraphs().end();
303
304         if (!undefined())
305                 sgml::openTag(os, params_.latexname,
306                         par->getID(buf, runparams) + params_.latexparam);
307
308         for (; par != end; ++par) {
309                 par->simpleDocBookOnePar(buf, os, runparams,
310                                          outerFont(std::distance(beg, par),
311                                                    paragraphs()));
312         }
313
314         if (!undefined())
315                 sgml::closeTag(os, params_.latexname);
316
317         return 0;
318 }
319
320
321 int InsetCharStyle::plaintext(Buffer const & buf, ostream & os,
322                               OutputParams const & runparams) const
323 {
324         return InsetText::plaintext(buf, os, runparams);
325 }
326
327
328 int InsetCharStyle::textString(Buffer const & buf, ostream & os,
329                        OutputParams const & op) const
330 {
331         return plaintext(buf, os, op);
332 }
333
334
335 void InsetCharStyle::validate(LaTeXFeatures & features) const
336 {
337         // Force inclusion of preamble snippet in layout file
338         features.require(params_.type);
339         InsetText::validate(features);
340 }
341
342
343 void InsetCharStyleParams::write(ostream & os) const
344 {
345         os << "CharStyle " << type << "\n";
346 }
347
348
349 void InsetCharStyleParams::read(LyXLex & lex)
350 {
351         if (lex.isOK()) {
352                 lex.next();
353                 string token = lex.getString();
354         }
355
356         if (lex.isOK()) {
357                 lex.next();
358                 type = lex.getString();
359         }
360 }