]> git.lyx.org Git - lyx.git/blob - src/Spacing.C
Simplify the mechanics of generating the 'inactive' pixmap.
[lyx.git] / src / Spacing.C
1 /**
2  * \file Spacing.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Lars Gullik Bjønnes
7  * \author Jean-Marc Lasgouttes
8  *
9  * Full author contact details are available in file CREDITS.
10  */
11
12 #include <config.h>
13 #include <string>
14
15 #include "Spacing.h"
16
17 #include "support/std_sstream.h"
18
19 using std::ios;
20 using std::istringstream;
21 using std::ostream;
22 using std::ostringstream;
23 using std::string;
24
25
26 string const Spacing::spacing_string[]
27         = {"single", "onehalf", "double", "other"};
28
29 float Spacing::getValue() const
30 {
31         switch (space) {
32         case Default: // nothing special should happen with this...
33         case Single: return 1.0;
34         case Onehalf: return 1.25;
35         case Double: return 1.667;
36         case Other: return value;
37         }
38         return 1.0;
39 }
40
41
42 void Spacing::set(Spacing::Space sp, float val)
43 {
44         space = sp;
45         if (sp == Other) {
46                 switch (int(val * 1000 + 0.5)) {
47                 case 1000: space = Single; break;
48                 case 1250: space = Onehalf; break;
49                 case 1667: space = Double; break;
50                 default: value = val; break;
51                 }
52         }
53 }
54
55
56 void Spacing::set(Spacing::Space sp, string const & val)
57 {
58         float fval = 0.0;
59         istringstream istr(val.c_str());
60         istr >> fval;
61         set(sp, fval);
62 }
63
64
65 void Spacing::writeFile(ostream & os, bool para) const
66 {
67         if (space == Default) return;
68
69         string cmd = para ? "\\paragraph_spacing " : "\\spacing ";
70
71         if (getSpace() == Spacing::Other) {
72                 os.setf(ios::showpoint|ios::fixed);
73                 os.precision(2);
74                 os << cmd << spacing_string[getSpace()]
75                    << ' ' << getValue() << " \n";
76         } else {
77                 os << cmd << spacing_string[getSpace()] << " \n";
78         }
79 }
80
81
82 string const Spacing::writeEnvirBegin() const
83 {
84         switch (space) {
85         case Default: break; // do nothing
86         case Single:
87                 return "\\begin{singlespace}";
88         case Onehalf:
89                 return "\\begin{onehalfspace}";
90         case Double:
91                 return "\\begin{doublespace}";
92         case Other:
93         {
94                 ostringstream ost;
95                 ost << "\\begin{spacing}{"
96                     << getValue() << '}';
97                 return ost.str();
98         }
99         }
100         return string();
101 }
102
103
104 string const Spacing::writeEnvirEnd() const
105 {
106         switch (space) {
107         case Default: break; // do nothing
108         case Single:
109                 return "\\end{singlespace}";
110         case Onehalf:
111                 return "\\end{onehalfspace}";
112         case Double:
113                 return "\\end{doublespace}";
114         case Other:
115                 return "\\end{spacing}";
116         }
117         return string();
118 }