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"
20 #include "support/gettext.h"
24 #include "Paragraph.h"
26 #include "support/lstrings.h"
31 using namespace lyx::support;
36 //NOTE The order of these MUST be the same as in Layout.h.
37 static char const * const string_align[] = {
38 "block", "left", "right", "center", "default", ""
42 ParagraphParameters::ParagraphParameters()
44 start_of_appendix_(false), appendix_(false),
45 align_(LYX_ALIGN_LAYOUT), depth_(0)
49 void ParagraphParameters::clear()
51 operator=(ParagraphParameters());
55 depth_type ParagraphParameters::depth() const
61 bool ParagraphParameters::sameLayout(ParagraphParameters const & pp) const
63 return align_ == pp.align_
64 && spacing_ == pp.spacing_
65 && noindent_ == pp.noindent_
66 && depth_ == pp.depth_;
70 Spacing const & ParagraphParameters::spacing() const
76 void ParagraphParameters::spacing(Spacing const & s)
82 bool ParagraphParameters::noindent() const
88 void ParagraphParameters::noindent(bool ni)
94 LyXAlignment ParagraphParameters::align() const
100 void ParagraphParameters::align(LyXAlignment la)
106 void ParagraphParameters::depth(depth_type d)
112 bool ParagraphParameters::startOfAppendix() const
114 return start_of_appendix_;
118 void ParagraphParameters::startOfAppendix(bool soa)
120 start_of_appendix_ = soa;
124 bool ParagraphParameters::appendix() const
130 void ParagraphParameters::appendix(bool a)
136 docstring const & ParagraphParameters::labelString() const
142 void ParagraphParameters::labelString(docstring const & ls)
148 docstring const & ParagraphParameters::labelWidthString() const
150 return labelwidthstring_;
154 void ParagraphParameters::labelWidthString(docstring const & lws)
156 labelwidthstring_ = lws;
160 Length const & ParagraphParameters::leftIndent() const
166 void ParagraphParameters::leftIndent(Length const & li)
172 void ParagraphParameters::read(string str, bool merge)
174 istringstream is(str);
181 void ParagraphParameters::read(Lexer & lex, bool merge)
187 string const token = lex.getString();
192 if (token[0] != '\\') {
193 lex.pushToken(token);
197 if (token == "\\noindent") {
199 } else if (token == "\\indent") {
200 //not found in LyX files but can be used with lfuns
202 } else if (token == "\\indent-toggle") {
203 //not found in LyX files but can be used with lfuns
204 noindent(!noindent());
205 } else if (token == "\\leftindent") {
207 Length value(lex.getString());
209 } else if (token == "\\start_of_appendix") {
210 startOfAppendix(true);
211 } else if (token == "\\paragraph_spacing") {
213 string const tmp = rtrim(lex.getString());
214 if (tmp == "default") {
215 //not found in LyX files but can be used with lfuns
216 spacing(Spacing(Spacing::Default));
217 } else if (tmp == "single") {
218 spacing(Spacing(Spacing::Single));
219 } else if (tmp == "onehalf") {
220 spacing(Spacing(Spacing::Onehalf));
221 } else if (tmp == "double") {
222 spacing(Spacing(Spacing::Double));
223 } else if (tmp == "other") {
225 spacing(Spacing(Spacing::Other,
228 lex.printError("Unknown spacing token: '$$Token'");
230 } else if (token == "\\align") {
232 int tmpret = findToken(string_align, lex.getString());
235 align(LyXAlignment(1 << tmpret));
236 } else if (token == "\\labelwidthstring") {
238 labelWidthString(lex.getDocString());
240 lex.pushToken(token);
247 void ParagraphParameters::apply(
248 ParagraphParameters const & p, Layout const & layout)
250 spacing(p.spacing());
251 // does the layout allow the new alignment?
252 if (p.align() & layout.alignpossible)
254 labelWidthString(p.labelWidthString());
255 noindent(p.noindent());
259 void ParagraphParameters::write(ostream & os) const
261 // Maybe the paragraph has special spacing
262 spacing().writeFile(os, true);
264 // The labelwidth string used in lists.
265 if (!labelWidthString().empty())
266 os << "\\labelwidthstring "
267 << to_utf8(labelWidthString()) << '\n';
269 // Start of appendix?
270 if (startOfAppendix())
271 os << "\\start_of_appendix\n";
275 os << "\\noindent\n";
277 // Do we have a manual left indent?
278 if (!leftIndent().zero())
279 os << "\\leftindent " << leftIndent().asString() << '\n';
282 if (align() != LYX_ALIGN_LAYOUT) {
285 case LYX_ALIGN_LEFT: h = 1; break;
286 case LYX_ALIGN_RIGHT: h = 2; break;
287 case LYX_ALIGN_CENTER: h = 3; break;
288 default: h = 0; break;
290 os << "\\align " << string_align[h] << '\n';
295 void params2string(Paragraph const & par, string & data)
298 ParagraphParameters params = par.params();
300 // This needs to be done separately
301 params.labelWidthString(par.getLabelWidthString());
306 Layout const & layout = par.layout();
308 // Is alignment possible
309 os << "\\alignpossible " << layout.alignpossible << '\n';
311 /// set default alignment
312 os << "\\aligndefault " << layout.align << '\n';
314 /// paragraph is always in inset. This is redundant.
315 os << "\\ininset " << 1 << '\n';
321 LyXErr & operator<<(LyXErr & os, ParagraphParameters const & parp) {
322 parp.write(os.stream());
328 bool operator==(ParagraphParameeters const & ps1,
329 ParagraphParameeters const & ps2)
332 ps1.spacing == ps2.spacing
333 && ps1.noindent == ps2.noindent
334 && ps1.align == ps2.align
335 && ps1.depth == ps2.depth
336 && ps1.start_of_appendix == ps2.start_of_appendix
337 && ps1.appendix == ps2.appendix
338 && ps1.labelstring == ps2.labelstring
339 && ps1.labelwidthstring == ps2.labelwidthstring
340 && ps1.leftindent == ps2.leftindent;