]> git.lyx.org Git - lyx.git/blob - src/insets/InsetERT.cpp
Improve handling of top and bottom margin
[lyx.git] / src / insets / InsetERT.cpp
1 /**
2  * \file InsetERT.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Jürgen Vigna
7  * \author Lars Gullik Bjønnes
8  *
9  * Full author contact details are available in file CREDITS.
10  */
11
12 #include <config.h>
13
14 #include "InsetERT.h"
15
16 #include "Buffer.h"
17 #include "BufferParams.h"
18 #include "BufferView.h"
19 #include "Cursor.h"
20 #include "DispatchResult.h"
21 #include "FuncRequest.h"
22 #include "FuncStatus.h"
23 #include "Language.h"
24 #include "Layout.h"
25 #include "Lexer.h"
26 #include "LyXAction.h"
27 #include "OutputParams.h"
28 #include "xml.h"
29 #include "ParagraphParameters.h"
30 #include "Paragraph.h"
31
32 #include "support/docstream.h"
33 #include "support/gettext.h"
34 #include "support/lstrings.h"
35 #include "support/TempFile.h"
36
37 #include <sstream>
38
39 using namespace std;
40 using namespace lyx::support;
41
42 namespace lyx {
43
44 InsetERT::InsetERT(Buffer * buf, CollapseStatus status)
45         : InsetCollapsible(buf)
46 {
47         status_ = status;
48 }
49
50
51 InsetERT::InsetERT(InsetERT const & old)
52         : InsetCollapsible(old)
53 {}
54
55
56 void InsetERT::write(ostream & os) const
57 {
58         os << "ERT" << "\n";
59         InsetCollapsible::write(os);
60 }
61
62
63 int InsetERT::plaintext(odocstringstream & os,
64         OutputParams const & rp, size_t max_length) const
65 {
66         if (!rp.inIndexEntry)
67                 // do not output TeX code
68                 return 0;
69
70         ParagraphList::const_iterator par = paragraphs().begin();
71         ParagraphList::const_iterator end = paragraphs().end();
72
73         while (par != end && os.str().size() <= max_length) {
74                 pos_type siz = par->size();
75                 for (pos_type i = 0; i < siz; ++i) {
76                         char_type const c = par->getChar(i);
77                         // output the active characters
78                         switch (c) {
79                         case '|':
80                         case '!':
81                         case '@':
82                                 os.put(c);
83                                 break;
84                         default:
85                                 break;
86                         }
87                 }
88                 ++par;
89         }
90         return 0;
91 }
92
93
94 void InsetERT::docbook(XMLStream & xs, OutputParams const &) const
95 {
96         // FIXME can we do the same thing here as for LaTeX?
97         ParagraphList::const_iterator par = paragraphs().begin();
98         ParagraphList::const_iterator end = paragraphs().end();
99
100         xs << XMLStream::ESCAPE_NONE << "<!-- ";
101         while (par != end) {
102                 pos_type siz = par->size();
103                 for (pos_type i = 0; i < siz; ++i) {
104                         xs << par->getChar(i);
105                 }
106                 ++par;
107                 if (par != end) {
108                         xs << "\n";
109                 }
110         }
111         xs << XMLStream::ESCAPE_NONE << " -->";
112 }
113
114
115 void InsetERT::doDispatch(Cursor & cur, FuncRequest & cmd)
116 {
117         switch (cmd.action()) {
118         case LFUN_INSET_MODIFY:
119                 if (cmd.getArg(0) == "ert") {
120                         cur.recordUndoInset(this);
121                         setStatus(cur, string2params(to_utf8(cmd.argument())));
122                         break;
123                 }
124                 //fall-through
125         default:
126                 InsetCollapsible::doDispatch(cur, cmd);
127                 break;
128         }
129
130 }
131
132
133 bool InsetERT::getStatus(Cursor & cur, FuncRequest const & cmd,
134         FuncStatus & status) const
135 {
136         switch (cmd.action()) {
137         case LFUN_INSET_INSERT:
138                 status.setEnabled(false);
139                 return true;
140         case LFUN_INSET_MODIFY:
141                 if (cmd.getArg(0) == "ert") {
142                         status.setEnabled(true);
143                         return true;
144                 }
145                 //fall through
146
147         default:
148                 return InsetCollapsible::getStatus(cur, cmd, status);
149         }
150 }
151
152
153
154 docstring const InsetERT::buttonLabel(BufferView const & bv) const
155 {
156         if (decoration() == InsetLayout::CLASSIC)
157                 return isOpen(bv) ? _("ERT") : getNewLabel(_("ERT"));
158         else
159                 return getNewLabel(_("ERT"));
160 }
161
162
163 InsetCollapsible::CollapseStatus InsetERT::string2params(string const & in)
164 {
165         if (in.empty())
166                 return Collapsed;
167         istringstream data(in);
168         Lexer lex;
169         lex.setStream(data);
170         lex.setContext("InsetERT::string2params");
171         lex >> "ert";
172         int s;
173         lex >> s;
174         return static_cast<CollapseStatus>(s);
175 }
176
177
178 string InsetERT::params2string(CollapseStatus status)
179 {
180         ostringstream data;
181         data << "ert" << ' ' << status;
182         return data.str();
183 }
184
185
186 docstring InsetERT::xhtml(XMLStream &, OutputParams const &) const
187 {
188         return docstring();
189 }
190
191 } // namespace lyx