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"
22 using namespace lyx::support;
26 // Initialize static member var.
27 ShareContainer<ParameterStruct> ParagraphParameters::container;
29 ParagraphParameters::ParagraphParameters()
36 void ParagraphParameters::clear()
38 ParameterStruct tmp(*param);
40 tmp.line_bottom = false;
41 tmp.pagebreak_top = false;
42 tmp.pagebreak_bottom = false;
43 tmp.added_space_top = VSpace(VSpace::NONE);
44 tmp.added_space_bottom = VSpace(VSpace::NONE);
45 tmp.spacing.set(Spacing::Default);
46 tmp.align = LYX_ALIGN_LAYOUT;
49 tmp.labelstring.erase();
50 tmp.labelwidthstring.erase();
51 tmp.start_of_appendix = false;
56 bool ParagraphParameters::sameLayout(ParagraphParameters const & pp) const
58 return param->align == pp.param->align &&
59 param->line_bottom == pp.param->line_bottom &&
60 param->pagebreak_bottom == pp.param->pagebreak_bottom &&
61 param->added_space_bottom == pp.param->added_space_bottom &&
63 param->line_top == pp.param->line_top &&
64 param->pagebreak_top == pp.param->pagebreak_top &&
65 param->added_space_top == pp.param->added_space_top &&
66 param->spacing == pp.param->spacing &&
67 param->noindent == pp.param->noindent &&
68 param->depth == pp.param->depth;
72 void ParagraphParameters::set_from_struct(ParameterStruct const & ps)
74 // get new param from container with tmp as template
75 param = container.get(ps);
79 VSpace const & ParagraphParameters::spaceTop() const
81 return param->added_space_top;
85 void ParagraphParameters::spaceTop(VSpace const & vs)
87 ParameterStruct tmp(*param);
88 tmp.added_space_top = vs;
93 VSpace const & ParagraphParameters::spaceBottom() const
95 return param->added_space_bottom;
99 void ParagraphParameters::spaceBottom(VSpace const & vs)
101 ParameterStruct tmp(*param);
102 tmp.added_space_bottom = vs;
103 set_from_struct(tmp);
107 Spacing const & ParagraphParameters::spacing() const
109 return param->spacing;
113 void ParagraphParameters::spacing(Spacing const & s)
115 ParameterStruct tmp(*param);
117 set_from_struct(tmp);
121 bool ParagraphParameters::noindent() const
123 return param->noindent;
127 void ParagraphParameters::noindent(bool ni)
129 ParameterStruct tmp(*param);
131 set_from_struct(tmp);
135 bool ParagraphParameters::lineTop() const
137 return param->line_top;
141 void ParagraphParameters::lineTop(bool lt)
143 ParameterStruct tmp(*param);
145 set_from_struct(tmp);
149 bool ParagraphParameters::lineBottom() const
151 return param->line_bottom;
155 void ParagraphParameters::lineBottom(bool lb)
157 ParameterStruct tmp(*param);
158 tmp.line_bottom = lb;
159 set_from_struct(tmp);
163 bool ParagraphParameters::pagebreakTop() const
165 return param->pagebreak_top;
169 void ParagraphParameters::pagebreakTop(bool pbt)
171 ParameterStruct tmp(*param);
172 tmp.pagebreak_top = pbt;
173 set_from_struct(tmp);
177 bool ParagraphParameters::pagebreakBottom() const
179 return param->pagebreak_bottom;
183 void ParagraphParameters::pagebreakBottom(bool pbb)
185 ParameterStruct tmp(*param);
186 tmp.pagebreak_bottom = pbb;
187 set_from_struct(tmp);
191 LyXAlignment ParagraphParameters::align() const
197 void ParagraphParameters::align(LyXAlignment la)
199 ParameterStruct tmp(*param);
201 set_from_struct(tmp);
205 void ParagraphParameters::depth(depth_type d)
207 ParameterStruct tmp(*param);
209 set_from_struct(tmp);
213 bool ParagraphParameters::startOfAppendix() const
215 return param->start_of_appendix;
219 void ParagraphParameters::startOfAppendix(bool soa)
221 ParameterStruct tmp(*param);
222 tmp.start_of_appendix = soa;
223 set_from_struct(tmp);
227 bool ParagraphParameters::appendix() const
229 return param->appendix;
233 void ParagraphParameters::appendix(bool a)
235 ParameterStruct tmp(*param);
237 set_from_struct(tmp);
241 string const & ParagraphParameters::labelString() const
243 return param->labelstring;
247 void ParagraphParameters::labelString(string const & ls)
249 ParameterStruct tmp(*param);
250 tmp.labelstring = ls;
251 set_from_struct(tmp);
255 string const & ParagraphParameters::labelWidthString() const
257 return param->labelwidthstring;
261 void ParagraphParameters::labelWidthString(string const & lws)
263 ParameterStruct tmp(*param);
264 tmp.labelwidthstring = lws;
265 set_from_struct(tmp);
269 LyXLength const & ParagraphParameters::leftIndent() const
271 return param->leftindent;
275 void ParagraphParameters::leftIndent(LyXLength const & li)
277 ParameterStruct tmp(*param);
279 set_from_struct(tmp);
283 void ParagraphParameters::read(LyXLex & lex)
287 string const token = lex.getString();
292 if (token[0] != '\\') {
293 lex.pushToken(token);
297 if (token == "\\noindent") {
299 } else if (token == "\\leftindent") {
301 LyXLength value(lex.getString());
303 } else if (token == "\\fill_top") {
304 spaceTop(VSpace(VSpace::VFILL));
305 } else if (token == "\\fill_bottom") {
306 spaceBottom(VSpace(VSpace::VFILL));
307 } else if (token == "\\line_top") {
309 } else if (token == "\\line_bottom") {
311 } else if (token == "\\pagebreak_top") {
313 } else if (token == "\\pagebreak_bottom") {
314 pagebreakBottom(true);
315 } else if (token == "\\start_of_appendix") {
316 startOfAppendix(true);
317 } else if (token == "\\paragraph_spacing") {
319 string const tmp = rtrim(lex.getString());
320 if (tmp == "single") {
321 spacing(Spacing(Spacing::Single));
322 } else if (tmp == "onehalf") {
323 spacing(Spacing(Spacing::Onehalf));
324 } else if (tmp == "double") {
325 spacing(Spacing(Spacing::Double));
326 } else if (tmp == "other") {
328 spacing(Spacing(Spacing::Other,
331 lex.printError("Unknown spacing token: '$$Token'");
333 } else if (token == "\\align") {
334 int tmpret = lex.findToken(string_align);
337 align(LyXAlignment(1 << tmpret));
338 } else if (token == "\\added_space_top") {
340 VSpace value = VSpace(lex.getString());
341 // only add the length when value > 0 or
343 if ((value.length().len().value() != 0) ||
345 (value.kind() != VSpace::LENGTH))
347 } else if (token == "\\added_space_bottom") {
349 VSpace value = VSpace(lex.getString());
350 // only add the length when value > 0 or
352 if ((value.length().len().value() != 0) ||
354 (value.kind() != VSpace::LENGTH))
356 } else if (token == "\\labelwidthstring") {
358 labelWidthString(lex.getString());
360 lex.pushToken(token);
367 void ParagraphParameters::write(ostream & os) const
369 // Maybe some vertical spaces.
370 if (spaceTop().kind() != VSpace::NONE)
371 os << "\\added_space_top "
372 << spaceTop().asLyXCommand() << ' ';
373 if (spaceBottom().kind() != VSpace::NONE)
374 os << "\\added_space_bottom "
375 << spaceBottom().asLyXCommand() << ' ';
377 // Maybe the paragraph has special spacing
378 spacing().writeFile(os, true);
380 // The labelwidth string used in lists.
381 if (!labelWidthString().empty())
382 os << "\\labelwidthstring "
383 << labelWidthString() << '\n';
385 // Lines above or below?
389 os << "\\line_bottom ";
391 // Pagebreaks above or below?
393 os << "\\pagebreak_top ";
394 if (pagebreakBottom())
395 os << "\\pagebreak_bottom ";
397 // Start of appendix?
398 if (startOfAppendix())
399 os << "\\start_of_appendix ";
405 // Do we have a manual left indent?
406 if (!leftIndent().zero())
407 os << "\\leftindent " << leftIndent().asString()
411 if (align() != LYX_ALIGN_LAYOUT) {
414 case LYX_ALIGN_LEFT: h = 1; break;
415 case LYX_ALIGN_RIGHT: h = 2; break;
416 case LYX_ALIGN_CENTER: h = 3; break;
417 default: h = 0; break;
419 os << "\\align " << string_align[h] << ' ';
425 void setParagraphParams(BufferView & bv, string const & data)
427 istringstream is(STRCONV(data));
431 ParagraphParameters params;
434 LyXText * text = bv.getLyXText();
435 text->setParagraph(params.lineTop(),
437 params.pagebreakTop(),
438 params.pagebreakBottom(),
440 params.spaceBottom(),
443 params.labelWidthString(),
447 bv.owner()->message(_("Paragraph layout set"));
451 void params2string(Paragraph const & par, string & data)
454 ParagraphParameters params = par.params();
456 // This needs to be done separately
457 params.labelWidthString(par.getLabelWidthString());
460 LyXLayout_ptr const & layout = par.layout();
461 if (params.align() == LYX_ALIGN_LAYOUT)
462 params.align(layout->align);
467 // Is alignment possible
468 os << '\n' << "\\alignpossible " << layout->alignpossible << '\n';
470 /// set default alignment
471 os << "\\aligndefault " << layout->align << '\n';
473 /// is paragraph in inset
474 os << "\\ininset " << (par.inInset()?1:0) << '\n';
476 data = STRCONV(os.str());