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