]> git.lyx.org Git - lyx.git/blob - src/insets/InsetCharStyle.cpp
Streamlining CollapseStatus stuff
[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                 theFontMetrics(font).rectText(s, w, a, d);
162                 dim.wid = max(dim.wid, w);
163         }
164         dim.asc += TEXT_TO_INSET_OFFSET;
165         dim.des += TEXT_TO_INSET_OFFSET;
166         dim.wid += 2 * TEXT_TO_INSET_OFFSET;
167         mi.base.textwidth += 2 * TEXT_TO_INSET_OFFSET;
168         if (params_.show_label)
169                 dim.des += ascent();
170         bool const changed = dim_ != dim;
171         dim_ = dim;
172         return changed;
173 }
174
175
176 void InsetCharStyle::draw(PainterInfo & pi, int x, int y) const
177 {
178         setPosCache(pi, x, y);
179
180         Font tmpfont = pi.base.font;
181         getDrawFont(pi.base.font);
182         // I don't understand why the above .reduce and .realize aren't
183         //needed, or even wanted, here. It just works. -- MV 10.04.2005
184         InsetText::draw(pi, x + TEXT_TO_INSET_OFFSET, y);
185         pi.base.font = tmpfont;
186
187         int desc = InsetText::descent();
188         if (params_.show_label)
189                 desc -= ascent();
190
191         pi.pain.line(x, y + desc - 4, x, y + desc, params_.labelfont.color());
192         pi.pain.line(x, y + desc, x + dim_.wid - 3, y + desc,
193                 params_.labelfont.color());
194         pi.pain.line(x + dim_.wid - 3, y + desc, x + dim_.wid - 3, y + desc - 4,
195                 params_.labelfont.color());
196
197         // the name of the charstyle. Can be toggled.
198         if (params_.show_label) {
199                 Font font(params_.labelfont);
200                 font.realize(Font(Font::ALL_SANE));
201                 font.decSize();
202                 font.decSize();
203                 int w = 0;
204                 int a = 0;
205                 int d = 0;
206                 // FIXME UNICODE
207                 docstring s(from_utf8(params_.type));
208                 if (undefined())
209                         s = _("Undef: ") + s;
210                 theFontMetrics(font).rectText(s, w, a, d);
211                 pi.pain.rectText(x + (dim_.wid - w) / 2, y + desc + a,
212                         s, font, Color::none, Color::none);
213         }
214
215         // a visual clue when the cursor is inside the inset
216         Cursor & cur = pi.base.bv->cursor();
217         if (cur.isInside(this)) {
218                 y -= ascent();
219                 pi.pain.line(x, y + 4, x, y, params_.labelfont.color());
220                 pi.pain.line(x + 4, y, x, y, params_.labelfont.color());
221                 pi.pain.line(x + dim_.wid - 3, y + 4, x + dim_.wid - 3, y,
222                         params_.labelfont.color());
223                 pi.pain.line(x + dim_.wid - 7, y, x + dim_.wid - 3, y,
224                         params_.labelfont.color());
225         }
226 }
227
228
229 void InsetCharStyle::getDrawFont(Font & font) const
230 {
231         font = params_.font;
232 }
233
234
235 void InsetCharStyle::doDispatch(Cursor & cur, FuncRequest & cmd)
236 {
237         switch (cmd.action) {
238
239         case LFUN_MOUSE_RELEASE:
240                         if (cmd.button() == mouse_button::button3)
241                                 params_.show_label = !params_.show_label;
242                         else
243                                 InsetCollapsable::doDispatch(cur, cmd);
244                         break;
245
246         case LFUN_INSET_TOGGLE:
247                 if (cmd.argument() == "open")
248                         params_.show_label = true;
249                 else if (cmd.argument() == "close")
250                         params_.show_label = false;
251                 else if (cmd.argument() == "toggle" || cmd.argument().empty())
252                         params_.show_label = !params_.show_label;
253                 else // if assign or anything else
254                         cur.undispatched();
255                 cur.dispatched();
256                 break;
257
258         default:
259                 InsetCollapsable::doDispatch(cur, cmd);
260                 break;
261         }
262 }
263
264
265 bool InsetCharStyle::getStatus(Cursor & cur, FuncRequest const & cmd,
266         FuncStatus & status) const
267 {
268         switch (cmd.action) {
269                 // paragraph breaks not allowed in charstyle insets
270                 case LFUN_BREAK_PARAGRAPH:
271                 case LFUN_BREAK_PARAGRAPH_KEEP_LAYOUT:
272                 case LFUN_BREAK_PARAGRAPH_SKIP:
273                         status.enabled(false);
274                         return true;
275
276                 default:
277                         return InsetCollapsable::getStatus(cur, cmd, status);
278                 }
279 }
280
281
282 int InsetCharStyle::latex(Buffer const & buf, odocstream & os,
283                           OutputParams const & runparams) const
284 {
285         if (!undefined()) {
286                 // FIXME UNICODE
287                 os << '\\' << from_utf8(params_.latexname);
288                 if (!params_.latexparam.empty())
289                         os << from_utf8(params_.latexparam);
290                 os << '{';
291         }
292         int i = InsetText::latex(buf, os, runparams);
293         if (!undefined())
294                 os << "}";
295         return i;
296 }
297
298
299 int InsetCharStyle::plaintext(Buffer const & buf, odocstream & os,
300                               OutputParams const & runparams) const
301 {
302         return InsetText::plaintext(buf, os, runparams);
303 }
304
305
306 int InsetCharStyle::docbook(Buffer const & buf, odocstream & os,
307                             OutputParams const & runparams) const
308 {
309         ParagraphList::const_iterator beg = paragraphs().begin();
310         ParagraphList::const_iterator par = paragraphs().begin();
311         ParagraphList::const_iterator end = paragraphs().end();
312
313         if (!undefined())
314                 // FIXME UNICODE
315                 sgml::openTag(os, params_.latexname,
316                               par->getID(buf, runparams) + params_.latexparam);
317
318         for (; par != end; ++par) {
319                 par->simpleDocBookOnePar(buf, os, runparams,
320                                          outerFont(std::distance(beg, par),
321                                                    paragraphs()));
322         }
323
324         if (!undefined())
325                 sgml::closeTag(os, params_.latexname);
326
327         return 0;
328 }
329
330
331 void InsetCharStyle::textString(Buffer const & buf, odocstream & os) const
332 {
333         os << paragraphs().begin()->asString(buf, true);
334 }
335
336
337 void InsetCharStyle::validate(LaTeXFeatures & features) const
338 {
339         // Force inclusion of preamble snippet in layout file
340         features.require(params_.type);
341         InsetText::validate(features);
342 }
343
344
345 void InsetCharStyleParams::write(ostream & os) const
346 {
347         os << "CharStyle " << type << "\n";
348         os << "show_label " << convert<string>(show_label) << "\n";
349 }
350
351
352 void InsetCharStyleParams::read(Lexer & lex)
353 {
354         while (lex.isOK()) {
355                 lex.next();
356                 string token = lex.getString();
357
358                 if (token == "CharStyle") {
359                         lex.next();
360                         type = lex.getString();
361                 }
362
363                 else if (token == "show_label") {
364                         lex.next();
365                         show_label = lex.getBool();
366                 }
367
368                 else if (token == "status") {
369                         lex.pushToken(token);
370                         break;
371                 }
372         }
373 }
374
375
376 } // namespace lyx