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