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