2 * \file ParagraphParameters.cpp
3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Lars Gullik Bjønnes
7 * \author Angus Leeming
10 * \author Jürgen Vigna
12 * Full author contact details are available in file CREDITS.
17 #include "ParagraphParameters.h"
24 #include "Paragraph.h"
26 #include "support/lstrings.h"
30 using lyx::support::rtrim;
32 using std::istringstream;
34 using std::ostringstream;
40 //NOTE The order of these MUST be the same as in Layout.h.
41 static char const * const string_align[] = {
42 "block", "left", "right", "center", "default", ""
46 ParagraphParameters::ParagraphParameters()
48 start_of_appendix_(false), appendix_(false),
49 align_(LYX_ALIGN_LAYOUT), depth_(0)
53 void ParagraphParameters::clear()
55 operator=(ParagraphParameters());
59 depth_type ParagraphParameters::depth() const
65 bool ParagraphParameters::sameLayout(ParagraphParameters const & pp) const
67 return align_ == pp.align_
68 && spacing_ == pp.spacing_
69 && noindent_ == pp.noindent_
70 && depth_ == pp.depth_;
74 Spacing const & ParagraphParameters::spacing() const
80 void ParagraphParameters::spacing(Spacing const & s)
86 bool ParagraphParameters::noindent() const
92 void ParagraphParameters::noindent(bool ni)
98 LyXAlignment ParagraphParameters::align() const
104 void ParagraphParameters::align(LyXAlignment la)
110 void ParagraphParameters::depth(depth_type d)
116 bool ParagraphParameters::startOfAppendix() const
118 return start_of_appendix_;
122 void ParagraphParameters::startOfAppendix(bool soa)
124 start_of_appendix_ = soa;
128 bool ParagraphParameters::appendix() const
134 void ParagraphParameters::appendix(bool a)
140 docstring const & ParagraphParameters::labelString() const
146 void ParagraphParameters::labelString(docstring const & ls)
152 docstring const & ParagraphParameters::labelWidthString() const
154 return labelwidthstring_;
158 void ParagraphParameters::labelWidthString(docstring const & lws)
160 labelwidthstring_ = lws;
164 Length const & ParagraphParameters::leftIndent() const
170 void ParagraphParameters::leftIndent(Length const & li)
176 void ParagraphParameters::read(std::string str, bool merge)
178 std::istringstream is(str);
185 void ParagraphParameters::read(Lexer & lex, bool merge)
191 string const token = lex.getString();
196 if (token[0] != '\\') {
197 lex.pushToken(token);
201 if (token == "\\noindent") {
203 } else if (token == "\\indent") {
204 //not found in LyX files but can be used with lfuns
206 } else if (token == "\\indent-toggle") {
207 //not found in LyX files but can be used with lfuns
208 noindent(!noindent());
209 } else if (token == "\\leftindent") {
211 Length value(lex.getString());
213 } else if (token == "\\start_of_appendix") {
214 startOfAppendix(true);
215 } else if (token == "\\paragraph_spacing") {
217 string const tmp = rtrim(lex.getString());
218 if (tmp == "default") {
219 //not found in LyX files but can be used with lfuns
220 spacing(Spacing(Spacing::Default));
221 } else if (tmp == "single") {
222 spacing(Spacing(Spacing::Single));
223 } else if (tmp == "onehalf") {
224 spacing(Spacing(Spacing::Onehalf));
225 } else if (tmp == "double") {
226 spacing(Spacing(Spacing::Double));
227 } else if (tmp == "other") {
229 spacing(Spacing(Spacing::Other,
232 lex.printError("Unknown spacing token: '$$Token'");
234 } else if (token == "\\align") {
236 int tmpret = support::findToken(string_align, lex.getString());
239 align(LyXAlignment(1 << tmpret));
240 } else if (token == "\\labelwidthstring") {
242 labelWidthString(lex.getDocString());
244 lex.pushToken(token);
251 void ParagraphParameters::apply(
252 ParagraphParameters const & p, Layout const & layout)
254 spacing(p.spacing());
255 // does the layout allow the new alignment?
256 if (p.align() & layout.alignpossible)
258 labelWidthString(p.labelWidthString());
259 noindent(p.noindent());
263 //FIXME This needs to be made a real method, so that getStatus()
264 //can return sensible information.
265 bool ParagraphParameters::canApply(
266 ParagraphParameters const & /*params*/, Layout const & /*layout*/)
272 void ParagraphParameters::write(ostream & os) const
274 // Maybe the paragraph has special spacing
275 spacing().writeFile(os, true);
277 // The labelwidth string used in lists.
278 if (!labelWidthString().empty())
279 os << "\\labelwidthstring "
280 << to_utf8(labelWidthString()) << '\n';
282 // Start of appendix?
283 if (startOfAppendix())
284 os << "\\start_of_appendix\n";
288 os << "\\noindent\n";
290 // Do we have a manual left indent?
291 if (!leftIndent().zero())
292 os << "\\leftindent " << leftIndent().asString() << '\n';
295 if (align() != LYX_ALIGN_LAYOUT) {
298 case LYX_ALIGN_LEFT: h = 1; break;
299 case LYX_ALIGN_RIGHT: h = 2; break;
300 case LYX_ALIGN_CENTER: h = 3; break;
301 default: h = 0; break;
303 os << "\\align " << string_align[h] << '\n';
308 void params2string(Paragraph const & par, string & data)
311 ParagraphParameters params = par.params();
313 // This needs to be done separately
314 params.labelWidthString(par.getLabelWidthString());
319 LayoutPtr const & layout = par.layout();
321 // Is alignment possible
322 os << "\\alignpossible " << layout->alignpossible << '\n';
324 /// set default alignment
325 os << "\\aligndefault " << layout->align << '\n';
327 /// paragraph is always in inset. This is redundant.
328 os << "\\ininset " << 1 << '\n';
335 bool operator==(ParagraphParameeters const & ps1,
336 ParagraphParameeters const & ps2)
339 ps1.spacing == ps2.spacing
340 && ps1.noindent == ps2.noindent
341 && ps1.align == ps2.align
342 && ps1.depth == ps2.depth
343 && ps1.start_of_appendix == ps2.start_of_appendix
344 && ps1.appendix == ps2.appendix
345 && ps1.labelstring == ps2.labelstring
346 && ps1.labelwidthstring == ps2.labelwidthstring
347 && ps1.leftindent == ps2.leftindent;