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;
43 // Initialize static member var.
44 ShareContainer<ParameterStruct> ParagraphParameters::container;
46 ParagraphParameters::ParagraphParameters()
53 void ParagraphParameters::clear()
55 ParameterStruct tmp(*param);
57 tmp.line_bottom = false;
58 tmp.pagebreak_top = false;
59 tmp.pagebreak_bottom = false;
60 tmp.added_space_top = VSpace(VSpace::NONE);
61 tmp.added_space_bottom = VSpace(VSpace::NONE);
62 tmp.spacing.set(Spacing::Default);
63 tmp.align = LYX_ALIGN_LAYOUT;
66 tmp.labelstring.erase();
67 tmp.labelwidthstring.erase();
68 tmp.start_of_appendix = false;
73 ParagraphParameters::depth_type ParagraphParameters::depth() const
79 bool ParagraphParameters::sameLayout(ParagraphParameters const & pp) const
81 return param->align == pp.param->align &&
82 param->line_bottom == pp.param->line_bottom &&
83 param->pagebreak_bottom == pp.param->pagebreak_bottom &&
84 param->added_space_bottom == pp.param->added_space_bottom &&
86 param->line_top == pp.param->line_top &&
87 param->pagebreak_top == pp.param->pagebreak_top &&
88 param->added_space_top == pp.param->added_space_top &&
89 param->spacing == pp.param->spacing &&
90 param->noindent == pp.param->noindent &&
91 param->depth == pp.param->depth;
95 void ParagraphParameters::set_from_struct(ParameterStruct const & ps)
97 // get new param from container with tmp as template
98 param = container.get(ps);
102 VSpace const & ParagraphParameters::spaceTop() const
104 return param->added_space_top;
108 void ParagraphParameters::spaceTop(VSpace const & vs)
110 ParameterStruct tmp(*param);
111 tmp.added_space_top = vs;
112 set_from_struct(tmp);
116 VSpace const & ParagraphParameters::spaceBottom() const
118 return param->added_space_bottom;
122 void ParagraphParameters::spaceBottom(VSpace const & vs)
124 ParameterStruct tmp(*param);
125 tmp.added_space_bottom = vs;
126 set_from_struct(tmp);
130 Spacing const & ParagraphParameters::spacing() const
132 return param->spacing;
136 void ParagraphParameters::spacing(Spacing const & s)
138 ParameterStruct tmp(*param);
140 set_from_struct(tmp);
144 bool ParagraphParameters::noindent() const
146 return param->noindent;
150 void ParagraphParameters::noindent(bool ni)
152 ParameterStruct tmp(*param);
154 set_from_struct(tmp);
158 bool ParagraphParameters::lineTop() const
160 return param->line_top;
164 void ParagraphParameters::lineTop(bool lt)
166 ParameterStruct tmp(*param);
168 set_from_struct(tmp);
172 bool ParagraphParameters::lineBottom() const
174 return param->line_bottom;
178 void ParagraphParameters::lineBottom(bool lb)
180 ParameterStruct tmp(*param);
181 tmp.line_bottom = lb;
182 set_from_struct(tmp);
186 bool ParagraphParameters::pagebreakTop() const
188 return param->pagebreak_top;
192 void ParagraphParameters::pagebreakTop(bool pbt)
194 ParameterStruct tmp(*param);
195 tmp.pagebreak_top = pbt;
196 set_from_struct(tmp);
200 bool ParagraphParameters::pagebreakBottom() const
202 return param->pagebreak_bottom;
206 void ParagraphParameters::pagebreakBottom(bool pbb)
208 ParameterStruct tmp(*param);
209 tmp.pagebreak_bottom = pbb;
210 set_from_struct(tmp);
214 LyXAlignment ParagraphParameters::align() const
220 void ParagraphParameters::align(LyXAlignment la)
222 ParameterStruct tmp(*param);
224 set_from_struct(tmp);
228 void ParagraphParameters::depth(depth_type d)
230 ParameterStruct tmp(*param);
232 set_from_struct(tmp);
236 bool ParagraphParameters::startOfAppendix() const
238 return param->start_of_appendix;
242 void ParagraphParameters::startOfAppendix(bool soa)
244 ParameterStruct tmp(*param);
245 tmp.start_of_appendix = soa;
246 set_from_struct(tmp);
250 bool ParagraphParameters::appendix() const
252 return param->appendix;
256 void ParagraphParameters::appendix(bool a)
258 ParameterStruct tmp(*param);
260 set_from_struct(tmp);
264 string const & ParagraphParameters::labelString() const
266 return param->labelstring;
270 void ParagraphParameters::labelString(string const & ls)
272 ParameterStruct tmp(*param);
273 tmp.labelstring = ls;
274 set_from_struct(tmp);
278 string const & ParagraphParameters::labelWidthString() const
280 return param->labelwidthstring;
284 void ParagraphParameters::labelWidthString(string const & lws)
286 ParameterStruct tmp(*param);
287 tmp.labelwidthstring = lws;
288 set_from_struct(tmp);
292 LyXLength const & ParagraphParameters::leftIndent() const
294 return param->leftindent;
298 void ParagraphParameters::leftIndent(LyXLength const & li)
300 ParameterStruct tmp(*param);
302 set_from_struct(tmp);
306 void ParagraphParameters::read(LyXLex & lex)
310 string const token = lex.getString();
315 if (token[0] != '\\') {
316 lex.pushToken(token);
320 if (token == "\\noindent") {
322 } else if (token == "\\leftindent") {
324 LyXLength value(lex.getString());
326 } else if (token == "\\fill_top") {
327 spaceTop(VSpace(VSpace::VFILL));
328 } else if (token == "\\fill_bottom") {
329 spaceBottom(VSpace(VSpace::VFILL));
330 } else if (token == "\\line_top") {
332 } else if (token == "\\line_bottom") {
334 } else if (token == "\\pagebreak_top") {
336 } else if (token == "\\pagebreak_bottom") {
337 pagebreakBottom(true);
338 } else if (token == "\\start_of_appendix") {
339 startOfAppendix(true);
340 } else if (token == "\\paragraph_spacing") {
342 string const tmp = rtrim(lex.getString());
343 if (tmp == "single") {
344 spacing(Spacing(Spacing::Single));
345 } else if (tmp == "onehalf") {
346 spacing(Spacing(Spacing::Onehalf));
347 } else if (tmp == "double") {
348 spacing(Spacing(Spacing::Double));
349 } else if (tmp == "other") {
351 spacing(Spacing(Spacing::Other,
354 lex.printError("Unknown spacing token: '$$Token'");
356 } else if (token == "\\align") {
357 int tmpret = lex.findToken(string_align);
360 align(LyXAlignment(1 << tmpret));
361 } else if (token == "\\added_space_top") {
363 VSpace value = VSpace(lex.getString());
364 // only add the length when value > 0 or
366 if ((value.length().len().value() != 0) ||
368 (value.kind() != VSpace::LENGTH))
370 } else if (token == "\\added_space_bottom") {
372 VSpace value = VSpace(lex.getString());
373 // only add the length when value > 0 or
375 if ((value.length().len().value() != 0) ||
377 (value.kind() != VSpace::LENGTH))
379 } else if (token == "\\labelwidthstring") {
381 labelWidthString(lex.getString());
383 lex.pushToken(token);
390 void ParagraphParameters::write(ostream & os) const
392 // Maybe some vertical spaces.
393 if (spaceTop().kind() != VSpace::NONE)
394 os << "\\added_space_top "
395 << spaceTop().asLyXCommand() << ' ';
396 if (spaceBottom().kind() != VSpace::NONE)
397 os << "\\added_space_bottom "
398 << spaceBottom().asLyXCommand() << ' ';
400 // Maybe the paragraph has special spacing
401 spacing().writeFile(os, true);
403 // The labelwidth string used in lists.
404 if (!labelWidthString().empty())
405 os << "\\labelwidthstring "
406 << labelWidthString() << '\n';
408 // Lines above or below?
412 os << "\\line_bottom ";
414 // Pagebreaks above or below?
416 os << "\\pagebreak_top ";
417 if (pagebreakBottom())
418 os << "\\pagebreak_bottom ";
420 // Start of appendix?
421 if (startOfAppendix())
422 os << "\\start_of_appendix ";
428 // Do we have a manual left indent?
429 if (!leftIndent().zero())
430 os << "\\leftindent " << leftIndent().asString()
434 if (align() != LYX_ALIGN_LAYOUT) {
437 case LYX_ALIGN_LEFT: h = 1; break;
438 case LYX_ALIGN_RIGHT: h = 2; break;
439 case LYX_ALIGN_CENTER: h = 3; break;
440 default: h = 0; break;
442 os << "\\align " << string_align[h] << ' ';
448 void setParagraphParams(BufferView & bv, string const & data)
450 istringstream is(data);
454 ParagraphParameters params;
457 LyXText * text = bv.getLyXText();
458 text->setParagraph(params.lineTop(),
460 params.pagebreakTop(),
461 params.pagebreakBottom(),
463 params.spaceBottom(),
466 params.labelWidthString(),
470 bv.owner()->message(_("Paragraph layout set"));
474 void params2string(Paragraph const & par, string & data)
477 ParagraphParameters params = par.params();
479 // This needs to be done separately
480 params.labelWidthString(par.getLabelWidthString());
483 LyXLayout_ptr const & layout = par.layout();
484 if (params.align() == LYX_ALIGN_LAYOUT)
485 params.align(layout->align);
490 // Is alignment possible
491 os << '\n' << "\\alignpossible " << layout->alignpossible << '\n';
493 /// set default alignment
494 os << "\\aligndefault " << layout->align << '\n';
496 /// is paragraph in inset
497 os << "\\ininset " << (par.inInset()?1:0) << '\n';