]> git.lyx.org Git - lyx.git/blob - src/insets/insetcharstyle.C
make sure the 'setStatus(Collapsed)' crash won't occur otherwise
[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 "BufferView.h"
18 #include "dispatchresult.h"
19 #include "funcrequest.h"
20 #include "FuncStatus.h"
21 #include "cursor.h"
22 #include "gettext.h"
23 #include "LaTeXFeatures.h"
24 #include "LColor.h"
25 #include "lyxlex.h"
26 #include "lyxtext.h"
27 #include "metricsinfo.h"
28 #include "paragraph.h"
29 #include "paragraph_funcs.h"
30 #include "sgml.h"
31
32 #include "frontends/font_metrics.h"
33 #include "frontends/Painter.h"
34
35 #include <sstream>
36
37
38 using std::string;
39 using std::auto_ptr;
40 using std::istringstream;
41 using std::ostream;
42 using std::ostringstream;
43
44
45 void InsetCharStyle::init()
46 {
47         setInsetName("CharStyle");
48         setInlined();
49         setDrawFrame(false);
50         has_label_ = true;
51 }
52
53
54 InsetCharStyle::InsetCharStyle(BufferParams const & bp, string const s)
55         : InsetCollapsable(bp)
56 {
57         params_.type = s;
58         setUndefined();
59         init();
60 }
61
62
63 InsetCharStyle::InsetCharStyle(BufferParams const & bp,
64                                 CharStyles::iterator cs)
65         : InsetCollapsable(bp)
66 {
67         params_.type = cs->name;
68         setDefined(cs);
69         init();
70 }
71
72
73 InsetCharStyle::InsetCharStyle(InsetCharStyle const & in)
74         : InsetCollapsable(in), params_(in.params_)
75 {
76         init();
77 }
78
79
80 auto_ptr<InsetBase> InsetCharStyle::doClone() const
81 {
82         return auto_ptr<InsetBase>(new InsetCharStyle(*this));
83 }
84
85
86 bool InsetCharStyle::undefined() const
87 {
88         return params_.latexname.empty();
89 }
90
91
92 void InsetCharStyle::setUndefined()
93 {
94         params_.latextype.clear();
95         params_.latexname.clear();
96         params_.latexparam.clear();
97         params_.font = LyXFont(LyXFont::ALL_INHERIT);
98         params_.labelfont = LyXFont(LyXFont::ALL_INHERIT);
99         params_.labelfont.setColor(LColor::red);
100 }
101
102
103 void InsetCharStyle::setDefined(CharStyles::iterator cs)
104 {
105         params_.latextype = cs->latextype;
106         params_.latexname = cs->latexname;
107         params_.latexparam = cs->latexparam;
108         params_.font = cs->font;
109         params_.labelfont = cs->labelfont;
110 }
111
112
113 string const InsetCharStyle::editMessage() const
114 {
115         return _("Opened CharStyle Inset");
116 }
117
118
119 void InsetCharStyle::write(Buffer const & buf, ostream & os) const
120 {
121         params_.write(os);
122         InsetCollapsable::write(buf, os);
123 }
124
125
126 void InsetCharStyle::read(Buffer const & buf, LyXLex & lex)
127 {
128         InsetCollapsable::read(buf, lex);
129         setInlined();
130 }
131
132
133 void InsetCharStyle::metrics(MetricsInfo & mi, Dimension & dim) const
134 {
135         LyXFont tmpfont = mi.base.font;
136         getDrawFont(mi.base.font);
137         mi.base.font.reduce(LyXFont(LyXFont::ALL_SANE));
138         mi.base.font.realize(tmpfont);
139         mi.base.textwidth -= 2 * TEXT_TO_INSET_OFFSET;
140         InsetText::metrics(mi, dim);
141         mi.base.font = tmpfont;
142         dim.asc += TEXT_TO_INSET_OFFSET;
143         dim.des += TEXT_TO_INSET_OFFSET;
144         dim.wid += 2 * TEXT_TO_INSET_OFFSET;
145         mi.base.textwidth += 2 * TEXT_TO_INSET_OFFSET;
146         dim_ = dim;
147         if (has_label_)
148                 dim_.des += ascent();
149 }
150
151
152 void InsetCharStyle::draw(PainterInfo & pi, int x, int y) const
153 {
154         setPosCache(pi, x, y);
155
156         LyXFont tmpfont = pi.base.font;
157         getDrawFont(pi.base.font);
158         // I don't understand why the above .reduce and .realize aren't
159         //needed, or even wanted, here. It just works. -- MV 10.04.2005
160         InsetText::draw(pi, x, y);
161         pi.base.font = tmpfont;
162
163         int desc = InsetText::descent();
164         if (has_label_)
165                 desc -= ascent();
166
167         pi.pain.line(x, y + desc - 4, x, y + desc, params_.labelfont.color());
168         pi.pain.line(x, y + desc, x + dim_.wid - 3, y + desc,
169                 params_.labelfont.color());
170         pi.pain.line(x + dim_.wid - 3, y + desc, x + dim_.wid - 3, y + desc - 4,
171                 params_.labelfont.color());
172
173         // the name of the charstyle. Can be toggled.
174         if (has_label_) {
175                 LyXFont font(params_.labelfont);
176                 font.realize(LyXFont(LyXFont::ALL_SANE));
177                 font.decSize();
178                 font.decSize();
179                 int w = 0;
180                 int a = 0;
181                 int d = 0;
182                 string s(params_.type);
183                 if (undefined())
184                         s = _("Undef: ") + s;
185                 font_metrics::rectText(params_.type, font, w, a, d);
186                 pi.pain.rectText(x + (dim_.wid - w) / 2, y + desc + a,
187                         s, font, LColor::none, LColor::none);
188         }
189
190         // a visual clue when the cursor is inside the inset
191         LCursor & cur = pi.base.bv->cursor();
192         if (cur.isInside(this)) {
193                 y -= ascent();
194                 pi.pain.line(x, y + 4, x, y, params_.labelfont.color());
195                 pi.pain.line(x + 4, y, x, y, params_.labelfont.color());
196                 pi.pain.line(x + dim_.wid - 3, y + 4, x + dim_.wid - 3, y,
197                         params_.labelfont.color());
198                 pi.pain.line(x + dim_.wid - 7, y, x + dim_.wid - 3, y,
199                         params_.labelfont.color());
200         }
201 }
202
203
204 void InsetCharStyle::getDrawFont(LyXFont & font) const
205 {
206         font = params_.font;
207 }
208
209
210 void InsetCharStyle::doDispatch(LCursor & cur, FuncRequest & cmd)
211 {
212         setInlined();
213         switch (cmd.action) {
214                 case LFUN_MOUSE_PRESS:
215                         if (cmd.button() == mouse_button::button3)
216                                 has_label_ = !has_label_;
217                         else
218                                 InsetText::doDispatch(cur, cmd);
219                         break;
220
221                 default:
222                         InsetCollapsable::doDispatch(cur, cmd);
223                         break;
224         }
225 }
226
227
228 bool InsetCharStyle::getStatus(LCursor & cur, FuncRequest const & cmd,
229         FuncStatus & status) const
230 {
231         switch (cmd.action) {
232                 // paragraph breaks not allowed in charstyle insets
233                 case LFUN_BREAKPARAGRAPH:
234                 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
235                 case LFUN_BREAKPARAGRAPH_SKIP:
236                         status.enabled(false);
237                         return true;
238
239                 default:
240                         return InsetCollapsable::getStatus(cur, cmd, status);
241                 }
242 }
243
244
245 int InsetCharStyle::latex(Buffer const & buf, ostream & os,
246                      OutputParams const & runparams) const
247 {
248         if (!undefined()) {
249                 os << "\\" << params_.latexname;
250                 if (!params_.latexparam.empty())
251                         os << params_.latexparam;
252                 os << "{";
253         }
254         int i = InsetText::latex(buf, os, runparams);
255         if (!undefined())
256                 os << "}";
257         return i;
258 }
259
260
261 int InsetCharStyle::linuxdoc(Buffer const & buf, ostream & os,
262                              OutputParams const & runparams) const
263 {
264         if (!undefined())
265                 sgml::openTag(os, params_.latexname, params_.latexparam);
266         int i = InsetText::linuxdoc(buf, os, runparams);
267         if (!undefined())
268                 sgml::closeTag(os, params_.latexname);
269         return i;
270 }
271
272
273 int InsetCharStyle::docbook(Buffer const & buf, ostream & os,
274                             OutputParams const & runparams) const
275 {
276         ParagraphList::const_iterator par = paragraphs().begin();
277         ParagraphList::const_iterator end = paragraphs().end();
278
279         if (!undefined())
280                 sgml::openTag(os, params_.latexname, 
281                         par->getID(buf, runparams) + params_.latexparam);
282
283         for (; par != end; ++par) {
284                 par->simpleDocBookOnePar(buf, os, runparams,
285                                  outerFont(par - paragraphs().begin(),
286                                                    paragraphs()));
287         }
288
289         if (!undefined())
290                 sgml::closeTag(os, params_.latexname);
291
292         return 0;
293 }
294
295
296 int InsetCharStyle::plaintext(Buffer const & buf, ostream & os,
297                               OutputParams const & runparams) const
298 {
299         return InsetText::plaintext(buf, os, runparams);
300 }
301
302
303 void InsetCharStyle::validate(LaTeXFeatures & features) const
304 {
305         // Force inclusion of preamble snippet in layout file
306         features.require(params_.type);
307         InsetText::validate(features);
308 }
309
310
311 void InsetCharStyleParams::write(ostream & os) const
312 {
313         os << "CharStyle " << type << "\n";
314 }
315
316
317 void InsetCharStyleParams::read(LyXLex & lex)
318 {
319         if (lex.isOK()) {
320                 lex.next();
321                 string token = lex.getString();
322         }
323
324         if (lex.isOK()) {
325                 lex.next();
326                 type = lex.getString();
327         }
328 }