]> git.lyx.org Git - lyx.git/blob - src/insets/insetcharstyle.C
3a24c12cda2167070f4da930adfa3638dd6f438c
[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 "support/std_sstream.h"
29
30
31 using std::string;
32 using std::auto_ptr;
33 using std::istringstream;
34 using std::ostream;
35 using std::ostringstream;
36
37
38 void InsetCharStyle::init()
39 {
40         setInsetName("CharStyle");
41         setButtonLabel();
42 }
43
44
45 InsetCharStyle::InsetCharStyle(BufferParams const & bp,
46                                 CharStyles::iterator cs)
47         : InsetCollapsable(bp)
48 {
49         params_.type = cs->name;
50         params_.latextype = cs->latextype;
51         params_.latexname = cs->latexname;
52         params_.latexparam = cs->latexparam;
53         params_.font = cs->font;
54         params_.labelfont = cs->labelfont;
55         init();
56 }
57
58
59 InsetCharStyle::InsetCharStyle(InsetCharStyle const & in)
60         : InsetCollapsable(in), params_(in.params_)
61 {
62         init();
63 }
64
65
66 auto_ptr<InsetBase> InsetCharStyle::clone() const
67 {
68         return auto_ptr<InsetBase>(new InsetCharStyle(*this));
69 }
70
71
72 string const InsetCharStyle::editMessage() const
73 {
74         return _("Opened CharStyle Inset");
75 }
76
77
78 void InsetCharStyle::write(Buffer const & buf, ostream & os) const
79 {
80         params_.write(os);
81         InsetCollapsable::write(buf, os);
82 }
83
84
85 void InsetCharStyle::read(Buffer const & buf, LyXLex & lex)
86 {
87         InsetCollapsable::read(buf, lex);
88         setButtonLabel();
89 }
90
91
92 void InsetCharStyle::setButtonLabel()
93 {
94         LyXFont font(params_.labelfont);
95         font.realize(LyXFont(LyXFont::ALL_SANE));
96         string const s = "Style: " + params_.type;
97         setLabel(isOpen() ? s : getNewLabel(s) );
98         setLabelFont(font);
99 }
100
101
102 void InsetCharStyle::metrics(MetricsInfo & mi, Dimension & dim) const
103 {
104         InsetCollapsable::metrics(mi, dim);
105         dim_ = dim;
106 }
107
108
109 void InsetCharStyle::getDrawFont(LyXFont & font) const
110 {
111         font = params_.font;
112 }
113
114
115 DispatchResult
116 InsetCharStyle::priv_dispatch(FuncRequest const & cmd,
117                         idx_type & idx, pos_type & pos)
118 {
119         DispatchResult dr = InsetCollapsable::priv_dispatch(cmd, idx, pos);
120         setButtonLabel();
121         return dr;
122 }
123
124
125 namespace {
126
127 int outputVerbatim(std::ostream & os, InsetText inset)
128 {
129         int lines = 0;
130         ParagraphList::iterator par = inset.paragraphs().begin();
131         ParagraphList::iterator end = inset.paragraphs().end();
132         while (par != end) {
133                 lyx::pos_type siz = par->size();
134                 for (lyx::pos_type i = 0; i < siz; ++i) {
135                         if (par->isNewline(i)) {
136                                 os << '\n';
137                                 ++lines;
138                         } else {
139                                 os << par->getChar(i);
140                         }
141                 }
142                 ++par;
143                 if (par != end) {
144                         os << "\n";
145                         lines ++;
146                 }
147         }
148         return lines;
149 }
150
151 } // namespace anon
152
153
154 int InsetCharStyle::latex(Buffer const &, ostream & os,
155                      OutputParams const &) const
156 {
157         os << "%\n\\" << params_.latexname;
158         if (!params_.latexparam.empty())
159                 os << params_.latexparam;
160         os << "{";
161         int i = outputVerbatim(os, inset);
162         os << "}%\n";
163                 i += 2;
164         return i;
165 }
166
167
168 int InsetCharStyle::linuxdoc(Buffer const &, std::ostream & os,
169                              OutputParams const &) const
170 {
171         os << "<" << params_.latexname;
172         if (!params_.latexparam.empty())
173                 os << " " << params_.latexparam;
174         os << ">";
175         int const i = outputVerbatim(os, inset);
176         os << "</" << params_.latexname << ">";
177         return i;
178 }
179
180
181 int InsetCharStyle::docbook(Buffer const &, std::ostream & os,
182                             OutputParams const &) const
183 {
184         os << "<" << params_.latexname;
185         if (!params_.latexparam.empty())
186                 os << " " << params_.latexparam;
187         os << ">";
188         int const i = outputVerbatim(os, inset);
189         os << "</" << params_.latexname << ">";
190         return i;
191 }
192
193
194 int InsetCharStyle::plaintext(Buffer const &, std::ostream & os,
195                               OutputParams const & runparams) const
196 {
197         return outputVerbatim(os, inset);
198 }
199
200
201 void InsetCharStyle::validate(LaTeXFeatures & features) const
202 {
203         features.require(params_.type);
204 }
205
206
207 void InsetCharStyleParams::write(ostream & os) const
208 {
209         os << "CharStyle " << type << "\n";
210 }
211
212
213 void InsetCharStyleParams::read(LyXLex & lex)
214 {
215         if (lex.isOK()) {
216                 lex.next();
217                 string token = lex.getString();
218         }
219
220         if (lex.isOK()) {
221                 lex.next();
222                 type = lex.getString();
223         }
224 }