]> git.lyx.org Git - lyx.git/blob - src/insets/insetcharstyle.C
Whitespace, only whitespace.
[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         setStatus(Inlined);
49         setDrawFrame(false);
50         has_label_ = true;
51 }
52
53
54 InsetCharStyle::InsetCharStyle(BufferParams const & bp,
55                                 CharStyles::iterator cs)
56         : InsetCollapsable(bp)
57 {
58         params_.type = cs->name;
59         params_.latextype = cs->latextype;
60         params_.latexname = cs->latexname;
61         params_.latexparam = cs->latexparam;
62         params_.font = cs->font;
63         params_.labelfont = cs->labelfont;
64         init();
65 }
66
67
68 InsetCharStyle::InsetCharStyle(InsetCharStyle const & in)
69         : InsetCollapsable(in), params_(in.params_)
70 {
71         init();
72 }
73
74
75 auto_ptr<InsetBase> InsetCharStyle::doClone() const
76 {
77         return auto_ptr<InsetBase>(new InsetCharStyle(*this));
78 }
79
80
81 string const InsetCharStyle::editMessage() const
82 {
83         return _("Opened CharStyle Inset");
84 }
85
86
87 void InsetCharStyle::write(Buffer const & buf, ostream & os) const
88 {
89         params_.write(os);
90         InsetCollapsable::write(buf, os);
91 }
92
93
94 void InsetCharStyle::read(Buffer const & buf, LyXLex & lex)
95 {
96         InsetCollapsable::read(buf, lex);
97         setStatus(Inlined);
98 }
99
100
101 void InsetCharStyle::metrics(MetricsInfo & mi, Dimension & dim) const
102 {
103         LyXFont tmpfont = mi.base.font;
104         getDrawFont(mi.base.font);
105         mi.base.font.reduce(LyXFont(LyXFont::ALL_SANE));
106         mi.base.font.realize(tmpfont);
107         mi.base.textwidth -= 2 * TEXT_TO_INSET_OFFSET;
108         InsetText::metrics(mi, dim);
109         mi.base.font = tmpfont;
110         dim.asc += TEXT_TO_INSET_OFFSET;
111         dim.des += TEXT_TO_INSET_OFFSET;
112         dim.wid += 2 * TEXT_TO_INSET_OFFSET;
113         mi.base.textwidth += 2 * TEXT_TO_INSET_OFFSET;
114         dim_ = dim;
115         if (has_label_)
116                 dim_.des += ascent();
117 }
118
119
120 void InsetCharStyle::draw(PainterInfo & pi, int x, int y) const
121 {
122         setPosCache(pi, x, y);
123
124         LyXFont tmpfont = pi.base.font;
125         getDrawFont(pi.base.font);
126         // I don't understand why the above .reduce and .realize aren't
127         //needed, or even wanted, here. It just works. -- MV 10.04.2005
128         InsetText::draw(pi, x, y);
129         pi.base.font = tmpfont;
130
131         int desc = InsetText::descent();
132         if (has_label_)
133                 desc -= ascent();
134
135         pi.pain.line(x, y + desc - 4, x, y + desc, params_.labelfont.color());
136         pi.pain.line(x, y + desc, x + dim_.wid - 3, y + desc,
137                 params_.labelfont.color());
138         pi.pain.line(x + dim_.wid - 3, y + desc, x + dim_.wid - 3, y + desc - 4,
139                 params_.labelfont.color());
140
141         // the name of the charstyle. Can be toggled.
142         if (has_label_) {
143                 LyXFont font(params_.labelfont);
144                 font.realize(LyXFont(LyXFont::ALL_SANE));
145                 font.decSize();
146                 font.decSize();
147                 int w = 0;
148                 int a = 0;
149                 int d = 0;
150                 font_metrics::rectText(params_.type, font, w, a, d);
151                 pi.pain.rectText(x + (dim_.wid - w) / 2, y + desc + a,
152                         params_.type, font, LColor::none, LColor::none);
153         }
154
155         // a visual clue when the cursor is inside the inset
156         LCursor & cur = pi.base.bv->cursor();
157         if (cur.isInside(this)) {
158                 y -= ascent();
159                 pi.pain.line(x, y + 4, x, y, params_.labelfont.color());
160                 pi.pain.line(x + 4, y, x, y, params_.labelfont.color());
161                 pi.pain.line(x + dim_.wid - 3, y + 4, x + dim_.wid - 3, y,
162                         params_.labelfont.color());
163                 pi.pain.line(x + dim_.wid - 7, y, x + dim_.wid - 3, y,
164                         params_.labelfont.color());
165         }
166 }
167
168
169 void InsetCharStyle::getDrawFont(LyXFont & font) const
170 {
171         font = params_.font;
172 }
173
174
175 void InsetCharStyle::doDispatch(LCursor & cur, FuncRequest & cmd)
176 {
177         setStatus(Inlined);
178         switch (cmd.action) {
179                 case LFUN_MOUSE_PRESS:
180                         if (cmd.button() == mouse_button::button3)
181                                 has_label_ = !has_label_;
182                         else
183                                 InsetText::doDispatch(cur, cmd);
184                         break;
185
186                 default:
187                         InsetCollapsable::doDispatch(cur, cmd);
188                         break;
189         }
190 }
191
192
193 bool InsetCharStyle::getStatus(LCursor & cur, FuncRequest const & cmd,
194         FuncStatus & status) const
195 {
196         switch (cmd.action) {
197                 // paragraph breaks not allowed in charstyle insets
198                 case LFUN_BREAKPARAGRAPH:
199                 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
200                 case LFUN_BREAKPARAGRAPH_SKIP:
201                         status.enabled(false);
202                         return true;
203
204                 default:
205                         return InsetCollapsable::getStatus(cur, cmd, status);
206                 }
207 }
208
209
210 int InsetCharStyle::latex(Buffer const & buf, ostream & os,
211                      OutputParams const & runparams) const
212 {
213         os << "\\" << params_.latexname;
214         if (!params_.latexparam.empty())
215                 os << params_.latexparam;
216         os << "{";
217         int i = InsetText::latex(buf, os, runparams);
218         os << "}";
219         return i;
220 }
221
222
223 int InsetCharStyle::linuxdoc(Buffer const & buf, ostream & os,
224                              OutputParams const & runparams) const
225 {
226         sgml::openTag(os, params_.latexname, params_.latexparam);
227         int i = InsetText::linuxdoc(buf, os, runparams);
228         sgml::closeTag(os, params_.latexname);
229         return i;
230 }
231
232
233 int InsetCharStyle::docbook(Buffer const & buf, ostream & os,
234                             OutputParams const & runparams) const
235 {
236         ParagraphList::const_iterator par = paragraphs().begin();
237         ParagraphList::const_iterator end = paragraphs().end();
238
239         sgml::openTag(os, params_.latexname, par->getID(buf, runparams) + params_.latexparam);
240
241         for (; par != end; ++par) {
242                 par->simpleDocBookOnePar(buf, os, runparams,
243                                          outerFont(par - paragraphs().begin(),
244                                                    paragraphs()));
245         }
246
247         sgml::closeTag(os, params_.latexname);
248         return 0;
249 }
250
251
252 int InsetCharStyle::plaintext(Buffer const & buf, ostream & os,
253                               OutputParams const & runparams) const
254 {
255         return InsetText::plaintext(buf, os, runparams);
256 }
257
258
259 void InsetCharStyle::validate(LaTeXFeatures & features) const
260 {
261         features.require(params_.type);
262         InsetText::validate(features);
263 }
264
265
266 void InsetCharStyleParams::write(ostream & os) const
267 {
268         os << "CharStyle " << type << "\n";
269 }
270
271
272 void InsetCharStyleParams::read(LyXLex & lex)
273 {
274         if (lex.isOK()) {
275                 lex.next();
276                 string token = lex.getString();
277         }
278
279         if (lex.isOK()) {
280                 lex.next();
281                 type = lex.getString();
282         }
283 }