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 //FIXME This needs to be made a real method, so that getStatus()
260 //can return sensible information.
261 bool ParagraphParameters::canApply(
262 ParagraphParameters const & /*params*/, Layout const & /*layout*/)
268 void ParagraphParameters::write(ostream & os) const
270 // Maybe the paragraph has special spacing
271 spacing().writeFile(os, true);
273 // The labelwidth string used in lists.
274 if (!labelWidthString().empty())
275 os << "\\labelwidthstring "
276 << to_utf8(labelWidthString()) << '\n';
278 // Start of appendix?
279 if (startOfAppendix())
280 os << "\\start_of_appendix\n";
284 os << "\\noindent\n";
286 // Do we have a manual left indent?
287 if (!leftIndent().zero())
288 os << "\\leftindent " << leftIndent().asString() << '\n';
291 if (align() != LYX_ALIGN_LAYOUT) {
294 case LYX_ALIGN_LEFT: h = 1; break;
295 case LYX_ALIGN_RIGHT: h = 2; break;
296 case LYX_ALIGN_CENTER: h = 3; break;
297 default: h = 0; break;
299 os << "\\align " << string_align[h] << '\n';
304 void params2string(Paragraph const & par, string & data)
307 ParagraphParameters params = par.params();
309 // This needs to be done separately
310 params.labelWidthString(par.getLabelWidthString());
315 Layout const & layout = par.layout();
317 // Is alignment possible
318 os << "\\alignpossible " << layout.alignpossible << '\n';
320 /// set default alignment
321 os << "\\aligndefault " << layout.align << '\n';
323 /// paragraph is always in inset. This is redundant.
324 os << "\\ininset " << 1 << '\n';
331 bool operator==(ParagraphParameeters const & ps1,
332 ParagraphParameeters const & ps2)
335 ps1.spacing == ps2.spacing
336 && ps1.noindent == ps2.noindent
337 && ps1.align == ps2.align
338 && ps1.depth == ps2.depth
339 && ps1.start_of_appendix == ps2.start_of_appendix
340 && ps1.appendix == ps2.appendix
341 && ps1.labelstring == ps2.labelstring
342 && ps1.labelwidthstring == ps2.labelwidthstring
343 && ps1.leftindent == ps2.leftindent;