]> git.lyx.org Git - lyx.git/blob - src/tex2lyx/context.C
Georg's minipage patch
[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)
26 {
27         os << "\n\\begin_layout " << layout->name() << "\n";
28 }
29
30
31 void end_layout(ostream & os)
32 {
33         os << "\n\\end_layout\n";
34 }
35
36
37 void begin_deeper(ostream & os)
38 {
39         os << "\n\\begin_deeper \n";
40 }
41
42
43 void end_deeper(ostream & os)
44 {
45         os << "\n\\end_deeper \n";
46 }
47
48 }
49
50 Context::Context(bool need_layout_,
51                  LyXTextClass const & textclass_,
52                  LyXLayout_ptr layout_, LyXLayout_ptr parent_layout_)
53         : need_layout(need_layout_),
54           need_end_layout(false), need_end_deeper(false),
55           has_item(false), deeper_paragraph(false),
56           textclass(textclass_),
57           layout(layout_), parent_layout(parent_layout_)
58 {
59         if (!layout.get())
60                 layout = textclass.defaultLayout();
61         if (!parent_layout.get())
62                 parent_layout = textclass.defaultLayout();
63 }
64
65
66 void Context::check_layout(ostream & os)
67 {
68         if (need_layout) {
69                 check_end_layout(os);
70
71                 // are we in a list-like environment?
72                 if (layout->isEnvironment()
73                     && layout->latextype != LATEX_ENVIRONMENT) {
74                         if (has_item) {
75                                 if (deeper_paragraph) {
76                                         end_deeper(os);
77                                         deeper_paragraph = false;
78                                 }
79                                 begin_layout(os, layout);
80                                 has_item = false;
81                                 need_layout=false;
82                                 need_end_layout = true;
83                         } else {
84                                 // a standard paragraph in an
85                                 // enumeration. We have to recognize
86                                 // that this may require a begin_deeper.
87                                 if (!deeper_paragraph)
88                                         begin_deeper(os);
89                                 begin_layout(os, textclass.defaultLayout());
90                                 need_layout=false;
91                                 need_end_layout = true;
92                                 deeper_paragraph = true;
93                         }
94                 } else {
95                         begin_layout(os, layout);
96                         need_layout=false;
97                         need_end_layout = true;
98                 }
99                 if (!extra_stuff.empty()) {
100                         os << extra_stuff;
101                         extra_stuff.erase();
102                 }
103                 os << "\n";
104         }
105 }
106
107
108 void Context::check_end_layout(ostream & os)
109 {
110         if (need_end_layout) {
111                 end_layout(os);
112                 need_end_layout = false;
113         }
114 }
115
116
117 void Context::check_deeper(ostream & os)
118 {
119         if (parent_layout->isEnvironment()) {
120                 if (need_end_deeper) {
121                         // no need to have \end_deeper \begin_deeper
122                         need_end_deeper = false;
123                 } else {
124                         begin_deeper(os);
125                         need_end_deeper = true;
126                 }
127         } else
128                 check_end_deeper(os);
129 }
130
131
132 void Context::check_end_deeper(ostream & os)
133 {
134         if (need_end_deeper) {
135                 end_deeper(os);
136                 need_end_deeper = false;
137         }
138         if (deeper_paragraph) {
139                 end_deeper(os);
140                 deeper_paragraph = false;
141         }
142 }
143
144
145 void Context::set_item()
146 {
147         need_layout = true;
148         has_item = true;
149 }
150
151
152 void Context::new_paragraph(ostream & os)
153 {
154         check_end_layout(os);
155         need_layout = true;
156 }
157
158
159 void Context::add_extra_stuff(std::string const & stuff)
160 {
161         if (!lyx::support::contains(extra_stuff, stuff))
162                 extra_stuff += stuff;
163 }
164
165
166 void Context::dump(ostream & os, string const & desc) const
167 {
168         os << "\n" << desc <<" [";
169         if (need_layout)
170                 os << "need_layout ";
171         if (need_end_layout)
172                 os << "need_end_layout ";
173         if (need_end_deeper)
174                 os << "need_end_deeper ";
175         if (has_item)
176                 os << "has_item ";
177         if (deeper_paragraph)
178                 os << "deeper_paragraph ";
179         if (!extra_stuff.empty())
180                 os << "extrastuff=[" << extra_stuff << "] ";
181         os << "layout=" << layout->name();
182         os << " parent_layout=" << parent_layout->name() << "]" << endl;
183 }