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"
21 #include "Paragraph.h"
23 #include "support/debug.h"
24 #include "support/docstring.h"
25 #include "support/lstrings.h"
30 using namespace lyx::support;
35 //NOTE The order of these MUST be the same as in Layout.h.
36 static char const * const string_align[] = {
37 "block", "left", "right", "center", "default", ""
41 ParagraphParameters::ParagraphParameters()
43 start_of_appendix_(false), appendix_(false),
44 align_(LYX_ALIGN_LAYOUT), depth_(0)
48 void ParagraphParameters::clear()
50 operator=(ParagraphParameters());
54 depth_type ParagraphParameters::depth() const
60 bool ParagraphParameters::sameLayout(ParagraphParameters const & pp) const
62 return align_ == pp.align_
63 && spacing_ == pp.spacing_
64 && noindent_ == pp.noindent_
65 && depth_ == pp.depth_;
69 Spacing const & ParagraphParameters::spacing() const
75 void ParagraphParameters::spacing(Spacing const & s)
81 bool ParagraphParameters::noindent() const
87 void ParagraphParameters::noindent(bool ni)
93 LyXAlignment ParagraphParameters::align() const
99 void ParagraphParameters::align(LyXAlignment la)
105 void ParagraphParameters::depth(depth_type d)
111 bool ParagraphParameters::startOfAppendix() const
113 return start_of_appendix_;
117 void ParagraphParameters::startOfAppendix(bool soa)
119 start_of_appendix_ = soa;
123 bool ParagraphParameters::appendix() const
129 void ParagraphParameters::appendix(bool a)
135 docstring const & ParagraphParameters::labelString() const
141 void ParagraphParameters::labelString(docstring const & ls)
147 docstring const & ParagraphParameters::labelWidthString() const
149 return labelwidthstring_;
153 void ParagraphParameters::labelWidthString(docstring const & lws)
155 labelwidthstring_ = lws;
159 Length const & ParagraphParameters::leftIndent() const
165 void ParagraphParameters::leftIndent(Length const & li)
171 void ParagraphParameters::read(string str, bool merge)
173 istringstream is(str);
180 void ParagraphParameters::read(Lexer & lex, bool merge)
186 string const token = lex.getString();
191 if (token[0] != '\\') {
192 lex.pushToken(token);
196 if (token == "\\noindent") {
198 } else if (token == "\\indent") {
199 //not found in LyX files but can be used with lfuns
201 } else if (token == "\\indent-toggle") {
202 //not found in LyX files but can be used with lfuns
203 noindent(!noindent());
204 } else if (token == "\\leftindent") {
206 Length value(lex.getString());
208 } else if (token == "\\start_of_appendix") {
209 startOfAppendix(true);
210 } else if (token == "\\paragraph_spacing") {
212 string const tmp = rtrim(lex.getString());
213 if (tmp == "default") {
214 //not found in LyX files but can be used with lfuns
215 spacing(Spacing(Spacing::Default));
216 } else if (tmp == "single") {
217 spacing(Spacing(Spacing::Single));
218 } else if (tmp == "onehalf") {
219 spacing(Spacing(Spacing::Onehalf));
220 } else if (tmp == "double") {
221 spacing(Spacing(Spacing::Double));
222 } else if (tmp == "other") {
224 spacing(Spacing(Spacing::Other,
227 lex.printError("Unknown spacing token: '$$Token'");
229 } else if (token == "\\align") {
231 int tmpret = findToken(string_align, lex.getString());
234 align(LyXAlignment(1 << tmpret));
235 } else if (token == "\\labelwidthstring") {
237 labelWidthString(lex.getDocString());
239 lex.pushToken(token);
246 void ParagraphParameters::apply(
247 ParagraphParameters const & p, Layout const & layout)
249 spacing(p.spacing());
250 // does the layout allow the new alignment?
251 if (p.align() & layout.alignpossible)
253 labelWidthString(p.labelWidthString());
254 noindent(p.noindent());
258 void ParagraphParameters::write(ostream & os) const
260 // Maybe the paragraph has special spacing
261 spacing().writeFile(os, true);
263 // The labelwidth string used in lists.
264 if (!labelWidthString().empty())
265 os << "\\labelwidthstring "
266 << to_utf8(labelWidthString()) << '\n';
268 // Start of appendix?
269 if (startOfAppendix())
270 os << "\\start_of_appendix\n";
274 os << "\\noindent\n";
276 // Do we have a manual left indent?
277 if (!leftIndent().zero())
278 os << "\\leftindent " << leftIndent().asString() << '\n';
281 if (align() != LYX_ALIGN_LAYOUT) {
284 case LYX_ALIGN_LEFT: h = 1; break;
285 case LYX_ALIGN_RIGHT: h = 2; break;
286 case LYX_ALIGN_CENTER: h = 3; break;
287 default: h = 0; break;
289 os << "\\align " << string_align[h] << '\n';
294 void params2string(Paragraph const & par, string & data)
297 ParagraphParameters params = par.params();
299 // This needs to be done separately
300 params.labelWidthString(par.getLabelWidthString());
305 Layout const & layout = par.layout();
307 // Is alignment possible
308 os << "\\alignpossible " << layout.alignpossible << '\n';
310 /// set default alignment
311 os << "\\aligndefault " << layout.align << '\n';
313 /// paragraph is always in inset. This is redundant.
314 os << "\\ininset " << 1 << '\n';
320 LyXErr & operator<<(LyXErr & os, ParagraphParameters const & parp) {
321 parp.write(os.stream());
327 bool operator==(ParagraphParameeters const & ps1,
328 ParagraphParameeters const & ps2)
331 ps1.spacing == ps2.spacing
332 && ps1.noindent == ps2.noindent
333 && ps1.align == ps2.align
334 && ps1.depth == ps2.depth
335 && ps1.start_of_appendix == ps2.start_of_appendix
336 && ps1.appendix == ps2.appendix
337 && ps1.labelstring == ps2.labelstring
338 && ps1.labelwidthstring == ps2.labelwidthstring
339 && ps1.leftindent == ps2.leftindent;