]> git.lyx.org Git - lyx.git/blob - src/tex2lyx/context.C
* funcrequest.C: initialize button_ member
[lyx.git] / src / tex2lyx / context.C
1 /** A small helper function
2     \author Jean-Marc Lasgouttes (2003)
3  */
4
5 #include <iostream>
6
7 #include "context.h"
8
9 using std::ostream;
10 using std::endl;
11
12 namespace {
13
14 void begin_layout(ostream & os, LyXLayout_ptr layout)
15 {
16         os << "\n\\begin_layout " << layout->name() << "\n\n";
17 }
18
19
20 void end_layout(ostream & os)
21 {
22         os << "\n\\end_layout\n";
23 }
24
25
26 void begin_deeper(ostream & os)
27 {
28         os << "\n\\begin_deeper \n";
29 }
30
31
32 void end_deeper(ostream & os)
33 {
34         os << "\n\\end_deeper \n";
35 }
36         
37 }
38
39 Context::Context(bool need_layout_,
40                  LyXTextClass const & textclass_,
41                  LyXLayout_ptr layout_, LyXLayout_ptr parent_layout_)
42         : need_layout(need_layout_),
43           need_end_layout(false), need_end_deeper(false),
44           has_item(false), deeper_paragraph(false),
45           textclass(textclass_),
46           layout(layout_), parent_layout(parent_layout_)
47 {
48         if (!layout.get())
49                 layout = textclass.defaultLayout();
50         if (!parent_layout.get())
51                 parent_layout = textclass.defaultLayout();
52 }
53
54
55 void Context::check_layout(ostream & os)
56 {
57         if (need_layout) {
58                 check_end_layout(os);
59
60                 // are we in a list-like environment?
61                 if (layout->isEnvironment()
62                     && layout->latextype != LATEX_ENVIRONMENT) {
63                         if (has_item) {
64                                 if (deeper_paragraph) {
65                                         end_deeper(os);
66                                         deeper_paragraph = false;
67                                 }
68                                 begin_layout(os, layout);
69                                 has_item = false;
70                                 need_layout=false;
71                                 need_end_layout = true;
72                         } else {
73                                 // a standard paragraph in an
74                                 // enumeration. We have to recognize
75                                 // that this may require a begin_deeper.
76                                 if (!deeper_paragraph)
77                                         begin_deeper(os);
78                                 begin_layout(os, textclass.defaultLayout());
79                                 need_layout=false;
80                                 need_end_layout = true;
81                                 deeper_paragraph = true;
82                         }
83                 } else {
84                         begin_layout(os, layout);
85                         need_layout=false;
86                         need_end_layout = true;
87                         if (!extra_stuff.empty()) {
88                                 os << extra_stuff;
89                                 extra_stuff.erase();
90                         }
91                 }
92         }
93 }
94
95
96 void Context::check_end_layout(ostream & os) 
97 {
98         if (need_end_layout) {          
99                 end_layout(os);
100                 need_end_layout = false;
101         }
102 }
103
104
105 void Context::check_deeper(ostream & os)
106 {
107         if (parent_layout->isEnvironment()) {
108                 if (need_end_deeper) {
109                         // no need to have \end_deeper \begin_deeper
110                         need_end_deeper = false;
111                 } else {
112                         begin_deeper(os);
113                         need_end_deeper = true;
114                 }
115         } else
116                 check_end_deeper(os);
117 }
118
119
120 void Context::check_end_deeper(ostream & os) 
121 {
122         if (need_end_deeper) {
123                 end_deeper(os);
124                 need_end_deeper = false;
125         }
126         if (deeper_paragraph) {
127                 end_deeper(os);
128                 deeper_paragraph = false;
129         }
130 }
131
132
133 void Context::dump(ostream & os, string const & desc) const
134 {
135         os << "\n" << desc <<" [";
136         if (need_layout)
137                 os << "need_layout ";
138         if (need_end_layout)
139                 os << "need_end_layout ";
140         if (!extra_stuff.empty())
141                 os << "extrastuff=[" << extra_stuff << "] ";
142         os << "layout=" << layout->name();
143         os << " parent_layout=" << parent_layout->name() << "]" << endl;
144 }