]> git.lyx.org Git - lyx.git/blob - src/Spacing.C
A few tweaks (fix new file name, make sstream.h work for gcc 2.8.1)
[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-2000 The LyX Team.
8  *
9  * ====================================================== */
10
11 #include <config.h>
12
13 #ifdef __GNUG__
14 #pragma implementation
15 #endif
16
17 #if 0
18 #ifdef HAVE_SSTREAM
19 #include <sstream>
20 using std::istringstream;
21 using std::ostringstream;
22 #else
23 #include <strstream>
24 #endif
25 #else
26 #include "Lsstream.h"
27 #endif
28
29 #include "Spacing.h"
30 #include "LString.h"
31
32 using std::ios;
33 using std::ostream;
34
35 /// how can I put this inside of Spacing (class)
36 static
37 char const * spacing_string[] = {"single", "onehalf", "double", "other"};
38
39
40 float Spacing::getValue() const 
41 {
42         switch(space) {
43         case Default: // nothing special should happen with this...
44         case Single: return 1.0;
45         case Onehalf: return 1.25;
46         case Double: return 1.667;
47         case Other: return value;
48         }
49         return 1.0;
50 }
51
52
53 void Spacing::set(Spacing::Space sp, float val)
54 {
55         space = sp;
56         if (sp == Other) {
57                 switch(int(val * 1000 + 0.5)) {
58                 case 1000: space = Single; break;
59                 case 1250: space = Onehalf; break;
60                 case 1667: space = Double; break;
61                 default: value = val; break;
62                 }
63         }
64 }
65
66
67 void Spacing::set(Spacing::Space sp, string const & val)
68 {
69         float fval;
70 //#ifdef HAVE_SSTREAM
71         istringstream istr(val);
72 //#else
73 //      istrstream istr(val.c_str());
74 //#endif
75         istr >> fval;
76         set(sp, fval);
77 }
78
79
80 void Spacing::writeFile(ostream & os, bool para) const
81 {
82         if (space == Default) return;
83         
84         string cmd = para ? "\\paragraph_spacing " : "\\spacing ";
85         
86         if (getSpace() == Spacing::Other) {
87                 os.setf(ios::showpoint|ios::fixed);
88                 os.precision(2);
89                 os << cmd << spacing_string[getSpace()]
90                    << " " << getValue() << " \n";
91         } else {
92                 os << cmd << spacing_string[getSpace()] << " \n";
93         }       
94 }
95
96
97 string const Spacing::writeEnvirBegin() const
98 {
99         switch(space) {
100         case Default: break; // do nothing
101         case Single:
102                 return "\\begin{singlespace}";
103         case Onehalf:
104                 return "\\begin{onehalfspace}";
105         case Double:
106                 return "\\begin{doublespace}";
107         case Other:
108 //#ifdef HAVE_SSTREAM
109         {
110                 ostringstream ost;
111                 ost << "\\begin{spacing}{"
112                     << getValue() << "}";
113                 return ost.str().c_str();
114         }
115 //#else
116 //      {
117 //              char tmp[512];
118 //              ostrstream ost(tmp, 512);
119 //              ost << "\\begin{spacing}{"
120 //                  << getValue() << "}\0";
121 //              return ost.str();
122 //      }
123 //#endif
124         }
125         return string();
126 }
127
128
129 string const Spacing::writeEnvirEnd() const
130 {
131         switch(space) {
132         case Default: break; // do nothing
133         case Single:
134                 return "\\end{singlespace}";
135         case Onehalf:
136                 return "\\end{onehalfspace}";
137         case Double:
138                 return "\\end{doublespace}";
139         case Other:
140                 return "\\end{spacing}";
141         }
142         return string();
143 }