3 #include "ParagraphParameters.h"
4 #include "ParameterStruct.h"
5 #include "tex-strings.h"
9 #include "BufferView.h"
11 #include "paragraph.h"
13 #include "frontends/LyXView.h"
15 #include "support/lstrings.h"
21 // Initialize static member var.
22 ShareContainer<ParameterStruct> ParagraphParameters::container;
24 ParagraphParameters::ParagraphParameters()
31 void ParagraphParameters::clear()
33 ParameterStruct tmp(*param);
35 tmp.line_bottom = false;
36 tmp.pagebreak_top = false;
37 tmp.pagebreak_bottom = false;
38 tmp.added_space_top = VSpace(VSpace::NONE);
39 tmp.added_space_bottom = VSpace(VSpace::NONE);
40 tmp.spacing.set(Spacing::Default);
41 tmp.align = LYX_ALIGN_LAYOUT;
44 tmp.labelstring.erase();
45 tmp.labelwidthstring.erase();
46 tmp.start_of_appendix = false;
51 bool ParagraphParameters::sameLayout(ParagraphParameters const & pp) const
53 return param->align == pp.param->align &&
54 param->line_bottom == pp.param->line_bottom &&
55 param->pagebreak_bottom == pp.param->pagebreak_bottom &&
56 param->added_space_bottom == pp.param->added_space_bottom &&
58 param->line_top == pp.param->line_top &&
59 param->pagebreak_top == pp.param->pagebreak_top &&
60 param->added_space_top == pp.param->added_space_top &&
61 param->spacing == pp.param->spacing &&
62 param->noindent == pp.param->noindent &&
63 param->depth == pp.param->depth;
67 void ParagraphParameters::set_from_struct(ParameterStruct const & ps)
69 // get new param from container with tmp as template
70 param = container.get(ps);
74 VSpace const & ParagraphParameters::spaceTop() const
76 return param->added_space_top;
80 void ParagraphParameters::spaceTop(VSpace const & vs)
82 ParameterStruct tmp(*param);
83 tmp.added_space_top = vs;
88 VSpace const & ParagraphParameters::spaceBottom() const
90 return param->added_space_bottom;
94 void ParagraphParameters::spaceBottom(VSpace const & vs)
96 ParameterStruct tmp(*param);
97 tmp.added_space_bottom = vs;
102 Spacing const & ParagraphParameters::spacing() const
104 return param->spacing;
108 void ParagraphParameters::spacing(Spacing const & s)
110 ParameterStruct tmp(*param);
112 set_from_struct(tmp);
116 bool ParagraphParameters::noindent() const
118 return param->noindent;
122 void ParagraphParameters::noindent(bool ni)
124 ParameterStruct tmp(*param);
126 set_from_struct(tmp);
130 bool ParagraphParameters::lineTop() const
132 return param->line_top;
136 void ParagraphParameters::lineTop(bool lt)
138 ParameterStruct tmp(*param);
140 set_from_struct(tmp);
144 bool ParagraphParameters::lineBottom() const
146 return param->line_bottom;
150 void ParagraphParameters::lineBottom(bool lb)
152 ParameterStruct tmp(*param);
153 tmp.line_bottom = lb;
154 set_from_struct(tmp);
158 bool ParagraphParameters::pagebreakTop() const
160 return param->pagebreak_top;
164 void ParagraphParameters::pagebreakTop(bool pbt)
166 ParameterStruct tmp(*param);
167 tmp.pagebreak_top = pbt;
168 set_from_struct(tmp);
172 bool ParagraphParameters::pagebreakBottom() const
174 return param->pagebreak_bottom;
178 void ParagraphParameters::pagebreakBottom(bool pbb)
180 ParameterStruct tmp(*param);
181 tmp.pagebreak_bottom = pbb;
182 set_from_struct(tmp);
186 LyXAlignment ParagraphParameters::align() const
192 void ParagraphParameters::align(LyXAlignment la)
194 ParameterStruct tmp(*param);
196 set_from_struct(tmp);
200 void ParagraphParameters::depth(depth_type d)
202 ParameterStruct tmp(*param);
204 set_from_struct(tmp);
208 bool ParagraphParameters::startOfAppendix() const
210 return param->start_of_appendix;
214 void ParagraphParameters::startOfAppendix(bool soa)
216 ParameterStruct tmp(*param);
217 tmp.start_of_appendix = soa;
218 set_from_struct(tmp);
222 bool ParagraphParameters::appendix() const
224 return param->appendix;
228 void ParagraphParameters::appendix(bool a)
230 ParameterStruct tmp(*param);
232 set_from_struct(tmp);
236 string const & ParagraphParameters::labelString() const
238 return param->labelstring;
242 void ParagraphParameters::labelString(string const & ls)
244 ParameterStruct tmp(*param);
245 tmp.labelstring = ls;
246 set_from_struct(tmp);
250 string const & ParagraphParameters::labelWidthString() const
252 return param->labelwidthstring;
256 void ParagraphParameters::labelWidthString(string const & lws)
258 ParameterStruct tmp(*param);
259 tmp.labelwidthstring = lws;
260 set_from_struct(tmp);
264 LyXLength const & ParagraphParameters::leftIndent() const
266 return param->leftindent;
270 void ParagraphParameters::leftIndent(LyXLength const & li)
272 ParameterStruct tmp(*param);
274 set_from_struct(tmp);
278 void ParagraphParameters::read(LyXLex & lex)
282 string const token = lex.getString();
287 if (token[0] != '\\') {
288 lex.pushToken(token);
292 if (token == "\\noindent") {
294 } else if (token == "\\leftindent") {
296 LyXLength value(lex.getString());
298 } else if (token == "\\fill_top") {
299 spaceTop(VSpace(VSpace::VFILL));
300 } else if (token == "\\fill_bottom") {
301 spaceBottom(VSpace(VSpace::VFILL));
302 } else if (token == "\\line_top") {
304 } else if (token == "\\line_bottom") {
306 } else if (token == "\\pagebreak_top") {
308 } else if (token == "\\pagebreak_bottom") {
309 pagebreakBottom(true);
310 } else if (token == "\\start_of_appendix") {
311 startOfAppendix(true);
312 } else if (token == "\\paragraph_spacing") {
314 string const tmp = rtrim(lex.getString());
315 if (tmp == "single") {
316 spacing(Spacing(Spacing::Single));
317 } else if (tmp == "onehalf") {
318 spacing(Spacing(Spacing::Onehalf));
319 } else if (tmp == "double") {
320 spacing(Spacing(Spacing::Double));
321 } else if (tmp == "other") {
323 spacing(Spacing(Spacing::Other,
326 lex.printError("Unknown spacing token: '$$Token'");
328 } else if (token == "\\align") {
329 int tmpret = lex.findToken(string_align);
332 align(LyXAlignment(1 << tmpret));
333 } else if (token == "\\added_space_top") {
335 VSpace value = VSpace(lex.getString());
336 // only add the length when value > 0 or
338 if ((value.length().len().value() != 0) ||
340 (value.kind() != VSpace::LENGTH))
342 } else if (token == "\\added_space_bottom") {
344 VSpace value = VSpace(lex.getString());
345 // only add the length when value > 0 or
347 if ((value.length().len().value() != 0) ||
349 (value.kind() != VSpace::LENGTH))
351 } else if (token == "\\labelwidthstring") {
353 labelWidthString(lex.getString());
355 lex.pushToken(token);
362 void ParagraphParameters::write(ostream & os) const
364 // Maybe some vertical spaces.
365 if (spaceTop().kind() != VSpace::NONE)
366 os << "\\added_space_top "
367 << spaceTop().asLyXCommand() << ' ';
368 if (spaceBottom().kind() != VSpace::NONE)
369 os << "\\added_space_bottom "
370 << spaceBottom().asLyXCommand() << ' ';
372 // Maybe the paragraph has special spacing
373 spacing().writeFile(os, true);
375 // The labelwidth string used in lists.
376 if (!labelWidthString().empty())
377 os << "\\labelwidthstring "
378 << labelWidthString() << '\n';
380 // Lines above or below?
384 os << "\\line_bottom ";
386 // Pagebreaks above or below?
388 os << "\\pagebreak_top ";
389 if (pagebreakBottom())
390 os << "\\pagebreak_bottom ";
392 // Start of appendix?
393 if (startOfAppendix())
394 os << "\\start_of_appendix ";
400 // Do we have a manual left indent?
401 if (!leftIndent().zero())
402 os << "\\leftindent " << leftIndent().asString()
406 if (align() != LYX_ALIGN_LAYOUT) {
409 case LYX_ALIGN_LEFT: h = 1; break;
410 case LYX_ALIGN_RIGHT: h = 2; break;
411 case LYX_ALIGN_CENTER: h = 3; break;
412 default: h = 0; break;
414 os << "\\align " << string_align[h] << ' ';
419 void setParagraphParams(BufferView & bv, string const & data)
421 istringstream is(data);
425 ParagraphParameters params;
428 LyXText * text = bv.getLyXText();
429 text->setParagraph(&bv,
432 params.pagebreakTop(),
433 params.pagebreakBottom(),
435 params.spaceBottom(),
438 params.labelWidthString(),
441 // Actually apply these settings
447 bv.buffer()->markDirty();
449 bv.owner()->message(_("Paragraph layout set"));
453 void params2string(Paragraph const & par, string & data)
456 ParagraphParameters params = par.params();
458 // This needs to be done separately
459 params.labelWidthString(par.getLabelWidthString());
462 LyXLayout_ptr const & layout = par.layout();
463 if (params.align() == LYX_ALIGN_LAYOUT)
464 params.align(layout->align);
469 // Is alignment possible
470 os << '\n' << "\\alignpossible " << layout->alignpossible << '\n';
472 /// set default alignment
473 os << "\\aligndefault " << layout->align << '\n';
475 /// is paragraph in inset
476 os << "\\ininset " << par.inInset() << '\n';