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