]> git.lyx.org Git - lyx.git/blob - src/ParagraphParameters.C
"Inter-word Space"
[lyx.git] / src / ParagraphParameters.C
1 #include <config.h>
2
3 #include "ParagraphParameters.h"
4 #include "ParameterStruct.h"
5 #include "tex-strings.h"
6 #include "lyxlex.h"
7
8 #include "buffer.h"
9 #include "BufferView.h"
10 #include "Lsstream.h"
11 #include "gettext.h"
12 #include "paragraph.h"
13 #include "lyxtext.h"
14 #include "Lsstream.h"
15
16 #include "frontends/LyXView.h"
17
18 #include "support/lstrings.h"
19
20 #include <iostream>
21
22 using std::ostream;
23
24 // Initialize static member var.
25 ShareContainer<ParameterStruct> ParagraphParameters::container;
26
27 ParagraphParameters::ParagraphParameters()
28 {
29         ParameterStruct tmp;
30         set_from_struct(tmp);
31 }
32
33
34 void ParagraphParameters::clear()
35 {
36         ParameterStruct tmp(*param);
37         tmp.line_top = false;
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;
45         tmp.depth = 0;
46         tmp.noindent = false;
47         tmp.labelstring.erase();
48         tmp.labelwidthstring.erase();
49         tmp.start_of_appendix = false;
50         set_from_struct(tmp);
51 }
52
53
54 bool ParagraphParameters::sameLayout(ParagraphParameters const & pp) const
55 {
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 &&
60
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;
67 }
68
69
70 void ParagraphParameters::set_from_struct(ParameterStruct const & ps)
71 {
72         // get new param from container with tmp as template
73         param = container.get(ps);
74 }
75
76
77 VSpace const & ParagraphParameters::spaceTop() const
78 {
79         return param->added_space_top;
80 }
81
82
83 void ParagraphParameters::spaceTop(VSpace const & vs)
84 {
85         ParameterStruct tmp(*param);
86         tmp.added_space_top = vs;
87         set_from_struct(tmp);
88 }
89
90
91 VSpace const & ParagraphParameters::spaceBottom() const
92 {
93         return param->added_space_bottom;
94 }
95
96
97 void ParagraphParameters::spaceBottom(VSpace const & vs)
98 {
99         ParameterStruct tmp(*param);
100         tmp.added_space_bottom = vs;
101         set_from_struct(tmp);
102 }
103
104
105 Spacing const & ParagraphParameters::spacing() const
106 {
107         return param->spacing;
108 }
109
110
111 void ParagraphParameters::spacing(Spacing const & s)
112 {
113         ParameterStruct tmp(*param);
114         tmp.spacing = s;
115         set_from_struct(tmp);
116 }
117
118
119 bool ParagraphParameters::noindent() const
120 {
121         return param->noindent;
122 }
123
124
125 void ParagraphParameters::noindent(bool ni)
126 {
127         ParameterStruct tmp(*param);
128         tmp.noindent = ni;
129         set_from_struct(tmp);
130 }
131
132
133 bool ParagraphParameters::lineTop() const
134 {
135         return param->line_top;
136 }
137
138
139 void ParagraphParameters::lineTop(bool lt)
140 {
141         ParameterStruct tmp(*param);
142         tmp.line_top = lt;
143         set_from_struct(tmp);
144 }
145
146
147 bool ParagraphParameters::lineBottom() const
148 {
149         return param->line_bottom;
150 }
151
152
153 void ParagraphParameters::lineBottom(bool lb)
154 {
155         ParameterStruct tmp(*param);
156         tmp.line_bottom = lb;
157         set_from_struct(tmp);
158 }
159
160
161 bool ParagraphParameters::pagebreakTop() const
162 {
163         return param->pagebreak_top;
164 }
165
166
167 void ParagraphParameters::pagebreakTop(bool pbt)
168 {
169         ParameterStruct tmp(*param);
170         tmp.pagebreak_top = pbt;
171         set_from_struct(tmp);
172 }
173
174
175 bool ParagraphParameters::pagebreakBottom() const
176 {
177         return param->pagebreak_bottom;
178 }
179
180
181 void ParagraphParameters::pagebreakBottom(bool pbb)
182 {
183         ParameterStruct tmp(*param);
184         tmp.pagebreak_bottom = pbb;
185         set_from_struct(tmp);
186 }
187
188
189 LyXAlignment ParagraphParameters::align() const
190 {
191         return param->align;
192 }
193
194
195 void ParagraphParameters::align(LyXAlignment la)
196 {
197         ParameterStruct tmp(*param);
198         tmp.align = la;
199         set_from_struct(tmp);
200 }
201
202
203 void ParagraphParameters::depth(depth_type d)
204 {
205         ParameterStruct tmp(*param);
206         tmp.depth = d;
207         set_from_struct(tmp);
208 }
209
210
211 bool ParagraphParameters::startOfAppendix() const
212 {
213         return param->start_of_appendix;
214 }
215
216
217 void ParagraphParameters::startOfAppendix(bool soa)
218 {
219         ParameterStruct tmp(*param);
220         tmp.start_of_appendix = soa;
221         set_from_struct(tmp);
222 }
223
224
225 bool ParagraphParameters::appendix() const
226 {
227         return param->appendix;
228 }
229
230
231 void ParagraphParameters::appendix(bool a)
232 {
233         ParameterStruct tmp(*param);
234         tmp.appendix = a;
235         set_from_struct(tmp);
236 }
237
238
239 string const & ParagraphParameters::labelString() const
240 {
241         return param->labelstring;
242 }
243
244
245 void ParagraphParameters::labelString(string const & ls)
246 {
247         ParameterStruct tmp(*param);
248         tmp.labelstring = ls;
249         set_from_struct(tmp);
250 }
251
252
253 string const & ParagraphParameters::labelWidthString() const
254 {
255         return param->labelwidthstring;
256 }
257
258
259 void ParagraphParameters::labelWidthString(string const & lws)
260 {
261         ParameterStruct tmp(*param);
262         tmp.labelwidthstring = lws;
263         set_from_struct(tmp);
264 }
265
266
267 LyXLength const & ParagraphParameters::leftIndent() const
268 {
269         return param->leftindent;
270 }
271
272
273 void ParagraphParameters::leftIndent(LyXLength const & li)
274 {
275         ParameterStruct tmp(*param);
276         tmp.leftindent = li;
277         set_from_struct(tmp);
278 }
279
280
281 void ParagraphParameters::read(LyXLex & lex)
282 {
283         while (lex.isOK()) {
284                 lex.nextToken();
285                 string const token = lex.getString();
286
287                 if (token.empty())
288                         continue;
289
290                 if (token[0] != '\\') {
291                         lex.pushToken(token);
292                         break;
293                 }
294
295                 if (token == "\\noindent") {
296                         noindent(true);
297                 } else if (token == "\\leftindent") {
298                         lex.nextToken();
299                         LyXLength value(lex.getString());
300                         leftIndent(value);
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") {
306                         lineTop(true);
307                 } else if (token == "\\line_bottom") {
308                         lineBottom(true);
309                 } else if (token == "\\pagebreak_top") {
310                         pagebreakTop(true);
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") {
316                         lex.next();
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") {
325                                 lex.next();
326                                 spacing(Spacing(Spacing::Other,
327                                                  lex.getFloat()));
328                         } else {
329                                 lex.printError("Unknown spacing token: '$$Token'");
330                         }
331                 } else if (token == "\\align") {
332                         int tmpret = lex.findToken(string_align);
333                         if (tmpret == -1)
334                                 ++tmpret;
335                         align(LyXAlignment(1 << tmpret));
336                 } else if (token == "\\added_space_top") {
337                         lex.nextToken();
338                         VSpace value = VSpace(lex.getString());
339                         // only add the length when value > 0 or
340                         // with option keep
341                         if ((value.length().len().value() != 0) ||
342                             value.keep() ||
343                             (value.kind() != VSpace::LENGTH))
344                                 spaceTop(value);
345                 } else if (token == "\\added_space_bottom") {
346                         lex.nextToken();
347                         VSpace value = VSpace(lex.getString());
348                         // only add the length when value > 0 or
349                         // with option keep
350                         if ((value.length().len().value() != 0) ||
351                            value.keep() ||
352                             (value.kind() != VSpace::LENGTH))
353                                 spaceBottom(value);
354                 } else if (token == "\\labelwidthstring") {
355                         lex.eatLine();
356                         labelWidthString(lex.getString());
357                 } else {
358                         lex.pushToken(token);
359                         break;
360                 }
361         }
362 }
363
364
365 void ParagraphParameters::write(ostream & os) const
366 {
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() << ' ';
374
375         // Maybe the paragraph has special spacing
376         spacing().writeFile(os, true);
377
378         // The labelwidth string used in lists.
379         if (!labelWidthString().empty())
380                 os << "\\labelwidthstring "
381                    << labelWidthString() << '\n';
382
383         // Lines above or below?
384         if (lineTop())
385                 os << "\\line_top ";
386         if (lineBottom())
387                 os << "\\line_bottom ";
388
389         // Pagebreaks above or below?
390         if (pagebreakTop())
391                 os << "\\pagebreak_top ";
392         if (pagebreakBottom())
393                 os << "\\pagebreak_bottom ";
394
395         // Start of appendix?
396         if (startOfAppendix())
397                 os << "\\start_of_appendix ";
398
399         // Noindent?
400         if (noindent())
401                 os << "\\noindent ";
402
403         // Do we have a manual left indent?
404         if (!leftIndent().zero())
405                 os << "\\leftindent " << leftIndent().asString()
406                    << ' ';
407
408         // Alignment?
409         if (align() != LYX_ALIGN_LAYOUT) {
410                 int h = 0;
411                 switch (align()) {
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;
416                 }
417                 os << "\\align " << string_align[h] << ' ';
418         }
419 }
420
421
422 void setParagraphParams(BufferView & bv, string const & data)
423 {
424         istringstream is(STRCONV(data));
425         LyXLex lex(0,0);
426         lex.setStream(is);
427
428         ParagraphParameters params;
429         params.read(lex);
430
431         LyXText * text = bv.getLyXText();
432         text->setParagraph(params.lineTop(),
433                            params.lineBottom(),
434                            params.pagebreakTop(),
435                            params.pagebreakBottom(),
436                            params.spaceTop(),
437                            params.spaceBottom(),
438                            params.spacing(),
439                            params.align(),
440                            params.labelWidthString(),
441                            params.noindent());
442
443         // Actually apply these settings
444         bv.update(text, BufferView::SELECT);
445
446         bv.owner()->message(_("Paragraph layout set"));
447 }
448
449
450 void params2string(Paragraph const & par, string & data)
451 {
452         // A local copy
453         ParagraphParameters params = par.params();
454
455         // This needs to be done separately
456         params.labelWidthString(par.getLabelWidthString());
457
458         // Alignment
459         LyXLayout_ptr const & layout = par.layout();
460         if (params.align() == LYX_ALIGN_LAYOUT)
461                 params.align(layout->align);
462
463         ostringstream os;
464         params.write(os);
465
466         // Is alignment possible
467         os << '\n' << "\\alignpossible " << layout->alignpossible << '\n';
468
469         /// set default alignment
470         os << "\\aligndefault " << layout->align << '\n';
471
472         /// is paragraph in inset
473         os << "\\ininset " << (par.inInset()?1:0) << '\n';
474
475         data = STRCONV(os.str());
476 }