]> git.lyx.org Git - features.git/blob - src/insets/InsetERT.cpp
streamline InsetERT
[features.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 "MetricsInfo.h"
28 #include "ParagraphParameters.h"
29 #include "Paragraph.h"
30 #include "TextClass.h"
31
32 #include "frontends/alert.h"
33 #include "frontends/Application.h"
34
35 #include "support/debug.h"
36 #include "support/gettext.h"
37 #include "support/lstrings.h"
38
39 #include <sstream>
40
41 using namespace std;
42 using namespace lyx::support;
43
44 namespace lyx {
45
46 InsetERT::InsetERT(Buffer const & buf, CollapseStatus status)
47         : InsetCollapsable(buf)
48 {
49         status_ = status;
50 }
51
52
53 InsetERT::~InsetERT()
54 {
55         hideDialogs("ert", this);
56 }
57
58
59 void InsetERT::write(ostream & os) const
60 {
61         os << "ERT" << "\n";
62         InsetCollapsable::write(os);
63 }
64
65
66 docstring InsetERT::editMessage() const
67 {
68         return _("Opened ERT Inset");
69 }
70
71
72 int InsetERT::plaintext(odocstream &, OutputParams const &) const
73 {
74         return 0; // do not output TeX code
75 }
76
77
78 int InsetERT::docbook(odocstream & os, OutputParams const &) const
79 {
80         // FIXME can we do the same thing here as for LaTeX?
81         ParagraphList::const_iterator par = paragraphs().begin();
82         ParagraphList::const_iterator end = paragraphs().end();
83
84         int lines = 0;
85         while (par != end) {
86                 pos_type siz = par->size();
87                 for (pos_type i = 0; i < siz; ++i)
88                         os.put(par->getChar(i));
89                 ++par;
90                 if (par != end) {
91                         os << "\n";
92                         ++lines;
93                 }
94         }
95
96         return lines;
97 }
98
99
100 void InsetERT::doDispatch(Cursor & cur, FuncRequest & cmd)
101 {
102         switch (cmd.action) {
103         case LFUN_INSET_MODIFY: {
104                 setStatus(cur, string2params(to_utf8(cmd.argument())));
105                 break;
106         }
107         default:
108                 InsetCollapsable::doDispatch(cur, cmd);
109                 break;
110         }
111 }
112
113
114 bool InsetERT::getStatus(Cursor & cur, FuncRequest const & cmd,
115         FuncStatus & status) const
116 {
117         switch (cmd.action) {
118         case LFUN_INSET_MODIFY:
119                 status.setEnabled(true);
120                 return true;
121                 
122         default:
123                 return InsetCollapsable::getStatus(cur, cmd, status);
124         }
125 }
126
127
128 docstring const InsetERT::buttonLabel(BufferView const & bv) const
129 {
130         if (decoration() == InsetLayout::CLASSIC)
131                 return isOpen(bv) ? _("ERT") : getNewLabel(_("ERT"));
132         else
133                 return getNewLabel(_("ERT"));
134 }
135
136
137 bool InsetERT::insetAllowed(InsetCode /* code */) const
138 {
139         return false;
140 }
141
142
143 bool InsetERT::showInsetDialog(BufferView * bv) const
144 {
145         bv->showDialog("ert", params2string(status(*bv)), 
146                 const_cast<InsetERT *>(this));
147         return true;
148 }
149
150
151 InsetCollapsable::CollapseStatus InsetERT::string2params(string const & in)
152 {
153         if (in.empty())
154                 return Collapsed;
155         istringstream data(in);
156         Lexer lex;
157         lex.setStream(data);
158         lex.setContext("InsetERT::string2params");
159         lex >> "ert";
160         int s;
161         lex >> s;
162         return static_cast<CollapseStatus>(s);
163 }
164
165
166 string InsetERT::params2string(CollapseStatus status)
167 {
168         ostringstream data;
169         data << "ert" << ' ' << status;
170         return data.str();
171 }
172
173
174 docstring InsetERT::xhtml(odocstream &, OutputParams const &) const
175 {
176         return docstring();
177 }
178
179 } // namespace lyx