]> git.lyx.org Git - lyx.git/blob - src/insets/insetcharstyle.C
Conglomerate-style CharStyles
[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         InsetCollapsable::metrics(mi, dim);
97         dim_ = dim;
98         if (has_label_)
99                 dim_.des += ascent();
100 }
101
102
103 void InsetCharStyle::draw(PainterInfo & pi, int x, int y) const
104 {
105         xo_ = x;
106         yo_ = y;
107
108         status_ = Inlined;
109         inset.setDrawFrame(InsetText::NEVER);
110         inset.draw(pi, x, y);
111
112         pi.pain.line(x + 2, y + inset.descent() - 4, x + 2,
113                 y + inset.descent(), params_.labelfont.color());
114         pi.pain.line(x + 2, y + inset.descent(), x + dim_.wid - 2,
115                 y + inset.descent(), params_.labelfont.color());
116         pi.pain.line(x + dim_.wid - 2, y + inset.descent(), x + dim_.wid - 2,
117                 y + inset.descent() - 4, params_.labelfont.color());
118
119         if (has_label_) {
120                 if (!owner())
121                         x += scroll();
122
123         LyXFont font(params_.labelfont);
124         font.realize(LyXFont(LyXFont::ALL_SANE));
125         font.decSize();
126         font.decSize();
127         int w = 0;
128         int a = 0;
129         int d = 0;
130         font_metrics::rectText(params_.type, font, w, a, d);
131         pi.pain.rectText(x + 0.5 * (dim_.wid - w), 
132                 y + inset.descent() + a,
133                 params_.type, font, LColor::none, LColor::none);
134         }
135 }
136
137
138 void InsetCharStyle::getDrawFont(LyXFont & font) const
139 {
140         font = params_.font;
141 }
142
143
144 DispatchResult
145 InsetCharStyle::priv_dispatch(FuncRequest const & cmd,
146                         idx_type & idx, pos_type & pos)
147 {
148         setStatus(Inlined);
149         switch (cmd.action) {
150                 case LFUN_MOUSE_PRESS:
151                         if (cmd.button() == mouse_button::button3) {
152                                 has_label_ = !has_label_;
153                                 return DispatchResult(true);
154                         }
155                         inset.dispatch(cmd);
156                         return DispatchResult(true, true);
157                         break;
158                 default:
159                         return InsetCollapsable::priv_dispatch(cmd, idx, pos);
160         }
161 }
162
163
164 namespace {
165
166 int outputVerbatim(std::ostream & os, InsetText inset)
167 {
168         int lines = 0;
169         ParagraphList::iterator par = inset.paragraphs().begin();
170         ParagraphList::iterator end = inset.paragraphs().end();
171         while (par != end) {
172                 lyx::pos_type siz = par->size();
173                 for (lyx::pos_type i = 0; i < siz; ++i) {
174                         if (par->isNewline(i)) {
175                                 os << '\n';
176                                 ++lines;
177                         } else {
178                                 os << par->getChar(i);
179                         }
180                 }
181                 ++par;
182                 if (par != end) {
183                         os << "\n";
184                         lines ++;
185                 }
186         }
187         return lines;
188 }
189
190 } // namespace anon
191
192
193 int InsetCharStyle::latex(Buffer const &, ostream & os,
194                      OutputParams const &) const
195 {
196         os << "%\n\\" << params_.latexname;
197         if (!params_.latexparam.empty())
198                 os << params_.latexparam;
199         os << "{";
200         int i = outputVerbatim(os, inset);
201         os << "}%\n";
202                 i += 2;
203         return i;
204 }
205
206
207 int InsetCharStyle::linuxdoc(Buffer const &, std::ostream & os,
208                              OutputParams const &) const
209 {
210         os << "<" << params_.latexname;
211         if (!params_.latexparam.empty())
212                 os << " " << params_.latexparam;
213         os << ">";
214         int const i = outputVerbatim(os, inset);
215         os << "</" << params_.latexname << ">";
216         return i;
217 }
218
219
220 int InsetCharStyle::docbook(Buffer const &, std::ostream & os,
221                             OutputParams const &) const
222 {
223         os << "<" << params_.latexname;
224         if (!params_.latexparam.empty())
225                 os << " " << params_.latexparam;
226         os << ">";
227         int const i = outputVerbatim(os, inset);
228         os << "</" << params_.latexname << ">";
229         return i;
230 }
231
232
233 int InsetCharStyle::plaintext(Buffer const &, std::ostream & os,
234                               OutputParams const & runparams) const
235 {
236         return outputVerbatim(os, inset);
237 }
238
239
240 void InsetCharStyle::validate(LaTeXFeatures & features) const
241 {
242         features.require(params_.type);
243 }
244
245
246 void InsetCharStyleParams::write(ostream & os) const
247 {
248         os << "CharStyle " << type << "\n";
249 }
250
251
252 void InsetCharStyleParams::read(LyXLex & lex)
253 {
254         if (lex.isOK()) {
255                 lex.next();
256                 string token = lex.getString();
257         }
258
259         if (lex.isOK()) {
260                 lex.next();
261                 type = lex.getString();
262         }
263 }