2 * \file ParagraphParameters.C
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 "BufferView.h"
22 #include "lyxlayout.h"
25 #include "paragraph.h"
26 #include "ParameterStruct.h"
27 #include "tex-strings.h"
29 #include "frontends/LyXView.h"
31 #include "support/lstrings.h"
33 #include "support/std_sstream.h"
35 using lyx::support::rtrim;
37 using std::istringstream;
39 using std::ostringstream;
42 // Initialize static member var.
43 ShareContainer<ParameterStruct> ParagraphParameters::container;
45 ParagraphParameters::ParagraphParameters()
52 void ParagraphParameters::clear()
54 ParameterStruct tmp(*param);
56 tmp.line_bottom = false;
57 tmp.pagebreak_top = false;
58 tmp.pagebreak_bottom = false;
59 tmp.added_space_top = VSpace(VSpace::NONE);
60 tmp.added_space_bottom = VSpace(VSpace::NONE);
61 tmp.spacing.set(Spacing::Default);
62 tmp.align = LYX_ALIGN_LAYOUT;
65 tmp.labelstring.erase();
66 tmp.labelwidthstring.erase();
67 tmp.start_of_appendix = false;
72 ParagraphParameters::depth_type ParagraphParameters::depth() const
78 bool ParagraphParameters::sameLayout(ParagraphParameters const & pp) const
80 return param->align == pp.param->align &&
81 param->line_bottom == pp.param->line_bottom &&
82 param->pagebreak_bottom == pp.param->pagebreak_bottom &&
83 param->added_space_bottom == pp.param->added_space_bottom &&
85 param->line_top == pp.param->line_top &&
86 param->pagebreak_top == pp.param->pagebreak_top &&
87 param->added_space_top == pp.param->added_space_top &&
88 param->spacing == pp.param->spacing &&
89 param->noindent == pp.param->noindent &&
90 param->depth == pp.param->depth;
94 void ParagraphParameters::set_from_struct(ParameterStruct const & ps)
96 // get new param from container with tmp as template
97 param = container.get(ps);
101 VSpace const & ParagraphParameters::spaceTop() const
103 return param->added_space_top;
107 void ParagraphParameters::spaceTop(VSpace const & vs)
109 ParameterStruct tmp(*param);
110 tmp.added_space_top = vs;
111 set_from_struct(tmp);
115 VSpace const & ParagraphParameters::spaceBottom() const
117 return param->added_space_bottom;
121 void ParagraphParameters::spaceBottom(VSpace const & vs)
123 ParameterStruct tmp(*param);
124 tmp.added_space_bottom = vs;
125 set_from_struct(tmp);
129 Spacing const & ParagraphParameters::spacing() const
131 return param->spacing;
135 void ParagraphParameters::spacing(Spacing const & s)
137 ParameterStruct tmp(*param);
139 set_from_struct(tmp);
143 bool ParagraphParameters::noindent() const
145 return param->noindent;
149 void ParagraphParameters::noindent(bool ni)
151 ParameterStruct tmp(*param);
153 set_from_struct(tmp);
157 bool ParagraphParameters::lineTop() const
159 return param->line_top;
163 void ParagraphParameters::lineTop(bool lt)
165 ParameterStruct tmp(*param);
167 set_from_struct(tmp);
171 bool ParagraphParameters::lineBottom() const
173 return param->line_bottom;
177 void ParagraphParameters::lineBottom(bool lb)
179 ParameterStruct tmp(*param);
180 tmp.line_bottom = lb;
181 set_from_struct(tmp);
185 bool ParagraphParameters::pagebreakTop() const
187 return param->pagebreak_top;
191 void ParagraphParameters::pagebreakTop(bool pbt)
193 ParameterStruct tmp(*param);
194 tmp.pagebreak_top = pbt;
195 set_from_struct(tmp);
199 bool ParagraphParameters::pagebreakBottom() const
201 return param->pagebreak_bottom;
205 void ParagraphParameters::pagebreakBottom(bool pbb)
207 ParameterStruct tmp(*param);
208 tmp.pagebreak_bottom = pbb;
209 set_from_struct(tmp);
213 LyXAlignment ParagraphParameters::align() const
219 void ParagraphParameters::align(LyXAlignment la)
221 ParameterStruct tmp(*param);
223 set_from_struct(tmp);
227 void ParagraphParameters::depth(depth_type d)
229 ParameterStruct tmp(*param);
231 set_from_struct(tmp);
235 bool ParagraphParameters::startOfAppendix() const
237 return param->start_of_appendix;
241 void ParagraphParameters::startOfAppendix(bool soa)
243 ParameterStruct tmp(*param);
244 tmp.start_of_appendix = soa;
245 set_from_struct(tmp);
249 bool ParagraphParameters::appendix() const
251 return param->appendix;
255 void ParagraphParameters::appendix(bool a)
257 ParameterStruct tmp(*param);
259 set_from_struct(tmp);
263 string const & ParagraphParameters::labelString() const
265 return param->labelstring;
269 void ParagraphParameters::labelString(string const & ls)
271 ParameterStruct tmp(*param);
272 tmp.labelstring = ls;
273 set_from_struct(tmp);
277 string const & ParagraphParameters::labelWidthString() const
279 return param->labelwidthstring;
283 void ParagraphParameters::labelWidthString(string const & lws)
285 ParameterStruct tmp(*param);
286 tmp.labelwidthstring = lws;
287 set_from_struct(tmp);
291 LyXLength const & ParagraphParameters::leftIndent() const
293 return param->leftindent;
297 void ParagraphParameters::leftIndent(LyXLength const & li)
299 ParameterStruct tmp(*param);
301 set_from_struct(tmp);
305 void ParagraphParameters::read(LyXLex & lex)
309 string const token = lex.getString();
314 if (token[0] != '\\') {
315 lex.pushToken(token);
319 if (token == "\\noindent") {
321 } else if (token == "\\leftindent") {
323 LyXLength value(lex.getString());
325 } else if (token == "\\fill_top") {
326 spaceTop(VSpace(VSpace::VFILL));
327 } else if (token == "\\fill_bottom") {
328 spaceBottom(VSpace(VSpace::VFILL));
329 } else if (token == "\\line_top") {
331 } else if (token == "\\line_bottom") {
333 } else if (token == "\\pagebreak_top") {
335 } else if (token == "\\pagebreak_bottom") {
336 pagebreakBottom(true);
337 } else if (token == "\\start_of_appendix") {
338 startOfAppendix(true);
339 } else if (token == "\\paragraph_spacing") {
341 string const tmp = rtrim(lex.getString());
342 if (tmp == "single") {
343 spacing(Spacing(Spacing::Single));
344 } else if (tmp == "onehalf") {
345 spacing(Spacing(Spacing::Onehalf));
346 } else if (tmp == "double") {
347 spacing(Spacing(Spacing::Double));
348 } else if (tmp == "other") {
350 spacing(Spacing(Spacing::Other,
353 lex.printError("Unknown spacing token: '$$Token'");
355 } else if (token == "\\align") {
356 int tmpret = lex.findToken(string_align);
359 align(LyXAlignment(1 << tmpret));
360 } else if (token == "\\added_space_top") {
362 VSpace value = VSpace(lex.getString());
363 // only add the length when value > 0 or
365 if ((value.length().len().value() != 0) ||
367 (value.kind() != VSpace::LENGTH))
369 } else if (token == "\\added_space_bottom") {
371 VSpace value = VSpace(lex.getString());
372 // only add the length when value > 0 or
374 if ((value.length().len().value() != 0) ||
376 (value.kind() != VSpace::LENGTH))
378 } else if (token == "\\labelwidthstring") {
380 labelWidthString(lex.getString());
382 lex.pushToken(token);
389 void ParagraphParameters::write(ostream & os) const
391 // Maybe some vertical spaces.
392 if (spaceTop().kind() != VSpace::NONE)
393 os << "\\added_space_top "
394 << spaceTop().asLyXCommand() << ' ';
395 if (spaceBottom().kind() != VSpace::NONE)
396 os << "\\added_space_bottom "
397 << spaceBottom().asLyXCommand() << ' ';
399 // Maybe the paragraph has special spacing
400 spacing().writeFile(os, true);
402 // The labelwidth string used in lists.
403 if (!labelWidthString().empty())
404 os << "\\labelwidthstring "
405 << labelWidthString() << '\n';
407 // Lines above or below?
411 os << "\\line_bottom ";
413 // Pagebreaks above or below?
415 os << "\\pagebreak_top ";
416 if (pagebreakBottom())
417 os << "\\pagebreak_bottom ";
419 // Start of appendix?
420 if (startOfAppendix())
421 os << "\\start_of_appendix ";
427 // Do we have a manual left indent?
428 if (!leftIndent().zero())
429 os << "\\leftindent " << leftIndent().asString()
433 if (align() != LYX_ALIGN_LAYOUT) {
436 case LYX_ALIGN_LEFT: h = 1; break;
437 case LYX_ALIGN_RIGHT: h = 2; break;
438 case LYX_ALIGN_CENTER: h = 3; break;
439 default: h = 0; break;
441 os << "\\align " << string_align[h] << ' ';
447 void setParagraphParams(BufferView & bv, string const & data)
449 istringstream is(data);
453 ParagraphParameters params;
456 LyXText * text = bv.getLyXText();
457 text->setParagraph(params.lineTop(),
459 params.pagebreakTop(),
460 params.pagebreakBottom(),
462 params.spaceBottom(),
465 params.labelWidthString(),
469 bv.owner()->message(_("Paragraph layout set"));
473 void params2string(Paragraph const & par, string & data)
476 ParagraphParameters params = par.params();
478 // This needs to be done separately
479 params.labelWidthString(par.getLabelWidthString());
482 LyXLayout_ptr const & layout = par.layout();
483 if (params.align() == LYX_ALIGN_LAYOUT)
484 params.align(layout->align);
489 // Is alignment possible
490 os << '\n' << "\\alignpossible " << layout->alignpossible << '\n';
492 /// set default alignment
493 os << "\\aligndefault " << layout->align << '\n';
495 /// is paragraph in inset
496 os << "\\ininset " << (par.inInset()?1:0) << '\n';