]> git.lyx.org Git - lyx.git/blob - src/Spacing.C
Fix paragraph spacing
[lyx.git] / src / Spacing.C
1 /* This file is part of
2  * ====================================================== 
3  * 
4  *           LyX, The Document Processor
5  *        
6  *           Copyright 1995 Matthias Ettrich
7  *           Copyright 1995-2001 The LyX Team.
8  *
9  * ====================================================== */
10
11 #include <config.h>
12
13 #ifdef __GNUG__
14 #pragma implementation
15 #endif
16
17 #include "Lsstream.h"
18 #include "Spacing.h"
19 #include "LString.h"
20
21 using std::ios;
22 using std::ostream;
23
24 /// how can I put this inside of Spacing (class)
25 namespace {
26
27 char const * const spacing_string[] = {"single", "onehalf", "double", "other"};
28
29 } // namespace anon
30
31
32 float Spacing::getValue() const 
33 {
34         switch (space) {
35         case Default: // nothing special should happen with this...
36         case Single: return 1.0;
37         case Onehalf: return 1.25;
38         case Double: return 1.667;
39         case Other: return value;
40         }
41         return 1.0;
42 }
43
44
45 void Spacing::set(Spacing::Space sp, float val)
46 {
47         space = sp;
48         if (sp == Other) {
49                 switch (int(val * 1000 + 0.5)) {
50                 case 1000: space = Single; break;
51                 case 1250: space = Onehalf; break;
52                 case 1667: space = Double; break;
53                 default: value = val; break;
54                 }
55         }
56 }
57
58
59 void Spacing::set(Spacing::Space sp, string const & val)
60 {
61         float fval = 0.0;
62         istringstream istr(val.c_str());
63         istr >> fval;
64         set(sp, fval);
65 }
66
67
68 void Spacing::writeFile(ostream & os, bool para) const
69 {
70         if (space == Default) return;
71         
72         string cmd = para ? "\\paragraph_spacing " : "\\spacing ";
73         
74         if (getSpace() == Spacing::Other) {
75                 os.setf(ios::showpoint|ios::fixed);
76                 os.precision(2);
77                 os << cmd << spacing_string[getSpace()]
78                    << " " << getValue() << " \n";
79         } else {
80                 os << cmd << spacing_string[getSpace()] << " \n";
81         }       
82 }
83
84
85 string const Spacing::writeEnvirBegin() const
86 {
87         switch (space) {
88         case Default: break; // do nothing
89         case Single:
90                 return "\\begin{singlespace}";
91         case Onehalf:
92                 return "\\begin{onehalfspace}";
93         case Double:
94                 return "\\begin{doublespace}";
95         case Other:
96         {
97                 ostringstream ost;
98                 ost << "\\begin{spacing}{"
99                     << getValue() << "}";
100                 return ost.str().c_str();
101         }
102         }
103         return string();
104 }
105
106
107 string const Spacing::writeEnvirEnd() const
108 {
109         switch (space) {
110         case Default: break; // do nothing
111         case Single:
112                 return "\\end{singlespace}";
113         case Onehalf:
114                 return "\\end{onehalfspace}";
115         case Double:
116                 return "\\end{doublespace}";
117         case Other:
118                 return "\\end{spacing}";
119         }
120         return string();
121 }