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