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"
18 #include "ParameterStruct.h"
19 #include "tex-strings.h"
23 #include "BufferView.h"
26 #include "paragraph.h"
30 #include "frontends/LyXView.h"
32 #include "support/lstrings.h"
36 using namespace lyx::support;
40 // Initialize static member var.
41 ShareContainer<ParameterStruct> ParagraphParameters::container;
43 ParagraphParameters::ParagraphParameters()
50 void ParagraphParameters::clear()
52 ParameterStruct tmp(*param);
54 tmp.line_bottom = false;
55 tmp.pagebreak_top = false;
56 tmp.pagebreak_bottom = false;
57 tmp.added_space_top = VSpace(VSpace::NONE);
58 tmp.added_space_bottom = VSpace(VSpace::NONE);
59 tmp.spacing.set(Spacing::Default);
60 tmp.align = LYX_ALIGN_LAYOUT;
63 tmp.labelstring.erase();
64 tmp.labelwidthstring.erase();
65 tmp.start_of_appendix = false;
70 bool ParagraphParameters::sameLayout(ParagraphParameters const & pp) const
72 return param->align == pp.param->align &&
73 param->line_bottom == pp.param->line_bottom &&
74 param->pagebreak_bottom == pp.param->pagebreak_bottom &&
75 param->added_space_bottom == pp.param->added_space_bottom &&
77 param->line_top == pp.param->line_top &&
78 param->pagebreak_top == pp.param->pagebreak_top &&
79 param->added_space_top == pp.param->added_space_top &&
80 param->spacing == pp.param->spacing &&
81 param->noindent == pp.param->noindent &&
82 param->depth == pp.param->depth;
86 void ParagraphParameters::set_from_struct(ParameterStruct const & ps)
88 // get new param from container with tmp as template
89 param = container.get(ps);
93 VSpace const & ParagraphParameters::spaceTop() const
95 return param->added_space_top;
99 void ParagraphParameters::spaceTop(VSpace const & vs)
101 ParameterStruct tmp(*param);
102 tmp.added_space_top = vs;
103 set_from_struct(tmp);
107 VSpace const & ParagraphParameters::spaceBottom() const
109 return param->added_space_bottom;
113 void ParagraphParameters::spaceBottom(VSpace const & vs)
115 ParameterStruct tmp(*param);
116 tmp.added_space_bottom = vs;
117 set_from_struct(tmp);
121 Spacing const & ParagraphParameters::spacing() const
123 return param->spacing;
127 void ParagraphParameters::spacing(Spacing const & s)
129 ParameterStruct tmp(*param);
131 set_from_struct(tmp);
135 bool ParagraphParameters::noindent() const
137 return param->noindent;
141 void ParagraphParameters::noindent(bool ni)
143 ParameterStruct tmp(*param);
145 set_from_struct(tmp);
149 bool ParagraphParameters::lineTop() const
151 return param->line_top;
155 void ParagraphParameters::lineTop(bool lt)
157 ParameterStruct tmp(*param);
159 set_from_struct(tmp);
163 bool ParagraphParameters::lineBottom() const
165 return param->line_bottom;
169 void ParagraphParameters::lineBottom(bool lb)
171 ParameterStruct tmp(*param);
172 tmp.line_bottom = lb;
173 set_from_struct(tmp);
177 bool ParagraphParameters::pagebreakTop() const
179 return param->pagebreak_top;
183 void ParagraphParameters::pagebreakTop(bool pbt)
185 ParameterStruct tmp(*param);
186 tmp.pagebreak_top = pbt;
187 set_from_struct(tmp);
191 bool ParagraphParameters::pagebreakBottom() const
193 return param->pagebreak_bottom;
197 void ParagraphParameters::pagebreakBottom(bool pbb)
199 ParameterStruct tmp(*param);
200 tmp.pagebreak_bottom = pbb;
201 set_from_struct(tmp);
205 LyXAlignment ParagraphParameters::align() const
211 void ParagraphParameters::align(LyXAlignment la)
213 ParameterStruct tmp(*param);
215 set_from_struct(tmp);
219 void ParagraphParameters::depth(depth_type d)
221 ParameterStruct tmp(*param);
223 set_from_struct(tmp);
227 bool ParagraphParameters::startOfAppendix() const
229 return param->start_of_appendix;
233 void ParagraphParameters::startOfAppendix(bool soa)
235 ParameterStruct tmp(*param);
236 tmp.start_of_appendix = soa;
237 set_from_struct(tmp);
241 bool ParagraphParameters::appendix() const
243 return param->appendix;
247 void ParagraphParameters::appendix(bool a)
249 ParameterStruct tmp(*param);
251 set_from_struct(tmp);
255 string const & ParagraphParameters::labelString() const
257 return param->labelstring;
261 void ParagraphParameters::labelString(string const & ls)
263 ParameterStruct tmp(*param);
264 tmp.labelstring = ls;
265 set_from_struct(tmp);
269 string const & ParagraphParameters::labelWidthString() const
271 return param->labelwidthstring;
275 void ParagraphParameters::labelWidthString(string const & lws)
277 ParameterStruct tmp(*param);
278 tmp.labelwidthstring = lws;
279 set_from_struct(tmp);
283 LyXLength const & ParagraphParameters::leftIndent() const
285 return param->leftindent;
289 void ParagraphParameters::leftIndent(LyXLength const & li)
291 ParameterStruct tmp(*param);
293 set_from_struct(tmp);
297 void ParagraphParameters::read(LyXLex & lex)
301 string const token = lex.getString();
306 if (token[0] != '\\') {
307 lex.pushToken(token);
311 if (token == "\\noindent") {
313 } else if (token == "\\leftindent") {
315 LyXLength value(lex.getString());
317 } else if (token == "\\fill_top") {
318 spaceTop(VSpace(VSpace::VFILL));
319 } else if (token == "\\fill_bottom") {
320 spaceBottom(VSpace(VSpace::VFILL));
321 } else if (token == "\\line_top") {
323 } else if (token == "\\line_bottom") {
325 } else if (token == "\\pagebreak_top") {
327 } else if (token == "\\pagebreak_bottom") {
328 pagebreakBottom(true);
329 } else if (token == "\\start_of_appendix") {
330 startOfAppendix(true);
331 } else if (token == "\\paragraph_spacing") {
333 string const tmp = rtrim(lex.getString());
334 if (tmp == "single") {
335 spacing(Spacing(Spacing::Single));
336 } else if (tmp == "onehalf") {
337 spacing(Spacing(Spacing::Onehalf));
338 } else if (tmp == "double") {
339 spacing(Spacing(Spacing::Double));
340 } else if (tmp == "other") {
342 spacing(Spacing(Spacing::Other,
345 lex.printError("Unknown spacing token: '$$Token'");
347 } else if (token == "\\align") {
348 int tmpret = lex.findToken(string_align);
351 align(LyXAlignment(1 << tmpret));
352 } else if (token == "\\added_space_top") {
354 VSpace value = VSpace(lex.getString());
355 // only add the length when value > 0 or
357 if ((value.length().len().value() != 0) ||
359 (value.kind() != VSpace::LENGTH))
361 } else if (token == "\\added_space_bottom") {
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 == "\\labelwidthstring") {
372 labelWidthString(lex.getString());
374 lex.pushToken(token);
381 void ParagraphParameters::write(ostream & os) const
383 // Maybe some vertical spaces.
384 if (spaceTop().kind() != VSpace::NONE)
385 os << "\\added_space_top "
386 << spaceTop().asLyXCommand() << ' ';
387 if (spaceBottom().kind() != VSpace::NONE)
388 os << "\\added_space_bottom "
389 << spaceBottom().asLyXCommand() << ' ';
391 // Maybe the paragraph has special spacing
392 spacing().writeFile(os, true);
394 // The labelwidth string used in lists.
395 if (!labelWidthString().empty())
396 os << "\\labelwidthstring "
397 << labelWidthString() << '\n';
399 // Lines above or below?
403 os << "\\line_bottom ";
405 // Pagebreaks above or below?
407 os << "\\pagebreak_top ";
408 if (pagebreakBottom())
409 os << "\\pagebreak_bottom ";
411 // Start of appendix?
412 if (startOfAppendix())
413 os << "\\start_of_appendix ";
419 // Do we have a manual left indent?
420 if (!leftIndent().zero())
421 os << "\\leftindent " << leftIndent().asString()
425 if (align() != LYX_ALIGN_LAYOUT) {
428 case LYX_ALIGN_LEFT: h = 1; break;
429 case LYX_ALIGN_RIGHT: h = 2; break;
430 case LYX_ALIGN_CENTER: h = 3; break;
431 default: h = 0; break;
433 os << "\\align " << string_align[h] << ' ';
439 void setParagraphParams(BufferView & bv, string const & data)
441 istringstream is(STRCONV(data));
445 ParagraphParameters params;
448 LyXText * text = bv.getLyXText();
449 text->setParagraph(params.lineTop(),
451 params.pagebreakTop(),
452 params.pagebreakBottom(),
454 params.spaceBottom(),
457 params.labelWidthString(),
461 bv.owner()->message(_("Paragraph layout set"));
465 void params2string(Paragraph const & par, string & data)
468 ParagraphParameters params = par.params();
470 // This needs to be done separately
471 params.labelWidthString(par.getLabelWidthString());
474 LyXLayout_ptr const & layout = par.layout();
475 if (params.align() == LYX_ALIGN_LAYOUT)
476 params.align(layout->align);
481 // Is alignment possible
482 os << '\n' << "\\alignpossible " << layout->alignpossible << '\n';
484 /// set default alignment
485 os << "\\aligndefault " << layout->align << '\n';
487 /// is paragraph in inset
488 os << "\\ininset " << (par.inInset()?1:0) << '\n';
490 data = STRCONV(os.str());