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"
33 #include "support/std_sstream.h"
35 using lyx::support::rtrim;
37 using std::istringstream;
39 using std::ostringstream;
43 // Initialize static member var.
44 ShareContainer<ParameterStruct> ParagraphParameters::container;
46 ParagraphParameters::ParagraphParameters()
53 void ParagraphParameters::clear()
55 ParameterStruct tmp(*param);
56 tmp.added_space_top = VSpace(VSpace::NONE);
57 tmp.added_space_bottom = VSpace(VSpace::NONE);
58 tmp.spacing.set(Spacing::Default);
59 tmp.align = LYX_ALIGN_LAYOUT;
62 tmp.labelstring.erase();
63 tmp.labelwidthstring.erase();
64 tmp.start_of_appendix = false;
69 ParagraphParameters::depth_type ParagraphParameters::depth() const
75 bool ParagraphParameters::sameLayout(ParagraphParameters const & pp) const
77 return param->align == pp.param->align &&
78 param->added_space_bottom == pp.param->added_space_bottom &&
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 LyXAlignment ParagraphParameters::align() const
155 void ParagraphParameters::align(LyXAlignment la)
157 ParameterStruct tmp(*param);
159 set_from_struct(tmp);
163 void ParagraphParameters::depth(depth_type d)
165 ParameterStruct tmp(*param);
167 set_from_struct(tmp);
171 bool ParagraphParameters::startOfAppendix() const
173 return param->start_of_appendix;
177 void ParagraphParameters::startOfAppendix(bool soa)
179 ParameterStruct tmp(*param);
180 tmp.start_of_appendix = soa;
181 set_from_struct(tmp);
185 bool ParagraphParameters::appendix() const
187 return param->appendix;
191 void ParagraphParameters::appendix(bool a)
193 ParameterStruct tmp(*param);
195 set_from_struct(tmp);
199 string const & ParagraphParameters::labelString() const
201 return param->labelstring;
205 void ParagraphParameters::labelString(string const & ls)
207 ParameterStruct tmp(*param);
208 tmp.labelstring = ls;
209 set_from_struct(tmp);
213 string const & ParagraphParameters::labelWidthString() const
215 return param->labelwidthstring;
219 void ParagraphParameters::labelWidthString(string const & lws)
221 ParameterStruct tmp(*param);
222 tmp.labelwidthstring = lws;
223 set_from_struct(tmp);
227 LyXLength const & ParagraphParameters::leftIndent() const
229 return param->leftindent;
233 void ParagraphParameters::leftIndent(LyXLength const & li)
235 ParameterStruct tmp(*param);
237 set_from_struct(tmp);
241 void ParagraphParameters::read(LyXLex & lex)
245 string const token = lex.getString();
250 if (token[0] != '\\') {
251 lex.pushToken(token);
255 if (token == "\\noindent") {
257 } else if (token == "\\leftindent") {
259 LyXLength value(lex.getString());
261 } else if (token == "\\fill_top") {
262 spaceTop(VSpace(VSpace::VFILL));
263 } else if (token == "\\fill_bottom") {
264 spaceBottom(VSpace(VSpace::VFILL));
265 } else if (token == "\\start_of_appendix") {
266 startOfAppendix(true);
267 } else if (token == "\\paragraph_spacing") {
269 string const tmp = rtrim(lex.getString());
270 if (tmp == "single") {
271 spacing(Spacing(Spacing::Single));
272 } else if (tmp == "onehalf") {
273 spacing(Spacing(Spacing::Onehalf));
274 } else if (tmp == "double") {
275 spacing(Spacing(Spacing::Double));
276 } else if (tmp == "other") {
278 spacing(Spacing(Spacing::Other,
281 lex.printError("Unknown spacing token: '$$Token'");
283 } else if (token == "\\align") {
284 int tmpret = lex.findToken(string_align);
287 align(LyXAlignment(1 << tmpret));
288 } else if (token == "\\added_space_top") {
290 VSpace value = VSpace(lex.getString());
291 // only add the length when value > 0 or
293 if ((value.length().len().value() != 0) ||
295 (value.kind() != VSpace::LENGTH))
297 } else if (token == "\\added_space_bottom") {
299 VSpace value = VSpace(lex.getString());
300 // only add the length when value > 0 or
302 if ((value.length().len().value() != 0) ||
304 (value.kind() != VSpace::LENGTH))
306 } else if (token == "\\labelwidthstring") {
308 labelWidthString(lex.getString());
310 lex.pushToken(token);
317 void ParagraphParameters::write(ostream & os) const
319 // Maybe some vertical spaces.
320 if (spaceTop().kind() != VSpace::NONE)
321 os << "\\added_space_top "
322 << spaceTop().asLyXCommand() << ' ';
323 if (spaceBottom().kind() != VSpace::NONE)
324 os << "\\added_space_bottom "
325 << spaceBottom().asLyXCommand() << ' ';
327 // Maybe the paragraph has special spacing
328 spacing().writeFile(os, true);
330 // The labelwidth string used in lists.
331 if (!labelWidthString().empty())
332 os << "\\labelwidthstring "
333 << labelWidthString() << '\n';
335 // Start of appendix?
336 if (startOfAppendix())
337 os << "\\start_of_appendix ";
343 // Do we have a manual left indent?
344 if (!leftIndent().zero())
345 os << "\\leftindent " << leftIndent().asString()
349 if (align() != LYX_ALIGN_LAYOUT) {
352 case LYX_ALIGN_LEFT: h = 1; break;
353 case LYX_ALIGN_RIGHT: h = 2; break;
354 case LYX_ALIGN_CENTER: h = 3; break;
355 default: h = 0; break;
357 os << "\\align " << string_align[h] << ' ';
363 void setParagraphParams(BufferView & bv, string const & data)
365 istringstream is(data);
369 ParagraphParameters params;
372 LyXText * text = bv.getLyXText();
375 params.spaceBottom(),
378 params.labelWidthString(),
382 bv.owner()->message(_("Paragraph layout set"));
386 void params2string(Paragraph const & par, string & data)
389 ParagraphParameters params = par.params();
391 // This needs to be done separately
392 params.labelWidthString(par.getLabelWidthString());
395 LyXLayout_ptr const & layout = par.layout();
396 if (params.align() == LYX_ALIGN_LAYOUT)
397 params.align(layout->align);
402 // Is alignment possible
403 os << '\n' << "\\alignpossible " << layout->alignpossible << '\n';
405 /// set default alignment
406 os << "\\aligndefault " << layout->align << '\n';
408 /// is paragraph in inset
409 os << "\\ininset " << (par.inInset()?1:0) << '\n';