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