]> git.lyx.org Git - lyx.git/blob - src/insets/insetcharstyle.C
Change tracking:
[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 using lyx::docstring;
42
43 using std::string;
44 using std::auto_ptr;
45 using std::istringstream;
46 using std::max;
47 using std::ostream;
48 using std::ostringstream;
49
50
51 void InsetCharStyle::init()
52 {
53         setInsetName("CharStyle");
54         setInlined();
55         setDrawFrame(false);
56 }
57
58
59 InsetCharStyle::InsetCharStyle(BufferParams const & bp, string const s)
60         : InsetCollapsable(bp)
61 {
62         params_.type = s;
63         setUndefined();
64         init();
65 }
66
67
68 InsetCharStyle::InsetCharStyle(BufferParams const & bp,
69                                 CharStyles::iterator cs)
70         : InsetCollapsable(bp)
71 {
72         params_.type = cs->name;
73         setDefined(cs);
74         init();
75 }
76
77
78 InsetCharStyle::InsetCharStyle(InsetCharStyle const & in)
79         : InsetCollapsable(in), params_(in.params_)
80 {
81         init();
82 }
83
84
85 auto_ptr<InsetBase> InsetCharStyle::doClone() const
86 {
87         return auto_ptr<InsetBase>(new InsetCharStyle(*this));
88 }
89
90
91 bool InsetCharStyle::undefined() const
92 {
93         return params_.latexname.empty();
94 }
95
96
97 void InsetCharStyle::setUndefined()
98 {
99         params_.latextype.clear();
100         params_.latexname.clear();
101         params_.latexparam.clear();
102         params_.font = LyXFont(LyXFont::ALL_INHERIT);
103         params_.labelfont = LyXFont(LyXFont::ALL_INHERIT);
104         params_.labelfont.setColor(LColor::error);
105         params_.show_label = true;
106 }
107
108
109 void InsetCharStyle::setDefined(CharStyles::iterator cs)
110 {
111         params_.latextype = cs->latextype;
112         params_.latexname = cs->latexname;
113         params_.latexparam = cs->latexparam;
114         params_.font = cs->font;
115         params_.labelfont = cs->labelfont;
116         params_.show_label = true;
117 }
118
119
120 docstring const InsetCharStyle::editMessage() const
121 {
122         return _("Opened CharStyle Inset");
123 }
124
125
126 void InsetCharStyle::write(Buffer const & buf, ostream & os) const
127 {
128         params_.write(os);
129         InsetCollapsable::write(buf, os);
130 }
131
132
133 void InsetCharStyle::read(Buffer const & buf, LyXLex & lex)
134 {
135         params_.read(lex);
136         InsetCollapsable::read(buf, lex);
137         setInlined();
138 }
139
140
141 void InsetCharStyle::metrics(MetricsInfo & mi, Dimension & dim) const
142 {
143         LyXFont tmpfont = mi.base.font;
144         getDrawFont(mi.base.font);
145         mi.base.font.reduce(LyXFont(LyXFont::ALL_SANE));
146         mi.base.font.realize(tmpfont);
147         mi.base.textwidth -= 2 * TEXT_TO_INSET_OFFSET;
148         InsetText::metrics(mi, dim);
149         mi.base.font = tmpfont;
150         if (params_.show_label) {
151                 // consider width of the inset label
152                 LyXFont font(params_.labelfont);
153                 font.realize(LyXFont(LyXFont::ALL_SANE));
154                 font.decSize();
155                 font.decSize();
156                 int w = 0;
157                 int a = 0;
158                 int d = 0;
159                 string s(params_.type);
160                 if (undefined())
161                         // FIXME UNICODE
162                         s = lyx::to_utf8(_("Undef: ")) + s;
163                 docstring ds(s.begin(), s.end());
164                 theFontMetrics(font).rectText(ds, 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                 string s(params_.type);
208                 if (undefined())
209                         // FIXME UNICODE
210                         s = lyx::to_utf8(_("Undef: ")) + s;
211                 docstring ds(s.begin(), s.end());
212                 theFontMetrics(font).rectText(ds, w, a, d);
213                 pi.pain.rectText(x + (dim_.wid - w) / 2, y + desc + a,
214                         ds, font, LColor::none, LColor::none);
215         }
216
217         // a visual clue when the cursor is inside the inset
218         LCursor & cur = pi.base.bv->cursor();
219         if (cur.isInside(this)) {
220                 y -= ascent();
221                 pi.pain.line(x, y + 4, x, y, params_.labelfont.color());
222                 pi.pain.line(x + 4, y, x, y, params_.labelfont.color());
223                 pi.pain.line(x + dim_.wid - 3, y + 4, x + dim_.wid - 3, y,
224                         params_.labelfont.color());
225                 pi.pain.line(x + dim_.wid - 7, y, x + dim_.wid - 3, y,
226                         params_.labelfont.color());
227         }
228 }
229
230
231 void InsetCharStyle::getDrawFont(LyXFont & font) const
232 {
233         font = params_.font;
234 }
235
236
237 void InsetCharStyle::doDispatch(LCursor & cur, FuncRequest & cmd)
238 {
239         setInlined();
240         switch (cmd.action) {
241         
242         case LFUN_MOUSE_PRESS:
243                         if (cmd.button() == mouse_button::button3)
244                                 params_.show_label = !params_.show_label;
245                         else
246                                 InsetText::doDispatch(cur, cmd);
247                         break;
248
249         case LFUN_INSET_TOGGLE:
250                 if (cmd.argument() == "open")
251                         params_.show_label = true;
252                 else if (cmd.argument() == "close")
253                         params_.show_label = false;
254                 else if (cmd.argument() == "toggle" || cmd.argument().empty())
255                         params_.show_label = !params_.show_label;
256                 else // if assign or anything else
257                         cur.undispatched();
258                 cur.dispatched();
259                 break;
260
261         default:
262                 InsetCollapsable::doDispatch(cur, cmd);
263                 break;
264         }
265 }
266
267
268 bool InsetCharStyle::getStatus(LCursor & cur, FuncRequest const & cmd,
269         FuncStatus & status) const
270 {
271         switch (cmd.action) {
272                 // paragraph breaks not allowed in charstyle insets
273                 case LFUN_BREAK_PARAGRAPH:
274                 case LFUN_BREAK_PARAGRAPH_KEEP_LAYOUT:
275                 case LFUN_BREAK_PARAGRAPH_SKIP:
276                         status.enabled(false);
277                         return true;
278
279                 default:
280                         return InsetCollapsable::getStatus(cur, cmd, status);
281                 }
282 }
283
284
285 int InsetCharStyle::latex(Buffer const & buf, ostream & os,
286                      OutputParams const & runparams) const
287 {
288         if (!undefined()) {
289                 os << "\\" << params_.latexname;
290                 if (!params_.latexparam.empty())
291                         os << 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, ostream & 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                 sgml::openTag(os, params_.latexname,
310                         par->getID(buf, runparams) + params_.latexparam);
311
312         for (; par != end; ++par) {
313                 par->simpleDocBookOnePar(buf, os, runparams,
314                                          outerFont(std::distance(beg, par),
315                                                    paragraphs()));
316         }
317
318         if (!undefined())
319                 sgml::closeTag(os, params_.latexname);
320
321         return 0;
322 }
323
324
325 int InsetCharStyle::plaintext(Buffer const & buf, lyx::odocstream & os,
326                               OutputParams const & runparams) const
327 {
328         return InsetText::plaintext(buf, os, runparams);
329 }
330
331
332 int InsetCharStyle::textString(Buffer const & buf, lyx::odocstream & os,
333                        OutputParams const & op) const
334 {
335         return plaintext(buf, os, op);
336 }
337
338
339 void InsetCharStyle::validate(LaTeXFeatures & features) const
340 {
341         // Force inclusion of preamble snippet in layout file
342         features.require(params_.type);
343         InsetText::validate(features);
344 }
345
346
347 void InsetCharStyleParams::write(ostream & os) const
348 {
349         os << "CharStyle " << type << "\n";
350         os << "show_label " << convert<string>(show_label) << "\n";
351 }
352
353
354 void InsetCharStyleParams::read(LyXLex & lex)
355 {
356         while (lex.isOK()) {
357                 lex.next();
358                 string token = lex.getString();
359
360                 if (token == "CharStyle") {
361                         lex.next();
362                         type = lex.getString();
363                 }
364
365                 else if (token == "show_label") {
366                         lex.next();
367                         show_label = lex.getBool();
368                 }
369
370                 else if (token == "status") {
371                         lex.pushToken(token);
372                         break;
373                 }
374         }
375 }