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"
32 #include "support/std_sstream.h"
34 using lyx::support::rtrim;
36 using std::istringstream;
38 using std::ostringstream;
42 // Initialize static member var.
43 ShareContainer<ParameterStruct> ParagraphParameters::container;
45 ParagraphParameters::ParagraphParameters()
52 void ParagraphParameters::clear()
54 ParameterStruct tmp(*param);
55 tmp.added_space_top = VSpace(VSpace::NONE);
56 tmp.added_space_bottom = VSpace(VSpace::NONE);
57 tmp.spacing.set(Spacing::Default);
58 tmp.align = LYX_ALIGN_LAYOUT;
61 tmp.labelstring.erase();
62 tmp.labelwidthstring.erase();
63 tmp.start_of_appendix = false;
68 ParagraphParameters::depth_type ParagraphParameters::depth() const
74 bool ParagraphParameters::sameLayout(ParagraphParameters const & pp) const
76 return param->align == pp.param->align &&
77 param->added_space_bottom == pp.param->added_space_bottom &&
78 param->added_space_top == pp.param->added_space_top &&
79 param->spacing == pp.param->spacing &&
80 param->noindent == pp.param->noindent &&
81 param->depth == pp.param->depth;
85 void ParagraphParameters::set_from_struct(ParameterStruct const & ps)
87 // get new param from container with tmp as template
88 param = container.get(ps);
92 VSpace const & ParagraphParameters::spaceTop() const
94 return param->added_space_top;
98 void ParagraphParameters::spaceTop(VSpace const & vs)
100 ParameterStruct tmp(*param);
101 tmp.added_space_top = vs;
102 set_from_struct(tmp);
106 VSpace const & ParagraphParameters::spaceBottom() const
108 return param->added_space_bottom;
112 void ParagraphParameters::spaceBottom(VSpace const & vs)
114 ParameterStruct tmp(*param);
115 tmp.added_space_bottom = vs;
116 set_from_struct(tmp);
120 Spacing const & ParagraphParameters::spacing() const
122 return param->spacing;
126 void ParagraphParameters::spacing(Spacing const & s)
128 ParameterStruct tmp(*param);
130 set_from_struct(tmp);
134 bool ParagraphParameters::noindent() const
136 return param->noindent;
140 void ParagraphParameters::noindent(bool ni)
142 ParameterStruct tmp(*param);
144 set_from_struct(tmp);
148 LyXAlignment ParagraphParameters::align() const
154 void ParagraphParameters::align(LyXAlignment la)
156 ParameterStruct tmp(*param);
158 set_from_struct(tmp);
162 void ParagraphParameters::depth(depth_type d)
164 ParameterStruct tmp(*param);
166 set_from_struct(tmp);
170 bool ParagraphParameters::startOfAppendix() const
172 return param->start_of_appendix;
176 void ParagraphParameters::startOfAppendix(bool soa)
178 ParameterStruct tmp(*param);
179 tmp.start_of_appendix = soa;
180 set_from_struct(tmp);
184 bool ParagraphParameters::appendix() const
186 return param->appendix;
190 void ParagraphParameters::appendix(bool a)
192 ParameterStruct tmp(*param);
194 set_from_struct(tmp);
198 string const & ParagraphParameters::labelString() const
200 return param->labelstring;
204 void ParagraphParameters::labelString(string const & ls)
206 ParameterStruct tmp(*param);
207 tmp.labelstring = ls;
208 set_from_struct(tmp);
212 string const & ParagraphParameters::labelWidthString() const
214 return param->labelwidthstring;
218 void ParagraphParameters::labelWidthString(string const & lws)
220 ParameterStruct tmp(*param);
221 tmp.labelwidthstring = lws;
222 set_from_struct(tmp);
226 LyXLength const & ParagraphParameters::leftIndent() const
228 return param->leftindent;
232 void ParagraphParameters::leftIndent(LyXLength const & li)
234 ParameterStruct tmp(*param);
236 set_from_struct(tmp);
240 void ParagraphParameters::read(LyXLex & lex)
244 string const token = lex.getString();
249 if (token[0] != '\\') {
250 lex.pushToken(token);
254 if (token == "\\noindent") {
256 } else if (token == "\\leftindent") {
258 LyXLength value(lex.getString());
260 } else if (token == "\\fill_top") {
261 spaceTop(VSpace(VSpace::VFILL));
262 } else if (token == "\\fill_bottom") {
263 spaceBottom(VSpace(VSpace::VFILL));
264 } else if (token == "\\start_of_appendix") {
265 startOfAppendix(true);
266 } else if (token == "\\paragraph_spacing") {
268 string const tmp = rtrim(lex.getString());
269 if (tmp == "single") {
270 spacing(Spacing(Spacing::Single));
271 } else if (tmp == "onehalf") {
272 spacing(Spacing(Spacing::Onehalf));
273 } else if (tmp == "double") {
274 spacing(Spacing(Spacing::Double));
275 } else if (tmp == "other") {
277 spacing(Spacing(Spacing::Other,
280 lex.printError("Unknown spacing token: '$$Token'");
282 } else if (token == "\\align") {
283 int tmpret = lex.findToken(string_align);
286 align(LyXAlignment(1 << tmpret));
287 } else if (token == "\\added_space_top") {
289 VSpace value = VSpace(lex.getString());
290 // only add the length when value > 0 or
292 if ((value.length().len().value() != 0) ||
294 (value.kind() != VSpace::LENGTH))
296 } else if (token == "\\added_space_bottom") {
298 VSpace value = VSpace(lex.getString());
299 // only add the length when value > 0 or
301 if ((value.length().len().value() != 0) ||
303 (value.kind() != VSpace::LENGTH))
305 } else if (token == "\\labelwidthstring") {
307 labelWidthString(lex.getString());
309 lex.pushToken(token);
316 void ParagraphParameters::write(ostream & os) const
318 // Maybe some vertical spaces.
319 if (spaceTop().kind() != VSpace::NONE)
320 os << "\\added_space_top "
321 << spaceTop().asLyXCommand() << ' ';
322 if (spaceBottom().kind() != VSpace::NONE)
323 os << "\\added_space_bottom "
324 << spaceBottom().asLyXCommand() << ' ';
326 // Maybe the paragraph has special spacing
327 spacing().writeFile(os, true);
329 // The labelwidth string used in lists.
330 if (!labelWidthString().empty())
331 os << "\\labelwidthstring "
332 << labelWidthString() << '\n';
334 // Start of appendix?
335 if (startOfAppendix())
336 os << "\\start_of_appendix ";
342 // Do we have a manual left indent?
343 if (!leftIndent().zero())
344 os << "\\leftindent " << leftIndent().asString()
348 if (align() != LYX_ALIGN_LAYOUT) {
351 case LYX_ALIGN_LEFT: h = 1; break;
352 case LYX_ALIGN_RIGHT: h = 2; break;
353 case LYX_ALIGN_CENTER: h = 3; break;
354 default: h = 0; break;
356 os << "\\align " << string_align[h] << ' ';
362 void setParagraphParams(BufferView & bv, string const & data)
364 istringstream is(data);
368 ParagraphParameters params;
371 LyXText * text = bv.getLyXText();
374 params.spaceBottom(),
377 params.labelWidthString(),
381 bv.owner()->message(_("Paragraph layout set"));
385 void params2string(Paragraph const & par, string & data)
388 ParagraphParameters params = par.params();
390 // This needs to be done separately
391 params.labelWidthString(par.getLabelWidthString());
394 LyXLayout_ptr const & layout = par.layout();
395 if (params.align() == LYX_ALIGN_LAYOUT)
396 params.align(layout->align);
401 // Is alignment possible
402 os << '\n' << "\\alignpossible " << layout->alignpossible << '\n';
404 /// set default alignment
405 os << "\\aligndefault " << layout->align << '\n';
407 /// is paragraph in inset
408 os << "\\ininset " << (par.inInset()?1:0) << '\n';