]> git.lyx.org Git - lyx.git/blob - src/tex2lyx/context.C
Continue to improve GtkLengthEntry
[lyx.git] / src / tex2lyx / context.C
1 /**
2  * \file context.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Jean-Marc Lasgouttes
7  *
8  * Full author contact details are available in file CREDITS.
9  */
10
11 #include <config.h>
12
13 #include <iostream>
14
15 #include "support/lstrings.h"
16 #include "context.h"
17
18 using std::ostream;
19 using std::endl;
20 using std::string;
21
22
23 namespace {
24
25 void begin_layout(ostream & os, LyXLayout_ptr layout, Font const & font,
26                   Font const & normalfont)
27 {
28         os << "\n\\begin_layout " << layout->name() << "\n";
29         // FIXME: This is not enough for things like
30         // \\Huge par1 \\par par2
31         output_font_change(os, normalfont, font);
32 }
33
34
35 void end_layout(ostream & os)
36 {
37         os << "\n\\end_layout\n";
38 }
39
40
41 void begin_deeper(ostream & os)
42 {
43         os << "\n\\begin_deeper";
44 }
45
46
47 void end_deeper(ostream & os)
48 {
49         os << "\n\\end_deeper";
50 }
51
52 }
53
54
55 bool operator==(Font const & f1, Font const & f2)
56 {
57         return
58                 f1.size == f2.size &&
59                 f1.family == f2.family &&
60                 f1.series == f2.series &&
61                 f1.shape == f2.shape;
62 }
63
64
65 void output_font_change(ostream & os, Font const & oldfont,
66                         Font const & newfont)
67 {
68         if (oldfont.family != newfont.family)
69                 os << "\n\\family " << newfont.family << '\n';
70         if (oldfont.series != newfont.series)
71                 os << "\n\\series " << newfont.series << '\n';
72         if (oldfont.shape != newfont.shape)
73                 os << "\n\\shape " << newfont.shape << '\n';
74         if (oldfont.size != newfont.size)
75                 os << "\n\\size " << newfont.size << '\n';
76 }
77
78
79 Font Context::normalfont;
80
81
82 Context::Context(bool need_layout_,
83                  LyXTextClass const & textclass_,
84                  LyXLayout_ptr layout_, LyXLayout_ptr parent_layout_,
85                  Font font_)
86         : need_layout(need_layout_),
87           need_end_layout(false), need_end_deeper(false),
88           has_item(false), deeper_paragraph(false),
89           new_layout_allowed(true), textclass(textclass_),
90           layout(layout_), parent_layout(parent_layout_),
91           font(font_)
92 {
93         if (!layout.get())
94                 layout = textclass.defaultLayout();
95         if (!parent_layout.get())
96                 parent_layout = textclass.defaultLayout();
97 }
98
99
100 Context::~Context()
101 {
102         if (!extra_stuff.empty())
103                 std::cerr << "Bug: Ignoring extra stuff '" << extra_stuff
104                           << '\'' << std::endl;
105 }
106
107
108 void Context::check_layout(ostream & os)
109 {
110         if (need_layout) {
111                 check_end_layout(os);
112
113                 // are we in a list-like environment?
114                 if (layout->isEnvironment()
115                     && layout->latextype != LATEX_ENVIRONMENT) {
116                         // A list-like environment
117                         if (has_item) {
118                                 // a new item. If we had a standard
119                                 // paragraph before, we have to end it.
120                                 if (deeper_paragraph) {
121                                         end_deeper(os);
122                                         deeper_paragraph = false;
123                                 }
124                                 begin_layout(os, layout, font, normalfont);
125                                 has_item = false;
126                         } else {
127                                 // a standard paragraph in an
128                                 // enumeration. We have to recognize
129                                 // that this may require a begin_deeper.
130                                 if (!deeper_paragraph)
131                                         begin_deeper(os);
132                                 begin_layout(os, textclass.defaultLayout(),
133                                              font, normalfont);
134                                 deeper_paragraph = true;
135                         }
136                         need_layout = false;
137                 } else {
138                         // No list-like environment
139                         begin_layout(os, layout, font, normalfont);
140                         need_layout=false;
141                 }
142                 need_end_layout = true;
143                 if (!extra_stuff.empty()) {
144                         os << extra_stuff;
145                         extra_stuff.erase();
146                 }
147                 os << "\n";
148         }
149 }
150
151
152 void Context::check_end_layout(ostream & os)
153 {
154         if (need_end_layout) {
155                 end_layout(os);
156                 need_end_layout = false;
157         }
158 }
159
160
161 void Context::check_deeper(ostream & os)
162 {
163         if (parent_layout->isEnvironment()) {
164                 // We start a nested environment.
165                 // We need to increase the depth.
166                 if (need_end_deeper) {
167                         // no need to have \end_deeper \begin_deeper
168                         need_end_deeper = false;
169                 } else {
170                         begin_deeper(os);
171                         need_end_deeper = true;
172                 }
173         } else
174                 check_end_deeper(os);
175 }
176
177
178 void Context::check_end_deeper(ostream & os)
179 {
180         if (need_end_deeper) {
181                 end_deeper(os);
182                 need_end_deeper = false;
183         }
184         if (deeper_paragraph) {
185                 end_deeper(os);
186                 deeper_paragraph = false;
187         }
188 }
189
190
191 void Context::set_item()
192 {
193         need_layout = true;
194         has_item = true;
195 }
196
197
198 void Context::new_paragraph(ostream & os)
199 {
200         check_end_layout(os);
201         need_layout = true;
202 }
203
204
205 void Context::add_extra_stuff(std::string const & stuff)
206 {
207         if (!lyx::support::contains(extra_stuff, stuff))
208                 extra_stuff += stuff;
209 }
210
211
212 void Context::dump(ostream & os, string const & desc) const
213 {
214         os << "\n" << desc <<" [";
215         if (need_layout)
216                 os << "need_layout ";
217         if (need_end_layout)
218                 os << "need_end_layout ";
219         if (need_end_deeper)
220                 os << "need_end_deeper ";
221         if (has_item)
222                 os << "has_item ";
223         if (deeper_paragraph)
224                 os << "deeper_paragraph ";
225         if (new_layout_allowed)
226                 os << "new_layout_allowed ";
227         if (!extra_stuff.empty())
228                 os << "extrastuff=[" << extra_stuff << "] ";
229         os << "textclass=" << textclass.name()
230            << " layout=" << layout->name()
231            << " parent_layout=" << parent_layout->name() << "] font=["
232            << font.size << ' ' << font.family << ' ' << font.series << ' '
233            << font.shape << ']' << endl;
234 }