]> git.lyx.org Git - lyx.git/blob - src/insets/insetcharstyle.C
* insettabular.[Ch]: remove remains of the 'update' mechanism,
[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 "gettext.h"
21 #include "LaTeXFeatures.h"
22 #include "LColor.h"
23 #include "lyxlex.h"
24 #include "lyxtext.h"
25 #include "metricsinfo.h"
26 #include "paragraph.h"
27
28 #include "frontends/font_metrics.h"
29 #include "frontends/Painter.h"
30 #include "support/std_sstream.h"
31
32
33 using std::string;
34 using std::auto_ptr;
35 using std::istringstream;
36 using std::ostream;
37 using std::ostringstream;
38
39
40 void InsetCharStyle::init()
41 {
42         setInsetName("CharStyle");
43         setStatus(Inlined);
44 }
45
46
47 InsetCharStyle::InsetCharStyle(BufferParams const & bp,
48                                 CharStyles::iterator cs)
49         : InsetCollapsable(bp), has_label_(true)
50 {
51         params_.type = cs->name;
52         params_.latextype = cs->latextype;
53         params_.latexname = cs->latexname;
54         params_.latexparam = cs->latexparam;
55         params_.font = cs->font;
56         params_.labelfont = cs->labelfont;
57         init();
58 }
59
60
61 InsetCharStyle::InsetCharStyle(InsetCharStyle const & in)
62         : InsetCollapsable(in), params_(in.params_), has_label_(true)
63 {
64         init();
65 }
66
67
68 auto_ptr<InsetBase> InsetCharStyle::clone() const
69 {
70         return auto_ptr<InsetBase>(new InsetCharStyle(*this));
71 }
72
73
74 string const InsetCharStyle::editMessage() const
75 {
76         return _("Opened CharStyle Inset");
77 }
78
79
80 void InsetCharStyle::write(Buffer const & buf, ostream & os) const
81 {
82         params_.write(os);
83         InsetCollapsable::write(buf, os);
84 }
85
86
87 void InsetCharStyle::read(Buffer const & buf, LyXLex & lex)
88 {
89         InsetCollapsable::read(buf, lex);
90         setStatus(Inlined);
91 }
92
93
94 void InsetCharStyle::metrics(MetricsInfo & mi, Dimension & dim) const
95 {
96         LyXFont tmpfont = mi.base.font;
97         getDrawFont(mi.base.font);
98         InsetCollapsable::metrics(mi, dim);
99         mi.base.font = tmpfont;
100         dim_ = dim;
101         if (has_label_)
102                 dim_.des += ascent();
103 }
104
105
106 void InsetCharStyle::draw(PainterInfo & pi, int x, int y) const
107 {
108         setPosCache(pi, x, y);
109
110         // FIXME: setStatus(Inlined); this is not a const operation
111         LyXFont tmpfont = pi.base.font;
112         inset.setDrawFrame(InsetText::NEVER);
113         getDrawFont(pi.base.font);
114         inset.draw(pi, x, y);
115         pi.base.font = tmpfont;
116
117         pi.pain.line(x + 2, y + inset.descent() - 4, x + 2,
118                 y + inset.descent(), params_.labelfont.color());
119         pi.pain.line(x + 2, y + inset.descent(), x + dim_.wid - 2,
120                 y + inset.descent(), params_.labelfont.color());
121         pi.pain.line(x + dim_.wid - 2, y + inset.descent(), x + dim_.wid - 2,
122                 y + inset.descent() - 4, params_.labelfont.color());
123
124         if (has_label_) {
125                 if (!owner())
126                         x += scroll();
127
128         LyXFont font(params_.labelfont);
129         font.realize(LyXFont(LyXFont::ALL_SANE));
130         font.decSize();
131         font.decSize();
132         int w = 0;
133         int a = 0;
134         int d = 0;
135         font_metrics::rectText(params_.type, font, w, a, d);
136         pi.pain.rectText(x + (dim_.wid - w) / 2, 
137                 y + inset.descent() + a,
138                 params_.type, font, LColor::none, LColor::none);
139         }
140 }
141
142
143 void InsetCharStyle::getDrawFont(LyXFont & font) const
144 {
145         font = params_.font;
146 }
147
148
149 void InsetCharStyle::priv_dispatch(LCursor & cur, FuncRequest const & cmd)
150 {
151         setStatus(Inlined);
152         switch (cmd.action) {
153                 case LFUN_MOUSE_PRESS:
154                         if (cmd.button() == mouse_button::button3)
155                                 has_label_ = !has_label_;
156                         else
157                                 inset.dispatch(cur, cmd);
158                         break;
159
160                 default:
161                         InsetCollapsable::priv_dispatch(cur, cmd);
162                         break;
163         }
164 }
165
166
167 namespace {
168
169 int outputVerbatim(std::ostream & os, InsetText inset)
170 {
171         int lines = 0;
172         ParagraphList::iterator par = inset.paragraphs().begin();
173         ParagraphList::iterator end = inset.paragraphs().end();
174         while (par != end) {
175                 lyx::pos_type siz = par->size();
176                 for (lyx::pos_type i = 0; i < siz; ++i) {
177                         if (par->isNewline(i)) {
178                                 os << '\n';
179                                 ++lines;
180                         } else {
181                                 os << par->getChar(i);
182                         }
183                 }
184                 ++par;
185                 if (par != end) {
186                         os << "\n";
187                         lines ++;
188                 }
189         }
190         return lines;
191 }
192
193 } // namespace anon
194
195
196 int InsetCharStyle::latex(Buffer const &, ostream & os,
197                      OutputParams const &) const
198 {
199         os << "%\n\\" << params_.latexname;
200         if (!params_.latexparam.empty())
201                 os << params_.latexparam;
202         os << "{";
203         int i = outputVerbatim(os, inset);
204         os << "}%\n";
205                 i += 2;
206         return i;
207 }
208
209
210 int InsetCharStyle::linuxdoc(Buffer const &, std::ostream & os,
211                              OutputParams const &) const
212 {
213         os << "<" << params_.latexname;
214         if (!params_.latexparam.empty())
215                 os << " " << params_.latexparam;
216         os << ">";
217         int const i = outputVerbatim(os, inset);
218         os << "</" << params_.latexname << ">";
219         return i;
220 }
221
222
223 int InsetCharStyle::docbook(Buffer const &, std::ostream & os,
224                             OutputParams const &) const
225 {
226         os << "<" << params_.latexname;
227         if (!params_.latexparam.empty())
228                 os << " " << params_.latexparam;
229         os << ">";
230         int const i = outputVerbatim(os, inset);
231         os << "</" << params_.latexname << ">";
232         return i;
233 }
234
235
236 int InsetCharStyle::plaintext(Buffer const &, std::ostream & os,
237                               OutputParams const & /*runparams*/) const
238 {
239         return outputVerbatim(os, inset);
240 }
241
242
243 void InsetCharStyle::validate(LaTeXFeatures & features) const
244 {
245         features.require(params_.type);
246 }
247
248
249 void InsetCharStyleParams::write(ostream & os) const
250 {
251         os << "CharStyle " << type << "\n";
252 }
253
254
255 void InsetCharStyleParams::read(LyXLex & lex)
256 {
257         if (lex.isOK()) {
258                 lex.next();
259                 string token = lex.getString();
260         }
261
262         if (lex.isOK()) {
263                 lex.next();
264                 type = lex.getString();
265         }
266 }