3 #include "ParagraphParameters.h"
4 #include "ParameterStruct.h"
5 #include "tex-strings.h"
9 #include "BufferView.h"
12 #include "paragraph.h"
16 #include "frontends/LyXView.h"
18 #include "support/lstrings.h"
24 // Initialize static member var.
25 ShareContainer<ParameterStruct> ParagraphParameters::container;
27 ParagraphParameters::ParagraphParameters()
34 void ParagraphParameters::clear()
36 ParameterStruct tmp(*param);
38 tmp.line_bottom = false;
39 tmp.pagebreak_top = false;
40 tmp.pagebreak_bottom = false;
41 tmp.added_space_top = VSpace(VSpace::NONE);
42 tmp.added_space_bottom = VSpace(VSpace::NONE);
43 tmp.spacing.set(Spacing::Default);
44 tmp.align = LYX_ALIGN_LAYOUT;
47 tmp.labelstring.erase();
48 tmp.labelwidthstring.erase();
49 tmp.start_of_appendix = false;
54 bool ParagraphParameters::sameLayout(ParagraphParameters const & pp) const
56 return param->align == pp.param->align &&
57 param->line_bottom == pp.param->line_bottom &&
58 param->pagebreak_bottom == pp.param->pagebreak_bottom &&
59 param->added_space_bottom == pp.param->added_space_bottom &&
61 param->line_top == pp.param->line_top &&
62 param->pagebreak_top == pp.param->pagebreak_top &&
63 param->added_space_top == pp.param->added_space_top &&
64 param->spacing == pp.param->spacing &&
65 param->noindent == pp.param->noindent &&
66 param->depth == pp.param->depth;
70 void ParagraphParameters::set_from_struct(ParameterStruct const & ps)
72 // get new param from container with tmp as template
73 param = container.get(ps);
77 VSpace const & ParagraphParameters::spaceTop() const
79 return param->added_space_top;
83 void ParagraphParameters::spaceTop(VSpace const & vs)
85 ParameterStruct tmp(*param);
86 tmp.added_space_top = vs;
91 VSpace const & ParagraphParameters::spaceBottom() const
93 return param->added_space_bottom;
97 void ParagraphParameters::spaceBottom(VSpace const & vs)
99 ParameterStruct tmp(*param);
100 tmp.added_space_bottom = vs;
101 set_from_struct(tmp);
105 Spacing const & ParagraphParameters::spacing() const
107 return param->spacing;
111 void ParagraphParameters::spacing(Spacing const & s)
113 ParameterStruct tmp(*param);
115 set_from_struct(tmp);
119 bool ParagraphParameters::noindent() const
121 return param->noindent;
125 void ParagraphParameters::noindent(bool ni)
127 ParameterStruct tmp(*param);
129 set_from_struct(tmp);
133 bool ParagraphParameters::lineTop() const
135 return param->line_top;
139 void ParagraphParameters::lineTop(bool lt)
141 ParameterStruct tmp(*param);
143 set_from_struct(tmp);
147 bool ParagraphParameters::lineBottom() const
149 return param->line_bottom;
153 void ParagraphParameters::lineBottom(bool lb)
155 ParameterStruct tmp(*param);
156 tmp.line_bottom = lb;
157 set_from_struct(tmp);
161 bool ParagraphParameters::pagebreakTop() const
163 return param->pagebreak_top;
167 void ParagraphParameters::pagebreakTop(bool pbt)
169 ParameterStruct tmp(*param);
170 tmp.pagebreak_top = pbt;
171 set_from_struct(tmp);
175 bool ParagraphParameters::pagebreakBottom() const
177 return param->pagebreak_bottom;
181 void ParagraphParameters::pagebreakBottom(bool pbb)
183 ParameterStruct tmp(*param);
184 tmp.pagebreak_bottom = pbb;
185 set_from_struct(tmp);
189 LyXAlignment ParagraphParameters::align() const
195 void ParagraphParameters::align(LyXAlignment la)
197 ParameterStruct tmp(*param);
199 set_from_struct(tmp);
203 void ParagraphParameters::depth(depth_type d)
205 ParameterStruct tmp(*param);
207 set_from_struct(tmp);
211 bool ParagraphParameters::startOfAppendix() const
213 return param->start_of_appendix;
217 void ParagraphParameters::startOfAppendix(bool soa)
219 ParameterStruct tmp(*param);
220 tmp.start_of_appendix = soa;
221 set_from_struct(tmp);
225 bool ParagraphParameters::appendix() const
227 return param->appendix;
231 void ParagraphParameters::appendix(bool a)
233 ParameterStruct tmp(*param);
235 set_from_struct(tmp);
239 string const & ParagraphParameters::labelString() const
241 return param->labelstring;
245 void ParagraphParameters::labelString(string const & ls)
247 ParameterStruct tmp(*param);
248 tmp.labelstring = ls;
249 set_from_struct(tmp);
253 string const & ParagraphParameters::labelWidthString() const
255 return param->labelwidthstring;
259 void ParagraphParameters::labelWidthString(string const & lws)
261 ParameterStruct tmp(*param);
262 tmp.labelwidthstring = lws;
263 set_from_struct(tmp);
267 LyXLength const & ParagraphParameters::leftIndent() const
269 return param->leftindent;
273 void ParagraphParameters::leftIndent(LyXLength const & li)
275 ParameterStruct tmp(*param);
277 set_from_struct(tmp);
281 void ParagraphParameters::read(LyXLex & lex)
285 string const token = lex.getString();
290 if (token[0] != '\\') {
291 lex.pushToken(token);
295 if (token == "\\noindent") {
297 } else if (token == "\\leftindent") {
299 LyXLength value(lex.getString());
301 } else if (token == "\\fill_top") {
302 spaceTop(VSpace(VSpace::VFILL));
303 } else if (token == "\\fill_bottom") {
304 spaceBottom(VSpace(VSpace::VFILL));
305 } else if (token == "\\line_top") {
307 } else if (token == "\\line_bottom") {
309 } else if (token == "\\pagebreak_top") {
311 } else if (token == "\\pagebreak_bottom") {
312 pagebreakBottom(true);
313 } else if (token == "\\start_of_appendix") {
314 startOfAppendix(true);
315 } else if (token == "\\paragraph_spacing") {
317 string const tmp = rtrim(lex.getString());
318 if (tmp == "single") {
319 spacing(Spacing(Spacing::Single));
320 } else if (tmp == "onehalf") {
321 spacing(Spacing(Spacing::Onehalf));
322 } else if (tmp == "double") {
323 spacing(Spacing(Spacing::Double));
324 } else if (tmp == "other") {
326 spacing(Spacing(Spacing::Other,
329 lex.printError("Unknown spacing token: '$$Token'");
331 } else if (token == "\\align") {
332 int tmpret = lex.findToken(string_align);
335 align(LyXAlignment(1 << tmpret));
336 } else if (token == "\\added_space_top") {
338 VSpace value = VSpace(lex.getString());
339 // only add the length when value > 0 or
341 if ((value.length().len().value() != 0) ||
343 (value.kind() != VSpace::LENGTH))
345 } else if (token == "\\added_space_bottom") {
347 VSpace value = VSpace(lex.getString());
348 // only add the length when value > 0 or
350 if ((value.length().len().value() != 0) ||
352 (value.kind() != VSpace::LENGTH))
354 } else if (token == "\\labelwidthstring") {
356 labelWidthString(lex.getString());
358 lex.pushToken(token);
365 void ParagraphParameters::write(ostream & os) const
367 // Maybe some vertical spaces.
368 if (spaceTop().kind() != VSpace::NONE)
369 os << "\\added_space_top "
370 << spaceTop().asLyXCommand() << ' ';
371 if (spaceBottom().kind() != VSpace::NONE)
372 os << "\\added_space_bottom "
373 << spaceBottom().asLyXCommand() << ' ';
375 // Maybe the paragraph has special spacing
376 spacing().writeFile(os, true);
378 // The labelwidth string used in lists.
379 if (!labelWidthString().empty())
380 os << "\\labelwidthstring "
381 << labelWidthString() << '\n';
383 // Lines above or below?
387 os << "\\line_bottom ";
389 // Pagebreaks above or below?
391 os << "\\pagebreak_top ";
392 if (pagebreakBottom())
393 os << "\\pagebreak_bottom ";
395 // Start of appendix?
396 if (startOfAppendix())
397 os << "\\start_of_appendix ";
403 // Do we have a manual left indent?
404 if (!leftIndent().zero())
405 os << "\\leftindent " << leftIndent().asString()
409 if (align() != LYX_ALIGN_LAYOUT) {
412 case LYX_ALIGN_LEFT: h = 1; break;
413 case LYX_ALIGN_RIGHT: h = 2; break;
414 case LYX_ALIGN_CENTER: h = 3; break;
415 default: h = 0; break;
417 os << "\\align " << string_align[h] << ' ';
422 void setParagraphParams(BufferView & bv, string const & data)
424 istringstream is(STRCONV(data));
428 ParagraphParameters params;
431 LyXText * text = bv.getLyXText();
432 text->setParagraph(params.lineTop(),
434 params.pagebreakTop(),
435 params.pagebreakBottom(),
437 params.spaceBottom(),
440 params.labelWidthString(),
443 // Actually apply these settings
444 bv.update(text, BufferView::SELECT);
446 bv.owner()->message(_("Paragraph layout set"));
450 void params2string(Paragraph const & par, string & data)
453 ParagraphParameters params = par.params();
455 // This needs to be done separately
456 params.labelWidthString(par.getLabelWidthString());
459 LyXLayout_ptr const & layout = par.layout();
460 if (params.align() == LYX_ALIGN_LAYOUT)
461 params.align(layout->align);
466 // Is alignment possible
467 os << '\n' << "\\alignpossible " << layout->alignpossible << '\n';
469 /// set default alignment
470 os << "\\aligndefault " << layout->align << '\n';
472 /// is paragraph in inset
473 os << "\\ininset " << par.inInset() << '\n';
475 data = STRCONV(os.str());