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